🎉 initiate project *astro_rewrite*
This commit is contained in:
parent
ffd4d5e86c
commit
2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions
2
node_modules/astro/dist/core/app/common.d.ts
generated
vendored
Normal file
2
node_modules/astro/dist/core/app/common.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
import type { SerializedSSRManifest, SSRManifest } from './types';
|
||||
export declare function deserializeManifest(serializedManifest: SerializedSSRManifest): SSRManifest;
|
25
node_modules/astro/dist/core/app/common.js
generated
vendored
Normal file
25
node_modules/astro/dist/core/app/common.js
generated
vendored
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { deserializeRouteData } from "../routing/manifest/serialization.js";
|
||||
function deserializeManifest(serializedManifest) {
|
||||
const routes = [];
|
||||
for (const serializedRoute of serializedManifest.routes) {
|
||||
routes.push({
|
||||
...serializedRoute,
|
||||
routeData: deserializeRouteData(serializedRoute.routeData)
|
||||
});
|
||||
const route = serializedRoute;
|
||||
route.routeData = deserializeRouteData(serializedRoute.routeData);
|
||||
}
|
||||
const assets = new Set(serializedManifest.assets);
|
||||
const componentMetadata = new Map(serializedManifest.componentMetadata);
|
||||
const clientDirectives = new Map(serializedManifest.clientDirectives);
|
||||
return {
|
||||
...serializedManifest,
|
||||
assets,
|
||||
componentMetadata,
|
||||
clientDirectives,
|
||||
routes
|
||||
};
|
||||
}
|
||||
export {
|
||||
deserializeManifest
|
||||
};
|
15
node_modules/astro/dist/core/app/index.d.ts
generated
vendored
Normal file
15
node_modules/astro/dist/core/app/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
import type { ManifestData, RouteData, SSRManifest } from '../../@types/astro';
|
||||
export { deserializeManifest } from './common.js';
|
||||
export interface MatchOptions {
|
||||
matchNotFound?: boolean | undefined;
|
||||
}
|
||||
export declare class App {
|
||||
#private;
|
||||
constructor(manifest: SSRManifest, streaming?: boolean);
|
||||
set setManifest(newManifest: SSRManifest);
|
||||
set setManifestData(newManifestData: ManifestData);
|
||||
removeBase(pathname: string): string;
|
||||
match(request: Request, { matchNotFound }?: MatchOptions): RouteData | undefined;
|
||||
render(request: Request, routeData?: RouteData, locals?: object): Promise<Response>;
|
||||
setCookieHeaders(response: Response): Generator<string, string[], unknown>;
|
||||
}
|
275
node_modules/astro/dist/core/app/index.js
generated
vendored
Normal file
275
node_modules/astro/dist/core/app/index.js
generated
vendored
Normal file
|
@ -0,0 +1,275 @@
|
|||
import mime from "mime";
|
||||
import { attachToResponse, getSetCookiesFromResponse } from "../cookies/index.js";
|
||||
import { callEndpoint } from "../endpoint/index.js";
|
||||
import { consoleLogDestination } from "../logger/console.js";
|
||||
import { error } from "../logger/core.js";
|
||||
import { prependForwardSlash, removeTrailingForwardSlash } from "../path.js";
|
||||
import { RedirectSinglePageBuiltModule } from "../redirects/index.js";
|
||||
import {
|
||||
createEnvironment,
|
||||
createRenderContext,
|
||||
tryRenderPage
|
||||
} from "../render/index.js";
|
||||
import { RouteCache } from "../render/route-cache.js";
|
||||
import {
|
||||
createAssetLink,
|
||||
createModuleScriptElement,
|
||||
createStylesheetElementSet
|
||||
} from "../render/ssr-element.js";
|
||||
import { matchRoute } from "../routing/match.js";
|
||||
import { deserializeManifest } from "./common.js";
|
||||
const clientLocalsSymbol = Symbol.for("astro.locals");
|
||||
const responseSentSymbol = Symbol.for("astro.responseSent");
|
||||
class App {
|
||||
/**
|
||||
* The current environment of the application
|
||||
*/
|
||||
#env;
|
||||
#manifest;
|
||||
#manifestData;
|
||||
#routeDataToRouteInfo;
|
||||
#encoder = new TextEncoder();
|
||||
#logging = {
|
||||
dest: consoleLogDestination,
|
||||
level: "info"
|
||||
};
|
||||
#baseWithoutTrailingSlash;
|
||||
constructor(manifest, streaming = true) {
|
||||
this.#manifest = manifest;
|
||||
this.#manifestData = {
|
||||
routes: manifest.routes.map((route) => route.routeData)
|
||||
};
|
||||
this.#routeDataToRouteInfo = new Map(manifest.routes.map((route) => [route.routeData, route]));
|
||||
this.#baseWithoutTrailingSlash = removeTrailingForwardSlash(this.#manifest.base);
|
||||
this.#env = this.#createEnvironment(streaming);
|
||||
}
|
||||
set setManifest(newManifest) {
|
||||
this.#manifest = newManifest;
|
||||
}
|
||||
/**
|
||||
* Creates an environment by reading the stored manifest
|
||||
*
|
||||
* @param streaming
|
||||
* @private
|
||||
*/
|
||||
#createEnvironment(streaming = false) {
|
||||
return createEnvironment({
|
||||
adapterName: this.#manifest.adapterName,
|
||||
logging: this.#logging,
|
||||
markdown: this.#manifest.markdown,
|
||||
mode: "production",
|
||||
compressHTML: this.#manifest.compressHTML,
|
||||
renderers: this.#manifest.renderers,
|
||||
clientDirectives: this.#manifest.clientDirectives,
|
||||
resolve: async (specifier) => {
|
||||
if (!(specifier in this.#manifest.entryModules)) {
|
||||
throw new Error(`Unable to resolve [${specifier}]`);
|
||||
}
|
||||
const bundlePath = this.#manifest.entryModules[specifier];
|
||||
switch (true) {
|
||||
case bundlePath.startsWith("data:"):
|
||||
case bundlePath.length === 0: {
|
||||
return bundlePath;
|
||||
}
|
||||
default: {
|
||||
return createAssetLink(bundlePath, this.#manifest.base, this.#manifest.assetsPrefix);
|
||||
}
|
||||
}
|
||||
},
|
||||
routeCache: new RouteCache(this.#logging),
|
||||
site: this.#manifest.site,
|
||||
ssr: true,
|
||||
streaming
|
||||
});
|
||||
}
|
||||
set setManifestData(newManifestData) {
|
||||
this.#manifestData = newManifestData;
|
||||
}
|
||||
removeBase(pathname) {
|
||||
if (pathname.startsWith(this.#manifest.base)) {
|
||||
return pathname.slice(this.#baseWithoutTrailingSlash.length + 1);
|
||||
}
|
||||
return pathname;
|
||||
}
|
||||
match(request, { matchNotFound = false } = {}) {
|
||||
const url = new URL(request.url);
|
||||
if (this.#manifest.assets.has(url.pathname)) {
|
||||
return void 0;
|
||||
}
|
||||
let pathname = prependForwardSlash(this.removeBase(url.pathname));
|
||||
let routeData = matchRoute(pathname, this.#manifestData);
|
||||
if (routeData) {
|
||||
if (routeData.prerender)
|
||||
return void 0;
|
||||
return routeData;
|
||||
} else if (matchNotFound) {
|
||||
const notFoundRouteData = matchRoute("/404", this.#manifestData);
|
||||
if (notFoundRouteData == null ? void 0 : notFoundRouteData.prerender)
|
||||
return void 0;
|
||||
return notFoundRouteData;
|
||||
} else {
|
||||
return void 0;
|
||||
}
|
||||
}
|
||||
async render(request, routeData, locals) {
|
||||
let defaultStatus = 200;
|
||||
if (!routeData) {
|
||||
routeData = this.match(request);
|
||||
if (!routeData) {
|
||||
defaultStatus = 404;
|
||||
routeData = this.match(request, { matchNotFound: true });
|
||||
}
|
||||
if (!routeData) {
|
||||
return new Response(null, {
|
||||
status: 404,
|
||||
statusText: "Not found"
|
||||
});
|
||||
}
|
||||
}
|
||||
Reflect.set(request, clientLocalsSymbol, locals ?? {});
|
||||
if (routeData.route === "/404") {
|
||||
defaultStatus = 404;
|
||||
}
|
||||
let mod = await this.#getModuleForRoute(routeData);
|
||||
if (routeData.type === "page" || routeData.type === "redirect") {
|
||||
let response = await this.#renderPage(request, routeData, mod, defaultStatus);
|
||||
if (response.status === 500 || response.status === 404) {
|
||||
const errorRouteData = matchRoute("/" + response.status, this.#manifestData);
|
||||
if (errorRouteData && errorRouteData.route !== routeData.route) {
|
||||
mod = await this.#getModuleForRoute(errorRouteData);
|
||||
try {
|
||||
let errorResponse = await this.#renderPage(
|
||||
request,
|
||||
errorRouteData,
|
||||
mod,
|
||||
response.status
|
||||
);
|
||||
return errorResponse;
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
}
|
||||
return response;
|
||||
} else if (routeData.type === "endpoint") {
|
||||
return this.#callEndpoint(request, routeData, mod, defaultStatus);
|
||||
} else {
|
||||
throw new Error(`Unsupported route type [${routeData.type}].`);
|
||||
}
|
||||
}
|
||||
setCookieHeaders(response) {
|
||||
return getSetCookiesFromResponse(response);
|
||||
}
|
||||
async #getModuleForRoute(route) {
|
||||
if (route.type === "redirect") {
|
||||
return RedirectSinglePageBuiltModule;
|
||||
} else {
|
||||
if (this.#manifest.pageMap) {
|
||||
const importComponentInstance = this.#manifest.pageMap.get(route.component);
|
||||
if (!importComponentInstance) {
|
||||
throw new Error(
|
||||
`Unexpectedly unable to find a component instance for route ${route.route}`
|
||||
);
|
||||
}
|
||||
const pageModule = await importComponentInstance();
|
||||
return pageModule;
|
||||
} else if (this.#manifest.pageModule) {
|
||||
const importComponentInstance = this.#manifest.pageModule;
|
||||
return importComponentInstance;
|
||||
} else {
|
||||
throw new Error(
|
||||
"Astro couldn't find the correct page to render, probably because it wasn't correctly mapped for SSR usage. This is an internal error, please file an issue."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
async #renderPage(request, routeData, page, status = 200) {
|
||||
const url = new URL(request.url);
|
||||
const pathname = prependForwardSlash(this.removeBase(url.pathname));
|
||||
const info = this.#routeDataToRouteInfo.get(routeData);
|
||||
const links = /* @__PURE__ */ new Set();
|
||||
const styles = createStylesheetElementSet(info.styles);
|
||||
let scripts = /* @__PURE__ */ new Set();
|
||||
for (const script of info.scripts) {
|
||||
if ("stage" in script) {
|
||||
if (script.stage === "head-inline") {
|
||||
scripts.add({
|
||||
props: {},
|
||||
children: script.children
|
||||
});
|
||||
}
|
||||
} else {
|
||||
scripts.add(createModuleScriptElement(script));
|
||||
}
|
||||
}
|
||||
try {
|
||||
const mod = await page.page();
|
||||
const renderContext = await createRenderContext({
|
||||
request,
|
||||
pathname,
|
||||
componentMetadata: this.#manifest.componentMetadata,
|
||||
scripts,
|
||||
styles,
|
||||
links,
|
||||
route: routeData,
|
||||
status,
|
||||
mod,
|
||||
env: this.#env
|
||||
});
|
||||
const response = await tryRenderPage(renderContext, this.#env, mod, page.onRequest);
|
||||
Reflect.set(request, responseSentSymbol, true);
|
||||
return response;
|
||||
} catch (err) {
|
||||
error(this.#logging, "ssr", err.stack || err.message || String(err));
|
||||
return new Response(null, {
|
||||
status: 500,
|
||||
statusText: "Internal server error"
|
||||
});
|
||||
}
|
||||
}
|
||||
async #callEndpoint(request, routeData, page, status = 200) {
|
||||
const url = new URL(request.url);
|
||||
const pathname = "/" + this.removeBase(url.pathname);
|
||||
const mod = await page.page();
|
||||
const handler = mod;
|
||||
const ctx = await createRenderContext({
|
||||
request,
|
||||
pathname,
|
||||
route: routeData,
|
||||
status,
|
||||
env: this.#env,
|
||||
mod: handler
|
||||
});
|
||||
const result = await callEndpoint(handler, this.#env, ctx, page.onRequest);
|
||||
if (result.type === "response") {
|
||||
if (result.response.headers.get("X-Astro-Response") === "Not-Found") {
|
||||
const fourOhFourRequest = new Request(new URL("/404", request.url));
|
||||
const fourOhFourRouteData = this.match(fourOhFourRequest);
|
||||
if (fourOhFourRouteData) {
|
||||
return this.render(fourOhFourRequest, fourOhFourRouteData);
|
||||
}
|
||||
}
|
||||
return result.response;
|
||||
} else {
|
||||
const body = result.body;
|
||||
const headers = new Headers();
|
||||
const mimeType = mime.getType(url.pathname);
|
||||
if (mimeType) {
|
||||
headers.set("Content-Type", `${mimeType};charset=utf-8`);
|
||||
} else {
|
||||
headers.set("Content-Type", "text/plain;charset=utf-8");
|
||||
}
|
||||
const bytes = this.#encoder.encode(body);
|
||||
headers.set("Content-Length", bytes.byteLength.toString());
|
||||
const response = new Response(bytes, {
|
||||
status: 200,
|
||||
headers
|
||||
});
|
||||
attachToResponse(response, result.cookies);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
export {
|
||||
App,
|
||||
deserializeManifest
|
||||
};
|
18
node_modules/astro/dist/core/app/node.d.ts
generated
vendored
Normal file
18
node_modules/astro/dist/core/app/node.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,18 @@
|
|||
/// <reference types="node" />
|
||||
import type { RouteData } from '../../@types/astro';
|
||||
import type { SSRManifest } from './types';
|
||||
import { IncomingMessage } from 'node:http';
|
||||
import { App, type MatchOptions } from './index.js';
|
||||
declare class NodeIncomingMessage extends IncomingMessage {
|
||||
/**
|
||||
* The read-only body property of the Request interface contains a ReadableStream with the body contents that have been added to the request.
|
||||
*/
|
||||
body?: unknown;
|
||||
}
|
||||
export declare class NodeApp extends App {
|
||||
match(req: NodeIncomingMessage | Request, opts?: MatchOptions): RouteData | undefined;
|
||||
render(req: NodeIncomingMessage | Request, routeData?: RouteData, locals?: object): Promise<Response>;
|
||||
}
|
||||
export declare function loadManifest(rootFolder: URL): Promise<SSRManifest>;
|
||||
export declare function loadApp(rootFolder: URL): Promise<NodeApp>;
|
||||
export {};
|
88
node_modules/astro/dist/core/app/node.js
generated
vendored
Normal file
88
node_modules/astro/dist/core/app/node.js
generated
vendored
Normal file
|
@ -0,0 +1,88 @@
|
|||
import * as fs from "node:fs";
|
||||
import { IncomingMessage } from "node:http";
|
||||
import { TLSSocket } from "node:tls";
|
||||
import { deserializeManifest } from "./common.js";
|
||||
import { App } from "./index.js";
|
||||
const clientAddressSymbol = Symbol.for("astro.clientAddress");
|
||||
function createRequestFromNodeRequest(req, body) {
|
||||
var _a;
|
||||
const protocol = req.socket instanceof TLSSocket || req.headers["x-forwarded-proto"] === "https" ? "https" : "http";
|
||||
const hostname = req.headers.host || req.headers[":authority"];
|
||||
const url = `${protocol}://${hostname}${req.url}`;
|
||||
const rawHeaders = req.headers;
|
||||
const entries = Object.entries(rawHeaders);
|
||||
const method = req.method || "GET";
|
||||
const request = new Request(url, {
|
||||
method,
|
||||
headers: new Headers(entries),
|
||||
body: ["HEAD", "GET"].includes(method) ? null : body
|
||||
});
|
||||
if ((_a = req.socket) == null ? void 0 : _a.remoteAddress) {
|
||||
Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress);
|
||||
}
|
||||
return request;
|
||||
}
|
||||
class NodeIncomingMessage extends IncomingMessage {
|
||||
}
|
||||
class NodeApp extends App {
|
||||
match(req, opts = {}) {
|
||||
return super.match(req instanceof Request ? req : createRequestFromNodeRequest(req), opts);
|
||||
}
|
||||
render(req, routeData, locals) {
|
||||
if (typeof req.body === "string" && req.body.length > 0) {
|
||||
return super.render(
|
||||
req instanceof Request ? req : createRequestFromNodeRequest(req, Buffer.from(req.body)),
|
||||
routeData,
|
||||
locals
|
||||
);
|
||||
}
|
||||
if (typeof req.body === "object" && req.body !== null && Object.keys(req.body).length > 0) {
|
||||
return super.render(
|
||||
req instanceof Request ? req : createRequestFromNodeRequest(req, Buffer.from(JSON.stringify(req.body))),
|
||||
routeData,
|
||||
locals
|
||||
);
|
||||
}
|
||||
if ("on" in req) {
|
||||
let body = Buffer.from([]);
|
||||
let reqBodyComplete = new Promise((resolve, reject) => {
|
||||
req.on("data", (d) => {
|
||||
body = Buffer.concat([body, d]);
|
||||
});
|
||||
req.on("end", () => {
|
||||
resolve(body);
|
||||
});
|
||||
req.on("error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
return reqBodyComplete.then(() => {
|
||||
return super.render(
|
||||
req instanceof Request ? req : createRequestFromNodeRequest(req, body),
|
||||
routeData,
|
||||
locals
|
||||
);
|
||||
});
|
||||
}
|
||||
return super.render(
|
||||
req instanceof Request ? req : createRequestFromNodeRequest(req),
|
||||
routeData,
|
||||
locals
|
||||
);
|
||||
}
|
||||
}
|
||||
async function loadManifest(rootFolder) {
|
||||
const manifestFile = new URL("./manifest.json", rootFolder);
|
||||
const rawManifest = await fs.promises.readFile(manifestFile, "utf-8");
|
||||
const serializedManifest = JSON.parse(rawManifest);
|
||||
return deserializeManifest(serializedManifest);
|
||||
}
|
||||
async function loadApp(rootFolder) {
|
||||
const manifest = await loadManifest(rootFolder);
|
||||
return new NodeApp(manifest);
|
||||
}
|
||||
export {
|
||||
NodeApp,
|
||||
loadApp,
|
||||
loadManifest
|
||||
};
|
54
node_modules/astro/dist/core/app/types.d.ts
generated
vendored
Normal file
54
node_modules/astro/dist/core/app/types.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
|
||||
import type { RouteData, SerializedRouteData, SSRComponentMetadata, SSRLoadedRenderer, SSRResult } from '../../@types/astro';
|
||||
import type { SinglePageBuiltModule } from '../build/types';
|
||||
export type ComponentPath = string;
|
||||
export type StylesheetAsset = {
|
||||
type: 'inline';
|
||||
content: string;
|
||||
} | {
|
||||
type: 'external';
|
||||
src: string;
|
||||
};
|
||||
export interface RouteInfo {
|
||||
routeData: RouteData;
|
||||
file: string;
|
||||
links: string[];
|
||||
scripts: ({
|
||||
children: string;
|
||||
stage: string;
|
||||
} | {
|
||||
type: 'inline' | 'external';
|
||||
value: string;
|
||||
})[];
|
||||
styles: StylesheetAsset[];
|
||||
}
|
||||
export type SerializedRouteInfo = Omit<RouteInfo, 'routeData'> & {
|
||||
routeData: SerializedRouteData;
|
||||
};
|
||||
export type ImportComponentInstance = () => Promise<SinglePageBuiltModule>;
|
||||
export type SSRManifest = {
|
||||
adapterName: string;
|
||||
routes: RouteInfo[];
|
||||
site?: string;
|
||||
base: string;
|
||||
compressHTML: boolean;
|
||||
assetsPrefix?: string;
|
||||
markdown: MarkdownRenderingOptions;
|
||||
renderers: SSRLoadedRenderer[];
|
||||
/**
|
||||
* Map of directive name (e.g. `load`) to the directive script code
|
||||
*/
|
||||
clientDirectives: Map<string, string>;
|
||||
entryModules: Record<string, string>;
|
||||
assets: Set<string>;
|
||||
componentMetadata: SSRResult['componentMetadata'];
|
||||
pageModule?: SinglePageBuiltModule;
|
||||
pageMap?: Map<ComponentPath, ImportComponentInstance>;
|
||||
};
|
||||
export type SerializedSSRManifest = Omit<SSRManifest, 'routes' | 'assets' | 'componentMetadata' | 'clientDirectives'> & {
|
||||
routes: SerializedRouteInfo[];
|
||||
assets: string[];
|
||||
componentMetadata: [string, SSRComponentMetadata][];
|
||||
clientDirectives: [string, string][];
|
||||
};
|
||||
export type AdapterCreateExports<T = any> = (manifest: SSRManifest, args?: T) => Record<string, any>;
|
0
node_modules/astro/dist/core/app/types.js
generated
vendored
Normal file
0
node_modules/astro/dist/core/app/types.js
generated
vendored
Normal file
Loading…
Add table
Add a link
Reference in a new issue