Source.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * @typedef {object} MapOptions
  8. * @property {boolean=} columns need columns?
  9. * @property {boolean=} module is module
  10. */
  11. /**
  12. * @typedef {object} RawSourceMap
  13. * @property {number} version version
  14. * @property {string[]} sources sources
  15. * @property {string[]} names names
  16. * @property {string=} sourceRoot source root
  17. * @property {string[]=} sourcesContent sources content
  18. * @property {string} mappings mappings
  19. * @property {string} file file
  20. * @property {string=} debugId debug id
  21. * @property {number[]=} ignoreList ignore list
  22. */
  23. /** @typedef {string | Buffer} SourceValue */
  24. /**
  25. * @typedef {object} SourceAndMap
  26. * @property {SourceValue} source source
  27. * @property {RawSourceMap | null} map map
  28. */
  29. /**
  30. * @typedef {object} HashLike
  31. * @property {(data: string | Buffer, inputEncoding?: string) => HashLike} update make hash update
  32. * @property {(encoding?: string) => string | Buffer} digest get hash digest
  33. */
  34. class Source {
  35. /**
  36. * @returns {SourceValue} source
  37. */
  38. source() {
  39. throw new Error("Abstract");
  40. }
  41. /**
  42. * @returns {Buffer} buffer
  43. */
  44. buffer() {
  45. const source = this.source();
  46. if (Buffer.isBuffer(source)) return source;
  47. return Buffer.from(source, "utf8");
  48. }
  49. /**
  50. * @returns {Buffer[]} buffers
  51. */
  52. buffers() {
  53. return [this.buffer()];
  54. }
  55. /**
  56. * @returns {number} size
  57. */
  58. size() {
  59. return this.buffer().length;
  60. }
  61. /**
  62. * @param {MapOptions=} options map options
  63. * @returns {RawSourceMap | null} map
  64. */
  65. // eslint-disable-next-line no-unused-vars
  66. map(options) {
  67. return null;
  68. }
  69. /**
  70. * @param {MapOptions=} options map options
  71. * @returns {SourceAndMap} source and map
  72. */
  73. sourceAndMap(options) {
  74. return {
  75. source: this.source(),
  76. map: this.map(options),
  77. };
  78. }
  79. /**
  80. * @param {HashLike} hash hash
  81. * @returns {void}
  82. */
  83. // eslint-disable-next-line no-unused-vars
  84. updateHash(hash) {
  85. throw new Error("Abstract");
  86. }
  87. }
  88. module.exports = Source;