@tinyshed/tinystore 0.1.0-rc.5 → 0.1.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +37 -37
  2. package/bin/tinystore.js +12 -1
  3. package/dist/backup.d.ts +33 -0
  4. package/dist/backup.js +49 -0
  5. package/dist/blobs.d.ts +160 -79
  6. package/dist/blobs.js +510 -217
  7. package/dist/channels.d.ts +90 -0
  8. package/dist/channels.js +291 -0
  9. package/dist/clock.js +3 -3
  10. package/dist/config.d.ts +209 -86
  11. package/dist/config.js +581 -538
  12. package/dist/connection.d.ts +64 -2
  13. package/dist/connection.js +201 -16
  14. package/dist/console.d.ts +27 -3
  15. package/dist/console.js +165 -12
  16. package/dist/dotenv.d.ts +7 -0
  17. package/dist/dotenv.js +79 -0
  18. package/dist/errors.d.ts +61 -1
  19. package/dist/errors.js +81 -1
  20. package/dist/handles.d.ts +44 -2
  21. package/dist/handles.js +81 -2
  22. package/dist/index.d.ts +17 -11
  23. package/dist/index.js +5 -3
  24. package/dist/instruments.d.ts +177 -0
  25. package/dist/instruments.js +819 -0
  26. package/dist/jobs.d.ts +335 -239
  27. package/dist/jobs.js +716 -536
  28. package/dist/json.d.ts +2 -0
  29. package/dist/json.js +122 -0
  30. package/dist/kv.d.ts +130 -243
  31. package/dist/kv.js +273 -640
  32. package/dist/limits.d.ts +80 -0
  33. package/dist/limits.js +139 -0
  34. package/dist/locks.d.ts +62 -0
  35. package/dist/locks.js +237 -0
  36. package/dist/logger.d.ts +45 -9
  37. package/dist/logger.js +150 -38
  38. package/dist/metrics.d.ts +210 -209
  39. package/dist/metrics.js +319 -556
  40. package/dist/once.d.ts +19 -18
  41. package/dist/once.js +74 -63
  42. package/dist/query.d.ts +181 -0
  43. package/dist/query.js +788 -0
  44. package/dist/records.d.ts +241 -150
  45. package/dist/records.js +507 -291
  46. package/dist/runtime/addon.d.ts +14 -0
  47. package/dist/runtime/addon.js +69 -0
  48. package/dist/runtime/bun.js +5 -7
  49. package/dist/runtime/node.js +5 -7
  50. package/dist/runtime/pipe.d.ts +17 -0
  51. package/dist/runtime/pipe.js +133 -0
  52. package/dist/runtime/shared.d.ts +6 -0
  53. package/dist/runtime/shared.js +19 -0
  54. package/dist/runtime.d.ts +5 -2
  55. package/dist/search.d.ts +189 -0
  56. package/dist/search.js +281 -0
  57. package/dist/session.d.ts +31 -3
  58. package/dist/session.js +153 -41
  59. package/dist/sql.d.ts +228 -78
  60. package/dist/sql.js +745 -240
  61. package/dist/store.d.ts +244 -24
  62. package/dist/store.js +270 -67
  63. package/dist/time.d.ts +5 -1
  64. package/dist/time.js +22 -2
  65. package/dist/tx.d.ts +61 -0
  66. package/dist/tx.js +314 -0
  67. package/dist/wire/codec.d.ts +23 -7
  68. package/dist/wire/codec.js +56 -35
  69. package/dist/wire/frame.d.ts +2 -0
  70. package/dist/wire/frame.js +31 -14
  71. package/dist/wire/messages.d.ts +259 -0
  72. package/dist/wire/messages.js +22 -0
  73. package/dist/wire/msgpack.d.ts +49 -1
  74. package/dist/wire/msgpack.js +293 -56
  75. package/dist/wire/protocol.d.ts +2683 -0
  76. package/dist/wire/protocol.js +10846 -0
  77. package/package.json +39 -38
  78. package/src/backup.ts +81 -0
  79. package/src/blobs.ts +678 -301
  80. package/src/channels.ts +377 -0
  81. package/src/clock.ts +4 -4
  82. package/src/config.ts +902 -612
  83. package/src/connection.ts +279 -21
  84. package/src/console.ts +180 -12
  85. package/src/dotenv.ts +82 -0
  86. package/src/errors.ts +105 -0
  87. package/src/handles.ts +157 -8
  88. package/src/index.ts +158 -52
  89. package/src/instruments.ts +1042 -0
  90. package/src/jobs.ts +1017 -733
  91. package/src/json.ts +133 -0
  92. package/src/kv.ts +424 -847
  93. package/src/limits.ts +202 -0
  94. package/src/locks.ts +320 -0
  95. package/src/logger.ts +209 -40
  96. package/src/metrics.ts +557 -752
  97. package/src/once.ts +86 -83
  98. package/src/query.ts +1005 -0
  99. package/src/records.ts +795 -418
  100. package/src/runtime/addon.ts +99 -0
  101. package/src/runtime/bun.ts +5 -7
  102. package/src/runtime/node.ts +5 -7
  103. package/src/runtime/pipe.ts +178 -0
  104. package/src/runtime/shared.ts +20 -0
  105. package/src/runtime.ts +5 -2
  106. package/src/search.ts +480 -0
  107. package/src/session.ts +187 -41
  108. package/src/sql.ts +993 -306
  109. package/src/store.ts +473 -78
  110. package/src/time.ts +48 -3
  111. package/src/tx.ts +426 -0
  112. package/src/wire/codec.ts +78 -37
  113. package/src/wire/frame.ts +44 -14
  114. package/src/wire/messages.ts +25 -0
  115. package/src/wire/msgpack.ts +327 -56
  116. package/src/wire/protocol.ts +11709 -0
  117. package/dist/limiter.d.ts +0 -45
  118. package/dist/limiter.js +0 -68
  119. package/dist/quota.d.ts +0 -44
  120. package/dist/quota.js +0 -98
  121. package/src/limiter.ts +0 -99
  122. package/src/quota.ts +0 -137
package/src/wire/codec.ts CHANGED
@@ -13,7 +13,7 @@ import { Reader, Writer } from './msgpack.ts'
13
13
  export interface Codec<Out, In = Out> {
14
14
  /** what the value is, which the tests that read vectors go by */
15
15
  readonly kind: string
16
- /** a list's items, or what a nullable holds when it holds something */
16
+ /** a list's items, a map's values, or what a nullable holds when it holds something */
17
17
  readonly item?: Codec<unknown, never>
18
18
  write(w: Writer, value: In): void
19
19
  read(r: Reader): Out
@@ -61,7 +61,7 @@ export const str: Codec<string> = {
61
61
  export const bin: Codec<Uint8Array> = {
62
62
  kind: 'bin',
63
63
  write: (w, v) => w.bin(v),
64
- read: r => r.bin().slice(),
64
+ read: r => r.bytes(),
65
65
  }
66
66
 
67
67
  const utf8 = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
@@ -82,7 +82,7 @@ export function textOf(bytes: Uint8Array): string | undefined {
82
82
  export const text: Codec<string | Uint8Array> = {
83
83
  kind: 'text',
84
84
  write: (w, v) => writeText(w, v),
85
- read: r => (r.type() === 'bin' ? r.bin().slice() : r.str()),
85
+ read: r => (r.type() === 'bin' ? r.bytes() : r.str()),
86
86
  }
87
87
 
88
88
  function writeText(w: Writer, v: string | Uint8Array): void {
@@ -116,7 +116,7 @@ export const key: Codec<string | Uint8Array, Key> = {
116
116
  read: r => {
117
117
  switch (r.type()) {
118
118
  case 'bin':
119
- return r.bin().slice()
119
+ return r.bytes()
120
120
  case 'int':
121
121
  return String(r.int64())
122
122
  }
@@ -142,7 +142,7 @@ export const kvValue: Codec<Raw, Raw | number> = {
142
142
  if (r.nil()) {
143
143
  return null
144
144
  }
145
- return r.type() === 'bin' ? r.bin().slice() : r.int64()
145
+ return r.type() === 'bin' ? r.bytes() : r.int64()
146
146
  },
147
147
  }
148
148
 
@@ -165,10 +165,8 @@ export const sqlValue: Codec<SqlValue | boolean, SqlArg> = {
165
165
  return null
166
166
  case 'bool':
167
167
  return r.bool()
168
- case 'int': {
169
- const n = r.int64()
170
- return n >= Number.MIN_SAFE_INTEGER && n <= Number.MAX_SAFE_INTEGER ? Number(n) : n
171
- }
168
+ case 'int':
169
+ return r.integer()
172
170
  case 'float':
173
171
  return r.float()
174
172
  case 'bin':
@@ -213,24 +211,35 @@ export function names<V, In = V>(
213
211
  ): Codec<Record<string, V>, Record<string, In>> {
214
212
  return {
215
213
  kind: value.kind === 'sql value' ? 'sql names' : 'names',
216
- write: (w, v) => {
217
- const sorted = Object.keys(v).sort(byCodePoint)
218
- w.map(sorted.length)
219
- for (const name of sorted) {
220
- w.str(name)
221
- value.write(w, v[name] as In)
222
- }
223
- },
224
- read: r => {
225
- const named: Record<string, V> = {}
226
- r.names(name => {
227
- named[name] = value.read(r)
228
- })
229
- return named
230
- },
214
+ item: value,
215
+ write: (w, v) => writeNames(w, v, value.write),
216
+ read: r => readNames(r, value.read),
231
217
  }
232
218
  }
233
219
 
220
+ /** Writes a map of names in the byte order of their UTF-8, each value as `item` writes it. */
221
+ export function writeNames<In>(
222
+ w: Writer,
223
+ named: Record<string, In>,
224
+ item: (w: Writer, value: In) => void,
225
+ ): void {
226
+ const sorted = Object.keys(named).sort(byCodePoint)
227
+ w.map(sorted.length)
228
+ for (const name of sorted) {
229
+ w.str(name)
230
+ item(w, named[name] as In)
231
+ }
232
+ }
233
+
234
+ /** Reads a map of names, a name twice refused, each value as `item` reads it. */
235
+ export function readNames<V>(r: Reader, item: (r: Reader) => V): Record<string, V> {
236
+ const named: Record<string, V> = {}
237
+ r.names(name => {
238
+ named[name] = item(r)
239
+ })
240
+ return named
241
+ }
242
+
234
243
  /** Orders strings as their UTF-8 bytes are ordered, which UTF-16's code units do not. */
235
244
  export function byCodePoint(a: string, b: string): number {
236
245
  const x = a[Symbol.iterator]()
@@ -360,25 +369,40 @@ type OutOf<C> = C extends Codec<infer O, never> ? O : never
360
369
  type InOf<C> = C extends Codec<unknown, infer I> ? I : never
361
370
 
362
371
  /** What a message reads: every field it holds, a field it lacks undefined. */
363
- export type Read<F extends Fields> = { [K in keyof F]?: OutOf<F[K][1]> }
372
+ export type Read<F extends Fields> = { [K in keyof F]?: OutOf<F[K][1]> | undefined }
364
373
 
365
374
  /** What a message is written from: a field undefined is left out. */
366
375
  export type Written<F extends Fields> = { [K in keyof F]?: InOf<F[K][1]> | undefined }
367
376
 
377
+ /**
378
+ * A message's reader and writer written out a field at a time, as
379
+ * crates/protocol writes every message of the schema: what its table of
380
+ * fields says, without walking the table.
381
+ */
382
+ export interface Made<F extends Fields> {
383
+ write(w: Writer, value: Written<F>): void
384
+ read(r: Reader): Read<F>
385
+ }
386
+
368
387
  /**
369
388
  * A message: a map of the fields its declaration names, keys ascending. A
370
389
  * field is written when its value is not undefined, so an absent field and a
371
390
  * zero one stay what the caller said; a key the declaration lacks is skipped
372
- * as it reads, well formed and within bounds.
391
+ * as it reads, well formed and within bounds. A message the schema declares
392
+ * is read and written by its own code; one declared by hand, by its table.
373
393
  */
374
394
  export class Message<F extends Fields> implements Codec<Read<F>, Written<F>> {
375
395
  readonly kind = 'message'
376
396
  readonly name: string
377
397
  readonly fields: F
398
+ /** Writes the message at the writer's end; a function of its own, to be passed as one. */
399
+ readonly write: (w: Writer, value: Written<F>) => void
400
+ /** Reads the message the reader is at; a function of its own, to be passed as one. */
401
+ readonly read: (r: Reader) => Read<F>
378
402
  readonly #order: { name: string; key: number; codec: Codec<unknown, unknown> }[]
379
403
  readonly #byKey: ({ name: string; codec: Codec<unknown, unknown> } | undefined)[] = []
380
404
 
381
- constructor(name: string, fields: F) {
405
+ constructor(name: string, fields: F, made?: Made<F>) {
382
406
  this.name = name
383
407
  this.fields = fields
384
408
  this.#order = Object.entries(fields)
@@ -391,24 +415,37 @@ export class Message<F extends Fields> implements Codec<Read<F>, Written<F>> {
391
415
  for (const f of this.#order) {
392
416
  this.#byKey[f.key] = f
393
417
  }
418
+ this.write = made?.write ?? ((w, value) => this.#write(w, value))
419
+ this.read = made?.read ?? (r => this.#read(r))
394
420
  }
395
421
 
396
- write(w: Writer, value: Written<F>): void {
422
+ #write(w: Writer, value: Written<F>): void {
397
423
  const held = value as Record<string, unknown>
398
- let n = 0
399
- for (const f of this.#order) {
400
- if (held[f.name] !== undefined) {
401
- n++
424
+ const order = this.#order
425
+ if (order.length > 15) {
426
+ let n = 0
427
+ for (const f of order) {
428
+ if (held[f.name] !== undefined) {
429
+ n++
430
+ }
402
431
  }
432
+ w.map(n)
403
433
  }
404
- w.map(n)
405
- for (const f of this.#order) {
434
+ // up to 15 fields are counted as they are written, into the byte kept for the count
435
+ const count = order.length > 15 ? -1 : w.reserve(1)
436
+ let n = 0
437
+ for (let i = 0; i < order.length; i++) {
438
+ const f = order[i]!
406
439
  const v = held[f.name]
407
440
  if (v !== undefined) {
441
+ n++
408
442
  w.uint(f.key)
409
443
  f.codec.write(w, v)
410
444
  }
411
445
  }
446
+ if (count >= 0) {
447
+ w.mapAt(count, n)
448
+ }
412
449
  }
413
450
 
414
451
  /** The message's bytes. */
@@ -426,7 +463,7 @@ export class Message<F extends Fields> implements Codec<Read<F>, Written<F>> {
426
463
  return value
427
464
  }
428
465
 
429
- read(r: Reader): Read<F> {
466
+ #read(r: Reader): Read<F> {
430
467
  const read: Record<string, unknown> = {}
431
468
  r.fields(key => {
432
469
  const f = this.#byKey[key]
@@ -440,6 +477,10 @@ export class Message<F extends Fields> implements Codec<Read<F>, Written<F>> {
440
477
  }
441
478
  }
442
479
 
443
- export function message<const F extends Fields>(name: string, fields: F): Message<F> {
444
- return new Message(name, fields)
480
+ export function message<const F extends Fields>(
481
+ name: string,
482
+ fields: F,
483
+ made?: Made<NoInfer<F>>,
484
+ ): Message<F> {
485
+ return new Message(name, fields, made)
445
486
  }
package/src/wire/frame.ts CHANGED
@@ -63,16 +63,51 @@ export function kindName(kind: number): string {
63
63
 
64
64
  /** Reads a header from its twelve bytes without checking it. */
65
65
  export function parseHeader(bytes: Uint8Array, at = 0): Header {
66
- const view = new DataView(bytes.buffer, bytes.byteOffset + at, headerSize)
66
+ if (bytes.length - at < headerSize) {
67
+ throw new ProtocolError(`a header of ${bytes.length - at} bytes`)
68
+ }
67
69
  return {
68
- length: view.getUint32(0, true),
69
- kind: view.getUint8(4),
70
- flags: view.getUint8(5),
71
- method: view.getUint16(6, true),
72
- stream: view.getUint32(8, true),
70
+ length: uint32At(bytes, at),
71
+ kind: bytes[at + 4]!,
72
+ flags: bytes[at + 5]!,
73
+ method: bytes[at + 6]! | (bytes[at + 7]! << 8),
74
+ stream: uint32At(bytes, at + 8),
73
75
  }
74
76
  }
75
77
 
78
+ /** Writes a header where its twelve bytes were kept, once its body's length is known. */
79
+ export function putHeader(
80
+ bytes: Uint8Array,
81
+ at: number,
82
+ kind: number,
83
+ flags: number,
84
+ method: number,
85
+ stream: number,
86
+ length: number,
87
+ ): void {
88
+ putUint32(bytes, at, length)
89
+ bytes[at + 4] = kind
90
+ bytes[at + 5] = flags
91
+ bytes[at + 6] = method
92
+ bytes[at + 7] = method >>> 8
93
+ putUint32(bytes, at + 8, stream)
94
+ }
95
+
96
+ // Four bytes, the low one first. Not through a DataView: asking a small array
97
+ // for its buffer makes the engine move it to one, for every frame.
98
+ function uint32At(bytes: Uint8Array, at: number): number {
99
+ return (
100
+ (bytes[at]! | (bytes[at + 1]! << 8) | (bytes[at + 2]! << 16) | (bytes[at + 3]! << 24)) >>> 0
101
+ )
102
+ }
103
+
104
+ function putUint32(bytes: Uint8Array, at: number, n: number): void {
105
+ bytes[at] = n
106
+ bytes[at + 1] = n >>> 8
107
+ bytes[at + 2] = n >>> 16
108
+ bytes[at + 3] = n >>> 24
109
+ }
110
+
76
111
  /**
77
112
  * Refuses a header that breaks the protocol: an unknown kind, a flag its kind
78
113
  * does not define, ERROR without END, a method outside a REQUEST or none in
@@ -117,12 +152,7 @@ export function frame(
117
152
  body: Uint8Array,
118
153
  ): Uint8Array {
119
154
  const bytes = new Uint8Array(headerSize + body.length)
120
- const view = new DataView(bytes.buffer)
121
- view.setUint32(0, body.length, true)
122
- view.setUint8(4, kind)
123
- view.setUint8(5, flags)
124
- view.setUint16(6, method, true)
125
- view.setUint32(8, stream, true)
155
+ putHeader(bytes, 0, kind, flags, method, stream, body.length)
126
156
  bytes.set(body, headerSize)
127
157
  return bytes
128
158
  }
@@ -130,10 +160,10 @@ export function frame(
130
160
  /** A CREDIT granting n bytes on a stream. */
131
161
  export function credit(stream: number, n: number): Uint8Array {
132
162
  const body = new Uint8Array(4)
133
- new DataView(body.buffer).setUint32(0, n, true)
163
+ putUint32(body, 0, n)
134
164
  return frame(Kind.credit, 0, 0, stream, body)
135
165
  }
136
166
 
137
167
  export function granted(body: Uint8Array): number {
138
- return new DataView(body.buffer, body.byteOffset, 4).getUint32(0, true)
168
+ return uint32At(body, 0)
139
169
  }
@@ -81,6 +81,9 @@ export const methods = {
81
81
  'metrics.aggregate': 0x0603,
82
82
  'metrics.drop': 0x0604,
83
83
  'metrics.explain': 0x0605,
84
+ 'metrics.latest': 0x0606,
85
+ 'metrics.describe': 0x0607,
86
+ 'metrics.described': 0x0608,
84
87
  } as const
85
88
 
86
89
  export type Method = keyof typeof methods
@@ -122,6 +125,8 @@ export const Empty = message('empty', {})
122
125
 
123
126
  export const Clock = message('clock', { at: [1, int], advance: [2, uint] })
124
127
 
128
+ export const Backup = message('backup', { files: [1, list(str)] })
129
+
125
130
  export const KvWindow = message('kv.window', { name: [1, str], limit: [2, uint], per: [3, uint] })
126
131
 
127
132
  export const KvBucket = message('kv.bucket', {
@@ -136,6 +141,7 @@ export const KvBucket = message('kv.bucket', {
136
141
  burst: [9, uint],
137
142
  once: [10, bool],
138
143
  windows: [11, list(KvWindow)],
144
+ in: [12, str],
139
145
  })
140
146
 
141
147
  const kvCall = {
@@ -158,6 +164,8 @@ export const KvOperation = message('kv.operation', { method: [0, uint], ...kvCal
158
164
 
159
165
  export const KvCalls = message('kv.calls', { calls: [1, list(KvOperation)] })
160
166
 
167
+ export const KvChanges = message('kv.changes', { handle: [1, uint], calls: [2, list(KvOperation)] })
168
+
161
169
  export const KvEntry = message('kv.entry', {
162
170
  found: [1, bool],
163
171
  value: [2, kvValue],
@@ -197,6 +205,7 @@ export const JobsRepeat = message('jobs.repeat', {
197
205
  cron: [1, str],
198
206
  zone: [2, str],
199
207
  every: [3, uint],
208
+ spread: [4, bool],
200
209
  })
201
210
 
202
211
  export const JobsQueue = message('jobs.queue', {
@@ -211,6 +220,9 @@ export const JobsQueue = message('jobs.queue', {
211
220
  schedule: [9, JobsRepeat],
212
221
  maxRunning: [10, uint],
213
222
  in: [11, str],
223
+ maxRunningInGroup: [12, uint],
224
+ rate: [13, uint],
225
+ per: [14, uint],
214
226
  })
215
227
 
216
228
  const jobsJob = {
@@ -219,6 +231,8 @@ const jobsJob = {
219
231
  at: [3, int],
220
232
  after: [4, uint],
221
233
  repeat: [5, JobsRepeat],
234
+ move: [7, bool],
235
+ group: [8, str],
222
236
  } as const
223
237
 
224
238
  export const JobsJob = message('jobs.job', jobsJob)
@@ -356,6 +370,7 @@ export const SqlStatements = message('sql.statements', {
356
370
  statements: [2, list(SqlStatement)],
357
371
  read: [3, bool],
358
372
  jobs: [4, list(JobsBatch)],
373
+ kv: [5, list(KvChanges)],
359
374
  })
360
375
 
361
376
  export const SqlDone = message('sql.done', { changes: [1, int], lastId: [2, int] })
@@ -473,6 +488,7 @@ export const MetricsRange = message('metrics.range', {
473
488
  where: [11, list(MetricsCondition)],
474
489
  by: [12, list(str)],
475
490
  without: [13, list(str)],
491
+ lookback: [14, uint],
476
492
  })
477
493
 
478
494
  export const MetricsBuckets = message('metrics.buckets', {
@@ -488,6 +504,12 @@ export const MetricsBuckets = message('metrics.buckets', {
488
504
 
489
505
  export const MetricsLabels = message('metrics.labels', { labels: [1, names(str)] })
490
506
 
507
+ export const MetricsDescription = message('metrics.description', {
508
+ name: [1, str],
509
+ unit: [2, str],
510
+ help: [3, str],
511
+ })
512
+
491
513
  export const MetricsDropped = message('metrics.dropped', {
492
514
  found: [1, bool],
493
515
  unreadableGroups: [2, uint],
@@ -502,10 +524,12 @@ export const messages = {
502
524
  handle: Handle,
503
525
  empty: Empty,
504
526
  clock: Clock,
527
+ backup: Backup,
505
528
  'kv.bucket': KvBucket,
506
529
  'kv.call': KvCall,
507
530
  'kv.operation': KvOperation,
508
531
  'kv.calls': KvCalls,
532
+ 'kv.changes': KvChanges,
509
533
  'kv.entry': KvEntry,
510
534
  'kv.results': KvResults,
511
535
  'kv.page': KvPage,
@@ -558,5 +582,6 @@ export const messages = {
558
582
  'metrics.plan': MetricsPlan,
559
583
  'metrics.buckets': MetricsBuckets,
560
584
  'metrics.labels': MetricsLabels,
585
+ 'metrics.description': MetricsDescription,
561
586
  'metrics.dropped': MetricsDropped,
562
587
  } as const