Skip to content

Commit

Permalink
add ability to override codec in transport impls
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Nov 30, 2023
1 parent 239309a commit 136a00e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 12 additions & 1 deletion transport/impls/stdio.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { Codec } from '../../codec';
import { NaiveJsonCodec } from '../../codec/json';
import { OpaqueTransportMessage, TransportClientId } from '../message';
import { Transport } from '../types';
import readline from 'readline';

interface Options {
codec: Codec;
}

const defaultOptions: Options = {
codec: NaiveJsonCodec,
};

/**
* A transport implementation that uses standard input and output streams.
* @extends Transport
Expand All @@ -27,8 +36,10 @@ export class StdioTransport extends Transport {
clientId: TransportClientId,
input: NodeJS.ReadableStream = process.stdin,
output: NodeJS.WritableStream = process.stdout,
providedOptions?: Partial<Options>,
) {
super(NaiveJsonCodec, clientId);
const options = { ...defaultOptions, ...providedOptions };
super(options.codec, clientId);
this.input = input;
this.output = output;
const rl = readline.createInterface({
Expand Down
12 changes: 8 additions & 4 deletions transport/impls/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import {
TransportClientId,
} from '../message';
import { log } from '../../logging';
import { type Codec } from '../../codec';

interface Options {
retryIntervalMs: number;
codec: Codec;
}

const defaultOptions: Options = {
retryIntervalMs: 250,
codec: NaiveJsonCodec,
};

type WebSocketResult = { ws: WebSocket } | { err: string };
Expand Down Expand Up @@ -53,17 +56,18 @@ export class WebSocketTransport extends Transport {
* Creates a new WebSocketTransport instance.
* @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
* @param clientId The ID of the client using the transport.
* @param options An optional object containing configuration options for the transport.
* @param providedOptions An optional object containing configuration options for the transport.
*/
constructor(
wsGetter: () => Promise<WebSocket>,
clientId: TransportClientId,
options?: Partial<Options>,
providedOptions?: Partial<Options>,
) {
super(NaiveJsonCodec, clientId);
const options = { ...defaultOptions, ...providedOptions };
super(options.codec, clientId);
this.destroyed = false;
this.wsGetter = wsGetter;
this.options = { ...defaultOptions, ...options };
this.options = options;
this.sendQueue = [];
this.tryConnect();
}
Expand Down

0 comments on commit 136a00e

Please sign in to comment.