@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
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Link } from './connection.ts';
|
|
2
|
+
import { type Unit } from './time.ts';
|
|
3
|
+
import type { Key } from './wire/codec.ts';
|
|
4
|
+
/**
|
|
5
|
+
* How many pass a span: '100/s', '5/10s', '1000/h', '300/7d', a span of one
|
|
6
|
+
* unit leaving out its 1. The type checks it as it is written.
|
|
7
|
+
*/
|
|
8
|
+
export type Rate = `${number}/${Unit}` | `${number}/${number}${Unit}`;
|
|
9
|
+
export interface LimiterOptions {
|
|
10
|
+
/** how many requests pass a span, each key on its own */
|
|
11
|
+
rate: Rate;
|
|
12
|
+
/** how many may pass at once before the rate holds the next; the rate's count when absent */
|
|
13
|
+
burst?: number;
|
|
14
|
+
}
|
|
15
|
+
/** What a limiter answers a request. */
|
|
16
|
+
export interface Allowance {
|
|
17
|
+
ok: boolean;
|
|
18
|
+
/** how many more requests would pass now */
|
|
19
|
+
left: number;
|
|
20
|
+
/** milliseconds until the request would pass; 0 when ok */
|
|
21
|
+
retryAfter: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A rate's count and span in milliseconds: a span of one unit may leave out
|
|
25
|
+
* its 1.
|
|
26
|
+
*
|
|
27
|
+
* '100/s' → 100 a 1000 ms, '5/10s' → 5 a 10000 ms
|
|
28
|
+
*/
|
|
29
|
+
export declare function rateOf(rate: string): {
|
|
30
|
+
count: number;
|
|
31
|
+
per: number;
|
|
32
|
+
};
|
|
33
|
+
/** A limiter: open it with `store.kv.limiter(name, { rate })`. */
|
|
34
|
+
export declare class Limiter {
|
|
35
|
+
#private;
|
|
36
|
+
readonly name: string;
|
|
37
|
+
/** Limiters come from `store.kv.limiter` and their `of`. */
|
|
38
|
+
constructor(link: Link, name: string, open: Uint8Array, owners: (string | Uint8Array)[]);
|
|
39
|
+
/** The limiter of a branch, each key apart from the same key elsewhere. */
|
|
40
|
+
of(...owners: Key[]): Limiter;
|
|
41
|
+
/** Asks for n requests of key, one unless given: all pass or none does; past the burst is InvalidError. */
|
|
42
|
+
allow(key: Key, n?: number): Promise<Allowance>;
|
|
43
|
+
}
|
|
44
|
+
/** A limiter's open: its name and rate, checked before anything leaves. */
|
|
45
|
+
export declare function limiterOpen(name: string, options: LimiterOptions): Uint8Array;
|
package/dist/limiter.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// A limiter of requests by key, as Go's kv.Limiter keeps one: the generic
|
|
2
|
+
// cell rate algorithm, one time a key, which the server holds in memory and
|
|
3
|
+
// writes every second, so a crash lets at most one burst more through.
|
|
4
|
+
import { InvalidError } from './errors.js';
|
|
5
|
+
import { checkName, handleOn, ownerText } from './handles.js';
|
|
6
|
+
import { ms } from './time.js';
|
|
7
|
+
import { KvAllowance, KvBucket, KvCall, methods } from './wire/messages.js';
|
|
8
|
+
/**
|
|
9
|
+
* A rate's count and span in milliseconds: a span of one unit may leave out
|
|
10
|
+
* its 1.
|
|
11
|
+
*
|
|
12
|
+
* '100/s' → 100 a 1000 ms, '5/10s' → 5 a 10000 ms
|
|
13
|
+
*/
|
|
14
|
+
export function rateOf(rate) {
|
|
15
|
+
const parts = /^(\d+)\/(\d*)([a-z]+)$/.exec(rate);
|
|
16
|
+
if (parts === null) {
|
|
17
|
+
throw new InvalidError(`the rate ${JSON.stringify(rate)}; write it as 100/s, 5/10s or 1000/h`);
|
|
18
|
+
}
|
|
19
|
+
const [, count, span, unit] = parts;
|
|
20
|
+
return { count: Number(count), per: ms(`${span === '' ? '1' : span}${unit}`) };
|
|
21
|
+
}
|
|
22
|
+
/** A limiter: open it with `store.kv.limiter(name, { rate })`. */
|
|
23
|
+
export class Limiter {
|
|
24
|
+
name;
|
|
25
|
+
#link;
|
|
26
|
+
#open;
|
|
27
|
+
#owners;
|
|
28
|
+
/** Limiters come from `store.kv.limiter` and their `of`. */
|
|
29
|
+
constructor(link, name, open, owners) {
|
|
30
|
+
this.#link = link;
|
|
31
|
+
this.name = name;
|
|
32
|
+
this.#open = open;
|
|
33
|
+
this.#owners = owners;
|
|
34
|
+
}
|
|
35
|
+
/** The limiter of a branch, each key apart from the same key elsewhere. */
|
|
36
|
+
of(...owners) {
|
|
37
|
+
return new Limiter(this.#link, this.name, this.#open, [
|
|
38
|
+
...this.#owners,
|
|
39
|
+
...owners.map(ownerText),
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
/** Asks for n requests of key, one unless given: all pass or none does; past the burst is InvalidError. */
|
|
43
|
+
async allow(key, n = 1) {
|
|
44
|
+
const answer = await this.#link.run('write', async (connection) => {
|
|
45
|
+
const handle = await handleOn(connection, methods['kv.open'], this.#open);
|
|
46
|
+
const body = KvCall.encode({
|
|
47
|
+
handle,
|
|
48
|
+
owners: this.#owners.length > 0 ? this.#owners : undefined,
|
|
49
|
+
key,
|
|
50
|
+
n: n === 1 ? undefined : n,
|
|
51
|
+
});
|
|
52
|
+
return KvAllowance.decode(await connection.session.call(methods['kv.allow'], body));
|
|
53
|
+
});
|
|
54
|
+
return { ok: answer.ok === true, left: answer.left ?? 0, retryAfter: answer.retryAfter ?? 0 };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** A limiter's open: its name and rate, checked before anything leaves. */
|
|
58
|
+
export function limiterOpen(name, options) {
|
|
59
|
+
checkName(name, 'limiter');
|
|
60
|
+
const { count, per } = rateOf(options.rate);
|
|
61
|
+
if (count < 1 || per < 1) {
|
|
62
|
+
throw new InvalidError(`limiter ${name}: a rate of ${options.rate}`);
|
|
63
|
+
}
|
|
64
|
+
if (options.burst !== undefined && (!Number.isInteger(options.burst) || options.burst < 1)) {
|
|
65
|
+
throw new InvalidError(`limiter ${name}: a burst of ${options.burst}`);
|
|
66
|
+
}
|
|
67
|
+
return KvBucket.encode({ name, rate: count, per, burst: options.burst });
|
|
68
|
+
}
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { DropNotice, FailureLog, type Say } from './background.ts';
|
|
2
|
+
import { type ConsoleFormat, type ConsoleLine } from './console.ts';
|
|
3
|
+
import type { Fields, Level } from './records.ts';
|
|
4
|
+
export interface LoggerOptions {
|
|
5
|
+
/** the lines held between writes; past it a line is dropped and counted: 1024 */
|
|
6
|
+
buffer?: number;
|
|
7
|
+
/** the least level a line is kept at; every level when absent */
|
|
8
|
+
level?: Level;
|
|
9
|
+
/** how a line is written as it is logged: pretty on a terminal and JSON otherwise when absent */
|
|
10
|
+
console?: ConsoleFormat;
|
|
11
|
+
/** fields whose values are hidden, in the store and on the console, at any depth, the case ignored */
|
|
12
|
+
redact?: readonly string[];
|
|
13
|
+
/** the console lines go to stdout rather than stderr */
|
|
14
|
+
stdout?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** Where a console line goes: a process's stream, or a test's. */
|
|
17
|
+
export interface ConsoleOut {
|
|
18
|
+
write(text: string): unknown;
|
|
19
|
+
readonly isTTY?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** What a logger and the loggers its `with` makes share: one buffer, one timer, one count, one console. */
|
|
22
|
+
interface Shared {
|
|
23
|
+
/** undefined for a logger of the console alone */
|
|
24
|
+
append: ((lines: ConsoleLine[]) => Promise<void>) | undefined;
|
|
25
|
+
stream: string;
|
|
26
|
+
most: number;
|
|
27
|
+
least: number;
|
|
28
|
+
hides: ((key: string) => boolean) | undefined;
|
|
29
|
+
print: ((line: ConsoleLine) => void) | undefined;
|
|
30
|
+
waiting: ConsoleLine[];
|
|
31
|
+
timer: ReturnType<typeof setInterval> | undefined;
|
|
32
|
+
writing: Promise<void> | undefined;
|
|
33
|
+
dropped: number;
|
|
34
|
+
/** undefined for a logger of the console alone, which writes nothing */
|
|
35
|
+
failures: FailureLog | undefined;
|
|
36
|
+
drops: DropNotice | undefined;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A logger of the console alone, for a program that wants the logger and not
|
|
40
|
+
* the records: its lines are written as a store's logger writes them, and
|
|
41
|
+
* `store.records.logger(stream)` in its place keeps them too.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* const log = logger('app', { redact: ['password'] })
|
|
45
|
+
* const db = log.with({ module: 'db' }) // passed to what needs it
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function logger(stream: string, options?: LoggerOptions): Logger;
|
|
49
|
+
/** A logger of one stream, its lines appended through append when it is a store's. */
|
|
50
|
+
export declare function newLogger(append: ((lines: ConsoleLine[]) => Promise<void>) | undefined, stream: string, options?: LoggerOptions, out?: ConsoleOut & {
|
|
51
|
+
utc?: boolean;
|
|
52
|
+
}): Logger;
|
|
53
|
+
/** TinyStore's own lines, on the console of a logger of stream tinystore made when first needed. */
|
|
54
|
+
export declare const sayOwn: Say;
|
|
55
|
+
/**
|
|
56
|
+
* A logger of one stream. `info`, `warn` and the rest return at once; a line
|
|
57
|
+
* goes to the console as it is logged, and becomes a record named `log`, its
|
|
58
|
+
* message the body, the call's fields its attributes and the fields of `with`
|
|
59
|
+
* its context, who is speaking.
|
|
60
|
+
*
|
|
61
|
+
* ```ts
|
|
62
|
+
* const log = store.records.logger('api')
|
|
63
|
+
* log.info('server started', { port: 3000 })
|
|
64
|
+
* log.with({ requestId }).warn('slow request', { ms: 1200 })
|
|
65
|
+
* log.event('user.created', { userId: 42 }) // a record named for the event
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare class Logger {
|
|
69
|
+
#private;
|
|
70
|
+
/** Loggers come from `logger(stream)`, `store.records.logger(stream)` and their `with`. */
|
|
71
|
+
constructor(shared: Shared, context: string[]);
|
|
72
|
+
/**
|
|
73
|
+
* The lines dropped since the logger began: a full buffer, or a write that
|
|
74
|
+
* failed. Each is also said on stderr, at most once in ten minutes.
|
|
75
|
+
*/
|
|
76
|
+
get dropped(): number;
|
|
77
|
+
debug(message: string, attrs?: Fields): void;
|
|
78
|
+
info(message: string, attrs?: Fields): void;
|
|
79
|
+
warn(message: string, attrs?: Fields): void;
|
|
80
|
+
error(message: string, attrs?: Fields): void;
|
|
81
|
+
/** An event of the stream: a record named for it, with no level and no body. */
|
|
82
|
+
event(name: string, attrs?: Fields): void;
|
|
83
|
+
/** The logger of the same stream, buffer and console, its context these fields besides its own. */
|
|
84
|
+
with(context: Fields): Logger;
|
|
85
|
+
/** Hands what the buffer holds to the server now, as the timer does every second. */
|
|
86
|
+
flush(): Promise<void>;
|
|
87
|
+
/** Hands over what the buffer holds and stops the timer; a later line starts it again. */
|
|
88
|
+
stop(): Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Fields as key, JSON pairs, as a console writes them and the store keeps
|
|
92
|
+
* them: an Error's value its stack, which JSON would write as {}, and a field
|
|
93
|
+
* hides names hidden at any depth.
|
|
94
|
+
*/
|
|
95
|
+
export declare function encodeFields(fields: Fields, hides: ((key: string) => boolean) | undefined): string[];
|
|
96
|
+
export {};
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// The application's own log lines, as Go's slog handler and Python's
|
|
2
|
+
// logging.Handler write them: each on the console as it is logged, and as a
|
|
3
|
+
// record when the logger is a store's. A call never waits for the server.
|
|
4
|
+
// Lines wait in a bounded buffer and go every second, or once half of it
|
|
5
|
+
// waits; what does not fit, or a write that fails, is dropped and counted,
|
|
6
|
+
// and said in TinyStore's own lines, since no call is waiting to be told.
|
|
7
|
+
import { DropNotice, FailureLog } from './background.js';
|
|
8
|
+
import { jsonLine, jsonOf, prettyLine, redacted, redactor, } from './console.js';
|
|
9
|
+
import { InvalidError } from './errors.js';
|
|
10
|
+
import { currentTrace } from './trace.js';
|
|
11
|
+
const levels = { debug: -4, info: 0, warn: 4, error: 8 };
|
|
12
|
+
const flushEvery = 1000;
|
|
13
|
+
/**
|
|
14
|
+
* A logger of the console alone, for a program that wants the logger and not
|
|
15
|
+
* the records: its lines are written as a store's logger writes them, and
|
|
16
|
+
* `store.records.logger(stream)` in its place keeps them too.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const log = logger('app', { redact: ['password'] })
|
|
20
|
+
* const db = log.with({ module: 'db' }) // passed to what needs it
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function logger(stream, options) {
|
|
24
|
+
return newLogger(undefined, stream, options);
|
|
25
|
+
}
|
|
26
|
+
/** A logger of one stream, its lines appended through append when it is a store's. */
|
|
27
|
+
export function newLogger(append, stream, options = {}, out) {
|
|
28
|
+
if (stream === '') {
|
|
29
|
+
throw new InvalidError('a logger of no stream');
|
|
30
|
+
}
|
|
31
|
+
const most = options.buffer ?? 1024;
|
|
32
|
+
if (!Number.isInteger(most) || most < 1) {
|
|
33
|
+
throw new InvalidError(`a logger's buffer of ${most} lines`);
|
|
34
|
+
}
|
|
35
|
+
const shared = {
|
|
36
|
+
append,
|
|
37
|
+
stream,
|
|
38
|
+
most,
|
|
39
|
+
least: options.level === undefined ? Number.NEGATIVE_INFINITY : levelOf(options.level),
|
|
40
|
+
hides: redactor(options.redact),
|
|
41
|
+
print: printer(options, out),
|
|
42
|
+
waiting: [],
|
|
43
|
+
timer: undefined,
|
|
44
|
+
writing: undefined,
|
|
45
|
+
dropped: 0,
|
|
46
|
+
failures: append === undefined ? undefined : new FailureLog(`records flush ${stream}`, sayOwn),
|
|
47
|
+
drops: append === undefined ? undefined : new DropNotice(stream, most, sayOwn),
|
|
48
|
+
};
|
|
49
|
+
return new Logger(shared, []);
|
|
50
|
+
}
|
|
51
|
+
let own;
|
|
52
|
+
/** TinyStore's own lines, on the console of a logger of stream tinystore made when first needed. */
|
|
53
|
+
export const sayOwn = (level, message, fields) => {
|
|
54
|
+
own ??= logger('tinystore');
|
|
55
|
+
own[level](message, fields);
|
|
56
|
+
};
|
|
57
|
+
/** How a logger prints its lines, or undefined when it prints none. */
|
|
58
|
+
function printer(options, out) {
|
|
59
|
+
if (options.console === 'off') {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
const stream = out ?? (options.stdout === true ? process.stdout : process.stderr);
|
|
63
|
+
const terminal = stream.isTTY === true;
|
|
64
|
+
const pretty = options.console === 'pretty' || (options.console === undefined && terminal);
|
|
65
|
+
if (!pretty) {
|
|
66
|
+
return line => void stream.write(jsonLine(line));
|
|
67
|
+
}
|
|
68
|
+
const color = terminal && out === undefined && !process.env.NO_COLOR && process.env.TERM !== 'dumb';
|
|
69
|
+
return line => void stream.write(prettyLine(line, color, out?.utc === true));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A logger of one stream. `info`, `warn` and the rest return at once; a line
|
|
73
|
+
* goes to the console as it is logged, and becomes a record named `log`, its
|
|
74
|
+
* message the body, the call's fields its attributes and the fields of `with`
|
|
75
|
+
* its context, who is speaking.
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* const log = store.records.logger('api')
|
|
79
|
+
* log.info('server started', { port: 3000 })
|
|
80
|
+
* log.with({ requestId }).warn('slow request', { ms: 1200 })
|
|
81
|
+
* log.event('user.created', { userId: 42 }) // a record named for the event
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export class Logger {
|
|
85
|
+
#shared;
|
|
86
|
+
/** key, JSON, key, JSON…, hidden as the logger's redact says */
|
|
87
|
+
#context;
|
|
88
|
+
/** Loggers come from `logger(stream)`, `store.records.logger(stream)` and their `with`. */
|
|
89
|
+
constructor(shared, context) {
|
|
90
|
+
this.#shared = shared;
|
|
91
|
+
this.#context = context;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The lines dropped since the logger began: a full buffer, or a write that
|
|
95
|
+
* failed. Each is also said on stderr, at most once in ten minutes.
|
|
96
|
+
*/
|
|
97
|
+
get dropped() {
|
|
98
|
+
return this.#shared.dropped;
|
|
99
|
+
}
|
|
100
|
+
debug(message, attrs) {
|
|
101
|
+
this.#log(levels.debug, message, attrs);
|
|
102
|
+
}
|
|
103
|
+
info(message, attrs) {
|
|
104
|
+
this.#log(levels.info, message, attrs);
|
|
105
|
+
}
|
|
106
|
+
warn(message, attrs) {
|
|
107
|
+
this.#log(levels.warn, message, attrs);
|
|
108
|
+
}
|
|
109
|
+
error(message, attrs) {
|
|
110
|
+
this.#log(levels.error, message, attrs);
|
|
111
|
+
}
|
|
112
|
+
/** An event of the stream: a record named for it, with no level and no body. */
|
|
113
|
+
event(name, attrs) {
|
|
114
|
+
if (name === '') {
|
|
115
|
+
throw new InvalidError('an event of no name');
|
|
116
|
+
}
|
|
117
|
+
this.#emit({ event: name }, attrs);
|
|
118
|
+
}
|
|
119
|
+
/** The logger of the same stream, buffer and console, its context these fields besides its own. */
|
|
120
|
+
with(context) {
|
|
121
|
+
return new Logger(this.#shared, [
|
|
122
|
+
...this.#context,
|
|
123
|
+
...encodeFields(context, this.#shared.hides),
|
|
124
|
+
]);
|
|
125
|
+
}
|
|
126
|
+
/** Hands what the buffer holds to the server now, as the timer does every second. */
|
|
127
|
+
async flush() {
|
|
128
|
+
const shared = this.#shared;
|
|
129
|
+
while (shared.writing !== undefined) {
|
|
130
|
+
await shared.writing;
|
|
131
|
+
}
|
|
132
|
+
shared.drops?.sayIfDue();
|
|
133
|
+
if (shared.waiting.length === 0 || shared.append === undefined) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
shared.writing = writeWaiting(shared, shared.append).finally(() => {
|
|
137
|
+
shared.writing = undefined;
|
|
138
|
+
});
|
|
139
|
+
await shared.writing;
|
|
140
|
+
}
|
|
141
|
+
/** Hands over what the buffer holds and stops the timer; a later line starts it again. */
|
|
142
|
+
async stop() {
|
|
143
|
+
await this.flush();
|
|
144
|
+
if (this.#shared.timer !== undefined) {
|
|
145
|
+
clearInterval(this.#shared.timer);
|
|
146
|
+
this.#shared.timer = undefined;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
#log(level, message, attrs) {
|
|
150
|
+
if (level < this.#shared.least) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
this.#emit({ level, msg: message }, attrs);
|
|
154
|
+
}
|
|
155
|
+
#emit(what, attrs) {
|
|
156
|
+
const trace = traceOf(currentTrace());
|
|
157
|
+
const line = {
|
|
158
|
+
at: new Date(),
|
|
159
|
+
stream: this.#shared.stream,
|
|
160
|
+
...what,
|
|
161
|
+
context: this.#context,
|
|
162
|
+
attrs: attrs === undefined ? [] : encodeFields(attrs, this.#shared.hides),
|
|
163
|
+
...trace,
|
|
164
|
+
};
|
|
165
|
+
this.#shared.print?.(line);
|
|
166
|
+
this.#queue(line);
|
|
167
|
+
}
|
|
168
|
+
#queue(line) {
|
|
169
|
+
const shared = this.#shared;
|
|
170
|
+
if (shared.append === undefined) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (shared.waiting.length >= shared.most) {
|
|
174
|
+
shared.dropped++;
|
|
175
|
+
shared.drops?.dropped();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
shared.waiting.push(line);
|
|
179
|
+
if (shared.timer === undefined) {
|
|
180
|
+
shared.timer = setInterval(() => void this.flush(), flushEvery);
|
|
181
|
+
shared.timer.unref?.();
|
|
182
|
+
}
|
|
183
|
+
if (shared.waiting.length >= shared.most / 2 && shared.writing === undefined) {
|
|
184
|
+
void this.flush();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Writes what the buffer holds, and again while half of it filled during the
|
|
190
|
+
* write before. A full buffer drops a line before the line could ask for a
|
|
191
|
+
* write, so a burst that filled it while a write ran would otherwise wait for
|
|
192
|
+
* the timer's next second, every line until then dropped.
|
|
193
|
+
*/
|
|
194
|
+
async function writeWaiting(shared, append) {
|
|
195
|
+
let batch = shared.waiting.splice(0);
|
|
196
|
+
while (batch.length > 0) {
|
|
197
|
+
try {
|
|
198
|
+
await append(batch);
|
|
199
|
+
shared.failures?.succeeded();
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
shared.dropped += batch.length;
|
|
203
|
+
shared.failures?.failed(err);
|
|
204
|
+
}
|
|
205
|
+
batch = shared.waiting.length >= shared.most / 2 ? shared.waiting.splice(0) : [];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Fields as key, JSON pairs, as a console writes them and the store keeps
|
|
210
|
+
* them: an Error's value its stack, which JSON would write as {}, and a field
|
|
211
|
+
* hides names hidden at any depth.
|
|
212
|
+
*/
|
|
213
|
+
export function encodeFields(fields, hides) {
|
|
214
|
+
const pairs = Array.isArray(fields)
|
|
215
|
+
? fields
|
|
216
|
+
: Object.entries(fields);
|
|
217
|
+
const flat = [];
|
|
218
|
+
for (const [key, value] of pairs) {
|
|
219
|
+
const shown = value instanceof Error ? (value.stack ?? String(value)) : value;
|
|
220
|
+
flat.push(key, hides?.(key) ? redacted : jsonOf(shown, hides));
|
|
221
|
+
}
|
|
222
|
+
return flat;
|
|
223
|
+
}
|
|
224
|
+
function levelOf(level) {
|
|
225
|
+
return typeof level === 'number' ? level : levels[level];
|
|
226
|
+
}
|
|
227
|
+
/** A trace's ids as hex, or none when they are not a trace's sixteen bytes and a span's eight. */
|
|
228
|
+
function traceOf(trace) {
|
|
229
|
+
if (trace === undefined) {
|
|
230
|
+
return {};
|
|
231
|
+
}
|
|
232
|
+
const hex = (id) => typeof id === 'string'
|
|
233
|
+
? id.toLowerCase()
|
|
234
|
+
: id === undefined
|
|
235
|
+
? undefined
|
|
236
|
+
: Buffer.from(id).toString('hex');
|
|
237
|
+
const traceId = hex(trace.traceId);
|
|
238
|
+
const spanId = hex(trace.spanId);
|
|
239
|
+
if (traceId === undefined || !/^[0-9a-f]{32}$/.test(traceId)) {
|
|
240
|
+
return {};
|
|
241
|
+
}
|
|
242
|
+
return spanId !== undefined && /^[0-9a-f]{16}$/.test(spanId) ? { traceId, spanId } : { traceId };
|
|
243
|
+
}
|