logixlysia 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.2.0](https://github.com/PunGrumpy/logixlysia/compare/v2.1.0...v2.2.0) (2024-03-19)
4
+
5
+
6
+ ### Features
7
+
8
+ * **options:** add show ip ([4e997c5](https://github.com/PunGrumpy/logixlysia/commit/4e997c557e9aef6046a93e0cab9fe11a87df3430))
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **logger:** change to use array and push them ([3feefb7](https://github.com/PunGrumpy/logixlysia/commit/3feefb7a8dddc42c4eec54de40ab9dd8d5c70e70))
14
+
3
15
  ## [2.1.0](https://github.com/PunGrumpy/logixlysia/compare/v2.0.2...v2.1.0) (2024-01-25)
4
16
 
5
17
 
package/README.md CHANGED
@@ -23,6 +23,14 @@ const app = new Elysia({
23
23
  app.listen(3000)
24
24
  ```
25
25
 
26
+ ## `📚` Documentation
27
+
28
+ ### Options
29
+
30
+ | Option | Type | Description | Default |
31
+ | ------ | --------- | --------------------------------------------------------------------- | ------- |
32
+ | `ip` | `boolean` | Display the incoming IP address based on the `X-Forwarded-For` header | `false` |
33
+
26
34
  ## `📄` License
27
35
 
28
36
  Licensed under the [MIT License](LICENSE).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logixlysia",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "🦊 Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
@@ -15,13 +15,11 @@
15
15
  },
16
16
  "license": "MIT",
17
17
  "scripts": {
18
- "pretest": "bun run lint && bun run lint:fix && bun run prettier",
19
18
  "lint": "eslint . --ext .ts",
20
19
  "lint:fix": "eslint . --ext .ts --fix",
21
- "prepublish": "bun run pretest && bun run test",
22
- "test": "bun test --timeout 5000 --coverage --update-snapshots",
23
- "test:ci": "bun test --timeout 5000 --coverage --update-snapshots",
24
- "publish": "bun run prepublish && npm publish",
20
+ "test": "bun test --timeout 5000 --coverage",
21
+ "test:ci": "bun test --timeout 5000 --coverage",
22
+ "publish": "npm publish",
25
23
  "dev": "bun run --watch example/basic.ts",
26
24
  "prepare": "husky install",
27
25
  "lint:staged": "lint-staged",
@@ -70,17 +68,18 @@
70
68
  "middleware"
71
69
  ],
72
70
  "dependencies": {
73
- "@typescript-eslint/eslint-plugin": "^6.18.1",
74
- "@typescript-eslint/parser": "^6.18.1",
71
+ "@typescript-eslint/eslint-plugin": "^7.3.1",
72
+ "@typescript-eslint/parser": "^7.3.1",
75
73
  "chalk": "^5.3.0",
76
- "elysia": "^0.8.9",
77
- "eslint": "^8.56.0"
74
+ "elysia": "^1.0.4",
75
+ "eslint": "^8.57.0"
78
76
  },
79
77
  "devDependencies": {
80
- "bun-types": "^1.0.22",
81
- "husky": "^8.0.3",
82
- "lint-staged": "^15.2.0",
83
- "prettier": "^3.1.1"
78
+ "@elysiajs/eden": "^1.0.4",
79
+ "bun-types": "^1.0.33",
80
+ "husky": "^9.0.11",
81
+ "lint-staged": "^15.2.2",
82
+ "prettier": "^3.2.5"
84
83
  },
85
84
  "peerDependencies": {
86
85
  "typescript": "^5.0.0"
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import Elysia from 'elysia'
2
2
  import { createLogger } from './logger'
3
3
  import startString from './utils/start'
4
4
  import { Server } from 'bun'
5
+ import { Options } from './options'
5
6
 
6
7
  /**
7
8
  * Creates a logger.
@@ -18,7 +19,7 @@ import { Server } from 'bun'
18
19
  *
19
20
  * @returns {Elysia} The logger.
20
21
  */
21
- export const logger = (): Elysia => {
22
+ export const logger = (options?: Options): Elysia => {
22
23
  const log = createLogger()
23
24
 
24
25
  const elysia = new Elysia({
@@ -30,9 +31,28 @@ export const logger = (): Elysia => {
30
31
  .onRequest(ctx => {
31
32
  ctx.store = { beforeTime: process.hrtime.bigint() }
32
33
  })
33
- .onAfterHandle(({ request, store }) => {
34
+ .onBeforeHandle({ as: 'global' }, ({ request, store }) => {
34
35
  log.log('INFO', request, {}, store as { beforeTime: bigint })
35
36
  })
37
+ .onAfterHandle({ as: 'global' }, ({ request, store }) => {
38
+ const logStr: string[] = []
39
+
40
+ if (options?.ip) {
41
+ const forwardedFor = request.headers.get('x-forwarded-for')
42
+ if (forwardedFor) {
43
+ logStr.push(`IP: ${forwardedFor}`)
44
+ }
45
+ }
46
+
47
+ log.log(
48
+ 'INFO',
49
+ request,
50
+ {
51
+ message: logStr.join(' ')
52
+ },
53
+ store as { beforeTime: bigint }
54
+ )
55
+ })
36
56
  .onError(({ request, error, store }) => {
37
57
  log.log('ERROR', request, error, store as { beforeTime: bigint })
38
58
  })
package/src/options.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Options for the logger.
3
+ */
4
+ interface Options {
5
+ /**
6
+ * Whether to log the IP address of the client.
7
+ */
8
+ ip?: boolean
9
+ }
10
+
11
+ export { Options }
@@ -1,29 +1,42 @@
1
1
  import chalk from 'chalk'
2
2
 
3
3
  /**
4
- * Converts the time difference between the start of the request and the end of the request to a formatted string.
4
+ * Converts a time difference into a formatted string with the most appropriate time unit.
5
+ * Units used are seconds (s), milliseconds (ms), microseconds (µs), and nanoseconds (ns).
5
6
  *
6
- * @param {bigint} beforeTime The timestamp taken before the request.
7
+ * @param {bigint} beforeTime - The timestamp taken before the request.
7
8
  *
8
- * @returns {string} A formatted duration string with a time unit.
9
+ * @returns {string} A formatted duration string including the time unit.
9
10
  */
10
11
  function durationString(beforeTime: bigint): string {
11
- const now = process.hrtime.bigint()
12
- const nanoseconds = Number(now - beforeTime)
12
+ const nanoseconds = Number(process.hrtime.bigint() - beforeTime)
13
13
 
14
- let timeMessage: string = ''
14
+ const timeUnits = [
15
+ { unit: 's', threshold: 1e9 },
16
+ { unit: 'ms', threshold: 1e6 },
17
+ { unit: 'µs', threshold: 1e3 }
18
+ ]
15
19
 
16
- if (nanoseconds >= 1e9) {
17
- timeMessage = `${(nanoseconds / 1e9).toFixed(2)}s`
18
- } else if (nanoseconds >= 1e6) {
19
- timeMessage = `${(nanoseconds / 1e6).toFixed(0)}ms`
20
- } else if (nanoseconds >= 1e3) {
21
- timeMessage = `${(nanoseconds / 1e3).toFixed(0)}µs`
22
- } else {
23
- timeMessage = `${nanoseconds}ns`
20
+ for (const { unit, threshold } of timeUnits) {
21
+ if (nanoseconds >= threshold) {
22
+ const value = (nanoseconds / threshold).toFixed(threshold === 1e9 ? 2 : 0)
23
+ return formatTime(value, unit)
24
+ }
24
25
  }
25
26
 
26
- return timeMessage ? chalk.gray(timeMessage).padStart(8).padEnd(16) : ''
27
+ return formatTime(nanoseconds.toString(), 'ns')
28
+ }
29
+
30
+ /**
31
+ * Formats the time value with the given unit and applies chalk styling.
32
+ *
33
+ * @param {string} value - The time value.
34
+ * @param {string} unit - The time unit.
35
+ *
36
+ * @returns {string} Styled time string.
37
+ */
38
+ function formatTime(value: string, unit: string): string {
39
+ return chalk.gray(`${value}${unit}`).padStart(8).padEnd(16)
27
40
  }
28
41
 
29
42
  export default durationString
@@ -0,0 +1,53 @@
1
+ import { Elysia } from 'elysia'
2
+ import { edenTreaty } from '@elysiajs/eden'
3
+ import { describe, it, expect, beforeAll, beforeEach } from 'bun:test'
4
+ import { logger } from '../src'
5
+
6
+ describe('Logixlysia', () => {
7
+ let server: Elysia
8
+ let app: any
9
+ let logs: string[] = []
10
+
11
+ describe('IP logging disabled', () => {
12
+ beforeAll(() => {
13
+ server = new Elysia()
14
+ .use(logger({ ip: false }))
15
+ .get('/', () => '🦊 Logixlysia Getting')
16
+ .post('logixlysia', () => '🦊 Logixlysia Posting')
17
+ .listen(3000)
18
+
19
+ app = edenTreaty<typeof server>('http://127.0.0.1:3000')
20
+ })
21
+
22
+ beforeEach(() => {
23
+ logs = []
24
+ })
25
+
26
+ it("Responds correctly to GET '/' requests", async () => {
27
+ const requestCount = 5
28
+
29
+ for (let i = 0; i < requestCount; i++) {
30
+ logs.push((await app.get('/')).data)
31
+ }
32
+
33
+ logs.forEach(log => {
34
+ expect(log).toBe('🦊 Logixlysia Getting')
35
+ })
36
+ })
37
+
38
+ it("Responds correctly to POST '/logixlysia' requests", async () => {
39
+ const requestCount = 5
40
+
41
+ for (let i = 0; i < requestCount; i++) {
42
+ const postResponse = await app.logixlysia.post({})
43
+ logs.push(
44
+ postResponse.status === 200 ? postResponse.data : postResponse.error
45
+ )
46
+ }
47
+
48
+ logs.forEach(log => {
49
+ expect(log).toBe('🦊 Logixlysia Posting')
50
+ })
51
+ })
52
+ })
53
+ })
@@ -2,19 +2,35 @@ import { describe, expect, it } from 'bun:test'
2
2
  import durationString from '~/utils/duration'
3
3
 
4
4
  describe('Duration String', () => {
5
- it('Generates a string representing the duration in appropriate units', () => {
6
- const testCases = [
7
- [1e9, 's'],
8
- [1e6, 'ms'],
9
- [1e3, 'µs'],
10
- [1, 'ns']
5
+ const testCases = [
6
+ [
7
+ 'Generates a string representing the duration in Seconds (s) unit',
8
+ 1e9,
9
+ 's'
10
+ ],
11
+ [
12
+ 'Generates a string representing the duration in Milliseconds (ms) unit',
13
+ 1e6,
14
+ 'ms'
15
+ ],
16
+ [
17
+ 'Generates a string representing the duration in Microseconds (µs) unit',
18
+ 1e3,
19
+ 'µs'
20
+ ],
21
+ [
22
+ 'Generates a string representing the duration in Nanoseconds (ns) unit',
23
+ 1,
24
+ 'ns'
11
25
  ]
26
+ ]
12
27
 
13
- for (const [nanoseconds, unit] of testCases) {
28
+ for (const [description, nanoseconds, unit] of testCases) {
29
+ it(`${description}`, () => {
14
30
  const beforeTime = process.hrtime.bigint() - BigInt(String(nanoseconds))
15
-
16
31
  const result = durationString(beforeTime)
32
+
17
33
  expect(result).toMatch(String(unit))
18
- }
19
- })
34
+ })
35
+ }
20
36
  })