Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored and astrobot-houston committed Jan 17, 2024
1 parent f612488 commit 7338066
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 86 deletions.
26 changes: 15 additions & 11 deletions packages/integrations/node/src/log-listening-on.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import os from "node:os";
import type http from "node:http";
import https from "node:https";
import type { AstroIntegrationLogger } from "astro";
import os from 'node:os';
import type http from 'node:http';
import https from 'node:https';
import type { AstroIntegrationLogger } from 'astro';
import type { Options } from './types.js';
import type { AddressInfo } from "node:net";
import type { AddressInfo } from 'node:net';

export async function logListeningOn(logger: AstroIntegrationLogger, server: http.Server | https.Server, options: Pick<Options, "host">) {
await new Promise<void>(resolve => server.once('listening', resolve))
const protocol = server instanceof https.Server ? 'https' : 'http';
// Allow to provide host value at runtime
export async function logListeningOn(
logger: AstroIntegrationLogger,
server: http.Server | https.Server,
options: Pick<Options, 'host'>
) {
await new Promise<void>((resolve) => server.once('listening', resolve));
const protocol = server instanceof https.Server ? 'https' : 'http';
// Allow to provide host value at runtime
const host = getResolvedHostForHttpServer(
process.env.HOST !== undefined && process.env.HOST !== '' ? process.env.HOST : options.host
);
const { port } = server.address() as AddressInfo;
const { port } = server.address() as AddressInfo;
const address = getNetworkAddress(protocol, host, port);

if (host === undefined) {
logger.info(
`Server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n`
Expand Down
46 changes: 22 additions & 24 deletions packages/integrations/node/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import { createAppHandler } from './serve-app.js';
import type { RequestHandler } from "./types.js";
import type { NodeApp } from "astro/app/node";
import type { RequestHandler } from './types.js';
import type { NodeApp } from 'astro/app/node';

/**
* Creates a middleware that can be used with Express, Connect, etc.
*
*
* Similar to `createAppHandler` but can additionally be placed in the express
* chain as an error middleware.
*
*
* https://expressjs.com/en/guide/using-middleware.html#middleware.error-handling
*/
export default function createMiddleware(
app: NodeApp,
): RequestHandler {
const handler = createAppHandler(app)
const logger = app.getAdapterLogger()
// using spread args because express trips up if the function's
// stringified body includes req, res, next, locals directly
return async function (...args) {
// assume normal invocation at first
const [req, res, next, locals] = args;
// short circuit if it is an error invocation
if (req instanceof Error) {
const error = req;
if (next) {
return next(error);
} else {
throw error;
}
}
export default function createMiddleware(app: NodeApp): RequestHandler {
const handler = createAppHandler(app);
const logger = app.getAdapterLogger();
// using spread args because express trips up if the function's
// stringified body includes req, res, next, locals directly
return async function (...args) {
// assume normal invocation at first
const [req, res, next, locals] = args;
// short circuit if it is an error invocation
if (req instanceof Error) {
const error = req;
if (next) {
return next(error);
} else {
throw error;
}
}
try {
await handler(req, res, next, locals);
} catch (err) {
Expand All @@ -39,5 +37,5 @@ export default function createMiddleware(
res.end();
}
}
}
};
}
14 changes: 7 additions & 7 deletions packages/integrations/node/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ const createPreviewServer: CreatePreviewServer = async function (preview) {
throw err;
}
}
const host = preview.host ?? "localhost"
const port = preview.port ?? 4321
const host = preview.host ?? 'localhost';
const port = preview.port ?? 4321;
const server = createServer(ssrHandler, host, port);
logListeningOn(preview.logger, server.server, options)
logListeningOn(preview.logger, server.server, options);
await new Promise<void>((resolve, reject) => {
server.server.once('listening', resolve);
server.server.once('error', reject);
server.server.listen(port, host);
server.server.once('listening', resolve);
server.server.once('error', reject);
server.server.listen(port, host);
});
return server;
};

export { createPreviewServer as default }
export { createPreviewServer as default };
38 changes: 19 additions & 19 deletions packages/integrations/node/src/serve-app.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { NodeApp } from "astro/app/node"
import type { RequestHandler } from "./types.js";
import { NodeApp } from 'astro/app/node';
import type { RequestHandler } from './types.js';

/**
* Creates a Node.js http listener for on-demand rendered pages, compatible with http.createServer and Connect middleware.
* If the next callback is provided, it will be called if the request does not have a matching route.
* Intended to be used in both standalone and middleware mode.
*/
export function createAppHandler(app: NodeApp): RequestHandler {
return async (req, res, next, locals) => {
const request = NodeApp.createRequest(req);
const routeData = app.match(request);
if (routeData) {
const response = await app.render(request, {
addCookieHeader: true,
locals,
routeData,
});
await NodeApp.writeResponse(response, res);
} else if (next) {
return next();
} else {
const response = await app.render(req);
await NodeApp.writeResponse(response, res);
}
}
return async (req, res, next, locals) => {
const request = NodeApp.createRequest(req);
const routeData = app.match(request);
if (routeData) {
const response = await app.render(request, {
addCookieHeader: true,
locals,
routeData,
});
await NodeApp.writeResponse(response, res);
} else if (next) {
return next();
} else {
const response = await app.render(req);
await NodeApp.writeResponse(response, res);
}
};
}
16 changes: 8 additions & 8 deletions packages/integrations/node/src/serve-static.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from "node:path";
import url from "node:url";
import send from "send";
import type { IncomingMessage, ServerResponse } from "node:http";
import type { Options } from "./types.js";
import type { NodeApp } from "astro/app/node";
import path from 'node:path';
import url from 'node:url';
import send from 'send';
import type { IncomingMessage, ServerResponse } from 'node:http';
import type { Options } from './types.js';
import type { NodeApp } from 'astro/app/node';

/**
* Creates a Node.js http listener for static files and prerendered pages.
Expand All @@ -16,7 +16,7 @@ export function createStaticHandler(app: NodeApp, options: Options) {
/**
* @param ssr The SSR handler to be called if the static handler does not find a matching file.
*/
return (req: IncomingMessage, res: ServerResponse, ssr: () => unknown) => {
return (req: IncomingMessage, res: ServerResponse, ssr: () => unknown) => {
if (req.url) {
let pathname = app.removeBase(req.url);
pathname = decodeURI(new URL(pathname, 'http://host').pathname);
Expand All @@ -39,7 +39,7 @@ export function createStaticHandler(app: NodeApp, options: Options) {
ssr();
});
stream.on('headers', (_res: ServerResponse) => {
// assets in dist/_astro are hashed and should get the immutable header
// assets in dist/_astro are hashed and should get the immutable header
if (pathname.startsWith(`/${options.assets}/`)) {
// This is the "far future" cache header, used for static files whose name includes their digest hash.
// 1 year (31,536,000 seconds) is convention.
Expand Down
4 changes: 1 addition & 3 deletions packages/integrations/node/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export function createExports(manifest: SSRManifest, options: Options) {
return {
options: options,
handler:
options.mode === "middleware"
? createMiddleware(app)
: createStandaloneHandler(app, options),
options.mode === 'middleware' ? createMiddleware(app) : createStandaloneHandler(app, options),
startServer: () => startServer(app, options),
};
}
Expand Down
20 changes: 8 additions & 12 deletions packages/integrations/node/src/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import type { PreviewServer } from 'astro';
export default function standalone(app: NodeApp, options: Options) {
const port = process.env.PORT ? Number(process.env.PORT) : options.port ?? 8080;
// Allow to provide host value at runtime
const hostOptions = typeof options.host === "boolean" ? "localhost" : options.host
const hostOptions = typeof options.host === 'boolean' ? 'localhost' : options.host;
const host = process.env.HOST ?? hostOptions;
const handler = createStandaloneHandler(app, options);
const server = createServer(handler, host, port);
server.server.listen(port, host)
if (process.env.ASTRO_NODE_LOGGING !== "disabled") {
logListeningOn(app.getAdapterLogger(), server.server, options)
server.server.listen(port, host);
if (process.env.ASTRO_NODE_LOGGING !== 'disabled') {
logListeningOn(app.getAdapterLogger(), server.server, options);
}
return {
server,
Expand All @@ -40,15 +40,11 @@ export function createStandaloneHandler(app: NodeApp, options: Options) {
return;
}
staticHandler(req, res, () => appHandler(req, res));
}
};
}

// also used by preview entrypoint
export function createServer(
listener: http.RequestListener,
host: string,
port: number
) {
export function createServer(listener: http.RequestListener, host: string, port: number) {
let httpServer: http.Server | https.Server;

if (process.env.SERVER_CERT_PATH && process.env.SERVER_KEY_PATH) {
Expand All @@ -69,7 +65,7 @@ export function createServer(
httpServer.addListener('close', resolve);
httpServer.addListener('error', reject);
});

const previewable = {
host,
port,
Expand All @@ -80,7 +76,7 @@ export function createServer(
await new Promise((resolve, reject) => {
httpServer.destroy((err) => (err ? reject(err) : resolve(undefined)));
});
}
},
} satisfies PreviewServer;

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/node/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import httpMocks from 'node-mocks-http';
import { EventEmitter } from 'node:events';
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';

process.env.ASTRO_NODE_AUTOSTART = "disabled";
process.env.ASTRO_NODE_LOGGING = "disabled";
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
process.env.ASTRO_NODE_LOGGING = 'disabled';
/**
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
*/
Expand Down

0 comments on commit 7338066

Please sign in to comment.