@travetto/net
Advanced tools
Comparing version 1.0.0-rc.2 to 1.0.0-rc.3
@@ -7,3 +7,3 @@ { | ||
"dependencies": { | ||
"@travetto/base": "^1.0.0-rc.2" | ||
"@travetto/base": "^1.0.0-rc.3" | ||
}, | ||
@@ -27,4 +27,4 @@ "description": "Network utilities of the travetto framework", | ||
}, | ||
"version": "1.0.0-rc.2", | ||
"gitHead": "019d593f07d43f679392e3e8fdfb4ab58e71da7a" | ||
"version": "1.0.0-rc.3", | ||
"gitHead": "c29f98db4b1d153b6dce41dc154f1154d0a4ba35" | ||
} |
import * as path from 'path'; | ||
import * as os from 'os'; | ||
import * as http from 'http'; | ||
import * as https from 'https'; | ||
import * as net from 'net'; | ||
@@ -34,64 +32,2 @@ import { HttpRequest } from './request'; | ||
} | ||
/** | ||
* Wait for the http url to return a valid response | ||
* | ||
* @param url URL to check | ||
* @param ms Maximum time to wait in milliseconds | ||
*/ | ||
static async waitForHttp(url: string, ms = 5000) { | ||
const start = Date.now(); | ||
const port = /:\d+/.test(url) ? parseInt(url.replace(/.*:(\d+).*/, (all, p) => p), 10) : (url.startsWith('https') ? 443 : 80); | ||
console.debug('Waiting for port', port); | ||
await this.waitForPort(port, ms); | ||
console.debug('Acquired port', port); | ||
while ((Date.now() - start) < ms) { | ||
const status = await new Promise<number>((resolve) => { | ||
try { | ||
const client = url.startsWith('https') ? https : http; | ||
const req = client.get(url, (msg) => | ||
msg | ||
.on('data', () => { }) // Consume data | ||
.on('error', (err) => resolve(500)) | ||
.on('end', () => resolve((msg.statusCode || 200))) | ||
.on('close', () => resolve((msg.statusCode || 200)))); | ||
req.on('error', (err) => resolve(500)); | ||
} catch (e) { | ||
resolve(400); | ||
} | ||
}); | ||
if (status >= 200 && status <= 299) { | ||
return; // We good | ||
} | ||
await new Promise(res => setTimeout(res, 100)); | ||
} | ||
} | ||
/** | ||
* Wait for a TCP port to become available | ||
*/ | ||
static async waitForPort(port: number, ms = 5000) { | ||
const start = Date.now(); | ||
while ((Date.now() - start) < ms) { | ||
try { | ||
await new Promise((res, rej) => { | ||
try { | ||
const sock = net.createConnection(port, 'localhost'); | ||
sock.on('connect', () => { | ||
sock.destroy(); | ||
res(); | ||
}); | ||
sock.on('error', rej); | ||
} catch (e) { | ||
rej(e); | ||
} | ||
}); | ||
return; | ||
} catch (e) { | ||
await new Promise(res => setTimeout(res, 50)); | ||
} | ||
} | ||
throw new Error('Could not acquire port'); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11328
241
Updated@travetto/base@^1.0.0-rc.3