RawSource.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. this._valueIsBuffer = !convertToString && isBuffer;
  33. const internedString =
  34. typeof value === "string" ? internString(value) : undefined;
  35. /**
  36. * @private
  37. * @type {undefined | string | Buffer}
  38. */
  39. this._value =
  40. convertToString && isBuffer
  41. ? undefined
  42. : typeof value === "string"
  43. ? internedString
  44. : value;
  45. /**
  46. * @private
  47. * @type {undefined | Buffer}
  48. */
  49. this._valueAsBuffer = isBuffer ? value : undefined;
  50. /**
  51. * @private
  52. * @type {undefined | string}
  53. */
  54. this._valueAsString = isBuffer ? undefined : internedString;
  55. }
  56. isBuffer() {
  57. return this._valueIsBuffer;
  58. }
  59. /**
  60. * @returns {SourceValue} source
  61. */
  62. source() {
  63. if (this._value === undefined) {
  64. const value =
  65. /** @type {Buffer} */
  66. (this._valueAsBuffer).toString("utf8");
  67. if (isDualStringBufferCachingEnabled()) {
  68. this._value = internString(value);
  69. }
  70. return value;
  71. }
  72. return this._value;
  73. }
  74. buffer() {
  75. if (this._valueAsBuffer === undefined) {
  76. const value = Buffer.from(/** @type {string} */ (this._value), "utf8");
  77. if (isDualStringBufferCachingEnabled()) {
  78. this._valueAsBuffer = value;
  79. }
  80. return value;
  81. }
  82. return this._valueAsBuffer;
  83. }
  84. /**
  85. * @param {MapOptions=} options map options
  86. * @returns {RawSourceMap | null} map
  87. */
  88. // eslint-disable-next-line no-unused-vars
  89. map(options) {
  90. return null;
  91. }
  92. /**
  93. * @param {Options} options options
  94. * @param {OnChunk} onChunk called for each chunk of code
  95. * @param {OnSource} onSource called for each source
  96. * @param {OnName} onName called for each name
  97. * @returns {GeneratedSourceInfo} generated source info
  98. */
  99. streamChunks(options, onChunk, onSource, onName) {
  100. let strValue = this._valueAsString;
  101. if (strValue === undefined) {
  102. const value = this.source();
  103. strValue = typeof value === "string" ? value : value.toString("utf8");
  104. if (isDualStringBufferCachingEnabled()) {
  105. this._valueAsString = internString(strValue);
  106. }
  107. }
  108. return streamChunksOfRawSource(
  109. strValue,
  110. onChunk,
  111. onSource,
  112. onName,
  113. Boolean(options && options.finalSource),
  114. );
  115. }
  116. /**
  117. * @param {HashLike} hash hash
  118. * @returns {void}
  119. */
  120. updateHash(hash) {
  121. hash.update("RawSource");
  122. hash.update(this.buffer());
  123. }
  124. }
  125. module.exports = RawSource;