@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/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,102 @@ package uses [semantic versioning](https://semver.org/).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.3.0] - 2026-11-10
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- **Loading progress.** While its first score waits, `<barmark-score>`
|
|
14
|
+
shows a themed status region (`role="status"`) and progress bar in
|
|
15
|
+
three phases: "Loading the score engine…" (with a percentage while the
|
|
16
|
+
download can be measured, and "X of Y MB" only when Y is the size
|
|
17
|
+
actually transferred), "Preparing…" while Verovio's WebAssembly
|
|
18
|
+
compiles and "Engraving…". Each phase is announced once; the bar
|
|
19
|
+
carries the percentage and stands still under `prefers-reduced-motion`.
|
|
20
|
+
A `verovio-url` is now downloaded by the worker with progress and
|
|
21
|
+
imported from a `blob:` URL. A compressed response is measured against
|
|
22
|
+
Verovio's decoded size (`VEROVIO_MODULE_BYTES`), never against its
|
|
23
|
+
compressed `Content-Length`, so the bar never runs past 100%. The
|
|
24
|
+
worker's new `Progress` message carries it (`LoadStatus`), and
|
|
25
|
+
`Engine.engrave` takes an `EngraveCall` with `onStatus`
|
|
26
|
+
(`EngineStatus`).
|
|
27
|
+
- **`verovio-integrity`.** With an SRI digest (`sha256-`, `sha384-` or
|
|
28
|
+
`sha512-`), the worker checks the `verovio-url` copy with
|
|
29
|
+
`crypto.subtle` before importing it, and on a mismatch or any failure
|
|
30
|
+
to load it warns, naming the reason, and loads the package's own copy
|
|
31
|
+
instead, once. `VEROVIO_CDN_URL` and `VEROVIO_CDN_INTEGRITY` are
|
|
32
|
+
Verovio 6.4.1 on jsDelivr and its sha384, with `VEROVIO_VERSION` and
|
|
33
|
+
the `VerovioSource` type.
|
|
34
|
+
- **`preload`.** `"intent"` starts loading Verovio on the first pointer,
|
|
35
|
+
focus or touch on the Score switch; `"idle"` once the page is idle
|
|
36
|
+
(`requestIdleCallback`, or half a second later), or on intent when the
|
|
37
|
+
reader saves data or is on 2G. `"none"`, the default, keeps today's
|
|
38
|
+
behaviour. A preload loads the engine only, shares the page's one load
|
|
39
|
+
and does nothing the second time (`Engine.preload`).
|
|
40
|
+
|
|
41
|
+
### Changed
|
|
42
|
+
|
|
43
|
+
- **Verovio is pinned exactly at 6.4.1** (was `^6.4.1`): the decoded size
|
|
44
|
+
and the CDN digest are those of that build.
|
|
45
|
+
- **A `verovio-url` is fetched, then imported from a `blob:` URL**, with
|
|
46
|
+
or without `verovio-integrity`, where the worker used to `import()` it
|
|
47
|
+
directly. A worker served with its own Content-Security-Policy needs
|
|
48
|
+
the module's origin in `connect-src` and `blob:` in `script-src`.
|
|
49
|
+
|
|
50
|
+
## [0.2.0] - 2026-11-09
|
|
51
|
+
|
|
52
|
+
### Added
|
|
53
|
+
|
|
54
|
+
- **Limits on what a score may cost, exported.** `EngraveOptions.maxCost`
|
|
55
|
+
and `maxXmlLength`, `scoreCost`, `MAX_SCORE_COST` and `MAX_XML_LENGTH`
|
|
56
|
+
(`engraveChart`), and `createEngine`'s options `grace`, `loadDeadline`
|
|
57
|
+
and `deadline` with their defaults `SUPERSEDED_GRACE_MS`,
|
|
58
|
+
`LOAD_DEADLINE_MS` and `JOB_DEADLINE_MS`, and the `EngineOptions`,
|
|
59
|
+
`Ready` and `WorkerMessage` types. What they bound is under Fixed.
|
|
60
|
+
|
|
61
|
+
### Changed
|
|
62
|
+
|
|
63
|
+
- **Requires `barmark` 0.22.0, `@barmark/render` 0.18.0 and
|
|
64
|
+
`@barmark/musicxml` 0.8.0**, whose minor releases a caret range would
|
|
65
|
+
not reach. Grid and Score now number a pickup alike: bar 0, with the
|
|
66
|
+
first full bar 1.
|
|
67
|
+
|
|
68
|
+
### Fixed
|
|
69
|
+
|
|
70
|
+
- **A score too large to draw is refused, not attempted.** A short chart
|
|
71
|
+
with repeats can play thousands of bars, and writing a crowded one out
|
|
72
|
+
took gigabytes of MusicXML (and a phone's tab). The export now stops
|
|
73
|
+
writing at `MAX_XML_LENGTH` (50,000,000 characters, `@barmark/musicxml`'s
|
|
74
|
+
new `maxLength`), and before it, as a cheap early refusal, `engraveChart`
|
|
75
|
+
estimates the score's size (`scoreCost`: every played bar's measure in
|
|
76
|
+
every declared part, tacet ones too, each slot, pitch, chord degree,
|
|
77
|
+
syllable and words with the length of its escaped text, and each bar's
|
|
78
|
+
heading, in one pass) and refuses more than `MAX_SCORE_COST`
|
|
79
|
+
(50,000,000) unexported. Either way the reason is "The score is too
|
|
80
|
+
large to draw", so the element shows its note. `EngraveOptions.maxCost`
|
|
81
|
+
and `maxXmlLength` set them. Needs the `@barmark/musicxml` release that
|
|
82
|
+
adds `maxLength`.
|
|
83
|
+
|
|
84
|
+
- **A slow score no longer holds up every later one.** The worker takes
|
|
85
|
+
one job at a time; a request its element has since replaced is never
|
|
86
|
+
sent, and one already engraving is cut off (the worker is terminated and
|
|
87
|
+
a fresh one takes the requests still waiting) once it has engraved for
|
|
88
|
+
`SUPERSEDED_GRACE_MS` (3 s), so a normal job still finishes and keeps
|
|
89
|
+
the loaded Verovio. A job still loading Verovio is never cut for this.
|
|
90
|
+
Loading Verovio has `LOAD_DEADLINE_MS` (60 s, for a 7 MB download on a
|
|
91
|
+
slow line) and engraving `JOB_DEADLINE_MS` (20 s, from the worker's new
|
|
92
|
+
`Ready` message); past either the job is cut off and its request rejects
|
|
93
|
+
with "The score engine failed", so the element shows its note. A worker
|
|
94
|
+
that crashes fails only the job it was on; the requests still waiting
|
|
95
|
+
run on a fresh one. All three are `createEngine` options (`grace`,
|
|
96
|
+
`loadDeadline`, `deadline`). A fresh worker loads the Verovio the first
|
|
97
|
+
one loaded (its `Ready` says which), so the first `verovio-url` that
|
|
98
|
+
loads still wins for the page.
|
|
99
|
+
|
|
100
|
+
### Technical
|
|
101
|
+
|
|
102
|
+
- **The README gives Verovio's copyright and source**, and how to build with
|
|
103
|
+
another Verovio (LGPL-3.0).
|
|
104
|
+
|
|
9
105
|
## [0.1.1] - 2026-11-09
|
|
10
106
|
|
|
11
107
|
Requires `@barmark/musicxml` 0.7.2.
|
|
@@ -91,6 +187,8 @@ First release.
|
|
|
91
187
|
needs no config; Vite 7 needs `optimizeDeps.exclude` and
|
|
92
188
|
`worker.format: "es"`.
|
|
93
189
|
|
|
94
|
-
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/score-v0.
|
|
190
|
+
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/score-v0.3.0...HEAD
|
|
191
|
+
[0.3.0]: https://github.com/kevinpeckham/barmark/compare/score-v0.2.0...score-v0.3.0
|
|
192
|
+
[0.2.0]: https://github.com/kevinpeckham/barmark/compare/score-v0.1.1...score-v0.2.0
|
|
95
193
|
[0.1.1]: https://github.com/kevinpeckham/barmark/compare/score-v0.1.0...score-v0.1.1
|
|
96
194
|
[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
|
|
|
@@ -49,6 +49,8 @@ technology the score is one image named for the chart's title, or "Score".
|
|
|
49
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) |
|
|
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
|
|
@@ -89,13 +91,93 @@ bundling the 7 MB inside the worker; it only changes where that module
|
|
|
89
91
|
comes from. The URL is resolved against the page and must be http(s);
|
|
90
92
|
anything else is refused and the score is not drawn. The first
|
|
91
93
|
`verovio-url` that loads wins for the whole page: every element shares one
|
|
92
|
-
worker, so a later or different URL is not loaded.
|
|
94
|
+
worker, so a later or different URL is not loaded. The worker downloads a
|
|
95
|
+
`verovio-url` with `fetch` (with or without `verovio-integrity`) and
|
|
96
|
+
imports it from a `blob:` URL, so a worker served with its own
|
|
97
|
+
Content-Security-Policy needs the module's origin (a CDN's, say) in
|
|
98
|
+
`connect-src` and `blob:` in `script-src`.
|
|
99
|
+
|
|
100
|
+
### While the score loads
|
|
101
|
+
|
|
102
|
+
The first score waits for Verovio to download and compile. Until it is
|
|
103
|
+
drawn, the score pane shows a status region (`role="status"`) with a
|
|
104
|
+
progress bar, in three phases:
|
|
105
|
+
|
|
106
|
+
1. **Loading the score engine…**, with how far the download has come
|
|
107
|
+
when that can be measured: a percentage, and "3.1 of 7.4 MB" only when
|
|
108
|
+
the total is the size actually transferred.
|
|
109
|
+
2. **Preparing…** while the worker compiles Verovio's WebAssembly.
|
|
110
|
+
3. **Engraving…** while the chart is engraved.
|
|
111
|
+
|
|
112
|
+
The live region speaks each phase once; the percentage is the progress
|
|
113
|
+
bar's value. With `prefers-reduced-motion`, the bar does not animate.
|
|
114
|
+
|
|
115
|
+
The download can be measured when the worker fetches Verovio itself,
|
|
116
|
+
which it does for a `verovio-url`: it reads the response as it arrives,
|
|
117
|
+
then imports the module from a `blob:` URL. A compressed response (gzip,
|
|
118
|
+
brotli) has a `Content-Length` that is the compressed size, while the
|
|
119
|
+
bytes read are decoded, so they are measured against Verovio's known
|
|
120
|
+
decoded size (`VEROVIO_MODULE_BYTES`) instead; an uncompressed one is
|
|
121
|
+
measured against its `Content-Length` (a cross-origin server that does
|
|
122
|
+
not expose `Content-Encoding` is taken as compressed when its length is
|
|
123
|
+
short of that size); anything else shows the bar without a percentage.
|
|
124
|
+
The package's own copy, without `verovio-url`, is loaded by your
|
|
125
|
+
bundler, so its download is shown without a percentage; so is a worker
|
|
126
|
+
bundled with Verovio inside it, whose download is the worker's own
|
|
127
|
+
script.
|
|
128
|
+
|
|
129
|
+
### A copy from a CDN
|
|
130
|
+
|
|
131
|
+
`verovio-url` can point at a public copy of the exact Verovio this
|
|
132
|
+
package uses, checked with `verovio-integrity`. For Verovio 6.4.1 on
|
|
133
|
+
jsDelivr (served with CORS `*` and cached as immutable):
|
|
134
|
+
|
|
135
|
+
```html
|
|
136
|
+
<barmark-score
|
|
137
|
+
verovio-url="https://cdn.jsdelivr.net/npm/verovio@6.4.1/dist/verovio-module.mjs"
|
|
138
|
+
verovio-integrity="sha384-iGNyhtxpKLNw90cjbI9ES538TJtbNZjLxsFVUuI8PwE2TTNQ+i2j9CjQnRMhZXHk"
|
|
139
|
+
src="/charts/peaceful-dreams.chart.md"
|
|
140
|
+
></barmark-score>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
The package exports both as `VEROVIO_CDN_URL` and `VEROVIO_CDN_INTEGRITY`
|
|
144
|
+
(with `VEROVIO_VERSION`). With `verovio-integrity` set, the worker
|
|
145
|
+
downloads the URL, checks its digest (`sha256-`, `sha384-` or `sha512-`)
|
|
146
|
+
with `crypto.subtle` before importing it, and on a mismatch or any
|
|
147
|
+
failure to load it warns in the console, naming the reason, and loads the
|
|
148
|
+
package's own copy instead, once. Without `verovio-integrity` there is no
|
|
149
|
+
fallback: a `verovio-url` that fails is a score that can't be drawn. A
|
|
150
|
+
malformed digest is refused like a bad URL.
|
|
151
|
+
|
|
152
|
+
A CDN does not save your readers a download they made on another site:
|
|
153
|
+
browsers partition their caches by site, so each site's readers fetch
|
|
154
|
+
Verovio once for that site whichever URL it comes from. What a CDN gives
|
|
155
|
+
is delivery from an edge near the reader and the 2.4 MB kept off your own
|
|
156
|
+
server.
|
|
157
|
+
|
|
158
|
+
### Preloading
|
|
159
|
+
|
|
160
|
+
`preload` starts loading Verovio before the first press, so the score
|
|
161
|
+
comes sooner. It only loads the engine (nothing is engraved), shares the
|
|
162
|
+
one load every element on the page uses, and asking twice does nothing.
|
|
163
|
+
|
|
164
|
+
- `"none"` (the default): Verovio loads on the first switch to the score.
|
|
165
|
+
- `"intent"`: on the first pointer over, focus on or touch of the Score
|
|
166
|
+
switch.
|
|
167
|
+
- `"idle"`: once the page is idle after the element connects
|
|
168
|
+
(`requestIdleCallback`, or half a second later where there is none).
|
|
169
|
+
When the reader has asked to save data, or is on a 2G connection
|
|
170
|
+
(`navigator.connection`), it waits for intent instead.
|
|
93
171
|
|
|
94
172
|
### When the score can't be drawn
|
|
95
173
|
|
|
96
174
|
If Verovio fails to load, or rejects the MusicXML, or the chart will not
|
|
97
|
-
export
|
|
98
|
-
|
|
175
|
+
export or is too large to draw (see "Engraving directly"), or Verovio
|
|
176
|
+
takes over 60 seconds to load or 20 seconds to engrave, the grid stays and
|
|
177
|
+
a note by the switch says "The score couldn't be drawn". The reason goes
|
|
178
|
+
to the console. A score an element no longer wants (its chart or width
|
|
179
|
+
changed) is dropped, and cut off after three seconds if the worker is
|
|
180
|
+
already engraving it.
|
|
99
181
|
|
|
100
182
|
## On the server
|
|
101
183
|
|
|
@@ -125,6 +207,17 @@ if (result.ok) document.body.innerHTML = result.svg;
|
|
|
125
207
|
else console.warn(result.reason);
|
|
126
208
|
```
|
|
127
209
|
|
|
210
|
+
A score is refused with the reason "The score is too large to draw" when
|
|
211
|
+
its MusicXML would pass 50,000,000 characters (`MAX_XML_LENGTH`): the
|
|
212
|
+
export stops writing there. A short chart with repeats can play thousands
|
|
213
|
+
of bars, and writing them all out could take gigabytes. Before exporting,
|
|
214
|
+
`scoreCost` estimates the MusicXML's bytes in one pass over the play order
|
|
215
|
+
(every played bar's measure in every declared part, a tacet part's rests
|
|
216
|
+
too, each slot, pitch, chord degree, syllable and words with the length of
|
|
217
|
+
its text, and each bar's heading), and a chart over 50,000,000
|
|
218
|
+
(`MAX_SCORE_COST`) is refused without exporting. A long lead sheet costs
|
|
219
|
+
about half a million. Pass `maxXmlLength` or `maxCost` to set either.
|
|
220
|
+
|
|
128
221
|
The SVG is cleaned with an allow-list (`cleanSvg`) before it is returned;
|
|
129
222
|
`scorePane` and `scoreStyles` wrap it so it follows the grid's light and
|
|
130
223
|
dark palette. `engraveChart` and `engraveXml` drop a chord `<degree>`
|
|
@@ -137,10 +230,13 @@ is the export unchanged.
|
|
|
137
230
|
This package is MIT. It uses [Verovio](https://github.com/rism-digital/verovio),
|
|
138
231
|
which is LGPL-3.0-only, as an ordinary npm dependency, loaded unchanged at
|
|
139
232
|
run time; its code is never copied into this package. barmark's own code
|
|
140
|
-
stays MIT. Verovio
|
|
233
|
+
stays MIT. Verovio © Laurent Pugin and others; its licence text is
|
|
141
234
|
[COPYING.LESSER](https://github.com/rism-digital/verovio/blob/develop/COPYING.LESSER).
|
|
142
235
|
|
|
143
236
|
A site that bundles the worker ships Verovio inside its bundle. Keeping
|
|
144
237
|
Verovio a separate file (through `verovio-url`, or the bundler's own
|
|
145
|
-
chunk) and keeping its notice is the easy way to meet the LGPL.
|
|
146
|
-
|
|
238
|
+
chunk) and keeping its notice is the easy way to meet the LGPL. If it is
|
|
239
|
+
bundled into one file with the worker instead (Vite 8 does this), credit it
|
|
240
|
+
with its copyright and licence, host or link the LGPL and GPL texts, and
|
|
241
|
+
link your site's source so a visitor can rebuild it with another Verovio;
|
|
242
|
+
barmark.dev does all three in its 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";
|