@tinyshed/tinystore 0.2.0-rc.1 → 0.3.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,
@@ -1517,6 +1524,8 @@ export const ConfigOpen = message('config.Open', {
1517
1524
  layers: [3, str],
1518
1525
  /** a kept change that no longer fits refuses the open */
1519
1526
  strict: [4, bool],
1527
+ /** as kv.db keeps it, for a command: the last declaration a program opened it with, */
1528
+ kept: [5, bool],
1520
1529
  }, {
1521
1530
  write(w, v) {
1522
1531
  const head = w.openMap();
@@ -1541,6 +1550,11 @@ export const ConfigOpen = message('config.Open', {
1541
1550
  w.bool(v.strict);
1542
1551
  n++;
1543
1552
  }
1553
+ if (v.kept !== undefined) {
1554
+ w.field(5);
1555
+ w.bool(v.kept);
1556
+ n++;
1557
+ }
1544
1558
  w.closeMap(head, n);
1545
1559
  },
1546
1560
  read(r) {
@@ -1548,6 +1562,7 @@ export const ConfigOpen = message('config.Open', {
1548
1562
  let $declaration;
1549
1563
  let $layers;
1550
1564
  let $strict;
1565
+ let $kept;
1551
1566
  for (let n = r.message(); n > 0; n--) {
1552
1567
  switch (r.field()) {
1553
1568
  case 1:
@@ -1562,12 +1577,21 @@ export const ConfigOpen = message('config.Open', {
1562
1577
  case 4:
1563
1578
  $strict = r.bool();
1564
1579
  break;
1580
+ case 5:
1581
+ $kept = r.bool();
1582
+ break;
1565
1583
  default:
1566
1584
  r.skip();
1567
1585
  }
1568
1586
  }
1569
1587
  r.leave();
1570
- return { name: $name, declaration: $declaration, layers: $layers, strict: $strict };
1588
+ return {
1589
+ name: $name,
1590
+ declaration: $declaration,
1591
+ layers: $layers,
1592
+ strict: $strict,
1593
+ kept: $kept,
1594
+ };
1571
1595
  },
1572
1596
  });
1573
1597
  /**
@@ -5392,6 +5416,10 @@ export const MetricsQuery = message('metrics.Query', {
5392
5416
  from: [4, int],
5393
5417
  /** excluded; now, a sample taken now in the range, unless given */
5394
5418
  to: [5, int],
5419
+ /** a page's samples or buckets at most, 100,000 at most: a page, which read and aggregate take */
5420
+ limit: [6, uint],
5421
+ /** a page's next, with limit */
5422
+ after: [7, str],
5395
5423
  }, {
5396
5424
  write(w, v) {
5397
5425
  const head = w.openMap();
@@ -5421,6 +5449,16 @@ export const MetricsQuery = message('metrics.Query', {
5421
5449
  w.int(v.to);
5422
5450
  n++;
5423
5451
  }
5452
+ if (v.limit !== undefined) {
5453
+ w.field(6);
5454
+ w.uint(v.limit);
5455
+ n++;
5456
+ }
5457
+ if (v.after !== undefined) {
5458
+ w.field(7);
5459
+ w.str(v.after);
5460
+ n++;
5461
+ }
5424
5462
  w.closeMap(head, n);
5425
5463
  },
5426
5464
  read(r) {
@@ -5429,6 +5467,8 @@ export const MetricsQuery = message('metrics.Query', {
5429
5467
  let $since;
5430
5468
  let $from;
5431
5469
  let $to;
5470
+ let $limit;
5471
+ let $after;
5432
5472
  for (let n = r.message(); n > 0; n--) {
5433
5473
  switch (r.field()) {
5434
5474
  case 1:
@@ -5446,12 +5486,26 @@ export const MetricsQuery = message('metrics.Query', {
5446
5486
  case 5:
5447
5487
  $to = r.int();
5448
5488
  break;
5489
+ case 6:
5490
+ $limit = r.uint();
5491
+ break;
5492
+ case 7:
5493
+ $after = r.str();
5494
+ break;
5449
5495
  default:
5450
5496
  r.skip();
5451
5497
  }
5452
5498
  }
5453
5499
  r.leave();
5454
- return { name: $name, where: $where, since: $since, from: $from, to: $to };
5500
+ return {
5501
+ name: $name,
5502
+ where: $where,
5503
+ since: $since,
5504
+ from: $from,
5505
+ to: $to,
5506
+ limit: $limit,
5507
+ after: $after,
5508
+ };
5455
5509
  },
5456
5510
  });
5457
5511
  /**
@@ -5467,7 +5521,7 @@ export const MetricsSeries = message('metrics.Series', {
5467
5521
  times: [4, bin],
5468
5522
  /** a float64 a sample */
5469
5523
  values: [5, bin],
5470
- /** the next part goes on with this series */
5524
+ /** the next part goes on with this series; on a page's last, the next page's first */
5471
5525
  more: [6, bool],
5472
5526
  }, {
5473
5527
  write(w, v) {
@@ -5549,6 +5603,8 @@ export const MetricsSeries = message('metrics.Series', {
5549
5603
  });
5550
5604
  export const MetricsRead = message('metrics.Read', {
5551
5605
  series: [1, list(MetricsSeries)],
5606
+ /** on the last part of a page that has more, to a request that gave limit */
5607
+ next: [2, str],
5552
5608
  }, {
5553
5609
  write(w, v) {
5554
5610
  const head = w.openMap();
@@ -5561,10 +5617,16 @@ export const MetricsRead = message('metrics.Read', {
5561
5617
  }
5562
5618
  n++;
5563
5619
  }
5620
+ if (v.next !== undefined) {
5621
+ w.field(2);
5622
+ w.str(v.next);
5623
+ n++;
5624
+ }
5564
5625
  w.closeMap(head, n);
5565
5626
  },
5566
5627
  read(r) {
5567
5628
  let $series;
5629
+ let $next;
5568
5630
  for (let n = r.message(); n > 0; n--) {
5569
5631
  switch (r.field()) {
5570
5632
  case 1: {
@@ -5576,12 +5638,15 @@ export const MetricsRead = message('metrics.Read', {
5576
5638
  $series = items0;
5577
5639
  break;
5578
5640
  }
5641
+ case 2:
5642
+ $next = r.str();
5643
+ break;
5579
5644
  default:
5580
5645
  r.skip();
5581
5646
  }
5582
5647
  }
5583
5648
  r.leave();
5584
- return { series: $series };
5649
+ return { series: $series, next: $next };
5585
5650
  },
5586
5651
  });
5587
5652
  /**
@@ -5950,6 +6015,7 @@ export const MetricsGroup = message('metrics.Group', {
5950
6015
  name: [1, str],
5951
6016
  labels: [2, names(str)],
5952
6017
  buckets: [3, list(MetricsBucket)],
6018
+ /** the next part goes on with this group; on a page's last, the next page's first */
5953
6019
  more: [4, bool],
5954
6020
  }, {
5955
6021
  write(w, v) {
@@ -6015,6 +6081,8 @@ export const MetricsGroup = message('metrics.Group', {
6015
6081
  });
6016
6082
  export const MetricsAggregates = message('metrics.Aggregates', {
6017
6083
  groups: [1, list(MetricsGroup)],
6084
+ /** on the last part of a page that has more, to a request that gave limit */
6085
+ next: [2, str],
6018
6086
  }, {
6019
6087
  write(w, v) {
6020
6088
  const head = w.openMap();
@@ -6027,10 +6095,16 @@ export const MetricsAggregates = message('metrics.Aggregates', {
6027
6095
  }
6028
6096
  n++;
6029
6097
  }
6098
+ if (v.next !== undefined) {
6099
+ w.field(2);
6100
+ w.str(v.next);
6101
+ n++;
6102
+ }
6030
6103
  w.closeMap(head, n);
6031
6104
  },
6032
6105
  read(r) {
6033
6106
  let $groups;
6107
+ let $next;
6034
6108
  for (let n = r.message(); n > 0; n--) {
6035
6109
  switch (r.field()) {
6036
6110
  case 1: {
@@ -6042,12 +6116,15 @@ export const MetricsAggregates = message('metrics.Aggregates', {
6042
6116
  $groups = items0;
6043
6117
  break;
6044
6118
  }
6119
+ case 2:
6120
+ $next = r.str();
6121
+ break;
6045
6122
  default:
6046
6123
  r.skip();
6047
6124
  }
6048
6125
  }
6049
6126
  r.leave();
6050
- return { groups: $groups };
6127
+ return { groups: $groups, next: $next };
6051
6128
  },
6052
6129
  });
6053
6130
  export const MetricsExceeds = message('metrics.Exceeds', {
@@ -6296,6 +6373,100 @@ export const MetricsExposition = message('metrics.Exposition', {
6296
6373
  return { text: $text, labels: $labels, at: $at, keep: $keep };
6297
6374
  },
6298
6375
  });
6376
+ /**
6377
+ * A text past what one message holds: what metrics.Exposition carries but
6378
+ * the text, whose bytes follow as the client's DATA, a metrics.Piece each.
6379
+ */
6380
+ export const MetricsUpload = message('metrics.Upload', {
6381
+ /** the text's bytes, 32 MiB at most: pieces that come to more or fewer are refused */
6382
+ size: [1, uint],
6383
+ labels: [2, names(str)],
6384
+ at: [3, int],
6385
+ keep: [4, uint],
6386
+ }, {
6387
+ write(w, v) {
6388
+ const head = w.openMap();
6389
+ let n = 0;
6390
+ if (v.size !== undefined) {
6391
+ w.field(1);
6392
+ w.uint(v.size);
6393
+ n++;
6394
+ }
6395
+ if (v.labels !== undefined) {
6396
+ w.field(2);
6397
+ writeNames(w, v.labels, str.write);
6398
+ n++;
6399
+ }
6400
+ if (v.at !== undefined) {
6401
+ w.field(3);
6402
+ w.int(v.at);
6403
+ n++;
6404
+ }
6405
+ if (v.keep !== undefined) {
6406
+ w.field(4);
6407
+ w.uint(v.keep);
6408
+ n++;
6409
+ }
6410
+ w.closeMap(head, n);
6411
+ },
6412
+ read(r) {
6413
+ let $size;
6414
+ let $labels;
6415
+ let $at;
6416
+ let $keep;
6417
+ for (let n = r.message(); n > 0; n--) {
6418
+ switch (r.field()) {
6419
+ case 1:
6420
+ $size = r.uint();
6421
+ break;
6422
+ case 2:
6423
+ $labels = readNames(r, str.read);
6424
+ break;
6425
+ case 3:
6426
+ $at = r.int();
6427
+ break;
6428
+ case 4:
6429
+ $keep = r.uint();
6430
+ break;
6431
+ default:
6432
+ r.skip();
6433
+ }
6434
+ }
6435
+ r.leave();
6436
+ return { size: $size, labels: $labels, at: $at, keep: $keep };
6437
+ },
6438
+ });
6439
+ /**
6440
+ * A piece of an uploaded text, cut anywhere.
6441
+ */
6442
+ export const MetricsPiece = message('metrics.Piece', {
6443
+ bytes: [1, bin],
6444
+ }, {
6445
+ write(w, v) {
6446
+ const head = w.openMap();
6447
+ let n = 0;
6448
+ if (v.bytes !== undefined) {
6449
+ w.field(1);
6450
+ w.bin(v.bytes);
6451
+ n++;
6452
+ }
6453
+ w.closeMap(head, n);
6454
+ },
6455
+ read(r) {
6456
+ let $bytes;
6457
+ for (let n = r.message(); n > 0; n--) {
6458
+ switch (r.field()) {
6459
+ case 1:
6460
+ $bytes = r.bytes();
6461
+ break;
6462
+ default:
6463
+ r.skip();
6464
+ }
6465
+ }
6466
+ r.leave();
6467
+ return { bytes: $bytes };
6468
+ },
6469
+ });
6299
6470
  /**
6300
6471
  * A page of names, of a name's labels or of a label's values.
6301
6472
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyshed/tinystore",
3
- "version": "0.2.0-rc.1",
3
+ "version": "0.3.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-rc.1",
35
- "@tinyshed/tinystore-linux-arm64": "0.2.0-rc.1",
36
- "@tinyshed/tinystore-linux-x64-musl": "0.2.0-rc.1",
37
- "@tinyshed/tinystore-linux-arm64-musl": "0.2.0-rc.1",
38
- "@tinyshed/tinystore-darwin-arm64": "0.2.0-rc.1",
39
- "@tinyshed/tinystore-darwin-x64": "0.2.0-rc.1",
40
- "@tinyshed/tinystore-win32-x64": "0.2.0-rc.1"
34
+ "@tinyshed/tinystore-linux-x64": "0.3.0",
35
+ "@tinyshed/tinystore-linux-arm64": "0.3.0",
36
+ "@tinyshed/tinystore-linux-x64-musl": "0.3.0",
37
+ "@tinyshed/tinystore-linux-arm64-musl": "0.3.0",
38
+ "@tinyshed/tinystore-darwin-arm64": "0.3.0",
39
+ "@tinyshed/tinystore-darwin-x64": "0.3.0",
40
+ "@tinyshed/tinystore-win32-x64": "0.3.0"
41
41
  }
42
42
  }
package/src/index.ts CHANGED
@@ -104,6 +104,7 @@ export type {
104
104
  export { type ConsoleOut, type Logger, type LoggerOptions, logger } from './logger.ts'
105
105
  export {
106
106
  type Aggregate,
107
+ type AggregatePage,
107
108
  type AggregateQuery,
108
109
  type Bucket as MetricsBucket,
109
110
  type Buckets,
@@ -135,6 +136,7 @@ export {
135
136
  type Series,
136
137
  type SeriesInput,
137
138
  type SeriesKind,
139
+ type SeriesPage,
138
140
  type Timer,
139
141
  type TimerOptions,
140
142
  type Timing,
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,6 +20,7 @@ import {
20
20
  Timer,
21
21
  type TimerOptions,
22
22
  } from './instruments.ts'
23
+ import { ConnectionLostError, type Stream } from './session.ts'
23
24
  import { type Calendar, type Duration, type DurationText, ms, type Time, unixMs } from './time.ts'
24
25
  import {
25
26
  Empty,
@@ -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
 
@@ -89,6 +92,10 @@ export interface Query {
89
92
  from?: Time | undefined
90
93
  /** the range's end, excluded; now, a sample taken now in the range, unless given */
91
94
  to?: Time | undefined
95
+ /** a page of read or aggregate: this many samples or buckets at most, 100,000 at most; without it, the whole answer or a limit error */
96
+ limit?: number | undefined
97
+ /** the page after the one whose next this is, read with the same query and limit */
98
+ after?: string | undefined
92
99
  }
93
100
 
94
101
  /** A series to write: its samples as [time, value] pairs, or as times and values columns. */
@@ -122,6 +129,19 @@ export interface Series {
122
129
  times: Float64Array
123
130
  /** the values, whose bytes keep each one's bits: -0 and a NaN's payload survive */
124
131
  values: Float64Array
132
+ /** a page cut the series here: the page its next reads begins with the rest of it */
133
+ more: boolean
134
+ }
135
+
136
+ /**
137
+ * One page of a read: its series, and next while there is more. Walking it
138
+ * with for await gives its series, then those of each page after it, one
139
+ * page held at a time. Without limit it is the whole answer.
140
+ */
141
+ export interface SeriesPage extends AsyncIterable<Series> {
142
+ series: Series[]
143
+ /** where the page after it begins; absent on the last */
144
+ next?: string
125
145
  }
126
146
 
127
147
  /** p and a rank: 'p95', 'p99.9'. The ranks offered are those asked most; any other is written the same way. */
@@ -206,6 +226,18 @@ export interface Aggregate<O extends Op = Op> {
206
226
  labels: Labels
207
227
  /** a bucket without samples is left out */
208
228
  buckets: Bucket<O>[]
229
+ /** a page cut the group here: the page its next reads begins with the rest of its buckets */
230
+ more: boolean
231
+ }
232
+
233
+ /**
234
+ * One page of an aggregate: its groups, and next while there is more,
235
+ * walked as a SeriesPage is. A page ends between two whole buckets.
236
+ */
237
+ export interface AggregatePage<O extends Op = Op> extends AsyncIterable<Aggregate<O>> {
238
+ groups: Aggregate<O>[]
239
+ /** where the page after it begins; absent on the last */
240
+ next?: string
209
241
  }
210
242
 
211
243
  /** A series' newest sample in the range. */
@@ -323,36 +355,69 @@ export class Metrics {
323
355
  for (const [label, value] of Object.entries(options.labels ?? {})) {
324
356
  labels[label] = String(value)
325
357
  }
326
- const body = MetricsExposition.encode({
327
- text: typeof text === 'string' ? new TextEncoder().encode(text) : text,
358
+ const given: ExpositionFields = {
328
359
  labels,
329
360
  at: options.at === undefined ? undefined : unixMs(options.at),
330
361
  keep: options.keep === undefined ? undefined : ms(options.keep),
362
+ }
363
+ const bytes = typeof text === 'string' ? new TextEncoder().encode(text) : text
364
+ await this.#link.run('write', async connection => {
365
+ const body = oneMessage(connection, bytes, given)
366
+ if (body !== undefined) {
367
+ await connection.session.call(methods['metrics.ingestPrometheus'], body)
368
+ return
369
+ }
370
+ const asked = MetricsUpload.encode({ size: bytes.length, ...given })
371
+ const stream = await connection.session.open(
372
+ methods['metrics.uploadPrometheus'],
373
+ asked,
374
+ false,
375
+ )
376
+ await uploaded(stream, bytes, pieceOf(connection))
331
377
  })
332
- await this.#link.run('write', connection =>
333
- connection.session.call(methods['metrics.ingestPrometheus'], body),
334
- )
335
378
  }
336
379
 
337
- /** The samples of each series the query finds, in its range: whole, or a limit error naming its bound. */
338
- async read(query: Query): Promise<Series[]> {
339
- const parts = await this.#download('metrics.read', MetricsQuery.encode(queryOf(query)))
380
+ /**
381
+ * The samples of each series the query finds, in its range: without limit
382
+ * the whole answer, or a limit error naming its bound; with limit a page,
383
+ * which may hold fewer samples than limit and still have next.
384
+ */
385
+ async read(query: Query): Promise<SeriesPage> {
386
+ const parts = (await this.#download('metrics.read', MetricsQuery.encode(queryOf(query)))).map(
387
+ part => MetricsRead.decode(part),
388
+ )
340
389
  const series: Series[] = []
341
390
  let more = false
342
- for (const piece of parts.flatMap(part => MetricsRead.decode(part).series ?? [])) {
391
+ for (const piece of parts.flatMap(part => part.series ?? [])) {
343
392
  const times = timesOf(piece.times ?? new Uint8Array(0))
344
393
  const values = valuesOf(piece.values ?? new Uint8Array(0))
345
394
  const last = series.at(-1)
346
395
  if (more && last !== undefined) {
347
396
  last.times = joined(last.times, times)
348
397
  last.values = joined(last.values, values)
398
+ last.more = piece.more === true
349
399
  } else {
350
400
  const kind = (piece.kind ?? 'gauge') as SeriesKind
351
- series.push({ name: piece.name ?? '', kind, labels: piece.labels ?? {}, times, values })
401
+ const labels = piece.labels ?? {}
402
+ series.push({
403
+ name: piece.name ?? '',
404
+ kind,
405
+ labels,
406
+ times,
407
+ values,
408
+ more: piece.more === true,
409
+ })
352
410
  }
353
411
  more = piece.more === true
354
412
  }
355
- return series
413
+ const next = parts.at(-1)?.next
414
+ const page: { series: Series[]; next?: string } =
415
+ next === undefined ? { series } : { series, next }
416
+ return walked(
417
+ page,
418
+ at => at.series,
419
+ after => this.read({ ...query, after }),
420
+ )
356
421
  }
357
422
 
358
423
  /** The newest sample of each series the query finds, when it has one in the range. */
@@ -372,24 +437,37 @@ export class Metrics {
372
437
  * The buckets of each group of the series the query finds, each one exact
373
438
  * value of op, rounded once; a bucket of count … last has its value.
374
439
  */
375
- async aggregate<O extends Op>(query: AggregateQuery<O>): Promise<Aggregate<O>[]> {
376
- const parts = await this.#download(
377
- 'metrics.aggregate',
378
- MetricsAggregateAsked.encode(aggregateOf(query)),
440
+ async aggregate<O extends Op>(query: AggregateQuery<O>): Promise<AggregatePage<O>> {
441
+ const asked = MetricsAggregateAsked.encode(aggregateOf(query))
442
+ const parts = (await this.#download('metrics.aggregate', asked)).map(part =>
443
+ MetricsAggregates.decode(part),
379
444
  )
380
445
  const groups: Aggregate<O>[] = []
381
446
  let more = false
382
- for (const piece of parts.flatMap(part => MetricsAggregates.decode(part).groups ?? [])) {
447
+ for (const piece of parts.flatMap(part => part.groups ?? [])) {
383
448
  const buckets = (piece.buckets ?? []).map(bucket => bucketOf(bucket) as Bucket<O>)
384
449
  const last = groups.at(-1)
385
450
  if (more && last !== undefined) {
386
451
  last.buckets.push(...buckets)
452
+ last.more = piece.more === true
387
453
  } else {
388
- groups.push({ name: piece.name ?? '', labels: piece.labels ?? {}, buckets })
454
+ groups.push({
455
+ name: piece.name ?? '',
456
+ labels: piece.labels ?? {},
457
+ buckets,
458
+ more: piece.more === true,
459
+ })
389
460
  }
390
461
  more = piece.more === true
391
462
  }
392
- return groups
463
+ const next = parts.at(-1)?.next
464
+ const page: { groups: Aggregate<O>[]; next?: string } =
465
+ next === undefined ? { groups } : { groups, next }
466
+ return walked(
467
+ page,
468
+ at => at.groups,
469
+ after => this.aggregate({ ...query, after }),
470
+ )
393
471
  }
394
472
 
395
473
  /** What a read, or an aggregate when given its op, would spend of each bound; it decodes no sample. */
@@ -577,6 +655,28 @@ export class Metrics {
577
655
  }
578
656
  }
579
657
 
658
+ /**
659
+ * A page that for await walks: its items, then those of each page after it,
660
+ * the next page read only once the walk reaches the end of the one it holds.
661
+ */
662
+ function walked<P extends { next?: string }, T>(
663
+ page: P,
664
+ items: (page: P) => readonly T[],
665
+ readOn: (after: string) => Promise<P>,
666
+ ): P & AsyncIterable<T> {
667
+ return Object.assign(page, {
668
+ async *[Symbol.asyncIterator](): AsyncGenerator<T> {
669
+ for (let at = page; ; ) {
670
+ yield* items(at)
671
+ if (at.next === undefined) {
672
+ return
673
+ }
674
+ at = await readOn(at.next)
675
+ }
676
+ },
677
+ })
678
+ }
679
+
580
680
  /** A query as the wire carries it; a condition given as undefined is refused, never left out. */
581
681
  function queryOf(query: Query): Parameters<typeof MetricsQuery.encode>[0] {
582
682
  const where: Record<string, { values?: string[]; not?: boolean; prefix?: string }> = {}
@@ -592,6 +692,8 @@ function queryOf(query: Query): Parameters<typeof MetricsQuery.encode>[0] {
592
692
  since: query.since === undefined ? undefined : ms(query.since),
593
693
  from: query.from === undefined ? undefined : unixMs(query.from),
594
694
  to: query.to === undefined ? undefined : unixMs(query.to),
695
+ limit: query.limit,
696
+ after: query.after,
595
697
  }
596
698
  }
597
699
 
@@ -690,6 +792,58 @@ function newSeries(
690
792
  }
691
793
  }
692
794
 
795
+ /** What a text's ingest gives every series of it, one message or an upload. */
796
+ interface ExpositionFields {
797
+ labels: Labels
798
+ at: number | undefined
799
+ keep: number | undefined
800
+ }
801
+
802
+ /** A text's one message, when it fits the body the connection agreed; a longer text goes in pieces. */
803
+ function oneMessage(
804
+ connection: Connection,
805
+ text: Uint8Array,
806
+ given: ExpositionFields,
807
+ ): Uint8Array | undefined {
808
+ const most = connection.session.agreed?.maxBody ?? 0
809
+ if (text.length > most) {
810
+ return undefined
811
+ }
812
+ const body = MetricsExposition.encode({ text, ...given })
813
+ return body.length <= most ? body : undefined
814
+ }
815
+
816
+ /**
817
+ * Sends a text's pieces as DATA within the stream's credit, the last ending
818
+ * them, and waits for the store to write it. A connection lost before that
819
+ * last DATA left nothing written, so the text goes again on the next one;
820
+ * lost after it, whether the text was written is unknown.
821
+ */
822
+ async function uploaded(stream: Stream, text: Uint8Array, piece: number): Promise<void> {
823
+ let ending = false
824
+ try {
825
+ for (let at = 0; !ending; at += piece) {
826
+ ending = at + piece >= text.length
827
+ await stream.send(MetricsPiece.encode({ bytes: text.subarray(at, at + piece) }), ending)
828
+ }
829
+ // the RESPONSE that began it, then the answer to its last piece
830
+ await stream.next()
831
+ await stream.next()
832
+ } catch (err) {
833
+ if (err instanceof ConnectionLostError && !ending) {
834
+ // the store let go of what came and wrote nothing, so the whole text goes again
835
+ throw new ConnectionLostError(err.message, false)
836
+ }
837
+ stream.cancel()
838
+ if (err instanceof ConnectionLostError) {
839
+ throw new OutcomeUnknownError(
840
+ `the connection was lost as the text was written: ${err.message}`,
841
+ )
842
+ }
843
+ throw err
844
+ }
845
+ }
846
+
693
847
  function columnsOf(given: SeriesInput): [ArrayLike<Time>, ArrayLike<number>] {
694
848
  if (given.samples !== undefined) {
695
849
  if (given.times !== undefined || given.values !== undefined) {