read-wasm.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. if (__PLATFORM_WEB__) {
  3. // Web version of reading a wasm file into an array buffer.
  4. let mappingsWasm = null
  5. module.exports = function readWasm() {
  6. if (typeof mappingsWasm === 'string') {
  7. return fetch(mappingsWasm).then((response) => response.arrayBuffer())
  8. }
  9. if (mappingsWasm instanceof ArrayBuffer) {
  10. return Promise.resolve(mappingsWasm)
  11. }
  12. throw new Error(
  13. 'You must provide the string URL or ArrayBuffer contents ' +
  14. 'of lib/mappings.wasm by calling ' +
  15. "SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) " +
  16. 'before using SourceMapConsumer'
  17. )
  18. }
  19. module.exports.initialize = (input) => (mappingsWasm = input)
  20. } else {
  21. // Node version of reading a wasm file into an array buffer.
  22. const fs = require('fs')
  23. const path = require('path')
  24. module.exports = function readWasm() {
  25. return new Promise((resolve, reject) => {
  26. const wasmPath = path.join(
  27. __dirname,
  28. '../lib/source-map/lib',
  29. 'mappings.wasm'
  30. )
  31. fs.readFile(wasmPath, null, (error, data) => {
  32. if (error) {
  33. reject(error)
  34. return
  35. }
  36. resolve(data.buffer)
  37. })
  38. })
  39. }
  40. module.exports.initialize = (_) => {
  41. console.debug(
  42. 'SourceMapConsumer.initialize is a no-op when running in node.js'
  43. )
  44. }
  45. }