Skip to content

Commit

Permalink
wip: test crappery
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored and kamilogorek committed Dec 3, 2020
1 parent 37cdf64 commit 26f4b20
Show file tree
Hide file tree
Showing 10 changed files with 650 additions and 69 deletions.
37 changes: 15 additions & 22 deletions packages/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,37 @@
"access": "public"
},
"dependencies": {
"@sentry/types": "5.27.6",
"@sentry/browser": "5.27.6",
"@sentry/types": "5.27.6",
"tslib": "^1.9.3"
},
"devDependencies": {
"@sentry-internal/eslint-config-sdk": "5.27.6",
"chai": "^4.1.2",
"@types/jest-environment-puppeteer": "^4.4.0",
"@types/puppeteer": "^5.4.0",
"eslint": "7.6.0",
"express": "^4.17.1",
"jest": "^24.7.1",
"jest-puppeteer": "^4.4.0",
"npm-run-all": "^4.1.2",
"prettier": "1.19.0",
"puppeteer": "^5.5.0",
"rimraf": "^2.6.3",
"rollup": "^1.10.1",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-node-resolve": "^4.2.3",
"rollup-plugin-terser": "^4.0.4",
"rollup-plugin-typescript2": "^0.21.0",
"typescript": "3.7.5"
},
"scripts": {
"build": "run-p build:es5 build:esm",
"build": "run-p build:es5 build:esm build:bundle",
"build:es5": "tsc -p tsconfig.build.json",
"build:esm": "tsc -p tsconfig.esm.json",
"build:watch": "run-p build:watch:es5 build:watch:esm",
"build:watch:es5": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
"build:watch:esm": "tsc -p tsconfig.esm.json -w --preserveWatchOutput",
"build:bundle": "rollup --config",
"clean": "rimraf dist esm coverage *.js *.js.map *.d.ts",
"link:yarn": "yarn link",
"lint": "run-s lint:prettier lint:eslint",
Expand All @@ -45,32 +55,15 @@
"fix": "run-s fix:eslint fix:prettier",
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
"fix:eslint": "eslint . --format stylish --fix",
"test": "jest",
"test": "PORT=1337 jest",
"test:watch": "jest --watch",
"pack": "npm pack"
},
"volta": {
"extends": "../../package.json"
},
"jest": {
"collectCoverage": true,
"transform": {
"^.+\\.ts$": "ts-jest"
},
"moduleFileExtensions": [
"js",
"ts"
],
"testEnvironment": "node",
"testMatch": [
"**/*.test.ts"
],
"globals": {
"ts-jest": {
"tsConfig": "./tsconfig.json",
"diagnostics": false
}
}
"preset": "jest-puppeteer"
},
"sideEffects": false
}
10 changes: 6 additions & 4 deletions packages/wasm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DebugImage, Hub, EventProcessor, Integration, StackFrame } from '@sentry/types';
import { DebugImage, EventProcessor, Hub, Integration, StackFrame } from '@sentry/types';

import { patchWebAssembly } from './patchWebAssembly';
import { getImage, getImages } from './registry';

Expand All @@ -9,9 +10,10 @@ export interface ModuleInfo {
debugFile: string | null;
}

/** plz don't */
function patchFrames(frames: Array<StackFrame>): boolean {
let haveWasm = false;
frames.forEach((frame) => {
frames.forEach(frame => {
if (!frame.filename) {
return;
}
Expand Down Expand Up @@ -53,11 +55,11 @@ export class Wasm implements Integration {
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, _getCurrentHub: () => Hub): void {
patchWebAssembly();

addGlobalEventProcessor((event) => {
addGlobalEventProcessor(event => {
let haveWasm = false;

if (event.exception && event.exception.values) {
event.exception.values.forEach((exception) => {
event.exception.values.forEach(exception => {
if (exception?.stacktrace?.frames) {
haveWasm = haveWasm || patchFrames(exception.stacktrace.frames);
}
Expand Down
51 changes: 28 additions & 23 deletions packages/wasm/src/patchWebAssembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@ import { registerModule } from './registry';
/**
* Patches the web assembly runtime.
*/
export function patchWebAssembly() {
const origInstantiateStreaming = WebAssembly.instantiateStreaming;
const origCompileStreaming = WebAssembly.compileStreaming;

function recordedInstanticateStreaming(promise: Promise<any>, obj: any) {
return Promise.resolve(promise).then(resp => {
return origInstantiateStreaming(resp, obj).then(rv => {
if (resp.url) {
registerModule(rv.module, resp.url);
}
return rv;
export function patchWebAssembly(): void {
if ('instantiateStreaming' in WebAssembly) {
const origInstantiateStreaming = WebAssembly.instantiateStreaming;
WebAssembly.instantiateStreaming = function instantiateStreaming(
response: Response | PromiseLike<Response>,
importObject: WebAssembly.Imports,
): Promise<WebAssembly.Module> {
return Promise.resolve(response).then(response => {
return origInstantiateStreaming(response, importObject).then(rv => {
if (response.url) {
registerModule(rv.module, response.url);
}
return rv;
});
});
});
} as typeof WebAssembly.instantiateStreaming;
}

function recordedCompileStreaming(promise: Promise<any>) {
return Promise.resolve(promise).then(resp => {
return origCompileStreaming(resp).then(module => {
if (resp.url) {
registerModule(module, resp.url);
}
return module;
if ('compileStreaming' in WebAssembly) {
const origCompileStreaming = WebAssembly.compileStreaming;
WebAssembly.compileStreaming = function compileStreaming(
source: Response | Promise<Response>,
): Promise<WebAssembly.Module> {
return Promise.resolve(source).then(response => {
return origCompileStreaming(response).then(module => {
if (response.url) {
registerModule(module, response.url);
}
return module;
});
});
});
} as typeof WebAssembly.compileStreaming;
}

(WebAssembly as any).instantiateStreaming = recordedInstanticateStreaming;
(WebAssembly as any).compileStreaming = recordedCompileStreaming;
}
10 changes: 5 additions & 5 deletions packages/wasm/src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DebugImage } from '@sentry/types';
import { ModuleInfo } from './index';
import { IMAGES } from './index';

import { IMAGES, ModuleInfo } from './index';

/**
* Returns the extracted meta information from a web assembly module that
Expand Down Expand Up @@ -33,7 +33,7 @@ export function getModuleInfo(module: WebAssembly.Module): ModuleInfo {
/**
* Records a module
*/
export function registerModule(module: WebAssembly.Module, url: string) {
export function registerModule(module: WebAssembly.Module, url: string): void {
const { buildId, debugFile } = getModuleInfo(module);
if (buildId) {
const oldIdx = IMAGES.findIndex(img => img.code_file === url);
Expand All @@ -45,7 +45,7 @@ export function registerModule(module: WebAssembly.Module, url: string) {
code_id: buildId,
code_file: url,
debug_file: debugFile ? new URL(debugFile, url).href : null,
debug_id: buildId.padEnd(32, '0').substr(0, 32) + '0',
debug_id: `${buildId.padEnd(32, '0').substr(0, 32)}0`,
});
}
}
Expand All @@ -62,6 +62,6 @@ export function getImages(): Array<DebugImage> {
*
* @param url the URL of the WebAssembly module.
*/
export function getImage(url: string) {
export function getImage(url: string): number {
return IMAGES.findIndex(img => img.code_file === url);
}
36 changes: 36 additions & 0 deletions packages/wasm/test/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<!-- Browser SDK Bundle -->
<script src="bundle.js"></script>
<!-- Wasm Integration Bundle -->
<script src="wasm.js"></script>
<script>
Sentry.init({
dsn: 'https://1337@sentry.io/42',
integrations: [new Sentry.Integrations.Wasm()],
beforeSend: event => {
window.events.push(event);
return null;
},
});

window.events = [];

window.getEvent = async () => {
function crash() {
throw new Error('whoops');
}

const { instance } = await WebAssembly.instantiateStreaming(fetch('simple.wasm'), {
env: {
external_func: crash,
},
});

try {
instance.exports.internal_func();
} catch (err) {
Sentry.captureException(err);
return window.events.pop();
}
};
</script>
Binary file added packages/wasm/test/public/simple.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/wasm/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declarationMap": false,
"baseUrl": ".",
"outDir": "dist",
"rootDir": "src"
"outDir": "dist"
},
"include": ["src/**/*.ts"]
}
1 change: 1 addition & 0 deletions packages/wasm/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../../tsconfig.esm.json",
"compilerOptions": {
"declarationMap": false,
"baseUrl": ".",
"outDir": "esm"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/wasm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"include": ["src/**/*.ts", "test/**/*.ts"],
"exclude": ["dist"],
"compilerOptions": {
"esModuleInterop": true,
"declarationMap": false,
"rootDir": ".",
"types": ["node", "jest"]
"types": ["node", "jest", "jest-environment-puppeteer"]
}
}
Loading

0 comments on commit 26f4b20

Please sign in to comment.