Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync WASM API #1

Closed
wants to merge 14 commits into from
15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ sudo: false

node_js:
- "8"
- "9"
- "10"
- "12"
- "14"
- "15"


install:
- npm install
- npm install coveralls
- yarn
- yarn add coveralls

script:
- npm run coverage
- npm run lint
- yarn run coverage
- yarn run lint

after_success:
- nyc report --reporter=text-lcov | coveralls
Expand All @@ -24,3 +28,4 @@ cache:
os:
- linux
- osx
- windows
37 changes: 7 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ This is a library to generate and consume the source map format

## Use on the Web

<script src="https://unpkg.com/source-map@0.7.3/dist/source-map.js"></script>
<script>
sourceMap.SourceMapConsumer.initialize({
"lib/mappings.wasm": "https://unpkg.com/source-map@0.7.3/lib/mappings.wasm"
});
</script>
Web is not supported in this fork. Use [original source-map](https://github.com/mozilla/source-map#use-on-the-web) for it.

---

Expand All @@ -40,7 +35,6 @@ This is a library to generate and consume the source map format
- [With SourceMapGenerator (low level API)](#with-sourcemapgenerator-low-level-api)
- [API](#api)
- [SourceMapConsumer](#sourcemapconsumer)
- [SourceMapConsumer.initialize(options)](#sourcemapconsumerinitializeoptions)
- [new SourceMapConsumer(rawSourceMap)](#new-sourcemapconsumerrawsourcemap)
- [SourceMapConsumer.with](#sourcemapconsumerwith)
- [SourceMapConsumer.prototype.destroy()](#sourcemapconsumerprototypedestroy)
Expand Down Expand Up @@ -87,7 +81,7 @@ const rawSourceMap = {
mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA"
};

const whatever = await SourceMapConsumer.with(rawSourceMap, null, consumer => {
const whatever = SourceMapConsumer.with(rawSourceMap, null, consumer => {
console.log(consumer.sources);
// [ 'http://example.com/www/js/one.js',
// 'http://example.com/www/js/two.js' ]
Expand Down Expand Up @@ -199,23 +193,6 @@ A `SourceMapConsumer` instance represents a parsed source map which we can query
for information about the original file positions by giving it a file position
in the generated source.

#### SourceMapConsumer.initialize(options)

When using `SourceMapConsumer` outside of node.js, for example on the Web, it
needs to know from what URL to load `lib/mappings.wasm`. You must inform it by
calling `initialize` before constructing any `SourceMapConsumer`s.

The options object has the following properties:

- `"lib/mappings.wasm"`: A `String` containing the URL of the
`lib/mappings.wasm` file, or an `ArrayBuffer` with the contents of `lib/mappings.wasm`.

```js
sourceMap.SourceMapConsumer.initialize({
"lib/mappings.wasm": "https://example.com/source-map/lib/mappings.wasm"
});
```

#### new SourceMapConsumer(rawSourceMap)

The only parameter is the raw source map (either as a string which can be
Expand All @@ -237,13 +214,13 @@ following attributes:

- `file`: Optional. The generated filename this source map is associated with.

The promise of the constructed souce map consumer is returned.
The constructed souce map consumer is returned.

When the `SourceMapConsumer` will no longer be used anymore, you must call its
`destroy` method.

```js
const consumer = await new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
const consumer = new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
doStuffWith(consumer);
consumer.destroy();
```
Expand All @@ -264,11 +241,11 @@ By using `with`, you do not have to remember to manually call `destroy` on
the consumer, since it will be called automatically once `f` completes.

```js
const xSquared = await SourceMapConsumer.with(myRawSourceMap, null, async function(consumer) {
const xSquared = SourceMapConsumer.with(myRawSourceMap, null, async function(consumer) {
// Use `consumer` inside here and don't worry about remembering
// to call `destroy`.

const x = await whatever(consumer);
const x = whatever(consumer);
return x * x;
});

Expand Down Expand Up @@ -659,7 +636,7 @@ Creates a SourceNode from generated code and a SourceMapConsumer.
should be relative to.

```js
const consumer = await new SourceMapConsumer(fs.readFileSync("path/to/my-file.js.map", "utf8"));
const consumer = new SourceMapConsumer(fs.readFileSync("path/to/my-file.js.map", "utf8"));
const node = SourceNode.fromStringWithSourceMap(fs.readFileSync("path/to/my-file.js"), consumer);
```

Expand Down
2 changes: 2 additions & 0 deletions lib/mappings.js

Large diffs are not rendered by default.

23 changes: 0 additions & 23 deletions lib/read-wasm-browser.js

This file was deleted.

14 changes: 3 additions & 11 deletions lib/read-wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@ const fs = require("fs");
const path = require("path");

module.exports = function readWasm() {
return new Promise((resolve, reject) => {
const wasmPath = path.join(__dirname, "mappings.wasm");
fs.readFile(wasmPath, null, (error, data) => {
if (error) {
reject(error);
return;
}

resolve(data.buffer);
});
});
const wasmPath = path.join(__dirname, "mappings.wasm");
const data = fs.readFileSync(wasmPath, null);
return data.buffer;
};

module.exports.initialize = _ => {
Expand Down
57 changes: 27 additions & 30 deletions lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,22 @@ const util = require("./util");
const binarySearch = require("./binary-search");
const ArraySet = require("./array-set").ArraySet;
const base64VLQ = require("./base64-vlq"); // eslint-disable-line no-unused-vars
const readWasm = require("../lib/read-wasm");
const wasm = require("./wasm");

const INTERNAL = Symbol("smcInternal");

class SourceMapConsumer {
constructor(aSourceMap, aSourceMapURL) {
// If the constructor was called by super(), just return Promise<this>.
// If the constructor was called by super(), just return this.
// Yes, this is a hack to retain the pre-existing API of the base-class
// constructor also being an async factory function.
// constructor also being an factory function.
if (aSourceMap == INTERNAL) {
return Promise.resolve(this);
return this;
}

return _factory(aSourceMap, aSourceMapURL);
}

static initialize(opts) {
readWasm.initialize(opts["lib/mappings.wasm"]);
}

static fromSourceMap(aSourceMap, aSourceMapURL) {
return _factoryBSM(aSourceMap, aSourceMapURL);
}
Expand All @@ -47,14 +42,14 @@ class SourceMapConsumer {
* the consumer, since it will be called automatically once `f` completes.
*
* ```js
* const xSquared = await SourceMapConsumer.with(
* const xSquared = SourceMapConsumer.with(
* myRawSourceMap,
* null,
* async function (consumer) {
* // Use `consumer` inside here and don't worry about remembering
* // to call `destroy`.
*
* const x = await whatever(consumer);
* const x = whatever(consumer);
* return x * x;
* }
* );
Expand All @@ -64,10 +59,10 @@ class SourceMapConsumer {
* console.log(xSquared);
* ```
*/
static async with(rawSourceMap, sourceMapUrl, f) {
const consumer = await new SourceMapConsumer(rawSourceMap, sourceMapUrl);
static with(rawSourceMap, sourceMapUrl, f) {
const consumer = new SourceMapConsumer(rawSourceMap, sourceMapUrl);
try {
return await f(consumer);
return f(consumer);
} finally {
consumer.destroy();
}
Expand Down Expand Up @@ -172,7 +167,8 @@ exports.SourceMapConsumer = SourceMapConsumer;
*/
class BasicSourceMapConsumer extends SourceMapConsumer {
constructor(aSourceMap, aSourceMapURL) {
return super(INTERNAL).then(that => {
/* eslint-disable prettier/prettier */
const that = super(INTERNAL);
let sourceMap = aSourceMap;
if (typeof aSourceMap === "string") {
sourceMap = util.parseSourceMapInput(aSourceMap);
Expand Down Expand Up @@ -220,11 +216,12 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
that._mappingsPtr = 0;
that._wasm = null;

return wasm().then(w => {
that._wasm = w;
// return wasm().then(w => {
that._wasm = wasm();
return that;
});
});
// });
// });
/* eslint-enable/ prettier/prettier */
}

/**
Expand Down Expand Up @@ -721,7 +718,7 @@ exports.BasicSourceMapConsumer = BasicSourceMapConsumer;
*/
class IndexedSourceMapConsumer extends SourceMapConsumer {
constructor(aSourceMap, aSourceMapURL) {
return super(INTERNAL).then(that => {
const that = super(INTERNAL);
let sourceMap = aSourceMap;
if (typeof aSourceMap === "string") {
sourceMap = util.parseSourceMapInput(aSourceMap);
Expand All @@ -738,8 +735,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
line: -1,
column: 0
};
return Promise.all(
sections.map(s => {
// return Promise.all(
const mappedSecions = sections.map(s => {
if (s.url) {
// The url field will require support for asynchronicity.
// See https://github.com/mozilla/source-map/issues/16
Expand All @@ -761,11 +758,11 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
}
lastOffset = offset;

const cons = new SourceMapConsumer(
const consumer = new SourceMapConsumer(
util.getArg(s, "map"),
aSourceMapURL
);
return cons.then(consumer => {
// return cons.then(consumer => {
return {
generatedOffset: {
// The offset fields are 0-based, but we use 1-based indices when
Expand All @@ -775,13 +772,13 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
},
consumer
};
});
})
).then(s => {
that._sections = s;
// });
});
// ).then(s => {
that._sections = mappedSecions;
return that;
});
});
// });
// });
}

/**
Expand Down Expand Up @@ -1058,7 +1055,7 @@ function _factory(aSourceMap, aSourceMapURL) {
sourceMap.sections != null
? new IndexedSourceMapConsumer(sourceMap, aSourceMapURL)
: new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
return Promise.resolve(consumer);
return consumer;
}

function _factoryBSM(aSourceMap, aSourceMapURL) {
Expand Down
21 changes: 0 additions & 21 deletions lib/url-browser.js

This file was deleted.

Loading