mirror of
https://git.pumpin.online/funkpower/typikon.git
synced 2026-10-22 18:57:13 +00:00
javascript and typescript, as one wasm package for every runtime
WebAssembly rather than a native Node addon: one artifact serves Node, Deno, Bun, a browser and anything built on them, where napi would have meant a prebuilt binary per platform and a source build for whoever the matrix missed. The engine is pure computation and the menologion is already compiled in, so there is nothing for it to give up. A day comes back as a plain object, not a handle. A wasm class would have meant a .free() that every caller eventually forgets, and this way JSON.stringify works and the thing spreads into state. Two serializer defaults had to be overridden to keep the promise that it is the same document --json emits: maps as objects, and an absent field as null rather than undefined, which stringify would otherwise drop entirely. The TypeScript is hand-written, because the generated types for a value crossing as JsValue are `any`, and a binding that types its whole return as `any` is a binding TypeScript cannot check. tsc --strict is a release gate now, the way mypy --strict already is, and the check includes three lines that must fail to compile. The one place this differs from every other binding: no remembered default jurisdiction, because a browser has nowhere to keep one. It is called out in the README, in the types and in docs/versioning.md. The test resolves typikon through a staged node_modules rather than a relative path, so what it exercises is the exports map, and it compares a year of days field by field against the command line for four jurisdictions.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
# typikon
|
||||
|
||||
The Orthodox liturgical day: feasts, seasons and the fast, computed rather
|
||||
than fetched. No network, no data files, no service to be up.
|
||||
|
||||
```sh
|
||||
npm install typikon
|
||||
```
|
||||
|
||||
```js
|
||||
import { today } from "typikon";
|
||||
|
||||
const day = today({ jurisdiction: "rocor" });
|
||||
|
||||
day.churchDate; // "5 September"
|
||||
day.fast; // "xerophagy"
|
||||
day.fastLabel; // "xerophagy — no oil or wine"
|
||||
day.emoji; // the glyph for the level
|
||||
day.title; // "The Holy Prophet Zachariah, father of the Forerunner"
|
||||
day.season; // null in ordinary time
|
||||
day.daysToPascha;
|
||||
```
|
||||
|
||||
In a browser or a bundler, load the module first:
|
||||
|
||||
```js
|
||||
import init, { today } from "typikon";
|
||||
|
||||
await init();
|
||||
const day = today({ jurisdiction: "rocor" });
|
||||
```
|
||||
|
||||
Node needs no `init`, because it can read the WebAssembly itself. The exports
|
||||
map picks the right build; `typikon/node` and `typikon/web` force one.
|
||||
|
||||
## What comes back
|
||||
|
||||
A plain object, not a handle: the same document `typikon --json` emits, with
|
||||
the same field names and the same stability promise. Nothing has to be freed
|
||||
and `JSON.stringify` works on it. TypeScript types ship with the package.
|
||||
|
||||
## The rest of it
|
||||
|
||||
```js
|
||||
import { describe, describeIso, week, pascha, jurisdictions } from "typikon";
|
||||
|
||||
describe(new Date(2026, 3, 12), { jurisdiction: "moscow" });
|
||||
describeIso("2026-04-12", { jurisdiction: "moscow" });
|
||||
|
||||
week(new Date(), 7, { jurisdiction: "goarch" }); // seven consecutive days
|
||||
pascha(2027, "old"); // 2027-05-02
|
||||
jurisdictions(); // [{ key, name, note }, ...]
|
||||
```
|
||||
|
||||
`week` reckons the year's movable cycle once rather than once a day, so it is
|
||||
the thing to call for a calendar grid.
|
||||
|
||||
## Which calendar
|
||||
|
||||
`jurisdiction` takes a church key, or a bare calendar where the church does not
|
||||
matter: `"old"` (Julian throughout), `"new"` (Revised Julian fixed feasts with
|
||||
the Julian Paschalion, the default) or `"western"` (Gregorian throughout,
|
||||
Pascha included). `jurisdictions()` lists the rest. Two more options:
|
||||
|
||||
- `tradition`, `"slavic"` or `"greek"`, overriding the church's own usage. It
|
||||
moves fish in the Nativity Fast and the rank of the Protection.
|
||||
- `typikon: true`, to show a day at the Typikon's full discipline rather than
|
||||
at the minimum a church's own particular law requires. What binds is reported
|
||||
either way, in `binds` and `obligation`. This is for the Byzantine Catholic
|
||||
churches, whose law asks very much less than the tradition they keep.
|
||||
|
||||
Unlike the command line and the Python bindings, there is no remembered
|
||||
default: a browser has nowhere to keep one, so the default is the New Calendar
|
||||
and anything else is passed.
|
||||
|
||||
## Accuracy
|
||||
|
||||
The Paschalion is exact. The fast follows the Typikon as the standard printed
|
||||
calendars apply it, which is the part where careful people differ; a parish
|
||||
calendar or a confessor overrides this, and neither is a bug report.
|
||||
|
||||
## Licence
|
||||
|
||||
GPL-2.0-only. Source: https://git.pumpin.online/funkpower/typikon
|
||||
Executable
+55
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build the npm package into npm/dist.
|
||||
#
|
||||
# npm/build.sh # needs wasm-bindgen-cli at the version below
|
||||
# WASM_BINDGEN=/path/to/wasm-bindgen npm/build.sh
|
||||
#
|
||||
# Two builds go in: `node/` for Node, which loads the module itself, and
|
||||
# `web/` for browsers and bundlers, which cannot compile WebAssembly
|
||||
# synchronously on the main thread and so has to be awaited. The exports map
|
||||
# in package.json picks between them.
|
||||
set -euo pipefail
|
||||
|
||||
root="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$root"
|
||||
|
||||
version="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)"
|
||||
bindgen="${WASM_BINDGEN:-wasm-bindgen}"
|
||||
dist="npm/dist"
|
||||
|
||||
command -v "$bindgen" >/dev/null || {
|
||||
echo "wasm-bindgen is not on PATH. cargo install wasm-bindgen-cli --version \
|
||||
$(sed -n 's/^wasm-bindgen = { version = "\(.*\)".*/\1/p' Cargo.toml)" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
cargo build --lib --release --target wasm32-unknown-unknown --features wasm
|
||||
|
||||
rm -rf "$dist"
|
||||
for target in nodejs web; do
|
||||
out="$dist/$([ "$target" = nodejs ] && echo node || echo web)"
|
||||
"$bindgen" --target "$target" --out-dir "$out" --out-name typikon \
|
||||
target/wasm32-unknown-unknown/release/typikon.wasm
|
||||
# wasm-bindgen writes an overridden parameter type without the optional
|
||||
# marker, so an options argument nobody wants to pass has to be passed as
|
||||
# `undefined`. The override is what gives the argument a real type at all;
|
||||
# this restores the `?` it costs.
|
||||
sed -i 's/options: Options/options?: Options/g' "$out/typikon.d.ts"
|
||||
done
|
||||
|
||||
# wasm-opt, where it is to hand. Worth about a fifth of the file.
|
||||
if command -v wasm-opt >/dev/null; then
|
||||
for w in "$dist"/*/typikon_bg.wasm; do
|
||||
wasm-opt -Oz -o "$w.opt" "$w" && mv "$w.opt" "$w"
|
||||
done
|
||||
fi
|
||||
|
||||
sed "s/@VERSION@/$version/" npm/package.json.in > "$dist/package.json"
|
||||
# The package is ESM; the Node build is what wasm-bindgen emits for Node, which
|
||||
# is CommonJS. One file says so for that directory alone.
|
||||
echo '{ "type": "commonjs" }' > "$dist/node/package.json"
|
||||
cp npm/README.md "$dist/README.md"
|
||||
cp LICENSE "$dist/LICENSE"
|
||||
|
||||
echo "built $dist, version $version"
|
||||
du -h "$dist"/*/typikon_bg.wasm
|
||||
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"name": "typikon",
|
||||
"version": "@VERSION@",
|
||||
"description": "The Orthodox liturgical day: feasts, seasons and the fast, computed rather than fetched",
|
||||
"license": "GPL-2.0-only",
|
||||
"homepage": "https://git.pumpin.online/funkpower/typikon",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://git.pumpin.online/funkpower/typikon.git"
|
||||
},
|
||||
"keywords": [
|
||||
"orthodox",
|
||||
"liturgical",
|
||||
"calendar",
|
||||
"byzantine",
|
||||
"pascha",
|
||||
"fasting",
|
||||
"wasm"
|
||||
],
|
||||
"type": "module",
|
||||
"types": "./web/typikon.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"node": {
|
||||
"types": "./node/typikon.d.ts",
|
||||
"default": "./node/typikon.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./web/typikon.d.ts",
|
||||
"default": "./web/typikon.js"
|
||||
}
|
||||
},
|
||||
"./node": {
|
||||
"types": "./node/typikon.d.ts",
|
||||
"default": "./node/typikon.js"
|
||||
},
|
||||
"./web": {
|
||||
"types": "./web/typikon.d.ts",
|
||||
"default": "./web/typikon.js"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"files": [
|
||||
"node/",
|
||||
"web/",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
]
|
||||
}
|
||||
+218
@@ -0,0 +1,218 @@
|
||||
// The npm package, exercised as a consumer would use it, against the same
|
||||
// answers the command line gives.
|
||||
//
|
||||
// npm/build.sh && node npm/test.mjs
|
||||
//
|
||||
// It imports by package path through a temporary node_modules link, so what is
|
||||
// tested is the exports map and the built files, not a lucky relative import.
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdtempSync, symlinkSync, mkdirSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, dirname, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const root = resolve(here, "..");
|
||||
const cli = join(root, "target", "release", "typikon");
|
||||
|
||||
const stage = mkdtempSync(join(tmpdir(), "typikon-npm-"));
|
||||
mkdirSync(join(stage, "node_modules"));
|
||||
symlinkSync(join(root, "npm", "dist"), join(stage, "node_modules", "typikon"), "dir");
|
||||
|
||||
// Resolve "typikon" from inside the staged directory, so the exports map is
|
||||
// what decides which build loads.
|
||||
const { createRequire } = await import("node:module");
|
||||
const req = createRequire(join(stage, "probe.js"));
|
||||
const entry = req.resolve("typikon");
|
||||
const typikon = await import(pathToFileURL(entry).href);
|
||||
|
||||
let failures = 0;
|
||||
function check(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(` ok ${name}`);
|
||||
} catch (e) {
|
||||
failures++;
|
||||
console.log(` FAIL ${name}\n ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`typikon ${typikon.version()} from ${entry}`);
|
||||
|
||||
check("node resolves to the node build", () => {
|
||||
assert.match(entry, /dist\/node\/typikon\.js$/);
|
||||
});
|
||||
|
||||
check("a day is a plain object, not a handle", () => {
|
||||
const day = typikon.describeIso("2026-04-12", { jurisdiction: "moscow" });
|
||||
assert.equal(Object.getPrototypeOf(day), Object.prototype);
|
||||
assert.equal(typeof day.free, "undefined");
|
||||
assert.doesNotThrow(() => JSON.stringify(day));
|
||||
});
|
||||
|
||||
check("it answers what the command line answers", () => {
|
||||
for (const [iso, jurisdiction] of [
|
||||
["2026-04-12", "moscow"],
|
||||
["2026-01-07", "goarch"],
|
||||
["2026-02-16", "ruthenian"],
|
||||
["2025-12-25", "old"],
|
||||
["2026-08-15", "antiochian"],
|
||||
]) {
|
||||
const expected = JSON.parse(
|
||||
execFileSync(cli, ["--date", iso, "--jurisdiction", jurisdiction, "--json"], {
|
||||
encoding: "utf8",
|
||||
env: { ...process.env, XDG_CONFIG_HOME: stage, XDG_DATA_HOME: stage },
|
||||
}),
|
||||
);
|
||||
const got = typikon.describeIso(iso, { jurisdiction });
|
||||
assert.deepEqual(got, expected, `${iso} ${jurisdiction}`);
|
||||
}
|
||||
});
|
||||
|
||||
check("a year of days matches the command line, day for day", () => {
|
||||
// Five dates prove the wiring; a year proves the engine crossed the boundary
|
||||
// whole. The CLI emits an array for --days, so this is one process per
|
||||
// jurisdiction rather than one per day.
|
||||
for (const [jurisdiction, opts] of [
|
||||
["moscow", {}],
|
||||
["goarch", {}],
|
||||
["ruthenian", { typikon: true }],
|
||||
["finnish", {}],
|
||||
]) {
|
||||
const args = ["--date", "2026-01-01", "--days", "365", "--jurisdiction", jurisdiction, "--json"];
|
||||
if (opts.typikon) args.push("--typikon");
|
||||
const expected = JSON.parse(
|
||||
execFileSync(cli, args, {
|
||||
encoding: "utf8",
|
||||
maxBuffer: 64 << 20,
|
||||
env: { ...process.env, XDG_CONFIG_HOME: stage, XDG_DATA_HOME: stage },
|
||||
}),
|
||||
).days; // a run of days is wrapped, a single day is not
|
||||
const got = typikon.week(new Date(2026, 0, 1), 365, { jurisdiction, ...opts });
|
||||
assert.equal(got.length, expected.length, jurisdiction);
|
||||
for (let i = 0; i < expected.length; i++) {
|
||||
assert.deepEqual(got[i], expected[i], `${jurisdiction} ${expected[i].date}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
check("a Date and an ISO string agree", () => {
|
||||
// JS months are zero-based, which is the single easiest thing to get wrong
|
||||
// on this boundary.
|
||||
const a = typikon.describe(new Date(2026, 3, 12), { jurisdiction: "moscow" });
|
||||
const b = typikon.describeIso("2026-04-12", { jurisdiction: "moscow" });
|
||||
assert.deepEqual(a, b);
|
||||
});
|
||||
|
||||
check("options are optional", () => {
|
||||
const day = typikon.today();
|
||||
assert.equal(day.jurisdiction, "new");
|
||||
});
|
||||
|
||||
check("a week is consecutive and matches day by day", () => {
|
||||
const days = typikon.week(new Date(2026, 3, 12), 7, { jurisdiction: "moscow" });
|
||||
assert.equal(days.length, 7);
|
||||
assert.equal(days[0].date, "2026-04-12");
|
||||
assert.equal(days[6].date, "2026-04-18");
|
||||
for (const d of days) {
|
||||
assert.deepEqual(d, typikon.describeIso(d.date, { jurisdiction: "moscow" }));
|
||||
}
|
||||
});
|
||||
|
||||
check("pascha matches the published dates", () => {
|
||||
const iso = (d) =>
|
||||
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
||||
assert.equal(iso(typikon.pascha(2026, "old")), "2026-04-12");
|
||||
assert.equal(iso(typikon.pascha(2026, "western")), "2026-04-05");
|
||||
assert.equal(iso(typikon.pascha(2027, "old")), "2027-05-02");
|
||||
});
|
||||
|
||||
check("every listed jurisdiction reckons", () => {
|
||||
const all = typikon.jurisdictions();
|
||||
assert.ok(all.length > 20);
|
||||
for (const j of all) {
|
||||
assert.equal(typeof j.key, "string");
|
||||
assert.equal(typeof j.name, "string");
|
||||
const day = typikon.describeIso("2026-04-12", { jurisdiction: j.key });
|
||||
assert.equal(day.jurisdiction, j.key);
|
||||
}
|
||||
});
|
||||
|
||||
check("the discipline flag moves what is shown but not what binds", () => {
|
||||
const law = typikon.describeIso("2026-03-04", { jurisdiction: "ruthenian", typikon: false });
|
||||
const full = typikon.describeIso("2026-03-04", { jurisdiction: "ruthenian", typikon: true });
|
||||
assert.notEqual(law.fast, full.fast);
|
||||
assert.equal(law.binds, full.binds);
|
||||
});
|
||||
|
||||
check("the usage can be overridden", () => {
|
||||
const greek = typikon.describeIso("2025-12-09", { jurisdiction: "old", tradition: "greek" });
|
||||
const slavic = typikon.describeIso("2025-12-09", { jurisdiction: "old", tradition: "slavic" });
|
||||
assert.equal(greek.tradition, "greek");
|
||||
assert.equal(slavic.tradition, "slavic");
|
||||
assert.notEqual(greek.fast, slavic.fast);
|
||||
});
|
||||
|
||||
check("a jurisdiction nobody keeps throws", () => {
|
||||
assert.throws(() => typikon.describeIso("2026-04-12", { jurisdiction: "shire" }), /shire/);
|
||||
});
|
||||
|
||||
check("a date outside the years throws", () => {
|
||||
assert.throws(() => typikon.describeIso("not-a-date"));
|
||||
assert.throws(() => typikon.describe(new Date(-5000, 0, 1)));
|
||||
});
|
||||
|
||||
check("the web build is there", () => {
|
||||
assert.match(req.resolve("typikon/web"), /dist\/web\/typikon\.js$/);
|
||||
});
|
||||
|
||||
// The web build, driven the way a browser drives it. Node can hand it the
|
||||
// bytes, which is enough to prove the glue and the module agree; what it
|
||||
// cannot prove is that a browser will fetch them, and no test here can.
|
||||
{
|
||||
const webEntry = req.resolve("typikon/web");
|
||||
const web = await import(pathToFileURL(webEntry).href);
|
||||
const { readFileSync } = await import("node:fs");
|
||||
await web.default({
|
||||
module_or_path: readFileSync(join(dirname(webEntry), "typikon_bg.wasm")),
|
||||
});
|
||||
check("the web build answers what the node build answers", () => {
|
||||
assert.deepEqual(
|
||||
web.describeIso("2026-04-12", { jurisdiction: "moscow" }),
|
||||
typikon.describeIso("2026-04-12", { jurisdiction: "moscow" }),
|
||||
);
|
||||
assert.equal(web.version(), typikon.version());
|
||||
});
|
||||
}
|
||||
|
||||
// TypeScript, if it is to hand. The Python bindings are gated on `mypy
|
||||
// --strict`; this is the same gate, and the same reason: a published type is a
|
||||
// promise, and a promise nobody checks drifts.
|
||||
{
|
||||
const tsc = process.env.TSC ?? "tsc";
|
||||
let have = true;
|
||||
try {
|
||||
execFileSync(tsc, ["--version"], { stdio: "ignore" });
|
||||
} catch {
|
||||
have = false;
|
||||
console.log(" skip typescript --strict (no tsc on PATH; set TSC=)");
|
||||
}
|
||||
if (have) {
|
||||
const { copyFileSync } = await import("node:fs");
|
||||
copyFileSync(join(here, "typecheck.ts"), join(stage, "typecheck.ts"));
|
||||
check("the published types pass tsc --strict", () => {
|
||||
execFileSync(
|
||||
tsc,
|
||||
["--strict", "--noEmit", "--target", "es2022", "--module", "nodenext",
|
||||
"--moduleResolution", "nodenext", "typecheck.ts"],
|
||||
{ cwd: stage, stdio: "pipe", encoding: "utf8" },
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
rmSync(stage, { recursive: true, force: true });
|
||||
console.log(failures ? `\n${failures} failed` : "\nall passed");
|
||||
process.exit(failures ? 1 : 0);
|
||||
@@ -0,0 +1,44 @@
|
||||
// The published types, checked the way a TypeScript consumer would meet them.
|
||||
// Run by npm/test.mjs. Nothing here is executed; the point is that `tsc
|
||||
// --strict` accepts it, and rejects the lines below that it should.
|
||||
import {
|
||||
describe,
|
||||
describeIso,
|
||||
today,
|
||||
week,
|
||||
pascha,
|
||||
jurisdictions,
|
||||
version,
|
||||
type Day,
|
||||
type Options,
|
||||
type JurisdictionInfo,
|
||||
} from "typikon";
|
||||
|
||||
const opts: Options = { jurisdiction: "rocor", tradition: "slavic", typikon: true };
|
||||
|
||||
const day: Day = describeIso("2026-04-12", opts);
|
||||
const byDate: Day = describe(new Date(), { jurisdiction: "moscow" });
|
||||
const now: Day = today();
|
||||
|
||||
const churchDate: string = day.churchDate;
|
||||
const season: string | null = day.season;
|
||||
const commemorations: string[] = day.commemorations;
|
||||
const daysToPascha: number = day.daysToPascha;
|
||||
|
||||
const seven: Day[] = week(new Date(), 7, opts);
|
||||
const p: Date = pascha(2027, "old");
|
||||
const all: JurisdictionInfo[] = jurisdictions();
|
||||
const v: string = version();
|
||||
|
||||
// Referenced so nothing is unused, which --strict would otherwise let pass but
|
||||
// a linter would not.
|
||||
export const _ = { byDate, now, churchDate, season, commemorations, daysToPascha, seven, p, all, v };
|
||||
|
||||
// @ts-expect-error a season may be absent, so it is not a plain string
|
||||
const wrong: string = day.season;
|
||||
// @ts-expect-error there is no such option
|
||||
const badOption: Options = { jurisdictionn: "rocor" };
|
||||
// @ts-expect-error days is a number
|
||||
const badWeek = week(new Date(), "7", opts);
|
||||
|
||||
export const __ = { wrong, badOption, badWeek };
|
||||
Reference in New Issue
Block a user