Skip to content

Commit cd75e37

Browse files
feat(NODE-7166)!: increase napi version to 9 (#275)
1 parent 7170783 commit cd75e37

File tree

10 files changed

+79
-85
lines changed

10 files changed

+79
-85
lines changed

.github/scripts/build.mjs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import util from 'node:util';
44
import process from 'node:process';
5-
import fs from 'node:fs/promises';
5+
import fs, { readFile } from 'node:fs/promises';
66
import child_process from 'node:child_process';
77
import events from 'node:events';
88
import path from 'node:path';
@@ -83,8 +83,16 @@ async function buildBindings(args, pkg) {
8383

8484
if (process.platform === 'darwin' && process.arch === 'arm64') {
8585
// The "arm64" build is actually a universal binary
86-
const armTar = `kerberos-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`;
87-
const x64Tar = `kerberos-v${pkg.version}-napi-v4-darwin-x64.tar.gz`;
86+
// @ts-ignore
87+
const {
88+
binary: {
89+
napi_versions: [
90+
napiVersion
91+
]
92+
}
93+
} = JSON.parse(await readFile(resolveRoot('package.json'), 'utf-8'));
94+
const armTar = `kerberos-v${pkg.version}-napi-v${napiVersion}-darwin-arm64.tar.gz`;
95+
const x64Tar = `kerberos-v${pkg.version}-napi-v${napiVersion}-darwin-x64.tar.gz`;
8896
await fs.copyFile(resolveRoot('prebuilds', armTar), resolveRoot('prebuilds', x64Tar));
8997
}
9098

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
os: [macos-latest, windows-2022]
19+
fail-fast: false
1920
runs-on: ${{ matrix.os }}
2021
steps:
2122
- uses: actions/checkout@v4
@@ -40,6 +41,7 @@ jobs:
4041
strategy:
4142
matrix:
4243
linux_arch: [s390x, arm64, amd64]
44+
fail-fast: false
4345
steps:
4446
- uses: actions/checkout@v4
4547

@@ -69,6 +71,7 @@ jobs:
6971
strategy:
7072
matrix:
7173
freebsd_arch: [aarch64, amd64]
74+
fail-fast: false
7275
steps:
7376
- uses: actions/checkout@v4
7477

.github/workflows/webpack.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: "Install dependencies"
2222
shell: bash
2323
run: |
24-
npm install
24+
npm install --ignore-scripts
2525
2626
- name: "Install dependencies in the Webpack bundle test package"
2727
shell: bash

lib/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ async function initializeServer(service) {
224224
return await promisifiedInitializeServer.call(this, service);
225225
}
226226

227+
/**
228+
* @private
229+
*
230+
* The NAPI version kerberos is configured to use, defined in kerberos.h.
231+
*/
232+
const definedNapiVersion = kerberos.definedNapiVersion;
233+
227234
module.exports = {
228235
initializeClient,
229236
initializeServer,
@@ -246,5 +253,6 @@ module.exports = {
246253
GSS_MECH_OID_KRB5,
247254
GSS_MECH_OID_SPNEGO,
248255

249-
version
256+
version,
257+
definedNapiVersion
250258
};

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"url": "https://jira.mongodb.org/projects/NODE/issues/"
3030
},
3131
"dependencies": {
32-
"node-addon-api": "^6.1.0",
32+
"node-addon-api": "^8.5.0",
3333
"prebuild-install": "^7.1.3"
3434
},
3535
"devDependencies": {
@@ -69,7 +69,7 @@
6969
},
7070
"binary": {
7171
"napi_versions": [
72-
4
72+
9
7373
]
7474
},
7575
"license": "Apache-2.0",

src/kerberos.cc

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "kerberos.h"
2+
23
#include "kerberos_worker.h"
34

45
/// KerberosClient
@@ -13,22 +14,22 @@ struct InstanceData {
1314
};
1415

1516
static constexpr napi_property_attributes writable_and_configurable =
16-
static_cast<napi_property_attributes>(
17-
static_cast<int>(napi_writable) | static_cast<int>(napi_configurable));
17+
static_cast<napi_property_attributes>(static_cast<int>(napi_writable) |
18+
static_cast<int>(napi_configurable));
1819

1920
inline String NewMaybeWideString(Env env, const char* str) {
2021
return String::New(env, str);
2122
}
2223
#ifdef _WIN32
2324
inline String NewMaybeWideString(Env env, const WCHAR* str) {
2425
static_assert(sizeof(std::wstring::value_type) == sizeof(std::u16string::value_type),
25-
"wstring and u16string have the same value type on Windows");
26+
"wstring and u16string have the same value type on Windows");
2627
std::wstring wstr(str);
2728
std::u16string u16(wstr.begin(), wstr.end());
2829
return String::New(env, u16);
2930
}
3031
#endif
31-
}
32+
} // namespace
3233

3334
std::string ToStringWithNonStringAsEmpty(Napi::Value value) {
3435
if (!value.IsString()) {
@@ -38,7 +39,8 @@ std::string ToStringWithNonStringAsEmpty(Napi::Value value) {
3839
}
3940

4041
int KerberosClient::ParseWrapOptionsProtect(const Napi::Object& options) {
41-
if (!options.Has("protect")) return 0;
42+
if (!options.Has("protect"))
43+
return 0;
4244

4345
if (!options.Get("protect").IsBoolean()) {
4446
throw TypeError::New(options.Env(), "options.protect must be a boolean.");
@@ -49,18 +51,16 @@ int KerberosClient::ParseWrapOptionsProtect(const Napi::Object& options) {
4951
}
5052

5153
Function KerberosClient::Init(Napi::Env env) {
52-
return
53-
DefineClass(env,
54-
"KerberosClient",
55-
{
56-
InstanceMethod("step", &KerberosClient::Step, writable_and_configurable),
57-
InstanceMethod("wrap", &KerberosClient::WrapData, writable_and_configurable),
58-
InstanceMethod("unwrap", &KerberosClient::UnwrapData, writable_and_configurable),
59-
InstanceAccessor("username", &KerberosClient::UserNameGetter, nullptr),
60-
InstanceAccessor("response", &KerberosClient::ResponseGetter, nullptr),
61-
InstanceAccessor("responseConf", &KerberosClient::ResponseConfGetter, nullptr),
62-
InstanceAccessor("contextComplete", &KerberosClient::ContextCompleteGetter, nullptr)
63-
});
54+
return DefineClass(
55+
env,
56+
"KerberosClient",
57+
{InstanceMethod("step", &KerberosClient::Step, writable_and_configurable),
58+
InstanceMethod("wrap", &KerberosClient::WrapData, writable_and_configurable),
59+
InstanceMethod("unwrap", &KerberosClient::UnwrapData, writable_and_configurable),
60+
InstanceAccessor("username", &KerberosClient::UserNameGetter, nullptr),
61+
InstanceAccessor("response", &KerberosClient::ResponseGetter, nullptr),
62+
InstanceAccessor("responseConf", &KerberosClient::ResponseConfGetter, nullptr),
63+
InstanceAccessor("contextComplete", &KerberosClient::ContextCompleteGetter, nullptr)});
6464
}
6565

6666
Object KerberosClient::NewInstance(Napi::Env env, std::shared_ptr<krb_client_state> state) {
@@ -71,8 +71,7 @@ Object KerberosClient::NewInstance(Napi::Env env, std::shared_ptr<krb_client_sta
7171
return obj;
7272
}
7373

74-
KerberosClient::KerberosClient(const CallbackInfo& info)
75-
: ObjectWrap(info) {}
74+
KerberosClient::KerberosClient(const CallbackInfo& info) : ObjectWrap(info) {}
7675

7776
std::shared_ptr<krb_client_state> KerberosClient::state() const {
7877
return _state;
@@ -102,16 +101,14 @@ Value KerberosClient::ContextCompleteGetter(const CallbackInfo& info) {
102101

103102
/// KerberosServer
104103
Function KerberosServer::Init(Napi::Env env) {
105-
return
106-
DefineClass(env,
107-
"KerberosServer",
108-
{
109-
InstanceMethod("step", &KerberosServer::Step, writable_and_configurable),
110-
InstanceAccessor("username", &KerberosServer::UserNameGetter, nullptr),
111-
InstanceAccessor("response", &KerberosServer::ResponseGetter, nullptr),
112-
InstanceAccessor("targetName", &KerberosServer::TargetNameGetter, nullptr),
113-
InstanceAccessor("contextComplete", &KerberosServer::ContextCompleteGetter, nullptr)
114-
});
104+
return DefineClass(
105+
env,
106+
"KerberosServer",
107+
{InstanceMethod("step", &KerberosServer::Step, writable_and_configurable),
108+
InstanceAccessor("username", &KerberosServer::UserNameGetter, nullptr),
109+
InstanceAccessor("response", &KerberosServer::ResponseGetter, nullptr),
110+
InstanceAccessor("targetName", &KerberosServer::TargetNameGetter, nullptr),
111+
InstanceAccessor("contextComplete", &KerberosServer::ContextCompleteGetter, nullptr)});
115112
}
116113

117114
Object KerberosServer::NewInstance(Napi::Env env, std::shared_ptr<krb_server_state> state) {
@@ -122,8 +119,7 @@ Object KerberosServer::NewInstance(Napi::Env env, std::shared_ptr<krb_server_sta
122119
return obj;
123120
}
124121

125-
KerberosServer::KerberosServer(const CallbackInfo& info)
126-
: ObjectWrap(info) {}
122+
KerberosServer::KerberosServer(const CallbackInfo& info) : ObjectWrap(info) {}
127123

128124
std::shared_ptr<krb_server_state> KerberosServer::state() const {
129125
return _state;
@@ -154,31 +150,8 @@ Value KerberosServer::ContextCompleteGetter(const CallbackInfo& info) {
154150
return Boolean::New(Env(), state()->context_complete);
155151
}
156152

157-
void TestMethod(const CallbackInfo& info) {
158-
std::string string = info[0].ToString();
159-
bool shouldError = info[1].ToBoolean();
160-
161-
std::string optionalString;
162-
Function callback;
163-
if (info[2].IsFunction()) {
164-
callback = info[2].As<Function>();
165-
} else {
166-
optionalString = info[2].ToString();
167-
callback = info[3].As<Function>();
168-
}
169-
170-
KerberosWorker::Run(callback, "kerberos:TestMethod", [=](KerberosWorker::SetOnFinishedHandler onFinished) {
171-
return onFinished([=](KerberosWorker* worker) {
172-
Napi::Env env = worker->Env();
173-
if (shouldError) {
174-
worker->Call(std::initializer_list<napi_value>
175-
{ Error::New(env).Value(), env.Null() });
176-
} else {
177-
worker->Call(std::initializer_list<napi_value>
178-
{ env.Null(), String::New(env, optionalString) });
179-
}
180-
});
181-
});
153+
Value GetDefinedNapiVersion(const CallbackInfo& info) {
154+
return String::New(info.Env(), std::to_string(NAPI_VERSION));
182155
}
183156

184157
static Object Init(Env env, Object exports) {
@@ -190,18 +163,16 @@ static Object Init(Env env, Object exports) {
190163
Function KerberosServerCtor = KerberosServer::Init(env);
191164
exports["KerberosClient"] = KerberosClientCtor;
192165
exports["KerberosServer"] = KerberosServerCtor;
193-
env.SetInstanceData(new InstanceData {
194-
Reference<Function>::New(KerberosClientCtor, 1),
195-
Reference<Function>::New(KerberosServerCtor, 1)
196-
});
166+
env.SetInstanceData(new InstanceData{Reference<Function>::New(KerberosClientCtor, 1),
167+
Reference<Function>::New(KerberosServerCtor, 1)});
197168
exports["initializeClient"] = Function::New(env, InitializeClient);
198169
exports["initializeServer"] = Function::New(env, InitializeServer);
199170
exports["principalDetails"] = Function::New(env, PrincipalDetails);
200171
exports["checkPassword"] = Function::New(env, CheckPassword);
201-
exports["_testMethod"] = Function::New(env, TestMethod);
172+
exports["definedNapiVersion"] = Function::New(env, GetDefinedNapiVersion);
202173
return exports;
203174
}
204175

205176
NODE_API_MODULE(kerberos, Init)
206177

207-
}
178+
} // namespace node_kerberos

src/kerberos.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
#ifndef KERBEROS_NATIVE_EXTENSION_H
22
#define KERBEROS_NATIVE_EXTENSION_H
33

4-
// We generally only target N-API version 4, but the instance data
5-
// feature is only available in N-API version 6. However, it is
6-
// available in all Node.js versions that have N-API version 4
7-
#define NAPI_VERSION 6
8-
// as an experimental feature (that has not been changed since then).
9-
#define NAPI_EXPERIMENTAL
10-
#define NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT
4+
#define NAPI_VERSION 9
115

126
#include <napi.h>
7+
138
#include "kerberos_common.h"
149

1510
namespace node_kerberos {
@@ -69,11 +64,10 @@ void InitializeClient(const Napi::CallbackInfo& info);
6964
void InitializeServer(const Napi::CallbackInfo& info);
7065
void CheckPassword(const Napi::CallbackInfo& info);
7166

72-
// NOTE: explicitly used for unit testing `defineOperation`, not meant to be exported
73-
void TestMethod(const Napi::CallbackInfo& info);
74-
7567
std::string ToStringWithNonStringAsEmpty(Napi::Value value);
7668

77-
}
69+
Napi::Value GetDefinedNapiVersion(const Napi::CallbackInfo& info);
70+
71+
} // namespace node_kerberos
7872

7973
#endif // KERBEROS_NATIVE_EXTENSION_H

test/bundling/webpack/install_kerberos.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
'use strict';
23

34
const { execSync } = require('node:child_process');
@@ -19,6 +20,7 @@ console.log(`kerberos Version: ${kerberosVersion}`);
1920

2021
xtrace('npm pack --pack-destination test/bundling/webpack', { cwd: kerberosRoot });
2122

22-
xtrace(`npm install --no-save kerberos-${kerberosVersion}.tgz`);
23+
// --ignore-scripts: don't worry about downloading prebuilt binaries, we're only bundling
24+
xtrace(`npm install --ignore-scripts --no-save kerberos-${kerberosVersion}.tgz`);
2325

2426
console.log('kerberos installed!');

test/kerberos_tests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,8 @@ describe('Kerberos', function () {
221221
});
222222
});
223223
});
224+
225+
it('definedNapiVersion() returns 9', function () {
226+
expect(kerberos.definedNapiVersion()).to.equal('9');
227+
});
224228
});

0 commit comments

Comments
 (0)