@deveye/types 0.20.3 → 0.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -72,6 +72,7 @@ export {
72
72
  AGENT_SYNC_INDEX,
73
73
  AGENT_SYNC_OP_RESULT,
74
74
  AGENT_SYNC_PUSH,
75
+ AGENT_SYNC_PUSH_ACK,
75
76
  AGENT_SYNC_SCAN,
76
77
  AGENT_TERM_CLOSE,
77
78
  AGENT_TERM_EXIT,
@@ -79,6 +80,15 @@ export {
79
80
  AGENT_TERM_OPEN,
80
81
  AGENT_TERM_OUTPUT,
81
82
  AGENT_TERM_RESIZE,
83
+ AGENT_TUNNEL_CLOSE,
84
+ AGENT_TUNNEL_CLOSED,
85
+ AGENT_TUNNEL_CREDIT,
86
+ AGENT_TUNNEL_DATA,
87
+ AGENT_TUNNEL_OPEN,
88
+ AGENT_TUNNEL_OPENED,
89
+ AGENT_TUNNEL_PIECE_BYTES,
90
+ AGENT_TUNNEL_PROBE,
91
+ AGENT_TUNNEL_WRITE,
82
92
  AGENT_UPDATE,
83
93
  AGENT_UPDATED,
84
94
  agentClientMessageSchema,
@@ -138,6 +148,7 @@ export {
138
148
  agentSyncIndexPayloadSchema,
139
149
  agentSyncOpResultPayloadSchema,
140
150
  agentSyncPushPayloadSchema,
151
+ agentSyncPushAckPayloadSchema,
141
152
  agentSyncScanPayloadSchema,
142
153
  agentTermClosePayloadSchema,
143
154
  agentTermExitPayloadSchema,
@@ -145,6 +156,13 @@ export {
145
156
  agentTermOpenPayloadSchema,
146
157
  agentTermOutputPayloadSchema,
147
158
  agentTermResizePayloadSchema,
159
+ agentTunnelClosedPayloadSchema,
160
+ agentTunnelClosePayloadSchema,
161
+ agentTunnelCreditPayloadSchema,
162
+ agentTunnelDataPayloadSchema,
163
+ agentTunnelOpenedPayloadSchema,
164
+ agentTunnelOpenPayloadSchema,
165
+ agentTunnelWritePayloadSchema,
148
166
  agentUpdatedMessagePayloadSchema,
149
167
  agentUpdatePayloadSchema,
150
168
  CLOUD_SYNC_CHUNK_EVENT,
@@ -243,11 +261,19 @@ export type {
243
261
  AgentSyncMovePayload,
244
262
  AgentSyncOpResultPayload,
245
263
  AgentSyncPushPayload,
264
+ AgentSyncPushAckPayload,
246
265
  AgentSyncScanPayload,
247
266
  AgentTermClosePayload,
248
267
  AgentTermInputPayload,
249
268
  AgentTermOpenPayload,
250
269
  AgentTermResizePayload,
270
+ AgentTunnelClosedPayload,
271
+ AgentTunnelClosePayload,
272
+ AgentTunnelCreditPayload,
273
+ AgentTunnelDataPayload,
274
+ AgentTunnelOpenedPayload,
275
+ AgentTunnelOpenPayload,
276
+ AgentTunnelWritePayload,
251
277
  AgentUpdatePayload,
252
278
  CloudSyncChunkPush,
253
279
  CloudSyncProgressPush,
@@ -313,6 +339,15 @@ export type {
313
339
  FeedbackStatus,
314
340
  FeedbackViewTrace
315
341
  } from './domain/feedback';
342
+ export {
343
+ IDLE_CLOSE_CODE,
344
+ QUEUE_CLOSE_CODE,
345
+ queueRefusalSchema,
346
+ seatCapsSchema,
347
+ seatSchema,
348
+ SESSION_ACTIVE_COMMAND
349
+ } from './domain/admission';
350
+ export type { QueueRefusal, Seat, SeatCaps } from './domain/admission';
316
351
  export {
317
352
  featureMaintenanceLevelSchema,
318
353
  MAINTENANCE_CLOSE_CODE,
@@ -562,8 +597,18 @@ export type {
562
597
  UserSettingFlag,
563
598
  UserStatus
564
599
  } from './domain/user';
565
- export { secrecyStatusSchema, secrecyWrapModeSchema } from './domain/secrecy';
566
- export type { SecrecyStatus, SecrecyWrapMode, UserSecretKeyRow } from './domain/secrecy';
600
+ export {
601
+ SECRECY_STATE_EVENT,
602
+ secrecyStatePushSchema,
603
+ secrecyStatusSchema,
604
+ secrecyWrapModeSchema
605
+ } from './domain/secrecy';
606
+ export type {
607
+ SecrecyStatePush,
608
+ SecrecyStatus,
609
+ SecrecyWrapMode,
610
+ UserSecretKeyRow
611
+ } from './domain/secrecy';
567
612
  export { workspaceKindSchema, workspaceSchema } from './domain/workspace';
568
613
  export {
569
614
  REMOTE_INSTANCES_MAX,
@@ -616,8 +661,13 @@ export {
616
661
  terminalUser
617
662
  } from './domain/device';
618
663
  export type { Device, DevicePlatform, DeviceRow, DeviceStatus } from './domain/device';
619
- export { packageManagerIdSchema, packageManagerSchema } from './domain/packages';
620
- export type { PackageManager, PackageManagerId } from './domain/packages';
664
+ export {
665
+ isManagedPackageManager,
666
+ packageManagerIdSchema,
667
+ packageManagerSchema,
668
+ unmanagedUpdaterIdSchema
669
+ } from './domain/packages';
670
+ export type { PackageManager, PackageManagerId, UnmanagedUpdaterId } from './domain/packages';
621
671
  export {
622
672
  agentInfoSchema,
623
673
  agentPolicySchema,
@@ -674,6 +724,9 @@ export {
674
724
  NOTIFICATION_EMAIL_MAX,
675
725
  NOTIFICATION_LABEL_MAX,
676
726
  NOTIFICATION_TARGET_MAX,
727
+ SYSTEM_NOTIFICATION_INFO,
728
+ SYSTEM_NOTIFICATION_TARGET,
729
+ systemNotificationTargetSchema,
677
730
  notificationChannelInputSchema,
678
731
  notificationChannelKindSchema,
679
732
  notificationChannelSchema,
@@ -697,7 +750,8 @@ export type {
697
750
  NotificationRouteInput,
698
751
  NotificationRouteRow,
699
752
  NotificationRouteTarget,
700
- NotificationTest
753
+ NotificationTest,
754
+ SystemNotificationTarget
701
755
  } from './domain/notifications';
702
756
 
703
757
  export {
@@ -825,7 +879,10 @@ export {
825
879
  adminMaintenanceDismissNotice,
826
880
  adminMaintenanceFeature,
827
881
  adminMaintenanceGet,
882
+ adminMaintenancePriority,
828
883
  adminMaintenanceSchema,
884
+ adminMaintenanceSeats,
885
+ adminMaintenanceSignups,
829
886
  adminMaintenanceSite,
830
887
  adminSetUserRole,
831
888
  adminSetUserStatus,
@@ -833,6 +890,64 @@ export {
833
890
  adminUserSchema
834
891
  } from './features/admin';
835
892
  export type { AdminMaintenance, AdminUser } from './features/admin';
893
+ export {
894
+ debugBenchCatalog,
895
+ debugBenchContextSchema,
896
+ debugBenchProfileSchema,
897
+ debugBenchReportSchema,
898
+ debugBenchStart,
899
+ debugCommands,
900
+ debugE2eCatalog,
901
+ debugE2eReportSchema,
902
+ debugE2eScenarioReportSchema,
903
+ debugE2eScenarioSchema,
904
+ debugE2eStart,
905
+ debugE2eStepSchema,
906
+ debugE2eSweep,
907
+ debugInstanceSchema,
908
+ debugMailCatalog,
909
+ debugMailPreview,
910
+ debugMailPreviewSchema,
911
+ debugMailSampleSchema,
912
+ debugMailSend,
913
+ debugMailSenderKindSchema,
914
+ debugOverview,
915
+ debugPing,
916
+ debugProbeResultSchema,
917
+ debugProbeSchema,
918
+ debugRunAbort,
919
+ debugRunGet,
920
+ debugRunKindSchema,
921
+ debugRunList,
922
+ debugRunSchema,
923
+ debugRunStatusSchema,
924
+ debugStepStatusSchema,
925
+ debugTrackingClear,
926
+ debugTrackingCreate,
927
+ debugTrackingGet,
928
+ debugTrackingSchema,
929
+ debugTrackingSet,
930
+ debugTrackingUse,
931
+ debugWorkspaceSenderSchema
932
+ } from './features/debug';
933
+ export type {
934
+ DebugBenchContext,
935
+ DebugBenchProfile,
936
+ DebugBenchReport,
937
+ DebugE2eReport,
938
+ DebugE2eScenario,
939
+ DebugE2eScenarioReport,
940
+ DebugE2eStep,
941
+ DebugMailPreview,
942
+ DebugMailSample,
943
+ DebugProbe,
944
+ DebugProbeResult,
945
+ DebugRun,
946
+ DebugRunKind,
947
+ DebugStepStatus,
948
+ DebugTracking,
949
+ DebugWorkspaceSender
950
+ } from './features/debug';
836
951
  export {
837
952
  workspaceActivate,
838
953
  workspaceAdd,
@@ -867,6 +982,9 @@ export {
867
982
  accountPlanSchema,
868
983
  userCommands,
869
984
  userDeleteAccount,
985
+ userExportPartSchema,
986
+ userExportPrepare,
987
+ userExportPreview,
870
988
  userPlan,
871
989
  userSetAvatar,
872
990
  userSetColor,
@@ -874,7 +992,7 @@ export {
874
992
  userSetTheme,
875
993
  userSetUsername
876
994
  } from './features/user';
877
- export type { ThemeStateDTO } from './features/user';
995
+ export type { ThemeStateDTO, UserExportPart } from './features/user';
878
996
  export {
879
997
  agentCollect,
880
998
  agentCommands,
@@ -1013,6 +1131,7 @@ export {
1013
1131
  agentTargetStatusSchema,
1014
1132
  enrollDeviceRequestSchema,
1015
1133
  enrollDeviceResponseSchema,
1134
+ LINK_CODE_MAX_USES,
1016
1135
  LINK_CODE_TTL_MAX_SECONDS,
1017
1136
  linkCodeRequestSchema,
1018
1137
  linkCodeResponseSchema,
@@ -345,6 +345,37 @@ export const agentFilesArchiveEndPayloadSchema = z.object({
345
345
  });
346
346
  export type AgentFilesArchiveEndPayload = z.infer<typeof agentFilesArchiveEndPayloadSchema>;
347
347
 
348
+ const tunnelId = z.string().min(1).max(64);
349
+ /** Raw bytes per `tunnel.data` or `tunnel.write` frame, before base64. */
350
+ export const AGENT_TUNNEL_PIECE_BYTES = 64 * 1024;
351
+ const tunnelData = z.string().max(90_000);
352
+
353
+ /** Agent → server: the TCP connection of a `tunnel.open` is up; bytes may flow. */
354
+ export const AGENT_TUNNEL_OPENED = 'tunnel.opened' as const;
355
+ export const agentTunnelOpenedPayloadSchema = z.object({ deviceId: z.uuid(), tunnelId });
356
+ export type AgentTunnelOpenedPayload = z.infer<typeof agentTunnelOpenedPayloadSchema>;
357
+
358
+ /** Agent → server: bytes read from the target (`data` base64). Spends one credit. */
359
+ export const AGENT_TUNNEL_DATA = 'tunnel.data' as const;
360
+ export const agentTunnelDataPayloadSchema = z.object({
361
+ deviceId: z.uuid(),
362
+ tunnelId,
363
+ data: tunnelData
364
+ });
365
+ export type AgentTunnelDataPayload = z.infer<typeof agentTunnelDataPayloadSchema>;
366
+
367
+ /**
368
+ * Agent → server: the tunnel ended, the last frame for its id. `error` when it
369
+ * did not end with the target closing (refused, unreachable, cut).
370
+ */
371
+ export const AGENT_TUNNEL_CLOSED = 'tunnel.closed' as const;
372
+ export const agentTunnelClosedPayloadSchema = z.object({
373
+ deviceId: z.uuid(),
374
+ tunnelId,
375
+ error: z.string().max(500).optional()
376
+ });
377
+ export type AgentTunnelClosedPayload = z.infer<typeof agentTunnelClosedPayloadSchema>;
378
+
348
379
  const syncOpId = z.string().min(1).max(64);
349
380
  const syncRelPath = z.string().min(1).max(SYNC_REL_PATH_MAX);
350
381
 
@@ -418,7 +449,13 @@ export const agentSyncChunkPayloadSchema = z.object({
418
449
  hash: sha256HexSchema.optional(),
419
450
  size: z.number().int().nonnegative().optional(),
420
451
  mtime: z.number().int().nonnegative().optional(),
421
- error: z.string().max(500).optional()
452
+ error: z.string().max(500).optional(),
453
+ /**
454
+ * Rank of the frame in its push, from 0. Present only when the agent
455
+ * honours the push's `window`: the server then answers each data frame with
456
+ * `sync.pushAck`. Absent (an older agent), nothing is acknowledged.
457
+ */
458
+ seq: z.number().int().nonnegative().optional()
422
459
  });
423
460
  export type AgentSyncChunkPayload = z.infer<typeof agentSyncChunkPayloadSchema>;
424
461
 
@@ -599,6 +636,18 @@ export const agentClientMessageSchema = z.discriminatedUnion('command', [
599
636
  command: z.literal(AGENT_FILES_ARCHIVE_END),
600
637
  payload: agentFilesArchiveEndPayloadSchema
601
638
  }),
639
+ z.object({
640
+ command: z.literal(AGENT_TUNNEL_OPENED),
641
+ payload: agentTunnelOpenedPayloadSchema
642
+ }),
643
+ z.object({
644
+ command: z.literal(AGENT_TUNNEL_DATA),
645
+ payload: agentTunnelDataPayloadSchema
646
+ }),
647
+ z.object({
648
+ command: z.literal(AGENT_TUNNEL_CLOSED),
649
+ payload: agentTunnelClosedPayloadSchema
650
+ }),
602
651
  z.object({
603
652
  command: z.literal(AGENT_SYNC_CHANGED),
604
653
  payload: agentSyncChangedPayloadSchema
@@ -865,6 +914,43 @@ export const AGENT_FILES_ARCHIVE_CANCEL = 'files.archiveCancel' as const;
865
914
  export const agentFilesArchiveCancelPayloadSchema = z.object({ opId: filesOpId });
866
915
  export type AgentFilesArchiveCancelPayload = z.infer<typeof agentFilesArchiveCancelPayloadSchema>;
867
916
 
917
+ /**
918
+ * Open a TCP connection from the machine to `host:port` and relay it: what
919
+ * lets a module reach a service only the machine can see (a database on its
920
+ * loopback). The agent answers `tunnel.opened` then `tunnel.data` frames under
921
+ * credits, or `tunnel.closed` with the reason. It only reaches its own
922
+ * loopback, plus the hosts its operator lists in `tunnel_targets`.
923
+ */
924
+ export const AGENT_TUNNEL_OPEN = 'tunnel.open' as const;
925
+ /** What an agent that knows `tunnel.open` declares in `report.agent.probes`. */
926
+ export const AGENT_TUNNEL_PROBE = 'tunnel';
927
+ export const agentTunnelOpenPayloadSchema = z.object({
928
+ tunnelId,
929
+ host: z.string().min(1).max(255),
930
+ port: z.number().int().min(1).max(65535),
931
+ /** Credits granted up front. */
932
+ window: z.number().int().min(1).max(64)
933
+ });
934
+ export type AgentTunnelOpenPayload = z.infer<typeof agentTunnelOpenPayloadSchema>;
935
+
936
+ /** Bytes for the target (`data` base64). */
937
+ export const AGENT_TUNNEL_WRITE = 'tunnel.write' as const;
938
+ export const agentTunnelWritePayloadSchema = z.object({ tunnelId, data: tunnelData });
939
+ export type AgentTunnelWritePayload = z.infer<typeof agentTunnelWritePayloadSchema>;
940
+
941
+ /** More credits for a tunnel, as the server consumes its `tunnel.data` frames. */
942
+ export const AGENT_TUNNEL_CREDIT = 'tunnel.credit' as const;
943
+ export const agentTunnelCreditPayloadSchema = z.object({
944
+ tunnelId,
945
+ credits: z.number().int().min(1).max(64)
946
+ });
947
+ export type AgentTunnelCreditPayload = z.infer<typeof agentTunnelCreditPayloadSchema>;
948
+
949
+ /** Close a tunnel. The agent sends nothing back: the server has already forgotten it. */
950
+ export const AGENT_TUNNEL_CLOSE = 'tunnel.close' as const;
951
+ export const agentTunnelClosePayloadSchema = z.object({ tunnelId });
952
+ export type AgentTunnelClosePayload = z.infer<typeof agentTunnelClosePayloadSchema>;
953
+
868
954
  /**
869
955
  * CloudSync, serveur → agent. Le serveur orchestre tout : l'agent reçoit ses
870
956
  * assignations (`sync.config`), scanne sur ordre, transfère sur ordre.
@@ -922,10 +1008,25 @@ export const agentSyncPushPayloadSchema = z.object({
922
1008
  opId: syncOpId,
923
1009
  shareId: z.number().int().positive(),
924
1010
  relPath: syncRelPath,
925
- startOffset: z.number().int().nonnegative().default(0)
1011
+ startOffset: z.number().int().nonnegative().default(0),
1012
+ /**
1013
+ * Data frames the agent may have unacknowledged at once: it numbers its
1014
+ * frames (`seq`) and waits for a `sync.pushAck` before going past the
1015
+ * window. The server acks a frame once written, and paces its acks to cap
1016
+ * the upload rate. Absent (an older server), the agent streams freely.
1017
+ */
1018
+ window: z.number().int().positive().max(64).optional()
926
1019
  });
927
1020
  export type AgentSyncPushPayload = z.infer<typeof agentSyncPushPayloadSchema>;
928
1021
 
1022
+ /** One credit back for a windowed push: data frame `seq` of `opId` is written. */
1023
+ export const AGENT_SYNC_PUSH_ACK = 'sync.pushAck' as const;
1024
+ export const agentSyncPushAckPayloadSchema = z.object({
1025
+ opId: syncOpId,
1026
+ seq: z.number().int().nonnegative()
1027
+ });
1028
+ export type AgentSyncPushAckPayload = z.infer<typeof agentSyncPushAckPayloadSchema>;
1029
+
929
1030
  /**
930
1031
  * Un chunk de download à installer. hash/size/mtime sont répétés sur chaque
931
1032
  * frame (sans état) ; l'agent écrit dans un fichier temporaire, vérifie le
@@ -1196,6 +1297,22 @@ export const agentServerMessageSchema = z.discriminatedUnion('command', [
1196
1297
  command: z.literal(AGENT_FILES_ARCHIVE_CANCEL),
1197
1298
  payload: agentFilesArchiveCancelPayloadSchema
1198
1299
  }),
1300
+ z.object({
1301
+ command: z.literal(AGENT_TUNNEL_OPEN),
1302
+ payload: agentTunnelOpenPayloadSchema
1303
+ }),
1304
+ z.object({
1305
+ command: z.literal(AGENT_TUNNEL_WRITE),
1306
+ payload: agentTunnelWritePayloadSchema
1307
+ }),
1308
+ z.object({
1309
+ command: z.literal(AGENT_TUNNEL_CREDIT),
1310
+ payload: agentTunnelCreditPayloadSchema
1311
+ }),
1312
+ z.object({
1313
+ command: z.literal(AGENT_TUNNEL_CLOSE),
1314
+ payload: agentTunnelClosePayloadSchema
1315
+ }),
1199
1316
  z.object({
1200
1317
  command: z.literal(AGENT_SYNC_CONFIG),
1201
1318
  payload: agentSyncConfigPayloadSchema
@@ -1208,6 +1325,10 @@ export const agentServerMessageSchema = z.discriminatedUnion('command', [
1208
1325
  command: z.literal(AGENT_SYNC_PUSH),
1209
1326
  payload: agentSyncPushPayloadSchema
1210
1327
  }),
1328
+ z.object({
1329
+ command: z.literal(AGENT_SYNC_PUSH_ACK),
1330
+ payload: agentSyncPushAckPayloadSchema
1331
+ }),
1211
1332
  z.object({
1212
1333
  command: z.literal(AGENT_SYNC_APPLY_CHUNK),
1213
1334
  payload: agentSyncApplyChunkPayloadSchema
@@ -1241,8 +1362,8 @@ export type OrderSignature = z.infer<typeof orderSignatureSchema>;
1241
1362
  /**
1242
1363
  * The orders the server signs (`ORDER_SIGNING_KEY`) and the agent refuses
1243
1364
  * unsigned: what runs code, writes or deletes files, changes the agent's
1244
- * privileges or its life, or drives its containers. Mirror of
1245
- * `SIGNED_COMMANDS` in `agent/src/protocol.rs`.
1365
+ * privileges or its life, drives its containers, or reaches into its network.
1366
+ * Mirror of `SIGNED_COMMANDS` in `agent/src/protocol.rs`.
1246
1367
  */
1247
1368
  export const SIGNED_AGENT_COMMANDS: ReadonlySet<string> = new Set([
1248
1369
  AGENT_TERM_OPEN,
@@ -1253,7 +1374,8 @@ export const SIGNED_AGENT_COMMANDS: ReadonlySet<string> = new Set([
1253
1374
  AGENT_DESTROY,
1254
1375
  AGENT_PKG_UPGRADE,
1255
1376
  AGENT_LIFECYCLE,
1256
- AGENT_DOCKER_ACTION
1377
+ AGENT_DOCKER_ACTION,
1378
+ AGENT_TUNNEL_OPEN
1257
1379
  ]);
1258
1380
 
1259
1381
  /**
@@ -19,7 +19,10 @@ export const ErrorCodeSchema = z.enum([
19
19
  'locked',
20
20
  // The site, or the feature the command belongs to, is under maintenance
21
21
  // (see `domain/maintenance.ts`). The message is the one to show.
22
- 'maintenance'
22
+ 'maintenance',
23
+ // The server is full for this kind of account: the socket waits in line
24
+ // (see `domain/admission.ts`). `details` carries `{ position }`.
25
+ 'queued'
23
26
  ]);
24
27
 
25
28
  export type ErrorCode = z.infer<typeof ErrorCodeSchema>;
@@ -0,0 +1,115 @@
1
+ import assert from 'node:assert/strict';
2
+ import { test } from 'node:test';
3
+
4
+ import { accountExportProblem, exportTableRows, type SdkExportTable } from './accountExport';
5
+ import type { SdkQueryable } from './server';
6
+
7
+ const spec: SdkExportTable = { file: 'notes.json', where: 'workspace_id = ?', key: ['id'] };
8
+
9
+ test('accountExportProblem accepts a sound declaration and names what is wrong', () => {
10
+ assert.equal(
11
+ accountExportProblem({
12
+ tables: {
13
+ notes: { ...spec, sealed: ['content'], json: ['content'], omit: ['secret_enc'] },
14
+ note_cache: { skip: 'Un cache, reconstruit par la synchronisation.' },
15
+ notes_links: 'custom'
16
+ },
17
+ store: { omit: ['apiKey'] }
18
+ }),
19
+ null
20
+ );
21
+ const problem = (tables: Record<string, unknown>) =>
22
+ accountExportProblem({ tables: tables as never });
23
+ assert.match(problem({ 'bad name': spec }) ?? '', /not an identifier/);
24
+ assert.match(problem({ a: { skip: ' ' } }) ?? '', /a skip says why/);
25
+ assert.match(problem({ a: { ...spec, file: 'Notes.JSON' } }) ?? '', /file/);
26
+ assert.match(problem({ a: spec, b: spec }) ?? '', /write the same file/);
27
+ assert.equal(problem({ a: spec, b: { ...spec, scope: 'account' } }), null);
28
+ assert.match(problem({ a: { ...spec, where: 'a = ? AND b = ?' } }) ?? '', /exactly one \?/);
29
+ assert.match(problem({ a: { ...spec, where: 'a = ?; DROP TABLE x' } }) ?? '', /holds a ;/);
30
+ assert.match(
31
+ problem({ a: { ...spec, sealed: ['x'], omit: ['x'] } }) ?? '',
32
+ /both sealed and omitted/
33
+ );
34
+ });
35
+
36
+ function fakeTable(rows: Record<string, unknown>[]): { q: SdkQueryable; sql: string[] } {
37
+ const sql: string[] = [];
38
+ const q: SdkQueryable = {
39
+ async query<T extends object>(text: string, params: unknown[] = []): Promise<T[]> {
40
+ sql.push(text);
41
+ const after = params.length > 1 ? Number(params[1]) : -Infinity;
42
+ const limit = Number(/LIMIT (\d+)/.exec(text)?.[1]);
43
+ return rows.filter((r) => Number(r.id) > after).slice(0, limit) as T[];
44
+ },
45
+ async execute() {
46
+ return { affectedRows: 0, insertId: 0 };
47
+ }
48
+ };
49
+ return { q, sql };
50
+ }
51
+
52
+ test('exportTableRows pages by key, opens sealed cells, and never lets an omitted column out', async () => {
53
+ const rows = Array.from({ length: 450 }, (_, i) => ({
54
+ id: i + 1,
55
+ workspace_id: 3,
56
+ content: i === 7 ? 'broken' : `sealed:${JSON.stringify({ n: i })}`,
57
+ secret_enc: 'nope',
58
+ created: 1_790_000_000,
59
+ big: BigInt('12345678901234567890'),
60
+ raw: Buffer.from('hi')
61
+ }));
62
+ const { q, sql } = fakeTable(rows);
63
+ let unreadable = 0;
64
+ const out: Record<string, unknown>[] = [];
65
+ for await (const row of exportTableRows(
66
+ q,
67
+ 'notes',
68
+ {
69
+ ...spec,
70
+ sealed: ['content'],
71
+ json: ['content'],
72
+ omit: ['secret_enc'],
73
+ dates: { created: 's' }
74
+ },
75
+ 3,
76
+ async (blob) => (blob.startsWith('sealed:') ? blob.slice(7) : null),
77
+ () => unreadable++
78
+ )) {
79
+ out.push(row);
80
+ }
81
+ assert.equal(out.length, 450);
82
+ assert.deepEqual(out[0], {
83
+ id: 1,
84
+ workspace_id: 3,
85
+ content: { n: 0 },
86
+ created: new Date(1_790_000_000_000).toISOString(),
87
+ big: '12345678901234567890',
88
+ raw: { base64: Buffer.from('hi').toString('base64') }
89
+ });
90
+ assert.equal(out[7].content, null);
91
+ assert.equal(unreadable, 1);
92
+ assert.ok(sql.length >= 2, 'several pages');
93
+ assert.match(
94
+ sql[0],
95
+ /^SELECT \* FROM `notes` WHERE \(workspace_id = \?\) ORDER BY `id` LIMIT 200$/
96
+ );
97
+ assert.match(sql[1], /AND \(`id`\) > \(\?\)/);
98
+ });
99
+
100
+ test('exportTableRows writes an opened `_enc` column without its suffix, unless the name is taken', async () => {
101
+ const { q } = fakeTable([
102
+ { id: 1, workspace_id: 3, query_enc: 'sealed:a', title: 't', title_enc: 'sealed:b' }
103
+ ]);
104
+ const out: Record<string, unknown>[] = [];
105
+ for await (const row of exportTableRows(
106
+ q,
107
+ 'lookups',
108
+ { ...spec, sealed: ['query_enc', 'title_enc'] },
109
+ 3,
110
+ async (blob) => blob.slice(7)
111
+ )) {
112
+ out.push(row);
113
+ }
114
+ assert.deepEqual(out, [{ id: 1, workspace_id: 3, query: 'a', title: 't', title_enc: 'b' }]);
115
+ });