Skip to content

Commit

Permalink
fix: Fix ws connection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Mar 21, 2021
1 parent a1e499c commit 71f6a90
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/serializers/serializer-impl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'isomorphic-form-data';

import { camelCase, snakeCase } from 'change-case';
import { ParsedUrlQuery, stringify } from 'querystring';

import { flattenObject } from './form-data';
import { MimeType, Serializer } from './serializer';
Expand All @@ -23,6 +24,8 @@ export class SerializerImpl implements Serializer {
.forEach(([key, value]) => formData.append(key, value as string));
return formData;
}
case 'application/x-www-form-urlencoded':
return stringify(data as ParsedUrlQuery);
default:
return;
}
Expand Down
5 changes: 4 additions & 1 deletion src/serializers/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export type MimeType = 'application/json' | 'multipart/form-data';
export type MimeType =
| 'application/json'
| 'multipart/form-data'
| 'application/x-www-form-urlencoded';

export interface Serializer {
serialize(type: MimeType, data: Record<string, unknown>): unknown;
Expand Down
18 changes: 9 additions & 9 deletions src/ws/ws-impl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import EventEmitter from 'eventemitter3';
import WebSocket from 'isomorphic-ws';
import querystring, { ParsedUrlQuery } from 'querystring';
import semver from 'semver';

import { Serializer } from '../serializers';
Expand Down Expand Up @@ -33,9 +32,9 @@ export class WsEventsImpl
return new Promise<WsEvents>((resolve, reject) => {
const ws = new WebSocket(url, protocols);
const instance = new WsEventsImpl(ws, serializer);
ws.addEventListener('open', () => resolve(instance));
ws.addEventListener('message', instance.handleMessage);
ws.addEventListener('error', reject);
ws.addEventListener('open', () => resolve(instance));
});
}

Expand All @@ -51,7 +50,7 @@ export class WsEventsImpl
* Parse JSON data and emit it as an event
* @param message Websocket message
*/
handleMessage = ({ data }: { data: string }) => {
private handleMessage = ({ data }: { data: string }) => {
const event = this.serializer.deserialize<Event>('application/json', data);
let args: EventTypeMap[EventType] = [];

Expand All @@ -73,7 +72,10 @@ export class WsImpl implements Ws {
private readonly accessToken?: string,
) {}

stream(path: string, rawParams: Record<string, unknown>): Promise<WsEvents> {
stream(
path: string,
rawParams: Record<string, unknown> = {},
): Promise<WsEvents> {
// Since v2.8.4, it is supported to pass access token with`Sec-Websocket-Protocol`
// https://github.com/tootsuite/mastodon/pull/10818
const protocols = [];
Expand All @@ -87,11 +89,9 @@ export class WsImpl implements Ws {
rawParams.accessToken = this.accessToken;
}

const params = querystring.stringify(
this.serializer.serialize(
'application/json',
rawParams,
) as ParsedUrlQuery,
const params = this.serializer.serialize(
'application/x-www-form-urlencoded',
rawParams,
);

const url =
Expand Down
5 changes: 4 additions & 1 deletion src/ws/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export interface Event {
export interface WsEvents {
// readonly connect: () => Promise<WsEvents>;
readonly disconnect: () => void;
readonly on: (name: string, cb: () => void) => void;
readonly on: <T extends EventType>(
name: T,
cb: (...data: EventTypeMap[T]) => void,
) => void;
}

export interface Ws {
Expand Down

0 comments on commit 71f6a90

Please sign in to comment.