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 streamChunksOfRawSource = require("./helpers/streamChunksOfRawSource");
|
---|
9 | const Source = require("./Source");
|
---|
10 |
|
---|
11 | class RawSource extends Source {
|
---|
12 | constructor(value, convertToString = false) {
|
---|
13 | super();
|
---|
14 | const isBuffer = Buffer.isBuffer(value);
|
---|
15 | if (!isBuffer && typeof value !== "string") {
|
---|
16 | throw new TypeError("argument 'value' must be either string of Buffer");
|
---|
17 | }
|
---|
18 | this._valueIsBuffer = !convertToString && isBuffer;
|
---|
19 | this._value = convertToString && isBuffer ? undefined : value;
|
---|
20 | this._valueAsBuffer = isBuffer ? value : undefined;
|
---|
21 | this._valueAsString = isBuffer ? undefined : value;
|
---|
22 | }
|
---|
23 |
|
---|
24 | isBuffer() {
|
---|
25 | return this._valueIsBuffer;
|
---|
26 | }
|
---|
27 |
|
---|
28 | source() {
|
---|
29 | if (this._value === undefined) {
|
---|
30 | this._value = this._valueAsBuffer.toString("utf-8");
|
---|
31 | }
|
---|
32 | return this._value;
|
---|
33 | }
|
---|
34 |
|
---|
35 | buffer() {
|
---|
36 | if (this._valueAsBuffer === undefined) {
|
---|
37 | this._valueAsBuffer = Buffer.from(this._value, "utf-8");
|
---|
38 | }
|
---|
39 | return this._valueAsBuffer;
|
---|
40 | }
|
---|
41 |
|
---|
42 | map(options) {
|
---|
43 | return null;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * @param {object} options options
|
---|
48 | * @param {function(string, number, number, number, number, number, number): void} onChunk called for each chunk of code
|
---|
49 | * @param {function(number, string, string)} onSource called for each source
|
---|
50 | * @param {function(number, string)} onName called for each name
|
---|
51 | * @returns {void}
|
---|
52 | */
|
---|
53 | streamChunks(options, onChunk, onSource, onName) {
|
---|
54 | if (this._value === undefined) {
|
---|
55 | this._value = Buffer.from(this._valueAsBuffer, "utf-8");
|
---|
56 | }
|
---|
57 | if (this._valueAsString === undefined) {
|
---|
58 | this._valueAsString =
|
---|
59 | typeof this._value === "string"
|
---|
60 | ? this._value
|
---|
61 | : this._value.toString("utf-8");
|
---|
62 | }
|
---|
63 | return streamChunksOfRawSource(
|
---|
64 | this._valueAsString,
|
---|
65 | onChunk,
|
---|
66 | onSource,
|
---|
67 | onName,
|
---|
68 | !!(options && options.finalSource)
|
---|
69 | );
|
---|
70 | }
|
---|
71 |
|
---|
72 | updateHash(hash) {
|
---|
73 | if (this._valueAsBuffer === undefined) {
|
---|
74 | this._valueAsBuffer = Buffer.from(this._value, "utf-8");
|
---|
75 | }
|
---|
76 | hash.update("RawSource");
|
---|
77 | hash.update(this._valueAsBuffer);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | module.exports = RawSource;
|
---|