@satorijs/core
Advanced tools
Comparing version 4.3.1 to 4.3.2
@@ -148,3 +148,3 @@ import { Context, Service, z, Logger } from 'cordis'; | ||
uid: string; | ||
_virtual: VirtualRouter; | ||
_internalRouter: InternalRouter<C>; | ||
_tempStore: Dict<Response>; | ||
@@ -154,15 +154,18 @@ constructor(ctx?: C); | ||
component(name: string, component: Component<C[typeof Context.session]>, options?: Component.Options): () => void; | ||
defineVirtualRoute<P extends string>(path: P, callback: (request: VirtualRequest<ExtractParams<P>>) => Promise<Response>): () => boolean; | ||
handleVirtualRoute(method: HTTP.Method, url: URL): Promise<Response>; | ||
defineInternalRoute<P extends string>(path: P, callback: (request: InternalRequest<C, ExtractParams<P>>) => Promise<Response>): () => boolean; | ||
handleInternalRoute(method: HTTP.Method, url: URL, headers?: Headers, body?: any): Promise<Response>; | ||
} | ||
export default Satori; | ||
export interface VirtualRequest<P = any> { | ||
export interface InternalRequest<C extends Context, P = any> { | ||
bot: Bot<C>; | ||
method: HTTP.Method; | ||
params: P; | ||
query: URLSearchParams; | ||
headers: Dict<string>; | ||
body: ArrayBuffer; | ||
} | ||
export interface VirtualRoute { | ||
export interface InternalRoute<C extends Context> { | ||
regexp: RegExp; | ||
keys: Key[]; | ||
callback: (request: VirtualRequest) => Promise<Response>; | ||
callback: (request: InternalRequest<C>) => Promise<Response>; | ||
} | ||
@@ -179,3 +182,3 @@ type Upper = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'; | ||
}), A> : never : C extends '{' ? ExtractParams<S, O, [0, ...A]> : C extends '}' ? A extends [0, ...infer A extends 0[]] ? ExtractParams<S, O, A> : ExtractParams<S, O, A> : ExtractParams<S, O, A> : O; | ||
export class VirtualRouter { | ||
export class InternalRouter<C extends Context> { | ||
ctx: Context; | ||
@@ -185,6 +188,6 @@ [Service.tracker]: { | ||
}; | ||
routes: VirtualRoute[]; | ||
routes: InternalRoute<C>[]; | ||
constructor(ctx: Context); | ||
define<P extends string>(path: P, callback: (request: VirtualRequest<ExtractParams<P>>) => Promise<Response>): () => boolean; | ||
handle(method: HTTP.Method, path: string, query: URLSearchParams): undefined | Promise<Response>; | ||
define<P extends string>(path: P, callback: (request: InternalRequest<C, ExtractParams<P>>) => Promise<Response>): () => boolean; | ||
handle(bot: Bot<C>, method: HTTP.Method, path: string, query: URLSearchParams, headers: Headers, body: any): undefined | Promise<Response>; | ||
} | ||
@@ -215,8 +218,8 @@ export interface Bot extends Methods { | ||
logger: Logger; | ||
_virtual: VirtualRouter; | ||
_internalRouter: InternalRouter<C>; | ||
protected context: Context; | ||
protected _status: Status; | ||
constructor(ctx: C, config: T, platform?: string); | ||
getVirtualUrl(path: string): string; | ||
defineVirtualRoute<P extends string>(path: P, callback: (request: VirtualRequest<ExtractParams<P>>) => Promise<Response>): () => boolean; | ||
getInternalUrl(path: string, init?: ConstructorParameters<typeof URLSearchParams>[0], slash?: boolean): string; | ||
defineInternalRoute<P extends string>(path: P, callback: (request: InternalRequest<C, ExtractParams<P>>) => Promise<Response>): () => boolean; | ||
update(login: Login): void; | ||
@@ -223,0 +226,0 @@ dispose(): Promise<void>; |
{ | ||
"name": "@satorijs/core", | ||
"description": "Core components of Satorijs", | ||
"version": "4.3.1", | ||
"version": "4.3.2", | ||
"type": "module", | ||
@@ -40,7 +40,2 @@ "main": "lib/index.cjs", | ||
], | ||
"scripts": { | ||
"compile:cjs": "esbuild src/index.ts --outfile=lib/index.cjs --bundle --sourcemap --sources-content=false --platform=node --external:cosmokit --external:cordis --target=es2022", | ||
"compile:esm": "esbuild src/index.ts --outfile=lib/index.mjs --bundle --sourcemap --sources-content=false --platform=neutral --external:cosmokit --external:cordis --target=es2022", | ||
"build": "yarn compile:cjs && yarn compile:esm && yarn dtsc" | ||
}, | ||
"cordis": { | ||
@@ -47,0 +42,0 @@ "ecosystem": { |
@@ -7,3 +7,3 @@ import { clone, Dict, pick } from 'cosmokit' | ||
import { defineAccessor, Session } from './session' | ||
import { ExtractParams, VirtualRequest, VirtualRouter } from './virtual' | ||
import { ExtractParams, InternalRequest, InternalRouter } from './internal' | ||
import { Event, List, Login, Methods, Response, SendOptions, Status, Upload, User } from '@satorijs/protocol' | ||
@@ -45,3 +45,3 @@ | ||
public _virtual: VirtualRouter | ||
public _internalRouter: InternalRouter<C> | ||
@@ -54,3 +54,3 @@ // Same as `this.ctx`, but with a more specific type. | ||
this.internal = null | ||
this._virtual = new VirtualRouter(ctx) | ||
this._internalRouter = new InternalRouter(ctx) | ||
this.context = ctx | ||
@@ -64,3 +64,3 @@ ctx.bots.push(this) | ||
this.proxyUrls = [`satori://temp/${ctx.satori.uid}/`] | ||
this.proxyUrls = [] | ||
this.features = Object.entries(Methods) | ||
@@ -84,8 +84,10 @@ .filter(([, value]) => this[value.name]) | ||
getVirtualUrl(path: string) { | ||
return `satori:${this.platform}/${this.selfId}${path}` | ||
getInternalUrl(path: string, init?: ConstructorParameters<typeof URLSearchParams>[0], slash?: boolean) { | ||
let search = new URLSearchParams(init).toString() | ||
if (search) search = '?' + search | ||
return `internal${slash ? '/' : ':'}${this.platform}/${this.selfId}${path}${search}` | ||
} | ||
defineVirtualRoute<P extends string>(path: P, callback: (request: VirtualRequest<ExtractParams<P>>) => Promise<Response>) { | ||
return this._virtual.define(path, callback) | ||
defineInternalRoute<P extends string>(path: P, callback: (request: InternalRequest<C, ExtractParams<P>>) => Promise<Response>) { | ||
return this._internalRouter.define(path, callback) | ||
} | ||
@@ -223,3 +225,3 @@ | ||
status: 200, | ||
data: upload.data, | ||
body: upload.data, | ||
headers, | ||
@@ -238,3 +240,3 @@ } | ||
const _dispose = this.ctx.on('dispose', dispose) | ||
return ids.map(id => this.getVirtualUrl(`/_tmp/${id}`)) | ||
return ids.map(id => this.getInternalUrl(`/_tmp/${id}`)) | ||
} | ||
@@ -241,0 +243,0 @@ |
import { Context, Logger, Service, z } from 'cordis' | ||
import { Awaitable, defineProperty, Dict } from 'cosmokit' | ||
import { Bot } from './bot' | ||
import { ExtractParams, VirtualRequest, VirtualRouter } from './virtual' | ||
import { ExtractParams, InternalRequest, InternalRouter } from './internal' | ||
import { Session } from './session' | ||
@@ -26,3 +26,3 @@ import { HTTP } from '@cordisjs/plugin-http' | ||
export * from './message' | ||
export * from './virtual' | ||
export * from './internal' | ||
export * from './session' | ||
@@ -124,3 +124,3 @@ | ||
public _virtual: VirtualRouter | ||
public _internalRouter: InternalRouter<C> | ||
public _tempStore: Dict<Response> = Object.create(null) | ||
@@ -132,7 +132,2 @@ | ||
this._virtual = new VirtualRouter(ctx) | ||
this.defineVirtualRoute('/_tmp/:id', async ({ params }) => { | ||
return this._tempStore[params.id] ?? { status: 404 } | ||
}) | ||
defineProperty(this.bots, Service.tracker, {}) | ||
@@ -143,4 +138,4 @@ | ||
const url = new URL(_url) | ||
if (url.protocol !== 'satori:') return | ||
const { status, data, headers } = await self.handleVirtualRoute('GET', url) | ||
if (url.protocol !== 'internal:') return | ||
const { status, body, headers } = await self.handleInternalRoute('GET', url) | ||
if (status >= 400) throw new Error(`Failed to fetch ${_url}, status code: ${status}`) | ||
@@ -153,4 +148,11 @@ if (status >= 300) { | ||
const filename = headers?.get('content-disposition')?.split('filename=')[1] | ||
return { data, filename, type, mime: type } | ||
return { data: body, filename, type, mime: type } | ||
}) | ||
this._internalRouter = new InternalRouter(ctx) | ||
this.defineInternalRoute('/_tmp/:id', async ({ params, method }) => { | ||
if (method !== 'GET') return { status: 405 } | ||
return this._tempStore[params.id] ?? { status: 404 } | ||
}) | ||
} | ||
@@ -187,7 +189,7 @@ | ||
defineVirtualRoute<P extends string>(path: P, callback: (request: VirtualRequest<ExtractParams<P>>) => Promise<Response>) { | ||
return this._virtual.define(path, callback) | ||
defineInternalRoute<P extends string>(path: P, callback: (request: InternalRequest<C, ExtractParams<P>>) => Promise<Response>) { | ||
return this._internalRouter.define(path, callback) | ||
} | ||
async handleVirtualRoute(method: HTTP.Method, url: URL): Promise<Response> { | ||
async handleInternalRoute(method: HTTP.Method, url: URL, headers = new Headers(), body?: any): Promise<Response> { | ||
const capture = /^([^/]+)\/([^/]+)(\/.+)$/.exec(url.pathname) | ||
@@ -198,4 +200,4 @@ if (!capture) return { status: 400 } | ||
if (!bot) return { status: 404 } | ||
let response = await bot._virtual.handle(method, path, url.searchParams) | ||
response ??= await this._virtual.handle(method, path, url.searchParams) | ||
let response = await bot._internalRouter.handle(bot, method, path, url.searchParams, headers, body) | ||
response ??= await this._internalRouter.handle(bot, method, path, url.searchParams, headers, body) | ||
if (!response) return { status: 404 } | ||
@@ -202,0 +204,0 @@ return response |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2706
95050
10