You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-24 23:26:50 +02:00
All: Use Lerna to manage monorepo
This commit is contained in:
40
packages/lib/net-utils.js
Normal file
40
packages/lib/net-utils.js
Normal file
@ -0,0 +1,40 @@
|
||||
const shim = require('./shim').default;
|
||||
|
||||
const netUtils = {};
|
||||
|
||||
netUtils.ip = async () => {
|
||||
const response = await shim.fetch('https://api.ipify.org/?format=json');
|
||||
if (!response.ok) {
|
||||
throw new Error(`Could not retrieve IP: ${await response.text()}`);
|
||||
}
|
||||
|
||||
const ip = await response.json();
|
||||
return ip.ip;
|
||||
};
|
||||
|
||||
netUtils.findAvailablePort = async (possiblePorts, extraRandomPortsToTry = 20) => {
|
||||
const tcpPortUsed = require('tcp-port-used');
|
||||
|
||||
for (let i = 0; i < extraRandomPortsToTry; i++) {
|
||||
possiblePorts.push(Math.floor(8000 + Math.random() * 2000));
|
||||
}
|
||||
|
||||
let port = null;
|
||||
for (let i = 0; i < possiblePorts.length; i++) {
|
||||
const inUse = await tcpPortUsed.check(possiblePorts[i]);
|
||||
if (!inUse) {
|
||||
port = possiblePorts[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return port;
|
||||
};
|
||||
|
||||
netUtils.mimeTypeFromHeaders = headers => {
|
||||
if (!headers || !headers['content-type']) return null;
|
||||
|
||||
const splitted = headers['content-type'].split(';');
|
||||
return splitted[0].trim().toLowerCase();
|
||||
};
|
||||
|
||||
module.exports = { netUtils };
|
Reference in New Issue
Block a user