Skip to content

Commit

Permalink
feat(http): support http/file hook
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 21, 2024
1 parent c2c6f96 commit b6b3833
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ declare module 'cordis' {
}

interface Events {
'http/config'(config: HTTP.Config): void
'http/fetch-init'(url: URL, init: RequestInit, config: HTTP.Config): void
'http/websocket-init'(url: URL, init: ClientOptions, config: HTTP.Config): void
'http/file'(this: HTTP, url: string, options: FileOptions): Awaitable<FileResponse | undefined>
'http/config'(this: HTTP, config: HTTP.Config): void
'http/fetch-init'(this: HTTP, url: URL, init: RequestInit, config: HTTP.Config): void
'http/websocket-init'(this: HTTP, url: URL, init: ClientOptions, config: HTTP.Config): void
}
}

Expand Down Expand Up @@ -117,7 +118,7 @@ export namespace HTTP {
}
}

export interface FileConfig {
export interface FileOptions {
timeout?: number | string
}

Expand Down Expand Up @@ -179,6 +180,7 @@ export class HTTP extends Service<HTTP.Config> {
this.decoder('arraybuffer', (raw) => raw.arrayBuffer())
this.decoder('formdata', (raw) => raw.formData())
this.decoder('stream', (raw) => raw.body as any)
this.ctx.on('http/file', (url, options) => loadFile(url))
}

static mergeConfig = (target: HTTP.Config, source?: HTTP.Config) => ({
Expand Down Expand Up @@ -206,7 +208,7 @@ export class HTTP extends Service<HTTP.Config> {
resolveConfig(init?: HTTP.RequestConfig): HTTP.RequestConfig {
const caller = this[Context.origin]
let result = { headers: {}, ...this.config }
caller.emit('http/config', result)
caller.emit(this, 'http/config', result)
let intercept = caller[Context.intercept]
while (intercept) {
result = HTTP.mergeConfig(result, intercept.http)
Expand Down Expand Up @@ -299,7 +301,7 @@ export class HTTP extends Service<HTTP.Config> {
headers.append('Content-Type', type)
}
}
caller.emit('http/fetch-init', url, init, config)
caller.emit(this, 'http/fetch-init', url, init, config)
const raw = await fetch(url, init).catch((cause) => {
if (HTTP.Error.is(cause)) throw cause
const error = new HTTP.Error(`fetch ${url} failed`)
Expand Down Expand Up @@ -369,7 +371,7 @@ export class HTTP extends Service<HTTP.Config> {
handshakeTimeout: config?.timeout,
headers: config?.headers,
}
caller.emit('http/websocket-init', url, options, config)
caller.emit(this, 'http/websocket-init', url, options, config)
}
const socket = new WebSocket(url, options as never)
const dispose = caller.on('dispose', () => {
Expand All @@ -381,9 +383,9 @@ export class HTTP extends Service<HTTP.Config> {
return socket
}

async file(this: HTTP, url: string, options: FileConfig = {}): Promise<FileResponse> {
const result = await loadFile(url)
if (result) return result
async file(this: HTTP, url: string, options: FileOptions = {}): Promise<FileResponse> {
const task = await this[Context.origin].serial(this, 'http/file', url, options)
if (task) return task
const capture = /^data:([\w/-]+);base64,(.*)$/.exec(url)
if (capture) {
const [, type, base64] = capture
Expand Down

0 comments on commit b6b3833

Please sign in to comment.