Socket
Socket
Sign inDemoInstall

on-exit-leak-free

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    on-exit-leak-free

Execute a function on exit without leaking memory, allowing all objects to be garbage collected


Version published
Maintainers
1
Install size
8.33 kB
Created

Readme

Source

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.

Requires WeakRef, WeakMap and FinalizationRegistry, i.e. use Node v14+.

Install

npm i on-exit-leak-free

Example

'use strict'

const { register, unregister } = require('on-exit-leak-free')
const assert = require('assert')

function setup () {
  // This object can be safely garbage collected,
  // and the resulting shutdown function will not be called.
  // There are no leaks.
  const obj = { foo: 'bar' }
  register(obj, shutdown)
  // call unregister(obj) to remove
}

let shutdownCalled = false

// Please make sure that the function passed to register()
// does not create a closure around unnecessary objects.
function shutdown (obj, eventName) {
  console.log(eventName) // beforeExit
  shutdownCalled = true
  assert.strictEqual(obj.foo, 'bar')
}

setup()

process.on('exit', function () {
  assert.strictEqual(shutdownCalled, true)
})

License

MIT

Keywords

FAQs

Last updated on 23 Jul 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc