@tinycloud/sdk-services 3.8.0-beta.9 → 3.8.1

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.
Files changed (55) hide show
  1. package/dist/{BaseService-DO7vZ67k.d.ts → BaseService-Ma2MwJAd.d.ts} +1 -1
  2. package/dist/{BaseService-D-29xShG.d.cts → BaseService-O0iyuFLk.d.cts} +1 -1
  3. package/dist/IHooksService-4QNODVy6.d.cts +120 -0
  4. package/dist/IHooksService-CvTpJbQD.d.ts +120 -0
  5. package/dist/ISQLService-Aly1CBsW.d.ts +242 -0
  6. package/dist/ISQLService-BOgWzHLT.d.cts +242 -0
  7. package/dist/context.d.cts +1 -1
  8. package/dist/context.d.ts +1 -1
  9. package/dist/encryption/index.cjs +80 -7
  10. package/dist/encryption/index.cjs.map +1 -1
  11. package/dist/encryption/index.d.cts +23 -11
  12. package/dist/encryption/index.d.ts +23 -11
  13. package/dist/encryption/index.js +79 -7
  14. package/dist/encryption/index.js.map +1 -1
  15. package/dist/encryption/named-keys.cjs.map +1 -1
  16. package/dist/encryption/named-keys.d.cts +2 -2
  17. package/dist/encryption/named-keys.d.ts +2 -2
  18. package/dist/encryption/named-keys.js.map +1 -1
  19. package/dist/encryption/resource.cjs.map +1 -1
  20. package/dist/encryption/resource.js.map +1 -1
  21. package/dist/feed/index.cjs +1309 -0
  22. package/dist/feed/index.cjs.map +1 -0
  23. package/dist/feed/index.d.cts +238 -0
  24. package/dist/feed/index.d.ts +238 -0
  25. package/dist/feed/index.js +1258 -0
  26. package/dist/feed/index.js.map +1 -0
  27. package/dist/index.cjs +131 -15
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.cts +16 -116
  30. package/dist/index.d.ts +16 -116
  31. package/dist/index.js +130 -15
  32. package/dist/index.js.map +1 -1
  33. package/dist/internal/decrypt-transport-response-error.d.cts +1 -1
  34. package/dist/internal/decrypt-transport-response-error.d.ts +1 -1
  35. package/dist/kv/index.cjs.map +1 -1
  36. package/dist/kv/index.d.cts +4 -4
  37. package/dist/kv/index.d.ts +4 -4
  38. package/dist/kv/index.js.map +1 -1
  39. package/dist/kv/replication/index.d.cts +3 -3
  40. package/dist/kv/replication/index.d.ts +3 -3
  41. package/dist/{scope-1-8skVDk.d.ts → scope-BZr0QwxX.d.ts} +1 -1
  42. package/dist/{scope-Dv3IAXMr.d.cts → scope-DEgc9f-W.d.cts} +1 -1
  43. package/dist/sql/index.cjs +21 -6
  44. package/dist/sql/index.cjs.map +1 -1
  45. package/dist/sql/index.d.cts +6 -236
  46. package/dist/sql/index.d.ts +6 -236
  47. package/dist/sql/index.js +21 -6
  48. package/dist/sql/index.js.map +1 -1
  49. package/dist/telemetry/requestLog.d.cts +1 -1
  50. package/dist/telemetry/requestLog.d.ts +1 -1
  51. package/dist/{types-BGRzS1ob.d.ts → types-Bf91gup6.d.ts} +7 -2
  52. package/dist/{types-DAOyIbXu.d.cts → types-CSd0RYir.d.cts} +1 -1
  53. package/dist/{types-DAOyIbXu.d.ts → types-CSd0RYir.d.ts} +1 -1
  54. package/dist/{types-B_rbS4XE.d.cts → types-D4XbIfFJ.d.cts} +7 -2
  55. package/package.json +6 -1
@@ -0,0 +1,242 @@
1
+ import { f as Result, c as IService } from './types-CSd0RYir.cjs';
2
+
3
+ /**
4
+ * SQL Service Types
5
+ *
6
+ * Type definitions for the SQL service operations.
7
+ */
8
+ /**
9
+ * Configuration for SQLService.
10
+ */
11
+ interface SQLServiceConfig {
12
+ /**
13
+ * Default database name.
14
+ * If not set, operations default to "default".
15
+ */
16
+ defaultDatabase?: string;
17
+ /**
18
+ * Timeout in milliseconds for each SQL request, including reading the
19
+ * response body. When it elapses the request is aborted and the operation
20
+ * returns an `ErrorCodes.TIMEOUT` error. Unset (the default), `0`, or
21
+ * `Infinity` means no timeout.
22
+ */
23
+ timeout?: number;
24
+ /** Allow additional config properties */
25
+ [key: string]: unknown;
26
+ }
27
+ /**
28
+ * Options for SQL query operations.
29
+ */
30
+ interface QueryOptions {
31
+ /**
32
+ * Custom abort signal for this operation.
33
+ */
34
+ signal?: AbortSignal;
35
+ /** Maximum rows the node may return. Nodes without limit support ignore it. */
36
+ maxRows?: number;
37
+ /** Maximum serialized result bytes the node may return. */
38
+ maxBytes?: number;
39
+ /** Redact query failure details before return and ordinary service-error events. */
40
+ redactErrors?: boolean;
41
+ }
42
+ /**
43
+ * Options for SQL execute operations.
44
+ */
45
+ interface ExecuteOptions {
46
+ /**
47
+ * Schema initialization statements (CREATE TABLE IF NOT EXISTS ...).
48
+ * Executed before the main statement on first write.
49
+ */
50
+ schema?: string[];
51
+ /**
52
+ * Custom abort signal for this operation.
53
+ */
54
+ signal?: AbortSignal;
55
+ /** Redact execute failure details before return and ordinary service-error events. */
56
+ redactErrors?: boolean;
57
+ }
58
+ /**
59
+ * Options for SQL batch operations.
60
+ */
61
+ interface BatchOptions {
62
+ /**
63
+ * Custom abort signal for this operation.
64
+ */
65
+ signal?: AbortSignal;
66
+ /** Redact batch failure details before return and ordinary service-error events. */
67
+ redactErrors?: boolean;
68
+ }
69
+ /**
70
+ * A SQL value: null, number, string, or binary data.
71
+ */
72
+ type SqlValue = null | number | string | Uint8Array;
73
+ /**
74
+ * A SQL statement with optional parameters.
75
+ */
76
+ interface SqlStatement {
77
+ sql: string;
78
+ params?: SqlValue[];
79
+ }
80
+ /**
81
+ * Response from SQL query operations.
82
+ */
83
+ interface QueryResponse<T = Record<string, unknown>> {
84
+ columns: string[];
85
+ rows: T[][];
86
+ rowCount: number;
87
+ }
88
+ /**
89
+ * Response from SQL execute operations.
90
+ */
91
+ interface ExecuteResponse {
92
+ changes: number;
93
+ lastInsertRowId: number | null;
94
+ }
95
+ /**
96
+ * Response from SQL batch operations.
97
+ */
98
+ interface BatchResponse {
99
+ results: ExecuteResponse[];
100
+ }
101
+ /**
102
+ * A versioned SQL migration owned by an application or SDK subsystem.
103
+ */
104
+ interface SqlMigration {
105
+ /**
106
+ * Stable migration id within the namespace, e.g. "001_initial".
107
+ */
108
+ id: string;
109
+ /**
110
+ * SQL statements to apply when this migration has not been recorded.
111
+ * Statements should be idempotent where SQLite supports it.
112
+ */
113
+ sql: Array<string | SqlStatement>;
114
+ }
115
+ /**
116
+ * Options for applying SQL migrations.
117
+ */
118
+ interface SqlMigrationApplyOptions {
119
+ /**
120
+ * Namespace for this migration set, usually an app id or SDK subsystem.
121
+ */
122
+ namespace: string;
123
+ /**
124
+ * Ordered migration list.
125
+ */
126
+ migrations: SqlMigration[];
127
+ /**
128
+ * Custom abort signal for migration operations.
129
+ */
130
+ signal?: AbortSignal;
131
+ }
132
+ /**
133
+ * Result from applying SQL migrations.
134
+ */
135
+ interface SqlMigrationApplyResponse {
136
+ database: string;
137
+ namespace: string;
138
+ status: "already_current" | "applied";
139
+ applied: string[];
140
+ skipped: string[];
141
+ }
142
+ /**
143
+ * SQL service action types.
144
+ *
145
+ * URNs derive from the canonical capability registry in `@tinycloud/bootstrap`
146
+ * (TC-112 single source of truth, vendored from tinycloud-node). `insert`,
147
+ * `update`, and `delete` were dropped — zero client refs and never accepted
148
+ * node-side. `select` is a deprecated alias of `read`. `execute`/`export` are
149
+ * request-kind artifacts, not grantable capabilities: the node routes them by
150
+ * request-body kind, so they are absent from the registry but kept here as the
151
+ * invocation ability the client sends today (wire alignment tracked in TC-114).
152
+ */
153
+ declare const SQLAction: {
154
+ readonly READ: "tinycloud.sql/read";
155
+ readonly WRITE: "tinycloud.sql/write";
156
+ readonly SCHEMA: "tinycloud.sql/schema";
157
+ readonly ADMIN: "tinycloud.sql/admin";
158
+ readonly SELECT: "tinycloud.sql/select";
159
+ readonly EXECUTE: "tinycloud.sql/execute";
160
+ readonly EXPORT: "tinycloud.sql/export";
161
+ readonly ALL: "tinycloud.sql/*";
162
+ };
163
+ type SQLActionType = (typeof SQLAction)[keyof typeof SQLAction];
164
+
165
+ /**
166
+ * ISQLService - Interface for SQL service.
167
+ *
168
+ * Platform-agnostic interface for SQL database operations.
169
+ */
170
+
171
+ /**
172
+ * Migration helper bound to a specific database.
173
+ */
174
+ interface ISQLMigrations {
175
+ /**
176
+ * Apply ordered, app-owned migrations and record completed ids.
177
+ */
178
+ apply(options: SqlMigrationApplyOptions): Promise<Result<SqlMigrationApplyResponse>>;
179
+ }
180
+ /**
181
+ * Database handle interface for operations on a specific named database.
182
+ */
183
+ interface IDatabaseHandle {
184
+ /** The database name */
185
+ readonly name: string;
186
+ /** Versioned schema migration helper. */
187
+ readonly migrations: ISQLMigrations;
188
+ /**
189
+ * Execute a SQL query and return rows.
190
+ */
191
+ query<T = Record<string, unknown>>(sql: string, params?: SqlValue[], options?: QueryOptions): Promise<Result<QueryResponse<T>>>;
192
+ /**
193
+ * Execute a SQL statement and return change count.
194
+ */
195
+ execute(sql: string, params?: SqlValue[], options?: ExecuteOptions): Promise<Result<ExecuteResponse>>;
196
+ /**
197
+ * Execute multiple statements in a transaction.
198
+ */
199
+ batch(statements: SqlStatement[], options?: BatchOptions): Promise<Result<BatchResponse>>;
200
+ /**
201
+ * Execute a named prepared statement from delegation caveats.
202
+ */
203
+ executeStatement(name: string, params?: SqlValue[], options?: QueryOptions): Promise<Result<QueryResponse | ExecuteResponse>>;
204
+ /**
205
+ * Export the raw SQLite database file.
206
+ */
207
+ export(options?: QueryOptions): Promise<Result<Blob>>;
208
+ }
209
+ /**
210
+ * SQL service interface.
211
+ *
212
+ * Provides SQL database operations with:
213
+ * - Result type pattern (no throwing)
214
+ * - Named database handles
215
+ * - Configurable timeouts
216
+ * - Abort signal support
217
+ */
218
+ interface ISQLService extends IService {
219
+ /**
220
+ * Get a handle to a named database.
221
+ * @param name - Database name (defaults to "default")
222
+ */
223
+ db(name?: string): IDatabaseHandle;
224
+ /**
225
+ * Shortcut: query the default database.
226
+ */
227
+ query<T = Record<string, unknown>>(sql: string, params?: SqlValue[], options?: QueryOptions): Promise<Result<QueryResponse<T>>>;
228
+ /**
229
+ * Shortcut: execute on the default database.
230
+ */
231
+ execute(sql: string, params?: SqlValue[], options?: ExecuteOptions): Promise<Result<ExecuteResponse>>;
232
+ /**
233
+ * Shortcut: batch on the default database.
234
+ */
235
+ batch(statements: SqlStatement[], options?: BatchOptions): Promise<Result<BatchResponse>>;
236
+ /**
237
+ * Service configuration.
238
+ */
239
+ readonly config: SQLServiceConfig;
240
+ }
241
+
242
+ export { type BatchOptions as B, type ExecuteOptions as E, type IDatabaseHandle as I, type QueryOptions as Q, SQLAction as S, type BatchResponse as a, type ExecuteResponse as b, type ISQLMigrations as c, type ISQLService as d, type QueryResponse as e, type SQLActionType as f, type SQLServiceConfig as g, type SqlMigration as h, type SqlMigrationApplyOptions as i, type SqlMigrationApplyResponse as j, type SqlStatement as k, type SqlValue as l };
@@ -1,4 +1,4 @@
1
- import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as FetchFunction, S as ServiceSession, R as RetryPolicy, T as TelemetryConfig, c as IService, E as EventHandler, d as FetchRequestInit, e as FetchResponse } from './types-DAOyIbXu.cjs';
1
+ import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as FetchFunction, S as ServiceSession, R as RetryPolicy, T as TelemetryConfig, c as IService, E as EventHandler, d as FetchRequestInit, e as FetchResponse } from './types-CSd0RYir.cjs';
2
2
 
3
3
  /**
4
4
  * ServiceContext implementation for TinyCloud SDK Services
package/dist/context.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as FetchFunction, S as ServiceSession, R as RetryPolicy, T as TelemetryConfig, c as IService, E as EventHandler, d as FetchRequestInit, e as FetchResponse } from './types-DAOyIbXu.js';
1
+ import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as FetchFunction, S as ServiceSession, R as RetryPolicy, T as TelemetryConfig, c as IService, E as EventHandler, d as FetchRequestInit, e as FetchResponse } from './types-CSd0RYir.js';
2
2
 
3
3
  /**
4
4
  * ServiceContext implementation for TinyCloud SDK Services
@@ -56,6 +56,7 @@ __export(encryption_exports, {
56
56
  isNetworkId: () => isNetworkId,
57
57
  networkDiscoveryKey: () => networkDiscoveryKey,
58
58
  openWrappedKey: () => openWrappedKey,
59
+ parseEncryptionResource: () => parseEncryptionResource,
59
60
  parseNetworkId: () => parseNetworkId,
60
61
  utf8Decode: () => utf8Decode,
61
62
  utf8Encode: () => utf8Encode,
@@ -201,6 +202,26 @@ function parseNetworkId(networkId) {
201
202
  }
202
203
  return { networkId, ownerDid, name };
203
204
  }
205
+ function parseEncryptionResource(resource) {
206
+ const networkSeparator = resource.lastIndexOf(":");
207
+ const nameAndPath = resource.slice(networkSeparator + 1);
208
+ const pathSeparator = nameAndPath.indexOf("/");
209
+ const name = pathSeparator === -1 ? nameAndPath : nameAndPath.slice(0, pathSeparator);
210
+ const networkId = pathSeparator === -1 ? resource : `${resource.slice(0, networkSeparator + 1)}${name}`;
211
+ const parsed = parseNetworkId(networkId);
212
+ if (pathSeparator === -1) return parsed;
213
+ const keyPath = nameAndPath.slice(pathSeparator + 1);
214
+ const segments = keyPath.split("/");
215
+ const prefix = keyPath.endsWith("/");
216
+ if (keyPath.length === 0 || /[\u0000-\u001f\u007f\\]/.test(keyPath) || segments.some(
217
+ (segment, index) => segment === "." || segment === ".." || segment.length === 0 && !(prefix && index === segments.length - 1)
218
+ )) {
219
+ throw new NetworkIdError(
220
+ `encryption resource has an invalid keyPath (got ${JSON.stringify(resource)})`
221
+ );
222
+ }
223
+ return { ...parsed, keyPath };
224
+ }
204
225
  function buildNetworkId(ownerDid, name) {
205
226
  if (typeof ownerDid !== "string" || !ownerDid.startsWith("did:")) {
206
227
  throw new NetworkIdError("ownerDid must be a DID");
@@ -913,7 +934,12 @@ function ensureNetworkUsableForDecrypt(descriptor) {
913
934
 
914
935
  // src/encryption/envelope.ts
915
936
  function encryptToNetwork(crypto, input) {
916
- parseNetworkId(input.networkId);
937
+ const parsedResource = parseEncryptionResource(
938
+ input.keyPath === void 0 ? input.networkId : `${input.networkId}/${input.keyPath}`
939
+ );
940
+ if (parsedResource.keyPath !== input.keyPath || input.keyPath?.endsWith("/")) {
941
+ throw new NetworkIdError("keyPath must name an exact encryption key");
942
+ }
917
943
  const alg = input.alg ?? DEFAULT_ENCRYPTION_ALG;
918
944
  const keyVersion = input.keyVersion ?? DEFAULT_KEY_VERSION;
919
945
  const symmetricKey = crypto.randomBytes(32);
@@ -927,6 +953,7 @@ function encryptToNetwork(crypto, input) {
927
953
  const envelope = {
928
954
  v: ENVELOPE_VERSION,
929
955
  networkId: input.networkId,
956
+ ...input.keyPath !== void 0 ? { keyPath: input.keyPath } : {},
930
957
  alg,
931
958
  keyVersion,
932
959
  encryptedSymmetricKey,
@@ -958,13 +985,24 @@ function validateEnvelope(crypto, envelope) {
958
985
  };
959
986
  }
960
987
  try {
961
- parseNetworkId(e.networkId);
988
+ if (parseEncryptionResource(e.networkId).keyPath !== void 0) {
989
+ throw new Error("networkId must identify the network root");
990
+ }
991
+ if (e.keyPath !== void 0) {
992
+ if (typeof e.keyPath !== "string") {
993
+ throw new Error("keyPath must be a string");
994
+ }
995
+ const parsed = parseEncryptionResource(`${e.networkId}/${e.keyPath}`);
996
+ if (parsed.keyPath !== e.keyPath || e.keyPath.endsWith("/")) {
997
+ throw new Error("keyPath must name an exact encryption key");
998
+ }
999
+ }
962
1000
  } catch (err2) {
963
1001
  return {
964
1002
  ok: false,
965
1003
  error: encryptionError({
966
1004
  code: "INVALID_ENVELOPE",
967
- message: `envelope.networkId is malformed: ${err2 instanceof Error ? err2.message : String(err2)}`
1005
+ message: `envelope encryption resource is malformed: ${err2 instanceof Error ? err2.message : String(err2)}`
968
1006
  })
969
1007
  };
970
1008
  }
@@ -1037,6 +1075,7 @@ function buildDecryptFacts(input) {
1037
1075
  type: DECRYPT_FACT_TYPE,
1038
1076
  targetNode: input.body.targetNode,
1039
1077
  networkId: input.body.networkId,
1078
+ ...input.body.keyPath !== void 0 ? { keyPath: input.body.keyPath } : {},
1040
1079
  bodyHash,
1041
1080
  encryptedSymmetricKeyHash: input.encryptedSymmetricKeyHash,
1042
1081
  receiverPublicKeyHash,
@@ -1044,10 +1083,14 @@ function buildDecryptFacts(input) {
1044
1083
  keyVersion: input.body.keyVersion
1045
1084
  };
1046
1085
  }
1047
- function buildDecryptAttenuation(networkId) {
1048
- parseNetworkId(networkId);
1086
+ function buildDecryptAttenuation(networkId, keyPath) {
1087
+ const resource = keyPath === void 0 ? networkId : `${networkId}/${keyPath}`;
1088
+ const parsed = parseEncryptionResource(resource);
1089
+ if (parsed.networkId !== networkId || parsed.keyPath !== keyPath || keyPath?.endsWith("/")) {
1090
+ throw new NetworkIdError("keyPath must name an exact encryption key");
1091
+ }
1049
1092
  return {
1050
- [networkId]: {
1093
+ [resource]: {
1051
1094
  [DECRYPT_ACTION]: {}
1052
1095
  }
1053
1096
  };
@@ -1107,6 +1150,15 @@ function checkDecryptInvocationInput(crypto, input) {
1107
1150
  })
1108
1151
  };
1109
1152
  }
1153
+ if (input.facts.keyPath !== input.body.keyPath) {
1154
+ return {
1155
+ ok: false,
1156
+ error: encryptionError({
1157
+ code: "INVALID_INPUT",
1158
+ message: "facts.keyPath must equal body.keyPath"
1159
+ })
1160
+ };
1161
+ }
1110
1162
  if (input.facts.alg !== input.body.alg) {
1111
1163
  return {
1112
1164
  ok: false,
@@ -1144,7 +1196,9 @@ function checkDecryptInvocationInput(crypto, input) {
1144
1196
  };
1145
1197
  }
1146
1198
  try {
1147
- parseNetworkId(input.networkId);
1199
+ if (parseEncryptionResource(input.networkId).keyPath !== void 0) {
1200
+ throw new NetworkIdError("networkId must identify the network root");
1201
+ }
1148
1202
  } catch (err2) {
1149
1203
  return {
1150
1204
  ok: false,
@@ -1154,6 +1208,22 @@ function checkDecryptInvocationInput(crypto, input) {
1154
1208
  })
1155
1209
  };
1156
1210
  }
1211
+ try {
1212
+ const resource = parseEncryptionResource(
1213
+ input.body.keyPath === void 0 ? input.body.networkId : `${input.body.networkId}/${input.body.keyPath}`
1214
+ );
1215
+ if (resource.keyPath !== input.body.keyPath || input.body.keyPath?.endsWith("/")) {
1216
+ throw new NetworkIdError("keyPath must name an exact encryption key");
1217
+ }
1218
+ } catch (err2) {
1219
+ return {
1220
+ ok: false,
1221
+ error: encryptionError({
1222
+ code: "INVALID_INPUT",
1223
+ message: err2 instanceof Error ? err2.message : String(err2)
1224
+ })
1225
+ };
1226
+ }
1157
1227
  const canonicalBody = canonicalize(
1158
1228
  input.body
1159
1229
  );
@@ -1394,6 +1464,7 @@ var EncryptionService = class extends BaseService {
1394
1464
  // that spelling in the envelope so a later response can be compared
1395
1465
  // byte-for-byte (notably for checksummed did:pkh principals).
1396
1466
  networkId: descriptor.networkId,
1467
+ ...options?.keyPath !== void 0 ? { keyPath: options.keyPath } : {},
1397
1468
  networkPublicKey,
1398
1469
  plaintext,
1399
1470
  ...options?.aad !== void 0 ? { aad: options.aad } : {},
@@ -1453,6 +1524,7 @@ var EncryptionService = class extends BaseService {
1453
1524
  type: DECRYPT_FACT_TYPE,
1454
1525
  targetNode,
1455
1526
  networkId: envelope.networkId,
1527
+ ...envelope.keyPath !== void 0 ? { keyPath: envelope.keyPath } : {},
1456
1528
  alg: envelope.alg,
1457
1529
  keyVersion: envelope.keyVersion,
1458
1530
  encryptedSymmetricKey: envelope.encryptedSymmetricKey,
@@ -1597,6 +1669,7 @@ var import_decrypt_transport_response_error3 = require("@tinycloud/sdk-services/
1597
1669
  isNetworkId,
1598
1670
  networkDiscoveryKey,
1599
1671
  openWrappedKey,
1672
+ parseEncryptionResource,
1600
1673
  parseNetworkId,
1601
1674
  utf8Decode,
1602
1675
  utf8Encode,