From a24540ff901ac8f2287947221bc7a189eb613f58 Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Mon, 21 Oct 2024 19:06:15 +1300 Subject: [PATCH 01/10] Make HTTP in SendOptions optional --- lib/raygun.ts | 18 +++++++++++------- lib/types.ts | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/raygun.ts b/lib/raygun.ts index 2470f00..0925038 100644 --- a/lib/raygun.ts +++ b/lib/raygun.ts @@ -571,13 +571,17 @@ class Raygun { skip: skip, options: { message: JSON.stringify(message), - http: { - host: this._host, - port: this._port, - useSSL: !!this._useSSL, - apiKey: apiKey, - timeout: this._timeout || DEFAULT_TIMEOUT, - }, + ...(this._batch + ? {} + : { + http: { + host: this._host, + port: this._port, + useSSL: !!this._useSSL, + apiKey: apiKey, + timeout: this._timeout || DEFAULT_TIMEOUT, + }, + }), }, }; } diff --git a/lib/types.ts b/lib/types.ts index a071013..178d8e9 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -70,7 +70,7 @@ export type Tag = string; export type SendOptions = { message: string; - http: HTTPOptions; + http?: HTTPOptions; }; export type HTTPOptions = { From a74d799622587d736d9213eff51b3d848ce032ba Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Tue, 22 Oct 2024 16:10:15 +1300 Subject: [PATCH 02/10] To avoid an error being displayed in the express sample when the provider is offline --- examples/express-sample/routes/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/express-sample/routes/index.js b/examples/express-sample/routes/index.js index 326270a..5826089 100644 --- a/examples/express-sample/routes/index.js +++ b/examples/express-sample/routes/index.js @@ -50,7 +50,7 @@ router.get("/send", function (req, res, next) { .then((message) => { res.render("send", { title: "Sent custom error to Raygun", - body: `Raygun status code: ${message.statusCode}`, + body: `Raygun status code: ${message?.statusCode}`, }); }) .catch((error) => { From 7f2b1b559187505ef6cff831fec7e61ddc665c8e Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Tue, 22 Oct 2024 16:10:48 +1300 Subject: [PATCH 03/10] Building up lighter SendOptions where possible --- lib/raygun.transport.ts | 10 +++++----- lib/raygun.ts | 14 ++++++++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/raygun.transport.ts b/lib/raygun.transport.ts index 66c8f8d..a468118 100644 --- a/lib/raygun.transport.ts +++ b/lib/raygun.transport.ts @@ -39,21 +39,21 @@ export function send( const data = Buffer.from(options.message); const httpOptions = { - host: options.http.host || API_HOST, - port: options.http.port || 443, + host: options.http?.host || API_HOST, + port: options.http?.port || 443, path: path, method: "POST", headers: { Host: API_HOST, "Content-Type": "application/json", "Content-Length": data.length, - "X-ApiKey": options.http.apiKey, + "X-ApiKey": options.http?.apiKey, }, }; // Wrap HTTP request in Promise return new Promise((resolve, reject) => { - const httpLib = options.http.useSSL ? https : http; + const httpLib = options.http?.useSSL ? https : http; const request = httpLib.request( httpOptions, (response: IncomingMessage) => { @@ -67,7 +67,7 @@ export function send( }, ); - if (options.http.timeout) { + if (options.http?.timeout) { debug(`[raygun.transport.ts] Timeout set: ${options.http.timeout}ms`); request.setTimeout(options.http.timeout, () => { console.error( diff --git a/lib/raygun.ts b/lib/raygun.ts index 0925038..c285ae3 100644 --- a/lib/raygun.ts +++ b/lib/raygun.ts @@ -601,12 +601,18 @@ class Raygun { transport .send({ message, - http: httpOptions, + ...(transport instanceof RaygunBatchTransport + ? {} + : { + http: httpOptions + }), }) .then((response) => { - debug( - `[raygun.ts] Sent message from offline transport: ${response}`, - ); + if (!(transport instanceof RaygunBatchTransport)) { + debug( + `[raygun.ts] Sent message from offline transport: ${response?.statusCode} ${response?.statusMessage}`, + ); + } }) .catch((error) => { console.error( From 1330c87b895239e89c339ff5d1ef06d5a6d96965 Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Tue, 22 Oct 2024 16:23:09 +1300 Subject: [PATCH 04/10] Prettier --- lib/raygun.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/raygun.ts b/lib/raygun.ts index c285ae3..028645b 100644 --- a/lib/raygun.ts +++ b/lib/raygun.ts @@ -601,17 +601,17 @@ class Raygun { transport .send({ message, - ...(transport instanceof RaygunBatchTransport + ...(transport instanceof RaygunBatchTransport ? {} : { - http: httpOptions - }), + http: httpOptions, + }), }) .then((response) => { if (!(transport instanceof RaygunBatchTransport)) { - debug( - `[raygun.ts] Sent message from offline transport: ${response?.statusCode} ${response?.statusMessage}`, - ); + debug( + `[raygun.ts] Sent message from offline transport: ${response?.statusCode} ${response?.statusMessage}`, + ); } }) .catch((error) => { From b6345647b03d3e431613b20cf8d8771ae9e240be Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Tue, 22 Oct 2024 16:32:43 +1300 Subject: [PATCH 05/10] Linting --- lib/raygun.batch.ts | 15 ++++++++++----- lib/raygun.ts | 11 ++++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/raygun.batch.ts b/lib/raygun.batch.ts index 97f200a..e949dca 100644 --- a/lib/raygun.batch.ts +++ b/lib/raygun.batch.ts @@ -41,9 +41,11 @@ export class RaygunBatchTransport { private batchId: number = 0; - private batchState: BatchState = { messages: [], messageSizeInBytes: 0 }; + private batchState: BatchState = { messages: [], + messageSizeInBytes: 0 }; - constructor(options: { interval: number; httpOptions: HTTPOptions }) { + constructor(options: { interval: number; + httpOptions: HTTPOptions; }) { this.interval = options.interval; this.httpOptions = options.httpOptions; } @@ -104,8 +106,10 @@ export class RaygunBatchTransport { } const promise = new Promise((resolve, reject) => { - const promise = { resolve, reject }; - this.batchState.messages.push({ serializedMessage, promise }); + const promise = { resolve, + reject }; + this.batchState.messages.push({ serializedMessage, + promise }); }); const batchIsFull = this.batchState.messages.length === 100; @@ -130,7 +134,8 @@ export class RaygunBatchTransport { this.sendBatch(batch); - this.batchState = { messages: [], messageSizeInBytes: 0 }; + this.batchState = { messages: [], + messageSizeInBytes: 0 }; this.stopProcessing(); } diff --git a/lib/raygun.ts b/lib/raygun.ts index 028645b..618fab9 100644 --- a/lib/raygun.ts +++ b/lib/raygun.ts @@ -38,8 +38,12 @@ import * as raygunSyncTransport from "./raygun.sync.transport"; import { v4 as uuidv4 } from "uuid"; type SendOptionsResult = - | { valid: true; message: Message; options: SendOptions; skip: boolean } - | { valid: false; message: Message }; + | { valid: true; + message: Message; + options: SendOptions; + skip: boolean; } + | { valid: false; + message: Message; }; const debug = require("debug")("raygun"); @@ -562,7 +566,8 @@ class Raygun { const apiKey = this._apiKey; if (!apiKey) { - return { valid: false, message }; + return { valid: false, + message }; } return { From 40754b66997c65afaae0fd93c6b653b1946fb943 Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Tue, 22 Oct 2024 16:51:12 +1300 Subject: [PATCH 06/10] Linting and Prettier --- lib/raygun.ts | 4 +--- tseslint.config.mjs | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/raygun.ts b/lib/raygun.ts index 618fab9..2f08509 100644 --- a/lib/raygun.ts +++ b/lib/raygun.ts @@ -608,9 +608,7 @@ class Raygun { message, ...(transport instanceof RaygunBatchTransport ? {} - : { - http: httpOptions, - }), + : { http: httpOptions }), }) .then((response) => { if (!(transport instanceof RaygunBatchTransport)) { diff --git a/tseslint.config.mjs b/tseslint.config.mjs index 36b3fed..8407335 100644 --- a/tseslint.config.mjs +++ b/tseslint.config.mjs @@ -68,7 +68,8 @@ export default tseslint.config( "@stylistic/ts/quote-props": ["off", 0], "@stylistic/ts/space-before-function-paren": ["off", 0], // Documentation format check - "tsdoc/syntax": "warn" + "tsdoc/syntax": "warn", + //"@stylistic/indent": "off", } } ); \ No newline at end of file From 6a994511b35c887765ba099c0c7f5c7856db1322 Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 22 Oct 2024 11:44:11 +0200 Subject: [PATCH 07/10] prettier takes care of indentation formatting, ignore rule in tseslint config --- lib/raygun.ts | 18 ++++++++++++------ tseslint.config.mjs | 3 +-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/raygun.ts b/lib/raygun.ts index 2f08509..c85dec8 100644 --- a/lib/raygun.ts +++ b/lib/raygun.ts @@ -38,12 +38,16 @@ import * as raygunSyncTransport from "./raygun.sync.transport"; import { v4 as uuidv4 } from "uuid"; type SendOptionsResult = - | { valid: true; + | { + valid: true; message: Message; options: SendOptions; - skip: boolean; } - | { valid: false; - message: Message; }; + skip: boolean; + } + | { + valid: false; + message: Message; + }; const debug = require("debug")("raygun"); @@ -566,8 +570,10 @@ class Raygun { const apiKey = this._apiKey; if (!apiKey) { - return { valid: false, - message }; + return { + valid: false, + message, + }; } return { diff --git a/tseslint.config.mjs b/tseslint.config.mjs index 8407335..0b3b044 100644 --- a/tseslint.config.mjs +++ b/tseslint.config.mjs @@ -50,8 +50,6 @@ export default tseslint.config( "@stylistic/semi": ["error", "always"], // Stick to double quotes "@stylistic/quotes": ["error", "double"], - // Always indent with two spaces - '@stylistic/ts/indent': ['error', 2], // Enforce curly braces spacing "@stylistic/ts/object-curly-spacing": ["error", "always"], // Enforce "one true brace style" @@ -67,6 +65,7 @@ export default tseslint.config( "@stylistic/ts/no-extra-parens": ["off", 0], "@stylistic/ts/quote-props": ["off", 0], "@stylistic/ts/space-before-function-paren": ["off", 0], + "@stylistic/ts/indent": ["off", 0], // Documentation format check "tsdoc/syntax": "warn", //"@stylistic/indent": "off", From 5eb55a3b4866173a09d779675f56ad6f44a62ead Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 22 Oct 2024 11:45:37 +0200 Subject: [PATCH 08/10] prettier run --- lib/raygun.batch.ts | 15 +++++---------- lib/raygun.ts | 16 ++++++++-------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/raygun.batch.ts b/lib/raygun.batch.ts index e949dca..97f200a 100644 --- a/lib/raygun.batch.ts +++ b/lib/raygun.batch.ts @@ -41,11 +41,9 @@ export class RaygunBatchTransport { private batchId: number = 0; - private batchState: BatchState = { messages: [], - messageSizeInBytes: 0 }; + private batchState: BatchState = { messages: [], messageSizeInBytes: 0 }; - constructor(options: { interval: number; - httpOptions: HTTPOptions; }) { + constructor(options: { interval: number; httpOptions: HTTPOptions }) { this.interval = options.interval; this.httpOptions = options.httpOptions; } @@ -106,10 +104,8 @@ export class RaygunBatchTransport { } const promise = new Promise((resolve, reject) => { - const promise = { resolve, - reject }; - this.batchState.messages.push({ serializedMessage, - promise }); + const promise = { resolve, reject }; + this.batchState.messages.push({ serializedMessage, promise }); }); const batchIsFull = this.batchState.messages.length === 100; @@ -134,8 +130,7 @@ export class RaygunBatchTransport { this.sendBatch(batch); - this.batchState = { messages: [], - messageSizeInBytes: 0 }; + this.batchState = { messages: [], messageSizeInBytes: 0 }; this.stopProcessing(); } diff --git a/lib/raygun.ts b/lib/raygun.ts index c85dec8..91af36d 100644 --- a/lib/raygun.ts +++ b/lib/raygun.ts @@ -39,15 +39,15 @@ import { v4 as uuidv4 } from "uuid"; type SendOptionsResult = | { - valid: true; - message: Message; - options: SendOptions; - skip: boolean; - } + valid: true; + message: Message; + options: SendOptions; + skip: boolean; + } | { - valid: false; - message: Message; - }; + valid: false; + message: Message; + }; const debug = require("debug")("raygun"); From 5dbe55c31a55745247eae1a0a4e370547f05b4fc Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 22 Oct 2024 11:49:55 +0200 Subject: [PATCH 09/10] remove commented out line --- tseslint.config.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/tseslint.config.mjs b/tseslint.config.mjs index 0b3b044..d9d99d6 100644 --- a/tseslint.config.mjs +++ b/tseslint.config.mjs @@ -68,7 +68,6 @@ export default tseslint.config( "@stylistic/ts/indent": ["off", 0], // Documentation format check "tsdoc/syntax": "warn", - //"@stylistic/indent": "off", } } ); \ No newline at end of file From c69248897248815b86ab6326c772f17e271bb3d9 Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 22 Oct 2024 11:50:20 +0200 Subject: [PATCH 10/10] remove extra coma --- tseslint.config.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tseslint.config.mjs b/tseslint.config.mjs index d9d99d6..f41363c 100644 --- a/tseslint.config.mjs +++ b/tseslint.config.mjs @@ -67,7 +67,7 @@ export default tseslint.config( "@stylistic/ts/space-before-function-paren": ["off", 0], "@stylistic/ts/indent": ["off", 0], // Documentation format check - "tsdoc/syntax": "warn", + "tsdoc/syntax": "warn" } } ); \ No newline at end of file