Unverified Commit 177e15b6 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

pass Buffer directly to Rust (#30975)

parent bc19c2a5
...@@ -50,7 +50,7 @@ if (!bindings) { ...@@ -50,7 +50,7 @@ if (!bindings) {
} }
async function transform(src, options) { async function transform(src, options) {
const isModule = typeof src !== 'string' const isModule = typeof src !== 'string' && !Buffer.isBuffer(src)
options = options || {} options = options || {}
if (options?.jsc?.parser) { if (options?.jsc?.parser) {
...@@ -65,7 +65,7 @@ async function transform(src, options) { ...@@ -65,7 +65,7 @@ async function transform(src, options) {
} }
function transformSync(src, options) { function transformSync(src, options) {
const isModule = typeof src !== 'string' const isModule = typeof src !== 'string' && !Buffer.isBuffer(src)
options = options || {} options = options || {}
if (options?.jsc?.parser) { if (options?.jsc?.parser) {
......
...@@ -32,8 +32,9 @@ use crate::{ ...@@ -32,8 +32,9 @@ use crate::{
TransformOptions, TransformOptions,
}; };
use anyhow::{anyhow, Context as _, Error}; use anyhow::{anyhow, Context as _, Error};
use napi::{CallContext, Env, JsBoolean, JsObject, JsString, Status, Task}; use napi::{CallContext, Env, JsBoolean, JsBuffer, JsObject, JsString, JsUnknown, Status, Task};
use std::{ use std::{
convert::TryFrom,
panic::{catch_unwind, AssertUnwindSafe}, panic::{catch_unwind, AssertUnwindSafe},
sync::Arc, sync::Arc,
}; };
...@@ -117,7 +118,23 @@ where ...@@ -117,7 +118,23 @@ where
{ {
let c = get_compiler(&cx); let c = get_compiler(&cx);
let src = cx.get::<JsString>(0)?.into_utf8()?.as_str()?.to_owned(); let unknown_src = cx.get::<JsUnknown>(0)?;
let src = match unknown_src.get_type()? {
napi::ValueType::String => napi::Result::Ok(
JsString::try_from(unknown_src)?
.into_utf8()?
.as_str()?
.to_owned(),
),
napi::ValueType::Object => napi::Result::Ok(
String::from_utf8_lossy(JsBuffer::try_from(unknown_src)?.into_value()?.as_ref())
.to_string(),
),
_ => Err(napi::Error::new(
Status::GenericFailure,
"first argument must be a String or Buffer".to_string(),
)),
}?;
let is_module = cx.get::<JsBoolean>(1)?; let is_module = cx.get::<JsBoolean>(1)?;
let options = cx.get_buffer_as_string(2)?; let options = cx.get_buffer_as_string(2)?;
......
...@@ -191,3 +191,6 @@ export default function swcLoader(inputSource, inputSourceMap) { ...@@ -191,3 +191,6 @@ export default function swcLoader(inputSource, inputSourceMap) {
} }
) )
} }
// accept Buffers instead of strings
export const raw = true
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment