Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ jobs:
strategy:
fail-fast: false
matrix:
runner:
- macos-latest
- ubuntu-latest
- windows-latest
node-version:
- 20.x
- 22.x
- 24.x
- 25.x
runs-on: ubuntu-latest
runs-on: ${{ matrix.runner }}
steps:
- name: Harden Runner
uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1
Expand All @@ -32,6 +36,10 @@ jobs:
npm --version
- name: Install dependencies
run: npm ci
- name: Configure and build addons
run: |
npm run addons:configure
npm run addons:build
- name: npm test
if: matrix.node-version != '20.x'
run: npm run node:test
Expand Down
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
node_modules/

# Shallow checkout for agents to reference
/node/

# Artifacts from building addons
/build/
/tests/**/*.node
/tests/**/*.pdb
46 changes: 46 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 3.15...3.31)
project(node-api-cts)

set(NODE_API_HEADERS_DIR ${PROJECT_SOURCE_DIR}/node_modules/node-api-headers)

if(NOT EXISTS ${NODE_API_HEADERS_DIR})
message(FATAL_ERROR "Expected ${NODE_API_HEADERS_DIR} to exist")
endif()

if(MSVC)
set(NODE_API_LIB ${PROJECT_BINARY_DIR}/node.lib)
set(NODE_API_DEF ${NODE_API_HEADERS_DIR}/def/node_api.def)
execute_process(COMMAND ${CMAKE_AR} /def:${NODE_API_DEF} /out:${NODE_API_LIB} ${CMAKE_STATIC_LINKER_FLAGS})
set(NODE_API_SRC ${PROJECT_SOURCE_DIR}/implementors/node/win_delay_load_hook.cc)
endif()

function(add_node_api_cts_addon ADDON_NAME SRC)
add_library(${ADDON_NAME} SHARED ${SRC} ${NODE_API_SRC})
set_target_properties(${ADDON_NAME} PROPERTIES
PREFIX ""
SUFFIX ".node"
# Co-locate the output binary with the source file
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
# (for MSVC)
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}
)
if(APPLE)
set_target_properties(${ADDON_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
elseif(MSVC)
set_target_properties(${ADDON_NAME} PROPERTIES LINK_FLAGS "/DELAYLOAD:NODE.EXE")
# Set the HOST_BINARY expected by win_delay_load_hook.cc
target_compile_definitions(${ADDON_NAME} PRIVATE HOST_BINARY="node.exe")
endif()
target_include_directories(${ADDON_NAME} PRIVATE ${NODE_API_HEADERS_DIR}/include)
target_link_libraries(${ADDON_NAME} PRIVATE ${NODE_API_LIB})
target_compile_features(${ADDON_NAME} PRIVATE cxx_std_17)
target_compile_definitions(${ADDON_NAME} PRIVATE ADDON_NAME=${ADDON_NAME})
endfunction()

file(GLOB_RECURSE cmake_dirs RELATIVE ${CMAKE_SOURCE_DIR} tests/*/CMakeLists.txt)

foreach(cmake_file ${cmake_dirs})
get_filename_component(subdir ${cmake_file} DIRECTORY)
add_subdirectory(${subdir})
endforeach()
17 changes: 17 additions & 0 deletions implementors/node/load-addon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assert from "node:assert/strict";
import { dlopen } from "node:process";
import { constants } from "node:os";
import path from "node:path";
import fs from "node:fs";

const loadAddon = (addonFileName) => {
assert(typeof addonFileName === "string", "Expected a string as addon filename");
assert(!addonFileName.endsWith(".node"), "Expected addon filename without the .node extension");
const addonPath = path.join(process.cwd(), addonFileName + ".node");
assert(fs.existsSync(addonPath), `Expected ${addonPath} to exist - did you build the addons?`);
const addon = { exports: {} };
dlopen(addon, addonPath, constants.dlopen.RTLD_NOW);
return addon.exports;
};

Object.assign(globalThis, { loadAddon });
29 changes: 21 additions & 8 deletions implementors/node/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import { test, type TestContext } from "node:test";

const ROOT_PATH = path.resolve(import.meta.dirname, "..", "..");
const TESTS_ROOT_PATH = path.join(ROOT_PATH, "tests");

const ASSERT_MODULE_PATH = path.join(
ROOT_PATH,
"implementors",
"node",
"assert.js"
);
const LOAD_ADDON_MODULE_PATH = path.join(
ROOT_PATH,
"implementors",
"node",
"load-addon.js"
);

async function listDirectoryEntries(dir: string) {
const entries = await fs.readdir(dir, { withFileTypes: true });
Expand All @@ -31,13 +38,20 @@ async function listDirectoryEntries(dir: string) {
return { directories, files };
}

function runFileInSubprocess(filePath: string): Promise<void> {
function runFileInSubprocess(cwd: string, filePath: string): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, [
"--import",
ASSERT_MODULE_PATH,
filePath,
]);
const child = spawn(
process.execPath,
[
// Using file scheme prefix when to enable imports on Windows
"--import",
"file://" + ASSERT_MODULE_PATH,
"--import",
"file://" + LOAD_ADDON_MODULE_PATH,
filePath,
],
{ cwd }
);

let stderrOutput = "";
child.stderr.setEncoding("utf8");
Expand Down Expand Up @@ -80,8 +94,7 @@ async function populateSuite(
const { directories, files } = await listDirectoryEntries(dir);

for (const file of files) {
const filePath = path.join(dir, file);
await testContext.test(file, () => runFileInSubprocess(filePath));
await testContext.test(file, () => runFileInSubprocess(dir, file));
}

for (const directory of directories) {
Expand Down
44 changes: 44 additions & 0 deletions implementors/node/win_delay_load_hook.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// This file is copied from
// https://github.com/nodejs/node-gyp/blob/db5385c5467e5bfb914b9954f0313c46f1f4e10d/src/win_delay_load_hook.cc

/*
* When this file is linked to a DLL, it sets up a delay-load hook that
* intervenes when the DLL is trying to load the host executable
* dynamically. Instead of trying to locate the .exe file it'll just
* return a handle to the process image.
*
* This allows compiled addons to work when the host executable is renamed.
*/

#ifdef _MSC_VER

#pragma managed(push, off)

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>

#include <delayimp.h>
#include <string.h>

static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
HMODULE m;
if (event != dliNotePreLoadLibrary)
return NULL;

if (_stricmp(info->szDll, "node.exe") != 0)
return NULL;

// try for libnode.dll to compat node.js that using 'vcbuild.bat dll'
m = GetModuleHandle(TEXT("libnode.dll"));
if (m == NULL) m = GetModuleHandle(NULL);
return (FARPROC) m;
}

decltype(__pfnDliNotifyHook2) __pfnDliNotifyHook2 = load_exe_hook;

#pragma managed(pop)

#endif
Loading
Loading