read-wasm.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* Determine browser vs node environment by testing the default top level context. Solution courtesy of: https://stackoverflow.com/questions/17575790/environment-detection-node-js-or-browser */
  2. const isBrowserEnvironment = (function() {
  3. // eslint-disable-next-line no-undef
  4. return (typeof window !== "undefined") && (this === window);
  5. }).call();
  6. if (isBrowserEnvironment) {
  7. // Web version of reading a wasm file into an array buffer.
  8. let mappingsWasm = null;
  9. module.exports = function readWasm() {
  10. if (typeof mappingsWasm === "string") {
  11. return fetch(mappingsWasm)
  12. .then(response => response.arrayBuffer());
  13. }
  14. if (mappingsWasm instanceof ArrayBuffer) {
  15. return Promise.resolve(mappingsWasm);
  16. }
  17. throw new Error("You must provide the string URL or ArrayBuffer contents " +
  18. "of lib/mappings.wasm by calling " +
  19. "SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) " +
  20. "before using SourceMapConsumer");
  21. };
  22. module.exports.initialize = input => mappingsWasm = input;
  23. } else {
  24. // Node version of reading a wasm file into an array buffer.
  25. const fs = require("fs");
  26. const path = require("path");
  27. module.exports = function readWasm() {
  28. return new Promise((resolve, reject) => {
  29. const wasmPath = path.join(__dirname, "mappings.wasm");
  30. fs.readFile(wasmPath, null, (error, data) => {
  31. if (error) {
  32. reject(error);
  33. return;
  34. }
  35. resolve(data.buffer);
  36. });
  37. });
  38. };
  39. module.exports.initialize = _ => {
  40. console.debug("SourceMapConsumer.initialize is a no-op when running in node.js");
  41. };
  42. }