Last active
September 12, 2018 10:44
-
-
Save kirrg001/3d56703628ea2dfb59a1d36f4dfe5317 to your computer and use it in GitHub Desktop.
bootstrap-test-server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useNetServer = options => new Promise((resolve, reject) => { | |
const net = require('net'); | |
let waitTimeout = null; | |
let ghostSocket = null; | |
const server = net.createServer((socket) => { | |
ghostSocket = socket; | |
socket.on('data', (data) => { | |
let message; | |
try { | |
message = JSON.parse(data); | |
} catch (err) { | |
message = {started: false, error: err}; | |
} | |
console.log('Received message from Client'); | |
if (waitTimeout) { | |
clearTimeout(waitTimeout); | |
} | |
socket.destroy(); | |
ghostSocket = null; | |
server.close(() => { | |
console.log('Closed server'); | |
if (message.started) { | |
resolve(); | |
} else { | |
reject(new Error(message.error.message)); | |
} | |
}); | |
}); | |
}); | |
waitTimeout = setTimeout(() => { | |
if (ghostSocket) { | |
ghostSocket.destroy(); | |
} | |
ghostSocket = null; | |
server.close(() => { | |
reject(new Error('Could not communicate with Ghost')); | |
}); | |
}, options.netServerTimeoutInMS); | |
server.listen({host: options.socketAddress.host, port: options.socketAddress.port}); | |
}); | |
const host = process.argv[3] || 'localhost'; | |
const port = process.argv[5] || 8989; | |
useNetServer({ | |
netServerTimeoutInMS: 1000 * 60 * 1, | |
socketAddress: { | |
host: host, | |
port: port | |
} | |
}); | |
console.log(`Start server ${host} ${port}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment