Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: load bytes instead of strings #357

Merged
merged 15 commits into from
Jan 31, 2024
10 changes: 10 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
2 changes: 1 addition & 1 deletion js/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,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
2 changes: 1 addition & 1 deletion lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ 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> {
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
Loading