-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from YDX-2147483647/ky
- Loading branch information
Showing
12 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
}, | ||
"imports": { | ||
"std/": "https://deno.land/[email protected]/", | ||
"ky": "https://esm.sh/[email protected]", | ||
"npm:aes-js": "npm:aes-js@^3.1.2", | ||
"npm:before-after-hook": "npm:before-after-hook@^2.2.2", | ||
"npm:chalk": "npm:chalk@^5.3.0", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { hook, update_notices } from '../core/index.ts' | ||
import { add_hook } from '../plugin/cli/index.ts' | ||
import add_normalize_hook from '../plugin/normalize/index.ts' | ||
import add_retry_hook from '../plugin/retry/index.ts' | ||
|
||
add_hook.verbose(hook) | ||
add_hook.preview_output(hook) | ||
add_hook.progress_bar(hook) | ||
add_hook.recent_filter(hook, 90) | ||
add_retry_hook(hook) | ||
add_normalize_hook(hook) | ||
|
||
await update_notices() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { hook, update_notices } from '../core/index.ts' | ||
import { add_hook } from '../plugin/cli/index.ts' | ||
import add_proxy_hook from '../plugin/proxy/index.ts' | ||
import add_normalize_hook from '../plugin/normalize/index.ts' | ||
import add_proxy_hook from '../plugin/proxy/index.ts' | ||
import add_retry_hook from '../plugin/retry/index.ts' | ||
|
||
add_hook.verbose(hook) | ||
add_hook.preview_output(hook) | ||
add_hook.progress_bar(hook) | ||
add_proxy_hook(hook) | ||
add_retry_hook(hook) | ||
add_normalize_hook(hook) | ||
|
||
await update_notices() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { hook, update_notices } from '../core/index.ts' | ||
import { add_hook } from '../plugin/cli/index.ts' | ||
import add_normalize_hook from '../plugin/normalize/index.ts' | ||
import add_retry_hook from '../plugin/retry/index.ts' | ||
import add_rss_hook from '../plugin/rss/index.ts' | ||
|
||
add_hook.verbose(hook) | ||
add_hook.progress_bar(hook) | ||
add_hook.recent_filter(hook, 90) | ||
add_normalize_hook(hook) | ||
add_retry_hook(hook) | ||
add_rss_hook(hook) | ||
|
||
await update_notices() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* 超时后重试 | ||
* | ||
* ky 遇到超时并不会重试。 | ||
* https://github.com/sindresorhus/ky/discussions/279#discussioncomment-187602 | ||
* https://github.com/sindresorhus/ky/issues/546 | ||
* @module | ||
*/ | ||
|
||
import ky, { TimeoutError } from 'ky' | ||
import { delay } from 'std/async/delay.ts' | ||
|
||
import { config, type HookCollectionType } from '../../core/index.ts' | ||
import { logger } from '../../util/logger.ts' | ||
|
||
const retry_options = Object.assign({ | ||
delay: 10_000, | ||
}, config.retry) as { | ||
delay: number | ||
} | ||
|
||
/** | ||
* `request`出错时用 ky 重试一次 | ||
* | ||
* 建议在其它`request` hook 之后调用。 | ||
*/ | ||
export default function add_retry_hook(hook: HookCollectionType) { | ||
hook.error('request', (error, options) => { | ||
if (error instanceof TimeoutError) { | ||
logger.http( | ||
`Time is out when requesting “${options.url}”. Retry later.`, | ||
{ | ||
plugin: 'retry', | ||
}, | ||
) | ||
|
||
return delay(retry_options.delay).then(() => { | ||
logger.http(`Retry “${options.url}”.`, { | ||
plugin: 'retry', | ||
}) | ||
const { url, ...init } = options | ||
return ky(url, init) | ||
}) | ||
} else { | ||
throw error | ||
} | ||
}) | ||
} |