mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Refactor: drop Bun.nanoseconds & Bun.sleep
This commit is contained in:
@@ -6,6 +6,7 @@ import { mkdirSync } from 'fs';
|
||||
import picocolors from 'picocolors';
|
||||
import { fastStringArrayJoin } from './misc';
|
||||
import { peek } from 'bun';
|
||||
import { performance } from 'perf_hooks';
|
||||
|
||||
const identity = (x: any) => x;
|
||||
|
||||
@@ -78,7 +79,7 @@ export class Cache<S = string> {
|
||||
tableName = 'cache',
|
||||
type
|
||||
}: CacheOptions<S> = {}) {
|
||||
const start = Bun.nanoseconds();
|
||||
const start = performance.now();
|
||||
|
||||
this.cachePath = cachePath;
|
||||
mkdirSync(this.cachePath, { recursive: true });
|
||||
@@ -120,7 +121,7 @@ export class Cache<S = string> {
|
||||
this.db.exec('VACUUM;');
|
||||
}
|
||||
|
||||
const end = Bun.nanoseconds();
|
||||
const end = performance.now();
|
||||
console.log(`${picocolors.gray(`[${((end - start) / 1e6).toFixed(3)}ms]`)} cache initialized from ${this.cachePath}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import picocolors from 'picocolors';
|
||||
import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
|
||||
import { setTimeout } from 'timers/promises';
|
||||
|
||||
class CustomAbortError extends Error {
|
||||
public readonly name = 'AbortError';
|
||||
@@ -15,7 +16,7 @@ const sleepWithAbort = (ms: number, signal: AbortSignal) => new Promise<void>((r
|
||||
function stop(this: AbortSignal) { reject(this.reason as Error); }
|
||||
|
||||
signal.addEventListener('abort', stop, { once: true });
|
||||
Bun.sleep(ms).then(resolve).catch(reject).finally(() => signal.removeEventListener('abort', stop));
|
||||
setTimeout(ms, undefined, { ref: false }).then(resolve).catch(reject).finally(() => signal.removeEventListener('abort', stop));
|
||||
});
|
||||
|
||||
export async function fetchAssets(url: string, fallbackUrls: string[] | readonly string[]) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import retry from 'async-retry';
|
||||
import picocolors from 'picocolors';
|
||||
import { setTimeout } from 'timers/promises';
|
||||
|
||||
// retry settings
|
||||
const MIN_TIMEOUT = 10;
|
||||
@@ -84,7 +85,7 @@ function createFetchRetry($fetch: typeof fetch): FetchWithRetry {
|
||||
if (retryAfter > retryOpts.maxRetryAfter) {
|
||||
return res;
|
||||
}
|
||||
await Bun.sleep(retryAfter * 1e3);
|
||||
await setTimeout(retryAfter * 1e3, undefined, { ref: false });
|
||||
}
|
||||
}
|
||||
throw new ResponseError(res);
|
||||
|
||||
Reference in New Issue
Block a user