Skip to content

Commit

Permalink
Support webassembly instantiate streaming (#431)
Browse files Browse the repository at this point in the history
* Allow loadPolicy to accept a PromiseLike<Response> or Response to be able to use WebAssembly.instantiateStreaming over WebAssembly.instantiate internally

Signed-off-by: Eric Tucker <[email protected]>

* Only accept promise instead of Promise for streaming

Signed-off-by: Eric Tucker <[email protected]>
  • Loading branch information
Tucker-Eric authored Jun 8, 2024
1 parent f94e758 commit c02cf13
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/opa.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,23 @@ function _preparePolicy(env, wasm, memory) {
* It will return a Promise, depending on the input type the promise
* resolves to both a compiled WebAssembly.Module and its first WebAssembly.Instance
* or to the WebAssemblyInstance.
* @param {BufferSource | WebAssembly.Module} policyWasm
* @param {BufferSource | WebAssembly.Module | Response | Promise<Response>} policyWasm
* @param {WebAssembly.Memory} memory
* @param {{ [builtinName: string]: Function }} customBuiltins
* @returns {Promise<{ policy: WebAssembly.WebAssemblyInstantiatedSource | WebAssembly.Instance, minorVersion: number }>}
*/
async function _loadPolicy(policyWasm, memory, customBuiltins) {
const env = {};

const wasm = await WebAssembly.instantiate(
policyWasm,
_importObject(env, memory, customBuiltins),
);
const isStreaming = policyWasm instanceof Response ||
policyWasm instanceof Promise;

const importObject = _importObject(env, memory, customBuiltins);

const wasm =
await (isStreaming
? WebAssembly.instantiateStreaming(policyWasm, importObject)
: WebAssembly.instantiate(policyWasm, importObject));

return _preparePolicy(env, wasm, memory);
}
Expand Down Expand Up @@ -460,7 +465,7 @@ module.exports = {
* To set custom memory size specify number of memory pages
* as second param.
* Defaults to 5 pages (320KB).
* @param {BufferSource | WebAssembly.Module} regoWasm
* @param {BufferSource | WebAssembly.Module | Response | Promise<Response>} regoWasm
* @param {number | WebAssembly.MemoryDescriptor} memoryDescriptor For backwards-compatibility, a 'number' argument is taken to be the initial memory size.
* @param {{ [builtinName: string]: Function }} customBuiltins A map from string names to builtin functions
* @returns {Promise<LoadedPolicy>}
Expand Down
16 changes: 16 additions & 0 deletions test/browser-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ test("esm script should expose working opa module", async () => {
]);
});

test("loadPolicy should allow for a response object that resolves to a fetched wasm module", async () => {
const result = await page.evaluate(async function () {
// NOTE: Paths are evaluated relative to the project root.
const { default: opa } = await import("/dist/opa-wasm-browser.esm.js");
const policy = await opa.loadPolicy(
fetch("/test/fixtures/multiple-entrypoints/policy.wasm"),
);
return policy.evaluate({}, "example/one");
});
expect(result).toEqual([
{
result: { myOtherRule: false, myRule: false },
},
]);
});

test("default script should expose working opa global", async () => {
// Load module into global scope.
const script = fs.readFileSync(
Expand Down

0 comments on commit c02cf13

Please sign in to comment.