Skip to content

Commit

Permalink
chore: 🤖 elysiajs update to 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
charlzyx committed Apr 2, 2024
1 parent 37b48cd commit ae0a3a6
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 46 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion docs/components/Codebox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect, useRef, useState } from "react";
import type React from "react";
import { useEffect, useRef, useState } from "react";
import "./style.css";

const holder = "https://bunpkg.com";
Expand Down
3 changes: 2 additions & 1 deletion docs/components/Flex.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useMemo } from "react";
import type React from "react";
import { useMemo } from "react";

export const Flex: React.FC<
React.PropsWithChildren<{
Expand Down
2 changes: 1 addition & 1 deletion docs/components/Icons.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from "react";
import type React from "react";

const sty: React.CSSProperties = {
display: "inline-block",
Expand Down
2 changes: 1 addition & 1 deletion docs/components/Loading/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMap, useRequest } from "ahooks";
import { useMap, type useRequest } from "ahooks";

import "./style.css";
import { useMemo } from "react";
Expand Down
2 changes: 1 addition & 1 deletion docs/components/MetaBrowser/FilesTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { MouseEventHandler, useMemo } from "react";
import React, { type MouseEventHandler, useMemo } from "react";
import { Loading } from "../Loading";
import { usePrefixData } from "./hooks";
import "./style.css";
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"docs:build": "vocs build"
},
"dependencies": {
"@elysiajs/cors": "^0.8.0",
"@elysiajs/html": "^0.8.0",
"@elysiajs/jwt": "^0.8.0",
"@elysiajs/static": "^0.8.1",
"@elysiajs/cors": "^1.0.2",
"@elysiajs/html": "^1.0.2",
"@elysiajs/jwt": "^1.0.2",
"@elysiajs/static": "^1.0.2",
"bun-compression": "^0.0.4",
"consola": "^3.2.3",
"defu": "^6.1.4",
"elysia": "^0.8.17",
"elysia": "^1.0.10",
"get-port-please": "^3.1.2",
"gunzip-maybe": "^1.4.2",
"lru-cache": "^10.2.0",
Expand All @@ -26,18 +26,18 @@
"devDependencies": {
"@types/gunzip-maybe": "^1.4.2",
"@types/mime": "^3.0.4",
"@types/react": "^18.2.64",
"@types/react-dom": "^18.2.21",
"@types/react": "^18.2.73",
"@types/react-dom": "^18.2.23",
"@types/semver": "^7.5.8",
"@types/tar-stream": "^3.1.3",
"@types/validate-npm-package-name": "^4.0.2",
"ahooks": "^3.7.10",
"bun-types": "^1.0.30",
"ahooks": "^3.7.11",
"bun-types": "^1.1.0",
"changelogen": "^0.5.5",
"ofetch": "^1.3.3",
"ofetch": "^1.3.4",
"react-hot-toast": "^2.4.1",
"react-tooltip": "^5.26.3",
"semver": "^7.6.0",
"vocs": "^1.0.0-alpha.43"
"vocs": "^1.0.0-alpha.45"
}
}
3 changes: 0 additions & 3 deletions src/bunpkg.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import path from "node:path";
import { cors } from "@elysiajs/cors";

export const BunPkgConfig = {
/** 服务器相关 */
server: {
Expand Down
4 changes: 2 additions & 2 deletions src/common/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { SqliteLRUCache } from "./sqlite-lru-cache";
import { BunPkgConfig } from "../config.final";
import path from "node:path";
import { LRUCache } from "lru-cache";
import { BunFile } from "bun";
import { serialize } from "v8";
import type { BunFile } from "bun";
import { serialize } from "node:v8";
import { markError } from "./err";

const database = path.join(BunPkgConfig.cache.dir, `cache.sqlite`);
Expand Down
1 change: 0 additions & 1 deletion src/common/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import consola from "consola";
import { BunPkgConfig } from "../config.final";
import { isTypedError, markError } from "./err";
import { encodePkgName } from "./pkg";
Expand Down
2 changes: 1 addition & 1 deletion src/common/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BunFile } from "bun";
import type { BunFile } from "bun";
import path from "node:path";
import mime from "mime";
import { parseTar } from "nanotar";
Expand Down
4 changes: 2 additions & 2 deletions src/common/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { resolve, semver, version } from "bun";
import validateNpmPackageName from "validate-npm-package-name";
import { BunPkgConfig } from "../config.final";
import { unlink } from "node:fs";
import { IFileMeta } from "../features/utils";
import type { IFileMeta } from "../features/utils";
import { memoCache, sqliteCache } from "./cache";
import { markError } from "./err";
import { fetchPackageInfo, fetchPackageTarball } from "./fetch";
import { getContentType, getIntegrityBy, tgzReader } from "./file";
import { TarFileItem } from "nanotar";
import type { TarFileItem } from "nanotar";

const helper = {
packagePathnameFormat: /^\/((?:@[^/@]+\/)?[^/@]+)(?:@([^/]+))?(\/.*)?$/,
Expand Down
4 changes: 2 additions & 2 deletions src/common/sqlite-lru-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Database, Statement } from "bun:sqlite";
import { serialize, deserialize } from "v8";
import { Database, type Statement } from "bun:sqlite";
import { serialize, deserialize } from "node:v8";

export type SqlCacheOptions<Meta> = {
/** name (in memory) of path string (file) */
Expand Down
6 changes: 3 additions & 3 deletions src/features/esm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Elysia } from "elysia";
import type { Elysia } from "elysia";
import { memoCache, sqliteCache } from "../common/cache";
import {
findIndex,
Expand All @@ -8,7 +8,7 @@ import {
resolveVersion,
queryPkgInfo,
} from "../common/pkg";
import { appendMetaHeaders, qs } from "./utils";
import { appendMetaHeaders, qs, queryHasKey } from "./utils";
import { toESM, isSupportedESM } from "../experimental/esm";
import { BunPkgConfig } from "../config.final";

Expand All @@ -25,7 +25,7 @@ export const esm = (app: Elysia) => {
*/
return app.get("/esm/*", async (ctx) => {
const { query, path, set } = ctx;
if (query.purge !== undefined) {
if (queryHasKey(query, "purge")) {
sqliteCache.purge(path);
return Response.json({ message: `PURGE CACHE ${path} SUCCESS!` });
}
Expand Down
13 changes: 7 additions & 6 deletions src/features/meta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Elysia, t } from "elysia";
import { TarFileItem } from "nanotar";
import { type Elysia, t } from "elysia";
import type { TarFileItem } from "nanotar";
import { memoCache, sqliteCache } from "../common/cache";
import {
parsePkgByPathname,
Expand All @@ -8,7 +8,7 @@ import {
queryPkgInfo,
getConfigOfVersion,
} from "../common/pkg";
import { simpleMeta } from "./utils";
import { queryHasKey, simpleMeta } from "./utils";

export const meta = (app: Elysia) => {
/**
Expand All @@ -30,7 +30,7 @@ export const meta = (app: Elysia) => {
"/meta/*",
async (ctx) => {
const { query, path, set } = ctx;
if (query.purge !== undefined) {
if (queryHasKey(query, "purge")) {
sqliteCache.purge(path);
return Response.json({ message: `PURGE CACHE ${path} SUCCESS!` });
}
Expand All @@ -39,7 +39,7 @@ export const meta = (app: Elysia) => {
const maybe =
memoCache.get(cacheKey) || (await sqliteCache.read(cacheKey));

if (maybe) {
if (maybe && !queryHasKey(query, "versions")) {
// transform sqlite cached to memoCache
if (!memoCache.has(cacheKey)) {
memoCache.set(cacheKey, maybe);
Expand All @@ -52,7 +52,8 @@ export const meta = (app: Elysia) => {
const pkg = parsePkgByPathname(pathname);
// ---step.1.2 query remote
const remote = await queryPkgInfo(pkg.pkgName);
if (query.versions !== undefined) {

if (queryHasKey(query, "versions")) {
const cacheKey = `${path}?versions`;
const cached = await sqliteCache.read(cacheKey);
if (cached) {
Expand Down
6 changes: 3 additions & 3 deletions src/features/npm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Elysia } from "elysia";
import type { Elysia } from "elysia";
import { memoCache, sqliteCache } from "../common/cache";
import {
findIndex,
Expand All @@ -8,7 +8,7 @@ import {
resolveVersion,
queryPkgInfo,
} from "../common/pkg";
import { appendMetaHeaders, qs } from "./utils";
import { appendMetaHeaders, qs, queryHasKey } from "./utils";

export const npm = (app: Elysia) => {
/**
Expand All @@ -23,7 +23,7 @@ export const npm = (app: Elysia) => {
*/
return app.get("/npm/*", async (ctx) => {
const { query, path, set } = ctx;
if (query.purge !== undefined) {
if (queryHasKey(query, "purge")) {
sqliteCache.purge(path);
return Response.json({ message: `PURGE CACHE ${path} SUCCESS!` });
}
Expand Down
5 changes: 3 additions & 2 deletions src/features/purge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Elysia } from "elysia";
import type { Elysia } from "elysia";
import { sqliteCache } from "../common/cache";
import { queryHasKey } from "./utils";

export const purge = (app: Elysia) => {
/**
Expand All @@ -17,6 +18,6 @@ export const purge = (app: Elysia) => {
return app.get("/purge/*", async (ctx) => {
const { path, query } = ctx;
const pathname = path.replace(/^\/purge\//, "");
sqliteCache.purge(pathname, query.wild !== undefined);
sqliteCache.purge(pathname, queryHasKey(query, "wild"));
});
};
2 changes: 1 addition & 1 deletion src/features/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Database, { Statement } from "bun:sqlite";
import Database, { type Statement } from "bun:sqlite";
import path from "node:path";
import { markError } from "../common/err";
import { BunPkgConfig } from "../config.final";
Expand Down
6 changes: 5 additions & 1 deletion src/features/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TarFileItem } from "nanotar";
import type { TarFileItem } from "nanotar";
import path from "node:path";

export type IFileMeta = {
Expand Down Expand Up @@ -51,3 +51,7 @@ export const simpleMeta = (x: TarFileItem) => {
lastModified: x.attrs?.mtime!,
};
};

export const queryHasKey = (query: Record<string, any>, key: string) => {
return Object.hasOwn(query, key);
};
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { user } from "./features/user";
import { staticPlugin } from "@elysiajs/static";

const app = new Elysia();
const noop = () => "";

const PORT = await getPort(BunPkgConfig.server.port);

Expand Down Expand Up @@ -102,9 +103,9 @@ app
// resp.headers.set("Content-Type", "text/html; charset=utf-8");
// return resp;
// })
.get("/favicon.ico", () => {
return "";
}),
.get("/favicon.ico", noop)
.get("/apple-touch-icon.png", noop)
.get("/apple-touch-icon-precomposed.png", noop),
)

.onError(({ code, error, path, set }) => {
Expand Down

0 comments on commit ae0a3a6

Please sign in to comment.