Skip to content

Commit 49d2621

Browse files
committed
fix(core): load the native addon on every Node major
The addon shipped ABI-pinned prebuilds for Node 22 and 24 only, so on any other major node-gyp-build found no candidate, the binding silently fell back to the no-op stub and every benchmark failed with "Native core module is not bound". Only one V8 entry point stood in the way of a single Node-API prebuild: v8::String::Utf8Value gained a defaulted argument in Node 24 and WriteUtf8 gave way to WriteUtf8V2 in Node 26, so no string conversion symbol resolves on all three. Every other V8 symbol the perf-map handler needs is stable across 22, 24 and 26. Route the conversion through Node-API and ship one node.napi.node again. prebuildify 6 defaults --name to the package name and no longer appends the napi tag, which node-gyp-build 4.6 rejects, hence the explicit --name.
1 parent 9338d9a commit 49d2621

6 files changed

Lines changed: 38 additions & 16 deletions

File tree

‎packages/core/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"gypfile": true,
1818
"scripts": {
1919
"build": "rollup -c",
20-
"build-native-addon": "prebuildify --name node --strip --no-napi --target 22.0.0 --target 24.0.0",
20+
"build-native-addon": "prebuildify --name node.napi --napi --strip --target 22.0.0",
2121
"build-tracer-client": "openapi --client axios --input ./tracer.spec.json --name MongoTracer --output ./src/generated/openapi",
2222
"test": "jest --passWithNoTests --silent",
2323
"test/integ": "jest --passWithNoTests --silent -c jest.config.integ.js",

‎packages/core/src/native_core/linux_perf/linux_perf.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LinuxPerf::LinuxPerf(const Napi::CallbackInfo &info)
2222
Napi::Value LinuxPerf::Start(const Napi::CallbackInfo &info) {
2323
if (handler == nullptr) {
2424
v8::Isolate *isolate = v8::Isolate::GetCurrent();
25-
handler = new LinuxPerfHandler(isolate);
25+
handler = new LinuxPerfHandler(isolate, info.Env());
2626
handler->Enable();
2727
return Napi::Boolean::New(info.Env(), true);
2828
}

‎packages/core/src/native_core/linux_perf/linux_perf.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace codspeed_native {
1010

1111
class LinuxPerfHandler : public v8::CodeEventHandler {
1212
public:
13-
explicit LinuxPerfHandler(v8::Isolate *isolate);
13+
LinuxPerfHandler(v8::Isolate *isolate, napi_env env);
1414
~LinuxPerfHandler() override;
1515

1616
void Handle(v8::CodeEvent *code_event) override;
@@ -19,6 +19,7 @@ class LinuxPerfHandler : public v8::CodeEventHandler {
1919
std::ofstream mapFile;
2020
std::string FormatName(v8::CodeEvent *code_event);
2121
v8::Isolate *isolate_;
22+
napi_env env_;
2223
};
2324

2425
class LinuxPerf : public Napi::ObjectWrap<LinuxPerf> {

‎packages/core/src/native_core/linux_perf/linux_perf_listener.cc‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
namespace codspeed_native {
77

8-
LinuxPerfHandler::LinuxPerfHandler(v8::Isolate *isolate)
8+
LinuxPerfHandler::LinuxPerfHandler(v8::Isolate *isolate, napi_env env)
99
: v8::CodeEventHandler(isolate) {
1010
isolate_ = isolate;
11+
env_ = env;
1112
int pid = static_cast<int>(uv_os_getpid());
1213
mapFile.open("/tmp/perf-" + std::to_string(pid) + ".map");
1314
}
@@ -17,7 +18,7 @@ LinuxPerfHandler::~LinuxPerfHandler() { mapFile.close(); }
1718
std::string LinuxPerfHandler::FormatName(v8::CodeEvent *code_event) {
1819
std::string name = std::string(code_event->GetComment());
1920
if (name.empty()) {
20-
name = v8LocalStringToString(code_event->GetFunctionName());
21+
name = v8LocalStringToString(env_, code_event->GetFunctionName());
2122
}
2223
return name;
2324
}
@@ -27,7 +28,8 @@ void LinuxPerfHandler::Handle(v8::CodeEvent *code_event) {
2728
<< code_event->GetCodeSize() << " ";
2829
mapFile << v8::CodeEvent::GetCodeEventTypeName(code_event->GetCodeType())
2930
<< ":" << FormatName(code_event) << " "
30-
<< v8LocalStringToString(code_event->GetScriptName()) << std::dec
31+
<< v8LocalStringToString(env_, code_event->GetScriptName())
32+
<< std::dec
3133
<< ":" << code_event->GetScriptLine() << ":"
3234
<< code_event->GetScriptColumn() << std::endl;
3335
}

‎packages/core/src/native_core/linux_perf/utils.h‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@
22
#define LINUX_PERF_UTILS_H
33

44
#include "v8-profiler.h"
5+
#include <js_native_api.h>
6+
#include <string>
57

8+
// The string conversions are the only part of the V8 C++ API used here that is
9+
// not ABI-stable: Utf8Value's constructor gained a defaulted argument in Node
10+
// 24, and WriteUtf8 gave way to WriteUtf8V2 in Node 26, so none of them
11+
// resolves on every major. Node-API is versioned and does not move, and
12+
// napi_value is layout-compatible with v8::Local<v8::Value> by construction.
613
static inline std::string
7-
v8LocalStringToString(v8::Local<v8::String> v8String) {
8-
// Utf8Value NUL-terminates, so the c-string constructor stops at the first
9-
// embedded NUL, as callers expect for symbol names. It yields nullptr when
10-
// the conversion throws.
11-
v8::String::Utf8Value value(v8::Isolate::GetCurrent(), v8String);
12-
return *value ? std::string(*value) : std::string();
14+
v8LocalStringToString(napi_env env, v8::Local<v8::String> v8String) {
15+
if (v8String.IsEmpty()) {
16+
return std::string();
17+
}
18+
19+
napi_value value = reinterpret_cast<napi_value>(*v8String);
20+
size_t length = 0;
21+
if (napi_get_value_string_utf8(env, value, nullptr, 0, &length) != napi_ok) {
22+
return std::string();
23+
}
24+
25+
std::string result(length, '\0');
26+
if (napi_get_value_string_utf8(env, value, &result[0], length + 1, &length) !=
27+
napi_ok) {
28+
return std::string();
29+
}
30+
31+
result.resize(length);
32+
return result;
1333
}
1434

1535
#endif // LINUX_PERF_UTILS_H

‎packages/core/src/nodeVersion.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
2-
* Majors the native addon ships prebuilds for, as listed in the
3-
* `build-native-addon` targets in package.json. Prebuilds are matched on the
4-
* exact ABI version, so on any other major the addon only loads when it has
5-
* been compiled from source locally.
2+
* Majors CodSpeed is tested against. The native addon itself is built as a
3+
* single Node-API binary and loads on any major, so this only gates the
4+
* warning about measurement stability.
65
*/
76
export const SUPPORTED_NODE_MAJORS = [22, 24];
87

0 commit comments

Comments
 (0)