Skip to content

Commit 3776f20

Browse files
authored
CommonJS binding for node addon (#558)
1 parent a1f859a commit 3776f20

File tree

5 files changed

+504
-138
lines changed

5 files changed

+504
-138
lines changed

.changeset/commonjs_bindings.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
node-addon: patch
3+
---
4+
5+
# CommonJS bindings
6+
7+
Adding support for CJS.

lib/node-addon/build.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const crossCompile = process.env["CROSS_COMPILE"] === "true";
1616
const { task } = await cli.build({
1717
release,
1818
platform: true,
19-
esm: true,
2019
crossCompile,
2120
target,
2221
// we dont rebuild the bindings because they will regenerate the node addon npm package versions

lib/node-addon/index.cjs

Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
// prettier-ignore
2+
/* eslint-disable */
3+
// @ts-nocheck
4+
/* auto-generated by NAPI-RS */
5+
6+
const { createRequire } = require('node:module')
7+
require = createRequire(__filename);
8+
9+
const { readFileSync } = require("node:fs");
10+
let nativeBinding = null;
11+
const loadErrors = [];
12+
13+
const isMusl = () => {
14+
let musl = false;
15+
if (process.platform === "linux") {
16+
musl = isMuslFromFilesystem();
17+
if (musl === null) {
18+
musl = isMuslFromReport();
19+
}
20+
if (musl === null) {
21+
musl = isMuslFromChildProcess();
22+
}
23+
}
24+
return musl;
25+
};
26+
27+
const isFileMusl = (f) => f.includes("libc.musl-") || f.includes("ld-musl-");
28+
29+
const isMuslFromFilesystem = () => {
30+
try {
31+
return readFileSync("/usr/bin/ldd", "utf-8").includes("musl");
32+
} catch {
33+
return null;
34+
}
35+
};
36+
37+
const isMuslFromReport = () => {
38+
let report = null;
39+
if (typeof process.report?.getReport === "function") {
40+
process.report.excludeNetwork = true;
41+
report = process.report.getReport();
42+
}
43+
if (!report) {
44+
return null;
45+
}
46+
if (report.header && report.header.glibcVersionRuntime) {
47+
return false;
48+
}
49+
if (Array.isArray(report.sharedObjects)) {
50+
if (report.sharedObjects.some(isFileMusl)) {
51+
return true;
52+
}
53+
}
54+
return false;
55+
};
56+
57+
const isMuslFromChildProcess = () => {
58+
try {
59+
return require("child_process")
60+
.execSync("ldd --version", { encoding: "utf8" })
61+
.includes("musl");
62+
} catch (e) {
63+
// If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
64+
return false;
65+
}
66+
};
67+
68+
function requireNative() {
69+
if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
70+
try {
71+
return require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH);
72+
} catch (err) {
73+
loadErrors.push(err);
74+
}
75+
} else if (process.platform === "android") {
76+
if (process.arch === "arm64") {
77+
try {
78+
return require("./hive_router_query_planner.android-arm64.node");
79+
} catch (e) {
80+
loadErrors.push(e);
81+
}
82+
} else if (process.arch === "arm") {
83+
try {
84+
return require("./hive_router_query_planner.android-arm-eabi.node");
85+
} catch (e) {
86+
loadErrors.push(e);
87+
}
88+
} else {
89+
loadErrors.push(
90+
new Error(`Unsupported architecture on Android ${process.arch}`)
91+
);
92+
}
93+
} else if (process.platform === "win32") {
94+
if (process.arch === "x64") {
95+
if (
96+
process.report?.getReport?.()?.header?.osName?.startsWith?.("MINGW")
97+
) {
98+
try {
99+
return require("./hive_router_query_planner.win32-x64-gnu.node");
100+
} catch (e) {
101+
loadErrors.push(e);
102+
}
103+
} else {
104+
try {
105+
return require("./hive_router_query_planner.win32-x64-msvc.node");
106+
} catch (e) {
107+
loadErrors.push(e);
108+
}
109+
}
110+
} else if (process.arch === "ia32") {
111+
try {
112+
return require("./hive_router_query_planner.win32-ia32-msvc.node");
113+
} catch (e) {
114+
loadErrors.push(e);
115+
}
116+
} else if (process.arch === "arm64") {
117+
try {
118+
return require("./hive_router_query_planner.win32-arm64-msvc.node");
119+
} catch (e) {
120+
loadErrors.push(e);
121+
}
122+
} else {
123+
loadErrors.push(
124+
new Error(`Unsupported architecture on Windows: ${process.arch}`)
125+
);
126+
}
127+
} else if (process.platform === "darwin") {
128+
try {
129+
return require("./hive_router_query_planner.darwin-universal.node");
130+
} catch (e) {
131+
loadErrors.push(e);
132+
}
133+
if (process.arch === "x64") {
134+
try {
135+
return require("./hive_router_query_planner.darwin-x64.node");
136+
} catch (e) {
137+
loadErrors.push(e);
138+
}
139+
} else if (process.arch === "arm64") {
140+
try {
141+
return require("./hive_router_query_planner.darwin-arm64.node");
142+
} catch (e) {
143+
loadErrors.push(e);
144+
}
145+
} else {
146+
loadErrors.push(
147+
new Error(`Unsupported architecture on macOS: ${process.arch}`)
148+
);
149+
}
150+
} else if (process.platform === "freebsd") {
151+
if (process.arch === "x64") {
152+
try {
153+
return require("./hive_router_query_planner.freebsd-x64.node");
154+
} catch (e) {
155+
loadErrors.push(e);
156+
}
157+
} else if (process.arch === "arm64") {
158+
try {
159+
return require("./hive_router_query_planner.freebsd-arm64.node");
160+
} catch (e) {
161+
loadErrors.push(e);
162+
}
163+
} else {
164+
loadErrors.push(
165+
new Error(`Unsupported architecture on FreeBSD: ${process.arch}`)
166+
);
167+
}
168+
} else if (process.platform === "linux") {
169+
if (process.arch === "x64") {
170+
if (isMusl()) {
171+
try {
172+
return require("./hive_router_query_planner.linux-x64-musl.node");
173+
} catch (e) {
174+
loadErrors.push(e);
175+
}
176+
} else {
177+
try {
178+
return require("./hive_router_query_planner.linux-x64-gnu.node");
179+
} catch (e) {
180+
loadErrors.push(e);
181+
}
182+
}
183+
} else if (process.arch === "arm64") {
184+
if (isMusl()) {
185+
try {
186+
return require("./hive_router_query_planner.linux-arm64-musl.node");
187+
} catch (e) {
188+
loadErrors.push(e);
189+
}
190+
} else {
191+
try {
192+
return require("./hive_router_query_planner.linux-arm64-gnu.node");
193+
} catch (e) {
194+
loadErrors.push(e);
195+
}
196+
}
197+
} else if (process.arch === "arm") {
198+
if (isMusl()) {
199+
try {
200+
return require("./hive_router_query_planner.linux-arm-musleabihf.node");
201+
} catch (e) {
202+
loadErrors.push(e);
203+
}
204+
} else {
205+
try {
206+
return require("./hive_router_query_planner.linux-arm-gnueabihf.node");
207+
} catch (e) {
208+
loadErrors.push(e);
209+
}
210+
}
211+
} else if (process.arch === "loong64") {
212+
if (isMusl()) {
213+
try {
214+
return require("./hive_router_query_planner.linux-loong64-musl.node");
215+
} catch (e) {
216+
loadErrors.push(e);
217+
}
218+
} else {
219+
try {
220+
return require("./hive_router_query_planner.linux-loong64-gnu.node");
221+
} catch (e) {
222+
loadErrors.push(e);
223+
}
224+
}
225+
} else if (process.arch === "riscv64") {
226+
if (isMusl()) {
227+
try {
228+
return require("./hive_router_query_planner.linux-riscv64-musl.node");
229+
} catch (e) {
230+
loadErrors.push(e);
231+
}
232+
} else {
233+
try {
234+
return require("./hive_router_query_planner.linux-riscv64-gnu.node");
235+
} catch (e) {
236+
loadErrors.push(e);
237+
}
238+
}
239+
} else if (process.arch === "ppc64") {
240+
try {
241+
return require("./hive_router_query_planner.linux-ppc64-gnu.node");
242+
} catch (e) {
243+
loadErrors.push(e);
244+
}
245+
} else if (process.arch === "s390x") {
246+
try {
247+
return require("./hive_router_query_planner.linux-s390x-gnu.node");
248+
} catch (e) {
249+
loadErrors.push(e);
250+
}
251+
} else {
252+
loadErrors.push(
253+
new Error(`Unsupported architecture on Linux: ${process.arch}`)
254+
);
255+
}
256+
} else if (process.platform === "openharmony") {
257+
if (process.arch === "arm64") {
258+
try {
259+
return require("./hive_router_query_planner.openharmony-arm64.node");
260+
} catch (e) {
261+
loadErrors.push(e);
262+
}
263+
} else if (process.arch === "x64") {
264+
try {
265+
return require("./hive_router_query_planner.openharmony-x64.node");
266+
} catch (e) {
267+
loadErrors.push(e);
268+
}
269+
} else if (process.arch === "arm") {
270+
try {
271+
return require("./hive_router_query_planner.openharmony-arm.node");
272+
} catch (e) {
273+
loadErrors.push(e);
274+
}
275+
} else {
276+
loadErrors.push(
277+
new Error(`Unsupported architecture on OpenHarmony: ${process.arch}`)
278+
);
279+
}
280+
} else {
281+
loadErrors.push(
282+
new Error(
283+
`Unsupported OS: ${process.platform}, architecture: ${process.arch}`
284+
)
285+
);
286+
}
287+
}
288+
289+
nativeBinding = requireNative();
290+
291+
if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
292+
let wasiBinding = null;
293+
let wasiBindingError = null;
294+
try {
295+
wasiBinding = require("./hive_router_query_planner.wasi.cjs");
296+
nativeBinding = wasiBinding;
297+
} catch (err) {
298+
if (process.env.NAPI_RS_FORCE_WASI) {
299+
wasiBindingError = err;
300+
}
301+
}
302+
if (process.env.NAPI_RS_FORCE_WASI === "error" && !wasiBinding) {
303+
const error = new Error(
304+
"WASI binding not found and NAPI_RS_FORCE_WASI is set to error"
305+
);
306+
error.cause = wasiBindingError;
307+
throw error;
308+
}
309+
}
310+
311+
if (!nativeBinding) {
312+
if (loadErrors.length > 0) {
313+
throw new Error(
314+
`Cannot find native binding. ` +
315+
`npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
316+
"Please try `npm i` again after removing both package-lock.json and node_modules directory.",
317+
{
318+
cause: loadErrors.reduce((err, cur) => {
319+
cur.cause = err;
320+
return cur;
321+
}),
322+
}
323+
);
324+
}
325+
throw new Error(`Failed to load native binding`);
326+
}
327+
328+
module.exports = nativeBinding;
329+
module.exports.QueryPlanner = nativeBinding.QueryPlanner;

0 commit comments

Comments
 (0)