Skip to content

Commit

Permalink
fix: fix gc() implementation on Deno runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Sep 28, 2024
1 parent 893f960 commit b386e8a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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]()
})()
Expand Down

0 comments on commit b386e8a

Please sign in to comment.