Skip to content

Commit

Permalink
feat: allow loading preloaded wasm/data, adding targets for creating …
Browse files Browse the repository at this point in the history
…compressed wasm/data
  • Loading branch information
dehydr8 committed Feb 23, 2023
1 parent c32c33b commit 113a6c3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
20 changes: 16 additions & 4 deletions lib/wiregasm/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,25 @@ out = executable(
)

# the data file doesn't get recognized currently
# as an output by meson, this is a hack for installing
# it in the datadir
# as an output by meson, this is a hack for installing it
custom_target(
'fake target for installing .data',
input: out,
output: ['wiregasm.data'],
command: [ find_program('true') ],
install: true,
install_dir: join_paths(get_option('datadir'), 'wiregasm')
)
install_dir: get_option('bindir')
)

# create gzipped versions of .data and .wasm
gzip = find_program('gzip', required: true)

foreach tgt: [ 'wiregasm.wasm', 'wiregasm.data' ]
custom_target('gz-' + tgt,
command: [ gzip, '--keep', '--force', tgt ],
input: out,
output: tgt + '.gz',
install: true,
install_dir: get_option('bindir'),
)
endforeach
24 changes: 19 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build:emscripten": "docker run -it --rm -v $(pwd):/src okhalid/wiregasm-builder:latest ./build-lib.sh",
"build:emscripten-local": "./build-lib.sh",
"build": "parcel build && npm run copy-assets",
"copy-assets": "cp built/bin/wiregasm.wasm built/bin/wiregasm.js built/share/wiregasm/wiregasm.data dist/",
"copy-assets": "cp built/bin/wiregasm.* dist/",
"lint": "eslint src/ --fix",
"test": "jest --coverage",
"fix:prettier": "prettier \"src/**/*.ts\" --write"
Expand All @@ -35,12 +35,14 @@
"@semantic-release/git": "^10.0.1",
"@types/jest": "^29.2.5",
"@types/node": "^18.11.18",
"@types/pako": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.3.1",
"pako": "^2.1.0",
"parcel": "^2.8.2",
"prettier": "^2.8.2",
"ts-jest": "^29.0.3"
Expand Down
49 changes: 48 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Wiregasm, WiregasmLib, WiregasmLibOverrides } from ".";
import loadWiregasm from "../built/bin/wiregasm.js";
import * as fs from "fs/promises";
import pako from "pako";

// overrides need to be copied over to every instance
const buildTestOverrides = (): WiregasmLibOverrides => {
return {
locateFile: (path, prefix) => {
if (path.endsWith(".data")) return "built/share/wiregasm/" + path;
if (path.endsWith(".data")) return "built/bin/" + path;
return prefix + path;
},
// supress all unwanted logs in test-suite
Expand Down Expand Up @@ -129,3 +130,49 @@ describe("Wiregasm Library Wrapper", () => {
expect(wg.test_filter("txx").ok).toBeFalsy();
});
});

const buildCompressedOverrides = async (): Promise<WiregasmLibOverrides> => {
const wasm = pako.inflate(await fs.readFile("built/bin/wiregasm.wasm.gz"));
const data = pako.inflate(await fs.readFile("built/bin/wiregasm.data.gz"));

return {
wasmBinary: wasm.buffer,

// eslint-disable-next-line @typescript-eslint/no-unused-vars
getPreloadedPackage(_name: string, _size: number): ArrayBuffer {
return data.buffer;
},

// eslint-disable-next-line @typescript-eslint/no-empty-function
printErr: () => {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
print: () => {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
handleStatus: () => {},
};
};

describe("Wiregasm Library - Compressed Loading", () => {
const wg = new Wiregasm();

beforeAll(async () => {
return wg.init(loadWiregasm, await buildCompressedOverrides());
});

afterAll(() => {
wg.destroy();
});

test("columns array returned correctly", async () => {
const cols = wg.columns();
expect(cols).toEqual([
"No.",
"Time",
"Source",
"Destination",
"Protocol",
"Length",
"Info",
]);
});
});
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ export interface WiregasmLibOverrides {
* @param message Message content
*/
handleStatus?(type: number, message: string): void;

/**
* If you can fetch the binary yourself, you can set it
*/
wasmBinary?: ArrayBuffer;

/**
* If you want to manually manage the download of .data file packages for
* custom caching, progress reporting and error handling behavior,
* you can implement this override.
*/
getPreloadedPackage?(name: string, size: number): ArrayBuffer;
}

export interface WiregasmLib extends EmscriptenModule {
Expand Down

0 comments on commit 113a6c3

Please sign in to comment.