From e5383cd3576f858bf65f6d460de397c4f2dae208 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 15 Apr 2022 17:01:33 -0400 Subject: [PATCH] Handle static file serving in Deno adapter's start command (#3121) * Handle static file serving in Deno adapter's start command * Adds a changeset * Ignore a .ts imort --- .changeset/brave-planes-punch.md | 5 ++++ packages/integrations/deno/src/index.ts | 2 +- packages/integrations/deno/src/server.ts | 23 +++++++++++---- .../integrations/deno/test/basics.test.js | 28 +++++++++++++++++-- packages/integrations/deno/test/deps.js | 1 + .../fixtures/basics/src/pages/index.astro | 1 + packages/integrations/deno/test/helpers.js | 3 ++ 7 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 .changeset/brave-planes-punch.md diff --git a/.changeset/brave-planes-punch.md b/.changeset/brave-planes-punch.md new file mode 100644 index 000000000..2c733ea8a --- /dev/null +++ b/.changeset/brave-planes-punch.md @@ -0,0 +1,5 @@ +--- +'@astrojs/deno': patch +--- + +Handles file serving in the main export diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index ad5462112..d1b3b14ba 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -10,7 +10,7 @@ export function getAdapter(args?: Options): AstroAdapter { name: '@astrojs/deno', serverEntrypoint: '@astrojs/deno/server.js', args: args ?? {}, - exports: ['stop', 'handle'], + exports: ['stop', 'handle', 'start', 'running'], }; } diff --git a/packages/integrations/deno/src/server.ts b/packages/integrations/deno/src/server.ts index e1659a843..1194cda23 100644 --- a/packages/integrations/deno/src/server.ts +++ b/packages/integrations/deno/src/server.ts @@ -3,6 +3,8 @@ import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; // @ts-ignore import { Server } from 'https://deno.land/std@0.132.0/http/server.ts'; +// @ts-ignore +import { fetch } from 'https://deno.land/x/file_fetch/mod.ts'; interface Options { port?: number; @@ -18,32 +20,43 @@ export function start(manifest: SSRManifest, options: Options) { return; } + const clientRoot = new URL('../client/', import.meta.url); const app = new App(manifest); - const handler = async (request: Request) => { - const response = await app.render(request); - return response; + if(app.match(request)) { + return await app.render(request); + } + + const url = new URL(request.url); + const localPath = new URL('.' + url.pathname, clientRoot); + return fetch(localPath.toString()); }; _server = new Server({ port: options.port ?? 8085, hostname: options.hostname ?? '0.0.0.0', handler, - //onError: options.onError, }); _startPromise = _server.listenAndServe(); } -export function createExports(manifest: SSRManifest) { +export function createExports(manifest: SSRManifest, options: Options) { const app = new App(manifest); return { async stop() { if (_server) { _server.close(); + _server = undefined; } await Promise.resolve(_startPromise); }, + running() { + return _server !== undefined; + }, + async start() { + return start(manifest, options); + }, async handle(request: Request) { return app.render(request); }, diff --git a/packages/integrations/deno/test/basics.test.js b/packages/integrations/deno/test/basics.test.js index 6f152d4f4..6efb3b0d5 100644 --- a/packages/integrations/deno/test/basics.test.js +++ b/packages/integrations/deno/test/basics.test.js @@ -1,10 +1,14 @@ import { runBuildAndStartApp } from './helpers.js'; -import { assertEquals, assert } from './deps.js'; +import { assertEquals, assert, DOMParser } from './deps.js'; + +async function startApp(cb) { + await runBuildAndStartApp('./fixtures/basics/', cb); +} Deno.test({ name: 'Basics', async fn() { - await runBuildAndStartApp('./fixtures/basics/', async () => { + await startApp(async () => { const resp = await fetch('http://127.0.0.1:8085/'); assertEquals(resp.status, 200); const html = await resp.text(); @@ -12,3 +16,23 @@ Deno.test({ }); }, }); + +Deno.test({ + name: 'Loads style assets', + async fn() { + await startApp(async () => { + let resp = await fetch('http://127.0.0.1:8085/'); + const html = await resp.text(); + + const doc = new DOMParser().parseFromString(html, `text/html`); + const link = doc.querySelector('link'); + const href = link.getAttribute('href'); + + resp = await fetch(new URL(href, 'http://127.0.0.1:8085/')); + assertEquals(resp.status, 200); + const ct = resp.headers.get('content-type'); + assertEquals(ct, 'text/css'); + await resp.body.cancel() + }); + } +}) diff --git a/packages/integrations/deno/test/deps.js b/packages/integrations/deno/test/deps.js index af5f89c13..80410a338 100644 --- a/packages/integrations/deno/test/deps.js +++ b/packages/integrations/deno/test/deps.js @@ -1,2 +1,3 @@ export * from 'https://deno.land/std@0.110.0/path/mod.ts'; export * from 'https://deno.land/std@0.132.0/testing/asserts.ts'; +export * from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts'; diff --git a/packages/integrations/deno/test/fixtures/basics/src/pages/index.astro b/packages/integrations/deno/test/fixtures/basics/src/pages/index.astro index d8bc06a72..9a37d684b 100644 --- a/packages/integrations/deno/test/fixtures/basics/src/pages/index.astro +++ b/packages/integrations/deno/test/fixtures/basics/src/pages/index.astro @@ -4,6 +4,7 @@ Basic App on Deno +

Basic App on Deno

diff --git a/packages/integrations/deno/test/helpers.js b/packages/integrations/deno/test/helpers.js index bc2bc0622..210877f1d 100644 --- a/packages/integrations/deno/test/helpers.js +++ b/packages/integrations/deno/test/helpers.js @@ -14,6 +14,9 @@ export async function runBuildAndStartApp(fixturePath, cb) { const url = new URL(fixturePath, dir); const close = await runBuild(fixturePath); const mod = await import(new URL('./dist/server/entry.mjs', url)); + if(!mod.running()) { + mod.start(); + } await cb(); await mod.stop(); await close();