@holz/core 0.11.1 → 0.11.2-rc.0.sha-c591264
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 +5 -5
- package/dist/{functions-BVJ92FOx.js → functions-Djc7GqxS.js} +16 -19
- package/dist/functions.d.ts +1 -1
- package/dist/functions.js +1 -1
- package/dist/holz-core.d.ts +1 -1
- package/dist/holz-core.js +1 -1
- package/dist/{index-BCJez7yY.d.ts → index-CbOZi-vP.d.ts} +4 -4
- package/package.json +2 -2
- package/src/functions/batcher.ts +13 -16
- package/src/functions/buffer.ts +4 -4
- package/src/functions/valve.ts +10 -12
package/README.md
CHANGED
|
@@ -196,7 +196,7 @@ select('file');
|
|
|
196
196
|
|
|
197
197
|
### `createBuffer`
|
|
198
198
|
|
|
199
|
-
Holds logs in a ring buffer until
|
|
199
|
+
Holds logs in a ring buffer until cleared. Once `capacity` is reached, each new log overwrites the oldest.
|
|
200
200
|
|
|
201
201
|
#### Use Cases
|
|
202
202
|
|
|
@@ -209,7 +209,7 @@ Holds logs in a ring buffer until drained. Once `capacity` is reached, each new
|
|
|
209
209
|
```typescript
|
|
210
210
|
import { createBuffer } from '@holz/core/functions';
|
|
211
211
|
|
|
212
|
-
const {
|
|
212
|
+
const { clear, processor } = createBuffer({
|
|
213
213
|
// Buffer up to 1k logs. Beyond that, drop the oldest.
|
|
214
214
|
// Defaults to `Infinity`.
|
|
215
215
|
capacity: 1_000,
|
|
@@ -218,7 +218,7 @@ const { drain, processor } = createBuffer({
|
|
|
218
218
|
const logger = createLogger(processor);
|
|
219
219
|
|
|
220
220
|
// Later: empty the buffer and return its logs in arrival order.
|
|
221
|
-
const logs =
|
|
221
|
+
const logs = clear();
|
|
222
222
|
```
|
|
223
223
|
|
|
224
224
|
### `createBatcher`
|
|
@@ -236,7 +236,7 @@ Groups logs into fixed-size batches and hands each full batch to a sink in one c
|
|
|
236
236
|
```typescript
|
|
237
237
|
import { createBatcher } from '@holz/core/functions';
|
|
238
238
|
|
|
239
|
-
const {
|
|
239
|
+
const { flush, clear, processor } = createBatcher({
|
|
240
240
|
// Flush every 50 logs.
|
|
241
241
|
size: 50,
|
|
242
242
|
|
|
@@ -247,5 +247,5 @@ const { drain, clear, processor } = createBatcher({
|
|
|
247
247
|
const logger = createLogger(processor);
|
|
248
248
|
|
|
249
249
|
// Later: flush the partial batch before exiting.
|
|
250
|
-
|
|
250
|
+
flush();
|
|
251
251
|
```
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
// src/functions/batcher.ts
|
|
2
2
|
var e = ({ size: e, sink: t }) => {
|
|
3
3
|
let n = [], r = () => {
|
|
4
|
-
if (n.length === 0) return;
|
|
5
4
|
let e = n;
|
|
6
|
-
n = [],
|
|
5
|
+
return n = [], e;
|
|
6
|
+
}, i = () => {
|
|
7
|
+
let e = r();
|
|
8
|
+
e.length > 0 && t(e);
|
|
7
9
|
};
|
|
8
10
|
return {
|
|
9
11
|
processor: (t) => {
|
|
10
|
-
n.push(t), n.length >= e &&
|
|
12
|
+
n.push(t), n.length >= e && i();
|
|
11
13
|
},
|
|
12
|
-
|
|
13
|
-
clear:
|
|
14
|
-
let e = n;
|
|
15
|
-
return n = [], e;
|
|
16
|
-
}
|
|
14
|
+
flush: i,
|
|
15
|
+
clear: r
|
|
17
16
|
};
|
|
18
17
|
}, t = ({ capacity: e = Infinity } = {}) => {
|
|
19
18
|
let t = [], n = 0;
|
|
@@ -21,7 +20,7 @@ var e = ({ size: e, sink: t }) => {
|
|
|
21
20
|
processor: (r) => {
|
|
22
21
|
t.length < e ? t.push(r) : e > 0 && (t[n] = r, n = (n + 1) % e);
|
|
23
22
|
},
|
|
24
|
-
|
|
23
|
+
clear: () => {
|
|
25
24
|
let e = t.slice(n).concat(t.slice(0, n));
|
|
26
25
|
return t.length = 0, n = 0, e;
|
|
27
26
|
}
|
|
@@ -36,21 +35,19 @@ var e = ({ size: e, sink: t }) => {
|
|
|
36
35
|
n = t[e];
|
|
37
36
|
}
|
|
38
37
|
};
|
|
39
|
-
}, a = ({
|
|
40
|
-
let o = t({ capacity:
|
|
41
|
-
closed:
|
|
38
|
+
}, a = ({ open: e, capacity: n, processor: r, drain: a }) => {
|
|
39
|
+
let { clear: o, processor: s } = t({ capacity: n }), { select: c, processor: l } = i(e ? "open" : "closed", {
|
|
40
|
+
closed: s,
|
|
42
41
|
open: r
|
|
43
42
|
});
|
|
44
43
|
return {
|
|
45
|
-
processor:
|
|
44
|
+
processor: l,
|
|
46
45
|
open: () => {
|
|
47
|
-
|
|
48
|
-
let e = o
|
|
49
|
-
e.length > 0 &&
|
|
46
|
+
c("open");
|
|
47
|
+
let e = o();
|
|
48
|
+
e.length > 0 && a(e);
|
|
50
49
|
},
|
|
51
|
-
close: () =>
|
|
52
|
-
s.select("closed");
|
|
53
|
-
}
|
|
50
|
+
close: () => c("closed")
|
|
54
51
|
};
|
|
55
52
|
};
|
|
56
53
|
export { t as a, n as i, i as n, e as o, r, a as t };
|
package/dist/functions.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as createRouter, c as LogBuffer, d as LogBatcher, f as LogBatcherOptions, i as LogRouter, l as LogBufferOptions, n as LogValveOptions, o as createGroup, p as createBatcher, r as createValve, s as createFilter, t as LogValve, u as createBuffer } from "./index-
|
|
1
|
+
import { a as createRouter, c as LogBuffer, d as LogBatcher, f as LogBatcherOptions, i as LogRouter, l as LogBufferOptions, n as LogValveOptions, o as createGroup, p as createBatcher, r as createValve, s as createFilter, t as LogValve, u as createBuffer } from "./index-CbOZi-vP.js";
|
|
2
2
|
export { type LogBatcher, type LogBatcherOptions, type LogBuffer, type LogBufferOptions, type LogRouter, type LogValve, type LogValveOptions, createBatcher, createBuffer, createFilter, createGroup, createRouter, createValve };
|
package/dist/functions.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { o as createBatcher, a as createBuffer, i as createFilter, r as createGroup, n as createRouter, t as createValve } from "./functions-
|
|
1
|
+
export { o as createBatcher, a as createBuffer, i as createFilter, r as createGroup, n as createRouter, t as createValve } from "./functions-Djc7GqxS.js";
|
package/dist/holz-core.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _ as LogLevel, b as level, g as LogContext, h as Log, m as CustomContext, o as createGroup, s as createFilter, v as LogProcessor, y as Logger } from "./index-
|
|
1
|
+
import { _ as LogLevel, b as level, g as LogContext, h as Log, m as CustomContext, o as createGroup, s as createFilter, v as LogProcessor, y as Logger } from "./index-CbOZi-vP.js";
|
|
2
2
|
/**
|
|
3
3
|
* Create a new logger. The processor is up to you. There are plugins for
|
|
4
4
|
* filtering, formatting, combining multiple processors together, and more.
|
package/dist/holz-core.js
CHANGED
|
@@ -123,7 +123,7 @@ interface LogBatcher {
|
|
|
123
123
|
/** The processor to install upstream. Flushes once a batch fills. */
|
|
124
124
|
processor: LogProcessor;
|
|
125
125
|
/** Flush the pending batch to the sink early. No-op when empty. */
|
|
126
|
-
|
|
126
|
+
flush: () => void;
|
|
127
127
|
/** Discard the pending batch without flushing it and return its logs. */
|
|
128
128
|
clear: () => Log[];
|
|
129
129
|
}
|
|
@@ -141,13 +141,13 @@ interface LogBatcherOptions {
|
|
|
141
141
|
declare const createBatcher: ({ size, sink }: LogBatcherOptions) => LogBatcher;
|
|
142
142
|
/**
|
|
143
143
|
* A bounded ring buffer over a log stream. Install {@link LogBuffer.processor}
|
|
144
|
-
* in a pipeline and collect what it held with {@link LogBuffer.
|
|
144
|
+
* in a pipeline and collect what it held with {@link LogBuffer.clear}.
|
|
145
145
|
*/
|
|
146
146
|
interface LogBuffer {
|
|
147
147
|
/** The processor to install upstream. Holds every log it receives. */
|
|
148
148
|
processor: LogProcessor;
|
|
149
149
|
/** Empty the buffer, returning the held logs in arrival order. */
|
|
150
|
-
|
|
150
|
+
clear: () => Log[];
|
|
151
151
|
}
|
|
152
152
|
/** Options for {@link createBuffer}. */
|
|
153
153
|
interface LogBufferOptions {
|
|
@@ -235,5 +235,5 @@ interface LogValveOptions {
|
|
|
235
235
|
* Create a {@link LogValve}. Reach for it to hold logs while a downstream
|
|
236
236
|
* isn't ready, then release them all at once when it is.
|
|
237
237
|
*/
|
|
238
|
-
declare const createValve: ({
|
|
238
|
+
declare const createValve: ({ open, capacity, processor, drain }: LogValveOptions) => LogValve;
|
|
239
239
|
export { LogLevel as _, createRouter as a, level as b, LogBuffer as c, LogBatcher as d, LogBatcherOptions as f, LogContext as g, Log as h, LogRouter as i, LogBufferOptions as l, CustomContext as m, LogValveOptions as n, createGroup as o, createBatcher as p, createValve as r, createFilter as s, LogValve as t, createBuffer as u, LogProcessor as v, Logger as y };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/core",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2-rc.0.sha-c591264",
|
|
4
4
|
"description": "A structured and composable logger",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
"vite": "^8.0.0",
|
|
51
51
|
"vitest": "^5.0.0"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "c5912649e7dc93b54d3804a3c23ba0ba4649949f"
|
|
54
54
|
}
|
package/src/functions/batcher.ts
CHANGED
|
@@ -10,7 +10,7 @@ export interface LogBatcher {
|
|
|
10
10
|
processor: LogProcessor;
|
|
11
11
|
|
|
12
12
|
/** Flush the pending batch to the sink early. No-op when empty. */
|
|
13
|
-
|
|
13
|
+
flush: () => void;
|
|
14
14
|
|
|
15
15
|
/** Discard the pending batch without flushing it and return its logs. */
|
|
16
16
|
clear: () => Log[];
|
|
@@ -35,30 +35,27 @@ export const createBatcher = ({
|
|
|
35
35
|
}: LogBatcherOptions): LogBatcher => {
|
|
36
36
|
let pending: Log[] = [];
|
|
37
37
|
|
|
38
|
-
const
|
|
39
|
-
|
|
38
|
+
const clear = () => {
|
|
39
|
+
const discarded = pending;
|
|
40
|
+
pending = [];
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
return discarded;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const flush = () => {
|
|
46
|
+
// Clear before calling the sink. A throwing sink can't leave the batch to
|
|
42
47
|
// grow unbounded, and a log emitted from inside the sink starts the next
|
|
43
48
|
// batch instead of mutating the one being flushed.
|
|
44
|
-
const batch =
|
|
45
|
-
pending = [];
|
|
49
|
+
const batch = clear();
|
|
46
50
|
|
|
47
|
-
sink(batch);
|
|
51
|
+
if (batch.length > 0) sink(batch);
|
|
48
52
|
};
|
|
49
53
|
|
|
50
54
|
const processor: LogProcessor = (log) => {
|
|
51
55
|
pending.push(log);
|
|
52
56
|
|
|
53
|
-
if (pending.length >= size)
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const clear = () => {
|
|
57
|
-
const discarded = pending;
|
|
58
|
-
pending = [];
|
|
59
|
-
|
|
60
|
-
return discarded;
|
|
57
|
+
if (pending.length >= size) flush();
|
|
61
58
|
};
|
|
62
59
|
|
|
63
|
-
return { processor,
|
|
60
|
+
return { processor, flush, clear };
|
|
64
61
|
};
|
package/src/functions/buffer.ts
CHANGED
|
@@ -2,14 +2,14 @@ import type { Log, LogProcessor } from '../types';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A bounded ring buffer over a log stream. Install {@link LogBuffer.processor}
|
|
5
|
-
* in a pipeline and collect what it held with {@link LogBuffer.
|
|
5
|
+
* in a pipeline and collect what it held with {@link LogBuffer.clear}.
|
|
6
6
|
*/
|
|
7
7
|
export interface LogBuffer {
|
|
8
8
|
/** The processor to install upstream. Holds every log it receives. */
|
|
9
9
|
processor: LogProcessor;
|
|
10
10
|
|
|
11
11
|
/** Empty the buffer, returning the held logs in arrival order. */
|
|
12
|
-
|
|
12
|
+
clear: () => Log[];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/** Options for {@link createBuffer}. */
|
|
@@ -45,7 +45,7 @@ export const createBuffer = ({
|
|
|
45
45
|
}
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
const
|
|
48
|
+
const clear = () => {
|
|
49
49
|
// The ring holds two contiguous runs, `oldest` to the end then the start
|
|
50
50
|
// up to `oldest`, so joining those slices restores arrival order.
|
|
51
51
|
const logs = buffer.slice(oldest).concat(buffer.slice(0, oldest));
|
|
@@ -56,5 +56,5 @@ export const createBuffer = ({
|
|
|
56
56
|
return logs;
|
|
57
57
|
};
|
|
58
58
|
|
|
59
|
-
return { processor,
|
|
59
|
+
return { processor, clear };
|
|
60
60
|
};
|
package/src/functions/valve.ts
CHANGED
|
@@ -50,32 +50,30 @@ export interface LogValveOptions {
|
|
|
50
50
|
* isn't ready, then release them all at once when it is.
|
|
51
51
|
*/
|
|
52
52
|
export const createValve = ({
|
|
53
|
+
open,
|
|
53
54
|
capacity,
|
|
54
|
-
drain,
|
|
55
55
|
processor,
|
|
56
|
-
|
|
56
|
+
drain,
|
|
57
57
|
}: LogValveOptions): LogValve => {
|
|
58
|
-
const buffer = createBuffer({ capacity });
|
|
59
|
-
const router = createRouter(
|
|
60
|
-
closed: buffer
|
|
58
|
+
const { clear, processor: buffer } = createBuffer({ capacity });
|
|
59
|
+
const { select, processor: router } = createRouter(open ? 'open' : 'closed', {
|
|
60
|
+
closed: buffer,
|
|
61
61
|
open: processor,
|
|
62
62
|
});
|
|
63
63
|
|
|
64
64
|
return {
|
|
65
|
-
processor: router
|
|
65
|
+
processor: router,
|
|
66
66
|
|
|
67
67
|
open: () => {
|
|
68
68
|
// Route before draining so a log emitted from inside `drain` streams
|
|
69
|
-
// straight through. `
|
|
69
|
+
// straight through. `clear()` resets before returning, so it can't
|
|
70
70
|
// land in the batch already released.
|
|
71
|
-
|
|
71
|
+
select('open');
|
|
72
72
|
|
|
73
|
-
const logs =
|
|
73
|
+
const logs = clear();
|
|
74
74
|
if (logs.length > 0) drain(logs);
|
|
75
75
|
},
|
|
76
76
|
|
|
77
|
-
close: () =>
|
|
78
|
-
router.select('closed');
|
|
79
|
-
},
|
|
77
|
+
close: () => select('closed'),
|
|
80
78
|
};
|
|
81
79
|
};
|