Browse Source

Refactor: `Bun.peek` polyfill for Node.js

SukkaW 1 year ago
parent
commit
b1481c87f2

+ 2 - 1
Build/build-speedtest-domainset.ts

@@ -13,6 +13,7 @@ import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
 import { TTL, deserializeArray, fsFetchCache, serializeArray } from './lib/cache-filesystem';
 import { TTL, deserializeArray, fsFetchCache, serializeArray } from './lib/cache-filesystem';
 
 
 import { createTrie } from './lib/trie';
 import { createTrie } from './lib/trie';
+import { peek } from './lib/bun';
 
 
 const s = new Sema(2);
 const s = new Sema(2);
 
 
@@ -239,7 +240,7 @@ export const buildSpeedtestDomainSet = task(import.meta.main, import.meta.path)(
     const timer = setTimeout(() => {
     const timer = setTimeout(() => {
       console.error(picocolors.red('Task timeout!'));
       console.error(picocolors.red('Task timeout!'));
       Object.entries(pMap).forEach(([name, p]) => {
       Object.entries(pMap).forEach(([name, p]) => {
-        console.log(`[${name}]`, Bun.peek.status(p));
+        console.log(`[${name}]`, peek.status(p));
       });
       });
 
 
       resolve();
       resolve();

+ 0 - 1
Build/download-previous-build.ts

@@ -33,7 +33,6 @@ export const downloadPreviousBuild = task(import.meta.main, import.meta.path)(as
         buildOutputList.push(line);
         buildOutputList.push(line);
 
 
         if (!isCI) {
         if (!isCI) {
-          // Bun.file().exists() doesn't check directory
           if (!existsSync(path.join(import.meta.dir, '..', line))) {
           if (!existsSync(path.join(import.meta.dir, '..', line))) {
             flag = flag & ~ALL_FILES_EXISTS;
             flag = flag & ~ALL_FILES_EXISTS;
           }
           }

+ 13 - 0
Build/lib/bun.ts

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

+ 2 - 1
Build/lib/cache-filesystem.ts

@@ -5,6 +5,7 @@ import path from 'path';
 import { mkdirSync } from 'fs';
 import { mkdirSync } from 'fs';
 import picocolors from 'picocolors';
 import picocolors from 'picocolors';
 import { fastStringArrayJoin } from './misc';
 import { fastStringArrayJoin } from './misc';
+import { peek } from 'bun';
 
 
 const identity = (x: any) => x;
 const identity = (x: any) => x;
 
 
@@ -178,7 +179,7 @@ export class Cache<S = string> {
       const serializer = 'serializer' in opt ? opt.serializer : identity;
       const serializer = 'serializer' in opt ? opt.serializer : identity;
 
 
       const promise = fn();
       const promise = fn();
-      const peeked = Bun.peek(promise);
+      const peeked = peek(promise);
 
 
       if (peeked === promise) {
       if (peeked === promise) {
         return promise.then((value) => {
         return promise.then((value) => {

BIN
bun.lockb


+ 3 - 1
package.json

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