Permalink
Browse files

Update for 0.6 compatibility

  • Loading branch information...
1 parent cdf804d commit ccddeba125eac9fc57ce21dd4d4d45151c98eaee @isaacs isaacs committed Aug 17, 2012
Showing with 47 additions and 4 deletions.
  1. +15 −0 README.md
  2. +14 −1 lib/read.js
  3. +6 −1 test/basic.js
  4. +6 −1 test/defaults.js
  5. +6 −1 test/many.js
View
15 README.md
@@ -33,6 +33,21 @@ Every option is optional.
If silent is true, and the input is a TTY, then read will set raw
mode, and read character by character.
+## COMPATIBILITY
+
+This module works sort of with node 0.6. It does not work with node
+versions less than 0.6. It is best on node 0.8.
+
+On node version 0.6, it will remove all listeners on the input
+stream's `data` and `keypress` events, because the readline module did
+not fully clean up after itself in that version of node, and did not
+make it possible to clean up after it in a way that has no potential
+for side effects.
+
+Additionally, some of the readline options (like `terminal`) will not
+function in versions of node before 0.8, because they were not
+implemented in the builtin readline module.
+
## CONTRIBUTING
Patches welcome.
View
15 lib/read.js
@@ -23,7 +23,13 @@ function read (opts, cb) {
var def = opts.default || ''
var terminal = !!(opts.terminal || output.isTTY)
var rlOpts = { input: input, output: output, terminal: terminal }
- var rl = readline.createInterface(rlOpts)
+
+ if (process.version.match(/^v0\.6/)) {
+ var rl = readline.createInterface(rlOpts.input, rlOpts.output)
+ } else {
+ var rl = readline.createInterface(rlOpts)
+ }
+
var prompt = (opts.prompt || '').trim() + ' '
var silent = opts.silent
var editDef = false
@@ -69,6 +75,13 @@ function read (opts, cb) {
function done () {
called = true
rl.close()
+
+ if (process.version.match(/^v0\.6/)) {
+ rl.input.removeAllListeners('data')
+ rl.input.removeAllListeners('keypress')
+ rl.input.pause()
+ }
+
clearTimeout(timer)
output.mute()
output.end()
View
7 test/basic.js
@@ -5,6 +5,11 @@ if (process.argv[2] === 'child') {
return child()
}
+var CLOSE = 'close'
+if (process.version.match(/^v0\.6/)) {
+ CLOSE = 'exit'
+}
+
var spawn = require('child_process').spawn
tap.test('basic', function (t) {
@@ -31,7 +36,7 @@ tap.test('basic', function (t) {
console.error('result %j', c.toString())
})
- child.on('close', function () {
+ child.on(CLOSE, function () {
result = JSON.parse(result)
t.same(result, {"user":"a user","pass":"a password","verify":"a password","passMatch":true})
t.equal(output, 'Username: (test-user) Password: (<default hidden>) Password again: (<default hidden>) ')
View
7 test/defaults.js
@@ -5,6 +5,11 @@ if (process.argv[2] === 'child') {
return child()
}
+var CLOSE = 'close'
+if (process.version.match(/^v0\.6/)) {
+ CLOSE = 'exit'
+}
+
var spawn = require('child_process').spawn
tap.test('defaults', function (t) {
@@ -31,7 +36,7 @@ tap.test('defaults', function (t) {
console.error('result %j', c.toString())
})
- child.on('close', function () {
+ child.on(CLOSE, function () {
result = JSON.parse(result)
t.same(result, {"user":"test-user","pass":"test-pass","verify":"test-pass","passMatch":true})
t.equal(output, 'Username: (test-user) Password: (<default hidden>) Password again: (<default hidden>) ')
View
7 test/many.js
@@ -1,6 +1,11 @@
var tap = require('tap')
var read = require('../lib/read.js')
+var CLOSE = 'close'
+if (process.version.match(/^v0\.6/)) {
+ CLOSE = 'exit'
+}
+
if (process.argv[2] === 'child') {
return child()
}
@@ -68,7 +73,7 @@ tap.test('many reads', function (t) {
output += c
console.error('' + c)
})
- child.on('close', function (c) {
+ child.on(CLOSE, function (c) {
t.equal(output, expect)
t.equal(n, 19)
t.end()

0 comments on commit ccddeba

Please sign in to comment.