@earendil-works/pi-client 0.84.3 → 0.85.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.
@@ -1 +1 @@
1
- {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,mBAAmB,EACnB,uBAAuB,GAMvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EACN,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EACb,sBAAsB,EACtB,uBAAuB,EACvB,OAAO,GACP,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAGN,aAAa,GAGb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAqBzC,MAAM,OAAO,QAAQ;IACX,QAAQ,CAAkB;IAC1B,WAAW,CAAa;IACxB,MAAM,CAAc;IACpB,gBAAgB,GAAG,IAAI,GAAG,EAA0B,CAAC;IACrD,mBAAmB,GAAG,IAAI,GAAG,EAAkB,CAAC;IAChD,uBAAuB,GAAG,IAAI,GAAG,EAA6B,CAAC;IAC/D,wBAAwB,GAAG,IAAI,GAAG,EAAkB,CAAC;IACrD,mBAAmB,GAAG,IAAI,GAAG,EAAyB,CAAC;IACvD,mBAAmB,GAAG,IAAI,GAAG,EAAyB,CAAC;IACvD,uBAAuB,GAAG,IAAI,GAAG,EAAU,CAAC;IAC5C,uBAAuB,GAAG,IAAI,GAAG,EAAyB,CAAC;IAC3D,yBAAyB,GAAG,IAAI,GAAG,EAA2C,CAAC;IACxF,gBAAgB,GAAG,CAAC,CAAC;IACrB,SAAS,GAAG,KAAK,CAAC;IAClB,eAAe,CAA4B;IAE3C,YAAY,OAAwB,EAAE;QACrC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IA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,CACrC;IAED,+BAA+B,CAAC,MAA6B,EAAQ;QACpE,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,yBAAyB,EAAE,CAAC;YACvD,IAAI,CAAC;gBACJ,QAAQ,CAAC,MAAM,CAAC,CAAC;YAClB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC;YAClC,CAAC;QACF,CAAC;IAAA,CACD;IAED,oBAAoB,CAAC,KAAc,EAAQ;QAC1C,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,eAAe;YAAE,OAAO;QAC3C,IAAI,CAAC;YACJ,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QAC/C,CAAC;QAAC,MAAM,CAAC;YACR,yDAAyD;QAC1D,CAAC;IAAA,CACD;CACD","sourcesContent":["import {\n\ttype Command,\n\ttype CommandResult,\n\ttype EventEnvelope,\n\tencodeClientMessage,\n\tProtocolValidationError,\n\ttype ResponseEnvelope,\n\ttype ResultForCommand,\n\ttype ServerEvent,\n\ttype ServerSnapshot,\n\ttype SessionMetadata,\n} from \"@earendil-works/pi-protocol\";\nimport { Connection } from \"./connection.ts\";\nimport {\n\tPiClientDisposedError,\n\tPiDisconnectedError,\n\tPiServerError,\n\tPiSessionDetachedError,\n\tPiSessionOwnershipError,\n\ttoError,\n} from \"./errors.ts\";\nimport { createPromiseResolvers } from \"./promise.ts\";\nimport {\n\ttype AcquireSessionOptions,\n\ttype PiSessionHandle,\n\tSessionHandle,\n\ttype SessionHandleCallbacks,\n\ttype SessionLeaseMode,\n} from \"./session-handle.ts\";\nimport { ClientState } from \"./state.ts\";\nimport type {\n\tConnectionState,\n\tConnectionStateChange,\n\tCreateSessionOptions,\n\tPiClientOptions,\n\tUnsubscribe,\n} from \"./types.ts\";\n\ntype SessionLeaseState = \"active\" | \"releasing\" | \"released\" | \"invalidated\";\n\ninterface SessionLeaseToken {\n\treadonly mode: SessionLeaseMode;\n}\n\ninterface PendingRequest {\n\tcommand: Command;\n\tresolve(result: CommandResult): void;\n\treject(error: Error): void;\n}\n\nexport class PiClient {\n\treadonly #options: PiClientOptions;\n\treadonly #connection: Connection;\n\treadonly #state: ClientState;\n\treadonly #pendingRequests = new Map<string, PendingRequest>();\n\treadonly #sessionLeaseCounts = new Map<string, number>();\n\treadonly #exclusiveSessionLeases = new Map<string, SessionLeaseToken>();\n\treadonly #sessionLeaseGenerations = new Map<string, number>();\n\treadonly #sessionAttachments = new Map<string, Promise<void>>();\n\treadonly #sessionDetachments = new Map<string, Promise<void>>();\n\treadonly #sessionCleanupRequired = new Set<string>();\n\treadonly #sessionReconciliations = new Map<string, Promise<void>>();\n\treadonly #connectionStateListeners = new Set<(change: ConnectionStateChange) => void>();\n\t#requestSequence = 0;\n\t#disposed = false;\n\t#disposePromise: Promise<void> | undefined;\n\n\tconstructor(options: PiClientOptions) {\n\t\tthis.#options = options;\n\t\tthis.#state = new ClientState(options.onListenerError);\n\t\tthis.#connection = new Connection({\n\t\t\ttransportFactory: options.transportFactory,\n\t\t\tmaxFrameLength: options.maxFrameLength,\n\t\t\tonHandshake: (snapshot) => this.#state.applyServerSnapshot(snapshot),\n\t\t\tonMessage: (message) => this.#handleMessage(message),\n\t\t\tonStateChange: (change) => this.#handleConnectionStateChange(change),\n\t\t});\n\t}\n\n\tget disposed(): boolean {\n\t\treturn this.#disposed;\n\t}\n\n\tget connectionState(): ConnectionState {\n\t\treturn this.#connection.state;\n\t}\n\n\tget connected(): boolean {\n\t\treturn this.#connection.state === \"connected\";\n\t}\n\n\tget snapshot(): ServerSnapshot | undefined {\n\t\treturn this.#state.snapshot;\n\t}\n\n\tstatic async connect(options: PiClientOptions): Promise<PiClient> {\n\t\tconst client = new PiClient(options);\n\t\ttry {\n\t\t\tawait client.connect();\n\t\t\treturn client;\n\t\t} catch (error) {\n\t\t\tawait client.dispose();\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tconnect(): Promise<ServerSnapshot> {\n\t\tif (this.#disposed) return Promise.reject(new PiClientDisposedError());\n\t\tif (this.#connection.state === \"disconnected\") this.#state.reset();\n\t\treturn this.#connection.connect();\n\t}\n\n\treconnect(): Promise<ServerSnapshot> {\n\t\treturn this.connect();\n\t}\n\n\tdisconnect(reason = \"Client disconnected\"): void {\n\t\tthis.#connection.disconnect(reason);\n\t}\n\n\tsubscribe(listener: (snapshot: ServerSnapshot) => void): Unsubscribe {\n\t\tthis.#assertNotDisposed();\n\t\treturn this.#state.subscribe(listener);\n\t}\n\n\tonEvent(listener: (event: ServerEvent) => void): Unsubscribe {\n\t\tthis.#assertNotDisposed();\n\t\treturn this.#state.onEvent(listener);\n\t}\n\n\tonConnectionStateChange(listener: (change: ConnectionStateChange) => void): Unsubscribe {\n\t\tthis.#assertNotDisposed();\n\t\tthis.#connectionStateListeners.add(listener);\n\t\treturn () => this.#connectionStateListeners.delete(listener);\n\t}\n\n\tasync listSessions(): Promise<readonly SessionMetadata[]> {\n\t\treturn (await this.#request({ command: \"list\" })).sessions;\n\t}\n\n\tasync createSession(options: CreateSessionOptions = {}): Promise<PiSessionHandle> {\n\t\tconst result = await this.#request({ command: \"create\", ...options });\n\t\tconst token = this.#reserveSessionLease(result.session.id, \"exclusive\");\n\t\treturn this.#createSessionLease(result.session.id, token);\n\t}\n\n\tasync attachSession(sessionId: string): Promise<PiSessionHandle> {\n\t\treturn this.acquireSession(sessionId, { mode: \"shared\" });\n\t}\n\n\tasync acquireSession(sessionId: string, options: AcquireSessionOptions): Promise<PiSessionHandle> {\n\t\tthis.#assertNotDisposed();\n\t\tconst token = this.#reserveSessionLease(sessionId, options.mode);\n\t\ttry {\n\t\t\tconst detachment = this.#sessionDetachments.get(sessionId);\n\t\t\tif (detachment) await detachment.catch(() => {});\n\t\t\tconst reconciled = this.#sessionCleanupRequired.has(sessionId)\n\t\t\t\t? await this.#reconcileSessionCleanup(sessionId)\n\t\t\t\t: false;\n\t\t\tif (reconciled || !this.#state.isSessionAttached(sessionId)) {\n\t\t\t\tlet attachment = this.#sessionAttachments.get(sessionId);\n\t\t\t\tif (!attachment) {\n\t\t\t\t\tattachment = this.#attachSession(sessionId);\n\t\t\t\t\tthis.#sessionAttachments.set(sessionId, attachment);\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tawait attachment;\n\t\t\t\t} finally {\n\t\t\t\t\tif (this.#sessionAttachments.get(sessionId) === attachment) this.#sessionAttachments.delete(sessionId);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn this.#createSessionLease(sessionId, token);\n\t\t} catch (error) {\n\t\t\tthis.#releaseSessionLease(sessionId, token);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tasync #attachSession(sessionId: string): Promise<void> {\n\t\tconst previous = this.#state.forgetSessionSnapshot(sessionId);\n\t\ttry {\n\t\t\tawait this.#request({ command: \"attach\", sessionId });\n\t\t} catch (error) {\n\t\t\tif (previous) this.#state.restoreSessionSnapshot(previous);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t#request<const TCommand extends Command>(command: TCommand): Promise<ResultForCommand<TCommand>> {\n\t\tif (this.#disposed) return Promise.reject(new PiClientDisposedError());\n\t\tif (!this.connected) return Promise.reject(new PiDisconnectedError());\n\t\tconst id = `request-${++this.#requestSequence}`;\n\t\tconst { promise, resolve, reject } = createPromiseResolvers<CommandResult>();\n\t\tthis.#pendingRequests.set(id, { command, resolve, reject });\n\t\tlet frame: Uint8Array;\n\t\ttry {\n\t\t\tframe = encodeClientMessage(\n\t\t\t\t{ type: \"request\", id, request: command },\n\t\t\t\t{ maxFrameLength: this.#connection.maxFrameLength },\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tthis.#takePendingRequest(id)?.reject(toError(error));\n\t\t\treturn promise as Promise<ResultForCommand<TCommand>>;\n\t\t}\n\t\tthis.#connection.send(frame);\n\t\treturn promise as Promise<ResultForCommand<TCommand>>;\n\t}\n\n\t#createSessionLease(sessionId: string, token: SessionLeaseToken): PiSessionHandle {\n\t\tconst generation = this.#sessionLeaseGenerations.get(sessionId) ?? 0;\n\t\tthis.#sessionLeaseGenerations.set(sessionId, generation);\n\t\tlet state: SessionLeaseState = \"active\";\n\t\tlet releasePromise: Promise<void> | undefined;\n\t\tconst refreshState = () => {\n\t\t\tif (\n\t\t\t\t(state === \"active\" || state === \"releasing\") &&\n\t\t\t\tthis.#sessionLeaseGenerations.get(sessionId) !== generation\n\t\t\t) {\n\t\t\t\tstate = \"invalidated\";\n\t\t\t}\n\t\t};\n\t\tconst isActive = () => {\n\t\t\trefreshState();\n\t\t\treturn state === \"active\" && this.#state.isSessionAttached(sessionId);\n\t\t};\n\t\tconst assertActive = () => {\n\t\t\tthis.#assertNotDisposed();\n\t\t\tif (!this.connected) throw new PiDisconnectedError();\n\t\t\tif (!isActive()) throw new PiSessionDetachedError(sessionId);\n\t\t};\n\t\tconst release = (relinquishOnFailure: boolean): Promise<void> => {\n\t\t\trefreshState();\n\t\t\tif (state === \"released\" || state === \"invalidated\") return Promise.resolve();\n\t\t\tif (releasePromise) return releasePromise;\n\t\t\tassertActive();\n\t\t\tstate = \"releasing\";\n\t\t\treleasePromise = (async () => {\n\t\t\t\tconst count = this.#sessionLeaseCounts.get(sessionId) ?? 0;\n\t\t\t\tif (count <= 1) {\n\t\t\t\t\tconst detachment = this.#request({ command: \"detach\", sessionId }).then(() => undefined);\n\t\t\t\t\tthis.#sessionDetachments.set(sessionId, detachment);\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait detachment;\n\t\t\t\t\t\tthis.#releaseSessionLease(sessionId, token);\n\t\t\t\t\t} finally {\n\t\t\t\t\t\tif (this.#sessionDetachments.get(sessionId) === detachment) {\n\t\t\t\t\t\t\tthis.#sessionDetachments.delete(sessionId);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tthis.#releaseSessionLease(sessionId, token);\n\t\t\t\t}\n\t\t\t\tstate = \"released\";\n\t\t\t})().catch((error: unknown) => {\n\t\t\t\trefreshState();\n\t\t\t\tif (state === \"invalidated\") return;\n\t\t\t\tif (relinquishOnFailure) {\n\t\t\t\t\tthis.#releaseSessionLease(sessionId, token);\n\t\t\t\t\tthis.#sessionCleanupRequired.add(sessionId);\n\t\t\t\t\tstate = \"released\";\n\t\t\t\t} else {\n\t\t\t\t\tstate = \"active\";\n\t\t\t\t\treleasePromise = undefined;\n\t\t\t\t}\n\t\t\t\tthrow error;\n\t\t\t});\n\t\t\treturn releasePromise;\n\t\t};\n\t\tconst callbacks: SessionHandleCallbacks = {\n\t\t\tisAttached: isActive,\n\t\t\tgetSnapshot: () => (isActive() ? this.#state.getSessionSnapshot(sessionId) : undefined),\n\t\t\tsubscribe: (listener) => {\n\t\t\t\tassertActive();\n\t\t\t\treturn this.#state.subscribeSession(sessionId, (snapshot) => {\n\t\t\t\t\tif (isActive()) listener(snapshot);\n\t\t\t\t});\n\t\t\t},\n\t\t\tonEvent: (listener) => {\n\t\t\t\tassertActive();\n\t\t\t\treturn this.#state.onSessionEvent(sessionId, (event) => {\n\t\t\t\t\tif (isActive() || event.type === \"session_removed\") listener(event);\n\t\t\t\t});\n\t\t\t},\n\t\t\tdetach: () => release(false),\n\t\t\tdispose: () => release(true),\n\t\t\trequest: (command) => {\n\t\t\t\tassertActive();\n\t\t\t\treturn this.#request(command);\n\t\t\t},\n\t\t};\n\t\treturn new SessionHandle(sessionId, callbacks);\n\t}\n\n\t#handleMessage(message: ResponseEnvelope | EventEnvelope): void {\n\t\tif (message.type === \"event\") {\n\t\t\tif (message.event.type === \"session_removed\") this.#invalidateSessionLeases(message.event.sessionId);\n\t\t\tthis.#state.applyEvent(message.event);\n\t\t\treturn;\n\t\t}\n\t\tconst pending = this.#takePendingRequest(message.id);\n\t\tif (!pending) {\n\t\t\tthis.#connection.fail(new ProtocolValidationError(\"Response has no matching request\"));\n\t\t\treturn;\n\t\t}\n\t\tif (!message.ok) {\n\t\t\tpending.reject(new PiServerError(message.error));\n\t\t\treturn;\n\t\t}\n\t\tif (message.result.command !== pending.command.command) {\n\t\t\tconst error = new ProtocolValidationError(\n\t\t\t\t`Response command ${message.result.command} does not match ${pending.command.command}`,\n\t\t\t);\n\t\t\tpending.reject(error);\n\t\t\tthis.#connection.fail(error);\n\t\t\treturn;\n\t\t}\n\t\tthis.#state.applyResult(message.result);\n\t\tpending.resolve(message.result);\n\t}\n\n\t#handleConnectionStateChange(change: ConnectionStateChange): void {\n\t\tif (change.state === \"disconnected\") {\n\t\t\tthis.#state.clearAttachments();\n\t\t\tthis.#invalidateAllSessionLeases();\n\t\t\tthis.#rejectPendingRequests(change.error ?? new PiDisconnectedError());\n\t\t}\n\t\tthis.#notifyConnectionStateListeners(change);\n\t}\n\n\t#takePendingRequest(id: string): PendingRequest | undefined {\n\t\tconst request = this.#pendingRequests.get(id);\n\t\tif (request) this.#pendingRequests.delete(id);\n\t\treturn request;\n\t}\n\n\t#rejectPendingRequests(error: Error): void {\n\t\tconst requests = [...this.#pendingRequests.values()];\n\t\tthis.#pendingRequests.clear();\n\t\tfor (const request of requests) request.reject(error);\n\t}\n\n\tdispose(): Promise<void> {\n\t\tif (this.#disposePromise) return this.#disposePromise;\n\t\tthis.#disposed = true;\n\t\tthis.#disposePromise = Promise.resolve();\n\t\tconst error = new PiClientDisposedError();\n\t\tthis.#rejectPendingRequests(error);\n\t\tthis.#connection.disconnect(error);\n\t\tthis.#state.dispose();\n\t\tthis.#invalidateAllSessionLeases();\n\t\tthis.#connectionStateListeners.clear();\n\t\treturn this.#disposePromise;\n\t}\n\n\t[Symbol.asyncDispose](): Promise<void> {\n\t\treturn this.dispose();\n\t}\n\n\t#assertNotDisposed(): void {\n\t\tif (this.#disposed) throw new PiClientDisposedError();\n\t}\n\n\tasync #reconcileSessionCleanup(sessionId: string): Promise<boolean> {\n\t\tif (!this.#sessionCleanupRequired.has(sessionId)) return false;\n\t\tlet reconciliation = this.#sessionReconciliations.get(sessionId);\n\t\tif (!reconciliation) {\n\t\t\treconciliation = this.#request({ command: \"detach\", sessionId })\n\t\t\t\t.then(() => undefined)\n\t\t\t\t.then(() => {\n\t\t\t\t\tthis.#sessionCleanupRequired.delete(sessionId);\n\t\t\t\t})\n\t\t\t\t.finally(() => {\n\t\t\t\t\tthis.#sessionReconciliations.delete(sessionId);\n\t\t\t\t});\n\t\t\tthis.#sessionReconciliations.set(sessionId, reconciliation);\n\t\t}\n\t\tawait reconciliation;\n\t\treturn true;\n\t}\n\n\t#reserveSessionLease(sessionId: string, mode: SessionLeaseMode): SessionLeaseToken {\n\t\tconst count = this.#sessionLeaseCounts.get(sessionId) ?? 0;\n\t\tif (mode === \"exclusive\" && count > 0) {\n\t\t\tthrow new PiSessionOwnershipError(sessionId, `Session ${sessionId} already has an active lease`);\n\t\t}\n\t\tif (mode === \"shared\" && this.#exclusiveSessionLeases.has(sessionId)) {\n\t\t\tthrow new PiSessionOwnershipError(sessionId, `Session ${sessionId} has an exclusive lease`);\n\t\t}\n\t\tconst token: SessionLeaseToken = { mode };\n\t\tthis.#sessionLeaseCounts.set(sessionId, count + 1);\n\t\tif (mode === \"exclusive\") this.#exclusiveSessionLeases.set(sessionId, token);\n\t\treturn token;\n\t}\n\n\t#releaseSessionLease(sessionId: string, token: SessionLeaseToken): void {\n\t\tconst count = this.#sessionLeaseCounts.get(sessionId) ?? 0;\n\t\tif (count <= 1) this.#sessionLeaseCounts.delete(sessionId);\n\t\telse this.#sessionLeaseCounts.set(sessionId, count - 1);\n\t\tif (this.#exclusiveSessionLeases.get(sessionId) === token) this.#exclusiveSessionLeases.delete(sessionId);\n\t}\n\n\t#invalidateSessionLeases(sessionId: string): void {\n\t\tthis.#sessionLeaseCounts.delete(sessionId);\n\t\tthis.#exclusiveSessionLeases.delete(sessionId);\n\t\tthis.#sessionCleanupRequired.delete(sessionId);\n\t\tthis.#sessionLeaseGenerations.set(sessionId, (this.#sessionLeaseGenerations.get(sessionId) ?? 0) + 1);\n\t}\n\n\t#invalidateAllSessionLeases(): void {\n\t\tfor (const sessionId of this.#sessionLeaseCounts.keys()) this.#invalidateSessionLeases(sessionId);\n\t\tthis.#sessionCleanupRequired.clear();\n\t}\n\n\t#notifyConnectionStateListeners(change: ConnectionStateChange): void {\n\t\tfor (const listener of this.#connectionStateListeners) {\n\t\t\ttry {\n\t\t\t\tlistener(change);\n\t\t\t} catch (error) {\n\t\t\t\tthis.#reportListenerError(error);\n\t\t\t}\n\t\t}\n\t}\n\n\t#reportListenerError(error: unknown): void {\n\t\tif (!this.#options.onListenerError) return;\n\t\ttry {\n\t\t\tthis.#options.onListenerError(toError(error));\n\t\t} catch {\n\t\t\t// Diagnostics cannot affect protocol or transport state.\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,0BAA0B,EAC1B,yBAAyB,EACzB,0BAA0B,EAC1B,4BAA4B,EAE5B,gBAAgB,EAChB,qBAAqB,EACrB,8BAA8B,EAC9B,oCAAoC,GAQpC,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAEN,mBAAmB,EACnB,UAAU,EACV,uBAAuB,GAMvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3F,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AA6BtD,MAAM,OAAO,MAAM;IACT,QAAQ,CAAgB;IACxB,WAAW,CAAa;IACxB,gBAAgB,GAAG,IAAI,GAAG,EAA0B,CAAC;IACrD,yBAAyB,GAAG,IAAI,GAAG,EAA2C,CAAC;IAC/E,oBAAoB,GAAG,IAAI,GAAG,EAA4B,CAAC;IAC3D,iBAAiB,GAAG,IAAI,GAAG,EAAiC,CAAC;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M,CAAC,MAAM,CAAC;IACtC,OAAO,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,YAAY,CAAC,2BAA2B,EAAE,YAAY,CAAC,CAAC;AAAA,CACtG","sourcesContent":["import {\n\tcreateServiceCatalogueCall,\n\tcreateServiceStateDecoder,\n\tcreateServiceSubscribeCall,\n\tcreateServiceUnsubscribeCall,\n\ttype JsonValue,\n\tparseServiceCall,\n\tparseServiceCatalogue,\n\tparseWireServiceProviderUpdate,\n\tparseWireServiceSubscriptionSnapshot,\n\ttype RemoteServiceTransport,\n\ttype ServiceCall,\n\ttype ServiceCatalogueEntry,\n\ttype ServiceMode,\n\ttype ServiceProviderUpdate,\n\ttype ServiceStateDecoder,\n\ttype ServiceSubscriptionSnapshot,\n} from \"@earendil-works/chord\";\nimport { BACKGROUND_CONTEXT } from \"@earendil-works/chord/context\";\nimport {\n\ttype AttachmentEnvelope,\n\tencodeClientMessage,\n\tisServerId,\n\tProtocolValidationError,\n\ttype ResponseEnvelope,\n\ttype RpcTarget,\n\ttype ServerHello,\n\ttype ServiceEventEnvelope,\n\ttype SessionTarget,\n} from \"@earendil-works/pi-protocol\";\nimport { Connection } from \"./connection.ts\";\nimport { ClientDisposedError, DisconnectedError, ServerError, toError } from \"./errors.ts\";\nimport { createPromiseResolvers } from \"./promise.ts\";\nimport type {\n\tAttachmentChangeListener,\n\tClientOptions,\n\tConnectionState,\n\tConnectionStateChange,\n\tServiceSubscription,\n\tUnsubscribe,\n} from \"./types.ts\";\n\ntype ServiceResult = JsonValue | undefined;\n\ninterface PendingRequest {\n\tresolve(result: ServiceResult): void;\n\treject(error: Error): void;\n\tcleanup(): void;\n}\n\ninterface ActiveServiceListener {\n\treadonly target: RpcTarget;\n\treadonly listener: (update: ServiceProviderUpdate) => void | Promise<void>;\n\treadonly decoder: ServiceStateDecoder;\n\treadonly queuedWireUpdates: JsonValue[];\n\treadonly queued: ServiceProviderUpdate[];\n\tdeliveryTail: Promise<void>;\n\thydrated: boolean;\n\tready: boolean;\n}\n\nexport class Client {\n\treadonly #options: ClientOptions;\n\treadonly #connection: Connection;\n\treadonly #pendingRequests = new Map<string, PendingRequest>();\n\treadonly #connectionStateListeners = new Set<(change: ConnectionStateChange) => void>();\n\treadonly #attachmentListeners = new Set<AttachmentChangeListener>();\n\treadonly #serviceListeners = new Map<string, ActiveServiceListener>();\n\t#requestSequence = 0;\n\t#serviceSubscriptionSequence = 0;\n\t#hello: ServerHello | undefined;\n\t#attachment: SessionTarget | undefined;\n\t#disposed = false;\n\t#disposePromise: Promise<void> | undefined;\n\n\tconstructor(options: ClientOptions) {\n\t\tif (!isServerId(options.serverId)) {\n\t\t\tthrow new TypeError(\"serverId must be a canonical lowercase UUIDv4\");\n\t\t}\n\t\tthis.#options = options;\n\t\tthis.#connection = new Connection({\n\t\t\ttransportFactory: options.transportFactory,\n\t\t\tserverId: options.serverId,\n\t\t\tmaxFrameLength: options.maxFrameLength,\n\t\t\tonHandshake: (hello) => {\n\t\t\t\tthis.#hello = hello;\n\t\t\t},\n\t\t\tonMessage: (message) => this.#handleMessage(message),\n\t\t\tonStateChange: (change) => this.#handleConnectionStateChange(change),\n\t\t});\n\t}\n\n\tget disposed(): boolean {\n\t\treturn this.#disposed;\n\t}\n\n\tget connectionState(): ConnectionState {\n\t\treturn this.#connection.state;\n\t}\n\n\tget connected(): boolean {\n\t\treturn this.#connection.state === \"connected\";\n\t}\n\n\tget serverId(): string {\n\t\treturn this.#options.serverId;\n\t}\n\n\tget hello(): ServerHello | undefined {\n\t\treturn this.#hello;\n\t}\n\n\tget attachment(): SessionTarget | undefined {\n\t\treturn this.#attachment;\n\t}\n\n\tstatic async connect(options: ClientOptions): Promise<Client> {\n\t\tconst client = new Client(options);\n\t\ttry {\n\t\t\tawait client.connect();\n\t\t\treturn client;\n\t\t} catch (error) {\n\t\t\tawait client.dispose();\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tconnect(): Promise<ServerHello> {\n\t\tif (this.#disposed) return Promise.reject(new ClientDisposedError());\n\t\tthis.#hello = undefined;\n\t\treturn this.#connection.connect();\n\t}\n\n\treconnect(): Promise<ServerHello> {\n\t\treturn this.connect();\n\t}\n\n\tdisconnect(reason = \"Client disconnected\"): void {\n\t\tthis.#connection.disconnect(reason);\n\t}\n\n\tonConnectionStateChange(listener: (change: ConnectionStateChange) => void): Unsubscribe {\n\t\tthis.#assertNotDisposed();\n\t\tthis.#connectionStateListeners.add(listener);\n\t\treturn () => this.#connectionStateListeners.delete(listener);\n\t}\n\n\tonAttachmentChange(listener: AttachmentChangeListener): Unsubscribe {\n\t\tthis.#assertNotDisposed();\n\t\tthis.#attachmentListeners.add(listener);\n\t\treturn () => this.#attachmentListeners.delete(listener);\n\t}\n\n\t/** Invoke one low-level protocol call against an explicit routed target. */\n\trequest(target: RpcTarget, call: ServiceCall, signal?: AbortSignal): Promise<ServiceResult> {\n\t\treturn this.#request(target, call, signal);\n\t}\n\n\tasync serviceCatalogue(target: RpcTarget, signal?: AbortSignal): Promise<readonly ServiceCatalogueEntry[]> {\n\t\tconst result = await this.#request(target, createServiceCatalogueCall(), signal);\n\t\ttry {\n\t\t\treturn parseServiceCatalogue(result);\n\t\t} catch (error) {\n\t\t\tconst validationError = new ProtocolValidationError(\n\t\t\t\terror instanceof Error ? error.message : \"Invalid service catalogue\",\n\t\t\t);\n\t\t\tthis.#connection.fail(validationError);\n\t\t\tthrow validationError;\n\t\t}\n\t}\n\n\tasync subscribeService(\n\t\ttarget: RpcTarget,\n\t\tserviceId: string,\n\t\tmode: ServiceMode,\n\t\tlistener: (update: ServiceProviderUpdate) => void | Promise<void>,\n\t\tsignal?: AbortSignal,\n\t): Promise<ServiceSubscription> {\n\t\tconst subscriptionId = `service-${++this.#serviceSubscriptionSequence}`;\n\t\tconst active: ActiveServiceListener = {\n\t\t\ttarget,\n\t\t\tlistener,\n\t\t\tdecoder: createServiceStateDecoder(),\n\t\t\tqueuedWireUpdates: [],\n\t\t\tqueued: [],\n\t\t\tdeliveryTail: Promise.resolve(),\n\t\t\thydrated: false,\n\t\t\tready: false,\n\t\t};\n\t\tthis.#serviceListeners.set(subscriptionId, active);\n\t\tlet snapshot: ServiceSubscriptionSnapshot;\n\t\ttry {\n\t\t\tsnapshot = await this.#request(\n\t\t\t\ttarget,\n\t\t\t\tcreateServiceSubscribeCall(subscriptionId, serviceId, mode),\n\t\t\t\tsignal,\n\t\t\t\t(result) => {\n\t\t\t\t\tconst decoded = active.decoder.decodeSnapshot(parseWireServiceSubscriptionSnapshot(result));\n\t\t\t\t\tactive.hydrated = true;\n\t\t\t\t\tfor (const update of active.queuedWireUpdates.splice(0)) {\n\t\t\t\t\t\tactive.queued.push(active.decoder.decodeUpdate(parseWireServiceProviderUpdate(update)));\n\t\t\t\t\t}\n\t\t\t\t\treturn decoded;\n\t\t\t\t},\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tif (this.#serviceListeners.get(subscriptionId) === active) this.#serviceListeners.delete(subscriptionId);\n\t\t\tthrow error;\n\t\t}\n\t\tif (this.#serviceListeners.get(subscriptionId) !== active) throw new DisconnectedError();\n\t\tlet disposed = false;\n\t\treturn {\n\t\t\tid: subscriptionId,\n\t\t\ttarget,\n\t\t\tsnapshot,\n\t\t\tstart: () => {\n\t\t\t\tif (disposed || active.ready) return;\n\t\t\t\tactive.ready = true;\n\t\t\t\tfor (const update of active.queued.splice(0)) this.#deliverServiceUpdate(active, update);\n\t\t\t},\n\t\t\tdispose: async () => {\n\t\t\t\tif (disposed) return;\n\t\t\t\tdisposed = true;\n\t\t\t\tif (this.#serviceListeners.get(subscriptionId) === active) this.#serviceListeners.delete(subscriptionId);\n\t\t\t\ttry {\n\t\t\t\t\tif (this.connected && this.#targetIsCurrent(target)) {\n\t\t\t\t\t\tawait this.#request(target, createServiceUnsubscribeCall(subscriptionId));\n\t\t\t\t\t}\n\t\t\t\t\tawait active.deliveryTail;\n\t\t\t\t} finally {\n\t\t\t\t\tactive.queuedWireUpdates.length = 0;\n\t\t\t\t\tactive.queued.length = 0;\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t}\n\n\t#request<T = ServiceResult>(\n\t\ttarget: RpcTarget,\n\t\tcall: ServiceCall,\n\t\tsignal?: AbortSignal,\n\t\ttransform?: (result: ServiceResult) => T,\n\t): Promise<T> {\n\t\tif (this.#disposed) return Promise.reject(new ClientDisposedError());\n\t\tif (!this.connected) return Promise.reject(new DisconnectedError());\n\t\tif (signal?.aborted) return Promise.reject(abortError(signal));\n\t\tconst id = `request-${++this.#requestSequence}`;\n\t\tconst { promise, resolve, reject } = createPromiseResolvers<T>();\n\t\tlet sent = false;\n\t\tlet aborted = false;\n\t\tlet onAbort: (() => void) | undefined;\n\t\tconst sendCancel = (): void => {\n\t\t\tif (!sent || !this.connected) return;\n\t\t\ttry {\n\t\t\t\tthis.#connection.send(\n\t\t\t\t\tencodeClientMessage({ type: \"cancel\", id, target }, { maxFrameLength: this.#connection.maxFrameLength }),\n\t\t\t\t);\n\t\t\t} catch (error) {\n\t\t\t\tthis.#connection.fail(toError(error));\n\t\t\t}\n\t\t};\n\t\tif (signal !== undefined) {\n\t\t\tonAbort = () => {\n\t\t\t\tif (aborted) return;\n\t\t\t\taborted = true;\n\t\t\t\treject(abortError(signal));\n\t\t\t\tsendCancel();\n\t\t\t};\n\t\t\tsignal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t}\n\t\tthis.#pendingRequests.set(id, {\n\t\t\tresolve: (result) => {\n\t\t\t\ttry {\n\t\t\t\t\tresolve(transform === undefined ? (result as T) : transform(result));\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst validationError = new ProtocolValidationError(\n\t\t\t\t\t\terror instanceof Error ? error.message : \"Invalid service operation stream\",\n\t\t\t\t\t);\n\t\t\t\t\tthis.#connection.fail(validationError);\n\t\t\t\t\treject(validationError);\n\t\t\t\t}\n\t\t\t},\n\t\t\treject,\n\t\t\tcleanup: () => {\n\t\t\t\tif (signal !== undefined && onAbort !== undefined) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t},\n\t\t});\n\t\tlet frame: Uint8Array;\n\t\ttry {\n\t\t\tframe = encodeClientMessage(\n\t\t\t\t{ type: \"request\", id, target, call: parseServiceCall(call) as unknown as JsonValue },\n\t\t\t\t{ maxFrameLength: this.#connection.maxFrameLength },\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tthis.#takePendingRequest(id)?.reject(toError(error));\n\t\t\treturn promise;\n\t\t}\n\t\tthis.#connection.send(frame);\n\t\tsent = true;\n\t\tif (aborted) sendCancel();\n\t\treturn promise;\n\t}\n\n\t#handleMessage(message: ResponseEnvelope | ServiceEventEnvelope | AttachmentEnvelope): void {\n\t\tif (message.type === \"attachment\") {\n\t\t\tif (message.attachment !== null && message.attachment.serverId !== this.#options.serverId) {\n\t\t\t\tthis.#connection.fail(new ProtocolValidationError(\"Attachment update belongs to another server\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthis.#setAttachment(message.attachment ?? undefined);\n\t\t\treturn;\n\t\t}\n\t\tif (message.type === \"service_update\") {\n\t\t\tconst active = this.#serviceListeners.get(message.subscriptionId);\n\t\t\tif (active === undefined) return;\n\t\t\tif (!active.hydrated) {\n\t\t\t\tactive.queuedWireUpdates.push(message.update);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tlet update: ServiceProviderUpdate;\n\t\t\ttry {\n\t\t\t\tupdate = active.decoder.decodeUpdate(parseWireServiceProviderUpdate(message.update));\n\t\t\t} catch (error) {\n\t\t\t\tthis.#connection.fail(\n\t\t\t\t\tnew ProtocolValidationError(error instanceof Error ? error.message : \"Invalid service operation stream\"),\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (active.ready) this.#deliverServiceUpdate(active, update);\n\t\t\telse active.queued.push(update);\n\t\t\treturn;\n\t\t}\n\t\tconst pending = this.#takePendingRequest(message.id);\n\t\tif (!pending) {\n\t\t\tthis.#connection.fail(new ProtocolValidationError(\"Response has no matching request\"));\n\t\t\treturn;\n\t\t}\n\t\tif (!message.ok) {\n\t\t\tpending.reject(new ServerError(message.error));\n\t\t\treturn;\n\t\t}\n\t\tpending.resolve(message.result);\n\t}\n\n\t#handleConnectionStateChange(change: ConnectionStateChange): void {\n\t\tif (change.state === \"disconnected\") {\n\t\t\tthis.#hello = undefined;\n\t\t\tthis.#setAttachment(undefined);\n\t\t\tthis.#rejectPendingRequests(change.error ?? new DisconnectedError());\n\t\t\tthis.#serviceListeners.clear();\n\t\t}\n\t\tfor (const listener of this.#connectionStateListeners) {\n\t\t\ttry {\n\t\t\t\tlistener(change);\n\t\t\t} catch (error) {\n\t\t\t\tthis.#reportListenerError(error);\n\t\t\t}\n\t\t}\n\t}\n\n\t#takePendingRequest(id: string): PendingRequest | undefined {\n\t\tconst request = this.#pendingRequests.get(id);\n\t\tif (request) {\n\t\t\tthis.#pendingRequests.delete(id);\n\t\t\trequest.cleanup();\n\t\t}\n\t\treturn request;\n\t}\n\n\t#rejectPendingRequests(error: Error): void {\n\t\tconst requests = [...this.#pendingRequests.values()];\n\t\tthis.#pendingRequests.clear();\n\t\tfor (const request of requests) {\n\t\t\trequest.cleanup();\n\t\t\trequest.reject(error);\n\t\t}\n\t}\n\n\tdispose(): Promise<void> {\n\t\tif (this.#disposePromise) return this.#disposePromise;\n\t\tthis.#disposed = true;\n\t\tthis.#disposePromise = Promise.resolve();\n\t\tconst error = new ClientDisposedError();\n\t\tthis.#rejectPendingRequests(error);\n\t\tthis.#connection.disconnect(error);\n\t\tthis.#hello = undefined;\n\t\tthis.#setAttachment(undefined);\n\t\tthis.#connectionStateListeners.clear();\n\t\tthis.#attachmentListeners.clear();\n\t\tthis.#serviceListeners.clear();\n\t\treturn this.#disposePromise;\n\t}\n\n\t[Symbol.asyncDispose](): Promise<void> {\n\t\treturn this.dispose();\n\t}\n\n\t#setAttachment(attachment: SessionTarget | undefined): void {\n\t\tconst previous = this.#attachment;\n\t\tif (\n\t\t\tprevious?.serverId === attachment?.serverId &&\n\t\t\tprevious?.sessionId === attachment?.sessionId &&\n\t\t\tprevious?.attachmentId === attachment?.attachmentId\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tthis.#attachment = attachment;\n\t\tfor (const listener of this.#attachmentListeners) {\n\t\t\ttry {\n\t\t\t\tlistener(attachment);\n\t\t\t} catch (error) {\n\t\t\t\tthis.#reportListenerError(error);\n\t\t\t}\n\t\t}\n\t}\n\n\t#deliverServiceUpdate(active: ActiveServiceListener, update: ServiceProviderUpdate): void {\n\t\tactive.deliveryTail = active.deliveryTail\n\t\t\t.then(() => active.listener(update))\n\t\t\t.catch((error: unknown) => this.#reportListenerError(error));\n\t}\n\n\t#targetIsCurrent(target: RpcTarget): boolean {\n\t\tif (!(\"sessionId\" in target)) return this.#hello?.serverId === target.serverId;\n\t\tconst attachment = this.#attachment;\n\t\treturn (\n\t\t\tattachment?.serverId === target.serverId &&\n\t\t\tattachment.sessionId === target.sessionId &&\n\t\t\tattachment.attachmentId === target.attachmentId\n\t\t);\n\t}\n\n\t#assertNotDisposed(): void {\n\t\tif (this.#disposed) throw new ClientDisposedError();\n\t}\n\n\t#reportListenerError(error: unknown): void {\n\t\tif (!this.#options.onListenerError) return;\n\t\ttry {\n\t\t\tthis.#options.onListenerError(toError(error));\n\t\t} catch {\n\t\t\t// Diagnostics cannot affect protocol or transport state.\n\t\t}\n\t}\n}\n\n/** Adapts a lazily resolved routed client target to a Chord service transport. */\nexport function createClientServiceTransport(\n\tclient: Client,\n\tgetTarget: () => RpcTarget | undefined,\n): RemoteServiceTransport {\n\tconst target = (): RpcTarget => {\n\t\tconst resolved = getTarget();\n\t\tif (resolved === undefined) throw new Error(\"Remote service target is unavailable\");\n\t\treturn resolved;\n\t};\n\treturn {\n\t\tinvoke: async (call, context) => client.request(target(), call, context.abortSignal),\n\t\tasync subscribe(serviceId, mode, listener, context) {\n\t\t\tconst subscription = await client.subscribeService(\n\t\t\t\ttarget(),\n\t\t\t\tserviceId,\n\t\t\t\tmode,\n\t\t\t\t(update) => listener(update, BACKGROUND_CONTEXT),\n\t\t\t\tcontext.abortSignal,\n\t\t\t);\n\t\t\treturn {\n\t\t\t\tsnapshot: subscription.snapshot,\n\t\t\t\tactivate: () => subscription.start(),\n\t\t\t\tclose: () => subscription.dispose(),\n\t\t\t};\n\t\t},\n\t};\n}\n\nfunction abortError(signal: AbortSignal): Error {\n\tconst reason: unknown = signal.reason;\n\treturn reason instanceof Error ? reason : new DOMException(\"The operation was aborted\", \"AbortError\");\n}\n"]}
@@ -1,10 +1,11 @@
1
- import { type ServerMessage, type ServerSnapshot } from "@earendil-works/pi-protocol";
1
+ import { type ServerHello, type ServerMessage } from "@earendil-works/pi-protocol";
2
2
  import type { ByteTransportFactory } from "./transport.ts";
3
3
  import type { ConnectionState, ConnectionStateChange } from "./types.ts";
4
4
  interface ConnectionOptions {
5
5
  transportFactory: ByteTransportFactory;
6
+ serverId: string;
6
7
  maxFrameLength?: number;
7
- onHandshake(snapshot: ServerSnapshot): void;
8
+ onHandshake(hello: ServerHello): void;
8
9
  onMessage(message: Exclude<ServerMessage, {
9
10
  type: "hello" | "hello_error";
10
11
  }>): void;
@@ -15,7 +16,7 @@ export declare class Connection {
15
16
  constructor(options: ConnectionOptions);
16
17
  get state(): ConnectionState;
17
18
  get maxFrameLength(): number;
18
- connect(): Promise<ServerSnapshot>;
19
+ connect(): Promise<ServerHello>;
19
20
  disconnect(reason?: string | Error): void;
20
21
  fail(error: Error): void;
21
22
  send(frame: Uint8Array): void;
@@ -1 +1 @@
1
- {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,KAAK,aAAa,EAElB,KAAK,cAAc,EACnB,MAAM,6BAA6B,CAAC;AAGrC,OAAO,KAAK,EAAiB,oBAAoB,EAAyB,MAAM,gBAAgB,CAAC;AACjG,OAAO,KAAK,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAmBzE,UAAU,iBAAiB;IAC1B,gBAAgB,EAAE,oBAAoB,CAAC;IACvC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,QAAQ,EAAE,cAAc,GAAG,IAAI,CAAC;IAC5C,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,aAAa,EAAE;QAAE,IAAI,EAAE,OAAO,GAAG,aAAa,CAAA;KAAE,CAAC,GAAG,IAAI,CAAC;IACpF,aAAa,CAAC,MAAM,EAAE,qBAAqB,GAAG,IAAI,CAAC;CACnD;AAED,qBAAa,UAAU;;IAMtB,YAAY,OAAO,EAAE,iBAAiB,EAUrC;IAED,IAAI,KAAK,IAAI,eAAe,CAE3B;IAED,IAAI,cAAc,IAAI,MAAM,CAE3B;IAED,OAAO,IAAI,OAAO,CAAC,cAAc,CAAC,CAwBjC;IAED,UAAU,CAAC,MAAM,GAAE,MAAM,GAAG,KAA6B,GAAG,IAAI,CAG/D;IAED,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAEvB;IAED,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAgB5B;CAuHD","sourcesContent":["import {\n\tDEFAULT_MAX_FRAME_LENGTH,\n\tencodeClientMessage,\n\tPROTOCOL_VERSION,\n\tProtocolValidationError,\n\ttype ServerMessage,\n\tServerMessageDecoder,\n\ttype ServerSnapshot,\n} from \"@earendil-works/pi-protocol\";\nimport { PiDisconnectedError, PiServerError, toDisconnectedError, toError } from \"./errors.ts\";\nimport { createPromiseResolvers, type PromiseResolvers } from \"./promise.ts\";\nimport type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from \"./transport.ts\";\nimport type { ConnectionState, ConnectionStateChange } from \"./types.ts\";\n\nconst MAX_UINT32 = 0xffff_ffff;\n\ntype ActiveConnection = {\n\tid: number;\n\tdecoder: ServerMessageDecoder;\n\ttransport?: ByteTransport;\n};\n\ntype ConnectionLifecycle =\n\t| { state: \"disconnected\" }\n\t| ({ state: \"connecting\"; handshake: PromiseResolvers<ServerSnapshot> } & ActiveConnection)\n\t| ({\n\t\t\tstate: \"connected\";\n\t\t\ttransport: ByteTransport;\n\t\t\thandshake: PromiseResolvers<ServerSnapshot> | undefined;\n\t } & ActiveConnection);\n\ninterface ConnectionOptions {\n\ttransportFactory: ByteTransportFactory;\n\tmaxFrameLength?: number;\n\tonHandshake(snapshot: ServerSnapshot): void;\n\tonMessage(message: Exclude<ServerMessage, { type: \"hello\" | \"hello_error\" }>): void;\n\tonStateChange(change: ConnectionStateChange): void;\n}\n\nexport class Connection {\n\treadonly #options: ConnectionOptions;\n\treadonly #maxFrameLength: number;\n\t#lifecycle: ConnectionLifecycle = { state: \"disconnected\" };\n\t#sequence = 0;\n\n\tconstructor(options: ConnectionOptions) {\n\t\tthis.#options = options;\n\t\tthis.#maxFrameLength = options.maxFrameLength ?? DEFAULT_MAX_FRAME_LENGTH;\n\t\tif (\n\t\t\t!Number.isSafeInteger(this.#maxFrameLength) ||\n\t\t\tthis.#maxFrameLength <= 0 ||\n\t\t\tthis.#maxFrameLength > MAX_UINT32\n\t\t) {\n\t\t\tthrow new TypeError(`PiClient maxFrameLength must be an integer between 1 and ${MAX_UINT32}`);\n\t\t}\n\t}\n\n\tget state(): ConnectionState {\n\t\treturn this.#lifecycle.state;\n\t}\n\n\tget maxFrameLength(): number {\n\t\treturn this.#maxFrameLength;\n\t}\n\n\tconnect(): Promise<ServerSnapshot> {\n\t\tif (this.#lifecycle.state !== \"disconnected\") {\n\t\t\treturn Promise.reject(new PiDisconnectedError(`PiClient is already ${this.#lifecycle.state}`));\n\t\t}\n\t\tconst id = ++this.#sequence;\n\t\tconst handshake = createPromiseResolvers<ServerSnapshot>();\n\t\tthis.#lifecycle = {\n\t\t\tstate: \"connecting\",\n\t\t\tid,\n\t\t\tdecoder: new ServerMessageDecoder({ maxFrameLength: this.#maxFrameLength }),\n\t\t\thandshake,\n\t\t};\n\t\tthis.#options.onStateChange({ state: \"connecting\" });\n\t\tconst handlers = {\n\t\t\tonData: (chunk) => this.#handleData(id, chunk),\n\t\t\tonClose: () => {\n\t\t\t\tif (this.#isCurrent(id)) this.#handleClose();\n\t\t\t},\n\t\t\tonError: (error) => {\n\t\t\t\tif (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));\n\t\t\t},\n\t\t} satisfies ByteTransportHandlers;\n\t\tvoid this.#openTransport(id, handlers);\n\t\treturn handshake.promise;\n\t}\n\n\tdisconnect(reason: string | Error = \"Client disconnected\"): void {\n\t\tif (this.#lifecycle.state === \"disconnected\") return;\n\t\tthis.#failAndClose(typeof reason === \"string\" ? new PiDisconnectedError(reason) : reason);\n\t}\n\n\tfail(error: Error): void {\n\t\tthis.#failAndClose(error);\n\t}\n\n\tsend(frame: Uint8Array): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state !== \"connected\") throw new PiDisconnectedError();\n\t\tlet sending: Promise<void>;\n\t\ttry {\n\t\t\tsending = lifecycle.transport.send(frame);\n\t\t} catch (error) {\n\t\t\tthis.#failAndClose(toDisconnectedError(error));\n\t\t\treturn;\n\t\t}\n\t\tvoid sending.catch((error: unknown) => {\n\t\t\tconst current = this.#lifecycle;\n\t\t\tif (current.state !== \"disconnected\" && current.transport === lifecycle.transport) {\n\t\t\t\tthis.#failAndClose(toDisconnectedError(error));\n\t\t\t}\n\t\t});\n\t}\n\n\tasync #openTransport(id: number, handlers: ByteTransportHandlers): Promise<void> {\n\t\tlet transport: ByteTransport;\n\t\ttry {\n\t\t\ttransport = await this.#options.transportFactory(handlers);\n\t\t} catch (error) {\n\t\t\tif (this.#isCurrent(id)) this.#fail(toDisconnectedError(error));\n\t\t\treturn;\n\t\t}\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state !== \"connecting\" || lifecycle.id !== id) {\n\t\t\ttransport.close();\n\t\t\treturn;\n\t\t}\n\t\tthis.#lifecycle = { ...lifecycle, transport };\n\t\ttry {\n\t\t\tawait transport.send(\n\t\t\t\tencodeClientMessage({ type: \"hello\", version: PROTOCOL_VERSION }, { maxFrameLength: this.#maxFrameLength }),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tif (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));\n\t\t}\n\t}\n\n\t#handleData(id: number, chunk: Uint8Array): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\" || lifecycle.id !== id) return;\n\t\tif (lifecycle.state === \"connecting\" && !lifecycle.transport) {\n\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Received server data before the client hello was sent\"));\n\t\t\treturn;\n\t\t}\n\t\tlet messages: ServerMessage[];\n\t\ttry {\n\t\t\tmessages = lifecycle.decoder.push(chunk);\n\t\t} catch (error) {\n\t\t\tthis.#failAndClose(toError(error));\n\t\t\treturn;\n\t\t}\n\t\tfor (const message of messages) {\n\t\t\tif (this.#lifecycle.state === \"disconnected\") return;\n\t\t\tthis.#handleMessage(message);\n\t\t}\n\t}\n\n\t#handleMessage(message: ServerMessage): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"connecting\") {\n\t\t\tif (message.type === \"hello_error\") {\n\t\t\t\tthis.#failAndClose(new PiServerError(message.error));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (message.type !== \"hello\") {\n\t\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Expected server hello as first message\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!lifecycle.transport) {\n\t\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Received server hello before the client hello was sent\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst connected = {\n\t\t\t\tstate: \"connected\",\n\t\t\t\tid: lifecycle.id,\n\t\t\t\tdecoder: lifecycle.decoder,\n\t\t\t\ttransport: lifecycle.transport,\n\t\t\t\thandshake: lifecycle.handshake,\n\t\t\t} satisfies Extract<ConnectionLifecycle, { state: \"connected\" }>;\n\t\t\tthis.#lifecycle = connected;\n\t\t\ttry {\n\t\t\t\tthis.#options.onHandshake(message.snapshot);\n\t\t\t} catch (error) {\n\t\t\t\tif (this.#lifecycle === connected) this.#failAndClose(toError(error));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (this.#lifecycle !== connected) return;\n\t\t\tthis.#options.onStateChange({ state: \"connected\" });\n\t\t\tif (this.#lifecycle !== connected) return;\n\t\t\tthis.#lifecycle = { ...connected, handshake: undefined };\n\t\t\tlifecycle.handshake.resolve(message.snapshot);\n\t\t\treturn;\n\t\t}\n\t\tif (lifecycle.state !== \"connected\") return;\n\t\tif (message.type === \"hello\" || message.type === \"hello_error\") {\n\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Unexpected handshake message\"));\n\t\t\treturn;\n\t\t}\n\t\tthis.#options.onMessage(message);\n\t}\n\n\t#handleClose(): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\") return;\n\t\tlet error: Error = new PiDisconnectedError(\"Byte transport closed\");\n\t\ttry {\n\t\t\tlifecycle.decoder.end();\n\t\t} catch (decoderError) {\n\t\t\terror = toError(decoderError);\n\t\t}\n\t\tthis.#fail(error);\n\t}\n\n\t#failAndClose(error: Error): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tconst transport = lifecycle.state === \"disconnected\" ? undefined : lifecycle.transport;\n\t\tthis.#fail(error);\n\t\ttransport?.close();\n\t}\n\n\t#fail(error: Error): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\") return;\n\t\tthis.#lifecycle = { state: \"disconnected\" };\n\t\tlifecycle.handshake?.reject(error);\n\t\tthis.#options.onStateChange({ state: \"disconnected\", error });\n\t}\n\n\t#isCurrent(id: number): boolean {\n\t\treturn this.#lifecycle.state !== \"disconnected\" && this.#lifecycle.id === id;\n\t}\n}\n"]}
1
+ {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,KAAK,WAAW,EAChB,KAAK,aAAa,EAElB,MAAM,6BAA6B,CAAC;AAGrC,OAAO,KAAK,EAAiB,oBAAoB,EAAyB,MAAM,gBAAgB,CAAC;AACjG,OAAO,KAAK,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAmBzE,UAAU,iBAAiB;IAC1B,gBAAgB,EAAE,oBAAoB,CAAC;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;IACtC,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,aAAa,EAAE;QAAE,IAAI,EAAE,OAAO,GAAG,aAAa,CAAA;KAAE,CAAC,GAAG,IAAI,CAAC;IACpF,aAAa,CAAC,MAAM,EAAE,qBAAqB,GAAG,IAAI,CAAC;CACnD;AAED,qBAAa,UAAU;;IAMtB,YAAY,OAAO,EAAE,iBAAiB,EAUrC;IAED,IAAI,KAAK,IAAI,eAAe,CAE3B;IAED,IAAI,cAAc,IAAI,MAAM,CAE3B;IAED,OAAO,IAAI,OAAO,CAAC,WAAW,CAAC,CAwB9B;IAED,UAAU,CAAC,MAAM,GAAE,MAAM,GAAG,KAA6B,GAAG,IAAI,CAG/D;IAED,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAEvB;IAED,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAgB5B;CA+HD","sourcesContent":["import {\n\tDEFAULT_MAX_FRAME_LENGTH,\n\tencodeClientMessage,\n\tPROTOCOL_VERSION,\n\tProtocolValidationError,\n\ttype ServerHello,\n\ttype ServerMessage,\n\tServerMessageDecoder,\n} from \"@earendil-works/pi-protocol\";\nimport { DisconnectedError, ServerError, toDisconnectedError, toError } from \"./errors.ts\";\nimport { createPromiseResolvers, type PromiseResolvers } from \"./promise.ts\";\nimport type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from \"./transport.ts\";\nimport type { ConnectionState, ConnectionStateChange } from \"./types.ts\";\n\nconst MAX_UINT32 = 0xffff_ffff;\n\ntype ActiveConnection = {\n\tid: number;\n\tdecoder: ServerMessageDecoder;\n\ttransport?: ByteTransport;\n};\n\ntype ConnectionLifecycle =\n\t| { state: \"disconnected\" }\n\t| ({ state: \"connecting\"; handshake: PromiseResolvers<ServerHello> } & ActiveConnection)\n\t| ({\n\t\t\tstate: \"connected\";\n\t\t\ttransport: ByteTransport;\n\t\t\thandshake: PromiseResolvers<ServerHello> | undefined;\n\t } & ActiveConnection);\n\ninterface ConnectionOptions {\n\ttransportFactory: ByteTransportFactory;\n\tserverId: string;\n\tmaxFrameLength?: number;\n\tonHandshake(hello: ServerHello): void;\n\tonMessage(message: Exclude<ServerMessage, { type: \"hello\" | \"hello_error\" }>): void;\n\tonStateChange(change: ConnectionStateChange): void;\n}\n\nexport class Connection {\n\treadonly #options: ConnectionOptions;\n\treadonly #maxFrameLength: number;\n\t#lifecycle: ConnectionLifecycle = { state: \"disconnected\" };\n\t#sequence = 0;\n\n\tconstructor(options: ConnectionOptions) {\n\t\tthis.#options = options;\n\t\tthis.#maxFrameLength = options.maxFrameLength ?? DEFAULT_MAX_FRAME_LENGTH;\n\t\tif (\n\t\t\t!Number.isSafeInteger(this.#maxFrameLength) ||\n\t\t\tthis.#maxFrameLength <= 0 ||\n\t\t\tthis.#maxFrameLength > MAX_UINT32\n\t\t) {\n\t\t\tthrow new TypeError(`Client maxFrameLength must be an integer between 1 and ${MAX_UINT32}`);\n\t\t}\n\t}\n\n\tget state(): ConnectionState {\n\t\treturn this.#lifecycle.state;\n\t}\n\n\tget maxFrameLength(): number {\n\t\treturn this.#maxFrameLength;\n\t}\n\n\tconnect(): Promise<ServerHello> {\n\t\tif (this.#lifecycle.state !== \"disconnected\") {\n\t\t\treturn Promise.reject(new DisconnectedError(`Client is already ${this.#lifecycle.state}`));\n\t\t}\n\t\tconst id = ++this.#sequence;\n\t\tconst handshake = createPromiseResolvers<ServerHello>();\n\t\tthis.#lifecycle = {\n\t\t\tstate: \"connecting\",\n\t\t\tid,\n\t\t\tdecoder: new ServerMessageDecoder({ maxFrameLength: this.#maxFrameLength }),\n\t\t\thandshake,\n\t\t};\n\t\tthis.#options.onStateChange({ state: \"connecting\" });\n\t\tconst handlers = {\n\t\t\tonData: (chunk) => this.#handleData(id, chunk),\n\t\t\tonClose: () => {\n\t\t\t\tif (this.#isCurrent(id)) this.#handleClose();\n\t\t\t},\n\t\t\tonError: (error) => {\n\t\t\t\tif (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));\n\t\t\t},\n\t\t} satisfies ByteTransportHandlers;\n\t\tvoid this.#openTransport(id, handlers);\n\t\treturn handshake.promise;\n\t}\n\n\tdisconnect(reason: string | Error = \"Client disconnected\"): void {\n\t\tif (this.#lifecycle.state === \"disconnected\") return;\n\t\tthis.#failAndClose(typeof reason === \"string\" ? new DisconnectedError(reason) : reason);\n\t}\n\n\tfail(error: Error): void {\n\t\tthis.#failAndClose(error);\n\t}\n\n\tsend(frame: Uint8Array): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state !== \"connected\") throw new DisconnectedError();\n\t\tlet sending: Promise<void>;\n\t\ttry {\n\t\t\tsending = lifecycle.transport.send(frame);\n\t\t} catch (error) {\n\t\t\tthis.#failAndClose(toDisconnectedError(error));\n\t\t\treturn;\n\t\t}\n\t\tvoid sending.catch((error: unknown) => {\n\t\t\tconst current = this.#lifecycle;\n\t\t\tif (current.state !== \"disconnected\" && current.transport === lifecycle.transport) {\n\t\t\t\tthis.#failAndClose(toDisconnectedError(error));\n\t\t\t}\n\t\t});\n\t}\n\n\tasync #openTransport(id: number, handlers: ByteTransportHandlers): Promise<void> {\n\t\tlet transport: ByteTransport;\n\t\ttry {\n\t\t\ttransport = await this.#options.transportFactory(handlers);\n\t\t} catch (error) {\n\t\t\tif (this.#isCurrent(id)) this.#fail(toDisconnectedError(error));\n\t\t\treturn;\n\t\t}\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state !== \"connecting\" || lifecycle.id !== id) {\n\t\t\ttransport.close();\n\t\t\treturn;\n\t\t}\n\t\tthis.#lifecycle = { ...lifecycle, transport };\n\t\ttry {\n\t\t\tawait transport.send(\n\t\t\t\tencodeClientMessage({ type: \"hello\", version: PROTOCOL_VERSION }, { maxFrameLength: this.#maxFrameLength }),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tif (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));\n\t\t}\n\t}\n\n\t#handleData(id: number, chunk: Uint8Array): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\" || lifecycle.id !== id) return;\n\t\tif (lifecycle.state === \"connecting\" && !lifecycle.transport) {\n\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Received server data before the client hello was sent\"));\n\t\t\treturn;\n\t\t}\n\t\tlet messages: ServerMessage[];\n\t\ttry {\n\t\t\tmessages = lifecycle.decoder.push(chunk);\n\t\t} catch (error) {\n\t\t\tthis.#failAndClose(toError(error));\n\t\t\treturn;\n\t\t}\n\t\tfor (const message of messages) {\n\t\t\tif (this.#lifecycle.state === \"disconnected\") return;\n\t\t\tthis.#handleMessage(message);\n\t\t}\n\t}\n\n\t#handleMessage(message: ServerMessage): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"connecting\") {\n\t\t\tif (message.type === \"hello_error\") {\n\t\t\t\tthis.#failAndClose(new ServerError(message.error));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (message.type !== \"hello\") {\n\t\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Expected server hello as first message\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (message.serverId !== this.#options.serverId) {\n\t\t\t\tthis.#failAndClose(\n\t\t\t\t\tnew ProtocolValidationError(\n\t\t\t\t\t\t`Connected server ${JSON.stringify(message.serverId)} does not match ${JSON.stringify(this.#options.serverId)}`,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!lifecycle.transport) {\n\t\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Received server hello before the client hello was sent\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst connected = {\n\t\t\t\tstate: \"connected\",\n\t\t\t\tid: lifecycle.id,\n\t\t\t\tdecoder: lifecycle.decoder,\n\t\t\t\ttransport: lifecycle.transport,\n\t\t\t\thandshake: lifecycle.handshake,\n\t\t\t} satisfies Extract<ConnectionLifecycle, { state: \"connected\" }>;\n\t\t\tthis.#lifecycle = connected;\n\t\t\ttry {\n\t\t\t\tthis.#options.onHandshake(message);\n\t\t\t} catch (error) {\n\t\t\t\tif (this.#lifecycle === connected) this.#failAndClose(toError(error));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (this.#lifecycle !== connected) return;\n\t\t\tthis.#options.onStateChange({ state: \"connected\" });\n\t\t\tif (this.#lifecycle !== connected) return;\n\t\t\tthis.#lifecycle = { ...connected, handshake: undefined };\n\t\t\tlifecycle.handshake.resolve(message);\n\t\t\treturn;\n\t\t}\n\t\tif (lifecycle.state !== \"connected\") return;\n\t\tif (message.type === \"hello\" || message.type === \"hello_error\") {\n\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Unexpected handshake message\"));\n\t\t\treturn;\n\t\t}\n\t\tthis.#options.onMessage(message);\n\t}\n\n\t#handleClose(): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\") return;\n\t\tlet error: Error = new DisconnectedError(\"Byte transport closed\");\n\t\ttry {\n\t\t\tlifecycle.decoder.end();\n\t\t} catch (decoderError) {\n\t\t\terror = toError(decoderError);\n\t\t}\n\t\tthis.#fail(error);\n\t}\n\n\t#failAndClose(error: Error): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tconst transport = lifecycle.state === \"disconnected\" ? undefined : lifecycle.transport;\n\t\tthis.#fail(error);\n\t\ttransport?.close();\n\t}\n\n\t#fail(error: Error): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\") return;\n\t\tthis.#lifecycle = { state: \"disconnected\" };\n\t\tlifecycle.handshake?.reject(error);\n\t\tthis.#options.onStateChange({ state: \"disconnected\", error });\n\t}\n\n\t#isCurrent(id: number): boolean {\n\t\treturn this.#lifecycle.state !== \"disconnected\" && this.#lifecycle.id === id;\n\t}\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import { DEFAULT_MAX_FRAME_LENGTH, encodeClientMessage, PROTOCOL_VERSION, ProtocolValidationError, ServerMessageDecoder, } from "@earendil-works/pi-protocol";
2
- import { PiDisconnectedError, PiServerError, toDisconnectedError, toError } from "./errors.js";
2
+ import { DisconnectedError, ServerError, toDisconnectedError, toError } from "./errors.js";
3
3
  import { createPromiseResolvers } from "./promise.js";
4
4
  const MAX_UINT32 = 0xffff_ffff;
5
5
  export class Connection {
@@ -13,7 +13,7 @@ export class Connection {
13
13
  if (!Number.isSafeInteger(this.#maxFrameLength) ||
14
14
  this.#maxFrameLength <= 0 ||
15
15
  this.#maxFrameLength > MAX_UINT32) {
16
- throw new TypeError(`PiClient maxFrameLength must be an integer between 1 and ${MAX_UINT32}`);
16
+ throw new TypeError(`Client maxFrameLength must be an integer between 1 and ${MAX_UINT32}`);
17
17
  }
18
18
  }
19
19
  get state() {
@@ -24,7 +24,7 @@ export class Connection {
24
24
  }
25
25
  connect() {
26
26
  if (this.#lifecycle.state !== "disconnected") {
27
- return Promise.reject(new PiDisconnectedError(`PiClient is already ${this.#lifecycle.state}`));
27
+ return Promise.reject(new DisconnectedError(`Client is already ${this.#lifecycle.state}`));
28
28
  }
29
29
  const id = ++this.#sequence;
30
30
  const handshake = createPromiseResolvers();
@@ -52,7 +52,7 @@ export class Connection {
52
52
  disconnect(reason = "Client disconnected") {
53
53
  if (this.#lifecycle.state === "disconnected")
54
54
  return;
55
- this.#failAndClose(typeof reason === "string" ? new PiDisconnectedError(reason) : reason);
55
+ this.#failAndClose(typeof reason === "string" ? new DisconnectedError(reason) : reason);
56
56
  }
57
57
  fail(error) {
58
58
  this.#failAndClose(error);
@@ -60,7 +60,7 @@ export class Connection {
60
60
  send(frame) {
61
61
  const lifecycle = this.#lifecycle;
62
62
  if (lifecycle.state !== "connected")
63
- throw new PiDisconnectedError();
63
+ throw new DisconnectedError();
64
64
  let sending;
65
65
  try {
66
66
  sending = lifecycle.transport.send(frame);
@@ -126,13 +126,17 @@ export class Connection {
126
126
  const lifecycle = this.#lifecycle;
127
127
  if (lifecycle.state === "connecting") {
128
128
  if (message.type === "hello_error") {
129
- this.#failAndClose(new PiServerError(message.error));
129
+ this.#failAndClose(new ServerError(message.error));
130
130
  return;
131
131
  }
132
132
  if (message.type !== "hello") {
133
133
  this.#failAndClose(new ProtocolValidationError("Expected server hello as first message"));
134
134
  return;
135
135
  }
136
+ if (message.serverId !== this.#options.serverId) {
137
+ this.#failAndClose(new ProtocolValidationError(`Connected server ${JSON.stringify(message.serverId)} does not match ${JSON.stringify(this.#options.serverId)}`));
138
+ return;
139
+ }
136
140
  if (!lifecycle.transport) {
137
141
  this.#failAndClose(new ProtocolValidationError("Received server hello before the client hello was sent"));
138
142
  return;
@@ -146,7 +150,7 @@ export class Connection {
146
150
  };
147
151
  this.#lifecycle = connected;
148
152
  try {
149
- this.#options.onHandshake(message.snapshot);
153
+ this.#options.onHandshake(message);
150
154
  }
151
155
  catch (error) {
152
156
  if (this.#lifecycle === connected)
@@ -159,7 +163,7 @@ export class Connection {
159
163
  if (this.#lifecycle !== connected)
160
164
  return;
161
165
  this.#lifecycle = { ...connected, handshake: undefined };
162
- lifecycle.handshake.resolve(message.snapshot);
166
+ lifecycle.handshake.resolve(message);
163
167
  return;
164
168
  }
165
169
  if (lifecycle.state !== "connected")
@@ -174,7 +178,7 @@ export class Connection {
174
178
  const lifecycle = this.#lifecycle;
175
179
  if (lifecycle.state === "disconnected")
176
180
  return;
177
- let error = new PiDisconnectedError("Byte transport closed");
181
+ let error = new DisconnectedError("Byte transport closed");
178
182
  try {
179
183
  lifecycle.decoder.end();
180
184
  }
@@ -1 +1 @@
1
- {"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,wBAAwB,EACxB,mBAAmB,EACnB,gBAAgB,EAChB,uBAAuB,EAEvB,oBAAoB,GAEpB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC/F,OAAO,EAAE,sBAAsB,EAAyB,MAAM,cAAc,CAAC;AAI7E,MAAM,UAAU,GAAG,WAAW,CAAC;AAyB/B,MAAM,OAAO,UAAU;IACb,QAAQ,CAAoB;IAC5B,eAAe,CAAS;IACjC,UAAU,GAAwB,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC;IAC5D,SAAS,GAAG,CAAC,CAAC;IAEd,YAAY,OAA0B,EAAE;QACvC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,cAAc,IAAI,wBAAwB,CAAC;QAC1E,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC;YAC3C,IAAI,CAAC,eAAe,IAAI,CAAC;YACzB,IAAI,CAAC,eAAe,GAAG,UAAU,EAChC,CAAC;YACF,MAAM,IAAI,SAAS,CAAC,4DAA4D,UAAU,EAAE,CAAC,CAAC;QAC/F,CAAC;IAAA,CACD;IAED,IAAI,KAAK,GAAoB;QAC5B,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC;IAAA,CAC7B;IAED,IAAI,cAAc,GAAW;QAC5B,OAAO,IAAI,CAAC,eAAe,CAAC;IAAA,CAC5B;IAED,OAAO,GAA4B;QAClC,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,KAAK,cAAc,EAAE,CAAC;YAC9C,OAAO,OAAO,CAAC,MAAM,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AK,CAAC,CAAC;IAAA,CAClB;IAED,aAAa,CAAC,KAAY,EAAQ;QACjC,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC;QAClC,MAAM,SAAS,GAAG,SAAS,CAAC,KAAK,KAAK,cAAc,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC;QACvF,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAClB,SAAS,EAAE,KAAK,EAAE,CAAC;IAAA,CACnB;IAED,KAAK,CAAC,KAAY,EAAQ;QACzB,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC;QAClC,IAAI,SAAS,CAAC,KAAK,KAAK,cAAc;YAAE,OAAO;QAC/C,IAAI,CAAC,UAAU,GAAG,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC;QAC5C,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QACnC,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC;IAAA,CAC9D;IAED,UAAU,CAAC,EAAU,EAAW;QAC/B,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,KAAK,cAAc,IAAI,IAAI,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE,CAAC;IAAA,CAC7E;CACD","sourcesContent":["import {\n\tDEFAULT_MAX_FRAME_LENGTH,\n\tencodeClientMessage,\n\tPROTOCOL_VERSION,\n\tProtocolValidationError,\n\ttype ServerMessage,\n\tServerMessageDecoder,\n\ttype ServerSnapshot,\n} from \"@earendil-works/pi-protocol\";\nimport { PiDisconnectedError, PiServerError, toDisconnectedError, toError } from \"./errors.ts\";\nimport { createPromiseResolvers, type PromiseResolvers } from \"./promise.ts\";\nimport type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from \"./transport.ts\";\nimport type { ConnectionState, ConnectionStateChange } from \"./types.ts\";\n\nconst MAX_UINT32 = 0xffff_ffff;\n\ntype ActiveConnection = {\n\tid: number;\n\tdecoder: ServerMessageDecoder;\n\ttransport?: ByteTransport;\n};\n\ntype ConnectionLifecycle =\n\t| { state: \"disconnected\" }\n\t| ({ state: \"connecting\"; handshake: PromiseResolvers<ServerSnapshot> } & ActiveConnection)\n\t| ({\n\t\t\tstate: \"connected\";\n\t\t\ttransport: ByteTransport;\n\t\t\thandshake: PromiseResolvers<ServerSnapshot> | undefined;\n\t } & ActiveConnection);\n\ninterface ConnectionOptions {\n\ttransportFactory: ByteTransportFactory;\n\tmaxFrameLength?: number;\n\tonHandshake(snapshot: ServerSnapshot): void;\n\tonMessage(message: Exclude<ServerMessage, { type: \"hello\" | \"hello_error\" }>): void;\n\tonStateChange(change: ConnectionStateChange): void;\n}\n\nexport class Connection {\n\treadonly #options: ConnectionOptions;\n\treadonly #maxFrameLength: number;\n\t#lifecycle: ConnectionLifecycle = { state: \"disconnected\" };\n\t#sequence = 0;\n\n\tconstructor(options: ConnectionOptions) {\n\t\tthis.#options = options;\n\t\tthis.#maxFrameLength = options.maxFrameLength ?? DEFAULT_MAX_FRAME_LENGTH;\n\t\tif (\n\t\t\t!Number.isSafeInteger(this.#maxFrameLength) ||\n\t\t\tthis.#maxFrameLength <= 0 ||\n\t\t\tthis.#maxFrameLength > MAX_UINT32\n\t\t) {\n\t\t\tthrow new TypeError(`PiClient maxFrameLength must be an integer between 1 and ${MAX_UINT32}`);\n\t\t}\n\t}\n\n\tget state(): ConnectionState {\n\t\treturn this.#lifecycle.state;\n\t}\n\n\tget maxFrameLength(): number {\n\t\treturn this.#maxFrameLength;\n\t}\n\n\tconnect(): Promise<ServerSnapshot> {\n\t\tif (this.#lifecycle.state !== \"disconnected\") {\n\t\t\treturn Promise.reject(new PiDisconnectedError(`PiClient is already ${this.#lifecycle.state}`));\n\t\t}\n\t\tconst id = ++this.#sequence;\n\t\tconst handshake = createPromiseResolvers<ServerSnapshot>();\n\t\tthis.#lifecycle = {\n\t\t\tstate: \"connecting\",\n\t\t\tid,\n\t\t\tdecoder: new ServerMessageDecoder({ maxFrameLength: this.#maxFrameLength }),\n\t\t\thandshake,\n\t\t};\n\t\tthis.#options.onStateChange({ state: \"connecting\" });\n\t\tconst handlers = {\n\t\t\tonData: (chunk) => this.#handleData(id, chunk),\n\t\t\tonClose: () => {\n\t\t\t\tif (this.#isCurrent(id)) this.#handleClose();\n\t\t\t},\n\t\t\tonError: (error) => {\n\t\t\t\tif (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));\n\t\t\t},\n\t\t} satisfies ByteTransportHandlers;\n\t\tvoid this.#openTransport(id, handlers);\n\t\treturn handshake.promise;\n\t}\n\n\tdisconnect(reason: string | Error = \"Client disconnected\"): void {\n\t\tif (this.#lifecycle.state === \"disconnected\") return;\n\t\tthis.#failAndClose(typeof reason === \"string\" ? new PiDisconnectedError(reason) : reason);\n\t}\n\n\tfail(error: Error): void {\n\t\tthis.#failAndClose(error);\n\t}\n\n\tsend(frame: Uint8Array): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state !== \"connected\") throw new PiDisconnectedError();\n\t\tlet sending: Promise<void>;\n\t\ttry {\n\t\t\tsending = lifecycle.transport.send(frame);\n\t\t} catch (error) {\n\t\t\tthis.#failAndClose(toDisconnectedError(error));\n\t\t\treturn;\n\t\t}\n\t\tvoid sending.catch((error: unknown) => {\n\t\t\tconst current = this.#lifecycle;\n\t\t\tif (current.state !== \"disconnected\" && current.transport === lifecycle.transport) {\n\t\t\t\tthis.#failAndClose(toDisconnectedError(error));\n\t\t\t}\n\t\t});\n\t}\n\n\tasync #openTransport(id: number, handlers: ByteTransportHandlers): Promise<void> {\n\t\tlet transport: ByteTransport;\n\t\ttry {\n\t\t\ttransport = await this.#options.transportFactory(handlers);\n\t\t} catch (error) {\n\t\t\tif (this.#isCurrent(id)) this.#fail(toDisconnectedError(error));\n\t\t\treturn;\n\t\t}\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state !== \"connecting\" || lifecycle.id !== id) {\n\t\t\ttransport.close();\n\t\t\treturn;\n\t\t}\n\t\tthis.#lifecycle = { ...lifecycle, transport };\n\t\ttry {\n\t\t\tawait transport.send(\n\t\t\t\tencodeClientMessage({ type: \"hello\", version: PROTOCOL_VERSION }, { maxFrameLength: this.#maxFrameLength }),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tif (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));\n\t\t}\n\t}\n\n\t#handleData(id: number, chunk: Uint8Array): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\" || lifecycle.id !== id) return;\n\t\tif (lifecycle.state === \"connecting\" && !lifecycle.transport) {\n\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Received server data before the client hello was sent\"));\n\t\t\treturn;\n\t\t}\n\t\tlet messages: ServerMessage[];\n\t\ttry {\n\t\t\tmessages = lifecycle.decoder.push(chunk);\n\t\t} catch (error) {\n\t\t\tthis.#failAndClose(toError(error));\n\t\t\treturn;\n\t\t}\n\t\tfor (const message of messages) {\n\t\t\tif (this.#lifecycle.state === \"disconnected\") return;\n\t\t\tthis.#handleMessage(message);\n\t\t}\n\t}\n\n\t#handleMessage(message: ServerMessage): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"connecting\") {\n\t\t\tif (message.type === \"hello_error\") {\n\t\t\t\tthis.#failAndClose(new PiServerError(message.error));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (message.type !== \"hello\") {\n\t\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Expected server hello as first message\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!lifecycle.transport) {\n\t\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Received server hello before the client hello was sent\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst connected = {\n\t\t\t\tstate: \"connected\",\n\t\t\t\tid: lifecycle.id,\n\t\t\t\tdecoder: lifecycle.decoder,\n\t\t\t\ttransport: lifecycle.transport,\n\t\t\t\thandshake: lifecycle.handshake,\n\t\t\t} satisfies Extract<ConnectionLifecycle, { state: \"connected\" }>;\n\t\t\tthis.#lifecycle = connected;\n\t\t\ttry {\n\t\t\t\tthis.#options.onHandshake(message.snapshot);\n\t\t\t} catch (error) {\n\t\t\t\tif (this.#lifecycle === connected) this.#failAndClose(toError(error));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (this.#lifecycle !== connected) return;\n\t\t\tthis.#options.onStateChange({ state: \"connected\" });\n\t\t\tif (this.#lifecycle !== connected) return;\n\t\t\tthis.#lifecycle = { ...connected, handshake: undefined };\n\t\t\tlifecycle.handshake.resolve(message.snapshot);\n\t\t\treturn;\n\t\t}\n\t\tif (lifecycle.state !== \"connected\") return;\n\t\tif (message.type === \"hello\" || message.type === \"hello_error\") {\n\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Unexpected handshake message\"));\n\t\t\treturn;\n\t\t}\n\t\tthis.#options.onMessage(message);\n\t}\n\n\t#handleClose(): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\") return;\n\t\tlet error: Error = new PiDisconnectedError(\"Byte transport closed\");\n\t\ttry {\n\t\t\tlifecycle.decoder.end();\n\t\t} catch (decoderError) {\n\t\t\terror = toError(decoderError);\n\t\t}\n\t\tthis.#fail(error);\n\t}\n\n\t#failAndClose(error: Error): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tconst transport = lifecycle.state === \"disconnected\" ? undefined : lifecycle.transport;\n\t\tthis.#fail(error);\n\t\ttransport?.close();\n\t}\n\n\t#fail(error: Error): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\") return;\n\t\tthis.#lifecycle = { state: \"disconnected\" };\n\t\tlifecycle.handshake?.reject(error);\n\t\tthis.#options.onStateChange({ state: \"disconnected\", error });\n\t}\n\n\t#isCurrent(id: number): boolean {\n\t\treturn this.#lifecycle.state !== \"disconnected\" && this.#lifecycle.id === id;\n\t}\n}\n"]}
1
+ {"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,wBAAwB,EACxB,mBAAmB,EACnB,gBAAgB,EAChB,uBAAuB,EAGvB,oBAAoB,GACpB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3F,OAAO,EAAE,sBAAsB,EAAyB,MAAM,cAAc,CAAC;AAI7E,MAAM,UAAU,GAAG,WAAW,CAAC;AA0B/B,MAAM,OAAO,UAAU;IACb,QAAQ,CAAoB;IAC5B,eAAe,CAAS;IACjC,UAAU,GAAwB,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC;IAC5D,SAAS,GAAG,CAAC,CAAC;IAEd,YAAY,OAA0B,EAAE;QACvC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,cAAc,IAAI,wBAAwB,CAAC;QAC1E,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC;YAC3C,IAAI,CAAC,eAAe,IAAI,CAAC;YACzB,IAAI,CAAC,eAAe,GAAG,UAAU,EAChC,CAAC;YACF,MAAM,IAAI,SAAS,CAAC,0DAA0D,UAAU,EAAE,CAAC,CAAC;QAC7F,CAAC;IAAA,CACD;IAED,IAAI,KAAK,GAAoB;QAC5B,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC;IAAA,CAC7B;IAED,IAAI,cAAc,GAAW;QAC5B,OAAO,IAAI,CAAC,eAAe,CAAC;IAAA,CAC5B;IAED,OAAO,GAAyB;QAC/B,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,KAAK,cAAc,EAAE,CAAC;YAC9C,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,iBAAiB,CAA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\tServerMessageDecoder,\n} from \"@earendil-works/pi-protocol\";\nimport { DisconnectedError, ServerError, toDisconnectedError, toError } from \"./errors.ts\";\nimport { createPromiseResolvers, type PromiseResolvers } from \"./promise.ts\";\nimport type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from \"./transport.ts\";\nimport type { ConnectionState, ConnectionStateChange } from \"./types.ts\";\n\nconst MAX_UINT32 = 0xffff_ffff;\n\ntype ActiveConnection = {\n\tid: number;\n\tdecoder: ServerMessageDecoder;\n\ttransport?: ByteTransport;\n};\n\ntype ConnectionLifecycle =\n\t| { state: \"disconnected\" }\n\t| ({ state: \"connecting\"; handshake: PromiseResolvers<ServerHello> } & ActiveConnection)\n\t| ({\n\t\t\tstate: \"connected\";\n\t\t\ttransport: ByteTransport;\n\t\t\thandshake: PromiseResolvers<ServerHello> | undefined;\n\t } & ActiveConnection);\n\ninterface ConnectionOptions {\n\ttransportFactory: ByteTransportFactory;\n\tserverId: string;\n\tmaxFrameLength?: number;\n\tonHandshake(hello: ServerHello): void;\n\tonMessage(message: Exclude<ServerMessage, { type: \"hello\" | \"hello_error\" }>): void;\n\tonStateChange(change: ConnectionStateChange): void;\n}\n\nexport class Connection {\n\treadonly #options: ConnectionOptions;\n\treadonly #maxFrameLength: number;\n\t#lifecycle: ConnectionLifecycle = { state: \"disconnected\" };\n\t#sequence = 0;\n\n\tconstructor(options: ConnectionOptions) {\n\t\tthis.#options = options;\n\t\tthis.#maxFrameLength = options.maxFrameLength ?? DEFAULT_MAX_FRAME_LENGTH;\n\t\tif (\n\t\t\t!Number.isSafeInteger(this.#maxFrameLength) ||\n\t\t\tthis.#maxFrameLength <= 0 ||\n\t\t\tthis.#maxFrameLength > MAX_UINT32\n\t\t) {\n\t\t\tthrow new TypeError(`Client maxFrameLength must be an integer between 1 and ${MAX_UINT32}`);\n\t\t}\n\t}\n\n\tget state(): ConnectionState {\n\t\treturn this.#lifecycle.state;\n\t}\n\n\tget maxFrameLength(): number {\n\t\treturn this.#maxFrameLength;\n\t}\n\n\tconnect(): Promise<ServerHello> {\n\t\tif (this.#lifecycle.state !== \"disconnected\") {\n\t\t\treturn Promise.reject(new DisconnectedError(`Client is already ${this.#lifecycle.state}`));\n\t\t}\n\t\tconst id = ++this.#sequence;\n\t\tconst handshake = createPromiseResolvers<ServerHello>();\n\t\tthis.#lifecycle = {\n\t\t\tstate: \"connecting\",\n\t\t\tid,\n\t\t\tdecoder: new ServerMessageDecoder({ maxFrameLength: this.#maxFrameLength }),\n\t\t\thandshake,\n\t\t};\n\t\tthis.#options.onStateChange({ state: \"connecting\" });\n\t\tconst handlers = {\n\t\t\tonData: (chunk) => this.#handleData(id, chunk),\n\t\t\tonClose: () => {\n\t\t\t\tif (this.#isCurrent(id)) this.#handleClose();\n\t\t\t},\n\t\t\tonError: (error) => {\n\t\t\t\tif (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));\n\t\t\t},\n\t\t} satisfies ByteTransportHandlers;\n\t\tvoid this.#openTransport(id, handlers);\n\t\treturn handshake.promise;\n\t}\n\n\tdisconnect(reason: string | Error = \"Client disconnected\"): void {\n\t\tif (this.#lifecycle.state === \"disconnected\") return;\n\t\tthis.#failAndClose(typeof reason === \"string\" ? new DisconnectedError(reason) : reason);\n\t}\n\n\tfail(error: Error): void {\n\t\tthis.#failAndClose(error);\n\t}\n\n\tsend(frame: Uint8Array): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state !== \"connected\") throw new DisconnectedError();\n\t\tlet sending: Promise<void>;\n\t\ttry {\n\t\t\tsending = lifecycle.transport.send(frame);\n\t\t} catch (error) {\n\t\t\tthis.#failAndClose(toDisconnectedError(error));\n\t\t\treturn;\n\t\t}\n\t\tvoid sending.catch((error: unknown) => {\n\t\t\tconst current = this.#lifecycle;\n\t\t\tif (current.state !== \"disconnected\" && current.transport === lifecycle.transport) {\n\t\t\t\tthis.#failAndClose(toDisconnectedError(error));\n\t\t\t}\n\t\t});\n\t}\n\n\tasync #openTransport(id: number, handlers: ByteTransportHandlers): Promise<void> {\n\t\tlet transport: ByteTransport;\n\t\ttry {\n\t\t\ttransport = await this.#options.transportFactory(handlers);\n\t\t} catch (error) {\n\t\t\tif (this.#isCurrent(id)) this.#fail(toDisconnectedError(error));\n\t\t\treturn;\n\t\t}\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state !== \"connecting\" || lifecycle.id !== id) {\n\t\t\ttransport.close();\n\t\t\treturn;\n\t\t}\n\t\tthis.#lifecycle = { ...lifecycle, transport };\n\t\ttry {\n\t\t\tawait transport.send(\n\t\t\t\tencodeClientMessage({ type: \"hello\", version: PROTOCOL_VERSION }, { maxFrameLength: this.#maxFrameLength }),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tif (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));\n\t\t}\n\t}\n\n\t#handleData(id: number, chunk: Uint8Array): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\" || lifecycle.id !== id) return;\n\t\tif (lifecycle.state === \"connecting\" && !lifecycle.transport) {\n\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Received server data before the client hello was sent\"));\n\t\t\treturn;\n\t\t}\n\t\tlet messages: ServerMessage[];\n\t\ttry {\n\t\t\tmessages = lifecycle.decoder.push(chunk);\n\t\t} catch (error) {\n\t\t\tthis.#failAndClose(toError(error));\n\t\t\treturn;\n\t\t}\n\t\tfor (const message of messages) {\n\t\t\tif (this.#lifecycle.state === \"disconnected\") return;\n\t\t\tthis.#handleMessage(message);\n\t\t}\n\t}\n\n\t#handleMessage(message: ServerMessage): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"connecting\") {\n\t\t\tif (message.type === \"hello_error\") {\n\t\t\t\tthis.#failAndClose(new ServerError(message.error));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (message.type !== \"hello\") {\n\t\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Expected server hello as first message\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (message.serverId !== this.#options.serverId) {\n\t\t\t\tthis.#failAndClose(\n\t\t\t\t\tnew ProtocolValidationError(\n\t\t\t\t\t\t`Connected server ${JSON.stringify(message.serverId)} does not match ${JSON.stringify(this.#options.serverId)}`,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!lifecycle.transport) {\n\t\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Received server hello before the client hello was sent\"));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst connected = {\n\t\t\t\tstate: \"connected\",\n\t\t\t\tid: lifecycle.id,\n\t\t\t\tdecoder: lifecycle.decoder,\n\t\t\t\ttransport: lifecycle.transport,\n\t\t\t\thandshake: lifecycle.handshake,\n\t\t\t} satisfies Extract<ConnectionLifecycle, { state: \"connected\" }>;\n\t\t\tthis.#lifecycle = connected;\n\t\t\ttry {\n\t\t\t\tthis.#options.onHandshake(message);\n\t\t\t} catch (error) {\n\t\t\t\tif (this.#lifecycle === connected) this.#failAndClose(toError(error));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (this.#lifecycle !== connected) return;\n\t\t\tthis.#options.onStateChange({ state: \"connected\" });\n\t\t\tif (this.#lifecycle !== connected) return;\n\t\t\tthis.#lifecycle = { ...connected, handshake: undefined };\n\t\t\tlifecycle.handshake.resolve(message);\n\t\t\treturn;\n\t\t}\n\t\tif (lifecycle.state !== \"connected\") return;\n\t\tif (message.type === \"hello\" || message.type === \"hello_error\") {\n\t\t\tthis.#failAndClose(new ProtocolValidationError(\"Unexpected handshake message\"));\n\t\t\treturn;\n\t\t}\n\t\tthis.#options.onMessage(message);\n\t}\n\n\t#handleClose(): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\") return;\n\t\tlet error: Error = new DisconnectedError(\"Byte transport closed\");\n\t\ttry {\n\t\t\tlifecycle.decoder.end();\n\t\t} catch (decoderError) {\n\t\t\terror = toError(decoderError);\n\t\t}\n\t\tthis.#fail(error);\n\t}\n\n\t#failAndClose(error: Error): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tconst transport = lifecycle.state === \"disconnected\" ? undefined : lifecycle.transport;\n\t\tthis.#fail(error);\n\t\ttransport?.close();\n\t}\n\n\t#fail(error: Error): void {\n\t\tconst lifecycle = this.#lifecycle;\n\t\tif (lifecycle.state === \"disconnected\") return;\n\t\tthis.#lifecycle = { state: \"disconnected\" };\n\t\tlifecycle.handshake?.reject(error);\n\t\tthis.#options.onStateChange({ state: \"disconnected\", error });\n\t}\n\n\t#isCurrent(id: number): boolean {\n\t\treturn this.#lifecycle.state !== \"disconnected\" && this.#lifecycle.id === id;\n\t}\n}\n"]}
package/dist/errors.d.ts CHANGED
@@ -1,23 +1,14 @@
1
- import type { JsonValue, ProtocolError, ProtocolErrorCode } from "@earendil-works/pi-protocol";
2
- export declare class PiServerError extends Error {
1
+ import type { ProtocolError, ProtocolErrorCode } from "@earendil-works/pi-protocol";
2
+ export declare class ServerError extends Error {
3
3
  readonly code: ProtocolErrorCode;
4
- readonly details: JsonValue | undefined;
5
4
  constructor(error: ProtocolError);
6
5
  }
7
- export declare class PiDisconnectedError extends Error {
8
- constructor(message?: string);
6
+ export declare class DisconnectedError extends Error {
7
+ constructor(message?: string, cause?: Error);
9
8
  }
10
- export declare class PiClientDisposedError extends Error {
9
+ export declare class ClientDisposedError extends Error {
11
10
  constructor();
12
11
  }
13
- export declare class PiSessionOwnershipError extends Error {
14
- readonly sessionId: string;
15
- constructor(sessionId: string, message: string);
16
- }
17
- export declare class PiSessionDetachedError extends Error {
18
- readonly sessionId: string;
19
- constructor(sessionId: string);
20
- }
21
12
  export declare function toError(error: unknown): Error;
22
- export declare function toDisconnectedError(error: unknown): PiDisconnectedError;
13
+ export declare function toDisconnectedError(error: unknown): DisconnectedError;
23
14
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAE/F,qBAAa,aAAc,SAAQ,KAAK;IACvC,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,SAAS,GAAG,SAAS,CAAC;IAExC,YAAY,KAAK,EAAE,aAAa,EAK/B;CACD;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IAC7C,YAAY,OAAO,SAA8B,EAGhD;CACD;AAED,qBAAa,qBAAsB,SAAQ,KAAK;IAC/C,cAGC;CACD;AAED,qBAAa,uBAAwB,SAAQ,KAAK;IACjD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,YAAY,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAI7C;CACD;AAED,qBAAa,sBAAuB,SAAQ,KAAK;IAChD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,YAAY,SAAS,EAAE,MAAM,EAI5B;CACD;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,CAE7C;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,mBAAmB,CAGvE","sourcesContent":["import type { JsonValue, ProtocolError, ProtocolErrorCode } from \"@earendil-works/pi-protocol\";\n\nexport class PiServerError extends Error {\n\treadonly code: ProtocolErrorCode;\n\treadonly details: JsonValue | undefined;\n\n\tconstructor(error: ProtocolError) {\n\t\tsuper(error.message);\n\t\tthis.name = \"PiServerError\";\n\t\tthis.code = error.code;\n\t\tthis.details = error.details;\n\t}\n}\n\nexport class PiDisconnectedError extends Error {\n\tconstructor(message = \"Pi client is disconnected\") {\n\t\tsuper(message);\n\t\tthis.name = \"PiDisconnectedError\";\n\t}\n}\n\nexport class PiClientDisposedError extends Error {\n\tconstructor() {\n\t\tsuper(\"Pi client is disposed\");\n\t\tthis.name = \"PiClientDisposedError\";\n\t}\n}\n\nexport class PiSessionOwnershipError extends Error {\n\treadonly sessionId: string;\n\n\tconstructor(sessionId: string, message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"PiSessionOwnershipError\";\n\t\tthis.sessionId = sessionId;\n\t}\n}\n\nexport class PiSessionDetachedError extends Error {\n\treadonly sessionId: string;\n\n\tconstructor(sessionId: string) {\n\t\tsuper(`Session ${sessionId} is not attached`);\n\t\tthis.name = \"PiSessionDetachedError\";\n\t\tthis.sessionId = sessionId;\n\t}\n}\n\nexport function toError(error: unknown): Error {\n\treturn error instanceof Error ? error : new Error(String(error));\n}\n\nexport function toDisconnectedError(error: unknown): PiDisconnectedError {\n\tconst cause = toError(error);\n\treturn cause instanceof PiDisconnectedError ? cause : new PiDisconnectedError(cause.message);\n}\n"]}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEpF,qBAAa,WAAY,SAAQ,KAAK;IACrC,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;IAEjC,YAAY,KAAK,EAAE,aAAa,EAI/B;CACD;AAED,qBAAa,iBAAkB,SAAQ,KAAK;IAC3C,YAAY,OAAO,SAA2B,EAAE,KAAK,CAAC,EAAE,KAAK,EAG5D;CACD;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IAC7C,cAGC;CACD;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,CAE7C;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,iBAAiB,CAGrE","sourcesContent":["import type { ProtocolError, ProtocolErrorCode } from \"@earendil-works/pi-protocol\";\n\nexport class ServerError extends Error {\n\treadonly code: ProtocolErrorCode;\n\n\tconstructor(error: ProtocolError) {\n\t\tsuper(error.message);\n\t\tthis.name = \"ServerError\";\n\t\tthis.code = error.code;\n\t}\n}\n\nexport class DisconnectedError extends Error {\n\tconstructor(message = \"Client is disconnected\", cause?: Error) {\n\t\tsuper(message, cause === undefined ? undefined : { cause });\n\t\tthis.name = \"DisconnectedError\";\n\t}\n}\n\nexport class ClientDisposedError extends Error {\n\tconstructor() {\n\t\tsuper(\"Client is disposed\");\n\t\tthis.name = \"ClientDisposedError\";\n\t}\n}\n\nexport function toError(error: unknown): Error {\n\treturn error instanceof Error ? error : new Error(String(error));\n}\n\nexport function toDisconnectedError(error: unknown): DisconnectedError {\n\tconst cause = toError(error);\n\treturn cause instanceof DisconnectedError ? cause : new DisconnectedError(cause.message, cause);\n}\n"]}
package/dist/errors.js CHANGED
@@ -1,39 +1,21 @@
1
- export class PiServerError extends Error {
1
+ export class ServerError extends Error {
2
2
  code;
3
- details;
4
3
  constructor(error) {
5
4
  super(error.message);
6
- this.name = "PiServerError";
5
+ this.name = "ServerError";
7
6
  this.code = error.code;
8
- this.details = error.details;
9
7
  }
10
8
  }
11
- export class PiDisconnectedError extends Error {
12
- constructor(message = "Pi client is disconnected") {
13
- super(message);
14
- this.name = "PiDisconnectedError";
9
+ export class DisconnectedError extends Error {
10
+ constructor(message = "Client is disconnected", cause) {
11
+ super(message, cause === undefined ? undefined : { cause });
12
+ this.name = "DisconnectedError";
15
13
  }
16
14
  }
17
- export class PiClientDisposedError extends Error {
15
+ export class ClientDisposedError extends Error {
18
16
  constructor() {
19
- super("Pi client is disposed");
20
- this.name = "PiClientDisposedError";
21
- }
22
- }
23
- export class PiSessionOwnershipError extends Error {
24
- sessionId;
25
- constructor(sessionId, message) {
26
- super(message);
27
- this.name = "PiSessionOwnershipError";
28
- this.sessionId = sessionId;
29
- }
30
- }
31
- export class PiSessionDetachedError extends Error {
32
- sessionId;
33
- constructor(sessionId) {
34
- super(`Session ${sessionId} is not attached`);
35
- this.name = "PiSessionDetachedError";
36
- this.sessionId = sessionId;
17
+ super("Client is disposed");
18
+ this.name = "ClientDisposedError";
37
19
  }
38
20
  }
39
21
  export function toError(error) {
@@ -41,6 +23,6 @@ export function toError(error) {
41
23
  }
42
24
  export function toDisconnectedError(error) {
43
25
  const cause = toError(error);
44
- return cause instanceof PiDisconnectedError ? cause : new PiDisconnectedError(cause.message);
26
+ return cause instanceof DisconnectedError ? cause : new DisconnectedError(cause.message, cause);
45
27
  }
46
28
  //# sourceMappingURL=errors.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC9B,IAAI,CAAoB;IACxB,OAAO,CAAwB;IAExC,YAAY,KAAoB,EAAE;QACjC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;IAAA,CAC7B;CACD;AAED,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC7C,YAAY,OAAO,GAAG,2BAA2B,EAAE;QAClD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IAAA,CAClC;CACD;AAED,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC/C,cAAc;QACb,KAAK,CAAC,uBAAuB,CAAC,CAAC;QAC/B,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IAAA,CACpC;CACD;AAED,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IACxC,SAAS,CAAS;IAE3B,YAAY,SAAiB,EAAE,OAAe,EAAE;QAC/C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;QACtC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAAA,CAC3B;CACD;AAED,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IACvC,SAAS,CAAS;IAE3B,YAAY,SAAiB,EAAE;QAC9B,KAAK,CAAC,WAAW,SAAS,kBAAkB,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;QACrC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAAA,CAC3B;CACD;AAED,MAAM,UAAU,OAAO,CAAC,KAAc,EAAS;IAC9C,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AAAA,CACjE;AAED,MAAM,UAAU,mBAAmB,CAAC,KAAc,EAAuB;IACxE,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,KAAK,YAAY,mBAAmB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,mBAAmB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AAAA,CAC7F","sourcesContent":["import type { JsonValue, ProtocolError, ProtocolErrorCode } from \"@earendil-works/pi-protocol\";\n\nexport class PiServerError extends Error {\n\treadonly code: ProtocolErrorCode;\n\treadonly details: JsonValue | undefined;\n\n\tconstructor(error: ProtocolError) {\n\t\tsuper(error.message);\n\t\tthis.name = \"PiServerError\";\n\t\tthis.code = error.code;\n\t\tthis.details = error.details;\n\t}\n}\n\nexport class PiDisconnectedError extends Error {\n\tconstructor(message = \"Pi client is disconnected\") {\n\t\tsuper(message);\n\t\tthis.name = \"PiDisconnectedError\";\n\t}\n}\n\nexport class PiClientDisposedError extends Error {\n\tconstructor() {\n\t\tsuper(\"Pi client is disposed\");\n\t\tthis.name = \"PiClientDisposedError\";\n\t}\n}\n\nexport class PiSessionOwnershipError extends Error {\n\treadonly sessionId: string;\n\n\tconstructor(sessionId: string, message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"PiSessionOwnershipError\";\n\t\tthis.sessionId = sessionId;\n\t}\n}\n\nexport class PiSessionDetachedError extends Error {\n\treadonly sessionId: string;\n\n\tconstructor(sessionId: string) {\n\t\tsuper(`Session ${sessionId} is not attached`);\n\t\tthis.name = \"PiSessionDetachedError\";\n\t\tthis.sessionId = sessionId;\n\t}\n}\n\nexport function toError(error: unknown): Error {\n\treturn error instanceof Error ? error : new Error(String(error));\n}\n\nexport function toDisconnectedError(error: unknown): PiDisconnectedError {\n\tconst cause = toError(error);\n\treturn cause instanceof PiDisconnectedError ? cause : new PiDisconnectedError(cause.message);\n}\n"]}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAY,SAAQ,KAAK;IAC5B,IAAI,CAAoB;IAEjC,YAAY,KAAoB,EAAE;QACjC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAAA,CACvB;CACD;AAED,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC3C,YAAY,OAAO,GAAG,wBAAwB,EAAE,KAAa,EAAE;QAC9D,KAAK,CAAC,OAAO,EAAE,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5D,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAAA,CAChC;CACD;AAED,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC7C,cAAc;QACb,KAAK,CAAC,oBAAoB,CAAC,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IAAA,CAClC;CACD;AAED,MAAM,UAAU,OAAO,CAAC,KAAc,EAAS;IAC9C,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AAAA,CACjE;AAED,MAAM,UAAU,mBAAmB,CAAC,KAAc,EAAqB;IACtE,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,KAAK,YAAY,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,iBAAiB,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAAA,CAChG","sourcesContent":["import type { ProtocolError, ProtocolErrorCode } from \"@earendil-works/pi-protocol\";\n\nexport class ServerError extends Error {\n\treadonly code: ProtocolErrorCode;\n\n\tconstructor(error: ProtocolError) {\n\t\tsuper(error.message);\n\t\tthis.name = \"ServerError\";\n\t\tthis.code = error.code;\n\t}\n}\n\nexport class DisconnectedError extends Error {\n\tconstructor(message = \"Client is disconnected\", cause?: Error) {\n\t\tsuper(message, cause === undefined ? undefined : { cause });\n\t\tthis.name = \"DisconnectedError\";\n\t}\n}\n\nexport class ClientDisposedError extends Error {\n\tconstructor() {\n\t\tsuper(\"Client is disposed\");\n\t\tthis.name = \"ClientDisposedError\";\n\t}\n}\n\nexport function toError(error: unknown): Error {\n\treturn error instanceof Error ? error : new Error(String(error));\n}\n\nexport function toDisconnectedError(error: unknown): DisconnectedError {\n\tconst cause = toError(error);\n\treturn cause instanceof DisconnectedError ? cause : new DisconnectedError(cause.message, cause);\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- export { PiClient } from "./client.ts";
2
- export { PiClientDisposedError, PiDisconnectedError, PiServerError, PiSessionDetachedError, PiSessionOwnershipError, } from "./errors.ts";
3
- export type { AcquireSessionOptions, PiSessionHandle, SessionLease, SessionLeaseMode } from "./session-handle.ts";
1
+ export { Client, createClientServiceTransport } from "./client.ts";
2
+ export { ClientDisposedError, DisconnectedError, ServerError } from "./errors.ts";
4
3
  export type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from "./transport.ts";
5
- export type { ConnectionState, ConnectionStateChange, CreateSessionOptions, ListenerErrorHandler, PiClientOptions, Unsubscribe, } from "./types.ts";
4
+ export type { AttachmentChangeListener, ClientOptions, ConnectionState, ConnectionStateChange, ListenerErrorHandler, ServiceSubscription, Unsubscribe, } from "./types.ts";
6
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EACN,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EACb,sBAAsB,EACtB,uBAAuB,GACvB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,qBAAqB,EAAE,eAAe,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAClH,YAAY,EAAE,aAAa,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACjG,YAAY,EACX,eAAe,EACf,qBAAqB,EACrB,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EACf,WAAW,GACX,MAAM,YAAY,CAAC","sourcesContent":["export { PiClient } from \"./client.ts\";\nexport {\n\tPiClientDisposedError,\n\tPiDisconnectedError,\n\tPiServerError,\n\tPiSessionDetachedError,\n\tPiSessionOwnershipError,\n} from \"./errors.ts\";\nexport type { AcquireSessionOptions, PiSessionHandle, SessionLease, SessionLeaseMode } from \"./session-handle.ts\";\nexport type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from \"./transport.ts\";\nexport type {\n\tConnectionState,\n\tConnectionStateChange,\n\tCreateSessionOptions,\n\tListenerErrorHandler,\n\tPiClientOptions,\n\tUnsubscribe,\n} from \"./types.ts\";\n"]}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAClF,YAAY,EAAE,aAAa,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACjG,YAAY,EACX,wBAAwB,EACxB,aAAa,EACb,eAAe,EACf,qBAAqB,EACrB,oBAAoB,EACpB,mBAAmB,EACnB,WAAW,GACX,MAAM,YAAY,CAAC","sourcesContent":["export { Client, createClientServiceTransport } from \"./client.ts\";\nexport { ClientDisposedError, DisconnectedError, ServerError } from \"./errors.ts\";\nexport type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from \"./transport.ts\";\nexport type {\n\tAttachmentChangeListener,\n\tClientOptions,\n\tConnectionState,\n\tConnectionStateChange,\n\tListenerErrorHandler,\n\tServiceSubscription,\n\tUnsubscribe,\n} from \"./types.ts\";\n"]}
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { PiClient } from "./client.js";
2
- export { PiClientDisposedError, PiDisconnectedError, PiServerError, PiSessionDetachedError, PiSessionOwnershipError, } from "./errors.js";
1
+ export { Client, createClientServiceTransport } from "./client.js";
2
+ export { ClientDisposedError, DisconnectedError, ServerError } from "./errors.js";
3
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EACN,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EACb,sBAAsB,EACtB,uBAAuB,GACvB,MAAM,aAAa,CAAC","sourcesContent":["export { PiClient } from \"./client.ts\";\nexport {\n\tPiClientDisposedError,\n\tPiDisconnectedError,\n\tPiServerError,\n\tPiSessionDetachedError,\n\tPiSessionOwnershipError,\n} from \"./errors.ts\";\nexport type { AcquireSessionOptions, PiSessionHandle, SessionLease, SessionLeaseMode } from \"./session-handle.ts\";\nexport type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from \"./transport.ts\";\nexport type {\n\tConnectionState,\n\tConnectionStateChange,\n\tCreateSessionOptions,\n\tListenerErrorHandler,\n\tPiClientOptions,\n\tUnsubscribe,\n} from \"./types.ts\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC","sourcesContent":["export { Client, createClientServiceTransport } from \"./client.ts\";\nexport { ClientDisposedError, DisconnectedError, ServerError } from \"./errors.ts\";\nexport type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from \"./transport.ts\";\nexport type {\n\tAttachmentChangeListener,\n\tClientOptions,\n\tConnectionState,\n\tConnectionStateChange,\n\tListenerErrorHandler,\n\tServiceSubscription,\n\tUnsubscribe,\n} from \"./types.ts\";\n"]}
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { ModelRef, ThinkingLevel } from "@earendil-works/pi-protocol";
1
+ import type { ServiceSubscriptionSnapshot } from "@earendil-works/chord";
2
+ import type { RpcTarget, SessionTarget } from "@earendil-works/pi-protocol";
2
3
  import type { ByteTransportFactory } from "./transport.ts";
3
4
  export type ConnectionState = "disconnected" | "connecting" | "connected";
4
5
  export interface ConnectionStateChange {
@@ -7,16 +8,21 @@ export interface ConnectionStateChange {
7
8
  }
8
9
  export type Unsubscribe = () => void;
9
10
  export type ListenerErrorHandler = (error: Error) => void;
10
- export interface PiClientOptions {
11
+ export type AttachmentChangeListener = (attachment: SessionTarget | undefined) => void;
12
+ export interface ServiceSubscription {
13
+ readonly id: string;
14
+ readonly target: RpcTarget;
15
+ readonly snapshot: ServiceSubscriptionSnapshot;
16
+ /** Begin ordered update delivery after the caller has installed the snapshot. */
17
+ start(): void;
18
+ dispose(): Promise<void>;
19
+ }
20
+ export interface ClientOptions {
11
21
  transportFactory: ByteTransportFactory;
22
+ /** Logical server identity expected at the physical endpoint. */
23
+ serverId: string;
12
24
  maxFrameLength?: number;
13
25
  /** Reports subscriber failures without allowing them to corrupt client state. */
14
26
  onListenerError?: ListenerErrorHandler;
15
27
  }
16
- export interface CreateSessionOptions {
17
- cwd?: string;
18
- name?: string;
19
- model?: ModelRef;
20
- thinkingLevel?: ThinkingLevel;
21
- }
22
28
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC3E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,MAAM,MAAM,eAAe,GAAG,cAAc,GAAG,YAAY,GAAG,WAAW,CAAC;AAE1E,MAAM,WAAW,qBAAqB;IACrC,KAAK,EAAE,eAAe,CAAC;IACvB,KAAK,CAAC,EAAE,KAAK,CAAC;CACd;AAED,MAAM,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;AACrC,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;AAE1D,MAAM,WAAW,eAAe;IAC/B,gBAAgB,EAAE,oBAAoB,CAAC;IACvC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,iFAAiF;IACjF,eAAe,CAAC,EAAE,oBAAoB,CAAC;CACvC;AAED,MAAM,WAAW,oBAAoB;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,aAAa,CAAC,EAAE,aAAa,CAAC;CAC9B","sourcesContent":["import type { ModelRef, ThinkingLevel } from \"@earendil-works/pi-protocol\";\nimport type { ByteTransportFactory } from \"./transport.ts\";\n\nexport type ConnectionState = \"disconnected\" | \"connecting\" | \"connected\";\n\nexport interface ConnectionStateChange {\n\tstate: ConnectionState;\n\terror?: Error;\n}\n\nexport type Unsubscribe = () => void;\nexport type ListenerErrorHandler = (error: Error) => void;\n\nexport interface PiClientOptions {\n\ttransportFactory: ByteTransportFactory;\n\tmaxFrameLength?: number;\n\t/** Reports subscriber failures without allowing them to corrupt client state. */\n\tonListenerError?: ListenerErrorHandler;\n}\n\nexport interface CreateSessionOptions {\n\tcwd?: string;\n\tname?: string;\n\tmodel?: ModelRef;\n\tthinkingLevel?: ThinkingLevel;\n}\n"]}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AACzE,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,MAAM,MAAM,eAAe,GAAG,cAAc,GAAG,YAAY,GAAG,WAAW,CAAC;AAE1E,MAAM,WAAW,qBAAqB;IACrC,KAAK,EAAE,eAAe,CAAC;IACvB,KAAK,CAAC,EAAE,KAAK,CAAC;CACd;AAED,MAAM,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;AACrC,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;AAC1D,MAAM,MAAM,wBAAwB,GAAG,CAAC,UAAU,EAAE,aAAa,GAAG,SAAS,KAAK,IAAI,CAAC;AAEvF,MAAM,WAAW,mBAAmB;IACnC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,2BAA2B,CAAC;IAC/C,iFAAiF;IACjF,KAAK,IAAI,IAAI,CAAC;IACd,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC7B,gBAAgB,EAAE,oBAAoB,CAAC;IACvC,iEAAiE;IACjE,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,iFAAiF;IACjF,eAAe,CAAC,EAAE,oBAAoB,CAAC;CACvC","sourcesContent":["import type { ServiceSubscriptionSnapshot } from \"@earendil-works/chord\";\nimport type { RpcTarget, SessionTarget } from \"@earendil-works/pi-protocol\";\nimport type { ByteTransportFactory } from \"./transport.ts\";\n\nexport type ConnectionState = \"disconnected\" | \"connecting\" | \"connected\";\n\nexport interface ConnectionStateChange {\n\tstate: ConnectionState;\n\terror?: Error;\n}\n\nexport type Unsubscribe = () => void;\nexport type ListenerErrorHandler = (error: Error) => void;\nexport type AttachmentChangeListener = (attachment: SessionTarget | undefined) => void;\n\nexport interface ServiceSubscription {\n\treadonly id: string;\n\treadonly target: RpcTarget;\n\treadonly snapshot: ServiceSubscriptionSnapshot;\n\t/** Begin ordered update delivery after the caller has installed the snapshot. */\n\tstart(): void;\n\tdispose(): Promise<void>;\n}\n\nexport interface ClientOptions {\n\ttransportFactory: ByteTransportFactory;\n\t/** Logical server identity expected at the physical endpoint. */\n\tserverId: string;\n\tmaxFrameLength?: number;\n\t/** Reports subscriber failures without allowing them to corrupt client state. */\n\tonListenerError?: ListenerErrorHandler;\n}\n"]}
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ModelRef, ThinkingLevel } from \"@earendil-works/pi-protocol\";\nimport type { ByteTransportFactory } from \"./transport.ts\";\n\nexport type ConnectionState = \"disconnected\" | \"connecting\" | \"connected\";\n\nexport interface ConnectionStateChange {\n\tstate: ConnectionState;\n\terror?: Error;\n}\n\nexport type Unsubscribe = () => void;\nexport type ListenerErrorHandler = (error: Error) => void;\n\nexport interface PiClientOptions {\n\ttransportFactory: ByteTransportFactory;\n\tmaxFrameLength?: number;\n\t/** Reports subscriber failures without allowing them to corrupt client state. */\n\tonListenerError?: ListenerErrorHandler;\n}\n\nexport interface CreateSessionOptions {\n\tcwd?: string;\n\tname?: string;\n\tmodel?: ModelRef;\n\tthinkingLevel?: ThinkingLevel;\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ServiceSubscriptionSnapshot } from \"@earendil-works/chord\";\nimport type { RpcTarget, SessionTarget } from \"@earendil-works/pi-protocol\";\nimport type { ByteTransportFactory } from \"./transport.ts\";\n\nexport type ConnectionState = \"disconnected\" | \"connecting\" | \"connected\";\n\nexport interface ConnectionStateChange {\n\tstate: ConnectionState;\n\terror?: Error;\n}\n\nexport type Unsubscribe = () => void;\nexport type ListenerErrorHandler = (error: Error) => void;\nexport type AttachmentChangeListener = (attachment: SessionTarget | undefined) => void;\n\nexport interface ServiceSubscription {\n\treadonly id: string;\n\treadonly target: RpcTarget;\n\treadonly snapshot: ServiceSubscriptionSnapshot;\n\t/** Begin ordered update delivery after the caller has installed the snapshot. */\n\tstart(): void;\n\tdispose(): Promise<void>;\n}\n\nexport interface ClientOptions {\n\ttransportFactory: ByteTransportFactory;\n\t/** Logical server identity expected at the physical endpoint. */\n\tserverId: string;\n\tmaxFrameLength?: number;\n\t/** Reports subscriber failures without allowing them to corrupt client state. */\n\tonListenerError?: ListenerErrorHandler;\n}\n"]}