feat: 🎸 Allow building and bundling of web workers in ember (#2923)

* feat: 🎸 Allow building and bundling of web workers in ember

* refactor: 💡 Cleanup code and add comments
This commit is contained in:
Zhihe Li committed 2025-09-03 11:41:40 -04:00
1 parent 1bafbe1ad2
commit a9a6518b23
15 files changed
+888 -108

No files matched your search

@@ -0,0 +1,19 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { getOwner, setOwner } from '@ember/owner';
export default class SqliteHandler {
constructor(context) {
setOwner(this, getOwner(context));
}
async request(context, next) {
switch (context.request.op) {
default:
return next(context.request);
}
}
}
+46
View File
@@ -0,0 +1,46 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Service from '@ember/service';
import { PWBHost } from 'promise-worker-bi';
export default class SqliteDbService extends Service {
// =attributes
// We need a handle on the original web worker to properly clean it up.
webWorker;
// This will be the worker we interact with that is wrapped by PWBHost.
worker;
// TODO: Actually use the name that gets passed into setup later
setup() {
if (this.worker) {
// If the worker already exists, we don't need to do anything.
return;
}
// In production, our JS files will be fingerprinted with am MD5 hash
// so we need a way to map the worker file name to be able to find it.
//
// Ember is automagically replacing any URL path with the fingerprinted
// asset, and this is done by broccoli-asset-rev.
//
// The problem is it's a bit finicky as seen in this issue
// https://github.com/ember-cli/broccoli-asset-rewrite/issues/66
// and will not work properly if it's part of a template string or interpolated.
// Therefore, we need the URL path in a string somewhere to be replaced properly.
this.webWorker = new Worker('/workers/web-worker.js', {
type: 'module',
});
// We use PWBHost to simplify communication so we can just send and receive
// messages with promises rather than having to manually set up event listeners
this.worker = new PWBHost(this.webWorker);
}
willDestroy() {
this.webWorker?.terminate();
}
}
+7 -1
View File
@@ -11,6 +11,7 @@ import { CacheHandler } from '@ember-data/store';
import RequestManager from '@ember-data/request';
import { LegacyNetworkHandler } from '@ember-data/legacy-compat';
import IndexedDbHandler from 'api/handlers/indexed-db-handler';
import SqliteHandler from 'api/handlers/sqlite-handler';
export default class extends Store {
requestManager = new RequestManager();
@@ -19,8 +20,13 @@ export default class extends Store {
super(args);
const indexedDbHandler = new IndexedDbHandler(this);
const sqliteHandler = new SqliteHandler(this);
this.requestManager.use([indexedDbHandler, LegacyNetworkHandler]);
this.requestManager.use([
sqliteHandler,
indexedDbHandler,
LegacyNetworkHandler,
]);
this.requestManager.useCache(CacheHandler);
}
}
+31
View File
@@ -0,0 +1,31 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { PWBWorker } from 'promise-worker-bi';
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
const promiseWorker = new PWBWorker();
promiseWorker.register((message) => {
console.log(`Received from main: ${message}`);
});
const initializeSQLite = async () => {
try {
const sqlite3 = await sqlite3InitModule({
print: console.log,
printErr: console.error,
});
console.log(`SQLite Version: ${sqlite3?.version.libVersion}`);
const poolUtil = await sqlite3.installOpfsSAHPoolVfs();
const db = new poolUtil.OpfsSAHPoolDb('/boundary');
console.log(db);
} catch (err) {
console.error('Initialization error:', err.name, err.message);
}
};
(async () => {
await initializeSQLite();
})();