@tinyshed/tinystore 0.1.0-rc.4 → 0.1.0-rc.6
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
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { Link } from './connection.ts';
|
|
2
|
+
import type { Key } from './wire/codec.ts';
|
|
3
|
+
/** A message as a subscriber receives it: what was published, and the branch it was published under. */
|
|
4
|
+
export interface Message<T> {
|
|
5
|
+
value: T;
|
|
6
|
+
branch: string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Messages a subscriber may have lost: it fell behind its branch's log, and
|
|
10
|
+
* `count` says how many; or its connection was lost and made again, and how
|
|
11
|
+
* many is unknown.
|
|
12
|
+
*/
|
|
13
|
+
export interface Missed {
|
|
14
|
+
reason: 'behind' | 'reconnect';
|
|
15
|
+
count?: number | undefined;
|
|
16
|
+
}
|
|
17
|
+
export interface SubscribeOptions {
|
|
18
|
+
/** take every branch below this one too */
|
|
19
|
+
recursive?: boolean | undefined;
|
|
20
|
+
/** told what was missed, in line with the messages; the store's log says it when absent */
|
|
21
|
+
onMissed?: ((missed: Missed) => unknown) | undefined;
|
|
22
|
+
/** told what the handler threw, with the message; the store's log says it when absent */
|
|
23
|
+
onError?: ((error: unknown, message: Message<unknown>) => unknown) | undefined;
|
|
24
|
+
}
|
|
25
|
+
export interface EachOptions {
|
|
26
|
+
recursive?: boolean | undefined;
|
|
27
|
+
onMissed?: ((missed: Missed) => unknown) | undefined;
|
|
28
|
+
}
|
|
29
|
+
/** A channel of messages of one type, or one of its branches: open it with `store.channel(name)`. */
|
|
30
|
+
export declare class Channel<T> {
|
|
31
|
+
#private;
|
|
32
|
+
readonly name: string;
|
|
33
|
+
readonly branch: readonly string[];
|
|
34
|
+
/** A channel comes from `store.channel` and its `under`. */
|
|
35
|
+
constructor(link: Link, name: string, branch?: readonly string[]);
|
|
36
|
+
/** The branch of owners under this one: whose messages these are. */
|
|
37
|
+
under(...owners: Key[]): Channel<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Hands value to every subscriber of this branch, and of the branches above
|
|
40
|
+
* it listened to recursive, that listens now. It answers once the store
|
|
41
|
+
* has the message, not once anyone handled it.
|
|
42
|
+
*/
|
|
43
|
+
publish(value: T): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Calls handler with each message published from now on, one at a time
|
|
46
|
+
* and in order: it answers once the store has the subscription, so that a
|
|
47
|
+
* message published after it arrives.
|
|
48
|
+
*
|
|
49
|
+
* const sub = await chat.under(roomId).subscribe(m => show(m.value), { onMissed: () => reload() })
|
|
50
|
+
* await sub.stop()
|
|
51
|
+
*/
|
|
52
|
+
subscribe(handler: (message: Message<T>) => unknown, options?: SubscribeOptions): Promise<Subscription>;
|
|
53
|
+
/**
|
|
54
|
+
* The messages published from now on, as a loop: it listens from its first
|
|
55
|
+
* turn, and break ends it. A throw in the loop's body ends it too.
|
|
56
|
+
*
|
|
57
|
+
* for await (const m of chat.under(roomId).each()) await save(m.value)
|
|
58
|
+
*/
|
|
59
|
+
each(options?: EachOptions): AsyncGenerator<Message<T>>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A subscription whose handler is called with each message: `stop` ends
|
|
63
|
+
* it, and the store's close does.
|
|
64
|
+
*/
|
|
65
|
+
export declare class Subscription {
|
|
66
|
+
#private;
|
|
67
|
+
/** A subscription comes from `channel.subscribe`. */
|
|
68
|
+
constructor(link: Link, asked: Uint8Array, shown: string);
|
|
69
|
+
/**
|
|
70
|
+
* No call starts after it, and it answers once the running call ended;
|
|
71
|
+
* called from inside the subscription's own handler, it answers at once.
|
|
72
|
+
*/
|
|
73
|
+
stop(): Promise<void>;
|
|
74
|
+
/** Registers the subscription, then runs its handler in the background. */
|
|
75
|
+
start(handler: (message: Message<unknown>) => unknown, options: SubscribeOptions | undefined): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
/** A publish's body: its channel, its branch and its value as JSON. */
|
|
78
|
+
export declare function publishBody(name: string, branch: readonly string[], value: unknown): Uint8Array;
|
|
79
|
+
/** A channel inside a transaction: its messages are handed over once the transaction commits. */
|
|
80
|
+
export interface TxChannel<T> {
|
|
81
|
+
under(...owners: Key[]): TxChannel<T>;
|
|
82
|
+
publish(value: T): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
/** A channel's name and branch, when handle is a channel. */
|
|
85
|
+
export declare function channelParts(handle: unknown): {
|
|
86
|
+
name: string;
|
|
87
|
+
branch: readonly string[];
|
|
88
|
+
} | undefined;
|
|
89
|
+
/** A channel inside a transaction, its publishes sent by send. */
|
|
90
|
+
export declare function txChannel<T>(name: string, branch: readonly string[], send: (body: Uint8Array) => Promise<void>): TxChannel<T>;
|
package/dist/channels.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// Channels, as docs/guides/channels/README.md has them: messages a program publishes
|
|
2
|
+
// on a named channel, which every subscriber listening at that moment
|
|
3
|
+
// receives, in memory and at most once. A subscription's handler is called
|
|
4
|
+
// one message at a time; a slow one lets the store's log run ahead and is
|
|
5
|
+
// told what it missed, and a connection lost is subscribed again and told
|
|
6
|
+
// that messages may have been lost meanwhile.
|
|
7
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
8
|
+
import { ClosedError, InvalidError, UnavailableError } from './errors.js';
|
|
9
|
+
import { checkName } from './handles.js';
|
|
10
|
+
import { keyText } from './kv.js';
|
|
11
|
+
import { sayOwn } from './logger.js';
|
|
12
|
+
import { ConnectionLostError, Stream } from './session.js';
|
|
13
|
+
import { ChannelsBatch, ChannelsPublish, ChannelsSubscribe, methods } from './wire/protocol.js';
|
|
14
|
+
/** How long a subscription without onMissed or onError waits between two lines of the store's log. */
|
|
15
|
+
const quiet = 60_000;
|
|
16
|
+
/** A channel of messages of one type, or one of its branches: open it with `store.channel(name)`. */
|
|
17
|
+
export class Channel {
|
|
18
|
+
name;
|
|
19
|
+
branch;
|
|
20
|
+
#link;
|
|
21
|
+
/** A channel comes from `store.channel` and its `under`. */
|
|
22
|
+
constructor(link, name, branch = []) {
|
|
23
|
+
if (branch.length === 0) {
|
|
24
|
+
checkName(name, 'channel');
|
|
25
|
+
}
|
|
26
|
+
this.#link = link;
|
|
27
|
+
this.name = name;
|
|
28
|
+
this.branch = branch;
|
|
29
|
+
}
|
|
30
|
+
/** The branch of owners under this one: whose messages these are. */
|
|
31
|
+
under(...owners) {
|
|
32
|
+
return new Channel(this.#link, this.name, [...this.branch, ...owners.map(keyText)]);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Hands value to every subscriber of this branch, and of the branches above
|
|
36
|
+
* it listened to recursive, that listens now. It answers once the store
|
|
37
|
+
* has the message, not once anyone handled it.
|
|
38
|
+
*/
|
|
39
|
+
async publish(value) {
|
|
40
|
+
const body = publishBody(this.name, this.branch, value);
|
|
41
|
+
await this.#link.run('write', connection => connection.session.call(methods['channels.publish'], body));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Calls handler with each message published from now on, one at a time
|
|
45
|
+
* and in order: it answers once the store has the subscription, so that a
|
|
46
|
+
* message published after it arrives.
|
|
47
|
+
*
|
|
48
|
+
* const sub = await chat.under(roomId).subscribe(m => show(m.value), { onMissed: () => reload() })
|
|
49
|
+
* await sub.stop()
|
|
50
|
+
*/
|
|
51
|
+
async subscribe(handler, options) {
|
|
52
|
+
const subscription = new Subscription(this.#link, this.#asked(options?.recursive), this.#shown());
|
|
53
|
+
await subscription.start(handler, options);
|
|
54
|
+
return subscription;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The messages published from now on, as a loop: it listens from its first
|
|
58
|
+
* turn, and break ends it. A throw in the loop's body ends it too.
|
|
59
|
+
*
|
|
60
|
+
* for await (const m of chat.under(roomId).each()) await save(m.value)
|
|
61
|
+
*/
|
|
62
|
+
async *each(options) {
|
|
63
|
+
const told = new Told(this.#shown(), options?.onMissed, undefined);
|
|
64
|
+
for (let first = true;; first = false) {
|
|
65
|
+
let stream;
|
|
66
|
+
try {
|
|
67
|
+
stream = await subscribed(this.#link, this.#asked(options?.recursive));
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
if (!first && again(err)) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
if (!first) {
|
|
76
|
+
await told.missed({ reason: 'reconnect' });
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
for (;;) {
|
|
80
|
+
const event = await stream.next();
|
|
81
|
+
if (event.end) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const batch = ChannelsBatch.decode(event.body);
|
|
85
|
+
if ((batch.missed ?? 0) > 0) {
|
|
86
|
+
await told.missed({ reason: 'behind', count: batch.missed });
|
|
87
|
+
}
|
|
88
|
+
for (const message of batch.messages ?? []) {
|
|
89
|
+
yield messageOf(message);
|
|
90
|
+
}
|
|
91
|
+
stream.consumed(event.body.length);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
if (!again(err)) {
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
stream.cancel();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
#asked(recursive) {
|
|
105
|
+
return ChannelsSubscribe.encode({
|
|
106
|
+
name: this.name,
|
|
107
|
+
under: [...this.branch],
|
|
108
|
+
recursive: recursive === true,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
#shown() {
|
|
112
|
+
return shown(this.name, this.branch);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* A subscription whose handler is called with each message: `stop` ends
|
|
117
|
+
* it, and the store's close does.
|
|
118
|
+
*/
|
|
119
|
+
export class Subscription {
|
|
120
|
+
#link;
|
|
121
|
+
#asked;
|
|
122
|
+
#shown;
|
|
123
|
+
#stream;
|
|
124
|
+
#stopped = false;
|
|
125
|
+
#done = Promise.resolve();
|
|
126
|
+
/** A subscription comes from `channel.subscribe`. */
|
|
127
|
+
constructor(link, asked, shown) {
|
|
128
|
+
this.#link = link;
|
|
129
|
+
this.#asked = asked;
|
|
130
|
+
this.#shown = shown;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* No call starts after it, and it answers once the running call ended;
|
|
134
|
+
* called from inside the subscription's own handler, it answers at once.
|
|
135
|
+
*/
|
|
136
|
+
async stop() {
|
|
137
|
+
this.#stopped = true;
|
|
138
|
+
this.#stream?.cancel();
|
|
139
|
+
if (inside.getStore() === this) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
await this.#done;
|
|
143
|
+
}
|
|
144
|
+
/** Registers the subscription, then runs its handler in the background. */
|
|
145
|
+
async start(handler, options) {
|
|
146
|
+
this.#stream = await subscribed(this.#link, this.#asked);
|
|
147
|
+
const told = new Told(this.#shown, options?.onMissed, options?.onError);
|
|
148
|
+
this.#done = this.#run(handler, told);
|
|
149
|
+
}
|
|
150
|
+
async #run(handler, told) {
|
|
151
|
+
for (;;) {
|
|
152
|
+
const stream = this.#stream;
|
|
153
|
+
if (stream === undefined || this.#stopped) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
await this.#deliver(stream, handler, told);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
if (this.#stopped || !again(err)) {
|
|
162
|
+
return told.ended(err, this.#stopped);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const made = await subscribed(this.#link, this.#asked).catch((err) => err);
|
|
166
|
+
if (!(made instanceof Stream)) {
|
|
167
|
+
return told.ended(made, this.#stopped);
|
|
168
|
+
}
|
|
169
|
+
this.#stream = made;
|
|
170
|
+
await told.missed({ reason: 'reconnect' });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Reads the stream's batches and calls the handler, one message at a time. */
|
|
174
|
+
async #deliver(stream, handler, told) {
|
|
175
|
+
for (;;) {
|
|
176
|
+
const event = await stream.next();
|
|
177
|
+
if (event.end) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const batch = ChannelsBatch.decode(event.body);
|
|
181
|
+
if ((batch.missed ?? 0) > 0 && !this.#stopped) {
|
|
182
|
+
await told.missed({ reason: 'behind', count: batch.missed });
|
|
183
|
+
}
|
|
184
|
+
for (const each of batch.messages ?? []) {
|
|
185
|
+
if (this.#stopped) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const message = messageOf(each);
|
|
189
|
+
try {
|
|
190
|
+
await inside.run(this, () => handler(message));
|
|
191
|
+
}
|
|
192
|
+
catch (err) {
|
|
193
|
+
await told.failed(err, message);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
stream.consumed(event.body.length);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
// The subscription whose handler the caller runs inside, so that its stop
|
|
201
|
+
// answers at once there rather than wait for itself.
|
|
202
|
+
const inside = new AsyncLocalStorage();
|
|
203
|
+
/** Opens a subscription's stream and waits for the RESPONSE that says the store has it. */
|
|
204
|
+
function subscribed(link, asked) {
|
|
205
|
+
return link.run('read', async (connection) => {
|
|
206
|
+
const stream = await connection.session.open(methods['channels.subscribe'], asked, true);
|
|
207
|
+
try {
|
|
208
|
+
await stream.next();
|
|
209
|
+
return stream;
|
|
210
|
+
}
|
|
211
|
+
catch (err) {
|
|
212
|
+
stream.cancel();
|
|
213
|
+
throw err;
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
/** Whether a subscription ended by its connection subscribes again on another. */
|
|
218
|
+
function again(err) {
|
|
219
|
+
return err instanceof ConnectionLostError || err instanceof UnavailableError;
|
|
220
|
+
}
|
|
221
|
+
function messageOf(message) {
|
|
222
|
+
return { value: JSON.parse(message.value ?? 'null'), branch: message.under ?? [] };
|
|
223
|
+
}
|
|
224
|
+
/** What tells a subscription's missed messages and its handler's throws: its own, or the store's log once a minute. */
|
|
225
|
+
class Told {
|
|
226
|
+
#shown;
|
|
227
|
+
#onMissed;
|
|
228
|
+
#onError;
|
|
229
|
+
#said = Number.NEGATIVE_INFINITY;
|
|
230
|
+
constructor(shown, onMissed, onError) {
|
|
231
|
+
this.#shown = shown;
|
|
232
|
+
this.#onMissed = onMissed;
|
|
233
|
+
this.#onError = onError;
|
|
234
|
+
}
|
|
235
|
+
async missed(missed) {
|
|
236
|
+
if (this.#onMissed !== undefined) {
|
|
237
|
+
await this.#onMissed(missed);
|
|
238
|
+
}
|
|
239
|
+
else if (this.#speaks()) {
|
|
240
|
+
sayOwn('warn', 'a subscriber may have missed messages', { channel: this.#shown, ...missed });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
async failed(error, message) {
|
|
244
|
+
if (this.#onError !== undefined) {
|
|
245
|
+
await this.#onError(error, message);
|
|
246
|
+
}
|
|
247
|
+
else if (this.#speaks()) {
|
|
248
|
+
sayOwn('warn', "a subscriber's handler threw", { channel: this.#shown, error });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/** Says why the subscription ended, unless it was stopped or its store closed. */
|
|
252
|
+
ended(err, stopped) {
|
|
253
|
+
if (!stopped && !(err instanceof ClosedError && !(err instanceof ConnectionLostError))) {
|
|
254
|
+
sayOwn('warn', 'a subscription ended', { channel: this.#shown, error: err });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
#speaks() {
|
|
258
|
+
const now = Date.now();
|
|
259
|
+
if (now - this.#said < quiet) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
this.#said = now;
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/** A channel's branch as an error names it: `channel chat: under "room-7"`. */
|
|
267
|
+
function shown(name, branch) {
|
|
268
|
+
if (branch.length === 0) {
|
|
269
|
+
return `channel ${name}`;
|
|
270
|
+
}
|
|
271
|
+
return `channel ${name}: under ${branch.map(owner => JSON.stringify(owner)).join(' ')}`;
|
|
272
|
+
}
|
|
273
|
+
/** A publish's body: its channel, its branch and its value as JSON. */
|
|
274
|
+
export function publishBody(name, branch, value) {
|
|
275
|
+
const json = JSON.stringify(value);
|
|
276
|
+
if (json === undefined) {
|
|
277
|
+
throw new InvalidError(`${shown(name, branch)}: a message that does not write as JSON`);
|
|
278
|
+
}
|
|
279
|
+
return ChannelsPublish.encode({ name, under: [...branch], value: json });
|
|
280
|
+
}
|
|
281
|
+
/** A channel's name and branch, when handle is a channel. */
|
|
282
|
+
export function channelParts(handle) {
|
|
283
|
+
return handle instanceof Channel ? { name: handle.name, branch: handle.branch } : undefined;
|
|
284
|
+
}
|
|
285
|
+
/** A channel inside a transaction, its publishes sent by send. */
|
|
286
|
+
export function txChannel(name, branch, send) {
|
|
287
|
+
return {
|
|
288
|
+
under: (...owners) => txChannel(name, [...branch, ...owners.map(keyText)], send),
|
|
289
|
+
publish: value => send(publishBody(name, branch, value)),
|
|
290
|
+
};
|
|
291
|
+
}
|
package/dist/clock.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// A test's clock: the time a private server opened with `clock` runs on, which
|
|
2
2
|
// a test moves forward rather than waits for.
|
|
3
3
|
import { ms, unixMs } from './time.js';
|
|
4
|
-
import {
|
|
4
|
+
import { methods, ServerClock } from './wire/protocol.js';
|
|
5
5
|
/**
|
|
6
6
|
* The clock of a store opened with `{ private: true, clock }`. Moving it
|
|
7
7
|
* forward expires keys, makes jobs due and ages records at once, as Go's
|
|
@@ -29,7 +29,7 @@ export class Clock {
|
|
|
29
29
|
return this.#move({ at: unixMs(to) });
|
|
30
30
|
}
|
|
31
31
|
async #move(fields) {
|
|
32
|
-
const body = await this.#link.run('write', connection => connection.session.call(methods['server.clock'],
|
|
33
|
-
return new Date(Number(
|
|
32
|
+
const body = await this.#link.run('write', connection => connection.session.call(methods['server.clock'], ServerClock.encode(fields)));
|
|
33
|
+
return new Date(Number(ServerClock.decode(body).at ?? 0));
|
|
34
34
|
}
|
|
35
35
|
}
|