Skip to content

Commit

Permalink
feat(ws-client, ws-adapter): allows to pass a context object as an ad…
Browse files Browse the repository at this point in the history
…dress query parameter
  • Loading branch information
rafamel committed Nov 6, 2019
1 parent 29bb394 commit 653d45a
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 10 deletions.
23 changes: 21 additions & 2 deletions packages/ws-adapter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/ws-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
},
"dependencies": {
"@karmic/core": "^0.2.0",
"@karmic/rpc": "^0.2.0"
"@karmic/rpc": "^0.2.0",
"query-string": "^6.8.3"
},
"peerDependencies": {
"rxjs": "6.x",
Expand Down
8 changes: 7 additions & 1 deletion packages/ws-adapter/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RPCServer, DataOutput, DataInput } from '@karmic/rpc';
import { RPCAdapterOptions, WSAdapter } from './types';
import createDefaults from './defaults';
import { Subject } from 'rxjs';
import qs from 'query-string';

const codes = {
TryAgain: 1013,
Expand All @@ -17,10 +18,15 @@ export default function adapter(
const rpc = new RPCServer(collection, opts);

opts.server.on('connection', (ws, req) => {
let context = {};
try {
const json = req.url ? qs.parseUrl(req.url).query.context : '{}';
context = JSON.parse((Array.isArray(json) ? json[0] : json) || '{}');
} catch (err) {}
let didClose = false;
const subject = new Subject<DataInput>();
const disconnect = rpc.connect({
context: () => opts.context(req, ws),
context: () => opts.context(context, req, ws),
actions: {
send: (data: DataOutput) => {
return new Promise((resolve, reject) => {
Expand Down
1 change: 1 addition & 0 deletions packages/ws-adapter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface RPCAdapterOptionsOnly {
}

export type RPCAdapterProvideContext<T = any> = (
context: { [key: string]: any },
req: http.IncomingMessage,
ws: WebSocket
) => Promise<T> | T;
23 changes: 21 additions & 2 deletions packages/ws-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/ws-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"@karmic/rpc": "^0.2.0",
"errorish": "^0.4.0",
"isomorphic-ws": "^4.0.1",
"promist": "^2.0.0"
"promist": "^2.0.0",
"query-string": "^6.8.3"
},
"peerDependencies": {
"rxjs": "6.x",
Expand Down
1 change: 1 addition & 0 deletions packages/ws-client/src/WSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class WSClient extends RPCClient {
const opts = Object.assign(createDefaults(), options);
const connection = connect(
address,
opts.context,
wso || {},
opts.attempts,
opts.connectTimeout
Expand Down
3 changes: 2 additions & 1 deletion packages/ws-client/src/connect/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const RECONNECT_DELAY = 5000;

export function connect(
address: string,
context: object,
wso: WebSocket.ClientOptions,
attempts: number,
timeout: number
Expand All @@ -30,7 +31,7 @@ export function connect(
retries = 0;
});

const connection = connectEach(address, wso, timeout);
const connection = connectEach(address, context, wso, timeout);
const subscription = connection.events$.subscribe({
next(value) {
if (!active) return;
Expand Down
9 changes: 8 additions & 1 deletion packages/ws-client/src/connect/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {
} from '@karmic/rpc';
import { Promist } from 'promist';
import { ensure } from 'errorish';
import qs from 'query-string';

// It's duck typing day
export const isNative = !Object.hasOwnProperty.call(WebSocket, 'Server');

export function connectEach(
address: string,
context: object,
wso: WebSocket.ClientOptions,
timeout?: number
): RPCClientConnection {
Expand All @@ -38,7 +40,12 @@ export function connectEach(

let socket: null | WebSocket = null;
try {
socket = isNative ? new WebSocket(address) : new WebSocket(address, wso);
const item = qs.parseUrl(address);
const query = Object.assign({}, item.query, {
context: JSON.stringify(context)
});
const url = item.url + '?' + qs.stringify(query);
socket = isNative ? new WebSocket(url) : new WebSocket(url, wso);
} catch (err) {
// failing asynchronously; if it fails synchronously
// events$ will complete before it is subscribed to on ./connect.ts
Expand Down
3 changes: 2 additions & 1 deletion packages/ws-client/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { WSClientOptionsOnly } from './types';
export function createDefaults(): Required<WSClientOptionsOnly> {
return {
attempts: 0,
connectTimeout: 7500
connectTimeout: 7500,
context: {}
};
}
4 changes: 4 additions & 0 deletions packages/ws-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export interface WSClientOptionsOnly {
* Connection timeout in milliseconds. It will cause all pending requests to fail. `0` for *infinity.* Default: `7500`.
*/
connectTimeout?: number;
/**
* A context object to be passed to the server as a query parameter.
*/
context?: object;
}

0 comments on commit 653d45a

Please sign in to comment.