Skip to content

Commit c0f02b2

Browse files
committed
Rewrite async API to sync
1 parent 44ed0d5 commit c0f02b2

File tree

6 files changed

+141
-163
lines changed

6 files changed

+141
-163
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Web is not supported in this fork. Use [original source-map](https://github.com/
3535
- [With SourceMapGenerator (low level API)](#with-sourcemapgenerator-low-level-api)
3636
- [API](#api)
3737
- [SourceMapConsumer](#sourcemapconsumer)
38-
- [SourceMapConsumer.initialize(options)](#sourcemapconsumerinitializeoptions)
3938
- [new SourceMapConsumer(rawSourceMap)](#new-sourcemapconsumerrawsourcemap)
4039
- [SourceMapConsumer.with](#sourcemapconsumerwith)
4140
- [SourceMapConsumer.prototype.destroy()](#sourcemapconsumerprototypedestroy)
@@ -82,7 +81,7 @@ const rawSourceMap = {
8281
mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA"
8382
};
8483

85-
const whatever = await SourceMapConsumer.with(rawSourceMap, null, consumer => {
84+
const whatever = SourceMapConsumer.with(rawSourceMap, null, consumer => {
8685
console.log(consumer.sources);
8786
// [ 'http://example.com/www/js/one.js',
8887
// 'http://example.com/www/js/two.js' ]
@@ -232,13 +231,13 @@ following attributes:
232231

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

235-
The promise of the constructed souce map consumer is returned.
234+
The constructed souce map consumer is returned.
236235

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

240239
```js
241-
const consumer = await new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
240+
const consumer = new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
242241
doStuffWith(consumer);
243242
consumer.destroy();
244243
```
@@ -259,11 +258,11 @@ By using `with`, you do not have to remember to manually call `destroy` on
259258
the consumer, since it will be called automatically once `f` completes.
260259

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

266-
const x = await whatever(consumer);
265+
const x = whatever(consumer);
267266
return x * x;
268267
});
269268

@@ -654,7 +653,7 @@ Creates a SourceNode from generated code and a SourceMapConsumer.
654653
should be relative to.
655654

656655
```js
657-
const consumer = await new SourceMapConsumer(fs.readFileSync("path/to/my-file.js.map", "utf8"));
656+
const consumer = new SourceMapConsumer(fs.readFileSync("path/to/my-file.js.map", "utf8"));
658657
const node = SourceNode.fromStringWithSourceMap(fs.readFileSync("path/to/my-file.js"), consumer);
659658
```
660659

lib/read-wasm.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@ const fs = require("fs");
77
const path = require("path");
88

99
module.exports = function readWasm() {
10-
return new Promise((resolve, reject) => {
11-
const wasmPath = path.join(__dirname, "mappings.wasm");
12-
fs.readFile(wasmPath, null, (error, data) => {
13-
if (error) {
14-
reject(error);
15-
return;
16-
}
17-
18-
resolve(data.buffer);
19-
});
20-
});
10+
const wasmPath = path.join(__dirname, "mappings.wasm");
11+
const data = fs.readFileSync(wasmPath, null);
12+
return data.buffer;
2113
};
2214

2315
module.exports.initialize = _ => {

lib/source-map-consumer.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,22 @@ const util = require("./util");
99
const binarySearch = require("./binary-search");
1010
const ArraySet = require("./array-set").ArraySet;
1111
const base64VLQ = require("./base64-vlq"); // eslint-disable-line no-unused-vars
12-
const readWasm = require("../lib/read-wasm");
1312
const wasm = require("./wasm");
1413

1514
const INTERNAL = Symbol("smcInternal");
1615

1716
class SourceMapConsumer {
1817
constructor(aSourceMap, aSourceMapURL) {
19-
// If the constructor was called by super(), just return Promise<this>.
18+
// If the constructor was called by super(), just return this.
2019
// Yes, this is a hack to retain the pre-existing API of the base-class
21-
// constructor also being an async factory function.
20+
// constructor also being an factory function.
2221
if (aSourceMap == INTERNAL) {
23-
return Promise.resolve(this);
22+
return this;
2423
}
2524

2625
return _factory(aSourceMap, aSourceMapURL);
2726
}
2827

29-
static initialize(opts) {
30-
readWasm.initialize(opts["lib/mappings.wasm"]);
31-
}
32-
3328
static fromSourceMap(aSourceMap, aSourceMapURL) {
3429
return _factoryBSM(aSourceMap, aSourceMapURL);
3530
}
@@ -47,14 +42,14 @@ class SourceMapConsumer {
4742
* the consumer, since it will be called automatically once `f` completes.
4843
*
4944
* ```js
50-
* const xSquared = await SourceMapConsumer.with(
45+
* const xSquared = SourceMapConsumer.with(
5146
* myRawSourceMap,
5247
* null,
5348
* async function (consumer) {
5449
* // Use `consumer` inside here and don't worry about remembering
5550
* // to call `destroy`.
5651
*
57-
* const x = await whatever(consumer);
52+
* const x = whatever(consumer);
5853
* return x * x;
5954
* }
6055
* );
@@ -65,9 +60,9 @@ class SourceMapConsumer {
6560
* ```
6661
*/
6762
static async with(rawSourceMap, sourceMapUrl, f) {
68-
const consumer = await new SourceMapConsumer(rawSourceMap, sourceMapUrl);
63+
const consumer = new SourceMapConsumer(rawSourceMap, sourceMapUrl);
6964
try {
70-
return await f(consumer);
65+
return f(consumer);
7166
} finally {
7267
consumer.destroy();
7368
}
@@ -172,7 +167,8 @@ exports.SourceMapConsumer = SourceMapConsumer;
172167
*/
173168
class BasicSourceMapConsumer extends SourceMapConsumer {
174169
constructor(aSourceMap, aSourceMapURL) {
175-
return super(INTERNAL).then(that => {
170+
/* eslint-disable prettier/prettier */
171+
const that = super(INTERNAL);
176172
let sourceMap = aSourceMap;
177173
if (typeof aSourceMap === "string") {
178174
sourceMap = util.parseSourceMapInput(aSourceMap);
@@ -220,11 +216,12 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
220216
that._mappingsPtr = 0;
221217
that._wasm = null;
222218

223-
return wasm().then(w => {
224-
that._wasm = w;
219+
// return wasm().then(w => {
220+
that._wasm = wasm();
225221
return that;
226-
});
227-
});
222+
// });
223+
// });
224+
/* eslint-enable/ prettier/prettier */
228225
}
229226

230227
/**
@@ -721,7 +718,7 @@ exports.BasicSourceMapConsumer = BasicSourceMapConsumer;
721718
*/
722719
class IndexedSourceMapConsumer extends SourceMapConsumer {
723720
constructor(aSourceMap, aSourceMapURL) {
724-
return super(INTERNAL).then(that => {
721+
const that = super(INTERNAL);
725722
let sourceMap = aSourceMap;
726723
if (typeof aSourceMap === "string") {
727724
sourceMap = util.parseSourceMapInput(aSourceMap);
@@ -738,8 +735,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
738735
line: -1,
739736
column: 0
740737
};
741-
return Promise.all(
742-
sections.map(s => {
738+
// return Promise.all(
739+
const mappedSecions = sections.map(s => {
743740
if (s.url) {
744741
// The url field will require support for asynchronicity.
745742
// See https://github.com/mozilla/source-map/issues/16
@@ -761,11 +758,11 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
761758
}
762759
lastOffset = offset;
763760

764-
const cons = new SourceMapConsumer(
761+
const consumer = new SourceMapConsumer(
765762
util.getArg(s, "map"),
766763
aSourceMapURL
767764
);
768-
return cons.then(consumer => {
765+
// return cons.then(consumer => {
769766
return {
770767
generatedOffset: {
771768
// The offset fields are 0-based, but we use 1-based indices when
@@ -775,13 +772,13 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
775772
},
776773
consumer
777774
};
778-
});
779-
})
780-
).then(s => {
781-
that._sections = s;
775+
// });
776+
});
777+
// ).then(s => {
778+
that._sections = mappedSecions;
782779
return that;
783-
});
784-
});
780+
// });
781+
// });
785782
}
786783

787784
/**
@@ -1058,7 +1055,7 @@ function _factory(aSourceMap, aSourceMapURL) {
10581055
sourceMap.sections != null
10591056
? new IndexedSourceMapConsumer(sourceMap, aSourceMapURL)
10601057
: new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
1061-
return Promise.resolve(consumer);
1058+
return consumer;
10621059
}
10631060

10641061
function _factoryBSM(aSourceMap, aSourceMapURL) {

0 commit comments

Comments
 (0)