UNPKG

1.88 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const streamChunksOfRawSource = require("./helpers/streamChunksOfRawSource");
9const Source = require("./Source");
10
11class 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 * @param {object} options options
47 * @param {function(string, number, number, number, number, number, number): void} onChunk called for each chunk of code
48 * @param {function(number, string, string)} onSource called for each source
49 * @param {function(number, string)} onName called for each name
50 * @returns {void}
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
74module.exports = RawSource;