@internetarchive/bookreader 5.0.0-95 → 5.0.0-97
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/BookReader/474.js +2 -0
- package/BookReader/474.js.map +1 -0
- package/BookReader/BookReader.css +2257 -0
- package/BookReader/BookReader.js +3 -0
- package/BookReader/BookReader.js.LICENSE.txt +72 -0
- package/BookReader/BookReader.js.map +1 -0
- package/BookReader/bergamot-translator-worker.js +2966 -0
- package/BookReader/bergamot-translator-worker.wasm +0 -0
- package/BookReader/hypothesis/LICENSE +50 -0
- package/BookReader/hypothesis/README.md +55 -0
- package/BookReader/hypothesis/build/boot.js +1 -0
- package/BookReader/hypothesis/build/manifest.json +20 -0
- package/BookReader/hypothesis/build/scripts/annotator.bundle.js +184 -0
- package/BookReader/hypothesis/build/scripts/annotator.bundle.js.map +1 -0
- package/BookReader/hypothesis/build/scripts/sidebar.bundle.js +798 -0
- package/BookReader/hypothesis/build/scripts/sidebar.bundle.js.map +1 -0
- package/BookReader/hypothesis/build/scripts/ui-playground.bundle.js +711 -0
- package/BookReader/hypothesis/build/scripts/ui-playground.bundle.js.map +1 -0
- package/BookReader/hypothesis/build/styles/annotator.css +2235 -0
- package/BookReader/hypothesis/build/styles/annotator.css.map +1 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Main-Bold.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Main-Italic.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Main-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Math-Italic.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Script-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/BookReader/hypothesis/build/styles/highlights.css +2 -0
- package/BookReader/hypothesis/build/styles/highlights.css.map +1 -0
- package/BookReader/hypothesis/build/styles/katex.min.css +2 -0
- package/BookReader/hypothesis/build/styles/katex.min.css.map +1 -0
- package/BookReader/hypothesis/build/styles/pdfjs-overrides.css +2 -0
- package/BookReader/hypothesis/build/styles/pdfjs-overrides.css.map +1 -0
- package/BookReader/hypothesis/build/styles/sidebar.css +2731 -0
- package/BookReader/hypothesis/build/styles/sidebar.css.map +1 -0
- package/BookReader/hypothesis/build/styles/ui-playground.css +2659 -0
- package/BookReader/hypothesis/build/styles/ui-playground.css.map +1 -0
- package/BookReader/hypothesis/package.json +126 -0
- package/BookReader/ia-bookreader-bundle.js +1782 -0
- package/BookReader/ia-bookreader-bundle.js.LICENSE.txt +7 -0
- package/BookReader/ia-bookreader-bundle.js.map +1 -0
- package/BookReader/icons/1up.svg +1 -0
- package/BookReader/icons/2up.svg +1 -0
- package/BookReader/icons/advance.svg +3 -0
- package/BookReader/icons/chevron-right.svg +1 -0
- package/BookReader/icons/close-circle-dark.svg +1 -0
- package/BookReader/icons/close-circle.svg +1 -0
- package/BookReader/icons/fullscreen.svg +1 -0
- package/BookReader/icons/fullscreen_exit.svg +1 -0
- package/BookReader/icons/hamburger.svg +1 -0
- package/BookReader/icons/left-arrow.svg +1 -0
- package/BookReader/icons/magnify-minus.svg +1 -0
- package/BookReader/icons/magnify-plus.svg +1 -0
- package/BookReader/icons/magnify.svg +1 -0
- package/BookReader/icons/pause.svg +1 -0
- package/BookReader/icons/play.svg +1 -0
- package/BookReader/icons/playback-speed.svg +1 -0
- package/BookReader/icons/read-aloud.svg +1 -0
- package/BookReader/icons/review.svg +3 -0
- package/BookReader/icons/thumbnails.svg +1 -0
- package/BookReader/icons/voice.svg +1 -0
- package/BookReader/icons/volume-full.svg +1 -0
- package/BookReader/images/BRicons.png +0 -0
- package/BookReader/images/BRicons.svg +5 -0
- package/BookReader/images/BRicons_ia.png +0 -0
- package/BookReader/images/back_pages.png +0 -0
- package/BookReader/images/book_bottom_icon.png +0 -0
- package/BookReader/images/book_down_icon.png +0 -0
- package/BookReader/images/book_left_icon.png +0 -0
- package/BookReader/images/book_leftmost_icon.png +0 -0
- package/BookReader/images/book_right_icon.png +0 -0
- package/BookReader/images/book_rightmost_icon.png +0 -0
- package/BookReader/images/book_top_icon.png +0 -0
- package/BookReader/images/book_up_icon.png +0 -0
- package/BookReader/images/books_graphic.svg +1 -0
- package/BookReader/images/booksplit.png +0 -0
- package/BookReader/images/control_pause_icon.png +0 -0
- package/BookReader/images/control_play_icon.png +0 -0
- package/BookReader/images/embed_icon.png +0 -0
- package/BookReader/images/hypothesis.ico +0 -0
- package/BookReader/images/icon-home-ia.png +0 -0
- package/BookReader/images/icon_OL-logo-xs.png +0 -0
- package/BookReader/images/icon_alert-xs.png +0 -0
- package/BookReader/images/icon_book.svg +1 -0
- package/BookReader/images/icon_bookmark.svg +1 -0
- package/BookReader/images/icon_close-pop.png +0 -0
- package/BookReader/images/icon_download.png +0 -0
- package/BookReader/images/icon_experiment.svg +1 -0
- package/BookReader/images/icon_gear.svg +1 -0
- package/BookReader/images/icon_hamburger.svg +1 -0
- package/BookReader/images/icon_home.png +0 -0
- package/BookReader/images/icon_home.svg +1 -0
- package/BookReader/images/icon_home_ia.png +0 -0
- package/BookReader/images/icon_indicator.png +0 -0
- package/BookReader/images/icon_info.svg +1 -0
- package/BookReader/images/icon_one_page.svg +1 -0
- package/BookReader/images/icon_pause.svg +1 -0
- package/BookReader/images/icon_play.svg +1 -0
- package/BookReader/images/icon_playback-rate.svg +1 -0
- package/BookReader/images/icon_return.png +0 -0
- package/BookReader/images/icon_search_button.svg +1 -0
- package/BookReader/images/icon_share.svg +1 -0
- package/BookReader/images/icon_skip-ahead.svg +1 -0
- package/BookReader/images/icon_skip-back.svg +2 -0
- package/BookReader/images/icon_speaker.svg +1 -0
- package/BookReader/images/icon_speaker_open.svg +1 -0
- package/BookReader/images/icon_thumbnails.svg +1 -0
- package/BookReader/images/icon_toc.svg +1 -0
- package/BookReader/images/icon_two_pages.svg +1 -0
- package/BookReader/images/icon_zoomer.png +0 -0
- package/BookReader/images/loading.gif +0 -0
- package/BookReader/images/logo_icon.png +0 -0
- package/BookReader/images/marker_chap-off.png +0 -0
- package/BookReader/images/marker_chap-off.svg +1 -0
- package/BookReader/images/marker_chap-off_ia.png +0 -0
- package/BookReader/images/marker_chap-on.png +0 -0
- package/BookReader/images/marker_chap-on.svg +1 -0
- package/BookReader/images/marker_srch-on.svg +1 -0
- package/BookReader/images/marker_srchchap-off.png +0 -0
- package/BookReader/images/marker_srchchap-on.png +0 -0
- package/BookReader/images/nav_control-dn.png +0 -0
- package/BookReader/images/nav_control-dn_ia.png +0 -0
- package/BookReader/images/nav_control-up.png +0 -0
- package/BookReader/images/nav_control-up_ia.png +0 -0
- package/BookReader/images/nav_control.png +0 -0
- package/BookReader/images/one_page_mode_icon.png +0 -0
- package/BookReader/images/paper-badge.png +0 -0
- package/BookReader/images/print_icon.png +0 -0
- package/BookReader/images/progressbar.gif +0 -0
- package/BookReader/images/right_edges.png +0 -0
- package/BookReader/images/slider.png +0 -0
- package/BookReader/images/slider_ia.png +0 -0
- package/BookReader/images/thumbnail_mode_icon.png +0 -0
- package/BookReader/images/translate.svg +1 -0
- package/BookReader/images/transparent.png +0 -0
- package/BookReader/images/two_page_mode_icon.png +0 -0
- package/BookReader/images/unviewable_page.png +0 -0
- package/BookReader/images/zoom_in_icon.png +0 -0
- package/BookReader/images/zoom_out_icon.png +0 -0
- package/BookReader/jquery-3.js +2 -0
- package/BookReader/jquery-3.js.LICENSE.txt +24 -0
- package/BookReader/plugins/plugin.archive_analytics.js +2 -0
- package/BookReader/plugins/plugin.archive_analytics.js.map +1 -0
- package/BookReader/plugins/plugin.autoplay.js +2 -0
- package/BookReader/plugins/plugin.autoplay.js.map +1 -0
- package/BookReader/plugins/plugin.chapters.js +26 -0
- package/BookReader/plugins/plugin.chapters.js.LICENSE.txt +1 -0
- package/BookReader/plugins/plugin.chapters.js.map +1 -0
- package/BookReader/plugins/plugin.experiments.js +3 -0
- package/BookReader/plugins/plugin.experiments.js.LICENSE.txt +1 -0
- package/BookReader/plugins/plugin.experiments.js.map +1 -0
- package/BookReader/plugins/plugin.iframe.js +2 -0
- package/BookReader/plugins/plugin.iframe.js.map +1 -0
- package/BookReader/plugins/plugin.iiif.js +2 -0
- package/BookReader/plugins/plugin.iiif.js.map +1 -0
- package/BookReader/plugins/plugin.resume.js +2 -0
- package/BookReader/plugins/plugin.resume.js.map +1 -0
- package/BookReader/plugins/plugin.search.js +3 -0
- package/BookReader/plugins/plugin.search.js.LICENSE.txt +1 -0
- package/BookReader/plugins/plugin.search.js.map +1 -0
- package/BookReader/plugins/plugin.text_selection.js +3 -0
- package/BookReader/plugins/plugin.text_selection.js.LICENSE.txt +1 -0
- package/BookReader/plugins/plugin.text_selection.js.map +1 -0
- package/BookReader/plugins/plugin.translate.js +3 -0
- package/BookReader/plugins/plugin.translate.js.LICENSE.txt +1 -0
- package/BookReader/plugins/plugin.translate.js.map +1 -0
- package/BookReader/plugins/plugin.tts.js +3 -0
- package/BookReader/plugins/plugin.tts.js.LICENSE.txt +29 -0
- package/BookReader/plugins/plugin.tts.js.map +1 -0
- package/BookReader/plugins/plugin.url.js +2 -0
- package/BookReader/plugins/plugin.url.js.map +1 -0
- package/BookReader/plugins/plugin.vendor-fullscreen.js +2 -0
- package/BookReader/plugins/plugin.vendor-fullscreen.js.map +1 -0
- package/BookReader/plugins/translator-worker.js +2 -0
- package/BookReader/plugins/translator-worker.js.map +1 -0
- package/BookReader/translator-worker.js +475 -0
- package/BookReader/webcomponents-bundle.js +3 -0
- package/BookReader/webcomponents-bundle.js.LICENSE.txt +9 -0
- package/BookReader/webcomponents-bundle.js.map +1 -0
- package/package.json +11 -3
- package/src/BookNavigator/book-navigator.js +1 -0
- package/src/BookReader/BookModel.js +564 -0
- package/src/BookReader/DragScrollable.js +233 -0
- package/src/BookReader/ImageCache.js +149 -0
- package/src/BookReader/Mode1Up.js +110 -0
- package/src/BookReader/Mode1UpLit.js +393 -0
- package/src/BookReader/Mode2Up.js +107 -0
- package/src/BookReader/Mode2UpLit.js +787 -0
- package/src/BookReader/ModeCoordinateSpace.js +29 -0
- package/src/BookReader/ModeSmoothZoom.js +312 -0
- package/src/BookReader/ModeThumb.js +344 -0
- package/src/BookReader/Navbar/Navbar.js +416 -0
- package/src/BookReader/PageContainer.js +172 -0
- package/src/BookReader/ReduceSet.js +26 -0
- package/src/BookReader/Toolbar/Toolbar.js +362 -0
- package/src/BookReader/events.js +19 -0
- package/src/BookReader/options.js +387 -0
- package/src/BookReader/utils/HTMLDimensionsCacher.js +44 -0
- package/src/BookReader/utils/ScrollClassAdder.js +31 -0
- package/src/BookReader/utils/SelectionObserver.js +45 -0
- package/src/BookReader/utils/classes.js +36 -0
- package/src/BookReader/utils.js +313 -0
- package/src/BookReader.js +22 -139
- package/src/assets/images/icon_experiment.svg +1 -0
- package/src/assets/images/translate.svg +1 -0
- package/src/css/_BRnav.scss +0 -24
- package/src/css/_BRsearch.scss +0 -5
- package/src/css/_TextSelection.scss +32 -1
- package/src/plugins/plugin.experiments.js +34 -9
- package/src/plugins/plugin.text_selection.js +8 -20
- package/src/plugins/translate/TranslationManager.js +167 -0
- package/src/plugins/translate/plugin.translate.js +414 -0
- package/src/plugins/tts/utils.js +9 -20
- package/src/util/cache.js +20 -0
- package/.eslintrc.cjs +0 -51
- package/.gitattributes +0 -2
- package/.github/ISSUE_TEMPLATE/bug.md +0 -32
- package/.github/ISSUE_TEMPLATE/feature-request.md +0 -30
- package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +0 -15
- package/.github/workflows/node.js.yml +0 -102
- package/.github/workflows/npm-publish.yml +0 -23
- package/.testcaferc.cjs +0 -10
- package/BookReaderDemo/BookReaderDemo.css +0 -40
- package/BookReaderDemo/BookReaderJSAdvanced.js +0 -112
- package/BookReaderDemo/BookReaderJSSimple.js +0 -56
- package/BookReaderDemo/IADemoBr.js +0 -149
- package/BookReaderDemo/assets/v5/Bookreader-logo-cool-grad.svg +0 -1
- package/BookReaderDemo/assets/v5/Bookreader-logo-flat.svg +0 -1
- package/BookReaderDemo/assets/v5/Bookreader-logo-hex-cool-grad.png +0 -0
- package/BookReaderDemo/assets/v5/Bookreader-logo-hex-flat.png +0 -0
- package/BookReaderDemo/assets/v5/Bookreader-logo-lines.png +0 -0
- package/BookReaderDemo/assets/v5/Bookreader-logo-lines.svg +0 -1
- package/BookReaderDemo/assets/v5/Bookreader-logo-warm.svg +0 -1
- package/BookReaderDemo/assets/v5/bookreader-logo-renders@1x.png +0 -0
- package/BookReaderDemo/assets/v5/bookreader-logo-renders@2x.png +0 -0
- package/BookReaderDemo/assets/v5/bookreader-v5-screenshot.png +0 -0
- package/BookReaderDemo/demo-advanced.html +0 -33
- package/BookReaderDemo/demo-embed-iframe-src.html +0 -85
- package/BookReaderDemo/demo-embed.html +0 -26
- package/BookReaderDemo/demo-fullscreen-mobile.html +0 -34
- package/BookReaderDemo/demo-fullscreen.html +0 -31
- package/BookReaderDemo/demo-iiif.html +0 -121
- package/BookReaderDemo/demo-internetarchive.html +0 -271
- package/BookReaderDemo/demo-multiple.html +0 -44
- package/BookReaderDemo/demo-preview-pages.html +0 -1093
- package/BookReaderDemo/demo-simple.html +0 -35
- package/BookReaderDemo/demo-vendor-fullscreen.html +0 -34
- package/BookReaderDemo/ia-multiple-volumes-manifest.js +0 -169
- package/BookReaderDemo/immersion-1up.html +0 -64
- package/BookReaderDemo/immersion-mode.html +0 -33
- package/BookReaderDemo/toggle_controls.html +0 -54
- package/BookReaderDemo/view_mode.html +0 -40
- package/BookReaderDemo/viewmode-cycle.html +0 -40
- package/CHANGELOG.md +0 -1087
- package/CONTRIBUTING.md +0 -7
- package/babel.config.cjs +0 -20
- package/codecov.yml +0 -23
- package/index.html +0 -34
- package/netlify.toml +0 -9
- package/renovate.json +0 -52
- package/screenshot.png +0 -0
- package/scripts/postversion.js +0 -11
- package/scripts/preversion.js +0 -15
- package/scripts/version.js +0 -24
- package/tests/e2e/README.md +0 -112
- package/tests/e2e/autoplay.test.js +0 -16
- package/tests/e2e/base.test.js +0 -27
- package/tests/e2e/helpers/base.js +0 -263
- package/tests/e2e/helpers/debug.js +0 -13
- package/tests/e2e/helpers/mockSearch.js +0 -90
- package/tests/e2e/helpers/params.js +0 -17
- package/tests/e2e/helpers/rightToLeft.js +0 -23
- package/tests/e2e/helpers/search.js +0 -73
- package/tests/e2e/models/BookReader.js +0 -11
- package/tests/e2e/models/Navigation.js +0 -39
- package/tests/e2e/rightToLeft.test.js +0 -19
- package/tests/e2e/viewmode.test.js +0 -44
- package/tests/jest/BookNavigator/book-navigator.test.js +0 -653
- package/tests/jest/BookNavigator/bookmarks/bookmark-button.test.js +0 -43
- package/tests/jest/BookNavigator/bookmarks/bookmark-edit.test.js +0 -132
- package/tests/jest/BookNavigator/bookmarks/bookmarks-list.test.js +0 -221
- package/tests/jest/BookNavigator/bookmarks/ia-bookmarks.test.js +0 -45
- package/tests/jest/BookNavigator/downloads/downloads-provider.test.js +0 -67
- package/tests/jest/BookNavigator/downloads/downloads.test.js +0 -53
- package/tests/jest/BookNavigator/search/search-provider.test.js +0 -179
- package/tests/jest/BookNavigator/search/search-results.test.js +0 -289
- package/tests/jest/BookNavigator/sharing/sharing-provider.test.js +0 -49
- package/tests/jest/BookNavigator/viewable-files/viewable-files-provider.test.js +0 -80
- package/tests/jest/BookNavigator/visual-adjustments.test.js +0 -200
- package/tests/jest/BookReader.keyboard.test.js +0 -190
- package/tests/jest/BookReader.options.test.js +0 -47
- package/tests/jest/BookReader.test.js +0 -316
- package/tests/jest/plugins/plugin.archive_analytics.test.js +0 -20
- package/tests/jest/plugins/plugin.autoplay.test.js +0 -35
- package/tests/jest/plugins/plugin.chapters.test.js +0 -193
- package/tests/jest/plugins/plugin.iframe.test.js +0 -42
- package/tests/jest/plugins/plugin.resume.test.js +0 -85
- package/tests/jest/plugins/plugin.text_selection.test.js +0 -447
- package/tests/jest/plugins/plugin.vendor-fullscreen.test.js +0 -65
- package/tests/jest/plugins/search/plugin.search.test.js +0 -120
- package/tests/jest/plugins/search/plugin.search.view.test.js +0 -131
- package/tests/jest/plugins/search/utils.js +0 -25
- package/tests/jest/plugins/search/utils.test.js +0 -29
- package/tests/jest/plugins/tts/AbstractTTSEngine.test.js +0 -173
- package/tests/jest/plugins/tts/FestivalTTSEngine.test.js +0 -59
- package/tests/jest/plugins/tts/PageChunk.test.js +0 -57
- package/tests/jest/plugins/tts/PageChunkIterator.test.js +0 -179
- package/tests/jest/plugins/tts/WebTTSEngine.test.js +0 -178
- package/tests/jest/plugins/tts/utils.test.js +0 -74
- package/tests/jest/plugins/url/UrlPlugin.test.js +0 -198
- package/tests/jest/plugins/url/plugin.url.test.js +0 -168
- package/tests/jest/setup.js +0 -3
- package/tests/jest/util/browserSniffing.test.js +0 -62
- package/tests/jest/util/docCookies.test.js +0 -24
- package/tests/jest/util/strings.test.js +0 -63
- package/tests/jest/utils.js +0 -83
- package/webpack.config.js +0 -97
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrapper around the dirty bits of Bergamot's WASM bindings.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Global because importScripts is global.
|
|
6
|
+
var Module = {};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* node.js compatibility: Fake GlobalWorkerScope that emulates being inside a
|
|
10
|
+
* WebWorker
|
|
11
|
+
*/
|
|
12
|
+
if (typeof self === 'undefined') {
|
|
13
|
+
global.Module = Module;
|
|
14
|
+
|
|
15
|
+
global.self = new class GlobalWorkerScope {
|
|
16
|
+
/** @type {import("node:worker_threads").MessagePort} */
|
|
17
|
+
#port;
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
const {parentPort} = require(/* webpackIgnore: true */ 'node:worker_threads');
|
|
21
|
+
this.#port = parentPort;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Add event listener to listen for messages posted to the worker.
|
|
26
|
+
* @param {string} eventName
|
|
27
|
+
* @param {(object)} callback
|
|
28
|
+
*/
|
|
29
|
+
addEventListener(eventName, callback) {
|
|
30
|
+
this.#port.on(eventName, (data) => callback({data}));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Post message outside, to the owner of the Worker.
|
|
35
|
+
* @param {any} message
|
|
36
|
+
*/
|
|
37
|
+
postMessage(message) {
|
|
38
|
+
this.#port.postMessage(message);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {...string} scripts - Paths to scripts to import in that order
|
|
43
|
+
*/
|
|
44
|
+
importScripts(...scripts) {
|
|
45
|
+
const {readFileSync} = require(/* webpackIgnore: true */ 'node:fs');
|
|
46
|
+
const {join} = require(/* webpackIgnore: true */ 'node:path');
|
|
47
|
+
for (let pathname of scripts) {
|
|
48
|
+
const script = readFileSync(join(__dirname, pathname), {encoding: 'utf-8'});
|
|
49
|
+
eval.call(global, script);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Adds support for local file urls. Assumes anything that doesn't start
|
|
55
|
+
* with "http" to be a local path.
|
|
56
|
+
* @param {string} url - path or url
|
|
57
|
+
* @param {object?} options - See `fetch()` options
|
|
58
|
+
* @return {Promise<Response>}
|
|
59
|
+
*/
|
|
60
|
+
async fetch(url, options) {
|
|
61
|
+
if (url.protocol === 'file:') {
|
|
62
|
+
const {readFile} = require(/* webpackIgnore: true */ 'node:fs/promises');
|
|
63
|
+
const buffer = await readFile(url.pathname);
|
|
64
|
+
const blob = new Blob([buffer]);
|
|
65
|
+
return new Response(blob, {
|
|
66
|
+
status: 200,
|
|
67
|
+
statusText: 'OK',
|
|
68
|
+
headers: {
|
|
69
|
+
'Content-Type': 'application/wasm',
|
|
70
|
+
'Content-Length': blob.size.toString()
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return await fetch(url, options);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get location() {
|
|
79
|
+
return new URL(`file://${__filename}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
class YAML {
|
|
85
|
+
/**
|
|
86
|
+
* Parses YAML into dictionary. Does not interpret types, all values are a
|
|
87
|
+
* string or a list of strings. No support for objects other than the top
|
|
88
|
+
* level.
|
|
89
|
+
* @param {string} yaml
|
|
90
|
+
* @return {{[string]: string | string[]}}
|
|
91
|
+
*/
|
|
92
|
+
static parse(yaml) {
|
|
93
|
+
const out = {};
|
|
94
|
+
|
|
95
|
+
yaml.split('\n').reduce((key, line, i) => {
|
|
96
|
+
let match;
|
|
97
|
+
if (match = line.match(/^\s*-\s+(.+?)$/)) {
|
|
98
|
+
if (!Array.isArray(out[key]))
|
|
99
|
+
out[key] = out[key].trim() ? [out[key]] : [];
|
|
100
|
+
out[key].push(match[1].trim());
|
|
101
|
+
}
|
|
102
|
+
else if (match = line.match(/^\s*([A-Za-z0-9_][A-Za-z0-9_-]*):\s*(.*)$/)) {
|
|
103
|
+
key = match[1];
|
|
104
|
+
out[key] = match[2].trim();
|
|
105
|
+
}
|
|
106
|
+
else if (!line.trim()) {
|
|
107
|
+
// whitespace, ignore
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
throw Error(`Could not parse line ${i+1}: "${line}"`);
|
|
111
|
+
}
|
|
112
|
+
return key;
|
|
113
|
+
}, null);
|
|
114
|
+
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Turns an object into a YAML string. No support for objects, only simple
|
|
120
|
+
* types and lists of simple types.
|
|
121
|
+
* @param {{[string]: string | number | boolean | string[]}} data
|
|
122
|
+
* @return {string}
|
|
123
|
+
*/
|
|
124
|
+
static stringify(data) {
|
|
125
|
+
return Object.entries(data).reduce((str, [key, value]) => {
|
|
126
|
+
let valstr = '';
|
|
127
|
+
if (Array.isArray(value))
|
|
128
|
+
valstr = value.map(val => `\n - ${val}`).join('');
|
|
129
|
+
else if (typeof value === 'number' || typeof value === 'boolean' || value.match(/^\d*(\.\d+)?$/))
|
|
130
|
+
valstr = `${value}`;
|
|
131
|
+
else
|
|
132
|
+
valstr = `${value}`; // Quote?
|
|
133
|
+
|
|
134
|
+
return `${str}${key}: ${valstr}\n`;
|
|
135
|
+
}, '');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Wrapper around the bergamot-translator exported module that hides the need
|
|
141
|
+
* of working with C++ style data structures and does model management.
|
|
142
|
+
*/
|
|
143
|
+
class BergamotTranslatorWorker {
|
|
144
|
+
/**
|
|
145
|
+
* Map of expected symbol -> name of fallback symbol for functions that can
|
|
146
|
+
* be swizzled for a faster implementation. Firefox Nightly makes use of
|
|
147
|
+
* this.
|
|
148
|
+
*/
|
|
149
|
+
static GEMM_TO_FALLBACK_FUNCTIONS_MAP = {
|
|
150
|
+
'int8_prepare_a': 'int8PrepareAFallback',
|
|
151
|
+
'int8_prepare_b': 'int8PrepareBFallback',
|
|
152
|
+
'int8_prepare_b_from_transposed': 'int8PrepareBFromTransposedFallback',
|
|
153
|
+
'int8_prepare_b_from_quantized_transposed': 'int8PrepareBFromQuantizedTransposedFallback',
|
|
154
|
+
'int8_prepare_bias': 'int8PrepareBiasFallback',
|
|
155
|
+
'int8_multiply_and_add_bias': 'int8MultiplyAndAddBiasFallback',
|
|
156
|
+
'int8_select_columns_of_b': 'int8SelectColumnsOfBFallback'
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Name of module exported by Firefox Nightly that exports an optimised
|
|
161
|
+
* implementation of the symbols mentioned above.
|
|
162
|
+
*/
|
|
163
|
+
static NATIVE_INT_GEMM = 'mozIntGemm';
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Empty because we can't do async constructors yet. It is the
|
|
167
|
+
* responsibility of whoever owns this WebWorker to call `initialize()`.
|
|
168
|
+
*/
|
|
169
|
+
constructor(options) {}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Instantiates a new translation worker with optional options object.
|
|
173
|
+
* If this call succeeds, the WASM runtime is loaded and ready.
|
|
174
|
+
*
|
|
175
|
+
* Available options are:
|
|
176
|
+
* useNativeIntGemm: {true | false} defaults to false. If true, it will
|
|
177
|
+
* attempt to link to the intgemm module available in
|
|
178
|
+
* Firefox Nightly which makes translations much faster.
|
|
179
|
+
* cacheSize: {Number} defaults to 0 which disables translation
|
|
180
|
+
* cache entirely. Note that this is a theoretical
|
|
181
|
+
* upper bound. In practice it will use about 1/3th of
|
|
182
|
+
* the cache specified here. 2^14 is not a bad starting
|
|
183
|
+
* value.
|
|
184
|
+
* @param {{useNativeIntGemm: boolean, cacheSize: number}} options
|
|
185
|
+
*/
|
|
186
|
+
async initialize(options) {
|
|
187
|
+
this.options = options || {};
|
|
188
|
+
this.models = new Map(); // Map<str,Promise<TranslationModel>>
|
|
189
|
+
this.module = await this.loadModule();
|
|
190
|
+
this.service = await this.loadTranslationService();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Tries to load native IntGEMM module for bergamot-translator. If that
|
|
195
|
+
* fails because it or any of the expected functions is not available, it
|
|
196
|
+
* falls back to using the naive implementations that come with the wasm
|
|
197
|
+
* binary itself through `linkFallbackIntGemm()`.
|
|
198
|
+
* @param {{env: {memory: WebAssembly.Memory}}} info
|
|
199
|
+
* @return {{[method:string]: (...any) => any}}
|
|
200
|
+
*/
|
|
201
|
+
linkNativeIntGemm(info) {
|
|
202
|
+
if (!WebAssembly['mozIntGemm']) {
|
|
203
|
+
console.warn('Native gemm requested but not available, falling back to embedded gemm');
|
|
204
|
+
return this.linkFallbackIntGemm(info);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const instance = new WebAssembly.Instance(WebAssembly['mozIntGemm'](), {
|
|
208
|
+
'': {memory: info['env']['memory']}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
if (!Array.from(Object.keys(BergamotTranslatorWorker.GEMM_TO_FALLBACK_FUNCTIONS_MAP)).every(fun => instance.exports[fun])) {
|
|
212
|
+
console.warn('Native gemm is missing expected functions, falling back to embedded gemm');
|
|
213
|
+
return this.linkFallbackIntGemm(info);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return instance.exports;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Links intgemm functions that are already available in the wasm binary,
|
|
221
|
+
* but just exports them under the name that is expected by
|
|
222
|
+
* bergamot-translator.
|
|
223
|
+
* @param {{env: {memory: WebAssembly.Memory}}} info
|
|
224
|
+
* @return {{[method:string]: (...any) => any}}
|
|
225
|
+
*/
|
|
226
|
+
linkFallbackIntGemm(info) {
|
|
227
|
+
const mapping = Object.entries(BergamotTranslatorWorker.GEMM_TO_FALLBACK_FUNCTIONS_MAP).map(([key, name]) => {
|
|
228
|
+
return [key, (...args) => Module['asm'][name](...args)]
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
return Object.fromEntries(mapping);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Internal method. Reads and instantiates the WASM binary. Returns a
|
|
236
|
+
* promise for the exported Module object that contains all the classes
|
|
237
|
+
* and functions exported by bergamot-translator.
|
|
238
|
+
* @return {Promise<BergamotTranslator>}
|
|
239
|
+
*/
|
|
240
|
+
loadModule() {
|
|
241
|
+
return new Promise(async (resolve, reject) => {
|
|
242
|
+
try {
|
|
243
|
+
const response = await self.fetch(new URL('./bergamot-translator-worker.wasm', self.location));
|
|
244
|
+
|
|
245
|
+
Object.assign(Module, {
|
|
246
|
+
instantiateWasm: (info, accept) => {
|
|
247
|
+
try {
|
|
248
|
+
WebAssembly.instantiateStreaming(response, {
|
|
249
|
+
...info,
|
|
250
|
+
'wasm_gemm': this.options.useNativeIntGemm
|
|
251
|
+
? this.linkNativeIntGemm(info)
|
|
252
|
+
: this.linkFallbackIntGemm(info)
|
|
253
|
+
}).then(({instance}) => accept(instance)).catch(reject);
|
|
254
|
+
} catch (err) {
|
|
255
|
+
reject(err);
|
|
256
|
+
}
|
|
257
|
+
return {};
|
|
258
|
+
},
|
|
259
|
+
onRuntimeInitialized: () => {
|
|
260
|
+
resolve(Module);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// Emscripten glue code. Webpack et al. should not mangle the `Module` property name!
|
|
265
|
+
self.Module = Module;
|
|
266
|
+
self.importScripts('bergamot-translator-worker.js');
|
|
267
|
+
} catch (err) {
|
|
268
|
+
reject(err);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Internal method. Instantiates a BlockingService()
|
|
275
|
+
* @return {BergamotTranslator.BlockingService}
|
|
276
|
+
*/
|
|
277
|
+
loadTranslationService() {
|
|
278
|
+
return new this.module.BlockingService({
|
|
279
|
+
cacheSize: Math.max(this.options.cacheSize || 0, 0)
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Returns whether a model has already been loaded in this worker. Marked
|
|
285
|
+
* async because the message passing interface we use expects async methods.
|
|
286
|
+
* @param {{from:string, to:string}}
|
|
287
|
+
* @return boolean
|
|
288
|
+
*/
|
|
289
|
+
hasTranslationModel({from,to}) {
|
|
290
|
+
const key = JSON.stringify({from,to});
|
|
291
|
+
return this.models.has(key);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Loads a translation model from a set of file buffers. After this, the
|
|
296
|
+
* model is available to translate with and `hasTranslationModel()` will
|
|
297
|
+
* return true for this pair.
|
|
298
|
+
* @param {{from:string, to:string}}
|
|
299
|
+
* @param {{
|
|
300
|
+
* model: ArrayBuffer,
|
|
301
|
+
* shortlist: ArrayBuffer,
|
|
302
|
+
* vocabs: ArrayBuffer[],
|
|
303
|
+
* qualityModel: ArrayBuffer?,
|
|
304
|
+
* config?: {
|
|
305
|
+
* [key:string]: string
|
|
306
|
+
* }
|
|
307
|
+
* }} buffers
|
|
308
|
+
*/
|
|
309
|
+
loadTranslationModel({from, to}, buffers) {
|
|
310
|
+
// This because service_bindings.cpp:prepareVocabsSmartMemories :(
|
|
311
|
+
const uniqueVocabs = buffers.vocabs.filter((vocab, index, vocabs) => {
|
|
312
|
+
return !vocabs.slice(0, index).includes(vocab);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
const [modelMemory, shortlistMemory, qualityModel, ...vocabMemory] = [
|
|
316
|
+
this.prepareAlignedMemoryFromBuffer(buffers.model, 256),
|
|
317
|
+
this.prepareAlignedMemoryFromBuffer(buffers.shortlist, 64),
|
|
318
|
+
buffers.qualityModel // optional quality model
|
|
319
|
+
? this.prepareAlignedMemoryFromBuffer(buffers.qualityModel, 64)
|
|
320
|
+
: null,
|
|
321
|
+
...uniqueVocabs.map(vocab => this.prepareAlignedMemoryFromBuffer(vocab, 64))
|
|
322
|
+
];
|
|
323
|
+
|
|
324
|
+
const vocabs = new this.module.AlignedMemoryList();
|
|
325
|
+
vocabMemory.forEach(vocab => vocabs.push_back(vocab));
|
|
326
|
+
|
|
327
|
+
// Defaults
|
|
328
|
+
let modelConfig = YAML.parse(`
|
|
329
|
+
beam-size: 1
|
|
330
|
+
normalize: 1.0
|
|
331
|
+
word-penalty: 0
|
|
332
|
+
cpu-threads: 0
|
|
333
|
+
gemm-precision: int8shiftAlphaAll
|
|
334
|
+
skip-cost: true
|
|
335
|
+
`);
|
|
336
|
+
|
|
337
|
+
if (buffers.config)
|
|
338
|
+
Object.assign(modelConfig, buffers.config);
|
|
339
|
+
|
|
340
|
+
// WASM marian is only compiled with support for shiftedAll.
|
|
341
|
+
if (modelConfig['gemm-precision'] === 'int8')
|
|
342
|
+
modelConfig['gemm-precision'] = 'int8shiftAll';
|
|
343
|
+
|
|
344
|
+
// Override these
|
|
345
|
+
Object.assign(modelConfig, YAML.parse(`
|
|
346
|
+
alignment: soft
|
|
347
|
+
quiet: true
|
|
348
|
+
quiet-translation: true
|
|
349
|
+
max-length-break: 128
|
|
350
|
+
mini-batch-words: 1024
|
|
351
|
+
workspace: 128
|
|
352
|
+
max-length-factor: 2.0
|
|
353
|
+
`));
|
|
354
|
+
|
|
355
|
+
const key = JSON.stringify({from,to});
|
|
356
|
+
this.models.set(key, new this.module.TranslationModel(YAML.stringify(modelConfig), modelMemory, shortlistMemory, vocabs, qualityModel));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Frees up memory used by old translation model. Does nothing if model is
|
|
361
|
+
* already deleted.
|
|
362
|
+
* @param {{from:string, to:string}}
|
|
363
|
+
*/
|
|
364
|
+
freeTranslationModel({from, to}) {
|
|
365
|
+
const key = JSON.stringify({from,to});
|
|
366
|
+
|
|
367
|
+
if (!this.models.has(key))
|
|
368
|
+
return;
|
|
369
|
+
|
|
370
|
+
const model = this.models.get(key);
|
|
371
|
+
this.models.delete(key);
|
|
372
|
+
|
|
373
|
+
model.delete();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Internal function. Copies the data from an ArrayBuffer into memory that
|
|
378
|
+
* can be used inside the WASM vm by Marian.
|
|
379
|
+
* @param {{ArrayBuffer}} buffer
|
|
380
|
+
* @param {number} alignmentSize
|
|
381
|
+
* @return {BergamotTranslator.AlignedMemory}
|
|
382
|
+
*/
|
|
383
|
+
prepareAlignedMemoryFromBuffer(buffer, alignmentSize) {
|
|
384
|
+
const bytes = new Int8Array(buffer);
|
|
385
|
+
const memory = new this.module.AlignedMemory(bytes.byteLength, alignmentSize);
|
|
386
|
+
memory.getByteArrayView().set(bytes);
|
|
387
|
+
return memory;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Public. Does actual translation work. You have to make sure that the
|
|
392
|
+
* models necessary for translating text are already loaded before calling
|
|
393
|
+
* this method. Returns a promise with translation responses.
|
|
394
|
+
* @param {{models: {from:string, to:string}[], texts: {text: string, html: boolean}[]}}
|
|
395
|
+
* @return {Promise<{target: {text: string}}[]>}
|
|
396
|
+
*/
|
|
397
|
+
translate({models, texts}) {
|
|
398
|
+
// Convert texts array into a std::vector<std::string>.
|
|
399
|
+
let input = new this.module.VectorString();
|
|
400
|
+
texts.forEach(({text}) => input.push_back(text));
|
|
401
|
+
|
|
402
|
+
// Extracts the texts[].html options into ResponseOption objects
|
|
403
|
+
let options = new this.module.VectorResponseOptions();
|
|
404
|
+
texts.forEach(({html, qualityScores}) => options.push_back({alignment: false, html, qualityScores}));
|
|
405
|
+
|
|
406
|
+
// Turn our model names into a list of TranslationModel pointers
|
|
407
|
+
const translationModels = models.map(({from,to}) => {
|
|
408
|
+
const key = JSON.stringify({from,to});
|
|
409
|
+
return this.models.get(key);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// translate the input, which is a vector<String>; the result is a vector<Response>
|
|
413
|
+
const responses = models.length > 1
|
|
414
|
+
? this.service.translateViaPivoting(...translationModels, input, options)
|
|
415
|
+
: this.service.translate(...translationModels, input, options);
|
|
416
|
+
|
|
417
|
+
input.delete();
|
|
418
|
+
options.delete();
|
|
419
|
+
|
|
420
|
+
// Convert the Response WASM wrappers into native JavaScript types we
|
|
421
|
+
// can send over the 'wire' (message passing) in the same format as we
|
|
422
|
+
// use in bergamot-translator.
|
|
423
|
+
const translations = texts.map((_, i) => ({
|
|
424
|
+
target: {
|
|
425
|
+
text: responses.get(i).getTranslatedText()
|
|
426
|
+
}
|
|
427
|
+
}));
|
|
428
|
+
|
|
429
|
+
responses.delete();
|
|
430
|
+
|
|
431
|
+
return translations;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Because you can't put an Error object in a message. But you can post a
|
|
437
|
+
* generic object!
|
|
438
|
+
* @param {Error} error
|
|
439
|
+
* @return {{
|
|
440
|
+
* name: string?,
|
|
441
|
+
* message: string?,
|
|
442
|
+
* stack: string?
|
|
443
|
+
* }}
|
|
444
|
+
*/
|
|
445
|
+
function cloneError(error) {
|
|
446
|
+
return {
|
|
447
|
+
name: error.name,
|
|
448
|
+
message: error.message,
|
|
449
|
+
stack: error.stack
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// (Constructor doesn't really do anything, we need to call `initialize()`
|
|
454
|
+
// first before using it. That happens from outside the worker.)
|
|
455
|
+
const worker = new BergamotTranslatorWorker();
|
|
456
|
+
|
|
457
|
+
self.addEventListener('message', async function({data: {id, name, args}}) {
|
|
458
|
+
if (!id)
|
|
459
|
+
console.error('Received message without id', arguments[0]);
|
|
460
|
+
|
|
461
|
+
try {
|
|
462
|
+
if (typeof worker[name] !== 'function')
|
|
463
|
+
throw TypeError(`worker[${name}] is not a function`);
|
|
464
|
+
|
|
465
|
+
// Using `Promise.resolve` to await any promises that worker[name]
|
|
466
|
+
// possibly returns.
|
|
467
|
+
const result = await Promise.resolve(Reflect.apply(worker[name], worker, args));
|
|
468
|
+
self.postMessage({id, result});
|
|
469
|
+
} catch (error) {
|
|
470
|
+
self.postMessage({
|
|
471
|
+
id,
|
|
472
|
+
error: cloneError(error)
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
});
|