novy-syndicate/src/sandbox.ts

40 lines
1.5 KiB
TypeScript

import { Actor, Ref, Turn } from "./actor";
import { Relay, spawnRelay } from "./relay";
import { sturdyDecode } from "./sturdy";
import { Resolve, asSturdyRef } from "./gen/sturdy";
import * as net from 'net';
import { Bytes } from "@preserves/core";
const [ moduleName, hexCap ] = process.argv.slice(2);
const cap = sturdyDecode(Bytes.fromHex(hexCap ?? ''));
const socket = net.createConnection({ port: 5999, host: 'localhost' }, () => {
Turn.for(new Actor(), t => {
let shutdownRef: Ref;
spawnRelay(t, {
packetWriter: bs => socket.write(bs),
setup(t: Turn, r: Relay) {
socket.on('error', err => t.freshen(t =>
((err as any).code === 'ECONNRESET') ? t.quit() : t.crash(err)));
socket.on('close', () => t.freshen(t => t.quit()));
socket.on('end', () => t.freshen(t => t.quit()));
socket.on('data', data => r.accept(data));
t.actor.atExit(() => socket.destroy());
shutdownRef = t.ref({
retract(t) { t.quit(); }
});
},
initialOid: 0,
// debug: true,
}).then(gatekeeper => import(moduleName).then(m => t.freshen(t => {
t.assert(shutdownRef, true);
t.assert(gatekeeper, Resolve(asSturdyRef(cap), t.ref({
assert(t, ds) {
m.default(t, ds);
}
})));
})));
});
});