@ac-kit/test-util 0.3.1 → 0.5.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/CHANGELOG.md +109 -0
- package/README.md +159 -1
- package/dist/exact-dot.d.ts +87 -0
- package/dist/exact-dot.d.ts.map +1 -0
- package/dist/exact-dot.js +237 -0
- package/dist/exact-dot.js.map +1 -0
- package/dist/expect-alias-safe.d.ts +31 -0
- package/dist/expect-alias-safe.d.ts.map +1 -0
- package/dist/expect-alias-safe.js +80 -0
- package/dist/expect-alias-safe.js.map +1 -0
- package/dist/fake-wall-clock.d.ts.map +1 -1
- package/dist/fake-wall-clock.js +2 -2
- package/dist/fake-wall-clock.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/out-first-case.d.ts +116 -0
- package/dist/out-first-case.d.ts.map +1 -0
- package/dist/out-first-case.js +113 -0
- package/dist/out-first-case.js.map +1 -0
- package/dist/v8/_collect.d.ts +25 -0
- package/dist/v8/_collect.d.ts.map +1 -0
- package/dist/v8/_collect.js +94 -0
- package/dist/v8/_collect.js.map +1 -0
- package/dist/v8/allocated-bytes.d.ts +97 -0
- package/dist/v8/allocated-bytes.d.ts.map +1 -0
- package/dist/v8/allocated-bytes.js +148 -0
- package/dist/v8/allocated-bytes.js.map +1 -0
- package/dist/v8/allocation-sites.d.ts +92 -0
- package/dist/v8/allocation-sites.d.ts.map +1 -0
- package/dist/v8/allocation-sites.js +131 -0
- package/dist/v8/allocation-sites.js.map +1 -0
- package/dist/v8/elements-kind.d.ts +50 -0
- package/dist/v8/elements-kind.d.ts.map +1 -0
- package/dist/v8/elements-kind.js +81 -0
- package/dist/v8/elements-kind.js.map +1 -0
- package/dist/v8/index.d.ts +4 -0
- package/dist/v8/index.d.ts.map +1 -0
- package/dist/v8/index.js +4 -0
- package/dist/v8/index.js.map +1 -0
- package/package.json +17 -6
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import v8 from "node:v8";
|
|
3
|
+
import { assertInteger } from "@ac-kit/core";
|
|
4
|
+
import { collect_, objectHeapUsed_, windowCollections_ } from "./_collect.js";
|
|
5
|
+
/** Empty windows whose median is the measurement's own cost. */
|
|
6
|
+
const CALIBRATION_WINDOWS_ = 9;
|
|
7
|
+
/**
|
|
8
|
+
* What a window of a body that allocates nothing reads above the cost, at most:
|
|
9
|
+
* the engine updating feedback and inline caches around the measuring loop, a
|
|
10
|
+
* few to a few hundred bytes a window whatever the number of calls (measured on
|
|
11
|
+
* Node 24 and 26). It is removed from every window with the cost.
|
|
12
|
+
*/
|
|
13
|
+
const WINDOW_NOISE_BYTES_ = 256;
|
|
14
|
+
/**
|
|
15
|
+
* Bytes allocated by `calls` calls of `body` (none when `calls` is 0), the
|
|
16
|
+
* measurement's own bookkeeping included.
|
|
17
|
+
*/
|
|
18
|
+
function measureWindow_(profiler, body, calls) {
|
|
19
|
+
collect_();
|
|
20
|
+
profiler.start();
|
|
21
|
+
const heapStart = objectHeapUsed_();
|
|
22
|
+
const externalStart = v8.getHeapStatistics().external_memory;
|
|
23
|
+
for (let call = 0; call < calls; call++) {
|
|
24
|
+
body();
|
|
25
|
+
}
|
|
26
|
+
// Read before `stop`, which builds the report outside the window.
|
|
27
|
+
const externalEnd = v8.getHeapStatistics().external_memory;
|
|
28
|
+
const heapEnd = objectHeapUsed_();
|
|
29
|
+
const { heapFreed, externalFreed, sweptAfter } = windowCollections_(profiler.stop());
|
|
30
|
+
// What a collection freed was counted as used at the start or allocated in
|
|
31
|
+
// the window; either way growth plus the freed bytes is the allocation.
|
|
32
|
+
return {
|
|
33
|
+
heap: heapEnd - heapStart + heapFreed,
|
|
34
|
+
external: externalEnd - externalStart + externalFreed,
|
|
35
|
+
sweptAfter,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function measure_(name, body, options) {
|
|
39
|
+
const { warmUpCalls = 10000, calls = 10000, windows = 5 } = options;
|
|
40
|
+
assertInteger(warmUpCalls, `${name}: warmUpCalls`, 0);
|
|
41
|
+
assertInteger(calls, `${name}: calls`, 1);
|
|
42
|
+
assertInteger(windows, `${name}: windows`, 1);
|
|
43
|
+
const profiler = new v8.GCProfiler();
|
|
44
|
+
// Through the measuring loop itself, so that loop is optimised too. The
|
|
45
|
+
// window starts with full collections, so code that embedded objects which
|
|
46
|
+
// died before the call is deoptimized and re-optimised during the warm-up,
|
|
47
|
+
// not inside a measured window.
|
|
48
|
+
measureWindow_(profiler, body, warmUpCalls);
|
|
49
|
+
const heapCosts = [];
|
|
50
|
+
const externalCosts = [];
|
|
51
|
+
for (let window = 0; window < CALIBRATION_WINDOWS_; window++) {
|
|
52
|
+
const empty = measureWindow_(profiler, body, 0);
|
|
53
|
+
heapCosts.push(empty.heap);
|
|
54
|
+
externalCosts.push(empty.external);
|
|
55
|
+
}
|
|
56
|
+
const heapCost = median_(heapCosts);
|
|
57
|
+
const externalCost = median_(externalCosts);
|
|
58
|
+
let least;
|
|
59
|
+
for (let window = 0; window < windows; window++) {
|
|
60
|
+
let windowCalls = calls;
|
|
61
|
+
let measured = measureWindow_(profiler, body, windowCalls);
|
|
62
|
+
// A window a sweeping collection ran in may read low: it is measured
|
|
63
|
+
// again with half the calls until none runs in it, or one call does.
|
|
64
|
+
while (measured.sweptAfter && windowCalls > 1) {
|
|
65
|
+
windowCalls = Math.ceil(windowCalls / 2);
|
|
66
|
+
measured = measureWindow_(profiler, body, windowCalls);
|
|
67
|
+
}
|
|
68
|
+
const heapBytes = Math.max(0, measured.heap - heapCost - WINDOW_NOISE_BYTES_) / windowCalls;
|
|
69
|
+
const externalBytes = Math.max(0, measured.external - externalCost) / windowCalls;
|
|
70
|
+
const bytes = heapBytes + externalBytes;
|
|
71
|
+
if (least === undefined || bytes < least.bytes) {
|
|
72
|
+
least = { bytes, heapBytes, externalBytes };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return least;
|
|
76
|
+
}
|
|
77
|
+
function median_(values) {
|
|
78
|
+
values.sort((first, second) => first - second);
|
|
79
|
+
const middle = values.length >> 1;
|
|
80
|
+
return values.length % 2 === 1
|
|
81
|
+
? values[middle]
|
|
82
|
+
: (values[middle - 1] + values[middle]) / 2;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Mean bytes one call of `body` allocates once warmed up, on the JavaScript
|
|
86
|
+
* heap and off it, for asserting that a hot path allocates nothing and, when it
|
|
87
|
+
* does, telling objects from buffers.
|
|
88
|
+
*
|
|
89
|
+
* Each window starts from two full collections and runs `calls` calls of `body`
|
|
90
|
+
* under `v8.GCProfiler`:
|
|
91
|
+
*
|
|
92
|
+
* - `heapBytes` is the growth of the spaces objects live in (new, old and
|
|
93
|
+
* large-object spaces) plus what every collection inside the window freed of
|
|
94
|
+
* them, so garbage is counted to the byte. The spaces of compiled code,
|
|
95
|
+
* bytecode and feedback metadata are left out: what they gain or lose in a
|
|
96
|
+
* window is the engine tiering code up, or sweeping code it collected, not
|
|
97
|
+
* what `body` allocates. A mark-compact collection goes on sweeping after it
|
|
98
|
+
* ends, freeing garbage its statistics do not show, so a window one ran in is
|
|
99
|
+
* measured again with half the calls, down to one call; a body that runs one
|
|
100
|
+
* in a single call reads low by what it swept. The measurement's own cost,
|
|
101
|
+
* the median of empty windows, is removed, and so are 256 bytes a window,
|
|
102
|
+
* what the engine's feedback and inline-cache updates around the loop add to
|
|
103
|
+
* it whatever the number of calls: a body that allocates nothing reads 0, and
|
|
104
|
+
* one that allocates reads at most `256 / calls` bytes low.
|
|
105
|
+
* - `externalBytes` is the growth of V8's external memory plus what every
|
|
106
|
+
* collection inside the window freed of it: each `ArrayBuffer` backing store
|
|
107
|
+
* counts once, at its byte length, when it is allocated, collected in the
|
|
108
|
+
* window or not. V8 is set to free dead backing stores inside the collection
|
|
109
|
+
* that finds them (`--no-concurrent-array-buffer-sweeping`) so that a
|
|
110
|
+
* collection's statistics show what it freed. A body that allocates no buffer
|
|
111
|
+
* reads 0; memory the embedder releases outside a collection (a native
|
|
112
|
+
* resource closed in `body`) offsets what `body` allocates, and the figure is
|
|
113
|
+
* then a lower bound.
|
|
114
|
+
*
|
|
115
|
+
* A backing store of 64 bytes or fewer lives on the heap, in `heapBytes`. The
|
|
116
|
+
* least of `windows` windows by `bytes` is kept, so the figure is the steady
|
|
117
|
+
* state rather than the engine still compiling. Never negative.
|
|
118
|
+
*
|
|
119
|
+
* What it shows is the engine's behaviour on this run: a body the engine has
|
|
120
|
+
* not optimised, or whose callers' inlining budget differs, may allocate where
|
|
121
|
+
* it otherwise would not (a double crossing a call that is not inlined is
|
|
122
|
+
* boxed). Warm it up as the code under test is warmed up in use.
|
|
123
|
+
*
|
|
124
|
+
* Node only (`node:v8`). Exposes `gc` to new contexts when the process was
|
|
125
|
+
* started without `--expose-gc`, and leaves array-buffer sweeping synchronous
|
|
126
|
+
* for the rest of the process. Runs `warmUpCalls + windows · calls` calls of
|
|
127
|
+
* `body`, more when sweeping collections run in a window.
|
|
128
|
+
*
|
|
129
|
+
* @throws {RangeError} `warmUpCalls` not an integer ≥ 0, `calls` or `windows`
|
|
130
|
+
* not an integer ≥ 1.
|
|
131
|
+
*/
|
|
132
|
+
export function allocatedMemoryPerCall(body, options = {}) {
|
|
133
|
+
return measure_("allocatedMemoryPerCall", body, options);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Mean bytes one call of `body` allocates once warmed up, on the JavaScript
|
|
137
|
+
* heap and off it (`ArrayBuffer` backing stores included), for asserting that a
|
|
138
|
+
* hot path allocates nothing: the `bytes` of {@link allocatedMemoryPerCall},
|
|
139
|
+
* which tells the two apart and documents what is counted. A body that
|
|
140
|
+
* allocates nothing reads 0.
|
|
141
|
+
*
|
|
142
|
+
* @throws {RangeError} `warmUpCalls` not an integer ≥ 0, `calls` or `windows`
|
|
143
|
+
* not an integer ≥ 1.
|
|
144
|
+
*/
|
|
145
|
+
export function allocatedBytesPerCall(body, options = {}) {
|
|
146
|
+
return measure_("allocatedBytesPerCall", body, options).bytes;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=allocated-bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allocated-bytes.js","sourceRoot":"","sources":["../../src/v8/allocated-bytes.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,OAAO,EAAE,aAAa,EAAuB,MAAM,cAAc,CAAC;AAElE,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAuC9E,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAE/B;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAahC;;;GAGG;AACH,SAAS,cAAc,CACtB,QAAuB,EACvB,IAAoB,EACpB,KAAa;IAEb,QAAQ,EAAE,CAAC;IACX,QAAQ,CAAC,KAAK,EAAE,CAAC;IACjB,MAAM,SAAS,GAAG,eAAe,EAAE,CAAC;IACpC,MAAM,aAAa,GAAG,EAAE,CAAC,iBAAiB,EAAE,CAAC,eAAe,CAAC;IAE7D,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC;QACzC,IAAI,EAAE,CAAC;IACR,CAAC;IAED,kEAAkE;IAClE,MAAM,WAAW,GAAG,EAAE,CAAC,iBAAiB,EAAE,CAAC,eAAe,CAAC;IAC3D,MAAM,OAAO,GAAG,eAAe,EAAE,CAAC;IAClC,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,GAAG,kBAAkB,CAClE,QAAQ,CAAC,IAAI,EAAE,CACf,CAAC;IAEF,2EAA2E;IAC3E,wEAAwE;IACxE,OAAO;QACN,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS;QACrC,QAAQ,EAAE,WA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AAG,MAAM,CAAC,MAAM,CAAE,CAAC,GAAG,CAAC,CAAC;AAChD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,UAAU,sBAAsB,CACrC,IAAoB,EACpB,OAAO,GAA0B,EAAE;IAEnC,OAAO,QAAQ,CAAC,wBAAwB,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC1D,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,qBAAqB,CACpC,IAAoB,EACpB,OAAO,GAA0B,EAAE;IAEnC,OAAO,QAAQ,CAAC,uBAAuB,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,KAAK,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type Callable, type CallableNoArgs } from "@ac-kit/core";
|
|
2
|
+
export type AllocationSitesOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Calls made before sampling, so the engine has optimised `body`. Default 10
|
|
5
|
+
* 000; an integer ≥ 0.
|
|
6
|
+
*/
|
|
7
|
+
readonly warmUpCalls?: number;
|
|
8
|
+
/** Calls sampled. Default 10 000; an integer ≥ 1. */
|
|
9
|
+
readonly calls?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Mean bytes between two samples (V8 draws the gaps from a Poisson
|
|
12
|
+
* distribution). Default 16; an integer ≥ 1.
|
|
13
|
+
*/
|
|
14
|
+
readonly samplingInterval?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Whether allocations made by a function of the script at `url` count.
|
|
17
|
+
* Default every function's.
|
|
18
|
+
*/
|
|
19
|
+
readonly include?: Callable<[url: string], boolean>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Where `body`'s calls allocated: one function, as V8 names it. What a builtin
|
|
23
|
+
* (`Array`, `slice`, a typed-array constructor) allocates is the script
|
|
24
|
+
* function's that called it.
|
|
25
|
+
*/
|
|
26
|
+
export type AllocationSite = {
|
|
27
|
+
readonly functionName: string;
|
|
28
|
+
/**
|
|
29
|
+
* The URL of the function's script; `""` for what no script function
|
|
30
|
+
* allocated.
|
|
31
|
+
*/
|
|
32
|
+
readonly url: string;
|
|
33
|
+
/** 1-based. */
|
|
34
|
+
readonly line: number;
|
|
35
|
+
/** Mean bytes it allocated per call of `body`. */
|
|
36
|
+
readonly bytesPerCall: number;
|
|
37
|
+
};
|
|
38
|
+
export type AllocationSites = {
|
|
39
|
+
/** Mean bytes per call of `body` over the sites that count. */
|
|
40
|
+
readonly bytesPerCall: number;
|
|
41
|
+
/** The sites that count, largest first. */
|
|
42
|
+
readonly sites: readonly AllocationSite[];
|
|
43
|
+
/**
|
|
44
|
+
* Mean off-heap bytes per call of `body` V8 accounts as external, which the
|
|
45
|
+
* sampling profiler does not see: `ArrayBuffer` and `SharedArrayBuffer`
|
|
46
|
+
* backing stores (those of typed arrays and `DataView`s over more than 64
|
|
47
|
+
* bytes, `Buffer`s) and memory the embedder reports to V8, counted as
|
|
48
|
+
* {@link allocatedMemoryPerCall} counts its `externalBytes`. It belongs to no
|
|
49
|
+
* site, `include` does not restrict it, and `bytesPerCall` leaves it out; the
|
|
50
|
+
* backing store's buffer and typed-array objects are on the heap, in a site.
|
|
51
|
+
*/
|
|
52
|
+
readonly externalBytesPerCall: number;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Mean bytes one call of `body` allocates once warmed up, by the function that
|
|
56
|
+
* allocated them, from V8's sampling heap profiler: what to look at when
|
|
57
|
+
* {@link allocatedBytesPerCall} reads more than nothing, and a measurement that
|
|
58
|
+
* `include` restricts to the code under test, leaving out what the engine, the
|
|
59
|
+
* runtime or the test runner allocate meanwhile (compiled code, feedback,
|
|
60
|
+
* timers).
|
|
61
|
+
*
|
|
62
|
+
* Every object, array, closure, string or boxed number a JavaScript function
|
|
63
|
+
* allocates is sampled, collected or not; what the engine allocates outside any
|
|
64
|
+
* JavaScript function is not. A site is the function V8 sees on the stack: a
|
|
65
|
+
* function the optimiser inlined allocates in its caller's name, so `include`
|
|
66
|
+
* can leave out what an excluded caller inlined — leave `include` out to
|
|
67
|
+
* measure everything `body` allocates. The figure is statistical: about one
|
|
68
|
+
* sample every `samplingInterval` bytes, so allocations of a few bytes per call
|
|
69
|
+
* need many calls. The profiler's own cost, an empty window sampled the same
|
|
70
|
+
* way, is removed site by site, so a body that allocates nothing reads about 0.
|
|
71
|
+
* Backing stores are off the heap and no sample sees them:
|
|
72
|
+
* `externalBytesPerCall` counts them, every window starting from two full
|
|
73
|
+
* collections, as {@link allocatedMemoryPerCall} does; a gate checks both. A
|
|
74
|
+
* full collection also runs before the warm-up, so code that embedded objects
|
|
75
|
+
* which died before the call is deoptimized and re-optimised during the
|
|
76
|
+
* warm-up, not inside the window.
|
|
77
|
+
*
|
|
78
|
+
* Node only (`node:inspector`, `node:v8`). Exposes `gc` to new contexts when
|
|
79
|
+
* the process was started without `--expose-gc`, and leaves array-buffer
|
|
80
|
+
* sweeping synchronous for the rest of the process. Runs `warmUpCalls + calls`
|
|
81
|
+
* calls of `body`.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const { bytesPerCall, sites } = await allocationSitesPerCall(render, {
|
|
85
|
+
* include: (url) => url.includes("/packages/"),
|
|
86
|
+
* });
|
|
87
|
+
*
|
|
88
|
+
* @throws {RangeError} `warmUpCalls` not an integer ≥ 0, `calls` or
|
|
89
|
+
* `samplingInterval` not an integer ≥ 1.
|
|
90
|
+
*/
|
|
91
|
+
export declare function allocationSitesPerCall(body: CallableNoArgs, options?: AllocationSitesOptions): Promise<AllocationSites>;
|
|
92
|
+
//# sourceMappingURL=allocation-sites.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allocation-sites.d.ts","sourceRoot":"","sources":["../../src/v8/allocation-sites.ts"],"names":[],"mappings":"AAIA,OAAO,EAEN,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,MAAM,cAAc,CAAC;AAItB,MAAM,MAAM,sBAAsB,GAAG;IACpC;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,qDAAqD;IACrD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC;CACpD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG;IAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,eAAe;IACf,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,kDAAkD;IAClD,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC7B,+DAA+D;IAC/D,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,2CAA2C;IAC3C,QAAQ,CAAC,KAAK,EAAE,SAAS,cAAc,EAAE,CAAC;IAC1C;;;;;;;;OAQG;IACH,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;CACtC,CAAC;AAkFF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAsB,sBAAsB,CAC3C,IAAI,EAAE,cAAc,EACpB,OAAO,GAAE,sBAA2B,GAClC,OAAO,CAAC,eAAe,CAAC,CA+D1B"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Session } from "node:inspector/promises";
|
|
3
|
+
import v8 from "node:v8";
|
|
4
|
+
import { assertInteger, } from "@ac-kit/core";
|
|
5
|
+
import { collect_, windowCollections_ } from "./_collect.js";
|
|
6
|
+
/**
|
|
7
|
+
* Bytes sampled per site key over `calls` calls of `body`, and the external
|
|
8
|
+
* bytes they allocated.
|
|
9
|
+
*/
|
|
10
|
+
async function sampleWindow_(session, profiler, body, calls, samplingInterval, include) {
|
|
11
|
+
const parameters = {
|
|
12
|
+
samplingInterval,
|
|
13
|
+
// garbage counts as much as what survives
|
|
14
|
+
includeObjectsCollectedByMinorGC: true,
|
|
15
|
+
includeObjectsCollectedByMajorGC: true,
|
|
16
|
+
};
|
|
17
|
+
collect_();
|
|
18
|
+
await session.post("HeapProfiler.startSampling", parameters);
|
|
19
|
+
profiler.start();
|
|
20
|
+
const start = process.memoryUsage().external;
|
|
21
|
+
for (let call = 0; call < calls; call++) {
|
|
22
|
+
body();
|
|
23
|
+
}
|
|
24
|
+
const end = process.memoryUsage().external;
|
|
25
|
+
const { externalFreed } = windowCollections_(profiler.stop());
|
|
26
|
+
const { profile } = await session.post("HeapProfiler.stopSampling");
|
|
27
|
+
const sites = new Map();
|
|
28
|
+
const visit = (node, caller) => {
|
|
29
|
+
// a builtin (`Array`, `slice`) allocates for the script function that
|
|
30
|
+
// called it: the sample goes to the innermost frame with a script
|
|
31
|
+
const frame = node.callFrame.url === "" ? caller : node.callFrame;
|
|
32
|
+
const { functionName, url, lineNumber } = frame;
|
|
33
|
+
if (node.selfSize > 0 && (include === undefined || include(url))) {
|
|
34
|
+
const key = `${url}:${lineNumber}:${functionName}`;
|
|
35
|
+
const entry = sites.get(key);
|
|
36
|
+
if (entry === undefined) {
|
|
37
|
+
sites.set(key, {
|
|
38
|
+
site: { functionName, url, line: lineNumber + 1 },
|
|
39
|
+
bytes: node.selfSize,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
entry.bytes += node.selfSize;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (const child of node.children) {
|
|
47
|
+
visit(child, frame);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const head = profile.head; // the sampling profile's head node has the SampledNode_ shape, which the V8 typings omit
|
|
51
|
+
visit(head, head.callFrame);
|
|
52
|
+
return { sites, external: end - start + externalFreed };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Mean bytes one call of `body` allocates once warmed up, by the function that
|
|
56
|
+
* allocated them, from V8's sampling heap profiler: what to look at when
|
|
57
|
+
* {@link allocatedBytesPerCall} reads more than nothing, and a measurement that
|
|
58
|
+
* `include` restricts to the code under test, leaving out what the engine, the
|
|
59
|
+
* runtime or the test runner allocate meanwhile (compiled code, feedback,
|
|
60
|
+
* timers).
|
|
61
|
+
*
|
|
62
|
+
* Every object, array, closure, string or boxed number a JavaScript function
|
|
63
|
+
* allocates is sampled, collected or not; what the engine allocates outside any
|
|
64
|
+
* JavaScript function is not. A site is the function V8 sees on the stack: a
|
|
65
|
+
* function the optimiser inlined allocates in its caller's name, so `include`
|
|
66
|
+
* can leave out what an excluded caller inlined — leave `include` out to
|
|
67
|
+
* measure everything `body` allocates. The figure is statistical: about one
|
|
68
|
+
* sample every `samplingInterval` bytes, so allocations of a few bytes per call
|
|
69
|
+
* need many calls. The profiler's own cost, an empty window sampled the same
|
|
70
|
+
* way, is removed site by site, so a body that allocates nothing reads about 0.
|
|
71
|
+
* Backing stores are off the heap and no sample sees them:
|
|
72
|
+
* `externalBytesPerCall` counts them, every window starting from two full
|
|
73
|
+
* collections, as {@link allocatedMemoryPerCall} does; a gate checks both. A
|
|
74
|
+
* full collection also runs before the warm-up, so code that embedded objects
|
|
75
|
+
* which died before the call is deoptimized and re-optimised during the
|
|
76
|
+
* warm-up, not inside the window.
|
|
77
|
+
*
|
|
78
|
+
* Node only (`node:inspector`, `node:v8`). Exposes `gc` to new contexts when
|
|
79
|
+
* the process was started without `--expose-gc`, and leaves array-buffer
|
|
80
|
+
* sweeping synchronous for the rest of the process. Runs `warmUpCalls + calls`
|
|
81
|
+
* calls of `body`.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const { bytesPerCall, sites } = await allocationSitesPerCall(render, {
|
|
85
|
+
* include: (url) => url.includes("/packages/"),
|
|
86
|
+
* });
|
|
87
|
+
*
|
|
88
|
+
* @throws {RangeError} `warmUpCalls` not an integer ≥ 0, `calls` or
|
|
89
|
+
* `samplingInterval` not an integer ≥ 1.
|
|
90
|
+
*/
|
|
91
|
+
export async function allocationSitesPerCall(body, options = {}) {
|
|
92
|
+
const { warmUpCalls = 10000, calls = 10000, samplingInterval = 16, include, } = options;
|
|
93
|
+
assertInteger(warmUpCalls, "allocationSitesPerCall: warmUpCalls", 0);
|
|
94
|
+
assertInteger(calls, "allocationSitesPerCall: calls", 1);
|
|
95
|
+
assertInteger(samplingInterval, "allocationSitesPerCall: samplingInterval", 1);
|
|
96
|
+
// a full collection first, so code that embedded objects which died before
|
|
97
|
+
// the call is deoptimized and re-optimised during the warm-up, not inside the
|
|
98
|
+
// window
|
|
99
|
+
collect_();
|
|
100
|
+
for (let call = 0; call < warmUpCalls; call++) {
|
|
101
|
+
body();
|
|
102
|
+
}
|
|
103
|
+
const session = new Session();
|
|
104
|
+
session.connect();
|
|
105
|
+
try {
|
|
106
|
+
await session.post("HeapProfiler.enable");
|
|
107
|
+
const profiler = new v8.GCProfiler();
|
|
108
|
+
const cost = await sampleWindow_(session, profiler, body, 0, samplingInterval, include);
|
|
109
|
+
const measured = await sampleWindow_(session, profiler, body, calls, samplingInterval, include);
|
|
110
|
+
await session.post("HeapProfiler.disable");
|
|
111
|
+
const sites = [];
|
|
112
|
+
let total = 0;
|
|
113
|
+
for (const [key, { site, bytes }] of measured.sites) {
|
|
114
|
+
const own = Math.max(0, bytes - (cost.sites.get(key)?.bytes ?? 0));
|
|
115
|
+
if (own > 0) {
|
|
116
|
+
total += own;
|
|
117
|
+
sites.push({ ...site, bytesPerCall: own / calls });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
sites.sort((first, second) => second.bytesPerCall - first.bytesPerCall);
|
|
121
|
+
return {
|
|
122
|
+
bytesPerCall: total / calls,
|
|
123
|
+
sites,
|
|
124
|
+
externalBytesPerCall: Math.max(0, measured.external - cost.external) / calls,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
session.disconnect();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=allocation-sites.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allocation-sites.js","sourceRoot":"","sources":["../../src/v8/allocation-sites.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,OAAO,EACN,aAAa,GAGb,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AA4E7D;;;GAGG;AACH,KAAK,UAAU,aAAa,CAC3B,OAAgB,EAChB,QAAuB,EACvB,IAAoB,EACpB,KAAa,EACb,gBAAwB,EACxB,OAAqD;IAErD,MAAM,UAAU,GAAG;QAClB,gBAAgB;QAChB,0CAA0C;QAC1C,gCAAgC,EAAE,IAAI;QACtC,gCAAgC,EAAE,IAAI;KACtC,CAAC;IACF,QAAQ,EAAE,CAAC;IACX,MAAM,OAAO,CAAC,IAAI,CAAC,4BAA4B,EAAE,UAAU,CAAC,CAAC;IAC7D,QAAQ,CAAC,KAAK,EAAE,CAAC;IACjB,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC;IAC7C,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC;QACzC,IAAI,EAAE,CAAC;IACR,CAAC;IACD,MAAM,GAAG,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC;IAC3C,MAAM,EAAE,aAAa,EAAE,GAAG,kBAAkB,CAAC,QAAQ,CAAC,IAAI,EAAE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AAgB,EAChB,OAAO,CACP,CAAC;QACF,MAAM,OAAO,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;QAC3C,MAAM,KAAK,GAAqB,EAAE,CAAC;QACnC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,MAAM,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,IAAI,QAAQ,CAAC,KAAK,EAAE,CAAC;YACrD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC;YACnE,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;gBACb,KAAK,IAAI,GAAG,CAAC;gBACb,KAAK,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,EAAE,YAAY,EAAE,GAAG,GAAG,KAAK,EAAE,CAAC,CAAC;YACpD,CAAC;QACF,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,YAAY,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;QACxE,OAAO;YACN,YAAY,EAAE,KAAK,GAAG,KAAK;YAC3B,KAAK;YACL,oBAAoB,EACnB,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,KAAK;SACvD,CAAC;IACH,CAAC;YAAS,CAAC;QACV,OAAO,CAAC,UAAU,EAAE,CAAC;IACtB,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shapes V8 gives an array's elements, from the most to the least compact.
|
|
3
|
+
*
|
|
4
|
+
* - `"packed-smi"` and `"holey-smi"`: small integers only (31-bit on
|
|
5
|
+
* pointer-compressed builds).
|
|
6
|
+
* - `"packed-double"` and `"holey-double"`: numbers, stored as unboxed doubles.
|
|
7
|
+
* - `"packed-object"` and `"holey-object"`: any value.
|
|
8
|
+
* - `"dictionary"`: a sparse hash table, after a very large index or many
|
|
9
|
+
* deletions.
|
|
10
|
+
* - `"other"`: everything else — typed arrays, frozen or sealed arrays, an
|
|
11
|
+
* `arguments` object.
|
|
12
|
+
*
|
|
13
|
+
* A holey kind has a missing element that falls through to the prototype chain.
|
|
14
|
+
* V8 moves an array towards the more general kinds as it is written to (smi to
|
|
15
|
+
* double to object, packed to holey), and promises nothing about the way back:
|
|
16
|
+
* `new Array(5).fill(1)` is reported packed on current V8. Assert the kind a
|
|
17
|
+
* creator hands out, not the path an array took to it.
|
|
18
|
+
*/
|
|
19
|
+
export type V8ElementsKind = "packed-smi" | "holey-smi" | "packed-double" | "holey-double" | "packed-object" | "holey-object" | "dictionary" | "other";
|
|
20
|
+
/**
|
|
21
|
+
* The elements kind V8 holds for `value` right now, to test that code keeps an
|
|
22
|
+
* array on the compact kind its speed depends on (a `number[]` that stays
|
|
23
|
+
* `"packed-double"`, not `"holey-smi"` or `"packed-object"`).
|
|
24
|
+
*
|
|
25
|
+
* It asks V8's own runtime (`%HasSmiElements` and its siblings), compiled on
|
|
26
|
+
* the first call, so it needs the flag `--allow-natives-syntax`; without it, or
|
|
27
|
+
* outside V8, the call throws an `Error` that names the flag. Importing this
|
|
28
|
+
* module never throws. The answer describes one engine version at one moment:
|
|
29
|
+
* assert the kinds a contract depends on, not every transition.
|
|
30
|
+
*
|
|
31
|
+
* Under Vitest, run each test file in its own process so that the kind a test
|
|
32
|
+
* provokes cannot reach another file's type feedback: `pool: "forks"`,
|
|
33
|
+
* `isolate: true` and `execArgv: ["--allow-natives-syntax"]` in the project's
|
|
34
|
+
* `test` options.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* v8ElementsKind([1.5, 2.5]); // "packed-double"
|
|
38
|
+
* v8ElementsKind([1, 2]); // "packed-smi"
|
|
39
|
+
* v8ElementsKind(new Array(3)); // "holey-smi"
|
|
40
|
+
*
|
|
41
|
+
* V8's intrinsics abort the whole process on what they refuse, so the call
|
|
42
|
+
* checks first: a `Proxy` has no elements of its own and answers `"other"`.
|
|
43
|
+
*
|
|
44
|
+
* @param value - An array, or any object that has elements.
|
|
45
|
+
* @returns The kind, `"other"` when it is none of the array kinds, or a proxy.
|
|
46
|
+
* @throws {TypeError} When `value` is not an object or a function.
|
|
47
|
+
* @throws {Error} When natives syntax is not available.
|
|
48
|
+
*/
|
|
49
|
+
export declare function v8ElementsKind(value: object): V8ElementsKind;
|
|
50
|
+
//# sourceMappingURL=elements-kind.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elements-kind.d.ts","sourceRoot":"","sources":["../../src/v8/elements-kind.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,cAAc,GACvB,YAAY,GACZ,WAAW,GACX,eAAe,GACf,cAAc,GACd,eAAe,GACf,cAAc,GACd,YAAY,GACZ,OAAO,CAAC;AA+CX;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAmC5D"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
let probes_;
|
|
2
|
+
// Natives syntax (`%HasSmiElements(value)`) is not JavaScript a module can
|
|
3
|
+
// contain, so each probe is compiled on first use from a fixed body; loading
|
|
4
|
+
// this module never needs the flag.
|
|
5
|
+
function compileProbe_(body) {
|
|
6
|
+
// oxlint-disable-next-line typescript/no-implied-eval -- natives syntax cannot be written in a module, only compiled
|
|
7
|
+
return new Function("value", body);
|
|
8
|
+
}
|
|
9
|
+
function getProbes_() {
|
|
10
|
+
if (probes_ !== undefined) {
|
|
11
|
+
return probes_;
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
probes_ = {
|
|
15
|
+
isProxy: compileProbe_("return %IsJSProxy(value)"),
|
|
16
|
+
hasDictionary: compileProbe_("return %HasDictionaryElements(value)"),
|
|
17
|
+
hasSmi: compileProbe_("return %HasSmiElements(value)"),
|
|
18
|
+
hasDouble: compileProbe_("return %HasDoubleElements(value)"),
|
|
19
|
+
hasObject: compileProbe_("return %HasObjectElements(value)"),
|
|
20
|
+
hasHoley: compileProbe_("return %HasHoleyElements(value)"),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
catch (cause) {
|
|
24
|
+
throw new Error("v8ElementsKind needs V8 started with --allow-natives-syntax; with Vitest, set `pool: 'forks'` and `execArgv: ['--allow-natives-syntax']` in the project's test options", { cause });
|
|
25
|
+
}
|
|
26
|
+
return probes_;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The elements kind V8 holds for `value` right now, to test that code keeps an
|
|
30
|
+
* array on the compact kind its speed depends on (a `number[]` that stays
|
|
31
|
+
* `"packed-double"`, not `"holey-smi"` or `"packed-object"`).
|
|
32
|
+
*
|
|
33
|
+
* It asks V8's own runtime (`%HasSmiElements` and its siblings), compiled on
|
|
34
|
+
* the first call, so it needs the flag `--allow-natives-syntax`; without it, or
|
|
35
|
+
* outside V8, the call throws an `Error` that names the flag. Importing this
|
|
36
|
+
* module never throws. The answer describes one engine version at one moment:
|
|
37
|
+
* assert the kinds a contract depends on, not every transition.
|
|
38
|
+
*
|
|
39
|
+
* Under Vitest, run each test file in its own process so that the kind a test
|
|
40
|
+
* provokes cannot reach another file's type feedback: `pool: "forks"`,
|
|
41
|
+
* `isolate: true` and `execArgv: ["--allow-natives-syntax"]` in the project's
|
|
42
|
+
* `test` options.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* v8ElementsKind([1.5, 2.5]); // "packed-double"
|
|
46
|
+
* v8ElementsKind([1, 2]); // "packed-smi"
|
|
47
|
+
* v8ElementsKind(new Array(3)); // "holey-smi"
|
|
48
|
+
*
|
|
49
|
+
* V8's intrinsics abort the whole process on what they refuse, so the call
|
|
50
|
+
* checks first: a `Proxy` has no elements of its own and answers `"other"`.
|
|
51
|
+
*
|
|
52
|
+
* @param value - An array, or any object that has elements.
|
|
53
|
+
* @returns The kind, `"other"` when it is none of the array kinds, or a proxy.
|
|
54
|
+
* @throws {TypeError} When `value` is not an object or a function.
|
|
55
|
+
* @throws {Error} When natives syntax is not available.
|
|
56
|
+
*/
|
|
57
|
+
export function v8ElementsKind(value) {
|
|
58
|
+
if ((typeof value !== "object" && typeof value !== "function") ||
|
|
59
|
+
value === null) {
|
|
60
|
+
throw new TypeError(`v8ElementsKind needs an object, got ${value === null ? "null" : typeof value}`);
|
|
61
|
+
}
|
|
62
|
+
const probes = getProbes_();
|
|
63
|
+
if (probes.isProxy(value)) {
|
|
64
|
+
return "other";
|
|
65
|
+
}
|
|
66
|
+
if (probes.hasDictionary(value)) {
|
|
67
|
+
return "dictionary";
|
|
68
|
+
}
|
|
69
|
+
const holey = probes.hasHoley(value);
|
|
70
|
+
if (probes.hasSmi(value)) {
|
|
71
|
+
return holey ? "holey-smi" : "packed-smi";
|
|
72
|
+
}
|
|
73
|
+
if (probes.hasDouble(value)) {
|
|
74
|
+
return holey ? "holey-double" : "packed-double";
|
|
75
|
+
}
|
|
76
|
+
if (probes.hasObject(value)) {
|
|
77
|
+
return holey ? "holey-object" : "packed-object";
|
|
78
|
+
}
|
|
79
|
+
return "other";
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=elements-kind.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elements-kind.js","sourceRoot":"","sources":["../../src/v8/elements-kind.ts"],"names":[],"mappings":"AAuCA,IAAI,OAA4B,CAAC;AAEjC,2EAA2E;AAC3E,6EAA6E;AAC7E,oCAAoC;AACpC,SAAS,aAAa,CAAC,IAAY;IAClC,qHAAqH;IACrH,OAAO,IAAI,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAW,CAAC;AAC9C,CAAC;AAED,SAAS,UAAU;IAClB,IAAI,OAA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AC;QAC1B,OAAO,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,YAAY,CAAC;IAC3C,CAAC;IAED,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,eAAe,CAAC;IACjD,CAAC;IAED,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,eAAe,CAAC;IACjD,CAAC;IAED,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/v8/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC"}
|
package/dist/v8/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/v8/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/test-util",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Dependency-free test-harness primitives, such as a deterministic synchronous delay for timing-sensitive tests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"test-utils",
|
|
@@ -24,8 +24,16 @@
|
|
|
24
24
|
"type": "module",
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"main": "./dist/index.js",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/index.js",
|
|
29
|
+
"./v8": "./dist/v8/index.js"
|
|
30
|
+
},
|
|
27
31
|
"publishConfig": {
|
|
28
32
|
"main": "./dist/index.js",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": "./dist/index.js",
|
|
35
|
+
"./v8": "./dist/v8/index.js"
|
|
36
|
+
},
|
|
29
37
|
"access": "public"
|
|
30
38
|
},
|
|
31
39
|
"scripts": {
|
|
@@ -37,19 +45,22 @@
|
|
|
37
45
|
"prepack": "yarn build"
|
|
38
46
|
},
|
|
39
47
|
"dependencies": {
|
|
40
|
-
"@ac-kit/core": "^0.
|
|
48
|
+
"@ac-kit/core": "^0.13.0"
|
|
41
49
|
},
|
|
42
50
|
"devDependencies": {
|
|
43
51
|
"@ac-kit/tsconfig": "0.1.0",
|
|
44
52
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
45
|
-
"@ac-kit/vitest-config": "0.
|
|
46
|
-
"@types/node": "26.6.
|
|
53
|
+
"@ac-kit/vitest-config": "0.2.0",
|
|
54
|
+
"@types/node": "26.6.11",
|
|
47
55
|
"@typescript/native": "typescript@7.0.2",
|
|
48
56
|
"rimraf": "6.1.4",
|
|
49
57
|
"typedoc": "0.28.20",
|
|
50
58
|
"typescript": "@typescript/typescript6@6.0.2",
|
|
51
|
-
"vite": "8.
|
|
52
|
-
"vitest": "5.1.
|
|
59
|
+
"vite": "8.4.0",
|
|
60
|
+
"vitest": "5.1.2"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"vitest": ">=4.0.0"
|
|
53
64
|
},
|
|
54
65
|
"engines": {
|
|
55
66
|
"node": ">=24"
|