-
-
Notifications
You must be signed in to change notification settings - Fork 144
refactor(web): unbundle lexical model worker from compiled artifact #16565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
12ffec8
c90737c
7e04aa7
13ac095
d296b53
5db91db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| /* | ||
| * Keyman is copyright (C) SIL Global. MIT License. | ||
| */ | ||
|
|
||
| import { KeymanEngine } from './keymanEngine.js' | ||
| import { SourcemappedWorker } from '@keymanapp/lexical-model-layer/web' | ||
| import { WebPredictiveTextWorkerFactory } from '@keymanapp/lexical-model-layer/web' | ||
|
|
||
| /** | ||
| * Determine path and protocol of executing script, setting them as | ||
|
|
@@ -9,4 +13,4 @@ const ss = (document.currentScript as HTMLScriptElement)?.src; | |
| const sPath = ss ? ss.substring(0, ss.lastIndexOf('/') + 1) : './'; | ||
|
|
||
| // @ts-ignore | ||
| window['keyman'] = new KeymanEngine(SourcemappedWorker, sPath); | ||
| window['keyman'] = new KeymanEngine(new WebPredictiveTextWorkerFactory(sPath + 'worker-thread.js'), sPath); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. devin.ai found a potential problem: |
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| /* | ||
| * Keyman is copyright (C) SIL Global. MIT License. | ||
| */ | ||
|
|
||
| import { KeymanEngine } from './keymanEngine.js' | ||
| import { WebWorker } from '@keymanapp/lexical-model-layer/web' | ||
| import { WebPredictiveTextWorkerFactory } from '@keymanapp/lexical-model-layer/web' | ||
|
|
||
| /** | ||
| * Determine path and protocol of executing script, setting them as | ||
|
|
@@ -9,4 +13,4 @@ const ss = (document.currentScript as HTMLScriptElement)?.src; | |
| const sPath = ss ? ss.substring(0, ss.lastIndexOf('/') + 1) : './'; | ||
|
|
||
| // @ts-ignore | ||
| window['keyman'] = new KeymanEngine(WebWorker, sPath); | ||
| window['keyman'] = new KeymanEngine(new WebPredictiveTextWorkerFactory(sPath + 'worker-thread.js'), sPath); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This solution appears to be bypassing the standard KeymanEngine path-configuration process, instead determining the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. This is making the (hopefully fairly safe) assumption that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My version of the PR just initialized the actual worker later, using the configured path as a parameter for the constructInstance method. |
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| export { LMLayer } from './lmlayer.js'; | ||
| export { NodeWorker } from './node/node-worker.js'; | ||
| export { WebWorker } from './web/web-worker.js'; | ||
| /* | ||
| * Keyman is copyright (C) SIL Global. MIT License. | ||
| */ | ||
|
|
||
| export interface WorkerFactory { | ||
| constructInstance(): Worker | ||
| } | ||
| export { LMLayer } from './lmlayer.js'; | ||
| export { type WorkerFactory } from './worker-factory.js'; | ||
| export { NodePredictiveTextWorkerFactory } from './node/node-predictive-text-worker-factory.js'; | ||
| export { WebPredictiveTextWorkerFactory } from './web/web-predictive-text-worker-factory.js'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| export { LMLayer } from '../lmlayer.js'; | ||
| export { NodeWorker } from './node-worker.js'; | ||
| export { SourcemappedWorker } from './sourcemappedWorker.js'; | ||
| /* | ||
| * Keyman is copyright (C) SIL Global. MIT License. | ||
| */ | ||
|
|
||
| export interface WorkerFactory { | ||
| constructInstance(): Worker | ||
| } | ||
| export { LMLayer } from '../lmlayer.js'; | ||
| export { type WorkerFactory } from '../worker-factory.js'; | ||
| export { NodePredictiveTextWorkerFactory } from './node-predictive-text-worker-factory.js'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Keyman is copyright (C) SIL Global. MIT License. | ||
| */ | ||
| import { WorkerFactory } from "../worker-factory.js"; | ||
| import { NodePredictiveTextWorker } from "./node-predictive-text-worker.js"; | ||
|
|
||
| import * as path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const __filename__ = fileURLToPath(import.meta.url); | ||
| const __dirname__ = path.dirname(__filename__); | ||
| export const nodeLexicalModelWorkerPath = __dirname__ + "/../../../../worker-thread/build/lib/worker-thread.js"; | ||
|
|
||
| export class NodePredictiveTextWorkerFactory implements WorkerFactory { | ||
| private workerFilename: string; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe better name |
||
| constructor(workerFilename?: string) { | ||
| this.workerFilename = workerFilename ?? nodeLexicalModelWorkerPath; | ||
| } | ||
| constructInstance(): Worker { | ||
| return new NodePredictiveTextWorker(this.workerFilename); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,10 @@ | ||||||||||
| /* | ||||||||||
| * Keyman is copyright (C) SIL Global. MIT License. | ||||||||||
| * | ||||||||||
| * NodePredictiveTextWorker mirrors the necessary interfaces for | ||||||||||
| * WebPredictiveTextWorker in Node. It is used only for unit tests. | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| // We use a subset of the Worker interface here; compiling directly against the true | ||||||||||
| // WebWorker type definitions would require us to implement more methods than we do. | ||||||||||
| /// <reference path="../worker-interface.d.ts" /> | ||||||||||
|
|
@@ -7,10 +14,13 @@ import * as worker from 'node:worker_threads'; | |||||||||
| import { Buffer } from 'node:buffer'; | ||||||||||
| import { URL } from 'node:url'; | ||||||||||
|
|
||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Defines mappings from Node Worker signatures to WebWorker signatures | ||||||||||
| * | ||||||||||
| * TODO: move this to a separate module, no need for it to be embedded string | ||||||||||
|
mcdurdin marked this conversation as resolved.
|
||||||||||
| */ | ||||||||||
| const nodeWorkerToWebWorkerMappingSource = ` | ||||||||||
| const nodeWorkerToWebWorkerMappingSource = (filename: string) => (` | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
| import { parentPort } from 'node:worker_threads'; | ||||||||||
| import fs from 'node:fs'; | ||||||||||
| import vm from 'node:vm'; | ||||||||||
|
|
@@ -42,37 +52,24 @@ function importScripts(...args) { | |||||||||
| const self = globalThis; | ||||||||||
| self.postMessage = postMessage; | ||||||||||
| self.importScripts = importScripts; | ||||||||||
| `; | ||||||||||
| self.self = self; // make it global! | ||||||||||
| // Start off by importing the main worker itself | ||||||||||
| importScripts(${JSON.stringify(filename)}); | ||||||||||
| `); | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Uses the Node version of Workers to provide proper, authentic separate-thread | ||||||||||
| * 'sandboxing'. Also intercepts and interprets certain WebWorker method signatures | ||||||||||
| * necessary to run the WebWorker-oriented worker code. | ||||||||||
| * | ||||||||||
| * Alternatively, only after writing this did I discover this package: | ||||||||||
| * https://github.com/developit/web-worker. They also ran one notable issue I did: | ||||||||||
| * Node 18.x, at least, does not support use of Node Blobs for construction of a | ||||||||||
| * Worker: https://github.com/developit/web-worker/pull/32... unlike Web Workers. | ||||||||||
| * | ||||||||||
| * So... Base64-encoded Data URLs it is. | ||||||||||
| * | ||||||||||
| * What we have here is perfectly fine for now, but if we need more complicated | ||||||||||
| * cross-platform Worker support in the future, it may be wise to swap to use of | ||||||||||
| * that package. | ||||||||||
| */ | ||||||||||
| export class MappedWorker extends worker.Worker implements Worker { | ||||||||||
| constructor(scriptStr: string) { | ||||||||||
| const concatenatedScript = ` | ||||||||||
| ${nodeWorkerToWebWorkerMappingSource} | ||||||||||
|
|
||||||||||
| ${scriptStr} | ||||||||||
| `; | ||||||||||
| const buffer = Buffer.from(concatenatedScript); | ||||||||||
| export class NodePredictiveTextWorker extends worker.Worker implements Worker { | ||||||||||
|
mcdurdin marked this conversation as resolved.
|
||||||||||
| constructor(workerFilename: string) { | ||||||||||
| const buffer = Buffer.from(nodeWorkerToWebWorkerMappingSource(workerFilename)); | ||||||||||
|
Comment on lines
+66
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| const dataSrc = "data:text/javascript;base64," + buffer.toString('base64'); | ||||||||||
| //@ts-ignore | ||||||||||
| super(new URL(dataSrc)); | ||||||||||
|
|
||||||||||
| // WebWorkers have a defined `onmessage` function, rather than this.on('message', ...) | ||||||||||
| // Workers have a defined `onmessage` function, rather than this.on('message', ...) | ||||||||||
| this.on('message', (ev) => { | ||||||||||
| if(this.onmessage) { | ||||||||||
| this.onmessage({data: ev[0]}); | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.