Skip to content

Commit cd2daf0

Browse files
authored
fix(js): prevent double free on Buffer by passing a BufferSlice (#369)
As a non-async function, `decode_buffer` doesn't require owning the `Buffer` and can do with only a `BufferSlice`. This takes the cleanup responsibility from `napi-rs` and should prevent the double free scenarios, as the `Buffer` is now Node runtime-managed. Closes #368
1 parent 8205156 commit cd2daf0

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

impit-node/src/response.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use impit::utils::{decode, ContentType};
44
use napi::bindgen_prelude::JsObjectValue;
55
use napi::{
66
bindgen_prelude::{
7-
Buffer, FromNapiValue, Function, Object, ReadableStream, Result, This, ToNapiValue,
7+
BufferSlice, FromNapiValue, Function, Object, ReadableStream, Result, This, ToNapiValue,
88
},
99
sys, Env, JsValue, Unknown,
1010
};
@@ -178,14 +178,14 @@ impl<'env> ImpitResponse {
178178

179179
/// @ignore
180180
#[napi(ts_return_type = "string")]
181-
pub fn decode_buffer(&self, buffer: Buffer) -> Result<String> {
181+
pub fn decode_buffer(&self, buffer: BufferSlice) -> Result<String> {
182182
let encoding = self
183183
.headers
184184
.get("content-type")
185185
.and_then(|content_type| ContentType::from(content_type).ok());
186186

187187
let string = decode(
188-
buffer.to_vec().as_slice(),
188+
&buffer,
189189
match encoding {
190190
Some(encoding) => encoding.into(),
191191
None => None,

0 commit comments

Comments
 (0)