Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use new Astro internals #8254

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import type { AstroIntegrationLogger, Logger, LoggerLevel } from '../core/logger
import type { AstroComponentFactory, AstroComponentInstance } from '../runtime/server';
import type { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../core/constants.js';

export { type AstroIntegrationLogger };

export type {
MarkdownHeading,
MarkdownMetadata,
Expand Down Expand Up @@ -2173,6 +2175,7 @@ export interface PreviewServerParams {
host: string | undefined;
port: number;
base: string;
logger: AstroIntegrationLogger;
}

export type CreatePreviewServer = (
Expand Down
10 changes: 8 additions & 2 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { matchRoute } from '../routing/match.js';
import { EndpointNotFoundError, SSRRoutePipeline } from './ssrPipeline.js';
import type { RouteInfo } from './types';
export { deserializeManifest } from './common.js';
import { AstroIntegrationLogger } from '../logger/core.js';

const clientLocalsSymbol = Symbol.for('astro.locals');

Expand Down Expand Up @@ -58,6 +59,7 @@ export class App {
#pipeline: SSRRoutePipeline;
#onRequest: MiddlewareEndpointHandler | undefined;
#middlewareLoaded: boolean;
#adapterLogger: AstroIntegrationLogger;

constructor(manifest: SSRManifest, streaming = true) {
this.#manifest = manifest;
Expand All @@ -68,10 +70,14 @@ export class App {
this.#baseWithoutTrailingSlash = removeTrailingForwardSlash(this.#manifest.base);
this.#pipeline = new SSRRoutePipeline(this.#createEnvironment(streaming));
this.#middlewareLoaded = false;
this.#adapterLogger = new AstroIntegrationLogger(
this.#logger.options,
this.#manifest.adapterName
);
}

set setManifest(newManifest: SSRManifest) {
this.#manifest = newManifest;
getAdapterLogger(): AstroIntegrationLogger {
return this.#adapterLogger;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createNodeLogger } from '../config/logging.js';
import { createSettings } from '../config/settings.js';
import createStaticPreviewServer from './static-preview-server.js';
import { getResolvedHostForHttpServer } from './util.js';
import { AstroIntegrationLogger } from '../../core/logger/core.js';

/**
* Starts a local server to serve your static dist/ directory. This command is useful for previewing
Expand Down Expand Up @@ -62,6 +63,7 @@ export default async function preview(inlineConfig: AstroInlineConfig): Promise<
host: getResolvedHostForHttpServer(settings.config.server.host),
port: settings.config.server.port,
base: settings.config.base,
logger: new AstroIntegrationLogger(logger.options, settings.adapter.name),
});

return server;
Expand Down
45 changes: 0 additions & 45 deletions packages/integrations/cloudflare/src/runtime.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
import { getRuntime } from '@astrojs/cloudflare/runtime';
const runtime = getRuntime(Astro.request);
const runtime = Astro.locals.runtime;
---
<html>
<head>
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AstroAdapter, AstroIntegration } from 'astro';
import type { Options, UserOptions } from './types';

import { AstroError } from 'astro/errors';
export function getAdapter(options: Options): AstroAdapter {
return {
name: '@astrojs/node',
Expand All @@ -23,7 +23,7 @@ export function getAdapter(options: Options): AstroAdapter {

export default function createIntegration(userOptions: UserOptions): AstroIntegration {
if (!userOptions?.mode) {
throw new Error(`[@astrojs/node] Setting the 'mode' option is required.`);
throw new AstroError(`Setting the 'mode' option is required.`);
}

let _options: Options;
Expand Down
12 changes: 6 additions & 6 deletions packages/integrations/node/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { fileURLToPath } from 'node:url';
import { getNetworkAddress } from './get-network-address.js';
import { createServer } from './http-server.js';
import type { createExports } from './server';
import { AstroError } from 'astro/errors';

const preview: CreatePreviewServer = async function ({
client,
serverEntrypoint,
host,
port,
base,
logger,
}) {
type ServerModule = ReturnType<typeof createExports>;
type MaybeServerModule = Partial<ServerModule>;
Expand All @@ -21,13 +23,13 @@ const preview: CreatePreviewServer = async function ({
if (typeof ssrModule.handler === 'function') {
ssrHandler = ssrModule.handler;
} else {
throw new Error(
throw new AstroError(
`The server entrypoint doesn't have a handler. Are you sure this is the right file?`
);
}
} catch (err) {
if ((err as any).code === 'ERR_MODULE_NOT_FOUND') {
throw new Error(
throw new AstroError(
`The server entrypoint ${fileURLToPath(
serverEntrypoint
)} does not exist. Have you ran a build yet?`
Expand Down Expand Up @@ -63,13 +65,11 @@ const preview: CreatePreviewServer = async function ({
const address = getNetworkAddress('http', host, port);

if (host === undefined) {
// eslint-disable-next-line no-console
console.log(
logger.info(
`Preview server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n`
);
} else {
// eslint-disable-next-line no-console
console.log(`Preview server listening on ${address.local[0]}`);
logger.info(`Preview server listening on ${address.local[0]}`);
}

return server;
Expand Down
5 changes: 4 additions & 1 deletion packages/integrations/node/src/response-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import type { ReadableStreamDefaultReadResult } from 'node:stream/web';
import { Readable as NodeReadableStream } from 'stream';
import { AstroError } from 'astro/errors';

interface NodeStreamIterator<T> {
next(): Promise<IteratorResult<T, boolean | undefined>>;
Expand Down Expand Up @@ -221,5 +222,7 @@ export function responseIterator<T>(response: Response | Buffer): AsyncIterableI

if (isNodeReadableStream(body)) return nodeStreamIterator<T>(body);

throw new Error('Unknown body type for responseIterator. Please pass a streamable response.');
throw new AstroError(
'Unknown body type for responseIterator. Please pass a streamable response.'
);
}
9 changes: 4 additions & 5 deletions packages/integrations/node/src/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function getResolvedHostForHttpServer(host: string | boolean) {
}

export default function startServer(app: NodeApp, options: Options) {
const logger = app.getAdapterLogger();
const port = process.env.PORT ? Number(process.env.PORT) : options.port ?? 8080;
const { client } = resolvePaths(options);
const handler = middleware(app, options.mode);
Expand All @@ -59,13 +60,11 @@ export default function startServer(app: NodeApp, options: Options) {
const address = getNetworkAddress(protocol, host, port);

if (host === undefined) {
// eslint-disable-next-line no-console
console.log(
`Preview server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n`
logger.info(
`Server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n`
);
} else {
// eslint-disable-next-line no-console
console.log(`Preview server listening on ${address.local[0]}`);
logger.info(`Server listening on ${address.local[0]}`);
}

return {
Expand Down
9 changes: 4 additions & 5 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro';

import { AstroError } from 'astro/errors';
import glob from 'fast-glob';
import { basename } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
Expand Down Expand Up @@ -136,10 +136,9 @@ export default function vercelServerless({
serverEntry = config.build.serverEntry;

if (config.output === 'static') {
throw new Error(`
[@astrojs/vercel] \`output: "server"\` or \`output: "hybrid"\` is required to use the serverless adapter.

`);
throw new AstroError(
'`output: "server"` or `output: "hybrid"` is required to use the serverless adapter.'
);
}
},

Expand Down