Skip to content

Commit

Permalink
feat: correctly handle import statement with JSR specifier (#336)
Browse files Browse the repository at this point in the history
* fix: we should handle jsr specifier now since deno HQ seems to have decided to use it on prod

* stamp: add an integration test for import statement with jsr specifier

* stamp: add an example for import statement with jsr specifier

* stamp(base): unpin oak version in `test_cases`

* stamp: update oak related integration tests
  • Loading branch information
nyannyacha committed May 6, 2024
1 parent 1bf0900 commit 0648aa0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 38 deletions.
35 changes: 35 additions & 0 deletions crates/base/test_cases/oak-v12-file-upload/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Application, Router } from 'https://deno.land/x/oak@v12.3.0/mod.ts';

const MB = 1024 * 1024;

const router = new Router();

router
.post('/file-upload', async (ctx) => {
try {
const body = ctx.request.body({ type: 'form-data' });
const formData = await body.value.read({
// Need to set the maxSize so files will be stored in memory.
// This is necessary as Edge Functions don't have disk write access.
// We are setting the max size as 10MB (an Edge Function has a max memory limit of 150MB)
// For more config options, check: https://deno.land/x/oak@v11.1.0/mod.ts?s=FormDataReadOptions
maxSize: 1 * MB,
});
const file = formData.files[0];

ctx.response.status = 201;
ctx.response.body = `file-type: ${file.contentType}`;
} catch (e) {
console.log('error occurred');
console.log(e);
ctx.response.status = 500;
ctx.response.body = 'Error!';
}
});

const app = new Application();

app.use(router.routes());
app.use(router.allowedMethods());

await app.listen();
17 changes: 17 additions & 0 deletions crates/base/test_cases/oak-with-jsr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Application } from "jsr:@oak/oak/application";
import { Router } from "jsr:@oak/oak/router";

const router = new Router();

router
// Note: path will be prefixed with function name
.get("/oak-with-jsr", (context) => {
context.response.body = "meow";
});

const app = new Application();

app.use(router.routes());
app.use(router.allowedMethods());

await app.listen();
37 changes: 4 additions & 33 deletions crates/base/test_cases/oak/index.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,19 @@
import { Application, Router } from 'https://deno.land/x/oak@v12.3.0/mod.ts';

const MB = 1024 * 1024;
import { Application, Router } from 'https://deno.land/x/oak/mod.ts';

const router = new Router();

router
// Note: path will be prefixed with function name
.get('/oak', (context) => {
context.response.body = 'This is an example Oak server running on Edge Functions!';
})
.post('/oak/greet', async (context) => {
// Note: request body will be streamed to the function as chunks, set limit to 0 to fully read it.
const result = context.request.body({ type: 'json', limit: 0 });
const body = await result.value;
const name = body.name || 'you';

context.response.body = { msg: `Hey ${name}!` };
})
.get('/oak/redirect', (context) => {
context.response.redirect('https://www.example.com');
})
.post('/file-upload', async (ctx) => {
try {
const body = ctx.request.body({ type: 'form-data' });
const formData = await body.value.read({
// Need to set the maxSize so files will be stored in memory.
// This is necessary as Edge Functions don't have disk write access.
// We are setting the max size as 10MB (an Edge Function has a max memory limit of 150MB)
// For more config options, check: https://deno.land/x/oak@v11.1.0/mod.ts?s=FormDataReadOptions
maxSize: 1 * MB,
});
const file = formData.files[0];

ctx.response.status = 201;
ctx.response.body = `file-type: ${file.contentType}`;
} catch (e) {
console.log('error occurred');
console.log(e);
ctx.response.status = 500;
ctx.response.body = 'Error!';
}
});

const app = new Application();

app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });
await app.listen();
20 changes: 19 additions & 1 deletion crates/base/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ async fn test_file_upload() {
let request_builder = Some(original);

integration_test!(
"./test_cases/oak",
"./test_cases/oak-v12-file-upload",
NON_SECURE_PORT,
"",
None,
Expand Down Expand Up @@ -1279,6 +1279,24 @@ async fn send_partial_payload_into_closed_pipe_should_not_be_affected_worker_sta
tb.exit(Duration::from_secs(TESTBED_DEADLINE_SEC)).await;
}

#[tokio::test]
#[serial]
async fn oak_with_jsr_specifier() {
integration_test!(
"./test_cases/main",
NON_SECURE_PORT,
"oak-with-jsr",
None,
None,
None,
None,
(|resp| async {
assert_eq!(resp.unwrap().text().await.unwrap(), "meow");
}),
TerminationToken::new()
);
}

trait TlsExt {
fn client(&self) -> reqwest::Client;
fn schema(&self) -> &'static str;
Expand Down
15 changes: 11 additions & 4 deletions crates/sb_module_loader/standalone/standalone_module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
.as_ref()
.map(|r| r.as_str())
.unwrap_or(specifier);

if let Ok(reference) = NpmPackageReqReference::from_str(specifier_text) {
return self.shared.node_resolver.resolve_req_reference(
&reference,
Expand All @@ -79,12 +80,18 @@ impl ModuleLoader for EmbeddedModuleLoader {
);
}

match maybe_mapped {
Some(resolved) => Ok(resolved),
None => {
deno_core::resolve_import(specifier, referrer.as_str()).map_err(|err| err.into())
let specifier = match maybe_mapped {
Some(resolved) => resolved,
None => deno_core::resolve_import(specifier, referrer.as_str())?,
};

if specifier.scheme() == "jsr" {
if let Some(module) = self.shared.eszip.get_module(specifier.as_str()) {
return Ok(ModuleSpecifier::parse(&module.specifier).unwrap());
}
}

Ok(specifier)
}

fn load(
Expand Down
17 changes: 17 additions & 0 deletions examples/oak-with-jsr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Application } from "jsr:@oak/oak/application";
import { Router } from "jsr:@oak/oak/router";

const router = new Router();

router
// Note: path will be prefixed with function name
.get("/oak-with-jsr", (context) => {
context.response.body = "This is an example Oak server running on Edge Functions!";
});

const app = new Application();

app.use(router.routes());
app.use(router.allowedMethods());

await app.listen();

0 comments on commit 0648aa0

Please sign in to comment.