@hyperswarm/secret-stream
Advanced tools
Comparing version 5.1.3 to 5.1.4
52
index.js
@@ -24,2 +24,6 @@ const { Pull, Push, HEADERBYTES, KEYBYTES, ABYTES } = require('sodium-secretstream') | ||
if (typeof isInitiator !== 'boolean') { | ||
throw new Error('isInitiator should be a boolean') | ||
} | ||
this.noiseStream = this | ||
@@ -94,9 +98,5 @@ this.isInitiator = isInitiator | ||
this._startHandshake(opts.handshake, opts.keyPair || null) | ||
this._continueOpen(null) | ||
if (this._startDone !== null) { | ||
const done = this._startDone | ||
this._startDone = null | ||
this._open(done) | ||
} | ||
if (this.destroying) return | ||
if (opts.data) this._onrawdata(opts.data) | ||
@@ -106,2 +106,30 @@ if (opts.ended) this._onrawend() | ||
_continueOpen (err) { | ||
if (err) this.destroy(err) | ||
if (this._startDone === null) return | ||
const done = this._startDone | ||
this._startDone = null | ||
this._open(done) | ||
} | ||
_onkeypairpromise (p) { | ||
const self = this | ||
const cont = this._continueOpen.bind(this) | ||
p.then(onkeypair, cont) | ||
function onkeypair (kp) { | ||
self._onkeypair(kp) | ||
cont(null) | ||
} | ||
} | ||
_onkeypair (keyPair) { | ||
const pattern = this._handshakePattern || 'XX' | ||
const remotePublicKey = this.remotePublicKey | ||
this._handshake = new Handshake(this.isInitiator, keyPair, remotePublicKey, pattern) | ||
this.publicKey = this._handshake.keyPair.publicKey | ||
} | ||
_startHandshake (handshake, keyPair) { | ||
@@ -115,7 +143,8 @@ if (handshake) { | ||
if (!keyPair) keyPair = Handshake.keyPair() | ||
const pattern = this._handshakePattern || 'XX' | ||
const remotePublicKey = this.remotePublicKey | ||
this._handshake = new Handshake(this.isInitiator, keyPair, remotePublicKey, pattern) | ||
this.publicKey = this._handshake.keyPair.publicKey | ||
if (typeof keyPair.then === 'function') { | ||
this._onkeypairpromise(keyPair) | ||
} else { | ||
this._onkeypair(keyPair) | ||
} | ||
} | ||
@@ -290,3 +319,4 @@ | ||
_open (cb) { | ||
if (this._rawStream === null) { // no autostart | ||
// no autostart or no handshake yet | ||
if (this._rawStream === null || (this._handshake === null && this._encrypt === null)) { | ||
this._startDone = cb | ||
@@ -293,0 +323,0 @@ return |
{ | ||
"name": "@hyperswarm/secret-stream", | ||
"version": "5.1.3", | ||
"version": "5.1.4", | ||
"description": "Secret stream backed by Noise and libsodium's secretstream", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -73,2 +73,6 @@ # @hyperswarm/secret-stream | ||
If need to load the key pair asynchronously, then secret-stream also supports passing in a promise | ||
instead of the keypair that later resolves to `{ publicKey, secretKey }`. The stream lifecycle will wait | ||
for the resolution and auto destroy the stream if the promise errors. | ||
#### `s.start(rawStream, [options])` | ||
@@ -75,0 +79,0 @@ |
54
test.js
@@ -346,2 +346,56 @@ const tape = require('tape') | ||
tape('keypair can be a promise', function (t) { | ||
t.plan(2) | ||
const kp = NoiseStream.keyPair() | ||
const a = new NoiseStream(true, null, { | ||
keyPair: new Promise((resolve) => { | ||
setImmediate(() => resolve(kp)) | ||
}) | ||
}) | ||
const b = new NoiseStream(false) | ||
a.rawStream.pipe(b.rawStream).pipe(a.rawStream) | ||
a.on('connect', function () { | ||
t.same(kp.publicKey, a.publicKey) | ||
}) | ||
b.on('connect', function () { | ||
t.same(kp.publicKey, b.remotePublicKey) | ||
}) | ||
}) | ||
tape('keypair can be a promise that rejects', function (t) { | ||
t.plan(4) | ||
const a = new NoiseStream(true, null, { | ||
keyPair: new Promise((resolve, reject) => { | ||
reject(new Error('stop')) | ||
}) | ||
}) | ||
const b = new NoiseStream(false) | ||
a.rawStream.pipe(b.rawStream).pipe(a.rawStream) | ||
a.rawStream.on('error', function (err) { | ||
t.same(err, new Error('stop')) | ||
}) | ||
a.on('error', function (err) { | ||
t.same(err, new Error('stop')) | ||
}) | ||
b.rawStream.on('error', function (err) { | ||
t.same(err, new Error('stop')) | ||
}) | ||
b.on('error', function (err) { | ||
t.same(err, new Error('stop')) | ||
}) | ||
}) | ||
function createHandshake () { | ||
@@ -348,0 +402,0 @@ return new Promise((resolve, reject) => { |
28390
783
120