-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2238 from cdr/code-asher/ch1385
- Loading branch information
Showing
49 changed files
with
3,320 additions
and
2,140 deletions.
There are no files selected for viewing
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { logger } from "@coder/logger" | ||
import express, { Express } from "express" | ||
import { promises as fs } from "fs" | ||
import http from "http" | ||
import * as httpolyglot from "httpolyglot" | ||
import { DefaultedArgs } from "./cli" | ||
import { handleUpgrade } from "./wsRouter" | ||
|
||
/** | ||
* Create an Express app and an HTTP/S server to serve it. | ||
*/ | ||
export const createApp = async (args: DefaultedArgs): Promise<[Express, Express, http.Server]> => { | ||
const app = express() | ||
|
||
const server = args.cert | ||
? httpolyglot.createServer( | ||
{ | ||
cert: args.cert && (await fs.readFile(args.cert.value)), | ||
key: args["cert-key"] && (await fs.readFile(args["cert-key"])), | ||
}, | ||
app, | ||
) | ||
: http.createServer(app) | ||
|
||
await new Promise<http.Server>(async (resolve, reject) => { | ||
server.on("error", reject) | ||
if (args.socket) { | ||
try { | ||
await fs.unlink(args.socket) | ||
} catch (error) { | ||
if (error.code !== "ENOENT") { | ||
logger.error(error.message) | ||
} | ||
} | ||
server.listen(args.socket, resolve) | ||
} else { | ||
// [] is the correct format when using :: but Node errors with them. | ||
server.listen(args.port, args.host.replace(/^\[|\]$/g, ""), resolve) | ||
} | ||
}) | ||
|
||
const wsApp = express() | ||
handleUpgrade(wsApp, server) | ||
|
||
return [app, wsApp, server] | ||
} | ||
|
||
/** | ||
* Get the address of a server as a string (protocol *is* included) while | ||
* ensuring there is one (will throw if there isn't). | ||
*/ | ||
export const ensureAddress = (server: http.Server): string => { | ||
const addr = server.address() | ||
if (!addr) { | ||
throw new Error("server has no address") | ||
} | ||
if (typeof addr !== "string") { | ||
return `http://${addr.address}:${addr.port}` | ||
} | ||
return addr | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.