@tinyshed/tinystore 0.1.0-rc.3 → 0.1.0-rc.5
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 +8 -8
- package/dist/config.d.ts +65 -15
- package/dist/config.js +291 -70
- package/dist/console.d.ts +10 -1
- package/dist/console.js +42 -12
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -0
- package/dist/kv.d.ts +11 -8
- package/dist/kv.js +12 -8
- package/dist/logger.d.ts +8 -4
- package/dist/logger.js +71 -5
- package/package.json +7 -7
- package/src/config.ts +346 -88
- package/src/console.ts +53 -12
- package/src/index.ts +16 -3
- package/src/kv.ts +16 -12
- package/src/logger.ts +83 -8
package/src/index.ts
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
export type { BlobBucket, BlobObject, Blobs, Body, Download, PutOptions } from './blobs.ts'
|
|
2
2
|
export { withSignal } from './cancel.ts'
|
|
3
3
|
export type { Clock } from './clock.ts'
|
|
4
|
-
export
|
|
5
|
-
|
|
4
|
+
export {
|
|
5
|
+
type Config,
|
|
6
|
+
type ConfigLayer,
|
|
7
|
+
type ConfigValue,
|
|
8
|
+
type DeepPartial,
|
|
9
|
+
type FromEnv,
|
|
10
|
+
fromEnv,
|
|
11
|
+
required,
|
|
12
|
+
type Setting,
|
|
13
|
+
type Source,
|
|
14
|
+
secret,
|
|
15
|
+
type Validate,
|
|
16
|
+
validate,
|
|
17
|
+
} from './config.ts'
|
|
18
|
+
export type { ConsoleFormat, ConsoleTime } from './console.ts'
|
|
6
19
|
export * from './errors.ts'
|
|
7
20
|
export type { Page } from './handles.ts'
|
|
8
21
|
export type {
|
|
@@ -32,7 +45,7 @@ export type {
|
|
|
32
45
|
WriteOptions,
|
|
33
46
|
} from './kv.ts'
|
|
34
47
|
export type { Allowance, Limiter, LimiterOptions, Rate } from './limiter.ts'
|
|
35
|
-
export { type Logger, type LoggerOptions, logger } from './logger.ts'
|
|
48
|
+
export { type ConsoleOut, type Logger, type LoggerOptions, logger } from './logger.ts'
|
|
36
49
|
export {
|
|
37
50
|
type Aggregate,
|
|
38
51
|
type AggregateOp,
|
package/src/kv.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// buckets of one value type by key, keys in branches, expiry by the store's
|
|
3
3
|
// clock, versions that never repeat. A handle opens with its first call.
|
|
4
4
|
|
|
5
|
-
import { Config, type
|
|
5
|
+
import { Config, type ConfigLayer, type ConfigValue } from './config.ts'
|
|
6
6
|
import type { Connection, Link } from './connection.ts'
|
|
7
7
|
import { ConflictError, CorruptError, InvalidError } from './errors.ts'
|
|
8
8
|
import { checkName, handleOn, ownerText, type Page, type Settled, settle } from './handles.ts'
|
|
@@ -91,23 +91,27 @@ export class Kv {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
|
-
* The config name, shaped and typed as its defaults
|
|
95
|
-
*
|
|
96
|
-
* It resolves once the server's state is read, and follows every
|
|
97
|
-
* from then on, whoever makes it, until the store closes.
|
|
94
|
+
* The config name, shaped and typed as its defaults, then each layer over
|
|
95
|
+
* the one before: a file's values, `fromEnv`, and what update kept over
|
|
96
|
+
* them all. It resolves once the server's state is read, and follows every
|
|
97
|
+
* change from then on, whoever makes it, until the store closes.
|
|
98
98
|
*
|
|
99
99
|
* ```ts
|
|
100
|
-
* const cfg = await store.kv.config('app', {
|
|
101
|
-
*
|
|
100
|
+
* const cfg = await store.kv.config('app', {
|
|
101
|
+
* port: 8080,
|
|
102
|
+
* db: { url: secret('DATABASE_URL'), pool: 10 },
|
|
103
|
+
* }, file, fromEnv('APP'))
|
|
104
|
+
* cfg.value.port // APP_PORT=3000 makes it 3000
|
|
102
105
|
* await cfg.update({ port: 4000 }) // kept: 4000 after a restart too
|
|
103
106
|
* ```
|
|
104
107
|
*/
|
|
105
|
-
async config<
|
|
108
|
+
async config<D extends object>(
|
|
106
109
|
name: string,
|
|
107
|
-
defaults:
|
|
108
|
-
|
|
109
|
-
): Promise<Config<
|
|
110
|
-
const config = new Config(this.#link, name, defaults
|
|
110
|
+
defaults: D,
|
|
111
|
+
...layers: ConfigLayer<ConfigValue<D>>[]
|
|
112
|
+
): Promise<Config<ConfigValue<D>>> {
|
|
113
|
+
const config = new Config<ConfigValue<D>>(this.#link, name, defaults)
|
|
114
|
+
await config.lay(layers)
|
|
111
115
|
this.#configs.add(config as unknown as Config<object>)
|
|
112
116
|
try {
|
|
113
117
|
await config.start()
|
package/src/logger.ts
CHANGED
|
@@ -9,8 +9,10 @@ import { DropNotice, FailureLog, type Say } from './background.ts'
|
|
|
9
9
|
import {
|
|
10
10
|
type ConsoleFormat,
|
|
11
11
|
type ConsoleLine,
|
|
12
|
+
type ConsoleTime,
|
|
12
13
|
jsonLine,
|
|
13
14
|
jsonOf,
|
|
15
|
+
type Look,
|
|
14
16
|
prettyLine,
|
|
15
17
|
redacted,
|
|
16
18
|
redactor,
|
|
@@ -26,13 +28,17 @@ export interface LoggerOptions {
|
|
|
26
28
|
level?: Level
|
|
27
29
|
/** how a line is written as it is logged: pretty on a terminal and JSON otherwise when absent */
|
|
28
30
|
console?: ConsoleFormat
|
|
31
|
+
/** how a pretty line shows its time: 'clock' when absent */
|
|
32
|
+
time?: ConsoleTime
|
|
33
|
+
/** leaves the stream out of a pretty line; the record keeps it */
|
|
34
|
+
hideStream?: boolean
|
|
35
|
+
/** where the console lines go: process.stderr when absent */
|
|
36
|
+
to?: ConsoleOut
|
|
29
37
|
/** fields whose values are hidden, in the store and on the console, at any depth, the case ignored */
|
|
30
38
|
redact?: readonly string[]
|
|
31
|
-
/** the console lines go to stdout rather than stderr */
|
|
32
|
-
stdout?: boolean
|
|
33
39
|
}
|
|
34
40
|
|
|
35
|
-
/** Where
|
|
41
|
+
/** Where console lines go: process.stdout, a file's stream, or any other writer. */
|
|
36
42
|
export interface ConsoleOut {
|
|
37
43
|
write(text: string): unknown
|
|
38
44
|
readonly isTTY?: boolean
|
|
@@ -87,13 +93,14 @@ export function newLogger(
|
|
|
87
93
|
if (!Number.isInteger(most) || most < 1) {
|
|
88
94
|
throw new InvalidError(`a logger's buffer of ${most} lines`)
|
|
89
95
|
}
|
|
96
|
+
const [read, ignored] = fromEnvironment(options)
|
|
90
97
|
const shared: Shared = {
|
|
91
98
|
append,
|
|
92
99
|
stream,
|
|
93
100
|
most,
|
|
94
|
-
least:
|
|
95
|
-
hides: redactor(
|
|
96
|
-
print: printer(
|
|
101
|
+
least: read.level === undefined ? Number.NEGATIVE_INFINITY : levelOf(read.level),
|
|
102
|
+
hides: redactor(read.redact),
|
|
103
|
+
print: printer(read, out),
|
|
97
104
|
waiting: [],
|
|
98
105
|
timer: undefined,
|
|
99
106
|
writing: undefined,
|
|
@@ -101,9 +108,71 @@ export function newLogger(
|
|
|
101
108
|
failures: append === undefined ? undefined : new FailureLog(`records flush ${stream}`, sayOwn),
|
|
102
109
|
drops: append === undefined ? undefined : new DropNotice(stream, most, sayOwn),
|
|
103
110
|
}
|
|
111
|
+
sayIgnored(shared.print, ignored)
|
|
104
112
|
return new Logger(shared, [])
|
|
105
113
|
}
|
|
106
114
|
|
|
115
|
+
type Unread = [name: string, value: string, expected: string]
|
|
116
|
+
|
|
117
|
+
/** LOG_LEVEL, LOG_FORMAT and LOG_TIME over the options, as Go and Python read them; a console turned off stays off. */
|
|
118
|
+
function fromEnvironment(options: LoggerOptions): [LoggerOptions, Unread[]] {
|
|
119
|
+
const read = { ...options }
|
|
120
|
+
const ignored: Unread[] = []
|
|
121
|
+
const level = variable('LOG_LEVEL')
|
|
122
|
+
if (level !== undefined) {
|
|
123
|
+
const name = level.toLowerCase() === 'warning' ? 'warn' : level.toLowerCase()
|
|
124
|
+
if (Object.hasOwn(levels, name)) {
|
|
125
|
+
read.level = name as keyof typeof levels
|
|
126
|
+
} else {
|
|
127
|
+
ignored.push(['LOG_LEVEL', level, 'debug, info, warn or error'])
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const format = variable('LOG_FORMAT')
|
|
131
|
+
if (format !== undefined) {
|
|
132
|
+
const name = format.toLowerCase()
|
|
133
|
+
if (name !== 'pretty' && name !== 'json' && name !== 'off') {
|
|
134
|
+
ignored.push(['LOG_FORMAT', format, 'pretty, json or off'])
|
|
135
|
+
} else if (read.console !== 'off') {
|
|
136
|
+
read.console = name
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const time = variable('LOG_TIME')
|
|
140
|
+
if (time !== undefined) {
|
|
141
|
+
const name = time.toLowerCase()
|
|
142
|
+
if (name === 'clock' || name === 'full' || name === 'off') {
|
|
143
|
+
read.time = name
|
|
144
|
+
} else {
|
|
145
|
+
ignored.push(['LOG_TIME', time, 'clock, full or off'])
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return [read, ignored]
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function variable(name: string): string | undefined {
|
|
152
|
+
const text = process.env[name]?.trim()
|
|
153
|
+
return text === '' ? undefined : text
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The values said to be ignored, so that a program making many loggers says each once. */
|
|
157
|
+
const saidIgnored = new Set<string>()
|
|
158
|
+
|
|
159
|
+
function sayIgnored(print: Shared['print'], ignored: Unread[]): void {
|
|
160
|
+
for (const [name, value, expected] of ignored) {
|
|
161
|
+
if (print === undefined || saidIgnored.has(`${name}=${value}`)) {
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
saidIgnored.add(`${name}=${value}`)
|
|
165
|
+
print({
|
|
166
|
+
at: new Date(),
|
|
167
|
+
stream: 'tinystore',
|
|
168
|
+
level: levels.warn,
|
|
169
|
+
msg: `${name} is ignored`,
|
|
170
|
+
context: [],
|
|
171
|
+
attrs: ['value', JSON.stringify(value), 'expected', JSON.stringify(expected)],
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
107
176
|
let own: Logger | undefined
|
|
108
177
|
|
|
109
178
|
/** TinyStore's own lines, on the console of a logger of stream tinystore made when first needed. */
|
|
@@ -122,7 +191,7 @@ function printer(options: LoggerOptions, out?: ConsoleOut & { utc?: boolean }):
|
|
|
122
191
|
if (options.console === 'off') {
|
|
123
192
|
return undefined
|
|
124
193
|
}
|
|
125
|
-
const stream: ConsoleOut = out ??
|
|
194
|
+
const stream: ConsoleOut = out ?? options.to ?? process.stderr
|
|
126
195
|
// FORCE_COLOR other than 0 says a person reads a pipe, as an IDE's run console is one
|
|
127
196
|
const forced = out === undefined && forceColor()
|
|
128
197
|
const terminal = stream.isTTY === true
|
|
@@ -135,7 +204,13 @@ function printer(options: LoggerOptions, out?: ConsoleOut & { utc?: boolean }):
|
|
|
135
204
|
(forced || (terminal && out === undefined)) &&
|
|
136
205
|
!process.env.NO_COLOR &&
|
|
137
206
|
process.env.TERM !== 'dumb'
|
|
138
|
-
|
|
207
|
+
const look: Look = {
|
|
208
|
+
color,
|
|
209
|
+
utc: out?.utc === true,
|
|
210
|
+
time: options.time,
|
|
211
|
+
hideStream: options.hideStream,
|
|
212
|
+
}
|
|
213
|
+
return line => void stream.write(prettyLine(line, look))
|
|
139
214
|
}
|
|
140
215
|
|
|
141
216
|
/**
|