Skip to content

Commit e019f28

Browse files
committed
feat: support build_from_source config in npmrc
1 parent 5b0a4ef commit e019f28

File tree

11 files changed

+70
-76
lines changed

11 files changed

+70
-76
lines changed

.clang-tidy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
---
2-
Checks: "*, -abseil-*, -altera-*, -android-*, -fuchsia-*, google-*,
2+
Checks:
3+
"*, -abseil-*, -altera-*, -android-*, -fuchsia-*, google-*,
34
-google-runtime-int, -llvm*, -modernize-use-trailing-return-type, -zircon-*,
45
readability-else-after-return, readability-static-accessed-through-instance,
56
readability-avoid-const-params-in-decls,
67
cppcoreguidelines-non-private-member-variables-in-classes,
7-
misc-non-private-member-variables-in-classes, -*-non-private-member-variables-in-classes,-misc-include-cleaner"
8+
misc-non-private-member-variables-in-classes,
9+
-*-non-private-member-variables-in-classes,-misc-include-cleaner"
810
WarningsAsErrors: ""
911
HeaderFilterRegex: ""
1012
FormatStyle: none

.npmrc

Lines changed: 0 additions & 2 deletions
This file was deleted.

.prettierignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@
55
/node_modules
66
pnpm-lock.yaml
77
/build
8-
/script/*.js
9-
/script/*.mjs

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,18 @@ When building from source, you can also specify additional build options in a
119119

120120
### Curve support
121121

122-
Enables CURVE security for encrypted communications. To enable CURVE support, add the following to your .npmrc:
122+
Enables CURVE security for encrypted communications. To enable CURVE support,
123+
add the following to your .npmrc:
123124

124125
```ini
125126
zmq_curve="true"
126127
```
127128

128129
### Libsodium for Curve
129130

130-
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:
131+
Enable libsodium for CURVE security instead of the built-in tweetnacl
132+
implementation. This can provide better performance for CURVE operations. To use
133+
libsodium, add the following to your .npmrc:
131134

132135
```ini
133136
zmq_sodium="true"
@@ -145,15 +148,18 @@ zmq_draft=false
145148

146149
#### Websocket support
147150

148-
Enables WebSocket transport, allowing ZeroMQ to communicate over WebSockets. To enable WebSocket support, add the following to your .npmrc:
151+
Enables WebSocket transport, allowing ZeroMQ to communicate over WebSockets. To
152+
enable WebSocket support, add the following to your .npmrc:
149153

150154
```ini
151155
zmq_websockets="true"
152156
```
153157

154158
#### Secure Websocket support
155159

156-
Enables WebSocket transport with TLS (wss), providing secure WebSocket communications. To enable secure WebSocket support, add the following to your .npmrc:
160+
Enables WebSocket transport with TLS (wss), providing secure WebSocket
161+
communications. To enable secure WebSocket support, add the following to your
162+
.npmrc:
157163

158164
```ini
159165
zmq_websockets_secure="true"
@@ -172,7 +178,10 @@ zmq_no_sync_resolve="true"
172178

173179
#### MacOS Deployment Target
174180

175-
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:
181+
Specifies the minimum macOS version that the binary will be compatible with.
182+
This is particularly useful when building for different macOS versions. To set
183+
this, add the following to your .npmrc, replacing 10.15 with your desired
184+
minimum macOS version:
176185

177186
```ini
178187
macos_deployment_target="10.15"

lib/load-addon.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/load-addon.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
"homepage": "http://zeromq.github.io/zeromq.js/",
2121
"dependencies": {
2222
"cmake-ts": "^0.3.0",
23-
"cross-env": "^7.0.3",
24-
"node-addon-api": "^8.2.1",
25-
"shelljs": "^0.8.5"
23+
"node-addon-api": "^8.2.1"
2624
},
2725
"devDependencies": {
2826
"@types/benchmark": "~2.1.5",

pnpm-lock.yaml

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

script/install.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
try {
2-
require("../lib/load-addon.js")
3-
} catch (error) {
4-
const cp = require("child_process")
1+
function cmakeTs() {
2+
const cp = require("child_process")
53

6-
console.error("Failed to load ZMQ addon:", error)
4+
// Run the build script to generate the addon.node file
5+
console.log(
6+
"Building addon node via cmake-ts (requires cmake, ninja, and the vcpkg dependencies)",
7+
)
8+
const cmakeTsPath = require.resolve("cmake-ts/build/main.js")
79

8-
// Run the build script to generate the addon.node file
9-
console.log("Building addon node via cmake-ts (requires cmake, ninja, and the vcpkg dependencies)")
10-
const cmakeTsPath = require.resolve("cmake-ts/build/main.js")
10+
cp.execFileSync(process.execPath, [cmakeTsPath, "nativeonly"], {
11+
stdio: "inherit",
12+
})
13+
}
1114

12-
cp.execFileSync(
13-
process.execPath, [cmakeTsPath, "nativeonly"],
14-
{ stdio: "inherit" },
15-
)
15+
function main() {
16+
if (process.env.npm_config_build_from_source === "true") {
17+
cmakeTs()
18+
} else {
19+
try {
20+
require("../lib/load-addon.js")
21+
} catch (error) {
22+
console.error("Failed to load ZMQ addon:", error)
23+
cmakeTs()
24+
}
25+
}
1626
}
27+
28+
main()

src/load-addon.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,34 @@ import path from "path"
22
import fs from "fs"
33

44
const addonParentDir = path.join(
5-
__dirname,
6-
"..",
7-
"build",
8-
process.platform,
9-
process.arch,
10-
"node",
5+
__dirname,
6+
"..",
7+
"build",
8+
process.platform,
9+
process.arch,
10+
"node",
1111
)
12-
const addOnAbiDirs = fs.readdirSync(addonParentDir)
13-
.sort((a, b) => {
14-
return Number.parseInt(b, 10) - Number.parseInt(a, 10)
15-
})
12+
const addOnAbiDirs = fs.readdirSync(addonParentDir).sort((a, b) => {
13+
return Number.parseInt(b, 10) - Number.parseInt(a, 10)
14+
})
1615

1716
let addon: undefined | any
1817
// try each available addon ABI
1918
for (const addOnAbiDir of addOnAbiDirs) {
20-
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
21-
try {
22-
addon = require(addonPath)
23-
} catch (err) {
24-
console.error(`Failed to load addon at ${addonPath}: ${err}\nTrying others...`)
25-
}
19+
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
20+
try {
21+
addon = require(addonPath)
22+
} catch (err) {
23+
console.error(
24+
`Failed to load addon at ${addonPath}: ${err}\nTrying others...`,
25+
)
26+
}
2627
}
2728

2829
if (addon === undefined) {
29-
throw new Error(`No compatible addon found in ${addonParentDir} folder. Please build addon with 'npm run build'`)
30+
throw new Error(
31+
`No compatible addon found in ${addonParentDir} folder. Please build addon with 'npm run build'`,
32+
)
3033
}
3134

3235
module.exports = addon

0 commit comments

Comments
 (0)