Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

unable to require native module dynamically (for use in VSCode extension) #106

@TomasHubelbauer

Description

@TomasHubelbauer

Hey, I want to use Keytar in a VS Code extension.

VS Code extensions do not allow for depending on native modules.

What extensions do to work around this so to download native dependencies after installation as a part of the activation procedure. I have drafted one such procedure substituting in arch, platform and modules to make sure I get the right binary for the runtime I'm in:

const version = '4.2.1';
const name = `keytar-v${version}-electron-v${process.versions.modules}-${process.platform}-${process.arch}`;
const tarGzFilePath = path.join(__dirname, name + '.tar.gz');
const tarFilePath = path.join(__dirname, name + '.tar.gz');
const nodeFilePath = path.join(__dirname, 'build/Release/keytar.node');
const response = await fetch('https://api.github.com/repos/atom/node-keytar/releases');
const data = await response.json();
const release = data.find(release => release.tag_name === 'v' + version);
const asset = release.assets.find(asset => asset.name === name + '.tar.gz');
await fs.writeFile(tarGzFilePath, await (await fetch(asset.browser_download_url)).buffer());
await tar.x({ f: tarGzFilePath, cwd: __dirname }, [name + '.tar']);
await tar.x({ f: tarFilePath, cwd: __dirname }, ['build/Release/keytar.node']);
const keytar = require(nodeFilePath);
await keytar.setPassword('test', 'test', 'test');
console.log(await keytar.findPassword('test'));

However (at least with keytar-v4.2.1-electron-v57-win32-x64), I'll get Module did not self-register. immediately when trying to use require on the downloaded file. I know the file is downloaded (otherwise I would get a file system exception) and I also know it is the right modules-platform-arch triplet as I substitute that dynamically based on the environment.

I checked the .node file and was able to verify it's a valid PE32+ executable built against x64 so I think I have the right file.

You can try this yourself.

What could be the problem?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions