@@ -2,6 +2,9 @@ import Koa from 'koa';
22import http from 'http';
33import https from 'https';
44import cors from '@koa/cors';
5+ import { dir } from 'tmp-promise';
6+ import { platform } from 'os';
7+ import path from 'path';
58import { 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 */
1619export 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