denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.91k stars 5.22k forks source link

Loading @napi-rs/cli bindings in built npm package on windows / deploy #23465

Open WingZer0o opened 4 months ago

WingZer0o commented 4 months ago

Version: deno 1.42.4 (release, x86_64-pc-windows-msvc)

My personal cryptographic NPM package npm:cas-typescript-sdkthat creates Rust bindings to a TS lib through @napi-rs/cli seems to be working fine on Debian in WSL. However, when running on Windows or attempting to deploy the project to deploy I get the following error(s).

Perhaps this is an error in the way I constructed my NPM package? I am not the most experienced with crafting NPM packages and Deno.

No rush or anything, Would be happy to help investigate the issue or possibly submit a PR if the issue is not with my lib.

Windows Error

error: Uncaught (in promise) TypeError: LoadLibraryExW failed
    at Object.Module._extensions..node (node:module:698:20)
    at Module.load (node:module:591:32)
    at Function.Module._load (node:module:486:12)
    at Module.require (node:module:603:19)
    at require (node:module:709:16)
    at Object.<anonymous> (file:///C:/Users/mtmul/AppData/Local/deno/npm/registry.npmjs.org/cas-typescript-sdk/1.0.16/lib/password-hashers/argon2-wrapper.js:4:17)
    at Object.<anonymous> (file:///C:/Users/mtmul/AppData/Local/deno/npm/registry.npmjs.org/cas-typescript-sdk/1.0.16/lib/password-hashers/argon2-wrapper.js:21:4)
    at Module._compile (node:module:653:34)
    at Object.Module._extensions..js (node:module:667:10)
    at Module.load (node:module:591:32)

Deploy Error

error: The deployment failed: UNCAUGHT_EXCEPTION

Error: Node API is not supported in this environment
    at Object.Module._extensions..node (node:module:697:20)
    at Module.load (node:module:590:32)
    at Function.Module._load (node:module:485:12)
    at Module.require (node:module:602:19)
    at require (node:module:708:16)
    at Object.<anonymous> (file:///node_modules/.deno/cas-typescript-sdk@1.0.16/node_modules/cas-typescript-sdk/lib/password-hashers/argon2-wrapper.js:4:17)
    at Object.<anonymous> (file:///node_modules/.deno/cas-typescript-sdk@1.0.16/node_modules/cas-typescript-sdk/lib/password-hashers/argon2-wrapper.js:21:4)
    at Module._compile (node:module:652:34)
    at Object.Module._extensions..js (node:module:666:10)
    at Module.load (node:module:590:32)

index.ts

import { Application } from "https://deno.land/x/oak/mod.ts";
import router from "./router-one.ts";

const app = new Application();
app.use(router.routes());

await app.listen({ port: 8080 });

router-one.ts

import { RouteParams, Router } from "https://deno.land/x/oak/mod.ts";
import { AESWrapper } from "npm:cas-typescript-sdk";

const router = new Router();

router.post("/testing", async (context) => {
  const body: TestingBody = await context.request.body.json();
  const aes = new AESWrapper();
  const nonce = aes.aesNonce();
  const key = aes.aes256Key();
  const encoder = new TextEncoder();
  const tohashBytes: Array<number> = Array.from(encoder.encode(body.email));
  const encrypted = aes.aes256Encrypt(key, nonce, tohashBytes);
  const decrypted = aes.aes256Decrypt(key, nonce, encrypted);
  const decoder = new TextDecoder();
  const decoded = decoder.decode(new Uint8Array(decrypted));
  context.response.body = decoded;
});

class TestingBody {
  public email!: string;
}

export default router;

Compiled TS file where the error points to

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Argon2Wrapper = void 0;
const index_1 = require("./../../index");
class Argon2Wrapper {
    hashPassword(password) {
        if (!password) {
            throw new Error("You must provide a password to hash with Argon2");
        }
        return (0, index_1.argon2Hash)(password);
    }
    verifyPassword(hashedPassword, passwordToVerify) {
        if (!hashedPassword || !passwordToVerify) {
            throw new Error("You must provide a hashed password and a plaintext password to verify with Argon2");
        }
        return (0, index_1.argon2Verify)(hashedPassword, passwordToVerify);
    }
}
exports.Argon2Wrapper = Argon2Wrapper;
lucacasonato commented 3 months ago

NAPI is not supported on Deploy, that is correct. Deploy does not support suprocesses or node addons.

Are you using a binary built for Windows on Windows (and not accidentially a Linux binary?). Does it work on Windows in Node?

WingZer0o commented 2 months ago

This wasn't being done in deploy this was being done on my local machine. Its not a very popular library so usage would be low so I imagine its not a major priority. It shouldn't be another platform binary because I was under the assumption @napi-rs builds for the target system that you are on. But yes this does work on Windows for node. Example below.

import {AESWrapper} from "cas-typescript-sdk";

const aes = new AESWrapper();
  const nonce = aes.generateAESNonce();
  const key = aes.aes256Key();
  const encoder = new TextEncoder();
  const tohashBytes: Array<number> = Array.from(encoder.encode("sometestemail@outlook.com"));
  const encrypted = aes.aes256Encrypt(key, nonce, tohashBytes);
  const decrypted = aes.aes256Decrypt(key, nonce, encrypted);
  const decoder = new TextDecoder();
  const decoded = decoder.decode(new Uint8Array(decrypted));
  console.log(decoded);
devsnek commented 2 months ago

@WingZer0o if you rename node.exe to something else does it break in node as well?

WingZer0o commented 2 months ago

I don't have a node.exe in my current library, I have an index.node that is compiled from @napi-rs is that what you are referring to?

On Mon, Jun 17, 2024 at 1:50 AM snek @.***> wrote:

@WingZer0o https://github.com/WingZer0o if you rename node.exe to something else does it break in node as well?

— Reply to this email directly, view it on GitHub https://github.com/denoland/deno/issues/23465#issuecomment-2172340656, or unsubscribe https://github.com/notifications/unsubscribe-auth/BEG2SNFH4YYCD3NLA4AB2GTZHZ2JLAVCNFSM6AAAAABGP6D7ZKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNZSGM2DANRVGY . You are receiving this because you were mentioned.Message ID: @.***>

devsnek commented 2 months ago

@WingZer0o I mean, on the windows computer you are running this on, when you run your code with Node.js, you're presumably using a node.exe binary. Can you try renaming it to something else and running your code? I believe this is due to an issue where native modules link against "node.exe" rather than the current process (so renaming node.exe to something else would trigger the same issue you see in deno.exe). node-gyp has a fix for this (https://github.com/nodejs/node-gyp/blob/main/src/win_delay_load_hook.cc) but napi-rs does not.