RawSource.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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").Hash} Hash */
  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("utf-8");
  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), "utf-8");
  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. map(options) {
  89. return null;
  90. }
  91. /**
  92. * @param {Options} options options
  93. * @param {OnChunk} onChunk called for each chunk of code
  94. * @param {OnSource} onSource called for each source
  95. * @param {OnName} onName called for each name
  96. * @returns {GeneratedSourceInfo} generated source info
  97. */
  98. streamChunks(options, onChunk, onSource, onName) {
  99. let strValue = this._valueAsString;
  100. if (strValue === undefined) {
  101. const value = this.source();
  102. strValue = typeof value === "string" ? value : value.toString("utf-8");
  103. if (isDualStringBufferCachingEnabled()) {
  104. this._valueAsString = internString(strValue);
  105. }
  106. }
  107. return streamChunksOfRawSource(
  108. strValue,
  109. onChunk,
  110. onSource,
  111. onName,
  112. !!(options && options.finalSource)
  113. );
  114. }
  115. /**
  116. * @param {Hash} hash hash
  117. * @returns {void}
  118. */
  119. updateHash(hash) {
  120. hash.update("RawSource");
  121. hash.update(
  122. this._valueAsBuffer
  123. ? /** @type {Buffer} */ (this._valueAsBuffer)
  124. : /** @type {string} */ (this._valueAsString)
  125. );
  126. }
  127. }
  128. module.exports = RawSource;