mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Refactor: drop Bun.nanoseconds & Bun.sleep
This commit is contained in:
parent
b1481c87f2
commit
e0e79c9fe5
@ -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);
|
||||
|
||||
@ -4,8 +4,6 @@ import picocolors from 'picocolors';
|
||||
const SPAN_STATUS_START = 0;
|
||||
const SPAN_STATUS_END = 1;
|
||||
|
||||
const NUM_OF_MS_IN_NANOSEC = 1_000_000;
|
||||
|
||||
const spanTag = Symbol('span');
|
||||
|
||||
export interface TraceResult {
|
||||
@ -36,7 +34,7 @@ export interface Span {
|
||||
}
|
||||
|
||||
export const createSpan = (name: string, parentTraceResult?: TraceResult): Span => {
|
||||
const start = Bun.nanoseconds();
|
||||
const start = performance.now();
|
||||
|
||||
let curTraceResult: TraceResult;
|
||||
|
||||
@ -45,7 +43,7 @@ export const createSpan = (name: string, parentTraceResult?: TraceResult): Span
|
||||
} else {
|
||||
curTraceResult = {
|
||||
name,
|
||||
start: start / NUM_OF_MS_IN_NANOSEC,
|
||||
start,
|
||||
end: 0,
|
||||
children: []
|
||||
};
|
||||
@ -58,9 +56,9 @@ export const createSpan = (name: string, parentTraceResult?: TraceResult): Span
|
||||
if (status === SPAN_STATUS_END) {
|
||||
throw new Error(`span already stopped: ${name}`);
|
||||
}
|
||||
const end = time ?? Bun.nanoseconds();
|
||||
const end = time ?? performance.now();
|
||||
|
||||
curTraceResult.end = end / NUM_OF_MS_IN_NANOSEC;
|
||||
curTraceResult.end = end;
|
||||
|
||||
status = SPAN_STATUS_END;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user