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 picocolors from 'picocolors';
|
||||||
import { fastStringArrayJoin } from './misc';
|
import { fastStringArrayJoin } from './misc';
|
||||||
import { peek } from 'bun';
|
import { peek } from 'bun';
|
||||||
|
import { performance } from 'perf_hooks';
|
||||||
|
|
||||||
const identity = (x: any) => x;
|
const identity = (x: any) => x;
|
||||||
|
|
||||||
@ -78,7 +79,7 @@ export class Cache<S = string> {
|
|||||||
tableName = 'cache',
|
tableName = 'cache',
|
||||||
type
|
type
|
||||||
}: CacheOptions<S> = {}) {
|
}: CacheOptions<S> = {}) {
|
||||||
const start = Bun.nanoseconds();
|
const start = performance.now();
|
||||||
|
|
||||||
this.cachePath = cachePath;
|
this.cachePath = cachePath;
|
||||||
mkdirSync(this.cachePath, { recursive: true });
|
mkdirSync(this.cachePath, { recursive: true });
|
||||||
@ -120,7 +121,7 @@ export class Cache<S = string> {
|
|||||||
this.db.exec('VACUUM;');
|
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}`);
|
console.log(`${picocolors.gray(`[${((end - start) / 1e6).toFixed(3)}ms]`)} cache initialized from ${this.cachePath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
|
import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
|
||||||
|
import { setTimeout } from 'timers/promises';
|
||||||
|
|
||||||
class CustomAbortError extends Error {
|
class CustomAbortError extends Error {
|
||||||
public readonly name = 'AbortError';
|
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); }
|
function stop(this: AbortSignal) { reject(this.reason as Error); }
|
||||||
|
|
||||||
signal.addEventListener('abort', stop, { once: true });
|
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[]) {
|
export async function fetchAssets(url: string, fallbackUrls: string[] | readonly string[]) {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import retry from 'async-retry';
|
import retry from 'async-retry';
|
||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
|
import { setTimeout } from 'timers/promises';
|
||||||
|
|
||||||
// retry settings
|
// retry settings
|
||||||
const MIN_TIMEOUT = 10;
|
const MIN_TIMEOUT = 10;
|
||||||
@ -84,7 +85,7 @@ function createFetchRetry($fetch: typeof fetch): FetchWithRetry {
|
|||||||
if (retryAfter > retryOpts.maxRetryAfter) {
|
if (retryAfter > retryOpts.maxRetryAfter) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
await Bun.sleep(retryAfter * 1e3);
|
await setTimeout(retryAfter * 1e3, undefined, { ref: false });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new ResponseError(res);
|
throw new ResponseError(res);
|
||||||
|
|||||||
@ -4,8 +4,6 @@ import picocolors from 'picocolors';
|
|||||||
const SPAN_STATUS_START = 0;
|
const SPAN_STATUS_START = 0;
|
||||||
const SPAN_STATUS_END = 1;
|
const SPAN_STATUS_END = 1;
|
||||||
|
|
||||||
const NUM_OF_MS_IN_NANOSEC = 1_000_000;
|
|
||||||
|
|
||||||
const spanTag = Symbol('span');
|
const spanTag = Symbol('span');
|
||||||
|
|
||||||
export interface TraceResult {
|
export interface TraceResult {
|
||||||
@ -36,7 +34,7 @@ export interface Span {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const createSpan = (name: string, parentTraceResult?: TraceResult): Span => {
|
export const createSpan = (name: string, parentTraceResult?: TraceResult): Span => {
|
||||||
const start = Bun.nanoseconds();
|
const start = performance.now();
|
||||||
|
|
||||||
let curTraceResult: TraceResult;
|
let curTraceResult: TraceResult;
|
||||||
|
|
||||||
@ -45,7 +43,7 @@ export const createSpan = (name: string, parentTraceResult?: TraceResult): Span
|
|||||||
} else {
|
} else {
|
||||||
curTraceResult = {
|
curTraceResult = {
|
||||||
name,
|
name,
|
||||||
start: start / NUM_OF_MS_IN_NANOSEC,
|
start,
|
||||||
end: 0,
|
end: 0,
|
||||||
children: []
|
children: []
|
||||||
};
|
};
|
||||||
@ -58,9 +56,9 @@ export const createSpan = (name: string, parentTraceResult?: TraceResult): Span
|
|||||||
if (status === SPAN_STATUS_END) {
|
if (status === SPAN_STATUS_END) {
|
||||||
throw new Error(`span already stopped: ${name}`);
|
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;
|
status = SPAN_STATUS_END;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user