@volter/teams 0.1.70 → 0.1.72

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.
@@ -49,8 +49,10 @@ export type BrowserTurnAnswer =
49
49
  /**
50
50
  * One line typed into the channel's session as its user's own turn; rejects when the machine refuses the stream.
51
51
  * `turnKey`: one per typed line, reused on retry, so the machine types the line once.
52
+ * Receipt waits time out after thirty seconds. Cancellation stops waiting; it
53
+ * does not undo delivery. Reuse the same key when retrying an uncertain receipt.
52
54
  */
53
- export function sendBrowserTurn(channel: BrowserChannel, text: string, options?: { turnKey?: string }): Promise<BrowserTurnAnswer>;
55
+ export function sendBrowserTurn(channel: BrowserChannel, text: string, options?: { turnKey?: string; signal?: AbortSignal }): Promise<BrowserTurnAnswer>;
54
56
 
55
57
  export interface MailHistoryEntry {
56
58
  id: string; from: string; fromName: string; to: string; text: string; at: number;
@@ -173,13 +173,33 @@ export function openBrowserSession({ origin, teamId, sessionId, token }, { WebSo
173
173
  * is free), or `{ code, text }` when it was not delivered; rejects when the machine refuses the stream.
174
174
  * `turnKey`: one key per line the person typed, reused when they retry it, so a line the machine took after its
175
175
  * first send was given up on is not typed twice (a machine of door vocabulary 3 or later keeps each key once).
176
+ * Receipt waits are bounded to thirty seconds and 256 KB. `signal` cancels the
177
+ * wait; neither cancellation nor timeout undoes a turn already accepted.
176
178
  */
177
- export async function sendBrowserTurn(channel, text, { turnKey } = {}) {
179
+ export async function sendBrowserTurn(channel, text, { turnKey, signal } = {}) {
180
+ signal?.throwIfAborted();
178
181
  const stream = await channel.open('mail', { request: { op: 'turn', text, ...(turnKey ? { turn_key: String(turnKey) } : {}) } });
179
- return new Promise((resolve) => {
180
- let answer = '';
181
- stream.on('data', (chunk) => { answer += chunk; });
182
- stream.on('close', () => { try { resolve(JSON.parse(answer)); } catch { resolve({ code: 1, text: answer || 'no answer from the machine' }); } });
182
+ return new Promise((resolve, reject) => {
183
+ let answer = '', settled = false;
184
+ const cleanup = () => { clearTimeout(timer); stream.off('data', data); stream.off('close', closed); signal?.removeEventListener('abort', cancelled); };
185
+ const fail = error => {
186
+ if (settled) return;
187
+ settled = true; cleanup(); reject(error);
188
+ // close emits synchronously. A failed wait must not parse an incomplete
189
+ // receipt as success, or imply that a typed turn was rolled back.
190
+ stream.close(error.message);
191
+ };
192
+ const cancelled = () => fail(Object.assign(new Error('Turn delivery wait cancelled'), { name: 'AbortError' }));
193
+ const timer = setTimeout(() => fail(new Error('Turn delivery acknowledgement timed out')), 30_000);
194
+ const data = chunk => { answer += chunk; if (answer.length > 256_000) fail(new Error('Turn delivery receipt too large')); };
195
+ const closed = () => {
196
+ if (settled) return;
197
+ settled = true; cleanup();
198
+ try { resolve(JSON.parse(answer)); } catch { resolve({ code: 1, text: answer || 'no answer from the machine' }); }
199
+ };
200
+ stream.on('data', data);
201
+ if (!settled) stream.on('close', closed);
202
+ if (!settled && signal) { signal.addEventListener('abort', cancelled, { once: true }); if (signal.aborted) cancelled(); }
183
203
  });
184
204
  }
185
205
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/teams",
3
- "version": "0.1.70",
3
+ "version": "0.1.72",
4
4
  "type": "module",
5
5
  "description": "Volter Teams, the neutral team plane: machine identity and daemon core, the local door and server link, admission from machine grants, product doors, and the Teams server's neutral routes (company RFC 0008).",
6
6
  "license": "MIT",