astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
99 lines (98 loc) • 3.77 kB
JavaScript
import { NOOP_ACTIONS_MOD } from "../actions/noop-actions.js";
import { createI18nMiddleware } from "../i18n/middleware.js";
import { createOriginCheckMiddleware } from "./app/middlewares.js";
import { ActionNotFoundError } from "./errors/errors-data.js";
import { AstroError } from "./errors/index.js";
import { NOOP_MIDDLEWARE_FN } from "./middleware/noop-middleware.js";
import { sequence } from "./middleware/sequence.js";
import { RouteCache } from "./render/route-cache.js";
import { createDefaultRoutes } from "./routing/default.js";
class Pipeline {
constructor(logger, manifest, runtimeMode, renderers, resolve, serverLike, streaming, adapterName = manifest.adapterName, clientDirectives = manifest.clientDirectives, inlinedScripts = manifest.inlinedScripts, compressHTML = manifest.compressHTML, i18n = manifest.i18n, middleware = manifest.middleware, routeCache = new RouteCache(logger, runtimeMode), site = manifest.site ? new URL(manifest.site) : void 0, defaultRoutes = createDefaultRoutes(manifest), actions = manifest.actions) {
this.logger = logger;
this.manifest = manifest;
this.runtimeMode = runtimeMode;
this.renderers = renderers;
this.resolve = resolve;
this.serverLike = serverLike;
this.streaming = streaming;
this.adapterName = adapterName;
this.clientDirectives = clientDirectives;
this.inlinedScripts = inlinedScripts;
this.compressHTML = compressHTML;
this.i18n = i18n;
this.middleware = middleware;
this.routeCache = routeCache;
this.site = site;
this.defaultRoutes = defaultRoutes;
this.actions = actions;
this.internalMiddleware = [];
if (i18n?.strategy !== "manual") {
this.internalMiddleware.push(
createI18nMiddleware(i18n, manifest.base, manifest.trailingSlash, manifest.buildFormat)
);
}
}
internalMiddleware;
resolvedMiddleware = void 0;
resolvedActions = void 0;
/**
* Resolves the middleware from the manifest, and returns the `onRequest` function. If `onRequest` isn't there,
* it returns a no-op function
*/
async getMiddleware() {
if (this.resolvedMiddleware) {
return this.resolvedMiddleware;
} else if (this.middleware) {
const middlewareInstance = await this.middleware();
const onRequest = middlewareInstance.onRequest ?? NOOP_MIDDLEWARE_FN;
if (this.manifest.checkOrigin) {
this.resolvedMiddleware = sequence(createOriginCheckMiddleware(), onRequest);
} else {
this.resolvedMiddleware = onRequest;
}
return this.resolvedMiddleware;
} else {
this.resolvedMiddleware = NOOP_MIDDLEWARE_FN;
return this.resolvedMiddleware;
}
}
setActions(actions) {
this.resolvedActions = actions;
}
async getActions() {
if (this.resolvedActions) {
return this.resolvedActions;
} else if (this.actions) {
return await this.actions();
}
return NOOP_ACTIONS_MOD;
}
async getAction(path) {
const pathKeys = path.split(".").map((key) => decodeURIComponent(key));
let { server } = await this.getActions();
if (!server || !(typeof server === "object")) {
throw new TypeError(
`Expected \`server\` export in actions file to be an object. Received ${typeof server}.`
);
}
for (const key of pathKeys) {
if (!(key in server)) {
throw new AstroError({
...ActionNotFoundError,
message: ActionNotFoundError.message(pathKeys.join("."))
});
}
server = server[key];
}
if (typeof server !== "function") {
throw new TypeError(
`Expected handler for action ${pathKeys.join(".")} to be a function. Received ${typeof server}.`
);
}
return server;
}
}
export {
Pipeline
};