From 9d1fa5c050bf203f9817d990a5c109fd0bb1729c Mon Sep 17 00:00:00 2001
From: Arsh <69170106+lilnasy@users.noreply.github.com>
Date: Mon, 23 Dec 2024 16:14:36 +0000
Subject: [PATCH 1/2] add unix socket option
---
package/src/index.ts | 8 ++--
package/src/server/index.ts | 16 ++++---
package/src/types.ts | 87 +++++++++++++++++++++++++++++--------
package/src/validators.ts | 6 +--
4 files changed, 84 insertions(+), 33 deletions(-)
diff --git a/package/src/index.ts b/package/src/index.ts
index 7def9a0..23cca75 100644
--- a/package/src/index.ts
+++ b/package/src/index.ts
@@ -6,9 +6,9 @@ import { OptionsSchema } from '~/validators';
import type { AstroAdapter, AstroIntegration } from 'astro';
-import type { Options } from '~/types.ts';
+import type { Options, InternalOptions } from '~/types.ts';
-export function getAdapter(args: Options = {}): AstroAdapter {
+export function getAdapter(args: InternalOptions): AstroAdapter {
return {
args,
exports: [
@@ -49,10 +49,10 @@ export default function integration(options?: Options): AstroIntegration {
getAdapter({
...parsedOptions.data,
assets: params.config.build.assets,
- client: params.config.build.client?.toString(),
+ client: params.config.build.client.toString(),
host: params.config.server.host,
port: params.config.server.port,
- server: params.config.build.server?.toString(),
+ server: params.config.build.server.toString(),
}),
);
},
diff --git a/package/src/server/index.ts b/package/src/server/index.ts
index 642a2dd..d11327b 100644
--- a/package/src/server/index.ts
+++ b/package/src/server/index.ts
@@ -10,9 +10,12 @@ import { extractHostname, serveStaticFile } from '~/server/utils';
import type { SSRManifest } from 'astro';
import type { Server } from 'bun';
-import type { CreateExports, Options } from '~/types';
+import type { CreateExports, InternalOptions } from '~/types';
-export function createExports(manifest: SSRManifest, options: Options): CreateExports {
+export function createExports(
+ manifest: SSRManifest,
+ options: InternalOptions,
+): CreateExports {
return {
handle: handler(manifest, options),
running: (): boolean => _server !== null,
@@ -26,11 +29,12 @@ export function createExports(manifest: SSRManifest, options: Options): CreateEx
}
let _server: Server | null = null;
-export function start(manifest: SSRManifest, options: Options): void {
+export function start(manifest: SSRManifest, options: InternalOptions): void {
const { env } = process;
const hostname = env.HOST ?? extractHostname(options.host);
const port = env.PORT ? Number.parseInt(env.PORT) : options.port;
+ const unix = env.UNIX ?? options.unix;
if (cluster.isPrimary && options.cluster) {
const numCPUs = os.cpus().length;
@@ -47,14 +51,12 @@ export function start(manifest: SSRManifest, options: Options): void {
const logger = app.getAdapterLogger();
_server = Bun.serve({
- development: import.meta.env.DEV,
error: (error): Response =>
new Response(`
${error}\n${error.stack}`, {
headers: { 'Content-Type': 'text/html' },
}),
fetch: handler(manifest, options),
- hostname,
- port,
+ ...(unix ? { unix } : { hostname, port }),
});
function exit(): void {
@@ -72,7 +74,7 @@ export function start(manifest: SSRManifest, options: Options): void {
function handler(
manifest: SSRManifest,
- options: Options,
+ options: InternalOptions,
): (req: Request, server: Server) => Promise {
const clientRoot = options.client ?? new URL('../client/', import.meta.url).href;
diff --git a/package/src/types.ts b/package/src/types.ts
index 50f0e19..abd1e53 100644
--- a/package/src/types.ts
+++ b/package/src/types.ts
@@ -1,7 +1,43 @@
import type { Server } from 'bun';
-import type { z } from 'zod';
-import type { OptionsSchema } from '~/validators';
+import type { AstroConfig } from 'astro';
+
+// the options available to the user when they are adding
+// the adapter to their configuration
+export interface Options {
+ /**
+ * Create a cluster of bun servers listening on the same port,
+ * and automatically load-balance incoming requests across them.
+ *
+ * Example:
+ * ```ts
+ * export default defineConfig({
+ * adapter: bun({ cluster: true })
+ * })
+ * ```
+ *
+ * Defaults to `false`
+ */
+ cluster?: boolean;
+ /**
+ * The path to the unix socket on which to host the server.
+ *
+ * This can provide better performance when Bun is running alongside
+ * a local reverse proxy that supports unix sockets.
+ *
+ * When a unix socket is provided, Bun does not bind to a TCP port,
+ * and the options and environment variables for the hostname and port
+ * are ignored.
+ *
+ * Example:
+ * ```ts
+ * export default defineConfig({
+ * adapter: bun({ unix: "/tmp/my-socket.sock" })
+ * })
+ * ```
+ */
+ unix?: string;
+}
export const CreateExports = {
HANDLE: 'handle',
@@ -19,27 +55,44 @@ export type CreateExports = {
// export type Options = z.infer;
-export interface Options {
- /** TODO(@nurodev): Undocumented */
- assets?: z.infer['assets'];
- /** TODO(@nurodev): Undocumented */
- client?: z.infer['client'];
+// options provided by the user combined with other
+// relevant configuration picked from the integration API
+export interface InternalOptions extends Options {
+ /**
+ * Name of the publicly exposed directory where all
+ * static assets are put.
+ *
+ * Astro defaults to `"_astro"`.
+ */
+ assets: AstroConfig['build']['assets'];
/**
- * Enable clustering for the server. (Only linux!)
+ * The full file URL to where astro is configured to put
+ * the client bundle and assets such as images, fonts,
+ * stylesheets, and static html.
*
- * @default false
+ * Astro defaults to `"/dist/client/"`.
*/
- cluster?: z.infer['cluster'];
+ client: AstroConfig['build']['server']['href'];
/**
- * The hostname to serve the application on.
+ * The full file URL to where astro is configured to put
+ * the server bundle.
+ *
+ * Astro defaults to `"/dist/server/""`.
+ */
+ server: AstroConfig['build']['server']['href'];
+ /**
+ * Network address where the astro dev server is
+ * configured to listen for requests in addition to
+ * `localhost`.
+ *
+ * Astro defaults to `false`.
*/
- host?: z.infer['host'];
+ host: AstroConfig['server']['host'];
/**
- * The port to serve the application on.
+ * Network port where the astro dev server is
+ * configured to listen for requests.
*
- * @default 4321
+ * Astro default to `4321`.
*/
- port?: z.infer['port'];
- /** TODO(@nurodev): Undocumented */
- server?: z.infer['server'];
+ port: AstroConfig['server']['port'];
}
diff --git a/package/src/validators.ts b/package/src/validators.ts
index e9f4513..b8cbe48 100644
--- a/package/src/validators.ts
+++ b/package/src/validators.ts
@@ -2,11 +2,7 @@ import { z } from 'zod';
export const OptionsSchema = z
.object({
- assets: z.string(),
- client: z.string(),
cluster: z.boolean().optional().default(false),
- host: z.union([z.string(), z.boolean()]),
- port: z.coerce.number().default(4321),
- server: z.string(),
+ unix: z.string().optional(),
})
.partial();
From 200072a0d2067e5ed054389a8b029e5a507be876 Mon Sep 17 00:00:00 2001
From: Arsh <69170106+lilnasy@users.noreply.github.com>
Date: Tue, 24 Dec 2024 13:03:23 +0000
Subject: [PATCH 2/2] defer to OptionsSchema for the type of user-provided
options
---
package/src/types.ts | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/package/src/types.ts b/package/src/types.ts
index abd1e53..66d73a4 100644
--- a/package/src/types.ts
+++ b/package/src/types.ts
@@ -1,7 +1,9 @@
import type { Server } from 'bun';
-
+import type { z } from 'zod';
import type { AstroConfig } from 'astro';
+import type { OptionsSchema } from '~/validators';
+
// the options available to the user when they are adding
// the adapter to their configuration
export interface Options {
@@ -18,7 +20,7 @@ export interface Options {
*
* Defaults to `false`
*/
- cluster?: boolean;
+ cluster?: z.infer["cluster"];
/**
* The path to the unix socket on which to host the server.
*
@@ -36,7 +38,7 @@ export interface Options {
* })
* ```
*/
- unix?: string;
+ unix?: z.infer["unix"];
}
export const CreateExports = {