@syncot/connection
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,4 +0,3 @@ | ||
/// <reference types="node" /> | ||
import { EmitterInterface, SyncOtEmitter } from '@syncot/util'; | ||
import { Duplex } from 'stream'; | ||
import { Duplex } from 'readable-stream'; | ||
declare type RequestId = number; | ||
@@ -163,2 +162,4 @@ declare type ServiceName = string; | ||
* It indicates presence of a bug in SyncOT or the client code. | ||
* - `SyncOtError InvalidStream` - emitted when a service returns a stream, or a promise which | ||
* resolves to a stream, and that stream is already destroyed, not readable, or not writable. | ||
* @event destroy Emitted when the Connection is destroyed. | ||
@@ -165,0 +166,0 @@ */ |
@@ -1,5 +0,5 @@ | ||
import { createDisconnectedError, createDuplicateIdError, createInvalidEntityError, createNoServiceError, } from '@syncot/error'; | ||
import { createDisconnectedError, createDuplicateIdError, createInvalidEntityError, createInvalidStreamError, createNoServiceError, } from '@syncot/error'; | ||
import { SyncOtEmitter, validate, } from '@syncot/util'; | ||
import { strict as assert } from 'assert'; | ||
import { Duplex } from 'stream'; | ||
import { Duplex, Stream } from 'readable-stream'; | ||
/** | ||
@@ -110,6 +110,3 @@ * The list of supported `Message` types. | ||
this.assertNotDestroyed(); | ||
assert.ok(stream instanceof Duplex, 'Argument "stream" must be a Duplex.'); | ||
assert.equal(stream.readable, true, '"stream" must be readable.'); | ||
assert.equal(stream.writable, true, '"stream" must be writable.'); | ||
assert.equal(stream.destroyed, false, '"stream" must not be destroyed.'); | ||
assert.ok(isOpenDuplex(stream), 'Argument "stream" must be an open Duplex.'); | ||
assert.ok(!this.stream, 'Connection is already associated with a stream.'); | ||
@@ -333,3 +330,3 @@ this.stream = stream; | ||
if (this.stream !== stream) { | ||
if (reply instanceof Duplex) { | ||
if (reply instanceof Stream) { | ||
reply.destroy(); | ||
@@ -339,3 +336,3 @@ } | ||
} | ||
if (reply instanceof Duplex) { | ||
if (isOpenDuplex(reply)) { | ||
const serviceStream = reply; | ||
@@ -406,2 +403,14 @@ if (serviceStreams.has(id)) { | ||
} | ||
else if (reply instanceof Stream) { | ||
const error = createInvalidStreamError('Service returned an invalid stream.'); | ||
this.emitAsync('error', error); | ||
this.send({ | ||
data: error, | ||
id, | ||
name: null, | ||
service, | ||
type: 3 /* REPLY_ERROR */, | ||
}); | ||
reply.destroy(); | ||
} | ||
else { | ||
@@ -560,1 +569,10 @@ const typeOfReply = typeof reply; | ||
} | ||
function isOpenDuplex(stream) { | ||
// Ideally we'd use `stream instanceof Duplex`, however, that evaluates to `false` in jest, | ||
// even if `stream` actually is a Duplex. | ||
// It might be related to https://github.com/facebook/jest/issues/2549#issuecomment-423202304. | ||
return (stream instanceof Stream && | ||
stream.readable === true && | ||
stream.writable === true && | ||
stream.destroyed === false); | ||
} |
{ | ||
"name": "@syncot/connection", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "An RPC library for communication over Duplex object streams.", | ||
@@ -29,8 +29,10 @@ "keywords": [ | ||
"dependencies": { | ||
"@syncot/error": "^0.0.3", | ||
"@syncot/util": "^0.0.2", | ||
"@syncot/error": "^0.0.4", | ||
"@syncot/util": "^0.0.3", | ||
"@types/node": "^10.12.21", | ||
"@types/readable-stream": "^2.3.1", | ||
"readable-stream": "^3.3.0", | ||
"tslib": "^1.9.3" | ||
}, | ||
"gitHead": "b952421ee19e038a054420c80e3b870f52e23ed9" | ||
"gitHead": "2b86fbb78a4b4b2f16fe3ed00ca08851bcc74e5c" | ||
} |
31215
747
6
+ Addedreadable-stream@^3.3.0
+ Added@syncot/error@0.0.4(transitive)
+ Added@syncot/util@0.0.3(transitive)
+ Added@types/readable-stream@2.3.15(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removed@syncot/error@0.0.3(transitive)
- Removed@syncot/util@0.0.2(transitive)
Updated@syncot/error@^0.0.4
Updated@syncot/util@^0.0.3