Skip to content

Commit

Permalink
[keyserver] Fix rust-node-addon after deprecation of --experimental-s…
Browse files Browse the repository at this point in the history
…pecifier-resolution=node

Summary:
Context in [ENG-3059](https://linear.app/comm/issue/ENG-3059/keyserver-fails-to-load-napi-compiled-module). `rust-node-addon` was broken by D6695, but `--experimental-specifier-resolution=node` is going away so we need another solution.

I got this solution from [this GitHub comment](nodejs/node#40541 (comment)).

Depends on D6783

Test Plan: Confirm I could import `rust-node-addon` and call methods on it from @jon's `jonringer/identity-deleteuserrpc` branch

Reviewers: jon, varun, max

Reviewed By: jon

Subscribers: tomek, atul, jon

Differential Revision: https://phab.comm.dev/D6784
  • Loading branch information
Ashoat committed Feb 20, 2023
1 parent c6ae007 commit 9acb9c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 12 additions & 5 deletions keyserver/addons/rust-node-addon/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// @flow

import invariant from 'invariant';
import { createRequire } from 'module';

const { platform, arch } = process;

const importMetaURL = import.meta.url;
invariant(importMetaURL, 'import.meta.url should be set');
const require = createRequire(importMetaURL);

type RustAPI = {
+registerUser: (
userId: string,
Expand All @@ -16,16 +23,16 @@ async function getRustAPI(): Promise<RustAPI> {
let nativeBinding = null;
if (platform === 'darwin' && arch === 'x64') {
// $FlowFixMe
nativeBinding = await import('./napi/rust-node-addon.darwin-x64.node');
nativeBinding = require('./napi/rust-node-addon.darwin-x64.node');
} else if (platform === 'darwin' && arch === 'arm64') {
// $FlowFixMe
nativeBinding = await import('./napi/rust-node-addon.darwin-arm64.node');
nativeBinding = require('./napi/rust-node-addon.darwin-arm64.node');
} else if (platform === 'linux' && arch === 'x64') {
// $FlowFixMe
nativeBinding = await import('./napi/rust-node-addon.linux-x64-gnu.node');
nativeBinding = require('./napi/rust-node-addon.linux-x64-gnu.node');
} else if (platform === 'linux' && arch === 'arm64') {
// $FlowFixMe
nativeBinding = await import('./napi/rust-node-addon.linux-arm64-gnu.node');
nativeBinding = require('./napi/rust-node-addon.linux-arm64-gnu.node');
} else {
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`);
}
Expand All @@ -34,7 +41,7 @@ async function getRustAPI(): Promise<RustAPI> {
throw new Error('Failed to load native binding');
}

const { registerUser } = nativeBinding.default;
const { registerUser } = nativeBinding;
return { registerUser };
}

Expand Down
3 changes: 3 additions & 0 deletions keyserver/addons/rust-node-addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
}
},
"license": "BSD-3-Clause",
"dependencies": {
"invariant": "^2.2.4"
},
"devDependencies": {
"@napi-rs/cli": "^2.13.0"
},
Expand Down

0 comments on commit 9acb9c0

Please sign in to comment.