Skip to content

Commit a00fa8e

Browse files
authored
Implement Scratch.external - extensions part (TurboWarp#2267)
5 new APIs: * Scratch.external.importModule() is a wrapper around import() that gets inlined at build time * Scratch.external.fetch() is a wrapper around fetch() that gets inlined at build time * Scratch.external.dataURL() is like external.fetch() but it gives you a base64 data: URL * Scratch.external.blob() is like external.fetch() but it gives you a Blob * Scratch.external.evalAndReturn() is for executing scripts inside of a function and then returning an expression of your choice In development, all of these call directly into VM implementation which does actually go and fetch each time the extension runs. In production, all of these are inlined in various ways so that everything is inside the JS file. All references to Scratch.external go away, so we're not breaking compatibility with anything. A new concept called "build snippets" are added into built JS as needed to implement things like base85 and zstd decompression. URLs should point to jsdelivr, unpkg, raw.githubusercontent.com, etc. In development mode, new URLs will be found and fetched so their SHA-256 can be stored. In production builds, these resources get fetched and the hashes are checked to ensure no tampering. Must use an immutable URL pinned to an npm tag or commit hash. Face sensing has been updated to use these as a first example. Ideally this API would be called Scratch.import, but using "import" verbatim causes various issues in TypeScript and elsewhere so we're avoiding that. Hence, "Scratch.external" and "importModule"
1 parent c08a1f8 commit a00fa8e

17 files changed

Lines changed: 1626 additions & 101 deletions

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# npm
1+
# Dependencies
22
node_modules
3+
cached-extension-dependencies
34

45
# Final built website and localizations
56
build

‎.prettierignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ website
66
*.yml
77
*.md
88
extensions/docs-examples
9+
build-snippets

‎build-snippets/base85decode.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
__internal.base85decode = (function () {
2+
const getBase85DecodeValue = (code) => {
3+
if (code === 0x28) code = 0x5c;
4+
return code - 0x2a;
5+
};
6+
const base85decode = (str, resultByteLength) => {
7+
const outBuffer = new ArrayBuffer(Math.ceil(resultByteLength / 4) * 4);
8+
const view = new DataView(outBuffer, 0, outBuffer.byteLength);
9+
for (let i = 0, j = 0; i < str.length; i += 5, j += 4) {
10+
view.setUint32(
11+
j,
12+
getBase85DecodeValue(str.charCodeAt(i + 4)) * 85 * 85 * 85 * 85 +
13+
getBase85DecodeValue(str.charCodeAt(i + 3)) * 85 * 85 * 85 +
14+
getBase85DecodeValue(str.charCodeAt(i + 2)) * 85 * 85 +
15+
getBase85DecodeValue(str.charCodeAt(i + 1)) * 85 +
16+
getBase85DecodeValue(str.charCodeAt(i)),
17+
true
18+
);
19+
}
20+
return new Uint8Array(outBuffer, 0, resultByteLength);
21+
};
22+
return base85decode;
23+
}());

0 commit comments

Comments
 (0)