Skip to content

Commit

Permalink
feat: load bytes instead of strings (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 31, 2024
1 parent 64e1c27 commit 19e2b28
Show file tree
Hide file tree
Showing 15 changed files with 564 additions and 125 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async-trait = "0.1.68"
data-url = "0.3.0"
deno_ast = { version = "0.32.0", features = ["dep_analysis", "module_specifier"] }
deno_semver = "0.5.4"
encoding_rs = "0.8.33"
futures = "0.3.26"
import_map = "0.18.0"
indexmap = { version = "2", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tasks": {
"build": "cp LICENSE js/LICENSE && deno run --unstable -A --no-check https://deno.land/x/wasmbuild@0.15.1/main.ts --no-default-features --project deno_graph_wasm --out js",
"build": "cp LICENSE js/LICENSE && deno run -A --no-check https://deno.land/x/wasmbuild@0.15.1/main.ts --no-default-features --project deno_graph_wasm --out js",
"build:npm": "deno run -A _build_npm.ts",
"test": "deno test --allow-read --allow-net"
},
Expand Down
21 changes: 19 additions & 2 deletions js/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export type {
TypesDependency,
} from "./types.ts";

const encoder = new TextEncoder();

// note: keep this in line with deno_cache
export type CacheSetting = "only" | "use" | "reload";

Expand Down Expand Up @@ -139,7 +141,22 @@ export async function createGraph(
const { createGraph } = await wasm.instantiate();
return await createGraph(
rootSpecifiers,
load,
async (
specifier: string,
isDynamic: boolean,
cacheSetting: CacheSetting,
) => {
const result = await load(specifier, isDynamic, cacheSetting);
if (result?.kind === "module") {
if (typeof result.content === "string") {
result.content = encoder.encode(result.content);
}
// need to convert to an array for serde_wasm_bindgen to work
// deno-lint-ignore no-explicit-any
(result as any).content = Array.from(result.content);
}
return result;
},
defaultJsxImportSource,
jsxImportSourceModule,
cacheInfo,
Expand Down Expand Up @@ -192,7 +209,7 @@ export async function init(opts?: wasm.InstantiateOptions) {
*/
export function parseModule(
specifier: string,
content: string,
content: Uint8Array,
options: ParseModuleOptions = {},
): ModuleJson {
const {
Expand Down
34 changes: 20 additions & 14 deletions js/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,13 @@ Deno.test({
await init();
const module = parseModule(
"file:///a/test01.js",
`
new TextEncoder().encode(`
/// <reference types="./test01.d.ts" />
import { a } from "./a.ts";
import * as b from "./b.ts";
export { c } from "./c.ts";
const d = await import("./d.ts");
`,
`),
);
assertEquals(module, {
"specifier": "file:///a/test01.js",
Expand Down Expand Up @@ -609,9 +609,9 @@ Deno.test({
await init();
const module = parseModule(
`https://example.com/a`,
`declare interface A {
new TextEncoder().encode(`declare interface A {
a: string;
}`,
}`),
{
headers: {
"content-type": "application/typescript; charset=utf-8",
Expand All @@ -628,10 +628,10 @@ Deno.test({
await init();
const module = parseModule(
`file:///a/test01.tsx`,
`/* @jsxImportSource http://example.com/preact */
new TextEncoder().encode(`/* @jsxImportSource http://example.com/preact */
export function A() {
<div>Hello Deno</div>
}`,
}`),
{
jsxImportSourceModule: "jsx-dev-runtime",
},
Expand All @@ -651,10 +651,10 @@ Deno.test({
await init();
const module = parseModule(
`file:///a/test01.tsx`,
`
new TextEncoder().encode(`
export function A() {
<div>Hello Deno</div>
}`,
}`),
{
defaultJsxImportSource: "http://example.com/preact",
},
Expand All @@ -673,7 +673,10 @@ Deno.test({
await init();
assertThrows(
() => {
parseModule("./bad.ts", `console.log("hello");`);
parseModule(
"./bad.ts",
new TextEncoder().encode(`console.log("hello");`),
);
},
Error,
"relative URL without a base",
Expand All @@ -687,7 +690,10 @@ Deno.test({
await init();
assertThrows(
() => {
parseModule("file:///a/test.md", `# Some Markdown\n\n**bold**`);
parseModule(
"file:///a/test.md",
new TextEncoder().encode(`# Some Markdown\n\n**bold**`),
);
},
Error,
"The module's source code could not be parsed",
Expand All @@ -701,10 +707,10 @@ Deno.test({
await init();
const module = parseModule(
"file:///a/test01.js",
`
new TextEncoder().encode(`
import a from "./a.json" with { type: "json" };
await import("./b.json", { with: { type: "json" } });
`,
`),
);
assertEquals(module, {
"dependencies": [
Expand Down Expand Up @@ -758,10 +764,10 @@ Deno.test({
await init();
const module = parseModule(
"file:///a/foo.ts",
`
new TextEncoder().encode(`
/// <reference path="./a.d.ts" />
/// <reference types="./b.d.ts" />
`,
`),
);
assertEquals(module, {
"dependencies": [
Expand Down
2 changes: 1 addition & 1 deletion js/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface LoadResponseModule {
* have been normalized to be lower case values. */
headers?: Record<string, string>;
/** The string value of the loaded resources. */
content: string;
content: string | Uint8Array;
}

export interface LoadResponseExternal {
Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0.43"
console_error_panic_hook = "0.1.7"
deno_graph = { path = "../" }
futures = "0.3.17"
js-sys = "0.3.63"
Expand Down
4 changes: 3 additions & 1 deletion lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub async fn js_create_graph(
maybe_graph_kind: Option<String>,
maybe_imports: JsValue,
) -> Result<JsValue, JsValue> {
console_error_panic_hook::set_once();
let roots_vec: Vec<String> = serde_wasm_bindgen::from_value(roots)
.map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))?;
let maybe_imports_map: Option<HashMap<String, Vec<String>>> =
Expand Down Expand Up @@ -274,10 +275,11 @@ pub fn js_parse_module(
maybe_headers: JsValue,
maybe_default_jsx_import_source: Option<String>,
maybe_jsx_import_source_module: Option<String>,
content: String,
content: Vec<u8>,
maybe_resolve: Option<js_sys::Function>,
maybe_resolve_types: Option<js_sys::Function>,
) -> Result<JsValue, JsValue> {
console_error_panic_hook::set_once();
let maybe_headers: Option<HashMap<String, String>> =
serde_wasm_bindgen::from_value(maybe_headers)
.map_err(|err| js_sys::Error::new(&err.to_string()))?;
Expand Down
2 changes: 1 addition & 1 deletion src/fast_check/range_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ impl<'a> PublicRangeFinder<'a> {
return true; // just analyze it
};
match module {
crate::Module::Esm(m) => is_typed_media_type(m.media_type),
crate::Module::Js(m) => is_typed_media_type(m.media_type),
crate::Module::Json(_) => true,
crate::Module::Npm(_)
| crate::Module::Node(_)
Expand Down
Loading

0 comments on commit 19e2b28

Please sign in to comment.