Add 'packages/sentry/' from commit '6cb773ae9b303ce4def1b801cac9fa91be5ea747'

git-subtree-dir: packages/sentry
git-subtree-mainline: fbb2aa875d
git-subtree-split: 6cb773ae9b
pull/31/head
Yusuke Wada 2023-02-04 16:12:03 +09:00
commit d6d5e25979
18 changed files with 6312 additions and 0 deletions

View File

@ -0,0 +1,61 @@
const { defineConfig } = require('eslint-define-config')
module.exports = defineConfig({
root: true,
extends: [
'eslint:recommended',
'plugin:node/recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2021,
},
plugins: ['@typescript-eslint', 'import'],
globals: {
fetch: false,
Response: false,
Request: false,
addEventListener: false,
},
rules: {
quotes: ['error', 'single'],
semi: ['error', 'never'],
'no-debugger': ['error'],
'no-empty': ['warn', { allowEmptyCatch: true }],
'no-process-exit': 'off',
'no-useless-escape': 'off',
'prefer-const': [
'warn',
{
destructuring: 'all',
},
],
'@typescript-eslint/ban-types': [
'error',
{
types: {
Function: false,
},
},
],
'sort-imports': 0,
'import/order': [2, { alphabetize: { order: 'asc' } }],
'node/no-missing-import': 'off',
'node/no-missing-require': 'off',
'node/no-deprecated-api': 'off',
'node/no-unpublished-import': 'off',
'node/no-unpublished-require': 'off',
'node/no-unsupported-features/es-syntax': 'off',
'@typescript-eslint/no-empty-function': ['error', { allow: ['arrowFunctions'] }],
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-inferrable-types': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/consistent-type-imports': ['error', { prefer: 'type-imports' }],
},
})

View File

@ -0,0 +1,27 @@
name: ci
on:
push:
branches: [main]
pull_request:
branches: ['*']
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16.x
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
deno:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: deno test deno_test

9
packages/sentry/.gitignore vendored 100644
View File

@ -0,0 +1,9 @@
dist
node_modules
.yarn/*
yarn-error.log
*.tgz
# for debug or playing
sandbox

View File

@ -0,0 +1,9 @@
{
"printWidth": 100,
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"endOfLine": "lf"
}

View File

@ -0,0 +1,49 @@
# Sentry middleware for Hono
## Information
Sentry Middleware `@honojs/sentry` is renamed to `@hono/sentry`.
`@honojs/sentry` is not maintained, please use `@hono/sentry`.
Also, for Deno, you can use import with `npm:` prefix like `npm:@hono/sentry`.
---
Sentry middleware for [Hono](https://github.com/honojs/hono).
This middleware sends captured exceptions to the specified Sentry data source name via [toucan-js](https://github.com/robertcepa/toucan-js).
## Usage
```ts
import { sentry } from '@hono/sentry'
import { Hono } from 'hono'
const app = new Hono()
app.use('*', sentry())
app.get('/', (c) => c.text('foo'))
export default app
```
## Deno
```ts
import { serve } from 'https://deno.land/std/http/server.ts'
import { sentry } from 'npm:@hono/sentry'
import { Hono } from 'https://deno.land/x/hono/mod.ts'
const app = new Hono()
app.use('*', sentry({ dsn: 'https://xxxxxx@xxx.ingest.sentry.io/xxxxxx' }))
app.get('/', (c) => c.text('foo'))
serve(app.fetch)
```
## Author
Samuel Lippert <https://github.com/sam-lippert>
## License
MIT

View File

@ -0,0 +1,49 @@
# Sentry middleware for Hono
## Information
Sentry Middleware `@honojs/sentry` is renamed to `@hono/sentry`.
`@honojs/sentry` is not maintained, please use `@hono/sentry`.
Also, for Deno, you can use import with `npm:` prefix like `npm:@hono/sentry`.
---
Sentry middleware for [Hono](https://github.com/honojs/hono).
This middleware sends captured exceptions to the specified Sentry data source name via [toucan-js](https://github.com/robertcepa/toucan-js).
## Usage
```ts
import { sentry } from '@hono/sentry'
import { Hono } from 'hono'
const app = new Hono()
app.use('*', sentry())
app.get('/', (c) => c.text('foo'))
export default app
```
## Deno
```ts
import { serve } from 'https://deno.land/std/http/server.ts'
import { sentry } from 'npm:@hono/sentry'
import { Hono } from 'https://deno.land/x/hono/mod.ts'
const app = new Hono()
app.use('*', sentry({ dsn: 'https://xxxxxx@xxx.ingest.sentry.io/xxxxxx' }))
app.get('/', (c) => c.text('foo'))
serve(app.fetch)
```
## Author
Samuel Lippert <https://github.com/sam-lippert>
## License
MIT

View File

@ -0,0 +1,68 @@
import type { Context, MiddlewareHandler } from 'https://deno.land/x/hono/mod.ts'
import Toucan from 'https://cdn.skypack.dev/toucan-js@2.6.1'
declare module 'https://deno.land/x/hono/mod.ts' {
interface ContextVariableMap {
sentry: Toucan
}
}
interface Bindings {
SENTRY_DSN?: string
NEXT_PUBLIC_SENTRY_DSN?: string
}
class MockContext implements ExecutionContext {
passThroughOnException(): void {
throw new Error('Method not implemented.')
}
async waitUntil(promise: Promise<any>): Promise<void> {
await promise
}
}
export type Options = {
dsn?: string
allowedCookies?: string[] | RegExp
allowedHeaders?: string[] | RegExp
allowedSearchParams?: string[] | RegExp
attachStacktrace?: boolean
debug?: boolean
environment?: string
maxBreadcrumbs?: number
pkg?: Record<string, any>
release?: string
}
export const sentry = (
options?: Options,
callback?: (sentry: Toucan) => void
): MiddlewareHandler<string, { Bindings: Bindings }> => {
return async (c, next) => {
let hasExecutionContext = true
try {
c.executionCtx
} catch {
hasExecutionContext = false
}
const sentry = new Toucan({
dsn: c.env.SENTRY_DSN ?? c.env.NEXT_PUBLIC_SENTRY_DSN,
allowedHeaders: ['user-agent'],
allowedSearchParams: /(.*)/,
request: c.req,
context: hasExecutionContext ? c.executionCtx : new MockContext(),
...options,
})
c.set('sentry', sentry)
if (callback) callback(sentry)
await next()
if (c.error) {
sentry.captureException(c.error)
}
}
}
export const getSentry = (c: Context) => {
return c.get('sentry')
}

View File

@ -0,0 +1 @@
export * from "./index.ts";

View File

@ -0,0 +1,3 @@
{
"deno.enable": true
}

View File

@ -0,0 +1,2 @@
export { assert, assertEquals } from 'https://deno.land/std@0.148.0/testing/asserts.ts'
export { Hono } from 'https://deno.land/x/hono@v2.6.1/mod.ts'

View File

@ -0,0 +1,29 @@
import { assertNotEquals } from 'https://deno.land/std@0.148.0/testing/asserts.ts'
import { sentry } from '../deno_dist/mod.ts'
import { assertEquals, Hono } from './deps.ts'
// Test just only minimal patterns.
// Because others are tested well in Cloudflare Workers environment already.
Deno.test('Sentry Middleware', async () => {
const app = new Hono()
app.use(
'/sentry/*',
sentry(undefined, (sentry) => {
sentry.setUser({ id: 'test' })
})
)
app.get('/sentry/foo', (c) => c.text('foo'))
app.get('/sentry/error', () => {
throw new Error('a catastrophic error')
})
let req = new Request('http://localhost/sentry/foo')
let res = await app.fetch(req)
assertNotEquals(res, null)
assertEquals(res.status, 200)
req = new Request('http://localhost/sentry/error')
res = await app.fetch(req)
assertNotEquals(res, null)
assertEquals(res.status, 500)
})

View File

@ -0,0 +1,7 @@
module.exports = {
testMatch: ['**/test/**/*.+(ts|tsx|js)', '**/src/**/(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
testEnvironment: 'miniflare',
}

View File

@ -0,0 +1,64 @@
{
"name": "@honojs/sentry",
"version": "0.0.6",
"description": "Sentry Middleware for Hono",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/index.js",
"dist/index.d.ts"
],
"scripts": {
"test": "jest",
"test:deno": "deno test deno_test",
"test:all": "yarn test && yarn test:deno",
"denoify": "rimraf deno_dist && denoify",
"build": "rimraf dist && tsc",
"prerelease": "yarn build && yarn denoify && yarn test:all",
"release": "np"
},
"denoify": {
"replacer": "dist/replacer.js"
},
"license": "MIT",
"private": false,
"repository": {
"type": "git",
"url": "https://github.com/honojs/middleware.git"
},
"homepage": "https://github.com/honojs/middleware",
"author": "Samuel Lippert <samuel@driv.ly> (https://github.com/sam-lippert)",
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"peerDependencies": {
"hono": "^2.6.1"
},
"dependencies": {
"toucan-js": "^2.6.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.14.0",
"@types/jest": "^28.1.4",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"denoify": "^1.4.5",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-define-config": "^1.6.0",
"eslint-import-resolver-typescript": "^3.4.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"hono": "^2.6.1",
"jest": "^28.1.2",
"jest-environment-miniflare": "^2.6.0",
"np": "^7.6.2",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.5",
"typescript": "^4.7.4"
}
}

View File

@ -0,0 +1,68 @@
import type { Context, MiddlewareHandler } from 'hono'
import Toucan from 'toucan-js'
declare module 'hono' {
interface ContextVariableMap {
sentry: Toucan
}
}
interface Bindings {
SENTRY_DSN?: string
NEXT_PUBLIC_SENTRY_DSN?: string
}
class MockContext implements ExecutionContext {
passThroughOnException(): void {
throw new Error('Method not implemented.')
}
async waitUntil(promise: Promise<any>): Promise<void> {
await promise
}
}
export type Options = {
dsn?: string
allowedCookies?: string[] | RegExp
allowedHeaders?: string[] | RegExp
allowedSearchParams?: string[] | RegExp
attachStacktrace?: boolean
debug?: boolean
environment?: string
maxBreadcrumbs?: number
pkg?: Record<string, any>
release?: string
}
export const sentry = (
options?: Options,
callback?: (sentry: Toucan) => void
): MiddlewareHandler<string, { Bindings: Bindings }> => {
return async (c, next) => {
let hasExecutionContext = true
try {
c.executionCtx
} catch {
hasExecutionContext = false
}
const sentry = new Toucan({
dsn: c.env.SENTRY_DSN ?? c.env.NEXT_PUBLIC_SENTRY_DSN,
allowedHeaders: ['user-agent'],
allowedSearchParams: /(.*)/,
request: c.req,
context: hasExecutionContext ? c.executionCtx : new MockContext(),
...options,
})
c.set('sentry', sentry)
if (callback) callback(sentry)
await next()
if (c.error) {
sentry.captureException(c.error)
}
}
}
export const getSentry = (c: Context) => {
return c.get('sentry')
}

View File

@ -0,0 +1,26 @@
// @denoify-ignore
import { makeThisModuleAnExecutableReplacer, ParsedImportExportStatement } from 'denoify'
makeThisModuleAnExecutableReplacer(async ({ parsedImportExportStatement, version }) => {
if (parsedImportExportStatement.parsedArgument.nodeModuleName === 'toucan-js') {
return ParsedImportExportStatement.stringify({
...parsedImportExportStatement,
parsedArgument: {
type: 'URL',
url: `https://cdn.skypack.dev/toucan-js@${version}`,
},
})
}
if (parsedImportExportStatement.parsedArgument.nodeModuleName === 'hono') {
return ParsedImportExportStatement.stringify({
...parsedImportExportStatement,
parsedArgument: {
type: 'URL',
url: `https://deno.land/x/hono/mod.ts`,
},
})
}
return undefined
})

View File

@ -0,0 +1,54 @@
import { Hono } from 'hono'
import { sentry, getSentry } from '../src'
// Mock
class Context implements ExecutionContext {
passThroughOnException(): void {
throw new Error('Method not implemented.')
}
async waitUntil(promise: Promise<any>): Promise<void> {
await promise
}
}
const captureException = jest.fn()
const log = jest.fn()
jest.mock('toucan-js', () => jest.fn().mockImplementation(() => ({ captureException, log })))
const callback = jest.fn()
describe('Sentry middleware', () => {
const app = new Hono()
app.use('/sentry/*', sentry(undefined, callback))
app.get('/sentry/foo', (c) => c.text('foo'))
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
app.get('/sentry/bar', (c) => getSentry(c).log('bar') || c.text('bar'))
app.get('/sentry/error', () => {
throw new Error('a catastrophic error')
})
it('Should initialize Toucan', async () => {
const req = new Request('http://localhost/sentry/foo')
const res = await app.fetch(req, {}, new Context())
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(callback).toHaveBeenCalled()
})
it('Should make Sentry available via context', async () => {
const req = new Request('http://localhost/sentry/bar')
const res = await app.fetch(req, {}, new Context())
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(log).toHaveBeenCalled()
})
it('Should report errors', async () => {
const req = new Request('http://localhost/sentry/error')
const res = await app.fetch(req, {}, new Context())
expect(res).not.toBeNull()
expect(res.status).toBe(500)
expect(captureException).toHaveBeenCalled()
})
})

View File

@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"declaration": true,
"moduleResolution": "Node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"strictPropertyInitialization": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"types": [
"jest",
"node",
"@cloudflare/workers-types"
],
"rootDir": "./src",
"outDir": "./dist",
},
"include": [
"src/**/*.ts"
],
}

File diff suppressed because it is too large Load Diff