RawSource.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Source = require("./Source");
  7. const streamChunksOfRawSource = require("./helpers/streamChunksOfRawSource");
  8. const {
  9. internString,
  10. isDualStringBufferCachingEnabled,
  11. } = require("./helpers/stringBufferUtils");
  12. /** @typedef {import("./Source").HashLike} HashLike */
  13. /** @typedef {import("./Source").MapOptions} MapOptions */
  14. /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
  15. /** @typedef {import("./Source").SourceValue} SourceValue */
  16. /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
  17. /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
  18. /** @typedef {import("./helpers/streamChunks").OnName} OnName */
  19. /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
  20. /** @typedef {import("./helpers/streamChunks").Options} Options */
  21. class RawSource extends Source {
  22. /**
  23. * @param {string | Buffer} value value
  24. * @param {boolean=} convertToString convert to string
  25. */
  26. constructor(value, convertToString = false) {
  27. super();
  28. const isBuffer = Buffer.isBuffer(value);
  29. if (!isBuffer && typeof value !== "string") {
  30. throw new TypeError("argument 'value' must be either string or Buffer");
  31. }
  32. /**
  33. * @private
  34. * @type {boolean}
  35. */
  36. this._valueIsBuffer = !convertToString && isBuffer;
  37. const internedString =
  38. typeof value === "string" ? internString(value) : undefined;
  39. /**
  40. * @private
  41. * @type {undefined | string | Buffer}
  42. */
  43. this._value =
  44. convertToString && isBuffer
  45. ? undefined
  46. : typeof value === "string"
  47. ? internedString
  48. : value;
  49. /**
  50. * @private
  51. * @type {undefined | Buffer}
  52. */
  53. this._valueAsBuffer = isBuffer ? value : undefined;
  54. /**
  55. * @private
  56. * @type {undefined | string}
  57. */
  58. this._valueAsString = isBuffer ? undefined : internedString;
  59. }
  60. isBuffer() {
  61. return this._valueIsBuffer;
  62. }
  63. /**
  64. * @returns {SourceValue} source
  65. */
  66. source() {
  67. if (this._value === undefined) {
  68. const value =
  69. /** @type {Buffer} */
  70. (this._valueAsBuffer).toString("utf8");
  71. if (isDualStringBufferCachingEnabled()) {
  72. this._value = internString(value);
  73. }
  74. return value;
  75. }
  76. return this._value;
  77. }
  78. buffer() {
  79. if (this._valueAsBuffer === undefined) {
  80. const value = Buffer.from(/** @type {string} */ (this._value), "utf8");
  81. if (isDualStringBufferCachingEnabled()) {
  82. this._valueAsBuffer = value;
  83. }
  84. return value;
  85. }
  86. return this._valueAsBuffer;
  87. }
  88. /**
  89. * @param {MapOptions=} options map options
  90. * @returns {RawSourceMap | null} map
  91. */
  92. // eslint-disable-next-line no-unused-vars
  93. map(options) {
  94. return null;
  95. }
  96. /**
  97. * @param {Options} options options
  98. * @param {OnChunk} onChunk called for each chunk of code
  99. * @param {OnSource} onSource called for each source
  100. * @param {OnName} onName called for each name
  101. * @returns {GeneratedSourceInfo} generated source info
  102. */
  103. streamChunks(options, onChunk, onSource, onName) {
  104. let strValue = this._valueAsString;
  105. if (strValue === undefined) {
  106. const value = this.source();
  107. strValue = typeof value === "string" ? value : value.toString("utf8");
  108. if (isDualStringBufferCachingEnabled()) {
  109. this._valueAsString = internString(strValue);
  110. }
  111. }
  112. return streamChunksOfRawSource(
  113. strValue,
  114. onChunk,
  115. onSource,
  116. onName,
  117. Boolean(options && options.finalSource),
  118. );
  119. }
  120. /**
  121. * @param {HashLike} hash hash
  122. * @returns {void}
  123. */
  124. updateHash(hash) {
  125. hash.update("RawSource");
  126. hash.update(this.buffer());
  127. }
  128. }
  129. module.exports = RawSource;