| 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 | const streamChunksOfRawSource = require("./helpers/streamChunksOfRawSource");
|
|---|
| 10 | const {
|
|---|
| 11 | internString,
|
|---|
| 12 | isDualStringBufferCachingEnabled,
|
|---|
| 13 | } = require("./helpers/stringBufferUtils");
|
|---|
| 14 |
|
|---|
| 15 | /** @typedef {import("./Source").HashLike} HashLike */
|
|---|
| 16 | /** @typedef {import("./Source").MapOptions} MapOptions */
|
|---|
| 17 | /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
|
|---|
| 18 | /** @typedef {import("./Source").SourceValue} SourceValue */
|
|---|
| 19 | /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
|
|---|
| 20 | /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
|
|---|
| 21 | /** @typedef {import("./helpers/streamChunks").OnName} OnName */
|
|---|
| 22 | /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
|
|---|
| 23 | /** @typedef {import("./helpers/streamChunks").Options} Options */
|
|---|
| 24 |
|
|---|
| 25 | class RawSource extends Source {
|
|---|
| 26 | /**
|
|---|
| 27 | * @param {string | Buffer} value value
|
|---|
| 28 | * @param {boolean=} convertToString convert to string
|
|---|
| 29 | */
|
|---|
| 30 | constructor(value, convertToString = false) {
|
|---|
| 31 | super();
|
|---|
| 32 | const isBuffer = Buffer.isBuffer(value);
|
|---|
| 33 | if (isBuffer) {
|
|---|
| 34 | /**
|
|---|
| 35 | * @private
|
|---|
| 36 | * @type {boolean}
|
|---|
| 37 | */
|
|---|
| 38 | this._valueIsBuffer = !convertToString;
|
|---|
| 39 | /**
|
|---|
| 40 | * @private
|
|---|
| 41 | * @type {undefined | string | Buffer}
|
|---|
| 42 | */
|
|---|
| 43 | this._value = convertToString ? undefined : value;
|
|---|
| 44 | /**
|
|---|
| 45 | * @private
|
|---|
| 46 | * @type {undefined | Buffer}
|
|---|
| 47 | */
|
|---|
| 48 | this._valueAsBuffer = value;
|
|---|
| 49 | /**
|
|---|
| 50 | * @private
|
|---|
| 51 | * @type {undefined | string}
|
|---|
| 52 | */
|
|---|
| 53 | this._valueAsString = undefined;
|
|---|
| 54 | } else if (typeof value === "string") {
|
|---|
| 55 | const interned = internString(value);
|
|---|
| 56 | this._valueIsBuffer = false;
|
|---|
| 57 | this._value = interned;
|
|---|
| 58 | this._valueAsBuffer = undefined;
|
|---|
| 59 | this._valueAsString = interned;
|
|---|
| 60 | } else {
|
|---|
| 61 | throw new TypeError("argument 'value' must be either string or Buffer");
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | isBuffer() {
|
|---|
| 66 | return this._valueIsBuffer;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * @returns {SourceValue} source
|
|---|
| 71 | */
|
|---|
| 72 | source() {
|
|---|
| 73 | if (this._value === undefined) {
|
|---|
| 74 | const value =
|
|---|
| 75 | /** @type {Buffer} */
|
|---|
| 76 | (this._valueAsBuffer).toString("utf8");
|
|---|
| 77 | if (isDualStringBufferCachingEnabled()) {
|
|---|
| 78 | this._value = internString(value);
|
|---|
| 79 | }
|
|---|
| 80 | return value;
|
|---|
| 81 | }
|
|---|
| 82 | return this._value;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * @returns {Buffer} buffer
|
|---|
| 87 | */
|
|---|
| 88 | buffer() {
|
|---|
| 89 | if (this._valueAsBuffer === undefined) {
|
|---|
| 90 | const value = Buffer.from(/** @type {string} */ (this._value), "utf8");
|
|---|
| 91 | if (isDualStringBufferCachingEnabled()) {
|
|---|
| 92 | this._valueAsBuffer = value;
|
|---|
| 93 | }
|
|---|
| 94 | return value;
|
|---|
| 95 | }
|
|---|
| 96 | return this._valueAsBuffer;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * @returns {number} size
|
|---|
| 101 | */
|
|---|
| 102 | size() {
|
|---|
| 103 | if (this._cachedSize !== undefined) return this._cachedSize;
|
|---|
| 104 | if (this._valueAsBuffer !== undefined) {
|
|---|
| 105 | return (this._cachedSize = this._valueAsBuffer.length);
|
|---|
| 106 | }
|
|---|
| 107 | return (this._cachedSize = Buffer.byteLength(
|
|---|
| 108 | /** @type {string} */ (this._valueAsString),
|
|---|
| 109 | "utf8",
|
|---|
| 110 | ));
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * @param {MapOptions=} options map options
|
|---|
| 115 | * @returns {RawSourceMap | null} map
|
|---|
| 116 | */
|
|---|
| 117 | // eslint-disable-next-line no-unused-vars
|
|---|
| 118 | map(options) {
|
|---|
| 119 | return null;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * @param {Options} options options
|
|---|
| 124 | * @param {OnChunk} onChunk called for each chunk of code
|
|---|
| 125 | * @param {OnSource} onSource called for each source
|
|---|
| 126 | * @param {OnName} onName called for each name
|
|---|
| 127 | * @returns {GeneratedSourceInfo} generated source info
|
|---|
| 128 | */
|
|---|
| 129 | streamChunks(options, onChunk, onSource, onName) {
|
|---|
| 130 | let strValue = this._valueAsString;
|
|---|
| 131 | if (strValue === undefined) {
|
|---|
| 132 | const value = this.source();
|
|---|
| 133 | strValue = typeof value === "string" ? value : value.toString("utf8");
|
|---|
| 134 | if (isDualStringBufferCachingEnabled()) {
|
|---|
| 135 | this._valueAsString = internString(strValue);
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | return streamChunksOfRawSource(
|
|---|
| 139 | strValue,
|
|---|
| 140 | onChunk,
|
|---|
| 141 | onSource,
|
|---|
| 142 | onName,
|
|---|
| 143 | Boolean(options && options.finalSource),
|
|---|
| 144 | );
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * @param {HashLike} hash hash
|
|---|
| 149 | * @returns {void}
|
|---|
| 150 | */
|
|---|
| 151 | updateHash(hash) {
|
|---|
| 152 | hash.update("RawSource");
|
|---|
| 153 | hash.update(this.buffer());
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | module.exports = RawSource;
|
|---|