Skip to content

Commit a51c76c

Browse files
authored
add unix socket option
1 parent 74c5c01 commit a51c76c

File tree

4 files changed

+131
-38
lines changed

4 files changed

+131
-38
lines changed

package/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
"type": "module",
2121
"exports": {
2222
".": {
23-
"default": "./dist/index.js",
24-
"types": "./dist/index.d.ts"
23+
"types": "./dist/index.d.ts",
24+
"default": "./dist/index.js"
2525
},
2626
"./server.js": {
27-
"default": "./dist/server.js",
28-
"types": "./dist/server.d.ts"
27+
"types": "./dist/server.d.ts",
28+
"default": "./dist/server.js"
2929
},
3030
"./package.json": "./package.json"
3131
},

package/src/index.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { defineIntegration } from 'astro-integration-kit';
2-
31
import { name as packageName } from '~/package.json';
42
import { CreateExportsEnum } from '~/types.ts';
5-
import { OptionsSchema } from '~/validators';
63

7-
import type { AstroAdapter } from 'astro';
4+
import type { AstroAdapter, AstroIntegration } from 'astro';
85

9-
import type { Options } from '~/types.ts';
6+
import type { Options, InternalOptions } from '~/types.ts';
107

11-
export function getAdapter(args: Options = {}): AstroAdapter {
8+
export function getAdapter(args: InternalOptions): AstroAdapter {
129
return {
1310
args,
1411
exports: [
@@ -30,23 +27,31 @@ export function getAdapter(args: Options = {}): AstroAdapter {
3027
};
3128
}
3229

33-
export default defineIntegration({
34-
name: packageName,
35-
optionsSchema: OptionsSchema.optional(),
36-
setup: (integration) => ({
30+
export default (options?: Options): AstroIntegration => {
31+
if (options) {
32+
if (options.cluster && typeof options.cluster !== "boolean") {
33+
throw new Error(`[${packageName}] options.cluster must be a boolean, but the adapter was provided with ${options.cluster} instead.`)
34+
}
35+
if (options.unix && typeof options.unix !== "string") {
36+
throw new Error(`[${packageName}] options.unix must be a string, but the adapter was provided with ${options.unix} instead.`)
37+
}
38+
}
39+
return {
40+
name: packageName,
3741
hooks: {
38-
'astro:config:done': (params) => {
42+
'astro:config:done': params => {
3943
params.setAdapter(
4044
getAdapter({
41-
...integration.options,
45+
cluster: options?.cluster,
46+
unix: options?.unix,
4247
assets: params.config.build.assets,
43-
client: params.config.build.client?.toString(),
48+
client: params.config.build.client.toString(),
4449
host: params.config.server.host,
4550
port: params.config.server.port,
46-
server: params.config.build.server?.toString(),
51+
server: params.config.build.server.toString(),
4752
}),
4853
);
4954
},
5055
},
51-
}),
52-
});
56+
}
57+
}

package/src/server/index.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { extractHostname, serveStaticFile } from '~/server/utils.ts';
1010
import type { SSRManifest } from 'astro';
1111
import type { Server } from 'bun';
1212

13-
import type { CreateExports, Options } from '~/types.ts';
13+
import type { CreateExports, InternalOptions } from '~/types.ts';
1414

15-
export function createExports(manifest: SSRManifest, options: Options): CreateExports {
15+
export function createExports(manifest: SSRManifest, options: InternalOptions): CreateExports {
1616
return {
1717
handle: handler(manifest, options),
1818
running: () => _server !== null,
@@ -26,9 +26,10 @@ export function createExports(manifest: SSRManifest, options: Options): CreateEx
2626
}
2727

2828
let _server: Server | null = null;
29-
export function start(manifest: SSRManifest, options: Options): void {
29+
export function start(manifest: SSRManifest, options: InternalOptions): void {
3030
const hostname = process.env.HOST ?? extractHostname(options.host);
3131
const port = process.env.PORT ? Number.parseInt(process.env.PORT) : options.port;
32+
const unix = process.env.UNIX ?? options.unix
3233

3334
if (cluster.isPrimary && options.cluster) {
3435
const numCPUs = os.cpus().length;
@@ -43,16 +44,27 @@ export function start(manifest: SSRManifest, options: Options): void {
4344
const app = new App(manifest);
4445
const logger = app.getAdapterLogger();
4546

46-
_server = Bun.serve({
47-
development: import.meta.env.DEV,
48-
error: (error) =>
49-
new Response(`<pre>${error}\n${error.stack}</pre>`, {
50-
headers: { 'Content-Type': 'text/html' },
51-
}),
52-
fetch: handler(manifest, options),
53-
hostname,
54-
port,
55-
});
47+
if (unix) {
48+
_server = Bun.serve({
49+
error: (error) =>
50+
new Response(`<pre>${error}\n${error.stack}</pre>`, {
51+
headers: { 'Content-Type': 'text/html' },
52+
}),
53+
fetch: handler(manifest, options),
54+
unix,
55+
});
56+
}
57+
else {
58+
_server = Bun.serve({
59+
error: (error) =>
60+
new Response(`<pre>${error}\n${error.stack}</pre>`, {
61+
headers: { 'Content-Type': 'text/html' },
62+
}),
63+
fetch: handler(manifest, options),
64+
hostname,
65+
port,
66+
});
67+
}
5668

5769
function exit(): void {
5870
if (_server) _server.stop();
@@ -69,7 +81,7 @@ export function start(manifest: SSRManifest, options: Options): void {
6981

7082
function handler(
7183
manifest: SSRManifest,
72-
options: Options,
84+
options: InternalOptions,
7385
): (req: Request, server: Server) => Promise<Response> {
7486
const clientRoot = options.client ?? new URL('../client/', import.meta.url).href;
7587

package/src/types.ts

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,85 @@
11
import type { Server } from 'bun';
2-
import type { z } from 'zod';
2+
import type { AstroConfig } from 'astro';
3+
4+
// the options available to the user when they are adding
5+
// the adapter to their configuration
6+
export interface Options {
7+
/**
8+
* Create a cluster of bun servers listening on the same port,
9+
* and automatically load-balance incoming requests across them.
10+
*
11+
* Example:
12+
* ```ts
13+
* export default defineConfig({
14+
* adapter: bun({ cluster: true })
15+
* })
16+
* ```
17+
*
18+
* Defaults to `false`
19+
*/
20+
cluster?: boolean
21+
/**
22+
* The path to the unix socket on which to host the server.
23+
*
24+
* This can provide better performance when Bun is running alongside
25+
* a local reverse proxy that supports unix sockets.
26+
*
27+
* When a unix socket is provided, Bun does not bind to a TCP port,
28+
* and the options and environment variables for the hostname and port
29+
* are ignored.
30+
*
31+
* Example:
32+
* ```ts
33+
* export default defineConfig({
34+
* adapter: bun({ unix: "/tmp/my-socket.sock" })
35+
* })
36+
* ```
37+
*/
38+
unix?: string
39+
}
40+
41+
// options provided by the user combined with other
42+
// relevant configuration picked from the integration API
43+
export interface InternalOptions extends Options {
44+
/**
45+
* Name of the publicly exposed directory where all
46+
* static assets are put.
47+
*
48+
* Astro defaults to `"_astro"`.
49+
*/
50+
assets: AstroConfig["build"]["assets"],
51+
/**
52+
* The full file URL to where astro is configured to put
53+
* the client bundle and assets such as images, fonts,
54+
* stylesheets, and static html.
55+
*
56+
* Astro defaults to `"<project root>/dist/client/"`.
57+
*/
58+
client: AstroConfig["build"]["server"]["href"],
59+
/**
60+
* The full file URL to where astro is configured to put
61+
* the server bundle.
62+
*
63+
* Astro defaults to `"<project root>/dist/server/""`.
64+
*/
65+
server: AstroConfig["build"]["server"]["href"],
66+
/**
67+
* Network address where the astro dev server is
68+
* configured to listen for requests in addition to
69+
* `localhost`.
70+
*
71+
* Astro defaults to `false`.
72+
*/
73+
host: AstroConfig["server"]["host"],
74+
/**
75+
* Network port where the astro dev server is
76+
* configured to listen for requests.
77+
*
78+
* Astro default to `4321`.
79+
*/
80+
port: AstroConfig["server"]["port"],
81+
}
382

4-
import type { OptionsSchema } from '~/validators';
583

684
export enum CreateExportsEnum {
785
HANDLE = 'handle',
@@ -16,5 +94,3 @@ export type CreateExports = {
1694
[CreateExportsEnum.START]: () => void;
1795
[CreateExportsEnum.STOP]: () => void;
1896
};
19-
20-
export type Options = z.infer<typeof OptionsSchema>;

0 commit comments

Comments
 (0)