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

Json modules coming to Supabase edge runtime #74

Merged
merged 1 commit into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions crates/base/src/js_worker/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,30 @@ impl ModuleLoader for DefaultModuleLoader {
let module_type = get_module_type(fetched_file.media_type)?;

let code = fetched_file.source;
let code = emit_parsed_source(
&emit_cache,
&parsed_source_cache,
&module_specifier,
fetched_file.media_type,
&code,
&emit_options,
emit_config_hash,
)?;
let code = match fetched_file.media_type {
MediaType::JavaScript
| MediaType::Unknown
| MediaType::Cjs
| MediaType::Mjs
| MediaType::Json => code.into(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also remove the need to always emit_parsed_source. But abstract it from doing so in files that aren't meant to be processed/emitted

MediaType::Dts | MediaType::Dcts | MediaType::Dmts => Default::default(),
MediaType::TypeScript
| MediaType::Mts
| MediaType::Cts
| MediaType::Jsx
| MediaType::Tsx => emit_parsed_source(
&emit_cache,
&parsed_source_cache,
&module_specifier,
fetched_file.media_type,
&code,
&emit_options,
emit_config_hash,
)?,
MediaType::TsBuildInfo | MediaType::Wasm | MediaType::SourceMap => {
panic!("Unexpected media type during import.")
}
};

let module = ModuleSource {
code,
Expand Down
9 changes: 9 additions & 0 deletions crates/base/test_cases/json_import/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import file from "./version.json" assert { type: "json" };
import { serve } from "https://deno.land/[email protected]/http/server.ts"

serve(async (req: Request) => {
return new Response(
JSON.stringify({ version: file.version }),
{ status: 200, headers: { "Content-Type": "application/json" } },
)
})
3 changes: 3 additions & 0 deletions crates/base/test_cases/json_import/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "1.0.0"
}
30 changes: 30 additions & 0 deletions crates/base/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,33 @@ async fn test_file_upload() {

assert_eq!(body_bytes, "file-type: text/plain");
}

#[tokio::test]
async fn test_json_imports() {
let user_rt_opts = EdgeUserRuntimeOpts::default();
let opts = EdgeContextInitOpts {
service_path: "./test_cases/json_import".into(),
no_module_cache: false,
import_map_path: None,
env_vars: HashMap::new(),
conf: EdgeContextOpts::UserWorker(user_rt_opts),
};
let worker_req_tx = create_worker(opts).await.unwrap();
let (res_tx, res_rx) = oneshot::channel::<Result<Response<Body>, hyper::Error>>();

let req = Request::builder()
.uri("/")
.method("GET")
.body(Body::empty())
.unwrap();

let msg = WorkerRequestMsg { req, res_tx };
let _ = worker_req_tx.send(msg);

let res = res_rx.await.unwrap().unwrap();
assert!(res.status().as_u16() == 200);

let body_bytes = hyper::body::to_bytes(res.into_body()).await.unwrap();

assert_eq!(body_bytes, r#"{"version":"1.0.0"}"#);
}