Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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 2.0.0 to 2.1.0

test/fixtures/beforeExit.js

62

index.js
'use strict'
let refs = []
const refs = {
exit: [],
beforeExit: []
}
const functions = {
exit: onExit,
beforeExit: onBeforeExit
}
const registry = new FinalizationRegistry(clear)
function install () {
if (refs.length > 0) {
function install (event) {
if (refs[event].length > 0) {
return
}
process.on('exit', onExit)
process.on(event, functions[event])
}
function uninstall () {
if (refs.length > 0) {
function uninstall (event) {
if (refs[event].length > 0) {
return
}
process.removeListener('exit', onExit)
process.removeListener(event, functions[event])
}

@@ -25,4 +32,8 @@

function onBeforeExit () {
callRefs('beforeExit')
}
function callRefs (event) {
for (const ref of refs) {
for (const ref of refs[event]) {
const obj = ref.deref()

@@ -41,12 +52,14 @@ const fn = ref.fn

function clear (ref) {
const index = refs.indexOf(ref)
refs.splice(index, index + 1)
uninstall()
for (const event of ['exit', 'beforeExit']) {
const index = refs[event].indexOf(ref)
refs[event].splice(index, index + 1)
uninstall(event)
}
}
function register (obj, fn) {
function _register (event, obj, fn) {
if (obj === undefined) {
throw new Error('the object can\'t be undefined')
}
install()
install(event)
const ref = new WeakRef(obj)

@@ -56,12 +69,22 @@ ref.fn = fn

registry.register(obj, ref)
refs.push(ref)
refs[event].push(ref)
}
function register (obj, fn) {
_register('exit', obj, fn)
}
function registerBeforeExit (obj, fn) {
_register('beforeExit', obj, fn)
}
function unregister (obj) {
registry.unregister(obj)
refs = refs.filter((ref) => {
const _obj = ref.deref()
return _obj && _obj !== obj
})
uninstall()
for (const event of ['exit', 'beforeExit']) {
refs[event] = refs[event].filter((ref) => {
const _obj = ref.deref()
return _obj && _obj !== obj
})
uninstall(event)
}
}

@@ -71,3 +94,4 @@

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

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

@@ -30,2 +30,4 @@ # on-exit-leak-free

register(obj, shutdown)
// use registerBeforeExit(obj, shutdown) to execute the function only
// on beforeExit
// call unregister(obj) to remove

@@ -32,0 +34,0 @@ }

@@ -11,2 +11,3 @@ 'use strict'

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

@@ -13,0 +14,0 @@ 'unregister.js'

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