Skip to content

Commit

Permalink
fix: 🐛 fix cors config
Browse files Browse the repository at this point in the history
  • Loading branch information
charlzyx committed Feb 20, 2024
1 parent fd79d46 commit 6e6e3e0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 22 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"@elysiajs/cors": "^0.8.0",
"@elysiajs/html": "^0.8.0",
"@elysiajs/jwt": "^0.8.0",
"@elysiajs/static": "^0.8.1",
"elysia": "latest",
"elysia": "^0.8.17",
"gunzip-maybe": "^1.4.2",
"lru-cache": "^10.2.0",
"mime": "^3.0.0",
Expand Down
12 changes: 9 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import path from "path";
import os from "os";
import type { cors } from "@elysiajs/cors";

const getCROSOrigin = () => {
const maybe = /^\//.test(Bun.env.CORS_ORIGIN ?? "")
? new RegExp(Bun.env.CORS_ORIGIN!)
: Bun.env.CORS_ORIGIN ?? "*";
// * will be ignore, use true to fix it.
return maybe === "*" ? true : maybe;
};

export const BunPkgConfig = {
get PORT() {
return process.env.PORT ?? "4567";
Expand All @@ -13,9 +21,7 @@ export const BunPkgConfig = {
return process.env.ORIGIN || `http://localhost${process.env.PORT}`;
},
cors: {
origin: /^\//.test(Bun.env.CORS_ORIGIN ?? "")
? new RegExp(Bun.env.CORS_ORIGIN!)
: Bun.env.CORS_ORIGIN ?? "*",
origin: getCROSOrigin(),
} as Parameters<typeof cors>[0],
get cacheDir() {
return Bun.env.CACHE_DIR || path.resolve(os.tmpdir(), "bunpkg_cache");
Expand Down
2 changes: 0 additions & 2 deletions src/utils/npm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import semver from "semver";
import { URL } from "whatwg-url";
import { BunPkgConfig } from "../config";
import {
TTL,
Expand All @@ -13,7 +12,6 @@ import {
findEntryInEntries,
findMatchEntries,
findMatchEntry,
promiseifyStream,
search,
} from "./stream";

Expand Down
15 changes: 0 additions & 15 deletions src/utils/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@ export const promiseifyStream = (stream: Transform | IncomingMessage) => {
});
};

export const setMetaHeaders = (resp: Response, meta: IFileMeta) => {
const tags = ["file"];

const ext = nodepath.extname(meta.path!).substr(1);
if (ext) {
tags.push(`${ext}-file`);
}
resp.headers.set("Cross-Origin-Resource-Policy", "cross-origin");
resp.headers.set("Content-Type", getContentType(meta.contentType!));
resp.headers.set("Content-Length", meta.size?.toString()!);
resp.headers.set("Cache-Control", "public, max-age=31536000");
resp.headers.set("Last-Modified", meta.lastModified!);
resp.headers.set("ETag", tags.join(", "));
};

/**
* Search the given tarball for entries that match the given name.
* Follows node's resolution algorithm.
Expand Down

0 comments on commit 6e6e3e0

Please sign in to comment.