2021-04-21 19:22:30 +00:00
|
|
|
import { Actor, Ref, Turn } from "../runtime/actor";
|
|
|
|
import { Relay, spawnRelay } from "../transport/relay";
|
|
|
|
import { sturdyDecode } from "../transport/sturdy";
|
2021-04-24 22:15:15 +00:00
|
|
|
import { asSturdyRef } from "../gen/sturdy";
|
|
|
|
import { Resolve, fromResolve } from "../gen/gatekeeper";
|
2021-04-21 19:22:30 +00:00
|
|
|
import { during } from "../runtime/dataspace";
|
2021-03-04 10:29:37 +00:00
|
|
|
|
|
|
|
import * as net from 'net';
|
2021-03-10 22:49:34 +00:00
|
|
|
import { Bytes } from "@preserves/core";
|
2021-03-04 10:29:37 +00:00
|
|
|
|
2021-03-04 21:08:49 +00:00
|
|
|
const [ moduleName, hexCap ] = process.argv.slice(2);
|
|
|
|
const cap = sturdyDecode(Bytes.fromHex(hexCap ?? ''));
|
2021-03-04 10:29:37 +00:00
|
|
|
|
|
|
|
const socket = net.createConnection({ port: 5999, host: 'localhost' }, () => {
|
2021-04-16 18:29:16 +00:00
|
|
|
new Actor(t => {
|
2021-03-04 10:29:37 +00:00
|
|
|
let shutdownRef: Ref;
|
2021-04-16 18:29:16 +00:00
|
|
|
const reenable = t.activeFacet.preventInertCheck();
|
|
|
|
const connectionClosedRef = t.ref({
|
|
|
|
retract(t) { t.stopActor(); },
|
|
|
|
});
|
2021-03-04 10:29:37 +00:00
|
|
|
spawnRelay(t, {
|
|
|
|
packetWriter: bs => socket.write(bs),
|
|
|
|
setup(t: Turn, r: Relay) {
|
|
|
|
socket.on('error', err => t.freshen(t =>
|
2021-04-16 18:29:16 +00:00
|
|
|
((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()));
|
2021-03-04 10:29:37 +00:00
|
|
|
socket.on('data', data => r.accept(data));
|
2021-04-16 18:29:16 +00:00
|
|
|
t.activeFacet.actor.atExit(() => socket.destroy());
|
|
|
|
t.assert(connectionClosedRef, true);
|
2021-03-04 10:29:37 +00:00
|
|
|
shutdownRef = t.ref({
|
2021-04-16 18:29:16 +00:00
|
|
|
retract(t) { t.stopActor(); }
|
2021-03-04 10:29:37 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
initialOid: 0,
|
|
|
|
// debug: true,
|
2021-03-04 21:08:49 +00:00
|
|
|
}).then(gatekeeper => import(moduleName).then(m => t.freshen(t => {
|
2021-04-16 18:29:16 +00:00
|
|
|
reenable();
|
2021-03-04 10:29:37 +00:00
|
|
|
t.assert(shutdownRef, true);
|
2021-03-23 18:18:26 +00:00
|
|
|
t.assert(gatekeeper, fromResolve(Resolve({
|
|
|
|
sturdyref: asSturdyRef(cap),
|
2021-04-19 11:00:08 +00:00
|
|
|
observer: t.ref(during(async (t, ds) => {
|
|
|
|
const facet = t.facet(t => m.default(t, ds));
|
|
|
|
return t => t.stop(facet);
|
|
|
|
})),
|
2021-03-23 18:18:26 +00:00
|
|
|
})));
|
2021-03-04 10:29:37 +00:00
|
|
|
})));
|
|
|
|
});
|
|
|
|
});
|