|
| 1 | +import { serve } from '@hono/node-server'; |
| 2 | +import * as Sentry from '@sentry/node'; |
| 3 | +import { sendPortToRunner } from '@sentry-internal/node-core-integration-tests'; |
| 4 | +import { Hono } from 'hono'; |
| 5 | +import { HTTPException } from 'hono/http-exception'; |
| 6 | + |
| 7 | +const app = new Hono(); |
| 8 | + |
| 9 | +Sentry.setupHonoErrorHandler(app); |
| 10 | + |
| 11 | +// Global middleware to capture all requests |
| 12 | +app.use(async function global(c, next) { |
| 13 | + await next(); |
| 14 | +}); |
| 15 | + |
| 16 | +const basePaths = ['/sync', '/async']; |
| 17 | +const methods = ['get', 'post', 'put', 'delete', 'patch']; |
| 18 | + |
| 19 | +basePaths.forEach(basePath => { |
| 20 | + // Sub-path middleware to capture all requests under the basePath |
| 21 | + app.use(`${basePath}/*`, async function base(c, next) { |
| 22 | + await next(); |
| 23 | + }); |
| 24 | + |
| 25 | + const baseApp = new Hono(); |
| 26 | + methods.forEach(method => { |
| 27 | + baseApp[method]('/', c => { |
| 28 | + const response = c.text('response 200'); |
| 29 | + if (basePath === '/sync') return response; |
| 30 | + return Promise.resolve(response); |
| 31 | + }); |
| 32 | + |
| 33 | + baseApp[method]( |
| 34 | + '/middleware', |
| 35 | + // anonymous middleware |
| 36 | + async (c, next) => { |
| 37 | + await next(); |
| 38 | + }, |
| 39 | + c => { |
| 40 | + const response = c.text('response 200'); |
| 41 | + if (basePath === '/sync') return response; |
| 42 | + return Promise.resolve(response); |
| 43 | + }, |
| 44 | + ); |
| 45 | + |
| 46 | + // anonymous middleware |
| 47 | + baseApp[method]('/middleware/separately', async (c, next) => { |
| 48 | + await next(); |
| 49 | + }); |
| 50 | + |
| 51 | + baseApp[method]('/middleware/separately', async c => { |
| 52 | + const response = c.text('response 200'); |
| 53 | + if (basePath === '/sync') return response; |
| 54 | + return Promise.resolve(response); |
| 55 | + }); |
| 56 | + |
| 57 | + baseApp.all('/all', c => { |
| 58 | + const response = c.text('response 200'); |
| 59 | + if (basePath === '/sync') return response; |
| 60 | + return Promise.resolve(response); |
| 61 | + }); |
| 62 | + |
| 63 | + baseApp.all( |
| 64 | + '/all/middleware', |
| 65 | + // anonymous middleware |
| 66 | + async (c, next) => { |
| 67 | + await next(); |
| 68 | + }, |
| 69 | + c => { |
| 70 | + const response = c.text('response 200'); |
| 71 | + if (basePath === '/sync') return response; |
| 72 | + return Promise.resolve(response); |
| 73 | + }, |
| 74 | + ); |
| 75 | + |
| 76 | + // anonymous middleware |
| 77 | + baseApp.all('/all/middleware/separately', async (c, next) => { |
| 78 | + await next(); |
| 79 | + }); |
| 80 | + |
| 81 | + baseApp.all('/all/middleware/separately', async c => { |
| 82 | + const response = c.text('response 200'); |
| 83 | + if (basePath === '/sync') return response; |
| 84 | + return Promise.resolve(response); |
| 85 | + }); |
| 86 | + |
| 87 | + baseApp.on(method, '/on', c => { |
| 88 | + const response = c.text('response 200'); |
| 89 | + if (basePath === '/sync') return response; |
| 90 | + return Promise.resolve(response); |
| 91 | + }); |
| 92 | + |
| 93 | + baseApp.on( |
| 94 | + method, |
| 95 | + '/on/middleware', |
| 96 | + // anonymous middleware |
| 97 | + async (c, next) => { |
| 98 | + await next(); |
| 99 | + }, |
| 100 | + c => { |
| 101 | + const response = c.text('response 200'); |
| 102 | + if (basePath === '/sync') return response; |
| 103 | + return Promise.resolve(response); |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + // anonymous middleware |
| 108 | + baseApp.on(method, '/on/middleware/separately', async (c, next) => { |
| 109 | + await next(); |
| 110 | + }); |
| 111 | + |
| 112 | + baseApp.on(method, '/on/middleware/separately', async c => { |
| 113 | + const response = c.text('response 200'); |
| 114 | + if (basePath === '/sync') return response; |
| 115 | + return Promise.resolve(response); |
| 116 | + }); |
| 117 | + |
| 118 | + baseApp[method]('/401', () => { |
| 119 | + const response = new HTTPException(401, { message: 'response 401' }); |
| 120 | + if (basePath === '/sync') throw response; |
| 121 | + return Promise.reject(response); |
| 122 | + }); |
| 123 | + |
| 124 | + baseApp.all('/all/401', () => { |
| 125 | + const response = new HTTPException(401, { message: 'response 401' }); |
| 126 | + if (basePath === '/sync') throw response; |
| 127 | + return Promise.reject(response); |
| 128 | + }); |
| 129 | + |
| 130 | + baseApp.on(method, '/on/401', () => { |
| 131 | + const response = new HTTPException(401, { message: 'response 401' }); |
| 132 | + if (basePath === '/sync') throw response; |
| 133 | + return Promise.reject(response); |
| 134 | + }); |
| 135 | + |
| 136 | + baseApp[method]('/402', () => { |
| 137 | + const response = new HTTPException(402, { message: 'response 402' }); |
| 138 | + if (basePath === '/sync') throw response; |
| 139 | + return Promise.reject(response); |
| 140 | + }); |
| 141 | + |
| 142 | + baseApp.all('/all/402', () => { |
| 143 | + const response = new HTTPException(402, { message: 'response 402' }); |
| 144 | + if (basePath === '/sync') throw response; |
| 145 | + return Promise.reject(response); |
| 146 | + }); |
| 147 | + |
| 148 | + baseApp.on(method, '/on/402', () => { |
| 149 | + const response = new HTTPException(402, { message: 'response 402' }); |
| 150 | + if (basePath === '/sync') throw response; |
| 151 | + return Promise.reject(response); |
| 152 | + }); |
| 153 | + |
| 154 | + baseApp[method]('/403', () => { |
| 155 | + const response = new HTTPException(403, { message: 'response 403' }); |
| 156 | + if (basePath === '/sync') throw response; |
| 157 | + return Promise.reject(response); |
| 158 | + }); |
| 159 | + |
| 160 | + baseApp.all('/all/403', () => { |
| 161 | + const response = new HTTPException(403, { message: 'response 403' }); |
| 162 | + if (basePath === '/sync') throw response; |
| 163 | + return Promise.reject(response); |
| 164 | + }); |
| 165 | + |
| 166 | + baseApp.on(method, '/on/403', () => { |
| 167 | + const response = new HTTPException(403, { message: 'response 403' }); |
| 168 | + if (basePath === '/sync') throw response; |
| 169 | + return Promise.reject(response); |
| 170 | + }); |
| 171 | + |
| 172 | + baseApp[method]('/500', () => { |
| 173 | + const response = new HTTPException(500, { message: 'response 500' }); |
| 174 | + if (basePath === '/sync') throw response; |
| 175 | + return Promise.reject(response); |
| 176 | + }); |
| 177 | + |
| 178 | + baseApp.all('/all/500', () => { |
| 179 | + const response = new HTTPException(500, { message: 'response 500' }); |
| 180 | + if (basePath === '/sync') throw response; |
| 181 | + return Promise.reject(response); |
| 182 | + }); |
| 183 | + |
| 184 | + baseApp.on(method, '/on/500', () => { |
| 185 | + const response = new HTTPException(500, { message: 'response 500' }); |
| 186 | + if (basePath === '/sync') throw response; |
| 187 | + return Promise.reject(response); |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + app.route(basePath, baseApp); |
| 192 | +}); |
| 193 | + |
| 194 | +const port = 8787; |
| 195 | +serve({ fetch: app.fetch, port }); |
| 196 | +sendPortToRunner(port); |
0 commit comments