Skip to content

Commit

Permalink
separate web-* init from hermetic bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Oct 6, 2023
1 parent 11c91c7 commit 9b89a57
Show file tree
Hide file tree
Showing 11 changed files with 611 additions and 474 deletions.
2 changes: 1 addition & 1 deletion packages/daemon/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<body>
<h1>Daemon</h1>
<p>Daemon is running.</p>
<script src="./dist-daemon-web-bundle.js"> </script>
<script src="./dist-daemon-web-init-bundle.js"> </script>
</body>
</html>
1 change: 1 addition & 0 deletions packages/daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"browserfs": "^2.0.0",
"buffer": "^6.0.3",
"events": "^3.3.0",
"idb-kv-store": "^4.5.0",
"make-dir": "^4.0.0",
"path-browserify": "^1.0.1",
"ses": "^0.18.8",
Expand Down
117 changes: 117 additions & 0 deletions packages/daemon/src/browserfs-fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@


const cb2promise =
(obj, method) =>
(...args) =>
new Promise((resolve, reject) => {
obj[method](...args, (err, ...rest) => {
if (err) {
reject(err);
} else {
// @ts-ignore
resolve(...rest);
}
});
});


// const permissionError = pth => {
// // This replicates the exception of `fs.mkdir` with native the
// // `recusive` option when run on an invalid drive under Windows.
// const error = new Error(`operation not permitted, mkdir '${pth}'`);
// error.code = 'EPERM';
// error.errno = -4048;
// error.path = pth;
// error.syscall = 'mkdir';
// return error;
// };

// const makeDirectory = async (input, options) => {
// const fs = options.fs.promises;
// const path = options.path;

// const make = async pth => {
// console.log(`makeDirectory ${pth}`);
// // workaround for browserfs bug?
// if (pth === '/') {
// return pth;
// }
// try {
// await fs.mkdir(pth, options.mode);

// return pth;
// } catch (error) {
// // workaround for browserfs bug?
// if (error.code === 'EEXIST') {
// // continue normally
// return pth;
// }

// if (error.code === 'EPERM') {
// throw error;
// }

// if (error.code === 'ENOENT') {
// if (path.dirname(pth) === pth) {
// throw permissionError(pth);
// }

// if (error.message.includes('null bytes')) {
// throw error;
// }

// await make(path.dirname(pth));

// return make(pth);
// }

// // try {
// // const stats = await stat(pth);
// // if (!stats.isDirectory()) {
// // throw new Error('The path is not a directory');
// // }
// // } catch {
// // throw error;
// // }

// return pth;
// }
// };

// return make(path.resolve(input));
// };


// shim fs
// await new Promise(cb => configure({
// fs: 'IndexedDB',
// options: {},
// }, cb));
// // const fs = BFSRequire('fs');
// // const fsPath = BFSRequire('path');
// // @ts-ignore
// fs.promises = {
// readFile: cb2promise(fs, 'readFile'),
// writeFile: cb2promise(fs, 'writeFile'),
// readdir: cb2promise(fs, 'readdir'),
// mkdir: cb2promise(fs, 'mkdir'),
// rm: cb2promise(fs, 'rmdir'),
// rename: cb2promise(fs, 'rename'),
// };
// // shim mkdir recursive: true
// const _mkDir = fs.mkdir;
// fs.mkdir = (path, options, cb) => {
// if (typeof options === 'function') {
// cb = options;
// options = undefined;
// }
// if (options && options.recursive) {
// makeDirectory(path, {
// fs,
// path: fsPath,
// mode: options.mode,
// }).then(() => cb(null), cb);
// return;
// }
// _mkDir.call(fs, path, options, cb);
// };
35 changes: 35 additions & 0 deletions packages/daemon/src/build-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ const daemonBundleLocation = new URL(
'../dist-daemon-web-bundle.js',
import.meta.url,
).toString();
const daemonInitModuleLocation = new URL(
'daemon-web-init.js',
import.meta.url,
).toString();
const daemonInitBundleLocation = new URL(
'../dist-daemon-web-init-bundle.js',
import.meta.url,
).toString();

const workerModuleLocation = new URL(
'worker-web.js',
import.meta.url,
Expand All @@ -29,6 +38,14 @@ const workerBundleLocation = new URL(
'../dist-worker-web-bundle.js',
import.meta.url,
).toString();
const workerInitModuleLocation = new URL(
'worker-web-init.js',
import.meta.url,
).toString();
const workerInitBundleLocation = new URL(
'../dist-worker-init-bundle.js',
import.meta.url,
).toString();

const bundleOptions = {
// node builtin shims for browser
Expand All @@ -43,20 +60,38 @@ const bundleOptions = {
}

async function main() {
// daemon kit hermetic bundle
await writeBundle(
write,
read,
daemonBundleLocation,
daemonModuleLocation,
bundleOptions,
)
// worker kit hermetic bundle
await writeBundle(
write,
read,
workerBundleLocation,
workerModuleLocation,
bundleOptions,
)
// self-initing daemon
await writeBundle(
write,
read,
daemonInitBundleLocation,
daemonInitModuleLocation,
bundleOptions,
)
// self-initing worker
await writeBundle(
write,
read,
workerInitBundleLocation,
workerInitModuleLocation,
bundleOptions,
)
}

main()
11 changes: 11 additions & 0 deletions packages/daemon/src/daemon-web-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import './daemon-web.js'

globalThis.startDaemon({
makeWebWorker () {
console.log('making endo worker in subworker')
const worker = new Worker('./dist-worker-web-init-bundle.js', {
name: 'Endo Worker',
});
return worker;
}
});
Loading

0 comments on commit 9b89a57

Please sign in to comment.