没有为“.node”文件配置加载程序:../rust.linux-x64-gnu.node

问题描述 投票:0回答:1

我正在尝试在 aws lambda 中使用 rust,所以我使用 napi 在 Rust 中构建预编译的 Node.js 插件,或者使用来自 nodejs 的 rust

但是当我尝试获取从 rust 转换为 js 的函数时,当我尝试部署到 aws lambda 时,我收到此错误

Bundling asset MyAppStack/lambda/Code/Stage...
✘ [ERROR] No loader is configured for ".node" files: ../rust.linux-x64-gnu.node

    ../index.js:188:38:
      188 │               nativeBinding = require('./rust.linux-x64-gnu.node')
          ╵                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error

这就是我尝试使用代码的方式

import { Hono } from 'hono'
import { handle } from 'hono/aws-lambda'
import {scrapeTextFromUrls} from "@gen-fellow/rust-lib"
const app = new Hono()

app.get('/', async (c) => {
    const urls = ["https://www.google.com"]
    const text = scrapeTextFromUrls(urls) //this is the function that is being converted from Rust to JS
    return c.json(text)
})

export const handler = handle(app)

在此文件中我收到错误

No loader is configured for ".node" files: ../rust.linux-x64-gnu.node

查看代码中突出显示的部分

/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */

/* 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, 'rust.android-arm64.node'))
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.android-arm64.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-android-arm64')
          }
        } catch (e) {
          loadError = e
        }
        break
      case 'arm':
        localFileExisted = existsSync(join(__dirname, 'rust.android-arm-eabi.node'))
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.android-arm-eabi.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-android-arm-eabi')
          }
        } catch (e) {
          loadError = e
        }
        break
      default:
        throw new Error(`Unsupported architecture on Android ${arch}`)
    }
    break
  case 'win32':
    switch (arch) {
      case 'x64':
        localFileExisted = existsSync(
          join(__dirname, 'rust.win32-x64-msvc.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.win32-x64-msvc.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-win32-x64-msvc')
          }
        } catch (e) {
          loadError = e
        }
        break
      case 'ia32':
        localFileExisted = existsSync(
          join(__dirname, 'rust.win32-ia32-msvc.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.win32-ia32-msvc.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-win32-ia32-msvc')
          }
        } catch (e) {
          loadError = e
        }
        break
      case 'arm64':
        localFileExisted = existsSync(
          join(__dirname, 'rust.win32-arm64-msvc.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.win32-arm64-msvc.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-win32-arm64-msvc')
          }
        } catch (e) {
          loadError = e
        }
        break
      default:
        throw new Error(`Unsupported architecture on Windows: ${arch}`)
    }
    break
  case 'darwin':
    localFileExisted = existsSync(join(__dirname, 'rust.darwin-universal.node'))
    try {
      if (localFileExisted) {
        nativeBinding = require('./rust.darwin-universal.node')
      } else {
        nativeBinding = require('@gen-fellow/rust-lib-darwin-universal')
      }
      break
    } catch {}
    switch (arch) {
      case 'x64':
        localFileExisted = existsSync(join(__dirname, 'rust.darwin-x64.node'))
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.darwin-x64.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-darwin-x64')
          }
        } catch (e) {
          loadError = e
        }
        break
      case 'arm64':
        localFileExisted = existsSync(
          join(__dirname, 'rust.darwin-arm64.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.darwin-arm64.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-darwin-arm64')
          }
        } catch (e) {
          loadError = e
        }
        break
      default:
        throw new Error(`Unsupported architecture on macOS: ${arch}`)
    }
    break
  case 'freebsd':
    if (arch !== 'x64') {
      throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
    }
    localFileExisted = existsSync(join(__dirname, 'rust.freebsd-x64.node'))
    try {
      if (localFileExisted) {
        nativeBinding = require('./rust.freebsd-x64.node')
      } else {
        nativeBinding = require('@gen-fellow/rust-lib-freebsd-x64')
      }
    } catch (e) {
      loadError = e
    }
    break
  case 'linux':
    switch (arch) {
      case 'x64':
        if (isMusl()) {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-x64-musl.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-x64-musl.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-x64-musl')
            }
          } catch (e) {
            loadError = e
          }
        } else {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-x64-gnu.node')
          )
          try {
            if (localFileExisted) {
             // ------------------here is the problem --------------------------------
              nativeBinding = require('./rust.linux-x64-gnu.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-x64-gnu')
            }
          } catch (e) {
            loadError = e
          }
        }
        break
      case 'arm64':
        if (isMusl()) {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-arm64-musl.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-arm64-musl.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-arm64-musl')
            }
          } catch (e) {
            loadError = e
          }
        } else {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-arm64-gnu.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-arm64-gnu.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-arm64-gnu')
            }
          } catch (e) {
            loadError = e
          }
        }
        break
      case 'arm':
        if (isMusl()) {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-arm-musleabihf.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-arm-musleabihf.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-arm-musleabihf')
            }
          } catch (e) {
            loadError = e
          }
        } else {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-arm-gnueabihf.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-arm-gnueabihf.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-arm-gnueabihf')
            }
          } catch (e) {
            loadError = e
          }
        }
        break
      case 'riscv64':
        if (isMusl()) {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-riscv64-musl.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-riscv64-musl.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-riscv64-musl')
            }
          } catch (e) {
            loadError = e
          }
        } else {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-riscv64-gnu.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-riscv64-gnu.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-riscv64-gnu')
            }
          } catch (e) {
            loadError = e
          }
        }
        break
      case 's390x':
        localFileExisted = existsSync(
          join(__dirname, 'rust.linux-s390x-gnu.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.linux-s390x-gnu.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-linux-s390x-gnu')
          }
        } catch (e) {
          loadError = e
        }
        break
      default:
        throw new Error(`Unsupported architecture on Linux: ${arch}`)
    }
    break
  default:
    throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}

if (!nativeBinding) {
  if (loadError) {
    throw loadError
  }
  throw new Error(`Failed to load native binding`)
}

const { scrapeWebsite, scrapeTextFromUrls, ModelTypes, generateEmbeddings } = nativeBinding

module.exports.scrapeWebsite = scrapeWebsite
module.exports.scrapeTextFromUrls = scrapeTextFromUrls
module.exports.ModelTypes = ModelTypes
module.exports.generateEmbeddings = generateEmbeddings

我想知道我应该如何继续前进...... 如果缺少东西请告诉我,我会发送 谢谢你

node.js rust aws-lambda hono
1个回答
0
投票

我得到了答案,我们可以像这样添加外部模块

my-app-stack.ts

bundling: {
        externalModules: ["@gen-fellow/rust-lib"],
      }
//my-app-stack.ts
import * as cdk from 'aws-cdk-lib'
import { Construct } from 'constructs'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as apigw from 'aws-cdk-lib/aws-apigateway'
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'

export class MyAppStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props)

    const fn = new NodejsFunction(this, 'lambda', {
      entry: 'lambda/index.ts',
      handler: 'handler',
      runtime: lambda.Runtime.NODEJS_20_X,
      bundling: {
        externalModules: ["@gen-fellow/rust-lib"],
      }
    })
    fn.addFunctionUrl({
      authType: lambda.FunctionUrlAuthType.NONE,
    })
    new apigw.LambdaRestApi(this, 'myapi', {
      handler: fn,
    })
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.