From e019f28822d7e79a6fe0517bb14e6228e510d3c6 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sun, 20 Oct 2024 06:13:07 -0700 Subject: [PATCH] feat: support build_from_source config in npmrc --- .clang-tidy | 6 ++++-- .npmrc | 2 -- .prettierignore | 2 -- README.md | 19 ++++++++++++++----- lib/load-addon.js | 3 +-- lib/load-addon.js.map | 2 +- package.json | 4 +--- pnpm-lock.yaml | 15 --------------- script/install.js | 36 ++++++++++++++++++++++++------------ src/load-addon.ts | 37 ++++++++++++++++++++----------------- vcpkg.json | 20 +++++--------------- 11 files changed, 70 insertions(+), 76 deletions(-) delete mode 100644 .npmrc diff --git a/.clang-tidy b/.clang-tidy index c5625ea7..ec22c69b 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,10 +1,12 @@ --- -Checks: "*, -abseil-*, -altera-*, -android-*, -fuchsia-*, google-*, +Checks: + "*, -abseil-*, -altera-*, -android-*, -fuchsia-*, google-*, -google-runtime-int, -llvm*, -modernize-use-trailing-return-type, -zircon-*, readability-else-after-return, readability-static-accessed-through-instance, readability-avoid-const-params-in-decls, cppcoreguidelines-non-private-member-variables-in-classes, - misc-non-private-member-variables-in-classes, -*-non-private-member-variables-in-classes,-misc-include-cleaner" + misc-non-private-member-variables-in-classes, + -*-non-private-member-variables-in-classes,-misc-include-cleaner" WarningsAsErrors: "" HeaderFilterRegex: "" FormatStyle: none diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 7cd35e4d..00000000 --- a/.npmrc +++ /dev/null @@ -1,2 +0,0 @@ -# Zmq settings -build-from-source=true diff --git a/.prettierignore b/.prettierignore index de424426..41b46290 100644 --- a/.prettierignore +++ b/.prettierignore @@ -5,5 +5,3 @@ /node_modules pnpm-lock.yaml /build -/script/*.js -/script/*.mjs diff --git a/README.md b/README.md index 1a625bc4..393cb8ad 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,8 @@ When building from source, you can also specify additional build options in a ### Curve support -Enables CURVE security for encrypted communications. To enable CURVE support, add the following to your .npmrc: +Enables CURVE security for encrypted communications. To enable CURVE support, +add the following to your .npmrc: ```ini zmq_curve="true" @@ -127,7 +128,9 @@ zmq_curve="true" ### Libsodium for Curve -Enable libsodium for CURVE security instead of the built-in tweetnacl implementation. This can provide better performance for CURVE operations. To use libsodium, add the following to your .npmrc: +Enable libsodium for CURVE security instead of the built-in tweetnacl +implementation. This can provide better performance for CURVE operations. To use +libsodium, add the following to your .npmrc: ```ini zmq_sodium="true" @@ -145,7 +148,8 @@ zmq_draft=false #### Websocket support -Enables WebSocket transport, allowing ZeroMQ to communicate over WebSockets. To enable WebSocket support, add the following to your .npmrc: +Enables WebSocket transport, allowing ZeroMQ to communicate over WebSockets. To +enable WebSocket support, add the following to your .npmrc: ```ini zmq_websockets="true" @@ -153,7 +157,9 @@ zmq_websockets="true" #### Secure Websocket support -Enables WebSocket transport with TLS (wss), providing secure WebSocket communications. To enable secure WebSocket support, add the following to your .npmrc: +Enables WebSocket transport with TLS (wss), providing secure WebSocket +communications. To enable secure WebSocket support, add the following to your +.npmrc: ```ini zmq_websockets_secure="true" @@ -172,7 +178,10 @@ zmq_no_sync_resolve="true" #### MacOS Deployment Target -Specifies the minimum macOS version that the binary will be compatible with. This is particularly useful when building for different macOS versions. To set this, add the following to your .npmrc, replacing 10.15 with your desired minimum macOS version: +Specifies the minimum macOS version that the binary will be compatible with. +This is particularly useful when building for different macOS versions. To set +this, add the following to your .npmrc, replacing 10.15 with your desired +minimum macOS version: ```ini macos_deployment_target="10.15" diff --git a/lib/load-addon.js b/lib/load-addon.js index eed7d619..b382e27a 100644 --- a/lib/load-addon.js +++ b/lib/load-addon.js @@ -6,8 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const addonParentDir = path_1.default.join(__dirname, "..", "build", process.platform, process.arch, "node"); -const addOnAbiDirs = fs_1.default.readdirSync(addonParentDir) - .sort((a, b) => { +const addOnAbiDirs = fs_1.default.readdirSync(addonParentDir).sort((a, b) => { return Number.parseInt(b, 10) - Number.parseInt(a, 10); }); let addon; diff --git a/lib/load-addon.js.map b/lib/load-addon.js.map index 47b064c4..bede7a9e 100644 --- a/lib/load-addon.js.map +++ b/lib/load-addon.js.map @@ -1 +1 @@ -{"version":3,"file":"load-addon.js","sourceRoot":"","sources":["../src/load-addon.ts"],"names":[],"mappings":";;;;;AAAA,gDAAuB;AACvB,4CAAmB;AAEnB,MAAM,cAAc,GAAG,cAAI,CAAC,IAAI,CAC5B,SAAS,EACT,IAAI,EACJ,OAAO,EACP,OAAO,CAAC,QAAQ,EAChB,OAAO,CAAC,IAAI,EACZ,MAAM,CACT,CAAA;AACD,MAAM,YAAY,GAAG,YAAE,CAAC,WAAW,CAAC,cAAc,CAAC;KAC9C,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;IACX,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;AAC1D,CAAC,CAAC,CAAA;AAEN,IAAI,KAAsB,CAAA;AAC1B,+BAA+B;AAC/B,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE;IACpC,MAAM,SAAS,GAAG,cAAI,CAAC,IAAI,CAAC,cAAc,EAAE,WAAW,EAAE,YAAY,CAAC,CAAA;IACtE,IAAI;QACA,KAAK,GAAG,OAAO,CAAC,SAAS,CAAC,CAAA;KAC7B;IAAC,OAAO,GAAG,EAAE;QACV,OAAO,CAAC,KAAK,CAAC,2BAA2B,SAAS,KAAK,GAAG,oBAAoB,CAAC,CAAA;KAClF;CACJ;AAED,IAAI,KAAK,KAAK,SAAS,EAAE;IACrB,MAAM,IAAI,KAAK,CAAC,gCAAgC,cAAc,kDAAkD,CAAC,CAAA;CACpH;AAED,MAAM,CAAC,OAAO,GAAG,KAAK,CAAA"} \ No newline at end of file +{"version":3,"file":"load-addon.js","sourceRoot":"","sources":["../src/load-addon.ts"],"names":[],"mappings":";;;;;AAAA,gDAAuB;AACvB,4CAAmB;AAEnB,MAAM,cAAc,GAAG,cAAI,CAAC,IAAI,CAC9B,SAAS,EACT,IAAI,EACJ,OAAO,EACP,OAAO,CAAC,QAAQ,EAChB,OAAO,CAAC,IAAI,EACZ,MAAM,CACP,CAAA;AACD,MAAM,YAAY,GAAG,YAAE,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;IAChE,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;AACxD,CAAC,CAAC,CAAA;AAEF,IAAI,KAAsB,CAAA;AAC1B,+BAA+B;AAC/B,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE;IACtC,MAAM,SAAS,GAAG,cAAI,CAAC,IAAI,CAAC,cAAc,EAAE,WAAW,EAAE,YAAY,CAAC,CAAA;IACtE,IAAI;QACF,KAAK,GAAG,OAAO,CAAC,SAAS,CAAC,CAAA;KAC3B;IAAC,OAAO,GAAG,EAAE;QACZ,OAAO,CAAC,KAAK,CACX,2BAA2B,SAAS,KAAK,GAAG,oBAAoB,CACjE,CAAA;KACF;CACF;AAED,IAAI,KAAK,KAAK,SAAS,EAAE;IACvB,MAAM,IAAI,KAAK,CACb,gCAAgC,cAAc,kDAAkD,CACjG,CAAA;CACF;AAED,MAAM,CAAC,OAAO,GAAG,KAAK,CAAA"} \ No newline at end of file diff --git a/package.json b/package.json index 9fb5ffb9..f7c0eebf 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,7 @@ "homepage": "http://zeromq.github.io/zeromq.js/", "dependencies": { "cmake-ts": "^0.3.0", - "cross-env": "^7.0.3", - "node-addon-api": "^8.2.1", - "shelljs": "^0.8.5" + "node-addon-api": "^8.2.1" }, "devDependencies": { "@types/benchmark": "~2.1.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7c115150..937bd15a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -15,15 +15,9 @@ importers: cmake-ts: specifier: ^0.3.0 version: 0.3.0(rollup@4.24.0)(typescript@4.9.5) - cross-env: - specifier: ^7.0.3 - version: 7.0.3 node-addon-api: specifier: ^8.2.1 version: 8.2.1 - shelljs: - specifier: ^0.8.5 - version: 0.8.5 devDependencies: '@types/benchmark': specifier: ~2.1.5 @@ -1633,11 +1627,6 @@ packages: create-require@1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} - cross-env@7.0.3: - resolution: {integrity: sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==} - engines: {node: '>=10.14', npm: '>=6', yarn: '>=1'} - hasBin: true - cross-spawn@7.0.3: resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==} engines: {node: '>= 8'} @@ -6058,10 +6047,6 @@ snapshots: create-require@1.1.1: {} - cross-env@7.0.3: - dependencies: - cross-spawn: 7.0.3 - cross-spawn@7.0.3: dependencies: path-key: 3.1.1 diff --git a/script/install.js b/script/install.js index 93284153..6b059e40 100644 --- a/script/install.js +++ b/script/install.js @@ -1,16 +1,28 @@ -try { - require("../lib/load-addon.js") -} catch (error) { - const cp = require("child_process") +function cmakeTs() { + const cp = require("child_process") - console.error("Failed to load ZMQ addon:", error) + // Run the build script to generate the addon.node file + console.log( + "Building addon node via cmake-ts (requires cmake, ninja, and the vcpkg dependencies)", + ) + const cmakeTsPath = require.resolve("cmake-ts/build/main.js") - // Run the build script to generate the addon.node file - console.log("Building addon node via cmake-ts (requires cmake, ninja, and the vcpkg dependencies)") - const cmakeTsPath = require.resolve("cmake-ts/build/main.js") + cp.execFileSync(process.execPath, [cmakeTsPath, "nativeonly"], { + stdio: "inherit", + }) +} - cp.execFileSync( - process.execPath, [cmakeTsPath, "nativeonly"], - { stdio: "inherit" }, - ) +function main() { + if (process.env.npm_config_build_from_source === "true") { + cmakeTs() + } else { + try { + require("../lib/load-addon.js") + } catch (error) { + console.error("Failed to load ZMQ addon:", error) + cmakeTs() + } + } } + +main() diff --git a/src/load-addon.ts b/src/load-addon.ts index 45422eea..f1f1b4c3 100644 --- a/src/load-addon.ts +++ b/src/load-addon.ts @@ -2,31 +2,34 @@ import path from "path" import fs from "fs" const addonParentDir = path.join( - __dirname, - "..", - "build", - process.platform, - process.arch, - "node", + __dirname, + "..", + "build", + process.platform, + process.arch, + "node", ) -const addOnAbiDirs = fs.readdirSync(addonParentDir) - .sort((a, b) => { - return Number.parseInt(b, 10) - Number.parseInt(a, 10) - }) +const addOnAbiDirs = fs.readdirSync(addonParentDir).sort((a, b) => { + return Number.parseInt(b, 10) - Number.parseInt(a, 10) +}) let addon: undefined | any // try each available addon ABI for (const addOnAbiDir of addOnAbiDirs) { - const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node") - try { - addon = require(addonPath) - } catch (err) { - console.error(`Failed to load addon at ${addonPath}: ${err}\nTrying others...`) - } + const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node") + try { + addon = require(addonPath) + } catch (err) { + console.error( + `Failed to load addon at ${addonPath}: ${err}\nTrying others...`, + ) + } } if (addon === undefined) { - throw new Error(`No compatible addon found in ${addonParentDir} folder. Please build addon with 'npm run build'`) + throw new Error( + `No compatible addon found in ${addonParentDir} folder. Please build addon with 'npm run build'`, + ) } module.exports = addon diff --git a/vcpkg.json b/vcpkg.json index fcd448e9..790a3ad1 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -15,9 +15,7 @@ "dependencies": [ { "name": "zeromq", - "features": [ - "curve" - ] + "features": ["curve"] } ] }, @@ -26,9 +24,7 @@ "dependencies": [ { "name": "zeromq", - "features": [ - "draft" - ] + "features": ["draft"] } ] }, @@ -37,9 +33,7 @@ "dependencies": [ { "name": "zeromq", - "features": [ - "sodium" - ] + "features": ["sodium"] } ] }, @@ -48,9 +42,7 @@ "dependencies": [ { "name": "zeromq", - "features": [ - "websockets" - ] + "features": ["websockets"] } ] }, @@ -59,9 +51,7 @@ "dependencies": [ { "name": "zeromq", - "features": [ - "websockets-secure" - ] + "features": ["websockets-secure"] } ] }