@barmark/score 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/dist/worker.d.ts CHANGED
@@ -1,26 +1,62 @@
1
1
  import { VerovioToolkit } from "verovio/esm";
2
- import type { Ready, Reply, Request } from "./engine.js";
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
+ };
6
13
  /**
7
14
  * A worker's one Verovio toolkit, loaded on first use and reused, and
8
- * where it came from once loaded (no `verovioUrl`: the package's own), so
9
- * the engine can give a replacement worker the same. The first load that
10
- * succeeds wins for the worker's life: later URLs are ignored.
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.
11
18
  */
12
- export declare function toolkitLoader<T>(load: (verovioUrl: string | undefined) => Promise<T>): {
13
- ready: (arg: string | undefined) => Promise<T>;
14
- loaded: () => {
15
- verovioUrl?: string;
16
- } | undefined;
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;
17
37
  };
18
38
  /**
19
- * The toolkit for `request`, saying it is `Ready` (and where it came
20
- * from) once loaded, before the job is engraved: the engine starts the
21
- * job's deadline then, so a slow download is not counted against it.
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.
22
58
  */
23
- export declare function withReady<T>(loader: ReturnType<typeof toolkitLoader<T>>, request: Request, post: (ready: Ready) => void): () => Promise<T>;
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>;
24
60
  /** Answer one request. Never throws: a failure is a reply with its reason. */
25
61
  export declare function handle(request: Request, loaded: () => Promise<Toolkit>): Promise<Reply>;
26
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. */
@@ -16,43 +18,115 @@ export function loadOnce(load) {
16
18
  }
17
19
  /**
18
20
  * A worker's one Verovio toolkit, loaded on first use and reused, and
19
- * where it came from once loaded (no `verovioUrl`: the package's own), so
20
- * the engine can give a replacement worker the same. The first load that
21
- * succeeds wins for the worker's life: later URLs are ignored.
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
25
  export function toolkitLoader(load) {
24
26
  let loaded;
25
- const ready = loadOnce(async (verovioUrl) => {
26
- const toolkit = await load(verovioUrl);
27
- loaded = verovioUrl === undefined ? {} : { verovioUrl };
28
- return toolkit;
27
+ const once = loadOnce(async ({ source, report }) => {
28
+ const r = await load(source, report);
29
+ loaded = r.from;
30
+ return r.toolkit;
29
31
  });
30
- return { ready, loaded: () => loaded };
32
+ return {
33
+ ready: (source, report) => once({ source, report }),
34
+ loaded: () => loaded,
35
+ };
31
36
  }
37
+ const UNMEASURED = { phase: "download", fraction: null, bytes: null };
32
38
  /**
33
- * By default the package's own `verovio/wasm`; when a request carries
34
- * `verovioUrl` that module is imported instead.
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.
35
45
  */
36
- const toolkit = toolkitLoader(async (verovioUrl) => {
37
- const m = verovioUrl
38
- ? await import(/* @vite-ignore */ verovioUrl)
39
- : await import("verovio/wasm");
40
- const module = await m.default();
41
- enableLog(LOG_OFF, module);
42
- return new VerovioToolkit(module);
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 }),
43
100
  });
44
101
  /**
45
- * The toolkit for `request`, saying it is `Ready` (and where it came
46
- * from) once loaded, before the job is engraved: the engine starts the
47
- * job's deadline then, so a slow download is not counted against it.
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.
48
106
  */
49
107
  export function withReady(loader, request, post) {
50
108
  return async () => {
51
- const toolkit = await loader.ready(request.verovioUrl);
109
+ const toolkit = await loader.ready(sourceOf(request), (status) => post({ id: request.id, status }));
52
110
  post({ id: request.id, loaded: loader.loaded() ?? {} });
53
111
  return toolkit;
54
112
  };
55
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
+ }
56
130
  /** Answer one request. Never throws: a failure is a reply with its reason. */
57
131
  export async function handle(request, loaded) {
58
132
  const { id, text, width } = request;
@@ -70,7 +144,5 @@ const scope = globalThis;
70
144
  // Only a worker global answers messages: importing this in a page or a test has no effect.
71
145
  if (typeof scope.WorkerGlobalScope === "function" &&
72
146
  globalThis instanceof scope.WorkerGlobalScope) {
73
- scope.onmessage = async (e) => {
74
- scope.postMessage(await handle(e.data, withReady(toolkit, e.data, (r) => scope.postMessage(r))));
75
- };
147
+ scope.onmessage = (e) => answer(e.data, toolkit, (m) => scope.postMessage(m), handle);
76
148
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barmark/score",
3
- "version": "0.2.0",
3
+ "version": "0.4.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.8.0",
53
- "@barmark/render": "^0.18.0",
54
- "barmark": "^0.22.0",
55
- "verovio": "^6.4.1"
52
+ "@barmark/musicxml": "^0.9.0",
53
+ "@barmark/render": "^0.19.0",
54
+ "barmark": "^0.23.0",
55
+ "verovio": "6.4.1"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "rm -rf dist && tsc -p tsconfig.build.json",