Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rsocket-core

Package Overview
Dependencies
Maintainers
2
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rsocket-core - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

build/index.js.flow

4

build/index.js

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -15,3 +15,3 @@

Object.defineProperty(exports, '__esModule', {value: true});
exports.JsonSerializers = exports.JsonSerializer = exports.IdentitySerializers = exports.IdentitySerializer = exports.UTF8Encoder = exports.Utf8Encoders = exports.BufferEncoder = exports.BufferEncoders = exports.writeUInt24BE = exports.toBuffer = exports.readUInt24BE = exports.createBuffer = exports.byteLength = exports.serializeFrameWithLength = exports.serializeFrame = exports.deserializeFrames = exports.deserializeFrameWithLength = exports.deserializeFrame = exports.printFrame = exports.isResumeEnable = exports.isRespond = exports.isNext = exports.isMetadata = exports.isLease = exports.isIgnore = exports.isComplete = exports.getErrorCodeExplanation = exports.createErrorFromFrame = exports.MAX_VERSION = exports.MAX_STREAM_ID = exports.MAX_RESUME_LENGTH = exports.MAX_MIME_LENGTH = exports.MAX_LIFETIME = exports.MAX_KEEPALIVE = exports.MAX_CODE = exports.FRAME_TYPES = exports.FRAME_TYPE_OFFFSET = exports.FLAGS = exports.FLAGS_MASK = exports.ERROR_EXPLANATIONS = exports.ERROR_CODES = exports.CONNECTION_STREAM_ID = exports.RSocketResumableTransport = exports.RSocketServer = exports.RSocketClient = undefined;
exports.JsonSerializers = (exports.JsonSerializer = (exports.IdentitySerializers = (exports.IdentitySerializer = (exports.UTF8Encoder = (exports.Utf8Encoders = (exports.BufferEncoder = (exports.BufferEncoders = (exports.writeUInt24BE = (exports.toBuffer = (exports.readUInt24BE = (exports.createBuffer = (exports.byteLength = (exports.serializeFrameWithLength = (exports.serializeFrame = (exports.deserializeFrames = (exports.deserializeFrameWithLength = (exports.deserializeFrame = (exports.printFrame = (exports.isResumeEnable = (exports.isRespond = (exports.isNext = (exports.isMetadata = (exports.isLease = (exports.isIgnore = (exports.isComplete = (exports.getErrorCodeExplanation = (exports.createErrorFromFrame = (exports.MAX_VERSION = (exports.MAX_STREAM_ID = (exports.MAX_RESUME_LENGTH = (exports.MAX_MIME_LENGTH = (exports.MAX_LIFETIME = (exports.MAX_KEEPALIVE = (exports.MAX_CODE = (exports.FRAME_TYPES = (exports.FRAME_TYPE_OFFFSET = (exports.FLAGS = (exports.FLAGS_MASK = (exports.ERROR_EXPLANATIONS = (exports.ERROR_CODES = (exports.CONNECTION_STREAM_ID = (exports.RSocketResumableTransport = (exports.RSocketServer = (exports.RSocketClient = undefined))))))))))))))))))))))))))))))))))))))))))));
var _RSocketFrame = require('./RSocketFrame');

@@ -18,0 +18,0 @@ Object.defineProperty(exports, 'CONNECTION_STREAM_ID', {

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -34,12 +34,15 @@

/**
* Frame header is:
* - stream id (uint32 = 4)
* - type + flags (uint 16 = 2)
*/ const FRAME_HEADER_SIZE = 6;
* Frame header is:
* - stream id (uint32 = 4)
* - type + flags (uint 16 = 2)
*/ const FRAME_HEADER_SIZE = 6;
/**
* Size of frame length and metadata length fields.
*/ const UINT24_SIZE = 3;
* Size of frame length and metadata length fields.
*/ const UINT24_SIZE = 3;
/**
* Reads a frame from a buffer that is prefixed with the frame length.
*/ function deserializeFrameWithLength(buffer, encoders) {
* Reads a frame from a buffer that is prefixed with the frame length.
*/ function deserializeFrameWithLength(
buffer,
encoders
) {
const frameLength = (0, _RSocketBufferUtils.readUInt24BE)(buffer, 0);

@@ -52,6 +55,9 @@ return deserializeFrame(

/**
* Given a buffer that may contain zero or more length-prefixed frames followed
* by zero or more bytes of a (partial) subsequent frame, returns an array of
* the frames and a buffer of the leftover bytes.
*/ function deserializeFrames(buffer, encoders) {
* Given a buffer that may contain zero or more length-prefixed frames followed
* by zero or more bytes of a (partial) subsequent frame, returns an array of
* the frames and a buffer of the leftover bytes.
*/ function deserializeFrames(
buffer,
encoders
) {
const frames = [];

@@ -75,4 +81,7 @@ let offset = 0;

/**
* Writes a frame to a buffer with a length prefix.
*/ function serializeFrameWithLength(frame, encoders) {
* Writes a frame to a buffer with a length prefix.
*/ function serializeFrameWithLength(
frame,
encoders
) {
const buffer = serializeFrame(frame, encoders);

@@ -87,4 +96,7 @@ const lengthPrefixed = (0, _RSocketBufferUtils.createBuffer)(

/**
* Read a frame from the buffer.
*/ function deserializeFrame(buffer, encoders) {
* Read a frame from the buffer.
*/ function deserializeFrame(
buffer,
encoders
) {
encoders = encoders || _RSocketEncoding.Utf8Encoders;

@@ -139,4 +151,7 @@ let offset = 0;

/**
* Convert the frame to a (binary) buffer.
*/ function serializeFrame(frame, encoders) {
* Convert the frame to a (binary) buffer.
*/ function serializeFrame(
frame,
encoders
) {
encoders = encoders || _RSocketEncoding.Utf8Encoders;

@@ -178,29 +193,26 @@ switch (frame.type) {

/**
* Writes a SETUP frame into a new buffer and returns it.
*
* Prefix size is:
* - version (2x uint16 = 4)
* - keepalive (uint32 = 4)
* - lifetime (uint32 = 4)
* - mime lengths (2x uint8 = 2)
*/
* Writes a SETUP frame into a new buffer and returns it.
*
* Prefix size is:
* - version (2x uint16 = 4)
* - keepalive (uint32 = 4)
* - lifetime (uint32 = 4)
* - mime lengths (2x uint8 = 2)
*/
const SETUP_FIXED_SIZE = 14;
const RESUME_TOKEN_LENGTH_SIZE = 2;
function serializeSetupFrame(frame, encoders) {
const resumeTokenLength =
frame.resumeToken != null
? encoders.resumeToken.byteLength(frame.resumeToken)
: 0;
const metadataMimeTypeLength =
frame.metadataMimeType != null
? encoders.metadataMimeType.byteLength(frame.metadataMimeType)
: 0;
const dataMimeTypeLength =
frame.dataMimeType != null
? encoders.dataMimeType.byteLength(frame.dataMimeType)
: 0;
const resumeTokenLength = frame.resumeToken != null
? encoders.resumeToken.byteLength(frame.resumeToken)
: 0;
const metadataMimeTypeLength = frame.metadataMimeType != null
? encoders.metadataMimeType.byteLength(frame.metadataMimeType)
: 0;
const dataMimeTypeLength = frame.dataMimeType != null
? encoders.dataMimeType.byteLength(frame.dataMimeType)
: 0;
const payloadLength = getPayloadLength(frame, encoders);
const buffer = (0, _RSocketBufferUtils.createBuffer)(
FRAME_HEADER_SIZE +
SETUP_FIXED_SIZE + //
SETUP_FIXED_SIZE + //
(resumeTokenLength ? RESUME_TOKEN_LENGTH_SIZE + resumeTokenLength : 0) +

@@ -255,4 +267,4 @@ metadataMimeTypeLength +

/**
* Reads a SETUP frame from the buffer and returns it.
*/
* Reads a SETUP frame from the buffer and returns it.
*/
function deserializeSetupFrame(buffer, streamId, flags, encoders) {

@@ -277,3 +289,2 @@ (0, _invariant2.default)(

_RSocketFrame.MAX_KEEPALIVE,
keepAlive

@@ -289,3 +300,2 @@ );

_RSocketFrame.MAX_LIFETIME,
lifetime

@@ -304,3 +314,2 @@ );

_RSocketFrame.MAX_RESUME_LENGTH,
resumeTokenLength

@@ -358,10 +367,11 @@ );

/**
* Writes an ERROR frame into a new buffer and returns it.
*
* Prefix size is for the error code (uint32 = 4).
*/
* Writes an ERROR frame into a new buffer and returns it.
*
* Prefix size is for the error code (uint32 = 4).
*/
const ERROR_FIXED_SIZE = 4;
function serializeErrorFrame(frame, encoders) {
const messageLength =
frame.message != null ? encoders.message.byteLength(frame.message) : 0;
const messageLength = frame.message != null
? encoders.message.byteLength(frame.message)
: 0;
const buffer = (0, _RSocketBufferUtils.createBuffer)(

@@ -385,4 +395,4 @@ FRAME_HEADER_SIZE + ERROR_FIXED_SIZE + messageLength

/**
* Reads an ERROR frame from the buffer and returns it.
*/
* Reads an ERROR frame from the buffer and returns it.
*/
function deserializeErrorFrame(buffer, streamId, flags, encoders) {

@@ -396,3 +406,2 @@ let offset = FRAME_HEADER_SIZE;

_RSocketFrame.MAX_CODE,
code

@@ -418,10 +427,11 @@ );

/**
* Writes a KEEPALIVE frame into a new buffer and returns it.
*
* Prefix size is for the last received position (uint64 = 8).
*/
* Writes a KEEPALIVE frame into a new buffer and returns it.
*
* Prefix size is for the last received position (uint64 = 8).
*/
const KEEPALIVE_FIXED_SIZE = 8;
function serializeKeepAliveFrame(frame, encoders) {
const dataLength =
frame.data != null ? encoders.data.byteLength(frame.data) : 0;
const dataLength = frame.data != null
? encoders.data.byteLength(frame.data)
: 0;
const buffer = (0, _RSocketBufferUtils.createBuffer)(

@@ -444,4 +454,4 @@ FRAME_HEADER_SIZE + KEEPALIVE_FIXED_SIZE + dataLength

/**
* Reads a KEEPALIVE frame from the buffer and returns it.
*/
* Reads a KEEPALIVE frame from the buffer and returns it.
*/
function deserializeKeepAliveFrame(buffer, streamId, flags, encoders) {

@@ -474,10 +484,11 @@ (0, _invariant2.default)(

/**
* Writes a LEASE frame into a new buffer and returns it.
*
* Prefix size is for the ttl (uint32) and requestcount (uint32).
*/
* Writes a LEASE frame into a new buffer and returns it.
*
* Prefix size is for the ttl (uint32) and requestcount (uint32).
*/
const LEASE_FIXED_SIZE = 8;
function serializeLeaseFrame(frame, encoders) {
const metaLength =
frame.metadata != null ? encoders.metadata.byteLength(frame.metadata) : 0;
const metaLength = frame.metadata != null
? encoders.metadata.byteLength(frame.metadata)
: 0;
const buffer = (0, _RSocketBufferUtils.createBuffer)(

@@ -502,4 +513,4 @@ FRAME_HEADER_SIZE + LEASE_FIXED_SIZE + metaLength

/**
* Reads a LEASE frame from the buffer and returns it.
*/
* Reads a LEASE frame from the buffer and returns it.
*/
function deserializeLeaseFrame(buffer, streamId, flags, encoders) {

@@ -531,7 +542,7 @@ (0, _invariant2.default)(

/**
* Writes a REQUEST_FNF or REQUEST_RESPONSE frame to a new buffer and returns
* it.
*
* Note that these frames have the same shape and only differ in their type.
*/
* Writes a REQUEST_FNF or REQUEST_RESPONSE frame to a new buffer and returns
* it.
*
* Note that these frames have the same shape and only differ in their type.
*/
function serializeRequestFrame(frame, encoders) {

@@ -584,9 +595,9 @@ const payloadLength = getPayloadLength(frame, encoders);

/**
* Writes a REQUEST_STREAM or REQUEST_CHANNEL frame to a new buffer and returns
* it.
*
* Note that these frames have the same shape and only differ in their type.
*
* Prefix size is for requestN (uint32 = 4).
*/
* Writes a REQUEST_STREAM or REQUEST_CHANNEL frame to a new buffer and returns
* it.
*
* Note that these frames have the same shape and only differ in their type.
*
* Prefix size is for requestN (uint32 = 4).
*/
const REQUEST_MANY_HEADER = 4;

@@ -662,6 +673,6 @@ function serializeRequestManyFrame(frame, encoders) {

/**
* Writes a REQUEST_N frame to a new buffer and returns it.
*
* Prefix size is for requestN (uint32 = 4).
*/
* Writes a REQUEST_N frame to a new buffer and returns it.
*
* Prefix size is for requestN (uint32 = 4).
*/
const REQUEST_N_HEADER = 4;

@@ -699,4 +710,4 @@ function serializeRequestNFrame(frame, encoders) {

/**
* Writes a CANCEL frame to a new buffer and returns it.
*/
* Writes a CANCEL frame to a new buffer and returns it.
*/
function serializeCancelFrame(frame, encoders) {

@@ -722,4 +733,4 @@ const buffer = (0, _RSocketBufferUtils.createBuffer)(FRAME_HEADER_SIZE);

/**
* Writes a PAYLOAD frame to a new buffer and returns it.
*/
* Writes a PAYLOAD frame to a new buffer and returns it.
*/
function serializePayloadFrame(frame, encoders) {

@@ -754,11 +765,11 @@ const payloadLength = getPayloadLength(frame, encoders);

/**
* Writes a RESUME frame into a new buffer and returns it.
*
* Fixed size is:
* - major version (uint16 = 2)
* - minor version (uint16 = 2)
* - token length (uint16 = 2)
* - client position (uint64 = 8)
* - server position (uint64 = 8)
*/
* Writes a RESUME frame into a new buffer and returns it.
*
* Fixed size is:
* - major version (uint16 = 2)
* - minor version (uint16 = 2)
* - token length (uint16 = 2)
* - client position (uint64 = 8)
* - server position (uint64 = 8)
*/
const RESUME_FIXED_SIZE = 22;

@@ -811,3 +822,2 @@ function serializeResumeFrame(frame, encoders) {

_RSocketFrame.MAX_RESUME_LENGTH,
resumeTokenLength

@@ -840,7 +850,7 @@ );

/**
* Writes a RESUME_OK frame into a new buffer and returns it.
*
* Fixed size is:
* - client position (uint64 = 8)
*/
* Writes a RESUME_OK frame into a new buffer and returns it.
*
* Fixed size is:
* - client position (uint64 = 8)
*/
const RESUME_OK_FIXED_SIZE = 8;

@@ -875,4 +885,4 @@ function serializeResumeOkFrame(frame, encoders) {

/**
* Write the header of the frame into the buffer.
*/
* Write the header of the frame into the buffer.
*/
function writeHeader(frame, buffer) {

@@ -882,4 +892,4 @@ const offset = buffer.writeInt32BE(frame.streamId, 0);

return buffer.writeUInt16BE(
(frame.type << _RSocketFrame.FRAME_TYPE_OFFFSET) |
(frame.flags & _RSocketFrame.FLAGS_MASK),
frame.type << _RSocketFrame.FRAME_TYPE_OFFFSET |
frame.flags & _RSocketFrame.FLAGS_MASK,
offset

@@ -890,5 +900,5 @@ );

/**
* Determine the length of the payload section of a frame. Only applies to
* frame types that MAY have both metadata and data.
*/
* Determine the length of the payload section of a frame. Only applies to
* frame types that MAY have both metadata and data.
*/
function getPayloadLength(frame, encoders) {

@@ -909,5 +919,5 @@ let payloadLength = 0;

/**
* Write the payload of a frame into the given buffer. Only applies to frame
* types that MAY have both metadata and data.
*/
* Write the payload of a frame into the given buffer. Only applies to frame
* types that MAY have both metadata and data.
*/
function writePayload(frame, buffer, encoders, offset) {

@@ -938,5 +948,5 @@ if ((0, _RSocketFrame.isMetadata)(frame.flags)) {

/**
* Read the payload from a buffer and write it into the frame. Only applies to
* frame types that MAY have both metadata and data.
*/
* Read the payload from a buffer and write it into the frame. Only applies to
* frame types that MAY have both metadata and data.
*/
function readPayload(buffer, frame, encoders, offset) {

@@ -943,0 +953,0 @@ if ((0, _RSocketFrame.isMetadata)(frame.flags)) {

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -32,6 +32,6 @@

/**
* Mimimum value that would overflow bitwise operators (2^32).
*/ const BITWISE_OVERFLOW = 0x100000000; /** // rewritten for browsers
* Read a uint24 from a buffer starting at the given offset.
*/
* Mimimum value that would overflow bitwise operators (2^32).
*/ const BITWISE_OVERFLOW = 0x100000000; /**
* Read a uint24 from a buffer starting at the given offset.
*/ // rewritten for browsers
function readUInt24BE(buffer, offset) {

@@ -44,13 +44,20 @@ const val1 = buffer.readUInt8(offset) << 16;

/**
* Writes a uint24 to a buffer starting at the given offset, returning the
* offset of the next byte.
*/ function writeUInt24BE(buffer, value, offset) {
* Writes a uint24 to a buffer starting at the given offset, returning the
* offset of the next byte.
*/ function writeUInt24BE(
buffer,
value,
offset
) {
offset = buffer.writeUInt8(value >>> 16, offset); // 3rd byte
offset = buffer.writeUInt8((value >>> 8) & 0xff, offset); // 2nd byte
offset = buffer.writeUInt8(value >>> 8 & 0xff, offset); // 2nd byte
return buffer.writeUInt8(value & 0xff, offset); // 1st byte
}
/**
* Read a uint64 (technically supports up to 53 bits per JS number
* representation).
*/ function readUInt64BE(buffer, offset) {
* Read a uint64 (technically supports up to 53 bits per JS number
* representation).
*/ function readUInt64BE(
buffer,
offset
) {
const high = buffer.readUInt32BE(offset);

@@ -61,6 +68,10 @@ const low = buffer.readUInt32BE(offset + 4);

/**
* Write a uint64 (technically supports up to 53 bits per JS number
* representation).
*/ function writeUInt64BE(buffer, value, offset) {
const high = (value / BITWISE_OVERFLOW) | 0;
* Write a uint64 (technically supports up to 53 bits per JS number
* representation).
*/ function writeUInt64BE(
buffer,
value,
offset
) {
const high = value / BITWISE_OVERFLOW | 0;
const low = value % BITWISE_OVERFLOW;

@@ -71,5 +82,8 @@ offset = buffer.writeUInt32BE(high, offset); // first half of uint64

/**
* Determine the number of bytes it would take to encode the given data with the
* given encoding.
*/ function byteLength(data, encoding) {
* Determine the number of bytes it would take to encode the given data with the
* given encoding.
*/ function byteLength(
data,
encoding
) {
if (data == null) {

@@ -81,4 +95,6 @@ return 0;

/**
* Attempts to construct a buffer from the input, throws if invalid.
*/ function toBuffer(data) {
* Attempts to construct a buffer from the input, throws if invalid.
*/ function toBuffer(
data
) {
// Buffer.from(buffer) copies which we don't want here

@@ -97,6 +113,6 @@ if (data instanceof _buffer.Buffer) {

/**
* Function to create a buffer of a given sized filled with zeros.
*/ const createBuffer = (exports.createBuffer =
typeof _buffer.Buffer.alloc === 'function'
? length => _buffer.Buffer.alloc(length)
: length => new _buffer.Buffer(length).fill(0));
* Function to create a buffer of a given sized filled with zeros.
*/ const createBuffer = (exports.createBuffer = typeof _buffer.Buffer.alloc ===
'function'
? length => _buffer.Buffer.alloc(length)
: length => new _buffer.Buffer(length).fill(0));

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -27,11 +27,11 @@

/**
* RSocketClient: A client in an RSocket connection that will communicates with
* the peer via the given transport client. Provides methods for establishing a
* connection and initiating the RSocket interactions:
* - fireAndForget()
* - requestResponse()
* - requestStream()
* - requestChannel()
* - metadataPush()
*/
* RSocketClient: A client in an RSocket connection that will communicates with
* the peer via the given transport client. Provides methods for establishing a
* connection and initiating the RSocket interactions:
* - fireAndForget()
* - requestResponse()
* - requestStream()
* - requestChannel()
* - metadataPush()
*/
class RSocketClient {

@@ -88,4 +88,4 @@ constructor(config) {

/**
* @private
*/
* @private
*/
class RSocketClientSocket {

@@ -145,3 +145,8 @@ constructor(config, connection) {

_buildSetupFrame(config) {
const {dataMimeType, keepAlive, lifetime, metadataMimeType} = config.setup;
const {
dataMimeType,
keepAlive,
lifetime,
metadataMimeType,
} = config.setup;
return {

@@ -148,0 +153,0 @@ data: undefined,

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -15,3 +15,3 @@

Object.defineProperty(exports, '__esModule', {value: true});
exports.BufferEncoders = exports.Utf8Encoders = exports.BufferEncoder = exports.UTF8Encoder = undefined;
exports.BufferEncoders = (exports.Utf8Encoders = (exports.BufferEncoder = (exports.UTF8Encoder = undefined)));

@@ -26,9 +26,9 @@ var _RSocketBufferUtils = require('./RSocketBufferUtils');

/**
* Commonly used subset of the allowed Node Buffer Encoder types.
*/
* Commonly used subset of the allowed Node Buffer Encoder types.
*/
/**
* The Encoders object specifies how values should be serialized/deserialized
* to/from binary.
*/
* The Encoders object specifies how values should be serialized/deserialized
* to/from binary.
*/

@@ -78,4 +78,4 @@ const UTF8Encoder = (exports.UTF8Encoder = {

/**
* Encode all values as UTF8 strings.
*/
* Encode all values as UTF8 strings.
*/
const Utf8Encoders = (exports.Utf8Encoders = {

@@ -91,4 +91,4 @@ data: UTF8Encoder,

/**
* Encode all values as buffers.
*/
* Encode all values as buffers.
*/
const BufferEncoders = (exports.BufferEncoders = {

@@ -95,0 +95,0 @@ data: BufferEncoder,

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -20,3 +20,3 @@

);
exports.MAX_VERSION = exports.MAX_TTL = exports.MAX_STREAM_ID = exports.MAX_RESUME_LENGTH = exports.MAX_REQUEST_N = exports.MAX_REQUEST_COUNT = exports.MAX_MIME_LENGTH = exports.MAX_METADATA_LENGTH = exports.MAX_LIFETIME = exports.MAX_KEEPALIVE = exports.MAX_CODE = exports.FRAME_TYPE_OFFFSET = exports.FLAGS_MASK = exports.ERROR_EXPLANATIONS = exports.ERROR_CODES = exports.FLAGS = exports.FRAME_TYPE_NAMES = exports.FRAME_TYPES = exports.CONNECTION_STREAM_ID = undefined;
exports.MAX_VERSION = (exports.MAX_TTL = (exports.MAX_STREAM_ID = (exports.MAX_RESUME_LENGTH = (exports.MAX_REQUEST_N = (exports.MAX_REQUEST_COUNT = (exports.MAX_MIME_LENGTH = (exports.MAX_METADATA_LENGTH = (exports.MAX_LIFETIME = (exports.MAX_KEEPALIVE = (exports.MAX_CODE = (exports.FRAME_TYPE_OFFFSET = (exports.FLAGS_MASK = (exports.ERROR_EXPLANATIONS = (exports.ERROR_CODES = (exports.FLAGS = (exports.FRAME_TYPE_NAMES = (exports.FRAME_TYPES = (exports.CONNECTION_STREAM_ID = undefined))))))))))))))))));
exports.isIgnore = isIgnore;

@@ -107,41 +107,56 @@ exports.isMetadata = isMetadata;

* Returns true iff the flags have the IGNORE bit set.
*/ function isIgnore(flags) {
*/ function isIgnore(
flags
) {
return (flags & FLAGS.IGNORE) === FLAGS.IGNORE;
}
/**
* Returns true iff the flags have the METADATA bit set.
*/ function isMetadata(flags) {
* Returns true iff the flags have the METADATA bit set.
*/ function isMetadata(
flags
) {
return (flags & FLAGS.METADATA) === FLAGS.METADATA;
}
/**
* Returns true iff the flags have the COMPLETE bit set.
*/ function isComplete(flags) {
* Returns true iff the flags have the COMPLETE bit set.
*/ function isComplete(
flags
) {
return (flags & FLAGS.COMPLETE) === FLAGS.COMPLETE;
}
/**
* Returns true iff the flags have the NEXT bit set.
*/ function isNext(flags) {
* Returns true iff the flags have the NEXT bit set.
*/ function isNext(
flags
) {
return (flags & FLAGS.NEXT) === FLAGS.NEXT;
}
/**
* Returns true iff the flags have the RESPOND bit set.
*/ function isRespond(flags) {
* Returns true iff the flags have the RESPOND bit set.
*/ function isRespond(
flags
) {
return (flags & FLAGS.RESPOND) === FLAGS.RESPOND;
}
/**
* Returns true iff the flags have the RESUME_ENABLE bit set.
*/ function isResumeEnable(flags) {
* Returns true iff the flags have the RESUME_ENABLE bit set.
*/ function isResumeEnable(
flags
) {
return (flags & FLAGS.RESUME_ENABLE) === FLAGS.RESUME_ENABLE;
}
/**
* Returns true iff the flags have the LEASE bit set.
*/ function isLease(flags) {
* Returns true iff the flags have the LEASE bit set.
*/ function isLease(
flags
) {
return (flags & FLAGS.LEASE) === FLAGS.LEASE;
}
/**
* Returns true iff the frame type is counted toward the implied
* client/server position used for the resumption protocol.
*/ function isResumePositionFrameType(type) {
return (
type === FRAME_TYPES.CANCEL ||
* Returns true iff the frame type is counted toward the implied
* client/server position used for the resumption protocol.
*/ function isResumePositionFrameType(
type
) {
return type === FRAME_TYPES.CANCEL ||
type === FRAME_TYPES.ERROR ||

@@ -153,4 +168,3 @@ type === FRAME_TYPES.METADATA_PUSH ||

type === FRAME_TYPES.REQUEST_RESPONSE ||
type === FRAME_TYPES.REQUEST_STREAM
);
type === FRAME_TYPES.REQUEST_STREAM;
}

@@ -162,10 +176,12 @@ function getFrameTypeName(type) {

/**
* Constructs an Error object given the contents of an error frame. The
* `source` property contains metadata about the error for use in introspecting
* the error at runtime:
* - `error.source.code: number`: the error code returned by the server.
* - `error.source.explanation: string`: human-readable explanation of the code
* (this value is not standardized and may change).
* - `error.source.message: string`: the error string returned by the server.
*/ function createErrorFromFrame(frame) {
* Constructs an Error object given the contents of an error frame. The
* `source` property contains metadata about the error for use in introspecting
* the error at runtime:
* - `error.source.code: number`: the error code returned by the server.
* - `error.source.explanation: string`: human-readable explanation of the code
* (this value is not standardized and may change).
* - `error.source.message: string`: the error string returned by the server.
*/ function createErrorFromFrame(
frame
) {
const {code, message} = frame;

@@ -185,5 +201,7 @@ const explanation = getErrorCodeExplanation(code);

/**
* Given a RSocket error code, returns a human-readable explanation of that
* code, following the names used in the protocol specification.
*/ function getErrorCodeExplanation(code) {
* Given a RSocket error code, returns a human-readable explanation of that
* code, following the names used in the protocol specification.
*/ function getErrorCodeExplanation(
code
) {
const explanation = ERROR_EXPLANATIONS[code];

@@ -199,5 +217,7 @@ if (explanation != null) {

/**
* Pretty-prints the frame for debugging purposes, with types, flags, and
* error codes annotated with descriptive names.
*/ function printFrame(frame) {
* Pretty-prints the frame for debugging purposes, with types, flags, and
* error codes annotated with descriptive names.
*/ function printFrame(
frame
) {
const obj = Object.assign({}, frame);

@@ -204,0 +224,0 @@ obj.type = getFrameTypeName(frame.type) + ` (${toHex(frame.type)})`;

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -129,4 +129,4 @@

this._subscriptions = new Map();
this._serializers =
serializers || _RSocketSerialization.IdentitySerializers;
this._serializers = serializers ||
_RSocketSerialization.IdentitySerializers;
this._requestHandler = new ResponderWrapper(requestHandler); // Subscribe to completion/errors before sending anything

@@ -186,4 +186,5 @@ connectionPublisher({

data,
flags:
payload.metadata !== undefined ? _RSocketFrame.FLAGS.METADATA : 0,
flags: payload.metadata !== undefined
? _RSocketFrame.FLAGS.METADATA
: 0,
metadata,

@@ -207,60 +208,62 @@ streamId,

const streamId = this._getNextStreamId();
return new _rsocketFlowable.Flowable(subscriber => {
this._receivers.set(streamId, subscriber);
let initialized = false;
subscriber.onSubscribe({
cancel: () => {
this._receivers.delete(streamId);
if (!initialized) {
return;
}
const cancelFrame = {
flags: 0,
streamId,
type: _RSocketFrame.FRAME_TYPES.CANCEL,
};
this._connection.sendOne(cancelFrame);
},
request: n => {
if (n > _RSocketFrame.MAX_REQUEST_N) {
(0, _warning2.default)(
false,
'RSocketClient: Invalid request value `%s`, the maximum ' +
'value supported by the RSocket protocol is `%s`. Sending ' +
'the maximum supported value instead.',
n,
_RSocketFrame.MAX_REQUEST_N
);
n = _RSocketFrame.MAX_REQUEST_N;
}
if (initialized) {
const requestNFrame = {
return new _rsocketFlowable.Flowable(
subscriber => {
this._receivers.set(streamId, subscriber);
let initialized = false;
subscriber.onSubscribe({
cancel: () => {
this._receivers.delete(streamId);
if (!initialized) {
return;
}
const cancelFrame = {
flags: 0,
requestN: n,
streamId,
type: _RSocketFrame.FRAME_TYPES.REQUEST_N,
type: _RSocketFrame.FRAME_TYPES.CANCEL,
};
this._connection.sendOne(requestNFrame);
} else {
initialized = true;
const data = this._serializers.data.serialize(payload.data);
const metadata = this._serializers.metadata.serialize(
payload.metadata
);
const requestStreamFrame = {
data,
flags:
payload.metadata !== undefined
this._connection.sendOne(cancelFrame);
},
request: n => {
if (n > _RSocketFrame.MAX_REQUEST_N) {
(0, _warning2.default)(
false,
'RSocketClient: Invalid request value `%s`, the maximum ' +
'value supported by the RSocket protocol is `%s`. Sending ' +
'the maximum supported value instead.',
n,
_RSocketFrame.MAX_REQUEST_N
);
n = _RSocketFrame.MAX_REQUEST_N;
}
if (initialized) {
const requestNFrame = {
flags: 0,
requestN: n,
streamId,
type: _RSocketFrame.FRAME_TYPES.REQUEST_N,
};
this._connection.sendOne(requestNFrame);
} else {
initialized = true;
const data = this._serializers.data.serialize(payload.data);
const metadata = this._serializers.metadata.serialize(
payload.metadata
);
const requestStreamFrame = {
data,
flags: payload.metadata !== undefined
? _RSocketFrame.FLAGS.METADATA
: 0,
metadata,
requestN: n,
streamId,
type: _RSocketFrame.FRAME_TYPES.REQUEST_STREAM,
};
this._connection.sendOne(requestStreamFrame);
}
},
});
}, _RSocketFrame.MAX_REQUEST_N);
metadata,
requestN: n,
streamId,
type: _RSocketFrame.FRAME_TYPES.REQUEST_STREAM,
};
this._connection.sendOne(requestStreamFrame);
}
},
});
},
_RSocketFrame.MAX_REQUEST_N
);
}

@@ -286,6 +289,8 @@ requestChannel(payloads) {

/**
* Handle the connection closing normally: this is an error for any open streams.
*/ /**
* Handle the transport connection closing abnormally or a connection-level protocol error.
*/ _handleConnectionError(error) {
* Handle the connection closing normally: this is an error for any open streams.
*/ /**
* Handle the transport connection closing abnormally or a connection-level protocol error.
*/ _handleConnectionError(
error
) {
this._handleError(error);

@@ -295,6 +300,8 @@ this._connection.close();

/**
* Handle a frame received from the transport client.
*/ /**
* Handle connection frames (stream id === 0).
*/ _handleConnectionFrame(frame) {
* Handle a frame received from the transport client.
*/ /**
* Handle connection frames (stream id === 0).
*/ _handleConnectionFrame(
frame
) {
switch (frame.type) {

@@ -347,4 +354,4 @@ case _RSocketFrame.FRAME_TYPES.ERROR:

/**
* Handle stream-specific frames (stream id !== 0).
*/
* Handle stream-specific frames (stream id !== 0).
*/
_handleStreamFrame(streamId, frame) {

@@ -496,4 +503,4 @@ switch (frame.type) {

/**
* Handle an error specific to a stream.
*/
* Handle an error specific to a stream.
*/
_handleStreamError(streamId, error) {

@@ -500,0 +507,0 @@ const receiver = this._receivers.get(streamId);

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -27,58 +27,58 @@

/**
* NOTE: This implementation conforms to an upcoming version of the RSocket protocol
* and will not work with version 1.0 servers.
*
* An implementation of the DuplexConnection interface that supports automatic
* resumption per the RSocket protocol.
*
* # Example
*
* Create a client instance:
* ```
* const client = new RSocketClient({
* ...,
* transport: new RSocketResumableTransport(
* () => new RSocketWebSocketClient(...), // provider for low-level transport instances
* {
* bufferSize: 10, // max number of sent & pending frames to buffer before failing
* resumeToken: 'abc123', // string to uniquely identify the session across connections
* }
* ),
* })
*
* Open the connection. After this if the connection dies it will be auto-resumed:
* ```
* client.connect().subscribe(...);
* ```
*
* Optionally, subscribe to the status of the connection:
* ```
* client.connectionStatus().subscribe(...);
* ```
*
* # Implementation Notes
*
* This transport maintains:
* - _currentConnection: a current low-level transport, which is null when not
* connected
* - _sentFrames: a buffer of frames written to a low-level transport (which
* may or may not have been received by the server)
* - _pendingFrames: a buffer of frames not yet written to the low-level
* connection, because they were sent while not connected.
*
* The initial connection is simple: connect using the low-level transport and
* flush any _pendingFrames (write them and add them to _sentFrames).
*
* Thereafter if the low-level transport drops, this transport attempts resumption.
* It obtains a fresh low-level transport from the given transport `source`
* and attempts to connect. Once connected, it sends a RESUME frame and waits.
* If RESUME_OK is received, _sentFrames and _pendingFrames are adjusted such
* that:
* - any frames the server has received are removed from _sentFrames
* - the remaining frames are merged (in correct order) into _pendingFrames
*
* Then the connection proceeds as above, where all pending frames are flushed.
* If anything other than RESUME_OK is received, resumption is considered to
* have failed and the connection is set to the ERROR status.
*/
* NOTE: This implementation conforms to an upcoming version of the RSocket protocol
* and will not work with version 1.0 servers.
*
* An implementation of the DuplexConnection interface that supports automatic
* resumption per the RSocket protocol.
*
* # Example
*
* Create a client instance:
* ```
* const client = new RSocketClient({
* ...,
* transport: new RSocketResumableTransport(
* () => new RSocketWebSocketClient(...), // provider for low-level transport instances
* {
* bufferSize: 10, // max number of sent & pending frames to buffer before failing
* resumeToken: 'abc123', // string to uniquely identify the session across connections
* }
* ),
* })
*
* Open the connection. After this if the connection dies it will be auto-resumed:
* ```
* client.connect().subscribe(...);
* ```
*
* Optionally, subscribe to the status of the connection:
* ```
* client.connectionStatus().subscribe(...);
* ```
*
* # Implementation Notes
*
* This transport maintains:
* - _currentConnection: a current low-level transport, which is null when not
* connected
* - _sentFrames: a buffer of frames written to a low-level transport (which
* may or may not have been received by the server)
* - _pendingFrames: a buffer of frames not yet written to the low-level
* connection, because they were sent while not connected.
*
* The initial connection is simple: connect using the low-level transport and
* flush any _pendingFrames (write them and add them to _sentFrames).
*
* Thereafter if the low-level transport drops, this transport attempts resumption.
* It obtains a fresh low-level transport from the given transport `source`
* and attempts to connect. Once connected, it sends a RESUME frame and waits.
* If RESUME_OK is received, _sentFrames and _pendingFrames are adjusted such
* that:
* - any frames the server has received are removed from _sentFrames
* - the remaining frames are merged (in correct order) into _pendingFrames
*
* Then the connection proceeds as above, where all pending frames are flushed.
* If anything other than RESUME_OK is received, resumption is considered to
* have failed and the connection is set to the ERROR status.
*/
class RSocketResumableTransport {

@@ -263,60 +263,56 @@ constructor(source, options) {

_handleResume(connection) {
connection
.receive()
.take(1)
.subscribe({
onNext: frame => {
try {
if (frame.type === _RSocketFrame.FRAME_TYPES.RESUME_OK) {
const {clientPosition} = frame;
// clientPosition indicates which frames the server is missing:
// - anything after that still needs to be sent
// - anything before that can be discarded
if (clientPosition < this._position.client) {
// Invalid RESUME_OK frame: server asked for an older
// client frame than is available
this._close(
new Error(
'RSocketResumableTransport: Resumption failed, server is ' +
'missing frames that are no longer in the client buffer.'
)
);
return;
}
// Extract "sent" frames that the server hasn't received...
const unreceivedSentFrames = this._sentFrames.slice(
clientPosition - this._position.client
connection.receive().take(1).subscribe({
onNext: frame => {
try {
if (frame.type === _RSocketFrame.FRAME_TYPES.RESUME_OK) {
const {clientPosition} = frame;
// clientPosition indicates which frames the server is missing:
// - anything after that still needs to be sent
// - anything before that can be discarded
if (clientPosition < this._position.client) {
// Invalid RESUME_OK frame: server asked for an older
// client frame than is available
this._close(
new Error(
'RSocketResumableTransport: Resumption failed, server is ' +
'missing frames that are no longer in the client buffer.'
)
);
// ...and mark them as pending again
this._pendingFrames = [
...unreceivedSentFrames,
...this._pendingFrames,
];
return;
}
// Extract "sent" frames that the server hasn't received...
const unreceivedSentFrames = this._sentFrames.slice(
clientPosition - this._position.client
);
// Drop sent frames that the server has received
this._sentFrames.length = clientPosition - this._position.client;
// Continue connecting, which will flush pending frames
this._handleConnected(connection);
} else {
const error =
frame.type === _RSocketFrame.FRAME_TYPES.ERROR
? (0, _RSocketFrame.createErrorFromFrame)(frame)
: new Error(
'RSocketResumableTransport: Resumption failed for an ' +
'unspecified reason.'
);
// ...and mark them as pending again
this._pendingFrames = [
...unreceivedSentFrames,
...this._pendingFrames,
];
this._close(error);
}
} catch (error) {
// Drop sent frames that the server has received
this._sentFrames.length = clientPosition - this._position.client;
// Continue connecting, which will flush pending frames
this._handleConnected(connection);
} else {
const error = frame.type === _RSocketFrame.FRAME_TYPES.ERROR
? (0, _RSocketFrame.createErrorFromFrame)(frame)
: new Error(
'RSocketResumableTransport: Resumption failed for an ' +
'unspecified reason.'
);
this._close(error);
}
},
onSubscribe: subscription => {
this._receiveSubscription = subscription;
subscription.request(1);
},
});
} catch (error) {
this._close(error);
}
},
onSubscribe: subscription => {
this._receiveSubscription = subscription;
subscription.request(1);
},
});

@@ -323,0 +319,0 @@ const setupFrame = this._setupFrame;

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -15,3 +15,3 @@

Object.defineProperty(exports, '__esModule', {value: true});
exports.IdentitySerializers = exports.IdentitySerializer = exports.JsonSerializers = exports.JsonSerializer = undefined;
exports.IdentitySerializers = (exports.IdentitySerializer = (exports.JsonSerializers = (exports.JsonSerializer = undefined)));

@@ -18,0 +18,0 @@ var _buffer = require('buffer');

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -28,5 +28,5 @@

/**
* RSocketServer: A server in an RSocket connection that accepts connections
* from peers via the given transport server.
*/
* RSocketServer: A server in an RSocket connection that accepts connections
* from peers via the given transport server.
*/
class RSocketServer {

@@ -134,5 +134,4 @@ constructor(config) {

_getSerializers() {
return (
this._config.serializers || _RSocketSerialization.IdentitySerializers
);
return this._config.serializers ||
_RSocketSerialization.IdentitySerializers;
}

@@ -139,0 +138,0 @@ }

@@ -9,3 +9,3 @@ /**

*
*
*
*/

@@ -12,0 +12,0 @@

{
"name": "rsocket-core",
"description": "RSocket core",
"version": "0.0.4",
"version": "0.0.5",
"repository": {

@@ -14,5 +14,5 @@ "type": "git",

"fbjs": "^0.8.12",
"rsocket-flowable": "^0.0.4",
"rsocket-types": "^0.0.4"
"rsocket-flowable": "^0.0.5",
"rsocket-types": "^0.0.5"
}
}

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc