novy-syndicate/src/sandbox.ts

32 lines
1.1 KiB
TypeScript

import { Actor, Ref, Turn } from "./actor.js";
import { Relay, spawnRelay } from "./relay.js";
import * as net from 'net';
const [ moduleName ] = process.argv.slice(2);
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(ds => import(moduleName).then(m => t.freshen(t => {
t.assert(shutdownRef, true);
m.default(t, ds);
})));
});
});