@byok-sdk/server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ids.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function generatePairingCode(length?: number): string;
2
+ export declare function generateDeviceId(): string;
3
+ export declare function generateTaskId(): string;
@@ -0,0 +1,116 @@
1
+ import type { Server as HttpServer } from 'node:http';
2
+ import type { Hono } from 'hono';
3
+ import { type TenantId } from './auth';
4
+ import { type PairingCodeClaims, type PairingCodeInfo } from './pairing';
5
+ import type { ByokServerEvent, CreateByokServerOptions, DispatchInput, HubStats, MachineInfo, TaskHandle, TaskSnapshot } from './types';
6
+ export type { ByokServerEvent, CreateByokServerOptions, DispatchInput, HubStats, MachineInfo, ServerTaskEvent, TaskHandle, TaskResult, TaskSnapshot, } from './types';
7
+ export type { CreateTaskInput, TaskRecord, TaskStore } from './task-store';
8
+ export { IllegalTaskTransitionError, InMemoryTaskStore } from './task-store';
9
+ /**
10
+ * M5 (approval targeting, docs/protocol.md §5.3): previously unreachable via
11
+ * this package's public entry point (only importable from the internal
12
+ * `./hub` path) — `TaskHandle.approve`/`reject`'s `opts.approvalId` targeting
13
+ * (`types.ts`) throws this, so a caller needs it exported here to
14
+ * `instanceof`-check/inspect it. See `hub.ts`'s own doc comment for the full
15
+ * staleness semantics.
16
+ */
17
+ export { StaleApprovalError } from './hub';
18
+ /**
19
+ * S0 (GAP-002): `TaskHandle.steer` (`types.ts`) throws this when the runtime
20
+ * that claimed the task cannot be steered, when the task isn't running, or
21
+ * when it's already terminal — a caller needs the class to `instanceof`-check
22
+ * it and the code union to switch on. See `hub.ts`'s own doc comments for the
23
+ * full gate order and the fail-closed-on-unknown rationale.
24
+ */
25
+ export { SteerRejectedError } from './hub';
26
+ export type { SteerRejectionCode } from './hub';
27
+ export { PairingCodeInvalidError } from './pairing';
28
+ export type { PairingCodeClaims, PairingCodeInfo } from './pairing';
29
+ export type { AccessTokenClaims, AuthenticatedDevice, DeviceRecord, TenantId, TokenSigner, } from './auth';
30
+ /**
31
+ * S1: `DeviceRegistry` itself is deliberately NOT exported. Its
32
+ * tenant-scoped surface is reachable through `ByokServer.devices` (below),
33
+ * and the one method that resolves a device without a tenant in scope
34
+ * (`resolveByDeviceId`, for the two pre-tenant wire endpoints) exists only
35
+ * inside this package — exporting the class would hand every embedder a
36
+ * cross-tenant device oracle for free.
37
+ */
38
+ export { createHmacTokenSigner } from './auth';
39
+ export type { BlobStore, CreateUploadInput, ReadContentResult, WriteContentResult, } from './blob-store';
40
+ export { LocalDiskBlobStore } from './blob-store';
41
+ export type { SqliteTaskStoreOptions } from './sqlite-task-store';
42
+ export { SqliteTaskStore } from './sqlite-task-store';
43
+ export type { SqliteBlobStoreOptions } from './sqlite-blob-store';
44
+ export { SqliteBlobStore } from './sqlite-blob-store';
45
+ export { SqliteUnavailableError } from './sqlite-support';
46
+ export type { RateLimiterOptions } from './rate-limiter';
47
+ /** The object `createByokServer` returns — the SaaS-embedder-facing surface. */
48
+ export interface ByokServer {
49
+ /** Hono app exposing the pair/challenge/token/blob/events HTTP routes. Mount it, or use its `.fetch` with `@hono/node-server`. */
50
+ hono: Hono;
51
+ /** Wire up the `GET /byok/ws` upgrade on the raw Node HTTP server serving `hono`. */
52
+ attachWebSocket(server: HttpServer): void;
53
+ pairing: {
54
+ /**
55
+ * S1: minting a code REQUIRES the tenant and product the redeeming
56
+ * device will be paired into (docs/protocol.md §6.1) — the SaaS's own
57
+ * auth/device-flow UI is the only party that knows them, and the device
58
+ * never gets to name its own. There is no claimless overload.
59
+ */
60
+ createPairingCode(claims: PairingCodeClaims): PairingCodeInfo;
61
+ };
62
+ dispatch(input: DispatchInput): Promise<TaskHandle>;
63
+ tasks: {
64
+ get(taskId: string): TaskSnapshot | undefined;
65
+ list(): TaskSnapshot[];
66
+ };
67
+ machines: {
68
+ list(): MachineInfo[];
69
+ };
70
+ events: {
71
+ subscribe(): AsyncIterable<ByokServerEvent>;
72
+ };
73
+ /**
74
+ * Device revocation (§6.3) — server-side only, no wire message. Revoking a
75
+ * device makes its next `/byok/challenge`, `/byok/token`, WSS connect, or
76
+ * authed HTTP call get a 401; its only recourse is to re-run `/byok/pair`.
77
+ *
78
+ * S1: tenant-first, and a tenant can only revoke a device it owns — a
79
+ * `(tenantId, deviceId)` pair belonging to someone else resolves to
80
+ * nothing and this is a silent no-op rather than a cross-tenant write.
81
+ */
82
+ devices: {
83
+ revoke(tenantId: TenantId, deviceId: string): void;
84
+ };
85
+ /**
86
+ * Stop background timers owned by this server instance — currently just
87
+ * the task-lease reaper (`ConnectionHub.stopLeaseReaper`, `hub.ts`). Call
88
+ * this on shutdown so nothing keeps the process alive or leaks a handle in
89
+ * tests; safe to call more than once.
90
+ */
91
+ stop(): void;
92
+ /**
93
+ * M4 Phase 4 (part B.1): a plain, serializable in-process snapshot of this
94
+ * hub's current state — connected device count, task counts by state,
95
+ * envelope in/out totals, dedup drops, rate-limit events, and uptime. See
96
+ * {@link HubStats} for the full contract. Deliberately in-process only —
97
+ * never exposed over HTTP by this SDK itself (see
98
+ * `CreateByokServerOptions.healthzRoute`'s doc comment); an embedder that
99
+ * wants any of this surfaced remotely builds its own authenticated route
100
+ * around this method.
101
+ */
102
+ stats(): HubStats;
103
+ }
104
+ /**
105
+ * In-memory reference implementation of the SaaS-side coordinator: Auth v2
106
+ * device pairing/renewal/revocation, a WS + long-poll connection hub with
107
+ * at-least-once redelivery, a local-disk blob store, and task dispatch/
108
+ * lifecycle tracking. See the per-module doc comments (`auth.ts`,
109
+ * `blob-store.ts`, `hub.ts`, `pairing.ts`, `ws-server.ts`) for what's a
110
+ * pinned wire/HTTP contract (docs/protocol.md) versus a reference-impl
111
+ * choice a SaaS embedder might swap out (`tokenSigner`, `blobStore`,
112
+ * `taskStore` — the latter two default to in-memory/local-disk and lose all
113
+ * state on restart; see `sqlite-task-store.ts`/`sqlite-blob-store.ts` for
114
+ * persistent M3 alternatives implementing the same interfaces).
115
+ */
116
+ export declare function createByokServer(opts: CreateByokServerOptions): ByokServer;