-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(deno): use deno serve * feat: add serve and handler Deno apis * feat: add idleTimeout support to deno * docs: add idleTimeout docs for deno * chore: fix package.json to allow brisa/server/deno * test: adapt test to deno * test: setup deno tests * chore: change workflow * chore: update package.json scripts * chore: move deno.json * chore: fix pipeline * chore: fix space * chore: fix iden * chore: fix * chore: add bun * chore: fix deno.json * chore: add @types/node * chore: build * chore: move deno.json to root * chore: remove dep * test: fix fixtures * test: fix imports * test: fix test
- Loading branch information
Showing
17 changed files
with
463 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tasks": { | ||
"test:deno": "deno test --allow-all \"**/*.deno-test.js\"" | ||
}, | ||
"nodeModulesDir": "auto" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
description: The handler function is the user Brisa handler in Deno. You can use it to create a custom server. | ||
--- | ||
|
||
# `handler` | ||
|
||
## Reference | ||
|
||
### `handler(req: Request, info: ServeHandlerInfo<Addr>): Response | Promise<Response>` | ||
|
||
The `handler` function is the user Brisa handler to handle the incoming requests. You can use it to create a custom server. | ||
|
||
## Example usage: | ||
|
||
In the next example, we use the `handler` function to use it with `Deno.serve`: | ||
|
||
```tsx 5 | ||
import { handler } from "brisa/server/deno"; | ||
|
||
async function customServer(req, info) { | ||
// Your implementation here ... | ||
await handler(req, info); | ||
} | ||
|
||
const server = Deno.listen({ port: 3000, handler: customServer }); | ||
``` | ||
|
||
## Types | ||
|
||
```tsx | ||
export function handler( | ||
req: Request, | ||
info: ServeHandlerInfo<Addr>, | ||
): Response | Promise<Response>; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
description: The serve function is used to start the Deno server and listen for incoming requests. | ||
--- | ||
|
||
# `serve` | ||
|
||
## Reference | ||
|
||
### `serve(options: ServeOptions): { port: number; hostname: string; server: ReturnType<typeof Deno.serve>; }` | ||
|
||
The `serve` function is used to start the Deno server and listen for incoming requests. | ||
|
||
## Example usage: | ||
|
||
In the next example, we use the `serve` function to start the Deno server. | ||
|
||
```tsx 3-5 | ||
import { serve } from "brisa/server/deno"; | ||
|
||
const { server, port, hostname } = serve({ | ||
port: 3001, | ||
}); | ||
|
||
console.log( | ||
"Deno Server ready 🥳", | ||
`listening on http://${hostname}:${port}...`, | ||
); | ||
``` | ||
|
||
> [!IMPORTANT] | ||
> | ||
> Keep in mind that the `serve` for Deno is not in `brisa/server` but in `brisa/server/deno`. | ||
> [!CAUTION] | ||
> | ||
> It only makes sense to use it if you need a [custom server](/building-your-application/configuring/custom-server) for extra things from the serve but if you start the server in the same way as Brisa. | ||
## Types | ||
|
||
```tsx | ||
export function serve(options: ServeOptions): { | ||
port: number; | ||
hostname: string; | ||
server: ReturnType<typeof Deno.serve>; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
packages/brisa/src/cli/serve/deno-serve/handler.deno-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { assertEquals, assert } from 'jsr:@std/assert'; | ||
import { resolve } from 'node:path'; | ||
|
||
const FIXTURES_DIR = resolve( | ||
import.meta.dirname, | ||
'..', | ||
'..', | ||
'..', | ||
'__fixtures__', | ||
); | ||
|
||
const absolutePath = new URL('../../../../server/deno.js', import.meta.url) | ||
.href; | ||
|
||
Deno.test('should resolve a page', async () => { | ||
globalThis.mockConstants = { | ||
IS_SERVE_PROCESS: true, | ||
ROOT_DIR: FIXTURES_DIR, | ||
SRC_DIR: resolve(FIXTURES_DIR, 'js'), | ||
BUILD_DIR: resolve(FIXTURES_DIR, 'js'), | ||
ASSETS_DIR: resolve(FIXTURES_DIR, 'public'), | ||
PAGES_DIR: resolve(FIXTURES_DIR, 'js', 'pages'), | ||
CONFIG: {}, | ||
HEADERS: { | ||
CACHE_CONTROL: 'no-cache, no-store, must-revalidate', | ||
}, | ||
LOG_PREFIX: {}, | ||
}; | ||
|
||
const req = new Request('http://localhost/'); | ||
const { handler } = await import(absolutePath); | ||
|
||
const response = await handler(req); | ||
assertEquals(response.status, 200); | ||
assert((await response.text()).includes('<title>Brisa</title><')); | ||
assertEquals( | ||
response.headers.get('cache-control'), | ||
'no-cache, no-store, must-revalidate', | ||
); | ||
}); | ||
|
||
Deno.test('should redirect to the locale', async () => { | ||
globalThis.mockConstants = { | ||
IS_SERVE_PROCESS: true, | ||
ROOT_DIR: FIXTURES_DIR, | ||
SRC_DIR: resolve(FIXTURES_DIR, 'js'), | ||
BUILD_DIR: resolve(FIXTURES_DIR, 'js'), | ||
ASSETS_DIR: resolve(FIXTURES_DIR, 'public'), | ||
PAGES_DIR: resolve(FIXTURES_DIR, 'js', 'pages'), | ||
I18N_CONFIG: { | ||
locales: ['en', 'es'], | ||
defaultLocale: 'es', | ||
}, | ||
LOCALES_SET: new Set(['en', 'es']), | ||
CONFIG: {}, | ||
HEADERS: { | ||
CACHE_CONTROL: 'no-cache, no-store, must-revalidate', | ||
}, | ||
LOG_PREFIX: {}, | ||
}; | ||
|
||
const req = new Request('http://localhost/somepage'); | ||
const { handler } = await import(absolutePath); | ||
|
||
const response = await handler(req); | ||
assertEquals(response.status, 301); | ||
assertEquals(response.headers.get('location'), '/es/somepage'); | ||
}); | ||
|
||
Deno.test('should redirect to trailingSlash', async () => { | ||
globalThis.mockConstants = { | ||
IS_SERVE_PROCESS: true, | ||
ROOT_DIR: FIXTURES_DIR, | ||
SRC_DIR: resolve(FIXTURES_DIR, 'js'), | ||
BUILD_DIR: resolve(FIXTURES_DIR, 'js'), | ||
ASSETS_DIR: resolve(FIXTURES_DIR, 'public'), | ||
PAGES_DIR: resolve(FIXTURES_DIR, 'js', 'pages'), | ||
CONFIG: { | ||
trailingSlash: true, | ||
}, | ||
HEADERS: { | ||
CACHE_CONTROL: 'no-cache, no-store, must-revalidate', | ||
}, | ||
LOG_PREFIX: {}, | ||
}; | ||
|
||
const req = new Request('http://localhost/somepage'); | ||
const { handler } = await import(absolutePath); | ||
|
||
const response = await handler(req); | ||
assertEquals(response.status, 301); | ||
assertEquals(response.headers.get('location'), 'http://localhost/somepage/'); | ||
}); | ||
|
||
Deno.test('should redirect locale and trailing slash', async () => { | ||
globalThis.mockConstants = { | ||
IS_SERVE_PROCESS: true, | ||
ROOT_DIR: FIXTURES_DIR, | ||
SRC_DIR: resolve(FIXTURES_DIR, 'js'), | ||
BUILD_DIR: resolve(FIXTURES_DIR, 'js'), | ||
ASSETS_DIR: resolve(FIXTURES_DIR, 'public'), | ||
PAGES_DIR: resolve(FIXTURES_DIR, 'js', 'pages'), | ||
I18N_CONFIG: { | ||
locales: ['en', 'es'], | ||
defaultLocale: 'es', | ||
}, | ||
LOCALES_SET: new Set(['en', 'es']), | ||
CONFIG: { | ||
trailingSlash: true, | ||
}, | ||
HEADERS: { | ||
CACHE_CONTROL: 'no-cache, no-store, must-revalidate', | ||
}, | ||
LOG_PREFIX: {}, | ||
}; | ||
|
||
const req = new Request('http://localhost/somepage'); | ||
const { handler } = await import(absolutePath); | ||
|
||
const response = await handler(req); | ||
assertEquals(response.status, 301); | ||
assertEquals(response.headers.get('location'), '/es/somepage/'); | ||
}); | ||
|
||
Deno.test('should return 404 if the asset does not exist', async () => { | ||
globalThis.mockConstants = { | ||
IS_SERVE_PROCESS: true, | ||
ROOT_DIR: FIXTURES_DIR, | ||
SRC_DIR: resolve(FIXTURES_DIR, 'js'), | ||
BUILD_DIR: resolve(FIXTURES_DIR, 'js'), | ||
ASSETS_DIR: resolve(FIXTURES_DIR, 'public'), | ||
PAGES_DIR: resolve(FIXTURES_DIR, 'js', 'pages'), | ||
CONFIG: {}, | ||
HEADERS: { | ||
CACHE_CONTROL: 'no-cache, no-store, must-revalidate', | ||
}, | ||
LOG_PREFIX: {}, | ||
}; | ||
|
||
const req = new Request('http://localhost/not-found.ico'); | ||
const { handler } = await import(absolutePath); | ||
|
||
const response = await handler(req); | ||
assertEquals(response.status, 404); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { ServeOptions, TLSOptions } from 'bun'; | ||
import { getServeOptions } from '../serve-options'; | ||
|
||
const serveOptions = await getServeOptions(); | ||
|
||
export default function serve(options: ServeOptions & { tls?: TLSOptions }) { | ||
// @ts-ignore | ||
const server = Deno.serve({ | ||
port: options.port, | ||
hostname: options.hostname, | ||
cert: options.tls?.cert, | ||
key: options.tls?.key, | ||
responseWriteTimeout: options.idleTimeout, | ||
handler, | ||
}); | ||
|
||
globalThis.brisaServer = server; | ||
|
||
return { port: server.addr.port, hostname: server.addr.hostname, server }; | ||
} | ||
|
||
export async function handler(req: Request, connInfo: any) { | ||
const bunServer = { | ||
upgrade: () => {}, | ||
requestIP: () => connInfo.remoteAddr, | ||
} as any; | ||
|
||
const res = await serveOptions.fetch.call(bunServer, req, bunServer); | ||
|
||
if (!res) { | ||
return new Response('Not Found', { status: 404 }); | ||
} | ||
|
||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import serve, { handler } from '@/cli/serve/deno-serve'; | ||
|
||
export { handler, serve }; |
Oops, something went wrong.