Skip to content

Commit

Permalink
feat: support build_from_source config in npmrc
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Oct 20, 2024
1 parent 03c4a26 commit 9bb547b
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 76 deletions.
6 changes: 4 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions .npmrc

This file was deleted.

2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
/node_modules
pnpm-lock.yaml
/build
/script/*.js
/script/*.mjs
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,18 @@ 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"
```

### 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"
Expand All @@ -145,15 +148,18 @@ 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"
```

#### 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"
Expand All @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions lib/load-addon.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/load-addon.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 0 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 24 additions & 12 deletions script/install.js
Original file line number Diff line number Diff line change
@@ -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()
37 changes: 20 additions & 17 deletions src/load-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 5 additions & 15 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
"dependencies": [
{
"name": "zeromq",
"features": [
"curve"
]
"features": ["curve"]
}
]
},
Expand All @@ -26,9 +24,7 @@
"dependencies": [
{
"name": "zeromq",
"features": [
"draft"
]
"features": ["draft"]
}
]
},
Expand All @@ -37,9 +33,7 @@
"dependencies": [
{
"name": "zeromq",
"features": [
"sodium"
]
"features": ["sodium"]
}
]
},
Expand All @@ -48,9 +42,7 @@
"dependencies": [
{
"name": "zeromq",
"features": [
"websockets"
]
"features": ["websockets"]
}
]
},
Expand All @@ -59,9 +51,7 @@
"dependencies": [
{
"name": "zeromq",
"features": [
"websockets-secure"
]
"features": ["websockets-secure"]
}
]
}
Expand Down

0 comments on commit 9bb547b

Please sign in to comment.