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) {
}
async function transform(src, options) {
const isModule = typeof src !== 'string'
const isModule = typeof src !== 'string' && !Buffer.isBuffer(src)
options = options || {}
if (options?.jsc?.parser) {
......@@ -65,7 +65,7 @@ async function transform(src, options) {
}
function transformSync(src, options) {
const isModule = typeof src !== 'string'
const isModule = typeof src !== 'string' && !Buffer.isBuffer(src)
options = options || {}
if (options?.jsc?.parser) {
......
......@@ -32,8 +32,9 @@ use crate::{
TransformOptions,
};
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::{
convert::TryFrom,
panic::{catch_unwind, AssertUnwindSafe},
sync::Arc,
};
......@@ -117,7 +118,23 @@ where
{
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 options = cx.get_buffer_as_string(2)?;
......
......@@ -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