Skip to content

Commit

Permalink
fix(rpc): issue with ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Oct 3, 2023
1 parent 7a1027a commit c7d87c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/rpc-tcp/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParsedHost, parseHost } from '@deepkit/core';
import { parseHost } from '@deepkit/core';
import { ClientTransportAdapter, TransportConnectionHooks } from '@deepkit/rpc';
import { connect } from 'net';

Expand Down
11 changes: 5 additions & 6 deletions packages/rpc-tcp/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { asyncOperation, ParsedHost, parseHost } from '@deepkit/core';
import { RpcKernel } from '@deepkit/rpc';
import { existsSync, unlinkSync } from 'fs';
import { createServer, Server, Socket } from 'net';
import type { ServerOptions as WebSocketServerOptions } from 'ws';
import { WebSocketServer } from 'ws';
import { IncomingMessage } from 'http';

/**
* Uses the node `net` module to create a server. Supports unix sockets.
Expand Down Expand Up @@ -74,12 +77,8 @@ export class RpcTcpServer {
}
}

import * as ws from 'ws';
import type { ServerOptions as WebSocketServerOptions } from 'ws';
import { IncomingMessage } from 'http';

export class RpcWebSocketServer {
protected server?: ws.Server;
protected server?: WebSocketServer;
protected host: ParsedHost;

constructor(
Expand All @@ -98,7 +97,7 @@ export class RpcWebSocketServer {

start(options: WebSocketServerOptions): void {
const defaultOptions = { host: this.host.host, port: this.host.port };
this.server = new ws.Server({ ...defaultOptions, ...options });
this.server = new WebSocketServer({ ...defaultOptions, ...options });

this.server.on('connection', (ws, req: IncomingMessage) => {
const connection = this.kernel?.createConnection({
Expand Down
8 changes: 5 additions & 3 deletions packages/rpc/src/client/client-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ export function createRpcWebSocketClientProvider(baseUrl: string = typeof locati
};
}

declare var require: (module: string) => any;
let webSocketConstructor: typeof WebSocket | undefined = undefined;

export class RpcWebSocketClientAdapter implements ClientTransportAdapter {
constructor(public url: string) {
}

public async connect(connection: TransportConnectionHooks) {
const wsPackage = 'ws';
const webSocketConstructor = 'undefined' === typeof WebSocket && 'undefined' !== typeof require ? require(wsPackage) : WebSocket;
if (!webSocketConstructor) {
webSocketConstructor = 'undefined' === typeof WebSocket ? (await import(wsPackage)).WebSocket : WebSocket;
}

const socket = new webSocketConstructor(this.url);
const socket = new webSocketConstructor!(this.url);
socket.binaryType = 'arraybuffer';

socket.onmessage = (event: MessageEvent) => {
Expand Down

0 comments on commit c7d87c5

Please sign in to comment.