@@ -2,6 +2,9 @@ import Koa from 'koa';
2
2
import http from 'http' ;
3
3
import https from 'https' ;
4
4
import cors from '@koa/cors' ;
5
+ import { dir } from 'tmp-promise' ;
6
+ import { platform } from 'os' ;
7
+ import path from 'path' ;
5
8
import { ApiRoutes } from './ApiRoutes.js' ;
6
9
7
10
/** @typedef {import('@koa/cors').Options } CorsOptions */
@@ -14,6 +17,35 @@ import { ApiRoutes } from './ApiRoutes.js';
14
17
* A web server that exposes an API to parse API projects with the AMF parser.
15
18
*/
16
19
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
+
17
49
/**
18
50
* @param {ServerConfiguration= } opts Optional server configuration options.
19
51
*/
@@ -55,18 +87,18 @@ export class Server {
55
87
56
88
/**
57
89
* 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
59
91
* @returns {Promise<void> }
60
92
*/
61
- startHttp ( port ) {
93
+ startHttp ( portOrSocket ) {
62
94
return new Promise ( ( resolve ) => {
63
95
const server = http . createServer ( this . app . callback ( ) ) ;
64
96
this . servers . push ( {
65
97
server,
66
98
type : 'http' ,
67
- port ,
99
+ portOrSocket ,
68
100
} ) ;
69
- server . listen ( port , ( ) => {
101
+ server . listen ( portOrSocket , ( ) => {
70
102
resolve ( ) ;
71
103
} ) ;
72
104
} ) ;
@@ -75,28 +107,28 @@ export class Server {
75
107
/**
76
108
* Stops a running www server, if any.
77
109
*
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.
79
111
* @returns {Promise<void[]> }
80
112
*/
81
- stopHttp ( port ) {
82
- return this . _stop ( 'http' , port ) ;
113
+ stopHttp ( portOrSocket ) {
114
+ return this . _stop ( 'http' , portOrSocket ) ;
83
115
}
84
116
85
117
/**
86
118
* Starts the www over SSL server on a given port.
87
119
*
88
120
* @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
90
122
*/
91
- startSsl ( sslOptions , port ) {
123
+ startSsl ( sslOptions , portOrSocket ) {
92
124
return new Promise ( ( resolve ) => {
93
125
const server = https . createServer ( sslOptions , this . app . callback ( ) ) ;
94
126
this . servers . push ( {
95
127
server,
96
128
type : 'https' ,
97
- port ,
129
+ portOrSocket ,
98
130
} ) ;
99
- server . listen ( port , ( ) => {
131
+ server . listen ( portOrSocket , ( ) => {
100
132
resolve ( ) ;
101
133
} ) ;
102
134
} ) ;
@@ -105,25 +137,25 @@ export class Server {
105
137
/**
106
138
* Stops a running www over SSL server, if any.
107
139
*
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.
109
141
* @returns {Promise<void[]> }
110
142
*/
111
- stopSsl ( port ) {
112
- return this . _stop ( 'https' , port ) ;
143
+ stopSsl ( portOrSocket ) {
144
+ return this . _stop ( 'https' , portOrSocket ) ;
113
145
}
114
146
115
147
/**
116
148
*
117
149
* @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.
119
151
* @returns {Promise<void[]> }
120
152
* @private
121
153
*/
122
- _stop ( type , port ) {
154
+ _stop ( type , portOrSocket ) {
123
155
const toStop = this . servers . filter ( ( s ) => {
124
156
if ( s . type === type ) {
125
- if ( port ) {
126
- return port === s . port ;
157
+ if ( portOrSocket ) {
158
+ return portOrSocket === s . portOrSocket ;
127
159
}
128
160
return true ;
129
161
}
0 commit comments