@myfield/sdk 0.9.1 → 0.9.3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@myfield/sdk",
3
- "version": "0.9.1",
4
- "description": "Myfield open SDK: instance auth and end-to-end-encrypted bucket sync for third-party web apps.",
3
+ "version": "0.9.3",
4
+ "description": "MyField SDK for third-party web apps: local-first object storage, end-to-end-encrypted sync, and internal MyField authorization.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "OPENTECH AI PTE. LTD.",
7
7
  "homepage": "https://github.com/myfield-one/sdk",
@@ -25,10 +25,8 @@
25
25
  }
26
26
  },
27
27
  "files": [
28
- "dist/",
29
- "src/",
28
+ "dist/index.js",
30
29
  "types/",
31
- "docs/",
32
30
  "README.md",
33
31
  "LICENSE",
34
32
  "CHANGELOG.md"
@@ -44,15 +42,26 @@
44
42
  ],
45
43
  "sideEffects": false,
46
44
  "scripts": {
47
- "build": "esbuild src/index.ts --bundle --format=esm --platform=browser --target=es2022 --outfile=dist/index.js",
45
+ "build": "npm run build:js && node scripts/api.mjs --build",
48
46
  "prepack": "npm run build",
49
47
  "test": "node --experimental-strip-types --test test/*.test.mjs",
50
- "typecheck": "tsc --noEmit"
48
+ "verify:remote": "node scripts/verify-remote.mjs",
49
+ "typecheck": "tsc --noEmit",
50
+ "typecheck:api": "node scripts/verify-docs.mjs && tsc -p tsconfig.consumer.json",
51
+ "build:js": "esbuild src/index.ts --bundle --format=esm --platform=browser --target=es2022 --outfile=dist/index.js",
52
+ "api:update": "node scripts/api.mjs --update",
53
+ "api:check": "node scripts/api.mjs",
54
+ "test:package": "node scripts/verify-package.mjs",
55
+ "test:docs": "node scripts/verify-docs.mjs",
56
+ "test:tooling": "node --test test/api-generation.test.mjs"
51
57
  },
52
58
  "devDependencies": {
53
59
  "@types/node": "^25.5.2",
54
60
  "esbuild": "^0.28.1",
55
61
  "fake-indexeddb": "6.2.4",
56
- "typescript": "5.5.4"
62
+ "typescript": "5.5.4",
63
+ "@microsoft/api-extractor": "7.59.4",
64
+ "typedoc": "0.28.20",
65
+ "typedoc-plugin-markdown": "4.14.1"
57
66
  }
58
67
  }
package/types/index.d.ts CHANGED
@@ -1,70 +1,412 @@
1
- export type BinaryInput = Blob | ArrayBuffer | Uint8Array;
1
+ /** Binary input supported by Local field values.
2
+ * @public
3
+ */
4
+ export declare type BinaryInput = Blob | ArrayBuffer | Uint8Array;
2
5
 
3
- export type BinaryOptions = {
4
- mimeType?: string;
5
- name?: string;
6
- lastModified?: number;
6
+ /** Optional binary metadata stored inside encrypted Local payloads.
7
+ * @public
8
+ */
9
+ export declare type BinaryOptions = {
10
+ mimeType?: string;
11
+ name?: string;
12
+ lastModified?: number;
7
13
  };
8
14
 
9
- export type BucketBinaryValue = {
10
- readonly kind: "binary";
11
- readonly size: number;
12
- readonly mimeType?: string;
13
- readonly name?: string;
14
- readonly lastModified?: number;
15
- blob(): Promise<Blob>;
16
- arrayBuffer(): Promise<ArrayBuffer>;
15
+ /** Local-first object storage. Network synchronization requires a connection.
16
+ * @public
17
+ */
18
+ export declare type Bucket = {
19
+ /** The local resource label; the granted remote bucket ID may differ. */
20
+ readonly id: string;
21
+ /** Current connection state. */
22
+ readonly state: BucketState;
23
+ /** Wraps a binary field without uploading it. */
24
+ binary(input: BinaryInput | BucketBinaryValue, options?: BinaryOptions): BucketBinaryValue;
25
+ /** @deprecated No-op kept for compatibility; operations initialize storage lazily. */
26
+ ready(): Promise<void>;
27
+ /** Replaces known local fields; concurrent unknown remote fields remain mergeable. */
28
+ put(objectId: string, object: Record<string, unknown>): Promise<void>;
29
+ /** Reads local data, returning null for absent or deleted objects. */
30
+ get(objectId: string): Promise<Record<string, unknown> | null>;
31
+ /** Updates top-level fields; undefined deletes a field. */
32
+ update(objectId: string, fields: Record<string, unknown>): Promise<void>;
33
+ /** Creates a local deletion and schedules synchronization when connected. */
34
+ delete(objectId: string): Promise<void>;
35
+ /** Lists known local objects newest first; does not query the cloud. */
36
+ list(options?: {
37
+ prefix?: string;
38
+ includeDeleted?: boolean;
39
+ }): Promise<ListEntry[]>;
40
+ /** Restores saved authorization without prompting. Load local data first. */
41
+ resume(): Promise<BucketState>;
42
+ /** Connects from a user gesture; authorization failures report disconnected state. */
43
+ connect(options?: ConnectOptions): Promise<BucketState>;
44
+ /** Drops authorization and connection while retaining local objects and identity keys. */
45
+ disconnect(): Promise<void>;
46
+ /** Runs one Local sync pass; rejects when disconnected. */
47
+ sync(): Promise<void>;
48
+ /** Enables or disables automatic Local sync; an active pass may finish. */
49
+ setAutoSync(enabled: boolean): void;
50
+ /** Subscribes to state or local changes; returns an unsubscribe function. */
51
+ on(event: "state", handler: (state: BucketState, detail?: string) => void): () => void;
52
+ /** Subscribes to state or local changes; returns an unsubscribe function. */
53
+ on(event: "change", handler: (objectId: string) => void): () => void;
54
+ /** Stops this bucket runtime and releases listeners and sync resources. */
55
+ dispose(): void;
56
+ /* Excluded from this release type: __bindTestTransport */
17
57
  };
18
58
 
19
- export type CreateInstanceOptions = {
20
- namespace?: string;
21
- storage?: "indexeddb" | "memory";
59
+ /** A binary field handle. Read bytes explicitly through its asynchronous methods.
60
+ * @public
61
+ */
62
+ export declare type BucketBinaryValue = {
63
+ readonly kind: "binary";
64
+ readonly size: number;
65
+ readonly mimeType?: string;
66
+ readonly name?: string;
67
+ readonly lastModified?: number;
68
+ blob(): Promise<Blob>;
69
+ arrayBuffer(): Promise<ArrayBuffer>;
22
70
  };
23
71
 
24
- export type BucketState = "disconnected" | "connecting" | "connected";
72
+ /** An issued bucket authorization and the bucket key wrapped for its target device.
73
+ * @public
74
+ */
75
+ export declare type BucketGrant = {
76
+ v: 1;
77
+ grantRecordId: string;
78
+ mfid: string;
79
+ resourceId: string;
80
+ instanceId: string;
81
+ issuedAt: string;
82
+ keyId: string;
83
+ capabilities: ("read" | "write")[];
84
+ envelope: {
85
+ kind: "bucket-key";
86
+ bucketId: string;
87
+ keyId: string;
88
+ targetInstanceId: string;
89
+ targetWrappingPublicJwk: JsonWebKey;
90
+ ephemeralPublicJwk: JsonWebKey;
91
+ wrapAlg: "ECDH-HKDF-SHA-256/AES-GCM-256";
92
+ salt: string;
93
+ iv: string;
94
+ aad: string;
95
+ ciphertext: string;
96
+ };
97
+ };
98
+
99
+ /** Current object heads and the starting cursor for feed catch-up.
100
+ * @public
101
+ */
102
+ export declare type BucketIndex = {
103
+ bucketId: string;
104
+ objects: FeedHint[];
105
+ feedCursor: string | null;
106
+ hasMoreFeed: boolean;
107
+ [field: string]: unknown;
108
+ };
25
109
 
26
- export type ListEntry = {
27
- id: string;
28
- updatedAt: string;
29
- deleted?: true;
110
+ /** The granted non-extractable AES-GCM key and remote bucket identity for app encryption.
111
+ * @public
112
+ */
113
+ export declare type BucketKey = {
114
+ bucketId: string;
115
+ keyId: string;
116
+ cryptoKey: CryptoKey;
30
117
  };
31
118
 
32
- export type ConnectOptions = {
33
- authBase?: string;
34
- autoSync?: boolean;
119
+ /** Connection state shared with the associated resource.
120
+ * @public
121
+ */
122
+ export declare type BucketState = "disconnected" | "connecting" | "connected";
123
+
124
+ declare type BucketTransportLike = {
125
+ pullHead(wireObjectId: string): Promise<TransportHeadResult>;
126
+ pullPayload(wireObjectId: string, versionId: string): Promise<TransportPayloadResult>;
127
+ push(wireObjectId: string, plaintext: Uint8Array, expectedHeadVersionId: string | null): Promise<TransportPushResult>;
128
+ listHeads?(): Promise<Map<string, string>>;
35
129
  };
36
130
 
37
- export type Bucket = {
38
- readonly id: string;
39
- readonly state: BucketState;
40
- binary(input: BinaryInput | BucketBinaryValue, options?: BinaryOptions): BucketBinaryValue;
41
- ready(): Promise<void>;
42
- put(objectId: string, object: Record<string, unknown>): Promise<void>;
43
- get(objectId: string): Promise<Record<string, unknown> | null>;
44
- update(objectId: string, fields: Record<string, unknown>): Promise<void>;
45
- delete(objectId: string): Promise<void>;
46
- list(options?: { prefix?: string; includeDeleted?: boolean }): Promise<ListEntry[]>;
47
- resume(): Promise<BucketState>;
48
- connect(options?: ConnectOptions): Promise<BucketState>;
49
- disconnect(): Promise<void>;
50
- sync(): Promise<void>;
51
- setAutoSync(enabled: boolean): void;
52
- on(event: "state", handler: (state: BucketState, detail?: string) => void): () => void;
53
- on(event: "change", handler: (objectId: string) => void): () => void;
54
- dispose(): void;
131
+ /** Machine-readable SDK failure with optional HTTP and retry diagnostics.
132
+ * @public
133
+ */
134
+ export declare class ClientError extends Error {
135
+ readonly code: string;
136
+ readonly status?: number;
137
+ readonly requestId?: string;
138
+ readonly retryable: boolean;
139
+ readonly retryAfterMs?: number;
140
+ readonly maxBytes?: number;
141
+ constructor(code: string, message: string, options?: ClientErrorOptions);
142
+ }
143
+
144
+ /** HTTP diagnostics, retry information, payload limit and original cause.
145
+ * @public
146
+ */
147
+ export declare type ClientErrorOptions = {
148
+ status?: number;
149
+ requestId?: string;
150
+ retryable?: boolean;
151
+ retryAfterMs?: number;
152
+ maxBytes?: number;
153
+ cause?: unknown;
55
154
  };
56
155
 
57
- export type Instance = {
58
- readonly namespace: string;
59
- createBucket(options: { id: string }): Bucket;
156
+ /** App-encrypted bytes and metadata submitted against an expected remote head.
157
+ * @public
158
+ */
159
+ export declare type CommitInput = {
160
+ meta: VersionMeta;
161
+ /** App-encrypted payload; never submit private plaintext. */
162
+ payload: Blob | Uint8Array;
163
+ /** Last observed head, or null to create an object. */
164
+ expectedHeadVersionId: string | null;
60
165
  };
61
166
 
167
+ /** A successful commit or a CAS rejection; other failures throw ClientError.
168
+ * @public
169
+ */
170
+ export declare type CommitResult = {
171
+ ok: true;
172
+ } | {
173
+ ok: false;
174
+ reason: "version-mismatch" | "conflict";
175
+ };
176
+
177
+ /** Automatic Local synchronization settings; defaults to enabled.
178
+ * @public
179
+ */
180
+ export declare type ConnectOptions = {
181
+ /** Enable automatic Local sync after connecting; defaults to true. */
182
+ autoSync?: boolean;
183
+ };
184
+
185
+ /** Creates a local Instance with an explicit application ID. No network I/O.
186
+ * @public
187
+ */
188
+ export declare function createInstance(appId: string): Instance;
189
+
190
+ /** Creates a local Instance; appId defaults to the browser hostname.
191
+ * @public
192
+ */
62
193
  export declare function createInstance(options?: CreateInstanceOptions): Instance;
63
194
 
195
+ /** Local persistence scope and authorization page settings. Creating an Instance does not connect.
196
+ * @public
197
+ */
198
+ export declare type CreateInstanceOptions = {
199
+ /** Existing local storage scope; defaults to explicit appId, otherwise "default". */
200
+ namespace?: string;
201
+ /** IndexedDB by default; memory is useful for tests and ephemeral clients. */
202
+ storage?: "indexeddb" | "memory";
203
+ /** Stable lowercase domain-form application ID; defaults to the browser hostname. */
204
+ appId?: string;
205
+ /** Absolute HTTP(S) Auth page URL without credentials, query or fragment. */
206
+ authUrl?: string;
207
+ };
208
+
209
+ /** The current committed version, with optional newest-first ancestry evidence.
210
+ * @public
211
+ */
212
+ export declare type CurrentVersion = {
213
+ versionId: string;
214
+ headVersionMeta: VersionMeta;
215
+ history?: string[] | null;
216
+ [field: string]: unknown;
217
+ };
218
+
219
+ /** An object/version notification that prompts feed catch-up, not an object payload.
220
+ * @public
221
+ */
222
+ export declare type FeedHint = {
223
+ objectId: string;
224
+ versionId: string;
225
+ };
226
+
227
+ /** One page of changes. Continue from cursor while hasMore is true.
228
+ * @public
229
+ */
230
+ export declare type FeedPage = {
231
+ entries: FeedHint[];
232
+ cursor: string;
233
+ hasMore: boolean;
234
+ [field: string]: unknown;
235
+ };
236
+
237
+ /** Application identity, authorization and access to Local and Remote buckets.
238
+ * @public
239
+ */
240
+ export declare type Instance = {
241
+ /** Local persistence namespace; never sent as the remote bucket ID. */
242
+ readonly namespace: string;
243
+ /** Remote access for the default bucket; the app owns encryption and local storage. */
244
+ readonly remote: {
245
+ getBucket(): RemoteBucket;
246
+ };
247
+ /** Current connection state. */
248
+ readonly state: BucketState;
249
+ /** Returns the cached default Local bucket identified by appId. */
250
+ getBucket(): Bucket;
251
+ /** Reads the saved instance identity, or null when absent. */
252
+ getKey(): Promise<InstanceKey | null>;
253
+ /** Creates an identity when disconnected; rejects if a key already exists. */
254
+ createKey(): Promise<InstanceKey>;
255
+ /** Saves a device identity while disconnected. A matching grant for the default bucket may be imported with it. Use resume() to verify access with the server. */
256
+ importKey(key: InstanceKey, options?: {
257
+ bucketGrant: BucketGrant;
258
+ apiUrl: string;
259
+ }): Promise<void>;
260
+ /** Removes the identity and dependent authorization when disconnected. */
261
+ removeKey(): Promise<void>;
262
+ /** Restores saved authorization without prompting. Load local data first. */
263
+ resume(options?: {
264
+ autoSync?: boolean;
265
+ }): Promise<BucketState>;
266
+ /** Connects from a user gesture; authorization failures report disconnected state. */
267
+ connect(options?: {
268
+ autoSync?: boolean;
269
+ }): Promise<BucketState>;
270
+ /** Drops authorization and connection while retaining local objects and identity keys. */
271
+ disconnect(): Promise<void>;
272
+ /** Subscribes to connection state and error notifications; returns an unsubscribe function. */
273
+ on(event: "state", handler: (state: BucketState, error?: ClientError) => void): () => void;
274
+ /** Gets a Local handle for another stable resource label; creation is local-only. */
275
+ createBucket(options: {
276
+ id: string;
277
+ }): Bucket;
278
+ };
279
+
280
+ /** Persisted instance identity with public JWKs and non-extractable private CryptoKeys.
281
+ * @public
282
+ */
283
+ export declare type InstanceKey = {
284
+ instanceId: string;
285
+ signingPublicJwk: JsonWebKey;
286
+ signingPrivateKey: CryptoKey;
287
+ wrappingPublicJwk: JsonWebKey;
288
+ wrappingPrivateKey: CryptoKey;
289
+ createdAt: string;
290
+ };
291
+
292
+ /** Known local object ID and UTC ISO update time; deleted entries are opt-in.
293
+ * @public
294
+ */
295
+ export declare type ListEntry = {
296
+ id: string;
297
+ updatedAt: string;
298
+ deleted?: true;
299
+ };
300
+
301
+ /** A Local sync operation requires an active connection.
302
+ * @public
303
+ */
64
304
  export declare class NotConnectedError extends Error {
65
- constructor();
305
+ constructor();
66
306
  }
67
307
 
308
+ /** Version metadata and its payload. Consume or cancel Response bodies.
309
+ * @public
310
+ */
311
+ export declare type ObjectVersion<TPayload = Response> = {
312
+ meta: VersionMeta;
313
+ payload: TPayload;
314
+ };
315
+
316
+ /** The Local sync transport is unavailable.
317
+ * @public
318
+ */
68
319
  export declare class OfflineError extends Error {
69
- constructor();
320
+ constructor();
70
321
  }
322
+
323
+ /** Opaque pagination cursor, optional positive page limit and cancellation.
324
+ * @public
325
+ */
326
+ export declare type PageOptions = RequestOptions & {
327
+ cursor?: string | null;
328
+ limit?: number;
329
+ };
330
+
331
+ /** Storage-independent Remote operations. The application owns payload encryption, merge and recovery.
332
+ * @public
333
+ */
334
+ export declare interface RemoteBucket {
335
+ /** Returns the granted key handle without a request. Use your own AAD and retain keyId. */
336
+ getBucketKey(options?: RequestOptions): Promise<BucketKey>;
337
+ /** Reads the current head, or null when absent. knownVersionId requests ancestry evidence. */
338
+ getCurrent(objectId: string, options?: RequestOptions & {
339
+ knownVersionId?: string | null;
340
+ }): Promise<CurrentVersion | null>;
341
+ /** Reads metadata and a payload Response; consume or cancel its body. */
342
+ getVersion(objectId: string, versionId: string, options?: RequestOptions): Promise<ObjectVersion>;
343
+ /** Reads metadata for one committed version; missing versions throw. */
344
+ getVersionMeta(objectId: string, versionId: string, options?: RequestOptions): Promise<VersionMeta>;
345
+ /** Returns the encrypted payload Response; consume or cancel its body. */
346
+ getPayload(objectId: string, versionId: string, options?: RequestOptions): Promise<Response>;
347
+ /** Submits encrypted bytes with CAS. Preserve IDs and bytes after uncertain failures. */
348
+ commit(input: CommitInput, options?: RequestOptions): Promise<CommitResult>;
349
+ /** Reads committed-version history anchored at one object/version; follow nextCursor. */
350
+ listVersions(objectId: string, versionId: string, options?: PageOptions): Promise<VersionHistory>;
351
+ /** Reads the current heads snapshot and its feed catch-up cursor. */
352
+ getIndex(options?: RequestOptions): Promise<BucketIndex>;
353
+ /** Reads a feed page; continue from cursor while hasMore is true. */
354
+ listFeed(options?: PageOptions): Promise<FeedPage>;
355
+ /** Resolves when the change feed is ready. Abort or close the stream to stop. */
356
+ watchFeed(options?: RequestOptions): Promise<AsyncGenerator<FeedHint>>;
357
+ }
358
+
359
+ /** Cancellation for a Remote operation; requests are not automatically retried.
360
+ * @public
361
+ */
362
+ export declare type RequestOptions = {
363
+ signal?: AbortSignal;
364
+ };
365
+
366
+ declare type TransportHeadResult = {
367
+ found: false;
368
+ } | {
369
+ found: true;
370
+ versionId: string;
371
+ };
372
+
373
+ declare type TransportPayloadResult = {
374
+ found: false;
375
+ } | {
376
+ found: true;
377
+ plaintext: Uint8Array;
378
+ };
379
+
380
+ declare type TransportPushResult = {
381
+ ok: true;
382
+ versionId: string;
383
+ } | {
384
+ ok: false;
385
+ conflict: true;
386
+ remoteVersionId: string | null;
387
+ };
388
+
389
+ /** Committed-version metadata with ancestry and an opaque continuation cursor.
390
+ * @public
391
+ */
392
+ export declare type VersionHistory = VersionMeta & {
393
+ history: string[];
394
+ nextCursor: string | null;
395
+ };
396
+
397
+ /** Transport metadata. Application plaintext and private metadata must remain encrypted.
398
+ * @public
399
+ */
400
+ export declare type VersionMeta = {
401
+ /** App-supplied Remote object ID; Remote does not apply Local ID blinding. */
402
+ objectId: string;
403
+ /** Fixed version identity; reuse it with the same bytes after an uncertain outcome. */
404
+ versionId: string;
405
+ /** Direct parents; include the expected CAS head for an update. */
406
+ parentVersionIds: string[];
407
+ /** SHA-256 of the submitted ciphertext bytes when supplied. */
408
+ payloadHash?: string;
409
+ [field: string]: unknown;
410
+ };
411
+
412
+ export { }