Skip to content

Commit e685587

Browse files
committed
feat: adding support for platform sockets
Signed-off-by: Pawel Psztyc <jarrodek@gmail.com>
1 parent 45206db commit e685587

File tree

5 files changed

+250
-126
lines changed

5 files changed

+250
-126
lines changed

src/Server.d.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ export class Server {
1414
opts: ServerConfiguration;
1515
apiHandler: ApiRoutes;
1616

17+
/**
18+
* This function can be used to create a temporary directory where a socket will be put.
19+
* @returns A path to a temporary folder where the socket path can be created.
20+
*/
21+
static createSocketPath(): Promise<string>;
22+
23+
/**
24+
* Use this with combination with the `Server.createSocketPath()`.
25+
*
26+
* ```javascript
27+
* const socketName = 'my-socket.sock';
28+
* const socketPath = await Server.createSocketPath();
29+
* const socketLocation = Server.createPlatformSocket(socketPath, socketName);
30+
* ```
31+
*
32+
* @param socketPath The path to the socket.
33+
* @param socketName The socket name.
34+
* @returns The platform specific socket path.,
35+
*/
36+
static createPlatformSocket(socketPath: string, socketName: string): string;
37+
1738
/**
1839
* @param opts Optional server configuration options.
1940
*/
@@ -34,38 +55,38 @@ export class Server {
3455

3556
/**
3657
* Starts the www server on a given port.
37-
* @param port The port number to use.
58+
* @param portOrSocket The port number to use or a socket path
3859
*/
39-
startHttp(port: number): Promise<void>;
60+
startHttp(portOrSocket: number|string): Promise<void>;
4061

4162
/**
4263
* Stops a running www server, if any.
4364
*
44-
* @param port When specified it closes a www server on a specific port. When not it stops all running http servers.
65+
* @param portOrSocket When specified it closes a www server on a specific port/socket. When not it stops all running http servers.
4566
*/
46-
stopHttp(port?: number): Promise<void[]>;
67+
stopHttp(portOrSocket?: number|string): Promise<void[]>;
4768

4869
/**
4970
* Starts the www over SSL server on a given port.
5071
*
5172
* @param sslOptions The SSL options to use when creating the server.
52-
* @param port The port number to use.
73+
* @param port The port number to use or a socket path
5374
*/
5475
startSsl(sslOptions: https.ServerOptions, port: number): Promise<void>;
5576

5677
/**
5778
* Stops a running www over SSL server, if any.
5879
*
59-
* @param port When specified it closes an ssl server on a specific port. When not it stops all running https servers.
80+
* @param portOrSocket When specified it closes an ssl server on a specific port/socket. When not it stops all running https servers.
6081
*/
61-
stopSsl(port?: number): Promise<void[]>;
82+
stopSsl(portOrSocket?: number|string): Promise<void[]>;
6283

6384
/**
6485
*
6586
* @param type The server type to stop.
66-
* @param port The optional port of the server.
87+
* @param portOrSocket The optional port of the server.
6788
*/
68-
_stop(type: SupportedServer, port: number): Promise<void[]>;
89+
_stop(type: SupportedServer, portOrSocket: number|string): Promise<void[]>;
6990

7091
_stopServer(server: https.Server | http.Server): Promise<void>;
7192
}

src/Server.js

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Koa from 'koa';
22
import http from 'http';
33
import https from 'https';
44
import cors from '@koa/cors';
5+
import { dir } from 'tmp-promise';
6+
import { platform } from 'os';
7+
import path from 'path';
58
import { ApiRoutes } from './ApiRoutes.js';
69

710
/** @typedef {import('@koa/cors').Options} CorsOptions */
@@ -14,6 +17,35 @@ import { ApiRoutes } from './ApiRoutes.js';
1417
* A web server that exposes an API to parse API projects with the AMF parser.
1518
*/
1619
export class Server {
20+
/**
21+
* This function can be used to create a temporary directory where a socket will be put.
22+
* @returns {Promise<string>} A path to a temporary folder where the socket path can be created.
23+
*/
24+
static async createSocketPath() {
25+
const tmpObj = await dir({ unsafeCleanup: true });
26+
return tmpObj.path;
27+
}
28+
29+
/**
30+
* Use this with combination with the `Server.createSocketPath()`.
31+
*
32+
* ```javascript
33+
* const socketName = 'my-socket.sock';
34+
* const socketPath = await Server.createSocketPath();
35+
* const socketLocation = Server.createPlatformSocket(socketPath, socketName);
36+
* ```
37+
*
38+
* @param {string} socketPath The path to the socket.
39+
* @param {string} socketName The socket name.
40+
* @returns {string} The platform specific socket path.,
41+
*/
42+
static createPlatformSocket(socketPath, socketName) {
43+
if (platform() === 'win32') {
44+
return path.join('\\\\?\\pipe', socketPath, socketName);
45+
}
46+
return path.join(socketPath, socketName)
47+
}
48+
1749
/**
1850
* @param {ServerConfiguration=} opts Optional server configuration options.
1951
*/
@@ -55,18 +87,18 @@ export class Server {
5587

5688
/**
5789
* Starts the www server on a given port.
58-
* @param {number} port The port number to use.
90+
* @param {number|string} portOrSocket The port number to use or a socket path
5991
* @returns {Promise<void>}
6092
*/
61-
startHttp(port) {
93+
startHttp(portOrSocket) {
6294
return new Promise((resolve) => {
6395
const server = http.createServer(this.app.callback());
6496
this.servers.push({
6597
server,
6698
type: 'http',
67-
port,
99+
portOrSocket,
68100
});
69-
server.listen(port, () => {
101+
server.listen(portOrSocket, () => {
70102
resolve();
71103
});
72104
});
@@ -75,28 +107,28 @@ export class Server {
75107
/**
76108
* Stops a running www server, if any.
77109
*
78-
* @param {number=} port When specified it closes a www server on a specific port. When not it stops all running http servers.
110+
* @param {number|string=} portOrSocket When specified it closes a www server on a specific port/socket. When not it stops all running http servers.
79111
* @returns {Promise<void[]>}
80112
*/
81-
stopHttp(port) {
82-
return this._stop('http', port);
113+
stopHttp(portOrSocket) {
114+
return this._stop('http', portOrSocket);
83115
}
84116

85117
/**
86118
* Starts the www over SSL server on a given port.
87119
*
88120
* @param {https.ServerOptions} sslOptions The SSL options to use when creating the server.
89-
* @param {number} port The port number to use.
121+
* @param {number|string} portOrSocket The port number to use or a socket path
90122
*/
91-
startSsl(sslOptions, port) {
123+
startSsl(sslOptions, portOrSocket) {
92124
return new Promise((resolve) => {
93125
const server = https.createServer(sslOptions, this.app.callback());
94126
this.servers.push({
95127
server,
96128
type: 'https',
97-
port,
129+
portOrSocket,
98130
});
99-
server.listen(port, () => {
131+
server.listen(portOrSocket, () => {
100132
resolve();
101133
});
102134
});
@@ -105,25 +137,25 @@ export class Server {
105137
/**
106138
* Stops a running www over SSL server, if any.
107139
*
108-
* @param {number=} port When specified it closes an ssl server on a specific port. When not it stops all running https servers.
140+
* @param {number|string=} portOrSocket When specified it closes an ssl server on a specific port/socket. When not it stops all running https servers.
109141
* @returns {Promise<void[]>}
110142
*/
111-
stopSsl(port) {
112-
return this._stop('https', port);
143+
stopSsl(portOrSocket) {
144+
return this._stop('https', portOrSocket);
113145
}
114146

115147
/**
116148
*
117149
* @param {SupportedServer} type The server type to stop.
118-
* @param {number} port The optional port of the server.
150+
* @param {number|string=} portOrSocket The optional port of the server.
119151
* @returns {Promise<void[]>}
120152
* @private
121153
*/
122-
_stop(type, port) {
154+
_stop(type, portOrSocket) {
123155
const toStop = this.servers.filter((s) => {
124156
if (s.type === type) {
125-
if (port) {
126-
return port === s.port;
157+
if (portOrSocket) {
158+
return portOrSocket === s.portOrSocket;
127159
}
128160
return true;
129161
}

0 commit comments

Comments
 (0)