Source.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. buffer() {
  42. const source = this.source();
  43. if (Buffer.isBuffer(source)) return source;
  44. return Buffer.from(source, "utf8");
  45. }
  46. size() {
  47. return this.buffer().length;
  48. }
  49. /**
  50. * @param {MapOptions=} options map options
  51. * @returns {RawSourceMap | null} map
  52. */
  53. // eslint-disable-next-line no-unused-vars
  54. map(options) {
  55. return null;
  56. }
  57. /**
  58. * @param {MapOptions=} options map options
  59. * @returns {SourceAndMap} source and map
  60. */
  61. sourceAndMap(options) {
  62. return {
  63. source: this.source(),
  64. map: this.map(options),
  65. };
  66. }
  67. /**
  68. * @param {HashLike} hash hash
  69. * @returns {void}
  70. */
  71. // eslint-disable-next-line no-unused-vars
  72. updateHash(hash) {
  73. throw new Error("Abstract");
  74. }
  75. }
  76. module.exports = Source;