Skip to content

Commit 19e2b28

Browse files
authored
feat: load bytes instead of strings (#357)
1 parent 64e1c27 commit 19e2b28

15 files changed

+564
-125
lines changed

Cargo.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async-trait = "0.1.68"
2929
data-url = "0.3.0"
3030
deno_ast = { version = "0.32.0", features = ["dep_analysis", "module_specifier"] }
3131
deno_semver = "0.5.4"
32+
encoding_rs = "0.8.33"
3233
futures = "0.3.26"
3334
import_map = "0.18.0"
3435
indexmap = { version = "2", features = ["serde"] }

deno.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"tasks": {
3-
"build": "cp LICENSE js/LICENSE && deno run --unstable -A --no-check https://deno.land/x/[email protected]/main.ts --no-default-features --project deno_graph_wasm --out js",
3+
"build": "cp LICENSE js/LICENSE && deno run -A --no-check https://deno.land/x/[email protected]/main.ts --no-default-features --project deno_graph_wasm --out js",
44
"build:npm": "deno run -A _build_npm.ts",
55
"test": "deno test --allow-read --allow-net"
66
},

js/mod.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export type {
4040
TypesDependency,
4141
} from "./types.ts";
4242

43+
const encoder = new TextEncoder();
44+
4345
// note: keep this in line with deno_cache
4446
export type CacheSetting = "only" | "use" | "reload";
4547

@@ -139,7 +141,22 @@ export async function createGraph(
139141
const { createGraph } = await wasm.instantiate();
140142
return await createGraph(
141143
rootSpecifiers,
142-
load,
144+
async (
145+
specifier: string,
146+
isDynamic: boolean,
147+
cacheSetting: CacheSetting,
148+
) => {
149+
const result = await load(specifier, isDynamic, cacheSetting);
150+
if (result?.kind === "module") {
151+
if (typeof result.content === "string") {
152+
result.content = encoder.encode(result.content);
153+
}
154+
// need to convert to an array for serde_wasm_bindgen to work
155+
// deno-lint-ignore no-explicit-any
156+
(result as any).content = Array.from(result.content);
157+
}
158+
return result;
159+
},
143160
defaultJsxImportSource,
144161
jsxImportSourceModule,
145162
cacheInfo,
@@ -192,7 +209,7 @@ export async function init(opts?: wasm.InstantiateOptions) {
192209
*/
193210
export function parseModule(
194211
specifier: string,
195-
content: string,
212+
content: Uint8Array,
196213
options: ParseModuleOptions = {},
197214
): ModuleJson {
198215
const {

js/test.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,13 @@ Deno.test({
538538
await init();
539539
const module = parseModule(
540540
"file:///a/test01.js",
541-
`
541+
new TextEncoder().encode(`
542542
/// <reference types="./test01.d.ts" />
543543
import { a } from "./a.ts";
544544
import * as b from "./b.ts";
545545
export { c } from "./c.ts";
546546
const d = await import("./d.ts");
547-
`,
547+
`),
548548
);
549549
assertEquals(module, {
550550
"specifier": "file:///a/test01.js",
@@ -609,9 +609,9 @@ Deno.test({
609609
await init();
610610
const module = parseModule(
611611
`https://example.com/a`,
612-
`declare interface A {
612+
new TextEncoder().encode(`declare interface A {
613613
a: string;
614-
}`,
614+
}`),
615615
{
616616
headers: {
617617
"content-type": "application/typescript; charset=utf-8",
@@ -628,10 +628,10 @@ Deno.test({
628628
await init();
629629
const module = parseModule(
630630
`file:///a/test01.tsx`,
631-
`/* @jsxImportSource http://example.com/preact */
631+
new TextEncoder().encode(`/* @jsxImportSource http://example.com/preact */
632632
export function A() {
633633
<div>Hello Deno</div>
634-
}`,
634+
}`),
635635
{
636636
jsxImportSourceModule: "jsx-dev-runtime",
637637
},
@@ -651,10 +651,10 @@ Deno.test({
651651
await init();
652652
const module = parseModule(
653653
`file:///a/test01.tsx`,
654-
`
654+
new TextEncoder().encode(`
655655
export function A() {
656656
<div>Hello Deno</div>
657-
}`,
657+
}`),
658658
{
659659
defaultJsxImportSource: "http://example.com/preact",
660660
},
@@ -673,7 +673,10 @@ Deno.test({
673673
await init();
674674
assertThrows(
675675
() => {
676-
parseModule("./bad.ts", `console.log("hello");`);
676+
parseModule(
677+
"./bad.ts",
678+
new TextEncoder().encode(`console.log("hello");`),
679+
);
677680
},
678681
Error,
679682
"relative URL without a base",
@@ -687,7 +690,10 @@ Deno.test({
687690
await init();
688691
assertThrows(
689692
() => {
690-
parseModule("file:///a/test.md", `# Some Markdown\n\n**bold**`);
693+
parseModule(
694+
"file:///a/test.md",
695+
new TextEncoder().encode(`# Some Markdown\n\n**bold**`),
696+
);
691697
},
692698
Error,
693699
"The module's source code could not be parsed",
@@ -701,10 +707,10 @@ Deno.test({
701707
await init();
702708
const module = parseModule(
703709
"file:///a/test01.js",
704-
`
710+
new TextEncoder().encode(`
705711
import a from "./a.json" with { type: "json" };
706712
await import("./b.json", { with: { type: "json" } });
707-
`,
713+
`),
708714
);
709715
assertEquals(module, {
710716
"dependencies": [
@@ -758,10 +764,10 @@ Deno.test({
758764
await init();
759765
const module = parseModule(
760766
"file:///a/foo.ts",
761-
`
767+
new TextEncoder().encode(`
762768
/// <reference path="./a.d.ts" />
763769
/// <reference types="./b.d.ts" />
764-
`,
770+
`),
765771
);
766772
assertEquals(module, {
767773
"dependencies": [

js/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface LoadResponseModule {
3434
* have been normalized to be lower case values. */
3535
headers?: Record<string, string>;
3636
/** The string value of the loaded resources. */
37-
content: string;
37+
content: string | Uint8Array;
3838
}
3939

4040
export interface LoadResponseExternal {

lib/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ crate-type = ["cdylib"]
1515

1616
[dependencies]
1717
anyhow = "1.0.43"
18+
console_error_panic_hook = "0.1.7"
1819
deno_graph = { path = "../" }
1920
futures = "0.3.17"
2021
js-sys = "0.3.63"

lib/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ pub async fn js_create_graph(
197197
maybe_graph_kind: Option<String>,
198198
maybe_imports: JsValue,
199199
) -> Result<JsValue, JsValue> {
200+
console_error_panic_hook::set_once();
200201
let roots_vec: Vec<String> = serde_wasm_bindgen::from_value(roots)
201202
.map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))?;
202203
let maybe_imports_map: Option<HashMap<String, Vec<String>>> =
@@ -274,10 +275,11 @@ pub fn js_parse_module(
274275
maybe_headers: JsValue,
275276
maybe_default_jsx_import_source: Option<String>,
276277
maybe_jsx_import_source_module: Option<String>,
277-
content: String,
278+
content: Vec<u8>,
278279
maybe_resolve: Option<js_sys::Function>,
279280
maybe_resolve_types: Option<js_sys::Function>,
280281
) -> Result<JsValue, JsValue> {
282+
console_error_panic_hook::set_once();
281283
let maybe_headers: Option<HashMap<String, String>> =
282284
serde_wasm_bindgen::from_value(maybe_headers)
283285
.map_err(|err| js_sys::Error::new(&err.to_string()))?;

src/fast_check/range_finder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ impl<'a> PublicRangeFinder<'a> {
10251025
return true; // just analyze it
10261026
};
10271027
match module {
1028-
crate::Module::Esm(m) => is_typed_media_type(m.media_type),
1028+
crate::Module::Js(m) => is_typed_media_type(m.media_type),
10291029
crate::Module::Json(_) => true,
10301030
crate::Module::Npm(_)
10311031
| crate::Module::Node(_)

0 commit comments

Comments
 (0)