Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
669461d
Convert @bugsnag/plugin-node-device to TypeScript (#2505)
AnastasiiaSvietlova Jul 30, 2025
ac30746
Convert @bugsnag/plugin-node-in-project to TypeScript (#2506)
AnastasiiaSvietlova Aug 5, 2025
46cc5fb
Convert @bugsnag/plugin-node-uncaught-exception to TypeScript (#2509)
AnastasiiaSvietlova Aug 7, 2025
0316b1c
Convert @bugsnag/plugin-node-unhandled-rejection to TypeScript (#2511)
AnastasiiaSvietlova Aug 7, 2025
09b3683
Convert @bugsnag/plugin-node-surrounding-code to TypeScript (#2507)
AnastasiiaSvietlova Aug 12, 2025
1320028
Convert @bugsnag/plugin-strip-project-root to TypeScript (#2520)
AnastasiiaSvietlova Aug 13, 2025
f34492a
Convert @bugsnag/plugin-server-session to TypeScript (#2514)
AnastasiiaSvietlova Aug 14, 2025
9027b52
fixup package lock
gingerbenw Sep 5, 2025
6423887
update node types
gingerbenw Sep 5, 2025
911bea5
Convert @bugsnag/plugin-intercept to TypeScript (#2539)
AnastasiiaSvietlova Sep 8, 2025
2e742b4
Convert @bugsnag/plugin-contextualize to TypeScript (#2540)
AnastasiiaSvietlova Sep 8, 2025
0c8b971
Convert @bugsnag/plugin-stackframe-path-normaliser to TypeScript (#2…
AnastasiiaSvietlova Sep 8, 2025
d9ca5e4
update package-lock.json
gingerbenw Sep 16, 2025
b776d02
Convert @bugsnag/delivery-node to TypeScript (#2545)
AnastasiiaSvietlova Sep 16, 2025
fecbcdd
Convert @bugsnag/plugin-express to TypeScript (#2546)
AnastasiiaSvietlova Sep 18, 2025
ebc1c1f
Convert @bugsnag/plugin-restify to TypeScript (#2549)
AnastasiiaSvietlova Sep 18, 2025
f8d0eb3
Convert @bugsnag/plugin-koa to TypeScript (#2550)
AnastasiiaSvietlova Sep 18, 2025
171c16f
Merge branch 'integration/typescript' into integration/typescript-node
gingerbenw Sep 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 0 additions & 64 deletions packages/delivery-node/delivery.js

This file was deleted.

18 changes: 16 additions & 2 deletions packages/delivery-node/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@bugsnag/delivery-node",
"version": "8.4.0",
"main": "delivery.js",
"main": "dist/delivery.js",
"types": "dist/types/delivery.d.ts",
"exports": {
".": {
"types": "./dist/types/delivery.d.ts",
"default": "./dist/delivery.js",
"import": "./dist/delivery.mjs"
}
},
"description": "@bugsnag/node delivery mechanism",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -12,10 +20,16 @@
"access": "public"
},
"files": [
"*.js"
"dist"
],
"author": "Bugsnag",
"license": "MIT",
"scripts": {
"build": "npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"clean": "rm -rf dist/*",
"test:types": "tsc -p tsconfig.json"
},
"devDependencies": {
"@bugsnag/core": "^8.4.0"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/delivery-node/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import createRollupConfig from "../../.rollup/index.mjs"

export default createRollupConfig({
input: "src/delivery.ts",
external: ['http', 'https', 'url'],
})
79 changes: 79 additions & 0 deletions packages/delivery-node/src/delivery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { Client, Config, Delivery } from '@bugsnag/core'
import * as jsonPayload from '@bugsnag/json-payload'
import request from './request'
import http from 'http'

interface PluginConfig extends Config {
agent?: http.Agent
}

interface InternalClient extends Client {
_config: Required<PluginConfig>
}

const delivery = (client: Client): Delivery => ({
sendEvent: (event, cb = () => {}) => {
const internalClient = client as InternalClient
const body = jsonPayload.event(event, internalClient._config.redactedKeys)

const _cb = (err: Error | null) => {
if (err) internalClient._logger.error(`Event failed to send…\n${(err && err.stack) ? err.stack : err}`, err)
if (body.length > 10e5) {
internalClient._logger.warn(`Event oversized (${(body.length / 10e5).toFixed(2)} MB)`)
}
cb(err)
}

if (internalClient._config.endpoints.notify === null) {
const err = new Error('Event not sent due to incomplete endpoint configuration')
return _cb(err)
}

try {
request({
url: internalClient._config.endpoints.notify,
headers: {
'Content-Type': 'application/json',
'Bugsnag-Api-Key': event.apiKey || internalClient._config.apiKey,
'Bugsnag-Payload-Version': '4',
'Bugsnag-Sent-At': (new Date()).toISOString()
},
body,
agent: internalClient._config.agent
}, (err) => _cb(err))
} catch (e: any) {
_cb(e)
}
},
sendSession: (session, cb = () => {}) => {
const internalClient = client as InternalClient
const _cb = (err: Error | null) => {
if (err) internalClient._logger.error(`Session failed to send…\n${(err && err.stack) ? err.stack : err}`, err)
cb(err)
}

if (internalClient._config.endpoints.sessions === null) {
const err = new Error('Session not sent due to incomplete endpoint configuration')
return _cb(err)
}

try {
request({
url: internalClient._config.endpoints.sessions,
headers: {
'Content-Type': 'application/json',
'Bugsnag-Api-Key': internalClient._config.apiKey,
'Bugsnag-Payload-Version': '1',
'Bugsnag-Sent-At': (new Date()).toISOString()
},
body: jsonPayload.session(session, internalClient._config.redactedKeys),
agent: internalClient._config.agent
}, err => _cb(err))
} catch (e: any) {
_cb(e)
}
}
})


export default delivery
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const http = require('http')
const https = require('https')
const { parse } = require('url')
import http from 'http'
import https from 'https'
import { parse } from 'url'

module.exports = ({ url, headers, body, agent }, cb) => {
interface RequestOptions {
url: string
headers?: http.OutgoingHttpHeaders
body?: string
agent?: http.Agent
}

const request = ({ url, headers, body, agent }: RequestOptions, cb: (err: Error | null, body?: string) => void) => {
let didError = false
const onError = (err) => {
const onError = (err: Error) => {
if (didError) return
didError = true
cb(err)
Expand All @@ -25,7 +32,7 @@ module.exports = ({ url, headers, body, agent }, cb) => {
req.on('response', res => {
bufferResponse(res, (err, body) => {
if (err) return onError(err)
if (res.statusCode < 200 || res.statusCode >= 300) {
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
return onError(new Error(`Bad statusCode from API: ${res.statusCode}\n${body}`))
}
cb(null, body)
Expand All @@ -35,10 +42,12 @@ module.exports = ({ url, headers, body, agent }, cb) => {
req.end()
}

const bufferResponse = (stream, cb) => {
const bufferResponse = (stream: NodeJS.ReadableStream, cb: (err: Error | null, body?: string) => void) => {
let data = ''
stream.on('error', cb)
stream.setEncoding('utf8')
stream.on('data', d => { data += d })
stream.on('end', () => cb(null, data))
}

export default request
2 changes: 1 addition & 1 deletion packages/delivery-node/test/delivery.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import delivery from '../'
import delivery from '../src/delivery'
import http from 'http'
import type { Client, EventDeliveryPayload, SessionDeliveryPayload } from '@bugsnag/core'
import type { AddressInfo } from 'net'
Expand Down
8 changes: 8 additions & 0 deletions packages/delivery-node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"lib": ["es2017"],
"types": ["node"]
}
}
Loading