Skip to content

Commit 4be4007

Browse files
committed
Move to Buffers to take advantage of zero-copy TypedArrays.
1 parent c4c9e10 commit 4be4007

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

bin/bench-ai.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export async function main() {
3030
Authorization: token, // eslint-disable-line @typescript-eslint/naming-convention
3131
'Content-Type': 'application/json',
3232
},
33-
setupRequest: path.join(import.meta.dirname, './setup-ai-request'),
33+
setupRequest: path.join(
34+
import.meta.dirname,
35+
'./hooks/setup-ai-request'
36+
),
3437
},
3538
],
3639
})

lib/jwt_rsa/src/authorizer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#![allow(missing_docs)]
22

3+
use std::str::from_utf8;
4+
35
use biscuit::{
46
jwa::SignatureAlgorithm,
57
jwk::{AlgorithmParameters, JWKSet, JWK},
68
jws::Secret,
79
ClaimPresenceOptions, ClaimsSet, Empty, ValidationOptions, JWT,
810
};
911
use derive_new::new;
12+
use napi::bindgen_prelude::Buffer;
1013

1114
use super::{outputs::RegisteredClaims, Error};
1215

@@ -47,7 +50,10 @@ impl Authorizer {
4750

4851
/// Authorize the request
4952
#[napi]
50-
pub fn authorize(&self, auth_header: String) -> napi::Result<RegisteredClaims> {
53+
pub fn authorize(&self, auth_header: Buffer) -> napi::Result<RegisteredClaims> {
54+
let auth_header =
55+
from_utf8(&auth_header).map_err(|e| napi::Error::from_reason(e.to_string()))?;
56+
5157
let jwt = jwt_from_header(auth_header)
5258
.map_err(|err| napi::Error::from_reason(err.to_string()))?
5359
.ok_or(napi::Error::from_reason("No JWT found".to_string()))?;
@@ -89,7 +95,7 @@ impl Authorizer {
8995

9096
/// If an authorization header is provided, make sure it's in the expected format, and
9197
/// return it as a String.
92-
fn jwt_from_header(header: String) -> Result<Option<String>, Error> {
98+
fn jwt_from_header(header: &str) -> Result<Option<String>, Error> {
9399
let auth_header = if let Ok(value) = std::str::from_utf8(header.as_bytes()) {
94100
value
95101
} else {

lib/text_classification_rs/src/infer.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#![allow(missing_docs)]
22

3-
use std::path::PathBuf;
3+
use std::{path::PathBuf, str::from_utf8};
44

55
use derive_new::new;
6+
use napi::bindgen_prelude::Buffer;
67
use rust_bert::{
78
pipelines::{
89
common::{ModelResource, ModelType},
@@ -47,9 +48,11 @@ impl Inference {
4748

4849
/// Infer the class of the input text
4950
#[napi]
50-
pub fn infer(&self, input: String) -> napi::Result<String> {
51-
let output = self.model.predict(vec![input.as_str()]);
51+
pub fn infer(&self, input: Buffer) -> napi::Result<Buffer> {
52+
let input = from_utf8(&input).map_err(|e| napi::Error::from_reason(e.to_string()))?;
5253

53-
Ok(output[0].text.clone())
54+
let output = self.model.predict(vec![input]);
55+
56+
Ok(output[0].text.clone().into())
5457
}
5558
}

types.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
/* eslint-disable import/no-unassigned-import, @typescript-eslint/consistent-type-definitions */
22
import 'autocannon'
33

4-
declare module 'onnxruntime-node' {
5-
export * from 'onnxruntime-common'
6-
}
7-
84
declare module 'autocannon' {
95
interface Request {
106
// Add your custom property here

0 commit comments

Comments
 (0)