Skip to content

Commit d32b550

Browse files
HazATAbhiPrasad
andauthored
feat: Add Bun SDK (#9029)
Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 4a4366d commit d32b550

21 files changed

+649
-1
lines changed

Diff for: .github/workflows/build.yml

+29
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,34 @@ jobs:
391391
- name: Compute test coverage
392392
uses: codecov/codecov-action@v3
393393

394+
job_bun_unit_tests:
395+
name: Bun Unit Tests
396+
needs: [job_get_metadata, job_build]
397+
timeout-minutes: 10
398+
runs-on: ubuntu-20.04
399+
strategy:
400+
fail-fast: false
401+
steps:
402+
- name: Check out current commit (${{ needs.job_get_metadata.outputs.commit_label }})
403+
uses: actions/checkout@v3
404+
with:
405+
ref: ${{ env.HEAD_COMMIT }}
406+
- name: Set up Node
407+
uses: actions/setup-node@v3
408+
with:
409+
node-version: ${{ matrix.node }}
410+
- name: Set up Bun
411+
uses: oven-sh/setup-bun@v1
412+
- name: Restore caches
413+
uses: ./.github/actions/restore-cache
414+
env:
415+
DEPENDENCY_CACHE_KEY: ${{ needs.job_build.outputs.dependency_cache_key }}
416+
- name: Run tests
417+
run: |
418+
yarn test-ci-bun
419+
- name: Compute test coverage
420+
uses: codecov/codecov-action@v3
421+
394422
job_node_unit_tests:
395423
name: Node (${{ matrix.node }}) Unit Tests
396424
needs: [job_get_metadata, job_build]
@@ -864,6 +892,7 @@ jobs:
864892
job_build,
865893
job_browser_build_tests,
866894
job_browser_unit_tests,
895+
job_bun_unit_tests,
867896
job_node_unit_tests,
868897
job_nextjs_integration_test,
869898
job_node_integration_tests,

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
"postpublish": "lerna run --stream --concurrency 1 postpublish",
2828
"test": "lerna run --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\" test",
2929
"test:unit": "lerna run --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\" test:unit",
30-
"test-ci-browser": "lerna run test --ignore \"@sentry/{node,node-experimental,opentelemetry-node,serverless,nextjs,remix,gatsby,sveltekit}\" --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\"",
30+
"test-ci-browser": "lerna run test --ignore \"@sentry/{bun,node,node-experimental,opentelemetry-node,serverless,nextjs,remix,gatsby,sveltekit}\" --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\"",
3131
"test-ci-node": "ts-node ./scripts/node-unit-tests.ts",
32+
"test-ci-bun": "lerna run test --scope @sentry/bun",
3233
"test:update-snapshots": "lerna run test:update-snapshots",
3334
"yalc:publish": "lerna run yalc:publish"
3435
},
@@ -41,6 +42,7 @@
4142
"packages/angular-ivy",
4243
"packages/browser",
4344
"packages/browser-integration-tests",
45+
"packages/bun",
4446
"packages/core",
4547
"packages/e2e-tests",
4648
"packages/ember",

Diff for: packages/bun/.eslintrc.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
env: {
3+
node: true,
4+
},
5+
extends: ['../../.eslintrc.js'],
6+
rules: {
7+
'@sentry-internal/sdk/no-optional-chaining': 'off',
8+
'@sentry-internal/sdk/no-nullish-coalescing': 'off',
9+
'@sentry-internal/sdk/no-unsupported-es6-methods': 'off',
10+
'@sentry-internal/sdk/no-class-field-initializers': 'off',
11+
},
12+
};

Diff for: packages/bun/LICENSE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Copyright (c) 2023 Sentry (https://sentry.io) and individual contributors. All rights reserved.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
5+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
6+
persons to whom the Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
9+
Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
13+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
14+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: packages/bun/README.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<p align="center">
2+
<a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank">
3+
<img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84">
4+
</a>
5+
</p>
6+
7+
# Official Sentry SDK for Bun (Beta)
8+
9+
[![npm version](https://img.shields.io/npm/v/@sentry/bun.svg)](https://www.npmjs.com/package/@sentry/bun)
10+
[![npm dm](https://img.shields.io/npm/dm/@sentry/bun.svg)](https://www.npmjs.com/package/@sentry/bun)
11+
[![npm dt](https://img.shields.io/npm/dt/@sentry/bun.svg)](https://www.npmjs.com/package/@sentry/bun)
12+
13+
## Links
14+
15+
- [Official SDK Docs](https://docs.sentry.io/quickstart/)
16+
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)
17+
18+
The Sentry Bun SDK is in beta. Please help us improve the SDK by [reporting any issues or giving us feedback](https://github.com/getsentry/sentry-javascript/issues).
19+
20+
## Usage
21+
22+
To use this SDK, call `init(options)` as early as possible in the main entry module. This will initialize the SDK and
23+
hook into the environment. Note that you can turn off almost all side effects using the respective options.
24+
25+
```javascript
26+
// ES5 Syntax
27+
const Sentry = require('@sentry/bun');
28+
// ES6 Syntax
29+
import * as Sentry from '@sentry/bun';
30+
31+
Sentry.init({
32+
dsn: '__DSN__',
33+
// ...
34+
});
35+
```
36+
37+
To set context information or send manual events, use the exported functions of `@sentry/bun`. Note that these
38+
functions will not perform any action before you have called `init()`:
39+
40+
```javascript
41+
// Set user information, as well as tags and further extras
42+
Sentry.configureScope(scope => {
43+
scope.setExtra('battery', 0.7);
44+
scope.setTag('user_mode', 'admin');
45+
scope.setUser({ id: '4711' });
46+
// scope.clear();
47+
});
48+
49+
// Add a breadcrumb for future events
50+
Sentry.addBreadcrumb({
51+
message: 'My Breadcrumb',
52+
// ...
53+
});
54+
55+
// Capture exceptions, messages or manual events
56+
Sentry.captureMessage('Hello, world!');
57+
Sentry.captureException(new Error('Good bye'));
58+
Sentry.captureEvent({
59+
message: 'Manual',
60+
stacktrace: [
61+
// ...
62+
],
63+
});
64+
```
65+
66+
It's not possible to capture unhandled exceptions, unhandled promise rejections now - Bun is working on adding support for it.
67+
[Github Issue](https://github.com/oven-sh/bun/issues/5091) follow this issue. To report errors to Sentry, you have to manually try-catch and call `Sentry.captureException` in the catch block.
68+
69+
```ts
70+
import * as Sentry from '@sentry/bun';
71+
72+
try {
73+
throw new Error('test');
74+
} catch (e) {
75+
Sentry.captureException(e);
76+
}
77+
```
78+

Diff for: packages/bun/jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../jest/jest.config.js');

Diff for: packages/bun/package.json

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "@sentry/bun",
3+
"version": "7.69.0",
4+
"description": "Official Sentry SDK for bun",
5+
"repository": "git://github.com/getsentry/sentry-javascript.git",
6+
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/bun",
7+
"author": "Sentry",
8+
"license": "MIT",
9+
"engines": {
10+
"node": ">=8"
11+
},
12+
"main": "build/esm/index.js",
13+
"module": "build/esm/index.js",
14+
"types": "build/types/index.d.ts",
15+
"typesVersions": {
16+
"<4.9": {
17+
"build/npm/types/index.d.ts": [
18+
"build/npm/types-ts3.8/index.d.ts"
19+
]
20+
}
21+
},
22+
"publishConfig": {
23+
"access": "public"
24+
},
25+
"dependencies": {
26+
"@sentry/core": "7.69.0",
27+
"@sentry/node": "7.69.0",
28+
"@sentry/types": "7.69.0",
29+
"@sentry/utils": "7.69.0"
30+
},
31+
"devDependencies": {
32+
"bun-types": "latest"
33+
},
34+
"scripts": {
35+
"build": "run-p build:transpile build:types",
36+
"build:dev": "yarn build",
37+
"build:transpile": "rollup -c rollup.npm.config.js",
38+
"build:types": "run-s build:types:core build:types:downlevel",
39+
"build:types:core": "tsc -p tsconfig.types.json",
40+
"build:types:downlevel": "yarn downlevel-dts build/types build/types-ts3.8 --to ts3.8",
41+
"build:watch": "run-p build:transpile:watch build:types:watch",
42+
"build:dev:watch": "yarn build:watch",
43+
"build:transpile:watch": "rollup -c rollup.npm.config.js --watch",
44+
"build:types:watch": "tsc -p tsconfig.types.json --watch",
45+
"build:tarball": "ts-node ../../scripts/prepack.ts && npm pack ./build",
46+
"circularDepCheck": "madge --circular src/index.ts",
47+
"clean": "rimraf build coverage sentry-node-*.tgz",
48+
"fix": "run-s fix:eslint fix:prettier",
49+
"fix:eslint": "eslint . --format stylish --fix",
50+
"fix:prettier": "prettier --write \"{src,test,scripts}/**/**.ts\"",
51+
"lint": "run-s lint:prettier lint:eslint",
52+
"lint:eslint": "eslint . --format stylish",
53+
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.ts\"",
54+
"install:bun": "node ./scripts/install-bun.js",
55+
"test": "run-s install:bun test:bun",
56+
"test:bun": "bun test",
57+
"test:watch": "bun test --watch",
58+
"yalc:publish": "ts-node ../../scripts/prepack.ts && yalc publish build --push"
59+
},
60+
"volta": {
61+
"extends": "../../package.json"
62+
},
63+
"sideEffects": false,
64+
"madge":{
65+
"detectiveOptions": {
66+
"ts": {
67+
"skipTypeImports": true
68+
}
69+
}
70+
}
71+
}

Diff for: packages/bun/rollup.npm.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
2+
3+
const config = makeNPMConfigVariants(makeBaseNPMConfig());
4+
5+
// remove cjs from config array config[0].output.format == cjs
6+
export default [config[1]];

Diff for: packages/bun/scripts/install-bun.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-disable no-console */
2+
if (process.env.CI) {
3+
// This script is not needed in CI we install bun via GH actions
4+
return;
5+
}
6+
const { exec } = require('child_process');
7+
const https = require('https');
8+
9+
// Define the URL of the Bash script for bun installation
10+
const installScriptUrl = 'https://bun.sh/install';
11+
12+
// Check if bun is installed
13+
exec('bun -version', error => {
14+
if (error) {
15+
console.error('bun is not installed. Installing...');
16+
// Download and execute the installation script
17+
https
18+
.get(installScriptUrl, res => {
19+
if (res.statusCode !== 200) {
20+
console.error(`Failed to download the installation script (HTTP ${res.statusCode})`);
21+
process.exit(1);
22+
}
23+
24+
res.setEncoding('utf8');
25+
let scriptData = '';
26+
27+
res.on('data', chunk => {
28+
scriptData += chunk;
29+
});
30+
31+
res.on('end', () => {
32+
// Execute the downloaded script
33+
exec(scriptData, installError => {
34+
if (installError) {
35+
console.error('Failed to install bun:', installError);
36+
process.exit(1);
37+
}
38+
console.log('bun has been successfully installed.');
39+
});
40+
});
41+
})
42+
.on('error', e => {
43+
console.error('Failed to download the installation script:', e);
44+
process.exit(1);
45+
});
46+
} else {
47+
// Bun is installed
48+
}
49+
});

Diff for: packages/bun/src/client.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { ServerRuntimeClientOptions } from '@sentry/core';
2+
import { SDK_VERSION, ServerRuntimeClient } from '@sentry/core';
3+
import * as os from 'os';
4+
5+
import type { BunClientOptions } from './types';
6+
7+
/**
8+
* The Sentry Bun SDK Client.
9+
*
10+
* @see BunClientOptions for documentation on configuration options.
11+
* @see SentryClient for usage documentation.
12+
*/
13+
export class BunClient extends ServerRuntimeClient<BunClientOptions> {
14+
/**
15+
* Creates a new Bun SDK instance.
16+
* @param options Configuration options for this SDK.
17+
*/
18+
public constructor(options: BunClientOptions) {
19+
options._metadata = options._metadata || {};
20+
options._metadata.sdk = options._metadata.sdk || {
21+
name: 'sentry.javascript.bun',
22+
packages: [
23+
{
24+
name: 'npm:@sentry/bun',
25+
version: SDK_VERSION,
26+
},
27+
],
28+
version: SDK_VERSION,
29+
};
30+
31+
const clientOptions: ServerRuntimeClientOptions = {
32+
...options,
33+
platform: 'bun',
34+
runtime: { name: 'bun', version: Bun.version },
35+
serverName: options.serverName || global.process.env.SENTRY_NAME || os.hostname(),
36+
};
37+
38+
super(clientOptions);
39+
}
40+
}

0 commit comments

Comments
 (0)