@c9up/chronos 0.1.16 → 0.1.17
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 +81 -8
- package/dist/DateTime.d.ts +64 -4
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +314 -32
- package/dist/DateTime.js.map +1 -1
- package/dist/format.d.ts +10 -0
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +5 -5
- package/dist/format.js.map +1 -1
- package/dist/holidays.d.ts +43 -0
- package/dist/holidays.d.ts.map +1 -0
- package/dist/holidays.js +102 -0
- package/dist/holidays.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/native/generated.d.ts +9 -0
- package/dist/native/generated.d.ts.map +1 -1
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +3 -0
- package/dist/native.js.map +1 -1
- package/dist/parse.d.ts +59 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +404 -0
- package/dist/parse.js.map +1 -0
- package/dist/rrule.d.ts +10 -2
- package/dist/rrule.d.ts.map +1 -1
- package/dist/rrule.js +33 -7
- package/dist/rrule.js.map +1 -1
- package/index.darwin-arm64.node +0 -0
- package/index.darwin-x64.node +0 -0
- package/index.linux-arm64-gnu.node +0 -0
- package/index.linux-x64-gnu.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +2 -3
- package/src/DateTime.ts +448 -32
- package/src/format.ts +6 -6
- package/src/holidays.ts +161 -0
- package/src/index.ts +18 -0
- package/src/native/generated.ts +21 -0
- package/src/native.ts +4 -0
- package/src/parse.ts +559 -0
- package/src/rrule.ts +47 -8
- package/wasm/chronos_engine_wasm.d.ts +9 -0
- package/wasm/chronos_engine_wasm.js +61 -0
- package/wasm/chronos_engine_wasm_bg.wasm +0 -0
- package/wasm/chronos_engine_wasm_bg.wasm.d.ts +3 -0
- package/dist/utils.d.ts +0 -15
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -17
- package/dist/utils.js.map +0 -1
- package/scripts/build-napi-types.mjs +0 -69
- package/scripts/clean-wasm-pack-meta.mjs +0 -32
- package/scripts/copy-napi.mjs +0 -56
- package/scripts/generate-napi-types.mjs +0 -156
- package/scripts/verify-napi.mjs +0 -46
- package/scripts/verify-wasm.mjs +0 -55
- package/src/utils.ts +0 -17
|
@@ -118,6 +118,29 @@ export function diff_in_zone(a_utc, b_utc, unit, zone) {
|
|
|
118
118
|
return ret[0];
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
/**
|
|
122
|
+
* @param {number} year
|
|
123
|
+
* @returns {string}
|
|
124
|
+
*/
|
|
125
|
+
export function easter_date(year) {
|
|
126
|
+
let deferred2_0;
|
|
127
|
+
let deferred2_1;
|
|
128
|
+
try {
|
|
129
|
+
const ret = wasm.easter_date(year);
|
|
130
|
+
var ptr1 = ret[0];
|
|
131
|
+
var len1 = ret[1];
|
|
132
|
+
if (ret[3]) {
|
|
133
|
+
ptr1 = 0; len1 = 0;
|
|
134
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
135
|
+
}
|
|
136
|
+
deferred2_0 = ptr1;
|
|
137
|
+
deferred2_1 = len1;
|
|
138
|
+
return getStringFromWasm0(ptr1, len1);
|
|
139
|
+
} finally {
|
|
140
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
121
144
|
/**
|
|
122
145
|
* @param {string} iso
|
|
123
146
|
* @param {string} unit
|
|
@@ -233,6 +256,44 @@ export function from_local(naive_iso, zone) {
|
|
|
233
256
|
}
|
|
234
257
|
}
|
|
235
258
|
|
|
259
|
+
/**
|
|
260
|
+
* @param {number} year
|
|
261
|
+
* @param {string} country
|
|
262
|
+
* @param {string | null} [region]
|
|
263
|
+
* @returns {any}
|
|
264
|
+
*/
|
|
265
|
+
export function holidays(year, country, region) {
|
|
266
|
+
const ptr0 = passStringToWasm0(country, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
267
|
+
const len0 = WASM_VECTOR_LEN;
|
|
268
|
+
var ptr1 = isLikeNone(region) ? 0 : passStringToWasm0(region, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
269
|
+
var len1 = WASM_VECTOR_LEN;
|
|
270
|
+
const ret = wasm.holidays(year, ptr0, len0, ptr1, len1);
|
|
271
|
+
if (ret[2]) {
|
|
272
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
273
|
+
}
|
|
274
|
+
return takeFromExternrefTable0(ret[0]);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* @param {string} date
|
|
279
|
+
* @param {string} country
|
|
280
|
+
* @param {string | null} [region]
|
|
281
|
+
* @returns {boolean}
|
|
282
|
+
*/
|
|
283
|
+
export function is_holiday(date, country, region) {
|
|
284
|
+
const ptr0 = passStringToWasm0(date, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
285
|
+
const len0 = WASM_VECTOR_LEN;
|
|
286
|
+
const ptr1 = passStringToWasm0(country, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
287
|
+
const len1 = WASM_VECTOR_LEN;
|
|
288
|
+
var ptr2 = isLikeNone(region) ? 0 : passStringToWasm0(region, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
289
|
+
var len2 = WASM_VECTOR_LEN;
|
|
290
|
+
const ret = wasm.is_holiday(ptr0, len0, ptr1, len1, ptr2, len2);
|
|
291
|
+
if (ret[2]) {
|
|
292
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
293
|
+
}
|
|
294
|
+
return ret[0] !== 0;
|
|
295
|
+
}
|
|
296
|
+
|
|
236
297
|
/**
|
|
237
298
|
* @param {string} input
|
|
238
299
|
* @returns {string}
|
|
Binary file
|
|
@@ -6,10 +6,13 @@ export const add_in_zone: (a: number, b: number, c: bigint, d: number, e: number
|
|
|
6
6
|
export const calendar_parts: (a: number, b: number) => [number, number, number];
|
|
7
7
|
export const diff: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
|
|
8
8
|
export const diff_in_zone: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => [number, number, number];
|
|
9
|
+
export const easter_date: (a: number) => [number, number, number, number];
|
|
9
10
|
export const end_of: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
10
11
|
export const end_of_in_zone: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
|
|
11
12
|
export const format: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
12
13
|
export const from_local: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
14
|
+
export const holidays: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
|
|
15
|
+
export const is_holiday: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
|
|
13
16
|
export const parse_http: (a: number, b: number) => [number, number, number, number];
|
|
14
17
|
export const parse_rfc2822: (a: number, b: number) => [number, number, number, number];
|
|
15
18
|
export const parse_sql: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
package/dist/utils.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared helpers for DateTime and RRule modules.
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Normalize an ISO 8601 string.
|
|
6
|
-
*
|
|
7
|
-
* Milliseconds are KEPT even when zero, so `toISO()` reads
|
|
8
|
-
* `2026-07-15T10:00:00.000Z` — what Luxon's `toISO()` returns, and therefore
|
|
9
|
-
* what `@adonisjs/lucid` puts in a serialized model. Chronos used to strip
|
|
10
|
-
* `.000`, which is Luxon's opt-in `toISO({ suppressMilliseconds: true })`:
|
|
11
|
-
* the same instant, a different literal, and a silent difference in every JSON
|
|
12
|
-
* payload carrying a date.
|
|
13
|
-
*/
|
|
14
|
-
export declare function normalizeIso(iso: string): string;
|
|
15
|
-
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD"}
|
package/dist/utils.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared helpers for DateTime and RRule modules.
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Normalize an ISO 8601 string.
|
|
6
|
-
*
|
|
7
|
-
* Milliseconds are KEPT even when zero, so `toISO()` reads
|
|
8
|
-
* `2026-07-15T10:00:00.000Z` — what Luxon's `toISO()` returns, and therefore
|
|
9
|
-
* what `@adonisjs/lucid` puts in a serialized model. Chronos used to strip
|
|
10
|
-
* `.000`, which is Luxon's opt-in `toISO({ suppressMilliseconds: true })`:
|
|
11
|
-
* the same instant, a different literal, and a silent difference in every JSON
|
|
12
|
-
* payload carrying a date.
|
|
13
|
-
*/
|
|
14
|
-
export function normalizeIso(iso) {
|
|
15
|
-
return iso;
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,GAAW;IACvC,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Regenerate `src/native/generated.ts` from the Rust.
|
|
4
|
-
*
|
|
5
|
-
* Node rather than a shell script because this runs inside `build:napi`, and
|
|
6
|
-
* that runs on the Windows prebuild runner too: a bash `mktemp` there hands
|
|
7
|
-
* cargo a `/tmp/...` path the native proc-macro cannot write to, so the
|
|
8
|
-
* type-def file comes back empty and the build fails for no visible reason.
|
|
9
|
-
*
|
|
10
|
-
* One crate at a time on purpose: with napi-derive 2 the writes all went to a
|
|
11
|
-
* single `TYPE_DEF_TMP_PATH` and a parallel build interleaved them
|
|
12
|
-
* while cargo compiles, and a parallel build interleaves the writes —
|
|
13
|
-
* definitions go missing, silently, and the generated file comes out short.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { execFileSync } from 'node:child_process'
|
|
17
|
-
import { createRequire } from 'node:module'
|
|
18
|
-
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
19
|
-
import { tmpdir } from 'node:os'
|
|
20
|
-
import { dirname, join } from 'node:path'
|
|
21
|
-
import { fileURLToPath } from 'node:url'
|
|
22
|
-
|
|
23
|
-
const CRATES = ['chronos-engine-napi']
|
|
24
|
-
|
|
25
|
-
const packageRoot = join(dirname(fileURLToPath(import.meta.url)), '..')
|
|
26
|
-
const output = join('src', 'native', 'generated.ts')
|
|
27
|
-
const scratch = mkdtempSync(join(tmpdir(), 'napi-types-'))
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
const lines = []
|
|
31
|
-
for (const crate of CRATES) {
|
|
32
|
-
const perCrate = join(scratch, crate)
|
|
33
|
-
writeFileSync(perCrate, '')
|
|
34
|
-
execFileSync('cargo', ['build', '-p', crate], {
|
|
35
|
-
cwd: packageRoot,
|
|
36
|
-
env: { ...process.env, NAPI_TYPE_DEF_TMP_FOLDER: scratch },
|
|
37
|
-
stdio: ['ignore', 'ignore', 'inherit'],
|
|
38
|
-
})
|
|
39
|
-
const emitted = readFileSync(perCrate, 'utf8').split('\n').filter(Boolean)
|
|
40
|
-
if (emitted.length === 0) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
`[napi-types] ${crate} emitted nothing — is napi-derive's "type-def" feature on?`,
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
lines.push(...emitted)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const combined = join(scratch, 'combined.jsonl')
|
|
49
|
-
writeFileSync(combined, `${lines.join('\n')}\n`)
|
|
50
|
-
|
|
51
|
-
execFileSync(
|
|
52
|
-
process.execPath,
|
|
53
|
-
[join('scripts', 'generate-napi-types.mjs'), combined, output],
|
|
54
|
-
{ cwd: packageRoot, stdio: 'inherit' },
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
// Formatted here rather than excluded from the linter: the file is checked
|
|
58
|
-
// in, so it should read like the rest of the tree — and formatting it at
|
|
59
|
-
// generation means it never shows up as a diff someone has to fix by hand.
|
|
60
|
-
// Resolved through the package entry rather than `node_modules/.bin`, whose
|
|
61
|
-
// shim is a shell script the Windows runner cannot execute directly.
|
|
62
|
-
const biome = createRequire(import.meta.url).resolve('@biomejs/biome/bin/biome')
|
|
63
|
-
execFileSync(process.execPath, [biome, 'format', '--write', output], {
|
|
64
|
-
cwd: packageRoot,
|
|
65
|
-
stdio: ['ignore', 'ignore', 'inherit'],
|
|
66
|
-
})
|
|
67
|
-
} finally {
|
|
68
|
-
rmSync(scratch, { recursive: true, force: true })
|
|
69
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { existsSync, rmSync } from 'node:fs'
|
|
2
|
-
import { dirname, join } from 'node:path'
|
|
3
|
-
import { fileURLToPath } from 'node:url'
|
|
4
|
-
|
|
5
|
-
// Removes the package metadata `wasm-pack` writes into its output directory.
|
|
6
|
-
//
|
|
7
|
-
// `wasm-pack build` treats its out-dir as a publishable package of its own and
|
|
8
|
-
// scaffolds one: a `package.json`, and a `.gitignore` whose entire content is
|
|
9
|
-
// `*`. Ours is not a package — it is a directory inside this one, listed in
|
|
10
|
-
// `files`.
|
|
11
|
-
//
|
|
12
|
-
// The `.gitignore` is the damaging half. npm honours a .gitignore nested INSIDE
|
|
13
|
-
// a published directory even when `files` lists that directory, so a single `*`
|
|
14
|
-
// silently strips every artifact from the tarball — the built .js and .wasm, and
|
|
15
|
-
// the committed .d.ts stub with them. The package then publishes green and every
|
|
16
|
-
// browser consumer gets the engine-missing error. Measured here: with the file
|
|
17
|
-
// present `npm pack --dry-run` reports zero wasm/ entries, without it, three.
|
|
18
|
-
//
|
|
19
|
-
// Runs right after wasm-pack, before anything packs.
|
|
20
|
-
|
|
21
|
-
const wasmDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'wasm')
|
|
22
|
-
|
|
23
|
-
// Only what wasm-pack scaffolds. The artifacts and the committed .d.ts stay.
|
|
24
|
-
const generated = ['.gitignore', 'package.json', 'README.md', 'LICENSE']
|
|
25
|
-
|
|
26
|
-
for (const name of generated) {
|
|
27
|
-
const path = join(wasmDir, name)
|
|
28
|
-
if (existsSync(path)) {
|
|
29
|
-
rmSync(path)
|
|
30
|
-
console.log(`[wasm] removed generated wasm/${name}`)
|
|
31
|
-
}
|
|
32
|
-
}
|
package/scripts/copy-napi.mjs
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { copyFileSync, existsSync } from 'node:fs'
|
|
2
|
-
import { dirname, join, resolve } from 'node:path'
|
|
3
|
-
import { arch, env, platform } from 'node:process'
|
|
4
|
-
import { fileURLToPath } from 'node:url'
|
|
5
|
-
|
|
6
|
-
const here = dirname(fileURLToPath(import.meta.url))
|
|
7
|
-
const root = join(here, '..')
|
|
8
|
-
const CRATE = 'chronos_engine_napi'
|
|
9
|
-
const TAG = '[chronos:napi]'
|
|
10
|
-
|
|
11
|
-
const tripleMap = {
|
|
12
|
-
'x86_64-unknown-linux-gnu': { suffix: 'linux-x64-gnu', os: 'linux' },
|
|
13
|
-
'aarch64-unknown-linux-gnu': { suffix: 'linux-arm64-gnu', os: 'linux' },
|
|
14
|
-
'x86_64-apple-darwin': { suffix: 'darwin-x64', os: 'darwin' },
|
|
15
|
-
'aarch64-apple-darwin': { suffix: 'darwin-arm64', os: 'darwin' },
|
|
16
|
-
'x86_64-pc-windows-msvc': { suffix: 'win32-x64-msvc', os: 'win32' },
|
|
17
|
-
}
|
|
18
|
-
const hostSuffixMap = {
|
|
19
|
-
'linux-x64': 'linux-x64-gnu', 'linux-arm64': 'linux-arm64-gnu',
|
|
20
|
-
'darwin-x64': 'darwin-x64', 'darwin-arm64': 'darwin-arm64', 'win32-x64': 'win32-x64-msvc',
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Cargo writes its artifacts under CARGO_TARGET_DIR when that is set — a
|
|
24
|
-
// shared cache, a CI mount — so they are not under this package's `target/` at
|
|
25
|
-
// all. A relative value is resolved against the directory cargo ran in, which
|
|
26
|
-
// is this package root.
|
|
27
|
-
const targetDir = env.CARGO_TARGET_DIR
|
|
28
|
-
? resolve(root, env.CARGO_TARGET_DIR)
|
|
29
|
-
: join(root, 'target')
|
|
30
|
-
|
|
31
|
-
const triple = env.CARGO_BUILD_TARGET ?? ''
|
|
32
|
-
let suffix, os, releaseDir
|
|
33
|
-
if (triple) {
|
|
34
|
-
const entry = tripleMap[triple]
|
|
35
|
-
if (!entry) throw new Error(`${TAG} unsupported CARGO_BUILD_TARGET: ${triple}`)
|
|
36
|
-
suffix = entry.suffix; os = entry.os
|
|
37
|
-
releaseDir = join(targetDir, triple, 'release')
|
|
38
|
-
} else {
|
|
39
|
-
suffix = hostSuffixMap[`${platform}-${arch}`]; os = platform
|
|
40
|
-
releaseDir = join(targetDir, 'release')
|
|
41
|
-
if (!suffix) throw new Error(`${TAG} unsupported platform/arch: ${platform}-${arch}`)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const candidates =
|
|
45
|
-
os === 'win32'
|
|
46
|
-
? [join(releaseDir, `${CRATE}.dll`), join(releaseDir, `lib${CRATE}.dll`)]
|
|
47
|
-
: os === 'darwin'
|
|
48
|
-
? [join(releaseDir, `lib${CRATE}.dylib`)]
|
|
49
|
-
: [join(releaseDir, `lib${CRATE}.so`)]
|
|
50
|
-
|
|
51
|
-
const source = candidates.find((candidate) => existsSync(candidate))
|
|
52
|
-
if (!source) throw new Error(`${TAG} native library not found. Looked for:\n${candidates.map((p) => `- ${p}`).join('\n')}`)
|
|
53
|
-
|
|
54
|
-
const target = join(root, `index.${suffix}.node`)
|
|
55
|
-
copyFileSync(source, target)
|
|
56
|
-
console.log(`${TAG} copied ${source} -> ${target}`)
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Generate the TypeScript surface of the native module FROM the Rust.
|
|
4
|
-
*
|
|
5
|
-
* `napi-derive`'s `type-def` feature emits one JSON line per exported item
|
|
6
|
-
* while cargo compiles. Those lines are the source of truth: they are derived
|
|
7
|
-
* from the `#[napi]` items themselves, so a signature cannot drift from the
|
|
8
|
-
* Rust without this file changing.
|
|
9
|
-
*
|
|
10
|
-
* Hand-written interfaces are what this replaces, and they do drift — nothing
|
|
11
|
-
* on the TypeScript side notices when a `pub fn` gains a parameter or stops
|
|
12
|
-
* being `async`.
|
|
13
|
-
*
|
|
14
|
-
* One thing napi-rs cannot infer: the shape of a `JsFunction` callback. It
|
|
15
|
-
* emits `(...args: any[]) => any`. Any such parameter is refined below, by
|
|
16
|
-
* name, and the script fails if a refinement no longer matches anything — so
|
|
17
|
-
* the table cannot rot either.
|
|
18
|
-
*
|
|
19
|
-
* TYPE_DEF_TMP_PATH=<file> cargo build -p <crate>
|
|
20
|
-
* node scripts/generate-napi-types.mjs <file> <out.d.ts>
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { readFileSync, writeFileSync } from 'node:fs'
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Callback signatures napi-rs erases to `any`, restored here.
|
|
27
|
-
*
|
|
28
|
-
* Keyed by `<owner>.<method>`; the value replaces the whole parameter. Every
|
|
29
|
-
* entry must match, or the script fails: a stale refinement is how a hand
|
|
30
|
-
* annotation quietly stops describing the Rust.
|
|
31
|
-
*
|
|
32
|
-
* Empty while this crate exposes no `JsFunction` parameter — add an entry the
|
|
33
|
-
* day one appears, rather than letting an `any` through.
|
|
34
|
-
*/
|
|
35
|
-
const CALLBACK_REFINEMENTS = {}
|
|
36
|
-
|
|
37
|
-
const [input, output] = process.argv.slice(2)
|
|
38
|
-
if (!input || !output) {
|
|
39
|
-
console.error('usage: generate-napi-types.mjs <type-def file> <out.d.ts>')
|
|
40
|
-
process.exit(2)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const entries = readFileSync(input, 'utf8')
|
|
44
|
-
.split('\n')
|
|
45
|
-
.map((l) => l.trim())
|
|
46
|
-
.filter(Boolean)
|
|
47
|
-
.map((l) => JSON.parse(l))
|
|
48
|
-
|
|
49
|
-
const used = new Set()
|
|
50
|
-
|
|
51
|
-
/** Apply the refinements that belong to `owner`, tracking which ones matched. */
|
|
52
|
-
function refine(owner, body) {
|
|
53
|
-
let out = body
|
|
54
|
-
for (const [key, { from, to }] of Object.entries(CALLBACK_REFINEMENTS)) {
|
|
55
|
-
const [entryOwner] = key.split('.')
|
|
56
|
-
if (entryOwner !== owner) continue
|
|
57
|
-
if (out.includes(from)) {
|
|
58
|
-
out = out.replace(from, to)
|
|
59
|
-
used.add(key)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return out
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* JSDoc as emitted by napi-derive, indented to sit above its member.
|
|
67
|
-
*
|
|
68
|
-
* `*` followed by `/` inside the text closes the comment early — a Rust doc
|
|
69
|
-
* example holding a cron expression (`0 */5 * * *`) is enough to do it, and
|
|
70
|
-
* the generated file then fails to parse. Escaped rather than stripped, so the
|
|
71
|
-
* example still reads correctly.
|
|
72
|
-
*/
|
|
73
|
-
function docBlock(doc, indent = '') {
|
|
74
|
-
if (!doc) return ''
|
|
75
|
-
// Escape every `*/` EXCEPT the one that closes the block. A Rust doc example
|
|
76
|
-
// holding a cron expression (`0 */5 * * *`) closes the comment early
|
|
77
|
-
// otherwise, and the generated file stops parsing — but escaping the closer
|
|
78
|
-
// too breaks it just as thoroughly, whether the block spans one line or many.
|
|
79
|
-
const closer = doc.lastIndexOf('*/')
|
|
80
|
-
const escaped =
|
|
81
|
-
closer === -1
|
|
82
|
-
? doc
|
|
83
|
-
: doc.slice(0, closer).replaceAll('*/', '*\\/') + doc.slice(closer)
|
|
84
|
-
return (
|
|
85
|
-
escaped
|
|
86
|
-
.split('\n')
|
|
87
|
-
.filter((l) => l.length > 0)
|
|
88
|
-
.map((l) => `${indent}${l}`)
|
|
89
|
-
.join('\n') + '\n'
|
|
90
|
-
)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const interfaces = entries.filter((e) => e.kind === 'interface')
|
|
94
|
-
const structs = entries.filter((e) => e.kind === 'struct')
|
|
95
|
-
const impls = new Map(entries.filter((e) => e.kind === 'impl').map((e) => [e.name, e]))
|
|
96
|
-
const fns = entries.filter((e) => e.kind === 'fn')
|
|
97
|
-
|
|
98
|
-
const out = [
|
|
99
|
-
'// GENERATED FROM THE RUST — do not edit.',
|
|
100
|
-
'//',
|
|
101
|
-
"// Produced by scripts/generate-napi-types.mjs from napi-derive's type-def",
|
|
102
|
-
'// output. Editing this file by hand puts it back where it started: a',
|
|
103
|
-
'// description that can disagree with the code it describes.',
|
|
104
|
-
'',
|
|
105
|
-
]
|
|
106
|
-
|
|
107
|
-
for (const iface of interfaces) {
|
|
108
|
-
out.push(docBlock(iface.js_doc))
|
|
109
|
-
out.push(`export interface ${iface.name} {`)
|
|
110
|
-
for (const line of iface.def.split('\n')) {
|
|
111
|
-
out.push(line ? ` ${line.trim()}` : '')
|
|
112
|
-
}
|
|
113
|
-
out.push('}', '')
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
for (const struct of structs) {
|
|
117
|
-
const impl = impls.get(struct.name)
|
|
118
|
-
out.push(docBlock(struct.js_doc))
|
|
119
|
-
out.push(`export declare class ${struct.name} {`)
|
|
120
|
-
if (impl) {
|
|
121
|
-
for (const line of refine(struct.name, impl.def).split('\n')) {
|
|
122
|
-
out.push(line ? ` ${line.trim()}` : '')
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
out.push('}', '')
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
for (const fn of fns) {
|
|
129
|
-
out.push(docBlock(fn.js_doc))
|
|
130
|
-
// napi-derive emits the whole declaration for a function, unlike a struct
|
|
131
|
-
// where `def` holds only the members.
|
|
132
|
-
const declaration = fn.def.trim()
|
|
133
|
-
// napi-derive 3 emits a bare `function name(...)` where 2 emitted the
|
|
134
|
-
// signature after the name; concatenating onto the former produced
|
|
135
|
-
// `function xfunction x(...)`.
|
|
136
|
-
const rendered = declaration.startsWith('export declare function')
|
|
137
|
-
? declaration
|
|
138
|
-
: declaration.startsWith('function ')
|
|
139
|
-
? `export declare ${declaration}`
|
|
140
|
-
: `export declare function ${fn.name}${declaration}`
|
|
141
|
-
out.push(`${rendered};`, '')
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const stale = Object.keys(CALLBACK_REFINEMENTS).filter((k) => !used.has(k))
|
|
145
|
-
if (stale.length > 0) {
|
|
146
|
-
console.error(
|
|
147
|
-
`[napi-types] refinement(s) that matched nothing: ${stale.join(', ')}\n` +
|
|
148
|
-
'[napi-types] the Rust changed under them — update or remove the entry.',
|
|
149
|
-
)
|
|
150
|
-
process.exit(1)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
writeFileSync(output, out.join('\n'))
|
|
154
|
-
console.log(
|
|
155
|
-
`[napi-types] ${output} — ${interfaces.length} interface(s), ${structs.length} class(es), ${fns.length} function(s)`,
|
|
156
|
-
)
|
package/scripts/verify-napi.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs'
|
|
2
|
-
import { createRequire } from 'node:module'
|
|
3
|
-
import { dirname, join } from 'node:path'
|
|
4
|
-
import { arch, platform } from 'node:process'
|
|
5
|
-
import { fileURLToPath } from 'node:url'
|
|
6
|
-
|
|
7
|
-
const here = dirname(fileURLToPath(import.meta.url))
|
|
8
|
-
const root = join(here, '..')
|
|
9
|
-
|
|
10
|
-
const suffixMap = {
|
|
11
|
-
'linux-x64': 'linux-x64-gnu',
|
|
12
|
-
'linux-arm64': 'linux-arm64-gnu',
|
|
13
|
-
'darwin-x64': 'darwin-x64',
|
|
14
|
-
'darwin-arm64': 'darwin-arm64',
|
|
15
|
-
'win32-x64': 'win32-x64-msvc',
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const suffix = suffixMap[`${platform}-${arch}`]
|
|
19
|
-
if (!suffix) {
|
|
20
|
-
throw new Error(`[chronos:napi] unsupported platform/arch: ${platform}-${arch}`)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const binary = join(root, `index.${suffix}.node`)
|
|
24
|
-
if (!existsSync(binary)) {
|
|
25
|
-
throw new Error(`[chronos:napi] binary missing: ${binary}`)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const require2 = createRequire(import.meta.url)
|
|
29
|
-
const binding = require2(binary)
|
|
30
|
-
|
|
31
|
-
for (const fn of ['add', 'diff', 'startOf', 'endOf', 'format', 'rruleExpand']) {
|
|
32
|
-
if (typeof binding[fn] !== 'function') {
|
|
33
|
-
throw new Error(`[chronos:napi] invalid exports: missing ${fn}()`)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (binding.add('2026-01-15T10:00:00Z', 1, 'month') !== '2026-02-15T10:00:00Z') {
|
|
38
|
-
throw new Error('[chronos:napi] add smoke test failed')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const recurring = binding.rruleExpand('2026-01-15T15:00:00Z', 'FREQ=MONTHLY;BYMONTHDAY=15;COUNT=3', 10)
|
|
42
|
-
if (!Array.isArray(recurring) || recurring.length !== 3) {
|
|
43
|
-
throw new Error('[chronos:napi] rrule smoke test failed')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
console.log('[chronos:napi] smoke test passed')
|
package/scripts/verify-wasm.mjs
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { execFileSync } from 'node:child_process'
|
|
2
|
-
import { existsSync, statSync } from 'node:fs'
|
|
3
|
-
import { dirname, join } from 'node:path'
|
|
4
|
-
import { fileURLToPath } from 'node:url'
|
|
5
|
-
|
|
6
|
-
// Publish-time gate: the `wasm-pack build --target web` output (JS glue + the
|
|
7
|
-
// binary) MUST be in the package. Without it the browser loader
|
|
8
|
-
// (src/native.ts) throws CHRONOS_ENGINE_REQUIRED at runtime — exactly the
|
|
9
|
-
// "stub-only tarball" failure this gate exists to prevent. Only the hand-written
|
|
10
|
-
// `chronos_engine_wasm.d.ts` typecheck stub is committed; the .js/.wasm are built.
|
|
11
|
-
|
|
12
|
-
const here = dirname(fileURLToPath(import.meta.url))
|
|
13
|
-
const wasmDir = join(here, '..', 'wasm')
|
|
14
|
-
|
|
15
|
-
const required = ['chronos_engine_wasm.js', 'chronos_engine_wasm_bg.wasm']
|
|
16
|
-
for (const name of required) {
|
|
17
|
-
const p = join(wasmDir, name)
|
|
18
|
-
if (!existsSync(p)) {
|
|
19
|
-
throw new Error(
|
|
20
|
-
`[chronos:wasm] missing build artifact: wasm/${name} — run \`pnpm build:wasm\` (wasm-pack) before publishing`,
|
|
21
|
-
)
|
|
22
|
-
}
|
|
23
|
-
if (statSync(p).size === 0) {
|
|
24
|
-
throw new Error(`[chronos:wasm] empty build artifact: wasm/${name}`)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Present on disk is NOT the same question as present in the package, and it is
|
|
29
|
-
// the weaker one. `wasm-pack` writes a `.gitignore` of `*` into its out-dir, and
|
|
30
|
-
// npm honours a .gitignore nested inside a published directory even when `files`
|
|
31
|
-
// lists that directory — so every check above can pass while the tarball ships
|
|
32
|
-
// nothing. That is exactly how 0.1.13 went out: green gate, empty wasm/.
|
|
33
|
-
//
|
|
34
|
-
// So ask the packer. `--ignore-scripts` keeps this from re-entering
|
|
35
|
-
// prepublishOnly, which is what invoked us.
|
|
36
|
-
const packed = JSON.parse(
|
|
37
|
-
execFileSync('npm', ['pack', '--dry-run', '--json', '--ignore-scripts'], {
|
|
38
|
-
cwd: join(here, '..'),
|
|
39
|
-
encoding: 'utf-8',
|
|
40
|
-
stdio: ['ignore', 'pipe', 'ignore'],
|
|
41
|
-
}),
|
|
42
|
-
)[0]
|
|
43
|
-
|
|
44
|
-
const shipped = new Set(packed.files.map((f) => f.path))
|
|
45
|
-
for (const name of required) {
|
|
46
|
-
if (!shipped.has(`wasm/${name}`)) {
|
|
47
|
-
throw new Error(
|
|
48
|
-
`[chronos:wasm] wasm/${name} is on disk but NOT in the tarball — something is excluding it ` +
|
|
49
|
-
`(a .gitignore or .npmignore nested in wasm/ will do this even though "wasm" is in package.json files). ` +
|
|
50
|
-
`Run \`node scripts/clean-wasm-pack-meta.mjs\` after wasm-pack.`,
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
console.log(`[chronos:wasm] browser artifacts present and in the tarball (${shipped.size} files)`)
|
package/src/utils.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared helpers for DateTime and RRule modules.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Normalize an ISO 8601 string.
|
|
7
|
-
*
|
|
8
|
-
* Milliseconds are KEPT even when zero, so `toISO()` reads
|
|
9
|
-
* `2026-07-15T10:00:00.000Z` — what Luxon's `toISO()` returns, and therefore
|
|
10
|
-
* what `@adonisjs/lucid` puts in a serialized model. Chronos used to strip
|
|
11
|
-
* `.000`, which is Luxon's opt-in `toISO({ suppressMilliseconds: true })`:
|
|
12
|
-
* the same instant, a different literal, and a silent difference in every JSON
|
|
13
|
-
* payload carrying a date.
|
|
14
|
-
*/
|
|
15
|
-
export function normalizeIso(iso: string): string {
|
|
16
|
-
return iso;
|
|
17
|
-
}
|