-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d165e9
commit 692e9c2
Showing
3 changed files
with
57 additions
and
5 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 |
---|---|---|
@@ -1,3 +1,46 @@ | ||
import { Agent } from 'undici' | ||
import { Agent, fetch as f, setGlobalDispatcher } from 'undici' | ||
|
||
export const getAgent = () => new Agent({ connect: { timeout: 120_000 } }) | ||
import { wait } from './common' | ||
|
||
setGlobalDispatcher(new Agent({ connect: { timeout: 120_000 } })) | ||
|
||
type Job = { | ||
url: string | ||
p: (data: unknown) => void | ||
} | ||
|
||
let jobs: Job[] = [] | ||
|
||
export const fetch = async (url: string) => { | ||
if (url.includes('music_tag')) { | ||
return f(url).then(res => res.json()) | ||
} | ||
const promise = new Promise<unknown>(resolve => jobs.push({ url, p: resolve })) | ||
return promise | ||
} | ||
|
||
export const dispatch = () => jobs[Math.floor(Math.random() * jobs.length)].url | ||
|
||
export const receipt = (url: string, data: any) => { | ||
if (data.code === 0) { | ||
jobs = jobs.filter(job => { | ||
if (job.url === url) { | ||
job.p(data) | ||
return false | ||
} | ||
return true | ||
}) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
while (true) { | ||
await wait(1000 * 10) | ||
const url = dispatch() | ||
if (url) { | ||
const res = await f(url) | ||
const data = await res.json() as any | ||
receipt(url, data) | ||
} | ||
} |
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