Skip to content

Commit 5e46482

Browse files
aryanjassaltegefaulkes
authored andcommitted
fix: fixed packaged executable
1 parent 726c3dc commit 5e46482

File tree

6 files changed

+108
-14
lines changed

6 files changed

+108
-14
lines changed

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
postBuild = ''
6161
npm run pkg -- \
6262
--output=out \
63-
--bin=dist/polykey.mjs \
63+
--bin=dist/polykey.cjs \
6464
--node-version=${utils.nodeVersion} \
6565
--platform=${platform} \
6666
--arch=${arch}

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@
102102
"node_modules/sodium-native/prebuilds/darwin-arm64/node.napi.node",
103103
"node_modules/sodium-native/prebuilds/darwin-x64/node.napi.node",
104104
"node_modules/sodium-native/prebuilds/linux-x64/node.napi.node",
105-
"node_modules/sodium-native/prebuilds/win32-x64/node.napi.node",
106-
"node_modules/sodium-native/index.js"
105+
"node_modules/sodium-native/prebuilds/win32-x64/node.napi.node"
107106
],
108107
"scripts": [
109108
"dist/polykeyWorkerManifest.js"
@@ -121,7 +120,7 @@
121120
"lintfix": "eslint '{src,tests,scripts,benches}/**/*.{js,mjs,ts,mts,jsx,tsx}' --fix",
122121
"lint-shell": "find ./src ./tests ./scripts -type f -regextype posix-extended -regex '.*\\.(sh)' -exec shellcheck {} +",
123122
"docs": "shx rm -rf ./docs && typedoc --gitRevision master --tsconfig ./tsconfig.build.json --out ./docs src",
124-
"pkg": "node ./scripts/pkg.mjs",
123+
"pkg": "node ./scripts/build.mjs --pkg && node ./scripts/pkg.mjs",
125124
"polykey": "node dist/polykey.mjs",
126125
"start": "node dist/polykey.mjs -- agent start --verbose"
127126
},
@@ -173,4 +172,4 @@
173172
"typedoc": "^0.24.8",
174173
"typescript": "^5.1.6"
175174
}
176-
}
175+
}

pkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"assets":["node_modules/@matrixai/db/prebuilds/darwin-x64+arm64/node.napi.node","node_modules/@matrixai/db/prebuilds/linux-x64/node.napi.node","node_modules/@matrixai/db/prebuilds/win32-x64/node.napi.node","node_modules/@matrixai/mdns-linux-x64/node.napi.node","node_modules/@matrixai/mdns-linux-x64/package.json","node_modules/@matrixai/quic-darwin-universal/node.napi.node","node_modules/@matrixai/quic-darwin-universal/package.json","node_modules/@matrixai/quic-linux-x64/node.napi.node","node_modules/@matrixai/quic-linux-x64/package.json","node_modules/@matrixai/quic-win32-x64/node.napi.node","node_modules/@matrixai/quic-win32-x64/package.json","node_modules/@matrixai/exec-darwin-universal/node.napi.node","node_modules/@matrixai/exec-darwin-universal/package.json","node_modules/@matrixai/exec-linux-x64/node.napi.node","node_modules/@matrixai/exec-linux-x64/package.json","node_modules/fd-lock/prebuilds/linux-x64/node.napi.node","node_modules/fd-lock/prebuilds/darwin-x64/node.napi.node","node_modules/fd-lock/prebuilds/darwin-arm64/node.napi.node","node_modules/fd-lock/prebuilds/win32-x64/node.napi.node","node_modules/sodium-native/prebuilds/darwin-arm64/node.napi.node","node_modules/sodium-native/prebuilds/darwin-x64/node.napi.node","node_modules/sodium-native/prebuilds/linux-x64/node.napi.node","node_modules/sodium-native/prebuilds/win32-x64/node.napi.node","node_modules/sodium-native/index.js","dist/**","node_modules/@matrixai/db/prebuilds/linux-x64/node.napi.node","node_modules/fd-lock/prebuilds/linux-x64/node.napi.node","node_modules/sodium-native/prebuilds/linux-x64/node.napi.node"],"scripts":["dist/polykeyWorkerManifest.js"]}

scripts/build.mjs

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,69 @@ const projectPath = path.dirname(
1616

1717
const platform = os.platform();
1818

19+
/**
20+
* This plugin intercepts loading of certain imports and replaces their contents.
21+
* It overrides the import paths for native imports for rocksdb, fd-lock and sodium-native
22+
*/
23+
const nativeNodeModulesPlugin = {
24+
name: 'native-node-modules',
25+
setup(build) {
26+
build.onLoad({ filter: /\.*/, namespace: 'file' }, (args) => {
27+
const filename = path.basename(args.path);
28+
if (filename === 'rocksdb.js') {
29+
return {
30+
contents: `
31+
import path from 'node:path';
32+
import url from 'node:url';
33+
import nodeGypBuild from 'node-gyp-build';
34+
const projectPath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '..', 'node_modules', '@matrixai', 'db');
35+
const rocksdb = nodeGypBuild(projectPath);
36+
export default rocksdb;
37+
//# sourceMappingURL=rocksdb.js.map
38+
`,
39+
loader: 'js',
40+
};
41+
}
42+
if (args.path.endsWith('fd-lock/index.js')) {
43+
return {
44+
contents: `
45+
const path = require('path');
46+
const binding = require('node-gyp-build')(path.join(__dirname, '..', 'node_modules', 'fd-lock'))
47+
48+
lock.unlock = unlock
49+
module.exports = lock
50+
51+
function lock (fd) {
52+
return !!binding.fd_lock(fd)
53+
}
54+
55+
function unlock (fd) {
56+
return !!binding.fd_unlock(fd)
57+
}
58+
`,
59+
loader: 'js',
60+
};
61+
}
62+
if (args.path.endsWith('sodium-native/index.js')) {
63+
return {
64+
contents: `
65+
const path = require('path');
66+
module.exports = require('node-gyp-build')(path.join(__dirname, '..', 'node_modules', 'sodium-native'))
67+
`,
68+
loader: 'js',
69+
};
70+
}
71+
return;
72+
});
73+
},
74+
};
75+
1976
/* eslint-disable no-console */
2077
async function main(argv = process.argv) {
2178
argv = argv.slice(2);
79+
const pkgIndex = argv.findIndex((v) => v === '--pkg');
80+
if (pkgIndex >= 0) argv.splice(pkgIndex, 1);
81+
const isPkg = pkgIndex >= 0;
2282
const buildPath = path.join(projectPath, 'build');
2383
const distPath = path.join(projectPath, 'dist');
2484
const gitPath = process.env.GIT_DIR ?? path.join(projectPath, '.git');
@@ -64,9 +124,29 @@ async function main(argv = process.argv) {
64124
path.join(buildPath, 'build.json'),
65125
JSON.stringify(buildJSON, null, 2),
66126
);
127+
67128
// This specifies import paths that is left as an external require
68129
// This is kept to packages that have a native binding
69-
const externalDependencies = Object.keys(packageJSON.optionalDependencies);
130+
/** @type { import('esbuild').BuildOptions } */
131+
const isPkgSwitchedOptions = isPkg
132+
? {
133+
external: [],
134+
format: 'cjs',
135+
inject: [path.join(projectPath, './shims/import-meta-url-shim.mjs')],
136+
// Fix import.meta.url in CJS output
137+
define: {
138+
'import.meta.url': '__import_meta_url',
139+
},
140+
outExtension: { '.js': '.cjs' },
141+
}
142+
: {
143+
// External: externalDependencies,
144+
format: 'esm',
145+
inject: [path.join(projectPath, './shims/require-shim.mjs')],
146+
outExtension: { '.js': '.mjs' },
147+
};
148+
149+
/** @type { import('esbuild').BuildOptions } */
70150
const esbuildOptions = {
71151
// 2 entrypoints, the main script and the worker script
72152
entryPoints: [
@@ -77,17 +157,14 @@ async function main(argv = process.argv) {
77157
bundle: true,
78158
platform: 'node',
79159
outdir: distPath,
80-
external: externalDependencies,
81160
treeShaking: true,
82161
// External source map for debugging
83162
sourcemap: true,
84163
// Minify and keep the original names
85164
minify: false,
86165
keepNames: true,
87-
// Supporting ESM
88-
format: 'esm',
89-
inject: [path.join(projectPath, './shims/require-shim.mjs')],
90-
outExtension: { '.js': '.mjs' },
166+
plugins: [nativeNodeModulesPlugin],
167+
...isPkgSwitchedOptions,
91168
};
92169
console.error('Running esbuild:');
93170
console.error(esbuildOptions);
@@ -96,7 +173,11 @@ async function main(argv = process.argv) {
96173
console.error('Renaming worker script');
97174
childProcess.execFileSync(
98175
'mv',
99-
['dist/polykeyWorkerManifest.mjs', 'dist/polykeyWorkerManifest.js'],
176+
[
177+
`dist/polykeyWorkerManifest.${isPkg ? 'cjs' : 'mjs'}`,
178+
'dist/polykeyWorkerManifest.js',
179+
],
180+
// ['dist/polykeyWorkerManifest.mjs', 'dist/polykeyWorkerManifest.js'],
100181
{
101182
stdio: ['inherit', 'inherit', 'inherit'],
102183
windowsHide: true,
@@ -106,7 +187,10 @@ async function main(argv = process.argv) {
106187
);
107188
childProcess.execFileSync(
108189
'mv',
109-
['dist/polykeyWorkerManifest.mjs.map', 'dist/polykeyWorkerManifest.js.map'],
190+
[
191+
`dist/polykeyWorkerManifest.${isPkg ? 'cjs' : 'mjs'}.map`,
192+
'dist/polykeyWorkerManifest.js.map',
193+
],
110194
{
111195
stdio: ['inherit', 'inherit', 'inherit'],
112196
windowsHide: true,

scripts/pkg.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ async function main(argv = process.argv) {
103103
const { default: nodeGypBuild } = await import('node-gyp-build');
104104
const pkgConfig = packageJSON.pkg ?? {};
105105
pkgConfig.assets = pkgConfig.assets ?? {};
106+
106107
const npmLsOut = childProcess.execFileSync(
107108
'npm',
108109
['ls', '--all', '--omit=dev', '--parseable'],
@@ -143,7 +144,6 @@ async function main(argv = process.argv) {
143144
`--config=${pkgConfigPath}`,
144145
`--targets=node${nodeVersion}-${pkgPlatform}-${pkgArch}`,
145146
'--no-bytecode',
146-
'--no-native-build',
147147
'--options=experimental-require-module',
148148
'--public',
149149
"--public-packages='*'",

shims/import-meta-url-shim.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let __import_meta_url;
2+
if (typeof document === 'undefined') {
3+
const { pathToFileURL } = require('url');
4+
__import_meta_url = pathToFileURL(__filename).href;
5+
} else {
6+
__import_meta_url =
7+
(document.currentScript && document.currentScript.src) ||
8+
new URL('main.js', document.baseURI).href;
9+
}
10+
export { __import_meta_url };

0 commit comments

Comments
 (0)