CompatSource.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. this._sourceLike = sourceLike;
  37. }
  38. /**
  39. * @returns {SourceValue} source
  40. */
  41. source() {
  42. return this._sourceLike.source();
  43. }
  44. buffer() {
  45. if (typeof this._sourceLike.buffer === "function") {
  46. return this._sourceLike.buffer();
  47. }
  48. return super.buffer();
  49. }
  50. size() {
  51. if (typeof this._sourceLike.size === "function") {
  52. return this._sourceLike.size();
  53. }
  54. return super.size();
  55. }
  56. /**
  57. * @param {MapOptions=} options map options
  58. * @returns {RawSourceMap | null} map
  59. */
  60. map(options) {
  61. if (typeof this._sourceLike.map === "function") {
  62. return this._sourceLike.map(options);
  63. }
  64. return super.map(options);
  65. }
  66. /**
  67. * @param {MapOptions=} options map options
  68. * @returns {SourceAndMap} source and map
  69. */
  70. sourceAndMap(options) {
  71. if (typeof this._sourceLike.sourceAndMap === "function") {
  72. return this._sourceLike.sourceAndMap(options);
  73. }
  74. return super.sourceAndMap(options);
  75. }
  76. /**
  77. * @param {HashLike} hash hash
  78. * @returns {void}
  79. */
  80. updateHash(hash) {
  81. if (typeof this._sourceLike.updateHash === "function") {
  82. return this._sourceLike.updateHash(hash);
  83. }
  84. if (typeof this._sourceLike.map === "function") {
  85. throw new Error(
  86. "A Source-like object with a 'map' method must also provide an 'updateHash' method",
  87. );
  88. }
  89. hash.update(this.buffer());
  90. }
  91. }
  92. module.exports = CompatSource;