49 lines
1.8 KiB
TypeScript
49 lines
1.8 KiB
TypeScript
import { Actor, Ref, Turn } from "./actor";
|
|
import { Relay, spawnRelay } from "./relay";
|
|
import { sturdyDecode } from "./sturdy";
|
|
import { Resolve, asSturdyRef, fromResolve } 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' }, () => {
|
|
new Actor(t => {
|
|
let shutdownRef: Ref;
|
|
const reenable = t.activeFacet.preventInertCheck();
|
|
const connectionClosedRef = t.ref({
|
|
retract(t) { t.stopActor(); },
|
|
});
|
|
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.stopActor() : t.crash(err)));
|
|
socket.on('close', () => t.freshen(t => t.stopActor()));
|
|
socket.on('end', () => t.freshen(t => t.stopActor()));
|
|
socket.on('data', data => r.accept(data));
|
|
t.activeFacet.actor.atExit(() => socket.destroy());
|
|
t.assert(connectionClosedRef, true);
|
|
shutdownRef = t.ref({
|
|
retract(t) { t.stopActor(); }
|
|
});
|
|
},
|
|
initialOid: 0,
|
|
// debug: true,
|
|
}).then(gatekeeper => import(moduleName).then(m => t.freshen(t => {
|
|
reenable();
|
|
t.assert(shutdownRef, true);
|
|
t.assert(gatekeeper, fromResolve(Resolve({
|
|
sturdyref: asSturdyRef(cap),
|
|
observer: t.ref({
|
|
assert(t, ds) {
|
|
m.default(t, ds);
|
|
}
|
|
})
|
|
})));
|
|
})));
|
|
});
|
|
});
|