Skip to content

Commit

Permalink
Merge pull request #53 from YDX-2147483647/ky
Browse files Browse the repository at this point in the history
  • Loading branch information
YDX-2147483647 authored Dec 17, 2023
2 parents eb04086 + 9e6458c commit 366ab51
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 8 deletions.
13 changes: 12 additions & 1 deletion config/config.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"$id": "config.schema.json-v0.3.1",
"$id": "config.schema.json-v0.3.2",
"title": "Bulletin IT 设置",
"type": "object",
"properties": {
Expand Down Expand Up @@ -77,6 +77,17 @@
"output_path"
]
},
"retry": {
"title": "超时后重试插件",
"type": "object",
"properties": {
"delay": {
"title": "超时后等待的毫秒数",
"type": "number",
"default": 10000
}
}
},
"proxy": {
"title": "虚拟校园网插件",
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/core/fetch_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* @module
*/

import ky from 'ky'

import type { HookCollectionType } from './hooks_type.ts'

function fetch_wrapper(request: RequestInit & { url: string }) {
const { url, ...init } = request
return fetch(url, init)
return ky(url, init)
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/examples/cli.ts
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()
2 changes: 2 additions & 0 deletions src/examples/ding-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { hook, update_notices } from '../core/index.ts'
import { add_hook } from '../plugin/cli/index.ts'
import robot from '../plugin/ding/index.ts'
import add_normalize_hook from '../plugin/normalize/index.ts'
import add_retry_hook from '../plugin/retry/index.ts'
import { logger } from '../util/logger.ts'

add_hook.verbose(hook)
add_hook.progress_bar(hook)
add_retry_hook(hook)
add_normalize_hook(hook)

const { all_notices, new_notices, change } = await update_notices()
Expand Down
4 changes: 3 additions & 1 deletion src/examples/proxy-cli.ts
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()
2 changes: 2 additions & 0 deletions src/examples/rss-cli.ts
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()
4 changes: 3 additions & 1 deletion src/examples/server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

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'
import add_rss_hook from '../plugin/rss/index.ts'

add_hook.verbose(hook)
add_hook.progress_bar(hook)
add_proxy_hook(hook)
add_retry_hook(hook)
add_normalize_hook(hook)
add_rss_hook(hook)

Expand Down
4 changes: 3 additions & 1 deletion src/examples/server-ding-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import { hook, update_notices } from '../core/index.ts'
import { add_hook } from '../plugin/cli/index.ts'
import robot from '../plugin/ding/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'
import add_rss_hook from '../plugin/rss/index.ts'
import { logger } from '../util/logger.ts'

add_hook.verbose(hook)
add_hook.progress_bar(hook)
add_proxy_hook(hook)
add_retry_hook(hook)
add_normalize_hook(hook)
add_rss_hook(hook)

Expand Down
16 changes: 14 additions & 2 deletions src/plugin/cli/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { TimeoutError } from 'ky'
import cliProgress from 'npm:cli-progress'

import type { HookCollectionType } from '../../core/index.ts'
import { logger } from '../../util/logger.ts'
import { recent_checker } from '../../util/my_date.ts'
Expand All @@ -9,6 +11,12 @@ export function verbose(hook: HookCollectionType) {
const { sources } = options
logger.info(`发现${sources.length}个通知来源。`, { plugin: 'cli' })
})
hook.before('fetch_each', ({ source }) => {
logger.debug(`开始获取“${source.name}”。`, {
plugin: 'cli',
hook: 'fetch_each.before',
})
})
hook.error('fetch_each', (
err,
// @ts-ignore If `fetch_each` has an error hook, the after hook may get `undefined`
Expand All @@ -19,9 +27,9 @@ export function verbose(hook: HookCollectionType) {
logger.warn(`未能访问“${source.name}”(NotFound)。将忽略。`, {
plugin: 'cli',
})
} else if (err instanceof Deno.errors.TimedOut) {
} else if (err instanceof TimeoutError) {
logger.error(
`访问“${source.name}”(TimedOut)超时,可能因为访问太频繁。将忽略。`,
`访问“${source.name}”(Timeout)超时,可能因为访问太频繁。将忽略。`,
{ plugin: 'cli' },
)
} else {
Expand All @@ -37,6 +45,10 @@ export function verbose(hook: HookCollectionType) {
plugin: 'cli',
})
}
logger.debug(`结束获取“${source.name}”。`, {
plugin: 'cli',
hook: 'fetch_each.after',
})
})
hook.after('update', (_result, { write_json_path }) => {
logger.info(`已按需保存到“${write_json_path}”。`, { plugin: 'cli' })
Expand Down
48 changes: 48 additions & 0 deletions src/plugin/retry/index.ts
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
}
})
}

0 comments on commit 366ab51

Please sign in to comment.