@cipherstash/stack 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.
Files changed (76) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +670 -0
  4. package/dist/bin/stash.js +5049 -0
  5. package/dist/bin/stash.js.map +1 -0
  6. package/dist/chunk-2GZMIJFO.js +2400 -0
  7. package/dist/chunk-2GZMIJFO.js.map +1 -0
  8. package/dist/chunk-5DCT6YU2.js +138 -0
  9. package/dist/chunk-5DCT6YU2.js.map +1 -0
  10. package/dist/chunk-7XRPN2KX.js +336 -0
  11. package/dist/chunk-7XRPN2KX.js.map +1 -0
  12. package/dist/chunk-SJ7JO4ME.js +28 -0
  13. package/dist/chunk-SJ7JO4ME.js.map +1 -0
  14. package/dist/chunk-SUYMGQBY.js +67 -0
  15. package/dist/chunk-SUYMGQBY.js.map +1 -0
  16. package/dist/client-BxJG56Ey.d.cts +647 -0
  17. package/dist/client-DtGq9dJp.d.ts +647 -0
  18. package/dist/client.cjs +347 -0
  19. package/dist/client.cjs.map +1 -0
  20. package/dist/client.d.cts +7 -0
  21. package/dist/client.d.ts +7 -0
  22. package/dist/client.js +11 -0
  23. package/dist/client.js.map +1 -0
  24. package/dist/drizzle/index.cjs +1528 -0
  25. package/dist/drizzle/index.cjs.map +1 -0
  26. package/dist/drizzle/index.d.cts +350 -0
  27. package/dist/drizzle/index.d.ts +350 -0
  28. package/dist/drizzle/index.js +1212 -0
  29. package/dist/drizzle/index.js.map +1 -0
  30. package/dist/dynamodb/index.cjs +382 -0
  31. package/dist/dynamodb/index.cjs.map +1 -0
  32. package/dist/dynamodb/index.d.cts +125 -0
  33. package/dist/dynamodb/index.d.ts +125 -0
  34. package/dist/dynamodb/index.js +355 -0
  35. package/dist/dynamodb/index.js.map +1 -0
  36. package/dist/identity/index.cjs +271 -0
  37. package/dist/identity/index.cjs.map +1 -0
  38. package/dist/identity/index.d.cts +3 -0
  39. package/dist/identity/index.d.ts +3 -0
  40. package/dist/identity/index.js +117 -0
  41. package/dist/identity/index.js.map +1 -0
  42. package/dist/index-9-Ya3fDK.d.cts +169 -0
  43. package/dist/index-9-Ya3fDK.d.ts +169 -0
  44. package/dist/index.cjs +2915 -0
  45. package/dist/index.cjs.map +1 -0
  46. package/dist/index.d.cts +22 -0
  47. package/dist/index.d.ts +22 -0
  48. package/dist/index.js +23 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/schema/index.cjs +368 -0
  51. package/dist/schema/index.cjs.map +1 -0
  52. package/dist/schema/index.d.cts +4 -0
  53. package/dist/schema/index.d.ts +4 -0
  54. package/dist/schema/index.js +23 -0
  55. package/dist/schema/index.js.map +1 -0
  56. package/dist/secrets/index.cjs +3207 -0
  57. package/dist/secrets/index.cjs.map +1 -0
  58. package/dist/secrets/index.d.cts +227 -0
  59. package/dist/secrets/index.d.ts +227 -0
  60. package/dist/secrets/index.js +323 -0
  61. package/dist/secrets/index.js.map +1 -0
  62. package/dist/supabase/index.cjs +1113 -0
  63. package/dist/supabase/index.cjs.map +1 -0
  64. package/dist/supabase/index.d.cts +144 -0
  65. package/dist/supabase/index.d.ts +144 -0
  66. package/dist/supabase/index.js +864 -0
  67. package/dist/supabase/index.js.map +1 -0
  68. package/dist/types-public-BCj1L4fi.d.cts +1013 -0
  69. package/dist/types-public-BCj1L4fi.d.ts +1013 -0
  70. package/dist/types-public.cjs +40 -0
  71. package/dist/types-public.cjs.map +1 -0
  72. package/dist/types-public.d.cts +4 -0
  73. package/dist/types-public.d.ts +4 -0
  74. package/dist/types-public.js +7 -0
  75. package/dist/types-public.js.map +1 -0
  76. package/package.json +202 -0
@@ -0,0 +1,3207 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/secrets/index.ts
31
+ var secrets_exports = {};
32
+ __export(secrets_exports, {
33
+ Secrets: () => Secrets
34
+ });
35
+ module.exports = __toCommonJS(secrets_exports);
36
+
37
+ // src/encryption/helpers/index.ts
38
+ function encryptedToPgComposite(obj) {
39
+ return {
40
+ data: obj
41
+ };
42
+ }
43
+ function encryptedToCompositeLiteral(obj) {
44
+ if (obj === null) {
45
+ throw new Error("encryptedToCompositeLiteral: obj cannot be null");
46
+ }
47
+ return `(${JSON.stringify(JSON.stringify(obj))})`;
48
+ }
49
+ function encryptedToEscapedCompositeLiteral(obj) {
50
+ if (obj === null) {
51
+ throw new Error("encryptedToEscapedCompositeLiteral: obj cannot be null");
52
+ }
53
+ return JSON.stringify(encryptedToCompositeLiteral(obj));
54
+ }
55
+ function formatEncryptedResult(encrypted, returnType) {
56
+ if (returnType === "composite-literal") {
57
+ return encryptedToCompositeLiteral(encrypted);
58
+ }
59
+ if (returnType === "escaped-composite-literal") {
60
+ return encryptedToEscapedCompositeLiteral(encrypted);
61
+ }
62
+ return encrypted;
63
+ }
64
+ function toFfiKeysetIdentifier(keyset) {
65
+ if (!keyset) return void 0;
66
+ if ("name" in keyset) {
67
+ return { Name: keyset.name };
68
+ }
69
+ return { Uuid: keyset.id };
70
+ }
71
+ function isEncryptedPayload(value) {
72
+ if (value === null) return false;
73
+ if (typeof value !== "object") return false;
74
+ const obj = value;
75
+ if (!("v" in obj) || typeof obj.v !== "number") return false;
76
+ if (!("i" in obj) || typeof obj.i !== "object") return false;
77
+ if (!("c" in obj) && !("sv" in obj)) return false;
78
+ return true;
79
+ }
80
+
81
+ // src/errors/index.ts
82
+ var EncryptionErrorTypes = {
83
+ ClientInitError: "ClientInitError",
84
+ EncryptionError: "EncryptionError",
85
+ DecryptionError: "DecryptionError",
86
+ LockContextError: "LockContextError",
87
+ CtsTokenError: "CtsTokenError"
88
+ };
89
+
90
+ // src/schema/index.ts
91
+ var import_zod = require("zod");
92
+ var castAsEnum = import_zod.z.enum(["bigint", "boolean", "date", "number", "string", "json"]).default("string");
93
+ var tokenFilterSchema = import_zod.z.object({
94
+ kind: import_zod.z.literal("downcase")
95
+ });
96
+ var tokenizerSchema = import_zod.z.union([
97
+ import_zod.z.object({
98
+ kind: import_zod.z.literal("standard")
99
+ }),
100
+ import_zod.z.object({
101
+ kind: import_zod.z.literal("ngram"),
102
+ token_length: import_zod.z.number()
103
+ })
104
+ ]).default({ kind: "ngram", token_length: 3 }).optional();
105
+ var oreIndexOptsSchema = import_zod.z.object({});
106
+ var uniqueIndexOptsSchema = import_zod.z.object({
107
+ token_filters: import_zod.z.array(tokenFilterSchema).default([]).optional()
108
+ });
109
+ var matchIndexOptsSchema = import_zod.z.object({
110
+ tokenizer: tokenizerSchema,
111
+ token_filters: import_zod.z.array(tokenFilterSchema).default([]).optional(),
112
+ k: import_zod.z.number().default(6).optional(),
113
+ m: import_zod.z.number().default(2048).optional(),
114
+ include_original: import_zod.z.boolean().default(false).optional()
115
+ });
116
+ var steVecIndexOptsSchema = import_zod.z.object({
117
+ prefix: import_zod.z.string()
118
+ });
119
+ var indexesSchema = import_zod.z.object({
120
+ ore: oreIndexOptsSchema.optional(),
121
+ unique: uniqueIndexOptsSchema.optional(),
122
+ match: matchIndexOptsSchema.optional(),
123
+ ste_vec: steVecIndexOptsSchema.optional()
124
+ }).default({});
125
+ var columnSchema = import_zod.z.object({
126
+ cast_as: castAsEnum,
127
+ indexes: indexesSchema
128
+ }).default({});
129
+ var tableSchema = import_zod.z.record(columnSchema).default({});
130
+ var tablesSchema = import_zod.z.record(tableSchema).default({});
131
+ var encryptConfigSchema = import_zod.z.object({
132
+ v: import_zod.z.number(),
133
+ tables: tablesSchema
134
+ });
135
+ var ProtectValue = class {
136
+ valueName;
137
+ castAsValue;
138
+ constructor(valueName) {
139
+ this.valueName = valueName;
140
+ this.castAsValue = "string";
141
+ }
142
+ /**
143
+ * Set or override the plaintext data type for this value.
144
+ *
145
+ * By default all values are treated as `'string'`. Use this method to specify
146
+ * a different type so the encryption layer knows how to encode the plaintext
147
+ * before encrypting.
148
+ *
149
+ * @param castAs - The plaintext data type: `'string'`, `'number'`, `'boolean'`, `'date'`, `'bigint'`, or `'json'`.
150
+ * @returns This `ProtectValue` instance for method chaining.
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * import { encryptedValue } from "@cipherstash/stack/schema"
155
+ *
156
+ * const age = encryptedValue("age").dataType("number")
157
+ * ```
158
+ */
159
+ dataType(castAs) {
160
+ this.castAsValue = castAs;
161
+ return this;
162
+ }
163
+ build() {
164
+ return {
165
+ cast_as: this.castAsValue,
166
+ indexes: {}
167
+ };
168
+ }
169
+ getName() {
170
+ return this.valueName;
171
+ }
172
+ };
173
+ var ProtectColumn = class {
174
+ columnName;
175
+ castAsValue;
176
+ indexesValue = {};
177
+ constructor(columnName) {
178
+ this.columnName = columnName;
179
+ this.castAsValue = "string";
180
+ }
181
+ /**
182
+ * Set or override the plaintext data type for this column.
183
+ *
184
+ * By default all columns are treated as `'string'`. Use this method to specify
185
+ * a different type so the encryption layer knows how to encode the plaintext
186
+ * before encrypting.
187
+ *
188
+ * @param castAs - The plaintext data type: `'string'`, `'number'`, `'boolean'`, `'date'`, `'bigint'`, or `'json'`.
189
+ * @returns This `ProtectColumn` instance for method chaining.
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * import { encryptedColumn } from "@cipherstash/stack/schema"
194
+ *
195
+ * const dateOfBirth = encryptedColumn("date_of_birth").dataType("date")
196
+ * ```
197
+ */
198
+ dataType(castAs) {
199
+ this.castAsValue = castAs;
200
+ return this;
201
+ }
202
+ /**
203
+ * Enable Order-Revealing Encryption (ORE) indexing on this column.
204
+ *
205
+ * ORE allows sorting, comparison, and range queries on encrypted data.
206
+ * Use with `encryptQuery` and `queryType: 'orderAndRange'`.
207
+ *
208
+ * @returns This `ProtectColumn` instance for method chaining.
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * import { encryptedTable, encryptedColumn } from "@cipherstash/stack/schema"
213
+ *
214
+ * const users = encryptedTable("users", {
215
+ * email: encryptedColumn("email").orderAndRange(),
216
+ * })
217
+ * ```
218
+ */
219
+ orderAndRange() {
220
+ this.indexesValue.ore = {};
221
+ return this;
222
+ }
223
+ /**
224
+ * Enable an exact-match (unique) index on this column.
225
+ *
226
+ * Allows equality queries on encrypted data. Use with `encryptQuery`
227
+ * and `queryType: 'equality'`.
228
+ *
229
+ * @param tokenFilters - Optional array of token filters (e.g. `[{ kind: 'downcase' }]`).
230
+ * When omitted, no token filters are applied.
231
+ * @returns This `ProtectColumn` instance for method chaining.
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * import { encryptedTable, encryptedColumn } from "@cipherstash/stack/schema"
236
+ *
237
+ * const users = encryptedTable("users", {
238
+ * email: encryptedColumn("email").equality(),
239
+ * })
240
+ * ```
241
+ */
242
+ equality(tokenFilters) {
243
+ this.indexesValue.unique = {
244
+ token_filters: tokenFilters ?? []
245
+ };
246
+ return this;
247
+ }
248
+ /**
249
+ * Enable a full-text / fuzzy search (match) index on this column.
250
+ *
251
+ * Uses n-gram tokenization by default for substring and fuzzy matching.
252
+ * Use with `encryptQuery` and `queryType: 'freeTextSearch'`.
253
+ *
254
+ * @param opts - Optional match index configuration. Defaults to 3-character ngram
255
+ * tokenization with a downcase filter, `k=6`, `m=2048`, and `include_original=true`.
256
+ * @returns This `ProtectColumn` instance for method chaining.
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * import { encryptedTable, encryptedColumn } from "@cipherstash/stack/schema"
261
+ *
262
+ * const users = encryptedTable("users", {
263
+ * email: encryptedColumn("email").freeTextSearch(),
264
+ * })
265
+ *
266
+ * // With custom options
267
+ * const posts = encryptedTable("posts", {
268
+ * body: encryptedColumn("body").freeTextSearch({
269
+ * tokenizer: { kind: "ngram", token_length: 4 },
270
+ * k: 8,
271
+ * m: 4096,
272
+ * }),
273
+ * })
274
+ * ```
275
+ */
276
+ freeTextSearch(opts) {
277
+ this.indexesValue.match = {
278
+ tokenizer: opts?.tokenizer ?? { kind: "ngram", token_length: 3 },
279
+ token_filters: opts?.token_filters ?? [
280
+ {
281
+ kind: "downcase"
282
+ }
283
+ ],
284
+ k: opts?.k ?? 6,
285
+ m: opts?.m ?? 2048,
286
+ include_original: opts?.include_original ?? true
287
+ };
288
+ return this;
289
+ }
290
+ /**
291
+ * Configure this column for searchable encrypted JSON (STE-Vec).
292
+ *
293
+ * Enables encrypted JSONPath selector queries (e.g. `'$.user.email'`) and
294
+ * containment queries (e.g. `{ role: 'admin' }`). Automatically sets the
295
+ * data type to `'json'`.
296
+ *
297
+ * When used with `encryptQuery`, the query operation is auto-inferred from
298
+ * the plaintext type: strings become selector queries, objects/arrays become
299
+ * containment queries.
300
+ *
301
+ * @returns This `ProtectColumn` instance for method chaining.
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * import { encryptedTable, encryptedColumn } from "@cipherstash/stack/schema"
306
+ *
307
+ * const documents = encryptedTable("documents", {
308
+ * metadata: encryptedColumn("metadata").searchableJson(),
309
+ * })
310
+ * ```
311
+ */
312
+ searchableJson() {
313
+ this.castAsValue = "json";
314
+ this.indexesValue.ste_vec = { prefix: "enabled" };
315
+ return this;
316
+ }
317
+ build() {
318
+ return {
319
+ cast_as: this.castAsValue,
320
+ indexes: this.indexesValue
321
+ };
322
+ }
323
+ getName() {
324
+ return this.columnName;
325
+ }
326
+ };
327
+ var ProtectTable = class {
328
+ constructor(tableName, columnBuilders) {
329
+ this.tableName = tableName;
330
+ this.columnBuilders = columnBuilders;
331
+ }
332
+ /**
333
+ * Compile this table schema into a `TableDefinition` used internally by the encryption client.
334
+ *
335
+ * Iterates over all column builders, calls `.build()` on each, and assembles
336
+ * the final `{ tableName, columns }` structure. For `searchableJson()` columns,
337
+ * the STE-Vec prefix is automatically set to `"<tableName>/<columnName>"`.
338
+ *
339
+ * @returns A `TableDefinition` containing the table name and built column configs.
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * const users = encryptedTable("users", {
344
+ * email: encryptedColumn("email").equality(),
345
+ * })
346
+ *
347
+ * const definition = users.build()
348
+ * // { tableName: "users", columns: { email: { cast_as: "string", indexes: { unique: ... } } } }
349
+ * ```
350
+ */
351
+ build() {
352
+ const builtColumns = {};
353
+ const processColumn = (builder, colName) => {
354
+ if (builder instanceof ProtectColumn) {
355
+ const builtColumn = builder.build();
356
+ if (builtColumn.cast_as === "json" && builtColumn.indexes.ste_vec?.prefix === "enabled") {
357
+ builtColumns[colName] = {
358
+ ...builtColumn,
359
+ indexes: {
360
+ ...builtColumn.indexes,
361
+ ste_vec: {
362
+ prefix: `${this.tableName}/${colName}`
363
+ }
364
+ }
365
+ };
366
+ } else {
367
+ builtColumns[colName] = builtColumn;
368
+ }
369
+ } else {
370
+ for (const [key, value] of Object.entries(builder)) {
371
+ if (value instanceof ProtectValue) {
372
+ builtColumns[value.getName()] = value.build();
373
+ } else {
374
+ processColumn(value, key);
375
+ }
376
+ }
377
+ }
378
+ };
379
+ for (const [colName, builder] of Object.entries(this.columnBuilders)) {
380
+ processColumn(builder, colName);
381
+ }
382
+ return {
383
+ tableName: this.tableName,
384
+ columns: builtColumns
385
+ };
386
+ }
387
+ };
388
+ function encryptedTable(tableName, columns) {
389
+ const tableBuilder = new ProtectTable(tableName, columns);
390
+ for (const [colName, colBuilder] of Object.entries(columns)) {
391
+ ;
392
+ tableBuilder[colName] = colBuilder;
393
+ }
394
+ return tableBuilder;
395
+ }
396
+ function encryptedColumn(columnName) {
397
+ return new ProtectColumn(columnName);
398
+ }
399
+ function buildEncryptConfig(...protectTables) {
400
+ const config = {
401
+ v: 2,
402
+ tables: {}
403
+ };
404
+ for (const tb of protectTables) {
405
+ const tableDef = tb.build();
406
+ config.tables[tableDef.tableName] = tableDef.columns;
407
+ }
408
+ return config;
409
+ }
410
+
411
+ // src/utils/config/index.ts
412
+ var import_node_fs = __toESM(require("fs"), 1);
413
+ var import_node_path = __toESM(require("path"), 1);
414
+ function getWorkspaceCrn(tomlString) {
415
+ let currentSection = "";
416
+ let workspaceCrn;
417
+ const lines = tomlString.split(/\r?\n/);
418
+ for (const line of lines) {
419
+ const trimmedLine = line.trim();
420
+ if (!trimmedLine || trimmedLine.startsWith("#")) {
421
+ continue;
422
+ }
423
+ const sectionMatch = trimmedLine.match(/^\[([^\]]+)\]$/);
424
+ if (sectionMatch) {
425
+ currentSection = sectionMatch[1];
426
+ continue;
427
+ }
428
+ const kvMatch = trimmedLine.match(/^(\w+)\s*=\s*"([^"]+)"$/);
429
+ if (kvMatch) {
430
+ const [_, key, value] = kvMatch;
431
+ if (currentSection === "auth" && key === "workspace_crn") {
432
+ workspaceCrn = value;
433
+ break;
434
+ }
435
+ }
436
+ }
437
+ return workspaceCrn;
438
+ }
439
+ function extractWorkspaceIdFromCrn(crn) {
440
+ const match = crn.match(/crn:[^:]+:([^:]+)$/);
441
+ if (!match) {
442
+ throw new Error("Invalid CRN format");
443
+ }
444
+ return match[1];
445
+ }
446
+ function loadWorkSpaceId(suppliedCrn) {
447
+ const configPath = import_node_path.default.join(process.cwd(), "cipherstash.toml");
448
+ if (suppliedCrn) {
449
+ return extractWorkspaceIdFromCrn(suppliedCrn);
450
+ }
451
+ if (!import_node_fs.default.existsSync(configPath) && !process.env.CS_WORKSPACE_CRN) {
452
+ throw new Error(
453
+ "You have not defined a workspace CRN in your config file, or the CS_WORKSPACE_CRN environment variable."
454
+ );
455
+ }
456
+ if (process.env.CS_WORKSPACE_CRN) {
457
+ return extractWorkspaceIdFromCrn(process.env.CS_WORKSPACE_CRN);
458
+ }
459
+ if (!import_node_fs.default.existsSync(configPath)) {
460
+ throw new Error(
461
+ "You have not defined a workspace CRN in your config file, or the CS_WORKSPACE_CRN environment variable."
462
+ );
463
+ }
464
+ const tomlString = import_node_fs.default.readFileSync(configPath, "utf8");
465
+ const workspaceCrn = getWorkspaceCrn(tomlString);
466
+ if (!workspaceCrn) {
467
+ throw new Error(
468
+ "You have not defined a workspace CRN in your config file, or the CS_WORKSPACE_CRN environment variable."
469
+ );
470
+ }
471
+ return extractWorkspaceIdFromCrn(workspaceCrn);
472
+ }
473
+
474
+ // src/utils/logger/index.ts
475
+ var import_evlog = require("evlog");
476
+ function samplingFromEnv() {
477
+ const env = process.env.STASH_LOG_LEVEL;
478
+ if (!env) return void 0;
479
+ const levels = ["debug", "info", "warn", "error"];
480
+ const idx = levels.indexOf(env);
481
+ if (idx === -1) return void 0;
482
+ return Object.fromEntries(levels.map((l, i) => [l, i >= idx ? 100 : 0]));
483
+ }
484
+ var initialized = false;
485
+ function initStackLogger(config) {
486
+ if (initialized) return;
487
+ initialized = true;
488
+ const rates = samplingFromEnv();
489
+ (0, import_evlog.initLogger)({
490
+ env: { service: "@cipherstash/stack" },
491
+ enabled: config?.enabled ?? true,
492
+ pretty: config?.pretty,
493
+ ...rates && { sampling: { rates } },
494
+ ...config?.drain && { drain: config.drain }
495
+ });
496
+ }
497
+ initStackLogger();
498
+ function safeMessage(args) {
499
+ return typeof args[0] === "string" ? args[0] : "";
500
+ }
501
+ var logger = {
502
+ debug(...args) {
503
+ const log = (0, import_evlog.createRequestLogger)();
504
+ log.set({ level: "debug", source: "@cipherstash/stack", message: safeMessage(args) });
505
+ log.emit();
506
+ },
507
+ info(...args) {
508
+ const log = (0, import_evlog.createRequestLogger)();
509
+ log.set({ source: "@cipherstash/stack" });
510
+ log.info(safeMessage(args));
511
+ log.emit();
512
+ },
513
+ warn(...args) {
514
+ const log = (0, import_evlog.createRequestLogger)();
515
+ log.warn(safeMessage(args));
516
+ log.emit();
517
+ },
518
+ error(...args) {
519
+ const log = (0, import_evlog.createRequestLogger)();
520
+ log.error(safeMessage(args));
521
+ log.emit();
522
+ }
523
+ };
524
+
525
+ // src/encryption/ffi/index.ts
526
+ var import_result11 = require("@byteslice/result");
527
+ var import_protect_ffi9 = require("@cipherstash/protect-ffi");
528
+
529
+ // src/encryption/ffi/helpers/type-guards.ts
530
+ function isScalarQueryTermArray(value) {
531
+ return Array.isArray(value) && value.length > 0 && typeof value[0] === "object" && value[0] !== null && "column" in value[0] && "table" in value[0];
532
+ }
533
+
534
+ // src/encryption/ffi/helpers/error-code.ts
535
+ var import_protect_ffi = require("@cipherstash/protect-ffi");
536
+ function getErrorCode(error) {
537
+ return error instanceof import_protect_ffi.ProtectError ? error.code : void 0;
538
+ }
539
+
540
+ // src/encryption/ffi/operations/batch-encrypt-query.ts
541
+ var import_result = require("@byteslice/result");
542
+ var import_protect_ffi2 = require("@cipherstash/protect-ffi");
543
+
544
+ // src/types.ts
545
+ var queryTypeToFfi = {
546
+ orderAndRange: "ore",
547
+ freeTextSearch: "match",
548
+ equality: "unique",
549
+ steVecSelector: "ste_vec",
550
+ steVecTerm: "ste_vec",
551
+ searchableJson: "ste_vec"
552
+ };
553
+ var queryTypeToQueryOp = {
554
+ steVecSelector: "ste_vec_selector",
555
+ steVecTerm: "ste_vec_term"
556
+ };
557
+
558
+ // src/encryption/ffi/helpers/infer-index-type.ts
559
+ function inferIndexType(column) {
560
+ const config = column.build();
561
+ const indexes = config.indexes;
562
+ if (!indexes || Object.keys(indexes).length === 0) {
563
+ throw new Error(`Column "${column.getName()}" has no indexes configured`);
564
+ }
565
+ if (indexes.unique) return "unique";
566
+ if (indexes.match) return "match";
567
+ if (indexes.ore) return "ore";
568
+ if (indexes.ste_vec) return "ste_vec";
569
+ throw new Error(
570
+ `Column "${column.getName()}" has no suitable index for queries`
571
+ );
572
+ }
573
+ function inferQueryOpFromPlaintext(plaintext) {
574
+ if (typeof plaintext === "string") {
575
+ return "ste_vec_selector";
576
+ }
577
+ if (typeof plaintext === "object" || typeof plaintext === "number" || typeof plaintext === "boolean" || typeof plaintext === "bigint") {
578
+ return "ste_vec_term";
579
+ }
580
+ return "ste_vec_term";
581
+ }
582
+ function validateIndexType(column, indexType) {
583
+ const config = column.build();
584
+ const indexes = config.indexes ?? {};
585
+ const indexMap = {
586
+ unique: !!indexes.unique,
587
+ match: !!indexes.match,
588
+ ore: !!indexes.ore,
589
+ ste_vec: !!indexes.ste_vec
590
+ };
591
+ if (!indexMap[indexType]) {
592
+ throw new Error(
593
+ `Index type "${indexType}" is not configured on column "${column.getName()}"`
594
+ );
595
+ }
596
+ }
597
+ function resolveIndexType(column, queryType, plaintext) {
598
+ const indexType = queryType ? queryTypeToFfi[queryType] : inferIndexType(column);
599
+ if (queryType) {
600
+ validateIndexType(column, indexType);
601
+ if (queryType === "searchableJson") {
602
+ if (plaintext === void 0 || plaintext === null) {
603
+ return { indexType };
604
+ }
605
+ return { indexType, queryOp: inferQueryOpFromPlaintext(plaintext) };
606
+ }
607
+ return { indexType, queryOp: queryTypeToQueryOp[queryType] };
608
+ }
609
+ if (indexType === "ste_vec") {
610
+ if (plaintext === void 0 || plaintext === null) {
611
+ return { indexType };
612
+ }
613
+ return { indexType, queryOp: inferQueryOpFromPlaintext(plaintext) };
614
+ }
615
+ return { indexType };
616
+ }
617
+
618
+ // src/encryption/ffi/helpers/validation.ts
619
+ function validateNumericValue(value) {
620
+ if (typeof value === "number" && Number.isNaN(value)) {
621
+ return {
622
+ failure: {
623
+ type: EncryptionErrorTypes.EncryptionError,
624
+ message: "[encryption]: Cannot encrypt NaN value"
625
+ }
626
+ };
627
+ }
628
+ if (typeof value === "number" && !Number.isFinite(value)) {
629
+ return {
630
+ failure: {
631
+ type: EncryptionErrorTypes.EncryptionError,
632
+ message: "[encryption]: Cannot encrypt Infinity value"
633
+ }
634
+ };
635
+ }
636
+ return void 0;
637
+ }
638
+ function assertValidNumericValue(value) {
639
+ if (typeof value === "number" && Number.isNaN(value)) {
640
+ throw new Error("[encryption]: Cannot encrypt NaN value");
641
+ }
642
+ if (typeof value === "number" && !Number.isFinite(value)) {
643
+ throw new Error("[encryption]: Cannot encrypt Infinity value");
644
+ }
645
+ }
646
+ function assertValueIndexCompatibility(value, indexType, columnName) {
647
+ if (typeof value === "number" && indexType === "match") {
648
+ throw new Error(
649
+ `[encryption]: Cannot use 'match' index with numeric value on column "${columnName}". The 'freeTextSearch' index only supports string values. Configure the column with 'orderAndRange()' or 'equality()' for numeric queries.`
650
+ );
651
+ }
652
+ }
653
+
654
+ // src/encryption/ffi/operations/base-operation.ts
655
+ var EncryptionOperation = class {
656
+ auditMetadata;
657
+ /**
658
+ * Attach audit metadata to this operation. Can be chained.
659
+ * @param config Configuration for ZeroKMS audit logging
660
+ * @param config.metadata Arbitrary JSON object for appending metadata to the audit log
661
+ */
662
+ audit(config) {
663
+ this.auditMetadata = config.metadata;
664
+ return this;
665
+ }
666
+ /**
667
+ * Get the audit data for this operation.
668
+ */
669
+ getAuditData() {
670
+ return {
671
+ metadata: this.auditMetadata
672
+ };
673
+ }
674
+ /**
675
+ * Make the operation thenable
676
+ */
677
+ then(onfulfilled, onrejected) {
678
+ return this.execute().then(onfulfilled, onrejected);
679
+ }
680
+ };
681
+
682
+ // src/encryption/ffi/operations/batch-encrypt-query.ts
683
+ function filterNullTerms(terms) {
684
+ const nullIndices = /* @__PURE__ */ new Set();
685
+ const nonNullTerms = [];
686
+ terms.forEach((term, index) => {
687
+ if (term.value === null || term.value === void 0) {
688
+ nullIndices.add(index);
689
+ } else {
690
+ nonNullTerms.push({ term, originalIndex: index });
691
+ }
692
+ });
693
+ return { nullIndices, nonNullTerms };
694
+ }
695
+ function buildQueryPayload(term, lockContext) {
696
+ assertValidNumericValue(term.value);
697
+ const { indexType, queryOp } = resolveIndexType(
698
+ term.column,
699
+ term.queryType,
700
+ term.value
701
+ );
702
+ assertValueIndexCompatibility(term.value, indexType, term.column.getName());
703
+ const payload = {
704
+ plaintext: term.value,
705
+ column: term.column.getName(),
706
+ table: term.table.tableName,
707
+ indexType,
708
+ queryOp
709
+ };
710
+ if (lockContext != null) {
711
+ payload.lockContext = lockContext;
712
+ }
713
+ return payload;
714
+ }
715
+ function assembleResults(totalLength, encryptedValues, nonNullTerms) {
716
+ const results = new Array(totalLength).fill(null);
717
+ nonNullTerms.forEach(({ term, originalIndex }, i) => {
718
+ const encrypted = encryptedValues[i];
719
+ results[originalIndex] = formatEncryptedResult(encrypted, term.returnType);
720
+ });
721
+ return results;
722
+ }
723
+ var BatchEncryptQueryOperation = class extends EncryptionOperation {
724
+ constructor(client, terms) {
725
+ super();
726
+ this.client = client;
727
+ this.terms = terms;
728
+ }
729
+ withLockContext(lockContext) {
730
+ return new BatchEncryptQueryOperationWithLockContext(
731
+ this.client,
732
+ this.terms,
733
+ lockContext,
734
+ this.auditMetadata
735
+ );
736
+ }
737
+ async execute() {
738
+ const log = (0, import_evlog.createRequestLogger)();
739
+ log.set({
740
+ op: "batchEncryptQuery",
741
+ count: this.terms.length,
742
+ lockContext: false
743
+ });
744
+ if (this.terms.length === 0) {
745
+ log.emit();
746
+ return { data: [] };
747
+ }
748
+ const { nullIndices, nonNullTerms } = filterNullTerms(this.terms);
749
+ if (nonNullTerms.length === 0) {
750
+ log.emit();
751
+ return { data: this.terms.map(() => null) };
752
+ }
753
+ const result = await (0, import_result.withResult)(
754
+ async () => {
755
+ if (!this.client) throw noClientError();
756
+ const { metadata } = this.getAuditData();
757
+ const queries = nonNullTerms.map(
758
+ ({ term }) => buildQueryPayload(term)
759
+ );
760
+ const encrypted = await (0, import_protect_ffi2.encryptQueryBulk)(this.client, {
761
+ queries,
762
+ unverifiedContext: metadata
763
+ });
764
+ return assembleResults(this.terms.length, encrypted, nonNullTerms);
765
+ },
766
+ (error) => {
767
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
768
+ return {
769
+ type: EncryptionErrorTypes.EncryptionError,
770
+ message: error.message,
771
+ code: getErrorCode(error)
772
+ };
773
+ }
774
+ );
775
+ log.emit();
776
+ return result;
777
+ }
778
+ };
779
+ var BatchEncryptQueryOperationWithLockContext = class extends EncryptionOperation {
780
+ constructor(client, terms, lockContext, auditMetadata) {
781
+ super();
782
+ this.client = client;
783
+ this.terms = terms;
784
+ this.lockContext = lockContext;
785
+ this.auditMetadata = auditMetadata;
786
+ }
787
+ async execute() {
788
+ const log = (0, import_evlog.createRequestLogger)();
789
+ log.set({
790
+ op: "batchEncryptQuery",
791
+ count: this.terms.length,
792
+ lockContext: true
793
+ });
794
+ if (this.terms.length === 0) {
795
+ log.emit();
796
+ return { data: [] };
797
+ }
798
+ const { nullIndices, nonNullTerms } = filterNullTerms(this.terms);
799
+ if (nonNullTerms.length === 0) {
800
+ log.emit();
801
+ return { data: this.terms.map(() => null) };
802
+ }
803
+ const lockContextResult = await this.lockContext.getLockContext();
804
+ if (lockContextResult.failure) {
805
+ log.emit();
806
+ return { failure: lockContextResult.failure };
807
+ }
808
+ const { ctsToken, context } = lockContextResult.data;
809
+ const result = await (0, import_result.withResult)(
810
+ async () => {
811
+ if (!this.client) throw noClientError();
812
+ const { metadata } = this.getAuditData();
813
+ const queries = nonNullTerms.map(
814
+ ({ term }) => buildQueryPayload(term, context)
815
+ );
816
+ const encrypted = await (0, import_protect_ffi2.encryptQueryBulk)(this.client, {
817
+ queries,
818
+ serviceToken: ctsToken,
819
+ unverifiedContext: metadata
820
+ });
821
+ return assembleResults(this.terms.length, encrypted, nonNullTerms);
822
+ },
823
+ (error) => {
824
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
825
+ return {
826
+ type: EncryptionErrorTypes.EncryptionError,
827
+ message: error.message,
828
+ code: getErrorCode(error)
829
+ };
830
+ }
831
+ );
832
+ log.emit();
833
+ return result;
834
+ }
835
+ };
836
+
837
+ // src/encryption/ffi/operations/bulk-decrypt.ts
838
+ var import_result2 = require("@byteslice/result");
839
+ var import_protect_ffi3 = require("@cipherstash/protect-ffi");
840
+ var createDecryptPayloads = (encryptedPayloads, lockContext) => {
841
+ return encryptedPayloads.map((item, index) => ({ ...item, originalIndex: index })).filter(({ data }) => data !== null).map(({ id, data, originalIndex }) => ({
842
+ id,
843
+ ciphertext: data,
844
+ originalIndex,
845
+ ...lockContext && { lockContext }
846
+ }));
847
+ };
848
+ var createNullResult = (encryptedPayloads) => {
849
+ return encryptedPayloads.map(({ id }) => ({
850
+ id,
851
+ data: null
852
+ }));
853
+ };
854
+ var mapDecryptedDataToResult = (encryptedPayloads, decryptedData) => {
855
+ const result = new Array(encryptedPayloads.length);
856
+ let decryptedIndex = 0;
857
+ for (let i = 0; i < encryptedPayloads.length; i++) {
858
+ if (encryptedPayloads[i].data === null) {
859
+ result[i] = { id: encryptedPayloads[i].id, data: null };
860
+ } else {
861
+ const decryptResult = decryptedData[decryptedIndex];
862
+ if ("error" in decryptResult) {
863
+ result[i] = {
864
+ id: encryptedPayloads[i].id,
865
+ error: decryptResult.error
866
+ };
867
+ } else {
868
+ result[i] = {
869
+ id: encryptedPayloads[i].id,
870
+ data: decryptResult.data
871
+ };
872
+ }
873
+ decryptedIndex++;
874
+ }
875
+ }
876
+ return result;
877
+ };
878
+ var BulkDecryptOperation = class extends EncryptionOperation {
879
+ client;
880
+ encryptedPayloads;
881
+ constructor(client, encryptedPayloads) {
882
+ super();
883
+ this.client = client;
884
+ this.encryptedPayloads = encryptedPayloads;
885
+ }
886
+ withLockContext(lockContext) {
887
+ return new BulkDecryptOperationWithLockContext(this, lockContext);
888
+ }
889
+ async execute() {
890
+ const log = (0, import_evlog.createRequestLogger)();
891
+ log.set({
892
+ op: "bulkDecrypt",
893
+ count: this.encryptedPayloads?.length ?? 0,
894
+ lockContext: false
895
+ });
896
+ const result = await (0, import_result2.withResult)(
897
+ async () => {
898
+ if (!this.client) throw noClientError();
899
+ if (!this.encryptedPayloads || this.encryptedPayloads.length === 0)
900
+ return [];
901
+ const nonNullPayloads = createDecryptPayloads(this.encryptedPayloads);
902
+ if (nonNullPayloads.length === 0) {
903
+ return createNullResult(this.encryptedPayloads);
904
+ }
905
+ const { metadata } = this.getAuditData();
906
+ const decryptedData = await (0, import_protect_ffi3.decryptBulkFallible)(this.client, {
907
+ ciphertexts: nonNullPayloads,
908
+ unverifiedContext: metadata
909
+ });
910
+ return mapDecryptedDataToResult(this.encryptedPayloads, decryptedData);
911
+ },
912
+ (error) => {
913
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
914
+ return {
915
+ type: EncryptionErrorTypes.DecryptionError,
916
+ message: error.message,
917
+ code: getErrorCode(error)
918
+ };
919
+ }
920
+ );
921
+ log.emit();
922
+ return result;
923
+ }
924
+ getOperation() {
925
+ return {
926
+ client: this.client,
927
+ encryptedPayloads: this.encryptedPayloads
928
+ };
929
+ }
930
+ };
931
+ var BulkDecryptOperationWithLockContext = class extends EncryptionOperation {
932
+ operation;
933
+ lockContext;
934
+ constructor(operation, lockContext) {
935
+ super();
936
+ this.operation = operation;
937
+ this.lockContext = lockContext;
938
+ const auditData = operation.getAuditData();
939
+ if (auditData) {
940
+ this.audit(auditData);
941
+ }
942
+ }
943
+ async execute() {
944
+ const { client, encryptedPayloads } = this.operation.getOperation();
945
+ const log = (0, import_evlog.createRequestLogger)();
946
+ log.set({
947
+ op: "bulkDecrypt",
948
+ count: encryptedPayloads?.length ?? 0,
949
+ lockContext: true
950
+ });
951
+ const result = await (0, import_result2.withResult)(
952
+ async () => {
953
+ if (!client) throw noClientError();
954
+ if (!encryptedPayloads || encryptedPayloads.length === 0) return [];
955
+ const context = await this.lockContext.getLockContext();
956
+ if (context.failure) {
957
+ throw new Error(`[encryption]: ${context.failure.message}`);
958
+ }
959
+ const nonNullPayloads = createDecryptPayloads(
960
+ encryptedPayloads,
961
+ context.data.context
962
+ );
963
+ if (nonNullPayloads.length === 0) {
964
+ return createNullResult(encryptedPayloads);
965
+ }
966
+ const { metadata } = this.getAuditData();
967
+ const decryptedData = await (0, import_protect_ffi3.decryptBulkFallible)(client, {
968
+ ciphertexts: nonNullPayloads,
969
+ serviceToken: context.data.ctsToken,
970
+ unverifiedContext: metadata
971
+ });
972
+ return mapDecryptedDataToResult(encryptedPayloads, decryptedData);
973
+ },
974
+ (error) => {
975
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
976
+ return {
977
+ type: EncryptionErrorTypes.DecryptionError,
978
+ message: error.message,
979
+ code: getErrorCode(error)
980
+ };
981
+ }
982
+ );
983
+ log.emit();
984
+ return result;
985
+ }
986
+ };
987
+
988
+ // src/encryption/ffi/operations/bulk-decrypt-models.ts
989
+ var import_result3 = require("@byteslice/result");
990
+
991
+ // src/encryption/ffi/model-helpers.ts
992
+ var import_protect_ffi4 = require("@cipherstash/protect-ffi");
993
+ function setNestedValue(obj, path2, value) {
994
+ const FORBIDDEN_KEYS = ["__proto__", "prototype", "constructor"];
995
+ let current = obj;
996
+ for (let i = 0; i < path2.length - 1; i++) {
997
+ const part = path2[i];
998
+ if (FORBIDDEN_KEYS.includes(part)) {
999
+ throw new Error(`[encryption]: Forbidden key "${part}" in field path`);
1000
+ }
1001
+ if (!(part in current) || typeof current[part] !== "object" || current[part] === null) {
1002
+ current[part] = {};
1003
+ }
1004
+ current = current[part];
1005
+ }
1006
+ const lastKey = path2[path2.length - 1];
1007
+ if (FORBIDDEN_KEYS.includes(lastKey)) {
1008
+ throw new Error(`[encryption]: Forbidden key "${lastKey}" in field path`);
1009
+ }
1010
+ current[lastKey] = value;
1011
+ }
1012
+ async function handleSingleModelBulkOperation(items, operation, keyMap) {
1013
+ if (items.length === 0) {
1014
+ return {};
1015
+ }
1016
+ const results = await operation(items);
1017
+ const mappedResults = {};
1018
+ results.forEach((result, index) => {
1019
+ const originalKey = keyMap[index.toString()];
1020
+ mappedResults[originalKey] = result;
1021
+ });
1022
+ return mappedResults;
1023
+ }
1024
+ async function handleMultiModelBulkOperation(items, operation, keyMap) {
1025
+ if (items.length === 0) {
1026
+ return {};
1027
+ }
1028
+ const results = await operation(items);
1029
+ const mappedResults = {};
1030
+ results.forEach((result, index) => {
1031
+ const key = index.toString();
1032
+ const { modelIndex, fieldKey } = keyMap[key];
1033
+ mappedResults[`${modelIndex}-${fieldKey}`] = result;
1034
+ });
1035
+ return mappedResults;
1036
+ }
1037
+ function prepareFieldsForDecryption(model) {
1038
+ const otherFields = { ...model };
1039
+ const operationFields = {};
1040
+ const nullFields = {};
1041
+ const keyMap = {};
1042
+ let index = 0;
1043
+ const processNestedFields = (obj, prefix = "") => {
1044
+ for (const [key, value] of Object.entries(obj)) {
1045
+ const fullKey = prefix ? `${prefix}.${key}` : key;
1046
+ if (value === null || value === void 0) {
1047
+ nullFields[fullKey] = value;
1048
+ continue;
1049
+ }
1050
+ if (typeof value === "object" && !isEncryptedPayload(value)) {
1051
+ processNestedFields(value, fullKey);
1052
+ } else if (isEncryptedPayload(value)) {
1053
+ const id = index.toString();
1054
+ keyMap[id] = fullKey;
1055
+ operationFields[fullKey] = value;
1056
+ index++;
1057
+ const parts = fullKey.split(".");
1058
+ let current = otherFields;
1059
+ for (let i = 0; i < parts.length - 1; i++) {
1060
+ current = current[parts[i]];
1061
+ }
1062
+ delete current[parts[parts.length - 1]];
1063
+ }
1064
+ }
1065
+ };
1066
+ processNestedFields(model);
1067
+ return { otherFields, operationFields, keyMap, nullFields };
1068
+ }
1069
+ function prepareFieldsForEncryption(model, table) {
1070
+ const otherFields = { ...model };
1071
+ const operationFields = {};
1072
+ const nullFields = {};
1073
+ const keyMap = {};
1074
+ let index = 0;
1075
+ const processNestedFields = (obj, prefix = "", columnPaths2 = []) => {
1076
+ for (const [key, value] of Object.entries(obj)) {
1077
+ const fullKey = prefix ? `${prefix}.${key}` : key;
1078
+ if (value === null || value === void 0) {
1079
+ nullFields[fullKey] = value;
1080
+ continue;
1081
+ }
1082
+ if (typeof value === "object" && !isEncryptedPayload(value) && !columnPaths2.includes(fullKey)) {
1083
+ if (columnPaths2.some((path2) => path2.startsWith(fullKey))) {
1084
+ processNestedFields(
1085
+ value,
1086
+ fullKey,
1087
+ columnPaths2
1088
+ );
1089
+ }
1090
+ } else if (columnPaths2.includes(fullKey)) {
1091
+ const id = index.toString();
1092
+ keyMap[id] = fullKey;
1093
+ operationFields[fullKey] = value;
1094
+ index++;
1095
+ const parts = fullKey.split(".");
1096
+ let current = otherFields;
1097
+ for (let i = 0; i < parts.length - 1; i++) {
1098
+ current = current[parts[i]];
1099
+ }
1100
+ delete current[parts[parts.length - 1]];
1101
+ }
1102
+ }
1103
+ };
1104
+ const columnPaths = Object.keys(table.build().columns);
1105
+ processNestedFields(model, "", columnPaths);
1106
+ return { otherFields, operationFields, keyMap, nullFields };
1107
+ }
1108
+ async function decryptModelFields(model, client, auditData) {
1109
+ if (!client) {
1110
+ throw new Error("Client not initialized");
1111
+ }
1112
+ const { otherFields, operationFields, keyMap, nullFields } = prepareFieldsForDecryption(model);
1113
+ const bulkDecryptPayload = Object.entries(operationFields).map(
1114
+ ([key, value]) => ({
1115
+ id: key,
1116
+ ciphertext: value
1117
+ })
1118
+ );
1119
+ const decryptedFields = await handleSingleModelBulkOperation(
1120
+ bulkDecryptPayload,
1121
+ (items) => (0, import_protect_ffi4.decryptBulk)(client, {
1122
+ ciphertexts: items,
1123
+ unverifiedContext: auditData?.metadata
1124
+ }),
1125
+ keyMap
1126
+ );
1127
+ const result = { ...otherFields };
1128
+ for (const [key, value] of Object.entries(nullFields)) {
1129
+ const parts = key.split(".");
1130
+ setNestedValue(result, parts, value);
1131
+ }
1132
+ for (const [key, value] of Object.entries(decryptedFields)) {
1133
+ const parts = key.split(".");
1134
+ setNestedValue(result, parts, value);
1135
+ }
1136
+ return result;
1137
+ }
1138
+ async function encryptModelFields(model, table, client, auditData) {
1139
+ if (!client) {
1140
+ throw new Error("Client not initialized");
1141
+ }
1142
+ const { otherFields, operationFields, keyMap, nullFields } = prepareFieldsForEncryption(model, table);
1143
+ const bulkEncryptPayload = Object.entries(operationFields).map(
1144
+ ([key, value]) => ({
1145
+ id: key,
1146
+ plaintext: value,
1147
+ table: table.tableName,
1148
+ column: key
1149
+ })
1150
+ );
1151
+ const encryptedData = await handleSingleModelBulkOperation(
1152
+ bulkEncryptPayload,
1153
+ (items) => (0, import_protect_ffi4.encryptBulk)(client, {
1154
+ plaintexts: items,
1155
+ unverifiedContext: auditData?.metadata
1156
+ }),
1157
+ keyMap
1158
+ );
1159
+ const result = { ...otherFields };
1160
+ for (const [key, value] of Object.entries(nullFields)) {
1161
+ const parts = key.split(".");
1162
+ setNestedValue(result, parts, value);
1163
+ }
1164
+ for (const [key, value] of Object.entries(encryptedData)) {
1165
+ const parts = key.split(".");
1166
+ setNestedValue(result, parts, value);
1167
+ }
1168
+ return result;
1169
+ }
1170
+ async function decryptModelFieldsWithLockContext(model, client, lockContext, auditData) {
1171
+ if (!client) {
1172
+ throw new Error("Client not initialized");
1173
+ }
1174
+ if (!lockContext) {
1175
+ throw new Error("Lock context is not initialized");
1176
+ }
1177
+ const { otherFields, operationFields, keyMap, nullFields } = prepareFieldsForDecryption(model);
1178
+ const bulkDecryptPayload = Object.entries(operationFields).map(
1179
+ ([key, value]) => ({
1180
+ id: key,
1181
+ ciphertext: value,
1182
+ lockContext: lockContext.context
1183
+ })
1184
+ );
1185
+ const decryptedFields = await handleSingleModelBulkOperation(
1186
+ bulkDecryptPayload,
1187
+ (items) => (0, import_protect_ffi4.decryptBulk)(client, {
1188
+ ciphertexts: items,
1189
+ serviceToken: lockContext.ctsToken,
1190
+ unverifiedContext: auditData?.metadata
1191
+ }),
1192
+ keyMap
1193
+ );
1194
+ const result = { ...otherFields };
1195
+ for (const [key, value] of Object.entries(nullFields)) {
1196
+ const parts = key.split(".");
1197
+ setNestedValue(result, parts, value);
1198
+ }
1199
+ for (const [key, value] of Object.entries(decryptedFields)) {
1200
+ const parts = key.split(".");
1201
+ setNestedValue(result, parts, value);
1202
+ }
1203
+ return result;
1204
+ }
1205
+ async function encryptModelFieldsWithLockContext(model, table, client, lockContext, auditData) {
1206
+ if (!client) {
1207
+ throw new Error("Client not initialized");
1208
+ }
1209
+ if (!lockContext) {
1210
+ throw new Error("Lock context is not initialized");
1211
+ }
1212
+ const { otherFields, operationFields, keyMap, nullFields } = prepareFieldsForEncryption(model, table);
1213
+ const bulkEncryptPayload = Object.entries(operationFields).map(
1214
+ ([key, value]) => ({
1215
+ id: key,
1216
+ plaintext: value,
1217
+ table: table.tableName,
1218
+ column: key,
1219
+ lockContext: lockContext.context
1220
+ })
1221
+ );
1222
+ const encryptedData = await handleSingleModelBulkOperation(
1223
+ bulkEncryptPayload,
1224
+ (items) => (0, import_protect_ffi4.encryptBulk)(client, {
1225
+ plaintexts: items,
1226
+ serviceToken: lockContext.ctsToken,
1227
+ unverifiedContext: auditData?.metadata
1228
+ }),
1229
+ keyMap
1230
+ );
1231
+ const result = { ...otherFields };
1232
+ for (const [key, value] of Object.entries(nullFields)) {
1233
+ const parts = key.split(".");
1234
+ setNestedValue(result, parts, value);
1235
+ }
1236
+ for (const [key, value] of Object.entries(encryptedData)) {
1237
+ const parts = key.split(".");
1238
+ setNestedValue(result, parts, value);
1239
+ }
1240
+ return result;
1241
+ }
1242
+ function prepareBulkModelsForOperation(models, table) {
1243
+ const otherFields = [];
1244
+ const operationFields = [];
1245
+ const nullFields = [];
1246
+ const keyMap = {};
1247
+ let index = 0;
1248
+ for (let modelIndex = 0; modelIndex < models.length; modelIndex++) {
1249
+ const model = models[modelIndex];
1250
+ const modelOtherFields = { ...model };
1251
+ const modelOperationFields = {};
1252
+ const modelNullFields = {};
1253
+ const processNestedFields = (obj, prefix = "", columnPaths = []) => {
1254
+ for (const [key, value] of Object.entries(obj)) {
1255
+ const fullKey = prefix ? `${prefix}.${key}` : key;
1256
+ if (value === null || value === void 0) {
1257
+ modelNullFields[fullKey] = value;
1258
+ continue;
1259
+ }
1260
+ if (typeof value === "object" && !isEncryptedPayload(value) && !columnPaths.includes(fullKey)) {
1261
+ if (columnPaths.some((path2) => path2.startsWith(fullKey))) {
1262
+ processNestedFields(
1263
+ value,
1264
+ fullKey,
1265
+ columnPaths
1266
+ );
1267
+ }
1268
+ } else if (columnPaths.includes(fullKey)) {
1269
+ const id = index.toString();
1270
+ keyMap[id] = { modelIndex, fieldKey: fullKey };
1271
+ modelOperationFields[fullKey] = value;
1272
+ index++;
1273
+ const parts = fullKey.split(".");
1274
+ let current = modelOtherFields;
1275
+ for (let i = 0; i < parts.length - 1; i++) {
1276
+ current = current[parts[i]];
1277
+ }
1278
+ delete current[parts[parts.length - 1]];
1279
+ }
1280
+ }
1281
+ };
1282
+ if (table) {
1283
+ const columnPaths = Object.keys(table.build().columns);
1284
+ processNestedFields(model, "", columnPaths);
1285
+ } else {
1286
+ const processEncryptedFields = (obj, prefix = "", columnPaths = []) => {
1287
+ for (const [key, value] of Object.entries(obj)) {
1288
+ const fullKey = prefix ? `${prefix}.${key}` : key;
1289
+ if (value === null || value === void 0) {
1290
+ modelNullFields[fullKey] = value;
1291
+ continue;
1292
+ }
1293
+ if (typeof value === "object" && !isEncryptedPayload(value) && !columnPaths.includes(fullKey)) {
1294
+ processEncryptedFields(
1295
+ value,
1296
+ fullKey,
1297
+ columnPaths
1298
+ );
1299
+ } else if (isEncryptedPayload(value)) {
1300
+ const id = index.toString();
1301
+ keyMap[id] = { modelIndex, fieldKey: fullKey };
1302
+ modelOperationFields[fullKey] = value;
1303
+ index++;
1304
+ const parts = fullKey.split(".");
1305
+ let current = modelOtherFields;
1306
+ for (let i = 0; i < parts.length - 1; i++) {
1307
+ current = current[parts[i]];
1308
+ }
1309
+ delete current[parts[parts.length - 1]];
1310
+ }
1311
+ }
1312
+ };
1313
+ processEncryptedFields(model);
1314
+ }
1315
+ otherFields.push(modelOtherFields);
1316
+ operationFields.push(modelOperationFields);
1317
+ nullFields.push(modelNullFields);
1318
+ }
1319
+ return { otherFields, operationFields, keyMap, nullFields };
1320
+ }
1321
+ async function bulkEncryptModels(models, table, client, auditData) {
1322
+ if (!client) {
1323
+ throw new Error("Client not initialized");
1324
+ }
1325
+ if (!models || models.length === 0) {
1326
+ return [];
1327
+ }
1328
+ const { otherFields, operationFields, keyMap, nullFields } = prepareBulkModelsForOperation(models, table);
1329
+ const bulkEncryptPayload = operationFields.flatMap(
1330
+ (fields, modelIndex) => Object.entries(fields).map(([key, value]) => ({
1331
+ id: `${modelIndex}-${key}`,
1332
+ plaintext: value,
1333
+ table: table.tableName,
1334
+ column: key
1335
+ }))
1336
+ );
1337
+ const encryptedData = await handleMultiModelBulkOperation(
1338
+ bulkEncryptPayload,
1339
+ (items) => (0, import_protect_ffi4.encryptBulk)(client, {
1340
+ plaintexts: items,
1341
+ unverifiedContext: auditData?.metadata
1342
+ }),
1343
+ keyMap
1344
+ );
1345
+ return models.map((_, modelIndex) => {
1346
+ const result = { ...otherFields[modelIndex] };
1347
+ for (const [key, value] of Object.entries(nullFields[modelIndex])) {
1348
+ const parts = key.split(".");
1349
+ setNestedValue(result, parts, value);
1350
+ }
1351
+ const modelData = Object.fromEntries(
1352
+ Object.entries(encryptedData).filter(([key]) => {
1353
+ const [idx] = key.split("-");
1354
+ return Number.parseInt(idx) === modelIndex;
1355
+ }).map(([key, value]) => {
1356
+ const [_2, fieldKey] = key.split("-");
1357
+ return [fieldKey, value];
1358
+ })
1359
+ );
1360
+ for (const [key, value] of Object.entries(modelData)) {
1361
+ const parts = key.split(".");
1362
+ setNestedValue(result, parts, value);
1363
+ }
1364
+ return result;
1365
+ });
1366
+ }
1367
+ async function bulkDecryptModels(models, client, auditData) {
1368
+ if (!client) {
1369
+ throw new Error("Client not initialized");
1370
+ }
1371
+ if (!models || models.length === 0) {
1372
+ return [];
1373
+ }
1374
+ const { otherFields, operationFields, keyMap, nullFields } = prepareBulkModelsForOperation(models);
1375
+ const bulkDecryptPayload = operationFields.flatMap(
1376
+ (fields, modelIndex) => Object.entries(fields).map(([key, value]) => ({
1377
+ id: `${modelIndex}-${key}`,
1378
+ ciphertext: value
1379
+ }))
1380
+ );
1381
+ const decryptedFields = await handleMultiModelBulkOperation(
1382
+ bulkDecryptPayload,
1383
+ (items) => (0, import_protect_ffi4.decryptBulk)(client, {
1384
+ ciphertexts: items,
1385
+ unverifiedContext: auditData?.metadata
1386
+ }),
1387
+ keyMap
1388
+ );
1389
+ return models.map((_, modelIndex) => {
1390
+ const result = { ...otherFields[modelIndex] };
1391
+ for (const [key, value] of Object.entries(nullFields[modelIndex])) {
1392
+ const parts = key.split(".");
1393
+ setNestedValue(result, parts, value);
1394
+ }
1395
+ const modelData = Object.fromEntries(
1396
+ Object.entries(decryptedFields).filter(([key]) => {
1397
+ const [idx] = key.split("-");
1398
+ return Number.parseInt(idx) === modelIndex;
1399
+ }).map(([key, value]) => {
1400
+ const [_2, fieldKey] = key.split("-");
1401
+ return [fieldKey, value];
1402
+ })
1403
+ );
1404
+ for (const [key, value] of Object.entries(modelData)) {
1405
+ const parts = key.split(".");
1406
+ setNestedValue(result, parts, value);
1407
+ }
1408
+ return result;
1409
+ });
1410
+ }
1411
+ async function bulkDecryptModelsWithLockContext(models, client, lockContext, auditData) {
1412
+ if (!client) {
1413
+ throw new Error("Client not initialized");
1414
+ }
1415
+ if (!lockContext) {
1416
+ throw new Error("Lock context is not initialized");
1417
+ }
1418
+ const { otherFields, operationFields, keyMap, nullFields } = prepareBulkModelsForOperation(models);
1419
+ const bulkDecryptPayload = operationFields.flatMap(
1420
+ (fields, modelIndex) => Object.entries(fields).map(([key, value]) => ({
1421
+ id: `${modelIndex}-${key}`,
1422
+ ciphertext: value,
1423
+ lockContext: lockContext.context
1424
+ }))
1425
+ );
1426
+ const decryptedFields = await handleMultiModelBulkOperation(
1427
+ bulkDecryptPayload,
1428
+ (items) => (0, import_protect_ffi4.decryptBulk)(client, {
1429
+ ciphertexts: items,
1430
+ serviceToken: lockContext.ctsToken,
1431
+ unverifiedContext: auditData?.metadata
1432
+ }),
1433
+ keyMap
1434
+ );
1435
+ return models.map((_, modelIndex) => {
1436
+ const result = { ...otherFields[modelIndex] };
1437
+ for (const [key, value] of Object.entries(nullFields[modelIndex])) {
1438
+ const parts = key.split(".");
1439
+ setNestedValue(result, parts, value);
1440
+ }
1441
+ const modelData = Object.fromEntries(
1442
+ Object.entries(decryptedFields).filter(([key]) => {
1443
+ const [idx] = key.split("-");
1444
+ return Number.parseInt(idx) === modelIndex;
1445
+ }).map(([key, value]) => {
1446
+ const [_2, fieldKey] = key.split("-");
1447
+ return [fieldKey, value];
1448
+ })
1449
+ );
1450
+ for (const [key, value] of Object.entries(modelData)) {
1451
+ const parts = key.split(".");
1452
+ setNestedValue(result, parts, value);
1453
+ }
1454
+ return result;
1455
+ });
1456
+ }
1457
+ async function bulkEncryptModelsWithLockContext(models, table, client, lockContext, auditData) {
1458
+ if (!client) {
1459
+ throw new Error("Client not initialized");
1460
+ }
1461
+ if (!lockContext) {
1462
+ throw new Error("Lock context is not initialized");
1463
+ }
1464
+ const { otherFields, operationFields, keyMap, nullFields } = prepareBulkModelsForOperation(models, table);
1465
+ const bulkEncryptPayload = operationFields.flatMap(
1466
+ (fields, modelIndex) => Object.entries(fields).map(([key, value]) => ({
1467
+ id: `${modelIndex}-${key}`,
1468
+ plaintext: value,
1469
+ table: table.tableName,
1470
+ column: key,
1471
+ lockContext: lockContext.context
1472
+ }))
1473
+ );
1474
+ const encryptedData = await handleMultiModelBulkOperation(
1475
+ bulkEncryptPayload,
1476
+ (items) => (0, import_protect_ffi4.encryptBulk)(client, {
1477
+ plaintexts: items,
1478
+ serviceToken: lockContext.ctsToken,
1479
+ unverifiedContext: auditData?.metadata
1480
+ }),
1481
+ keyMap
1482
+ );
1483
+ return models.map((_, modelIndex) => {
1484
+ const result = { ...otherFields[modelIndex] };
1485
+ for (const [key, value] of Object.entries(nullFields[modelIndex])) {
1486
+ const parts = key.split(".");
1487
+ setNestedValue(result, parts, value);
1488
+ }
1489
+ const modelData = Object.fromEntries(
1490
+ Object.entries(encryptedData).filter(([key]) => {
1491
+ const [idx] = key.split("-");
1492
+ return Number.parseInt(idx) === modelIndex;
1493
+ }).map(([key, value]) => {
1494
+ const [_2, fieldKey] = key.split("-");
1495
+ return [fieldKey, value];
1496
+ })
1497
+ );
1498
+ for (const [key, value] of Object.entries(modelData)) {
1499
+ const parts = key.split(".");
1500
+ setNestedValue(result, parts, value);
1501
+ }
1502
+ return result;
1503
+ });
1504
+ }
1505
+
1506
+ // src/encryption/ffi/operations/bulk-decrypt-models.ts
1507
+ var BulkDecryptModelsOperation = class extends EncryptionOperation {
1508
+ client;
1509
+ models;
1510
+ constructor(client, models) {
1511
+ super();
1512
+ this.client = client;
1513
+ this.models = models;
1514
+ }
1515
+ withLockContext(lockContext) {
1516
+ return new BulkDecryptModelsOperationWithLockContext(this, lockContext);
1517
+ }
1518
+ async execute() {
1519
+ const log = (0, import_evlog.createRequestLogger)();
1520
+ log.set({
1521
+ op: "bulkDecryptModels",
1522
+ count: this.models.length,
1523
+ lockContext: false
1524
+ });
1525
+ const result = await (0, import_result3.withResult)(
1526
+ async () => {
1527
+ if (!this.client) {
1528
+ throw noClientError();
1529
+ }
1530
+ const auditData = this.getAuditData();
1531
+ return await bulkDecryptModels(this.models, this.client, auditData);
1532
+ },
1533
+ (error) => {
1534
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
1535
+ return {
1536
+ type: EncryptionErrorTypes.DecryptionError,
1537
+ message: error.message,
1538
+ code: getErrorCode(error)
1539
+ };
1540
+ }
1541
+ );
1542
+ log.emit();
1543
+ return result;
1544
+ }
1545
+ getOperation() {
1546
+ return {
1547
+ client: this.client,
1548
+ models: this.models
1549
+ };
1550
+ }
1551
+ };
1552
+ var BulkDecryptModelsOperationWithLockContext = class extends EncryptionOperation {
1553
+ operation;
1554
+ lockContext;
1555
+ constructor(operation, lockContext) {
1556
+ super();
1557
+ this.operation = operation;
1558
+ this.lockContext = lockContext;
1559
+ const auditData = operation.getAuditData();
1560
+ if (auditData) {
1561
+ this.audit(auditData);
1562
+ }
1563
+ }
1564
+ async execute() {
1565
+ const { client, models } = this.operation.getOperation();
1566
+ const log = (0, import_evlog.createRequestLogger)();
1567
+ log.set({
1568
+ op: "bulkDecryptModels",
1569
+ count: models.length,
1570
+ lockContext: true
1571
+ });
1572
+ const result = await (0, import_result3.withResult)(
1573
+ async () => {
1574
+ if (!client) {
1575
+ throw noClientError();
1576
+ }
1577
+ const context = await this.lockContext.getLockContext();
1578
+ if (context.failure) {
1579
+ throw new Error(`[encryption]: ${context.failure.message}`);
1580
+ }
1581
+ const auditData = this.getAuditData();
1582
+ return await bulkDecryptModelsWithLockContext(
1583
+ models,
1584
+ client,
1585
+ context.data,
1586
+ auditData
1587
+ );
1588
+ },
1589
+ (error) => {
1590
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
1591
+ return {
1592
+ type: EncryptionErrorTypes.DecryptionError,
1593
+ message: error.message,
1594
+ code: getErrorCode(error)
1595
+ };
1596
+ }
1597
+ );
1598
+ log.emit();
1599
+ return result;
1600
+ }
1601
+ };
1602
+
1603
+ // src/encryption/ffi/operations/bulk-encrypt.ts
1604
+ var import_result4 = require("@byteslice/result");
1605
+ var import_protect_ffi5 = require("@cipherstash/protect-ffi");
1606
+ var createEncryptPayloads = (plaintexts, column, table, lockContext) => {
1607
+ return plaintexts.map((item, index) => ({ ...item, originalIndex: index })).filter(({ plaintext }) => plaintext !== null).map(({ id, plaintext, originalIndex }) => ({
1608
+ id,
1609
+ plaintext,
1610
+ column: column.getName(),
1611
+ table: table.tableName,
1612
+ originalIndex,
1613
+ ...lockContext && { lockContext }
1614
+ }));
1615
+ };
1616
+ var createNullResult2 = (plaintexts) => {
1617
+ return plaintexts.map(({ id }) => ({ id, data: null }));
1618
+ };
1619
+ var mapEncryptedDataToResult = (plaintexts, encryptedData) => {
1620
+ const result = new Array(plaintexts.length);
1621
+ let encryptedIndex = 0;
1622
+ for (let i = 0; i < plaintexts.length; i++) {
1623
+ if (plaintexts[i].plaintext === null) {
1624
+ result[i] = { id: plaintexts[i].id, data: null };
1625
+ } else {
1626
+ result[i] = {
1627
+ id: plaintexts[i].id,
1628
+ data: encryptedData[encryptedIndex]
1629
+ };
1630
+ encryptedIndex++;
1631
+ }
1632
+ }
1633
+ return result;
1634
+ };
1635
+ var BulkEncryptOperation = class extends EncryptionOperation {
1636
+ client;
1637
+ plaintexts;
1638
+ column;
1639
+ table;
1640
+ constructor(client, plaintexts, opts) {
1641
+ super();
1642
+ this.client = client;
1643
+ this.plaintexts = plaintexts;
1644
+ this.column = opts.column;
1645
+ this.table = opts.table;
1646
+ }
1647
+ withLockContext(lockContext) {
1648
+ return new BulkEncryptOperationWithLockContext(this, lockContext);
1649
+ }
1650
+ async execute() {
1651
+ const log = (0, import_evlog.createRequestLogger)();
1652
+ log.set({
1653
+ op: "bulkEncrypt",
1654
+ table: this.table.tableName,
1655
+ column: this.column.getName(),
1656
+ count: this.plaintexts?.length ?? 0,
1657
+ lockContext: false
1658
+ });
1659
+ const result = await (0, import_result4.withResult)(
1660
+ async () => {
1661
+ if (!this.client) {
1662
+ throw noClientError();
1663
+ }
1664
+ if (!this.plaintexts || this.plaintexts.length === 0) {
1665
+ return [];
1666
+ }
1667
+ const nonNullPayloads = createEncryptPayloads(
1668
+ this.plaintexts,
1669
+ this.column,
1670
+ this.table
1671
+ );
1672
+ if (nonNullPayloads.length === 0) {
1673
+ return createNullResult2(this.plaintexts);
1674
+ }
1675
+ const { metadata } = this.getAuditData();
1676
+ const encryptedData = await (0, import_protect_ffi5.encryptBulk)(this.client, {
1677
+ plaintexts: nonNullPayloads,
1678
+ unverifiedContext: metadata
1679
+ });
1680
+ return mapEncryptedDataToResult(this.plaintexts, encryptedData);
1681
+ },
1682
+ (error) => {
1683
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
1684
+ return {
1685
+ type: EncryptionErrorTypes.EncryptionError,
1686
+ message: error.message,
1687
+ code: getErrorCode(error)
1688
+ };
1689
+ }
1690
+ );
1691
+ log.emit();
1692
+ return result;
1693
+ }
1694
+ getOperation() {
1695
+ return {
1696
+ client: this.client,
1697
+ plaintexts: this.plaintexts,
1698
+ column: this.column,
1699
+ table: this.table
1700
+ };
1701
+ }
1702
+ };
1703
+ var BulkEncryptOperationWithLockContext = class extends EncryptionOperation {
1704
+ operation;
1705
+ lockContext;
1706
+ constructor(operation, lockContext) {
1707
+ super();
1708
+ this.operation = operation;
1709
+ this.lockContext = lockContext;
1710
+ const auditData = operation.getAuditData();
1711
+ if (auditData) {
1712
+ this.audit(auditData);
1713
+ }
1714
+ }
1715
+ async execute() {
1716
+ const { client, plaintexts, column, table } = this.operation.getOperation();
1717
+ const log = (0, import_evlog.createRequestLogger)();
1718
+ log.set({
1719
+ op: "bulkEncrypt",
1720
+ table: table.tableName,
1721
+ column: column.getName(),
1722
+ count: plaintexts?.length ?? 0,
1723
+ lockContext: true
1724
+ });
1725
+ const result = await (0, import_result4.withResult)(
1726
+ async () => {
1727
+ if (!client) {
1728
+ throw noClientError();
1729
+ }
1730
+ if (!plaintexts || plaintexts.length === 0) {
1731
+ return [];
1732
+ }
1733
+ const context = await this.lockContext.getLockContext();
1734
+ if (context.failure) {
1735
+ throw new Error(`[encryption]: ${context.failure.message}`);
1736
+ }
1737
+ const nonNullPayloads = createEncryptPayloads(
1738
+ plaintexts,
1739
+ column,
1740
+ table,
1741
+ context.data.context
1742
+ );
1743
+ if (nonNullPayloads.length === 0) {
1744
+ return createNullResult2(plaintexts);
1745
+ }
1746
+ const { metadata } = this.getAuditData();
1747
+ const encryptedData = await (0, import_protect_ffi5.encryptBulk)(client, {
1748
+ plaintexts: nonNullPayloads,
1749
+ serviceToken: context.data.ctsToken,
1750
+ unverifiedContext: metadata
1751
+ });
1752
+ return mapEncryptedDataToResult(plaintexts, encryptedData);
1753
+ },
1754
+ (error) => {
1755
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
1756
+ return {
1757
+ type: EncryptionErrorTypes.EncryptionError,
1758
+ message: error.message,
1759
+ code: getErrorCode(error)
1760
+ };
1761
+ }
1762
+ );
1763
+ log.emit();
1764
+ return result;
1765
+ }
1766
+ };
1767
+
1768
+ // src/encryption/ffi/operations/bulk-encrypt-models.ts
1769
+ var import_result5 = require("@byteslice/result");
1770
+ var BulkEncryptModelsOperation = class extends EncryptionOperation {
1771
+ client;
1772
+ models;
1773
+ table;
1774
+ constructor(client, models, table) {
1775
+ super();
1776
+ this.client = client;
1777
+ this.models = models;
1778
+ this.table = table;
1779
+ }
1780
+ withLockContext(lockContext) {
1781
+ return new BulkEncryptModelsOperationWithLockContext(this, lockContext);
1782
+ }
1783
+ async execute() {
1784
+ const log = (0, import_evlog.createRequestLogger)();
1785
+ log.set({
1786
+ op: "bulkEncryptModels",
1787
+ table: this.table.tableName,
1788
+ count: this.models.length,
1789
+ lockContext: false
1790
+ });
1791
+ const result = await (0, import_result5.withResult)(
1792
+ async () => {
1793
+ if (!this.client) {
1794
+ throw noClientError();
1795
+ }
1796
+ const auditData = this.getAuditData();
1797
+ return await bulkEncryptModels(
1798
+ this.models,
1799
+ this.table,
1800
+ this.client,
1801
+ auditData
1802
+ );
1803
+ },
1804
+ (error) => {
1805
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
1806
+ return {
1807
+ type: EncryptionErrorTypes.EncryptionError,
1808
+ message: error.message,
1809
+ code: getErrorCode(error)
1810
+ };
1811
+ }
1812
+ );
1813
+ log.emit();
1814
+ return result;
1815
+ }
1816
+ getOperation() {
1817
+ return {
1818
+ client: this.client,
1819
+ models: this.models,
1820
+ table: this.table
1821
+ };
1822
+ }
1823
+ };
1824
+ var BulkEncryptModelsOperationWithLockContext = class extends EncryptionOperation {
1825
+ operation;
1826
+ lockContext;
1827
+ constructor(operation, lockContext) {
1828
+ super();
1829
+ this.operation = operation;
1830
+ this.lockContext = lockContext;
1831
+ const auditData = operation.getAuditData();
1832
+ if (auditData) {
1833
+ this.audit(auditData);
1834
+ }
1835
+ }
1836
+ async execute() {
1837
+ const { client, models, table } = this.operation.getOperation();
1838
+ const log = (0, import_evlog.createRequestLogger)();
1839
+ log.set({
1840
+ op: "bulkEncryptModels",
1841
+ table: table.tableName,
1842
+ count: models.length,
1843
+ lockContext: true
1844
+ });
1845
+ const result = await (0, import_result5.withResult)(
1846
+ async () => {
1847
+ if (!client) {
1848
+ throw noClientError();
1849
+ }
1850
+ const context = await this.lockContext.getLockContext();
1851
+ if (context.failure) {
1852
+ throw new Error(`[encryption]: ${context.failure.message}`);
1853
+ }
1854
+ const auditData = this.getAuditData();
1855
+ return await bulkEncryptModelsWithLockContext(
1856
+ models,
1857
+ table,
1858
+ client,
1859
+ context.data,
1860
+ auditData
1861
+ );
1862
+ },
1863
+ (error) => {
1864
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
1865
+ return {
1866
+ type: EncryptionErrorTypes.EncryptionError,
1867
+ message: error.message,
1868
+ code: getErrorCode(error)
1869
+ };
1870
+ }
1871
+ );
1872
+ log.emit();
1873
+ return result;
1874
+ }
1875
+ };
1876
+
1877
+ // src/encryption/ffi/operations/decrypt.ts
1878
+ var import_result6 = require("@byteslice/result");
1879
+ var import_protect_ffi6 = require("@cipherstash/protect-ffi");
1880
+ var DecryptOperation = class extends EncryptionOperation {
1881
+ client;
1882
+ encryptedData;
1883
+ constructor(client, encryptedData) {
1884
+ super();
1885
+ this.client = client;
1886
+ this.encryptedData = encryptedData;
1887
+ }
1888
+ withLockContext(lockContext) {
1889
+ return new DecryptOperationWithLockContext(this, lockContext);
1890
+ }
1891
+ async execute() {
1892
+ const log = (0, import_evlog.createRequestLogger)();
1893
+ log.set({
1894
+ op: "decrypt",
1895
+ lockContext: false
1896
+ });
1897
+ const result = await (0, import_result6.withResult)(
1898
+ async () => {
1899
+ if (!this.client) {
1900
+ throw noClientError();
1901
+ }
1902
+ if (this.encryptedData === null) {
1903
+ return null;
1904
+ }
1905
+ const { metadata } = this.getAuditData();
1906
+ return await (0, import_protect_ffi6.decrypt)(this.client, {
1907
+ ciphertext: this.encryptedData,
1908
+ unverifiedContext: metadata
1909
+ });
1910
+ },
1911
+ (error) => {
1912
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
1913
+ return {
1914
+ type: EncryptionErrorTypes.DecryptionError,
1915
+ message: error.message,
1916
+ code: getErrorCode(error)
1917
+ };
1918
+ }
1919
+ );
1920
+ log.emit();
1921
+ return result;
1922
+ }
1923
+ getOperation() {
1924
+ return {
1925
+ client: this.client,
1926
+ encryptedData: this.encryptedData,
1927
+ auditData: this.getAuditData()
1928
+ };
1929
+ }
1930
+ };
1931
+ var DecryptOperationWithLockContext = class extends EncryptionOperation {
1932
+ operation;
1933
+ lockContext;
1934
+ constructor(operation, lockContext) {
1935
+ super();
1936
+ this.operation = operation;
1937
+ this.lockContext = lockContext;
1938
+ const auditData = operation.getAuditData();
1939
+ if (auditData) {
1940
+ this.audit(auditData);
1941
+ }
1942
+ }
1943
+ async execute() {
1944
+ const log = (0, import_evlog.createRequestLogger)();
1945
+ log.set({
1946
+ op: "decrypt",
1947
+ lockContext: true
1948
+ });
1949
+ const result = await (0, import_result6.withResult)(
1950
+ async () => {
1951
+ const { client, encryptedData } = this.operation.getOperation();
1952
+ if (!client) {
1953
+ throw noClientError();
1954
+ }
1955
+ if (encryptedData === null) {
1956
+ return null;
1957
+ }
1958
+ const { metadata } = this.getAuditData();
1959
+ const context = await this.lockContext.getLockContext();
1960
+ if (context.failure) {
1961
+ throw new Error(`[encryption]: ${context.failure.message}`);
1962
+ }
1963
+ return await (0, import_protect_ffi6.decrypt)(client, {
1964
+ ciphertext: encryptedData,
1965
+ unverifiedContext: metadata,
1966
+ lockContext: context.data.context,
1967
+ serviceToken: context.data.ctsToken
1968
+ });
1969
+ },
1970
+ (error) => {
1971
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
1972
+ return {
1973
+ type: EncryptionErrorTypes.DecryptionError,
1974
+ message: error.message,
1975
+ code: getErrorCode(error)
1976
+ };
1977
+ }
1978
+ );
1979
+ log.emit();
1980
+ return result;
1981
+ }
1982
+ };
1983
+
1984
+ // src/encryption/ffi/operations/decrypt-model.ts
1985
+ var import_result7 = require("@byteslice/result");
1986
+ var DecryptModelOperation = class extends EncryptionOperation {
1987
+ client;
1988
+ model;
1989
+ constructor(client, model) {
1990
+ super();
1991
+ this.client = client;
1992
+ this.model = model;
1993
+ }
1994
+ withLockContext(lockContext) {
1995
+ return new DecryptModelOperationWithLockContext(this, lockContext);
1996
+ }
1997
+ async execute() {
1998
+ const log = (0, import_evlog.createRequestLogger)();
1999
+ log.set({
2000
+ op: "decryptModel",
2001
+ lockContext: false
2002
+ });
2003
+ const result = await (0, import_result7.withResult)(
2004
+ async () => {
2005
+ if (!this.client) {
2006
+ throw noClientError();
2007
+ }
2008
+ const auditData = this.getAuditData();
2009
+ return await decryptModelFields(this.model, this.client, auditData);
2010
+ },
2011
+ (error) => {
2012
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
2013
+ return {
2014
+ type: EncryptionErrorTypes.DecryptionError,
2015
+ message: error.message,
2016
+ code: getErrorCode(error)
2017
+ };
2018
+ }
2019
+ );
2020
+ log.emit();
2021
+ return result;
2022
+ }
2023
+ getOperation() {
2024
+ return {
2025
+ client: this.client,
2026
+ model: this.model
2027
+ };
2028
+ }
2029
+ };
2030
+ var DecryptModelOperationWithLockContext = class extends EncryptionOperation {
2031
+ operation;
2032
+ lockContext;
2033
+ constructor(operation, lockContext) {
2034
+ super();
2035
+ this.operation = operation;
2036
+ this.lockContext = lockContext;
2037
+ const auditData = operation.getAuditData();
2038
+ if (auditData) {
2039
+ this.audit(auditData);
2040
+ }
2041
+ }
2042
+ async execute() {
2043
+ const log = (0, import_evlog.createRequestLogger)();
2044
+ log.set({
2045
+ op: "decryptModel",
2046
+ lockContext: true
2047
+ });
2048
+ const result = await (0, import_result7.withResult)(
2049
+ async () => {
2050
+ const { client, model } = this.operation.getOperation();
2051
+ if (!client) {
2052
+ throw noClientError();
2053
+ }
2054
+ const context = await this.lockContext.getLockContext();
2055
+ if (context.failure) {
2056
+ throw new Error(`[encryption]: ${context.failure.message}`);
2057
+ }
2058
+ const auditData = this.getAuditData();
2059
+ return await decryptModelFieldsWithLockContext(
2060
+ model,
2061
+ client,
2062
+ context.data,
2063
+ auditData
2064
+ );
2065
+ },
2066
+ (error) => {
2067
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
2068
+ return {
2069
+ type: EncryptionErrorTypes.DecryptionError,
2070
+ message: error.message,
2071
+ code: getErrorCode(error)
2072
+ };
2073
+ }
2074
+ );
2075
+ log.emit();
2076
+ return result;
2077
+ }
2078
+ };
2079
+
2080
+ // src/encryption/ffi/operations/encrypt.ts
2081
+ var import_result8 = require("@byteslice/result");
2082
+ var import_protect_ffi7 = require("@cipherstash/protect-ffi");
2083
+ var EncryptOperation = class extends EncryptionOperation {
2084
+ client;
2085
+ plaintext;
2086
+ column;
2087
+ table;
2088
+ constructor(client, plaintext, opts) {
2089
+ super();
2090
+ this.client = client;
2091
+ this.plaintext = plaintext;
2092
+ this.column = opts.column;
2093
+ this.table = opts.table;
2094
+ }
2095
+ withLockContext(lockContext) {
2096
+ return new EncryptOperationWithLockContext(this, lockContext);
2097
+ }
2098
+ async execute() {
2099
+ const log = (0, import_evlog.createRequestLogger)();
2100
+ log.set({
2101
+ op: "encrypt",
2102
+ table: this.table.tableName,
2103
+ column: this.column.getName(),
2104
+ lockContext: false
2105
+ });
2106
+ const result = await (0, import_result8.withResult)(
2107
+ async () => {
2108
+ if (!this.client) {
2109
+ throw noClientError();
2110
+ }
2111
+ if (this.plaintext === null) {
2112
+ return null;
2113
+ }
2114
+ if (typeof this.plaintext === "number" && Number.isNaN(this.plaintext)) {
2115
+ throw new Error("[encryption]: Cannot encrypt NaN value");
2116
+ }
2117
+ if (typeof this.plaintext === "number" && !Number.isFinite(this.plaintext)) {
2118
+ throw new Error("[encryption]: Cannot encrypt Infinity value");
2119
+ }
2120
+ const { metadata } = this.getAuditData();
2121
+ return await (0, import_protect_ffi7.encrypt)(this.client, {
2122
+ plaintext: this.plaintext,
2123
+ column: this.column.getName(),
2124
+ table: this.table.tableName,
2125
+ unverifiedContext: metadata
2126
+ });
2127
+ },
2128
+ (error) => {
2129
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
2130
+ return {
2131
+ type: EncryptionErrorTypes.EncryptionError,
2132
+ message: error.message,
2133
+ code: getErrorCode(error)
2134
+ };
2135
+ }
2136
+ );
2137
+ log.emit();
2138
+ return result;
2139
+ }
2140
+ getOperation() {
2141
+ return {
2142
+ client: this.client,
2143
+ plaintext: this.plaintext,
2144
+ column: this.column,
2145
+ table: this.table
2146
+ };
2147
+ }
2148
+ };
2149
+ var EncryptOperationWithLockContext = class extends EncryptionOperation {
2150
+ operation;
2151
+ lockContext;
2152
+ constructor(operation, lockContext) {
2153
+ super();
2154
+ this.operation = operation;
2155
+ this.lockContext = lockContext;
2156
+ const auditData = operation.getAuditData();
2157
+ if (auditData) {
2158
+ this.audit(auditData);
2159
+ }
2160
+ }
2161
+ async execute() {
2162
+ const { client, plaintext, column, table } = this.operation.getOperation();
2163
+ const log = (0, import_evlog.createRequestLogger)();
2164
+ log.set({
2165
+ op: "encrypt",
2166
+ table: table.tableName,
2167
+ column: column.getName(),
2168
+ lockContext: true
2169
+ });
2170
+ const result = await (0, import_result8.withResult)(
2171
+ async () => {
2172
+ if (!client) {
2173
+ throw noClientError();
2174
+ }
2175
+ if (plaintext === null) {
2176
+ return null;
2177
+ }
2178
+ const { metadata } = this.getAuditData();
2179
+ const context = await this.lockContext.getLockContext();
2180
+ if (context.failure) {
2181
+ throw new Error(`[encryption]: ${context.failure.message}`);
2182
+ }
2183
+ return await (0, import_protect_ffi7.encrypt)(client, {
2184
+ plaintext,
2185
+ column: column.getName(),
2186
+ table: table.tableName,
2187
+ lockContext: context.data.context,
2188
+ serviceToken: context.data.ctsToken,
2189
+ unverifiedContext: metadata
2190
+ });
2191
+ },
2192
+ (error) => {
2193
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
2194
+ return {
2195
+ type: EncryptionErrorTypes.EncryptionError,
2196
+ message: error.message,
2197
+ code: getErrorCode(error)
2198
+ };
2199
+ }
2200
+ );
2201
+ log.emit();
2202
+ return result;
2203
+ }
2204
+ };
2205
+
2206
+ // src/encryption/ffi/operations/encrypt-model.ts
2207
+ var import_result9 = require("@byteslice/result");
2208
+ var EncryptModelOperation = class extends EncryptionOperation {
2209
+ client;
2210
+ model;
2211
+ table;
2212
+ constructor(client, model, table) {
2213
+ super();
2214
+ this.client = client;
2215
+ this.model = model;
2216
+ this.table = table;
2217
+ }
2218
+ withLockContext(lockContext) {
2219
+ return new EncryptModelOperationWithLockContext(this, lockContext);
2220
+ }
2221
+ async execute() {
2222
+ const log = (0, import_evlog.createRequestLogger)();
2223
+ log.set({
2224
+ op: "encryptModel",
2225
+ table: this.table.tableName,
2226
+ lockContext: false
2227
+ });
2228
+ const result = await (0, import_result9.withResult)(
2229
+ async () => {
2230
+ if (!this.client) {
2231
+ throw noClientError();
2232
+ }
2233
+ const auditData = this.getAuditData();
2234
+ return await encryptModelFields(
2235
+ this.model,
2236
+ this.table,
2237
+ this.client,
2238
+ auditData
2239
+ );
2240
+ },
2241
+ (error) => {
2242
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
2243
+ return {
2244
+ type: EncryptionErrorTypes.EncryptionError,
2245
+ message: error.message,
2246
+ code: getErrorCode(error)
2247
+ };
2248
+ }
2249
+ );
2250
+ log.emit();
2251
+ return result;
2252
+ }
2253
+ getOperation() {
2254
+ return {
2255
+ client: this.client,
2256
+ model: this.model,
2257
+ table: this.table
2258
+ };
2259
+ }
2260
+ };
2261
+ var EncryptModelOperationWithLockContext = class extends EncryptionOperation {
2262
+ operation;
2263
+ lockContext;
2264
+ constructor(operation, lockContext) {
2265
+ super();
2266
+ this.operation = operation;
2267
+ this.lockContext = lockContext;
2268
+ const auditData = operation.getAuditData();
2269
+ if (auditData) {
2270
+ this.audit(auditData);
2271
+ }
2272
+ }
2273
+ async execute() {
2274
+ const { client, model, table } = this.operation.getOperation();
2275
+ const log = (0, import_evlog.createRequestLogger)();
2276
+ log.set({
2277
+ op: "encryptModel",
2278
+ table: table.tableName,
2279
+ lockContext: true
2280
+ });
2281
+ const result = await (0, import_result9.withResult)(
2282
+ async () => {
2283
+ if (!client) {
2284
+ throw noClientError();
2285
+ }
2286
+ const context = await this.lockContext.getLockContext();
2287
+ if (context.failure) {
2288
+ throw new Error(`[encryption]: ${context.failure.message}`);
2289
+ }
2290
+ const auditData = this.getAuditData();
2291
+ return await encryptModelFieldsWithLockContext(
2292
+ model,
2293
+ table,
2294
+ client,
2295
+ context.data,
2296
+ auditData
2297
+ );
2298
+ },
2299
+ (error) => {
2300
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
2301
+ return {
2302
+ type: EncryptionErrorTypes.EncryptionError,
2303
+ message: error.message,
2304
+ code: getErrorCode(error)
2305
+ };
2306
+ }
2307
+ );
2308
+ log.emit();
2309
+ return result;
2310
+ }
2311
+ };
2312
+
2313
+ // src/encryption/ffi/operations/encrypt-query.ts
2314
+ var import_result10 = require("@byteslice/result");
2315
+ var import_protect_ffi8 = require("@cipherstash/protect-ffi");
2316
+ var EncryptQueryOperation = class extends EncryptionOperation {
2317
+ constructor(client, plaintext, opts) {
2318
+ super();
2319
+ this.client = client;
2320
+ this.plaintext = plaintext;
2321
+ this.opts = opts;
2322
+ }
2323
+ withLockContext(lockContext) {
2324
+ return new EncryptQueryOperationWithLockContext(
2325
+ this.client,
2326
+ this.plaintext,
2327
+ this.opts,
2328
+ lockContext,
2329
+ this.auditMetadata
2330
+ );
2331
+ }
2332
+ async execute() {
2333
+ const log = (0, import_evlog.createRequestLogger)();
2334
+ log.set({
2335
+ op: "encryptQuery",
2336
+ table: this.opts.table.tableName,
2337
+ column: this.opts.column.getName(),
2338
+ queryType: this.opts.queryType,
2339
+ lockContext: false
2340
+ });
2341
+ if (this.plaintext === null || this.plaintext === void 0) {
2342
+ log.emit();
2343
+ return { data: null };
2344
+ }
2345
+ const validationError = validateNumericValue(this.plaintext);
2346
+ if (validationError?.failure) {
2347
+ log.emit();
2348
+ return { failure: validationError.failure };
2349
+ }
2350
+ const result = await (0, import_result10.withResult)(
2351
+ async () => {
2352
+ if (!this.client) throw noClientError();
2353
+ const { metadata } = this.getAuditData();
2354
+ const { indexType, queryOp } = resolveIndexType(
2355
+ this.opts.column,
2356
+ this.opts.queryType,
2357
+ this.plaintext
2358
+ );
2359
+ assertValueIndexCompatibility(
2360
+ this.plaintext,
2361
+ indexType,
2362
+ this.opts.column.getName()
2363
+ );
2364
+ const encrypted = await (0, import_protect_ffi8.encryptQuery)(this.client, {
2365
+ plaintext: this.plaintext,
2366
+ column: this.opts.column.getName(),
2367
+ table: this.opts.table.tableName,
2368
+ indexType,
2369
+ queryOp,
2370
+ unverifiedContext: metadata
2371
+ });
2372
+ return formatEncryptedResult(encrypted, this.opts.returnType);
2373
+ },
2374
+ (error) => {
2375
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
2376
+ return {
2377
+ type: EncryptionErrorTypes.EncryptionError,
2378
+ message: error.message,
2379
+ code: getErrorCode(error)
2380
+ };
2381
+ }
2382
+ );
2383
+ log.emit();
2384
+ return result;
2385
+ }
2386
+ getOperation() {
2387
+ return { client: this.client, plaintext: this.plaintext, ...this.opts };
2388
+ }
2389
+ };
2390
+ var EncryptQueryOperationWithLockContext = class extends EncryptionOperation {
2391
+ constructor(client, plaintext, opts, lockContext, auditMetadata) {
2392
+ super();
2393
+ this.client = client;
2394
+ this.plaintext = plaintext;
2395
+ this.opts = opts;
2396
+ this.lockContext = lockContext;
2397
+ this.auditMetadata = auditMetadata;
2398
+ }
2399
+ async execute() {
2400
+ const log = (0, import_evlog.createRequestLogger)();
2401
+ log.set({
2402
+ op: "encryptQuery",
2403
+ table: this.opts.table.tableName,
2404
+ column: this.opts.column.getName(),
2405
+ queryType: this.opts.queryType,
2406
+ lockContext: true
2407
+ });
2408
+ if (this.plaintext === null || this.plaintext === void 0) {
2409
+ log.emit();
2410
+ return { data: null };
2411
+ }
2412
+ const validationError = validateNumericValue(this.plaintext);
2413
+ if (validationError?.failure) {
2414
+ log.emit();
2415
+ return { failure: validationError.failure };
2416
+ }
2417
+ const lockContextResult = await this.lockContext.getLockContext();
2418
+ if (lockContextResult.failure) {
2419
+ log.emit();
2420
+ return { failure: lockContextResult.failure };
2421
+ }
2422
+ const { ctsToken, context } = lockContextResult.data;
2423
+ const result = await (0, import_result10.withResult)(
2424
+ async () => {
2425
+ if (!this.client) throw noClientError();
2426
+ const { metadata } = this.getAuditData();
2427
+ const { indexType, queryOp } = resolveIndexType(
2428
+ this.opts.column,
2429
+ this.opts.queryType,
2430
+ this.plaintext
2431
+ );
2432
+ assertValueIndexCompatibility(
2433
+ this.plaintext,
2434
+ indexType,
2435
+ this.opts.column.getName()
2436
+ );
2437
+ const encrypted = await (0, import_protect_ffi8.encryptQuery)(this.client, {
2438
+ plaintext: this.plaintext,
2439
+ column: this.opts.column.getName(),
2440
+ table: this.opts.table.tableName,
2441
+ indexType,
2442
+ queryOp,
2443
+ lockContext: context,
2444
+ serviceToken: ctsToken,
2445
+ unverifiedContext: metadata
2446
+ });
2447
+ return formatEncryptedResult(encrypted, this.opts.returnType);
2448
+ },
2449
+ (error) => {
2450
+ log.set({ errorCode: getErrorCode(error) ?? "unknown" });
2451
+ return {
2452
+ type: EncryptionErrorTypes.EncryptionError,
2453
+ message: error.message,
2454
+ code: getErrorCode(error)
2455
+ };
2456
+ }
2457
+ );
2458
+ log.emit();
2459
+ return result;
2460
+ }
2461
+ };
2462
+
2463
+ // src/encryption/ffi/index.ts
2464
+ var noClientError = () => new Error(
2465
+ "The EQL client has not been initialized. Please call init() before using the client."
2466
+ );
2467
+ var EncryptionClient = class {
2468
+ client;
2469
+ encryptConfig;
2470
+ workspaceId;
2471
+ constructor(workspaceCrn) {
2472
+ const workspaceId = loadWorkSpaceId(workspaceCrn);
2473
+ this.workspaceId = workspaceId;
2474
+ }
2475
+ /**
2476
+ * Initializes the EncryptionClient with the provided configuration.
2477
+ * @internal
2478
+ * @param config - The configuration object for initializing the client.
2479
+ * @returns A promise that resolves to a {@link Result} containing the initialized EncryptionClient or an {@link EncryptionError}.
2480
+ **/
2481
+ async init(config) {
2482
+ return await (0, import_result11.withResult)(
2483
+ async () => {
2484
+ const validated = encryptConfigSchema.parse(
2485
+ config.encryptConfig
2486
+ );
2487
+ logger.debug(
2488
+ "Initializing the Protect.js client with the following encrypt config:",
2489
+ {
2490
+ encryptConfig: validated
2491
+ }
2492
+ );
2493
+ this.client = await (0, import_protect_ffi9.newClient)({
2494
+ encryptConfig: validated,
2495
+ clientOpts: {
2496
+ workspaceCrn: config.workspaceCrn,
2497
+ accessKey: config.accessKey,
2498
+ clientId: config.clientId,
2499
+ clientKey: config.clientKey,
2500
+ keyset: toFfiKeysetIdentifier(config.keyset)
2501
+ }
2502
+ });
2503
+ this.encryptConfig = validated;
2504
+ logger.debug("Successfully initialized the Protect.js client.");
2505
+ return this;
2506
+ },
2507
+ (error) => ({
2508
+ type: EncryptionErrorTypes.ClientInitError,
2509
+ message: error.message
2510
+ })
2511
+ );
2512
+ }
2513
+ /**
2514
+ * Encrypt a value - returns a promise which resolves to an encrypted value.
2515
+ *
2516
+ * @param plaintext - The plaintext value to be encrypted. Can be null.
2517
+ * @param opts - Options specifying the column and table for encryption.
2518
+ * @returns An EncryptOperation that can be awaited or chained with additional methods.
2519
+ *
2520
+ * @example
2521
+ * The following example demonstrates how to encrypt a value using the Encryption client.
2522
+ * It includes defining an encryption schema with {@link encryptedTable} and {@link encryptedColumn},
2523
+ * initializing the client with {@link Encryption}, and performing the encryption.
2524
+ *
2525
+ * `encrypt` returns an {@link EncryptOperation} which can be awaited to get a {@link Result}
2526
+ * which can either be the encrypted value or an {@link EncryptionError}.
2527
+ *
2528
+ * ```typescript
2529
+ * // Define encryption schema
2530
+ * import { Encryption } from "@cipherstash/stack"
2531
+ * import { encryptedTable, encryptedColumn } from "@cipherstash/stack/schema"
2532
+ * const userSchema = encryptedTable("users", {
2533
+ * email: encryptedColumn("email"),
2534
+ * });
2535
+ *
2536
+ * // Initialize Encryption client
2537
+ * const client = await Encryption({ schemas: [userSchema] })
2538
+ *
2539
+ * // Encrypt a value
2540
+ * const encryptedResult = await client.encrypt(
2541
+ * "person@example.com",
2542
+ * { column: userSchema.email, table: userSchema }
2543
+ * )
2544
+ *
2545
+ * // Handle encryption result
2546
+ * if (encryptedResult.failure) {
2547
+ * throw new Error(`Encryption failed: ${encryptedResult.failure.message}`);
2548
+ * }
2549
+ *
2550
+ * console.log("Encrypted data:", encryptedResult.data);
2551
+ * ```
2552
+ *
2553
+ * @example
2554
+ * When encrypting data, a {@link LockContext} can be provided to tie the encryption to a specific user or session.
2555
+ * This ensures that the same lock context is required for decryption.
2556
+ *
2557
+ * The following example demonstrates how to create a lock context using a user's JWT token
2558
+ * and use it during encryption.
2559
+ *
2560
+ * ```typescript
2561
+ * // Define encryption schema and initialize client as above
2562
+ *
2563
+ * // Create a lock for the user's `sub` claim from their JWT
2564
+ * const lc = new LockContext();
2565
+ * const lockContext = await lc.identify(userJwt);
2566
+ *
2567
+ * if (lockContext.failure) {
2568
+ * // Handle the failure
2569
+ * }
2570
+ *
2571
+ * // Encrypt a value with the lock context
2572
+ * // Decryption will then require the same lock context
2573
+ * const encryptedResult = await client.encrypt(
2574
+ * "person@example.com",
2575
+ * { column: userSchema.email, table: userSchema }
2576
+ * )
2577
+ * .withLockContext(lockContext)
2578
+ * ```
2579
+ *
2580
+ * @see {@link Result}
2581
+ * @see {@link encryptedTable}
2582
+ * @see {@link LockContext}
2583
+ * @see {@link EncryptOperation}
2584
+ */
2585
+ encrypt(plaintext, opts) {
2586
+ return new EncryptOperation(this.client, plaintext, opts);
2587
+ }
2588
+ encryptQuery(plaintextOrTerms, opts) {
2589
+ if (isScalarQueryTermArray(plaintextOrTerms)) {
2590
+ return new BatchEncryptQueryOperation(this.client, plaintextOrTerms);
2591
+ }
2592
+ if (Array.isArray(plaintextOrTerms) && plaintextOrTerms.length === 0 && !opts) {
2593
+ return new BatchEncryptQueryOperation(
2594
+ this.client,
2595
+ []
2596
+ );
2597
+ }
2598
+ if (!opts) {
2599
+ throw new Error("EncryptQueryOptions are required");
2600
+ }
2601
+ return new EncryptQueryOperation(
2602
+ this.client,
2603
+ plaintextOrTerms,
2604
+ opts
2605
+ );
2606
+ }
2607
+ /**
2608
+ * Decryption - returns a promise which resolves to a decrypted value.
2609
+ *
2610
+ * @param encryptedData - The encrypted data to be decrypted.
2611
+ * @returns A DecryptOperation that can be awaited or chained with additional methods.
2612
+ *
2613
+ * @example
2614
+ * The following example demonstrates how to decrypt a value that was previously encrypted using the {@link encrypt} method.
2615
+ * It includes encrypting a value first, then decrypting it, and handling the result.
2616
+ *
2617
+ * ```typescript
2618
+ * const encryptedData = await client.encrypt(
2619
+ * "person@example.com",
2620
+ * { column: "email", table: "users" }
2621
+ * )
2622
+ * const decryptResult = await client.decrypt(encryptedData)
2623
+ * if (decryptResult.failure) {
2624
+ * throw new Error(`Decryption failed: ${decryptResult.failure.message}`);
2625
+ * }
2626
+ * console.log("Decrypted data:", decryptResult.data);
2627
+ * ```
2628
+ *
2629
+ * @example
2630
+ * Provide a lock context when decrypting:
2631
+ * ```typescript
2632
+ * await client.decrypt(encryptedData)
2633
+ * .withLockContext(lockContext)
2634
+ * ```
2635
+ *
2636
+ * @see {@link LockContext}
2637
+ * @see {@link DecryptOperation}
2638
+ */
2639
+ decrypt(encryptedData) {
2640
+ return new DecryptOperation(this.client, encryptedData);
2641
+ }
2642
+ /**
2643
+ * Encrypt a model (object) based on the table schema.
2644
+ *
2645
+ * Only fields whose keys match columns defined in the table schema are encrypted.
2646
+ * All other fields are passed through unchanged. Returns a thenable operation
2647
+ * that supports `.withLockContext()` for identity-aware encryption.
2648
+ *
2649
+ * @param input - The model object with plaintext values to encrypt.
2650
+ * @param table - The table schema defining which fields to encrypt.
2651
+ * @returns An `EncryptModelOperation<T>` that can be awaited to get a `Result`
2652
+ * containing the model with encrypted fields, or an `EncryptionError`.
2653
+ *
2654
+ * @example
2655
+ * ```typescript
2656
+ * import { Encryption } from "@cipherstash/stack"
2657
+ * import { encryptedTable, encryptedColumn } from "@cipherstash/stack/schema"
2658
+ *
2659
+ * type User = { id: string; email: string; createdAt: Date }
2660
+ *
2661
+ * const usersSchema = encryptedTable("users", {
2662
+ * email: encryptedColumn("email").equality(),
2663
+ * })
2664
+ *
2665
+ * const client = await Encryption({ schemas: [usersSchema] })
2666
+ *
2667
+ * const result = await client.encryptModel<User>(
2668
+ * { id: "user_123", email: "alice@example.com", createdAt: new Date() },
2669
+ * usersSchema,
2670
+ * )
2671
+ *
2672
+ * if (result.failure) {
2673
+ * console.error(result.failure.message)
2674
+ * } else {
2675
+ * // result.data.id is unchanged, result.data.email is encrypted
2676
+ * console.log(result.data)
2677
+ * }
2678
+ * ```
2679
+ */
2680
+ encryptModel(input, table) {
2681
+ return new EncryptModelOperation(this.client, input, table);
2682
+ }
2683
+ /**
2684
+ * Decrypt a model (object) whose fields contain encrypted values.
2685
+ *
2686
+ * Identifies encrypted fields automatically and decrypts them, returning the
2687
+ * model with plaintext values. Returns a thenable operation that supports
2688
+ * `.withLockContext()` for identity-aware decryption.
2689
+ *
2690
+ * @param input - The model object with encrypted field values.
2691
+ * @returns A `DecryptModelOperation<T>` that can be awaited to get a `Result`
2692
+ * containing the model with decrypted plaintext fields, or an `EncryptionError`.
2693
+ *
2694
+ * @example
2695
+ * ```typescript
2696
+ * // Decrypt a previously encrypted model
2697
+ * const decrypted = await client.decryptModel<User>(encryptedUser)
2698
+ *
2699
+ * if (decrypted.failure) {
2700
+ * console.error(decrypted.failure.message)
2701
+ * } else {
2702
+ * console.log(decrypted.data.email) // "alice@example.com"
2703
+ * }
2704
+ *
2705
+ * // With a lock context
2706
+ * const decrypted = await client
2707
+ * .decryptModel<User>(encryptedUser)
2708
+ * .withLockContext(lockContext)
2709
+ * ```
2710
+ */
2711
+ decryptModel(input) {
2712
+ return new DecryptModelOperation(this.client, input);
2713
+ }
2714
+ /**
2715
+ * Encrypt multiple models (objects) in a single bulk operation.
2716
+ *
2717
+ * Performs a single call to ZeroKMS regardless of the number of models,
2718
+ * while still using a unique key for each encrypted value. Only fields
2719
+ * matching the table schema are encrypted; other fields pass through unchanged.
2720
+ *
2721
+ * @param input - An array of model objects with plaintext values to encrypt.
2722
+ * @param table - The table schema defining which fields to encrypt.
2723
+ * @returns A `BulkEncryptModelsOperation<T>` that can be awaited to get a `Result`
2724
+ * containing an array of models with encrypted fields, or an `EncryptionError`.
2725
+ *
2726
+ * @example
2727
+ * ```typescript
2728
+ * import { Encryption } from "@cipherstash/stack"
2729
+ * import { encryptedTable, encryptedColumn } from "@cipherstash/stack/schema"
2730
+ *
2731
+ * type User = { id: string; email: string }
2732
+ *
2733
+ * const usersSchema = encryptedTable("users", {
2734
+ * email: encryptedColumn("email"),
2735
+ * })
2736
+ *
2737
+ * const client = await Encryption({ schemas: [usersSchema] })
2738
+ *
2739
+ * const result = await client.bulkEncryptModels<User>(
2740
+ * [
2741
+ * { id: "1", email: "alice@example.com" },
2742
+ * { id: "2", email: "bob@example.com" },
2743
+ * ],
2744
+ * usersSchema,
2745
+ * )
2746
+ *
2747
+ * if (!result.failure) {
2748
+ * console.log(result.data) // array of models with encrypted email fields
2749
+ * }
2750
+ * ```
2751
+ */
2752
+ bulkEncryptModels(input, table) {
2753
+ return new BulkEncryptModelsOperation(this.client, input, table);
2754
+ }
2755
+ /**
2756
+ * Decrypt multiple models (objects) in a single bulk operation.
2757
+ *
2758
+ * Performs a single call to ZeroKMS regardless of the number of models,
2759
+ * restoring all encrypted fields to their original plaintext values.
2760
+ *
2761
+ * @param input - An array of model objects with encrypted field values.
2762
+ * @returns A `BulkDecryptModelsOperation<T>` that can be awaited to get a `Result`
2763
+ * containing an array of models with decrypted plaintext fields, or an `EncryptionError`.
2764
+ *
2765
+ * @example
2766
+ * ```typescript
2767
+ * const encryptedUsers = encryptedResult.data // from bulkEncryptModels
2768
+ *
2769
+ * const result = await client.bulkDecryptModels<User>(encryptedUsers)
2770
+ *
2771
+ * if (!result.failure) {
2772
+ * for (const user of result.data) {
2773
+ * console.log(user.email) // plaintext email
2774
+ * }
2775
+ * }
2776
+ *
2777
+ * // With a lock context
2778
+ * const result = await client
2779
+ * .bulkDecryptModels<User>(encryptedUsers)
2780
+ * .withLockContext(lockContext)
2781
+ * ```
2782
+ */
2783
+ bulkDecryptModels(input) {
2784
+ return new BulkDecryptModelsOperation(this.client, input);
2785
+ }
2786
+ /**
2787
+ * Encrypt multiple plaintext values in a single bulk operation.
2788
+ *
2789
+ * Each value is encrypted with its own unique key via a single call to ZeroKMS.
2790
+ * Values can include optional `id` fields for correlating results back to
2791
+ * your application data. Null plaintext values are preserved as null.
2792
+ *
2793
+ * @param plaintexts - An array of objects with `plaintext` (and optional `id`) fields.
2794
+ * @param opts - Options specifying the target column and table for encryption.
2795
+ * @returns A `BulkEncryptOperation` that can be awaited to get a `Result`
2796
+ * containing an array of `{ id?, data: Encrypted }` objects, or an `EncryptionError`.
2797
+ *
2798
+ * @example
2799
+ * ```typescript
2800
+ * import { Encryption } from "@cipherstash/stack"
2801
+ * import { encryptedTable, encryptedColumn } from "@cipherstash/stack/schema"
2802
+ *
2803
+ * const users = encryptedTable("users", {
2804
+ * email: encryptedColumn("email"),
2805
+ * })
2806
+ * const client = await Encryption({ schemas: [users] })
2807
+ *
2808
+ * const result = await client.bulkEncrypt(
2809
+ * [
2810
+ * { id: "u1", plaintext: "alice@example.com" },
2811
+ * { id: "u2", plaintext: "bob@example.com" },
2812
+ * { id: "u3", plaintext: null },
2813
+ * ],
2814
+ * { column: users.email, table: users },
2815
+ * )
2816
+ *
2817
+ * if (!result.failure) {
2818
+ * // result.data = [{ id: "u1", data: Encrypted }, { id: "u2", data: Encrypted }, ...]
2819
+ * console.log(result.data)
2820
+ * }
2821
+ * ```
2822
+ */
2823
+ bulkEncrypt(plaintexts, opts) {
2824
+ return new BulkEncryptOperation(this.client, plaintexts, opts);
2825
+ }
2826
+ /**
2827
+ * Decrypt multiple encrypted values in a single bulk operation.
2828
+ *
2829
+ * Performs a single call to ZeroKMS to decrypt all values. The result uses
2830
+ * a multi-status pattern: each item in the returned array has either a `data`
2831
+ * field (success) or an `error` field (failure), allowing graceful handling
2832
+ * of partial failures.
2833
+ *
2834
+ * @param encryptedPayloads - An array of objects with `data` (encrypted payload) and optional `id` fields.
2835
+ * @returns A `BulkDecryptOperation` that can be awaited to get a `Result`
2836
+ * containing an array of `{ id?, data: plaintext }` or `{ id?, error: string }` objects,
2837
+ * or an `EncryptionError` if the entire operation fails.
2838
+ *
2839
+ * @example
2840
+ * ```typescript
2841
+ * const encrypted = await client.bulkEncrypt(plaintexts, { column: users.email, table: users })
2842
+ *
2843
+ * const result = await client.bulkDecrypt(encrypted.data)
2844
+ *
2845
+ * if (!result.failure) {
2846
+ * for (const item of result.data) {
2847
+ * if ("data" in item) {
2848
+ * console.log(`${item.id}: ${item.data}`)
2849
+ * } else {
2850
+ * console.error(`${item.id} failed: ${item.error}`)
2851
+ * }
2852
+ * }
2853
+ * }
2854
+ * ```
2855
+ */
2856
+ bulkDecrypt(encryptedPayloads) {
2857
+ return new BulkDecryptOperation(this.client, encryptedPayloads);
2858
+ }
2859
+ /** e.g., debugging or environment info */
2860
+ clientInfo() {
2861
+ return {
2862
+ workspaceId: this.workspaceId
2863
+ };
2864
+ }
2865
+ };
2866
+
2867
+ // src/index.ts
2868
+ function isValidUuid(uuid) {
2869
+ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
2870
+ return uuidRegex.test(uuid);
2871
+ }
2872
+ var Encryption = async (config) => {
2873
+ if (config.logging) {
2874
+ initStackLogger(config.logging);
2875
+ }
2876
+ const { schemas, config: clientConfig } = config;
2877
+ if (!schemas.length) {
2878
+ throw new Error(
2879
+ "[encryption]: At least one encryptedTable must be provided to initialize the encryption client"
2880
+ );
2881
+ }
2882
+ if (clientConfig?.keyset && "id" in clientConfig.keyset && !isValidUuid(clientConfig.keyset.id)) {
2883
+ throw new Error(
2884
+ "[encryption]: Invalid UUID provided for keyset id. Must be a valid UUID."
2885
+ );
2886
+ }
2887
+ const client = new EncryptionClient(clientConfig?.workspaceCrn);
2888
+ const encryptConfig = buildEncryptConfig(...schemas);
2889
+ const result = await client.init({
2890
+ encryptConfig,
2891
+ ...clientConfig
2892
+ });
2893
+ if (result.failure) {
2894
+ throw new Error(`[encryption]: ${result.failure.message}`);
2895
+ }
2896
+ return result.data;
2897
+ };
2898
+
2899
+ // src/secrets/index.ts
2900
+ var Secrets = class {
2901
+ encryptionClient = null;
2902
+ config;
2903
+ apiBaseUrl = process.env.STASH_API_URL || "https://dashboard.cipherstash.com/api/secrets";
2904
+ secretsSchema = encryptedTable("secrets", {
2905
+ value: encryptedColumn("value")
2906
+ });
2907
+ constructor(config) {
2908
+ this.config = config;
2909
+ }
2910
+ initPromise = null;
2911
+ /**
2912
+ * Initialize the Secrets client and underlying Encryption client
2913
+ */
2914
+ async ensureInitialized() {
2915
+ if (!this.initPromise) {
2916
+ this.initPromise = this._doInit();
2917
+ }
2918
+ return this.initPromise;
2919
+ }
2920
+ async _doInit() {
2921
+ this.encryptionClient = await Encryption({
2922
+ schemas: [this.secretsSchema],
2923
+ config: {
2924
+ workspaceCrn: this.config.workspaceCRN,
2925
+ clientId: this.config.clientId,
2926
+ clientKey: this.config.clientKey,
2927
+ accessKey: this.config.apiKey,
2928
+ keyset: {
2929
+ name: this.config.environment
2930
+ }
2931
+ }
2932
+ });
2933
+ }
2934
+ /**
2935
+ * Get the authorization header for API requests
2936
+ */
2937
+ getAuthHeader() {
2938
+ return `Bearer ${this.config.apiKey}`;
2939
+ }
2940
+ /**
2941
+ * Make an API request with error handling.
2942
+ *
2943
+ * For GET requests, `params` are appended as URL query parameters.
2944
+ * For POST requests, `body` is sent as JSON in the request body.
2945
+ */
2946
+ async apiRequest(method, path2, options) {
2947
+ try {
2948
+ let url = `${this.apiBaseUrl}${path2}`;
2949
+ if (options?.params) {
2950
+ const searchParams = new URLSearchParams(options.params);
2951
+ url = `${url}?${searchParams.toString()}`;
2952
+ }
2953
+ const headers = {
2954
+ "Content-Type": "application/json",
2955
+ Authorization: this.getAuthHeader()
2956
+ };
2957
+ const response = await fetch(url, {
2958
+ method,
2959
+ headers,
2960
+ body: options?.body ? JSON.stringify(options.body) : void 0
2961
+ });
2962
+ if (!response.ok) {
2963
+ const errorText = await response.text();
2964
+ let errorMessage = `API request failed with status ${response.status}`;
2965
+ try {
2966
+ const errorJson = JSON.parse(errorText);
2967
+ errorMessage = errorJson.message || errorJson.error || errorMessage;
2968
+ } catch {
2969
+ errorMessage = errorText || errorMessage;
2970
+ }
2971
+ return {
2972
+ failure: {
2973
+ type: "ApiError",
2974
+ message: errorMessage
2975
+ }
2976
+ };
2977
+ }
2978
+ const data = await response.json();
2979
+ return { data };
2980
+ } catch (error) {
2981
+ return {
2982
+ failure: {
2983
+ type: "NetworkError",
2984
+ message: error instanceof Error ? error.message : "Unknown network error occurred"
2985
+ }
2986
+ };
2987
+ }
2988
+ }
2989
+ /**
2990
+ * Store an encrypted secret in the vault.
2991
+ * The value is encrypted locally before being sent to the API.
2992
+ *
2993
+ * API: POST /api/secrets/set
2994
+ *
2995
+ * @param name - The name of the secret
2996
+ * @param value - The plaintext value to encrypt and store
2997
+ * @returns A Result containing the API response or an error
2998
+ */
2999
+ async set(name, value) {
3000
+ await this.ensureInitialized();
3001
+ if (!this.encryptionClient) {
3002
+ return {
3003
+ failure: {
3004
+ type: "ClientError",
3005
+ message: "Failed to initialize Encryption client"
3006
+ }
3007
+ };
3008
+ }
3009
+ const encryptResult = await this.encryptionClient.encrypt(value, {
3010
+ column: this.secretsSchema.value,
3011
+ table: this.secretsSchema
3012
+ });
3013
+ if (encryptResult.failure) {
3014
+ return {
3015
+ failure: {
3016
+ type: "EncryptionError",
3017
+ message: encryptResult.failure.message
3018
+ }
3019
+ };
3020
+ }
3021
+ const workspaceId = extractWorkspaceIdFromCrn(this.config.workspaceCRN);
3022
+ return await this.apiRequest("POST", "/set", {
3023
+ body: {
3024
+ workspaceId,
3025
+ environment: this.config.environment,
3026
+ name,
3027
+ encryptedValue: encryptedToPgComposite(encryptResult.data)
3028
+ }
3029
+ });
3030
+ }
3031
+ /**
3032
+ * Retrieve and decrypt a secret from the vault.
3033
+ * The secret is decrypted locally after retrieval.
3034
+ *
3035
+ * API: GET /api/secrets/get?workspaceId=...&environment=...&name=...
3036
+ *
3037
+ * @param name - The name of the secret to retrieve
3038
+ * @returns A Result containing the decrypted value or an error
3039
+ */
3040
+ async get(name) {
3041
+ await this.ensureInitialized();
3042
+ if (!this.encryptionClient) {
3043
+ return {
3044
+ failure: {
3045
+ type: "ClientError",
3046
+ message: "Failed to initialize Encryption client"
3047
+ }
3048
+ };
3049
+ }
3050
+ const workspaceId = extractWorkspaceIdFromCrn(this.config.workspaceCRN);
3051
+ const apiResult = await this.apiRequest("GET", "/get", {
3052
+ params: {
3053
+ workspaceId,
3054
+ environment: this.config.environment,
3055
+ name
3056
+ }
3057
+ });
3058
+ if (apiResult.failure) {
3059
+ return apiResult;
3060
+ }
3061
+ const decryptResult = await this.encryptionClient.decrypt(
3062
+ apiResult.data.encryptedValue.data
3063
+ );
3064
+ if (decryptResult.failure) {
3065
+ return {
3066
+ failure: {
3067
+ type: "DecryptionError",
3068
+ message: decryptResult.failure.message
3069
+ }
3070
+ };
3071
+ }
3072
+ if (typeof decryptResult.data !== "string") {
3073
+ return {
3074
+ failure: {
3075
+ type: "DecryptionError",
3076
+ message: "Decrypted value is not a string"
3077
+ }
3078
+ };
3079
+ }
3080
+ return { data: decryptResult.data };
3081
+ }
3082
+ /**
3083
+ * Retrieve and decrypt many secrets from the vault.
3084
+ * The secrets are decrypted locally after retrieval.
3085
+ * This method only triggers a single network request to the ZeroKMS.
3086
+ *
3087
+ * API: GET /api/secrets/get-many?workspaceId=...&environment=...&names=name1,name2,...
3088
+ *
3089
+ * Constraints:
3090
+ * - Minimum 2 secret names required
3091
+ * - Maximum 100 secret names per request
3092
+ *
3093
+ * @param names - The names of the secrets to retrieve (min 2, max 100)
3094
+ * @returns A Result containing an object mapping secret names to their decrypted values
3095
+ */
3096
+ async getMany(names) {
3097
+ await this.ensureInitialized();
3098
+ if (!this.encryptionClient) {
3099
+ return {
3100
+ failure: {
3101
+ type: "ClientError",
3102
+ message: "Failed to initialize Encryption client"
3103
+ }
3104
+ };
3105
+ }
3106
+ if (names.length < 2) {
3107
+ return {
3108
+ failure: {
3109
+ type: "ClientError",
3110
+ message: "At least 2 secret names are required for getMany"
3111
+ }
3112
+ };
3113
+ }
3114
+ if (names.length > 100) {
3115
+ return {
3116
+ failure: {
3117
+ type: "ClientError",
3118
+ message: "Maximum 100 secret names per request"
3119
+ }
3120
+ };
3121
+ }
3122
+ const workspaceId = extractWorkspaceIdFromCrn(this.config.workspaceCRN);
3123
+ const apiResult = await this.apiRequest(
3124
+ "GET",
3125
+ "/get-many",
3126
+ {
3127
+ params: {
3128
+ workspaceId,
3129
+ environment: this.config.environment,
3130
+ names: names.join(",")
3131
+ }
3132
+ }
3133
+ );
3134
+ if (apiResult.failure) {
3135
+ return apiResult;
3136
+ }
3137
+ const dataToDecrypt = apiResult.data.map((item) => ({
3138
+ name: item.name,
3139
+ value: item.encryptedValue.data
3140
+ }));
3141
+ const decryptResult = await this.encryptionClient.bulkDecryptModels(dataToDecrypt);
3142
+ if (decryptResult.failure) {
3143
+ return {
3144
+ failure: {
3145
+ type: "DecryptionError",
3146
+ message: decryptResult.failure.message
3147
+ }
3148
+ };
3149
+ }
3150
+ const decryptedSecrets = decryptResult.data;
3151
+ const secretsMap = {};
3152
+ for (const secret of decryptedSecrets) {
3153
+ if (secret.name && secret.value) {
3154
+ secretsMap[secret.name] = secret.value;
3155
+ }
3156
+ }
3157
+ return { data: secretsMap };
3158
+ }
3159
+ /**
3160
+ * List all secrets in the environment.
3161
+ * Only names and metadata are returned; values remain encrypted.
3162
+ *
3163
+ * API: GET /api/secrets/list?workspaceId=...&environment=...
3164
+ *
3165
+ * @returns A Result containing the list of secrets or an error
3166
+ */
3167
+ async list() {
3168
+ const workspaceId = extractWorkspaceIdFromCrn(this.config.workspaceCRN);
3169
+ const apiResult = await this.apiRequest(
3170
+ "GET",
3171
+ "/list",
3172
+ {
3173
+ params: {
3174
+ workspaceId,
3175
+ environment: this.config.environment
3176
+ }
3177
+ }
3178
+ );
3179
+ if (apiResult.failure) {
3180
+ return apiResult;
3181
+ }
3182
+ return { data: apiResult.data.secrets };
3183
+ }
3184
+ /**
3185
+ * Delete a secret from the vault.
3186
+ *
3187
+ * API: POST /api/secrets/delete
3188
+ *
3189
+ * @param name - The name of the secret to delete
3190
+ * @returns A Result containing the API response or an error
3191
+ */
3192
+ async delete(name) {
3193
+ const workspaceId = extractWorkspaceIdFromCrn(this.config.workspaceCRN);
3194
+ return await this.apiRequest("POST", "/delete", {
3195
+ body: {
3196
+ workspaceId,
3197
+ environment: this.config.environment,
3198
+ name
3199
+ }
3200
+ });
3201
+ }
3202
+ };
3203
+ // Annotate the CommonJS export names for ESM import in node:
3204
+ 0 && (module.exports = {
3205
+ Secrets
3206
+ });
3207
+ //# sourceMappingURL=index.cjs.map