-
Notifications
You must be signed in to change notification settings - Fork 504
[ECMAScript6BestPractices] fix common-serve.js
#2773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
cd3f993
7d1eb76
6cc4dcc
b48205f
eb97996
15fd403
e2dfdc4
e5d81a3
f558260
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,65 +1,45 @@ | ||
| // this file will be glued to the top of the specific xy-serve.js file | ||
| const debug_serve = false; // set to true for debug log output in node process | ||
| const shutdownServer = require("http-graceful-shutdown"); | ||
| const express = require("express"); | ||
| const app = express(); | ||
| const fs = require("fs"); | ||
| const shutdownServer = require("http-graceful-shutdown"); | ||
|
|
||
| app.use(express.json({limit: "50mb"})); | ||
| // this file will be glued to the top of the specific xy-serve.js file | ||
|
|
||
| const fs = require("fs"); | ||
| function streamServerOutput(instanceId, port) { | ||
| debugLog(`Server running on port ${port} for instance ${instanceId}`); | ||
| fs.writeFile("server.port.tmp", port, err => { | ||
| if (err) return console.log(err); | ||
| fs.rename("server.port.tmp", `server-${instanceId}.port`, err => { | ||
| if (err) console.log(err); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function debugLog() { | ||
| if (debug_serve) { | ||
| console.log.apply(this, arguments) | ||
| } | ||
| if (false) console.log.apply(this, arguments); | ||
| } | ||
|
|
||
| function getInstanceId() { | ||
| const args = process.argv.slice(2); | ||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Look for the --node-server-instance-id option | ||
| let instanceId; | ||
| const app = express(); | ||
|
|
||
| args.forEach(arg => { | ||
| if (arg.startsWith('--node-server-instance-id=')) { | ||
| instanceId = arg.split('=')[1]; | ||
| } | ||
| }); | ||
| app.use(express.json({ limit: "50mb" })); | ||
|
|
||
| // throw if instanceId is not set | ||
| if (!instanceId) { | ||
| throw new Error("Missing --node-server-instance-id argument"); | ||
| } | ||
| return instanceId; | ||
| } | ||
|
|
||
| var listener = app.listen(0, "127.0.0.1", () => { | ||
| const instanceId = getInstanceId(); | ||
| debugLog("Server running on port " + listener.address().port + " for instance " + instanceId); | ||
| fs.writeFile("server.port.tmp", "" + listener.address().port, function (err) { | ||
| if (err) { | ||
| return console.log(err); | ||
| } else { | ||
| fs.rename("server.port.tmp", `server-${instanceId}.port`, function (err) { | ||
| if (err) { | ||
| return console.log(err); | ||
| } | ||
| }); // try to be as atomic as possible | ||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
| }); | ||
| const shutdown = shutdownServer(listener, { | ||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| forceExit: false, // let the event loop clear | ||
| finally: () => debugLog("graceful shutdown finished."), | ||
| }); | ||
| const server = app.listen(0,"127.0.0.1", () => | ||
| streamServerOutput( | ||
| process.argv.slice(2).find(arg => arg.startsWith("--node-server-instance-id="))?.split("=")[1] | ||
| || (() => { throw new Error("Missing --node-server-instance-id argument"); })(), | ||
| server.address().port | ||
| ) | ||
| ); | ||
|
|
||
| app.post("/shutdown", (req, res) => { | ||
| res.status(200).send("Shutting down"); | ||
| setTimeout(async () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assuming if we fail here we still would have send out 200 before. |
||
| try { | ||
| await shutdown(); | ||
| } catch (err) { | ||
| console.error("Error during shutdown:", err); | ||
| } | ||
| }, 200); | ||
| setTimeout(async () => { | ||
| try { | ||
| await shutdownServer(server, { | ||
| forceExit: false, | ||
| finally: () => debugLog("graceful shutdown finished.") | ||
| })(); | ||
| } catch (err) { | ||
| console.error("Error during shutdown:", err); | ||
| } | ||
| }, 200); | ||
| res.ok().send("Graceful shutdown."); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be safe to make this the last call. The only potential issue is if a third-party tool relies on the current log parsing, but that’s unlikely. We’re updating it to present tense for consistency with standard logging. |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,7 @@ | ||
| const tsfmt = require("typescript-formatter"); | ||
|
|
||
| app.post("/tsfmt/format", (req, res) => { | ||
| var format_data = req.body; | ||
| tsfmt.processString("spotless-format-string.ts", format_data.file_content, format_data.config_options).then(resultMap => { | ||
| /* | ||
| export interface ResultMap { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we still need this? |
||
| [fileName: string]: Result; | ||
| } | ||
| export interface Result { | ||
| fileName: string; | ||
| settings: ts.FormatCodeSettings | null; | ||
| message: string; | ||
| error: boolean; | ||
| src: string; | ||
| dest: string; | ||
| } | ||
| */ | ||
| // result contains 'message' (String), 'error' (boolean), 'dest' (String) => formatted | ||
| tsfmt.processString("spotless-format-string.ts", req.body.file_content, req.body.config_options).then(resultMap => { | ||
| if (resultMap.error !== undefined && resultMap.error) { | ||
| res.status(400).send(resultMap.message); | ||
| return; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.