🎉 initiate project *astro_rewrite*
This commit is contained in:
parent
ffd4d5e86c
commit
2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions
37
node_modules/astro/dist/core/middleware/callMiddleware.d.ts
generated
vendored
Normal file
37
node_modules/astro/dist/core/middleware/callMiddleware.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
import type { APIContext, MiddlewareHandler } from '../../@types/astro';
|
||||
import type { Environment } from '../render';
|
||||
/**
|
||||
* Utility function that is in charge of calling the middleware.
|
||||
*
|
||||
* It accepts a `R` generic, which usually is the `Response` returned.
|
||||
* It is a generic because endpoints can return a different payload.
|
||||
*
|
||||
* When calling a middleware, we provide a `next` function, this function might or
|
||||
* might not be called.
|
||||
*
|
||||
* A middleware, to behave correctly, can:
|
||||
* - return a `Response`;
|
||||
* - call `next`;
|
||||
*
|
||||
* Failing doing so will result an error. A middleware can call `next` and do not return a
|
||||
* response. A middleware can not call `next` and return a new `Response` from scratch (maybe with a redirect).
|
||||
*
|
||||
* ```js
|
||||
* const onRequest = async (context, next) => {
|
||||
* const response = await next(context);
|
||||
* return response;
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* ```js
|
||||
* const onRequest = async (context, next) => {
|
||||
* context.locals = "foo";
|
||||
* next();
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param onRequest The function called which accepts a `context` and a `resolve` function
|
||||
* @param apiContext The API context
|
||||
* @param responseFunction A callback function that should return a promise with the response
|
||||
*/
|
||||
export declare function callMiddleware<R>(logging: Environment['logging'], onRequest: MiddlewareHandler<R>, apiContext: APIContext, responseFunction: () => Promise<R>): Promise<Response | R>;
|
49
node_modules/astro/dist/core/middleware/callMiddleware.js
generated
vendored
Normal file
49
node_modules/astro/dist/core/middleware/callMiddleware.js
generated
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
import { bold } from "kleur/colors";
|
||||
import { AstroError, AstroErrorData } from "../errors/index.js";
|
||||
import { warn } from "../logger/core.js";
|
||||
async function callMiddleware(logging, onRequest, apiContext, responseFunction) {
|
||||
let nextCalled = false;
|
||||
let responseFunctionPromise = void 0;
|
||||
const next = async () => {
|
||||
nextCalled = true;
|
||||
responseFunctionPromise = responseFunction();
|
||||
return responseFunctionPromise;
|
||||
};
|
||||
let middlewarePromise = onRequest(apiContext, next);
|
||||
return await Promise.resolve(middlewarePromise).then(async (value) => {
|
||||
if (isEndpointOutput(value)) {
|
||||
warn(
|
||||
logging,
|
||||
"middleware",
|
||||
`Using simple endpoints can cause unexpected issues in the chain of middleware functions.
|
||||
It's strongly suggested to use full ${bold("Response")} objects.`
|
||||
);
|
||||
}
|
||||
if (nextCalled) {
|
||||
if (typeof value !== "undefined") {
|
||||
if (value instanceof Response === false) {
|
||||
throw new AstroError(AstroErrorData.MiddlewareNotAResponse);
|
||||
}
|
||||
return value;
|
||||
} else {
|
||||
if (responseFunctionPromise) {
|
||||
return responseFunctionPromise;
|
||||
} else {
|
||||
throw new AstroError(AstroErrorData.MiddlewareNotAResponse);
|
||||
}
|
||||
}
|
||||
} else if (typeof value === "undefined") {
|
||||
throw new AstroError(AstroErrorData.MiddlewareNoDataOrNextCalled);
|
||||
} else if (value instanceof Response === false) {
|
||||
throw new AstroError(AstroErrorData.MiddlewareNotAResponse);
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
});
|
||||
}
|
||||
function isEndpointOutput(endpointResult) {
|
||||
return !(endpointResult instanceof Response) && typeof endpointResult === "object" && typeof endpointResult.body === "string";
|
||||
}
|
||||
export {
|
||||
callMiddleware
|
||||
};
|
33
node_modules/astro/dist/core/middleware/index.d.ts
generated
vendored
Normal file
33
node_modules/astro/dist/core/middleware/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,33 @@
|
|||
import type { MiddlewareResponseHandler, Params } from '../../@types/astro';
|
||||
import { sequence } from './sequence.js';
|
||||
declare function defineMiddleware(fn: MiddlewareResponseHandler): MiddlewareResponseHandler;
|
||||
/**
|
||||
* Payload for creating a context to be passed to Astro middleware
|
||||
*/
|
||||
export type CreateContext = {
|
||||
/**
|
||||
* The incoming request
|
||||
*/
|
||||
request: Request;
|
||||
/**
|
||||
* Optional parameters
|
||||
*/
|
||||
params?: Params;
|
||||
};
|
||||
/**
|
||||
* Creates a context to be passed to Astro middleware `onRequest` function.
|
||||
*/
|
||||
declare function createContext({ request, params }: CreateContext): import("../../@types/astro").APIContext<Record<string, any>>;
|
||||
/**
|
||||
* It attempts to serialize `value` and return it as a string.
|
||||
*
|
||||
* ## Errors
|
||||
* If the `value` is not serializable if the function will throw a runtime error.
|
||||
*
|
||||
* Something is **not serializable** when it contains properties/values like functions, `Map`, `Set`, `Date`,
|
||||
* and other types that can't be made a string.
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
declare function trySerializeLocals(value: unknown): string;
|
||||
export { sequence, defineMiddleware, createContext, trySerializeLocals };
|
54
node_modules/astro/dist/core/middleware/index.js
generated
vendored
Normal file
54
node_modules/astro/dist/core/middleware/index.js
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { createAPIContext } from "../endpoint/index.js";
|
||||
import { sequence } from "./sequence.js";
|
||||
function defineMiddleware(fn) {
|
||||
return fn;
|
||||
}
|
||||
function createContext({ request, params }) {
|
||||
return createAPIContext({
|
||||
request,
|
||||
params: params ?? {},
|
||||
props: {},
|
||||
site: void 0
|
||||
});
|
||||
}
|
||||
function isLocalsSerializable(value) {
|
||||
let type = typeof value;
|
||||
let plainObject = true;
|
||||
if (type === "object" && isPlainObject(value)) {
|
||||
for (const [, nestedValue] of Object.entries(value)) {
|
||||
if (!isLocalsSerializable(nestedValue)) {
|
||||
plainObject = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
plainObject = false;
|
||||
}
|
||||
let result = value === null || type === "string" || type === "number" || type === "boolean" || Array.isArray(value) || plainObject;
|
||||
return result;
|
||||
}
|
||||
function isPlainObject(value) {
|
||||
if (typeof value !== "object" || value === null)
|
||||
return false;
|
||||
let proto = Object.getPrototypeOf(value);
|
||||
if (proto === null)
|
||||
return true;
|
||||
let baseProto = proto;
|
||||
while (Object.getPrototypeOf(baseProto) !== null) {
|
||||
baseProto = Object.getPrototypeOf(baseProto);
|
||||
}
|
||||
return proto === baseProto;
|
||||
}
|
||||
function trySerializeLocals(value) {
|
||||
if (isLocalsSerializable(value)) {
|
||||
return JSON.stringify(value);
|
||||
} else {
|
||||
throw new Error("The passed value can't be serialized.");
|
||||
}
|
||||
}
|
||||
export {
|
||||
createContext,
|
||||
defineMiddleware,
|
||||
sequence,
|
||||
trySerializeLocals
|
||||
};
|
8
node_modules/astro/dist/core/middleware/loadMiddleware.d.ts
generated
vendored
Normal file
8
node_modules/astro/dist/core/middleware/loadMiddleware.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
import type { AstroSettings } from '../../@types/astro';
|
||||
import type { ModuleLoader } from '../module-loader';
|
||||
/**
|
||||
* It accepts a module loader and the astro settings, and it attempts to load the middlewares defined in the configuration.
|
||||
*
|
||||
* If not middlewares were not set, the function returns an empty array.
|
||||
*/
|
||||
export declare function loadMiddleware(moduleLoader: ModuleLoader, srcDir: AstroSettings['config']['srcDir']): Promise<Record<string, any> | undefined>;
|
13
node_modules/astro/dist/core/middleware/loadMiddleware.js
generated
vendored
Normal file
13
node_modules/astro/dist/core/middleware/loadMiddleware.js
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { MIDDLEWARE_PATH_SEGMENT_NAME } from "../constants.js";
|
||||
async function loadMiddleware(moduleLoader, srcDir) {
|
||||
let middlewarePath = srcDir.pathname + "/" + MIDDLEWARE_PATH_SEGMENT_NAME;
|
||||
try {
|
||||
const module = await moduleLoader.import(middlewarePath);
|
||||
return module;
|
||||
} catch {
|
||||
return void 0;
|
||||
}
|
||||
}
|
||||
export {
|
||||
loadMiddleware
|
||||
};
|
6
node_modules/astro/dist/core/middleware/sequence.d.ts
generated
vendored
Normal file
6
node_modules/astro/dist/core/middleware/sequence.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
import type { MiddlewareResponseHandler } from '../../@types/astro';
|
||||
/**
|
||||
*
|
||||
* It accepts one or more middleware handlers and makes sure that they are run in sequence.
|
||||
*/
|
||||
export declare function sequence(...handlers: MiddlewareResponseHandler[]): MiddlewareResponseHandler;
|
27
node_modules/astro/dist/core/middleware/sequence.js
generated
vendored
Normal file
27
node_modules/astro/dist/core/middleware/sequence.js
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { defineMiddleware } from "./index.js";
|
||||
function sequence(...handlers) {
|
||||
const length = handlers.length;
|
||||
if (!length) {
|
||||
const handler = defineMiddleware((context, next) => {
|
||||
return next();
|
||||
});
|
||||
return handler;
|
||||
}
|
||||
return defineMiddleware((context, next) => {
|
||||
return applyHandle(0, context);
|
||||
function applyHandle(i, handleContext) {
|
||||
const handle = handlers[i];
|
||||
const result = handle(handleContext, async () => {
|
||||
if (i < length - 1) {
|
||||
return applyHandle(i + 1, handleContext);
|
||||
} else {
|
||||
return next();
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
});
|
||||
}
|
||||
export {
|
||||
sequence
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue