Created
September 21, 2017 21:46
-
-
Save iarna/846412c9e41064a0e472b7d7e40d51a4 to your computer and use it in GitHub Desktop.
instanceof checks, readable-stream and require-inject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const test = require('tap').test | |
const requireInject = require('require-inject') | |
const EventEmitter = require('events') | |
class NullSink extends EventEmitter { | |
write () { | |
return true | |
} | |
end () { | |
this.emit('prefinish') | |
this.emit('finish') | |
} | |
} | |
test('requireInject', t => { | |
t.plan(1) | |
t.doesNotThrow(() => { | |
// requireInject without any additional arguments loads the module but | |
// maintains the require cache exactly as it was prior to calling it. | |
// As such, while it returns a the PassThrough class from | |
// `readable-stream`, `readable-stream` and its dependencies will no | |
// longer be in the cache. | |
const stream = requireInject('readable-stream') | |
const PassThrough = stream.PassThrough | |
const pt = new PassThrough() | |
// The error has already actually happened at this point. When we | |
// construct a new object here the constructor chain fails to execute | |
// properly, due to the intanceof checks failing. | |
// WHY the instanceof check fails is not obvious to me because contrary | |
// to my expectations there is no deferred loading… | |
pt.pipe(new NullSink()) | |
pt.end('example') | |
// We have to have activity on the stream to actuall crash, as can be seen here. | |
}) | |
}) | |
test('require', t => { | |
t.plan(1) | |
t.doesNotThrow(() => { | |
const stream = require('readable-stream') | |
const PassThrough = stream.PassThrough | |
const pt = new PassThrough() | |
pt.pipe(new NullSink()) | |
pt.end('example') | |
}) | |
}) | |
test('requireInject after require', t => { | |
t.plan(2) | |
t.doesNotThrow(() => { | |
const stream = requireInject('readable-stream') | |
const PassThrough = stream.PassThrough | |
const pt = new PassThrough() | |
pt.pipe(new NullSink()) | |
pt.end('example') | |
// further use (once a version is cached from the previous test) works | |
// because while we force a new copy of the top level `readable-stream` | |
// object, the things it requires still come from cache. | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "readable-stream-incompat", | |
"version": "1.0.0", | |
"license": "ISC", | |
"dependencies": { | |
"readable-stream": "^2.3.3", | |
"require-inject": "^1.4.2", | |
"tap": "^10.7.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment