@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/CHANGELOG.md +83 -1
- package/README.md +124 -21
- package/dist/download.d.ts +43 -0
- package/dist/download.js +118 -0
- package/dist/element.d.ts +2 -1
- package/dist/element.js +129 -16
- package/dist/engine.d.ts +45 -9
- package/dist/engine.js +67 -10
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- 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 +49 -13
- package/dist/worker.js +96 -24
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,86 @@ package uses [semantic versioning](https://semver.org/).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.4.0] - 2026-11-10
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- **The grid takes `columns`** (`@barmark/render`'s `columns: "shared" |
|
|
14
|
+
"row"`): `<barmark-score>` observes the `columns` attribute and redraws
|
|
15
|
+
its grid, and `renderScoreHtml` passes the option through. With
|
|
16
|
+
`columns="row"` each row of the grid sizes its own bars; the score view
|
|
17
|
+
is unchanged.
|
|
18
|
+
- **A `scoreerror` event says why the score couldn't be drawn.** Whenever
|
|
19
|
+
`<barmark-score>` shows "The score couldn't be drawn", it fires
|
|
20
|
+
`scoreerror` (`bubbles`, `composed`) with `detail: { reason }`, the
|
|
21
|
+
reason its console warning gives: Verovio failing to load or engrave, a
|
|
22
|
+
refused `verovio-url` or `verovio-integrity`, a `src` that could not be
|
|
23
|
+
loaded, or an engraving that is not clean SVG. Once per failure; a
|
|
24
|
+
retry the chart or a new URL enables fires again if it fails.
|
|
25
|
+
|
|
26
|
+
### Changed
|
|
27
|
+
|
|
28
|
+
- **Requires `barmark` 0.23.0, `@barmark/render` 0.19.0** (the tempo
|
|
29
|
+
beat, the `rehearsal-letters` rule) **and `@barmark/musicxml` 0.9.0**
|
|
30
|
+
(an ending across lines), whose minor releases a caret range would not
|
|
31
|
+
reach.
|
|
32
|
+
- **The grid follows `@barmark/render`'s new `rehearsal-letters` rule**:
|
|
33
|
+
no letters beside a chart's own rehearsal marks (`@A`), so the grid's
|
|
34
|
+
headings no longer show two marks, matching the score, which draws only
|
|
35
|
+
the chart's own.
|
|
36
|
+
|
|
37
|
+
### Fixed
|
|
38
|
+
|
|
39
|
+
- **The grid's tempo shows the beat it counts**, from `@barmark/render`:
|
|
40
|
+
"♩. = 40" in 6/8, 9/8 and 12/8, where it showed "♩ = 40".
|
|
41
|
+
|
|
42
|
+
### Technical
|
|
43
|
+
|
|
44
|
+
- **The README says when Vite needs `worker: { format: "es" }`**: on Vite 8
|
|
45
|
+
too, with `verovio-url`, or the worker inlines Verovio's fallback copy
|
|
46
|
+
and a reader downloads Verovio twice.
|
|
47
|
+
|
|
48
|
+
## [0.3.0] - 2026-11-10
|
|
49
|
+
|
|
50
|
+
### Added
|
|
51
|
+
|
|
52
|
+
- **Loading progress.** While its first score waits, `<barmark-score>`
|
|
53
|
+
shows a themed status region (`role="status"`) and progress bar in
|
|
54
|
+
three phases: "Loading the score engine…" (with a percentage while the
|
|
55
|
+
download can be measured, and "X of Y MB" only when Y is the size
|
|
56
|
+
actually transferred), "Preparing…" while Verovio's WebAssembly
|
|
57
|
+
compiles and "Engraving…". Each phase is announced once; the bar
|
|
58
|
+
carries the percentage and stands still under `prefers-reduced-motion`.
|
|
59
|
+
A `verovio-url` is now downloaded by the worker with progress and
|
|
60
|
+
imported from a `blob:` URL. A compressed response is measured against
|
|
61
|
+
Verovio's decoded size (`VEROVIO_MODULE_BYTES`), never against its
|
|
62
|
+
compressed `Content-Length`, so the bar never runs past 100%. The
|
|
63
|
+
worker's new `Progress` message carries it (`LoadStatus`), and
|
|
64
|
+
`Engine.engrave` takes an `EngraveCall` with `onStatus`
|
|
65
|
+
(`EngineStatus`).
|
|
66
|
+
- **`verovio-integrity`.** With an SRI digest (`sha256-`, `sha384-` or
|
|
67
|
+
`sha512-`), the worker checks the `verovio-url` copy with
|
|
68
|
+
`crypto.subtle` before importing it, and on a mismatch or any failure
|
|
69
|
+
to load it warns, naming the reason, and loads the package's own copy
|
|
70
|
+
instead, once. `VEROVIO_CDN_URL` and `VEROVIO_CDN_INTEGRITY` are
|
|
71
|
+
Verovio 6.4.1 on jsDelivr and its sha384, with `VEROVIO_VERSION` and
|
|
72
|
+
the `VerovioSource` type.
|
|
73
|
+
- **`preload`.** `"intent"` starts loading Verovio on the first pointer,
|
|
74
|
+
focus or touch on the Score switch; `"idle"` once the page is idle
|
|
75
|
+
(`requestIdleCallback`, or half a second later), or on intent when the
|
|
76
|
+
reader saves data or is on 2G. `"none"`, the default, keeps today's
|
|
77
|
+
behaviour. A preload loads the engine only, shares the page's one load
|
|
78
|
+
and does nothing the second time (`Engine.preload`).
|
|
79
|
+
|
|
80
|
+
### Changed
|
|
81
|
+
|
|
82
|
+
- **Verovio is pinned exactly at 6.4.1** (was `^6.4.1`): the decoded size
|
|
83
|
+
and the CDN digest are those of that build.
|
|
84
|
+
- **A `verovio-url` is fetched, then imported from a `blob:` URL**, with
|
|
85
|
+
or without `verovio-integrity`, where the worker used to `import()` it
|
|
86
|
+
directly. A worker served with its own Content-Security-Policy needs
|
|
87
|
+
the module's origin in `connect-src` and `blob:` in `script-src`.
|
|
88
|
+
|
|
9
89
|
## [0.2.0] - 2026-11-09
|
|
10
90
|
|
|
11
91
|
### Added
|
|
@@ -146,7 +226,9 @@ First release.
|
|
|
146
226
|
needs no config; Vite 7 needs `optimizeDeps.exclude` and
|
|
147
227
|
`worker.format: "es"`.
|
|
148
228
|
|
|
149
|
-
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/score-v0.
|
|
229
|
+
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/score-v0.4.0...HEAD
|
|
230
|
+
[0.4.0]: https://github.com/kevinpeckham/barmark/compare/score-v0.3.0...score-v0.4.0
|
|
231
|
+
[0.3.0]: https://github.com/kevinpeckham/barmark/compare/score-v0.2.0...score-v0.3.0
|
|
150
232
|
[0.2.0]: https://github.com/kevinpeckham/barmark/compare/score-v0.1.1...score-v0.2.0
|
|
151
233
|
[0.1.1]: https://github.com/kevinpeckham/barmark/compare/score-v0.1.0...score-v0.1.1
|
|
152
234
|
[0.1.0]: https://github.com/kevinpeckham/barmark/releases/tag/score-v0.1.0
|
package/README.md
CHANGED
|
@@ -14,9 +14,9 @@ npm install @barmark/score
|
|
|
14
14
|
- **`<barmark-chart>`** from [`@barmark/render`](../render/) is the light
|
|
15
15
|
one: the bar grid, no Verovio.
|
|
16
16
|
- **`<barmark-score>`** from this package is the heavier one: the same grid,
|
|
17
|
-
plus a Grid / Score switch to a full score. Verovio (about 7 MB
|
|
18
|
-
|
|
19
|
-
serves every score on the page.
|
|
17
|
+
plus a Grid / Score switch to a full score. Verovio (about 7 MB, 2.4 MB
|
|
18
|
+
gzipped) loads on the first switch to the score, never on page load
|
|
19
|
+
unless you ask (`preload`), and one copy serves every score on the page.
|
|
20
20
|
|
|
21
21
|
Use `<barmark-chart>` when the grid is enough.
|
|
22
22
|
|
|
@@ -46,13 +46,17 @@ technology the score is one image named for the chart's title, or "Score".
|
|
|
46
46
|
|
|
47
47
|
| Attribute (property) | |
|
|
48
48
|
|---|---|
|
|
49
|
-
| `src`, `theme`, `bar-numbers`, `rehearsal-letters`, `numerals`, `wrap`, `show-part` | the grid's own, as in [`@barmark/render`](../render/README.md#options-and-attributes) |
|
|
49
|
+
| `src`, `theme`, `bar-numbers`, `rehearsal-letters`, `numerals`, `wrap`, `show-part`, `columns` | the grid's own, as in [`@barmark/render`](../render/README.md#options-and-attributes) |
|
|
50
50
|
| `view` (`view`) | `"grid"` (default) or `"score"`; the view being shown. Set before the chart arrives, `"score"` waits for it |
|
|
51
51
|
| `verovio-url` | where to load Verovio from (below) |
|
|
52
|
+
| `verovio-integrity` | the SRI digest (`sha384-…`) the `verovio-url` copy must have; on a mismatch the package's own copy loads (below) |
|
|
53
|
+
| `preload` | `"none"` (default), `"intent"` or `"idle"`: when to start loading Verovio (below) |
|
|
52
54
|
|
|
53
55
|
The `text` property is the chart's text (`null` goes back to `src` or the
|
|
54
56
|
script child). The element fires a `viewchange` event, with
|
|
55
|
-
`detail.view`, whenever the view changes, from the switch or from script
|
|
57
|
+
`detail.view`, whenever the view changes, from the switch or from script,
|
|
58
|
+
and a `scoreerror` event, with `detail.reason`, when the score can't be
|
|
59
|
+
drawn (below).
|
|
56
60
|
|
|
57
61
|
```js
|
|
58
62
|
const score = document.querySelector("barmark-score");
|
|
@@ -67,16 +71,21 @@ The engine runs in a module Worker, created as
|
|
|
67
71
|
That is the pattern Vite and webpack follow, so a bundler finds the worker
|
|
68
72
|
and Verovio for you.
|
|
69
73
|
|
|
70
|
-
Vite 8 (and Vite+)
|
|
71
|
-
|
|
72
|
-
the
|
|
73
|
-
|
|
74
|
+
Vite 8 (and Vite+) draws a score with no config, in `vite dev` or a
|
|
75
|
+
build. But Vite builds workers as `iife` by default, which cannot split, so
|
|
76
|
+
the worker's fallback copy of Verovio is inlined into it: with
|
|
77
|
+
`verovio-url`, a reader then downloads Verovio twice, inside the worker and
|
|
78
|
+
from your URL. Set `worker: { format: "es" }` and Verovio becomes a chunk
|
|
79
|
+
of its own, fetched only if your URL fails. Vite 7 needs that line in any
|
|
80
|
+
case (an `iife` worker cannot load Verovio on demand) and one more: its dev
|
|
81
|
+
pre-bundler moves the element away from the worker and the font it finds
|
|
82
|
+
by URL.
|
|
74
83
|
|
|
75
84
|
```js
|
|
76
|
-
// vite.config.js
|
|
85
|
+
// vite.config.js
|
|
77
86
|
export default {
|
|
78
|
-
|
|
79
|
-
|
|
87
|
+
worker: { format: "es" }, // Vite 8 with verovio-url; Vite 7 always
|
|
88
|
+
optimizeDeps: { exclude: ["@barmark/score"] }, // Vite 7 only (harmless on Vite 8)
|
|
80
89
|
};
|
|
81
90
|
```
|
|
82
91
|
|
|
@@ -89,17 +98,110 @@ bundling the 7 MB inside the worker; it only changes where that module
|
|
|
89
98
|
comes from. The URL is resolved against the page and must be http(s);
|
|
90
99
|
anything else is refused and the score is not drawn. The first
|
|
91
100
|
`verovio-url` that loads wins for the whole page: every element shares one
|
|
92
|
-
worker, so a later or different URL is not loaded.
|
|
101
|
+
worker, so a later or different URL is not loaded. The worker downloads a
|
|
102
|
+
`verovio-url` with `fetch` (with or without `verovio-integrity`) and
|
|
103
|
+
imports it from a `blob:` URL, so a worker served with its own
|
|
104
|
+
Content-Security-Policy needs the module's origin (a CDN's, say) in
|
|
105
|
+
`connect-src` and `blob:` in `script-src`.
|
|
106
|
+
|
|
107
|
+
### While the score loads
|
|
108
|
+
|
|
109
|
+
The first score waits for Verovio to download and compile. Until it is
|
|
110
|
+
drawn, the score pane shows a status region (`role="status"`) with a
|
|
111
|
+
progress bar, in three phases:
|
|
112
|
+
|
|
113
|
+
1. **Loading the score engine…**, with how far the download has come
|
|
114
|
+
when that can be measured: a percentage, and "3.1 of 7.4 MB" only when
|
|
115
|
+
the total is the size actually transferred.
|
|
116
|
+
2. **Preparing…** while the worker compiles Verovio's WebAssembly.
|
|
117
|
+
3. **Engraving…** while the chart is engraved.
|
|
118
|
+
|
|
119
|
+
The live region speaks each phase once; the percentage is the progress
|
|
120
|
+
bar's value. With `prefers-reduced-motion`, the bar does not animate.
|
|
121
|
+
|
|
122
|
+
The download can be measured when the worker fetches Verovio itself,
|
|
123
|
+
which it does for a `verovio-url`: it reads the response as it arrives,
|
|
124
|
+
then imports the module from a `blob:` URL. A compressed response (gzip,
|
|
125
|
+
brotli) has a `Content-Length` that is the compressed size, while the
|
|
126
|
+
bytes read are decoded, so they are measured against Verovio's known
|
|
127
|
+
decoded size (`VEROVIO_MODULE_BYTES`) instead; an uncompressed one is
|
|
128
|
+
measured against its `Content-Length` (a cross-origin server that does
|
|
129
|
+
not expose `Content-Encoding` is taken as compressed when its length is
|
|
130
|
+
short of that size); anything else shows the bar without a percentage.
|
|
131
|
+
The package's own copy, without `verovio-url`, is loaded by your
|
|
132
|
+
bundler, so its download is shown without a percentage; so is a worker
|
|
133
|
+
bundled with Verovio inside it, whose download is the worker's own
|
|
134
|
+
script.
|
|
135
|
+
|
|
136
|
+
### A copy from a CDN
|
|
137
|
+
|
|
138
|
+
`verovio-url` can point at a public copy of the exact Verovio this
|
|
139
|
+
package uses, checked with `verovio-integrity`. For Verovio 6.4.1 on
|
|
140
|
+
jsDelivr (served with CORS `*` and cached as immutable):
|
|
141
|
+
|
|
142
|
+
```html
|
|
143
|
+
<barmark-score
|
|
144
|
+
verovio-url="https://cdn.jsdelivr.net/npm/verovio@6.4.1/dist/verovio-module.mjs"
|
|
145
|
+
verovio-integrity="sha384-iGNyhtxpKLNw90cjbI9ES538TJtbNZjLxsFVUuI8PwE2TTNQ+i2j9CjQnRMhZXHk"
|
|
146
|
+
src="/charts/peaceful-dreams.chart.md"
|
|
147
|
+
></barmark-score>
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The package exports both as `VEROVIO_CDN_URL` and `VEROVIO_CDN_INTEGRITY`
|
|
151
|
+
(with `VEROVIO_VERSION`). With `verovio-integrity` set, the worker
|
|
152
|
+
downloads the URL, checks its digest (`sha256-`, `sha384-` or `sha512-`)
|
|
153
|
+
with `crypto.subtle` before importing it, and on a mismatch or any
|
|
154
|
+
failure to load it warns in the console, naming the reason, and loads the
|
|
155
|
+
package's own copy instead, once. Without `verovio-integrity` there is no
|
|
156
|
+
fallback: a `verovio-url` that fails is a score that can't be drawn. A
|
|
157
|
+
malformed digest is refused like a bad URL.
|
|
158
|
+
|
|
159
|
+
A CDN does not save your readers a download they made on another site:
|
|
160
|
+
browsers partition their caches by site, so each site's readers fetch
|
|
161
|
+
Verovio once for that site whichever URL it comes from. What a CDN gives
|
|
162
|
+
is delivery from an edge near the reader and the 2.4 MB kept off your own
|
|
163
|
+
server.
|
|
164
|
+
|
|
165
|
+
### Preloading
|
|
166
|
+
|
|
167
|
+
`preload` starts loading Verovio before the first press, so the score
|
|
168
|
+
comes sooner. It only loads the engine (nothing is engraved), shares the
|
|
169
|
+
one load every element on the page uses, and asking twice does nothing.
|
|
170
|
+
|
|
171
|
+
- `"none"` (the default): Verovio loads on the first switch to the score.
|
|
172
|
+
- `"intent"`: on the first pointer over, focus on or touch of the Score
|
|
173
|
+
switch.
|
|
174
|
+
- `"idle"`: once the page is idle after the element connects
|
|
175
|
+
(`requestIdleCallback`, or half a second later where there is none).
|
|
176
|
+
When the reader has asked to save data, or is on a 2G connection
|
|
177
|
+
(`navigator.connection`), it waits for intent instead.
|
|
93
178
|
|
|
94
179
|
### When the score can't be drawn
|
|
95
180
|
|
|
96
181
|
If Verovio fails to load, or rejects the MusicXML, or the chart will not
|
|
97
182
|
export or is too large to draw (see "Engraving directly"), or Verovio
|
|
98
183
|
takes over 60 seconds to load or 20 seconds to engrave, the grid stays and
|
|
99
|
-
a note by the switch says "The score couldn't be drawn". The
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
184
|
+
a note by the switch says "The score couldn't be drawn". The same happens
|
|
185
|
+
when `verovio-url` or `verovio-integrity` is refused, when the chart's
|
|
186
|
+
`src` could not be loaded, or when the engraving is not clean SVG. The
|
|
187
|
+
Score radio is disabled until the chart (`text`, `src` or the script
|
|
188
|
+
child), `verovio-url` or `verovio-integrity` changes.
|
|
189
|
+
|
|
190
|
+
The reason goes to the console as a warning, and the element fires a
|
|
191
|
+
`scoreerror` event (`bubbles`, `composed`) whose `detail.reason` is the
|
|
192
|
+
same string, so a page can show or log it:
|
|
193
|
+
|
|
194
|
+
```js
|
|
195
|
+
score.addEventListener("scoreerror", (e) => report(e.detail.reason));
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
It fires once per failure, as the note appears, after the grid is shown
|
|
199
|
+
again (and after the `viewchange` back to the grid, when the score was
|
|
200
|
+
being shown). Pressing the disabled Score radio fires nothing; once a
|
|
201
|
+
change enables it, a score that fails again fires again. A score an
|
|
202
|
+
element no longer wants (its chart or width changed) is dropped, not
|
|
203
|
+
failed, and fires nothing: it is cut off after three seconds if the worker
|
|
204
|
+
is already engraving it.
|
|
103
205
|
|
|
104
206
|
## On the server
|
|
105
207
|
|
|
@@ -158,7 +260,8 @@ stays MIT. Verovio © Laurent Pugin and others; its licence text is
|
|
|
158
260
|
A site that bundles the worker ships Verovio inside its bundle. Keeping
|
|
159
261
|
Verovio a separate file (through `verovio-url`, or the bundler's own
|
|
160
262
|
chunk) and keeping its notice is the easy way to meet the LGPL. If it is
|
|
161
|
-
bundled into one file with the worker instead (Vite
|
|
162
|
-
|
|
163
|
-
link your site's source so a visitor
|
|
164
|
-
barmark.dev does all three in its
|
|
263
|
+
bundled into one file with the worker instead (Vite's default `iife`
|
|
264
|
+
worker format does this), credit it with its copyright and licence, host
|
|
265
|
+
or link the LGPL and GPL texts, and link your site's source so a visitor
|
|
266
|
+
can rebuild it with another Verovio; barmark.dev does all three in its
|
|
267
|
+
footer. This is not legal advice.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downloading a Verovio module with progress, and checking its digest.
|
|
3
|
+
* Verovio-free: the worker uses it before any Verovio is loaded.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* How far a download has come: `fraction` from 0 to 1, or null when it
|
|
7
|
+
* cannot be known; `bytes` only when its total is the real transfer size.
|
|
8
|
+
*/
|
|
9
|
+
export type Download = {
|
|
10
|
+
phase: "download";
|
|
11
|
+
fraction: number | null;
|
|
12
|
+
bytes: {
|
|
13
|
+
done: number;
|
|
14
|
+
total: number;
|
|
15
|
+
} | null;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Measures a download whose body arrives decoded. With Content-Encoding,
|
|
19
|
+
* Content-Length is the compressed size, so decoded bytes are measured
|
|
20
|
+
* against it only when the response is not encoded; otherwise against
|
|
21
|
+
* `expected`, the module's decoded size, when known; otherwise not at all.
|
|
22
|
+
* A `crossOrigin` response hides Content-Encoding unless the server
|
|
23
|
+
* exposes it, so there a length short of `expected` is taken as
|
|
24
|
+
* compressed. A body that outgrows the length it is measured against is
|
|
25
|
+
* measured by the next rule.
|
|
26
|
+
*/
|
|
27
|
+
export declare function downloadMeter(headers: Headers, expected?: number, crossOrigin?: boolean): (decoded: number) => Download;
|
|
28
|
+
/** "42%", with "(3.1 of 7.4 MB)" when the total is the real transfer size; "" when unknown. */
|
|
29
|
+
export declare function progressText(d: Download): string;
|
|
30
|
+
/** Whether `integrity` is one SRI digest this package checks: sha256, sha384 or sha512. */
|
|
31
|
+
export declare const isIntegrity: (integrity: string) => boolean;
|
|
32
|
+
/** Whether `bytes` have the SRI digest `integrity` (`sha384-…`); anything else fails. */
|
|
33
|
+
export declare function checkIntegrity(bytes: BufferSource, integrity: string): Promise<boolean>;
|
|
34
|
+
export type FetchModuleOptions = {
|
|
35
|
+
fetch: (url: string) => Promise<Response>;
|
|
36
|
+
onProgress: (d: Download) => void;
|
|
37
|
+
/** The SRI digest the module must have; checked before it is returned. */
|
|
38
|
+
integrity?: string;
|
|
39
|
+
/** The module's decoded size, when known. */
|
|
40
|
+
expected?: number;
|
|
41
|
+
};
|
|
42
|
+
/** Downloads the module at `url` with progress, as a JavaScript blob ready to import from an object URL. */
|
|
43
|
+
export declare function fetchModule(url: string, options: FetchModuleOptions): Promise<Blob>;
|
package/dist/download.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downloading a Verovio module with progress, and checking its digest.
|
|
3
|
+
* Verovio-free: the worker uses it before any Verovio is loaded.
|
|
4
|
+
*/
|
|
5
|
+
const unknown = () => ({ phase: "download", fraction: null, bytes: null });
|
|
6
|
+
const lengthOf = (headers) => {
|
|
7
|
+
const n = Number(headers.get("content-length") ?? "");
|
|
8
|
+
return Number.isSafeInteger(n) && n > 0 ? n : null;
|
|
9
|
+
};
|
|
10
|
+
const encoded = (headers) => {
|
|
11
|
+
const e = headers.get("content-encoding")?.trim().toLowerCase();
|
|
12
|
+
return e !== undefined && e !== "" && e !== "identity";
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Measures a download whose body arrives decoded. With Content-Encoding,
|
|
16
|
+
* Content-Length is the compressed size, so decoded bytes are measured
|
|
17
|
+
* against it only when the response is not encoded; otherwise against
|
|
18
|
+
* `expected`, the module's decoded size, when known; otherwise not at all.
|
|
19
|
+
* A `crossOrigin` response hides Content-Encoding unless the server
|
|
20
|
+
* exposes it, so there a length short of `expected` is taken as
|
|
21
|
+
* compressed. A body that outgrows the length it is measured against is
|
|
22
|
+
* measured by the next rule.
|
|
23
|
+
*/
|
|
24
|
+
export function downloadMeter(headers, expected, crossOrigin = false) {
|
|
25
|
+
const given = lengthOf(headers);
|
|
26
|
+
const hidden = crossOrigin && expected !== undefined && given !== null && given < expected;
|
|
27
|
+
const length = encoded(headers) || hidden ? null : given;
|
|
28
|
+
const byExpected = (done) => expected !== undefined && done <= expected
|
|
29
|
+
? { phase: "download", fraction: done / expected, bytes: null }
|
|
30
|
+
: unknown();
|
|
31
|
+
return (done) => length !== null && done <= length
|
|
32
|
+
? { phase: "download", fraction: done / length, bytes: { done, total: length } }
|
|
33
|
+
: byExpected(done);
|
|
34
|
+
}
|
|
35
|
+
const mb = (n) => (n / 1_000_000).toFixed(1);
|
|
36
|
+
/** "42%", with "(3.1 of 7.4 MB)" when the total is the real transfer size; "" when unknown. */
|
|
37
|
+
export function progressText(d) {
|
|
38
|
+
if (d.fraction === null)
|
|
39
|
+
return "";
|
|
40
|
+
const percent = `${Math.floor(d.fraction * 100)}%`;
|
|
41
|
+
return d.bytes ? `${percent} (${mb(d.bytes.done)} of ${mb(d.bytes.total)} MB)` : percent;
|
|
42
|
+
}
|
|
43
|
+
const SRI = /^(sha256|sha384|sha512)-([A-Za-z0-9+/]+={0,2})$/;
|
|
44
|
+
/** Whether `integrity` is one SRI digest this package checks: sha256, sha384 or sha512. */
|
|
45
|
+
export const isIntegrity = (integrity) => SRI.test(integrity.trim());
|
|
46
|
+
const base64 = (buffer) => {
|
|
47
|
+
let s = "";
|
|
48
|
+
for (const b of new Uint8Array(buffer))
|
|
49
|
+
s += String.fromCharCode(b);
|
|
50
|
+
return btoa(s);
|
|
51
|
+
};
|
|
52
|
+
/** Whether `bytes` have the SRI digest `integrity` (`sha384-…`); anything else fails. */
|
|
53
|
+
export async function checkIntegrity(bytes, integrity) {
|
|
54
|
+
const m = SRI.exec(integrity.trim());
|
|
55
|
+
if (!m)
|
|
56
|
+
return false;
|
|
57
|
+
const alg = `SHA-${m[1].slice(3)}`;
|
|
58
|
+
return base64(await crypto.subtle.digest(alg, bytes)) === m[2];
|
|
59
|
+
}
|
|
60
|
+
/** How often an unmeasurable download reports: every this many bytes. */
|
|
61
|
+
const UNKNOWN_STEP = 512 * 1024;
|
|
62
|
+
/** A download's measure, but unmeasured from the first time it would go down (the body outgrew its length). */
|
|
63
|
+
function rising() {
|
|
64
|
+
let high = -1;
|
|
65
|
+
let lost = false;
|
|
66
|
+
return (d) => {
|
|
67
|
+
const percent = d.fraction === null ? -1 : Math.floor(d.fraction * 100);
|
|
68
|
+
lost ||= percent >= 0 && percent < high;
|
|
69
|
+
high = Math.max(high, percent);
|
|
70
|
+
return lost ? { ...d, fraction: null, bytes: null } : d;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Reports a download only when its whole percentage rises (it never goes
|
|
75
|
+
* down: see `rising`), or, unmeasured, every UNKNOWN_STEP bytes.
|
|
76
|
+
*/
|
|
77
|
+
function throttled(report) {
|
|
78
|
+
const monotone = rising();
|
|
79
|
+
let last = null;
|
|
80
|
+
return (d, done) => {
|
|
81
|
+
const shown = monotone(d);
|
|
82
|
+
const mark = shown.fraction === null
|
|
83
|
+
? -1 - Math.floor(done / UNKNOWN_STEP)
|
|
84
|
+
: Math.floor(shown.fraction * 100);
|
|
85
|
+
if (mark === last)
|
|
86
|
+
return;
|
|
87
|
+
last = mark;
|
|
88
|
+
report(shown);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** Downloads the module at `url` with progress, as a JavaScript blob ready to import from an object URL. */
|
|
92
|
+
export async function fetchModule(url, options) {
|
|
93
|
+
const response = await options.fetch(url);
|
|
94
|
+
if (!response.ok)
|
|
95
|
+
throw new Error(`${response.status} ${response.statusText}`.trim());
|
|
96
|
+
const meter = downloadMeter(response.headers, options.expected, response.type === "cors");
|
|
97
|
+
const report = throttled(options.onProgress);
|
|
98
|
+
const chunks = [];
|
|
99
|
+
let done = 0;
|
|
100
|
+
report(meter(0), 0);
|
|
101
|
+
const reader = response.body?.getReader();
|
|
102
|
+
for (let r = await reader?.read(); r && !r.done; r = await reader?.read()) {
|
|
103
|
+
chunks.push(r.value);
|
|
104
|
+
done += r.value.byteLength;
|
|
105
|
+
report(meter(done), done);
|
|
106
|
+
}
|
|
107
|
+
// One copy of the module: the chunks are joined and dropped before it is digested and wrapped.
|
|
108
|
+
const bytes = new Uint8Array(done);
|
|
109
|
+
let at = 0;
|
|
110
|
+
for (const chunk of chunks.splice(0)) {
|
|
111
|
+
bytes.set(chunk, at);
|
|
112
|
+
at += chunk.byteLength;
|
|
113
|
+
}
|
|
114
|
+
if (options.integrity !== undefined && !(await checkIntegrity(bytes, options.integrity)))
|
|
115
|
+
throw new Error(`${url} does not match its integrity digest`);
|
|
116
|
+
const blob = new Blob([bytes], { type: "text/javascript" });
|
|
117
|
+
return blob;
|
|
118
|
+
}
|
package/dist/element.d.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* Grid / Score switch. Its chart comes from the `text` property, a `src` URL or
|
|
4
4
|
* a child `<script type="text/barmark">`, as `<barmark-chart>`'s does. A shadow
|
|
5
5
|
* root drawn by `renderScoreHtml` is reused. The score engine (Verovio, in a
|
|
6
|
-
* worker) loads on the first switch to the score,
|
|
6
|
+
* worker) loads on the first switch to the score, or earlier as `preload`
|
|
7
|
+
* asks, showing its progress while it loads. Importing
|
|
7
8
|
* this module registers the element (once) and the font (once per document).
|
|
8
9
|
*/
|
|
9
10
|
import type { Engine } from "./engine.js";
|