Initial commit
This commit is contained in:
commit
b3a51a4115
10336 changed files with 2381973 additions and 0 deletions
87
node_modules/hono/dist/middleware/cors/index.js
generated
vendored
Normal file
87
node_modules/hono/dist/middleware/cors/index.js
generated
vendored
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
// src/middleware/cors/index.ts
|
||||
var cors = (options) => {
|
||||
const defaults = {
|
||||
origin: "*",
|
||||
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"],
|
||||
allowHeaders: [],
|
||||
exposeHeaders: []
|
||||
};
|
||||
const opts = {
|
||||
...defaults,
|
||||
...options
|
||||
};
|
||||
const findAllowOrigin = ((optsOrigin) => {
|
||||
if (typeof optsOrigin === "string") {
|
||||
if (optsOrigin === "*") {
|
||||
return () => optsOrigin;
|
||||
} else {
|
||||
return (origin) => optsOrigin === origin ? origin : null;
|
||||
}
|
||||
} else if (typeof optsOrigin === "function") {
|
||||
return optsOrigin;
|
||||
} else {
|
||||
return (origin) => optsOrigin.includes(origin) ? origin : null;
|
||||
}
|
||||
})(opts.origin);
|
||||
const findAllowMethods = ((optsAllowMethods) => {
|
||||
if (typeof optsAllowMethods === "function") {
|
||||
return optsAllowMethods;
|
||||
} else if (Array.isArray(optsAllowMethods)) {
|
||||
return () => optsAllowMethods;
|
||||
} else {
|
||||
return () => [];
|
||||
}
|
||||
})(opts.allowMethods);
|
||||
return async function cors2(c, next) {
|
||||
function set(key, value) {
|
||||
c.res.headers.set(key, value);
|
||||
}
|
||||
const allowOrigin = await findAllowOrigin(c.req.header("origin") || "", c);
|
||||
if (allowOrigin) {
|
||||
set("Access-Control-Allow-Origin", allowOrigin);
|
||||
}
|
||||
if (opts.credentials) {
|
||||
set("Access-Control-Allow-Credentials", "true");
|
||||
}
|
||||
if (opts.exposeHeaders?.length) {
|
||||
set("Access-Control-Expose-Headers", opts.exposeHeaders.join(","));
|
||||
}
|
||||
if (c.req.method === "OPTIONS") {
|
||||
if (opts.origin !== "*") {
|
||||
set("Vary", "Origin");
|
||||
}
|
||||
if (opts.maxAge != null) {
|
||||
set("Access-Control-Max-Age", opts.maxAge.toString());
|
||||
}
|
||||
const allowMethods = await findAllowMethods(c.req.header("origin") || "", c);
|
||||
if (allowMethods.length) {
|
||||
set("Access-Control-Allow-Methods", allowMethods.join(","));
|
||||
}
|
||||
let headers = opts.allowHeaders;
|
||||
if (!headers?.length) {
|
||||
const requestHeaders = c.req.header("Access-Control-Request-Headers");
|
||||
if (requestHeaders) {
|
||||
headers = requestHeaders.split(/\s*,\s*/);
|
||||
}
|
||||
}
|
||||
if (headers?.length) {
|
||||
set("Access-Control-Allow-Headers", headers.join(","));
|
||||
c.res.headers.append("Vary", "Access-Control-Request-Headers");
|
||||
}
|
||||
c.res.headers.delete("Content-Length");
|
||||
c.res.headers.delete("Content-Type");
|
||||
return new Response(null, {
|
||||
headers: c.res.headers,
|
||||
status: 204,
|
||||
statusText: "No Content"
|
||||
});
|
||||
}
|
||||
await next();
|
||||
if (opts.origin !== "*") {
|
||||
c.header("Vary", "Origin", { append: true });
|
||||
}
|
||||
};
|
||||
};
|
||||
export {
|
||||
cors
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue