@openedges/pkl-napi 0.32.0-rc.3 → 0.32.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/dist/index.mjs CHANGED
@@ -28,11 +28,6 @@ export const {
28
28
  format,
29
29
  decodePklBinary,
30
30
  monotonicClockMs,
31
- roundtripValue,
32
- roundtripInteger,
33
- roundtripBytes,
34
- roundtripString,
35
- measureConversion,
36
31
  } = pkg;
37
32
 
38
33
  export default pkg;
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+
3
+ const pkg = require("./index.cjs");
4
+
5
+ const TESTING_INTERNALS = Symbol.for("@openedges/pkl-napi/testing-internals");
6
+ const internals = pkg[TESTING_INTERNALS];
7
+ if (!internals) {
8
+ throw new Error("Missing @openedges/pkl-napi testing internals");
9
+ }
10
+
11
+ const { native, unwrapOutcome, dehydrateValue, hydrateValue } = internals;
12
+ const { PklError, PklObject, PklDuration, PklDataSize, PklPair } = pkg;
13
+
14
+ const MIN_I64 = -(1n << 63n);
15
+ const MAX_I64 = (1n << 63n) - 1n;
16
+
17
+ function roundtripValue(val) {
18
+ const dehydrated = dehydrateValue(val);
19
+ const raw = unwrapOutcome(native.productRoundtripValue(dehydrated));
20
+ return hydrateValue(raw);
21
+ }
22
+
23
+ function roundtripInteger(val) {
24
+ if (typeof val !== "bigint") {
25
+ throw new PklError("roundtripInteger expects a BigInt.", "invalid-argument");
26
+ }
27
+ return unwrapOutcome(native.productRoundtripInteger(val));
28
+ }
29
+
30
+ function roundtripBytes(val) {
31
+ if (!(val instanceof Uint8Array)) {
32
+ throw new PklError("roundtripBytes expects a Uint8Array.", "invalid-argument");
33
+ }
34
+ return native.roundtripBytes(new Uint8Array(val.buffer, val.byteOffset, val.byteLength));
35
+ }
36
+
37
+ function roundtripString(val) {
38
+ if (typeof val !== "string") {
39
+ throw new PklError("roundtripString expects a string.", "invalid-argument");
40
+ }
41
+ return native.roundtripString(val);
42
+ }
43
+
44
+ function defaultBenchmarkPayload() {
45
+ return new PklObject(
46
+ "Bench#Payload",
47
+ "repl:bench",
48
+ {
49
+ intMin: MIN_I64,
50
+ intMax: MAX_I64,
51
+ floatVal: 3.141592653589793,
52
+ text: "hello-pkl-🚀-utf16",
53
+ bytes: new Uint8Array([0, 1, 2, 127, 128, 255]),
54
+ duration: new PklDuration(5, "s"),
55
+ dataSize: new PklDataSize(16, "mib"),
56
+ pair: new PklPair("left", 42n),
57
+ list: [1n, 2n, 3n, true, null],
58
+ set: new Set(["a", "b", "c"]),
59
+ map: new Map([
60
+ ["k1", 100n],
61
+ ["k2", "v2"],
62
+ ]),
63
+ },
64
+ new Map(),
65
+ [],
66
+ );
67
+ }
68
+
69
+ function measureConversion(valOrIterations = 1000, maybeSecond = undefined) {
70
+ let value;
71
+ let iterations;
72
+ if (
73
+ typeof valOrIterations === "number" &&
74
+ (maybeSecond === undefined || Number.isInteger(valOrIterations))
75
+ ) {
76
+ iterations = valOrIterations;
77
+ value = maybeSecond !== undefined ? maybeSecond : defaultBenchmarkPayload();
78
+ } else {
79
+ value = valOrIterations;
80
+ iterations = maybeSecond === undefined ? 1000 : maybeSecond;
81
+ }
82
+ if (!Number.isInteger(iterations) || iterations < 1 || iterations > 1_000_000) {
83
+ throw new PklError(
84
+ `iterations must be an integer between 1 and 1000000, got ${iterations}.`,
85
+ "invalid-argument",
86
+ );
87
+ }
88
+ const dehydrated = dehydrateValue(value);
89
+ const startNs = process.hrtime.bigint();
90
+ const nativeStats = unwrapOutcome(native.productMeasureConversion(dehydrated, iterations));
91
+ const hydrated = hydrateValue(nativeStats.lastValue);
92
+ const totalElapsedMs = Number(process.hrtime.bigint() - startNs) / 1e6;
93
+ const perIterationUs = (totalElapsedMs * 1000) / nativeStats.iterations;
94
+ return {
95
+ iterations: nativeStats.iterations,
96
+ totalMs: totalElapsedMs,
97
+ elapsedMs: totalElapsedMs,
98
+ nativeElapsedMs: nativeStats.elapsedMs,
99
+ avgUs: perIterationUs,
100
+ perIterationUs,
101
+ nativePerIterationUs: nativeStats.perIterationUs,
102
+ opsPerSec: totalElapsedMs > 0 ? (nativeStats.iterations * 1000) / totalElapsedMs : Infinity,
103
+ sample: hydrated,
104
+ lastValue: hydrated,
105
+ };
106
+ }
107
+
108
+ module.exports = {
109
+ roundtripValue,
110
+ roundtripInteger,
111
+ roundtripBytes,
112
+ roundtripString,
113
+ measureConversion,
114
+ };
@@ -0,0 +1,29 @@
1
+ import type { PklValue } from "./index.cjs";
2
+
3
+ export function roundtripValue<T extends PklValue>(val: T): PklValue;
4
+ export function roundtripInteger(val: bigint): bigint;
5
+ export function roundtripBytes(val: Uint8Array): Uint8Array;
6
+ export function roundtripString(val: string): string;
7
+
8
+ export interface ConversionBenchmarkResult {
9
+ iterations: number;
10
+ totalMs: number;
11
+ elapsedMs: number;
12
+ nativeElapsedMs: number;
13
+ avgUs: number;
14
+ perIterationUs: number;
15
+ nativePerIterationUs: number;
16
+ opsPerSec: number;
17
+ sample: PklValue;
18
+ lastValue: PklValue;
19
+ }
20
+ export type ConversionTelemetry = ConversionBenchmarkResult;
21
+
22
+ export function measureConversion(
23
+ iterations?: number,
24
+ sampleValue?: PklValue,
25
+ ): ConversionBenchmarkResult;
26
+ export function measureConversion(
27
+ value: PklValue,
28
+ iterations?: number,
29
+ ): ConversionBenchmarkResult;
@@ -0,0 +1,38 @@
1
+ import type { PklValue } from "./index.mjs";
2
+
3
+ export function roundtripValue<T extends PklValue>(val: T): PklValue;
4
+ export function roundtripInteger(val: bigint): bigint;
5
+ export function roundtripBytes(val: Uint8Array): Uint8Array;
6
+ export function roundtripString(val: string): string;
7
+
8
+ export interface ConversionBenchmarkResult {
9
+ iterations: number;
10
+ totalMs: number;
11
+ elapsedMs: number;
12
+ nativeElapsedMs: number;
13
+ avgUs: number;
14
+ perIterationUs: number;
15
+ nativePerIterationUs: number;
16
+ opsPerSec: number;
17
+ sample: PklValue;
18
+ lastValue: PklValue;
19
+ }
20
+ export type ConversionTelemetry = ConversionBenchmarkResult;
21
+
22
+ export function measureConversion(
23
+ iterations?: number,
24
+ sampleValue?: PklValue,
25
+ ): ConversionBenchmarkResult;
26
+ export function measureConversion(
27
+ value: PklValue,
28
+ iterations?: number,
29
+ ): ConversionBenchmarkResult;
30
+
31
+ declare const _testingDefault: {
32
+ roundtripValue: typeof roundtripValue;
33
+ roundtripInteger: typeof roundtripInteger;
34
+ roundtripBytes: typeof roundtripBytes;
35
+ roundtripString: typeof roundtripString;
36
+ measureConversion: typeof measureConversion;
37
+ };
38
+ export default _testingDefault;
@@ -0,0 +1,29 @@
1
+ import type { PklValue } from "./index";
2
+
3
+ export function roundtripValue<T extends PklValue>(val: T): PklValue;
4
+ export function roundtripInteger(val: bigint): bigint;
5
+ export function roundtripBytes(val: Uint8Array): Uint8Array;
6
+ export function roundtripString(val: string): string;
7
+
8
+ export interface ConversionBenchmarkResult {
9
+ iterations: number;
10
+ totalMs: number;
11
+ elapsedMs: number;
12
+ nativeElapsedMs: number;
13
+ avgUs: number;
14
+ perIterationUs: number;
15
+ nativePerIterationUs: number;
16
+ opsPerSec: number;
17
+ sample: PklValue;
18
+ lastValue: PklValue;
19
+ }
20
+ export type ConversionTelemetry = ConversionBenchmarkResult;
21
+
22
+ export function measureConversion(
23
+ iterations?: number,
24
+ sampleValue?: PklValue,
25
+ ): ConversionBenchmarkResult;
26
+ export function measureConversion(
27
+ value: PklValue,
28
+ iterations?: number,
29
+ ): ConversionBenchmarkResult;
@@ -0,0 +1,14 @@
1
+ import { createRequire } from "node:module";
2
+
3
+ const require = createRequire(import.meta.url);
4
+ const testing = require("./testing.cjs");
5
+
6
+ export const {
7
+ roundtripValue,
8
+ roundtripInteger,
9
+ roundtripBytes,
10
+ roundtripString,
11
+ measureConversion,
12
+ } = testing;
13
+
14
+ export default testing;
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openedges/pkl-napi",
3
- "version": "0.32.0-rc.3",
3
+ "version": "0.32.0",
4
4
  "description": "Node.js N-API bindings for the shared pure-Rust Apple Pkl 0.32.0 engine",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -17,9 +17,24 @@
17
17
  "types": "./dist/index.d.ts",
18
18
  "exports": {
19
19
  ".": {
20
- "types": "./dist/index.d.ts",
21
- "import": "./dist/index.mjs",
22
- "require": "./dist/index.cjs"
20
+ "import": {
21
+ "types": "./dist/index.d.mts",
22
+ "default": "./dist/index.mjs"
23
+ },
24
+ "require": {
25
+ "types": "./dist/index.d.cts",
26
+ "default": "./dist/index.cjs"
27
+ }
28
+ },
29
+ "./testing": {
30
+ "import": {
31
+ "types": "./dist/testing.d.mts",
32
+ "default": "./dist/testing.mjs"
33
+ },
34
+ "require": {
35
+ "types": "./dist/testing.d.cts",
36
+ "default": "./dist/testing.cjs"
37
+ }
23
38
  }
24
39
  },
25
40
  "files": [