decklight 0.2.0 → 0.4.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/README.md +69 -31
- package/SPEC.md +881 -80
- package/cli/agents.mjs +116 -10
- package/cli/associate.mjs +286 -0
- package/cli/audit.mjs +424 -0
- package/cli/bundle.mjs +174 -46
- package/cli/deckfile.mjs +233 -0
- package/cli/decklight.mjs +201 -8
- package/cli/dev.mjs +244 -55
- package/cli/edit.mjs +577 -107
- package/cli/extension.mjs +108 -0
- package/cli/git.mjs +141 -0
- package/cli/import.mjs +398 -0
- package/cli/init.mjs +421 -116
- package/cli/loader.mjs +263 -0
- package/cli/marketplace.mjs +911 -0
- package/cli/pdf.mjs +156 -0
- package/cli/pkg.mjs +69 -0
- package/cli/plugin.mjs +654 -0
- package/cli/port-conflict.mjs +94 -0
- package/cli/present.mjs +564 -0
- package/cli/publish.mjs +342 -0
- package/cli/qr.mjs +334 -0
- package/cli/rec.mjs +5 -8
- package/cli/remote.mjs +149 -0
- package/cli/report-bug.mjs +97 -0
- package/cli/restore.mjs +174 -0
- package/cli/serve.mjs +247 -0
- package/cli/sign.mjs +216 -0
- package/cli/skill-content.mjs +222 -0
- package/cli/skills.mjs +338 -0
- package/cli/supervise.mjs +57 -0
- package/cli/theme.mjs +219 -0
- package/cli/units.mjs +500 -0
- package/cli/upgrade.mjs +250 -0
- package/cli/util.mjs +90 -0
- package/cli/wizard.mjs +432 -0
- package/cli/zip.mjs +121 -0
- package/dist/decklight.css +716 -29
- package/dist/decklight.js +198 -125
- package/dist/decklight.js.map +4 -4
- package/docs/architecture.svg +1 -1
- package/package.json +7 -5
- package/themes/README.md +6 -4
- package/themes/aurora.css +1 -1
- package/themes/berry.css +1 -1
- package/themes/carbon.css +1 -1
- package/themes/citrus.css +1 -1
- package/themes/coastal.css +1 -1
- package/themes/cosmos.css +1 -1
- package/themes/dune.css +1 -1
- package/themes/eclipse.css +1 -1
- package/themes/ember.css +1 -1
- package/themes/fjord.css +1 -1
- package/themes/gallery.html +3 -11
- package/themes/glacier.css +1 -1
- package/themes/graphite.css +1 -1
- package/themes/harvest.css +1 -1
- package/themes/ibm-modern.css +1 -1
- package/themes/ink.css +1 -1
- package/themes/latte.css +1 -1
- package/themes/linen.css +1 -1
- package/themes/meadow.css +1 -1
- package/themes/metropolis.css +1 -1
- package/themes/midnight.css +1 -1
- package/themes/mint.css +1 -1
- package/themes/moss.css +1 -1
- package/themes/obsidian.css +1 -1
- package/themes/orchid.css +1 -1
- package/themes/packs.json +2 -30
- package/themes/paper.css +1 -1
- package/themes/peony.css +1 -1
- package/themes/porcelain.css +1 -1
- package/themes/reveal-beige.css +1 -1
- package/themes/reveal-black.css +1 -1
- package/themes/reveal-blood.css +1 -1
- package/themes/reveal-dracula.css +1 -1
- package/themes/reveal-league.css +1 -1
- package/themes/reveal-moon.css +1 -1
- package/themes/reveal-night.css +1 -1
- package/themes/reveal-serif.css +1 -1
- package/themes/reveal-simple.css +1 -1
- package/themes/reveal-sky.css +1 -1
- package/themes/reveal-solarized.css +1 -1
- package/themes/reveal-white.css +1 -1
- package/themes/sepia.css +1 -1
- package/themes/seriph.css +1 -1
- package/themes/slate.css +1 -1
- package/themes/storm.css +1 -1
- package/themes/synthwave.css +1 -1
- package/themes/velvet.css +1 -1
- package/themes/yamabuki.css +1 -1
- package/tools/args.mjs +32 -0
- package/tools/bridge.mjs +30 -0
- package/tools/chrome.mjs +46 -11
- package/tools/color.mjs +110 -0
- package/tools/deck-html.mjs +138 -0
- package/tools/elevenlabs-tts.mjs +248 -0
- package/tools/escape.mjs +40 -0
- package/tools/extension-check-runner.mjs +41 -0
- package/tools/extension-check.mjs +266 -0
- package/tools/gemini-tts.mjs +25 -3
- package/tools/lipsync-engines.mjs +81 -0
- package/tools/lipsync-server.mjs +78 -37
- package/tools/lipsync.mjs +36 -21
- package/tools/local-voice.mjs +209 -0
- package/tools/ooxml.mjs +151 -0
- package/tools/pptx.mjs +320 -0
- package/tools/publish-site-voices.mjs +2 -1
- package/tools/publish-targets.mjs +143 -0
- package/tools/publish-voices.mjs +230 -0
- package/tools/shot.mjs +91 -47
- package/tools/theme-check.mjs +144 -0
- package/tools/tts-engines.mjs +270 -41
- package/tools/tts-setup.mjs +257 -0
- package/tools/veo.mjs +204 -0
- package/tools/video.mjs +440 -0
- package/tools/voiceover-server.mjs +163 -54
- package/tools/voiceover.mjs +24 -7
- package/tools/zip.mjs +143 -0
- package/docs/demo.svg +0 -124
- package/themes/aliens.css +0 -87
- package/themes/apple2.css +0 -129
- package/themes/blade-runner.css +0 -86
- package/themes/c64.css +0 -88
- package/themes/friends.css +0 -85
- package/themes/gameboy.css +0 -121
- package/themes/genesis.css +0 -87
- package/themes/godfather.css +0 -85
- package/themes/ibm-oldschool.css +0 -87
- package/themes/miami-vice.css +0 -87
- package/themes/pulp-fiction.css +0 -87
- package/themes/severance.css +0 -85
- package/themes/snes.css +0 -85
- package/themes/star-wars.css +0 -88
- package/themes/stranger-things.css +0 -87
- package/themes/terminator.css +0 -87
package/cli/publish.mjs
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Copyright 2026 Gilles Philippart
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* decklight publish — turn a deck into a shareable URL in one command.
|
|
7
|
+
*
|
|
8
|
+
* decklight publish <deck.html> [--branch gh-pages] [--remote origin]
|
|
9
|
+
* [--no-bundle] [--no-sign] [--path <subdir>]
|
|
10
|
+
* [--target gh-pages|netlify|vercel]
|
|
11
|
+
*
|
|
12
|
+
* Bundles the deck (via `decklight bundle`) to index.html + .nojekyll and
|
|
13
|
+
* pushes them to a gh-pages branch on the remote, then prints the GitHub
|
|
14
|
+
* Pages URL derived from the remote.
|
|
15
|
+
*
|
|
16
|
+
* The commit is built entirely with git plumbing — hash-object → mktree →
|
|
17
|
+
* commit-tree → push <sha>:refs/heads/<branch> — so the author's working
|
|
18
|
+
* tree, index, and checked-out branch are never touched. The first publish
|
|
19
|
+
* creates the branch as an orphan; later publishes parent on the previous
|
|
20
|
+
* gh-pages commit (history, not force-push). --path publishes under a
|
|
21
|
+
* subdirectory, preserving whatever else the branch already carries.
|
|
22
|
+
*
|
|
23
|
+
* --target picks where the bundled page goes (MARKETPLACE.md ENGINES).
|
|
24
|
+
* gh-pages (the default) is the git-plumbing path above and needs no
|
|
25
|
+
* credential. netlify and vercel need a token, read from that provider's own
|
|
26
|
+
* CLI env var (NETLIFY_AUTH_TOKEN, VERCEL_TOKEN, …) — never a terminal
|
|
27
|
+
* prompt, never written to disk (tools/publish-targets.mjs explains why).
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import fs from 'node:fs';
|
|
31
|
+
import os from 'node:os';
|
|
32
|
+
import path from 'node:path';
|
|
33
|
+
import { execFileSync } from 'node:child_process';
|
|
34
|
+
import { makeFail, runMain } from './util.mjs';
|
|
35
|
+
import { isMain } from '../tools/args.mjs';
|
|
36
|
+
import { TARGETS, schemaFor, envVarsFor, envAnswers, deploy } from '../tools/publish-targets.mjs';
|
|
37
|
+
import { checkAnswers } from './wizard.mjs';
|
|
38
|
+
|
|
39
|
+
const fail = makeFail('publish');
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The GitHub Pages URL a remote publishes to, or null when the remote is
|
|
43
|
+
* not GitHub (the caller then prints the pushed ref instead).
|
|
44
|
+
*
|
|
45
|
+
* git@github.com:owner/repo.git → https://owner.github.io/repo/
|
|
46
|
+
* https://github.com/owner/repo(.git) → https://owner.github.io/repo/
|
|
47
|
+
* …/owner/owner.github.io → https://owner.github.io/
|
|
48
|
+
*/
|
|
49
|
+
export function pagesUrl(remoteUrl) {
|
|
50
|
+
const m = (remoteUrl || '').trim().match(
|
|
51
|
+
/^(?:https?:\/\/(?:[^/@]+@)?|git@|ssh:\/\/git@)github\.com[:/]([^/]+)\/([^/]+?)(?:\.git)?\/?$/i,
|
|
52
|
+
);
|
|
53
|
+
if (!m) return null;
|
|
54
|
+
const owner = m[1].toLowerCase();
|
|
55
|
+
const repo = m[2];
|
|
56
|
+
if (repo.toLowerCase() === `${owner}.github.io`) return `https://${owner}.github.io/`;
|
|
57
|
+
return `https://${owner}.github.io/${repo}/`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ---------------------------------------------------------------- arguments
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* `client` is the sigstore client seam (cli/sign.mjs owns the concept): pass one
|
|
64
|
+
* to drive signing without an OIDC identity, `null` to assert the
|
|
65
|
+
* not-installed path. Omitted, the real client is found the usual way.
|
|
66
|
+
*
|
|
67
|
+
* `fetchImpl` is the same seam for a token-based target's HTTP call
|
|
68
|
+
* (tools/publish-targets.mjs) — tests exercise Netlify/Vercel deploys against a
|
|
69
|
+
* script, never a live account.
|
|
70
|
+
*/
|
|
71
|
+
export async function publishMain(argv = process.argv.slice(2), { client, fetchImpl } = {}) {
|
|
72
|
+
|
|
73
|
+
if (!argv.length || argv.includes('--help') || argv.includes('-h')) {
|
|
74
|
+
process.stdout.write(`decklight publish — bundle a deck and push it to GitHub Pages (or another target)
|
|
75
|
+
|
|
76
|
+
Usage:
|
|
77
|
+
decklight publish <deck.html> [--branch gh-pages] [--remote origin]
|
|
78
|
+
[--no-bundle] [--no-sign] [--deck] [--path <subdir>]
|
|
79
|
+
[--target gh-pages|netlify|vercel]
|
|
80
|
+
|
|
81
|
+
Options:
|
|
82
|
+
--branch <name> branch to publish to (default: gh-pages; gh-pages target only)
|
|
83
|
+
--remote <name> git remote to push to (default: origin; gh-pages target only)
|
|
84
|
+
--no-bundle push the deck file as-is (skip single-file bundling)
|
|
85
|
+
--no-sign publish without a Sigstore signature
|
|
86
|
+
--deck also publish index.decklight — the signed container, which
|
|
87
|
+
is the file a visitor downloads and forwards
|
|
88
|
+
--path <subdir> publish under a subdirectory of the site (other content
|
|
89
|
+
on the branch is preserved)
|
|
90
|
+
--target <name> gh-pages (default, no credential) | netlify | vercel
|
|
91
|
+
netlify needs NETLIFY_AUTH_TOKEN + NETLIFY_SITE_ID
|
|
92
|
+
vercel needs VERCEL_TOKEN + VERCEL_PROJECT (+ VERCEL_TEAM_ID)
|
|
93
|
+
|
|
94
|
+
Publishing SIGNS by default: Sigstore keyless mints a short-lived certificate
|
|
95
|
+
against an OIDC identity — the ambient token in CI (GitHub Actions
|
|
96
|
+
needs permissions: id-token: write), or SIGSTORE_ID_TOKEN — and the detached
|
|
97
|
+
signature is pushed as index.html.sig beside the page. There are no keys to
|
|
98
|
+
manage; that is what keyless means. A publish is already a network action, so
|
|
99
|
+
signing adds no failure mode it did not already have. If signing fails nothing
|
|
100
|
+
is pushed, and --no-sign is the deliberate way past it.
|
|
101
|
+
|
|
102
|
+
The commit is built with git plumbing, so your working tree, index, and
|
|
103
|
+
current branch are untouched. The first publish creates the branch as an
|
|
104
|
+
orphan; later publishes append to its history.
|
|
105
|
+
`);
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let deck = null, branch = 'gh-pages', remote = 'origin', bundle = true, subdir = '', sign = true, deckFile = false,
|
|
110
|
+
target = 'gh-pages', branchGiven = false, remoteGiven = false;
|
|
111
|
+
for (let i = 0; i < argv.length; i++) {
|
|
112
|
+
const a = argv[i];
|
|
113
|
+
if (a === '--branch') { branch = argv[++i]; branchGiven = true; }
|
|
114
|
+
else if (a === '--remote') { remote = argv[++i]; remoteGiven = true; }
|
|
115
|
+
else if (a === '--no-bundle') bundle = false;
|
|
116
|
+
else if (a === '--no-sign') sign = false;
|
|
117
|
+
else if (a === '--deck') deckFile = true;
|
|
118
|
+
else if (a === '--path') subdir = argv[++i];
|
|
119
|
+
else if (a === '--target') target = argv[++i];
|
|
120
|
+
else if (!a.startsWith('-') && !deck) deck = a;
|
|
121
|
+
else fail(`unknown argument: ${a}`);
|
|
122
|
+
}
|
|
123
|
+
if (!deck) fail('no deck given');
|
|
124
|
+
// Checked here rather than at pack time: an argument that cannot work should
|
|
125
|
+
// be refused before the command starts doing things.
|
|
126
|
+
if (deckFile && !sign) fail('--deck packs the SIGNED deck — it cannot be combined with --no-sign');
|
|
127
|
+
if (target !== 'gh-pages' && !TARGETS.includes(target)) {
|
|
128
|
+
fail(`"${target}" is not a publish target this decklight knows — try: gh-pages, ${TARGETS.join(', ')}`);
|
|
129
|
+
}
|
|
130
|
+
if (target !== 'gh-pages' && (branchGiven || remoteGiven)) {
|
|
131
|
+
fail(`--branch/--remote are gh-pages options — "${target}" is not git, there is nothing for them to name`);
|
|
132
|
+
}
|
|
133
|
+
if (target === 'gh-pages' && (!branch || !/^[\w][\w./-]*$/.test(branch))) fail(`not a usable branch name: "${branch}"`);
|
|
134
|
+
const parts = (subdir || '').split('/').filter(Boolean);
|
|
135
|
+
if (parts.some((p) => p === '.' || p === '..')) fail(`--path must stay inside the site: "${subdir}"`);
|
|
136
|
+
const deckPath = path.resolve(deck);
|
|
137
|
+
if (!fs.existsSync(deckPath)) fail(`deck not found: ${deckPath}`);
|
|
138
|
+
|
|
139
|
+
// Every git call runs in the deck's directory — the deck's repo is the one
|
|
140
|
+
// being published, wherever the command was launched from. Only gh-pages
|
|
141
|
+
// touches git at all; a token-based target has no working tree to respect.
|
|
142
|
+
const cwd = path.dirname(deckPath);
|
|
143
|
+
const git = (args, input) => {
|
|
144
|
+
try {
|
|
145
|
+
return execFileSync('git', args, { cwd, encoding: 'utf8', input }).trim();
|
|
146
|
+
} catch (e) {
|
|
147
|
+
fail(`git ${args[0]} failed: ${(e.stderr || e.message || '').toString().trim()}`);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
let remoteUrl;
|
|
152
|
+
if (target === 'gh-pages') {
|
|
153
|
+
try { execFileSync('git', ['rev-parse', '--git-dir'], { cwd, stdio: 'ignore' }); }
|
|
154
|
+
catch { fail(`${cwd} is not inside a git repository`); }
|
|
155
|
+
try { remoteUrl = execFileSync('git', ['remote', 'get-url', remote], { cwd, encoding: 'utf8' }).trim(); }
|
|
156
|
+
catch { fail(`no remote "${remote}" in this repository (git remote add ${remote} …)`); }
|
|
157
|
+
// get-url applies url.*.insteadOf; the Pages URL derives from the remote as
|
|
158
|
+
// CONFIGURED — insteadOf is a transport rewrite, not a different site.
|
|
159
|
+
try {
|
|
160
|
+
remoteUrl = execFileSync('git', ['config', '--get', `remote.${remote}.url`],
|
|
161
|
+
{ cwd, encoding: 'utf8' }).trim() || remoteUrl;
|
|
162
|
+
} catch { /* unset (e.g. pushurl-only remotes) — keep get-url's answer */ }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// A token-based target needs its answers before any network call is made —
|
|
166
|
+
// the same refusal-before-action rule signing already follows below.
|
|
167
|
+
let targetAnswers = null;
|
|
168
|
+
if (target !== 'gh-pages') {
|
|
169
|
+
const schema = schemaFor(target);
|
|
170
|
+
targetAnswers = envAnswers(target, process.env);
|
|
171
|
+
const checked = checkAnswers(schema, targetAnswers);
|
|
172
|
+
if (!checked.ok) {
|
|
173
|
+
const vars = envVarsFor(target);
|
|
174
|
+
fail(`${target} needs: ${checked.errors.join('; ')}\n`
|
|
175
|
+
+ ` set it in the environment: ${Object.entries(vars).map(([f, v]) => `${v} (${f})`).join(', ')}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// --------------------------------------------------------------------- bundle
|
|
180
|
+
|
|
181
|
+
let sitePage = deckPath;
|
|
182
|
+
let tmpDir = null;
|
|
183
|
+
if (bundle) {
|
|
184
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'decklight-publish-'));
|
|
185
|
+
sitePage = path.join(tmpDir, 'index.html');
|
|
186
|
+
const { bundleMain } = await import('./bundle.mjs');
|
|
187
|
+
await bundleMain([deckPath, '-o', sitePage]);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// --------------------------------------------------------------- plumbing
|
|
191
|
+
|
|
192
|
+
// Signing is the DEFAULT here and opt-in on `bundle`, which looks inconsistent
|
|
193
|
+
// until you notice what publish already is: a network action. Sigstore keyless
|
|
194
|
+
// needs Fulcio and Rekor, so signing adds no failure mode a publish did not
|
|
195
|
+
// already have — while `bundle` is offline-clean and would gain one
|
|
196
|
+
// (INTEGRITY). It runs before any object is written, so a signing failure
|
|
197
|
+
// leaves the repository and the remote exactly as they were.
|
|
198
|
+
let signature = null;
|
|
199
|
+
if (sign) {
|
|
200
|
+
const { signBytes, verifyBytes, formatSignature } = await import('./sign.mjs');
|
|
201
|
+
try {
|
|
202
|
+
signature = await signBytes(fs.readFileSync(sitePage), { client });
|
|
203
|
+
} catch (e) {
|
|
204
|
+
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
205
|
+
// Never silently unsigned: say what broke and name the deliberate way out,
|
|
206
|
+
// so the unsigned publish is a decision someone made rather than one that
|
|
207
|
+
// happened to them.
|
|
208
|
+
fail(`${e.message}\n publish signs by default — pass --no-sign to publish without a signature`);
|
|
209
|
+
}
|
|
210
|
+
process.stdout.write(`${formatSignature(await verifyBytes(fs.readFileSync(sitePage), signature, { client }))}\n`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// The container is opt-in on both commands until it proves itself (INTEGRITY),
|
|
214
|
+
// and it needs the signature it wraps — so --deck without one is refused here
|
|
215
|
+
// rather than quietly producing an unsigned archive under an attesting name.
|
|
216
|
+
let deckBytes = null;
|
|
217
|
+
if (deckFile) {
|
|
218
|
+
const { packContainer, originOf, runtimeVersionOf } = await import('./deckfile.mjs');
|
|
219
|
+
const payload = fs.readFileSync(sitePage);
|
|
220
|
+
const { bytes } = packContainer({
|
|
221
|
+
payload, signature, name: 'index.html',
|
|
222
|
+
runtime: runtimeVersionOf(payload.toString('utf8')), origin: originOf(deckPath),
|
|
223
|
+
});
|
|
224
|
+
deckBytes = bytes;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (target !== 'gh-pages') {
|
|
228
|
+
// No git object database involved: a token-based target has no working
|
|
229
|
+
// tree, no history, and nothing else on the site to preserve or diff
|
|
230
|
+
// against — the files below are simply what gets sent, in full, every time.
|
|
231
|
+
const pageBytes = fs.readFileSync(sitePage);
|
|
232
|
+
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
233
|
+
const files = [{ path: [...parts, 'index.html'].join('/'), data: pageBytes }];
|
|
234
|
+
if (signature) files.push({ path: [...parts, 'index.html.sig'].join('/'), data: `${JSON.stringify(signature, null, 2)}\n` });
|
|
235
|
+
if (deckBytes) files.push({ path: [...parts, 'index.decklight'].join('/'), data: deckBytes });
|
|
236
|
+
|
|
237
|
+
let result;
|
|
238
|
+
try { result = await deploy(target, targetAnswers, files, { fetchImpl }); }
|
|
239
|
+
catch (e) { fail(`${target}: ${e.message}`); }
|
|
240
|
+
process.stdout.write(`deployed to ${target}${result.id ? ` (${result.id})` : ''}\n`);
|
|
241
|
+
process.stdout.write(result.url ? `${result.url}\n` : `${target} did not report a URL\n`);
|
|
242
|
+
return { target, ...result };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Objects land in the repo's database; nothing references them until the
|
|
246
|
+
// push, and neither the working tree nor the index ever hears about it.
|
|
247
|
+
const pageBlob = git(['hash-object', '-w', '--', sitePage]);
|
|
248
|
+
const nojekyllBlob = git(['hash-object', '-w', '--stdin'], '');
|
|
249
|
+
const sigBlob = signature ? git(['hash-object', '-w', '--stdin'], `${JSON.stringify(signature, null, 2)}\n`) : null;
|
|
250
|
+
const deckBlob = deckBytes
|
|
251
|
+
// -w --stdin takes bytes on stdin; a Buffer keeps them exact.
|
|
252
|
+
? execFileSync('git', ['hash-object', '-w', '--stdin'], { cwd, input: deckBytes, encoding: 'utf8' }).trim()
|
|
253
|
+
: null;
|
|
254
|
+
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
255
|
+
|
|
256
|
+
// A second publish parents on the branch as the REMOTE has it — fetched
|
|
257
|
+
// fresh, so two machines publishing the same deck append to one history.
|
|
258
|
+
let parent = null;
|
|
259
|
+
if (git(['ls-remote', remote, `refs/heads/${branch}`])) {
|
|
260
|
+
git(['fetch', '--quiet', remote, `refs/heads/${branch}`]);
|
|
261
|
+
parent = git(['rev-parse', 'FETCH_HEAD']);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const lsTree = (sha) => git(['ls-tree', sha]).split('\n').filter(Boolean).map((l) => {
|
|
265
|
+
const [meta, name] = l.split('\t');
|
|
266
|
+
const [mode, type, entrySha] = meta.split(/\s+/);
|
|
267
|
+
return { mode, type, sha: entrySha, name };
|
|
268
|
+
});
|
|
269
|
+
const mktree = (entries) => {
|
|
270
|
+
// git's tree order: byte-wise, with tree names comparing as "name/"
|
|
271
|
+
const key = (e) => (e.type === 'tree' ? `${e.name}/` : e.name);
|
|
272
|
+
entries.sort((a, b) => (key(a) < key(b) ? -1 : 1));
|
|
273
|
+
return git(['mktree'], entries.map((e) => `${e.mode} ${e.type} ${e.sha}\t${e.name}\n`).join(''));
|
|
274
|
+
};
|
|
275
|
+
// Return a copy of the tree at `treeish` with the blob placed at pathParts,
|
|
276
|
+
// building intermediate trees and keeping every sibling entry.
|
|
277
|
+
const putBlob = (treeish, pathParts, blobSha) => {
|
|
278
|
+
const entries = treeish ? lsTree(treeish) : [];
|
|
279
|
+
const name = pathParts[0];
|
|
280
|
+
const kept = entries.filter((e) => e.name !== name);
|
|
281
|
+
if (pathParts.length === 1) {
|
|
282
|
+
kept.push({ mode: '100644', type: 'blob', sha: blobSha, name });
|
|
283
|
+
} else {
|
|
284
|
+
const sub = entries.find((e) => e.name === name && e.type === 'tree');
|
|
285
|
+
kept.push({ mode: '040000', type: 'tree', sha: putBlob(sub?.sha, pathParts.slice(1), blobSha), name });
|
|
286
|
+
}
|
|
287
|
+
return mktree(kept);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
let tree = putBlob(parent, ['.nojekyll'], nojekyllBlob);
|
|
291
|
+
tree = putBlob(tree, [...parts, 'index.html'], pageBlob);
|
|
292
|
+
// Beside the page it covers, under the name a verifier will look for.
|
|
293
|
+
if (sigBlob) tree = putBlob(tree, [...parts, 'index.html.sig'], sigBlob);
|
|
294
|
+
if (deckBlob) tree = putBlob(tree, [...parts, 'index.decklight'], deckBlob);
|
|
295
|
+
|
|
296
|
+
const config = (key) => {
|
|
297
|
+
try { return execFileSync('git', ['config', key], { cwd, encoding: 'utf8' }).trim(); }
|
|
298
|
+
catch { return ''; }
|
|
299
|
+
};
|
|
300
|
+
const name = config('user.name'), email = config('user.email');
|
|
301
|
+
let message = `publish: ${path.basename(deckPath)} → ${branch}`
|
|
302
|
+
+ (parts.length ? ` (${parts.join('/')}/)` : '');
|
|
303
|
+
if (name && email) message += `\n\nSigned-off-by: ${name} <${email}>`;
|
|
304
|
+
const env = name && email ? process.env : {
|
|
305
|
+
...process.env,
|
|
306
|
+
GIT_AUTHOR_NAME: 'decklight', GIT_AUTHOR_EMAIL: 'decklight@localhost',
|
|
307
|
+
GIT_COMMITTER_NAME: 'decklight', GIT_COMMITTER_EMAIL: 'decklight@localhost',
|
|
308
|
+
};
|
|
309
|
+
let commit;
|
|
310
|
+
try {
|
|
311
|
+
commit = execFileSync('git',
|
|
312
|
+
['commit-tree', tree, ...(parent ? ['-p', parent] : []), '-m', message],
|
|
313
|
+
{ cwd, encoding: 'utf8', env }).trim();
|
|
314
|
+
} catch (e) {
|
|
315
|
+
fail(`git commit-tree failed: ${(e.stderr || e.message || '').toString().trim()}`);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
git(['push', '--quiet', remote, `${commit}:refs/heads/${branch}`]);
|
|
319
|
+
|
|
320
|
+
// ------------------------------------------------------------------ report
|
|
321
|
+
|
|
322
|
+
const short = commit.slice(0, 7);
|
|
323
|
+
process.stdout.write(parent
|
|
324
|
+
? `pushed ${short} → ${remote} refs/heads/${branch} (parent ${parent.slice(0, 7)})\n`
|
|
325
|
+
: `pushed ${short} → ${remote} refs/heads/${branch} (first publish — new orphan branch)\n`);
|
|
326
|
+
|
|
327
|
+
const base = pagesUrl(remoteUrl);
|
|
328
|
+
const url = base ? base + (parts.length ? parts.join('/') + '/' : '') : null;
|
|
329
|
+
if (url) {
|
|
330
|
+
process.stdout.write(`${url}\n`);
|
|
331
|
+
} else {
|
|
332
|
+
process.stdout.write(`remote is not GitHub (${remoteUrl}) — pushed refs/heads/${branch}\n`);
|
|
333
|
+
}
|
|
334
|
+
if (!parent) {
|
|
335
|
+
process.stdout.write(`first publish: enable Pages in the repo Settings → Pages → `
|
|
336
|
+
+ `Deploy from a branch → ${branch} / (root)\n`);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return { commit, parent, tree, branch, remote, url };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (isMain(import.meta.url)) process.exitCode = await runMain('publish', publishMain);
|
package/cli/qr.mjs
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// A zero-dependency QR encoder (ISO/IEC 18004) — the building block the phone
|
|
5
|
+
// remote (#39) needs to put a scannable URL in the speaker view. Deliberately
|
|
6
|
+
// narrow: byte mode, error-correction level L, versions 1–10 (a v10-L symbol
|
|
7
|
+
// holds 271 bytes; a `http://host:port/remote?t=token` URL is well under v4).
|
|
8
|
+
// Numeric/alphanumeric modes and higher EC levels buy nothing here and are
|
|
9
|
+
// left out. Node-only (cli/): the browser runtime bundle never sees this.
|
|
10
|
+
|
|
11
|
+
// ---- version tables (EC level L only) --------------------------------------
|
|
12
|
+
|
|
13
|
+
// Per version 1–10: EC codewords per block, and the blocks as [count, dataLen]
|
|
14
|
+
// groups (ISO/IEC 18004 table 9).
|
|
15
|
+
const EC_BLOCKS = [
|
|
16
|
+
[7, [[1, 19]]],
|
|
17
|
+
[10, [[1, 34]]],
|
|
18
|
+
[15, [[1, 55]]],
|
|
19
|
+
[20, [[1, 80]]],
|
|
20
|
+
[26, [[1, 108]]],
|
|
21
|
+
[18, [[2, 68]]],
|
|
22
|
+
[20, [[2, 78]]],
|
|
23
|
+
[24, [[2, 97]]],
|
|
24
|
+
[30, [[2, 116]]],
|
|
25
|
+
[18, [[2, 68], [2, 69]]],
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
// Alignment pattern center coordinates per version 1–10 (table E.1).
|
|
29
|
+
const ALIGN = [[], [6, 18], [6, 22], [6, 26], [6, 30], [6, 34], [6, 22, 38], [6, 24, 42], [6, 26, 46], [6, 28, 50]];
|
|
30
|
+
|
|
31
|
+
const dataCodewords = (version) =>
|
|
32
|
+
EC_BLOCKS[version - 1][1].reduce((n, [count, len]) => n + count * len, 0);
|
|
33
|
+
|
|
34
|
+
// Byte-mode payload capacity: data bits minus the 4-bit mode indicator and the
|
|
35
|
+
// character count (8 bits through v9, 16 from v10).
|
|
36
|
+
const byteCapacity = (version) =>
|
|
37
|
+
Math.floor((dataCodewords(version) * 8 - 4 - (version < 10 ? 8 : 16)) / 8);
|
|
38
|
+
|
|
39
|
+
// ---- Reed–Solomon over GF(256), polynomial 0x11d ---------------------------
|
|
40
|
+
|
|
41
|
+
const EXP = new Uint8Array(510);
|
|
42
|
+
const LOG = new Uint8Array(256);
|
|
43
|
+
for (let i = 0, x = 1; i < 255; i++) {
|
|
44
|
+
EXP[i] = EXP[i + 255] = x;
|
|
45
|
+
LOG[x] = i;
|
|
46
|
+
x <<= 1;
|
|
47
|
+
if (x & 0x100) x ^= 0x11d;
|
|
48
|
+
}
|
|
49
|
+
const gfMul = (a, b) => (a && b ? EXP[LOG[a] + LOG[b]] : 0);
|
|
50
|
+
|
|
51
|
+
/** Generator polynomial for `n` EC codewords: ∏(x − α^i), MSB-first coefficients. */
|
|
52
|
+
function rsGenerator(n) {
|
|
53
|
+
let poly = [1];
|
|
54
|
+
for (let i = 0; i < n; i++) {
|
|
55
|
+
const next = new Array(poly.length + 1).fill(0);
|
|
56
|
+
for (let j = 0; j < poly.length; j++) {
|
|
57
|
+
next[j] ^= poly[j];
|
|
58
|
+
next[j + 1] ^= gfMul(poly[j], EXP[i]);
|
|
59
|
+
}
|
|
60
|
+
poly = next;
|
|
61
|
+
}
|
|
62
|
+
return poly;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The `ecLen` Reed–Solomon codewords for a data block (polynomial remainder). */
|
|
66
|
+
function rsEncode(data, ecLen) {
|
|
67
|
+
const gen = rsGenerator(ecLen);
|
|
68
|
+
const buf = new Uint8Array(data.length + ecLen);
|
|
69
|
+
buf.set(data);
|
|
70
|
+
for (let i = 0; i < data.length; i++) {
|
|
71
|
+
const factor = buf[i];
|
|
72
|
+
if (!factor) continue;
|
|
73
|
+
for (let j = 1; j < gen.length; j++) buf[i + j] ^= gfMul(gen[j], factor);
|
|
74
|
+
}
|
|
75
|
+
return buf.slice(data.length);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ---- format / version information (BCH-protected) --------------------------
|
|
79
|
+
|
|
80
|
+
/** 15 format bits for EC level L + `mask`: BCH(15,5) with 0x537, XOR 0x5412. */
|
|
81
|
+
function formatBits(mask) {
|
|
82
|
+
const data = (0b01 << 3) | mask; // 01 = level L
|
|
83
|
+
let rem = data;
|
|
84
|
+
for (let i = 0; i < 10; i++) rem = (rem << 1) ^ (((rem >> 9) & 1) * 0x537);
|
|
85
|
+
return ((data << 10) | rem) ^ 0x5412;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 18 version bits (versions ≥ 7 only): BCH(18,6) with 0x1f25. */
|
|
89
|
+
function versionBits(version) {
|
|
90
|
+
let rem = version;
|
|
91
|
+
for (let i = 0; i < 12; i++) rem = (rem << 1) ^ (((rem >> 11) & 1) * 0x1f25);
|
|
92
|
+
return (version << 12) | rem;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ---- mask patterns and penalty scoring --------------------------------------
|
|
96
|
+
|
|
97
|
+
const MASKS = [
|
|
98
|
+
(r, c) => (r + c) % 2 === 0,
|
|
99
|
+
(r) => r % 2 === 0,
|
|
100
|
+
(_, c) => c % 3 === 0,
|
|
101
|
+
(r, c) => (r + c) % 3 === 0,
|
|
102
|
+
(r, c) => (Math.floor(r / 2) + Math.floor(c / 3)) % 2 === 0,
|
|
103
|
+
(r, c) => ((r * c) % 2) + ((r * c) % 3) === 0,
|
|
104
|
+
(r, c) => (((r * c) % 2) + ((r * c) % 3)) % 2 === 0,
|
|
105
|
+
(r, c) => (((r + c) % 2) + ((r * c) % 3)) % 2 === 0,
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
// The four penalty rules (table 11); lower total wins the mask choice.
|
|
109
|
+
function penalty(m) {
|
|
110
|
+
const size = m.length;
|
|
111
|
+
let score = 0;
|
|
112
|
+
// N1: runs of ≥5 same-colored modules in a row/column
|
|
113
|
+
for (let axis = 0; axis < 2; axis++) {
|
|
114
|
+
const at = axis === 0 ? (i, j) => m[i][j] : (i, j) => m[j][i];
|
|
115
|
+
for (let i = 0; i < size; i++) {
|
|
116
|
+
let run = 1;
|
|
117
|
+
for (let j = 1; j <= size; j++) {
|
|
118
|
+
if (j < size && at(i, j) === at(i, j - 1)) run++;
|
|
119
|
+
else {
|
|
120
|
+
if (run >= 5) score += 3 + run - 5;
|
|
121
|
+
run = 1;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// N2: 2×2 blocks of one color
|
|
127
|
+
for (let r = 0; r < size - 1; r++)
|
|
128
|
+
for (let c = 0; c < size - 1; c++)
|
|
129
|
+
if (m[r][c] === m[r][c + 1] && m[r][c] === m[r + 1][c] && m[r][c] === m[r + 1][c + 1]) score += 3;
|
|
130
|
+
// N3: finder-lookalike 1011101 with 4 light modules on one side
|
|
131
|
+
const P1 = [1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0];
|
|
132
|
+
const P2 = [0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1];
|
|
133
|
+
for (let axis = 0; axis < 2; axis++) {
|
|
134
|
+
const at = axis === 0 ? (i, j) => m[i][j] : (i, j) => m[j][i];
|
|
135
|
+
for (let i = 0; i < size; i++)
|
|
136
|
+
for (let j = 0; j + 11 <= size; j++) {
|
|
137
|
+
let a = true;
|
|
138
|
+
let b = true;
|
|
139
|
+
for (let k = 0; k < 11 && (a || b); k++) {
|
|
140
|
+
const v = at(i, j + k) ? 1 : 0;
|
|
141
|
+
if (v !== P1[k]) a = false;
|
|
142
|
+
if (v !== P2[k]) b = false;
|
|
143
|
+
}
|
|
144
|
+
if (a || b) score += 40;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// N4: dark-module proportion, 10 points per 5% step away from 50%
|
|
148
|
+
let dark = 0;
|
|
149
|
+
for (const row of m) for (const cell of row) if (cell) dark++;
|
|
150
|
+
const total = size * size;
|
|
151
|
+
score += Math.floor(Math.abs(dark * 100 - total * 50) / (total * 5)) * 10;
|
|
152
|
+
return score;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// ---- matrix construction -----------------------------------------------------
|
|
156
|
+
|
|
157
|
+
/** Draw the 15 format bits into both of their reserved areas. */
|
|
158
|
+
function drawFormat(modules, bits) {
|
|
159
|
+
const size = modules.length;
|
|
160
|
+
const bit = (i) => ((bits >> i) & 1) === 1;
|
|
161
|
+
for (let i = 0; i <= 5; i++) modules[i][8] = bit(i); // top-left, going down
|
|
162
|
+
modules[7][8] = bit(6);
|
|
163
|
+
modules[8][8] = bit(7);
|
|
164
|
+
modules[8][7] = bit(8);
|
|
165
|
+
for (let i = 9; i < 15; i++) modules[8][14 - i] = bit(i); // top-left, going left
|
|
166
|
+
for (let i = 0; i < 8; i++) modules[8][size - 1 - i] = bit(i); // top-right
|
|
167
|
+
for (let i = 8; i < 15; i++) modules[size - 15 + i][8] = bit(i); // bottom-left
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Encode `text` (UTF-8, byte mode, EC level L) into a QR module matrix — a
|
|
172
|
+
* size×size array of row arrays of booleans, `true` = dark. Picks the smallest
|
|
173
|
+
* fitting version in 1–10 and the best of the 8 masks by penalty score; throws
|
|
174
|
+
* when the text won't fit v10 or `ecLevel` isn't 'L'.
|
|
175
|
+
*/
|
|
176
|
+
export function encodeQR(text, { ecLevel = 'L' } = {}) {
|
|
177
|
+
if (ecLevel !== 'L') {
|
|
178
|
+
throw new Error(`unsupported error-correction level "${ecLevel}" — this encoder only implements level L`);
|
|
179
|
+
}
|
|
180
|
+
const bytes = new TextEncoder().encode(String(text));
|
|
181
|
+
let version = 0;
|
|
182
|
+
for (let v = 1; v <= 10 && !version; v++) if (bytes.length <= byteCapacity(v)) version = v;
|
|
183
|
+
if (!version) {
|
|
184
|
+
throw new Error(
|
|
185
|
+
`text is ${bytes.length} bytes; QR version 10 at EC level L holds at most ${byteCapacity(10)} — shorten the text`,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Bit stream: mode 0100, char count, data, terminator, byte-align, pad bytes.
|
|
190
|
+
const bits = [];
|
|
191
|
+
const push = (val, n) => {
|
|
192
|
+
for (let i = n - 1; i >= 0; i--) bits.push((val >> i) & 1);
|
|
193
|
+
};
|
|
194
|
+
push(0b0100, 4);
|
|
195
|
+
push(bytes.length, version < 10 ? 8 : 16);
|
|
196
|
+
for (const b of bytes) push(b, 8);
|
|
197
|
+
const capacityBits = dataCodewords(version) * 8;
|
|
198
|
+
push(0, Math.min(4, capacityBits - bits.length));
|
|
199
|
+
while (bits.length % 8) bits.push(0);
|
|
200
|
+
const codewords = [];
|
|
201
|
+
for (let i = 0; i < bits.length; i += 8) {
|
|
202
|
+
let b = 0;
|
|
203
|
+
for (let j = 0; j < 8; j++) b = (b << 1) | bits[i + j];
|
|
204
|
+
codewords.push(b);
|
|
205
|
+
}
|
|
206
|
+
for (let pad = 0xec; codewords.length < capacityBits / 8; pad ^= 0xec ^ 0x11) codewords.push(pad);
|
|
207
|
+
|
|
208
|
+
// Split into blocks, append RS codewords, interleave data then EC.
|
|
209
|
+
const [ecLen, groups] = EC_BLOCKS[version - 1];
|
|
210
|
+
const blocks = [];
|
|
211
|
+
let off = 0;
|
|
212
|
+
for (const [count, len] of groups) {
|
|
213
|
+
for (let i = 0; i < count; i++, off += len) {
|
|
214
|
+
const data = codewords.slice(off, off + len);
|
|
215
|
+
blocks.push({ data, ec: rsEncode(data, ecLen) });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const stream = [];
|
|
219
|
+
const maxLen = Math.max(...blocks.map((b) => b.data.length));
|
|
220
|
+
for (let i = 0; i < maxLen; i++) for (const b of blocks) if (i < b.data.length) stream.push(b.data[i]);
|
|
221
|
+
for (let i = 0; i < ecLen; i++) for (const b of blocks) stream.push(b.ec[i]);
|
|
222
|
+
|
|
223
|
+
// Function patterns. `func` marks modules the data zigzag must skip.
|
|
224
|
+
const size = version * 4 + 17;
|
|
225
|
+
const modules = Array.from({ length: size }, () => new Array(size).fill(false));
|
|
226
|
+
const func = Array.from({ length: size }, () => new Array(size).fill(false));
|
|
227
|
+
const set = (r, c, dark) => {
|
|
228
|
+
modules[r][c] = dark;
|
|
229
|
+
func[r][c] = true;
|
|
230
|
+
};
|
|
231
|
+
for (const [fr, fc] of [[0, 0], [0, size - 7], [size - 7, 0]]) {
|
|
232
|
+
// 7×7 finder ring plus its one-module light separator
|
|
233
|
+
for (let r = -1; r <= 7; r++)
|
|
234
|
+
for (let c = -1; c <= 7; c++) {
|
|
235
|
+
const rr = fr + r;
|
|
236
|
+
const cc = fc + c;
|
|
237
|
+
if (rr < 0 || rr >= size || cc < 0 || cc >= size) continue;
|
|
238
|
+
const d = Math.max(Math.abs(r - 3), Math.abs(c - 3));
|
|
239
|
+
set(rr, cc, d <= 3 && d !== 2);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (let i = 8; i < size - 8; i++) {
|
|
243
|
+
set(6, i, i % 2 === 0);
|
|
244
|
+
set(i, 6, i % 2 === 0);
|
|
245
|
+
}
|
|
246
|
+
// Alignment patterns everywhere except the three finder corners (they DO
|
|
247
|
+
// overlap the timing lines — v7's (6, 22) sits right on the timing row).
|
|
248
|
+
const centers = ALIGN[version - 1];
|
|
249
|
+
const last = centers.length - 1;
|
|
250
|
+
for (let i = 0; i <= last; i++)
|
|
251
|
+
for (let j = 0; j <= last; j++) {
|
|
252
|
+
if ((i === 0 && j === 0) || (i === 0 && j === last) || (i === last && j === 0)) continue;
|
|
253
|
+
for (let r = -2; r <= 2; r++)
|
|
254
|
+
for (let c = -2; c <= 2; c++)
|
|
255
|
+
set(centers[i] + r, centers[j] + c, Math.max(Math.abs(r), Math.abs(c)) !== 1);
|
|
256
|
+
}
|
|
257
|
+
set(size - 8, 8, true); // the always-dark module
|
|
258
|
+
if (version >= 7) {
|
|
259
|
+
const vbits = versionBits(version);
|
|
260
|
+
for (let i = 0; i < 18; i++) {
|
|
261
|
+
const dark = ((vbits >> i) & 1) === 1;
|
|
262
|
+
const a = size - 11 + (i % 3);
|
|
263
|
+
const b = Math.floor(i / 3);
|
|
264
|
+
set(b, a, dark);
|
|
265
|
+
set(a, b, dark);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
// Reserve the format areas so the zigzag skips them (real bits drawn per
|
|
269
|
+
// mask): row/column 8 around the top-left finder (position 6 is timing,
|
|
270
|
+
// already reserved), the top-right 8 of row 8, the bottom-left 8 of column 8.
|
|
271
|
+
for (let i = 0; i <= 8; i++) {
|
|
272
|
+
if (i !== 6) {
|
|
273
|
+
func[8][i] = true;
|
|
274
|
+
func[i][8] = true;
|
|
275
|
+
}
|
|
276
|
+
if (i < 8) {
|
|
277
|
+
func[8][size - 1 - i] = true;
|
|
278
|
+
func[size - 1 - i][8] = true;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Zigzag data placement: column pairs right to left, alternating up/down,
|
|
283
|
+
// skipping the vertical timing column. Left-over modules are remainder bits.
|
|
284
|
+
let bitIndex = 0;
|
|
285
|
+
const dataBits = stream.flatMap((cw) => Array.from({ length: 8 }, (_, i) => (cw >> (7 - i)) & 1));
|
|
286
|
+
for (let right = size - 1; right >= 1; right -= 2) {
|
|
287
|
+
if (right === 6) right = 5;
|
|
288
|
+
for (let vert = 0; vert < size; vert++) {
|
|
289
|
+
for (let j = 0; j < 2; j++) {
|
|
290
|
+
const c = right - j;
|
|
291
|
+
const upward = ((right + 1) & 2) === 0;
|
|
292
|
+
const r = upward ? size - 1 - vert : vert;
|
|
293
|
+
if (!func[r][c] && bitIndex < dataBits.length) {
|
|
294
|
+
modules[r][c] = dataBits[bitIndex] === 1;
|
|
295
|
+
bitIndex++;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Try all 8 masks on the data modules; keep the lowest penalty (XOR is its
|
|
302
|
+
// own inverse, so each candidate is applied, scored, and peeled off).
|
|
303
|
+
const applyMask = (mask) => {
|
|
304
|
+
for (let r = 0; r < size; r++)
|
|
305
|
+
for (let c = 0; c < size; c++) if (!func[r][c] && MASKS[mask](r, c)) modules[r][c] = !modules[r][c];
|
|
306
|
+
};
|
|
307
|
+
let best = { mask: 0, score: Infinity };
|
|
308
|
+
for (let mask = 0; mask < 8; mask++) {
|
|
309
|
+
applyMask(mask);
|
|
310
|
+
drawFormat(modules, formatBits(mask));
|
|
311
|
+
const score = penalty(modules);
|
|
312
|
+
if (score < best.score) best = { mask, score };
|
|
313
|
+
applyMask(mask);
|
|
314
|
+
}
|
|
315
|
+
applyMask(best.mask);
|
|
316
|
+
drawFormat(modules, formatBits(best.mask));
|
|
317
|
+
return modules;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Render `text` as a self-contained SVG string — no external refs, quiet zone
|
|
322
|
+
* included, sized by `viewBox` (one unit per module) so the consumer scales it
|
|
323
|
+
* freely. Options: `ecLevel`, `quiet` (modules of margin, default 4, the
|
|
324
|
+
* spec minimum), `dark`/`light` colors.
|
|
325
|
+
*/
|
|
326
|
+
export function qrSvg(text, { ecLevel = 'L', quiet = 4, dark = '#000', light = '#fff' } = {}) {
|
|
327
|
+
const matrix = encodeQR(text, { ecLevel });
|
|
328
|
+
const size = matrix.length + quiet * 2;
|
|
329
|
+
let d = '';
|
|
330
|
+
for (let r = 0; r < matrix.length; r++)
|
|
331
|
+
for (let c = 0; c < matrix.length; c++) if (matrix[r][c]) d += `M${c + quiet} ${r + quiet}h1v1h-1z`;
|
|
332
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" shape-rendering="crispEdges">`
|
|
333
|
+
+ `<rect width="${size}" height="${size}" fill="${light}"/><path d="${d}" fill="${dark}"/></svg>`;
|
|
334
|
+
}
|