精简代码,新增 js 类型提示
This commit is contained in:
@@ -1,35 +1,22 @@
|
|||||||
const name = (request.getQuery("name"))?.trim();
|
const name = request.getQuery("name")?.trim();
|
||||||
|
if (!name) throw new Error('Missing or empty name parameter');
|
||||||
if (!name) {
|
|
||||||
throw new Error(`Missing or empty name parameter`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ws = websocket();
|
const ws = websocket();
|
||||||
|
|
||||||
async function eventPull() {
|
const eventPull = async () => {
|
||||||
|
while (true) await ws.writeText(await event.load('messages'));
|
||||||
|
};
|
||||||
|
|
||||||
|
const messagePull = async () => {
|
||||||
while (true) {
|
while (true) {
|
||||||
const data = await event.load('messages')
|
const data = await ws.readText();
|
||||||
await ws.writeText(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async function messagePull() {
|
|
||||||
while (true) {
|
|
||||||
const data = await ws.readText()
|
|
||||||
if (data === "exit")
|
|
||||||
await event.put("messages", JSON.stringify({
|
|
||||||
name:name,
|
|
||||||
data: name+' 已断开连接'
|
|
||||||
}));
|
|
||||||
break;
|
|
||||||
if (data?.trim()) {
|
if (data?.trim()) {
|
||||||
await event.put("messages", JSON.stringify({
|
await event.put("messages", JSON.stringify({
|
||||||
name:name,
|
name: name,
|
||||||
data: data.trim()
|
data: data === "exit" ? `${name} 已断开连接` : data.trim()
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
if (data === "exit") break;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
(async () => await Promise.any([eventPull(), messagePull()]))();
|
||||||
(async () => {
|
|
||||||
await Promise.any([eventPull(), messagePull()])
|
|
||||||
})()
|
|
||||||
2
global-types/.gitignore
vendored
Normal file
2
global-types/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
node_modules
|
||||||
|
dist
|
||||||
125
global-types/globals.d.ts
vendored
Normal file
125
global-types/globals.d.ts
vendored
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
// goja.d.ts
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// WebSocket 相关类型
|
||||||
|
interface WebSocketConnection {
|
||||||
|
TypeTextMessage: number;
|
||||||
|
TypeBinaryMessage: number;
|
||||||
|
|
||||||
|
readText(): Promise<string>;
|
||||||
|
read(): Promise<{
|
||||||
|
type: number;
|
||||||
|
data: Uint8Array;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
writeText(data: string): Promise<void>;
|
||||||
|
write(mType: number, data: string | Uint8Array): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function websocket(): Promise<WebSocketConnection>;
|
||||||
|
|
||||||
|
// Event 相关类型
|
||||||
|
interface EventSystem {
|
||||||
|
load(key: string): Promise<any>;
|
||||||
|
put(key: string, value: string): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
const event: EventSystem;
|
||||||
|
|
||||||
|
// Request 相关类型
|
||||||
|
interface RequestObject {
|
||||||
|
method: string;
|
||||||
|
url: URL;
|
||||||
|
rawPath: string;
|
||||||
|
host: string;
|
||||||
|
remoteAddr: string;
|
||||||
|
proto: string;
|
||||||
|
httpVersion: string;
|
||||||
|
path: string;
|
||||||
|
query: Record<string, string>;
|
||||||
|
headers: Record<string, string>;
|
||||||
|
|
||||||
|
get(key: string): string | null;
|
||||||
|
getQuery(key: string): string;
|
||||||
|
getHeader(name: string): string;
|
||||||
|
getHeaderNames(): string[];
|
||||||
|
getHeaders(): Record<string, string>;
|
||||||
|
getRawHeaderNames(): string[];
|
||||||
|
hasHeader(name: string): boolean;
|
||||||
|
readBody(): Uint8Array;
|
||||||
|
protocol: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const request: RequestObject;
|
||||||
|
|
||||||
|
// Response 相关类型
|
||||||
|
interface CookieOptions {
|
||||||
|
maxAge?: number;
|
||||||
|
expires?: number;
|
||||||
|
path?: string;
|
||||||
|
domain?: string;
|
||||||
|
secure?: boolean;
|
||||||
|
httpOnly?: boolean;
|
||||||
|
sameSite?: "lax" | "strict" | "none";
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ResponseObject {
|
||||||
|
setHeader(key: string, value: string): void;
|
||||||
|
getHeader(key: string): string;
|
||||||
|
removeHeader(key: string): void;
|
||||||
|
hasHeader(key: string): boolean;
|
||||||
|
|
||||||
|
setStatus(statusCode: number): void;
|
||||||
|
statusCode(statusCode: number): void;
|
||||||
|
|
||||||
|
write(data: string): void;
|
||||||
|
writeHead(statusCode: number, headers?: Record<string, string>): void;
|
||||||
|
end(data?: string): void;
|
||||||
|
|
||||||
|
redirect(location: string, statusCode?: number): void;
|
||||||
|
|
||||||
|
json(data: any): void;
|
||||||
|
|
||||||
|
setCookie(name: string, value: string, options?: CookieOptions): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response: ResponseObject;
|
||||||
|
|
||||||
|
// KV 存储相关类型
|
||||||
|
interface KVListResult {
|
||||||
|
keys: string[];
|
||||||
|
cursor: string;
|
||||||
|
hasNext: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface KVOps {
|
||||||
|
get(key: string): Promise<string | null>;
|
||||||
|
set(key: string, value: string): Promise<void>;
|
||||||
|
delete(key: string): Promise<boolean>;
|
||||||
|
putIfNotExists(key: string, value: string): Promise<boolean>;
|
||||||
|
compareAndSwap(key: string, oldValue: string, newValue: string): Promise<boolean>;
|
||||||
|
list(limit?: number, cursor?: string): Promise<KVListResult>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface KVSystem {
|
||||||
|
repo(group: string): Promise<KVOps>;
|
||||||
|
org(group: string): Promise<KVOps>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const kv: KVSystem;
|
||||||
|
|
||||||
|
// Console 相关 (假设通过 require 引入)
|
||||||
|
interface Console {
|
||||||
|
log(...args: any[]): void;
|
||||||
|
warn(...args: any[]): void;
|
||||||
|
error(...args: any[]): void;
|
||||||
|
info(...args: any[]): void;
|
||||||
|
debug(...args: any[]): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
const console: Console;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {};
|
||||||
45
global-types/package-lock.json
generated
Normal file
45
global-types/package-lock.json
generated
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
"name": "@d7z-project/gitea-pages",
|
||||||
|
"version": "0.0.1-rc1",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"name": "@d7z-project/gitea-pages",
|
||||||
|
"version": "0.0.1-rc1",
|
||||||
|
"dependencies": {
|
||||||
|
"@dop251/types-goja_nodejs-url": "^0.0.0-20251015174255-5e94316bedaf"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "next"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@dop251/types-goja_nodejs-global": {
|
||||||
|
"version": "0.0.0-20251015174255-5e94316bedaf",
|
||||||
|
"resolved": "https://registry.npmmirror.com/@dop251/types-goja_nodejs-global/-/types-goja_nodejs-global-0.0.0-20251015174255-5e94316bedaf.tgz",
|
||||||
|
"integrity": "sha512-K54niz+UY67vH4IYfGJztG049z6RdtZfNGoI6VPC97NtRUR16D/k+iwg6dnbppuj/uBwCowimI+Qj9dWheucpg=="
|
||||||
|
},
|
||||||
|
"node_modules/@dop251/types-goja_nodejs-url": {
|
||||||
|
"version": "0.0.0-20251015174255-5e94316bedaf",
|
||||||
|
"resolved": "https://registry.npmmirror.com/@dop251/types-goja_nodejs-url/-/types-goja_nodejs-url-0.0.0-20251015174255-5e94316bedaf.tgz",
|
||||||
|
"integrity": "sha512-h9v7QtZydYruGj7vdIe/KSCqeywv8L0XruaH3wzSHb7A0PZtaQxRr4ANpKGoTxqPrLYfqbQaG7IRAbg/ylEBEA==",
|
||||||
|
"dependencies": {
|
||||||
|
"@dop251/types-goja_nodejs-global": "0.0.0-20251015174255-5e94316bedaf"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/typescript": {
|
||||||
|
"version": "6.0.0-dev.20251121",
|
||||||
|
"resolved": "https://registry.npmmirror.com/typescript/-/typescript-6.0.0-dev.20251121.tgz",
|
||||||
|
"integrity": "sha512-TrGhGS4hOAKgwizhMuH/3pbTNNBMCpxRA7ia8Lrv4HRMOAOzI5lWhP5uoKRDmmaF3pUVe90MBYjSieM498zUqQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"bin": {
|
||||||
|
"tsc": "bin/tsc",
|
||||||
|
"tsserver": "bin/tsserver"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.17"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
global-types/package.json
Normal file
19
global-types/package.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "@d7z-project/gitea-pages",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"types": "globals.d.ts",
|
||||||
|
"scripts": {
|
||||||
|
"test": "tsc --noEmit"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "next"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/d7z-project/gitea-pages.git"
|
||||||
|
},
|
||||||
|
"private": false,
|
||||||
|
"dependencies": {
|
||||||
|
"@dop251/types-goja_nodejs-url": "^0.0.0-20251015174255-5e94316bedaf"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user