Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAPI RS Support for serverless Edge runtimes #1990

Open
aaronvg opened this issue Mar 6, 2024 · 2 comments
Open

NAPI RS Support for serverless Edge runtimes #1990

aaronvg opened this issue Mar 6, 2024 · 2 comments

Comments

@aaronvg
Copy link

aaronvg commented Mar 6, 2024

Some edge runtimes only have a limited number of node APIs available.

For example, vercel's runtime does not have the fs API:
https://nextjs.org/docs/app/api-reference/edge#unsupported-apis

I noticed the index.js script generated by the template napi-rs project has a require('fs') which crashes in these runtimes:

/* auto-generated by NAPI-RS */

const { existsSync, readFileSync } = require('fs')
const { join } = require('path')

const { platform, arch } = process

let nativeBinding = null
let localFileExisted = false
let loadError = null

function isMusl() {
  // For Node 10
  if (!process.report || typeof process.report.getReport !== 'function') {
    try {
      const lddPath = require('child_process').execSync('which ldd').toString().trim()
      return readFileSync(lddPath, 'utf8').includes('musl')
    } catch (e) {
      return true
    }
  } else {
    const { glibcVersionRuntime } = process.report.getReport().header
    return !glibcVersionRuntime
  }
}

switch (platform) {
  case 'android':
    switch (arch) {
      case 'arm64':
        localFileExisted = existsSync(join(__dirname, 'baml-core-napi.android-arm64.node'))
        try {
...

The script uses fs mainly to check for android-related binaries, which are not relevant in these edge function environments. Is there a way to remove that from the generated index.js? I'm trying to avoid having to edit the file.

@Brooooooklyn
Copy link
Sponsor Member

The edge runtime doesn't support the native addon, I will support it in the wasm target.

@aaronvg
Copy link
Author

aaronvg commented Mar 7, 2024

Oh interesting, is that easier than potentially creating a build that does not depend on the unsupported apis?

Also, so far we are loving this napi-rs. We were using neon and the release process left much to be desired. This just works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants