{"_id":"weak-napi","_rev":"161184","name":"weak-napi","description":"Make weak references to JavaScript Objects.","dist-tags":{"latest":"2.0.2"},"maintainers":[{"name":"addaleax","email":"anna@addaleax.net"}],"time":{"modified":"2021-06-03T11:40:26.000Z","created":"2017-11-17T01:03:53.675Z","2.0.2":"2020-05-29T17:26:20.330Z","2.0.1":"2020-05-05T13:16:17.012Z","2.0.0":"2020-04-22T19:42:32.323Z","1.0.3":"2019-04-10T11:24:15.597Z","1.0.2":"2018-01-02T19:50:42.869Z","1.0.1":"2017-11-24T02:55:04.846Z","1.0.0":"2017-11-17T01:03:53.675Z"},"users":{"bcowgi11":true},"author":{"name":"Anna Henningsen","email":"anna@addaleax.net"},"repository":{"type":"git","url":"git://github.com/node-ffi-napi/weak-napi.git"},"versions":{"2.0.2":{"author":{"name":"Anna Henningsen","email":"anna@addaleax.net"},"contributors":[{"name":"Ben Noordhuis","email":"info@bnoordhuis.nl"},{"name":"Nathan Rajlich","email":"nathan@tootallnate.net"}],"name":"weak-napi","license":"MIT","description":"Make weak references to JavaScript Objects.","keywords":["weak","reference","js","javascript","object","function","callback","napi"],"version":"2.0.2","repository":{"type":"git","url":"git://github.com/node-ffi-napi/weak-napi.git"},"main":"lib/weak.js","scripts":{"test":"nyc mocha --expose-gc","install":"node-gyp-build","prebuild":"prebuildify --napi --tag-armv --tag-uv","prepack":"prebuildify-ci download && ([ $(ls prebuilds | wc -l) = '5' ] || (echo 'Some prebuilds are missing'; exit 1))"},"dependencies":{"node-addon-api":"^3.0.0","node-gyp-build":"^4.2.1","setimmediate-napi":"^1.0.3"},"devDependencies":{"mocha":"^7.1.1","nyc":"^15.0.0","prebuildify":"^3.0.4","prebuildify-ci":"^1.0.5"},"gitHead":"463f7920dcf39e804cf5b47a6a68d149c01e6e52","bugs":{"url":"https://github.com/node-ffi-napi/weak-napi/issues"},"homepage":"https://github.com/node-ffi-napi/weak-napi#readme","_id":"weak-napi@2.0.2","_nodeVersion":"15.0.0-pre","_npmVersion":"6.14.5","dist":{"shasum":"40662d0931498397979edb38a571409f5afce6d3","size":648073,"noattachment":false,"key":"/weak-napi/-/weak-napi-2.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/weak-napi/download/weak-napi-2.0.2.tgz"},"maintainers":[{"name":"addaleax","email":"anna@addaleax.net"}],"_npmUser":{"name":"addaleax","email":"anna@addaleax.net"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/weak-napi_2.0.2_1590773180174_0.6938429836928983"},"_hasShrinkwrap":false,"publish_time":1590773180330,"_cnpm_publish_time":1590773180330},"2.0.1":{"author":{"name":"Anna Henningsen","email":"anna@addaleax.net"},"contributors":[{"name":"Ben Noordhuis","email":"info@bnoordhuis.nl"},{"name":"Nathan Rajlich","email":"nathan@tootallnate.net"}],"name":"weak-napi","license":"MIT","description":"Make weak references to JavaScript Objects.","keywords":["weak","reference","js","javascript","object","function","callback","napi"],"version":"2.0.1","repository":{"type":"git","url":"git://github.com/node-ffi-napi/weak-napi.git"},"main":"lib/weak.js","scripts":{"test":"nyc mocha --expose-gc","install":"node-gyp-build","prebuild":"prebuildify --napi","prepack":"prebuildify-ci download && ([ $(ls prebuilds | wc -l) = '4' ] || (echo 'Some prebuilds are missing'; exit 1))"},"dependencies":{"node-addon-api":"^3.0.0","node-gyp-build":"^4.2.1","setimmediate-napi":"^1.0.3"},"devDependencies":{"mocha":"^7.1.1","nyc":"^15.0.0","prebuildify":"^3.0.4","prebuildify-ci":"^1.0.5"},"gitHead":"183e2c4136ebada251529aacea4da5cc6249a6ff","bugs":{"url":"https://github.com/node-ffi-napi/weak-napi/issues"},"homepage":"https://github.com/node-ffi-napi/weak-napi#readme","_id":"weak-napi@2.0.1","_nodeVersion":"15.0.0-pre","_npmVersion":"6.14.4","dist":{"shasum":"406c69e4b6924b1b336e6c93e150334bd6e6e43e","size":600131,"noattachment":false,"key":"/weak-napi/-/weak-napi-2.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/weak-napi/download/weak-napi-2.0.1.tgz"},"maintainers":[{"name":"addaleax","email":"anna@addaleax.net"}],"_npmUser":{"name":"addaleax","email":"anna@addaleax.net"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/weak-napi_2.0.1_1588684576816_0.5547523835057899"},"_hasShrinkwrap":false,"publish_time":1588684577012,"_cnpm_publish_time":1588684577012},"2.0.0":{"author":{"name":"Anna Henningsen","email":"anna@addaleax.net"},"contributors":[{"name":"Ben Noordhuis","email":"info@bnoordhuis.nl"},{"name":"Nathan Rajlich","email":"nathan@tootallnate.net"}],"name":"weak-napi","license":"MIT","description":"Make weak references to JavaScript Objects.","keywords":["weak","reference","js","javascript","object","function","callback","napi"],"version":"2.0.0","repository":{"type":"git","url":"git://github.com/node-ffi-napi/weak-napi.git"},"main":"lib/weak.js","scripts":{"test":"nyc mocha --expose-gc","install":"node-gyp-build","prebuild":"prebuildify --napi","prepack":"prebuildify-ci download && ([ $(ls prebuilds | wc -l) = '4' ] || (echo 'Some prebuilds are missing'; exit 1))"},"dependencies":{"node-addon-api":"github:nodejs/node-addon-api#fedc8195e3a33e32a8a6b4f8b8285cdc6a104a44","setimmediate-napi":"^1.0.3","node-gyp-build":"^4.2.1"},"devDependencies":{"mocha":"^7.1.1","nyc":"^15.0.0","prebuildify":"^3.0.4","prebuildify-ci":"^1.0.5"},"gitHead":"2c84d89222c0cccf1ebc9d2758c916c2e26ae7ee","bugs":{"url":"https://github.com/node-ffi-napi/weak-napi/issues"},"homepage":"https://github.com/node-ffi-napi/weak-napi#readme","_id":"weak-napi@2.0.0","_nodeVersion":"14.0.0","_npmVersion":"6.14.4","dist":{"shasum":"c653cea4b84fe17e838e4026a5ca0f274ad2b954","size":600166,"noattachment":false,"key":"/weak-napi/-/weak-napi-2.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/weak-napi/download/weak-napi-2.0.0.tgz"},"maintainers":[{"name":"addaleax","email":"anna@addaleax.net"}],"_npmUser":{"name":"addaleax","email":"anna@addaleax.net"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/weak-napi_2.0.0_1587584552133_0.7782774795963112"},"_hasShrinkwrap":false,"publish_time":1587584552323,"_cnpm_publish_time":1587584552323},"1.0.3":{"author":{"name":"Anna Henningsen","email":"anna@addaleax.net"},"contributors":[{"name":"Ben Noordhuis","email":"info@bnoordhuis.nl"},{"name":"Nathan Rajlich","email":"nathan@tootallnate.net"}],"name":"weak-napi","license":"MIT","description":"Make weak references to JavaScript Objects.","keywords":["weak","reference","js","javascript","object","function","callback","napi"],"version":"1.0.3","repository":{"type":"git","url":"git://github.com/node-ffi-napi/weak-napi.git"},"main":"lib/weak.js","scripts":{"test":"nyc mocha -gc","install":"node-gyp rebuild"},"dependencies":{"bindings":"^1.3.0","node-addon-api":"^1.1.0","setimmediate-napi":"^1.0.3"},"devDependencies":{"mocha":"^5.0.0","nyc":"^11.3.0"},"gypfile":true,"gitHead":"3efed548b5cd5ec2f3f82858eeae8d9bbd5d26e6","bugs":{"url":"https://github.com/node-ffi-napi/weak-napi/issues"},"homepage":"https://github.com/node-ffi-napi/weak-napi#readme","_id":"weak-napi@1.0.3","_npmVersion":"6.4.1","_nodeVersion":"10.15.3","_npmUser":{"name":"addaleax","email":"anna@addaleax.net"},"dist":{"shasum":"ff4dfa818db1c509ba4166530b42414ef74cbba6","size":9407,"noattachment":false,"key":"/weak-napi/-/weak-napi-1.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/weak-napi/download/weak-napi-1.0.3.tgz"},"maintainers":[{"name":"addaleax","email":"anna@addaleax.net"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/weak-napi_1.0.3_1554895455498_0.2927942326764752"},"_hasShrinkwrap":false,"publish_time":1554895455597,"_cnpm_publish_time":1554895455597},"1.0.2":{"author":{"name":"Anna Henningsen","email":"anna@addaleax.net"},"contributors":[{"name":"Ben Noordhuis","email":"info@bnoordhuis.nl"},{"name":"Nathan Rajlich","email":"nathan@tootallnate.net"}],"name":"weak-napi","license":"MIT","description":"Make weak references to JavaScript Objects.","keywords":["weak","reference","js","javascript","object","function","callback","napi"],"version":"1.0.2","repository":{"type":"git","url":"git://github.com/node-ffi-napi/weak-napi.git"},"main":"lib/weak.js","scripts":{"test":"nyc mocha -gc","install":"node-gyp rebuild"},"dependencies":{"bindings":"^1.3.0","node-addon-api":"^1.1.0","setimmediate-napi":"^1.0.3"},"devDependencies":{"mocha":"^4.0.1","nyc":"^11.3.0"},"gypfile":true,"gitHead":"897f607b5b9c30d68d2a8b25a816f388807ac463","bugs":{"url":"https://github.com/node-ffi-napi/weak-napi/issues"},"homepage":"https://github.com/node-ffi-napi/weak-napi#readme","_id":"weak-napi@1.0.2","_npmVersion":"5.6.0","_nodeVersion":"10.0.0-pre","_npmUser":{"name":"addaleax","email":"anna@addaleax.net"},"dist":{"shasum":"74f1228caa8e14d5361af6593f32cf35c085b936","size":9404,"noattachment":false,"key":"/weak-napi/-/weak-napi-1.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/weak-napi/download/weak-napi-1.0.2.tgz"},"maintainers":[{"name":"addaleax","email":"anna@addaleax.net"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/weak-napi-1.0.2.tgz_1514922640755_0.9529211923945695"},"directories":{},"publish_time":1514922642869,"_hasShrinkwrap":false,"_cnpm_publish_time":1514922642869},"1.0.1":{"author":{"name":"Anna Henningsen","email":"anna@addaleax.net"},"contributors":[{"name":"Ben Noordhuis","email":"info@bnoordhuis.nl"},{"name":"Nathan Rajlich","email":"nathan@tootallnate.net"}],"name":"weak-napi","license":"MIT","description":"Make weak references to JavaScript Objects.","keywords":["weak","reference","js","javascript","object","function","callback","napi"],"version":"1.0.1","repository":{"type":"git","url":"git://github.com/node-ffi-napi/weak-napi.git"},"main":"lib/weak.js","scripts":{"test":"nyc mocha -gc","install":"node-gyp rebuild"},"dependencies":{"bindings":"^1.3.0","node-addon-api":"^1.1.0","setimmediate-napi":"^1.0.3"},"devDependencies":{"mocha":"^4.0.1","nyc":"^11.3.0"},"gypfile":true,"gitHead":"1e139b6676ce8e6ff58d102e4fffdcb28ea1681e","bugs":{"url":"https://github.com/node-ffi-napi/weak-napi/issues"},"homepage":"https://github.com/node-ffi-napi/weak-napi#readme","_id":"weak-napi@1.0.1","_npmVersion":"5.5.1","_nodeVersion":"10.0.0-pre","_npmUser":{"name":"addaleax","email":"anna@addaleax.net"},"dist":{"shasum":"53085e46aaeec6b79aa8cce4340680f94a8bae0d","size":9357,"noattachment":false,"key":"/weak-napi/-/weak-napi-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/weak-napi/download/weak-napi-1.0.1.tgz"},"maintainers":[{"name":"addaleax","email":"anna@addaleax.net"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/weak-napi-1.0.1.tgz_1511492103916_0.9063464603386819"},"directories":{},"publish_time":1511492104846,"_hasShrinkwrap":false,"_cnpm_publish_time":1511492104846},"1.0.0":{"author":{"name":"Anna Henningsen","email":"anna@addaleax.net"},"contributors":[{"name":"Ben Noordhuis","email":"info@bnoordhuis.nl"},{"name":"Nathan Rajlich","email":"nathan@tootallnate.net"}],"name":"weak-napi","license":"MIT","description":"Make weak references to JavaScript Objects.","keywords":["weak","reference","js","javascript","object","function","callback","napi"],"version":"1.0.0","repository":{"type":"git","url":"git://github.com/node-ffi-napi/weak-napi.git"},"main":"lib/weak.js","scripts":{"test":"nyc mocha -gc","install":"node-gyp rebuild"},"dependencies":{"bindings":"^1.3.0","node-addon-api":"https://github.com/nodejs/node-addon-api/archive/82656bb.tar.gz"},"devDependencies":{"mocha":"^4.0.1","nyc":"^11.3.0"},"gypfile":true,"gitHead":"8ac85ec455738beb3299bf3872aae54ff5a60ebc","bugs":{"url":"https://github.com/node-ffi-napi/weak-napi/issues"},"homepage":"https://github.com/node-ffi-napi/weak-napi#readme","_id":"weak-napi@1.0.0","_npmVersion":"5.5.1","_nodeVersion":"10.0.0-pre","_npmUser":{"name":"addaleax","email":"anna@addaleax.net"},"dist":{"shasum":"3e94815703fae3bd66b2c830ef3f7450a2582958","size":8291,"noattachment":false,"key":"/weak-napi/-/weak-napi-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/weak-napi/download/weak-napi-1.0.0.tgz"},"maintainers":[{"name":"addaleax","email":"anna@addaleax.net"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/weak-napi-1.0.0.tgz_1510880632819_0.24534549354575574"},"directories":{},"publish_time":1510880633675,"_cnpm_publish_time":1510880633675,"_hasShrinkwrap":false}},"readme":"weak-napi\n=========\n### Make weak references to JavaScript Objects.\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/node-ffi-napi/weak-napi.svg)](https://greenkeeper.io/)\n\n[![NPM Version](https://img.shields.io/npm/v/weak-napi.svg?style=flat)](https://npmjs.org/package/weak-napi)\n[![NPM Downloads](https://img.shields.io/npm/dm/weak-napi.svg?style=flat)](https://npmjs.org/package/weak-napi)\n[![Build Status](https://travis-ci.org/node-ffi-napi/weak-napi.svg?style=flat&branch=master)](https://travis-ci.org/node-ffi-napi/weak-napi?branch=master)\n[![Coverage Status](https://coveralls.io/repos/node-ffi-napi/weak-napi/badge.svg?branch=master)](https://coveralls.io/r/node-ffi-napi/weak-napi?branch=master)\n[![Dependency Status](https://david-dm.org/node-ffi-napi/weak-napi.svg?style=flat)](https://david-dm.org/node-ffi-napi/weak-napi)\n\nOn certain rarer occasions, you run into the need to be notified when a JavaScript\nobject is going to be garbage collected. This feature is exposed to V8's C++ API,\nbut not to JavaScript.\n\nThat's where `weak-napi` comes in! This module exports the JS engine's GC tracking\nfunctionality to JavaScript. This allows you to create weak references, and\noptionally attach a callback function to any arbitrary JS object. The callback\nfunction will be invoked right after the Object is garbage collected (i.e. after\nthere are no more remaining references to the Object in JS-land).\n\nThis module can, for example, be used for debugging; to determine whether or not\nan Object is being garbage collected as it should.\nTake a look at the example below for commented walkthrough scenario.\n\n\nInstallation\n------------\n\nInstall with `npm`:\n\n``` bash\n$ npm install weak-napi\n```\n\nDifferences from node-weak\n--------------------------\n\nThis module exports the full `node-weak` API. The main differences are:\n\n- This module uses N-API! Yay. That’s a good thing – you don’t need to worry\n  about re-compiling your code anymore when upgrading Node.\n- GC callbacks are invoked at a suitable time (after the actual GC run has\n  finished). **This is nice because it means your process won’t\n  potentially crash.**\n- This module works on Node 6+ only, since it uses a `Proxy` object to give a\n  more complete referral of properties.\n- `isNearDeath()` is not supported (always returns false).\n\nThat’s it!\n\nExample\n-------\n\nHere's an example of calling a `cleanup()` function on a Object after it gets\ngarbage collected:\n\n``` js\nvar weak = require('weak-napi')\n\n// we are going to \"monitor\" this Object and invoke \"cleanup\"\n// before the object is garbage collected\nvar obj = {\n    a: true\n  , foo: 'bar'\n}\n\n// Here's where we set up the weak reference\nvar ref = weak(obj, function () {\n  // `this` inside the callback is the EventEmitter.\n  console.log('\"obj\" has been garbage collected!')\n})\n\n// While `obj` is alive, `ref` proxies everything to it, so:\nref.a   === obj.a\nref.foo === obj.foo\n\n// Clear out any references to the object, so that it will be GC'd at some point...\nobj = null\n\n//\n//// Time passes, and the garbage collector is run\n//\n\n// `callback()` above is called, and `ref` now acts like an empty object.\ntypeof ref.foo === 'undefined'\n```\n\n\nWeak Callback Function \"Best Practices\"\n---------------------------------------\n\nIt's important to be careful when using the \"callbacks\" feature of `weak-napi`,\notherwise you can end up in a situation where the watched object will never\nbe garbage collected.\n\nYou _should **not**_ define the callback function in the same scope as the\nobject that is being watched. It's often best to define the callback function\nat the highest scope possible (top-level being the best). Named functions\nwork really well for this:\n\n``` js\nvar http = require('http')\n  , weak = require('weak-napi')\n\nhttp.createServer(function (req, res) {\n  weak(req, gcReq)\n  weak(res, gcRes)\n  res.end('Hello World\\n')\n}).listen(3000)\n\nfunction gcReq () {\n  console.log('GC\\'d `req` object')\n}\n\nfunction gcRes () {\n  console.log('GC\\'d `res` object')\n}\n```\n\n\nAPI\n---\n\n### Weakref weak(Object obj [, Function callback])\n\nThe main exports is the function that creates the weak reference.\nThe first argument is the Object that should be monitored.\nThe Object can be a regular Object, an Array, a Function, a RegExp, or any of\nthe primitive types or constructor function created with `new`.\n\nOptionally, you can set a callback function to be invoked\nbefore the object is garbage collected.\n\n\n### Object weak.get(Weakref ref)\n\n`get()` returns the actual reference to the Object that this weak reference was\ncreated with. If this is called with a dead reference, `undefined` is returned.\n\n\n### Boolean weak.isDead(Weakref ref)\n\nChecks to see if `ref` is a dead reference. Returns `true` if the original Object\nhas already been GC'd, `false` otherwise.\n\n\n### Boolean weak.isNearDeath(Weakref ref)\n\n*Note* The N-API port of this module does not implement this and\nalways returns `false`.\n\n\n### Boolean weak.isWeakRef(Object obj)\n\nChecks to see if `obj` is \"weak reference\" instance. Returns `true` if the\npassed in object is a \"weak reference\", `false` otherwise.\n\n\n### EventEmitter weak.addCallback(Weakref ref, Function callback)\n\nAdds `callback` to the Array of callback functions that will be invoked before the\nObject gets garbage collected. The callbacks get executed in the order that they\nare added.\n\n\n### EventEmitter weak.removeCallback(Weakref ref, Function callback)\n\nRemoves `callback` from the Array of callback functions that will be invoked before\nthe Object gets garbage collected.\n\n\n### EventEmitter weak.removeCallbacks(Weakref ref)\n\nEmpties the Array of callback functions that will be invoked before the Object gets\ngarbage collected.\n\n\n### Array weak.callbacks(Weakref ref)\n\nReturns an Array that `ref` iterates through to invoke the GC callbacks. This\nutilizes node's `EventEmitter#listeners()` function and therefore returns a copy\nin node 0.10 and newer.\n","_attachments":{},"homepage":"https://github.com/node-ffi-napi/weak-napi#readme","bugs":{"url":"https://github.com/node-ffi-napi/weak-napi/issues"},"license":"MIT"}