CompatSource.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /** @typedef {import("./Source").HashLike} HashLike */
  8. /** @typedef {import("./Source").MapOptions} MapOptions */
  9. /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
  10. /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
  11. /** @typedef {import("./Source").SourceValue} SourceValue */
  12. /**
  13. * @typedef {object} SourceLike
  14. * @property {() => SourceValue} source source
  15. * @property {(() => Buffer)=} buffer buffer
  16. * @property {(() => number)=} size size
  17. * @property {((options?: MapOptions) => RawSourceMap | null)=} map map
  18. * @property {((options?: MapOptions) => SourceAndMap)=} sourceAndMap source and map
  19. * @property {((hash: HashLike) => void)=} updateHash hash updater
  20. */
  21. class CompatSource extends Source {
  22. /**
  23. * @param {SourceLike} sourceLike source like
  24. * @returns {Source} source
  25. */
  26. static from(sourceLike) {
  27. return sourceLike instanceof Source
  28. ? sourceLike
  29. : new CompatSource(sourceLike);
  30. }
  31. /**
  32. * @param {SourceLike} sourceLike source like
  33. */
  34. constructor(sourceLike) {
  35. super();
  36. /**
  37. * @private
  38. * @type {SourceLike}
  39. */
  40. this._sourceLike = sourceLike;
  41. }
  42. /**
  43. * @returns {SourceValue} source
  44. */
  45. source() {
  46. return this._sourceLike.source();
  47. }
  48. buffer() {
  49. if (typeof this._sourceLike.buffer === "function") {
  50. return this._sourceLike.buffer();
  51. }
  52. return super.buffer();
  53. }
  54. size() {
  55. if (typeof this._sourceLike.size === "function") {
  56. return this._sourceLike.size();
  57. }
  58. return super.size();
  59. }
  60. /**
  61. * @param {MapOptions=} options map options
  62. * @returns {RawSourceMap | null} map
  63. */
  64. map(options) {
  65. if (typeof this._sourceLike.map === "function") {
  66. return this._sourceLike.map(options);
  67. }
  68. return super.map(options);
  69. }
  70. /**
  71. * @param {MapOptions=} options map options
  72. * @returns {SourceAndMap} source and map
  73. */
  74. sourceAndMap(options) {
  75. if (typeof this._sourceLike.sourceAndMap === "function") {
  76. return this._sourceLike.sourceAndMap(options);
  77. }
  78. return super.sourceAndMap(options);
  79. }
  80. /**
  81. * @param {HashLike} hash hash
  82. * @returns {void}
  83. */
  84. updateHash(hash) {
  85. if (typeof this._sourceLike.updateHash === "function") {
  86. return this._sourceLike.updateHash(hash);
  87. }
  88. if (typeof this._sourceLike.map === "function") {
  89. throw new Error(
  90. "A Source-like object with a 'map' method must also provide an 'updateHash' method",
  91. );
  92. }
  93. hash.update(this.buffer());
  94. }
  95. }
  96. module.exports = CompatSource;