@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.
Files changed (122) hide show
  1. package/README.md +37 -37
  2. package/bin/tinystore.js +12 -1
  3. package/dist/backup.d.ts +33 -0
  4. package/dist/backup.js +49 -0
  5. package/dist/blobs.d.ts +160 -79
  6. package/dist/blobs.js +510 -217
  7. package/dist/channels.d.ts +90 -0
  8. package/dist/channels.js +291 -0
  9. package/dist/clock.js +3 -3
  10. package/dist/config.d.ts +209 -86
  11. package/dist/config.js +581 -538
  12. package/dist/connection.d.ts +64 -2
  13. package/dist/connection.js +201 -16
  14. package/dist/console.d.ts +27 -3
  15. package/dist/console.js +165 -12
  16. package/dist/dotenv.d.ts +7 -0
  17. package/dist/dotenv.js +79 -0
  18. package/dist/errors.d.ts +61 -1
  19. package/dist/errors.js +81 -1
  20. package/dist/handles.d.ts +44 -2
  21. package/dist/handles.js +81 -2
  22. package/dist/index.d.ts +17 -11
  23. package/dist/index.js +5 -3
  24. package/dist/instruments.d.ts +177 -0
  25. package/dist/instruments.js +819 -0
  26. package/dist/jobs.d.ts +335 -239
  27. package/dist/jobs.js +716 -536
  28. package/dist/json.d.ts +2 -0
  29. package/dist/json.js +122 -0
  30. package/dist/kv.d.ts +130 -243
  31. package/dist/kv.js +273 -640
  32. package/dist/limits.d.ts +80 -0
  33. package/dist/limits.js +139 -0
  34. package/dist/locks.d.ts +62 -0
  35. package/dist/locks.js +237 -0
  36. package/dist/logger.d.ts +45 -9
  37. package/dist/logger.js +150 -38
  38. package/dist/metrics.d.ts +210 -209
  39. package/dist/metrics.js +319 -556
  40. package/dist/once.d.ts +19 -18
  41. package/dist/once.js +74 -63
  42. package/dist/query.d.ts +181 -0
  43. package/dist/query.js +788 -0
  44. package/dist/records.d.ts +241 -150
  45. package/dist/records.js +507 -291
  46. package/dist/runtime/addon.d.ts +14 -0
  47. package/dist/runtime/addon.js +69 -0
  48. package/dist/runtime/bun.js +5 -7
  49. package/dist/runtime/node.js +5 -7
  50. package/dist/runtime/pipe.d.ts +17 -0
  51. package/dist/runtime/pipe.js +133 -0
  52. package/dist/runtime/shared.d.ts +6 -0
  53. package/dist/runtime/shared.js +19 -0
  54. package/dist/runtime.d.ts +5 -2
  55. package/dist/search.d.ts +189 -0
  56. package/dist/search.js +281 -0
  57. package/dist/session.d.ts +31 -3
  58. package/dist/session.js +153 -41
  59. package/dist/sql.d.ts +228 -78
  60. package/dist/sql.js +745 -240
  61. package/dist/store.d.ts +244 -24
  62. package/dist/store.js +270 -67
  63. package/dist/time.d.ts +5 -1
  64. package/dist/time.js +22 -2
  65. package/dist/tx.d.ts +61 -0
  66. package/dist/tx.js +314 -0
  67. package/dist/wire/codec.d.ts +23 -7
  68. package/dist/wire/codec.js +56 -35
  69. package/dist/wire/frame.d.ts +2 -0
  70. package/dist/wire/frame.js +31 -14
  71. package/dist/wire/messages.d.ts +259 -0
  72. package/dist/wire/messages.js +22 -0
  73. package/dist/wire/msgpack.d.ts +49 -1
  74. package/dist/wire/msgpack.js +293 -56
  75. package/dist/wire/protocol.d.ts +2683 -0
  76. package/dist/wire/protocol.js +10846 -0
  77. package/package.json +39 -38
  78. package/src/backup.ts +81 -0
  79. package/src/blobs.ts +678 -301
  80. package/src/channels.ts +377 -0
  81. package/src/clock.ts +4 -4
  82. package/src/config.ts +902 -612
  83. package/src/connection.ts +279 -21
  84. package/src/console.ts +180 -12
  85. package/src/dotenv.ts +82 -0
  86. package/src/errors.ts +105 -0
  87. package/src/handles.ts +157 -8
  88. package/src/index.ts +158 -52
  89. package/src/instruments.ts +1042 -0
  90. package/src/jobs.ts +1017 -733
  91. package/src/json.ts +133 -0
  92. package/src/kv.ts +424 -847
  93. package/src/limits.ts +202 -0
  94. package/src/locks.ts +320 -0
  95. package/src/logger.ts +209 -40
  96. package/src/metrics.ts +557 -752
  97. package/src/once.ts +86 -83
  98. package/src/query.ts +1005 -0
  99. package/src/records.ts +795 -418
  100. package/src/runtime/addon.ts +99 -0
  101. package/src/runtime/bun.ts +5 -7
  102. package/src/runtime/node.ts +5 -7
  103. package/src/runtime/pipe.ts +178 -0
  104. package/src/runtime/shared.ts +20 -0
  105. package/src/runtime.ts +5 -2
  106. package/src/search.ts +480 -0
  107. package/src/session.ts +187 -41
  108. package/src/sql.ts +993 -306
  109. package/src/store.ts +473 -78
  110. package/src/time.ts +48 -3
  111. package/src/tx.ts +426 -0
  112. package/src/wire/codec.ts +78 -37
  113. package/src/wire/frame.ts +44 -14
  114. package/src/wire/messages.ts +25 -0
  115. package/src/wire/msgpack.ts +327 -56
  116. package/src/wire/protocol.ts +11709 -0
  117. package/dist/limiter.d.ts +0 -45
  118. package/dist/limiter.js +0 -68
  119. package/dist/quota.d.ts +0 -44
  120. package/dist/quota.js +0 -98
  121. package/src/limiter.ts +0 -99
  122. package/src/quota.ts +0 -137
package/src/logger.ts CHANGED
@@ -5,18 +5,26 @@
5
5
  // waits; what does not fit, or a write that fails, is dropped and counted,
6
6
  // and said in TinyStore's own lines, since no call is waiting to be told.
7
7
 
8
+ import { readFileSync } from 'node:fs'
9
+ import { fileURLToPath } from 'node:url'
10
+
8
11
  import { DropNotice, FailureLog, type Say } from './background.ts'
12
+ import { type EnvLayer, env as environmentOf, live, oneOf, type Setting } from './config.ts'
9
13
  import {
10
14
  type ConsoleFormat,
11
15
  type ConsoleLine,
12
16
  type ConsoleTime,
17
+ hideUrlPasswords,
13
18
  jsonLine,
14
19
  jsonOf,
15
20
  type Look,
16
21
  prettyLine,
22
+ type Redactor,
17
23
  redacted,
18
24
  redactor,
25
+ sourceKey,
19
26
  } from './console.ts'
27
+ import { parseDotenv } from './dotenv.ts'
20
28
  import { InvalidError } from './errors.ts'
21
29
  import type { Fields, Level } from './records.ts'
22
30
  import { currentTrace, type Trace } from './trace.ts'
@@ -34,8 +42,25 @@ export interface LoggerOptions {
34
42
  hideStream?: boolean
35
43
  /** where the console lines go: process.stderr when absent */
36
44
  to?: ConsoleOut
37
- /** fields whose values are hidden, in the store and on the console, at any depth, the case ignored */
45
+ /**
46
+ * secrets whose fields are hidden, in the store and on the console, at any
47
+ * depth: a key hides its value when some of its words in a row, written
48
+ * together, are a name's, so 'api key' hides api_key and apiKey; `secrets`
49
+ * holds the usual ones
50
+ */
38
51
  redact?: readonly string[]
52
+ /** leaves a URL's password in a value as it is, which is otherwise hidden */
53
+ keepUrlPasswords?: boolean
54
+ /** changes each field's value before it is hidden, kept and shown */
55
+ replace?: (key: string, value: unknown) => unknown
56
+ /**
57
+ * where LOG_LEVEL, LOG_FORMAT and LOG_TIME are read, which win over the
58
+ * options: `env('APP')` reads APP_LOG_LEVEL and the rest, and false
59
+ * reads nothing; the bare names when absent
60
+ */
61
+ env?: EnvLayer | false
62
+ /** adds where each line was logged: source={"function":…,"file":…,"line":…} */
63
+ source?: boolean
39
64
  }
40
65
 
41
66
  /** Where console lines go: process.stdout, a file's stream, or any other writer. */
@@ -44,7 +69,11 @@ export interface ConsoleOut {
44
69
  readonly isTTY?: boolean
45
70
  }
46
71
 
47
- const levels = { debug: -4, info: 0, warn: 4, error: 8 } as const
72
+ // slog's numbers, trace below debug as Python's levels map, so that a logger
73
+ // at trace takes every line
74
+ const levels = { trace: -8, debug: -4, info: 0, warn: 4, error: 8 } as const
75
+ const formats = ['pretty', 'json', 'off'] as const
76
+ const times = ['clock', 'full', 'off'] as const
48
77
  const flushEvery = 1000
49
78
 
50
79
  /** What a logger and the loggers its `with` makes share: one buffer, one timer, one count, one console. */
@@ -54,7 +83,9 @@ interface Shared {
54
83
  stream: string
55
84
  most: number
56
85
  least: number
57
- hides: ((key: string) => boolean) | undefined
86
+ redact: Redactor | undefined
87
+ replace: ((key: string, value: unknown) => unknown) | undefined
88
+ source: boolean
58
89
  print: ((line: ConsoleLine) => void) | undefined
59
90
  waiting: ConsoleLine[]
60
91
  timer: ReturnType<typeof setInterval> | undefined
@@ -68,7 +99,7 @@ interface Shared {
68
99
  /**
69
100
  * A logger of the console alone, for a program that wants the logger and not
70
101
  * the records: its lines are written as a store's logger writes them, and
71
- * `store.records.logger(stream)` in its place keeps them too.
102
+ * `store.logger(stream)` in its place keeps them too.
72
103
  *
73
104
  * ```ts
74
105
  * const log = logger('app', { redact: ['password'] })
@@ -86,6 +117,71 @@ export function newLogger(
86
117
  options: LoggerOptions = {},
87
118
  out?: ConsoleOut & { utc?: boolean },
88
119
  ): Logger {
120
+ return new Logger(sharedOf(append, stream, options, out).shared, [])
121
+ }
122
+
123
+ /**
124
+ * What a store's logger declares its config `log` with: the code's options
125
+ * as the defaults, trace for a logger told no level, and the variables it
126
+ * read as the layer over them; and how it takes the level each value gives.
127
+ */
128
+ export interface LogConfig {
129
+ readonly layers: readonly [LogDeclaration, ...EnvLayer[]]
130
+ take(level: string): void
131
+ }
132
+
133
+ type LogDeclaration = {
134
+ level: Setting<string, true>
135
+ format: Setting<string>
136
+ time: Setting<string>
137
+ }
138
+
139
+ /** A store's logger of one stream, and what it declares its config `log` with. */
140
+ export function newStoreLogger(
141
+ append: (lines: ConsoleLine[]) => Promise<void>,
142
+ stream: string,
143
+ options: LoggerOptions = {},
144
+ ): [Logger, LogConfig] {
145
+ const { shared, spelled } = sharedOf(append, stream, options, undefined)
146
+ const level = options.level === undefined ? 'trace' : nameOf(levelOf(options.level))
147
+ const format = options.console ?? (prettyUntold(options, undefined) ? 'pretty' : 'json')
148
+ const time = options.time ?? 'clock'
149
+ const others = (all: readonly string[], one: string) => all.filter(name => name !== one)
150
+ const declaration: LogDeclaration = {
151
+ level: live(oneOf(level, others(Object.keys(levels), level))),
152
+ format: oneOf(format, others(formats, format)),
153
+ time: oneOf(time, others(times, time)),
154
+ }
155
+ const env = options.env ?? environmentOf('')
156
+ const take = (name: string) => {
157
+ if (Object.hasOwn(levels, name)) {
158
+ shared.least = levels[name as keyof typeof levels]
159
+ }
160
+ }
161
+ if (env === false) {
162
+ return [new Logger(shared, []), { layers: [declaration], take }]
163
+ }
164
+ const prefix = env.prefix === '' ? 'LOG' : `${env.prefix}_LOG`
165
+ const layers = [declaration, environmentOf(prefix, { from: spelled })] as const
166
+ return [new Logger(shared, []), { layers, take }]
167
+ }
168
+
169
+ /** The least named level at or above a level the code gave as a number. */
170
+ function nameOf(least: number): string {
171
+ const found = Object.entries(levels).find(([, number]) => number >= least)
172
+ return found?.[0] ?? 'error'
173
+ }
174
+
175
+ /**
176
+ * What a logger shares with the loggers its `with` makes, and the variables
177
+ * it read as the config `log` spells them.
178
+ */
179
+ function sharedOf(
180
+ append: ((lines: ConsoleLine[]) => Promise<void>) | undefined,
181
+ stream: string,
182
+ options: LoggerOptions,
183
+ out: (ConsoleOut & { utc?: boolean }) | undefined,
184
+ ): { shared: Shared; spelled: Record<string, string> } {
89
185
  if (stream === '') {
90
186
  throw new InvalidError('a logger of no stream')
91
187
  }
@@ -93,13 +189,15 @@ export function newLogger(
93
189
  if (!Number.isInteger(most) || most < 1) {
94
190
  throw new InvalidError(`a logger's buffer of ${most} lines`)
95
191
  }
96
- const [read, ignored] = fromEnvironment(options)
192
+ const [read, ignored, spelled] = fromEnvironment(options)
97
193
  const shared: Shared = {
98
194
  append,
99
195
  stream,
100
196
  most,
101
197
  least: read.level === undefined ? Number.NEGATIVE_INFINITY : levelOf(read.level),
102
- hides: redactor(read.redact),
198
+ redact: redactor(read.redact, read.keepUrlPasswords),
199
+ replace: read.replace,
200
+ source: read.source === true,
103
201
  print: printer(read, out),
104
202
  waiting: [],
105
203
  timer: undefined,
@@ -109,48 +207,77 @@ export function newLogger(
109
207
  drops: append === undefined ? undefined : new DropNotice(stream, most, sayOwn),
110
208
  }
111
209
  sayIgnored(shared.print, ignored)
112
- return new Logger(shared, [])
210
+ return { shared, spelled }
113
211
  }
114
212
 
115
213
  type Unread = [name: string, value: string, expected: string]
116
214
 
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[]] {
215
+ /**
216
+ * LOG_LEVEL, LOG_FORMAT and LOG_TIME over the options, after the prefix env
217
+ * names, as Go and Python read them; a console turned off stays off. Each
218
+ * variable read is spelled as the config `log` reads it, by its name.
219
+ */
220
+ function fromEnvironment(
221
+ options: LoggerOptions,
222
+ ): [LoggerOptions, Unread[], Record<string, string>] {
119
223
  const read = { ...options }
120
224
  const ignored: Unread[] = []
121
- const level = variable('LOG_LEVEL')
225
+ const spelled: Record<string, string> = {}
226
+ const env = options.env ?? environmentOf('')
227
+ if (env === false) {
228
+ return [read, ignored, spelled]
229
+ }
230
+ const variables = environment(env)
231
+ const name = (variable: string) => (env.prefix === '' ? variable : `${env.prefix}_${variable}`)
232
+ const level = variables(name('LOG_LEVEL'))
122
233
  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
234
+ const known = level.toLowerCase() === 'warning' ? 'warn' : level.toLowerCase()
235
+ if (Object.hasOwn(levels, known)) {
236
+ read.level = levels[known as keyof typeof levels]
237
+ spelled[name('LOG_LEVEL')] = known
126
238
  } else {
127
- ignored.push(['LOG_LEVEL', level, 'debug, info, warn or error'])
239
+ ignored.push([name('LOG_LEVEL'), level, 'trace, debug, info, warn or error'])
128
240
  }
129
241
  }
130
- const format = variable('LOG_FORMAT')
242
+ const format = variables(name('LOG_FORMAT'))
131
243
  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'])
244
+ const known = format.toLowerCase()
245
+ if (known !== 'pretty' && known !== 'json' && known !== 'off') {
246
+ ignored.push([name('LOG_FORMAT'), format, 'pretty, json or off'])
135
247
  } else if (read.console !== 'off') {
136
- read.console = name
248
+ read.console = known
249
+ spelled[name('LOG_FORMAT')] = known
137
250
  }
138
251
  }
139
- const time = variable('LOG_TIME')
252
+ const time = variables(name('LOG_TIME'))
140
253
  if (time !== undefined) {
141
- const name = time.toLowerCase()
142
- if (name === 'clock' || name === 'full' || name === 'off') {
143
- read.time = name
254
+ const known = time.toLowerCase()
255
+ if (known === 'clock' || known === 'full' || known === 'off') {
256
+ read.time = known
257
+ spelled[name('LOG_TIME')] = known
144
258
  } else {
145
- ignored.push(['LOG_TIME', time, 'clock, full or off'])
259
+ ignored.push([name('LOG_TIME'), time, 'clock, full or off'])
146
260
  }
147
261
  }
148
- return [read, ignored]
262
+ return [read, ignored, spelled]
149
263
  }
150
264
 
151
- function variable(name: string): string | undefined {
152
- const text = process.env[name]?.trim()
153
- return text === '' ? undefined : text
265
+ /** The variables of env's files, the process's or `from`'s over them, a missing file skipped; an empty one is none. */
266
+ function environment(env: EnvLayer): (name: string) => string | undefined {
267
+ const fromFiles: Record<string, string> = {}
268
+ for (const file of env.files) {
269
+ let text: string
270
+ try {
271
+ text = readFileSync(file, 'utf8')
272
+ } catch {
273
+ continue
274
+ }
275
+ Object.assign(fromFiles, parseDotenv(text))
276
+ }
277
+ return name => {
278
+ const text = ((env.from ?? process.env)[name] ?? fromFiles[name])?.trim()
279
+ return text === '' ? undefined : text
280
+ }
154
281
  }
155
282
 
156
283
  /** The values said to be ignored, so that a program making many loggers says each once. */
@@ -186,17 +313,23 @@ function forceColor(): boolean {
186
313
  return force !== undefined && force !== '' && force !== '0'
187
314
  }
188
315
 
316
+ /** Whether a console told no format prints pretty lines: on a terminal, or where FORCE_COLOR asks. */
317
+ function prettyUntold(options: LoggerOptions, out: ConsoleOut | undefined): boolean {
318
+ const stream: ConsoleOut = out ?? options.to ?? process.stderr
319
+ // FORCE_COLOR other than 0 says a person reads a pipe, as an IDE's run console is one
320
+ return stream.isTTY === true || (out === undefined && forceColor())
321
+ }
322
+
189
323
  /** How a logger prints its lines, or undefined when it prints none. */
190
324
  function printer(options: LoggerOptions, out?: ConsoleOut & { utc?: boolean }): Shared['print'] {
191
325
  if (options.console === 'off') {
192
326
  return undefined
193
327
  }
194
328
  const stream: ConsoleOut = out ?? options.to ?? process.stderr
195
- // FORCE_COLOR other than 0 says a person reads a pipe, as an IDE's run console is one
196
329
  const forced = out === undefined && forceColor()
197
330
  const terminal = stream.isTTY === true
198
331
  const pretty =
199
- options.console === 'pretty' || (options.console === undefined && (terminal || forced))
332
+ options.console === 'pretty' || (options.console === undefined && prettyUntold(options, out))
200
333
  if (!pretty) {
201
334
  return line => void stream.write(jsonLine(line))
202
335
  }
@@ -220,7 +353,7 @@ function printer(options: LoggerOptions, out?: ConsoleOut & { utc?: boolean }):
220
353
  * its context, who is speaking.
221
354
  *
222
355
  * ```ts
223
- * const log = store.records.logger('api')
356
+ * const log = store.logger('api')
224
357
  * log.info('server started', { port: 3000 })
225
358
  * log.with({ requestId }).warn('slow request', { ms: 1200 })
226
359
  * log.event('user.created', { userId: 42 }) // a record named for the event
@@ -231,7 +364,7 @@ export class Logger {
231
364
  /** key, JSON, key, JSON…, hidden as the logger's redact says */
232
365
  readonly #context: string[]
233
366
 
234
- /** Loggers come from `logger(stream)`, `store.records.logger(stream)` and their `with`. */
367
+ /** Loggers come from `logger(stream)`, `store.logger(stream)` and their `with`. */
235
368
  constructor(shared: Shared, context: string[]) {
236
369
  this.#shared = shared
237
370
  this.#context = context
@@ -273,7 +406,7 @@ export class Logger {
273
406
  with(context: Fields): Logger {
274
407
  return new Logger(this.#shared, [
275
408
  ...this.#context,
276
- ...encodeFields(context, this.#shared.hides),
409
+ ...encodeFields(context, this.#shared.redact, this.#shared.replace),
277
410
  ])
278
411
  }
279
412
 
@@ -311,12 +444,14 @@ export class Logger {
311
444
 
312
445
  #emit(what: Pick<ConsoleLine, 'level' | 'event' | 'msg'>, attrs: Fields | undefined): void {
313
446
  const trace = traceOf(currentTrace())
447
+ const shared = this.#shared
448
+ const encoded = attrs === undefined ? [] : encodeFields(attrs, shared.redact, shared.replace)
314
449
  const line: ConsoleLine = {
315
450
  at: new Date(),
316
- stream: this.#shared.stream,
451
+ stream: shared.stream,
317
452
  ...what,
318
453
  context: this.#context,
319
- attrs: attrs === undefined ? [] : encodeFields(attrs, this.#shared.hides),
454
+ attrs: shared.source ? [...encoded, sourceKey, sourceOfCaller()] : encoded,
320
455
  ...trace,
321
456
  }
322
457
  this.#shared.print?.(line)
@@ -369,20 +504,27 @@ async function writeWaiting(
369
504
 
370
505
  /**
371
506
  * Fields as key, JSON pairs, as a console writes them and the store keeps
372
- * them: an Error's value its stack, which JSON would write as {}, and a field
373
- * hides names hidden at any depth.
507
+ * them: an Error's value its stack, which JSON would write as {}, a secret's
508
+ * value hidden at any depth, and a URL's password unless kept.
374
509
  */
375
510
  export function encodeFields(
376
511
  fields: Fields,
377
- hides: ((key: string) => boolean) | undefined,
512
+ redact: Redactor | undefined,
513
+ replace?: (key: string, value: unknown) => unknown,
378
514
  ): string[] {
379
515
  const pairs = Array.isArray(fields)
380
516
  ? (fields as readonly (readonly [string, unknown])[])
381
517
  : Object.entries(fields as Record<string, unknown>)
382
518
  const flat: string[] = []
383
- for (const [key, value] of pairs) {
519
+ for (const [key, given] of pairs) {
520
+ const value = replace === undefined ? given : replace(key, given)
384
521
  const shown = value instanceof Error ? (value.stack ?? String(value)) : value
385
- flat.push(key, hides?.(key) ? redacted : jsonOf(shown, hides))
522
+ if (redact?.hides(key)) {
523
+ flat.push(key, redacted)
524
+ continue
525
+ }
526
+ const json = jsonOf(shown, redact?.hides)
527
+ flat.push(key, redact?.urls === true ? hideUrlPasswords(json) : json)
386
528
  }
387
529
  return flat
388
530
  }
@@ -409,3 +551,30 @@ function traceOf(trace: Trace | undefined): Pick<ConsoleLine, 'traceId' | 'spanI
409
551
  }
410
552
  return spanId !== undefined && /^[0-9a-f]{16}$/.test(spanId) ? { traceId, spanId } : { traceId }
411
553
  }
554
+
555
+ /** This file, whose frames a line's source skips. */
556
+ const here = pathOf(import.meta.url)
557
+
558
+ /**
559
+ * Where a line was logged, as slog's handlers spell a source: the first frame
560
+ * of the stack outside this file.
561
+ */
562
+ function sourceOfCaller(): string {
563
+ for (const frame of (new Error().stack ?? '').split('\n').slice(1)) {
564
+ const found = /at (?:(.+?) \()?(.+):(\d+):\d+\)?$/.exec(frame.trim())
565
+ if (found === null || pathOf(found[2] as string) === here) {
566
+ continue
567
+ }
568
+ return JSON.stringify({
569
+ function: found[1] ?? '',
570
+ file: pathOf(found[2] as string),
571
+ line: Number(found[3]),
572
+ })
573
+ }
574
+ return '{}'
575
+ }
576
+
577
+ /** A frame's file as a path, which Node writes as a file: URL. */
578
+ function pathOf(file: string): string {
579
+ return file.startsWith('file://') ? fileURLToPath(file) : file
580
+ }