1 |
|
2 |
|
3 |
|
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 | }
|
22 |
|
23 | isBuffer() {
|
24 | return this._valueIsBuffer;
|
25 | }
|
26 |
|
27 | source() {
|
28 | if (this._value === undefined) {
|
29 | this._value = this._valueAsBuffer.toString("utf-8");
|
30 | }
|
31 | return this._value;
|
32 | }
|
33 |
|
34 | buffer() {
|
35 | if (this._valueAsBuffer === undefined) {
|
36 | this._valueAsBuffer = Buffer.from(this._value, "utf-8");
|
37 | }
|
38 | return this._valueAsBuffer;
|
39 | }
|
40 |
|
41 | map(options) {
|
42 | return null;
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | streamChunks(options, onChunk, onSource, onName) {
|
53 | if (this._value === undefined) {
|
54 | this._value = this._valueAsBuffer.toString("utf-8");
|
55 | }
|
56 | return streamChunksOfRawSource(
|
57 | this._value,
|
58 | onChunk,
|
59 | onSource,
|
60 | onName,
|
61 | !!(options && options.finalSource)
|
62 | );
|
63 | }
|
64 |
|
65 | updateHash(hash) {
|
66 | if (this._valueAsBuffer === undefined) {
|
67 | this._valueAsBuffer = Buffer.from(this._value, "utf-8");
|
68 | }
|
69 | hash.update("RawSource");
|
70 | hash.update(this._valueAsBuffer);
|
71 | }
|
72 | }
|
73 |
|
74 | module.exports = RawSource;
|