Refactor: Bun.peek polyfill for Node.js

This commit is contained in:
SukkaW 2024-07-23 15:46:38 +08:00
parent eed0d58697
commit b1481c87f2
6 changed files with 20 additions and 4 deletions

View File

@ -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();

View File

@ -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
View 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;

View File

@ -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) => {

BIN
bun.lockb

Binary file not shown.

View File

@ -23,6 +23,7 @@
"csv-parse": "^5.5.6",
"fast-cidr-tools": "^0.2.5",
"fdir": "^6.1.1",
"foxact": "^0.2.36",
"mnemonist": "^0.39.8",
"path-scurry": "^1.11.1",
"picocolors": "^1.0.1",
@ -47,5 +48,6 @@
},
"resolutions": {
"has": "npm:@nolyfill/has@latest"
}
},
"packageManager": "pnpm@9.5.0+sha512.140036830124618d624a2187b50d04289d5a087f326c9edfc0ccd733d76c4f52c3a313d4fc148794a2a9d81553016004e6742e8cf850670268a7387fc220c903"
}