@michaelserra.dev/sable-sdk 0.0.0-stage → 0.2.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/README.md +142 -2
- package/dist/config.d.ts +17 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +48 -0
- package/dist/config.js.map +1 -0
- package/dist/crypto.d.ts +25 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +267 -0
- package/dist/crypto.js.map +1 -0
- package/dist/errors.d.ts +67 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +116 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +10 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +136 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/poll-loop.d.ts +26 -0
- package/dist/react/poll-loop.d.ts.map +1 -0
- package/dist/react/poll-loop.js +182 -0
- package/dist/react/poll-loop.js.map +1 -0
- package/dist/react/use-bot-poll.d.ts +22 -0
- package/dist/react/use-bot-poll.d.ts.map +1 -0
- package/dist/react/use-bot-poll.js +184 -0
- package/dist/react/use-bot-poll.js.map +1 -0
- package/dist/receiver.d.ts +3 -0
- package/dist/receiver.d.ts.map +1 -0
- package/dist/receiver.js +209 -0
- package/dist/receiver.js.map +1 -0
- package/dist/responses.d.ts +34 -0
- package/dist/responses.d.ts.map +1 -0
- package/dist/responses.js +31 -0
- package/dist/responses.js.map +1 -0
- package/dist/sender.d.ts +3 -0
- package/dist/sender.d.ts.map +1 -0
- package/dist/sender.js +56 -0
- package/dist/sender.js.map +1 -0
- package/dist/types.d.ts +80 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/examples/send-and-receive.mjs +62 -0
- package/package.json +53 -3
package/README.md
CHANGED
|
@@ -1,3 +1,143 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @michaelserra.dev/sable-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Client library for a Sable channel. It derives an AES-256-GCM key from a passphrase, encrypts on the sender, and decrypts on the receiver. The API receives the encrypted envelope and the channel credential. It does not receive the passphrase, the key, or the plaintext.
|
|
4
|
+
|
|
5
|
+
Install it from npm with `npm install @michaelserra.dev/sable-sdk`. `@michaelserra.dev/sable-shared` is installed with it. Every request goes to `https://sable.michaelserra.dev`. There is no API URL to configure.
|
|
6
|
+
|
|
7
|
+
## Runtime
|
|
8
|
+
|
|
9
|
+
- Node.js 22.22 or newer, which is the workspace baseline. Node.js 24 is what the tests run on.
|
|
10
|
+
- Browsers that provide the Web Crypto API and `fetch`.
|
|
11
|
+
- The SDK calls global `crypto.subtle` and `fetch`. It does not use `node:crypto`. The core entry has no React dependency. `useBotPoll` is exported from `@michaelserra.dev/sable-sdk/react`, and React is an optional peer of that entry.
|
|
12
|
+
|
|
13
|
+
PBKDF2-HMAC-SHA-256 at 600,000 iterations is the key derivation for new messages. On Node.js 24.20.0, measured with `node scripts/bench-kdf.mjs` on 2026-12-02, the median was 65.1 ms on this machine. The SDK derives a key once per process and keeps the `CryptoKey` in memory. See [docs/encryption-spec.md](../../docs/encryption-spec.md).
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @michaelserra.dev/sable-sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
In this repository the packages are linked with `workspace:*`. Build them with `pnpm --filter @michaelserra.dev/sable-sdk build`.
|
|
22
|
+
|
|
23
|
+
## Send
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { createBotBridge } from "@michaelserra.dev/sable-sdk";
|
|
27
|
+
|
|
28
|
+
const sender = createBotBridge({
|
|
29
|
+
channelId: "chan_AAAAAAAAAAAAAAAAAAAAAA",
|
|
30
|
+
publishToken: "sable_pub_...",
|
|
31
|
+
passphrase: "a channel passphrase",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const published = await sender.send({
|
|
35
|
+
type: "task.created",
|
|
36
|
+
payload: { taskId: "123" },
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`published` carries `messageId`, `sequence`, `publishedAt`, `expiresAt`, and `idempotentReplay`. The plaintext is JSON `{ "type", "payload" }`, at most 48 KiB, and the canonical envelope must fit in 64 KiB. The SDK rejects anything larger before the request.
|
|
41
|
+
|
|
42
|
+
`sender.salt` is the 16-byte salt written into every envelope from this sender. Keep it with the passphrase if a later process should use the same derived key. The salt is not secret: it is inside each envelope. Omit `salt` and the sender generates one. A new passphrase uses a new salt. Old envelopes still open with the salt they carry.
|
|
43
|
+
|
|
44
|
+
The same derived key must not encrypt more than 2^32 messages. Past that limit, create the sender with a new salt.
|
|
45
|
+
|
|
46
|
+
A publish retry reuses the message id and the envelope. `idempotentReplay: true` is success.
|
|
47
|
+
|
|
48
|
+
## Receive
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { createBotBridgeReceiver } from "@michaelserra.dev/sable-sdk";
|
|
52
|
+
|
|
53
|
+
const receiver = createBotBridgeReceiver({
|
|
54
|
+
channelId: "chan_AAAAAAAAAAAAAAAAAAAAAA",
|
|
55
|
+
receiverToken: "sable_rcv_...",
|
|
56
|
+
passphrase: "a channel passphrase",
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const { messages } = await receiver.poll();
|
|
60
|
+
for (const message of messages) {
|
|
61
|
+
try {
|
|
62
|
+
handle(message.payload);
|
|
63
|
+
await receiver.ack([message.id]);
|
|
64
|
+
} catch {
|
|
65
|
+
receiver.resetCursor(message.leaseExpiresAt);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`poll` leases messages and decrypts them. It does not acknowledge them. `ack` is a separate call, after the handler succeeds. Delivery is at-least-once: a message can arrive again if the lease ends before the ack.
|
|
71
|
+
|
|
72
|
+
The receiver keeps a cursor while `hasMore` is true. After the last page, the next poll starts from the oldest visible message so an expired lease can be picked up. `suggestedWaitMs` is `0` when another page is waiting, `5000` after a non-empty final page, and it doubles up to `30000` across empty polls. After a decryption failure, or after `resetCursor(leaseExpiresAt)`, it is the time left on that lease.
|
|
73
|
+
|
|
74
|
+
`resetCursor(leaseExpiresAt)` drops the cursor and refuses another automatic poll until that lease ends. A decryption failure does the same. The error's `messageId` is the id to ack if the application wants to drop a message it will never be able to open. Ack is never implied by a failed decrypt.
|
|
75
|
+
|
|
76
|
+
## React
|
|
77
|
+
|
|
78
|
+
`useBotPoll` polls, decrypts, and calls `onMessage` for each message. It acks a message only after that call resolves. A thrown handler is not acked. The hook then waits until the lease ends and polls again. Delivery stays at-least-once.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { useBotPoll } from "@michaelserra.dev/sable-sdk/react";
|
|
82
|
+
|
|
83
|
+
useBotPoll({
|
|
84
|
+
receiver: {
|
|
85
|
+
channelId: "chan_AAAAAAAAAAAAAAAAAAAAAA",
|
|
86
|
+
receiverToken: "sable_rcv_...",
|
|
87
|
+
passphrase: "a channel passphrase",
|
|
88
|
+
},
|
|
89
|
+
intervalMs: 5000,
|
|
90
|
+
batchSize: 10,
|
|
91
|
+
onMessage: async (message) => {
|
|
92
|
+
await handle(message.payload);
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
One poll is in flight per hook instance. Unmounting aborts the current request. A handler that has not finished is not acked.
|
|
98
|
+
|
|
99
|
+
The hook sets the receiver's `maxRetries` to 0 and does its own retry. Transient network failures, HTTP 500, HTTP 503, rate limits, and storage errors use exponential backoff with jitter. Authentication, authorization, validation, quota, and decryption failures stop the loop.
|
|
100
|
+
|
|
101
|
+
`loading` is true during a poll cycle. `retrying` is true while the hook waits to try again. `error` is the last failure and clears after a successful cycle. `retryAttempt` counts retries since the last success.
|
|
102
|
+
|
|
103
|
+
`intervalMs` is the idle wait. When the receiver's suggested wait is longer, the hook uses that. `batchSize` is the poll `limit`, an integer from 1 to 50. Omit it to use the server default.
|
|
104
|
+
|
|
105
|
+
The application that renders the hook must depend on React 19. Packages that only publish and poll do not.
|
|
106
|
+
|
|
107
|
+
## Errors
|
|
108
|
+
|
|
109
|
+
Errors are subclasses of `SableError`. Messages are fixed sentences. They do not include the passphrase, the key, the plaintext, or the credential.
|
|
110
|
+
|
|
111
|
+
| Class | When |
|
|
112
|
+
| --------------------------- | -------------------------------------------------------------------------------- |
|
|
113
|
+
| `SableAuthenticationError` | HTTP 401 |
|
|
114
|
+
| `SableAuthorizationError` | HTTP 403 |
|
|
115
|
+
| `SableNetworkError` | The request did not receive a response |
|
|
116
|
+
| `SableRateLimitError` | HTTP 429 `rate_limited`, with `retryAfterSeconds` |
|
|
117
|
+
| `SableQuotaError` | HTTP 429 `quota_exceeded` |
|
|
118
|
+
| `SableEnvelopeError` | The envelope is not version 1 |
|
|
119
|
+
| `SableDecryptionError` | The tag check failed, including a wrong passphrase |
|
|
120
|
+
| `SableExpiredMessageError` | A polled message is already past `expiresAt` |
|
|
121
|
+
| `SableLeaseError` | The next poll would run while a failed message is still leased |
|
|
122
|
+
| `SableValidationError` | The local arguments are invalid |
|
|
123
|
+
| `SableConflictError` | The message id was published with a different envelope |
|
|
124
|
+
| `SablePayloadTooLargeError` | Plaintext exceeds 48 KiB, the envelope exceeds 64 KiB, or the server returns 413 |
|
|
125
|
+
| `SableMessageNotFoundError` | An ack id was `not_found` and this process has not acked it |
|
|
126
|
+
|
|
127
|
+
Network failures, HTTP 500, HTTP 503, and `rate_limited` retry up to `maxRetries` (default 2, maximum 5). The same publish body is sent again. `quota_exceeded` does not retry. A rate limit waits for `Retry-After`, capped at 60 seconds.
|
|
128
|
+
|
|
129
|
+
`not_found` on an ack is an error the first time. `acked` and `already_acked` are success. A later `not_found` for an id this process already acked does not throw.
|
|
130
|
+
|
|
131
|
+
## Example
|
|
132
|
+
|
|
133
|
+
`examples/send-and-receive.mjs` publishes one event and acknowledges it. Build the package first, then:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
SABLE_CHANNEL_ID=chan_... \
|
|
137
|
+
SABLE_PUBLISH_TOKEN=sable_pub_... \
|
|
138
|
+
SABLE_RECEIVER_TOKEN=sable_rcv_... \
|
|
139
|
+
SABLE_PASSPHRASE='a channel passphrase' \
|
|
140
|
+
node packages/sdk/examples/send-and-receive.mjs
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
The example prints the decrypted payload on the machine that holds the passphrase. It does not print the tokens.
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Production API. Callers do not choose a host. */
|
|
2
|
+
export declare const SABLE_API_URL = "https://sable.michaelserra.dev";
|
|
3
|
+
export interface ResolvedTransport {
|
|
4
|
+
apiUrl: string;
|
|
5
|
+
channelId: string;
|
|
6
|
+
fetch: typeof fetch;
|
|
7
|
+
maxRetries: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function resolveTransport(config: {
|
|
10
|
+
channelId: string;
|
|
11
|
+
fetch?: typeof fetch;
|
|
12
|
+
maxRetries?: number;
|
|
13
|
+
}): ResolvedTransport;
|
|
14
|
+
export declare function parsePublishToken(token: string): string;
|
|
15
|
+
export declare function parseReceiveToken(token: string): string;
|
|
16
|
+
export declare function messageUrl(apiUrl: string, channelId: string, suffix: "" | "/poll" | "/ack"): string;
|
|
17
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAQA,oDAAoD;AACpD,eAAO,MAAM,aAAa,mCAAmC,CAAC;AAI9D,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IACvC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GAAG,iBAAiB,CAOpB;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKvD;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAOvD;AAED,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,EAAE,GAAG,OAAO,GAAG,MAAM,GAC5B,MAAM,CAER"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { channelIdSchema, publishCredentialSchema, receiveCredentialSchema, } from "@michaelserra.dev/sable-shared";
|
|
2
|
+
import { SableValidationError } from "./errors.js";
|
|
3
|
+
/** Production API. Callers do not choose a host. */
|
|
4
|
+
export const SABLE_API_URL = "https://sable.michaelserra.dev";
|
|
5
|
+
const DEFAULT_MAX_RETRIES = 2;
|
|
6
|
+
export function resolveTransport(config) {
|
|
7
|
+
return {
|
|
8
|
+
apiUrl: SABLE_API_URL,
|
|
9
|
+
channelId: parseChannelId(config.channelId),
|
|
10
|
+
fetch: config.fetch ?? defaultFetch,
|
|
11
|
+
maxRetries: parseMaxRetries(config.maxRetries),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function parsePublishToken(token) {
|
|
15
|
+
if (!publishCredentialSchema.safeParse(token).success) {
|
|
16
|
+
throw new SableValidationError("publishToken is not a publish credential.");
|
|
17
|
+
}
|
|
18
|
+
return token;
|
|
19
|
+
}
|
|
20
|
+
export function parseReceiveToken(token) {
|
|
21
|
+
if (!receiveCredentialSchema.safeParse(token).success) {
|
|
22
|
+
throw new SableValidationError("receiverToken is not a receive credential.");
|
|
23
|
+
}
|
|
24
|
+
return token;
|
|
25
|
+
}
|
|
26
|
+
export function messageUrl(apiUrl, channelId, suffix) {
|
|
27
|
+
return `${apiUrl}/v1/channels/${channelId}/messages${suffix}`;
|
|
28
|
+
}
|
|
29
|
+
function parseChannelId(channelId) {
|
|
30
|
+
if (!channelIdSchema.safeParse(channelId).success) {
|
|
31
|
+
throw new SableValidationError("channelId is not a channel id.");
|
|
32
|
+
}
|
|
33
|
+
return channelId;
|
|
34
|
+
}
|
|
35
|
+
function parseMaxRetries(maxRetries) {
|
|
36
|
+
const value = maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
37
|
+
if (!Number.isInteger(value) || value < 0 || value > 5) {
|
|
38
|
+
throw new SableValidationError("maxRetries must be an integer from 0 to 5.");
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
function defaultFetch(input, init) {
|
|
43
|
+
if (typeof globalThis.fetch !== "function") {
|
|
44
|
+
throw new SableValidationError("This runtime has no fetch implementation.");
|
|
45
|
+
}
|
|
46
|
+
return globalThis.fetch(input, init);
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD,oDAAoD;AACpD,MAAM,CAAC,MAAM,aAAa,GAAG,gCAAgC,CAAC;AAE9D,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAS9B,MAAM,UAAU,gBAAgB,CAAC,MAIhC;IACC,OAAO;QACL,MAAM,EAAE,aAAa;QACrB,SAAS,EAAE,cAAc,CAAC,MA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ED,SAAS,YAAY,CACnB,KAAwB,EACxB,IAAkB;IAElB,IAAI,OAAO,UAAU,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;QAC3C,MAAM,IAAI,oBAAoB,CAAC,2CAA2C,CAAC,CAAC;IAC9E,CAAC;IACD,OAAO,UAAU,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AACvC,CAAC"}
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Envelope } from "@michaelserra.dev/sable-shared";
|
|
2
|
+
import type { ApplicationMessage } from "./types.js";
|
|
3
|
+
export declare function createMessageId(): string;
|
|
4
|
+
export declare function createSalt(): string;
|
|
5
|
+
export declare function normalizePassphrase(passphrase: string): string;
|
|
6
|
+
export declare function assertSalt(salt: string): string;
|
|
7
|
+
export declare function encryptMessage<TPayload>(input: {
|
|
8
|
+
passphrase: string;
|
|
9
|
+
salt: string;
|
|
10
|
+
message: ApplicationMessage<TPayload>;
|
|
11
|
+
messageId?: string;
|
|
12
|
+
}): Promise<{
|
|
13
|
+
messageId: string;
|
|
14
|
+
envelope: Envelope;
|
|
15
|
+
}>;
|
|
16
|
+
export declare function decryptMessage<TPayload = unknown>(passphrase: string, value: unknown): Promise<ApplicationMessage<TPayload>>;
|
|
17
|
+
/**
|
|
18
|
+
* Draws a fresh 12-byte IV and records it for this key.
|
|
19
|
+
* A repeated draw is discarded. The caller must not reuse the buffer.
|
|
20
|
+
*/
|
|
21
|
+
export declare function allocateIv(used: Set<string>, fill?: (bytes: Uint8Array<ArrayBuffer>) => void): {
|
|
22
|
+
bytes: Uint8Array<ArrayBuffer>;
|
|
23
|
+
encoded: string;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAkBL,KAAK,QAAQ,EACd,MAAM,gCAAgC,CAAC;AASxC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAgBrD,wBAAgB,eAAe,IAAI,MAAM,CAExC;AAED,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AAED,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAc9D;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO/C;AAED,wBAAsB,cAAc,CAAC,QAAQ,EAAE,KAAK,EAAE;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,kBAAkB,CAAC,QAAQ,CAAC,CAAC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,GAAG,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,CA0DrD;AAED,wBAAsB,cAAc,CAAC,QAAQ,GAAG,OAAO,EACrD,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CA2CvC;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,EACjB,IAAI,GAAE,CAAC,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,KAAK,IAAiB,GAC1D;IAAE,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAqBrD"}
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { AES_GCM_IV_BYTES, AES_GCM_KEY_BITS, AES_GCM_NAME, AES_GCM_TAG_BYTES, base64UrlDecodedLength, base64UrlToBytes, bytesToBase64Url, canonicalAad, canonicalJson, envelopeSchema, KDF_V1_HASH, KDF_V1_NAME, KDF_V1_RECOMMENDED_ITERATIONS, KDF_V1_SALT_BYTES, MAX_ENVELOPE_JSON_BYTES, MAX_PLAINTEXT_BYTES, messageIdSchema, } from "@michaelserra.dev/sable-shared";
|
|
2
|
+
import { SableDecryptionError, SableEncryptionError, SableEnvelopeError, SablePayloadTooLargeError, SableValidationError, } from "./errors.js";
|
|
3
|
+
const PASSPHRASE_MIN_CODE_POINTS = 12;
|
|
4
|
+
const PASSPHRASE_MAX_CODE_POINTS = 128;
|
|
5
|
+
const TYPE_MAX_CODE_POINTS = 128;
|
|
6
|
+
/** NIST SP 800-38D random 96-bit IV limit under one key. */
|
|
7
|
+
const RANDOM_IV_LIMIT = 2 ** 32;
|
|
8
|
+
const IV_DRAW_ATTEMPTS = 8;
|
|
9
|
+
const keyCache = new Map();
|
|
10
|
+
export function createMessageId() {
|
|
11
|
+
return `msg_${bytesToBase64Url(randomBytes(16))}`;
|
|
12
|
+
}
|
|
13
|
+
export function createSalt() {
|
|
14
|
+
return bytesToBase64Url(randomBytes(KDF_V1_SALT_BYTES));
|
|
15
|
+
}
|
|
16
|
+
export function normalizePassphrase(passphrase) {
|
|
17
|
+
if (typeof passphrase !== "string") {
|
|
18
|
+
throw new SableValidationError("Passphrase must be a string.");
|
|
19
|
+
}
|
|
20
|
+
const normalized = passphrase.normalize("NFC");
|
|
21
|
+
const length = Array.from(normalized).length;
|
|
22
|
+
if (length < PASSPHRASE_MIN_CODE_POINTS ||
|
|
23
|
+
length > PASSPHRASE_MAX_CODE_POINTS) {
|
|
24
|
+
throw new SableValidationError("Passphrase must be 12 to 128 characters.");
|
|
25
|
+
}
|
|
26
|
+
return normalized;
|
|
27
|
+
}
|
|
28
|
+
export function assertSalt(salt) {
|
|
29
|
+
if (base64UrlDecodedLength(salt) !== KDF_V1_SALT_BYTES) {
|
|
30
|
+
throw new SableValidationError("Salt must be 16 bytes of unpadded base64url.");
|
|
31
|
+
}
|
|
32
|
+
return salt;
|
|
33
|
+
}
|
|
34
|
+
export async function encryptMessage(input) {
|
|
35
|
+
const passphrase = normalizePassphrase(input.passphrase);
|
|
36
|
+
const salt = assertSalt(input.salt);
|
|
37
|
+
const messageId = input.messageId ?? createMessageId();
|
|
38
|
+
if (!messageIdSchema.safeParse(messageId).success) {
|
|
39
|
+
throw new SableValidationError("messageId is not a valid message id.");
|
|
40
|
+
}
|
|
41
|
+
const plaintext = serializePlaintext(input.message);
|
|
42
|
+
const entry = cachedKey(passphrase, salt, KDF_V1_RECOMMENDED_ITERATIONS);
|
|
43
|
+
const iv = allocateIv(entry.ivs);
|
|
44
|
+
const draft = {
|
|
45
|
+
v: 1,
|
|
46
|
+
messageId,
|
|
47
|
+
kdf: {
|
|
48
|
+
version: 1,
|
|
49
|
+
name: KDF_V1_NAME,
|
|
50
|
+
hash: KDF_V1_HASH,
|
|
51
|
+
iterations: KDF_V1_RECOMMENDED_ITERATIONS,
|
|
52
|
+
salt,
|
|
53
|
+
},
|
|
54
|
+
alg: AES_GCM_NAME,
|
|
55
|
+
iv: iv.encoded,
|
|
56
|
+
ciphertext: bytesToBase64Url(new Uint8Array()),
|
|
57
|
+
tag: bytesToBase64Url(new Uint8Array(AES_GCM_TAG_BYTES)),
|
|
58
|
+
};
|
|
59
|
+
const aad = canonicalAad(draft);
|
|
60
|
+
const key = await entry.key;
|
|
61
|
+
let encrypted;
|
|
62
|
+
try {
|
|
63
|
+
encrypted = await subtle().encrypt({
|
|
64
|
+
name: "AES-GCM",
|
|
65
|
+
iv: bufferSource(iv.bytes),
|
|
66
|
+
additionalData: bufferSource(aad),
|
|
67
|
+
tagLength: AES_GCM_TAG_BYTES * 8,
|
|
68
|
+
}, key, bufferSource(plaintext));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
throw new SableEncryptionError("Encryption failed.");
|
|
72
|
+
}
|
|
73
|
+
const combined = new Uint8Array(encrypted);
|
|
74
|
+
if (combined.byteLength < AES_GCM_TAG_BYTES) {
|
|
75
|
+
throw new SableEncryptionError("Encryption failed.");
|
|
76
|
+
}
|
|
77
|
+
const split = combined.byteLength - AES_GCM_TAG_BYTES;
|
|
78
|
+
const candidate = {
|
|
79
|
+
...draft,
|
|
80
|
+
ciphertext: bytesToBase64Url(combined.subarray(0, split)),
|
|
81
|
+
tag: bytesToBase64Url(combined.subarray(split)),
|
|
82
|
+
};
|
|
83
|
+
if (canonicalJson(candidate).length > MAX_ENVELOPE_JSON_BYTES) {
|
|
84
|
+
throw new SablePayloadTooLargeError();
|
|
85
|
+
}
|
|
86
|
+
return { messageId, envelope: parseEnvelope(candidate) };
|
|
87
|
+
}
|
|
88
|
+
export async function decryptMessage(passphrase, value) {
|
|
89
|
+
const normalized = normalizePassphrase(passphrase);
|
|
90
|
+
const envelope = parseEnvelope(value);
|
|
91
|
+
const salt = base64UrlToBytes(envelope.kdf.salt);
|
|
92
|
+
const iv = base64UrlToBytes(envelope.iv);
|
|
93
|
+
const ciphertext = base64UrlToBytes(envelope.ciphertext);
|
|
94
|
+
const tag = base64UrlToBytes(envelope.tag);
|
|
95
|
+
if (salt === null ||
|
|
96
|
+
iv === null ||
|
|
97
|
+
ciphertext === null ||
|
|
98
|
+
tag === null ||
|
|
99
|
+
tag.byteLength !== AES_GCM_TAG_BYTES) {
|
|
100
|
+
throw new SableEnvelopeError();
|
|
101
|
+
}
|
|
102
|
+
const combined = new Uint8Array(ciphertext.byteLength + tag.byteLength);
|
|
103
|
+
combined.set(ciphertext, 0);
|
|
104
|
+
combined.set(tag, ciphertext.byteLength);
|
|
105
|
+
const entry = cachedKey(normalized, envelope.kdf.salt, envelope.kdf.iterations);
|
|
106
|
+
let plain;
|
|
107
|
+
try {
|
|
108
|
+
plain = await subtle().decrypt({
|
|
109
|
+
name: "AES-GCM",
|
|
110
|
+
iv: bufferSource(iv),
|
|
111
|
+
additionalData: bufferSource(canonicalAad(envelope)),
|
|
112
|
+
tagLength: AES_GCM_TAG_BYTES * 8,
|
|
113
|
+
}, await entry.key, bufferSource(combined));
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
throw new SableDecryptionError();
|
|
117
|
+
}
|
|
118
|
+
return parsePlaintext(new Uint8Array(plain));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Draws a fresh 12-byte IV and records it for this key.
|
|
122
|
+
* A repeated draw is discarded. The caller must not reuse the buffer.
|
|
123
|
+
*/
|
|
124
|
+
export function allocateIv(used, fill = fillRandom) {
|
|
125
|
+
if (used.size >= RANDOM_IV_LIMIT) {
|
|
126
|
+
throw new SableEncryptionError("This passphrase salt has reached the AES-GCM IV limit. Choose a new salt.");
|
|
127
|
+
}
|
|
128
|
+
for (let attempt = 0; attempt < IV_DRAW_ATTEMPTS; attempt += 1) {
|
|
129
|
+
const bytes = new Uint8Array(AES_GCM_IV_BYTES);
|
|
130
|
+
fill(bytes);
|
|
131
|
+
const encoded = bytesToBase64Url(bytes);
|
|
132
|
+
if (base64UrlDecodedLength(encoded) !== AES_GCM_IV_BYTES) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (!used.has(encoded)) {
|
|
136
|
+
used.add(encoded);
|
|
137
|
+
return { bytes, encoded };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
throw new SableEncryptionError("Could not allocate a unique IV.");
|
|
141
|
+
}
|
|
142
|
+
function cachedKey(passphrase, salt, iterations) {
|
|
143
|
+
const id = `1\u0000${iterations}\u0000${salt}\u0000${passphrase}`;
|
|
144
|
+
const existing = keyCache.get(id);
|
|
145
|
+
if (existing) {
|
|
146
|
+
return existing;
|
|
147
|
+
}
|
|
148
|
+
const saltBytes = base64UrlToBytes(salt);
|
|
149
|
+
if (saltBytes === null) {
|
|
150
|
+
throw new SableValidationError("Salt must be 16 bytes of unpadded base64url.");
|
|
151
|
+
}
|
|
152
|
+
const entry = {
|
|
153
|
+
key: deriveAesKey(passphrase, saltBytes, iterations),
|
|
154
|
+
ivs: new Set(),
|
|
155
|
+
};
|
|
156
|
+
entry.key.catch(() => {
|
|
157
|
+
if (keyCache.get(id) === entry) {
|
|
158
|
+
keyCache.delete(id);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
keyCache.set(id, entry);
|
|
162
|
+
return entry;
|
|
163
|
+
}
|
|
164
|
+
async function deriveAesKey(passphrase, salt, iterations) {
|
|
165
|
+
const material = await subtle().importKey("raw", bufferSource(utf8(passphrase)), "PBKDF2", false, ["deriveKey"]);
|
|
166
|
+
return subtle().deriveKey({
|
|
167
|
+
name: "PBKDF2",
|
|
168
|
+
hash: "SHA-256",
|
|
169
|
+
salt: bufferSource(salt),
|
|
170
|
+
iterations,
|
|
171
|
+
}, material, { name: "AES-GCM", length: AES_GCM_KEY_BITS }, false, ["encrypt", "decrypt"]);
|
|
172
|
+
}
|
|
173
|
+
function parseEnvelope(value) {
|
|
174
|
+
const parsed = envelopeSchema.safeParse(value);
|
|
175
|
+
if (!parsed.success) {
|
|
176
|
+
throw new SableEnvelopeError();
|
|
177
|
+
}
|
|
178
|
+
return parsed.data;
|
|
179
|
+
}
|
|
180
|
+
function serializePlaintext(message) {
|
|
181
|
+
if (message === null ||
|
|
182
|
+
typeof message !== "object" ||
|
|
183
|
+
Array.isArray(message)) {
|
|
184
|
+
throw new SableValidationError("Message must be an object.");
|
|
185
|
+
}
|
|
186
|
+
if (typeof message.type !== "string") {
|
|
187
|
+
throw new SableValidationError("Message type must be a string.");
|
|
188
|
+
}
|
|
189
|
+
const type = message.type.normalize("NFC");
|
|
190
|
+
const typeLength = Array.from(type).length;
|
|
191
|
+
if (typeLength < 1 || typeLength > TYPE_MAX_CODE_POINTS) {
|
|
192
|
+
throw new SableValidationError("Message type must be 1 to 128 characters.");
|
|
193
|
+
}
|
|
194
|
+
if (message.payload === undefined) {
|
|
195
|
+
throw new SableValidationError("Message payload is required.");
|
|
196
|
+
}
|
|
197
|
+
let json;
|
|
198
|
+
try {
|
|
199
|
+
json = JSON.stringify({ type, payload: message.payload });
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
throw new SableValidationError("Message payload must be JSON serializable.");
|
|
203
|
+
}
|
|
204
|
+
if (typeof json !== "string") {
|
|
205
|
+
throw new SableValidationError("Message payload must be JSON serializable.");
|
|
206
|
+
}
|
|
207
|
+
let parsed;
|
|
208
|
+
try {
|
|
209
|
+
parsed = JSON.parse(json);
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
throw new SableValidationError("Message payload must be JSON serializable.");
|
|
213
|
+
}
|
|
214
|
+
if (!isRecord(parsed) || parsed.type !== type || !("payload" in parsed)) {
|
|
215
|
+
throw new SableValidationError("Message payload must be JSON serializable.");
|
|
216
|
+
}
|
|
217
|
+
const bytes = utf8(json);
|
|
218
|
+
if (bytes.byteLength > MAX_PLAINTEXT_BYTES) {
|
|
219
|
+
throw new SableValidationError("Message plaintext exceeds 48 KiB.");
|
|
220
|
+
}
|
|
221
|
+
return bytes;
|
|
222
|
+
}
|
|
223
|
+
function parsePlaintext(bytes) {
|
|
224
|
+
let parsed;
|
|
225
|
+
try {
|
|
226
|
+
parsed = JSON.parse(new TextDecoder().decode(bytes));
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
throw new SableValidationError("Decrypted message is not valid JSON.");
|
|
230
|
+
}
|
|
231
|
+
if (!isRecord(parsed) ||
|
|
232
|
+
typeof parsed.type !== "string" ||
|
|
233
|
+
!("payload" in parsed)) {
|
|
234
|
+
throw new SableValidationError("Decrypted message is missing type or payload.");
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
type: parsed.type,
|
|
238
|
+
payload: parsed.payload,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function subtle() {
|
|
242
|
+
const cryptoApi = globalThis.crypto;
|
|
243
|
+
if (!cryptoApi?.subtle) {
|
|
244
|
+
throw new SableEncryptionError("Web Crypto is not available in this runtime.");
|
|
245
|
+
}
|
|
246
|
+
return cryptoApi.subtle;
|
|
247
|
+
}
|
|
248
|
+
function fillRandom(bytes) {
|
|
249
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
250
|
+
}
|
|
251
|
+
function bufferSource(bytes) {
|
|
252
|
+
const copy = new ArrayBuffer(bytes.byteLength);
|
|
253
|
+
new Uint8Array(copy).set(bytes);
|
|
254
|
+
return copy;
|
|
255
|
+
}
|
|
256
|
+
function randomBytes(length) {
|
|
257
|
+
const bytes = new Uint8Array(length);
|
|
258
|
+
fillRandom(bytes);
|
|
259
|
+
return bytes;
|
|
260
|
+
}
|
|
261
|
+
function utf8(value) {
|
|
262
|
+
return new TextEncoder().encode(value);
|
|
263
|
+
}
|
|
264
|
+
function isRecord(value) {
|
|
265
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
266
|
+
}
|
|
267
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,iBAAiB,EACjB,sBAAsB,EACtB,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,cAAc,EACd,WAAW,EACX,WAAW,EACX,6BAA6B,EAC7B,iBAAiB,EACjB,uBAAuB,EACvB,mBAAmB,EACnB,eAAe,GAEhB,MAAM,gCAAgC,CAAC;AAExC,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,kBAAkB,EAClB,yBAAyB,EACzB,oBAAoB,GACrB,MAAM,aAAa,CAAC;AAGrB,MAAM,0BAA0B,GAAG,EAAE,CAAC;AACtC,MAAM,0BAA0B,GAAG,GAAG,CAAC;AACvC,MAAM,oBAAoB,GAAG,GAAG,CAAC;AACjC,4DAA4D;AAC5D,MAAM,eAAe,GAAG,CAAC,IAAI,EAAE,CAAC;AAChC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAO3B,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAsB,CAAC;AAE/C,MAAM,UAAU,eAAe;IAC7B,O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MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,UAAU,CAAC,KAAK,CAAC,CAAC;IAClB,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,IAAI,CAAC,KAAa;IACzB,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK errors use fixed sentences. They do not carry passphrases, keys,
|
|
3
|
+
* plaintext, credential secrets, or envelope bodies.
|
|
4
|
+
*/
|
|
5
|
+
export declare class SableError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
export declare class SableAuthenticationError extends SableError {
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
export declare class SableAuthorizationError extends SableError {
|
|
12
|
+
constructor();
|
|
13
|
+
}
|
|
14
|
+
export declare class SableNetworkError extends SableError {
|
|
15
|
+
constructor();
|
|
16
|
+
}
|
|
17
|
+
export declare class SableRateLimitError extends SableError {
|
|
18
|
+
readonly retryAfterSeconds: number | undefined;
|
|
19
|
+
constructor(retryAfterSeconds?: number);
|
|
20
|
+
}
|
|
21
|
+
export declare class SableQuotaError extends SableError {
|
|
22
|
+
constructor();
|
|
23
|
+
}
|
|
24
|
+
export declare class SableEnvelopeError extends SableError {
|
|
25
|
+
readonly messageId: string | undefined;
|
|
26
|
+
constructor(messageId?: string);
|
|
27
|
+
}
|
|
28
|
+
export declare class SableDecryptionError extends SableError {
|
|
29
|
+
readonly messageId: string | undefined;
|
|
30
|
+
constructor(messageId?: string);
|
|
31
|
+
}
|
|
32
|
+
export declare class SableLeaseError extends SableError {
|
|
33
|
+
readonly retryAfterMs: number;
|
|
34
|
+
constructor(retryAfterMs: number);
|
|
35
|
+
}
|
|
36
|
+
export declare class SableExpiredMessageError extends SableError {
|
|
37
|
+
readonly messageId: string;
|
|
38
|
+
constructor(messageId: string);
|
|
39
|
+
}
|
|
40
|
+
export declare class SableValidationError extends SableError {
|
|
41
|
+
readonly messageId: string | undefined;
|
|
42
|
+
constructor(message: string, messageId?: string);
|
|
43
|
+
}
|
|
44
|
+
export declare class SableConflictError extends SableError {
|
|
45
|
+
constructor();
|
|
46
|
+
}
|
|
47
|
+
export declare class SablePayloadTooLargeError extends SableError {
|
|
48
|
+
constructor();
|
|
49
|
+
}
|
|
50
|
+
export declare class SableStorageError extends SableError {
|
|
51
|
+
constructor();
|
|
52
|
+
}
|
|
53
|
+
export declare class SableServerError extends SableError {
|
|
54
|
+
constructor();
|
|
55
|
+
}
|
|
56
|
+
export declare class SableNotFoundError extends SableError {
|
|
57
|
+
constructor();
|
|
58
|
+
}
|
|
59
|
+
export declare class SableMessageNotFoundError extends SableError {
|
|
60
|
+
readonly messageIds: readonly string[];
|
|
61
|
+
constructor(messageIds: readonly string[]);
|
|
62
|
+
}
|
|
63
|
+
export declare class SableEncryptionError extends SableError {
|
|
64
|
+
constructor(message: string);
|
|
65
|
+
}
|
|
66
|
+
export declare function isRetryableError(error: SableError): boolean;
|
|
67
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,wBAAyB,SAAQ,UAAU;;CAIvD;AAED,qBAAa,uBAAwB,SAAQ,UAAU;;CAItD;AAED,qBAAa,iBAAkB,SAAQ,UAAU;;CAIhD;AAED,qBAAa,mBAAoB,SAAQ,U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