Skip to content

Commit

Permalink
fix(rpc-tcp): make sure unixsocket folder is created
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Jan 21, 2024
1 parent 67e2fed commit 00fd4ed
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/rpc-tcp/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { asyncOperation, ParsedHost, parseHost } from '@deepkit/core';
import { RpcKernel } from '@deepkit/rpc';
import { existsSync, unlinkSync } from 'fs';
import { existsSync, mkdirSync, unlinkSync } from 'fs';
import { createServer, Server, Socket } from 'net';
import type { ServerOptions as WebSocketServerOptions } from 'ws';
import { WebSocketServer } from 'ws';
import { IncomingMessage } from 'http';
import { dirname } from 'path';

/**
* Uses the node `net` module to create a server. Supports unix sockets.
Expand All @@ -15,11 +16,12 @@ export class RpcTcpServer {

constructor(
protected kernel: RpcKernel,
host: string
host: string,
) {
this.host = parseHost(host);
if (this.host.isUnixSocket && existsSync(this.host.unixSocket)) {
unlinkSync(this.host.unixSocket);
if (this.host.isUnixSocket) {
if (existsSync(this.host.unixSocket)) unlinkSync(this.host.unixSocket);
mkdirSync(dirname(this.host.unixSocket), { recursive: true });
}
}

Expand Down Expand Up @@ -48,7 +50,7 @@ export class RpcTcpServer {
},
bufferedAmount(): number {
return socket.writableLength || 0;
}
},
});

socket.on('data', (data: Uint8Array) => {
Expand Down Expand Up @@ -83,11 +85,12 @@ export class RpcWebSocketServer {

constructor(
protected kernel: RpcKernel,
host: string
host: string,
) {
this.host = parseHost(host);
if (this.host.isUnixSocket && existsSync(this.host.unixSocket)) {
unlinkSync(this.host.unixSocket);
if (existsSync(this.host.unixSocket)) unlinkSync(this.host.unixSocket);
mkdirSync(dirname(this.host.unixSocket), { recursive: true });
}
}

Expand All @@ -112,7 +115,7 @@ export class RpcWebSocketServer {
},
clientAddress(): string {
return req.socket.remoteAddress || '';
}
},
});

ws.on('message', async (message: Uint8Array) => {
Expand Down

0 comments on commit 00fd4ed

Please sign in to comment.