diff --git a/lib/utils/helpers.test.ts b/lib/utils/helpers.test.ts new file mode 100644 index 0000000000..5220e0c5b1 --- /dev/null +++ b/lib/utils/helpers.test.ts @@ -0,0 +1,8 @@ +import { describe, expect, it } from '@jest/globals'; +import { getRouteNameFromPath } from '@/utils/helpers'; + +describe('helpers', () => { + it('getRouteNameFromPath', () => { + expect(getRouteNameFromPath('/test/1')).toBe('test'); + }); +}); diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index f7f93c0988..32cff45b11 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -1,5 +1,7 @@ import { Context } from 'hono'; -import { ReadableStream } from 'node:stream/web'; +import { Ipware } from '@fullerstack/nax-ipware'; + +const ipware = new Ipware(); export const getRouteNameFromPath = (path: string) => { const p = path.split('/').filter(Boolean); @@ -9,20 +11,4 @@ export const getRouteNameFromPath = (path: string) => { return null; }; -export const getIp = (ctx: Context) => ctx.req.header('X-Real-IP') || ctx.req.header('X-Forwarded-For'); - -export const streamToString = async (stream: ReadableStream) => { - const reader = stream.getReader(); - const chunks: Uint8Array[] = []; - // eslint-disable-next-line no-constant-condition - while (true) { - // eslint-disable-next-line no-await-in-loop - const { done, value } = await reader.read(); - if (done) { - break; - } - chunks.push(value); - } - const result = new TextDecoder('utf-8').decode(new Uint8Array(chunks.reduce((a, c) => a + c.length, 0))); - return result; -}; +export const getIp = (ctx: Context) => ipware.getClientIP(ctx.req.raw)?.ip; diff --git a/package.json b/package.json index 857614654e..f22d82dfe0 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "outputDirectory": "coverage" }, "dependencies": { + "@fullerstack/nax-ipware": "0.10.0", "@hono/node-server": "1.8.1", "@koa/router": "12.0.1", "@notionhq/client": "2.2.14", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 447b908aff..8b0b5bc40e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + '@fullerstack/nax-ipware': + specifier: 0.10.0 + version: 0.10.0(typescript@5.3.3) '@hono/node-server': specifier: 1.8.1 version: 1.8.1 @@ -2229,6 +2232,16 @@ packages: engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dev: true + /@fullerstack/nax-ipware@0.10.0(typescript@5.3.3): + resolution: {integrity: sha512-A2g9pqeRprTcaK9zQk4fQVKKoH0YFTVTFJkJNPuq6UIs2nWnaWDeoK8xWIQHJF13k9Tz84iDQlovs7YDw/iZDQ==} + dependencies: + lodash: 4.17.21 + ts-essentials: 7.0.3(typescript@5.3.3) + tslib: 2.6.2 + transitivePeerDependencies: + - typescript + dev: false + /@hono/node-server@1.8.1: resolution: {integrity: sha512-9ondWs/5EuyZOd/iyiRp0GQfMEq+IA+IbV8Wy2bgdr5+cC55I3xbQrqtXKprw7jJhfvq3IVw7ALiuvCEmLdCAA==} engines: {node: '>=18.14.1'} @@ -10947,6 +10960,14 @@ packages: deprecated: npm package tarball contains useless codeclimate-reporter binary, please update to version 3.1.1. See https://github.com/adriengibrat/ts-custom-error/issues/32 dev: false + /ts-essentials@7.0.3(typescript@5.3.3): + resolution: {integrity: sha512-8+gr5+lqO3G84KdiTSMRLtuyJ+nTBVRKuCrK4lidMPdVeEp0uqC875uE5NMcaA7YYMN7XsNiFQuMvasF8HT/xQ==} + peerDependencies: + typescript: '>=3.7.0' + dependencies: + typescript: 5.3.3 + dev: false + /ts-jest@29.1.2(@babel/core@7.23.9)(jest@29.7.0)(typescript@5.3.3): resolution: {integrity: sha512-br6GJoH/WUX4pu7FbZXuWGKGNDuU7b8Uj77g/Sp7puZV6EXzuByl6JrECvm0MzVzSTkSHWTihsXt+5XYER5b+g==} engines: {node: ^16.10.0 || ^18.0.0 || >=20.0.0}