@c9up/chronos 0.1.15 → 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.
Files changed (74) hide show
  1. package/README.md +136 -16
  2. package/dist/DateTime.d.ts +209 -8
  3. package/dist/DateTime.d.ts.map +1 -1
  4. package/dist/DateTime.js +815 -116
  5. package/dist/DateTime.js.map +1 -1
  6. package/dist/Duration.d.ts +37 -20
  7. package/dist/Duration.d.ts.map +1 -1
  8. package/dist/Duration.js +287 -120
  9. package/dist/Duration.js.map +1 -1
  10. package/dist/Interval.d.ts +50 -7
  11. package/dist/Interval.d.ts.map +1 -1
  12. package/dist/Interval.js +142 -29
  13. package/dist/Interval.js.map +1 -1
  14. package/dist/atlas.d.ts +11 -15
  15. package/dist/atlas.d.ts.map +1 -1
  16. package/dist/atlas.js +22 -17
  17. package/dist/atlas.js.map +1 -1
  18. package/dist/format.d.ts +47 -0
  19. package/dist/format.d.ts.map +1 -0
  20. package/dist/format.js +387 -0
  21. package/dist/format.js.map +1 -0
  22. package/dist/holidays.d.ts +43 -0
  23. package/dist/holidays.d.ts.map +1 -0
  24. package/dist/holidays.js +102 -0
  25. package/dist/holidays.js.map +1 -0
  26. package/dist/index.d.ts +8 -6
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +4 -3
  29. package/dist/index.js.map +1 -1
  30. package/dist/native/generated.d.ts +11 -2
  31. package/dist/native/generated.d.ts.map +1 -1
  32. package/dist/native.d.ts.map +1 -1
  33. package/dist/native.js +5 -2
  34. package/dist/native.js.map +1 -1
  35. package/dist/parse.d.ts +59 -0
  36. package/dist/parse.d.ts.map +1 -0
  37. package/dist/parse.js +404 -0
  38. package/dist/parse.js.map +1 -0
  39. package/dist/rrule.d.ts +34 -2
  40. package/dist/rrule.d.ts.map +1 -1
  41. package/dist/rrule.js +156 -32
  42. package/dist/rrule.js.map +1 -1
  43. package/index.darwin-arm64.node +0 -0
  44. package/index.darwin-x64.node +0 -0
  45. package/index.linux-arm64-gnu.node +0 -0
  46. package/index.linux-x64-gnu.node +0 -0
  47. package/index.win32-x64-msvc.node +0 -0
  48. package/package.json +2 -3
  49. package/src/DateTime.ts +1089 -124
  50. package/src/Duration.ts +318 -139
  51. package/src/Interval.ts +160 -53
  52. package/src/atlas.ts +23 -17
  53. package/src/format.ts +449 -0
  54. package/src/holidays.ts +161 -0
  55. package/src/index.ts +35 -7
  56. package/src/native/generated.ts +27 -2
  57. package/src/native.ts +7 -3
  58. package/src/parse.ts +559 -0
  59. package/src/rrule.ts +229 -38
  60. package/wasm/chronos_engine_wasm.d.ts +13 -4
  61. package/wasm/chronos_engine_wasm.js +87 -16
  62. package/wasm/chronos_engine_wasm_bg.wasm +0 -0
  63. package/wasm/chronos_engine_wasm_bg.wasm.d.ts +5 -2
  64. package/dist/utils.d.ts +0 -15
  65. package/dist/utils.d.ts.map +0 -1
  66. package/dist/utils.js +0 -17
  67. package/dist/utils.js.map +0 -1
  68. package/scripts/build-napi-types.mjs +0 -69
  69. package/scripts/clean-wasm-pack-meta.mjs +0 -32
  70. package/scripts/copy-napi.mjs +0 -56
  71. package/scripts/generate-napi-types.mjs +0 -156
  72. package/scripts/verify-napi.mjs +0 -46
  73. package/scripts/verify-wasm.mjs +0 -55
  74. package/src/utils.ts +0 -17
@@ -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
- )
@@ -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')
@@ -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
- }