@barmark/score 0.1.1 → 0.3.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/CHANGELOG.md +99 -1
- package/README.md +105 -9
- package/dist/download.d.ts +43 -0
- package/dist/download.js +118 -0
- package/dist/element.d.ts +2 -1
- package/dist/element.js +123 -15
- package/dist/engine.d.ts +81 -8
- package/dist/engine.js +166 -23
- package/dist/engrave.d.ts +30 -2
- package/dist/engrave.js +79 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/loading.d.ts +17 -0
- package/dist/loading.js +78 -0
- package/dist/shell.js +2 -1
- package/dist/verovio.d.ts +8 -0
- package/dist/verovio.js +8 -0
- package/dist/worker.d.ts +55 -1
- package/dist/worker.js +111 -15
- package/package.json +5 -5
package/dist/engrave.js
CHANGED
|
@@ -1,5 +1,73 @@
|
|
|
1
1
|
import { exportMusicXml } from "@barmark/musicxml";
|
|
2
|
+
import { parseChart, } from "barmark";
|
|
2
3
|
import { cleanSvg } from "./markup.js";
|
|
4
|
+
/**
|
|
5
|
+
* The most a score may cost (`scoreCost`, about the bytes of MusicXML it
|
|
6
|
+
* writes) before it is refused, unexported: a short chart with repeats can
|
|
7
|
+
* play thousands of bars, and writing each out could take gigabytes. A
|
|
8
|
+
* long lead sheet costs about half a megabyte.
|
|
9
|
+
*/
|
|
10
|
+
export const MAX_SCORE_COST = 50_000_000;
|
|
11
|
+
/** The longest MusicXML handed to Verovio, in characters: the export stops writing past it (`maxLength`). */
|
|
12
|
+
export const MAX_XML_LENGTH = 50_000_000;
|
|
13
|
+
const TOO_LARGE = "The score is too large to draw";
|
|
14
|
+
// What the export writes for each thing, beyond its text, in bytes, from measuring it
|
|
15
|
+
// (see the calibration test in tests/engrave.test.ts): a part's measure (a rest when tacet), a
|
|
16
|
+
// line's voice in it, a note or slash, each pitch of a note set or chord, each degree of a
|
|
17
|
+
// chord symbol, a syllable, words, and a bar's heading or mark.
|
|
18
|
+
const COST = {
|
|
19
|
+
measure: 600,
|
|
20
|
+
line: 200,
|
|
21
|
+
slot: 450,
|
|
22
|
+
pitch: 250,
|
|
23
|
+
degree: 150,
|
|
24
|
+
syllable: 200,
|
|
25
|
+
words: 400,
|
|
26
|
+
heading: 300,
|
|
27
|
+
};
|
|
28
|
+
/** Text as the export escapes it: `&<>"'` take up to six bytes each. */
|
|
29
|
+
const escaped = (text) => text.length + 5 * (text.match(/[&<>"']/g)?.length ?? 0);
|
|
30
|
+
function chordCost(c) {
|
|
31
|
+
if (!c)
|
|
32
|
+
return 0;
|
|
33
|
+
return escaped(c.source) + c.pitches.length * COST.pitch + c.intervals.length * COST.degree;
|
|
34
|
+
}
|
|
35
|
+
/** A note or slash, and its chord or notes; its syllable is the bar's (`lyricsCost`). */
|
|
36
|
+
const memberCost = (m) => m.kind === "chord" ? COST.slot + chordCost(m.chord) + chordCost(m.voicing) : COST.slot;
|
|
37
|
+
const slotCost = (s) => s.kind === "group" ? s.members.reduce((n, m) => n + memberCost(m), 0) : memberCost(s);
|
|
38
|
+
const wordsCost = (words) => (words ?? []).reduce((n, w) => n + COST.words + escaped(w.text), 0);
|
|
39
|
+
const lineCost = (l) => COST.line + l.slots.reduce((n, s) => n + slotCost(s), 0) + wordsCost(l.words);
|
|
40
|
+
/** A part's share of a bar: its measure, then each line's notes, or its own. */
|
|
41
|
+
const partCost = (p) => COST.measure +
|
|
42
|
+
(p === null ? 0 : p.lines ? p.lines.reduce((n, l) => n + lineCost(l), 0) : lineCost(p));
|
|
43
|
+
/** A syllable and its text; one on no note of the singing line is a lyric staff's slash too. */
|
|
44
|
+
const syllableCost = ({ syllable, note }) => COST.syllable +
|
|
45
|
+
(syllable.kind === "sung" ? escaped(syllable.parts.join("")) : 0) +
|
|
46
|
+
(note ? 0 : COST.slot);
|
|
47
|
+
const lyricsCost = (bar) => (bar.lyrics ?? []).reduce((n, l) => n + l.syllables.reduce((m, s) => m + syllableCost(s), 0), 0);
|
|
48
|
+
/** One played bar: every declared part's measure (a tacet one's rest too), its notes and words, the sung syllables (a lyric staff's measure with them), and its heading. */
|
|
49
|
+
function barCost(bar, parts) {
|
|
50
|
+
const played = bar.parts
|
|
51
|
+
? bar.parts.reduce((n, p) => n + partCost(p), 0) + (parts - bar.parts.length) * COST.measure
|
|
52
|
+
: partCost(bar);
|
|
53
|
+
const sung = bar.lyrics?.length ? COST.measure + lyricsCost(bar) : 0;
|
|
54
|
+
const heading = COST.heading + escaped(bar.section ?? "") + escaped(bar.rehearsal ?? "");
|
|
55
|
+
return played + sung + heading;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* About how many bytes of MusicXML a score of the chart writes: every
|
|
59
|
+
* played bar's measure in every declared part (tacet ones too), each
|
|
60
|
+
* slot, pitch, degree, syllable and words, with the length of its text,
|
|
61
|
+
* and its heading. One pass over the play order, cheap enough to refuse a
|
|
62
|
+
* chart before exporting it. It is an early gate, not the bound: it
|
|
63
|
+
* leaves out what is rarely large (a part's silent lines, text lines), and
|
|
64
|
+
* the export's own `maxLength` budget, which `engraveChart` sets, is what
|
|
65
|
+
* keeps the MusicXML within `MAX_XML_LENGTH`.
|
|
66
|
+
*/
|
|
67
|
+
export function scoreCost(parse) {
|
|
68
|
+
const parts = Math.max(1, parse.parts.length);
|
|
69
|
+
return parse.bars.reduce((n, bar) => n + barCost(bar, parts), 0);
|
|
70
|
+
}
|
|
3
71
|
/** Verovio's options for a score, all but `pageWidth` and `scale`. */
|
|
4
72
|
export const LAYOUT = {
|
|
5
73
|
pageHeight: 60000,
|
|
@@ -63,9 +131,18 @@ function refusal(diagnostics) {
|
|
|
63
131
|
// A diagnostic about the whole chart has line 0: there is no line to name.
|
|
64
132
|
return first.line > 0 ? `Line ${first.line}: ${first.message}` : first.message;
|
|
65
133
|
}
|
|
66
|
-
/**
|
|
134
|
+
/**
|
|
135
|
+
* A chart as a full score, or why it cannot be one. A chart that would
|
|
136
|
+
* cost more than `maxCost` is refused before it is exported, and the
|
|
137
|
+
* export stops writing MusicXML longer than `maxXmlLength`.
|
|
138
|
+
*/
|
|
67
139
|
export function engraveChart(vrv, text, options) {
|
|
68
|
-
|
|
140
|
+
if (scoreCost(parseChart(text)) > (options.maxCost ?? MAX_SCORE_COST))
|
|
141
|
+
return { ok: false, reason: TOO_LARGE, diagnostics: [] };
|
|
142
|
+
// The export's own budget is the bound: it stops writing at the limit.
|
|
143
|
+
const exported = exportMusicXml(text, { maxLength: options.maxXmlLength ?? MAX_XML_LENGTH });
|
|
144
|
+
if (!exported.ok && exported.tooLarge)
|
|
145
|
+
return { ok: false, reason: TOO_LARGE, diagnostics: [] };
|
|
69
146
|
if (!exported.ok)
|
|
70
147
|
return { ok: false, reason: refusal(exported.diagnostics), diagnostics: exported.diagnostics };
|
|
71
148
|
try {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { createEngine, type Engine, type EngraveReply, engine, type Reply, type Request, type WorkerLike, } from "./engine.js";
|
|
2
|
-
export { type Engraved, type EngraveOptions, engraveChart, engraveXml, LAYOUT } from "./engrave.js";
|
|
1
|
+
export { createEngine, type Engine, type EngineOptions, type EngineStatus, type EngraveCall, type EngraveReply, engine, JOB_DEADLINE_MS, LOAD_DEADLINE_MS, type LoadStatus, type Progress, type Ready, type Reply, type Request, SUPERSEDED_GRACE_MS, type VerovioSource, type WorkerLike, type WorkerMessage, } from "./engine.js";
|
|
2
|
+
export { type Engraved, type EngraveOptions, engraveChart, engraveXml, LAYOUT, MAX_SCORE_COST, MAX_XML_LENGTH, scoreCost, } from "./engrave.js";
|
|
3
3
|
export { cleanSvg, type ScoreTheme, scorePane, scoreStyles } from "./markup.js";
|
|
4
|
+
export { VEROVIO_CDN_INTEGRITY, VEROVIO_CDN_URL, VEROVIO_MODULE_BYTES, VEROVIO_VERSION, } from "./verovio.js";
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { createEngine, engine, } from "./engine.js";
|
|
2
|
-
export { engraveChart, engraveXml, LAYOUT } from "./engrave.js";
|
|
1
|
+
export { createEngine, engine, JOB_DEADLINE_MS, LOAD_DEADLINE_MS, SUPERSEDED_GRACE_MS, } from "./engine.js";
|
|
2
|
+
export { engraveChart, engraveXml, LAYOUT, MAX_SCORE_COST, MAX_XML_LENGTH, scoreCost, } from "./engrave.js";
|
|
3
3
|
export { cleanSvg, scorePane, scoreStyles } from "./markup.js";
|
|
4
|
+
export { VEROVIO_CDN_INTEGRITY, VEROVIO_CDN_URL, VEROVIO_MODULE_BYTES, VEROVIO_VERSION, } from "./verovio.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { EngineStatus } from "./engine.js";
|
|
2
|
+
/** Before anything is heard: the engine downloading, unmeasured. */
|
|
3
|
+
export declare const UNMEASURED: EngineStatus;
|
|
4
|
+
/**
|
|
5
|
+
* Shows `status` in `pane`, building the region the first time and then
|
|
6
|
+
* updating it in place: the live region's text changes only with the
|
|
7
|
+
* phase, so a screen reader hears each phase once, and the percentage is
|
|
8
|
+
* the bar's value (and the amount beside it, hidden from assistive
|
|
9
|
+
* technology, which reads the bar).
|
|
10
|
+
*/
|
|
11
|
+
export declare function showStatus(pane: Element, status: EngineStatus): void;
|
|
12
|
+
/**
|
|
13
|
+
* The region's look, in the switch's ink and rule. An indeterminate bar
|
|
14
|
+
* (no `aria-valuenow`) slides; with reduced motion it stands still, a
|
|
15
|
+
* faint full bar, and a measured one jumps rather than glides.
|
|
16
|
+
*/
|
|
17
|
+
export declare const loadingStyles = ".bms-loading{display:flex;flex-wrap:wrap;align-items:center;gap:.375rem .75rem;padding:.75rem 0;color:var(--bms-ink);font-family:var(--barmark-label-font,system-ui,-apple-system,\"Segoe UI\",sans-serif);font-size:.875em}\n.bms-status{margin:0;flex:1 0 100%}\n.bms-bar{position:relative;flex:0 1 16rem;height:.375rem;border:1px solid var(--bms-rule);border-radius:.375rem;overflow:hidden}\n.bms-bar>span{position:absolute;top:0;bottom:0;left:0;width:var(--bms-done,0%);background:var(--bms-ink);transition:width .2s linear}\n.bms-bar:not([aria-valuenow])>span{width:30%;animation:bms-slide 1.2s ease-in-out infinite alternate}\n@keyframes bms-slide{from{left:0}to{left:70%}}\n.bms-amount{font-variant-numeric:tabular-nums}\n@media (prefers-reduced-motion:reduce){.bms-bar>span{transition:none}.bms-bar:not([aria-valuenow])>span{animation:none;width:100%;opacity:.35}}";
|
package/dist/loading.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// What `<barmark-score>` shows while its score is on its way: the phase in a status region,
|
|
2
|
+
// a progress bar and, while the download can be measured, how far it has come.
|
|
3
|
+
// Verovio-free, like ./shell.js.
|
|
4
|
+
import { progressText } from "./download.js";
|
|
5
|
+
/** Each phase's words: shown (and spoken, once per phase) and the bar's name. */
|
|
6
|
+
const PHASES = {
|
|
7
|
+
download: { text: "Loading the score engine…", label: "Loading the score engine" },
|
|
8
|
+
prepare: { text: "Preparing…", label: "Preparing" },
|
|
9
|
+
engrave: { text: "Engraving…", label: "Engraving" },
|
|
10
|
+
};
|
|
11
|
+
/** Before anything is heard: the engine downloading, unmeasured. */
|
|
12
|
+
export const UNMEASURED = { phase: "download", fraction: null, bytes: null };
|
|
13
|
+
function build(doc) {
|
|
14
|
+
const make = (tag, cls, attrs = {}) => {
|
|
15
|
+
const e = doc.createElement(tag);
|
|
16
|
+
e.className = cls;
|
|
17
|
+
for (const [k, v] of Object.entries(attrs))
|
|
18
|
+
e.setAttribute(k, v);
|
|
19
|
+
return e;
|
|
20
|
+
};
|
|
21
|
+
const box = make("div", "bms-loading");
|
|
22
|
+
const bar = make("div", "bms-bar", {
|
|
23
|
+
role: "progressbar",
|
|
24
|
+
"aria-valuemin": "0",
|
|
25
|
+
"aria-valuemax": "100",
|
|
26
|
+
});
|
|
27
|
+
bar.append(doc.createElement("span"));
|
|
28
|
+
box.append(make("p", "bms-status", { role: "status" }), bar, make("span", "bms-amount", { "aria-hidden": "true" }));
|
|
29
|
+
return box;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Shows `status` in `pane`, building the region the first time and then
|
|
33
|
+
* updating it in place: the live region's text changes only with the
|
|
34
|
+
* phase, so a screen reader hears each phase once, and the percentage is
|
|
35
|
+
* the bar's value (and the amount beside it, hidden from assistive
|
|
36
|
+
* technology, which reads the bar).
|
|
37
|
+
*/
|
|
38
|
+
export function showStatus(pane, status) {
|
|
39
|
+
let box = pane.querySelector(":scope > .bms-loading");
|
|
40
|
+
if (!box) {
|
|
41
|
+
box = build(pane.ownerDocument);
|
|
42
|
+
pane.replaceChildren(box);
|
|
43
|
+
}
|
|
44
|
+
const { text, label } = PHASES[status.phase];
|
|
45
|
+
const live = box.querySelector(".bms-status");
|
|
46
|
+
if (live && live.textContent !== text)
|
|
47
|
+
live.textContent = text;
|
|
48
|
+
const bar = box.querySelector(".bms-bar");
|
|
49
|
+
bar?.setAttribute("aria-label", label);
|
|
50
|
+
const fraction = status.phase === "download" ? status.fraction : null;
|
|
51
|
+
const amount = status.phase === "download" ? progressText(status) : "";
|
|
52
|
+
if (fraction === null) {
|
|
53
|
+
for (const a of ["aria-valuenow", "aria-valuetext"])
|
|
54
|
+
bar?.removeAttribute(a);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const percent = Math.floor(fraction * 100);
|
|
58
|
+
bar?.setAttribute("aria-valuenow", String(percent));
|
|
59
|
+
bar?.setAttribute("aria-valuetext", `${percent}%`);
|
|
60
|
+
bar?.style?.setProperty("--bms-done", `${percent}%`);
|
|
61
|
+
}
|
|
62
|
+
const shown = box.querySelector(".bms-amount");
|
|
63
|
+
if (shown)
|
|
64
|
+
shown.textContent = amount;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The region's look, in the switch's ink and rule. An indeterminate bar
|
|
68
|
+
* (no `aria-valuenow`) slides; with reduced motion it stands still, a
|
|
69
|
+
* faint full bar, and a measured one jumps rather than glides.
|
|
70
|
+
*/
|
|
71
|
+
export const loadingStyles = `.bms-loading{display:flex;flex-wrap:wrap;align-items:center;gap:.375rem .75rem;padding:.75rem 0;color:var(--bms-ink);font-family:var(--barmark-label-font,system-ui,-apple-system,"Segoe UI",sans-serif);font-size:.875em}
|
|
72
|
+
.bms-status{margin:0;flex:1 0 100%}
|
|
73
|
+
.bms-bar{position:relative;flex:0 1 16rem;height:.375rem;border:1px solid var(--bms-rule);border-radius:.375rem;overflow:hidden}
|
|
74
|
+
.bms-bar>span{position:absolute;top:0;bottom:0;left:0;width:var(--bms-done,0%);background:var(--bms-ink);transition:width .2s linear}
|
|
75
|
+
.bms-bar:not([aria-valuenow])>span{width:30%;animation:bms-slide 1.2s ease-in-out infinite alternate}
|
|
76
|
+
@keyframes bms-slide{from{left:0}to{left:70%}}
|
|
77
|
+
.bms-amount{font-variant-numeric:tabular-nums}
|
|
78
|
+
@media (prefers-reduced-motion:reduce){.bms-bar>span{transition:none}.bms-bar:not([aria-valuenow])>span{animation:none;width:100%;opacity:.35}}`;
|
package/dist/shell.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// What the server and the element both write inside `<barmark-score>`'s shadow root,
|
|
2
2
|
// so client-drawn and server-drawn markup are the same. Verovio-free, like ./server.js.
|
|
3
3
|
import { stylesheet } from "@barmark/render";
|
|
4
|
+
import { loadingStyles } from "./loading.js";
|
|
4
5
|
import { escapeHtml, scoreStyles } from "./markup.js";
|
|
5
6
|
import { switchHtml, switchStyles } from "./switch.js";
|
|
6
7
|
/** Chart text for a `<script>`: the two sequences that would end or hide it are written with a backslash. */
|
|
@@ -8,7 +9,7 @@ export const carry = (text) => text.replace(/<\/(script)/gi, "<\\/$1").replaceAl
|
|
|
8
9
|
/** The text `renderScoreHtml` carried, as it was given. */
|
|
9
10
|
export const decodeCarried = (text) => text.replace(/<\\\/(script)/gi, "</$1").replaceAll("<\\!--", "<!--");
|
|
10
11
|
/** The shadow root's content around a drawn grid: styles, the switch, the grid, the (empty) score pane. */
|
|
11
|
-
export const scoreShell = (grid, view) => `<style>${stylesheet}${switchStyles}${scoreStyles}</style>${switchHtml(view)}<div class="bms-grid">${grid}</div><div class="bms-pane" hidden></div>`;
|
|
12
|
+
export const scoreShell = (grid, view) => `<style>${stylesheet}${switchStyles}${scoreStyles}${loadingStyles}</style>${switchHtml(view)}<div class="bms-grid">${grid}</div><div class="bms-pane" hidden></div>`;
|
|
12
13
|
/** A chart that could not be loaded, drawn where the grid would be, as `<barmark-chart>` draws it. */
|
|
13
14
|
export function gridError(message, theme) {
|
|
14
15
|
const m = escapeHtml(message);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The Verovio this package is pinned to (tests/download.test.ts holds these to the installed copy). */
|
|
2
|
+
export declare const VEROVIO_VERSION = "6.4.1";
|
|
3
|
+
/** `verovio/dist/verovio-module.mjs`'s size, decoded: what a compressed download is measured against. */
|
|
4
|
+
export declare const VEROVIO_MODULE_BYTES = 7400749;
|
|
5
|
+
/** The same module on jsDelivr (CORS `*`, immutable), for `verovio-url`. */
|
|
6
|
+
export declare const VEROVIO_CDN_URL = "https://cdn.jsdelivr.net/npm/verovio@6.4.1/dist/verovio-module.mjs";
|
|
7
|
+
/** Its digest, for `verovio-integrity`. */
|
|
8
|
+
export declare const VEROVIO_CDN_INTEGRITY = "sha384-iGNyhtxpKLNw90cjbI9ES538TJtbNZjLxsFVUuI8PwE2TTNQ+i2j9CjQnRMhZXHk";
|
package/dist/verovio.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The Verovio this package is pinned to (tests/download.test.ts holds these to the installed copy). */
|
|
2
|
+
export const VEROVIO_VERSION = "6.4.1";
|
|
3
|
+
/** `verovio/dist/verovio-module.mjs`'s size, decoded: what a compressed download is measured against. */
|
|
4
|
+
export const VEROVIO_MODULE_BYTES = 7_400_749;
|
|
5
|
+
/** The same module on jsDelivr (CORS `*`, immutable), for `verovio-url`. */
|
|
6
|
+
export const VEROVIO_CDN_URL = `https://cdn.jsdelivr.net/npm/verovio@${VEROVIO_VERSION}/dist/verovio-module.mjs`;
|
|
7
|
+
/** Its digest, for `verovio-integrity`. */
|
|
8
|
+
export const VEROVIO_CDN_INTEGRITY = "sha384-iGNyhtxpKLNw90cjbI9ES538TJtbNZjLxsFVUuI8PwE2TTNQ+i2j9CjQnRMhZXHk";
|
package/dist/worker.d.ts
CHANGED
|
@@ -1,8 +1,62 @@
|
|
|
1
1
|
import { VerovioToolkit } from "verovio/esm";
|
|
2
|
-
import
|
|
2
|
+
import { type Download } from "./download.js";
|
|
3
|
+
import type { LoadStatus, Progress, Ready, Reply, Request, VerovioSource, WorkerMessage } from "./engine.js";
|
|
3
4
|
type Toolkit = VerovioToolkit;
|
|
4
5
|
/** Run `load` once and keep its result; a load that fails is forgotten, so the next call tries again. */
|
|
5
6
|
export declare function loadOnce<A, T>(load: (arg: A) => Promise<T>): (arg: A) => Promise<T>;
|
|
7
|
+
type Report = (status: LoadStatus) => void;
|
|
8
|
+
/** A toolkit, and where its Verovio came from (no `verovioUrl`: the package's own). */
|
|
9
|
+
export type Loaded<T> = {
|
|
10
|
+
toolkit: T;
|
|
11
|
+
from: VerovioSource;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A worker's one Verovio toolkit, loaded on first use and reused, and
|
|
15
|
+
* where it came from once loaded, so the engine can give a replacement
|
|
16
|
+
* worker the same. The first load that succeeds wins for the worker's
|
|
17
|
+
* life: later sources are ignored. The first load's `report` hears it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function toolkitLoader<T>(load: (source: VerovioSource, report: Report) => Promise<Loaded<T>>): {
|
|
20
|
+
ready: (source: VerovioSource, report: Report) => Promise<T>;
|
|
21
|
+
loaded: () => VerovioSource | undefined;
|
|
22
|
+
};
|
|
23
|
+
/** What loading Verovio needs, so a test can pass fakes. `M` is a Verovio module, `T` a toolkit. */
|
|
24
|
+
export type LoaderDeps<M, T> = {
|
|
25
|
+
/** Downloads `url` with progress, checked against `integrity` when given (download.ts). */
|
|
26
|
+
fetchModule(url: string, options: {
|
|
27
|
+
integrity?: string;
|
|
28
|
+
onProgress: (d: Download) => void;
|
|
29
|
+
}): Promise<Blob>;
|
|
30
|
+
/** Imports a downloaded module. */
|
|
31
|
+
importBlob(blob: Blob): Promise<M>;
|
|
32
|
+
/** The package's own Verovio, as the bundler placed it. */
|
|
33
|
+
own(): Promise<M>;
|
|
34
|
+
/** Instantiates the module's WebAssembly: the toolkit. */
|
|
35
|
+
instantiate(m: M): Promise<T>;
|
|
36
|
+
warn(message: string): void;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Loads Verovio from `source`. The package's own copy comes through the
|
|
40
|
+
* bundler, its download unmeasured. A `verovioUrl` is downloaded here,
|
|
41
|
+
* with progress, and imported from a blob; with `verovioIntegrity` its
|
|
42
|
+
* digest is checked first, and a mismatch or any failure falls back to
|
|
43
|
+
* the package's own copy, once, with a warning. Without one, a failure is
|
|
44
|
+
* the failure.
|
|
45
|
+
*/
|
|
46
|
+
export declare function verovioLoader<M, T>(deps: LoaderDeps<M, T>): (source: VerovioSource, report: Report) => Promise<Loaded<T>>;
|
|
47
|
+
/**
|
|
48
|
+
* The toolkit for `request`, posting its load's progress, then saying it
|
|
49
|
+
* is `Ready` (and where it came from) once loaded, before the job is
|
|
50
|
+
* engraved: the engine starts the job's deadline then, so a slow download
|
|
51
|
+
* is not counted against it.
|
|
52
|
+
*/
|
|
53
|
+
export declare function withReady<T>(loader: ReturnType<typeof toolkitLoader<T>>, request: Request, post: (message: Ready | Progress) => void): () => Promise<T>;
|
|
54
|
+
/**
|
|
55
|
+
* Answers one request: a job is engraved (`engrave`, which never throws)
|
|
56
|
+
* and replied to; a preload only loads, posting `Ready`, or a reply with
|
|
57
|
+
* the reason when the load fails.
|
|
58
|
+
*/
|
|
59
|
+
export declare function answer<T>(request: Request, loader: ReturnType<typeof toolkitLoader<T>>, post: (message: WorkerMessage) => void, engrave: (request: Request, ready: () => Promise<T>) => Promise<Reply>): Promise<void>;
|
|
6
60
|
/** Answer one request. Never throws: a failure is a reply with its reason. */
|
|
7
61
|
export declare function handle(request: Request, loaded: () => Promise<Toolkit>): Promise<Reply>;
|
|
8
62
|
export {};
|
package/dist/worker.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as esm from "verovio/esm";
|
|
2
2
|
import { VerovioToolkit } from "verovio/esm";
|
|
3
|
+
import { fetchModule } from "./download.js";
|
|
3
4
|
import { engraveChart } from "./engrave.js";
|
|
5
|
+
import { VEROVIO_MODULE_BYTES } from "./verovio.js";
|
|
4
6
|
// Verovio's typings omit the log switch; it is exported all the same.
|
|
5
7
|
const { enableLog, LOG_OFF } = esm;
|
|
6
8
|
/** Run `load` once and keep its result; a load that fails is forgotten, so the next call tries again. */
|
|
@@ -15,19 +17,116 @@ export function loadOnce(load) {
|
|
|
15
17
|
};
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
20
|
+
* A worker's one Verovio toolkit, loaded on first use and reused, and
|
|
21
|
+
* where it came from once loaded, so the engine can give a replacement
|
|
22
|
+
* worker the same. The first load that succeeds wins for the worker's
|
|
23
|
+
* life: later sources are ignored. The first load's `report` hears it.
|
|
22
24
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
export function toolkitLoader(load) {
|
|
26
|
+
let loaded;
|
|
27
|
+
const once = loadOnce(async ({ source, report }) => {
|
|
28
|
+
const r = await load(source, report);
|
|
29
|
+
loaded = r.from;
|
|
30
|
+
return r.toolkit;
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
ready: (source, report) => once({ source, report }),
|
|
34
|
+
loaded: () => loaded,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const UNMEASURED = { phase: "download", fraction: null, bytes: null };
|
|
38
|
+
/**
|
|
39
|
+
* Loads Verovio from `source`. The package's own copy comes through the
|
|
40
|
+
* bundler, its download unmeasured. A `verovioUrl` is downloaded here,
|
|
41
|
+
* with progress, and imported from a blob; with `verovioIntegrity` its
|
|
42
|
+
* digest is checked first, and a mismatch or any failure falls back to
|
|
43
|
+
* the package's own copy, once, with a warning. Without one, a failure is
|
|
44
|
+
* the failure.
|
|
45
|
+
*/
|
|
46
|
+
export function verovioLoader(deps) {
|
|
47
|
+
const prepared = async (m, report) => {
|
|
48
|
+
report({ phase: "prepare" });
|
|
49
|
+
return deps.instantiate(m);
|
|
50
|
+
};
|
|
51
|
+
const own = async (report) => {
|
|
52
|
+
report(UNMEASURED);
|
|
53
|
+
return { toolkit: await prepared(await deps.own(), report), from: {} };
|
|
54
|
+
};
|
|
55
|
+
const fromUrl = async (verovioUrl, integrity, report) => {
|
|
56
|
+
const options = integrity === undefined ? { onProgress: report } : { integrity, onProgress: report };
|
|
57
|
+
const blob = await deps.fetchModule(verovioUrl, options);
|
|
58
|
+
return prepared(await deps.importBlob(blob), report);
|
|
59
|
+
};
|
|
60
|
+
return async (source, report) => {
|
|
61
|
+
const { verovioUrl, verovioIntegrity } = source;
|
|
62
|
+
if (verovioUrl === undefined)
|
|
63
|
+
return own(report);
|
|
64
|
+
if (verovioIntegrity === undefined)
|
|
65
|
+
return { toolkit: await fromUrl(verovioUrl, undefined, report), from: { verovioUrl } };
|
|
66
|
+
try {
|
|
67
|
+
return { toolkit: await fromUrl(verovioUrl, verovioIntegrity, report), from: source };
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
deps.warn(`<barmark-score>: Verovio from ${verovioUrl} was not used (${String(err)}); loading the package's own copy instead`);
|
|
71
|
+
return own(report);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** A module from a blob, through an object URL revoked once it is imported. */
|
|
76
|
+
async function importBlob(blob) {
|
|
77
|
+
const url = URL.createObjectURL(blob);
|
|
78
|
+
try {
|
|
79
|
+
return await import(/* @vite-ignore */ url);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
URL.revokeObjectURL(url);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const toolkit = toolkitLoader(verovioLoader({
|
|
86
|
+
fetchModule: (url, options) => fetchModule(url, { ...options, fetch: (u) => fetch(u), expected: VEROVIO_MODULE_BYTES }),
|
|
87
|
+
importBlob,
|
|
88
|
+
own: () => import("verovio/wasm"),
|
|
89
|
+
instantiate: async (m) => {
|
|
90
|
+
const module = await m.default();
|
|
91
|
+
enableLog(LOG_OFF, module);
|
|
92
|
+
return new VerovioToolkit(module);
|
|
93
|
+
},
|
|
94
|
+
warn: (message) => console.warn(message),
|
|
95
|
+
}));
|
|
96
|
+
/** `request`'s Verovio source, without its absent fields. */
|
|
97
|
+
const sourceOf = ({ verovioUrl, verovioIntegrity }) => ({
|
|
98
|
+
...(verovioUrl === undefined ? {} : { verovioUrl }),
|
|
99
|
+
...(verovioIntegrity === undefined ? {} : { verovioIntegrity }),
|
|
30
100
|
});
|
|
101
|
+
/**
|
|
102
|
+
* The toolkit for `request`, posting its load's progress, then saying it
|
|
103
|
+
* is `Ready` (and where it came from) once loaded, before the job is
|
|
104
|
+
* engraved: the engine starts the job's deadline then, so a slow download
|
|
105
|
+
* is not counted against it.
|
|
106
|
+
*/
|
|
107
|
+
export function withReady(loader, request, post) {
|
|
108
|
+
return async () => {
|
|
109
|
+
const toolkit = await loader.ready(sourceOf(request), (status) => post({ id: request.id, status }));
|
|
110
|
+
post({ id: request.id, loaded: loader.loaded() ?? {} });
|
|
111
|
+
return toolkit;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Answers one request: a job is engraved (`engrave`, which never throws)
|
|
116
|
+
* and replied to; a preload only loads, posting `Ready`, or a reply with
|
|
117
|
+
* the reason when the load fails.
|
|
118
|
+
*/
|
|
119
|
+
export async function answer(request, loader, post, engrave) {
|
|
120
|
+
const ready = withReady(loader, request, post);
|
|
121
|
+
if (!request.preload)
|
|
122
|
+
return post(await engrave(request, ready));
|
|
123
|
+
try {
|
|
124
|
+
await ready();
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
post({ id: request.id, ok: false, reason: String(err) });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
31
130
|
/** Answer one request. Never throws: a failure is a reply with its reason. */
|
|
32
131
|
export async function handle(request, loaded) {
|
|
33
132
|
const { id, text, width } = request;
|
|
@@ -45,8 +144,5 @@ const scope = globalThis;
|
|
|
45
144
|
// Only a worker global answers messages: importing this in a page or a test has no effect.
|
|
46
145
|
if (typeof scope.WorkerGlobalScope === "function" &&
|
|
47
146
|
globalThis instanceof scope.WorkerGlobalScope) {
|
|
48
|
-
scope.onmessage =
|
|
49
|
-
const { verovioUrl } = e.data;
|
|
50
|
-
scope.postMessage(await handle(e.data, () => ready(verovioUrl)));
|
|
51
|
-
};
|
|
147
|
+
scope.onmessage = (e) => answer(e.data, toolkit, (m) => scope.postMessage(m), handle);
|
|
52
148
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barmark/score",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Show a barmark chord chart as a full engraved score: MusicXML from @barmark/musicxml, engraved by Verovio (LGPL-3.0, a dependency loaded unchanged).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -49,10 +49,10 @@
|
|
|
49
49
|
"url": "https://github.com/kevinpeckham/barmark/issues"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@barmark/musicxml": "^0.
|
|
53
|
-
"@barmark/render": "^0.
|
|
54
|
-
"barmark": "^0.
|
|
55
|
-
"verovio": "
|
|
52
|
+
"@barmark/musicxml": "^0.8.0",
|
|
53
|
+
"@barmark/render": "^0.18.0",
|
|
54
|
+
"barmark": "^0.22.0",
|
|
55
|
+
"verovio": "6.4.1"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|