Suppose you have a Node package thing that defines
var theThing = {}
exports.theThing = theThing
Package consumer does this:
var thing1 = require('thing')
var thing2 = require('thing')
assert(thing1.theThing === thing2.theThing)
This appears to pass a quick test. Presumably when Node executes the second require, it notices thing is already instantiated, and quietly returns a pointer to the existing instance?
Now suppose the following happens:
-
Two versions of
thingare published on npm, 1.0.0 and 2.0.0. -
Two packages,
consumer1andconsumer2, are published on npm; they have dependencies on versions 1.0.0 and 2.0.0 respectively ofthing, and each re-exportstheThing. -
Another packager
endConsumerhas dependency onconsumer1andconsumer2.
So if endConsumer tries this test:
assert(consumer1.theThing === consumer2.theThing)
That must fail, right? At this point, Node cannot reuse one instance of thing because two distinct versions of the package are required?
I would guess you should end up being able to rely on a singleton being unique everywhere in a process, if and only if all transitive dependencies on the package exporting that singleton, are on the exact same version of that package. But is that guess correct, or is the actual rule something else?
via rwallace
No comments:
Post a Comment