@tinyshed/tinystore 0.2.0 → 0.4.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.
@@ -56,6 +56,7 @@ export declare const methods: {
56
56
  readonly 'kv.expire': 264;
57
57
  readonly 'kv.clear': 265;
58
58
  readonly 'kv.list': 266;
59
+ readonly 'kv.reseal': 267;
59
60
  readonly 'kv.counters.open': 272;
60
61
  readonly 'kv.counters.add': 273;
61
62
  readonly 'kv.counters.get': 274;
@@ -180,6 +181,8 @@ export declare const BlobsOpen: import("./codec.ts").Message<{
180
181
  readonly ttl: readonly [2, import("./codec.ts").Codec<number, number>];
181
182
  /** the largest file a write may leave */
182
183
  readonly maxFileSize: readonly [3, import("./codec.ts").Codec<number, number>];
184
+ /** a database's handle: the files live in its file, and put, head, delete, copy and rename join its transactions; blobs.db when absent */
185
+ readonly database: readonly [4, import("./codec.ts").Codec<number, number>];
183
186
  }>;
184
187
  /**
185
188
  * Where a call stands: the files, a folder's segments, a path within it.
@@ -406,6 +409,8 @@ export declare const ConfigOpen: import("./codec.ts").Message<{
406
409
  readonly layers: readonly [3, import("./codec.ts").Codec<string, string>];
407
410
  /** a kept change that no longer fits refuses the open */
408
411
  readonly strict: readonly [4, import("./codec.ts").Codec<boolean, boolean>];
412
+ /** as kv.db keeps it, for a command: the last declaration a program opened it with, */
413
+ readonly kept: readonly [5, import("./codec.ts").Codec<boolean, boolean>];
409
414
  }>;
410
415
  /**
411
416
  * Where a value came from: its kind, and only the fields that kind names.
@@ -527,6 +532,8 @@ export declare const StoreOptions: import("./codec.ts").Message<{
527
532
  readonly withoutSystem: readonly [14, import("./codec.ts").Codec<boolean, boolean>];
528
533
  /** the metrics series the store holds at most; 100,000 when absent */
529
534
  readonly maxSeries: readonly [15, import("./codec.ts").Codec<number, number>];
535
+ /** the files of the store's older keys, which open the values they sealed and seal none */
536
+ readonly olderEncryptionKeys: readonly [16, import("./codec.ts").Codec<string[], readonly string[]>];
530
537
  }>;
531
538
  /**
532
539
  * The last frame of a connection.
@@ -951,6 +958,12 @@ export declare const KvWritten: import("./codec.ts").Message<{
951
958
  export declare const KvFound: import("./codec.ts").Message<{
952
959
  readonly found: readonly [1, import("./codec.ts").Codec<boolean, boolean>];
953
960
  }>;
961
+ /**
962
+ * How many values a reseal sealed again with the sealing key.
963
+ */
964
+ export declare const KvResealed: import("./codec.ts").Message<{
965
+ readonly count: readonly [1, import("./codec.ts").Codec<number, number>];
966
+ }>;
954
967
  /**
955
968
  * A branch of a handle, for a clear.
956
969
  */
@@ -1298,6 +1311,10 @@ export declare const MetricsQuery: import("./codec.ts").Message<{
1298
1311
  readonly from: readonly [4, import("./codec.ts").Codec<number, number>];
1299
1312
  /** excluded; now, a sample taken now in the range, unless given */
1300
1313
  readonly to: readonly [5, import("./codec.ts").Codec<number, number>];
1314
+ /** a page's samples or buckets at most, 100,000 at most: a page, which read and aggregate take */
1315
+ readonly limit: readonly [6, import("./codec.ts").Codec<number, number>];
1316
+ /** a page's next, with limit */
1317
+ readonly after: readonly [7, import("./codec.ts").Codec<string, string>];
1301
1318
  }>;
1302
1319
  /**
1303
1320
  * A series and its samples, or a part of them: a series longer than a body
@@ -1312,7 +1329,7 @@ export declare const MetricsSeries: import("./codec.ts").Message<{
1312
1329
  readonly times: readonly [4, import("./codec.ts").Codec<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>];
1313
1330
  /** a float64 a sample */
1314
1331
  readonly values: readonly [5, import("./codec.ts").Codec<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>];
1315
- /** the next part goes on with this series */
1332
+ /** the next part goes on with this series; on a page's last, the next page's first */
1316
1333
  readonly more: readonly [6, import("./codec.ts").Codec<boolean, boolean>];
1317
1334
  }>;
1318
1335
  export declare const MetricsRead: import("./codec.ts").Message<{
@@ -1325,7 +1342,7 @@ export declare const MetricsRead: import("./codec.ts").Message<{
1325
1342
  readonly times: readonly [4, import("./codec.ts").Codec<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>];
1326
1343
  /** a float64 a sample */
1327
1344
  readonly values: readonly [5, import("./codec.ts").Codec<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>];
1328
- /** the next part goes on with this series */
1345
+ /** the next part goes on with this series; on a page's last, the next page's first */
1329
1346
  readonly more: readonly [6, import("./codec.ts").Codec<boolean, boolean>];
1330
1347
  }>[], readonly import("./codec.ts").Written<{
1331
1348
  readonly name: readonly [1, import("./codec.ts").Codec<string, string>];
@@ -1336,9 +1353,11 @@ export declare const MetricsRead: import("./codec.ts").Message<{
1336
1353
  readonly times: readonly [4, import("./codec.ts").Codec<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>];
1337
1354
  /** a float64 a sample */
1338
1355
  readonly values: readonly [5, import("./codec.ts").Codec<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>];
1339
- /** the next part goes on with this series */
1356
+ /** the next part goes on with this series; on a page's last, the next page's first */
1340
1357
  readonly more: readonly [6, import("./codec.ts").Codec<boolean, boolean>];
1341
1358
  }>[]>];
1359
+ /** on the last part of a page that has more, to a request that gave limit */
1360
+ readonly next: readonly [2, import("./codec.ts").Codec<string, string>];
1342
1361
  }>;
1343
1362
  /**
1344
1363
  * A series' newest sample in the range.
@@ -1383,6 +1402,10 @@ export declare const MetricsAggregateAsked: import("./codec.ts").Message<{
1383
1402
  readonly from: readonly [4, import("./codec.ts").Codec<number, number>];
1384
1403
  /** excluded; now, a sample taken now in the range, unless given */
1385
1404
  readonly to: readonly [5, import("./codec.ts").Codec<number, number>];
1405
+ /** a page's samples or buckets at most, 100,000 at most: a page, which read and aggregate take */
1406
+ readonly limit: readonly [6, import("./codec.ts").Codec<number, number>];
1407
+ /** a page's next, with limit */
1408
+ readonly after: readonly [7, import("./codec.ts").Codec<string, string>];
1386
1409
  }>];
1387
1410
  readonly every: readonly [2, import("./codec.ts").Codec<number, number>];
1388
1411
  /** count, sum, avg, min, max, increase, rate, delta, first, last, or p and a rank; none for a read's plan */
@@ -1453,6 +1476,7 @@ export declare const MetricsGroup: import("./codec.ts").Message<{
1453
1476
  /** its first step began before the bucket before it */
1454
1477
  readonly gap: readonly [9, import("./codec.ts").Codec<boolean, boolean>];
1455
1478
  }>[]>];
1479
+ /** the next part goes on with this group; on a page's last, the next page's first */
1456
1480
  readonly more: readonly [4, import("./codec.ts").Codec<boolean, boolean>];
1457
1481
  }>;
1458
1482
  export declare const MetricsAggregates: import("./codec.ts").Message<{
@@ -1490,6 +1514,7 @@ export declare const MetricsAggregates: import("./codec.ts").Message<{
1490
1514
  /** its first step began before the bucket before it */
1491
1515
  readonly gap: readonly [9, import("./codec.ts").Codec<boolean, boolean>];
1492
1516
  }>[]>];
1517
+ /** the next part goes on with this group; on a page's last, the next page's first */
1493
1518
  readonly more: readonly [4, import("./codec.ts").Codec<boolean, boolean>];
1494
1519
  }>[], readonly import("./codec.ts").Written<{
1495
1520
  readonly name: readonly [1, import("./codec.ts").Codec<string, string>];
@@ -1525,8 +1550,11 @@ export declare const MetricsAggregates: import("./codec.ts").Message<{
1525
1550
  /** its first step began before the bucket before it */
1526
1551
  readonly gap: readonly [9, import("./codec.ts").Codec<boolean, boolean>];
1527
1552
  }>[]>];
1553
+ /** the next part goes on with this group; on a page's last, the next page's first */
1528
1554
  readonly more: readonly [4, import("./codec.ts").Codec<boolean, boolean>];
1529
1555
  }>[]>];
1556
+ /** on the last part of a page that has more, to a request that gave limit */
1557
+ readonly next: readonly [2, import("./codec.ts").Codec<string, string>];
1530
1558
  }>;
1531
1559
  export declare const MetricsExceeds: import("./codec.ts").Message<{
1532
1560
  /** series, bytes read, samples decoded or answer */
@@ -57,6 +57,7 @@ export const methods = {
57
57
  'kv.expire': 0x0108,
58
58
  'kv.clear': 0x0109,
59
59
  'kv.list': 0x010a,
60
+ 'kv.reseal': 0x010b,
60
61
  'kv.counters.open': 0x0110,
61
62
  'kv.counters.add': 0x0111,
62
63
  'kv.counters.get': 0x0112,
@@ -291,6 +292,8 @@ export const BlobsOpen = message('blobs.Open', {
291
292
  ttl: [2, uint],
292
293
  /** the largest file a write may leave */
293
294
  maxFileSize: [3, uint],
295
+ /** a database's handle: the files live in its file, and put, head, delete, copy and rename join its transactions; blobs.db when absent */
296
+ database: [4, uint],
294
297
  }, {
295
298
  write(w, v) {
296
299
  const head = w.openMap();
@@ -310,12 +313,18 @@ export const BlobsOpen = message('blobs.Open', {
310
313
  w.uint(v.maxFileSize);
311
314
  n++;
312
315
  }
316
+ if (v.database !== undefined) {
317
+ w.field(4);
318
+ w.uint(v.database);
319
+ n++;
320
+ }
313
321
  w.closeMap(head, n);
314
322
  },
315
323
  read(r) {
316
324
  let $name;
317
325
  let $ttl;
318
326
  let $maxFileSize;
327
+ let $database;
319
328
  for (let n = r.message(); n > 0; n--) {
320
329
  switch (r.field()) {
321
330
  case 1:
@@ -327,12 +336,15 @@ export const BlobsOpen = message('blobs.Open', {
327
336
  case 3:
328
337
  $maxFileSize = r.uint();
329
338
  break;
339
+ case 4:
340
+ $database = r.uint();
341
+ break;
330
342
  default:
331
343
  r.skip();
332
344
  }
333
345
  }
334
346
  r.leave();
335
- return { name: $name, ttl: $ttl, maxFileSize: $maxFileSize };
347
+ return { name: $name, ttl: $ttl, maxFileSize: $maxFileSize, database: $database };
336
348
  },
337
349
  });
338
350
  /**
@@ -1524,6 +1536,8 @@ export const ConfigOpen = message('config.Open', {
1524
1536
  layers: [3, str],
1525
1537
  /** a kept change that no longer fits refuses the open */
1526
1538
  strict: [4, bool],
1539
+ /** as kv.db keeps it, for a command: the last declaration a program opened it with, */
1540
+ kept: [5, bool],
1527
1541
  }, {
1528
1542
  write(w, v) {
1529
1543
  const head = w.openMap();
@@ -1548,6 +1562,11 @@ export const ConfigOpen = message('config.Open', {
1548
1562
  w.bool(v.strict);
1549
1563
  n++;
1550
1564
  }
1565
+ if (v.kept !== undefined) {
1566
+ w.field(5);
1567
+ w.bool(v.kept);
1568
+ n++;
1569
+ }
1551
1570
  w.closeMap(head, n);
1552
1571
  },
1553
1572
  read(r) {
@@ -1555,6 +1574,7 @@ export const ConfigOpen = message('config.Open', {
1555
1574
  let $declaration;
1556
1575
  let $layers;
1557
1576
  let $strict;
1577
+ let $kept;
1558
1578
  for (let n = r.message(); n > 0; n--) {
1559
1579
  switch (r.field()) {
1560
1580
  case 1:
@@ -1569,12 +1589,21 @@ export const ConfigOpen = message('config.Open', {
1569
1589
  case 4:
1570
1590
  $strict = r.bool();
1571
1591
  break;
1592
+ case 5:
1593
+ $kept = r.bool();
1594
+ break;
1572
1595
  default:
1573
1596
  r.skip();
1574
1597
  }
1575
1598
  }
1576
1599
  r.leave();
1577
- return { name: $name, declaration: $declaration, layers: $layers, strict: $strict };
1600
+ return {
1601
+ name: $name,
1602
+ declaration: $declaration,
1603
+ layers: $layers,
1604
+ strict: $strict,
1605
+ kept: $kept,
1606
+ };
1578
1607
  },
1579
1608
  });
1580
1609
  /**
@@ -2094,6 +2123,8 @@ export const StoreOptions = message('store.Options', {
2094
2123
  withoutSystem: [14, bool],
2095
2124
  /** the metrics series the store holds at most; 100,000 when absent */
2096
2125
  maxSeries: [15, uint],
2126
+ /** the files of the store's older keys, which open the values they sealed and seal none */
2127
+ olderEncryptionKeys: [16, list(str)],
2097
2128
  }, {
2098
2129
  write(w, v) {
2099
2130
  const head = w.openMap();
@@ -2182,6 +2213,14 @@ export const StoreOptions = message('store.Options', {
2182
2213
  w.uint(v.maxSeries);
2183
2214
  n++;
2184
2215
  }
2216
+ if (v.olderEncryptionKeys !== undefined) {
2217
+ w.field(16);
2218
+ w.array(v.olderEncryptionKeys.length);
2219
+ for (const item0 of v.olderEncryptionKeys) {
2220
+ w.str(item0);
2221
+ }
2222
+ n++;
2223
+ }
2185
2224
  w.closeMap(head, n);
2186
2225
  },
2187
2226
  read(r) {
@@ -2200,6 +2239,7 @@ export const StoreOptions = message('store.Options', {
2200
2239
  let $selfMetrics;
2201
2240
  let $withoutSystem;
2202
2241
  let $maxSeries;
2242
+ let $olderEncryptionKeys;
2203
2243
  for (let n = r.message(); n > 0; n--) {
2204
2244
  switch (r.field()) {
2205
2245
  case 1:
@@ -2265,6 +2305,15 @@ export const StoreOptions = message('store.Options', {
2265
2305
  case 15:
2266
2306
  $maxSeries = r.uint();
2267
2307
  break;
2308
+ case 16: {
2309
+ const items0 = [];
2310
+ for (let i0 = r.array(); i0 > 0; i0--) {
2311
+ items0.push(r.str());
2312
+ }
2313
+ r.leave();
2314
+ $olderEncryptionKeys = items0;
2315
+ break;
2316
+ }
2268
2317
  default:
2269
2318
  r.skip();
2270
2319
  }
@@ -2286,6 +2335,7 @@ export const StoreOptions = message('store.Options', {
2286
2335
  selfMetrics: $selfMetrics,
2287
2336
  withoutSystem: $withoutSystem,
2288
2337
  maxSeries: $maxSeries,
2338
+ olderEncryptionKeys: $olderEncryptionKeys,
2289
2339
  };
2290
2340
  },
2291
2341
  });
@@ -4128,6 +4178,37 @@ export const KvFound = message('kv.Found', {
4128
4178
  return { found: $found };
4129
4179
  },
4130
4180
  });
4181
+ /**
4182
+ * How many values a reseal sealed again with the sealing key.
4183
+ */
4184
+ export const KvResealed = message('kv.Resealed', {
4185
+ count: [1, uint],
4186
+ }, {
4187
+ write(w, v) {
4188
+ const head = w.openMap();
4189
+ let n = 0;
4190
+ if (v.count !== undefined) {
4191
+ w.field(1);
4192
+ w.uint(v.count);
4193
+ n++;
4194
+ }
4195
+ w.closeMap(head, n);
4196
+ },
4197
+ read(r) {
4198
+ let $count;
4199
+ for (let n = r.message(); n > 0; n--) {
4200
+ switch (r.field()) {
4201
+ case 1:
4202
+ $count = r.uint();
4203
+ break;
4204
+ default:
4205
+ r.skip();
4206
+ }
4207
+ }
4208
+ r.leave();
4209
+ return { count: $count };
4210
+ },
4211
+ });
4131
4212
  /**
4132
4213
  * A branch of a handle, for a clear.
4133
4214
  */
@@ -5399,6 +5480,10 @@ export const MetricsQuery = message('metrics.Query', {
5399
5480
  from: [4, int],
5400
5481
  /** excluded; now, a sample taken now in the range, unless given */
5401
5482
  to: [5, int],
5483
+ /** a page's samples or buckets at most, 100,000 at most: a page, which read and aggregate take */
5484
+ limit: [6, uint],
5485
+ /** a page's next, with limit */
5486
+ after: [7, str],
5402
5487
  }, {
5403
5488
  write(w, v) {
5404
5489
  const head = w.openMap();
@@ -5428,6 +5513,16 @@ export const MetricsQuery = message('metrics.Query', {
5428
5513
  w.int(v.to);
5429
5514
  n++;
5430
5515
  }
5516
+ if (v.limit !== undefined) {
5517
+ w.field(6);
5518
+ w.uint(v.limit);
5519
+ n++;
5520
+ }
5521
+ if (v.after !== undefined) {
5522
+ w.field(7);
5523
+ w.str(v.after);
5524
+ n++;
5525
+ }
5431
5526
  w.closeMap(head, n);
5432
5527
  },
5433
5528
  read(r) {
@@ -5436,6 +5531,8 @@ export const MetricsQuery = message('metrics.Query', {
5436
5531
  let $since;
5437
5532
  let $from;
5438
5533
  let $to;
5534
+ let $limit;
5535
+ let $after;
5439
5536
  for (let n = r.message(); n > 0; n--) {
5440
5537
  switch (r.field()) {
5441
5538
  case 1:
@@ -5453,12 +5550,26 @@ export const MetricsQuery = message('metrics.Query', {
5453
5550
  case 5:
5454
5551
  $to = r.int();
5455
5552
  break;
5553
+ case 6:
5554
+ $limit = r.uint();
5555
+ break;
5556
+ case 7:
5557
+ $after = r.str();
5558
+ break;
5456
5559
  default:
5457
5560
  r.skip();
5458
5561
  }
5459
5562
  }
5460
5563
  r.leave();
5461
- return { name: $name, where: $where, since: $since, from: $from, to: $to };
5564
+ return {
5565
+ name: $name,
5566
+ where: $where,
5567
+ since: $since,
5568
+ from: $from,
5569
+ to: $to,
5570
+ limit: $limit,
5571
+ after: $after,
5572
+ };
5462
5573
  },
5463
5574
  });
5464
5575
  /**
@@ -5474,7 +5585,7 @@ export const MetricsSeries = message('metrics.Series', {
5474
5585
  times: [4, bin],
5475
5586
  /** a float64 a sample */
5476
5587
  values: [5, bin],
5477
- /** the next part goes on with this series */
5588
+ /** the next part goes on with this series; on a page's last, the next page's first */
5478
5589
  more: [6, bool],
5479
5590
  }, {
5480
5591
  write(w, v) {
@@ -5556,6 +5667,8 @@ export const MetricsSeries = message('metrics.Series', {
5556
5667
  });
5557
5668
  export const MetricsRead = message('metrics.Read', {
5558
5669
  series: [1, list(MetricsSeries)],
5670
+ /** on the last part of a page that has more, to a request that gave limit */
5671
+ next: [2, str],
5559
5672
  }, {
5560
5673
  write(w, v) {
5561
5674
  const head = w.openMap();
@@ -5568,10 +5681,16 @@ export const MetricsRead = message('metrics.Read', {
5568
5681
  }
5569
5682
  n++;
5570
5683
  }
5684
+ if (v.next !== undefined) {
5685
+ w.field(2);
5686
+ w.str(v.next);
5687
+ n++;
5688
+ }
5571
5689
  w.closeMap(head, n);
5572
5690
  },
5573
5691
  read(r) {
5574
5692
  let $series;
5693
+ let $next;
5575
5694
  for (let n = r.message(); n > 0; n--) {
5576
5695
  switch (r.field()) {
5577
5696
  case 1: {
@@ -5583,12 +5702,15 @@ export const MetricsRead = message('metrics.Read', {
5583
5702
  $series = items0;
5584
5703
  break;
5585
5704
  }
5705
+ case 2:
5706
+ $next = r.str();
5707
+ break;
5586
5708
  default:
5587
5709
  r.skip();
5588
5710
  }
5589
5711
  }
5590
5712
  r.leave();
5591
- return { series: $series };
5713
+ return { series: $series, next: $next };
5592
5714
  },
5593
5715
  });
5594
5716
  /**
@@ -5957,6 +6079,7 @@ export const MetricsGroup = message('metrics.Group', {
5957
6079
  name: [1, str],
5958
6080
  labels: [2, names(str)],
5959
6081
  buckets: [3, list(MetricsBucket)],
6082
+ /** the next part goes on with this group; on a page's last, the next page's first */
5960
6083
  more: [4, bool],
5961
6084
  }, {
5962
6085
  write(w, v) {
@@ -6022,6 +6145,8 @@ export const MetricsGroup = message('metrics.Group', {
6022
6145
  });
6023
6146
  export const MetricsAggregates = message('metrics.Aggregates', {
6024
6147
  groups: [1, list(MetricsGroup)],
6148
+ /** on the last part of a page that has more, to a request that gave limit */
6149
+ next: [2, str],
6025
6150
  }, {
6026
6151
  write(w, v) {
6027
6152
  const head = w.openMap();
@@ -6034,10 +6159,16 @@ export const MetricsAggregates = message('metrics.Aggregates', {
6034
6159
  }
6035
6160
  n++;
6036
6161
  }
6162
+ if (v.next !== undefined) {
6163
+ w.field(2);
6164
+ w.str(v.next);
6165
+ n++;
6166
+ }
6037
6167
  w.closeMap(head, n);
6038
6168
  },
6039
6169
  read(r) {
6040
6170
  let $groups;
6171
+ let $next;
6041
6172
  for (let n = r.message(); n > 0; n--) {
6042
6173
  switch (r.field()) {
6043
6174
  case 1: {
@@ -6049,12 +6180,15 @@ export const MetricsAggregates = message('metrics.Aggregates', {
6049
6180
  $groups = items0;
6050
6181
  break;
6051
6182
  }
6183
+ case 2:
6184
+ $next = r.str();
6185
+ break;
6052
6186
  default:
6053
6187
  r.skip();
6054
6188
  }
6055
6189
  }
6056
6190
  r.leave();
6057
- return { groups: $groups };
6191
+ return { groups: $groups, next: $next };
6058
6192
  },
6059
6193
  });
6060
6194
  export const MetricsExceeds = message('metrics.Exceeds', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyshed/tinystore",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "TinyStore for Bun and Node: kv, jobs, blobs, SQL, records and metrics in one directory, served by a sidecar",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://tinyshed.org/tinystore",
@@ -31,12 +31,12 @@
31
31
  "README.md"
32
32
  ],
33
33
  "optionalDependencies": {
34
- "@tinyshed/tinystore-linux-x64": "0.2.0",
35
- "@tinyshed/tinystore-linux-arm64": "0.2.0",
36
- "@tinyshed/tinystore-linux-x64-musl": "0.2.0",
37
- "@tinyshed/tinystore-linux-arm64-musl": "0.2.0",
38
- "@tinyshed/tinystore-darwin-arm64": "0.2.0",
39
- "@tinyshed/tinystore-darwin-x64": "0.2.0",
40
- "@tinyshed/tinystore-win32-x64": "0.2.0"
34
+ "@tinyshed/tinystore-linux-x64": "0.4.0",
35
+ "@tinyshed/tinystore-linux-arm64": "0.4.0",
36
+ "@tinyshed/tinystore-linux-x64-musl": "0.4.0",
37
+ "@tinyshed/tinystore-linux-arm64-musl": "0.4.0",
38
+ "@tinyshed/tinystore-darwin-arm64": "0.4.0",
39
+ "@tinyshed/tinystore-darwin-x64": "0.4.0",
40
+ "@tinyshed/tinystore-win32-x64": "0.4.0"
41
41
  }
42
42
  }
package/src/blobs.ts CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { type Connection, type Idempotence, type Link, pieceOf } from './connection.ts'
7
7
  import { ClosedError, InvalidError, OutcomeUnknownError } from './errors.ts'
8
- import { checkName, handleOn, type Via, viaLink } from './handles.ts'
8
+ import { checkName, type Home, handleOn, type Open, type Via, viaLink } from './handles.ts'
9
9
  import { ConnectionLostError, type Stream, watch } from './session.ts'
10
10
  import { type Duration, dateOf, ms, unixMs } from './time.ts'
11
11
  import {
@@ -96,13 +96,35 @@ const nothing = new Uint8Array(0)
96
96
 
97
97
  /** Files as `store.files` opens them. */
98
98
  export function openFiles(link: Link, name: string, options?: FilesOptions): Files {
99
+ return new Files(link, name, filesOpen(name, options), [])
100
+ }
101
+
102
+ /** The open of a set of files, kept in the database of the handle `database` when it is given. */
103
+ export function filesOpen(name: string, options?: FilesOptions, database?: number): Uint8Array {
99
104
  checkName(name, 'files')
100
- const open = BlobsOpen.encode({
105
+ return BlobsOpen.encode({
101
106
  name,
102
107
  ttl: options?.ttl === undefined ? undefined : ms(options.ttl),
103
108
  maxFileSize: options?.maxFileSize === undefined ? undefined : bytesOf(options.maxFileSize),
109
+ database,
104
110
  })
105
- return new Files(link, name, open, [])
111
+ }
112
+
113
+ /** What a transaction needs of a set of files it takes in. */
114
+ export interface FilesParts {
115
+ readonly name: string
116
+ readonly open: Open
117
+ /** the database whose file keeps the files; the store's own when undefined */
118
+ readonly home: Home | undefined
119
+ /** the same files, their calls through the transaction's `via` */
120
+ inside(via: Via): Files
121
+ }
122
+
123
+ // a set of files' parts, kept out of its public shape, for db.tx
124
+ const parts = new WeakMap<object, FilesParts>()
125
+
126
+ export function filesParts(handle: object): FilesParts | undefined {
127
+ return parts.get(handle)
106
128
  }
107
129
 
108
130
  /**
@@ -113,24 +135,49 @@ export class Files {
113
135
  readonly name: string
114
136
  readonly #link: Link
115
137
  readonly #via: Via
116
- readonly #open: Uint8Array
138
+ readonly #open: Open
117
139
  readonly #folder: string[]
140
+ readonly #home: Home | undefined
141
+ /** whether its calls go through a database's transaction */
142
+ readonly #inside: boolean
118
143
 
119
- /** Files come from `store.files`. */
120
- constructor(link: Link, name: string, open: Uint8Array, folder: string[]) {
144
+ /**
145
+ * Files come from `store.files` and `db.files`; inside a database's
146
+ * transaction, from `tx.with(files)`, whose writes go through the
147
+ * transaction.
148
+ */
149
+ constructor(link: Link, name: string, open: Open, folder: string[], home?: Home, via?: Via) {
121
150
  this.name = name
122
151
  this.#link = link
123
- this.#via = viaLink(link)
152
+ this.#via = via ?? home?.via(`files ${name}`) ?? viaLink(link)
124
153
  this.#open = open
125
154
  this.#folder = folder
155
+ this.#home = home
156
+ this.#inside = via !== undefined
157
+ const inside = (through: Via) => new Files(link, name, open, folder, home, through)
158
+ parts.set(this, { name, open, home, inside })
126
159
  }
127
160
 
128
161
  /** The folder of these segments, one each: `folder('users', 42)` is users/42, never users/420. */
129
162
  folder(...segments: (string | number | bigint)[]): Files {
130
- return new Files(this.#link, this.name, this.#open, [
131
- ...this.#folder,
132
- ...segments.map(segmentOf),
133
- ])
163
+ const folder = [...this.#folder, ...segments.map(segmentOf)]
164
+ return new Files(
165
+ this.#link,
166
+ this.name,
167
+ this.#open,
168
+ folder,
169
+ this.#home,
170
+ this.#inside ? this.#via : undefined,
171
+ )
172
+ }
173
+
174
+ /** Refuses what streams a file's bytes inside a transaction, which takes a put's bytes whole. */
175
+ #outside(what: string): void {
176
+ if (this.#inside) {
177
+ throw new InvalidError(
178
+ `files ${this.name}: ${what} inside a transaction: put a file's bytes whole, and read them after its commit`,
179
+ )
180
+ }
134
181
  }
135
182
 
136
183
  /**
@@ -277,6 +324,7 @@ export class Files {
277
324
  * leaving the block without commit aborts it.
278
325
  */
279
326
  async upload(path: string, options?: FileOptions): Promise<Upload> {
327
+ this.#outside('an upload')
280
328
  const connection = await this.#link.connection()
281
329
  const write = BlobsWrite.encode({
282
330
  ...this.#fields(path, options),
@@ -396,6 +444,15 @@ export class Files {
396
444
  size: options?.size ?? source.size,
397
445
  create,
398
446
  }
447
+ if (this.#inside) {
448
+ const bytes = await allOf(source)
449
+ const answer = await this.#call(
450
+ 'blobs.put',
451
+ handle => BlobsWrite.encode({ ...fields, handle, bytes }),
452
+ 'write',
453
+ )
454
+ return writtenOf(answer)
455
+ }
399
456
  return this.#link.run(
400
457
  'write',
401
458
  async connection => {
@@ -688,6 +745,26 @@ function sourceOf(body: Body): Source {
688
745
  }
689
746
  }
690
747
 
748
+ /** Every byte of a body at once, as a put inside a transaction sends it. */
749
+ async function allOf(source: Source): Promise<Uint8Array> {
750
+ const chunks: Uint8Array[] = []
751
+ let size = 0
752
+ for await (const chunk of source.chunks()) {
753
+ chunks.push(chunk)
754
+ size += chunk.length
755
+ }
756
+ if (chunks.length === 1) {
757
+ return chunks[0] as Uint8Array
758
+ }
759
+ const all = new Uint8Array(size)
760
+ let at = 0
761
+ for (const chunk of chunks) {
762
+ all.set(chunk, at)
763
+ at += chunk.length
764
+ }
765
+ return all
766
+ }
767
+
691
768
  /**
692
769
  * The bytes of chunks again in pieces of `size` bytes, the last shorter. A
693
770
  * chunk that holds a whole piece goes as it is, without a copy.