@tinyshed/tinystore 0.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +262 -0
- package/bin/tinystore.js +50 -0
- package/dist/background.d.ts +30 -0
- package/dist/background.js +93 -0
- package/dist/blobs.d.ts +98 -0
- package/dist/blobs.js +299 -0
- package/dist/cancel.d.ts +14 -0
- package/dist/cancel.js +22 -0
- package/dist/clock.d.ts +21 -0
- package/dist/clock.js +35 -0
- package/dist/config.d.ts +60 -0
- package/dist/config.js +417 -0
- package/dist/connection.d.ts +78 -0
- package/dist/connection.js +405 -0
- package/dist/console.d.ts +48 -0
- package/dist/console.js +195 -0
- package/dist/errors.d.ts +103 -0
- package/dist/errors.js +158 -0
- package/dist/handles.d.ts +28 -0
- package/dist/handles.js +37 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +7 -0
- package/dist/jobs.d.ts +304 -0
- package/dist/jobs.js +684 -0
- package/dist/kv.d.ts +283 -0
- package/dist/kv.js +815 -0
- package/dist/limiter.d.ts +45 -0
- package/dist/limiter.js +68 -0
- package/dist/logger.d.ts +96 -0
- package/dist/logger.js +243 -0
- package/dist/metrics.d.ts +249 -0
- package/dist/metrics.js +610 -0
- package/dist/once.d.ts +31 -0
- package/dist/once.js +116 -0
- package/dist/quota.d.ts +44 -0
- package/dist/quota.js +98 -0
- package/dist/records.d.ts +182 -0
- package/dist/records.js +346 -0
- package/dist/runtime/bun.d.ts +2 -0
- package/dist/runtime/bun.js +164 -0
- package/dist/runtime/node.d.ts +2 -0
- package/dist/runtime/node.js +157 -0
- package/dist/runtime/shared.d.ts +25 -0
- package/dist/runtime/shared.js +45 -0
- package/dist/runtime.d.ts +48 -0
- package/dist/runtime.js +4 -0
- package/dist/schema.d.ts +39 -0
- package/dist/schema.js +21 -0
- package/dist/session.d.ts +120 -0
- package/dist/session.js +546 -0
- package/dist/sql.d.ts +121 -0
- package/dist/sql.js +342 -0
- package/dist/store.d.ts +103 -0
- package/dist/store.js +153 -0
- package/dist/time.d.ts +35 -0
- package/dist/time.js +44 -0
- package/dist/trace.d.ts +18 -0
- package/dist/trace.js +22 -0
- package/dist/wire/codec.d.ts +90 -0
- package/dist/wire/codec.js +383 -0
- package/dist/wire/frame.d.ts +41 -0
- package/dist/wire/frame.js +102 -0
- package/dist/wire/messages.d.ts +1403 -0
- package/dist/wire/messages.js +472 -0
- package/dist/wire/msgpack.d.ts +93 -0
- package/dist/wire/msgpack.js +680 -0
- package/package.json +40 -0
- package/src/background.ts +108 -0
- package/src/blobs.ts +437 -0
- package/src/cancel.ts +26 -0
- package/src/clock.ts +45 -0
- package/src/config.ts +501 -0
- package/src/connection.ts +512 -0
- package/src/console.ts +228 -0
- package/src/errors.ts +211 -0
- package/src/handles.ts +68 -0
- package/src/index.ts +92 -0
- package/src/jobs.ts +963 -0
- package/src/kv.ts +1093 -0
- package/src/limiter.ts +99 -0
- package/src/logger.ts +326 -0
- package/src/metrics.ts +896 -0
- package/src/once.ts +142 -0
- package/src/quota.ts +137 -0
- package/src/records.ts +537 -0
- package/src/runtime/bun.ts +190 -0
- package/src/runtime/node.ts +177 -0
- package/src/runtime/shared.ts +61 -0
- package/src/runtime.ts +57 -0
- package/src/schema.ts +69 -0
- package/src/session.ts +665 -0
- package/src/sql.ts +478 -0
- package/src/store.ts +219 -0
- package/src/time.ts +87 -0
- package/src/trace.ts +32 -0
- package/src/wire/codec.ts +445 -0
- package/src/wire/frame.ts +139 -0
- package/src/wire/messages.ts +562 -0
- package/src/wire/msgpack.ts +712 -0
package/src/console.ts
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// A logger's console line, as Go's records.Handler and Python's handler write
|
|
2
|
+
// it, byte for byte the lines of records/testdata/console.json: pretty for a
|
|
3
|
+
// person at a terminal, one JSON object a line for a collector.
|
|
4
|
+
//
|
|
5
|
+
// 11:02:11.123 WARN api slow request requestId=7f3a ms=1200
|
|
6
|
+
// {"time":"2026-10-02T11:02:11.123Z","level":"WARN","stream":"api","msg":"slow request","requestId":"7f3a","ms":1200}
|
|
7
|
+
|
|
8
|
+
/** How a logger writes its lines as they are logged; without one, pretty on a terminal and JSON otherwise. */
|
|
9
|
+
export type ConsoleFormat = 'pretty' | 'json' | 'off'
|
|
10
|
+
|
|
11
|
+
/** A line as a console writes it and the store keeps it: its fields already JSON. */
|
|
12
|
+
export interface ConsoleLine {
|
|
13
|
+
at: Date
|
|
14
|
+
stream: string
|
|
15
|
+
/** absent for an event */
|
|
16
|
+
level?: number
|
|
17
|
+
/** an event's name; a log line has none */
|
|
18
|
+
event?: string
|
|
19
|
+
msg?: string
|
|
20
|
+
/** key, JSON, key, JSON…, as the wire carries fields */
|
|
21
|
+
context: readonly string[]
|
|
22
|
+
attrs: readonly string[]
|
|
23
|
+
/** 32 hex digits */
|
|
24
|
+
traceId?: string
|
|
25
|
+
/** 16 hex digits */
|
|
26
|
+
spanId?: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A line as one JSON object and a newline, its keys in this order: time,
|
|
31
|
+
* level, stream, event, msg, the context's fields, the attributes', the trace
|
|
32
|
+
* and span. The time is UTC to the millisecond; a key repeated stays repeated.
|
|
33
|
+
*/
|
|
34
|
+
export function jsonLine(line: ConsoleLine): string {
|
|
35
|
+
let out = `{"time":"${line.at.toISOString()}"`
|
|
36
|
+
if (line.level !== undefined) {
|
|
37
|
+
out += `,"level":${JSON.stringify(levelLabel(line.level))}`
|
|
38
|
+
}
|
|
39
|
+
out += `,"stream":${JSON.stringify(line.stream)}`
|
|
40
|
+
if (line.event !== undefined) {
|
|
41
|
+
out += `,"event":${JSON.stringify(line.event)}`
|
|
42
|
+
}
|
|
43
|
+
if (line.msg !== undefined) {
|
|
44
|
+
out += `,"msg":${JSON.stringify(line.msg)}`
|
|
45
|
+
}
|
|
46
|
+
for (const fields of [line.context, line.attrs]) {
|
|
47
|
+
for (let i = 0; i < fields.length; i += 2) {
|
|
48
|
+
out += `,${JSON.stringify(fields[i])}:${fields[i + 1]}`
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (line.traceId !== undefined) {
|
|
52
|
+
out += `,"trace_id":"${line.traceId}"`
|
|
53
|
+
}
|
|
54
|
+
if (line.spanId !== undefined) {
|
|
55
|
+
out += `,"span_id":"${line.spanId}"`
|
|
56
|
+
}
|
|
57
|
+
return `${out}}\n`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// the terminal's own sixteen colours, so that its theme chooses the shades
|
|
61
|
+
const reset = '\x1b[0m'
|
|
62
|
+
const dim = '\x1b[2m'
|
|
63
|
+
const red = '\x1b[31m'
|
|
64
|
+
const green = '\x1b[32m'
|
|
65
|
+
const yellow = '\x1b[33m'
|
|
66
|
+
const blue = '\x1b[34m'
|
|
67
|
+
const magenta = '\x1b[35m'
|
|
68
|
+
const cyan = '\x1b[36m'
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A line as a person reads it: the time of day, the level padded to five, the
|
|
72
|
+
* stream, the message, the context's fields and the attributes', and the
|
|
73
|
+
* trace's first eight digits. A value of several lines, a stack or a
|
|
74
|
+
* traceback, follows the line, indented. A key or a string shows without its
|
|
75
|
+
* quotes when it is one word with no '=', quote or backslash.
|
|
76
|
+
*/
|
|
77
|
+
export function prettyLine(line: ConsoleLine, color: boolean, utc = false): string {
|
|
78
|
+
const paint = (hue: string, text: string) => (color ? hue + text + reset : text)
|
|
79
|
+
const [label, hue] = prettyLevel(line.level)
|
|
80
|
+
let out = `${paint(dim, clock(line.at, utc))} ${paint(hue, label)}${' '.repeat(Math.max(0, 5 - label.length))}`
|
|
81
|
+
out += ` ${paint(cyan, line.stream)}`
|
|
82
|
+
const message = prettyMessage(line)
|
|
83
|
+
if (message !== '') {
|
|
84
|
+
out += ` ${message}`
|
|
85
|
+
}
|
|
86
|
+
let separator = ' '
|
|
87
|
+
const field = (key: string, text: string) => {
|
|
88
|
+
out += `${separator}${paint(dim, `${bare(key) ? key : JSON.stringify(key)}=`)}${text}`
|
|
89
|
+
separator = ' '
|
|
90
|
+
}
|
|
91
|
+
const below: [string, string][] = []
|
|
92
|
+
for (const fields of [line.context, line.attrs]) {
|
|
93
|
+
for (let i = 0; i < fields.length; i += 2) {
|
|
94
|
+
const [shown, lines] = prettyValue(fields[i + 1] as string)
|
|
95
|
+
if (lines) {
|
|
96
|
+
below.push([fields[i] as string, shown])
|
|
97
|
+
} else {
|
|
98
|
+
field(fields[i] as string, shown)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (line.traceId !== undefined) {
|
|
103
|
+
field('trace', line.traceId.slice(0, 8))
|
|
104
|
+
}
|
|
105
|
+
for (const [key, text] of below) {
|
|
106
|
+
text
|
|
107
|
+
.replace(/[\r\n]+$/, '')
|
|
108
|
+
.split('\n')
|
|
109
|
+
.forEach((part, i) => {
|
|
110
|
+
const shown = part.replace(/\r$/, '')
|
|
111
|
+
out += `\n ${paint(dim, i === 0 ? `${key}: ${shown}` : shown)}`
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
return `${out}\n`
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A level as slog spells it: INFO, WARN+2, DEBUG-4. */
|
|
118
|
+
export function levelLabel(level: number): string {
|
|
119
|
+
const spell = (base: string, from: number) => {
|
|
120
|
+
const off = level - from
|
|
121
|
+
return off === 0 ? base : `${base}${off > 0 ? '+' : ''}${off}`
|
|
122
|
+
}
|
|
123
|
+
if (level < 0) {
|
|
124
|
+
return spell('DEBUG', -4)
|
|
125
|
+
}
|
|
126
|
+
if (level < 4) {
|
|
127
|
+
return spell('INFO', 0)
|
|
128
|
+
}
|
|
129
|
+
if (level < 8) {
|
|
130
|
+
return spell('WARN', 4)
|
|
131
|
+
}
|
|
132
|
+
return spell('ERROR', 8)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function prettyLevel(level: number | undefined): [string, string] {
|
|
136
|
+
if (level === undefined) {
|
|
137
|
+
return ['EVENT', magenta]
|
|
138
|
+
}
|
|
139
|
+
const hue = level < 0 ? blue : level < 4 ? green : level < 8 ? yellow : red
|
|
140
|
+
return [levelLabel(level), hue]
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function prettyMessage(line: ConsoleLine): string {
|
|
144
|
+
const body = line.msg ?? ''
|
|
145
|
+
if (line.event === undefined) {
|
|
146
|
+
return body
|
|
147
|
+
}
|
|
148
|
+
return body === '' ? line.event : `${line.event} ${body}`
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** How a field's JSON shows, and whether it is text of several lines, which goes under the line. */
|
|
152
|
+
function prettyValue(json: string): [string, boolean] {
|
|
153
|
+
if (!json.startsWith('"')) {
|
|
154
|
+
return [json, false]
|
|
155
|
+
}
|
|
156
|
+
const text: string = JSON.parse(json)
|
|
157
|
+
if (text.includes('\n')) {
|
|
158
|
+
return [text, true]
|
|
159
|
+
}
|
|
160
|
+
return [bare(text) ? text : json, false]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** One word a person reads without its quotes: no character a space or below, DEL, '"', '=' or '\'. */
|
|
164
|
+
function bare(text: string): boolean {
|
|
165
|
+
if (text === '') {
|
|
166
|
+
return false
|
|
167
|
+
}
|
|
168
|
+
for (const c of text) {
|
|
169
|
+
const code = c.codePointAt(0) as number
|
|
170
|
+
if (code <= 0x20 || code === 0x7f || c === '"' || c === '=' || c === '\\') {
|
|
171
|
+
return false
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return true
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function clock(at: Date, utc: boolean): string {
|
|
178
|
+
const parts = utc
|
|
179
|
+
? [at.getUTCHours(), at.getUTCMinutes(), at.getUTCSeconds()]
|
|
180
|
+
: [at.getHours(), at.getMinutes(), at.getSeconds()]
|
|
181
|
+
const millis = utc ? at.getUTCMilliseconds() : at.getMilliseconds()
|
|
182
|
+
return `${parts.map(n => String(n).padStart(2, '0')).join(':')}.${String(millis).padStart(3, '0')}`
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Says whether a field's key hides its value: the key, or the part of a dotted
|
|
187
|
+
* key after its last dot, is one of the names, the case ignored.
|
|
188
|
+
*/
|
|
189
|
+
export function redactor(
|
|
190
|
+
names: readonly string[] | undefined,
|
|
191
|
+
): ((key: string) => boolean) | undefined {
|
|
192
|
+
if (names === undefined || names.length === 0) {
|
|
193
|
+
return undefined
|
|
194
|
+
}
|
|
195
|
+
const hidden = new Set(names.map(name => name.toLowerCase()))
|
|
196
|
+
return key => {
|
|
197
|
+
const lower = key.toLowerCase()
|
|
198
|
+
return hidden.has(lower) || hidden.has(lower.slice(lower.lastIndexOf('.') + 1))
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** What a hidden value becomes, in the store and on the console. */
|
|
203
|
+
export const redacted = '"[redacted]"'
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* A value as JSON, its keys at any depth hidden as hides says; a bigint is its
|
|
207
|
+
* digits, and what JSON cannot write is its text, so that a line is never lost
|
|
208
|
+
* to one field.
|
|
209
|
+
*/
|
|
210
|
+
export function jsonOf(value: unknown, hides?: (key: string) => boolean): string {
|
|
211
|
+
if (typeof value === 'bigint') {
|
|
212
|
+
return value.toString()
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
const json = JSON.stringify(value, (key, inner) => {
|
|
216
|
+
if (key !== '' && hides?.(key)) {
|
|
217
|
+
return '[redacted]'
|
|
218
|
+
}
|
|
219
|
+
return typeof inner === 'bigint' ? inner.toString() : inner
|
|
220
|
+
})
|
|
221
|
+
if (json !== undefined) {
|
|
222
|
+
return json
|
|
223
|
+
}
|
|
224
|
+
} catch {
|
|
225
|
+
// a cycle, or a toJSON that throws: the value's text below
|
|
226
|
+
}
|
|
227
|
+
return JSON.stringify(String(value))
|
|
228
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What failed, as the server's error or the SDK's own says it. Each code is a
|
|
3
|
+
* class of its own, so a caller catches `ConflictError` as a Go program asks
|
|
4
|
+
* `errors.Is(err, tinystore.ErrConflict)`.
|
|
5
|
+
*/
|
|
6
|
+
export type Code =
|
|
7
|
+
| 'invalid'
|
|
8
|
+
| 'limit'
|
|
9
|
+
| 'closed'
|
|
10
|
+
| 'in_use'
|
|
11
|
+
| 'conflict'
|
|
12
|
+
| 'corrupt'
|
|
13
|
+
| 'too_old'
|
|
14
|
+
| 'too_new'
|
|
15
|
+
| 'suspended'
|
|
16
|
+
| 'outcome_unknown'
|
|
17
|
+
| 'permission'
|
|
18
|
+
| 'unimplemented'
|
|
19
|
+
| 'cancelled'
|
|
20
|
+
| 'unavailable'
|
|
21
|
+
| 'internal'
|
|
22
|
+
| 'protocol'
|
|
23
|
+
| 'unauthenticated'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The item a failure is about, by the names the server gives: a bucket and a
|
|
27
|
+
* key, a queue and a key, a series' labels, a table and a constraint. A value
|
|
28
|
+
* whose bytes are not UTF-8, as a key of bytes may be, comes as its bytes.
|
|
29
|
+
*/
|
|
30
|
+
export type What = Record<string, string | Uint8Array>
|
|
31
|
+
|
|
32
|
+
export class TinystoreError extends Error {
|
|
33
|
+
readonly code: Code
|
|
34
|
+
readonly what: What
|
|
35
|
+
|
|
36
|
+
constructor(code: Code, message: string, what: What = {}) {
|
|
37
|
+
super(message)
|
|
38
|
+
this.name = new.target.name
|
|
39
|
+
this.code = code
|
|
40
|
+
this.what = what
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The request cannot be done as asked. */
|
|
45
|
+
export class InvalidError extends TinystoreError {
|
|
46
|
+
constructor(message: string, what?: What) {
|
|
47
|
+
super('invalid', message, what)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A bound: memory, size or count. Later, or smaller, it may go through. A
|
|
53
|
+
* limit the store names says which, what the call would have taken of it,
|
|
54
|
+
* and the bound: `decoded samples`, 120000, 100000.
|
|
55
|
+
*/
|
|
56
|
+
export class LimitError extends TinystoreError {
|
|
57
|
+
readonly limit: string | undefined
|
|
58
|
+
readonly wanted: number | undefined
|
|
59
|
+
readonly bound: number | undefined
|
|
60
|
+
|
|
61
|
+
constructor(message: string, what?: What) {
|
|
62
|
+
super('limit', message, what)
|
|
63
|
+
this.limit = typeof what?.limit === 'string' ? what.limit : undefined
|
|
64
|
+
this.wanted = typeof what?.wanted === 'string' ? Number(what.wanted) : undefined
|
|
65
|
+
this.bound = typeof what?.bound === 'string' ? Number(what.bound) : undefined
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The store, the handle or the connection closed. */
|
|
70
|
+
export class ClosedError extends TinystoreError {
|
|
71
|
+
constructor(message: string, what?: What) {
|
|
72
|
+
super('closed', message, what)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A name is taken. */
|
|
77
|
+
export class InUseError extends TinystoreError {
|
|
78
|
+
constructor(message: string, what?: What) {
|
|
79
|
+
super('in_use', message, what)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A condition or a version no longer holds: read again, then decide. */
|
|
84
|
+
export class ConflictError extends TinystoreError {
|
|
85
|
+
constructor(message: string, what?: What) {
|
|
86
|
+
super('conflict', message, what)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Stored bytes no longer read. */
|
|
91
|
+
export class CorruptError extends TinystoreError {
|
|
92
|
+
constructor(message: string, what?: What) {
|
|
93
|
+
super('corrupt', message, what)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A time before its engine's window. */
|
|
98
|
+
export class TooOldError extends TinystoreError {
|
|
99
|
+
constructor(message: string, what?: What) {
|
|
100
|
+
super('too_old', message, what)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** A time past its engine's window, ahead of the store's clock. */
|
|
105
|
+
export class TooNewError extends TinystoreError {
|
|
106
|
+
constructor(message: string, what?: What) {
|
|
107
|
+
super('too_new', message, what)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A metrics series in quarantine until its repair. */
|
|
112
|
+
export class SuspendedError extends TinystoreError {
|
|
113
|
+
constructor(message: string, what?: What) {
|
|
114
|
+
super('suspended', message, what)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A write whose commit may or may not have happened: the connection was lost
|
|
120
|
+
* while it was in flight, or the server's commit failed. Read what it wrote
|
|
121
|
+
* before writing again.
|
|
122
|
+
*/
|
|
123
|
+
export class OutcomeUnknownError extends TinystoreError {
|
|
124
|
+
constructor(message: string, what?: What) {
|
|
125
|
+
super('outcome_unknown', message, what)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The connection's capability does not allow it: a data token changing a schema. */
|
|
130
|
+
export class PermissionDeniedError extends TinystoreError {
|
|
131
|
+
constructor(message: string, what?: What) {
|
|
132
|
+
super('permission', message, what)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** A method the server does not have. */
|
|
137
|
+
export class UnimplementedError extends TinystoreError {
|
|
138
|
+
constructor(message: string, what?: What) {
|
|
139
|
+
super('unimplemented', message, what)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The call was cancelled by its signal. */
|
|
144
|
+
export class CancelledError extends TinystoreError {
|
|
145
|
+
constructor(message: string, what?: What) {
|
|
146
|
+
super('cancelled', message, what)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The server is closing; the call was not run and may be sent on another connection. */
|
|
151
|
+
export class UnavailableError extends TinystoreError {
|
|
152
|
+
constructor(message: string, what?: What) {
|
|
153
|
+
super('unavailable', message, what)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** A fault of the server's own. */
|
|
158
|
+
export class InternalError extends TinystoreError {
|
|
159
|
+
constructor(message: string, what?: What) {
|
|
160
|
+
super('internal', message, what)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** A frame or a message that breaks the protocol; the connection ends with it. */
|
|
165
|
+
export class ProtocolError extends TinystoreError {
|
|
166
|
+
constructor(message: string, what?: What) {
|
|
167
|
+
super('protocol', message, what)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** A remote connection whose token the server does not know. */
|
|
172
|
+
export class UnauthenticatedError extends TinystoreError {
|
|
173
|
+
constructor(message: string, what?: What) {
|
|
174
|
+
super('unauthenticated', message, what)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const classes: Record<Code, new (message: string, what?: What) => TinystoreError> = {
|
|
179
|
+
invalid: InvalidError,
|
|
180
|
+
limit: LimitError,
|
|
181
|
+
closed: ClosedError,
|
|
182
|
+
in_use: InUseError,
|
|
183
|
+
conflict: ConflictError,
|
|
184
|
+
corrupt: CorruptError,
|
|
185
|
+
too_old: TooOldError,
|
|
186
|
+
too_new: TooNewError,
|
|
187
|
+
suspended: SuspendedError,
|
|
188
|
+
outcome_unknown: OutcomeUnknownError,
|
|
189
|
+
permission: PermissionDeniedError,
|
|
190
|
+
unimplemented: UnimplementedError,
|
|
191
|
+
cancelled: CancelledError,
|
|
192
|
+
unavailable: UnavailableError,
|
|
193
|
+
internal: InternalError,
|
|
194
|
+
protocol: ProtocolError,
|
|
195
|
+
unauthenticated: UnauthenticatedError,
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The codes this SDK knows, as `messages.json` lists the server's. */
|
|
199
|
+
export const codes = Object.keys(classes) as Code[]
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The error a code names. A code this SDK does not know yet is an
|
|
203
|
+
* `InternalError` keeping the code in its message.
|
|
204
|
+
*/
|
|
205
|
+
export function errorOf(code: string, message: string, what: What = {}): TinystoreError {
|
|
206
|
+
const known = classes[code as Code]
|
|
207
|
+
if (known === undefined) {
|
|
208
|
+
return new InternalError(`${code}: ${message}`, what)
|
|
209
|
+
}
|
|
210
|
+
return new known(message, what)
|
|
211
|
+
}
|
package/src/handles.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Connection } from './connection.ts'
|
|
2
|
+
import { InvalidError } from './errors.ts'
|
|
3
|
+
import type { Key } from './wire/codec.ts'
|
|
4
|
+
import { Handle } from './wire/messages.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The handle an open answers on a connection, opened once a connection: the
|
|
8
|
+
* open's own bytes name it, so a bucket opened twice with the same options
|
|
9
|
+
* shares its handle. An open that fails is tried again by the next call.
|
|
10
|
+
*/
|
|
11
|
+
export function handleOn(
|
|
12
|
+
connection: Connection,
|
|
13
|
+
method: number,
|
|
14
|
+
open: Uint8Array,
|
|
15
|
+
): Promise<number> {
|
|
16
|
+
const key = `${method}:${Buffer.from(open).toString('base64')}`
|
|
17
|
+
let opening = connection.handles.get(key)
|
|
18
|
+
if (opening === undefined) {
|
|
19
|
+
opening = connection.session.call(method, open).then(body => Handle.decode(body).handle ?? 0)
|
|
20
|
+
connection.handles.set(key, opening)
|
|
21
|
+
opening.catch(() => connection.handles.delete(key))
|
|
22
|
+
}
|
|
23
|
+
return opening
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A bucket's or a queue's name, as every engine takes it: [a-z0-9][a-z0-9_-]{0,63}. */
|
|
27
|
+
export function checkName(name: string, of: string): void {
|
|
28
|
+
if (!/^[a-z0-9][a-z0-9_-]{0,63}$/.test(name)) {
|
|
29
|
+
throw new InvalidError(
|
|
30
|
+
`the ${of} name ${JSON.stringify(name)}: a name is [a-z0-9][a-z0-9_-]{0,63}`,
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* What a batch's or a view's function returned, once the batch has settled: a
|
|
37
|
+
* promise its answer, an array or a tuple each element's as Promise.all gives
|
|
38
|
+
* them, and anything else itself.
|
|
39
|
+
*/
|
|
40
|
+
export type Settled<T> =
|
|
41
|
+
T extends PromiseLike<infer V>
|
|
42
|
+
? V
|
|
43
|
+
: T extends readonly unknown[]
|
|
44
|
+
? { -readonly [K in keyof T]: Awaited<T[K]> }
|
|
45
|
+
: T
|
|
46
|
+
|
|
47
|
+
/** Answers what a batch's function returned, once the batch's calls have settled. */
|
|
48
|
+
export async function settle<T>(returned: T): Promise<Settled<T>> {
|
|
49
|
+
return (Array.isArray(returned) ? await Promise.all(returned) : await returned) as Settled<T>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A page of a scan: its items, and where the next begins when more remain. */
|
|
53
|
+
export interface Page<T, After> {
|
|
54
|
+
items: T[]
|
|
55
|
+
/** the place the next page begins after; undefined once the scan has ended */
|
|
56
|
+
next: After | undefined
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A key's owners, each its text: an integer is its decimal spelling, as Go's Of takes it. */
|
|
60
|
+
export function ownerText(owner: Key): string | Uint8Array {
|
|
61
|
+
if (typeof owner === 'number' || typeof owner === 'bigint') {
|
|
62
|
+
if (typeof owner === 'number' && !Number.isSafeInteger(owner)) {
|
|
63
|
+
throw new InvalidError(`the owner ${owner} is not an integer; an owner is text or an integer`)
|
|
64
|
+
}
|
|
65
|
+
return String(owner)
|
|
66
|
+
}
|
|
67
|
+
return owner
|
|
68
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export type { BlobBucket, BlobObject, Blobs, Body, Download, PutOptions } from './blobs.ts'
|
|
2
|
+
export { withSignal } from './cancel.ts'
|
|
3
|
+
export type { Clock } from './clock.ts'
|
|
4
|
+
export type { Config, ConfigOptions, DeepPartial, Source } from './config.ts'
|
|
5
|
+
export type { ConsoleFormat } from './console.ts'
|
|
6
|
+
export * from './errors.ts'
|
|
7
|
+
export type { Page } from './handles.ts'
|
|
8
|
+
export type {
|
|
9
|
+
ClaimedJob,
|
|
10
|
+
EnqueueOptions,
|
|
11
|
+
Job,
|
|
12
|
+
JobEntry,
|
|
13
|
+
JobState,
|
|
14
|
+
Jobs,
|
|
15
|
+
Queue,
|
|
16
|
+
QueueOptions,
|
|
17
|
+
QueueTx,
|
|
18
|
+
Repeat,
|
|
19
|
+
WorkOptions,
|
|
20
|
+
} from './jobs.ts'
|
|
21
|
+
export type {
|
|
22
|
+
Batch,
|
|
23
|
+
Bucket,
|
|
24
|
+
BucketOptions,
|
|
25
|
+
BucketTx,
|
|
26
|
+
CounterOptions,
|
|
27
|
+
Counters,
|
|
28
|
+
Entry,
|
|
29
|
+
Kind,
|
|
30
|
+
Scanned,
|
|
31
|
+
Tx,
|
|
32
|
+
WriteOptions,
|
|
33
|
+
} from './kv.ts'
|
|
34
|
+
export type { Allowance, Limiter, LimiterOptions, Rate } from './limiter.ts'
|
|
35
|
+
export { type Logger, type LoggerOptions, logger } from './logger.ts'
|
|
36
|
+
export {
|
|
37
|
+
type Aggregate,
|
|
38
|
+
type AggregateOp,
|
|
39
|
+
type AggregateRange,
|
|
40
|
+
type Bucket as MetricsBucket,
|
|
41
|
+
Condition,
|
|
42
|
+
type Counter,
|
|
43
|
+
type Gauge,
|
|
44
|
+
type Labels,
|
|
45
|
+
type Metrics,
|
|
46
|
+
noneOf,
|
|
47
|
+
oneOf,
|
|
48
|
+
type Plan,
|
|
49
|
+
prefix,
|
|
50
|
+
type Range,
|
|
51
|
+
type Series,
|
|
52
|
+
type SeriesInput,
|
|
53
|
+
type SeriesKind,
|
|
54
|
+
type Timer,
|
|
55
|
+
} from './metrics.ts'
|
|
56
|
+
export type { Once, OnceOptions } from './once.ts'
|
|
57
|
+
export type { Quota, QuotaUsage, WindowUsage } from './quota.ts'
|
|
58
|
+
export {
|
|
59
|
+
type Cursor,
|
|
60
|
+
type Damage,
|
|
61
|
+
type Fields,
|
|
62
|
+
fields,
|
|
63
|
+
type Level,
|
|
64
|
+
type LinesWriter,
|
|
65
|
+
type LogRecord,
|
|
66
|
+
type RecordInput,
|
|
67
|
+
type Records,
|
|
68
|
+
type RecordsQuery,
|
|
69
|
+
} from './records.ts'
|
|
70
|
+
export type { StandardSchemaV1 } from './schema.ts'
|
|
71
|
+
export type {
|
|
72
|
+
Database,
|
|
73
|
+
Done,
|
|
74
|
+
Migrations,
|
|
75
|
+
Row,
|
|
76
|
+
SqlArg,
|
|
77
|
+
SqlBatch,
|
|
78
|
+
SqlOptions,
|
|
79
|
+
SqlValue,
|
|
80
|
+
Statement,
|
|
81
|
+
} from './sql.ts'
|
|
82
|
+
export {
|
|
83
|
+
type ConnectOptions,
|
|
84
|
+
connect,
|
|
85
|
+
type OpenOptions,
|
|
86
|
+
open,
|
|
87
|
+
type Status,
|
|
88
|
+
Store,
|
|
89
|
+
} from './store.ts'
|
|
90
|
+
export type { Duration, DurationText, Time } from './time.ts'
|
|
91
|
+
export { type Trace, withTrace } from './trace.ts'
|
|
92
|
+
export type { Key } from './wire/codec.ts'
|