Skip to content

Commit

Permalink
feat(node): add support of .node module (#2804)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Oct 24, 2022
1 parent 627844f commit 820474d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions node/_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@ if (Deno?.core) {
eventLoopHasMoreWork(): boolean {
return false;
},
ops: {
op_napi_open(_filename: string) {
throw new Error(
"Node API is not supported in this environment",
);
},
},
};
}
4 changes: 3 additions & 1 deletion node/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,9 @@ Module._extensions[".json"] = (module: Module, filename: string) => {
}
};

// .node extension is not supported
Module._extensions[".node"] = (module: Module, filename: string) => {
module.exports = core.ops.op_napi_open(filename);
};

function createRequireFromPath(filename: string): RequireFunction {
// Allow a directory to be passed as the filename
Expand Down
16 changes: 16 additions & 0 deletions node/module_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,19 @@ Deno.test("createRequire with http(s):// URL throws with correct error message"
"createRequire only supports 'file://' URLs for the 'filename' parameter. Received 'https://example.com/foo.js'",
);
});

Deno.test("require Node-API module", () => {
const require = createRequire(import.meta.url);
if (Deno.build.os === "windows") {
// TODO(kt3k): Add lib binary for windows from 1_hello_world example of
// https://github.com/nodejs/node-addon-examples
}
if (Deno.build.os === "linux") {
// TODO(kt3k): Add lib binary for linux from 1_hello_world example of
// https://github.com/nodejs/node-addon-examples
}
if (Deno.build.os === "darwin") {
const str = require("./testdata/libhello_darwin.node").hello();
assertEquals(str, "world");
}
});
Binary file added node/testdata/libhello_darwin.node
Binary file not shown.

0 comments on commit 820474d

Please sign in to comment.