diff --git a/.husky/pre-commit b/.husky/pre-commit index f5c8eadb..61d94aad 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -yarn lint-staged && cargo fmt --all \ No newline at end of file +yarn lint-staged && cargo fmt --all diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index 3a7a80e7..a05ba110 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -148,6 +148,7 @@ static KNOWN_TYPES: Lazy> = La ("Map", ("Record", false, false)), ("HashMap", ("Record<{}, {}>", false, false)), ("ArrayBuffer", ("ArrayBuffer", false, false)), + ("JsArrayBuffer", ("ArrayBuffer", false, false)), ("Int8Array", ("Int8Array", false, false)), ("Uint8Array", ("Uint8Array", false, false)), ("Uint8ClampedArray", ("Uint8ClampedArray", false, false)), diff --git a/crates/napi/src/js_values/arraybuffer.rs b/crates/napi/src/js_values/arraybuffer.rs index deb12b2d..ae8bf3da 100644 --- a/crates/napi/src/js_values/arraybuffer.rs +++ b/crates/napi/src/js_values/arraybuffer.rs @@ -147,7 +147,7 @@ impl JsArrayBuffer { let mut data = ptr::null_mut(); let mut len: usize = 0; check_status!(unsafe { - sys::napi_get_arraybuffer_info(self.0.env, self.0.value, &mut data, &mut len as *mut usize) + sys::napi_get_arraybuffer_info(self.0.env, self.0.value, &mut data, &mut len) })?; Ok(JsArrayBufferValue { data, diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index 492626e0..3b6b9133 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -252,6 +252,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function arrayBufferPassThrough(buf: Uint8Array): Promise␊ ␊ + export function asyncBufferToArray(buf: ArrayBuffer): Array␊ + ␊ export function asyncMultiTwo(arg: number): Promise␊ ␊ export function asyncPlus100(p: Promise): Promise␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 9d28de1e..8c15e912 100644 Binary files a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap and b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap differ diff --git a/examples/napi/__tests__/values.spec.ts b/examples/napi/__tests__/values.spec.ts index 6adede8b..4de2771b 100644 --- a/examples/napi/__tests__/values.spec.ts +++ b/examples/napi/__tests__/values.spec.ts @@ -54,6 +54,7 @@ const { getPackageJsonName, getBuffer, getEmptyBuffer, + asyncBufferToArray, readFileAsync, eitherStringOrNumber, returnEither, @@ -649,6 +650,9 @@ test('buffer', (t) => { const b = getEmptyBuffer() t.is(a.toString(), '') t.is(b.toString(), '') + + // @ts-expect-error + t.true(Array.isArray(asyncBufferToArray(Buffer.from([1, 2, 3]).buffer))) }) test('reset empty buffer', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index b4386b8e..72b926a1 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -242,6 +242,8 @@ export function apply1(ctx: Animal, callback: (...args: any[]) => any, name: str export function arrayBufferPassThrough(buf: Uint8Array): Promise +export function asyncBufferToArray(buf: ArrayBuffer): Array + export function asyncMultiTwo(arg: number): Promise export function asyncPlus100(p: Promise): Promise diff --git a/examples/napi/index.wasi-browser.js b/examples/napi/index.wasi-browser.js index b600ef7d..091bd169 100644 --- a/examples/napi/index.wasi-browser.js +++ b/examples/napi/index.wasi-browser.js @@ -346,6 +346,7 @@ function __napi_rs_initialize_modules(__napiInstance) { __napiInstance.exports['__napi_register__array_buffer_pass_through_295']?.() __napiInstance.exports['__napi_register__AsyncBuffer_impl_296']?.() __napiInstance.exports['__napi_register__async_reduce_buffer_297']?.() + __napiInstance.exports['__napi_register__async_buffer_to_array_298']?.() } export const Animal = __napiModule.exports.Animal export const AnimalWithDefaultConstructor = @@ -399,6 +400,7 @@ export const apply0 = __napiModule.exports.apply0 export const apply1 = __napiModule.exports.apply1 export const arrayBufferPassThrough = __napiModule.exports.arrayBufferPassThrough +export const asyncBufferToArray = __napiModule.exports.asyncBufferToArray export const asyncMultiTwo = __napiModule.exports.asyncMultiTwo export const asyncPlus100 = __napiModule.exports.asyncPlus100 export const asyncReduceBuffer = __napiModule.exports.asyncReduceBuffer diff --git a/examples/napi/index.wasi.cjs b/examples/napi/index.wasi.cjs index d55d2e39..465698df 100644 --- a/examples/napi/index.wasi.cjs +++ b/examples/napi/index.wasi.cjs @@ -311,6 +311,7 @@ function __napi_rs_initialize_modules(__napiInstance) { __napiInstance.exports['__napi_register__array_buffer_pass_through_295']?.() __napiInstance.exports['__napi_register__AsyncBuffer_impl_296']?.() __napiInstance.exports['__napi_register__async_reduce_buffer_297']?.() + __napiInstance.exports['__napi_register__async_buffer_to_array_298']?.() } module.exports.Animal = __napiModule.exports.Animal module.exports.AnimalWithDefaultConstructor = __napiModule.exports.AnimalWithDefaultConstructor @@ -358,6 +359,7 @@ module.exports.appendBuffer = __napiModule.exports.appendBuffer module.exports.apply0 = __napiModule.exports.apply0 module.exports.apply1 = __napiModule.exports.apply1 module.exports.arrayBufferPassThrough = __napiModule.exports.arrayBufferPassThrough +module.exports.asyncBufferToArray = __napiModule.exports.asyncBufferToArray module.exports.asyncMultiTwo = __napiModule.exports.asyncMultiTwo module.exports.asyncPlus100 = __napiModule.exports.asyncPlus100 module.exports.asyncReduceBuffer = __napiModule.exports.asyncReduceBuffer diff --git a/examples/napi/src/typed_array.rs b/examples/napi/src/typed_array.rs index 2d7caaef..ba2029a0 100644 --- a/examples/napi/src/typed_array.rs +++ b/examples/napi/src/typed_array.rs @@ -1,4 +1,4 @@ -use napi::bindgen_prelude::*; +use napi::{bindgen_prelude::*, JsArrayBuffer}; #[napi] fn get_buffer() -> Buffer { @@ -71,3 +71,8 @@ impl Task for AsyncBuffer { fn async_reduce_buffer(buf: Buffer) -> Result> { Ok(AsyncTask::new(AsyncBuffer { buf })) } + +#[napi] +fn async_buffer_to_array(buf: JsArrayBuffer) -> Result> { + Ok(buf.into_value()?.as_ref().to_vec()) +} diff --git a/package.json b/package.json index 7fafced1..a8108268 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "test:macro": "cargo test -p napi-examples", "test:memory": "node memory-testing/index.mjs", "postinstall": "husky install", - "prepublish": "yarn build" + "prepublish": "yarn build", + "prepare": "husky" }, "bugs": { "url": "https://github.com/napi-rs/napi-rs/issues" @@ -80,7 +81,7 @@ "eslint": "^8.55.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-import": "^2.29.0", - "husky": "^9.0.0", + "husky": "^9.0.6", "lerna": "^8.0.0", "lint-staged": "^15.2.0", "npm-run-all": "^4.1.5", diff --git a/yarn.lock b/yarn.lock index a2b6aef8..3b828fc5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6668,12 +6668,12 @@ __metadata: languageName: node linkType: hard -"husky@npm:^9.0.0": - version: 9.0.1 - resolution: "husky@npm:9.0.1" +"husky@npm:^9.0.6": + version: 9.0.6 + resolution: "husky@npm:9.0.6" bin: husky: bin.js - checksum: d7f3d158fffc8cc91211172637f7069b2173dbf5e732f46596a2a512e5f5f17ddebd895202cc67b0f159b747b698e2cc5601282b198d9cb5d97a5671fcb2afb7 + checksum: 1ef034899dcdc35e5bf5436e1e02f8a7177dbb8a10dd91b77456d7fe110c204d8d4a7d53180e0651b87e35f39d7e0e95a53b4b7c334f68aefbc0d93bce4b54f8 languageName: node linkType: hard @@ -8705,7 +8705,7 @@ __metadata: eslint: "npm:^8.55.0" eslint-config-prettier: "npm:^9.1.0" eslint-plugin-import: "npm:^2.29.0" - husky: "npm:^9.0.0" + husky: "npm:^9.0.6" lerna: "npm:^8.0.0" lint-staged: "npm:^15.2.0" npm-run-all: "npm:^4.1.5"