1'use strict' 2 3/* istanbul ignore file: only for Node 12 */ 4 5const { kConnected, kSize } = require('../core/symbols') 6 7class CompatWeakRef { 8 constructor (value) { 9 this.value = value 10 } 11 12 deref () { 13 return this.value[kConnected] === 0 && this.value[kSize] === 0 14 ? undefined 15 : this.value 16 } 17} 18 19class CompatFinalizer { 20 constructor (finalizer) { 21 this.finalizer = finalizer 22 } 23 24 register (dispatcher, key) { 25 if (dispatcher.on) { 26 dispatcher.on('disconnect', () => { 27 if (dispatcher[kConnected] === 0 && dispatcher[kSize] === 0) { 28 this.finalizer(key) 29 } 30 }) 31 } 32 } 33} 34 35module.exports = function () { 36 // FIXME: remove workaround when the Node bug is fixed 37 // https://github.com/nodejs/node/issues/49344#issuecomment-1741776308 38 if (process.env.NODE_V8_COVERAGE) { 39 return { 40 WeakRef: CompatWeakRef, 41 FinalizationRegistry: CompatFinalizer 42 } 43 } 44 return { 45 WeakRef: global.WeakRef || CompatWeakRef, 46 FinalizationRegistry: global.FinalizationRegistry || CompatFinalizer 47 } 48} 49