@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.
- package/README.md +37 -37
- package/bin/tinystore.js +12 -1
- package/dist/backup.d.ts +33 -0
- package/dist/backup.js +49 -0
- package/dist/blobs.d.ts +160 -79
- package/dist/blobs.js +510 -217
- package/dist/channels.d.ts +90 -0
- package/dist/channels.js +291 -0
- package/dist/clock.js +3 -3
- package/dist/config.d.ts +209 -86
- package/dist/config.js +581 -538
- package/dist/connection.d.ts +64 -2
- package/dist/connection.js +201 -16
- package/dist/console.d.ts +27 -3
- package/dist/console.js +165 -12
- package/dist/dotenv.d.ts +7 -0
- package/dist/dotenv.js +79 -0
- package/dist/errors.d.ts +61 -1
- package/dist/errors.js +81 -1
- package/dist/handles.d.ts +44 -2
- package/dist/handles.js +81 -2
- package/dist/index.d.ts +17 -11
- package/dist/index.js +5 -3
- package/dist/instruments.d.ts +177 -0
- package/dist/instruments.js +819 -0
- package/dist/jobs.d.ts +335 -239
- package/dist/jobs.js +716 -536
- package/dist/json.d.ts +2 -0
- package/dist/json.js +122 -0
- package/dist/kv.d.ts +130 -243
- package/dist/kv.js +273 -640
- package/dist/limits.d.ts +80 -0
- package/dist/limits.js +139 -0
- package/dist/locks.d.ts +62 -0
- package/dist/locks.js +237 -0
- package/dist/logger.d.ts +45 -9
- package/dist/logger.js +150 -38
- package/dist/metrics.d.ts +210 -209
- package/dist/metrics.js +319 -556
- package/dist/once.d.ts +19 -18
- package/dist/once.js +74 -63
- package/dist/query.d.ts +181 -0
- package/dist/query.js +788 -0
- package/dist/records.d.ts +241 -150
- package/dist/records.js +507 -291
- package/dist/runtime/addon.d.ts +14 -0
- package/dist/runtime/addon.js +69 -0
- package/dist/runtime/bun.js +5 -7
- package/dist/runtime/node.js +5 -7
- package/dist/runtime/pipe.d.ts +17 -0
- package/dist/runtime/pipe.js +133 -0
- package/dist/runtime/shared.d.ts +6 -0
- package/dist/runtime/shared.js +19 -0
- package/dist/runtime.d.ts +5 -2
- package/dist/search.d.ts +189 -0
- package/dist/search.js +281 -0
- package/dist/session.d.ts +31 -3
- package/dist/session.js +153 -41
- package/dist/sql.d.ts +228 -78
- package/dist/sql.js +745 -240
- package/dist/store.d.ts +244 -24
- package/dist/store.js +270 -67
- package/dist/time.d.ts +5 -1
- package/dist/time.js +22 -2
- package/dist/tx.d.ts +61 -0
- package/dist/tx.js +314 -0
- package/dist/wire/codec.d.ts +23 -7
- package/dist/wire/codec.js +56 -35
- package/dist/wire/frame.d.ts +2 -0
- package/dist/wire/frame.js +31 -14
- package/dist/wire/messages.d.ts +259 -0
- package/dist/wire/messages.js +22 -0
- package/dist/wire/msgpack.d.ts +49 -1
- package/dist/wire/msgpack.js +293 -56
- package/dist/wire/protocol.d.ts +2683 -0
- package/dist/wire/protocol.js +10846 -0
- package/package.json +39 -38
- package/src/backup.ts +81 -0
- package/src/blobs.ts +678 -301
- package/src/channels.ts +377 -0
- package/src/clock.ts +4 -4
- package/src/config.ts +902 -612
- package/src/connection.ts +279 -21
- package/src/console.ts +180 -12
- package/src/dotenv.ts +82 -0
- package/src/errors.ts +105 -0
- package/src/handles.ts +157 -8
- package/src/index.ts +158 -52
- package/src/instruments.ts +1042 -0
- package/src/jobs.ts +1017 -733
- package/src/json.ts +133 -0
- package/src/kv.ts +424 -847
- package/src/limits.ts +202 -0
- package/src/locks.ts +320 -0
- package/src/logger.ts +209 -40
- package/src/metrics.ts +557 -752
- package/src/once.ts +86 -83
- package/src/query.ts +1005 -0
- package/src/records.ts +795 -418
- package/src/runtime/addon.ts +99 -0
- package/src/runtime/bun.ts +5 -7
- package/src/runtime/node.ts +5 -7
- package/src/runtime/pipe.ts +178 -0
- package/src/runtime/shared.ts +20 -0
- package/src/runtime.ts +5 -2
- package/src/search.ts +480 -0
- package/src/session.ts +187 -41
- package/src/sql.ts +993 -306
- package/src/store.ts +473 -78
- package/src/time.ts +48 -3
- package/src/tx.ts +426 -0
- package/src/wire/codec.ts +78 -37
- package/src/wire/frame.ts +44 -14
- package/src/wire/messages.ts +25 -0
- package/src/wire/msgpack.ts +327 -56
- package/src/wire/protocol.ts +11709 -0
- package/dist/limiter.d.ts +0 -45
- package/dist/limiter.js +0 -68
- package/dist/quota.d.ts +0 -44
- package/dist/quota.js +0 -98
- package/src/limiter.ts +0 -99
- package/src/quota.ts +0 -137
package/src/once.ts
CHANGED
|
@@ -1,108 +1,102 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// A function run once a key, its answer kept: a request sent twice is answered
|
|
2
|
+
// as the first was rather than done twice. The core lets one run of a key go
|
|
3
|
+
// at a time, every client of the store included: it answers the kept answer,
|
|
4
|
+
// or hands the run to this client and keeps what it sends back.
|
|
5
5
|
|
|
6
6
|
import { currentSignal } from './cancel.ts'
|
|
7
7
|
import type { Link } from './connection.ts'
|
|
8
|
-
import { OutcomeUnknownError } from './errors.ts'
|
|
9
|
-
import {
|
|
10
|
-
import type
|
|
11
|
-
import {
|
|
12
|
-
import type
|
|
8
|
+
import { OutcomeUnknownError, ProtocolError } from './errors.ts'
|
|
9
|
+
import { checkName, handleOn } from './handles.ts'
|
|
10
|
+
import { callOn, jsonValues, keyText, underField, type Values } from './kv.ts'
|
|
11
|
+
import { ConnectionLostError, type Stream, watch } from './session.ts'
|
|
12
|
+
import { type Duration, ms } from './time.ts'
|
|
13
13
|
import type { Key } from './wire/codec.ts'
|
|
14
|
-
import { KvCall,
|
|
14
|
+
import { KvAnswer, KvCall, KvFound, KvOnceOpen, methods } from './wire/protocol.ts'
|
|
15
15
|
|
|
16
16
|
export interface OnceOptions {
|
|
17
17
|
/** how long an answer is kept: a day when absent */
|
|
18
|
-
|
|
18
|
+
keep?: Duration | undefined
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
/** Answers kept once a key: open them with `store.
|
|
22
|
-
export class Once<
|
|
21
|
+
/** Answers kept once a key: open them with `store.once(name)`. */
|
|
22
|
+
export class Once<T> {
|
|
23
23
|
readonly name: string
|
|
24
24
|
readonly #link: Link
|
|
25
25
|
readonly #open: Uint8Array
|
|
26
|
-
readonly #values: Values<
|
|
27
|
-
readonly #
|
|
26
|
+
readonly #values: Values<T>
|
|
27
|
+
readonly #under: readonly string[]
|
|
28
28
|
|
|
29
|
-
/** Answers come from `store.
|
|
30
|
-
constructor(
|
|
31
|
-
link: Link,
|
|
32
|
-
name: string,
|
|
33
|
-
open: Uint8Array,
|
|
34
|
-
values: Values<V>,
|
|
35
|
-
owners: (string | Uint8Array)[],
|
|
36
|
-
) {
|
|
29
|
+
/** Answers come from `store.once` and their `under`. */
|
|
30
|
+
constructor(link: Link, name: string, open: Uint8Array, under: readonly string[]) {
|
|
37
31
|
this.#link = link
|
|
38
32
|
this.name = name
|
|
39
33
|
this.#open = open
|
|
40
|
-
this.#values =
|
|
41
|
-
this.#
|
|
34
|
+
this.#values = jsonValues as Values<T>
|
|
35
|
+
this.#under = under
|
|
42
36
|
}
|
|
43
37
|
|
|
44
38
|
/** The answers of a branch, each key apart from the same key elsewhere. */
|
|
45
|
-
|
|
46
|
-
return new Once(this.#link, this.name, this.#open, this.#
|
|
47
|
-
...this.#owners,
|
|
48
|
-
...owners.map(ownerText),
|
|
49
|
-
])
|
|
39
|
+
under(...owners: Key[]): Once<T> {
|
|
40
|
+
return new Once(this.#link, this.name, this.#open, [...this.#under, ...owners.map(keyText)])
|
|
50
41
|
}
|
|
51
42
|
|
|
52
43
|
/**
|
|
53
|
-
* The answer kept
|
|
54
|
-
* another
|
|
55
|
-
* keeps nothing and is thrown, so the next
|
|
56
|
-
* connection lost after fn returned and before its answer was
|
|
57
|
-
* OutcomeUnknownError.
|
|
44
|
+
* The answer kept for key, or fn's, which is kept. A run of a key that
|
|
45
|
+
* another runs, here or in another process, waits for it and gets its
|
|
46
|
+
* answer; a throw of fn keeps nothing and is thrown, so the next run runs
|
|
47
|
+
* fn again. A connection lost after fn returned and before its answer was
|
|
48
|
+
* kept is OutcomeUnknownError.
|
|
58
49
|
*
|
|
59
50
|
* const receipt = await charges.run(requestId, () => pay.charge(order, requestId))
|
|
60
51
|
*/
|
|
61
|
-
async run(key: Key, fn: () =>
|
|
62
|
-
const
|
|
63
|
-
if (
|
|
64
|
-
return
|
|
52
|
+
async run(key: Key, fn: () => T | Promise<T>): Promise<T> {
|
|
53
|
+
const handed = await this.#claim(key)
|
|
54
|
+
if (!(handed instanceof Run)) {
|
|
55
|
+
return handed.value
|
|
65
56
|
}
|
|
66
|
-
let answer:
|
|
67
|
-
let
|
|
57
|
+
let answer: T
|
|
58
|
+
let kept: Uint8Array
|
|
68
59
|
try {
|
|
69
60
|
answer = await fn()
|
|
70
|
-
|
|
61
|
+
kept = KvAnswer.encode({ found: true, value: this.#values.encode(answer) })
|
|
71
62
|
} catch (err) {
|
|
72
|
-
await end(
|
|
63
|
+
await handed.end(KvAnswer.encode({})).catch(() => {})
|
|
73
64
|
throw err
|
|
74
65
|
}
|
|
75
|
-
await end(
|
|
66
|
+
await handed.end(kept)
|
|
76
67
|
return answer
|
|
77
68
|
}
|
|
78
69
|
|
|
79
|
-
/** The answer kept
|
|
80
|
-
async get(key: Key): Promise<
|
|
81
|
-
const
|
|
82
|
-
|
|
70
|
+
/** The answer kept for key, undefined when none is. */
|
|
71
|
+
async get(key: Key): Promise<T | undefined> {
|
|
72
|
+
const body = await this.#call('kv.once.get', key, 'read')
|
|
73
|
+
const kept = KvAnswer.decode(body)
|
|
74
|
+
return kept.found === true ? this.#values.decode(kept.value ?? null) : undefined
|
|
83
75
|
}
|
|
84
76
|
|
|
85
|
-
/** Forgets the answer kept
|
|
86
|
-
async delete(key: Key): Promise<
|
|
87
|
-
await this.#call('kv.delete', key, 'write')
|
|
77
|
+
/** Forgets the answer kept for key, so that its next run runs fn again. */
|
|
78
|
+
async delete(key: Key): Promise<boolean> {
|
|
79
|
+
return KvFound.decode(await this.#call('kv.once.delete', key, 'write')).found === true
|
|
88
80
|
}
|
|
89
81
|
|
|
90
|
-
// asks for key's run: the answer kept
|
|
91
|
-
//
|
|
92
|
-
async #claim(
|
|
93
|
-
key: Key,
|
|
94
|
-
): Promise<{ stream?: Stream; kept?: ReturnType<typeof KvEntry.decode>['value'] }> {
|
|
82
|
+
// asks for key's run: the answer kept for it, or the run the core handed
|
|
83
|
+
// over, which a connection lost before the answer asks for again
|
|
84
|
+
async #claim(key: Key): Promise<Run | { value: T }> {
|
|
95
85
|
return this.#link.run('read', async connection => {
|
|
96
|
-
const handle = await handleOn(connection, methods['kv.open'], this.#open)
|
|
97
|
-
const
|
|
98
|
-
const stream = await connection.session.open(methods['kv.run'],
|
|
86
|
+
const handle = await handleOn(connection, methods['kv.once.open'], this.#open)
|
|
87
|
+
const call = KvCall.encode({ handle, under: underField(this.#under), key })
|
|
88
|
+
const stream = await connection.session.open(methods['kv.once.run'], call, false)
|
|
99
89
|
const unwatch = watch(currentSignal(), stream)
|
|
100
90
|
try {
|
|
101
91
|
const first = await stream.next()
|
|
102
|
-
|
|
103
|
-
|
|
92
|
+
const offered = KvAnswer.decode(first.body)
|
|
93
|
+
if (!first.end) {
|
|
94
|
+
return new Run(stream)
|
|
104
95
|
}
|
|
105
|
-
|
|
96
|
+
if (offered.found !== true) {
|
|
97
|
+
throw new ProtocolError(`kv once ${this.name}: a run ended without its answer`)
|
|
98
|
+
}
|
|
99
|
+
return { value: this.#values.decode(offered.value ?? null) }
|
|
106
100
|
} catch (err) {
|
|
107
101
|
stream.cancel()
|
|
108
102
|
throw err
|
|
@@ -112,31 +106,40 @@ export class Once<V> {
|
|
|
112
106
|
})
|
|
113
107
|
}
|
|
114
108
|
|
|
115
|
-
|
|
116
|
-
return this.#link.
|
|
117
|
-
const handle = await handleOn(connection, methods['kv.open'], this.#open)
|
|
118
|
-
const body = KvCall.encode({ handle, owners: this.#ownersField(), key })
|
|
119
|
-
return KvEntry.decode(await connection.session.call(methods[method], body))
|
|
120
|
-
})
|
|
109
|
+
#call(method: 'kv.once.get' | 'kv.once.delete', key: Key, idempotence: 'read' | 'write') {
|
|
110
|
+
return callOn(this.#link, 'kv.once.open', this.#open, this.#under, method, { key }, idempotence)
|
|
121
111
|
}
|
|
112
|
+
}
|
|
122
113
|
|
|
123
|
-
|
|
124
|
-
|
|
114
|
+
/** A run the core handed to this client, until it sends what to keep. */
|
|
115
|
+
class Run {
|
|
116
|
+
readonly #stream: Stream
|
|
117
|
+
|
|
118
|
+
constructor(stream: Stream) {
|
|
119
|
+
this.#stream = stream
|
|
125
120
|
}
|
|
126
|
-
}
|
|
127
121
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
122
|
+
/** Sends the run's answer as the stream's last DATA, and waits for the core's, once it is kept. */
|
|
123
|
+
async end(answer: Uint8Array): Promise<void> {
|
|
124
|
+
try {
|
|
125
|
+
await this.#stream.send(answer, true)
|
|
126
|
+
await this.#stream.next()
|
|
127
|
+
} catch (err) {
|
|
128
|
+
if (err instanceof ConnectionLostError) {
|
|
129
|
+
throw new OutcomeUnknownError(
|
|
130
|
+
`the connection was lost before the answer was kept; the next run may run again: ${err.message}`,
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
throw err
|
|
139
134
|
}
|
|
140
|
-
throw err
|
|
141
135
|
}
|
|
142
136
|
}
|
|
137
|
+
|
|
138
|
+
/** What a once's open sends, checked before anything leaves. */
|
|
139
|
+
export function onceOpen(name: string, options: OnceOptions = {}): Uint8Array {
|
|
140
|
+
checkName(name, 'once')
|
|
141
|
+
return KvOnceOpen.encode({
|
|
142
|
+
name,
|
|
143
|
+
keep: options.keep === undefined ? undefined : ms(options.keep),
|
|
144
|
+
})
|
|
145
|
+
}
|