Socket
Socket
Sign inDemoInstall

on-exit-leak-free

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

on-exit-leak-free - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

67

index.js
'use strict'
function genWrap (wraps, ref, fn, event) {
function wrap () {
let refs = []
const registry = new FinalizationRegistry(clear)
function install () {
if (refs.length > 0) {
return
}
process.on('exit', onExit)
}
function uninstall () {
if (refs.length > 0) {
return
}
process.removeListener('exit', onExit)
}
function onExit () {
callRefs('exit')
}
function callRefs (event) {
for (const ref of refs) {
const obj = ref.deref()
const fn = ref.fn
// This should always happen, however GC is

@@ -13,19 +37,8 @@ // undeterministic so it might not happen.

}
wraps[event] = wrap
// We are allowing infinite amount of listeners added
// by on-exit-leak-free
const maxListeners = process.getMaxListeners()
if (process.listenerCount(event) === maxListeners) {
process.setMaxListeners(maxListeners + 1)
}
process.once(event, wrap)
}
const registry = new FinalizationRegistry(clear)
const map = new WeakMap()
function clear (wraps) {
process.removeListener('exit', wraps.exit)
process.removeListener('beforeExit', wraps.beforeExit)
function clear (ref) {
const index = refs.indexOf(ref)
refs.splice(index, index + 1)
uninstall()
}

@@ -37,19 +50,17 @@

}
install()
const ref = new WeakRef(obj)
ref.fn = fn
const wraps = {}
map.set(obj, wraps)
registry.register(obj, wraps)
genWrap(wraps, ref, fn, 'exit')
genWrap(wraps, ref, fn, 'beforeExit')
registry.register(obj, ref)
refs.push(ref)
}
function unregister (obj) {
const wraps = map.get(obj)
map.delete(obj)
if (wraps) {
clear(wraps)
}
registry.unregister(obj)
refs = refs.filter((ref) => {
const _obj = ref.deref()
return _obj && _obj !== obj
})
uninstall()
}

@@ -56,0 +67,0 @@

{
"name": "on-exit-leak-free",
"version": "1.0.0",
"version": "2.0.0",
"description": "Execute a function on exit without leaking memory, allowing all objects to be garbage collected",

@@ -31,5 +31,5 @@ "main": "index.js",

"snazzy": "^9.0.0",
"standard": "^16.0.3",
"standard": "^17.0.0",
"tap": "^16.0.0"
}
}
# on-exit-leak-free
Execute a function on exit without leaking memory, allowing all objects to be garbage collected.
Listen to both `'beforeExit'` and `'exit'`, executing the given function only once.
This module helps dispose of an object gracefully when the Node.js process exits.
It executes a function with a given parameter
on [`'exit'`](https://nodejs.org/api/process.html#event-exit) without leaking memory,
cleaning things up appropriately if the object is garbage collected.
Requires `WeakRef` and `FinalizationRegistry`, i.e. use Node v14+.
Requires `WeakRef`, `WeakMap` and `FinalizationRegistry`, i.e. use Node v14+.
## Install

@@ -10,0 +11,0 @@

@@ -12,4 +12,3 @@ 'use strict'

'gc-not-close.js',
'unregister.js',
'beforeExit.js'
'unregister.js'
]

@@ -16,0 +15,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc