Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@perezd
Created April 9, 2011 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perezd/911661 to your computer and use it in GitHub Desktop.
Save perezd/911661 to your computer and use it in GitHub Desktop.
This is a repeatable example of a Node HTTPS bug found in v0.4.5. The test relies on cradle.js (which is basically just wrapping HTTP requests, npm install cradle). I describe the bug scenario in the comment below. comment out the different "conn" variabl
// NodeJS BUG:
//
// HTTP mode always calls log message on L30.
// HTTP has not yet failed.
//
// HTTPS mode never calls the log message on L30.
// HTTPS mode randomly throws the following exception:
// node.js:134
// throw e; // process.nextTick error, or 'error' event on first tick
// ^
// TypeError: Cannot call method 'emit' of undefined
// at CleartextStream.<anonymous> (http.js:1201:9)
// at CleartextStream.emit (events.js:64:17)
// at Socket.onerror (tls.js:874:17)
// at Socket.emit (events.js:64:17)
// at Array.<anonymous> (net.js:824:27)
// at EventEmitter._tickCallback (node.js:126:26)
var Cradle, account, conn, db, fs, http, server;
Cradle = require('cradle');
fs = require('fs');
// MOCK SERVERS
// HTTP
// server = require('http').Server(function(req,res) {
// console.log(req.url)
// res.writeHead(201, {"Content-Type": "text/plain;charset=utf8"});
// res.end('{"ok":true,"id":"myDoc4","rev":"44-438530acef7245421ff3b6a9998adf56"}');
// });
// server.listen(8000);
//
// conn = new Cradle.Connection("localhost", 8000, {
// secure: false,
// });
// HTTPS
// options = {
// key: fs.readFileSync(__dirname+"/server.key"),
// cert: fs.readFileSync(__dirname+"/server.crt")
// }
//
// server = require('https').Server(options, function(req, res) {
// console.log(req.url)
// res.writeHead(201, {"Content-Type": "text/plain;charset=utf8"});
// res.end('{"ok":true,"id":"myDoc4","rev":"44-438530acef7245421ff3b6a9998adf56"}');
// });
// server.listen(8000)
// conn = new Cradle.Connection("localhost", 8000, {
// secure: true,
// });
// CLOUDANT TESTS
account = {
username: "nodebug",
password: "omgnodebug"
};
// conn = new Cradle.Connection("nodebug.cloudant.com", 80, {
// secure: false,
// auth: account
// });
conn = new Cradle.Connection("nodebug.cloudant.com", 443, {
secure: true,
auth: account
});
Events = require("events")
promise = new Events.EventEmitter
db = conn.database("test");
db.save("myDoc4", { "foo": "bar" }, function(err, doc) {
console.log("created.");
promise.emit("created", doc)
});
promise.on("created", function(doc){
fs.readFile(__dirname+"/cherryTree.JPG", function(err, imgData) {
console.log("loaded img.")
promise.emit("loaded", doc, imgData)
})
})
promise.on("loaded", function(doc, imgData) {
db.saveAttachment(
doc.id,
doc.rev,
"photo",
"image/jpg",
imgData,
function(err, doc) {
console.log("attached.")
promise.emit("attached")
});
})
promise.on("attached", function(){
console.log("GREAT SUCCESS.") // if this fires, we are good.
if (server) server.close()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment