From b386e8a384b4440184bcaa3c189b9a81911736ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 28 Sep 2024 20:53:14 +0200 Subject: [PATCH] fix: fix gc() implementation on Deno runtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/lib.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/lib.js b/src/lib.js index b068b29..9b47b7a 100644 --- a/src/lib.js +++ b/src/lib.js @@ -61,7 +61,15 @@ export const noColor = (() => { export const writeFileSync = await (async () => { return await { - unknown: () => emptyFunction, + unknown: async () => { + try { + await import('node:fs') + + return (await import('node:fs')).writeFileSync + } catch { + return emptyFunction + } + }, browser: () => emptyFunction, node: async () => (await import('node:fs')).writeFileSync, deno: () => Deno.writeTextFileSync, @@ -88,12 +96,12 @@ export const spawnSync = (() => { }[runtime]() })() -const conditionalGC = () => - typeof globalThis.gc === 'function' ? () => globalThis.gc() : emptyFunction - export const gc = (() => { return { - unknown: conditionalGC(), + unknown: () => + typeof globalThis.gc === 'function' + ? () => globalThis.gc() + : emptyFunction, browser: () => { try { globalThis.$262.gc() @@ -108,7 +116,10 @@ export const gc = (() => { const gc = runInNewContext('gc') gc() }, - deno: conditionalGC(), + deno: () => + typeof globalThis.gc === 'function' + ? () => globalThis.gc() + : emptyFunction, bun: () => () => Bun.gc(true), }[runtime]() })()