From 24413c53dd780067800eded7929a4f9ed3e159d9 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 11 Mar 2024 21:45:27 +0000 Subject: [PATCH] The listen command shouldn't return until the server is listening --- core/modules/commands/listen.js | 8 ++++++-- core/modules/server/server.js | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/modules/commands/listen.js b/core/modules/commands/listen.js index ca6e6e076..acaecf38d 100644 --- a/core/modules/commands/listen.js +++ b/core/modules/commands/listen.js @@ -16,7 +16,7 @@ var Server = require("$:/core/modules/server/server.js").Server; exports.info = { name: "listen", - synchronous: true, + synchronous: false, namedParameterMode: true, mandatoryParameters: [] }; @@ -38,7 +38,11 @@ Command.prototype.execute = function() { wiki: this.commander.wiki, variables: self.params }); - var nodeServer = this.server.listen(); + var nodeServer = this.server.listen(null,null,null,{ + callback: function() { + self.callback(); + } + }); $tw.hooks.invokeHook("th-server-command-post-start",this.server,nodeServer,"tiddlywiki"); return null; }; diff --git a/core/modules/server/server.js b/core/modules/server/server.js index 4c782ef24..1243190f4 100644 --- a/core/modules/server/server.js +++ b/core/modules/server/server.js @@ -430,8 +430,9 @@ Listen for requests port: optional port number (falls back to value of "port" variable) host: optional host address (falls back to value of "host" variable) prefix: optional prefix (falls back to value of "path-prefix" variable) +callback: optional callback(err) to be invoked when the listener is up and running */ -Server.prototype.listen = function(port,host,prefix) { +Server.prototype.listen = function(port,host,prefix,options) { var self = this; // Handle defaults for port and host port = port || this.get("port"); @@ -458,7 +459,7 @@ Server.prototype.listen = function(port,host,prefix) { if(self.get("debug-level") !== "none") { var start = $tw.utils.timer(); response.on("finish",function() { - console.log("Response tim:",request.method,request.url,$tw.utils.timer() - start); + console.log("Response time:",request.method,request.url,$tw.utils.timer() - start); }); } self.requestHandler(request,response,options); @@ -469,6 +470,9 @@ Server.prototype.listen = function(port,host,prefix) { url = self.protocol + "://" + (address.family === "IPv6" ? "[" + address.address + "]" : address.address) + ":" + address.port + prefix; $tw.utils.log("Serving on " + url,"brown/orange"); $tw.utils.log("(press ctrl-C to exit)","red"); + if(options.callback) { + options.callback(null); + } }); // Listen return server.listen(port,host);