From b9d899d1261abefb603c59be08ea845afc173511 Mon Sep 17 00:00:00 2001 From: Trevor Johnston Date: Wed, 13 Apr 2016 16:54:59 -0400 Subject: [PATCH 1/2] randombytes alias should implement callback --- src/cloud/social/alias/randombytes.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cloud/social/alias/randombytes.js b/src/cloud/social/alias/randombytes.js index e8774d6..14ec43e 100644 --- a/src/cloud/social/alias/randombytes.js +++ b/src/cloud/social/alias/randombytes.js @@ -14,7 +14,7 @@ try { cryptoAvailable = false; } -module.exports = function(size) { +module.exports = function(size, cb) { var buffer = new Buffer(size); if (cryptoAvailable) { // Although this looks weird, it's how crypto-browserify does it too: @@ -25,5 +25,9 @@ module.exports = function(size) { buffer[i] = Math.floor(Math.random() * 256); } } - return buffer; + if (cb) { + cb(undefined, buffer); + } else { + return buffer; + } } From 68cff2bc9bd008767fa235fd4fe2334c4ecfc8f8 Mon Sep 17 00:00:00 2001 From: Trevor Johnston Date: Wed, 13 Apr 2016 16:55:12 -0400 Subject: [PATCH 2/2] migrate to ssh2 0.5.0 --- Gruntfile.coffee | 10 ++++------ package.json | 3 +-- src/cloud/install/installer.ts | 2 ++ src/cloud/social/alias/brorand.js | 18 ++++++++++++++++++ src/cloud/social/alias/ssh2-streams.js | 11 ----------- src/cloud/social/monkey/process.js | 9 +++++++++ src/cloud/social/provider.ts | 2 ++ 7 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 src/cloud/social/alias/brorand.js delete mode 100644 src/cloud/social/alias/ssh2-streams.js create mode 100644 src/cloud/social/monkey/process.js diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 8f7686d..aa9407f 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -314,9 +314,8 @@ config = # with a couple of fixes. './src/cloud/social/shim/net.js:net' './src/cloud/social/shim/dns.js:dns' - # Subset of ssh2-streams (all except SFTP) which works well in - # the browser. - './src/cloud/social/alias/ssh2-streams.js:ssh2-streams' + # Alternative that works for freedomjs modules. + './src/cloud/social/alias/brorand.js:brorand' # Fallback for crypto-browserify's randombytes, for Firefox. './src/cloud/social/alias/randombytes.js:randombytes' ] @@ -327,9 +326,8 @@ config = # with a couple of fixes. './src/cloud/social/shim/net.js:net' './src/cloud/social/shim/dns.js:dns' - # Subset of ssh2-streams (all except SFTP) which works well in - # the browser. - './src/cloud/social/alias/ssh2-streams.js:ssh2-streams' + # Alternative that works for freedomjs modules. + './src/cloud/social/alias/brorand.js:brorand' # Fallback for crypto-browserify's randombytes, for Firefox. './src/cloud/social/alias/randombytes.js:randombytes' ] diff --git a/package.json b/package.json index 7506cb7..7e672ef 100644 --- a/package.json +++ b/package.json @@ -60,8 +60,7 @@ "lodash": "^3.10.1", "request": "^2.53.0", "socks5-http-client": "^1.0.2", - "ssh2": "0.4.12", - "ssh2-streams": "0.0.18", + "ssh2": "0.5.0", "tslint": "^3.3.0", "typescript": "~1.8.2", "typings": "^0.7.12", diff --git a/src/cloud/install/installer.ts b/src/cloud/install/installer.ts index 4f7dd15..080272a 100644 --- a/src/cloud/install/installer.ts +++ b/src/cloud/install/installer.ts @@ -1,5 +1,7 @@ /// +require('../social/monkey/process'); + import arraybuffers = require('../../arraybuffers/arraybuffers'); import linefeeder = require('../../net/linefeeder'); import logging = require('../../logging/logging'); diff --git a/src/cloud/social/alias/brorand.js b/src/cloud/social/alias/brorand.js new file mode 100644 index 0000000..a667327 --- /dev/null +++ b/src/cloud/social/alias/brorand.js @@ -0,0 +1,18 @@ +// brorand alias which works in a freedomjs module context. +// Uses randombytes, for which we have an alias which works +// for Firefox. + +const randombytes = require('randombytes'); + +module.exports = function rand(len) { + return randombytes(len); +}; + +function Rand(fallback) { + // no-op: always use randombytes +} +module.exports.Rand = Rand; + +Rand.prototype.generate = function generate(len) { + return randombytes(len); +}; diff --git a/src/cloud/social/alias/ssh2-streams.js b/src/cloud/social/alias/ssh2-streams.js deleted file mode 100644 index 5d91b85..0000000 --- a/src/cloud/social/alias/ssh2-streams.js +++ /dev/null @@ -1,11 +0,0 @@ -// This is identical to ssh2-streams' index.js except for SFTPStream. -// We do this because sftp.js contains a call to process.bindings -// that raises an error in the browser. This is safe because we don't -// actually care about using SFTP, it's just that ssh2's Client sets -// some constants to point to some of SFTPStream's constants. -module.exports = { - SFTPStream: {}, - SSH2Stream: require('ssh2-streams/lib/ssh'), - utils: require('ssh2-streams/lib/utils'), - constants: require('ssh2-streams/lib/constants') -}; diff --git a/src/cloud/social/monkey/process.js b/src/cloud/social/monkey/process.js new file mode 100644 index 0000000..690912a --- /dev/null +++ b/src/cloud/social/monkey/process.js @@ -0,0 +1,9 @@ +// Monkey patch for browserify's process shim, for ssh2-streams. + +// Node.js 4.2 is an LTS release and, very roughly, is what the +// browserify shims, e.g. Buffer, provide. +process.version = '4.2.0'; + +process.binding = function() { + return {}; +}; diff --git a/src/cloud/social/provider.ts b/src/cloud/social/provider.ts index 06658c5..931212b 100644 --- a/src/cloud/social/provider.ts +++ b/src/cloud/social/provider.ts @@ -1,5 +1,7 @@ /// +require('../social/monkey/process'); + import arraybuffers = require('../../arraybuffers/arraybuffers'); import linefeeder = require('../../net/linefeeder'); import logging = require('../../logging/logging');