Skip to content

Commit 26ecaf9

Browse files
committed
utf8 support, playground
1 parent fcbbe2a commit 26ecaf9

File tree

12 files changed

+1073
-29
lines changed

12 files changed

+1073
-29
lines changed

workers/sentry/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"author": "CodeX",
77
"license": "UNLICENSED",
88
"private": true,
9-
"workerType": "errors/sentry",
9+
"workerType": "external/sentry",
1010
"dependencies": {
1111
"@sentry/core": "^8.45.1"
1212
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# You can get this token from the Hawk dashboard
2+
HAWK_INTEGRATION_TOKEN=
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Hawk Sentry Playground
2+
3+
You can send some events via Sentry SDK to Hawk here.
4+
5+
## How to use
6+
7+
1. Copy `.env.example` to `.env` and fill in the `HAWK_INTEGRATION_TOKEN`
8+
2. Run `yarn install`
9+
3. Run `yarn start`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "hawk-sentry-playground",
3+
"version": "0.0.1",
4+
"description": "You can send some events via Sentry SDK to Hawk here",
5+
"main": "src/index.ts",
6+
"author": "CodeX",
7+
"license": "MIT",
8+
"scripts": {
9+
"start": "ts-node src/index.ts"
10+
},
11+
"dependencies": {
12+
"@sentry/node": "^8.46.0",
13+
"dotenv": "^16.4.7",
14+
"ts-node": "^10.9.2"
15+
}
16+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { DecodedIntegrationToken } from '@hawk.so/types';
2+
import * as Sentry from '@sentry/node';
3+
import dotenv from 'dotenv';
4+
5+
dotenv.config();
6+
7+
if (!process.env.HAWK_INTEGRATION_TOKEN) {
8+
throw new Error('Fill HAWK_INTEGRATION_TOKEN in .env file');
9+
}
10+
11+
/**
12+
* Decode Hawk integration token
13+
*
14+
* @param token - stringified integration token
15+
*/
16+
function decodeIntegrationToken(token: string): DecodedIntegrationToken {
17+
return JSON.parse(Buffer.from(token, 'base64').toString('utf-8'));
18+
}
19+
20+
/**
21+
* Sentry DSN should follow this:
22+
* const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+)?)?@)([\w.-]+)(?::(\d+))?\/(.+)/;
23+
* https://github.com/getsentry/sentry-javascript/blob/d773cb7324480ed3cffc14504f0e41951e344d19/packages/core/src/utils-hoist/dsn.ts#L7
24+
*
25+
* So we can't use our integration token as is.
26+
* Instead, we will concatinate integrationId and secret and remove hyphens from their uuids.
27+
*/
28+
function getHexIntegrationToken(): string {
29+
const token = process.env.HAWK_INTEGRATION_TOKEN as string;
30+
31+
const { integrationId, secret } = decodeIntegrationToken(token);
32+
33+
const removeHyphens = (str: string): string => str.replace(/-/g, '');
34+
35+
return `${removeHyphens(integrationId)}${removeHyphens(secret)}`;
36+
}
37+
38+
const dsn = `https://${getHexIntegrationToken()}@k1.hawk.so/0`;
39+
40+
console.log('dsn', dsn);
41+
42+
/**
43+
* Initialize Sentry
44+
*/
45+
Sentry.init({
46+
dsn: `https://${getHexIntegrationToken()}@k1.hawk.so/0`,
47+
debug: true,
48+
});
49+
50+
/**
51+
* Function that will throw an error
52+
*/
53+
function throwDemoError(): void {
54+
throw new Error('This is a demo error for Sentry!');
55+
}
56+
57+
/**
58+
* Main function to run our demo
59+
*/
60+
async function main(): Promise<void> {
61+
try {
62+
/**
63+
* Attempt to run function that throws error
64+
*/
65+
throwDemoError();
66+
} catch (error) {
67+
/**
68+
* Capture and send error to Sentry
69+
*/
70+
Sentry.captureException(error);
71+
// console.error('Error caught and sent to Sentry:', error);
72+
}
73+
74+
/**
75+
* Allow time for Sentry to send the error before the process exits
76+
*/
77+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
78+
await Sentry.close(2000);
79+
}
80+
81+
/**
82+
* Run the demo
83+
*/
84+
main()
85+
.then(() => {
86+
console.log('✨ Demo completed');
87+
})
88+
.catch(console.error);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
"lib": ["ES2020"],
6+
"rootDir": "./",
7+
"strict": true,
8+
"esModuleInterop": true,
9+
"skipLibCheck": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"moduleResolution": "node",
12+
"resolveJsonModule": true,
13+
"sourceMap": true
14+
},
15+
"include": [
16+
"*.ts"
17+
],
18+
"exclude": [
19+
"node_modules",
20+
"dist"
21+
]
22+
}

0 commit comments

Comments
 (0)