1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 | /**
* Loads the REAL TypeScript implementation into the vector generators.
*
* dev/docs/CLJS.md's method: "splice the real modules into a harness rather
* than transcribing their logic into a generator" — it removes the whole class
* of "my fixture encodes my misreading". Here the real modules are `src/**.ts`,
* so esbuild bundles them once (single module graph => one copy of every shared
* module, one class identity) with every npm package left external, and the
* Vite-only globals replaced by the values the production build injects.
*
* Hard-private members (`#name`) are aliased onto a public `__t` bag by the same
* mechanical class-field injection rooms-kit and chat use: not one line of logic
* is retyped. Values in EXPOSE are the accessor bodies, so a private FIELD can
* be read as well as a private method called.
*
* Only usable while the TypeScript is still in the tree, and only before
* package.json is swapped to the CLJS shape (the CLJS install drops esbuild).
*
* Canon: chat/client/test/harness/ts.mjs.
*/
import { execFileSync } from "node:child_process";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const HERE = dirname(fileURLToPath(import.meta.url));
export const CLIENT = join(HERE, "..", "..");
const STAGE = join(CLIENT, "node_modules", ".board-ts");
/** Everything the generators need, re-exported from one entry. */
const ENTRY = `
export * from "../../src/config";
export * from "../../src/lib/crypto";
export * as access from "../../src/lib/access";
export * as encryption from "../../src/lib/encryption";
export * as turn from "../../src/lib/turn";
export * as descriptor from "../../src/lib/descriptor";
export * as net from "../../src/lib/net";
export * as orbitIdentity from "../../src/lib/orbit-identity";
export * as log from "../../src/lib/log";
export * as mailbox from "../../src/lib/mailbox";
export * as selftest from "../../src/lib/selftest";
export * as epoch from "../../src/lib/epoch";
export * as policy from "../../src/lib/policy";
export * as wrapbox from "../../src/lib/wrapbox";
export * as ops from "../../src/app/ops";
export * as state from "../../src/app/state";
export * as trust from "../../src/app/trust";
export * as boards from "../../src/app/boards";
export * as tools from "../../src/app/tools";
export * as canvas from "../../src/app/canvas";
export * as i18n from "../../src/i18n/index";
export { en } from "../../src/i18n/en";
export { ro } from "../../src/i18n/ro";
export { hu } from "../../src/i18n/hu";
`;
// NOT in the entry: `main.ts` (it imports the Vite-only `virtual:pwa-register`),
// `board.ts` (composition root; nothing serialisable), and the pure-DOM leaves
// `app/{toolbar,identity-sheet,text-editor,input,cursors,export,images,access}.ts`
// — `app/style.css` would also need a CSS loader.
//
// `lib/net.ts` and `helia` both pull @libp2p/webrtc, whose Node build loads the
// libdatachannel native addon. `npm install` fetches it via prebuild-install;
// if it is missing, run
// (cd node_modules/@ipshipyard/node-datachannel && npx prebuild-install -r napi)
/** className -> { exposedName: accessor body }. */
const EXPOSE = {
BoardStore: {
idFields: "(...a) => this.#idFields(...a)",
applyAdd: "(...a) => this.#applyAdd(...a)",
applyEdit: "(...a) => this.#applyEdit(...a)",
applyDel: "(...a) => this.#applyDel(...a)",
pastGate: "(...a) => this.#pastGate(...a)",
onRoomSyncIn: "(...a) => this.#onRoomSync(...a)",
verifyPeerIdentity: "(...a) => this.#verifyPeerIdentity(...a)",
pending: "() => this.#pending",
tombs: "() => this.#tombs",
editSeen: "() => this.#editSeen",
},
// the same member lists rooms-kit's and chat's machines/mailbox transcripts
// drive, so their log/net/relay/mailbox fixtures replay unchanged here
RoomLog: {
emit: "(...a) => this.#emit(...a)",
onUpdate: "(...a) => this.#onUpdate(...a)",
sweepUnseen: "(...a) => this.#sweepUnseen(...a)",
authorOf: "(...a) => this.#authorOf(...a)",
canJoinLocally: "(...a) => this.#canJoinLocally(...a)",
identityLocal: "(...a) => this.#identityLocal(...a)",
},
Net: {
dialPeer: "(...a) => this.#dialPeer(...a)",
recFor: "(...a) => this.#recFor(...a)",
onConnChange: "(...a) => this.#onConnChange(...a)",
refreshState: "(...a) => this.#refreshState(...a)",
maybeReady: "(...a) => this.#maybeReady(...a)",
sweep: "(...a) => this.#sweep(...a)",
endChannels: "(...a) => this.#endChannels(...a)",
dropPeer: "(...a) => this.#dropPeer(...a)",
hangUpStr: "(...a) => this.#hangUpStr(...a)",
beacon: "(...a) => this.#beacon(...a)",
verified: "(...a) => this.#verified(...a)",
onTopicMessage: "(...a) => this.#onTopicMessage(...a)",
onPresence: "(...a) => this.#onPresence(...a)",
beaconTo: "(...a) => this.#beaconTo(...a)",
onMsgFrame: "(...a) => this.#onMsgFrame(...a)",
frame: "(...a) => this.#frame(...a)",
relayPeerId: "(...a) => this.#relayPeerId(...a)",
ensureRelay: "(...a) => this.#ensureRelay(...a)",
scheduleRedial: "(...a) => this.#scheduleRedial(...a)",
setRelayUp: "(...a) => this.#setRelayUp(...a)",
pubsub: "(...a) => this.#pubsub(...a)",
},
MailboxSync: {
buildPayload: "(...a) => this.#buildPayload(...a)",
processMessage: "(...a) => this.#processMessage(...a)",
enqueueIdentityOnce: "(...a) => this.#enqueueIdentityOnce(...a)",
push: "(...a) => this.#push(...a)",
dropHead: "(...a) => this.#dropHead(...a)",
markSent: "(...a) => this.#markSent(...a)",
queue: "() => this.#queue",
},
ToolController: {
pts: "() => this.#pts",
prs: "() => this.#prs",
penId: "() => this.#penId",
commitStroke: "(...a) => this.#commitStroke(...a)",
bcastSegment: "(...a) => this.#bcastSegment(...a)",
anchorAt: "(...a) => this.#anchorAt(...a)",
preciseHit: "(...a) => this.#preciseHit(...a)",
},
AccessFold: {
certOk: "() => this.#certOk",
collectWraps: "(...a) => this.#collectWraps(...a)",
},
EpochKeyring: {
opCipher: "(...a) => this.#opCipher(...a)",
chainCipher: "(...a) => this.#chainCipher(...a)",
secrets: "() => this.#secrets",
},
};
const SPEC_INJECT = (src) => {
let out = src;
for (const [cls, members] of Object.entries(EXPOSE)) {
// esbuild emits `var Name = class [_Name] {` for an exported class
const re = new RegExp(`(var\\s+${cls}\\s*=\\s*class\\b[^{]*\\{)`);
if (!re.test(out)) throw new Error(`expose: class ${cls} not found in the bundle`);
const bag = Object.entries(members)
.map(([k, body]) => `${k}: ${body}`)
.join(", ");
out = out.replace(re, `$1\n __t = { ${bag} };\n`);
}
return out;
};
/** The version the production build defines; fixtures must not depend on it. */
const APP_VERSION = JSON.parse(readFileSync(join(CLIENT, "version.json"), "utf8")).version;
let cached = null;
export async function loadTs() {
if (cached) return cached;
mkdirSync(STAGE, { recursive: true });
const entryFile = join(STAGE, "entry.ts");
writeFileSync(entryFile, ENTRY);
const bundleFile = join(STAGE, "bundle.mjs");
execFileSync(
join(CLIENT, "node_modules", ".bin", "esbuild"),
[
entryFile,
"--bundle",
"--format=esm",
"--target=es2022",
"--platform=neutral",
"--packages=external",
"--define:import.meta.env={}",
`--define:__APP_VERSION__=${APP_VERSION}`,
`--outfile=${bundleFile}`,
"--log-level=warning",
],
{ cwd: CLIENT, stdio: ["ignore", "inherit", "inherit"] },
);
writeFileSync(bundleFile, SPEC_INJECT(readFileSync(bundleFile, "utf8")));
cached = await import(pathToFileURL(bundleFile).href);
return cached;
}
|