Skip to content

Commit

Permalink
feat(undios): support config.signal
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 9, 2024
1 parent 213e6c8 commit 3247ea2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
13 changes: 6 additions & 7 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
"./adapter": {
"node": {
"require": "./lib/adapter/node.cjs",
"import": "./lib/adapter/node.js"
"import": "./lib/adapter/node.js",
"types": "./lib/adapter/node.d.ts"
},
"default": {
"import": "./lib/adapter/browser.js"
},
"types": "./lib/adapter/index.d.ts"
"default": "./lib/adapter/browser.js",
"types": "./lib/adapter/browser.d.ts"
},
"./package.json": "./package.json"
},
Expand Down Expand Up @@ -50,11 +49,11 @@
"plugin"
],
"devDependencies": {
"cordis": "^3.11.0",
"cordis": "^3.13.3",
"undici": "^6.6.2"
},
"peerDependencies": {
"cordis": "^3.11.0"
"cordis": "^3.13.3"
},
"dependencies": {
"cosmokit": "^1.5.2",
Expand Down
40 changes: 25 additions & 15 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export namespace HTTP {
data?: any
keepAlive?: boolean
redirect?: RequestRedirect
signal?: AbortSignal
responseType?: keyof ResponseTypes
validateStatus?: (status: number) => boolean
}
Expand Down Expand Up @@ -173,7 +174,7 @@ export class HTTP extends Service<HTTP.Config> {
})

decoder<K extends keyof HTTP.ResponseTypes>(type: K, decoder: (raw: Response) => Awaitable<HTTP.ResponseTypes[K]>) {
return this[Context.current].effect(() => {
return this[Context.origin].effect(() => {
this._decoders[type] = decoder
return () => delete this._decoders[type]
})
Expand All @@ -186,7 +187,7 @@ export class HTTP extends Service<HTTP.Config> {
}

resolveConfig(init?: HTTP.RequestConfig): HTTP.RequestConfig {
const caller = this[Context.current]
const caller = this[Context.origin]
let result = { headers: {}, ...this.config }
caller.emit('http/config', result)
let intercept = caller[Context.intercept]
Expand All @@ -200,7 +201,7 @@ export class HTTP extends Service<HTTP.Config> {

resolveURL(url: string | URL, config: HTTP.RequestConfig) {
if (config.endpoint) {
// this[Context.current].emit('internal/warning', 'endpoint is deprecated, please use baseURL instead')
// this[Context.origin].emit('internal/warning', 'endpoint is deprecated, please use baseURL instead')
try {
new URL(url)
} catch {
Expand Down Expand Up @@ -231,7 +232,7 @@ export class HTTP extends Service<HTTP.Config> {
}

async [Service.invoke](...args: any[]) {
const caller = this[Context.current]
const caller = this[Context.origin]
let method: HTTP.Method | undefined
if (typeof args[1] === 'string' || args[1] instanceof URL) {
method = args.shift()
Expand All @@ -241,16 +242,25 @@ export class HTTP extends Service<HTTP.Config> {
method ??= config.method ?? 'GET'

const controller = new AbortController()
let timer: NodeJS.Timeout | number | undefined
const dispose = caller.on('dispose', () => {
clearTimeout(timer)
controller.abort(new HTTPError('context disposed', 'ETIMEDOUT'))
})
if (config.timeout) {
timer = setTimeout(() => {
if (config.signal) {
if (config.signal.aborted) {
throw config.signal.reason
}
config.signal.addEventListener('abort', () => {
controller.abort(config.signal!.reason)
})
}

const dispose = caller.effect(() => {
const timer = config.timeout && setTimeout(() => {
controller.abort(new HTTPError('request timeout', 'ETIMEDOUT'))
}, config.timeout)
}
return () => {
clearTimeout(timer)
controller.abort(new HTTPError('context disposed', 'ETIMEDOUT'))
}
})
controller.signal.addEventListener('abort', dispose)

try {
const headers = new Headers(config.headers)
Expand Down Expand Up @@ -307,7 +317,7 @@ export class HTTP extends Service<HTTP.Config> {
}
return response
} finally {
dispose()
controller.abort()
}
}

Expand All @@ -320,7 +330,7 @@ export class HTTP extends Service<HTTP.Config> {
axios<T = any>(config: { url: string } & HTTP.RequestConfig): Promise<HTTP.Response<T>>
axios<T = any>(url: string, config?: HTTP.RequestConfig): Promise<HTTP.Response<T>>
axios(...args: any[]) {
const caller = this[Context.current]
const caller = this[Context.origin]
caller.emit('internal/warning', 'ctx.http.axios() is deprecated, use ctx.http() instead')
if (typeof args[0] === 'string') {
return this(args[0], args[1])
Expand All @@ -330,7 +340,7 @@ export class HTTP extends Service<HTTP.Config> {
}

ws(url: string | URL, init?: HTTP.Config) {
const caller = this[Context.current]
const caller = this[Context.origin]
const config = this.resolveConfig(init)
url = this.resolveURL(url, config)
let options: ClientOptions | undefined
Expand Down

0 comments on commit 3247ea2

Please sign in to comment.