CompatSource.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. class CompatSource extends Source {
  8. static from(sourceLike) {
  9. return sourceLike instanceof Source
  10. ? sourceLike
  11. : new CompatSource(sourceLike);
  12. }
  13. constructor(sourceLike) {
  14. super();
  15. this._sourceLike = sourceLike;
  16. }
  17. source() {
  18. return this._sourceLike.source();
  19. }
  20. buffer() {
  21. if (typeof this._sourceLike.buffer === "function") {
  22. return this._sourceLike.buffer();
  23. }
  24. return super.buffer();
  25. }
  26. size() {
  27. if (typeof this._sourceLike.size === "function") {
  28. return this._sourceLike.size();
  29. }
  30. return super.size();
  31. }
  32. map(options) {
  33. if (typeof this._sourceLike.map === "function") {
  34. return this._sourceLike.map(options);
  35. }
  36. return super.map(options);
  37. }
  38. sourceAndMap(options) {
  39. if (typeof this._sourceLike.sourceAndMap === "function") {
  40. return this._sourceLike.sourceAndMap(options);
  41. }
  42. return super.sourceAndMap(options);
  43. }
  44. updateHash(hash) {
  45. if (typeof this._sourceLike.updateHash === "function") {
  46. return this._sourceLike.updateHash(hash);
  47. }
  48. if (typeof this._sourceLike.map === "function") {
  49. throw new Error(
  50. "A Source-like object with a 'map' method must also provide an 'updateHash' method"
  51. );
  52. }
  53. hash.update(this.buffer());
  54. }
  55. }
  56. module.exports = CompatSource;