dsh-runtime-log 0.0.0-stage → 0.1.2
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/CHANGELOG.md +53 -0
- package/LICENSE +21 -0
- package/README.md +142 -2
- package/cordis.patch.yml +7 -0
- package/package.json +40 -4
- package/src/index.js +284 -0
- package/src/log-sink.js +489 -0
- package/src/retention.js +348 -0
package/src/log-sink.js
ADDED
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded, best-effort JSONL sink; not crash-safe storage.
|
|
3
|
+
* capture is synchronous; flush shares one never-rejecting writer promise and
|
|
4
|
+
* appends the pending queue as one batched write per drain cycle; close stops
|
|
5
|
+
* intake and drains to one deadline. An issued OS call cannot be
|
|
6
|
+
* cancelled. After timeout only late settlement and handle cleanup are allowed.
|
|
7
|
+
*
|
|
8
|
+
* Only primitive arguments and native Error own string data fields are logged.
|
|
9
|
+
* Unsupported objects are [Object]; no object traversal, coercion or getters.
|
|
10
|
+
* Lazy/accessor or unsafe Error stacks become [Stack unavailable]. In V8 older
|
|
11
|
+
* than Node 24 even inspecting a lazy stack descriptor may execute code;
|
|
12
|
+
* native Error stacks on those versions conservatively use the marker.
|
|
13
|
+
* Recognized secrets are redacted before clipping, with bounded text scanning:
|
|
14
|
+
* key=value and key:value forms, quoted or bare values directly after a
|
|
15
|
+
* credential keyword (skipping common English prose via a stopword guard),
|
|
16
|
+
* and URL userinfo (scheme://user:pass@host). This is best-effort filtering,
|
|
17
|
+
* not complete scrubbing of arbitrary secrets.
|
|
18
|
+
*
|
|
19
|
+
* filePath is a unique run namespace, exclusively created and never resumed.
|
|
20
|
+
* A write/rotation failure disables this sink: a partial append may leave a
|
|
21
|
+
* torn final line, but no later JSONL will be appended to it. Retention of other
|
|
22
|
+
* runs belongs to the caller. fs/timers/now remain injectable for fault tests.
|
|
23
|
+
*/
|
|
24
|
+
import defaultFs from 'node:fs/promises'
|
|
25
|
+
import nodePath from 'node:path'
|
|
26
|
+
import nodeOs from 'node:os'
|
|
27
|
+
import { randomUUID } from 'node:crypto'
|
|
28
|
+
import { types } from 'node:util'
|
|
29
|
+
|
|
30
|
+
const DEFAULTS = Object.freeze({
|
|
31
|
+
maxFileBytes: 5 * 1024 * 1024,
|
|
32
|
+
maxRotations: 3,
|
|
33
|
+
maxQueueRecords: 2000,
|
|
34
|
+
maxQueueBytes: 1024 * 1024,
|
|
35
|
+
flushIntervalMs: 200,
|
|
36
|
+
maxEntryBytes: 16 * 1024,
|
|
37
|
+
closeTimeoutMs: 2000,
|
|
38
|
+
})
|
|
39
|
+
const DEFAULT_LEVELS = ['error', 'warn', 'info', 'debug']
|
|
40
|
+
const MAX_ARGS = 20
|
|
41
|
+
const MAX_SCAN = 64 * 1024
|
|
42
|
+
const STACK_UNAVAILABLE = '[Stack unavailable]'
|
|
43
|
+
const NativeError = Error
|
|
44
|
+
// V8 switched lazy stack storage to ordinary accessors in Node 24. Earlier
|
|
45
|
+
// versions can run the formatter merely by requesting its descriptor.
|
|
46
|
+
const stackDescriptorIsInert = Number(process.versions.node.split('.')[0]) >= 24
|
|
47
|
+
// A quoted value may contain escaped quotes; an unclosed quote consumes the
|
|
48
|
+
// rest of its line so a partially written secret is never left behind.
|
|
49
|
+
const QUOTED_VALUE = String.raw`"(?:[^"\\\r\n]|\\.)*(?:"|\\(?=[\r\n]|$)|(?=[\r\n]|$))|'(?:[^'\\\r\n]|\\.)*(?:'|\\(?=[\r\n]|$)|(?=[\r\n]|$))`
|
|
50
|
+
// Cookie/Set-Cookie carries ';'-separated pairs; quoted values may contain
|
|
51
|
+
// semicolons or escaped quotes.
|
|
52
|
+
const COOKIE_TOKEN = String.raw`[^\s,;"'=]+`
|
|
53
|
+
const COOKIE_VALUE = String.raw`(?:${QUOTED_VALUE}|[^;\r\n]*)`
|
|
54
|
+
const COOKIE_PAIR = String.raw`${COOKIE_TOKEN}[^\S\r\n]*=[^\S\r\n]*${COOKIE_VALUE}|${QUOTED_VALUE}|[^\s,;"'=]+`
|
|
55
|
+
// Compound names such as DB_PASSWORD are suffixes of a longer identifier, so
|
|
56
|
+
// the leading boundary must allow '_' (and '-') while still refusing letters.
|
|
57
|
+
const CREDENTIAL_KEY = 'api[_-]?key|access[_-]?key|access[_-]?token|refresh[_-]?token|session[_-]?token|client[_-]?secret|secret[_-]?key|private[_-]?key|credentials?|authorization|secret|password|passwd|pwd|token|auth|cookie'
|
|
58
|
+
// A bare value directly after a credential keyword is redacted unless it is
|
|
59
|
+
// one of these common English prose words, so diagnostic sentences such as
|
|
60
|
+
// "token was invalid" or "password is required" stay readable.
|
|
61
|
+
const BARE_VALUE_STOPWORDS = [
|
|
62
|
+
'is', 'are', 'was', 'were', 'be', 'been', 'being', 'am',
|
|
63
|
+
'has', 'have', 'had', 'do', 'does', 'did', 'done',
|
|
64
|
+
'will', 'would', 'shall', 'should', 'can', 'could', 'may', 'might', 'must',
|
|
65
|
+
'not', 'no', 'none', 'null', 'undefined', 'empty', 'missing',
|
|
66
|
+
'invalid', 'incorrect', 'wrong', 'mismatch', 'expired',
|
|
67
|
+
'required', 'failed', 'failure', 'denied', 'rejected', 'revoked',
|
|
68
|
+
'disabled', 'enabled', 'error', 'errors', 'warning', 'warn',
|
|
69
|
+
'for', 'from', 'with', 'without', 'to', 'into', 'onto', 'of', 'in', 'on', 'at', 'by',
|
|
70
|
+
'the', 'a', 'an', 'and', 'or', 'if', 'when', 'while', 'then', 'than',
|
|
71
|
+
'that', 'this', 'these', 'those', 'it', 'its',
|
|
72
|
+
'use', 'used', 'using', 'please', 'provide', 'enter', 'set', 'get',
|
|
73
|
+
'check', 'verify', 'expected', 'found', 'type', 'value', 'values',
|
|
74
|
+
'field', 'fields', 'key', 'keys', 'name', 'names',
|
|
75
|
+
]
|
|
76
|
+
const TEXT_RULES = [
|
|
77
|
+
// Authorization may use any scheme (token, Digest, ...); redact its line value.
|
|
78
|
+
[new RegExp('((?<![A-Za-z0-9])(?:(?:proxy|x)[-_])?authorization)["\']?\\s*[=:]\\s*[^\\r\\n]*', 'gi'), '$1: [REDACTED]'],
|
|
79
|
+
// All cookie pairs are secrets, not only the first one.
|
|
80
|
+
[new RegExp('((?<![A-Za-z0-9])(?:(?:set|x)[-_])?cookie)["\']?\\s*[=:]\\s*(?:' + COOKIE_PAIR + ')(?:[^\\S\\r\\n]*;[^\\S\\r\\n]*(?:' + COOKIE_PAIR + '))*', 'gi'), '$1: [REDACTED]'],
|
|
81
|
+
// The scheme explicitly marks the following token as an auth credential.
|
|
82
|
+
// Preserve only the known Cordis-style prose messages, not short passwords.
|
|
83
|
+
[/\b(?!(?:Basic\s+authentication\s+failed|Bearer\s+authentication\s+required)\b)(?:Bearer|Basic)\s+[^\s,;]+/gi, '[REDACTED]'],
|
|
84
|
+
[/\b(?:sk|pk|rk)-[A-Za-z0-9_-]{16,}/g, '[REDACTED]'],
|
|
85
|
+
[/\bgh[pousr]_[A-Za-z0-9]+/g, '[REDACTED]'],
|
|
86
|
+
[/\bxox[abprs]-[A-Za-z0-9-]+/g, '[REDACTED]'],
|
|
87
|
+
[/\bAKIA[0-9A-Z]+/g, '[REDACTED]'],
|
|
88
|
+
[/\beyJ[A-Za-z0-9_.-]+/g, '[REDACTED]'],
|
|
89
|
+
[new RegExp('((?<![A-Za-z0-9])(?:' + CREDENTIAL_KEY + ')["\']?\\s*[=:]\\s*)(?:' + QUOTED_VALUE + '|[^\\s,;]+)', 'gi'), '$1[REDACTED]'],
|
|
90
|
+
// URL userinfo (scheme://user:pass@host) is a credential in any position;
|
|
91
|
+
// only the colon form matches, so plain host:port URLs are untouched.
|
|
92
|
+
[new RegExp('((?:[a-z][a-z0-9+.-]*):\\/\\/)[^\\s\\/@:]*:[^\\s\\/@]*@', 'gi'), '$1[REDACTED]@'],
|
|
93
|
+
// A value quoted directly after a credential keyword needs no = or : at all.
|
|
94
|
+
[new RegExp('((?<![A-Za-z0-9])(?:' + CREDENTIAL_KEY + ')["\']?\\s+)(' + QUOTED_VALUE + ')', 'gi'), '$1[REDACTED]'],
|
|
95
|
+
// A bare value directly after a credential keyword, guarded against prose:
|
|
96
|
+
// the value must start with a word character and not be a stop word, so
|
|
97
|
+
// "password hunter2" is redacted while "password is required" stays intact.
|
|
98
|
+
[new RegExp('((?<![A-Za-z0-9])(?:' + CREDENTIAL_KEY + ')["\']?)(\\s+)(?!(?:' + BARE_VALUE_STOPWORDS.join('|') + ')(?![A-Za-z0-9]))[A-Za-z0-9_$][^\\s,;]*', 'gi'), '$1$2[REDACTED]'],
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
function positiveInt(value, fallback, minimum) {
|
|
102
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value >= minimum ? value : fallback
|
|
103
|
+
}
|
|
104
|
+
function createLimits(opts) {
|
|
105
|
+
const rotations = positiveInt(opts.maxFiles, 4, 1) - 1
|
|
106
|
+
return {
|
|
107
|
+
maxFileBytes: positiveInt(opts.maxFileBytes, DEFAULTS.maxFileBytes, 1),
|
|
108
|
+
maxRotations: positiveInt(opts.maxRotations, rotations, 0),
|
|
109
|
+
maxQueueRecords: positiveInt(opts.maxQueueRecords, DEFAULTS.maxQueueRecords, 1),
|
|
110
|
+
maxQueueBytes: positiveInt(opts.maxQueueBytes, DEFAULTS.maxQueueBytes, 1),
|
|
111
|
+
flushIntervalMs: positiveInt(opts.flushIntervalMs, DEFAULTS.flushIntervalMs, 0),
|
|
112
|
+
maxEntryBytes: positiveInt(opts.maxEntryBytes, DEFAULTS.maxEntryBytes, 128),
|
|
113
|
+
closeTimeoutMs: positiveInt(opts.closeTimeoutMs, positiveInt(opts.drainTimeoutMs, DEFAULTS.closeTimeoutMs, 1), 1),
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function ownData(value, key) {
|
|
117
|
+
if (value === null || (typeof value !== 'object' && typeof value !== 'function') || types.isProxy(value)) return undefined
|
|
118
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key)
|
|
119
|
+
return descriptor && Object.hasOwn(descriptor, 'value') ? descriptor.value : undefined
|
|
120
|
+
}
|
|
121
|
+
function clip(text, count) {
|
|
122
|
+
if (text.length <= count) return text
|
|
123
|
+
let end = Math.max(0, count - 1)
|
|
124
|
+
if (end > 0 && /[\uD800-\uDBFF]/.test(text[end - 1])) end--
|
|
125
|
+
return text.slice(0, end) + '…'
|
|
126
|
+
}
|
|
127
|
+
function scan(input, ctx) {
|
|
128
|
+
if (typeof input !== 'string') return ''
|
|
129
|
+
const size = Math.min(input.length, ctx.remaining)
|
|
130
|
+
ctx.remaining -= size
|
|
131
|
+
let text = input.slice(0, size)
|
|
132
|
+
if (size < input.length) {
|
|
133
|
+
ctx.truncated = true
|
|
134
|
+
// Discard the entire unfinished token, not a possibly sensitive prefix.
|
|
135
|
+
text = text.replace(/\S*$/, '') + '…'
|
|
136
|
+
}
|
|
137
|
+
return text
|
|
138
|
+
}
|
|
139
|
+
function redact(input, ctx) {
|
|
140
|
+
let text = scan(input, ctx)
|
|
141
|
+
for (const [pattern, replacement] of TEXT_RULES) text = text.replace(pattern, replacement)
|
|
142
|
+
return text
|
|
143
|
+
}
|
|
144
|
+
function safeErrorStack(error) {
|
|
145
|
+
// On older V8 there is no inert way to tell a materialized data property
|
|
146
|
+
// from a lazy one: descriptor lookup itself may invoke arbitrary hooks or
|
|
147
|
+
// allocate an unbounded stack. Fail closed rather than materializing it.
|
|
148
|
+
if (!stackDescriptorIsInert) return STACK_UNAVAILABLE
|
|
149
|
+
const value = ownData(error, 'stack')
|
|
150
|
+
return typeof value === 'string' ? value : STACK_UNAVAILABLE
|
|
151
|
+
}
|
|
152
|
+
function primitive(value, ctx) {
|
|
153
|
+
switch (typeof value) {
|
|
154
|
+
case 'string': return redact(value, ctx)
|
|
155
|
+
case 'number': return Object.is(value, -0) ? '-0' : String(value)
|
|
156
|
+
case 'boolean': return value ? 'true' : 'false'
|
|
157
|
+
// Huge bigint conversion is unbounded; keep it inert too.
|
|
158
|
+
case 'bigint': return '[BigInt]'
|
|
159
|
+
case 'undefined': return 'undefined'
|
|
160
|
+
case 'symbol': return '[Symbol]'
|
|
161
|
+
case 'function': return '[Function]'
|
|
162
|
+
default: break
|
|
163
|
+
}
|
|
164
|
+
if (value === null) return 'null'
|
|
165
|
+
if (types.isProxy(value) || !types.isNativeError(value)) return '[Object]'
|
|
166
|
+
const name = ownData(value, 'name')
|
|
167
|
+
const message = ownData(value, 'message')
|
|
168
|
+
if (ctx.stack === null) ctx.stack = redact(safeErrorStack(value), ctx)
|
|
169
|
+
return redact(typeof name === 'string' ? name : 'Error', ctx)
|
|
170
|
+
+ (typeof message === 'string' && message ? ': ' + redact(message, ctx) : '')
|
|
171
|
+
}
|
|
172
|
+
function extractArgs(record) {
|
|
173
|
+
const args = ownData(record, 'args')
|
|
174
|
+
if (types.isProxy(args)) return ['[Object]']
|
|
175
|
+
if (!Array.isArray(args)) {
|
|
176
|
+
const value = args === undefined ? ownData(record, 'message') : args
|
|
177
|
+
return value === undefined || value === null ? [] : [value]
|
|
178
|
+
}
|
|
179
|
+
const length = ownData(args, 'length')
|
|
180
|
+
const count = Math.min(typeof length === 'number' ? length : 0, MAX_ARGS)
|
|
181
|
+
const result = []
|
|
182
|
+
for (let i = 0; i < count; i++) {
|
|
183
|
+
const d = Object.getOwnPropertyDescriptor(args, String(i))
|
|
184
|
+
result.push(d && Object.hasOwn(d, 'value') ? d.value : '[Unavailable]')
|
|
185
|
+
}
|
|
186
|
+
if (length > MAX_ARGS) result.push('…')
|
|
187
|
+
return result
|
|
188
|
+
}
|
|
189
|
+
function formatArgs(args, ctx) {
|
|
190
|
+
if (args.length === 0) return ''
|
|
191
|
+
// Interpolate the bounded raw template before redacting its assembled text;
|
|
192
|
+
// otherwise api_key=%s could consume the directive and expose a spare arg.
|
|
193
|
+
const rendered = args.map((value, index) => index === 0 && typeof value === 'string' ? scan(value, ctx) : primitive(value, ctx))
|
|
194
|
+
if (typeof args[0] !== 'string') return rendered.join(' ')
|
|
195
|
+
let used = 1
|
|
196
|
+
const text = rendered[0].replace(/%([sdifjoOc%])/g, (match, code) => {
|
|
197
|
+
if (code === '%') return '%'
|
|
198
|
+
if (used >= rendered.length) return match
|
|
199
|
+
const raw = args[used]
|
|
200
|
+
const safe = rendered[used++]
|
|
201
|
+
if (code === 'c') return ''
|
|
202
|
+
if (code === 'd' || code === 'i' || code === 'f') {
|
|
203
|
+
if (raw !== null && !['number', 'boolean', 'string', 'undefined'].includes(typeof raw)) return safe
|
|
204
|
+
// Coercion is allowed only on already bounded, redacted primitive text.
|
|
205
|
+
const number = Number(typeof raw === 'string' ? safe : raw)
|
|
206
|
+
return String(code === 'i' && Number.isFinite(number) ? Math.trunc(number) : number)
|
|
207
|
+
}
|
|
208
|
+
return safe
|
|
209
|
+
})
|
|
210
|
+
return [text, ...rendered.slice(used)].join(' ').trim()
|
|
211
|
+
}
|
|
212
|
+
function normalizeLevel(record, levels) {
|
|
213
|
+
let raw
|
|
214
|
+
for (const key of ['level', 'levelName', 'severity', 'type']) {
|
|
215
|
+
raw = ownData(record, key)
|
|
216
|
+
if (raw !== null && raw !== undefined) break
|
|
217
|
+
}
|
|
218
|
+
let level = typeof raw === 'string' && raw.length <= 16 ? raw.toLowerCase()
|
|
219
|
+
: typeof raw === 'number' && Number.isInteger(raw) ? levels[raw] : null
|
|
220
|
+
if (level === 'warning') level = 'warn'
|
|
221
|
+
return level === 'warn' || level === 'error' ? level : null
|
|
222
|
+
}
|
|
223
|
+
function isoNow(now) {
|
|
224
|
+
try {
|
|
225
|
+
const value = now()
|
|
226
|
+
if (typeof value === 'number' && Number.isFinite(value)) return new Date(value).toISOString()
|
|
227
|
+
} catch { /* trusted injected clock may fail */ }
|
|
228
|
+
return new Date().toISOString()
|
|
229
|
+
}
|
|
230
|
+
function buildEntry(record, level, limits, now) {
|
|
231
|
+
const ctx = { remaining: Math.min(MAX_SCAN, limits.maxEntryBytes * 4), truncated: false, stack: null }
|
|
232
|
+
// All fields are redacted before any final field/encoded byte clipping.
|
|
233
|
+
const name = redact(ownData(record, 'name'), ctx)
|
|
234
|
+
const message = formatArgs(extractArgs(record), ctx)
|
|
235
|
+
const stack = ctx.stack ?? (types.isNativeError(record) ? STACK_UNAVAILABLE : redact(ownData(record, 'stack'), ctx))
|
|
236
|
+
const finalCtx = { remaining: MAX_SCAN, truncated: false }
|
|
237
|
+
const nameText = redact(name, finalCtx)
|
|
238
|
+
const nameClipped = nameText.length > 120
|
|
239
|
+
const entry = { time: isoNow(now), level, name: nameClipped ? clip(nameText, 120) : nameText, message: redact(message, finalCtx) }
|
|
240
|
+
if (stack) entry.stack = redact(stack, finalCtx)
|
|
241
|
+
if (ctx.truncated || finalCtx.truncated || nameClipped) entry.truncated = true
|
|
242
|
+
const cap = Math.min(limits.maxEntryBytes, limits.maxFileBytes, limits.maxQueueBytes)
|
|
243
|
+
for (;;) {
|
|
244
|
+
const data = Buffer.from(JSON.stringify(entry) + '\n', 'utf8')
|
|
245
|
+
if (data.length <= cap) return data
|
|
246
|
+
entry.truncated = true
|
|
247
|
+
// JSON escaping is included in each measurement; never split a JSON line.
|
|
248
|
+
if (entry.stack && entry.stack.length > 1) entry.stack = clip(entry.stack, Math.floor(entry.stack.length / 2))
|
|
249
|
+
else if (entry.stack) delete entry.stack
|
|
250
|
+
else if (entry.message.length > 1) entry.message = clip(entry.message, Math.floor(entry.message.length / 2))
|
|
251
|
+
else if (entry.message) entry.message = ''
|
|
252
|
+
else if (entry.name.length > 1) entry.name = clip(entry.name, Math.floor(entry.name.length / 2))
|
|
253
|
+
else if (entry.name) entry.name = ''
|
|
254
|
+
else return null // fixed JSON envelope cannot fit this deliberately tiny cap
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function createLogSink(options = {}) {
|
|
259
|
+
const opts = options && typeof options === 'object' ? options : {}
|
|
260
|
+
const limits = createLimits(opts)
|
|
261
|
+
const fsp = opts.fs ?? defaultFs
|
|
262
|
+
const timers = { setInterval, clearInterval, setTimeout, clearTimeout, ...opts.timers }
|
|
263
|
+
const now = typeof opts.now === 'function' ? opts.now : Date.now
|
|
264
|
+
const levels = Array.isArray(opts.levels) ? opts.levels : DEFAULT_LEVELS
|
|
265
|
+
const onError = typeof opts.onError === 'function' ? opts.onError : null
|
|
266
|
+
const filePath = typeof opts.filePath === 'string' ? nodePath.resolve(opts.filePath)
|
|
267
|
+
: nodePath.join(typeof opts.dir === 'string' ? nodePath.resolve(opts.dir) : nodePath.join(nodeOs.tmpdir(), 'dsh-runtime-log'),
|
|
268
|
+
typeof opts.fileName === 'string' ? nodePath.basename(opts.fileName) : 'runtime-' + process.pid + '-' + randomUUID() + '.jsonl')
|
|
269
|
+
const state = { accepted: 0, ignored: 0, dropped: 0, written: 0, errors: 0, rotations: 0, bytesWritten: 0, lost: 0, lastError: null }
|
|
270
|
+
let queue = []
|
|
271
|
+
let queuedBytes = 0
|
|
272
|
+
let inFlight = null
|
|
273
|
+
let handle = null
|
|
274
|
+
let initialized = false
|
|
275
|
+
let currentSize = 0
|
|
276
|
+
let accepting = true
|
|
277
|
+
let closed = false
|
|
278
|
+
let disabled = false
|
|
279
|
+
let timedOut = false
|
|
280
|
+
let writerPromise = null
|
|
281
|
+
let closePromise = null
|
|
282
|
+
let timer = null
|
|
283
|
+
const done = Promise.resolve()
|
|
284
|
+
const stopped = () => disabled || timedOut || closed
|
|
285
|
+
|
|
286
|
+
function clearTimer() {
|
|
287
|
+
if (timer !== null) {
|
|
288
|
+
try { timers.clearInterval(timer) } catch { /* no logger recursion */ }
|
|
289
|
+
timer = null
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function discardPending() {
|
|
293
|
+
state.lost += queue.length
|
|
294
|
+
queue = []
|
|
295
|
+
queuedBytes = 0
|
|
296
|
+
}
|
|
297
|
+
function noteError() {
|
|
298
|
+
state.errors++
|
|
299
|
+
state.lastError = 'Runtime log I/O failed; sink disabled.'
|
|
300
|
+
// No raw filesystem text, paths, credentials or arbitrary error hooks.
|
|
301
|
+
if (onError && state.errors === 1) {
|
|
302
|
+
try { onError(new NativeError(state.lastError)) } catch { /* callback containment */ }
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function disable() {
|
|
306
|
+
disabled = true
|
|
307
|
+
accepting = false
|
|
308
|
+
clearTimer()
|
|
309
|
+
discardPending()
|
|
310
|
+
noteError()
|
|
311
|
+
}
|
|
312
|
+
async function releaseHandle() {
|
|
313
|
+
const current = handle
|
|
314
|
+
handle = null
|
|
315
|
+
if (current) {
|
|
316
|
+
try { await current.close() } catch { if (!disabled && !timedOut) disable() }
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
async function initialize() {
|
|
320
|
+
if (initialized || stopped()) return
|
|
321
|
+
await fsp.mkdir(nodePath.dirname(filePath), { recursive: true })
|
|
322
|
+
if (stopped()) return
|
|
323
|
+
// O_EXCL refuses existing files, symlinks and concurrent owners atomically.
|
|
324
|
+
handle = await fsp.open(filePath, 'wx', 0o600)
|
|
325
|
+
initialized = true
|
|
326
|
+
}
|
|
327
|
+
async function remove(target) {
|
|
328
|
+
if (stopped()) return
|
|
329
|
+
try { await fsp.unlink(target) } catch (error) { if (ownData(error, 'code') !== 'ENOENT') throw error }
|
|
330
|
+
}
|
|
331
|
+
async function rotate() {
|
|
332
|
+
await releaseHandle()
|
|
333
|
+
if (stopped()) return
|
|
334
|
+
if (limits.maxRotations === 0) {
|
|
335
|
+
await remove(filePath)
|
|
336
|
+
} else {
|
|
337
|
+
await remove(filePath + '.' + limits.maxRotations)
|
|
338
|
+
for (let i = limits.maxRotations - 1; i >= 1 && !stopped(); i--) {
|
|
339
|
+
try {
|
|
340
|
+
if (!stopped()) await fsp.rename(filePath + '.' + i, filePath + '.' + (i + 1))
|
|
341
|
+
} catch (error) { if (ownData(error, 'code') !== 'ENOENT') throw error }
|
|
342
|
+
}
|
|
343
|
+
if (!stopped()) await fsp.rename(filePath, filePath + '.1')
|
|
344
|
+
}
|
|
345
|
+
if (stopped()) return
|
|
346
|
+
handle = await fsp.open(filePath, 'wx', 0o600)
|
|
347
|
+
currentSize = 0
|
|
348
|
+
state.rotations++
|
|
349
|
+
}
|
|
350
|
+
async function drain() {
|
|
351
|
+
try {
|
|
352
|
+
if (stopped()) return
|
|
353
|
+
if (!queue.length) return
|
|
354
|
+
await initialize()
|
|
355
|
+
while (queue.length && !stopped()) {
|
|
356
|
+
// Keep the pending record evictable throughout initialization/rotation.
|
|
357
|
+
if (currentSize + queue[0].length > limits.maxFileBytes) {
|
|
358
|
+
await rotate()
|
|
359
|
+
continue
|
|
360
|
+
}
|
|
361
|
+
// One appendFile per drain cycle instead of one per record: every
|
|
362
|
+
// queued record that fits into the current segment shares a single
|
|
363
|
+
// write through the writeFile machinery. The batch is bounded by the
|
|
364
|
+
// queue byte cap, and inFlight keeps it counted against capacity
|
|
365
|
+
// while the append is outstanding.
|
|
366
|
+
const batch = []
|
|
367
|
+
let batchBytes = 0
|
|
368
|
+
while (queue.length) {
|
|
369
|
+
const next = queue[0]
|
|
370
|
+
if (currentSize + batchBytes + next.length > limits.maxFileBytes) break
|
|
371
|
+
queue.shift()
|
|
372
|
+
queuedBytes -= next.length
|
|
373
|
+
batch.push(next)
|
|
374
|
+
batchBytes += next.length
|
|
375
|
+
}
|
|
376
|
+
inFlight = {
|
|
377
|
+
records: batch.length,
|
|
378
|
+
bytes: batchBytes,
|
|
379
|
+
data: batchBytes === batch[0].length ? batch[0] : Buffer.concat(batch, batchBytes),
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
await fsp.appendFile(handle, inFlight.data)
|
|
383
|
+
currentSize += batchBytes
|
|
384
|
+
state.written += batch.length
|
|
385
|
+
state.bytesWritten += batchBytes
|
|
386
|
+
} catch (error) {
|
|
387
|
+
state.lost += batch.length
|
|
388
|
+
throw error
|
|
389
|
+
} finally { inFlight = null }
|
|
390
|
+
}
|
|
391
|
+
} catch { disable() }
|
|
392
|
+
finally {
|
|
393
|
+
if (stopped() || !accepting) await releaseHandle()
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function flush() {
|
|
397
|
+
if (writerPromise) return writerPromise
|
|
398
|
+
if (stopped() || (queue.length === 0 && (accepting || !handle))) return done
|
|
399
|
+
// Schedule the work only after publishing the sole shared promise.
|
|
400
|
+
writerPromise = Promise.resolve().then(async () => {
|
|
401
|
+
try {
|
|
402
|
+
// Include arrivals in the microtask window between drain settlement
|
|
403
|
+
// and this shared promise's completion, without chaining flush calls.
|
|
404
|
+
do { await drain() } while (queue.length && !stopped())
|
|
405
|
+
} catch { disable() }
|
|
406
|
+
finally { writerPromise = null }
|
|
407
|
+
})
|
|
408
|
+
return writerPromise
|
|
409
|
+
}
|
|
410
|
+
function capture(record) {
|
|
411
|
+
try {
|
|
412
|
+
if (!accepting || stopped() || !record || typeof record !== 'object' || types.isProxy(record)) {
|
|
413
|
+
state.ignored++
|
|
414
|
+
return false
|
|
415
|
+
}
|
|
416
|
+
const level = normalizeLevel(record, levels)
|
|
417
|
+
if (level === null) { state.ignored++; return false }
|
|
418
|
+
const data = buildEntry(record, level, limits, now)
|
|
419
|
+
if (data === null) { state.dropped++; return false }
|
|
420
|
+
const flightBytes = inFlight?.bytes ?? 0
|
|
421
|
+
const flightCount = inFlight?.records ?? 0
|
|
422
|
+
while (queue.length && (queue.length + flightCount + 1 > limits.maxQueueRecords || queuedBytes + flightBytes + data.length > limits.maxQueueBytes)) {
|
|
423
|
+
queuedBytes -= queue.shift().length
|
|
424
|
+
state.dropped++
|
|
425
|
+
}
|
|
426
|
+
if (queue.length + flightCount + 1 > limits.maxQueueRecords || queuedBytes + flightBytes + data.length > limits.maxQueueBytes) {
|
|
427
|
+
state.dropped++
|
|
428
|
+
return false
|
|
429
|
+
}
|
|
430
|
+
queue.push(data)
|
|
431
|
+
queuedBytes += data.length
|
|
432
|
+
state.accepted++
|
|
433
|
+
return true
|
|
434
|
+
} catch {
|
|
435
|
+
// Ordinary formatting failures are contained without retaining raw args.
|
|
436
|
+
state.dropped++
|
|
437
|
+
return false
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
function close() {
|
|
441
|
+
if (closePromise) return closePromise
|
|
442
|
+
accepting = false
|
|
443
|
+
clearTimer()
|
|
444
|
+
// close can begin after drain's finally ran but before its shared promise
|
|
445
|
+
// settles; always perform one final owned-handle cleanup in that case.
|
|
446
|
+
const draining = flush().then(releaseHandle)
|
|
447
|
+
closePromise = new Promise(resolve => {
|
|
448
|
+
let settled = false
|
|
449
|
+
let deadline
|
|
450
|
+
const finish = timeout => {
|
|
451
|
+
if (settled) return
|
|
452
|
+
settled = true
|
|
453
|
+
if (timeout) {
|
|
454
|
+
timedOut = true
|
|
455
|
+
discardPending()
|
|
456
|
+
}
|
|
457
|
+
closed = true
|
|
458
|
+
if (deadline !== undefined) {
|
|
459
|
+
try { timers.clearTimeout(deadline) } catch { /* bounded close */ }
|
|
460
|
+
}
|
|
461
|
+
resolve()
|
|
462
|
+
}
|
|
463
|
+
// The explicit close deadline stays referenced: awaiting close must work
|
|
464
|
+
// even when an injected write never settles and no other handles exist.
|
|
465
|
+
try { deadline = timers.setTimeout(() => finish(true), limits.closeTimeoutMs) }
|
|
466
|
+
catch { finish(true) }
|
|
467
|
+
draining.then(() => finish(false), () => finish(false))
|
|
468
|
+
})
|
|
469
|
+
return closePromise
|
|
470
|
+
}
|
|
471
|
+
function stats() {
|
|
472
|
+
const flight = { records: inFlight?.records ?? 0, bytes: inFlight?.bytes ?? 0 }
|
|
473
|
+
return {
|
|
474
|
+
...state,
|
|
475
|
+
queued: { records: queue.length, bytes: queuedBytes },
|
|
476
|
+
inFlight: flight,
|
|
477
|
+
buffered: { records: queue.length + flight.records, bytes: queuedBytes + flight.bytes },
|
|
478
|
+
limits: { ...limits },
|
|
479
|
+
accepting: accepting && !stopped(), closed, disabled, timedOut,
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (limits.flushIntervalMs > 0) {
|
|
483
|
+
try {
|
|
484
|
+
timer = timers.setInterval(() => { void flush() }, limits.flushIntervalMs)
|
|
485
|
+
timer?.unref?.()
|
|
486
|
+
} catch { disable() }
|
|
487
|
+
}
|
|
488
|
+
return { capture, flush, close, stats }
|
|
489
|
+
}
|