Skip to content

Feature-detect "use regular streams as an underlying socket for tls.connect()" #981

Closed
@dougwilson

Description

@dougwilson

Hi, I'm looking for a good solution to be able to detect if this feature exists in the runtime my module is running on so I can throw if it's not supported. It was suggested on IRC to make an issue here.

I originally thought perhaps if I just construct a duplex stream and pass it into tls.connect, but it doesn't seem to throw an error on pre-1.4.1 versions. Suggestions?

// This doesn't seem to work
var supportswrappedjsstreams = (function(){
  var s = require("stream").Duplex();s._read=s._write=function(){};
  try { require("tls").connect({ stream: s }); return true; } catch { return false; }
});
// This does work, but not sure if this is going to suddenly break in the future
var supportswrappedjsstreams = (function(){
  try { require("_stream_wrap"); return true; } catch { return false; }
});

Activity

tellnes

tellnes commented on Feb 27, 2015

@tellnes
Contributor
chrisdickinson

chrisdickinson commented on Feb 27, 2015

@chrisdickinson
Contributor

Not sure about the state it leaves the SSL context in, but something like:

var supportswrappedjsstreams = (function(){
  var s = require("stream").Duplex(); s._read = s._write = Function();
  require('tls').connect({socket: s});
  return require('events').listenerCount('error', s) > 0;
});

should work since TLS/streamwrap will always have to install error handlers.

dougwilson

dougwilson commented on Feb 27, 2015

@dougwilson
MemberAuthor

Ah, yes, that does seem to work, even to weed out Node.js stuff. Thanks!

dougwilson

dougwilson commented on Feb 27, 2015

@dougwilson
MemberAuthor

Oops, I may have closed that too early, because I only tested the pre-1.4.1 versions; it actually seems to have 0 listeners, even on 1.4.1:

$ iojs -pe 's=require("stream").Duplex();s._read=s._write=function(){};require("tls").connect({socket:s});require("events").EventEmitter.listenerCount("error",s)'
0

$ iojs -v
v1.4.1
chrisdickinson

chrisdickinson commented on Feb 27, 2015

@chrisdickinson
Contributor

Ah, whoops: it should be EE.listenerCount(emitter, "eventName");. My bad!

indutny

indutny commented on Feb 27, 2015

@indutny
Member

Interesting question! Why not just check the process.version? :)

mathiasbynens

mathiasbynens commented on Feb 27, 2015

@mathiasbynens
Contributor

Checking process.version is to user-agent sniffing as feature testing is to, uhm, feature testing. Inferring behavior from a version number seems brittle IMHO.

indutny

indutny commented on Feb 27, 2015

@indutny
Member

@mathiasbynens the proposed check is much more brittle than version checking. We have semver, so this thing can't go away until 2.0.0 for sure.

chrisdickinson

chrisdickinson commented on Mar 12, 2015

@chrisdickinson
Contributor

@dougwilson I think the check works now – ok to close?

dougwilson

dougwilson commented on Mar 12, 2015

@dougwilson
MemberAuthor

The only working check that was provided here was to sniff the version and at the same time, I'm told in this thread not to sniff the version. I need clarification still: should I start version sniffing io.js features or can someone come up with a non-version-sniffing solution to this?

chrisdickinson

chrisdickinson commented on Mar 12, 2015

@chrisdickinson
Contributor

@dougwilson Did you see my message about the order of EE.listenerCount params?

25 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.tlsIssues and PRs related to the tls subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Feature-detect "use regular streams as an underlying socket for tls.connect()" · Issue #981 · nodejs/node