@parcel/packager-wasm 2.9.4-nightly.3014
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/lib/WasmPackager.js +52 -0
- package/lib/wasm-sourcemap.js +166 -0
- package/package.json +26 -0
- package/src/WasmPackager.js +39 -0
- package/src/wasm-sourcemap.js +181 -0
- package/src/wasm-sourcemap.js.flow +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present Devon Govett
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
function _assert() {
|
|
8
|
+
const data = _interopRequireDefault(require("assert"));
|
|
9
|
+
_assert = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _plugin() {
|
|
15
|
+
const data = require("@parcel/plugin");
|
|
16
|
+
_plugin = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
var wasmmap = _interopRequireWildcard(require("./wasm-sourcemap"));
|
|
22
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
23
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
24
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
+
var _default = new (_plugin().Packager)({
|
|
26
|
+
async package({
|
|
27
|
+
bundle,
|
|
28
|
+
getSourceMapReference
|
|
29
|
+
}) {
|
|
30
|
+
let assets = [];
|
|
31
|
+
bundle.traverseAssets(asset => {
|
|
32
|
+
assets.push(asset);
|
|
33
|
+
});
|
|
34
|
+
_assert().default.equal(assets.length, 1, 'Wasm bundles must only contain one asset');
|
|
35
|
+
let [contents, map] = await Promise.all([assets[0].getBuffer(), assets[0].getMap()]);
|
|
36
|
+
let sourcemapReference = await getSourceMapReference(map);
|
|
37
|
+
if (sourcemapReference != null) {
|
|
38
|
+
return {
|
|
39
|
+
contents: Buffer.from(wasmmap.SetSourceMapURL(contents, sourcemapReference, sourcemapReference.includes('HASH_REF_') ?
|
|
40
|
+
// HASH_REF_\w{16} -> \w{8}
|
|
41
|
+
sourcemapReference.length - (9 + 16 - 8) : undefined)),
|
|
42
|
+
map
|
|
43
|
+
};
|
|
44
|
+
} else {
|
|
45
|
+
return {
|
|
46
|
+
contents,
|
|
47
|
+
map
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
exports.default = _default;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/* eslint-disable */
|
|
4
|
+
// Modified version of https://github.com/oasislabs/wasm-sourcemap/blob/77242f93ebd010cf69515d988b984244a119dd9d/index.js
|
|
5
|
+
|
|
6
|
+
// Modifications Copyright 2019 Oasis Labs
|
|
7
|
+
// Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
8
|
+
// provided that the following conditions are met:
|
|
9
|
+
|
|
10
|
+
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions
|
|
11
|
+
// and the following disclaimer.
|
|
12
|
+
|
|
13
|
+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of
|
|
14
|
+
// conditions and the following disclaimer in the documentation and/or other materials provided
|
|
15
|
+
// with the distribution.
|
|
16
|
+
|
|
17
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
|
18
|
+
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
19
|
+
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
20
|
+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
21
|
+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
22
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
23
|
+
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
|
24
|
+
// WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
25
|
+
|
|
26
|
+
const url = require('url');
|
|
27
|
+
const section = 'sourceMappingURL';
|
|
28
|
+
|
|
29
|
+
// read a variable uint encoding from the buffer stream.
|
|
30
|
+
// return the int, and the next position in the stream.
|
|
31
|
+
function read_uint(buf, pos) {
|
|
32
|
+
let n = 0;
|
|
33
|
+
let shift = 0;
|
|
34
|
+
let b = buf[pos];
|
|
35
|
+
let outpos = pos + 1;
|
|
36
|
+
while (b >= 128) {
|
|
37
|
+
n = n | b - 128 << shift;
|
|
38
|
+
b = buf[outpos];
|
|
39
|
+
outpos++;
|
|
40
|
+
shift += 7;
|
|
41
|
+
}
|
|
42
|
+
return [n + (b << shift), outpos];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Write a buffer with a variable uint encoding of a number.
|
|
46
|
+
function encode_uint(n) {
|
|
47
|
+
let result = [];
|
|
48
|
+
while (n > 127) {
|
|
49
|
+
result.push(128 | n & 127);
|
|
50
|
+
n = n >> 7;
|
|
51
|
+
}
|
|
52
|
+
result.push(n);
|
|
53
|
+
return new Uint8Array(result);
|
|
54
|
+
}
|
|
55
|
+
function ab2str(buf) {
|
|
56
|
+
let str = '';
|
|
57
|
+
let bytes = new Uint8Array(buf);
|
|
58
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
59
|
+
str += String.fromCharCode(bytes[i]);
|
|
60
|
+
}
|
|
61
|
+
return str;
|
|
62
|
+
}
|
|
63
|
+
function str2ab(str) {
|
|
64
|
+
let bytes = new Uint8Array(str.length);
|
|
65
|
+
for (let i = 0; i < str.length; i++) {
|
|
66
|
+
bytes[i] = str[i].charCodeAt(0);
|
|
67
|
+
}
|
|
68
|
+
return bytes;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Construct an array buffer representing a WASM 0-id
|
|
73
|
+
* sections containing a given name and value pair.
|
|
74
|
+
* @param {String} name
|
|
75
|
+
* @param {String} value
|
|
76
|
+
* @returns {Uint8Array}
|
|
77
|
+
*/
|
|
78
|
+
function WriteSection(name, value, valLenOverride) {
|
|
79
|
+
const nameBuf = str2ab(name);
|
|
80
|
+
const valBuf = str2ab(value);
|
|
81
|
+
const nameLen = encode_uint(nameBuf.length);
|
|
82
|
+
const valLen = encode_uint(valLenOverride !== null && valLenOverride !== void 0 ? valLenOverride : valBuf.length);
|
|
83
|
+
const sectionLenDeclared = nameLen.length + nameBuf.length + valLen.length + (valLenOverride !== null && valLenOverride !== void 0 ? valLenOverride : valBuf.length);
|
|
84
|
+
const sectionLenActual = nameLen.length + nameBuf.length + valLen.length + valBuf.length;
|
|
85
|
+
const headerLen = encode_uint(sectionLenDeclared);
|
|
86
|
+
let bytes = new Uint8Array(sectionLenActual + headerLen.length + 1);
|
|
87
|
+
let pos = 1;
|
|
88
|
+
bytes.set(headerLen, pos);
|
|
89
|
+
pos += headerLen.length;
|
|
90
|
+
bytes.set(nameLen, pos);
|
|
91
|
+
pos += nameLen.length;
|
|
92
|
+
bytes.set(nameBuf, pos);
|
|
93
|
+
pos += nameBuf.length;
|
|
94
|
+
bytes.set(valLen, pos);
|
|
95
|
+
pos += valLen.length;
|
|
96
|
+
bytes.set(valBuf, pos);
|
|
97
|
+
return bytes;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Search the module sections of a WASM buffer to find
|
|
102
|
+
* a section with a given identifier.
|
|
103
|
+
* @param {Buffer} buf
|
|
104
|
+
* @param {String} id
|
|
105
|
+
* @returns {Array.<Number>} An array with the index of
|
|
106
|
+
* the section, the length of the section, and the index
|
|
107
|
+
* of the beginning of the body of the section.
|
|
108
|
+
*/
|
|
109
|
+
function FindSection(buf, id) {
|
|
110
|
+
let pos = 8;
|
|
111
|
+
while (pos < buf.byteLength) {
|
|
112
|
+
const sec_start = pos;
|
|
113
|
+
const [sec_id, pos2] = read_uint(buf, pos);
|
|
114
|
+
const [sec_size, body_pos] = read_uint(buf, pos2);
|
|
115
|
+
pos = body_pos + sec_size;
|
|
116
|
+
if (sec_id == 0) {
|
|
117
|
+
const [name_len, name_pos] = read_uint(buf, body_pos);
|
|
118
|
+
const name = buf.slice(name_pos, name_pos + name_len);
|
|
119
|
+
const nameString = ab2str(name);
|
|
120
|
+
if (nameString == id) {
|
|
121
|
+
return [sec_start, sec_size + 1 + (body_pos - pos2), name_pos + name_len];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return [-1, null, null];
|
|
126
|
+
}
|
|
127
|
+
module.exports = {
|
|
128
|
+
/**
|
|
129
|
+
* GetSourceMapURL extracts the source map from a WASM buffer.
|
|
130
|
+
* @param {Buffer} buf The WASM buffer
|
|
131
|
+
* @returns {String|null} The linked sourcemap URL if present.
|
|
132
|
+
*/
|
|
133
|
+
GetSourceMapURL: function (buf) {
|
|
134
|
+
buf = new Uint8Array(buf);
|
|
135
|
+
const [sec_start, _, uri_start] = FindSection(buf, section);
|
|
136
|
+
if (sec_start == -1) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const [uri_len, uri_pos] = read_uint(buf, uri_start);
|
|
140
|
+
return ab2str(buf.slice(uri_pos, uri_pos + uri_len));
|
|
141
|
+
},
|
|
142
|
+
RemoveSourceMapURL: function (buf) {
|
|
143
|
+
buf = new Uint8Array(buf);
|
|
144
|
+
const [sec_start, sec_size, _] = FindSection(buf, section);
|
|
145
|
+
if (sec_start == -1) {
|
|
146
|
+
return buf;
|
|
147
|
+
}
|
|
148
|
+
let strippedBuf = new Uint8Array(buf.length - sec_size);
|
|
149
|
+
strippedBuf.set(buf.slice(0, sec_start));
|
|
150
|
+
strippedBuf.set(buf.slice(sec_start + sec_size), sec_start);
|
|
151
|
+
return strippedBuf;
|
|
152
|
+
},
|
|
153
|
+
SetSourceMapURL: function (buf, url, urlLenOverride) {
|
|
154
|
+
const stripped = module.exports.RemoveSourceMapURL(buf);
|
|
155
|
+
const newSection = WriteSection(section, url, urlLenOverride);
|
|
156
|
+
const outBuf = new Uint8Array(stripped.length + newSection.length);
|
|
157
|
+
outBuf.set(stripped);
|
|
158
|
+
outBuf.set(newSection, stripped.length);
|
|
159
|
+
return outBuf;
|
|
160
|
+
},
|
|
161
|
+
SetSourceMapURLRelativeTo: function (buf, relativeURL) {
|
|
162
|
+
const originalURL = module.exports.GetSourceMapURL(buf);
|
|
163
|
+
const newURL = url.resolve(relativeURL, originalURL);
|
|
164
|
+
return module.exports.SetSourceMapURL(buf, newURL);
|
|
165
|
+
}
|
|
166
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@parcel/packager-wasm",
|
|
3
|
+
"version": "2.9.4-nightly.3014+ddd838171",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"funding": {
|
|
9
|
+
"type": "opencollective",
|
|
10
|
+
"url": "https://opencollective.com/parcel"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/parcel-bundler/parcel.git"
|
|
15
|
+
},
|
|
16
|
+
"main": "lib/WasmPackager.js",
|
|
17
|
+
"source": "src/WasmPackager.js",
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=12.0.0",
|
|
20
|
+
"parcel": "2.0.0-nightly.1389+ddd838171"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@parcel/plugin": "2.0.0-nightly.1391+ddd838171"
|
|
24
|
+
},
|
|
25
|
+
"gitHead": "ddd838171d3082e3d65505314bda24f126f6daa1"
|
|
26
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
|
|
3
|
+
import assert from 'assert';
|
|
4
|
+
import {Packager} from '@parcel/plugin';
|
|
5
|
+
import * as wasmmap from './wasm-sourcemap';
|
|
6
|
+
|
|
7
|
+
export default (new Packager({
|
|
8
|
+
async package({bundle, getSourceMapReference}) {
|
|
9
|
+
let assets = [];
|
|
10
|
+
bundle.traverseAssets(asset => {
|
|
11
|
+
assets.push(asset);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
assert.equal(assets.length, 1, 'Wasm bundles must only contain one asset');
|
|
15
|
+
|
|
16
|
+
let [contents, map] = await Promise.all([
|
|
17
|
+
assets[0].getBuffer(),
|
|
18
|
+
assets[0].getMap(),
|
|
19
|
+
]);
|
|
20
|
+
let sourcemapReference = await getSourceMapReference(map);
|
|
21
|
+
if (sourcemapReference != null) {
|
|
22
|
+
return {
|
|
23
|
+
contents: Buffer.from(
|
|
24
|
+
wasmmap.SetSourceMapURL(
|
|
25
|
+
contents,
|
|
26
|
+
sourcemapReference,
|
|
27
|
+
sourcemapReference.includes('HASH_REF_')
|
|
28
|
+
? // HASH_REF_\w{16} -> \w{8}
|
|
29
|
+
sourcemapReference.length - (9 + 16 - 8)
|
|
30
|
+
: undefined,
|
|
31
|
+
),
|
|
32
|
+
),
|
|
33
|
+
map,
|
|
34
|
+
};
|
|
35
|
+
} else {
|
|
36
|
+
return {contents, map};
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
}): Packager);
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// Modified version of https://github.com/oasislabs/wasm-sourcemap/blob/77242f93ebd010cf69515d988b984244a119dd9d/index.js
|
|
3
|
+
|
|
4
|
+
// Modifications Copyright 2019 Oasis Labs
|
|
5
|
+
// Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
6
|
+
// provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions
|
|
9
|
+
// and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of
|
|
12
|
+
// conditions and the following disclaimer in the documentation and/or other materials provided
|
|
13
|
+
// with the distribution.
|
|
14
|
+
|
|
15
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
|
16
|
+
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
18
|
+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
19
|
+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
20
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
21
|
+
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
|
22
|
+
// WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
23
|
+
|
|
24
|
+
const url = require('url');
|
|
25
|
+
|
|
26
|
+
const section = 'sourceMappingURL';
|
|
27
|
+
|
|
28
|
+
// read a variable uint encoding from the buffer stream.
|
|
29
|
+
// return the int, and the next position in the stream.
|
|
30
|
+
function read_uint(buf, pos) {
|
|
31
|
+
let n = 0;
|
|
32
|
+
let shift = 0;
|
|
33
|
+
let b = buf[pos];
|
|
34
|
+
let outpos = pos + 1;
|
|
35
|
+
while (b >= 128) {
|
|
36
|
+
n = n | ((b - 128) << shift);
|
|
37
|
+
b = buf[outpos];
|
|
38
|
+
outpos++;
|
|
39
|
+
shift += 7;
|
|
40
|
+
}
|
|
41
|
+
return [n + (b << shift), outpos];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Write a buffer with a variable uint encoding of a number.
|
|
45
|
+
function encode_uint(n) {
|
|
46
|
+
let result = [];
|
|
47
|
+
while (n > 127) {
|
|
48
|
+
result.push(128 | (n & 127));
|
|
49
|
+
n = n >> 7;
|
|
50
|
+
}
|
|
51
|
+
result.push(n);
|
|
52
|
+
return new Uint8Array(result);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function ab2str(buf) {
|
|
56
|
+
let str = '';
|
|
57
|
+
let bytes = new Uint8Array(buf);
|
|
58
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
59
|
+
str += String.fromCharCode(bytes[i]);
|
|
60
|
+
}
|
|
61
|
+
return str;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function str2ab(str) {
|
|
65
|
+
let bytes = new Uint8Array(str.length);
|
|
66
|
+
for (let i = 0; i < str.length; i++) {
|
|
67
|
+
bytes[i] = str[i].charCodeAt(0);
|
|
68
|
+
}
|
|
69
|
+
return bytes;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Construct an array buffer representing a WASM 0-id
|
|
74
|
+
* sections containing a given name and value pair.
|
|
75
|
+
* @param {String} name
|
|
76
|
+
* @param {String} value
|
|
77
|
+
* @returns {Uint8Array}
|
|
78
|
+
*/
|
|
79
|
+
function WriteSection(name, value, valLenOverride) {
|
|
80
|
+
const nameBuf = str2ab(name);
|
|
81
|
+
const valBuf = str2ab(value);
|
|
82
|
+
const nameLen = encode_uint(nameBuf.length);
|
|
83
|
+
const valLen = encode_uint(valLenOverride ?? valBuf.length);
|
|
84
|
+
const sectionLenDeclared =
|
|
85
|
+
nameLen.length +
|
|
86
|
+
nameBuf.length +
|
|
87
|
+
valLen.length +
|
|
88
|
+
(valLenOverride ?? valBuf.length);
|
|
89
|
+
const sectionLenActual =
|
|
90
|
+
nameLen.length + nameBuf.length + valLen.length + valBuf.length;
|
|
91
|
+
const headerLen = encode_uint(sectionLenDeclared);
|
|
92
|
+
let bytes = new Uint8Array(sectionLenActual + headerLen.length + 1);
|
|
93
|
+
let pos = 1;
|
|
94
|
+
bytes.set(headerLen, pos);
|
|
95
|
+
pos += headerLen.length;
|
|
96
|
+
bytes.set(nameLen, pos);
|
|
97
|
+
pos += nameLen.length;
|
|
98
|
+
bytes.set(nameBuf, pos);
|
|
99
|
+
pos += nameBuf.length;
|
|
100
|
+
const val_start = pos;
|
|
101
|
+
bytes.set(valLen, pos);
|
|
102
|
+
pos += valLen.length;
|
|
103
|
+
bytes.set(valBuf, pos);
|
|
104
|
+
return bytes;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Search the module sections of a WASM buffer to find
|
|
109
|
+
* a section with a given identifier.
|
|
110
|
+
* @param {Buffer} buf
|
|
111
|
+
* @param {String} id
|
|
112
|
+
* @returns {Array.<Number>} An array with the index of
|
|
113
|
+
* the section, the length of the section, and the index
|
|
114
|
+
* of the beginning of the body of the section.
|
|
115
|
+
*/
|
|
116
|
+
function FindSection(buf, id) {
|
|
117
|
+
let pos = 8;
|
|
118
|
+
while (pos < buf.byteLength) {
|
|
119
|
+
const sec_start = pos;
|
|
120
|
+
const [sec_id, pos2] = read_uint(buf, pos);
|
|
121
|
+
const [sec_size, body_pos] = read_uint(buf, pos2);
|
|
122
|
+
pos = body_pos + sec_size;
|
|
123
|
+
if (sec_id == 0) {
|
|
124
|
+
const [name_len, name_pos] = read_uint(buf, body_pos);
|
|
125
|
+
const name = buf.slice(name_pos, name_pos + name_len);
|
|
126
|
+
const nameString = ab2str(name);
|
|
127
|
+
if (nameString == id) {
|
|
128
|
+
return [
|
|
129
|
+
sec_start,
|
|
130
|
+
sec_size + 1 + (body_pos - pos2),
|
|
131
|
+
name_pos + name_len,
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return [-1, null, null];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = {
|
|
140
|
+
/**
|
|
141
|
+
* GetSourceMapURL extracts the source map from a WASM buffer.
|
|
142
|
+
* @param {Buffer} buf The WASM buffer
|
|
143
|
+
* @returns {String|null} The linked sourcemap URL if present.
|
|
144
|
+
*/
|
|
145
|
+
GetSourceMapURL: function (buf) {
|
|
146
|
+
buf = new Uint8Array(buf);
|
|
147
|
+
const [sec_start, _, uri_start] = FindSection(buf, section);
|
|
148
|
+
if (sec_start == -1) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const [uri_len, uri_pos] = read_uint(buf, uri_start);
|
|
152
|
+
return ab2str(buf.slice(uri_pos, uri_pos + uri_len));
|
|
153
|
+
},
|
|
154
|
+
RemoveSourceMapURL: function (buf) {
|
|
155
|
+
buf = new Uint8Array(buf);
|
|
156
|
+
const [sec_start, sec_size, _] = FindSection(buf, section);
|
|
157
|
+
if (sec_start == -1) {
|
|
158
|
+
return buf;
|
|
159
|
+
}
|
|
160
|
+
let strippedBuf = new Uint8Array(buf.length - sec_size);
|
|
161
|
+
strippedBuf.set(buf.slice(0, sec_start));
|
|
162
|
+
strippedBuf.set(buf.slice(sec_start + sec_size), sec_start);
|
|
163
|
+
|
|
164
|
+
return strippedBuf;
|
|
165
|
+
},
|
|
166
|
+
SetSourceMapURL: function (buf, url, urlLenOverride) {
|
|
167
|
+
const stripped = module.exports.RemoveSourceMapURL(buf);
|
|
168
|
+
const newSection = WriteSection(section, url, urlLenOverride);
|
|
169
|
+
|
|
170
|
+
const outBuf = new Uint8Array(stripped.length + newSection.length);
|
|
171
|
+
outBuf.set(stripped);
|
|
172
|
+
outBuf.set(newSection, stripped.length);
|
|
173
|
+
|
|
174
|
+
return outBuf;
|
|
175
|
+
},
|
|
176
|
+
SetSourceMapURLRelativeTo: function (buf, relativeURL) {
|
|
177
|
+
const originalURL = module.exports.GetSourceMapURL(buf);
|
|
178
|
+
const newURL = url.resolve(relativeURL, originalURL);
|
|
179
|
+
return module.exports.SetSourceMapURL(buf, newURL);
|
|
180
|
+
},
|
|
181
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
declare module.exports: {|
|
|
4
|
+
/**
|
|
5
|
+
* GetSourceMapURL extracts the source map from a WASM buffer.
|
|
6
|
+
* @returns {String|null} The linked sourcemap URL if present.
|
|
7
|
+
*/
|
|
8
|
+
GetSourceMapURL(buf: Uint8Array): string | null,
|
|
9
|
+
RemoveSourceMapURL(buf: Uint8Array): Uint8Array,
|
|
10
|
+
SetSourceMapURL(
|
|
11
|
+
buf: Uint8Array,
|
|
12
|
+
url: string,
|
|
13
|
+
urlLenOverride?: number,
|
|
14
|
+
): Uint8Array,
|
|
15
|
+
SetSourceMapURLRelativeTo(buf: Uint8Array, relativeURL: string): Uint8Array,
|
|
16
|
+
|};
|