From 4d7955b2e939e7fe224ecaf3fcd6f00bf8fb1413 Mon Sep 17 00:00:00 2001 From: Oleh Dokuka Date: Fri, 22 Nov 2019 17:44:23 +0200 Subject: [PATCH] Provides Aligning of RSocket-xxx definitions with the latest RSocket-JS release (#40580) * provides alignment with the latest RSocket-JS * provides alignment with the latest RSocket-JS Signed-off-by: Oleh Dokuka --- types/rsocket-core/CompositeMetadata.d.ts | 167 +++++++++++++++ types/rsocket-core/RSocketBinaryFraming.d.ts | 200 +----------------- types/rsocket-core/RSocketBufferUtils.d.ts | 7 +- types/rsocket-core/RSocketClient.d.ts | 20 +- types/rsocket-core/RSocketEncoding.d.ts | 1 - types/rsocket-core/RSocketLease.d.ts | 72 +++++++ types/rsocket-core/RSocketMachine.d.ts | 77 ++----- .../RSocketResumableTransport.d.ts | 31 ++- types/rsocket-core/RSocketServer.d.ts | 40 +--- types/rsocket-core/WellKnownMimeType.d.ts | 95 +++++++++ types/rsocket-core/index.d.ts | 61 ++++++ types/rsocket-core/tsconfig.json | 5 +- types/rsocket-types/ReactiveSocketTypes.d.ts | 198 +++++++++-------- types/rsocket-types/index.d.ts | 1 + .../RSocketWebSocketClient.d.ts | 13 +- types/rsocket-websocket-client/index.d.ts | 1 + .../RSocketWebSocketServer.d.ts | 1 - types/rsocket-websocket-server/index.d.ts | 1 + 18 files changed, 563 insertions(+), 428 deletions(-) create mode 100644 types/rsocket-core/CompositeMetadata.d.ts create mode 100644 types/rsocket-core/RSocketLease.d.ts create mode 100644 types/rsocket-core/WellKnownMimeType.d.ts diff --git a/types/rsocket-core/CompositeMetadata.d.ts b/types/rsocket-core/CompositeMetadata.d.ts new file mode 100644 index 0000000000..9cc3c02aad --- /dev/null +++ b/types/rsocket-core/CompositeMetadata.d.ts @@ -0,0 +1,167 @@ +import WellKnownMimeType from './WellKnownMimeType'; + +export class CompositeMetadata { + constructor(buffer: Buffer); + + [Symbol.iterator](): Iterator; +} + +/** + * Encode a new sub-metadata information into a composite metadata {@link CompositeByteBuf + * buffer}, without checking if the {@link String} can be matched with a well known compressable + * mime type. Prefer using this method and {@link #encodeAndAddMetadata(CompositeByteBuf, + * ByteBufAllocator, WellKnownMimeType, ByteBuf)} if you know in advance whether or not the mime + * is well known. Otherwise use {@link #encodeAndAddMetadataWithCompression(CompositeByteBuf, + * ByteBufAllocator, String, ByteBuf)} + * + * @param compositeMetaData the buffer that will hold all composite metadata information. + * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. + * @param customMimeType the custom mime type to encode. + * @param metadata the metadata value to encode. + */ +// see #encodeMetadataHeader(ByteBufAllocator, String, int) +export function encodeAndAddCustomMetadata(compositeMetaData: Buffer, customMimeType: string, metadata: Buffer): Buffer; + +/** + * Encode a new sub-metadata information into a composite metadata {@link CompositeByteBuf + * buffer}. + * + * @param compositeMetaData the buffer that will hold all composite metadata information. + * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. + * @param knownMimeType the {@link WellKnownMimeType} to encode. + * @param metadata the metadata value to encode. + */ +// see #encodeMetadataHeader(ByteBufAllocator, byte, int) +export function encodeAndAddWellKnownMetadata( + compositeMetaData: Buffer, + knownMimeType: WellKnownMimeType | number, + metadata: Buffer +): Buffer; + +/** + * Decode the next metadata entry (a mime header + content pair of {@link ByteBuf}) from a {@link + * ByteBuf} that contains at least enough bytes for one more such entry. These buffers are + * actually slices of the full metadata buffer, and this method doesn't move the full metadata + * buffer's {@link ByteBuf#readerIndex()}. As such, it requires the user to provide an {@code + * index} to read from. The next index is computed by calling {@link #computeNextEntryIndex(int, + * ByteBuf, ByteBuf)}. Size of the first buffer (the "header buffer") drives which decoding method + * should be further applied to it. + * + *

The header buffer is either: + * + *

    + *
  • made up of a single byte: this represents an encoded mime id, which can be further + * decoded using {@link #decodeMimeIdFromMimeBuffer(ByteBuf)} + *
  • made up of 2 or more bytes: this represents an encoded mime String + its length, which + * can be further decoded using {@link #decodeMimeTypeFromMimeBuffer(ByteBuf)}. Note the + * encoded length, in the first byte, is skipped by this decoding method because the + * remaining length of the buffer is that of the mime string. + *
+ * + * @param compositeMetadata the source {@link ByteBuf} that originally contains one or more + * metadata entries + * @param entryIndex the {@link ByteBuf#readerIndex()} to start decoding from. original reader + * index is kept on the source buffer + * @param retainSlices should produced metadata entry buffers {@link ByteBuf#slice() slices} be + * {@link ByteBuf#retainedSlice() retained}? + * @return a {@link ByteBuf} array of length 2 containing the mime header buffer + * slice and the content buffer slice, or one of the + * zero-length error constant arrays + */ +export function decodeMimeAndContentBuffersSlices(compositeMetadata: Buffer, entryIndex: number): Buffer[]; + +/** + * Decode a {@link CharSequence} custome mime type from a {@link ByteBuf}, assuming said buffer + * properly contains such a mime type. + * + *

The buffer must at least have two readable bytes, which distinguishes it from the {@link + * #decodeMimeIdFromMimeBuffer(ByteBuf) compressed id} case. The first byte is a size and the + * remaining bytes must correspond to the {@link CharSequence}, encoded fully in US_ASCII. As a + * result, the first byte can simply be skipped, and the remaining of the buffer be decoded to the + * mime type. + * + *

If the mime header buffer is less than 2 bytes long, returns {@code null}. + * + * @param flyweightMimeBuffer the mime header {@link ByteBuf} that contains length + custom mime + * type + * @return the decoded custom mime type, as a {@link CharSequence}, or null if the input is + * invalid + * @see #decodeMimeIdFromMimeBuffer(ByteBuf) + */ +export function decodeMimeTypeFromMimeBuffer(flyweightMimeBuffer: Buffer): string; + +export function encodeCustomMetadataHeader(customMime: string, metadataLength: number): Buffer; + +/** + * Encode a {@link WellKnownMimeType well known mime type} and a metadata value length into a + * newly allocated {@link ByteBuf}. + * + *

This compact representation encodes the mime type via its ID on a single byte, and the + * unsigned value length on 3 additional bytes. + * + * @param allocator the {@link ByteBufAllocator} to use to create the buffer. + * @param mimeType a byte identifier of a {@link WellKnownMimeType} to encode. + * @param metadataLength the metadata length to append to the buffer as an unsigned 24 bits + * integer. + * @return the encoded mime and metadata length information + */ +export function encodeWellKnownMetadataHeader(mimeType: number, metadataLength: number): Buffer; + +export interface Entry { + /** + * Returns the un-decoded content of the {@link Entry}. + * + * @return the un-decoded content of the {@link Entry} + */ + readonly content: Buffer; + + /** + * Returns the MIME type of the entry, if it can be decoded. + * + * @return the MIME type of the entry, if it can be decoded, otherwise {@code null}. + */ + readonly mimeType: string | undefined; +} + +export class ExplicitMimeTimeEntry implements Entry { + constructor(content: Buffer, type: string); + + readonly content: Buffer; + + readonly mimeType: string; +} + +export class ReservedMimeTypeEntry implements Entry { + constructor(content: Buffer, type: number); + + readonly content: Buffer; + + /** + * {@inheritDoc} Since this entry represents a compressed id that couldn't be decoded, this is + * always {@code null}. + */ + readonly mimeType: undefined; + + /** + * Returns the reserved, but unknown {@link WellKnownMimeType} for this entry. Range is 0-127 + * (inclusive). + * + * @return the reserved, but unknown {@link WellKnownMimeType} for this entry + */ + readonly type: number; +} + +export class WellKnownMimeTypeEntry implements Entry { + constructor(content: Buffer, type: WellKnownMimeType); + + readonly content: Buffer; + + readonly mimeType: string; + + /** + * Returns the {@link WellKnownMimeType} for this entry. + * + * @return the {@link WellKnownMimeType} for this entry + */ + readonly type: WellKnownMimeType; +} diff --git a/types/rsocket-core/RSocketBinaryFraming.d.ts b/types/rsocket-core/RSocketBinaryFraming.d.ts index 9a371e8f6a..93590da9d7 100644 --- a/types/rsocket-core/RSocketBinaryFraming.d.ts +++ b/types/rsocket-core/RSocketBinaryFraming.d.ts @@ -1,42 +1,9 @@ /// - import { - CancelFrame, - ErrorFrame, - Frame, - KeepAliveFrame, - LeaseFrame, - PayloadFrame, - RequestChannelFrame, - RequestFnfFrame, - RequestNFrame, - RequestResponseFrame, - RequestStreamFrame, - ResumeFrame, - ResumeOkFrame, - SetupFrame, -} from 'rsocket-types'; +import { Frame } from 'rsocket-types'; import { Encoders } from './RSocketEncoding'; -export interface FrameWithPayload { - data: any; - flags: number; - metadata: any; -} - -/** - * Frame header is: - * - stream id (uint32 = 4) - * - type + flags (uint 16 = 2) - */ -export const FRAME_HEADER_SIZE = 6; - -/** - * Size of frame length and metadata length fields. - */ -export const UINT24_SIZE = 3; - /** * Reads a frame from a buffer that is prefixed with the frame length. */ @@ -65,167 +32,6 @@ export function deserializeFrame(buffer: Buffer, encoders?: Encoders): Fram export function serializeFrame(frame: Frame, encoders?: Encoders): Buffer; /** - * 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) + * Byte size of frame without size prefix */ -export const SETUP_FIXED_SIZE = 14; -export const RESUME_TOKEN_LENGTH_SIZE = 2; -export function serializeSetupFrame(frame: SetupFrame, encoders: Encoders): Buffer; - -/** - * Reads a SETUP frame from the buffer and returns it. - */ -export function deserializeSetupFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): SetupFrame; - -/** - * Writes an ERROR frame into a new buffer and returns it. - * - * Prefix size is for the error code (uint32 = 4). - */ -export const ERROR_FIXED_SIZE = 4; -export function serializeErrorFrame(frame: ErrorFrame, encoders: Encoders): Buffer; - -/** - * Reads an ERROR frame from the buffer and returns it. - */ -export function deserializeErrorFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): ErrorFrame; - -/** - * Writes a KEEPALIVE frame into a new buffer and returns it. - * - * Prefix size is for the last received position (uint64 = 8). - */ -export const KEEPALIVE_FIXED_SIZE = 8; -export function serializeKeepAliveFrame(frame: KeepAliveFrame, encoders: Encoders): Buffer; - -/** - * Reads a KEEPALIVE frame from the buffer and returns it. - */ -export function deserializeKeepAliveFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): KeepAliveFrame; - -/** - * Writes a LEASE frame into a new buffer and returns it. - * - * Prefix size is for the ttl (uint32) and requestcount (uint32). - */ -export const LEASE_FIXED_SIZE = 8; -export function serializeLeaseFrame(frame: LeaseFrame, encoders: Encoders): Buffer; - -/** - * Reads a LEASE frame from the buffer and returns it. - */ -export function deserializeLeaseFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): LeaseFrame; -/** - * 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. - */ -export function serializeRequestFrame(frame: RequestFnfFrame | RequestResponseFrame, encoders: Encoders): Buffer; - -export function deserializeRequestFnfFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): RequestFnfFrame; - -export function deserializeRequestResponseFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): RequestResponseFrame; - -/** - * 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). - */ -export const REQUEST_MANY_HEADER = 4; -export function serializeRequestManyFrame(frame: RequestStreamFrame | RequestChannelFrame, encoders: Encoders): Buffer; - -export function deserializeRequestStreamFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): RequestStreamFrame; - -export function deserializeRequestChannelFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): RequestChannelFrame; - -/** - * Writes a REQUEST_N frame to a new buffer and returns it. - * - * Prefix size is for requestN (uint32 = 4). - */ -export const REQUEST_N_HEADER = 4; -export function serializeRequestNFrame(frame: RequestNFrame, encoders: Encoders): Buffer; - -export function deserializeRequestNFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): RequestNFrame; - -/** - * Writes a CANCEL frame to a new buffer and returns it. - */ -export function serializeCancelFrame(frame: CancelFrame, encoders: Encoders): Buffer; - -export function deserializeCancelFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): CancelFrame; - -/** - * Writes a PAYLOAD frame to a new buffer and returns it. - */ -export function serializePayloadFrame(frame: PayloadFrame, encoders: Encoders): Buffer; - -export function deserializePayloadFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): PayloadFrame; - -/** - * 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) - */ -export const RESUME_FIXED_SIZE = 22; -export function serializeResumeFrame(frame: ResumeFrame, encoders: Encoders): Buffer; - -export function deserializeResumeFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): ResumeFrame; - -/** - * Writes a RESUME_OK frame into a new buffer and returns it. - * - * Fixed size is: - * - client position (uint64 = 8) - */ -export const RESUME_OK_FIXED_SIZE = 8; -export function serializeResumeOkFrame(frame: ResumeOkFrame, encoders: Encoders): Buffer; - -export function deserializeResumeOkFrame(buffer: Buffer, streamId: number, flags: number, encoders: Encoders): ResumeOkFrame; - -/** - * Write the header of the frame into the buffer. - */ -export function writeHeader(frame: Frame, buffer: Buffer): number; - -/** - * Determine the length of the payload section of a frame. Only applies to - * frame types that MAY have both metadata and data. - */ -export function getPayloadLength(frame: FrameWithPayload, encoders: Encoders): number; - -/** - * Write the payload of a frame into the given buffer. Only applies to frame - * types that MAY have both metadata and data. - */ -export function writePayload( - frame: FrameWithPayload, - buffer: Buffer, - encoders: Encoders, - offset: number -): void; - -/** - * Read the payload from a buffer and write it into the frame. Only applies to - * frame types that MAY have both metadata and data. - */ -export function readPayload( - buffer: Buffer, - frame: FrameWithPayload, - encoders: Encoders, - offset: number -): void; +export function sizeOfFrame(frame: Frame, encoders?: Encoders): number; diff --git a/types/rsocket-core/RSocketBufferUtils.d.ts b/types/rsocket-core/RSocketBufferUtils.d.ts index f809da74ce..08c1acd7b8 100644 --- a/types/rsocket-core/RSocketBufferUtils.d.ts +++ b/types/rsocket-core/RSocketBufferUtils.d.ts @@ -1,11 +1,6 @@ /// - export type Encoding = "ascii" | "base64" | "hex" | "utf8"; - -/** - * Mimimum value that would overflow bitwise operators (2^32). - */ -export const BITWISE_OVERFLOW = 0x100000000; +export type Encoding = 'ascii' | 'base64' | 'hex' | 'utf8'; /** * Read a uint24 from a buffer starting at the given offset. diff --git a/types/rsocket-core/RSocketClient.d.ts b/types/rsocket-core/RSocketClient.d.ts index b7a7da4c0b..2ee846bc8d 100644 --- a/types/rsocket-core/RSocketClient.d.ts +++ b/types/rsocket-core/RSocketClient.d.ts @@ -1,11 +1,14 @@ /// -import { ConnectionStatus, DuplexConnection, Payload, ReactiveSocket, SetupFrame, Responder } from 'rsocket-types'; +import { DuplexConnection, Payload, ReactiveSocket, Responder } from 'rsocket-types'; import { PayloadSerializers } from './RSocketSerialization'; -import { Flowable, Single } from 'rsocket-flowable'; +import { Single } from 'rsocket-flowable'; +import { Leases } from './RSocketLease'; + export interface ClientConfig { serializers?: PayloadSerializers; setup: { + payload?: Payload, dataMimeType: string; keepAlive: number; lifetime: number; @@ -13,6 +16,8 @@ export interface ClientConfig { }; transport: DuplexConnection; responder?: Partial>; + errorHandler?: (error: Error) => void; + leases?: () => Leases; } /** @@ -30,14 +35,3 @@ export default class RSocketClient { close(): void; connect(): Single>; } - -export class RSocketClientSocket implements ReactiveSocket { - constructor(config: ClientConfig, connection: DuplexConnection); - fireAndForget(payload: Payload): void; - requestResponse(payload: Payload): Single>; - requestStream(payload: Payload): Flowable>; - requestChannel(payloads: Flowable>): Flowable>; - metadataPush(payload: Payload): Single; - close(): void; - connectionStatus(): Flowable; -} diff --git a/types/rsocket-core/RSocketEncoding.d.ts b/types/rsocket-core/RSocketEncoding.d.ts index 5381f8337f..51197807cd 100644 --- a/types/rsocket-core/RSocketEncoding.d.ts +++ b/types/rsocket-core/RSocketEncoding.d.ts @@ -1,7 +1,6 @@ /// import { Encodable } from 'rsocket-types'; -import { byteLength } from './RSocketBufferUtils'; /** * Commonly used subset of the allowed Node Buffer Encoder types. diff --git a/types/rsocket-core/RSocketLease.d.ts b/types/rsocket-core/RSocketLease.d.ts new file mode 100644 index 0000000000..dfea549b71 --- /dev/null +++ b/types/rsocket-core/RSocketLease.d.ts @@ -0,0 +1,72 @@ +import { Encodable, LeaseFrame } from 'rsocket-types'; +import { Flowable } from 'rsocket-flowable'; + +export type EventType = 'Accept' | 'Reject' | 'Terminate'; + +export interface LeaseStats { + onEvent(event: EventType): void; +} + +export interface Disposable { + dispose(): void; + + isDisposed(): boolean; +} + +export class Lease { + allowedRequests: number; + startingAllowedRequests: number; + timeToLiveMillis: number; + expiry: number; + metadata?: Encodable; + + constructor(timeToLiveMillis: number, allowedRequests: number, metadata?: Encodable); + + expired(): boolean; + + valid(): boolean; +} + +export class Leases { + sender(sender: (t?: T) => Flowable): Leases; + + receiver(receiver: (flowable: Flowable) => void): Leases; + + stats(stats: T): Leases; +} + +export interface LeaseHandler { + use(): boolean; + + errorMessage(): string; +} + +export class RequesterLeaseHandler implements LeaseHandler, Disposable { + constructor(leaseReceiver: (flowable: Flowable) => void); + + use(): boolean; + + errorMessage(): string; + + receive(frame: LeaseFrame): void; + + availability(): number; + + dispose(): void; + + isDisposed(): boolean; +} + +export class ResponderLeaseHandler implements LeaseHandler { + constructor( + leaseSender: (leaseStats?: LeaseStats) => Flowable, + stats?: LeaseStats, + errorConsumer?: (e: Error) => void + ); + + use(): boolean; + + errorMessage(): string; + + send(send: (lease: Lease) => void): Disposable; +} diff --git a/types/rsocket-core/RSocketMachine.d.ts b/types/rsocket-core/RSocketMachine.d.ts index 4e45e09cad..ddfcab2b93 100644 --- a/types/rsocket-core/RSocketMachine.d.ts +++ b/types/rsocket-core/RSocketMachine.d.ts @@ -1,69 +1,28 @@ -import { - CancelFrame, - ConnectionStatus, - DuplexConnection, - Frame, - FrameWithData, - Payload, - Responder, - ReactiveSocket, - RequestFnfFrame, - RequestNFrame, - RequestResponseFrame, - RequestStreamFrame, - RequestChannelFrame, - ISubject, - ISubscription, - ISubscriber -} from 'rsocket-types'; - -import { Flowable, FlowableProcessor, Single } from 'rsocket-flowable'; -import { - createErrorFromFrame, - getFrameTypeName, - isComplete, - isNext, - isRespond, - CONNECTION_STREAM_ID, - ERROR_CODES, - FLAGS, - FRAME_TYPES, - MAX_REQUEST_N, - MAX_STREAM_ID, -} from './RSocketFrame'; -import { PayloadSerializers, IdentitySerializers } from './RSocketSerialization'; - -export type Role = 'CLIENT' | 'SERVER'; - -export class ResponderWrapper implements Responder { - constructor(responder: Partial>) - setResponder(responder: Partial>): void; - fireAndForget(payload: Payload): void; - requestResponse(payload: Payload): Single>; - requestStream(payload: Payload): Flowable>; - requestChannel(payloads: Flowable>): Flowable>; - metadataPush(payload: Payload): Single; -} +import { DuplexConnection, Frame, ISubscriber, ReactiveSocket, Responder } from 'rsocket-types'; +import { PayloadSerializers } from './RSocketSerialization'; +import { RequesterLeaseHandler, ResponderLeaseHandler } from './RSocketLease'; export interface RSocketMachine extends ReactiveSocket { setRequestHandler(requestHandler?: Partial>): void; } export function createServerMachine( - connection: DuplexConnection, - connectionPublisher: (partialSubscriber: Partial>) => void, - serializers?: PayloadSerializers, - requestHandler?: Partial>, + connection: DuplexConnection, + connectionPublisher: (partialSubscriber: Partial>) => void, + keepAliveTimeout: number, + serializers?: PayloadSerializers, + errorHandler?: (e: Error) => void, + requesterLeaseHandler?: RequesterLeaseHandler, + responderLeaseHandler?: ResponderLeaseHandler ): RSocketMachine; export function createClientMachine( - connection: DuplexConnection, - connectionPublisher: (partialSubscriber: Partial>) => void, - serializers?: PayloadSerializers, - requestHandler?: Partial>, + connection: DuplexConnection, + connectionPublisher: (partialSubscriber: Partial>) => void, + keepAliveTimeout: number, + serializers?: PayloadSerializers, + requestHandler?: Partial>, + errorHandler?: (e: Error) => void, + requesterLeaseHandler?: RequesterLeaseHandler, + responderLeaseHandler?: ResponderLeaseHandler ): RSocketMachine; - -export function deserializePayload( - serializers: PayloadSerializers, - frame: FrameWithData, -): Payload; diff --git a/types/rsocket-core/RSocketResumableTransport.d.ts b/types/rsocket-core/RSocketResumableTransport.d.ts index 005f9d38ef..3438b81713 100644 --- a/types/rsocket-core/RSocketResumableTransport.d.ts +++ b/types/rsocket-core/RSocketResumableTransport.d.ts @@ -1,17 +1,12 @@ -import { ConnectionStatus, DuplexConnection, Frame, SetupFrame, ISubject, ISubscription, CONNECTION_STATUS } from 'rsocket-types'; +import { ConnectionStatus, DuplexConnection, Encodable, Frame } from 'rsocket-types'; import { Flowable } from 'rsocket-flowable'; -import { - createErrorFromFrame, - isResumePositionFrameType, - CONNECTION_STREAM_ID, - FLAGS, - FRAME_TYPES, -} from './RSocketFrame'; +import { Encoders } from './RSocketEncoding'; export interface Options { - bufferSize: number; - resumeToken: string; + bufferSize: number; + resumeToken: Encodable; + sessionDurationSeconds: number; } /** @@ -72,11 +67,11 @@ export interface Options { * have failed and the connection is set to the ERROR status. */ export default class RSocketResumableTransport implements DuplexConnection { - constructor(source: () => DuplexConnection, options: Options) - close(): void; - connect(): void; - connectionStatus(): Flowable; - receive(): Flowable; - sendOne(frame: Frame): void; - send(frames: Flowable): void; -} + constructor(source: () => DuplexConnection, options: Options, encoders?: Encoders); + close(): void; + connect(): void; + connectionStatus(): Flowable; + receive(): Flowable; + sendOne(frame: Frame): void; + send(frames: Flowable): void; + } diff --git a/types/rsocket-core/RSocketServer.d.ts b/types/rsocket-core/RSocketServer.d.ts index 88c53a8d86..f8dba247ee 100644 --- a/types/rsocket-core/RSocketServer.d.ts +++ b/types/rsocket-core/RSocketServer.d.ts @@ -1,23 +1,8 @@ -import { - DuplexConnection, - Frame, - FrameWithData, - Payload, - Responder, - ReactiveSocket, - ISubscription, - ISubscriber -} from 'rsocket-types'; -import { IdentitySerializers, PayloadSerializers } from './RSocketSerialization'; +import { DuplexConnection, Payload, ReactiveSocket, Responder } from 'rsocket-types'; +import { PayloadSerializers } from './RSocketSerialization'; import { Flowable } from 'rsocket-flowable'; -import { - getFrameTypeName, - CONNECTION_STREAM_ID, - ERROR_CODES, - FRAME_TYPES, -} from './RSocketFrame'; -import { createServerMachine } from './RSocketMachine'; +import { Leases } from './RSocketLease'; export interface TransportServer { start: () => Flowable; @@ -25,9 +10,11 @@ export interface TransportServer { } export interface ServerConfig { - getRequestHandler: (socket: ReactiveSocket, payload: Payload) => Partial>; - serializers?: PayloadSerializers; - transport: TransportServer; + getRequestHandler: (socket: ReactiveSocket, payload: Payload) => Partial>; + serializers?: PayloadSerializers; + transport: TransportServer; + errorHandler?: (e: Error) => void; + leases?: () => Leases; } /** @@ -39,14 +26,3 @@ export default class RSocketServer { start(): void; stop(): void; } - -export class SubscriberSwapper implements ISubscriber { - constructor(target?: Partial>); - swap(next: Partial>): ISubscriber; - onComplete(): void; - onError(error: Error): void; - onNext(value: T): void; - onSubscribe(subscription: ISubscription): void; -} - -export function deserializePayload(serializers: PayloadSerializers, frame: FrameWithData): Payload; diff --git a/types/rsocket-core/WellKnownMimeType.d.ts b/types/rsocket-core/WellKnownMimeType.d.ts new file mode 100644 index 0000000000..b1ea904789 --- /dev/null +++ b/types/rsocket-core/WellKnownMimeType.d.ts @@ -0,0 +1,95 @@ +export default class WellKnownMimeType { + constructor(str: string, identifier: number); + + /** + * Find the {@link WellKnownMimeType} for the given identifier (as an {@code int}). Valid + * identifiers are defined to be integers between 0 and 127, inclusive. Identifiers outside of + * this range will produce the {@link #UNPARSEABLE_MIME_TYPE}. Additionally, some identifiers in + * that range are still only reserved and don't have a type associated yet: this method returns + * the {@link #UNKNOWN_RESERVED_MIME_TYPE} when passing such an identifier, which lets call sites + * potentially detect this and keep the original representation when transmitting the associated + * metadata buffer. + * + * @param id the looked up identifier + * @return the {@link WellKnownMimeType}, or {@link #UNKNOWN_RESERVED_MIME_TYPE} if the id is out + * of the specification's range, or {@link #UNKNOWN_RESERVED_MIME_TYPE} if the id is one that + * is merely reserved but unknown to this implementation. + */ + static fromIdentifier(id: number): WellKnownMimeType; + + /** + * Find the {@link WellKnownMimeType} for the given {@link String} representation. If the + * representation is {@code null} or doesn't match a {@link WellKnownMimeType}, the {@link + * #UNPARSEABLE_MIME_TYPE} is returned. + * + * @param mimeType the looked up mime type + * @return the matching {@link WellKnownMimeType}, or {@link #UNPARSEABLE_MIME_TYPE} if none matches + */ + static fromString(mimeType: string): WellKnownMimeType; + + /** @return the byte identifier of the mime type, guaranteed to be positive or zero. */ + readonly identifier: number; + + /** + * @return the mime type represented as a {@link String}, which is made of US_ASCII compatible + * characters only + */ + readonly string: string; + + /** @see #getString() */ + toString(): string; +} + +export const UNPARSEABLE_MIME_TYPE: WellKnownMimeType; +export const UNKNOWN_RESERVED_MIME_TYPE: WellKnownMimeType; + +export const APPLICATION_AVRO: WellKnownMimeType; +export const APPLICATION_CBOR: WellKnownMimeType; +export const APPLICATION_GRAPHQL: WellKnownMimeType; +export const APPLICATION_GZIP: WellKnownMimeType; +export const APPLICATION_JAVASCRIPT: WellKnownMimeType; +export const APPLICATION_JSON: WellKnownMimeType; +export const APPLICATION_OCTET_STREAM: WellKnownMimeType; +export const APPLICATION_PDF: WellKnownMimeType; +export const APPLICATION_THRIFT: WellKnownMimeType; +export const APPLICATION_PROTOBUF: WellKnownMimeType; +export const APPLICATION_XML: WellKnownMimeType; +export const APPLICATION_ZIP: WellKnownMimeType; +export const AUDIO_AAC: WellKnownMimeType; +export const AUDIO_MP3: WellKnownMimeType; +export const AUDIO_MP4: WellKnownMimeType; +export const AUDIO_MPEG3: WellKnownMimeType; +export const AUDIO_MPEG: WellKnownMimeType; +export const AUDIO_OGG: WellKnownMimeType; +export const AUDIO_OPUS: WellKnownMimeType; +export const AUDIO_VORBIS: WellKnownMimeType; +export const IMAGE_BMP: WellKnownMimeType; +export const IMAGE_GIG: WellKnownMimeType; +export const IMAGE_HEIC_SEQUENCE: WellKnownMimeType; +export const IMAGE_HEIC: WellKnownMimeType; +export const IMAGE_HEIF_SEQUENCE: WellKnownMimeType; +export const IMAGE_HEIF: WellKnownMimeType; +export const IMAGE_JPEG: WellKnownMimeType; +export const IMAGE_PNG: WellKnownMimeType; +export const IMAGE_TIFF: WellKnownMimeType; +export const MULTIPART_MIXED: WellKnownMimeType; +export const TEXT_CSS: WellKnownMimeType; +export const TEXT_CSV: WellKnownMimeType; +export const TEXT_HTML: WellKnownMimeType; +export const TEXT_PLAIN: WellKnownMimeType; +export const TEXT_XML: WellKnownMimeType; +export const VIDEO_H264: WellKnownMimeType; +export const VIDEO_H265: WellKnownMimeType; +export const VIDEO_VP8: WellKnownMimeType; +export const APPLICATION_HESSIAN: WellKnownMimeType; +export const APPLICATION_JAVA_OBJECT: WellKnownMimeType; +export const APPLICATION_CLOUDEVENTS_JSON: WellKnownMimeType; + +// ... reserved for future use ... + +export const MESSAGE_RSOCKET_TRACING_ZIPKIN: WellKnownMimeType; +export const MESSAGE_RSOCKET_ROUTING: WellKnownMimeType; +export const MESSAGE_RSOCKET_COMPOSITE_METADATA: WellKnownMimeType; + +export const TYPES_BY_MIME_ID: WellKnownMimeType[]; +export const TYPES_BY_MIME_STRING: Map; diff --git a/types/rsocket-core/index.d.ts b/types/rsocket-core/index.d.ts index e021527f05..debd4388db 100644 --- a/types/rsocket-core/index.d.ts +++ b/types/rsocket-core/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for rsocket-core 0.0 // Project: https://github.com/rsocket/rsocket-js/ // Definitions by: Adrian Hope-Bailie +// Oleh Dokuka // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -15,6 +16,8 @@ import RSocketServer from "./RSocketServer"; export { RSocketServer }; import RSocketResumableTransport from "./RSocketResumableTransport"; export { RSocketResumableTransport }; +import WellKnownMimeType from './WellKnownMimeType'; +export { WellKnownMimeType }; export { CONNECTION_STREAM_ID, ERROR_CODES, @@ -67,3 +70,61 @@ export { JsonSerializer, JsonSerializers } from "./RSocketSerialization"; +export { LeaseStats, Leases, Lease } from './RSocketLease'; +export { + UNPARSEABLE_MIME_TYPE, + UNKNOWN_RESERVED_MIME_TYPE, + APPLICATION_AVRO, + APPLICATION_CBOR, + APPLICATION_GRAPHQL, + APPLICATION_GZIP, + APPLICATION_JAVASCRIPT, + APPLICATION_JSON, + APPLICATION_OCTET_STREAM, + APPLICATION_PDF, + APPLICATION_THRIFT, + APPLICATION_PROTOBUF, + APPLICATION_XML, + APPLICATION_ZIP, + AUDIO_AAC, + AUDIO_MP3, + AUDIO_MP4, + AUDIO_MPEG3, + AUDIO_MPEG, + AUDIO_OGG, + AUDIO_OPUS, + AUDIO_VORBIS, + IMAGE_BMP, + IMAGE_GIG, + IMAGE_HEIC_SEQUENCE, + IMAGE_HEIC, + IMAGE_HEIF_SEQUENCE, + IMAGE_HEIF, + IMAGE_JPEG, + IMAGE_PNG, + IMAGE_TIFF, + MULTIPART_MIXED, + TEXT_CSS, + TEXT_CSV, + TEXT_HTML, + TEXT_PLAIN, + TEXT_XML, + VIDEO_H264, + VIDEO_H265, + VIDEO_VP8, + APPLICATION_HESSIAN, + APPLICATION_JAVA_OBJECT, + APPLICATION_CLOUDEVENTS_JSON, + MESSAGE_RSOCKET_TRACING_ZIPKIN, + MESSAGE_RSOCKET_ROUTING, + MESSAGE_RSOCKET_COMPOSITE_METADATA, +} from './WellKnownMimeType'; +export { + Entry, + CompositeMetadata, + ReservedMimeTypeEntry, + WellKnownMimeTypeEntry, + ExplicitMimeTimeEntry, + encodeAndAddCustomMetadata, + encodeAndAddWellKnownMetadata, +} from './CompositeMetadata'; diff --git a/types/rsocket-core/tsconfig.json b/types/rsocket-core/tsconfig.json index 5ab85bd408..90bab9c1fa 100644 --- a/types/rsocket-core/tsconfig.json +++ b/types/rsocket-core/tsconfig.json @@ -24,10 +24,13 @@ "RSocketClient.d.ts", "RSocketEncoding.d.ts", "RSocketFrame.d.ts", + "RSocketLease.d.ts", "RSocketMachine.d.ts", "RSocketResumableTransport.d.ts", "RSocketSerialization.d.ts", "RSocketServer.d.ts", - "RSocketVersion.d.ts" + "RSocketVersion.d.ts", + "CompositeMetadata.d.ts", + "WellKnownMimeType.d.ts" ] } diff --git a/types/rsocket-types/ReactiveSocketTypes.d.ts b/types/rsocket-types/ReactiveSocketTypes.d.ts index 66c6ce1a8d..4ee8880d5a 100644 --- a/types/rsocket-types/ReactiveSocketTypes.d.ts +++ b/types/rsocket-types/ReactiveSocketTypes.d.ts @@ -39,18 +39,24 @@ export interface Responder { * (https://github.com/ReactiveSocket/reactivesocket/blob/master/Protocol.md). */ export interface ReactiveSocket extends Responder { - /** - * Close this `ReactiveSocket` and the underlying transport connection. - */ - close(): void; + /** + * Close this `ReactiveSocket` and the underlying transport connection. + */ + close(): void; - /** - * Returns a Flowable that immediately publishes the current connection - * status and thereafter updates as it changes. Once a connection is in - * the CLOSED or ERROR state, it may not be connected again. - * Implementations must publish values per the comments on ConnectionStatus. - */ - connectionStatus(): Flowable; + /** + * Returns a Flowable that immediately publishes the current connection + * status and thereafter updates as it changes. Once a connection is in + * the CLOSED or ERROR state, it may not be connected again. + * Implementations must publish values per the comments on ConnectionStatus. + */ + connectionStatus(): Flowable; + + /** + * Returns positive number representing the availability of RSocket requester. Higher is better, 0.0 + * means not available. + */ + availability(): number; } /** @@ -160,120 +166,134 @@ export interface FrameWithData { } export interface CancelFrame { - type: 0x09; - flags: number; - streamId: number; + type: 0x09; + flags: number; + streamId: number; + length?: number; } export interface ErrorFrame { - type: 0x0B; - flags: number; - code: number; - message: string; - streamId: number; + type: 0x0b; + flags: number; + code: number; + message: string; + streamId: number; + length?: number; } export interface KeepAliveFrame { - type: 0x03; - flags: number; - data?: Encodable; - lastReceivedPosition: number; - streamId: 0; + type: 0x03; + flags: number; + data?: Encodable; + lastReceivedPosition: number; + streamId: 0; + length?: number; } export interface LeaseFrame { - type: 0x02; - flags: number; - ttl: number; - requestCount: number; - metadata?: Encodable; - streamId: 0; + type: 0x02; + flags: number; + ttl: number; + requestCount: number; + metadata?: Encodable; + streamId: 0; + length?: number; } export interface PayloadFrame { - type: 0x0A; - flags: number; - data?: Encodable; - metadata?: Encodable; - streamId: number; + type: 0x0a; + flags: number; + data?: Encodable; + metadata?: Encodable; + streamId: number; + length?: number; } export interface RequestChannelFrame { - type: 0x07; - data?: Encodable; - metadata?: Encodable; - flags: number; - requestN: number; - streamId: number; + type: 0x07; + data?: Encodable; + metadata?: Encodable; + flags: number; + requestN: number; + streamId: number; + length?: number; } export interface RequestFnfFrame { - type: 0x05; - data?: Encodable; - metadata?: Encodable; - flags: number; - streamId: number; + type: 0x05; + data?: Encodable; + metadata?: Encodable; + flags: number; + streamId: number; + length?: number; } export interface RequestNFrame { - type: 0x08; - flags: number; - requestN: number; - streamId: number; + type: 0x08; + flags: number; + requestN: number; + streamId: number; + length?: number; } - +// prettier-ignore export interface RequestResponseFrame { - type: 0x04; - data?: Encodable; - metadata?: Encodable; - flags: number; - streamId: number; + type: 0x04; + data?: Encodable; + metadata?: Encodable; + flags: number; + streamId: number; + length?: number; } export interface RequestStreamFrame { - type: 0x06; - data: Encodable; - metadata: Encodable; - flags: number; - requestN: number; - streamId: number; + type: 0x06; + data: Encodable; + metadata: Encodable; + flags: number; + requestN: number; + streamId: number; + length?: number; } export interface ResumeFrame { - type: 0x0d; - clientPosition: number; - flags: number; - majorVersion: number; - minorVersion: number; - resumeToken: Encodable; - serverPosition: number; - streamId: 0; + type: 0x0d; + clientPosition: number; + flags: number; + majorVersion: number; + minorVersion: number; + resumeToken: Encodable; + serverPosition: number; + streamId: 0; + length?: number; } export interface ResumeOkFrame { - type: 0x0e; - clientPosition: number; - flags: number; - streamId: 0; + type: 0x0e; + clientPosition: number; + flags: number; + streamId: 0; + length?: number; } export interface SetupFrame { - type: 0x01; - data?: Encodable; - dataMimeType: string; - flags: number; - keepAlive: number; - lifetime: number; - metadata?: Encodable; - metadataMimeType: string; - resumeToken?: Encodable; - streamId: 0; - majorVersion: number; - minorVersion: number; + type: 0x01; + data?: Encodable; + dataMimeType: string; + flags: number; + keepAlive: number; + lifetime: number; + metadata?: Encodable; + metadataMimeType: string; + resumeToken?: Encodable; + streamId: 0; + majorVersion: number; + minorVersion: number; + length?: number; } export interface UnsupportedFrame { - type: 0x3f | 0x0c | 0x00; - streamId: 0; - flags: number; + type: 0x3f | 0x0c | 0x00; + streamId: 0; + flags: number; + length?: number; } diff --git a/types/rsocket-types/index.d.ts b/types/rsocket-types/index.d.ts index bce6bc8112..c4fc13ef3d 100644 --- a/types/rsocket-types/index.d.ts +++ b/types/rsocket-types/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for rsocket-types 0.0 // Project: https://github.com/rsocket/rsocket-js/ // Definitions by: Adrian Hope-Bailie +// Oleh Dokuka // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 diff --git a/types/rsocket-websocket-client/RSocketWebSocketClient.d.ts b/types/rsocket-websocket-client/RSocketWebSocketClient.d.ts index d1894fe8ff..d5a8672c8c 100644 --- a/types/rsocket-websocket-client/RSocketWebSocketClient.d.ts +++ b/types/rsocket-websocket-client/RSocketWebSocketClient.d.ts @@ -1,16 +1,7 @@ -import { ConnectionStatus, DuplexConnection, Frame, ISubject, ISubscriber, ISubscription, CONNECTION_STATUS } from 'rsocket-types'; +import { ConnectionStatus, DuplexConnection, Frame } from 'rsocket-types'; import { Flowable } from 'rsocket-flowable'; -import { - deserializeFrame, - deserializeFrameWithLength, - Encoders, - printFrame, - serializeFrame, - serializeFrameWithLength, - toBuffer, -} from 'rsocket-core'; -import * as ws from 'ws'; +import { Encoders } from 'rsocket-core'; export interface ClientOptions { url: string; diff --git a/types/rsocket-websocket-client/index.d.ts b/types/rsocket-websocket-client/index.d.ts index 31638e54b5..bf74e2e8e1 100644 --- a/types/rsocket-websocket-client/index.d.ts +++ b/types/rsocket-websocket-client/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for rsocket-websocket-client 0.0 // Project: https://github.com/rsocket/rsocket-js/ // Definitions by: Adrian Hope-Bailie +// Oleh Dokuka // Bryce Matheson // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 diff --git a/types/rsocket-websocket-server/RSocketWebSocketServer.d.ts b/types/rsocket-websocket-server/RSocketWebSocketServer.d.ts index 50b55b89a9..6a46b98d76 100644 --- a/types/rsocket-websocket-server/RSocketWebSocketServer.d.ts +++ b/types/rsocket-websocket-server/RSocketWebSocketServer.d.ts @@ -1,6 +1,5 @@ import { DuplexConnection } from 'rsocket-types'; import { Encoders, TransportServer } from 'rsocket-core'; -import { EventEmitter } from 'events'; import { Flowable } from 'rsocket-flowable'; import * as ws from 'ws'; diff --git a/types/rsocket-websocket-server/index.d.ts b/types/rsocket-websocket-server/index.d.ts index 95a591b800..aec4c59d47 100644 --- a/types/rsocket-websocket-server/index.d.ts +++ b/types/rsocket-websocket-server/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for rsocket-websocket-server 0.0 // Project: https://github.com/rsocket/rsocket-js/ // Definitions by: Adrian Hope-Bailie +// Oleh Dokuka // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2