-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a single transmitter for Diagnose
Diagnose script performs two external HTTP calls, one for push api key validation, and another one to send the diagnose results to AppSignal if the user wants. These calls were written separatedly inside the diagnose script, now they are done through a new class called `Transmitter`.
- Loading branch information
1 parent
48589ca
commit 95dd168
Showing
3 changed files
with
127 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import fs from "fs" | ||
import path from "path" | ||
import https from "https" | ||
import http from "http" | ||
|
||
import { Configuration } from "./config" | ||
import { URL, URLSearchParams } from "url" | ||
import { HashMap } from "@appsignal/types" | ||
|
||
export class Transmitter { | ||
#config: Configuration | ||
#url: string | ||
#data: string | ||
|
||
constructor(url: string, data = "", active = true) { | ||
this.#config = new Configuration({ active }) | ||
this.#url = url | ||
this.#data = data | ||
} | ||
|
||
public async transmit() { | ||
return new Promise<HashMap<any>>((resolve, reject) => { | ||
const config_data = this.#config.data | ||
const params = new URLSearchParams({ | ||
api_key: config_data["pushApiKey"] || "", | ||
name: config_data["name"] || "", | ||
environment: config_data["environment"] || "", | ||
hostname: config_data["hostname"] || "" | ||
}) | ||
|
||
const url = new URL(`${this.#url}?${params.toString()}`) | ||
const opts = this.requestOptions(url) | ||
const requestModule = url.protocol == "http:" ? http : https | ||
|
||
const request = requestModule.request(opts, (stream: any) => { | ||
const responseStatus = stream.statusCode | ||
stream.setEncoding("utf8") | ||
let responseBody = "" | ||
|
||
stream | ||
.on("data", (chunk: any) => { | ||
responseBody += chunk | ||
}) | ||
.on("end", () => { | ||
let parsedResponse | ||
try { | ||
parsedResponse = JSON.parse(responseBody) | ||
} catch { | ||
parsedResponse = {} | ||
} | ||
|
||
if (responseStatus == 200) { | ||
resolve({ status: responseStatus, body: parsedResponse }) | ||
} else { | ||
reject({ status: responseStatus, body: parsedResponse }) | ||
} | ||
}) | ||
}) | ||
|
||
request.write(this.#data) | ||
request.end() | ||
}) | ||
} | ||
|
||
private requestOptions(requestUrl: URL): HashMap<any> { | ||
const configData = this.#config.data | ||
|
||
return { | ||
method: "POST", | ||
protocol: requestUrl.protocol, | ||
host: requestUrl.hostname, | ||
port: requestUrl.port, | ||
path: requestUrl.toString(), | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Content-Length": this.#data.length | ||
}, | ||
cert: fs.readFileSync( | ||
path.resolve(__dirname, "../cert/cacert.pem"), | ||
"utf-8" | ||
) | ||
} | ||
} | ||
} |