-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[POC] Use WebAssembly build of package-spec for package validation #128076
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
// Provides global.Go runtime | ||
import './wasm_exec'; | ||
// @ts-expect-error | ||
const go = new Go(); | ||
|
||
const wasmBuffer = fs.readFileSync(path.join(__dirname, 'validator.wasm')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Just curious, I see this module is about 12mb, I assume it's not optimized yet (with more aggressive build optimization flags, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep we still need to explore making this smaller and distribute it via a proper npm module. This is currently being built with go-wasm, but I think it would make sense to look at tinygo. |
||
/** | ||
* Validates a zip archive using the package-spec | ||
*/ | ||
export async function validateZipBufferWithPackageSpec( | ||
name: string, | ||
size: number, | ||
buffer: Uint8Array | ||
): Promise<void> { | ||
const validator = await WebAssembly.instantiate(wasmBuffer, go.importObject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: if it's called multiple times then it'd probably make sense to compile module just once and reuse it for instantiation (with |
||
go.run(validator.instance); | ||
|
||
try { | ||
// @ts-expect-error | ||
await global.elasticPackageSpec.validateFromZipReader(name, size, buffer); | ||
} finally { | ||
// @ts-expect-error | ||
global.elasticPackageSpec.stop(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: I'm not familiar with
wasm_exec.js
\tinygo, is it possible to use itsimportObject
without polluting the global scope withGo
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without editing the
wasm_exec.js
from Go (this is not from tinygo), I don't see a way. I think it would be a small edit though and I agree we shouldn't merge this in its current state (polluting the global scope).