@tinyshed/tinystore 0.1.1 → 0.2.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.
@@ -98,6 +98,13 @@ export const methods = {
98
98
  'metrics.flush': 0x060d,
99
99
  'metrics.instruments': 0x060e,
100
100
  'metrics.ingestPrometheus': 0x060f,
101
+ /**
102
+ * Answers its RESPONSE at once; the store's memory for the whole text is
103
+ * taken before a piece is kept, and the last piece is answered once the text
104
+ * is written, every series or none. A cancel or a lost connection leaves
105
+ * nothing written and lets go of what came.
106
+ */
107
+ 'metrics.uploadPrometheus': 0x0610,
101
108
  'records.open': 0x0501,
102
109
  'records.append': 0x0502,
103
110
  'records.list': 0x0503,
@@ -2569,6 +2576,8 @@ export const JobsQueueOpen = message('jobs.QueueOpen', {
2569
2576
  maxWaiting: [9, uint],
2570
2577
  /** a database's handle: the queue lives in its file; jobs.db when absent */
2571
2578
  database: [10, uint],
2579
+ /** each group's starts, beside the queue's rate */
2580
+ groupRate: [11, JobsRate],
2572
2581
  }, {
2573
2582
  write(w, v) {
2574
2583
  const head = w.openMap();
@@ -2623,6 +2632,11 @@ export const JobsQueueOpen = message('jobs.QueueOpen', {
2623
2632
  w.uint(v.database);
2624
2633
  n++;
2625
2634
  }
2635
+ if (v.groupRate !== undefined) {
2636
+ w.field(11);
2637
+ JobsRate.write(w, v.groupRate);
2638
+ n++;
2639
+ }
2626
2640
  w.closeMap(head, n);
2627
2641
  },
2628
2642
  read(r) {
@@ -2636,6 +2650,7 @@ export const JobsQueueOpen = message('jobs.QueueOpen', {
2636
2650
  let $keep;
2637
2651
  let $maxWaiting;
2638
2652
  let $database;
2653
+ let $groupRate;
2639
2654
  for (let n = r.message(); n > 0; n--) {
2640
2655
  switch (r.field()) {
2641
2656
  case 1:
@@ -2668,6 +2683,9 @@ export const JobsQueueOpen = message('jobs.QueueOpen', {
2668
2683
  case 10:
2669
2684
  $database = r.uint();
2670
2685
  break;
2686
+ case 11:
2687
+ $groupRate = JobsRate.read(r);
2688
+ break;
2671
2689
  default:
2672
2690
  r.skip();
2673
2691
  }
@@ -2684,6 +2702,7 @@ export const JobsQueueOpen = message('jobs.QueueOpen', {
2684
2702
  keep: $keep,
2685
2703
  maxWaiting: $maxWaiting,
2686
2704
  database: $database,
2705
+ groupRate: $groupRate,
2687
2706
  };
2688
2707
  },
2689
2708
  });
@@ -5684,6 +5703,10 @@ export const MetricsAggregateAsked = message('metrics.AggregateAsked', {
5684
5703
  without: [5, list(str)],
5685
5704
  /** a timer's percentile also gives a point between its bounds */
5686
5705
  estimate: [6, bool],
5706
+ /** day, week or month: buckets of a calendar in timeZone, in place of every */
5707
+ calendar: [7, str],
5708
+ /** an IANA name, UTC among them, which calendar takes */
5709
+ timeZone: [8, str],
5687
5710
  }, {
5688
5711
  write(w, v) {
5689
5712
  const head = w.openMap();
@@ -5724,6 +5747,16 @@ export const MetricsAggregateAsked = message('metrics.AggregateAsked', {
5724
5747
  w.bool(v.estimate);
5725
5748
  n++;
5726
5749
  }
5750
+ if (v.calendar !== undefined) {
5751
+ w.field(7);
5752
+ w.str(v.calendar);
5753
+ n++;
5754
+ }
5755
+ if (v.timeZone !== undefined) {
5756
+ w.field(8);
5757
+ w.str(v.timeZone);
5758
+ n++;
5759
+ }
5727
5760
  w.closeMap(head, n);
5728
5761
  },
5729
5762
  read(r) {
@@ -5733,6 +5766,8 @@ export const MetricsAggregateAsked = message('metrics.AggregateAsked', {
5733
5766
  let $by;
5734
5767
  let $without;
5735
5768
  let $estimate;
5769
+ let $calendar;
5770
+ let $timeZone;
5736
5771
  for (let n = r.message(); n > 0; n--) {
5737
5772
  switch (r.field()) {
5738
5773
  case 1:
@@ -5765,6 +5800,12 @@ export const MetricsAggregateAsked = message('metrics.AggregateAsked', {
5765
5800
  case 6:
5766
5801
  $estimate = r.bool();
5767
5802
  break;
5803
+ case 7:
5804
+ $calendar = r.str();
5805
+ break;
5806
+ case 8:
5807
+ $timeZone = r.str();
5808
+ break;
5768
5809
  default:
5769
5810
  r.skip();
5770
5811
  }
@@ -5777,6 +5818,8 @@ export const MetricsAggregateAsked = message('metrics.AggregateAsked', {
5777
5818
  by: $by,
5778
5819
  without: $without,
5779
5820
  estimate: $estimate,
5821
+ calendar: $calendar,
5822
+ timeZone: $timeZone,
5780
5823
  };
5781
5824
  },
5782
5825
  });
@@ -6260,6 +6303,100 @@ export const MetricsExposition = message('metrics.Exposition', {
6260
6303
  return { text: $text, labels: $labels, at: $at, keep: $keep };
6261
6304
  },
6262
6305
  });
6306
+ /**
6307
+ * A text past what one message holds: what metrics.Exposition carries but
6308
+ * the text, whose bytes follow as the client's DATA, a metrics.Piece each.
6309
+ */
6310
+ export const MetricsUpload = message('metrics.Upload', {
6311
+ /** the text's bytes, 32 MiB at most: pieces that come to more or fewer are refused */
6312
+ size: [1, uint],
6313
+ labels: [2, names(str)],
6314
+ at: [3, int],
6315
+ keep: [4, uint],
6316
+ }, {
6317
+ write(w, v) {
6318
+ const head = w.openMap();
6319
+ let n = 0;
6320
+ if (v.size !== undefined) {
6321
+ w.field(1);
6322
+ w.uint(v.size);
6323
+ n++;
6324
+ }
6325
+ if (v.labels !== undefined) {
6326
+ w.field(2);
6327
+ writeNames(w, v.labels, str.write);
6328
+ n++;
6329
+ }
6330
+ if (v.at !== undefined) {
6331
+ w.field(3);
6332
+ w.int(v.at);
6333
+ n++;
6334
+ }
6335
+ if (v.keep !== undefined) {
6336
+ w.field(4);
6337
+ w.uint(v.keep);
6338
+ n++;
6339
+ }
6340
+ w.closeMap(head, n);
6341
+ },
6342
+ read(r) {
6343
+ let $size;
6344
+ let $labels;
6345
+ let $at;
6346
+ let $keep;
6347
+ for (let n = r.message(); n > 0; n--) {
6348
+ switch (r.field()) {
6349
+ case 1:
6350
+ $size = r.uint();
6351
+ break;
6352
+ case 2:
6353
+ $labels = readNames(r, str.read);
6354
+ break;
6355
+ case 3:
6356
+ $at = r.int();
6357
+ break;
6358
+ case 4:
6359
+ $keep = r.uint();
6360
+ break;
6361
+ default:
6362
+ r.skip();
6363
+ }
6364
+ }
6365
+ r.leave();
6366
+ return { size: $size, labels: $labels, at: $at, keep: $keep };
6367
+ },
6368
+ });
6369
+ /**
6370
+ * A piece of an uploaded text, cut anywhere.
6371
+ */
6372
+ export const MetricsPiece = message('metrics.Piece', {
6373
+ bytes: [1, bin],
6374
+ }, {
6375
+ write(w, v) {
6376
+ const head = w.openMap();
6377
+ let n = 0;
6378
+ if (v.bytes !== undefined) {
6379
+ w.field(1);
6380
+ w.bin(v.bytes);
6381
+ n++;
6382
+ }
6383
+ w.closeMap(head, n);
6384
+ },
6385
+ read(r) {
6386
+ let $bytes;
6387
+ for (let n = r.message(); n > 0; n--) {
6388
+ switch (r.field()) {
6389
+ case 1:
6390
+ $bytes = r.bytes();
6391
+ break;
6392
+ default:
6393
+ r.skip();
6394
+ }
6395
+ }
6396
+ r.leave();
6397
+ return { bytes: $bytes };
6398
+ },
6399
+ });
6263
6400
  /**
6264
6401
  * A page of names, of a name's labels or of a label's values.
6265
6402
  */
@@ -7398,6 +7535,8 @@ export const RecordsNewEvent = message('records.NewEvent', {
7398
7535
  traceId: [7, bin],
7399
7536
  /** 8 bytes */
7400
7537
  spanId: [8, bin],
7538
+ /** 1 to 128 bytes: an event of an id its stream holds is written as nothing */
7539
+ id: [9, str],
7401
7540
  }, {
7402
7541
  write(w, v) {
7403
7542
  const head = w.openMap();
@@ -7442,6 +7581,11 @@ export const RecordsNewEvent = message('records.NewEvent', {
7442
7581
  w.bin(v.spanId);
7443
7582
  n++;
7444
7583
  }
7584
+ if (v.id !== undefined) {
7585
+ w.field(9);
7586
+ w.str(v.id);
7587
+ n++;
7588
+ }
7445
7589
  w.closeMap(head, n);
7446
7590
  },
7447
7591
  read(r) {
@@ -7453,6 +7597,7 @@ export const RecordsNewEvent = message('records.NewEvent', {
7453
7597
  let $context;
7454
7598
  let $traceId;
7455
7599
  let $spanId;
7600
+ let $id;
7456
7601
  for (let n = r.message(); n > 0; n--) {
7457
7602
  switch (r.field()) {
7458
7603
  case 1:
@@ -7479,6 +7624,9 @@ export const RecordsNewEvent = message('records.NewEvent', {
7479
7624
  case 8:
7480
7625
  $spanId = r.bytes();
7481
7626
  break;
7627
+ case 9:
7628
+ $id = r.str();
7629
+ break;
7482
7630
  default:
7483
7631
  r.skip();
7484
7632
  }
@@ -7493,6 +7641,7 @@ export const RecordsNewEvent = message('records.NewEvent', {
7493
7641
  context: $context,
7494
7642
  traceId: $traceId,
7495
7643
  spanId: $spanId,
7644
+ id: $id,
7496
7645
  };
7497
7646
  },
7498
7647
  });
@@ -7546,6 +7695,39 @@ export const RecordsAppend = message('records.Append', {
7546
7695
  return { handle: $handle, events: $events };
7547
7696
  },
7548
7697
  });
7698
+ /**
7699
+ * What an append wrote: none of an event whose id its stream held, or an
7700
+ * earlier event of the append held.
7701
+ */
7702
+ export const RecordsAppended = message('records.Appended', {
7703
+ /** the events written as nothing */
7704
+ held: [1, uint],
7705
+ }, {
7706
+ write(w, v) {
7707
+ const head = w.openMap();
7708
+ let n = 0;
7709
+ if (v.held !== undefined) {
7710
+ w.field(1);
7711
+ w.uint(v.held);
7712
+ n++;
7713
+ }
7714
+ w.closeMap(head, n);
7715
+ },
7716
+ read(r) {
7717
+ let $held;
7718
+ for (let n = r.message(); n > 0; n--) {
7719
+ switch (r.field()) {
7720
+ case 1:
7721
+ $held = r.uint();
7722
+ break;
7723
+ default:
7724
+ r.skip();
7725
+ }
7726
+ }
7727
+ r.leave();
7728
+ return { held: $held };
7729
+ },
7730
+ });
7549
7731
  /**
7550
7732
  * An event as a read gives it.
7551
7733
  */
@@ -7560,6 +7742,7 @@ export const RecordsEvent = message('records.Event', {
7560
7742
  context: [7, str],
7561
7743
  traceId: [8, bin],
7562
7744
  spanId: [9, bin],
7745
+ id: [10, str],
7563
7746
  }, {
7564
7747
  write(w, v) {
7565
7748
  const head = w.openMap();
@@ -7609,6 +7792,11 @@ export const RecordsEvent = message('records.Event', {
7609
7792
  w.bin(v.spanId);
7610
7793
  n++;
7611
7794
  }
7795
+ if (v.id !== undefined) {
7796
+ w.field(10);
7797
+ w.str(v.id);
7798
+ n++;
7799
+ }
7612
7800
  w.closeMap(head, n);
7613
7801
  },
7614
7802
  read(r) {
@@ -7621,6 +7809,7 @@ export const RecordsEvent = message('records.Event', {
7621
7809
  let $context;
7622
7810
  let $traceId;
7623
7811
  let $spanId;
7812
+ let $id;
7624
7813
  for (let n = r.message(); n > 0; n--) {
7625
7814
  switch (r.field()) {
7626
7815
  case 1:
@@ -7650,6 +7839,9 @@ export const RecordsEvent = message('records.Event', {
7650
7839
  case 9:
7651
7840
  $spanId = r.bytes();
7652
7841
  break;
7842
+ case 10:
7843
+ $id = r.str();
7844
+ break;
7653
7845
  default:
7654
7846
  r.skip();
7655
7847
  }
@@ -7665,6 +7857,7 @@ export const RecordsEvent = message('records.Event', {
7665
7857
  context: $context,
7666
7858
  traceId: $traceId,
7667
7859
  spanId: $spanId,
7860
+ id: $id,
7668
7861
  };
7669
7862
  },
7670
7863
  });
@@ -8058,6 +8251,10 @@ export const RecordsAggregateAsked = message('records.AggregateAsked', {
8058
8251
  by: [3, list(str)],
8059
8252
  stats: [4, list(str)],
8060
8253
  distinct: [5, list(str)],
8254
+ /** day, week or month: rows of a calendar in timeZone, in place of every */
8255
+ calendar: [6, str],
8256
+ /** an IANA name, UTC among them, which calendar takes */
8257
+ timeZone: [7, str],
8061
8258
  }, {
8062
8259
  write(w, v) {
8063
8260
  const head = w.openMap();
@@ -8096,6 +8293,16 @@ export const RecordsAggregateAsked = message('records.AggregateAsked', {
8096
8293
  }
8097
8294
  n++;
8098
8295
  }
8296
+ if (v.calendar !== undefined) {
8297
+ w.field(6);
8298
+ w.str(v.calendar);
8299
+ n++;
8300
+ }
8301
+ if (v.timeZone !== undefined) {
8302
+ w.field(7);
8303
+ w.str(v.timeZone);
8304
+ n++;
8305
+ }
8099
8306
  w.closeMap(head, n);
8100
8307
  },
8101
8308
  read(r) {
@@ -8104,6 +8311,8 @@ export const RecordsAggregateAsked = message('records.AggregateAsked', {
8104
8311
  let $by;
8105
8312
  let $stats;
8106
8313
  let $distinct;
8314
+ let $calendar;
8315
+ let $timeZone;
8107
8316
  for (let n = r.message(); n > 0; n--) {
8108
8317
  switch (r.field()) {
8109
8318
  case 1:
@@ -8139,12 +8348,26 @@ export const RecordsAggregateAsked = message('records.AggregateAsked', {
8139
8348
  $distinct = items0;
8140
8349
  break;
8141
8350
  }
8351
+ case 6:
8352
+ $calendar = r.str();
8353
+ break;
8354
+ case 7:
8355
+ $timeZone = r.str();
8356
+ break;
8142
8357
  default:
8143
8358
  r.skip();
8144
8359
  }
8145
8360
  }
8146
8361
  r.leave();
8147
- return { query: $query, every: $every, by: $by, stats: $stats, distinct: $distinct };
8362
+ return {
8363
+ query: $query,
8364
+ every: $every,
8365
+ by: $by,
8366
+ stats: $stats,
8367
+ distinct: $distinct,
8368
+ calendar: $calendar,
8369
+ timeZone: $timeZone,
8370
+ };
8148
8371
  },
8149
8372
  });
8150
8373
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyshed/tinystore",
3
- "version": "0.1.1",
3
+ "version": "0.2.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.1.1",
35
- "@tinyshed/tinystore-linux-arm64": "0.1.1",
36
- "@tinyshed/tinystore-linux-x64-musl": "0.1.1",
37
- "@tinyshed/tinystore-linux-arm64-musl": "0.1.1",
38
- "@tinyshed/tinystore-darwin-arm64": "0.1.1",
39
- "@tinyshed/tinystore-darwin-x64": "0.1.1",
40
- "@tinyshed/tinystore-win32-x64": "0.1.1"
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"
41
41
  }
42
42
  }
package/src/index.ts CHANGED
@@ -210,7 +210,7 @@ export {
210
210
  type Status,
211
211
  Store,
212
212
  } from './store.ts'
213
- export type { Duration, DurationText, Time } from './time.ts'
213
+ export type { Calendar, Duration, DurationText, Time } from './time.ts'
214
214
  export { type Trace, withTrace } from './trace.ts'
215
215
  export type { Tx, TxBucket, TxCounters } from './tx.ts'
216
216
  export type { Key } from './wire/codec.ts'
package/src/jobs.ts CHANGED
@@ -57,6 +57,8 @@ export interface QueueOptions {
57
57
  concurrency?: number | Concurrency | undefined
58
58
  /** jobs started in any span: '30/s' */
59
59
  rate?: Rate | undefined
60
+ /** jobs of each group started in any span, beside rate: '2/s', each account's own limit; it lives in memory */
61
+ groupRate?: Rate | undefined
60
62
  /** how long a done job's id stays taken, so that an add of it adds nothing */
61
63
  dedupe?: Duration | undefined
62
64
  /** how long a failed job stays, to be found and started again: a week */
@@ -983,6 +985,7 @@ export function openQueue<T>(
983
985
  group: whole(concurrency.group, describe, "a group's concurrency"),
984
986
  },
985
987
  rate: options.rate === undefined ? undefined : rateOf(options.rate),
988
+ groupRate: options.groupRate === undefined ? undefined : rateOf(options.groupRate),
986
989
  dedupe: msOf(options.dedupe),
987
990
  keep: msOf(options.keep),
988
991
  maxWaiting: whole(options.maxWaiting, describe, 'a maxWaiting'),
package/src/metrics.ts CHANGED
@@ -5,8 +5,8 @@
5
5
  // A series' samples travel as two columns, an int64 time and a float64 value
6
6
  // a sample, so that every value's bits come back as they were written.
7
7
 
8
- import { download, type Link } from './connection.ts'
9
- import { InvalidError } from './errors.ts'
8
+ import { type Connection, download, type Link, pieceOf } from './connection.ts'
9
+ import { InvalidError, OutcomeUnknownError } from './errors.ts'
10
10
  import {
11
11
  Counter,
12
12
  Gauge,
@@ -20,7 +20,8 @@ import {
20
20
  Timer,
21
21
  type TimerOptions,
22
22
  } from './instruments.ts'
23
- import { type Duration, type DurationText, ms, type Time, unixMs } from './time.ts'
23
+ import { ConnectionLostError, type Stream } from './session.ts'
24
+ import { type Calendar, type Duration, type DurationText, ms, type Time, unixMs } from './time.ts'
24
25
  import {
25
26
  Empty,
26
27
  MetricsAggregateAsked,
@@ -34,11 +35,13 @@ import {
34
35
  MetricsNameAsked,
35
36
  MetricsNamePage,
36
37
  MetricsPageAsked,
38
+ MetricsPiece,
37
39
  MetricsPlan,
38
40
  MetricsQuery,
39
41
  MetricsRead,
40
42
  MetricsSeriesNamed,
41
43
  MetricsTextPage,
44
+ MetricsUpload,
42
45
  methods,
43
46
  } from './wire/protocol.ts'
44
47
 
@@ -153,6 +156,10 @@ export type Op =
153
156
  export interface AggregateQuery<O extends Op = Op> extends Query {
154
157
  /** the buckets' span, counted from 1970 in UTC; the range is one bucket without it */
155
158
  every?: Duration | undefined
159
+ /** buckets of a calendar in timeZone, in place of every: 'day' begins each at a local midnight */
160
+ calendar?: Calendar | undefined
161
+ /** the IANA zone a calendar's buckets are in: 'Europe/Moscow', 'UTC' */
162
+ timeZone?: string | undefined
156
163
  op: O
157
164
  /** the labels a group keeps; without by and without, every series of a name is one group */
158
165
  by?: readonly string[] | undefined
@@ -319,15 +326,26 @@ export class Metrics {
319
326
  for (const [label, value] of Object.entries(options.labels ?? {})) {
320
327
  labels[label] = String(value)
321
328
  }
322
- const body = MetricsExposition.encode({
323
- text: typeof text === 'string' ? new TextEncoder().encode(text) : text,
329
+ const given: ExpositionFields = {
324
330
  labels,
325
331
  at: options.at === undefined ? undefined : unixMs(options.at),
326
332
  keep: options.keep === undefined ? undefined : ms(options.keep),
333
+ }
334
+ const bytes = typeof text === 'string' ? new TextEncoder().encode(text) : text
335
+ await this.#link.run('write', async connection => {
336
+ const body = oneMessage(connection, bytes, given)
337
+ if (body !== undefined) {
338
+ await connection.session.call(methods['metrics.ingestPrometheus'], body)
339
+ return
340
+ }
341
+ const asked = MetricsUpload.encode({ size: bytes.length, ...given })
342
+ const stream = await connection.session.open(
343
+ methods['metrics.uploadPrometheus'],
344
+ asked,
345
+ false,
346
+ )
347
+ await uploaded(stream, bytes, pieceOf(connection))
327
348
  })
328
- await this.#link.run('write', connection =>
329
- connection.session.call(methods['metrics.ingestPrometheus'], body),
330
- )
331
349
  }
332
350
 
333
351
  /** The samples of each series the query finds, in its range: whole, or a limit error naming its bound. */
@@ -617,6 +635,8 @@ function aggregateOf(query: AggregateQuery): Parameters<typeof MetricsAggregateA
617
635
  return {
618
636
  query: queryOf(query),
619
637
  every: query.every === undefined ? undefined : ms(query.every),
638
+ calendar: query.calendar,
639
+ timeZone: query.timeZone,
620
640
  op: query.op,
621
641
  by: query.by === undefined ? undefined : [...query.by],
622
642
  without: query.without === undefined ? undefined : [...query.without],
@@ -684,6 +704,58 @@ function newSeries(
684
704
  }
685
705
  }
686
706
 
707
+ /** What a text's ingest gives every series of it, one message or an upload. */
708
+ interface ExpositionFields {
709
+ labels: Labels
710
+ at: number | undefined
711
+ keep: number | undefined
712
+ }
713
+
714
+ /** A text's one message, when it fits the body the connection agreed; a longer text goes in pieces. */
715
+ function oneMessage(
716
+ connection: Connection,
717
+ text: Uint8Array,
718
+ given: ExpositionFields,
719
+ ): Uint8Array | undefined {
720
+ const most = connection.session.agreed?.maxBody ?? 0
721
+ if (text.length > most) {
722
+ return undefined
723
+ }
724
+ const body = MetricsExposition.encode({ text, ...given })
725
+ return body.length <= most ? body : undefined
726
+ }
727
+
728
+ /**
729
+ * Sends a text's pieces as DATA within the stream's credit, the last ending
730
+ * them, and waits for the store to write it. A connection lost before that
731
+ * last DATA left nothing written, so the text goes again on the next one;
732
+ * lost after it, whether the text was written is unknown.
733
+ */
734
+ async function uploaded(stream: Stream, text: Uint8Array, piece: number): Promise<void> {
735
+ let ending = false
736
+ try {
737
+ for (let at = 0; !ending; at += piece) {
738
+ ending = at + piece >= text.length
739
+ await stream.send(MetricsPiece.encode({ bytes: text.subarray(at, at + piece) }), ending)
740
+ }
741
+ // the RESPONSE that began it, then the answer to its last piece
742
+ await stream.next()
743
+ await stream.next()
744
+ } catch (err) {
745
+ if (err instanceof ConnectionLostError && !ending) {
746
+ // the store let go of what came and wrote nothing, so the whole text goes again
747
+ throw new ConnectionLostError(err.message, false)
748
+ }
749
+ stream.cancel()
750
+ if (err instanceof ConnectionLostError) {
751
+ throw new OutcomeUnknownError(
752
+ `the connection was lost as the text was written: ${err.message}`,
753
+ )
754
+ }
755
+ throw err
756
+ }
757
+ }
758
+
687
759
  function columnsOf(given: SeriesInput): [ArrayLike<Time>, ArrayLike<number>] {
688
760
  if (given.samples !== undefined) {
689
761
  if (given.times !== undefined || given.values !== undefined) {