Skip to content

Commit

Permalink
chore: Trying to fix the pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
FranklinWaller committed Oct 3, 2023
1 parent cc6311e commit 41a7314
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 21 deletions.
5 changes: 3 additions & 2 deletions libs/as-sdk-integration-tests/src/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { callVm, } from '../../../dist/libs/vm';
import { callVm } from '../../../dist/libs/vm';
import { jest } from '@jest/globals';
import { readFile } from 'node:fs/promises';
import { HttpFetchResponse } from '../../../dist/libs/vm/src/types/vm-actions';
import { PromiseStatus } from '../../../dist/libs/vm/src/types/vm-promise';

const mockHttpFetch = jest.fn();

jest.setTimeout(30_000);

const TestVmAdapter = jest.fn().mockImplementation(() => {
return { httpFetch: mockHttpFetch };
});

describe('Http', () => {
it('Test SDK HTTP Rejection', async () => {

const wasmBinary = await readFile('dist/libs/as-sdk-integration-tests/debug.wasm');
const result = await callVm({
args: ['testHttpRejection'],
Expand Down
45 changes: 36 additions & 9 deletions libs/vm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { VmAdapter } from "./types/vm-adapter.js";
import DefaultVmAdapter from "./default-vm-adapter.js";
import { parse, format } from "node:path";
import { HostToWorker } from "./worker-host-communication.js";
import { createCallDataId } from "./utils.js";

const CURRENT_FILE_PATH = parse(import.meta.url);
CURRENT_FILE_PATH.base = 'worker.js';
Expand All @@ -25,18 +26,44 @@ export function callVm(callData: VmCallData, workerUrl = DEFAULT_WORKER_PATH, vm
type: WorkerMessageType.VmCall,
};

worker.on('message', (message: WorkerMessage) => {
if (message.type === WorkerMessageType.VmResult) {
resolve(message.result);
} else if (message.type === WorkerMessageType.VmActionExecute) {
hostToWorker.executeAction(message.action);
} else if (message.type === WorkerMessageType.VmActionResultBuffer) {
hostToWorker.sendActionResultToWorker(message.buffer);
} else {
console.warn('Unknown message', message);
setInterval(() => {
console.error("hey it has a worker", worker.threadId);
}, 2000);

worker.on('message', async (message: WorkerMessage) => {
try {
console.error('@message: ', message, createCallDataId(callData));
if (message.type === WorkerMessageType.VmResult) {
worker.terminate();
resolve(message.result);
} else if (message.type === WorkerMessageType.VmActionExecute) {
await hostToWorker.executeAction(message.action);
} else if (message.type === WorkerMessageType.VmActionResultBuffer) {
await hostToWorker.sendActionResultToWorker(message.buffer);
} else {
console.warn('Unknown message', message);
}
} catch (error) {
console.error('@callVm-onMessage: ', error, createCallDataId(callData));
}
});

worker.on('error', (error) => {
resolve({
exitCode: 1,
stderr: 'Worker threw an uncaught error: ' + error,
stdout: '',
});
});

worker.on('exit', (exitCode) => {
resolve({
exitCode,
stderr: 'The worker has been terminated',
stdout: '',
})
});

worker.postMessage(workerMessage);
});
}
11 changes: 11 additions & 0 deletions libs/vm/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createHash } from "node:crypto";
import type { VmCallData } from "./vm";

export function createCallDataId(callData: VmCallData): string {
const hasher = createHash('sha256');
const binary = new Uint8Array(callData.binary);
hasher.update(binary);
const binaryId = hasher.digest().toString('hex');

return `${callData.args.join('-')}-${binaryId}`;
}
4 changes: 4 additions & 0 deletions libs/vm/src/vm-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ export default class VmImports {
}

httpFetch(action: number, actionLength: number) {
console.log("HTTP Action called");
const rawAction = new Uint8Array(
this.memory?.buffer.slice(action, action + actionLength) ?? []
);
const messageRaw = Buffer.from(rawAction).toString('utf-8');

console.log('HTTP Action called with ', messageRaw);

try {
const message: HttpFetchAction = JSON.parse(messageRaw);
this.callResult = this.workerToHost.callActionOnHost(message);
console.log('We got a result: ', this.callResult);
return this.callResult.length;
} catch (error) {
console.error(`@httpFetch: ${messageRaw}`, error);
Expand Down
2 changes: 2 additions & 0 deletions libs/vm/src/vm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { PromiseStatus } from "../../../dist/libs/vm/src/types/vm-promise";

const mockHttpFetch = jest.fn();

jest.setTimeout(10_000);

const TestVmAdapter = jest.fn().mockImplementation(() => {
return { httpFetch: mockHttpFetch };
});
Expand Down
5 changes: 5 additions & 0 deletions libs/vm/src/vm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { init, WASI } from '@wasmer/wasi';
import VmImports from './vm-imports.js';
import { createCallDataId } from './utils.js';

export interface VmCallData {
binary: Uint8Array | number[];
Expand Down Expand Up @@ -34,7 +35,9 @@ export async function executeVm(callData: VmCallData, notifierBuffer: SharedArra
const memory = instance.exports.memory;
vmImports.setMemory(memory as WebAssembly.Memory);

console.log("Starting WASI", createCallDataId(callData));
const exitCode = wasi.start(instance);
console.log("exit: ", exitCode, createCallDataId(callData));

return {
exitCode,
Expand All @@ -44,6 +47,7 @@ export async function executeVm(callData: VmCallData, notifierBuffer: SharedArra
resultAsString: new TextDecoder().decode(vmImports.result),
}
} catch (err) {
console.log('Exception thrown', err, createCallDataId(callData));
console.error(`
@executeWasm
Exception threw: ${err}
Expand All @@ -62,3 +66,4 @@ export async function executeVm(callData: VmCallData, notifierBuffer: SharedArra
};
}
}

7 changes: 6 additions & 1 deletion libs/vm/src/worker-host-communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class HostToWorker {
const notifierBufferi32 = new Int32Array(this.notifierBuffer);
notifierBufferi32.set([this.actionResult.length], 1);

console.error("setted buffer, ready to send");

updateNotifierState(notifierBufferi32, AtomicState.ResponseResultLength);
}

Expand All @@ -88,7 +90,7 @@ export class HostToWorker {
}

export class WorkerToHost {
constructor(private notifierBuffer: SharedArrayBuffer) {}
constructor(private notifierBuffer: SharedArrayBuffer, private id: string = '') {}

/**
* Calls the given action on the host machine and sleeps the thread until an answer has been received
Expand All @@ -112,6 +114,8 @@ export class WorkerToHost {

const length = notifierBufferi32[1];

console.error("The length is: ", length, this.id);

// No need to do a full roundtrip if there are no bytes
if (length === 0) {
return Buffer.from([]);
Expand All @@ -123,6 +127,7 @@ export class WorkerToHost {
type: WorkerMessageType.VmActionResultBuffer,
};

console.error('Requesting message: ', message, this.id);
parentPort?.postMessage(message);

waitForNotifierStateChange(notifierBufferi32, AtomicState.RequestResult);
Expand Down
20 changes: 12 additions & 8 deletions libs/vm/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import {
} from './types/worker-messages.js';

parentPort?.on('message', async function (event) {
const message: WorkerMessage = event;
try {
const message: WorkerMessage = event;

if (message.type === WorkerMessageType.VmCall) {
const result = await executeVm(message.callData, message.notifierBuffer);
const response: VmResultWorkerMessage = {
result,
type: WorkerMessageType.VmResult,
};
if (message.type === WorkerMessageType.VmCall) {
const result = await executeVm(message.callData, message.notifierBuffer);
const response: VmResultWorkerMessage = {
result,
type: WorkerMessageType.VmResult,
};

parentPort?.postMessage(response);
parentPort?.postMessage(response);
}
} catch (error) {
console.error("@worker:message, error thrown: ", error);
}
});
9 changes: 8 additions & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"dotenv": "^16.3.1",
"figlet": "^1.6.0",
"node-gzip": "^1.1.2",
"tslib": "^2.3.0",
"ora": "^7.0.1",
"protobufjs": "^7.2.5"
}
Expand Down

0 comments on commit 41a7314

Please sign in to comment.