@tinyshed/tinystore 0.5.1 → 0.7.0
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/dist/config.d.ts +67 -4
- package/dist/config.js +127 -13
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/logger.d.ts +10 -0
- package/dist/logger.js +16 -6
- package/dist/metrics.d.ts +17 -0
- package/dist/metrics.js +22 -1
- package/dist/records.d.ts +90 -5
- package/dist/records.js +114 -6
- package/dist/sql.d.ts +19 -3
- package/dist/sql.js +31 -4
- package/dist/wire/protocol.d.ts +105 -9
- package/dist/wire/protocol.js +296 -8
- package/package.json +8 -8
- package/src/config.ts +204 -25
- package/src/index.ts +13 -1
- package/src/logger.ts +17 -7
- package/src/metrics.ts +25 -0
- package/src/records.ts +178 -7
- package/src/sql.ts +48 -7
- package/src/wire/protocol.ts +321 -8
package/src/config.ts
CHANGED
|
@@ -36,7 +36,7 @@ const layerMark = '~layer'
|
|
|
36
36
|
const attempts = 8
|
|
37
37
|
|
|
38
38
|
// The declaration as the core reads it: docs/guides/config/README.md, and
|
|
39
|
-
// crates/tinystore/src/
|
|
39
|
+
// crates/tinystore/src/config/data.rs, which reads this JSON.
|
|
40
40
|
type Kind =
|
|
41
41
|
| 'text'
|
|
42
42
|
| 'integer'
|
|
@@ -62,6 +62,12 @@ interface Described extends Rules {
|
|
|
62
62
|
required?: boolean
|
|
63
63
|
live?: boolean
|
|
64
64
|
secret?: boolean
|
|
65
|
+
/** of a map of entries, each entry's group; its kind is 'object', as an older release reads it */
|
|
66
|
+
entries?: Group
|
|
67
|
+
/** of a chosen group, the key of the setting beside it whose value names its group */
|
|
68
|
+
chosenBy?: string
|
|
69
|
+
/** of a chosen group, each group by the name its chooser gives it; its kind is 'object' too */
|
|
70
|
+
groups?: Record<string, Group>
|
|
65
71
|
}
|
|
66
72
|
|
|
67
73
|
/**
|
|
@@ -84,6 +90,25 @@ export interface Setting<
|
|
|
84
90
|
readonly '~secret'?: IsSecret
|
|
85
91
|
}
|
|
86
92
|
|
|
93
|
+
/**
|
|
94
|
+
* A map of named entries, as `entries` declares one: each entry the group
|
|
95
|
+
* `G`, read as its settings are.
|
|
96
|
+
*/
|
|
97
|
+
export interface Entries<G> extends Setting<Readonly<Record<string, Value<G>>>> {
|
|
98
|
+
/** what an object in code gives each entry; nothing at run time */
|
|
99
|
+
readonly '~entries': G
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A group whose settings are those of one of `G`, the one a text setting
|
|
104
|
+
* beside it names, as `chosenBy` declares one: each of `G`'s groups read as
|
|
105
|
+
* its settings are.
|
|
106
|
+
*/
|
|
107
|
+
export interface Chosen<G> extends Setting<Value<G[keyof G]>> {
|
|
108
|
+
/** what an object in code gives the group; nothing at run time */
|
|
109
|
+
readonly '~chosen': G
|
|
110
|
+
}
|
|
111
|
+
|
|
87
112
|
/** A setting's text that prints as *** wherever it goes; `reveal` gives it. */
|
|
88
113
|
export class Secret {
|
|
89
114
|
readonly #text: string
|
|
@@ -153,15 +178,19 @@ export type Changes<D> = {
|
|
|
153
178
|
|
|
154
179
|
/** An object in code over the declaration: any of its settings, at any depth, a secret as text. */
|
|
155
180
|
export type Overlay<D> = {
|
|
156
|
-
readonly [K in keyof D]?: D[K] extends
|
|
157
|
-
?
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
:
|
|
181
|
+
readonly [K in keyof D]?: D[K] extends Entries<infer G>
|
|
182
|
+
? Readonly<Record<string, Overlay<G>>>
|
|
183
|
+
: D[K] extends Chosen<infer G>
|
|
184
|
+
? Overlay<G[keyof G]>
|
|
185
|
+
: D[K] extends Setting<infer V, boolean, boolean, boolean>
|
|
186
|
+
? V extends Secret
|
|
187
|
+
? string
|
|
188
|
+
: V
|
|
189
|
+
: D[K] extends readonly unknown[]
|
|
190
|
+
? Value<D[K]>
|
|
191
|
+
: D[K] extends object
|
|
192
|
+
? Overlay<D[K]>
|
|
193
|
+
: Value<D[K]>
|
|
165
194
|
}
|
|
166
195
|
|
|
167
196
|
/** A file a config reads: TOML, YAML or JSON by its extension. */
|
|
@@ -169,6 +198,7 @@ export interface FileLayer {
|
|
|
169
198
|
readonly [layerMark]: 'file'
|
|
170
199
|
readonly path: string
|
|
171
200
|
readonly optional: boolean
|
|
201
|
+
readonly secrets: boolean
|
|
172
202
|
}
|
|
173
203
|
|
|
174
204
|
/** The environment a config reads. */
|
|
@@ -187,6 +217,12 @@ export type Layer<D> = FileLayer | EnvLayer | Overlay<D> | false | null | undefi
|
|
|
187
217
|
export interface FileOptions {
|
|
188
218
|
/** a file that may be absent: then it declares nothing, and explain says so */
|
|
189
219
|
optional?: boolean | undefined
|
|
220
|
+
/**
|
|
221
|
+
* false refuses a secret the file gives, for a file committed beside the
|
|
222
|
+
* code: its secrets come from the environment, and the error names the
|
|
223
|
+
* variable and its _FILE that give one, never the file
|
|
224
|
+
*/
|
|
225
|
+
secrets?: boolean | undefined
|
|
190
226
|
}
|
|
191
227
|
|
|
192
228
|
/** Settings' own variables by their paths: `{ port: 'PORT', db: { url: 'DATABASE_URL' } }`. */
|
|
@@ -222,7 +258,7 @@ export interface UpdateOptions {
|
|
|
222
258
|
/** A setting's line of explain. */
|
|
223
259
|
export interface ExplainLine {
|
|
224
260
|
readonly path: string
|
|
225
|
-
/** as JSON, a duration spelled, a secret as
|
|
261
|
+
/** as JSON, a duration spelled, a secret as ***, unset for none */
|
|
226
262
|
readonly value: string
|
|
227
263
|
/** the layer that gave it: APP_PORT, app.yaml, layer 2, the declaration, a change */
|
|
228
264
|
readonly from: string
|
|
@@ -239,7 +275,7 @@ export interface ExplainLine {
|
|
|
239
275
|
|
|
240
276
|
/** A value a setting was given, and where it came from. */
|
|
241
277
|
export interface ExplainGiven {
|
|
242
|
-
/** as JSON, a duration spelled, a secret as
|
|
278
|
+
/** as JSON, a duration spelled, a secret as ***, unset for none */
|
|
243
279
|
readonly value: string
|
|
244
280
|
readonly from: ExplainOrigin
|
|
245
281
|
}
|
|
@@ -299,6 +335,11 @@ export interface DurationBounds {
|
|
|
299
335
|
max?: DurationText | undefined
|
|
300
336
|
}
|
|
301
337
|
|
|
338
|
+
/** The entries a map of entries' declaration gives, each as an object in code gives one. */
|
|
339
|
+
export interface EntriesOptions<G> {
|
|
340
|
+
default?: Readonly<Record<string, Overlay<G>>> | undefined
|
|
341
|
+
}
|
|
342
|
+
|
|
302
343
|
/** A list's default and how many items it holds at least and at most. */
|
|
303
344
|
export interface ListOptions<V> extends Bounds {
|
|
304
345
|
default?: readonly V[] | undefined
|
|
@@ -326,6 +367,11 @@ function kindOf(token: Token): Kind {
|
|
|
326
367
|
return token === String ? 'text' : token === Number ? 'number' : 'boolean'
|
|
327
368
|
}
|
|
328
369
|
|
|
370
|
+
/** What `required` and `optional` mark: a type, `String`, or a rule. */
|
|
371
|
+
function ruleOf(given: Token | Setting<unknown, boolean, boolean, boolean>): Described | undefined {
|
|
372
|
+
return typeof given === 'function' ? { kind: kindOf(given) } : describedOf(given)
|
|
373
|
+
}
|
|
374
|
+
|
|
329
375
|
function bounds(rules: Bounds | undefined): Rules {
|
|
330
376
|
const out: Rules = {}
|
|
331
377
|
if (rules?.min !== undefined) {
|
|
@@ -357,10 +403,11 @@ export function required<V>(rule: Setting<V, false, false>): Setting<V>
|
|
|
357
403
|
export function required(
|
|
358
404
|
given: Token | Setting<unknown, boolean, boolean, boolean>,
|
|
359
405
|
): Setting<unknown> {
|
|
360
|
-
const inner =
|
|
406
|
+
const inner = ruleOf(given)
|
|
361
407
|
if (inner === undefined) {
|
|
362
408
|
throw new InvalidError('required takes String, Number, Boolean, or a rule without its default')
|
|
363
409
|
}
|
|
410
|
+
refuseEntries(inner)
|
|
364
411
|
if (inner.secret === true) {
|
|
365
412
|
throw new InvalidError('a secret is required already: secret(String)')
|
|
366
413
|
}
|
|
@@ -371,6 +418,32 @@ export function required(
|
|
|
371
418
|
return setting({ ...rest, required: true })
|
|
372
419
|
}
|
|
373
420
|
|
|
421
|
+
/**
|
|
422
|
+
* A setting with no value until a layer gives one: `optional(String)` reads as
|
|
423
|
+
* `string | null`, `optional(secret(String))` as `Secret | null`, and
|
|
424
|
+
* `optional(int({ min: 1 }))` as `number | null`. explain says it is unset.
|
|
425
|
+
*/
|
|
426
|
+
export function optional<T extends Token>(type: T): Setting<Of<T> | null>
|
|
427
|
+
export function optional<V, S extends boolean>(
|
|
428
|
+
rule: Setting<V, false, boolean, S>,
|
|
429
|
+
): Setting<V | null, false, true, S>
|
|
430
|
+
export function optional(
|
|
431
|
+
given: Token | Setting<unknown, boolean, boolean, boolean>,
|
|
432
|
+
): Setting<unknown, false, true, boolean> {
|
|
433
|
+
const inner = ruleOf(given)
|
|
434
|
+
if (inner?.live === true) {
|
|
435
|
+
throw new InvalidError('live is the outermost mark: live(optional(…))')
|
|
436
|
+
}
|
|
437
|
+
if (inner === undefined || inner.default !== undefined) {
|
|
438
|
+
throw new InvalidError(
|
|
439
|
+
'optional takes String, Number, Boolean, secret(String) or a rule without its default',
|
|
440
|
+
)
|
|
441
|
+
}
|
|
442
|
+
refuseEntries(inner)
|
|
443
|
+
const { required: _required, ...rest } = inner
|
|
444
|
+
return setting({ ...rest, default: null })
|
|
445
|
+
}
|
|
446
|
+
|
|
374
447
|
/**
|
|
375
448
|
* A setting that may change while the program runs: its change is kept in
|
|
376
449
|
* the store, goes over every layer until it is reset, and reaches every
|
|
@@ -385,6 +458,7 @@ export function live(given: unknown): Setting<unknown, true> {
|
|
|
385
458
|
throw new InvalidError('a mark goes on a setting, not on a group: mark each of its settings')
|
|
386
459
|
}
|
|
387
460
|
const inner = describedOf(given) ?? declaredValue(given, 'live')
|
|
461
|
+
refuseEntries(inner)
|
|
388
462
|
if (inner.secret === true) {
|
|
389
463
|
throw new InvalidError('a secret is never live')
|
|
390
464
|
}
|
|
@@ -474,6 +548,7 @@ function itemOf(item: unknown): Item {
|
|
|
474
548
|
}
|
|
475
549
|
const inner = describedOf(item)
|
|
476
550
|
if (inner !== undefined) {
|
|
551
|
+
refuseEntries(inner)
|
|
477
552
|
if (inner.live === true || inner.secret === true) {
|
|
478
553
|
throw new InvalidError("a list's items are neither live nor secret: the list is one setting")
|
|
479
554
|
}
|
|
@@ -486,9 +561,79 @@ function itemOf(item: unknown): Item {
|
|
|
486
561
|
throw new InvalidError(`a list of ${String(item)}: of String, Number, Boolean, a rule or a group`)
|
|
487
562
|
}
|
|
488
563
|
|
|
489
|
-
/**
|
|
564
|
+
/**
|
|
565
|
+
* A map of named entries, each the group `item` declares, whose fields are
|
|
566
|
+
* settings of their own, with their own marks: a file gives an entry field
|
|
567
|
+
* by field, and `APP_INSTANCE_AIMEE_PASSWORD`, or its `_FILE`, one field of
|
|
568
|
+
* the entry `aimee` that a layer before it made. A map written as a value,
|
|
569
|
+
* `{}`, is one setting, given whole.
|
|
570
|
+
*/
|
|
571
|
+
export function entries<G extends object>(item: G, options: EntriesOptions<G> = {}): Entries<G> {
|
|
572
|
+
if (!isPlainObject(item) || describedOf(item) !== undefined || Object.keys(item).length === 0) {
|
|
573
|
+
throw new InvalidError(
|
|
574
|
+
'an entry is a group of settings: entries({ host: String, password: secret(String) })',
|
|
575
|
+
)
|
|
576
|
+
}
|
|
577
|
+
const described = {
|
|
578
|
+
kind: 'object' as const,
|
|
579
|
+
default: options.default ?? {},
|
|
580
|
+
entries: declaredGroup(item, '*'),
|
|
581
|
+
}
|
|
582
|
+
return setting(described) as unknown as Entries<G>
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* A group whose settings are those of one of `groups`, the one the text
|
|
587
|
+
* setting beside it, `by`, names: `settings: chosenBy('plugin', { postgres,
|
|
588
|
+
* stripe })` reads an instance's settings as postgres's when its `plugin` is
|
|
589
|
+
* 'postgres'. Its fields are settings of their own, with their own marks; a
|
|
590
|
+
* field of a group not chosen is refused, and so is a `plugin` that names no
|
|
591
|
+
* group. A layer after the one that chose a group may choose another: then
|
|
592
|
+
* what the layers gave the first is dropped, and explain says so.
|
|
593
|
+
*/
|
|
594
|
+
export function chosenBy<const G extends Readonly<Record<string, object>>>(
|
|
595
|
+
by: string,
|
|
596
|
+
groups: G,
|
|
597
|
+
): Chosen<G> {
|
|
598
|
+
const named = Object.entries(groups)
|
|
599
|
+
if (typeof by !== 'string' || by === '' || !isPlainObject(groups) || named.length === 0) {
|
|
600
|
+
throw new InvalidError(
|
|
601
|
+
"a chosen group: chosenBy('plugin', { postgres: { host: String }, stripe: { key: secret(String) } })",
|
|
602
|
+
)
|
|
603
|
+
}
|
|
604
|
+
const declared: Record<string, Group> = {}
|
|
605
|
+
for (const [name, group] of named) {
|
|
606
|
+
if (!isPlainObject(group) || describedOf(group) !== undefined) {
|
|
607
|
+
throw new InvalidError(`chosenBy: ${name} is a group of settings, not a setting`)
|
|
608
|
+
}
|
|
609
|
+
declared[name] = declaredGroup(group, name)
|
|
610
|
+
}
|
|
611
|
+
const described = { kind: 'object' as const, default: {}, chosenBy: by, groups: declared }
|
|
612
|
+
return setting(described) as unknown as Chosen<G>
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** A mark around a map of entries or a chosen group, which marks none of their settings. */
|
|
616
|
+
function refuseEntries(inner: Described): void {
|
|
617
|
+
if (inner.entries !== undefined) {
|
|
618
|
+
throw new InvalidError('entries take no mark: each of their fields carries its own')
|
|
619
|
+
}
|
|
620
|
+
if (inner.groups !== undefined) {
|
|
621
|
+
throw new InvalidError('a chosen group takes no mark: each of its settings carries its own')
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* A file a config reads: TOML, YAML or JSON by its extension, a relative path
|
|
627
|
+
* the program's. `file('dashbin.toml', { secrets: false })` refuses a secret it
|
|
628
|
+
* gives.
|
|
629
|
+
*/
|
|
490
630
|
export function file(path: string, options: FileOptions = {}): FileLayer {
|
|
491
|
-
return Object.freeze({
|
|
631
|
+
return Object.freeze({
|
|
632
|
+
[layerMark]: 'file' as const,
|
|
633
|
+
path,
|
|
634
|
+
optional: options.optional === true,
|
|
635
|
+
secrets: options.secrets !== false,
|
|
636
|
+
})
|
|
492
637
|
}
|
|
493
638
|
|
|
494
639
|
/**
|
|
@@ -586,11 +731,23 @@ function plainItem(first: unknown): Item {
|
|
|
586
731
|
}
|
|
587
732
|
}
|
|
588
733
|
|
|
589
|
-
/** Every setting's path, as the core names its variable. */
|
|
734
|
+
/** Every setting's path, as the core names its variable, every map of entries' and every chosen group's. */
|
|
590
735
|
function paths(group: Group, path: string): string[] {
|
|
591
736
|
return Object.entries(group).flatMap(([key, node]) => {
|
|
592
737
|
const at = path === '' ? key : `${path}.${key}`
|
|
593
|
-
|
|
738
|
+
if ('group' in node) {
|
|
739
|
+
return paths(node.group, at)
|
|
740
|
+
}
|
|
741
|
+
const chosen = Object.values(node.groups ?? {}).flatMap(inner => paths(inner, at))
|
|
742
|
+
return node.groups === undefined ? [at] : [...new Set(chosen)]
|
|
743
|
+
})
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/** Every map of entries' path, whose entries' variables begin with its own. */
|
|
747
|
+
function entryMaps(group: Group, path: string): string[] {
|
|
748
|
+
return Object.entries(group).flatMap(([key, node]) => {
|
|
749
|
+
const at = path === '' ? key : `${path}.${key}`
|
|
750
|
+
return 'group' in node ? entryMaps(node.group, at) : node.entries === undefined ? [] : [at]
|
|
594
751
|
})
|
|
595
752
|
}
|
|
596
753
|
|
|
@@ -648,9 +805,10 @@ async function layerData(
|
|
|
648
805
|
}
|
|
649
806
|
const kind = layerOf(layer)
|
|
650
807
|
if (kind === 'file') {
|
|
651
|
-
const { path, optional } = layer as FileLayer
|
|
808
|
+
const { path, optional, secrets } = layer as FileLayer
|
|
652
809
|
const text = await readText(path)
|
|
653
|
-
|
|
810
|
+
const refused = secrets ? {} : { secrets: false }
|
|
811
|
+
return { file: { name: path, optional, ...refused, ...(text === undefined ? {} : { text }) } }
|
|
654
812
|
}
|
|
655
813
|
if (kind === 'env') {
|
|
656
814
|
return { env: await envData(layer as EnvLayer, declared) }
|
|
@@ -671,11 +829,15 @@ async function envData(layer: EnvLayer, declared: Group): Promise<object> {
|
|
|
671
829
|
const key = (name: string) => (windows ? name.toUpperCase() : name)
|
|
672
830
|
const named = paths(declared, '').map(path => layer.vars[path] ?? envName(layer.prefix, path))
|
|
673
831
|
const wanted = new Set(named.flatMap(name => [key(name), key(`${name}_FILE`)]))
|
|
674
|
-
|
|
832
|
+
// an entry's field is named by its key, so its map's variable is what it begins with
|
|
833
|
+
const starts = entryMaps(declared, '').map(path => key(`${envName(layer.prefix, path)}_`))
|
|
834
|
+
if (layer.prefix !== '') {
|
|
835
|
+
starts.push(key(`${layer.prefix}_`))
|
|
836
|
+
}
|
|
675
837
|
const variables: Record<string, string> = {}
|
|
676
838
|
for (const [name, value] of Object.entries(layer.from ?? process.env)) {
|
|
677
839
|
const k = key(name)
|
|
678
|
-
if (value !== undefined && (wanted.has(k) || (
|
|
840
|
+
if (value !== undefined && (wanted.has(k) || starts.some(start => k.startsWith(start)))) {
|
|
679
841
|
variables[k] = value
|
|
680
842
|
}
|
|
681
843
|
}
|
|
@@ -711,19 +873,36 @@ function fileVariables(text: string): [string, string][] {
|
|
|
711
873
|
return found
|
|
712
874
|
}
|
|
713
875
|
|
|
714
|
-
/**
|
|
876
|
+
/**
|
|
877
|
+
* A value as the program reads it: a duration spelled, a secret wrapped, a
|
|
878
|
+
* chosen group as the group its chooser names, all of it frozen.
|
|
879
|
+
*/
|
|
715
880
|
function viewed(group: Group, value: unknown): unknown {
|
|
716
881
|
const source = (isPlainObject(value) ? value : {}) as Record<string, unknown>
|
|
717
882
|
const out: Record<string, unknown> = {}
|
|
718
883
|
for (const [key, node] of Object.entries(group)) {
|
|
719
|
-
|
|
884
|
+
if ('group' in node) {
|
|
885
|
+
out[key] = viewed(node.group, source[key])
|
|
886
|
+
} else if (node.groups !== undefined) {
|
|
887
|
+
const name = source[node.chosenBy ?? '']
|
|
888
|
+
const chosen = typeof name === 'string' ? node.groups[name] : undefined
|
|
889
|
+
out[key] = viewed(chosen ?? {}, source[key])
|
|
890
|
+
} else {
|
|
891
|
+
out[key] = viewedSetting(node, source[key])
|
|
892
|
+
}
|
|
720
893
|
}
|
|
721
894
|
return Object.freeze(out)
|
|
722
895
|
}
|
|
723
896
|
|
|
724
897
|
function viewedSetting(described: Described, value: unknown): unknown {
|
|
898
|
+
const item = described.entries
|
|
899
|
+
if (item !== undefined) {
|
|
900
|
+
const each = Object.entries(isPlainObject(value) ? value : {})
|
|
901
|
+
return Object.freeze(Object.fromEntries(each.map(([key, entry]) => [key, viewed(item, entry)])))
|
|
902
|
+
}
|
|
725
903
|
if (described.secret === true) {
|
|
726
|
-
|
|
904
|
+
// one declared optional is null until a layer gives it
|
|
905
|
+
return value === null ? null : new Secret(typeof value === 'string' ? value : '')
|
|
727
906
|
}
|
|
728
907
|
return viewedKind(described.kind, value)
|
|
729
908
|
}
|
|
@@ -831,7 +1010,7 @@ function originOf(sent: SentOrigin): ExplainOrigin {
|
|
|
831
1010
|
}
|
|
832
1011
|
}
|
|
833
1012
|
|
|
834
|
-
/** The text the core's `from` says: crates/tinystore/src/
|
|
1013
|
+
/** The text the core's `from` says: crates/tinystore/src/config/explain.rs, Origin's Display. */
|
|
835
1014
|
function originText(origin: ExplainOrigin): string {
|
|
836
1015
|
switch (origin.kind) {
|
|
837
1016
|
case 'declaration':
|
package/src/index.ts
CHANGED
|
@@ -26,16 +26,21 @@ export type { Clock } from './clock.ts'
|
|
|
26
26
|
export {
|
|
27
27
|
type Bounds,
|
|
28
28
|
type Changes,
|
|
29
|
+
type Chosen,
|
|
29
30
|
type Config,
|
|
30
31
|
type ConfigOptions,
|
|
32
|
+
chosenBy,
|
|
31
33
|
type DurationBounds,
|
|
32
34
|
duration,
|
|
35
|
+
type Entries,
|
|
36
|
+
type EntriesOptions,
|
|
33
37
|
type EnvLayer,
|
|
34
38
|
type EnvOptions,
|
|
35
39
|
type Explained,
|
|
36
40
|
type ExplainGiven,
|
|
37
41
|
type ExplainLine,
|
|
38
42
|
type ExplainOrigin,
|
|
43
|
+
entries,
|
|
39
44
|
env,
|
|
40
45
|
type FileLayer,
|
|
41
46
|
file,
|
|
@@ -45,6 +50,7 @@ export {
|
|
|
45
50
|
live,
|
|
46
51
|
type Overlay,
|
|
47
52
|
oneOf,
|
|
53
|
+
optional,
|
|
48
54
|
required,
|
|
49
55
|
Secret,
|
|
50
56
|
type Setting,
|
|
@@ -101,7 +107,7 @@ export type {
|
|
|
101
107
|
SemaphoreOptions,
|
|
102
108
|
Tried,
|
|
103
109
|
} from './locks.ts'
|
|
104
|
-
export { type ConsoleOut, type Logger, type LoggerOptions, logger } from './logger.ts'
|
|
110
|
+
export { type ConsoleOut, colors, type Logger, type LoggerOptions, logger } from './logger.ts'
|
|
105
111
|
export {
|
|
106
112
|
type Aggregate,
|
|
107
113
|
type AggregatePage,
|
|
@@ -148,6 +154,7 @@ export type { ColumnType, Query, RowPage, Table, TableOptions, UpsertOptions } f
|
|
|
148
154
|
export type {
|
|
149
155
|
Aggregate as EventsAggregate,
|
|
150
156
|
AllEvents,
|
|
157
|
+
Appending,
|
|
151
158
|
AppendOptions,
|
|
152
159
|
Counted,
|
|
153
160
|
Damage,
|
|
@@ -155,10 +162,12 @@ export type {
|
|
|
155
162
|
EventLevel,
|
|
156
163
|
Events,
|
|
157
164
|
EventsOptions,
|
|
165
|
+
FieldInfo,
|
|
158
166
|
Fields,
|
|
159
167
|
Level,
|
|
160
168
|
Levels,
|
|
161
169
|
Lines,
|
|
170
|
+
NameCount,
|
|
162
171
|
NewEvent,
|
|
163
172
|
Page as EventsPage,
|
|
164
173
|
PollBatch,
|
|
@@ -166,10 +175,13 @@ export type {
|
|
|
166
175
|
Row as EventsRow,
|
|
167
176
|
Stats,
|
|
168
177
|
StoreLoggerOptions,
|
|
178
|
+
StreamFields,
|
|
169
179
|
StreamInfo,
|
|
170
180
|
TailBatch,
|
|
171
181
|
TailQuery,
|
|
172
182
|
Top,
|
|
183
|
+
TxEvents,
|
|
184
|
+
ValueKind,
|
|
173
185
|
} from './records.ts'
|
|
174
186
|
export type { StandardSchemaV1 } from './schema.ts'
|
|
175
187
|
export {
|
package/src/logger.ts
CHANGED
|
@@ -368,6 +368,22 @@ function forceColor(): boolean {
|
|
|
368
368
|
return force !== undefined && force !== '' && force !== '0'
|
|
369
369
|
}
|
|
370
370
|
|
|
371
|
+
/**
|
|
372
|
+
* Whether a stream shows colours, by the rule every logger keeps, in Bun,
|
|
373
|
+
* Python, Go and Rust, and the command line too, so that a program's own
|
|
374
|
+
* words and the log lines printed beside them are coloured alike: off under
|
|
375
|
+
* NO_COLOR or a TERM of dumb, on where FORCE_COLOR is other than 0, a pipe or
|
|
376
|
+
* a file too, and otherwise on a terminal alone.
|
|
377
|
+
*
|
|
378
|
+
* `const painted = colors(process.stdout)`
|
|
379
|
+
*/
|
|
380
|
+
export function colors(stream: Pick<ConsoleOut, 'isTTY'>): boolean {
|
|
381
|
+
if (process.env.NO_COLOR || process.env.TERM === 'dumb') {
|
|
382
|
+
return false
|
|
383
|
+
}
|
|
384
|
+
return forceColor() || stream.isTTY === true
|
|
385
|
+
}
|
|
386
|
+
|
|
371
387
|
/** Whether a console told no format prints pretty lines: on a terminal, or where FORCE_COLOR asks. */
|
|
372
388
|
function prettyUntold(options: Pick<LoggerOptions, 'to'>, out: ConsoleOut | undefined): boolean {
|
|
373
389
|
const stream: ConsoleOut = out ?? options.to ?? process.stderr
|
|
@@ -384,19 +400,13 @@ function printer(
|
|
|
384
400
|
return undefined
|
|
385
401
|
}
|
|
386
402
|
const stream: ConsoleOut = out ?? options.to ?? process.stderr
|
|
387
|
-
const forced = out === undefined && forceColor()
|
|
388
|
-
const terminal = stream.isTTY === true
|
|
389
403
|
const pretty =
|
|
390
404
|
options.console === 'pretty' || (options.console === undefined && prettyUntold(options, out))
|
|
391
405
|
if (!pretty) {
|
|
392
406
|
return line => void stream.write(jsonLine(line))
|
|
393
407
|
}
|
|
394
|
-
const color =
|
|
395
|
-
(forced || (terminal && out === undefined)) &&
|
|
396
|
-
!process.env.NO_COLOR &&
|
|
397
|
-
process.env.TERM !== 'dumb'
|
|
398
408
|
const look: Look = {
|
|
399
|
-
color,
|
|
409
|
+
color: out === undefined && colors(stream),
|
|
400
410
|
utc: out?.utc === true,
|
|
401
411
|
time: options.time,
|
|
402
412
|
hideStream: options.hideStream,
|
package/src/metrics.ts
CHANGED
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
MetricsPlan,
|
|
40
40
|
MetricsQuery,
|
|
41
41
|
MetricsRead,
|
|
42
|
+
MetricsReplace,
|
|
42
43
|
MetricsSeriesNamed,
|
|
43
44
|
MetricsTextPage,
|
|
44
45
|
MetricsUpload,
|
|
@@ -339,6 +340,30 @@ export class Metrics {
|
|
|
339
340
|
)
|
|
340
341
|
}
|
|
341
342
|
|
|
343
|
+
/**
|
|
344
|
+
* Replaces a gauge's samples from `from` to `to`, `to` excluded, with the series' own,
|
|
345
|
+
* whether the store had packed them or not: a time given takes its value, a time not there
|
|
346
|
+
* before is added, and a time the series does not give goes. One write: a read sees the
|
|
347
|
+
* range as it was or as it is now, never half, and the same samples sent again write
|
|
348
|
+
* nothing. A counter's or a histogram's is an InvalidError.
|
|
349
|
+
*
|
|
350
|
+
* // the last three days read again from the shop's database, a refund taken off yesterday
|
|
351
|
+
* await store.metrics.replace(
|
|
352
|
+
* { name: 'revenue', kind: 'gauge', labels: { shop: 'msk' }, samples: [[day(3), 1000], [day(2), 1000], [day(1), 900]] },
|
|
353
|
+
* { from: day(3), to: day(0) },
|
|
354
|
+
* )
|
|
355
|
+
*/
|
|
356
|
+
async replace(series: SeriesInput, range: { from: Time; to: Time }): Promise<void> {
|
|
357
|
+
const body = MetricsReplace.encode({
|
|
358
|
+
series: newSeries(series),
|
|
359
|
+
from: unixMs(range.from),
|
|
360
|
+
to: unixMs(range.to),
|
|
361
|
+
})
|
|
362
|
+
await this.#link.run('write', connection =>
|
|
363
|
+
connection.session.call(methods['metrics.replace'], body),
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
|
|
342
367
|
/**
|
|
343
368
|
* Reads the text an exporter serves, in Prometheus' format or OpenMetrics', and writes its
|
|
344
369
|
* series, every one or none: a counter as a counter, a histogram's _bucket series as
|