| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const Source = require("./Source");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("./Source").HashLike} HashLike */
|
|---|
| 11 | /** @typedef {import("./Source").MapOptions} MapOptions */
|
|---|
| 12 | /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
|
|---|
| 13 | /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
|
|---|
| 14 | /** @typedef {import("./Source").SourceValue} SourceValue */
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * @typedef {object} SourceLike
|
|---|
| 18 | * @property {() => SourceValue} source source
|
|---|
| 19 | * @property {(() => Buffer)=} buffer buffer
|
|---|
| 20 | * @property {(() => Buffer[])=} buffers buffers
|
|---|
| 21 | * @property {(() => number)=} size size
|
|---|
| 22 | * @property {((options?: MapOptions) => RawSourceMap | null)=} map map
|
|---|
| 23 | * @property {((options?: MapOptions) => SourceAndMap)=} sourceAndMap source and map
|
|---|
| 24 | * @property {((hash: HashLike) => void)=} updateHash hash updater
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | class CompatSource extends Source {
|
|---|
| 28 | /**
|
|---|
| 29 | * @param {SourceLike} sourceLike source like
|
|---|
| 30 | * @returns {Source} source
|
|---|
| 31 | */
|
|---|
| 32 | static from(sourceLike) {
|
|---|
| 33 | return sourceLike instanceof Source
|
|---|
| 34 | ? sourceLike
|
|---|
| 35 | : new CompatSource(sourceLike);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * @param {SourceLike} sourceLike source like
|
|---|
| 40 | */
|
|---|
| 41 | constructor(sourceLike) {
|
|---|
| 42 | super();
|
|---|
| 43 | /**
|
|---|
| 44 | * @private
|
|---|
| 45 | * @type {SourceLike}
|
|---|
| 46 | */
|
|---|
| 47 | this._sourceLike = sourceLike;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * @returns {SourceValue} source
|
|---|
| 52 | */
|
|---|
| 53 | source() {
|
|---|
| 54 | return this._sourceLike.source();
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * @returns {Buffer} buffer
|
|---|
| 59 | */
|
|---|
| 60 | buffer() {
|
|---|
| 61 | if (typeof this._sourceLike.buffer === "function") {
|
|---|
| 62 | return this._sourceLike.buffer();
|
|---|
| 63 | }
|
|---|
| 64 | return super.buffer();
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * @returns {Buffer[]} buffers
|
|---|
| 69 | */
|
|---|
| 70 | buffers() {
|
|---|
| 71 | if (typeof this._sourceLike.buffers === "function") {
|
|---|
| 72 | return this._sourceLike.buffers();
|
|---|
| 73 | }
|
|---|
| 74 | return super.buffers();
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * @returns {number} size
|
|---|
| 79 | */
|
|---|
| 80 | size() {
|
|---|
| 81 | if (typeof this._sourceLike.size === "function") {
|
|---|
| 82 | return this._sourceLike.size();
|
|---|
| 83 | }
|
|---|
| 84 | return super.size();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * @param {MapOptions=} options map options
|
|---|
| 89 | * @returns {RawSourceMap | null} map
|
|---|
| 90 | */
|
|---|
| 91 | map(options) {
|
|---|
| 92 | if (typeof this._sourceLike.map === "function") {
|
|---|
| 93 | return this._sourceLike.map(options);
|
|---|
| 94 | }
|
|---|
| 95 | return super.map(options);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * @param {MapOptions=} options map options
|
|---|
| 100 | * @returns {SourceAndMap} source and map
|
|---|
| 101 | */
|
|---|
| 102 | sourceAndMap(options) {
|
|---|
| 103 | if (typeof this._sourceLike.sourceAndMap === "function") {
|
|---|
| 104 | return this._sourceLike.sourceAndMap(options);
|
|---|
| 105 | }
|
|---|
| 106 | return super.sourceAndMap(options);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * @param {HashLike} hash hash
|
|---|
| 111 | * @returns {void}
|
|---|
| 112 | */
|
|---|
| 113 | updateHash(hash) {
|
|---|
| 114 | if (typeof this._sourceLike.updateHash === "function") {
|
|---|
| 115 | return this._sourceLike.updateHash(hash);
|
|---|
| 116 | }
|
|---|
| 117 | if (typeof this._sourceLike.map === "function") {
|
|---|
| 118 | throw new Error(
|
|---|
| 119 | "A Source-like object with a 'map' method must also provide an 'updateHash' method",
|
|---|
| 120 | );
|
|---|
| 121 | }
|
|---|
| 122 | hash.update(this.buffer());
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | module.exports = CompatSource;
|
|---|