Archived
1
0

Use our logger instead of raw console.log

This commit is contained in:
Asher 2019-07-31 15:22:05 -05:00
parent 62719ab544
commit 12af311ce7
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
5 changed files with 32 additions and 16 deletions

View File

@ -23,6 +23,7 @@
"@types/node": "^10.12.12" "@types/node": "^10.12.12"
}, },
"dependencies": { "dependencies": {
"@coder/logger": "^1.1.5",
"httpolyglot": "^0.1.2", "httpolyglot": "^0.1.2",
"pem": "^1.14.2", "pem": "^1.14.2",
"safe-compare": "^1.1.4" "safe-compare": "^1.1.4"

View File

@ -9,7 +9,9 @@ import product from "vs/platform/product/node/product";
import { MainServer } from "vs/server/src/server"; import { MainServer } from "vs/server/src/server";
import "vs/server/src/tar"; import "vs/server/src/tar";
import { AuthType, buildAllowedMessage, generateCertificate, generatePassword, open, unpackExecutables } from "vs/server/src/util"; import { AuthType, buildAllowedMessage, generateCertificate, generatePassword, localRequire, open, unpackExecutables } from "vs/server/src/util";
const { logger } = localRequire<typeof import("@coder/logger/out/index")>("@coder/logger/out/index");
interface Args extends ParsedArgs { interface Args extends ParsedArgs {
auth?: AuthType; auth?: AuthType;
@ -67,7 +69,7 @@ interface IMainCli {
main: (argv: ParsedArgs) => Promise<void>; main: (argv: ParsedArgs) => Promise<void>;
} }
const main = async (): Promise<void> => { const main = async (): Promise<void | void[]> => {
const args = validatePaths(parseMainProcessArgv(process.argv)) as Args; const args = validatePaths(parseMainProcessArgv(process.argv)) as Args;
["extra-extensions-dir", "extra-builtin-extensions-dir"].forEach((key) => { ["extra-extensions-dir", "extra-builtin-extensions-dir"].forEach((key) => {
if (typeof args[key] === "string") { if (typeof args[key] === "string") {
@ -91,7 +93,7 @@ const main = async (): Promise<void> => {
} }
if (args.version) { if (args.version) {
return console.log(buildVersionMessage(version, product.commit)); return buildVersionMessage(version, product.commit).split("\n").map((line) => logger.info(line));
} }
const shouldSpawnCliProcess = (): boolean => { const shouldSpawnCliProcess = (): boolean => {
@ -146,32 +148,32 @@ const main = async (): Promise<void> => {
server.listen(), server.listen(),
unpackExecutables(), unpackExecutables(),
]); ]);
console.log(`Server listening on ${serverAddress}`); logger.info(`Server listening on ${serverAddress}`);
if (options.auth && !process.env.PASSWORD) { if (options.auth && !process.env.PASSWORD) {
console.log(" - Password is", options.password); logger.info(` - Password is ${options.password}`);
console.log(" - To use your own password, set the PASSWORD environment variable"); logger.info(" - To use your own password, set the PASSWORD environment variable");
} else if (options.auth) { } else if (options.auth) {
console.log(" - Using custom password for authentication"); logger.info(" - Using custom password for authentication");
} else { } else {
console.log(" - No authentication"); logger.info(" - No authentication");
} }
if (server.protocol === "https") { if (server.protocol === "https") {
console.log( logger.info(
args.cert args.cert
? ` - Using provided certificate${args["cert-key"] ? " and key" : ""} for HTTPS` ? ` - Using provided certificate${args["cert-key"] ? " and key" : ""} for HTTPS`
: ` - Using generated certificate and key for HTTPS`, : ` - Using generated certificate and key for HTTPS`,
); );
} else { } else {
console.log(" - Not serving HTTPS"); logger.info(" - Not serving HTTPS");
} }
if (!server.options.socket && args.open) { if (!server.options.socket && args.open) {
// The web socket doesn't seem to work if using 0.0.0.0. // The web socket doesn't seem to work if using 0.0.0.0.
const openAddress = `http://localhost:${server.options.port}`; const openAddress = `http://localhost:${server.options.port}`;
await open(openAddress).catch(console.error); await open(openAddress).catch(console.error);
console.log(` - Opened ${openAddress}`); logger.info(` - Opened ${openAddress}`);
} }
}; };

View File

@ -57,7 +57,7 @@ import { Connection, ManagementConnection, ExtensionHostConnection } from "vs/se
import { ExtensionEnvironmentChannel, FileProviderChannel , } from "vs/server/src/channel"; import { ExtensionEnvironmentChannel, FileProviderChannel , } from "vs/server/src/channel";
import { TelemetryClient } from "vs/server/src/insights"; import { TelemetryClient } from "vs/server/src/insights";
import { Protocol } from "vs/server/src/protocol"; import { Protocol } from "vs/server/src/protocol";
import { AuthType, getMediaMime, getUriTransformer, tmpdir } from "vs/server/src/util"; import { AuthType, getMediaMime, getUriTransformer, localRequire, tmpdir } from "vs/server/src/util";
export enum HttpCode { export enum HttpCode {
Ok = 200, Ok = 200,
@ -124,7 +124,7 @@ export abstract class Server {
}; };
this.protocol = this.options.cert ? "https" : "http"; this.protocol = this.options.cert ? "https" : "http";
if (this.protocol === "https") { if (this.protocol === "https") {
const httpolyglot = require.__$__nodeRequire(path.resolve(__dirname, "../node_modules/httpolyglot/lib/index")) as typeof import("httpolyglot"); const httpolyglot = localRequire<typeof import("httpolyglot")>("httpolyglot/lib/index");
this.server = httpolyglot.createServer({ this.server = httpolyglot.createServer({
cert: this.options.cert && fs.readFileSync(this.options.cert), cert: this.options.cert && fs.readFileSync(this.options.cert),
key: this.options.certKey && fs.readFileSync(this.options.certKey), key: this.options.certKey && fs.readFileSync(this.options.certKey),
@ -213,7 +213,7 @@ export abstract class Server {
const parsedUrl = request.url ? url.parse(request.url, true) : { query: {}}; const parsedUrl = request.url ? url.parse(request.url, true) : { query: {}};
const fullPath = decodeURIComponent(parsedUrl.pathname || "/"); const fullPath = decodeURIComponent(parsedUrl.pathname || "/");
const match = fullPath.match(/^(\/?[^/]*)(.*)$/); const match = fullPath.match(/^(\/?[^/]*)(.*)$/);
let [, base, requestPath] = match let [/* ignore */, base, requestPath] = match
? match.map((p) => p.replace(/\/+$/, "")) ? match.map((p) => p.replace(/\/+$/, ""))
: ["", "", ""]; : ["", "", ""];
if (base.indexOf(".") !== -1) { // Assume it's a file at the root. if (base.indexOf(".") !== -1) { // Assume it's a file at the root.
@ -363,7 +363,7 @@ export abstract class Server {
if (!this.options.auth) { if (!this.options.auth) {
return true; return true;
} }
const safeCompare = require.__$__nodeRequire(path.resolve(__dirname, "../node_modules/safe-compare/index")) as typeof import("safe-compare"); const safeCompare = localRequire<typeof import("safe-compare")>("safe-compare/index");
if (typeof payload === "undefined") { if (typeof payload === "undefined") {
payload = this.parseCookies<LoginPayload>(request); payload = this.parseCookies<LoginPayload>(request);
} }

View File

@ -30,7 +30,7 @@ export const generateCertificate = async (): Promise<{ cert: string, certKey: st
]); ]);
if (!exists[0] || !exists[1]) { if (!exists[0] || !exists[1]) {
const pem = require.__$__nodeRequire(path.resolve(__dirname, "../node_modules/pem/lib/pem")) as typeof import("pem"); const pem = localRequire<typeof import("pem")>("pem/lib/pem");
const certs = await new Promise<import("pem").CertificateCreationResult>((resolve, reject): void => { const certs = await new Promise<import("pem").CertificateCreationResult>((resolve, reject): void => {
pem.createCertificate({ selfSigned: true }, (error, result) => { pem.createCertificate({ selfSigned: true }, (error, result) => {
if (error) { if (error) {
@ -117,3 +117,11 @@ export const buildAllowedMessage = (t: typeof AuthType): string => {
const values = <string[]>Object.keys(t).map((k) => t[k]); const values = <string[]>Object.keys(t).map((k) => t[k]);
return `Allowed value${values.length === 1 ? " is" : "s are"} ${values.map((t) => `'${t}'`).join(",")}`; return `Allowed value${values.length === 1 ? " is" : "s are"} ${values.map((t) => `'${t}'`).join(",")}`;
}; };
/**
* Require a local module. This is necessary since VS Code's loader only looks
* at the root for Node modules.
*/
export const localRequire = <T>(modulePath: string): T => {
return require.__$__nodeRequire(path.resolve(__dirname, "../node_modules", modulePath));
};

View File

@ -2,6 +2,11 @@
# yarn lockfile v1 # yarn lockfile v1
"@coder/logger@^1.1.5":
version "1.1.5"
resolved "https://registry.yarnpkg.com/@coder/logger/-/logger-1.1.5.tgz#e5b0e6207a00b6b54e9c63ad8afab60643b10f25"
integrity sha512-ehOcZ2HXCDTKIjORPDvEzJyNk3X2vOE4Tcb78UTHR71fG6CIL1KP5Rx4Nj5M4Jg2X5laouWwbG9oWtkmQeKkJg==
"@types/node@*", "@types/node@^10.12.12": "@types/node@*", "@types/node@^10.12.12":
version "10.14.12" version "10.14.12"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.12.tgz#0eec3155a46e6c4db1f27c3e588a205f767d622f" resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.12.tgz#0eec3155a46e6c4db1f27c3e588a205f767d622f"