mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-28 17:41:54 +08:00
Refactor: Bun.peek polyfill for Node.js
This commit is contained in:
@@ -13,6 +13,7 @@ import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
|
||||
import { TTL, deserializeArray, fsFetchCache, serializeArray } from './lib/cache-filesystem';
|
||||
|
||||
import { createTrie } from './lib/trie';
|
||||
import { peek } from './lib/bun';
|
||||
|
||||
const s = new Sema(2);
|
||||
|
||||
@@ -239,7 +240,7 @@ export const buildSpeedtestDomainSet = task(import.meta.main, import.meta.path)(
|
||||
const timer = setTimeout(() => {
|
||||
console.error(picocolors.red('Task timeout!'));
|
||||
Object.entries(pMap).forEach(([name, p]) => {
|
||||
console.log(`[${name}]`, Bun.peek.status(p));
|
||||
console.log(`[${name}]`, peek.status(p));
|
||||
});
|
||||
|
||||
resolve();
|
||||
|
||||
@@ -33,7 +33,6 @@ export const downloadPreviousBuild = task(import.meta.main, import.meta.path)(as
|
||||
buildOutputList.push(line);
|
||||
|
||||
if (!isCI) {
|
||||
// Bun.file().exists() doesn't check directory
|
||||
if (!existsSync(path.join(import.meta.dir, '..', line))) {
|
||||
flag = flag & ~ALL_FILES_EXISTS;
|
||||
}
|
||||
|
||||
13
Build/lib/bun.ts
Normal file
13
Build/lib/bun.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
interface Peek {
|
||||
<T = undefined>(promise: T | Promise<T>): Promise<T> | T,
|
||||
status<T = undefined>(
|
||||
promise: T | Promise<T>,
|
||||
): 'pending' | 'fulfilled' | 'rejected' | 'unknown'
|
||||
}
|
||||
|
||||
const noopPeek = <T = undefined>(_: Promise<T>) => _;
|
||||
noopPeek.status = () => 'unknown';
|
||||
|
||||
export const peek: Peek = typeof Bun !== 'undefined'
|
||||
? Bun.peek
|
||||
: noopPeek as Peek;
|
||||
@@ -5,6 +5,7 @@ import path from 'path';
|
||||
import { mkdirSync } from 'fs';
|
||||
import picocolors from 'picocolors';
|
||||
import { fastStringArrayJoin } from './misc';
|
||||
import { peek } from 'bun';
|
||||
|
||||
const identity = (x: any) => x;
|
||||
|
||||
@@ -178,7 +179,7 @@ export class Cache<S = string> {
|
||||
const serializer = 'serializer' in opt ? opt.serializer : identity;
|
||||
|
||||
const promise = fn();
|
||||
const peeked = Bun.peek(promise);
|
||||
const peeked = peek(promise);
|
||||
|
||||
if (peeked === promise) {
|
||||
return promise.then((value) => {
|
||||
|
||||
Reference in New Issue
Block a user