@rstreamlabs/webtty 1.7.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/node.mjs ADDED
@@ -0,0 +1,451 @@
1
+ import {
2
+ WebTTY,
3
+ WebTTYCommand,
4
+ WebTTYFileSystem,
5
+ WebTTYFileSystemError,
6
+ WebTTYRemoteExecutor,
7
+ createWebTTYE2EClientPayloadCrypto,
8
+ createWebTTYE2EKeyContext,
9
+ createWebTTYE2EReplayPayloadCryptoFromKeyGrant,
10
+ createWebTTYE2EServerPayloadCrypto,
11
+ decodeWebTTYE2EKeyMaterial,
12
+ decryptWebTTYRecordedEvent,
13
+ decryptWebTTYRecordedTextLog,
14
+ encodeWebTTYE2EKeyMaterial,
15
+ executeWebTTYCommand,
16
+ generateWebTTYE2EIdentity,
17
+ generateWebTTYSigningIdentity,
18
+ hashWebTTYAttachGrant,
19
+ hashWebTTYClientCredential,
20
+ hashWebTTYClientProofTranscript,
21
+ hashWebTTYConfig,
22
+ hashWebTTYServerProofTranscript,
23
+ hashWebTTYSessionKeyGrant,
24
+ openWebTTYCommand,
25
+ parseWebDAVMultiStatus,
26
+ renderWebTTYRecordedTextLog,
27
+ resolveWebTTYExecutionURL,
28
+ resolveWebTTYFileSystemURL,
29
+ runWebTTYCommand,
30
+ signWebTTYClientProofTranscript,
31
+ signWebTTYServerProofTranscript,
32
+ verifyWebTTYClientProofTranscript,
33
+ verifyWebTTYServerProofTranscript,
34
+ webTTYClientProofTranscriptBytes,
35
+ webTTYE2EKeyID,
36
+ webTTYRecordedEventEncryptedPayload,
37
+ webTTYServerProofTranscriptBytes,
38
+ webTTYSigningKeyID
39
+ } from "./chunk-7OHAFDJE.mjs";
40
+
41
+ // src/local-e2e.ts
42
+ var localKeyFileVersion = 1;
43
+ var localKeyFileCryptoSuite = "webtty-e2e-x25519-hpke-aes-256-gcm-v1";
44
+ var x25519PublicKeySize = 32;
45
+ var payloadKeyIDSize = 16;
46
+ var knownServerKeyEnv = "RSTREAM_WEBTTY_KNOWN_SERVER_KEY";
47
+ var knownServersFileEnv = "RSTREAM_WEBTTY_KNOWN_SERVERS_FILE";
48
+ async function defaultWebTTYKnownServersPath(env) {
49
+ const path = await nodePath();
50
+ return path.join(
51
+ await homeDir(env ?? defaultEnv()),
52
+ ".rstream",
53
+ "webtty",
54
+ "known_servers.json"
55
+ );
56
+ }
57
+ async function parseWebTTYKnownServerKey(value) {
58
+ const trimmed = value.trim();
59
+ if (trimmed === "") {
60
+ throw new Error("known WebTTY server key is empty");
61
+ }
62
+ const parts = trimmed.split(":");
63
+ if (parts.length !== 1 && parts.length !== 2 && parts.length !== 4) {
64
+ throw new Error(
65
+ "known WebTTY server key must be public_key, key_id:public_key, or endpoint identity"
66
+ );
67
+ }
68
+ if (parts.length === 4) {
69
+ validateSigningKeyMaterial(parts[2] ?? "", parts[3] ?? "");
70
+ }
71
+ if (parts.length === 1) {
72
+ const publicKey2 = decodeFixedKeyMaterial(
73
+ parts[0] ?? "",
74
+ x25519PublicKeySize,
75
+ "known WebTTY server public key"
76
+ );
77
+ return { keyId: await webTTYE2EKeyID(publicKey2), publicKey: publicKey2 };
78
+ }
79
+ const keyId = decodeFixedKeyMaterial(
80
+ parts[0] ?? "",
81
+ payloadKeyIDSize,
82
+ "known WebTTY server key id"
83
+ );
84
+ const publicKey = decodeFixedKeyMaterial(
85
+ parts[1] ?? "",
86
+ x25519PublicKeySize,
87
+ "known WebTTY server public key"
88
+ );
89
+ const expectedKeyId = await webTTYE2EKeyID(publicKey);
90
+ if (!bytesEqual(keyId, expectedKeyId)) {
91
+ throw new Error("known WebTTY server key id does not match public key");
92
+ }
93
+ return { keyId, publicKey };
94
+ }
95
+ async function readWebTTYKnownServerKeysFile(filePath, env) {
96
+ const resolvedPath = cleanString(filePath) ?? await defaultWebTTYKnownServersPath(env);
97
+ const fs = await nodeFS();
98
+ const data = await fs.readFile(resolvedPath, "utf8");
99
+ return decodeWebTTYKnownServerKeysFile(data, resolvedPath);
100
+ }
101
+ async function loadWebTTYKnownServerKeysFile(filePath, env) {
102
+ const doc = await readWebTTYKnownServerKeysFile(filePath, env);
103
+ return uniqueRecipients(
104
+ await Promise.all(doc.known_servers.map(knownServerKeyFromEntry))
105
+ );
106
+ }
107
+ async function createWebTTYE2EClientPayloadCryptoFromLocalTrust(options = {}) {
108
+ const env = options.env ?? defaultEnv();
109
+ const directKeys = optionKeys(options.knownServerKey);
110
+ const directRecipients = await parseWebTTYKnownServerKeys(directKeys);
111
+ const envKey = cleanString(env[knownServerKeyEnv]);
112
+ const envRecipients = envKey === void 0 ? [] : [await parseWebTTYKnownServerKey(envKey)];
113
+ const explicitFile = cleanString(options.knownServersFile) ?? cleanString(env[knownServersFileEnv]);
114
+ const fileRecipients = explicitFile === void 0 ? [] : await loadWebTTYKnownServerKeysFile(explicitFile, env);
115
+ const explicitConfigured = directKeys.length > 0 || envKey !== void 0 || explicitFile !== void 0;
116
+ const defaultRecipients = explicitConfigured ? [] : await loadDefaultKnownServerRecipients(env, {
117
+ hostKeyId: options.hostKeyId,
118
+ target: options.target
119
+ });
120
+ const configured = explicitConfigured || defaultRecipients.length > 0;
121
+ const recipients = uniqueRecipients([
122
+ ...directRecipients,
123
+ ...envRecipients,
124
+ ...fileRecipients,
125
+ ...defaultRecipients
126
+ ]);
127
+ if (!configured && options.required !== true) {
128
+ return void 0;
129
+ }
130
+ if (recipients.length === 0) {
131
+ throw new Error(
132
+ `E2E client mode requires knownServerKey, knownServersFile, ${knownServerKeyEnv}, ${knownServersFileEnv}, or ${await defaultWebTTYKnownServersPath(env)}`
133
+ );
134
+ }
135
+ return await createWebTTYE2EClientPayloadCrypto({
136
+ keyContext: options.keyContext,
137
+ recipients
138
+ });
139
+ }
140
+ async function parseWebTTYKnownServerKeys(values) {
141
+ return await Promise.all(values.map(parseWebTTYKnownServerKey));
142
+ }
143
+ async function loadDefaultKnownServerRecipients(env, scope) {
144
+ const defaultPath = await defaultWebTTYKnownServersPath(env);
145
+ const defaultDoc = await readOptionalKnownServersFile(defaultPath);
146
+ const entries = defaultDoc === void 0 ? [] : filterKnownServerEntries(defaultDoc.known_servers, scope);
147
+ return entries.length === 0 ? [] : uniqueRecipients(await Promise.all(entries.map(knownServerKeyFromEntry)));
148
+ }
149
+ function filterKnownServerEntries(entries, scope) {
150
+ const target = cleanString(scope.target);
151
+ const hostKeyId = cleanString(scope.hostKeyId);
152
+ if (target === void 0 && hostKeyId === void 0) {
153
+ return [...entries];
154
+ }
155
+ const targetMatches = target === void 0 ? [] : entries.filter((entry) => entry.name === target);
156
+ if (hostKeyId === void 0) {
157
+ return targetMatches;
158
+ }
159
+ if (targetMatches.length > 0) {
160
+ return targetMatches.filter((entry) => entry.key_id === hostKeyId);
161
+ }
162
+ return entries.filter((entry) => entry.key_id === hostKeyId);
163
+ }
164
+ function decodeWebTTYKnownServerKeysFile(data, filePath) {
165
+ const parsed = parseJSONRecord(
166
+ data,
167
+ `decode known WebTTY server keys ${filePath}`
168
+ );
169
+ assertKnownKeys(
170
+ parsed,
171
+ ["crypto_suite", "known_servers", "version"],
172
+ `known WebTTY server keys ${filePath}`
173
+ );
174
+ const version = parsed.version;
175
+ if (version !== localKeyFileVersion) {
176
+ throw new Error(
177
+ `unsupported known WebTTY server keys version ${String(version)}`
178
+ );
179
+ }
180
+ const cryptoSuite = parsed.crypto_suite;
181
+ if (cryptoSuite !== localKeyFileCryptoSuite) {
182
+ throw new Error(
183
+ `unsupported known WebTTY server keys crypto suite ${String(cryptoSuite)}`
184
+ );
185
+ }
186
+ const entries = parsed.known_servers;
187
+ if (!Array.isArray(entries)) {
188
+ throw new Error("known WebTTY server keys must include known_servers");
189
+ }
190
+ return {
191
+ crypto_suite: cryptoSuite,
192
+ known_servers: entries.map(
193
+ (entry, index) => decodeKnownServerKeyEntry(entry, `known WebTTY server key ${index}`)
194
+ ),
195
+ version
196
+ };
197
+ }
198
+ function decodeKnownServerKeyEntry(value, label) {
199
+ const entry = asRecord(value, label);
200
+ assertKnownKeys(
201
+ entry,
202
+ [
203
+ "client_identity",
204
+ "created_at",
205
+ "key_id",
206
+ "name",
207
+ "public_key",
208
+ "signing_key_id",
209
+ "signing_public_key"
210
+ ],
211
+ label
212
+ );
213
+ const name = stringField(entry, "name", label).trim();
214
+ if (name === "") {
215
+ throw new Error(`${label} name is required`);
216
+ }
217
+ const signingKeyID = optionalStringField(entry, "signing_key_id", label);
218
+ const signingPublicKey = optionalStringField(
219
+ entry,
220
+ "signing_public_key",
221
+ label
222
+ );
223
+ if (signingKeyID === void 0 !== (signingPublicKey === void 0)) {
224
+ throw new Error(
225
+ `${label} signing_key_id and signing_public_key must be provided together`
226
+ );
227
+ }
228
+ if (signingKeyID !== void 0 && signingPublicKey !== void 0) {
229
+ validateSigningKeyMaterial(signingKeyID, signingPublicKey);
230
+ }
231
+ return {
232
+ client_identity: optionalStringField(entry, "client_identity", label),
233
+ created_at: optionalStringField(entry, "created_at", label),
234
+ key_id: stringField(entry, "key_id", label),
235
+ name,
236
+ public_key: stringField(entry, "public_key", label),
237
+ signing_key_id: signingKeyID,
238
+ signing_public_key: signingPublicKey
239
+ };
240
+ }
241
+ async function knownServerKeyFromEntry(entry) {
242
+ return await parseWebTTYKnownServerKey(`${entry.key_id}:${entry.public_key}`);
243
+ }
244
+ async function readOptionalKnownServersFile(filePath) {
245
+ try {
246
+ const fs = await nodeFS();
247
+ const data = await fs.readFile(filePath, "utf8");
248
+ return decodeWebTTYKnownServerKeysFile(data, filePath);
249
+ } catch (error) {
250
+ if (isNodeErrorCode(error, "ENOENT")) {
251
+ return void 0;
252
+ }
253
+ throw error;
254
+ }
255
+ }
256
+ function appendUniqueRecipient(recipients, next) {
257
+ const nextKeyId = bytesFromKeyMaterial(next.keyId);
258
+ const nextPublicKey = bytesFromKeyMaterial(next.publicKey);
259
+ for (const existing of recipients) {
260
+ const existingKeyId = bytesFromKeyMaterial(existing.keyId);
261
+ if (!bytesEqual(existingKeyId, nextKeyId)) {
262
+ continue;
263
+ }
264
+ if (!bytesEqual(bytesFromKeyMaterial(existing.publicKey), nextPublicKey)) {
265
+ throw new Error("conflicting known WebTTY server public keys for key id");
266
+ }
267
+ return;
268
+ }
269
+ recipients.push({ keyId: nextKeyId, publicKey: nextPublicKey });
270
+ }
271
+ function uniqueRecipients(recipients) {
272
+ const out = [];
273
+ for (const recipient of recipients) {
274
+ appendUniqueRecipient(out, recipient);
275
+ }
276
+ return out;
277
+ }
278
+ function optionKeys(value) {
279
+ if (value === void 0) {
280
+ return [];
281
+ }
282
+ return (Array.isArray(value) ? value : [value]).map((entry) => entry.trim()).filter((entry) => entry !== "");
283
+ }
284
+ function decodeFixedKeyMaterial(value, size, label) {
285
+ const decoded = decodeWebTTYE2EKeyMaterial(value.trim());
286
+ if (decoded.byteLength !== size) {
287
+ throw new Error(`${label} must decode to ${size} bytes`);
288
+ }
289
+ return decoded;
290
+ }
291
+ function validateSigningKeyMaterial(keyID, publicKey) {
292
+ decodeFixedKeyMaterial(keyID, 32, "known WebTTY server signing key id");
293
+ decodeWebTTYE2EKeyMaterial(publicKey.trim());
294
+ }
295
+ function bytesFromKeyMaterial(value) {
296
+ if (value === void 0) {
297
+ return new Uint8Array();
298
+ }
299
+ return typeof value === "string" ? decodeWebTTYE2EKeyMaterial(value) : new Uint8Array(value);
300
+ }
301
+ function bytesEqual(left, right) {
302
+ if (left.byteLength !== right.byteLength) {
303
+ return false;
304
+ }
305
+ const diff = left.reduce(
306
+ (current, value, index) => current | value ^ (right[index] ?? 0),
307
+ 0
308
+ );
309
+ return diff === 0;
310
+ }
311
+ function parseJSONRecord(data, label) {
312
+ return asRecord(parseJSON(data, label), label);
313
+ }
314
+ function parseJSON(data, label) {
315
+ try {
316
+ return JSON.parse(data);
317
+ } catch (error) {
318
+ throw new Error(`${label}: ${errorMessage(error)}`);
319
+ }
320
+ }
321
+ function asRecord(value, label) {
322
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
323
+ throw new Error(`${label} must be an object`);
324
+ }
325
+ return Object.fromEntries(Object.entries(value));
326
+ }
327
+ function assertKnownKeys(value, allowed, label) {
328
+ const allowedSet = new Set(allowed);
329
+ for (const key of Object.keys(value)) {
330
+ if (!allowedSet.has(key)) {
331
+ throw new Error(`${label} contains unknown field ${key}`);
332
+ }
333
+ }
334
+ }
335
+ function stringField(value, key, label) {
336
+ const field = value[key];
337
+ if (typeof field !== "string") {
338
+ throw new Error(`${label} ${key} must be a string`);
339
+ }
340
+ return field;
341
+ }
342
+ function optionalStringField(value, key, label) {
343
+ const field = value[key];
344
+ if (field === void 0) {
345
+ return void 0;
346
+ }
347
+ if (typeof field !== "string") {
348
+ throw new Error(`${label} ${key} must be a string`);
349
+ }
350
+ return field;
351
+ }
352
+ function cleanString(value) {
353
+ const trimmed = value?.trim();
354
+ return trimmed === void 0 || trimmed === "" ? void 0 : trimmed;
355
+ }
356
+ function defaultEnv() {
357
+ const env = objectField(objectField(globalThis, "process"), "env");
358
+ return env === void 0 ? {} : stringRecord(env);
359
+ }
360
+ async function homeDir(env) {
361
+ const configured = cleanString(env.HOME) ?? cleanString(env.USERPROFILE);
362
+ if (configured !== void 0) {
363
+ return configured;
364
+ }
365
+ const os = await import("os");
366
+ const resolved = os.homedir();
367
+ if (resolved === "") {
368
+ throw new Error("unable to resolve user home directory");
369
+ }
370
+ return resolved;
371
+ }
372
+ async function nodePath() {
373
+ try {
374
+ return await import("path");
375
+ } catch {
376
+ throw new Error("local WebTTY E2E trust helpers require a Node.js runtime");
377
+ }
378
+ }
379
+ async function nodeFS() {
380
+ try {
381
+ return await import("fs/promises");
382
+ } catch {
383
+ throw new Error("local WebTTY E2E trust helpers require a Node.js runtime");
384
+ }
385
+ }
386
+ function isNodeErrorCode(error, code) {
387
+ return objectField(error, "code") === code;
388
+ }
389
+ function objectField(value, key) {
390
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
391
+ return void 0;
392
+ }
393
+ return Object.fromEntries(Object.entries(value))[key];
394
+ }
395
+ function stringRecord(value) {
396
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
397
+ return {};
398
+ }
399
+ return Object.fromEntries(
400
+ Object.entries(value).filter(
401
+ ([, entry]) => typeof entry === "string" || entry === void 0
402
+ )
403
+ );
404
+ }
405
+ function errorMessage(error) {
406
+ return error instanceof Error ? error.message : String(error);
407
+ }
408
+ export {
409
+ WebTTY,
410
+ WebTTYCommand,
411
+ WebTTYFileSystem,
412
+ WebTTYFileSystemError,
413
+ WebTTYRemoteExecutor,
414
+ createWebTTYE2EClientPayloadCrypto,
415
+ createWebTTYE2EClientPayloadCryptoFromLocalTrust,
416
+ createWebTTYE2EKeyContext,
417
+ createWebTTYE2EReplayPayloadCryptoFromKeyGrant,
418
+ createWebTTYE2EServerPayloadCrypto,
419
+ decodeWebTTYE2EKeyMaterial,
420
+ decryptWebTTYRecordedEvent,
421
+ decryptWebTTYRecordedTextLog,
422
+ defaultWebTTYKnownServersPath,
423
+ encodeWebTTYE2EKeyMaterial,
424
+ executeWebTTYCommand,
425
+ generateWebTTYE2EIdentity,
426
+ generateWebTTYSigningIdentity,
427
+ hashWebTTYAttachGrant,
428
+ hashWebTTYClientCredential,
429
+ hashWebTTYClientProofTranscript,
430
+ hashWebTTYConfig,
431
+ hashWebTTYServerProofTranscript,
432
+ hashWebTTYSessionKeyGrant,
433
+ loadWebTTYKnownServerKeysFile,
434
+ openWebTTYCommand,
435
+ parseWebDAVMultiStatus,
436
+ parseWebTTYKnownServerKey,
437
+ readWebTTYKnownServerKeysFile,
438
+ renderWebTTYRecordedTextLog,
439
+ resolveWebTTYExecutionURL,
440
+ resolveWebTTYFileSystemURL,
441
+ runWebTTYCommand,
442
+ signWebTTYClientProofTranscript,
443
+ signWebTTYServerProofTranscript,
444
+ verifyWebTTYClientProofTranscript,
445
+ verifyWebTTYServerProofTranscript,
446
+ webTTYClientProofTranscriptBytes,
447
+ webTTYE2EKeyID,
448
+ webTTYRecordedEventEncryptedPayload,
449
+ webTTYServerProofTranscriptBytes,
450
+ webTTYSigningKeyID
451
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rstreamlabs/webtty",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "Client-side implementation of the rstream WebTTY protocol.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,6 +11,19 @@
11
11
  "main": "./dist/index.js",
12
12
  "module": "./dist/index.mjs",
13
13
  "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.mjs",
18
+ "require": "./dist/index.js"
19
+ },
20
+ "./node": {
21
+ "types": "./dist/node.d.ts",
22
+ "import": "./dist/node.mjs",
23
+ "require": "./dist/node.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
14
27
  "files": [
15
28
  "dist",
16
29
  "examples",