@20syldev/api 4.3.0 → 4.5.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.
Files changed (88) hide show
  1. package/README.md +3 -3
  2. package/dist/config/env.js +5 -1
  3. package/dist/config/env.js.map +1 -1
  4. package/dist/config/plans.js +44 -0
  5. package/dist/config/plans.js.map +1 -0
  6. package/dist/config/versions.js +10 -0
  7. package/dist/config/versions.js.map +1 -1
  8. package/dist/constants.js +1 -2
  9. package/dist/constants.js.map +1 -1
  10. package/dist/middleware/cors.js +7 -2
  11. package/dist/middleware/cors.js.map +1 -1
  12. package/dist/middleware/error.js +3 -1
  13. package/dist/middleware/error.js.map +1 -1
  14. package/dist/middleware/ratelimit.js +39 -21
  15. package/dist/middleware/ratelimit.js.map +1 -1
  16. package/dist/modules/v4/captcha.js +60 -23
  17. package/dist/modules/v4/captcha.js.map +1 -1
  18. package/dist/modules/v4/chat.js +1 -1
  19. package/dist/modules/v4/chat.js.map +1 -1
  20. package/dist/modules/v4/color.js +44 -35
  21. package/dist/modules/v4/color.js.map +1 -1
  22. package/dist/modules/v4/colors.js +54 -0
  23. package/dist/modules/v4/colors.js.map +1 -0
  24. package/dist/modules/v4/convert.js +93 -19
  25. package/dist/modules/v4/convert.js.map +1 -1
  26. package/dist/modules/v4/domain.js +1 -1
  27. package/dist/modules/v4/domain.js.map +1 -1
  28. package/dist/modules/v4/hash.js +11 -4
  29. package/dist/modules/v4/hash.js.map +1 -1
  30. package/dist/modules/v4/hyperplanning.js +32 -2
  31. package/dist/modules/v4/hyperplanning.js.map +1 -1
  32. package/dist/modules/v4/palette.js +2 -43
  33. package/dist/modules/v4/palette.js.map +1 -1
  34. package/dist/modules/v4/personal.js +1 -1
  35. package/dist/modules/v4/personal.js.map +1 -1
  36. package/dist/modules/v4/placeholder.js +5 -10
  37. package/dist/modules/v4/placeholder.js.map +1 -1
  38. package/dist/modules/v4/qrcode.js +79 -6
  39. package/dist/modules/v4/qrcode.js.map +1 -1
  40. package/dist/modules/v4/username.js +1 -1
  41. package/dist/modules/v4/username.js.map +1 -1
  42. package/dist/routes/delete.js +14 -14
  43. package/dist/routes/delete.js.map +1 -1
  44. package/dist/routes/get.js +73 -17
  45. package/dist/routes/get.js.map +1 -1
  46. package/dist/routes/post.js +11 -3
  47. package/dist/routes/post.js.map +1 -1
  48. package/dist/utils/colors.js +93 -0
  49. package/dist/utils/colors.js.map +1 -0
  50. package/dist/utils/helpers.js +77 -0
  51. package/dist/utils/helpers.js.map +1 -0
  52. package/dist/utils/response.js +18 -2
  53. package/dist/utils/response.js.map +1 -1
  54. package/package.json +1 -1
  55. package/src/config/env.ts +6 -1
  56. package/src/config/plans.ts +61 -0
  57. package/src/config/versions.ts +10 -0
  58. package/src/constants.ts +1 -2
  59. package/src/middleware/cors.ts +8 -2
  60. package/src/middleware/error.ts +10 -2
  61. package/src/middleware/ratelimit.ts +42 -20
  62. package/src/modules/v4/captcha.ts +84 -27
  63. package/src/modules/v4/chat.ts +1 -1
  64. package/src/modules/v4/color.ts +56 -52
  65. package/src/modules/v4/convert.ts +100 -20
  66. package/src/modules/v4/domain.ts +1 -1
  67. package/src/modules/v4/hash.ts +18 -4
  68. package/src/modules/v4/hyperplanning.ts +28 -2
  69. package/src/modules/v4/palette.ts +3 -43
  70. package/src/modules/v4/personal.ts +1 -1
  71. package/src/modules/v4/placeholder.ts +6 -9
  72. package/src/modules/v4/qrcode.ts +108 -6
  73. package/src/modules/v4/username.ts +1 -1
  74. package/src/routes/get.ts +75 -19
  75. package/src/routes/post.ts +11 -3
  76. package/src/utils/colors.ts +91 -0
  77. package/src/utils/helpers.ts +90 -0
  78. package/src/utils/response.ts +18 -2
  79. package/tests/integration/api.test.ts +89 -7
  80. package/tests/unit/captcha.test.ts +50 -12
  81. package/tests/unit/chat.test.ts +4 -1
  82. package/tests/unit/color.test.ts +24 -4
  83. package/tests/unit/convert.test.ts +26 -1
  84. package/tests/unit/hash.test.ts +17 -10
  85. package/tests/unit/hyperplanning.test.ts +19 -0
  86. package/tests/unit/qrcode.test.ts +72 -4
  87. package/tests/unit/tic_tac_toe.test.ts +4 -1
  88. package/src/modules/v4/utils.ts +0 -38
@@ -2,13 +2,81 @@ import { test, describe } from 'node:test';
2
2
  import { strict as assert } from 'node:assert';
3
3
  import qrcode from '../../src/modules/v4/qrcode.js';
4
4
 
5
+ const PNG_MAGIC = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
6
+
5
7
  describe('qrcode', () => {
6
- test('returns data URL for valid url', async () => {
7
- const result = await qrcode('https://example.com');
8
- assert.match(result, /^data:image\/png;base64,/);
8
+ test('returns PNG buffer by default', async () => {
9
+ const result = await qrcode({ url: 'https://example.com' });
10
+ assert.equal(result.contentType, 'image/png');
11
+ assert.ok(Buffer.isBuffer(result.body));
12
+ assert.ok((result.body as Buffer).subarray(0, 4).equals(PNG_MAGIC));
13
+ });
14
+
15
+ test('returns data URL with format=base64', async () => {
16
+ const result = await qrcode({ url: 'https://example.com', format: 'base64' });
17
+ assert.equal(result.contentType, 'application/json');
18
+ assert.match(result.body as string, /^data:image\/png;base64,/);
19
+ });
20
+
21
+ test('throws on invalid format', async () => {
22
+ await assert.rejects(() => qrcode({ url: 'https://example.com', format: 'jpg' as 'png' }), /format/);
9
23
  });
10
24
 
11
25
  test('throws on missing url', async () => {
12
- await assert.rejects(() => qrcode(''));
26
+ await assert.rejects(() => qrcode({ url: '' }));
27
+ });
28
+
29
+ test('accepts custom size', async () => {
30
+ const result = await qrcode({ url: 'https://example.com', size: 400 });
31
+ assert.equal(result.contentType, 'image/png');
32
+ });
33
+
34
+ test('throws on invalid size', async () => {
35
+ await assert.rejects(() => qrcode({ url: 'https://example.com', size: 9999 }), /size/);
36
+ });
37
+
38
+ test('accepts custom colors', async () => {
39
+ const result = await qrcode({ url: 'https://example.com', dark: 'ff0000', light: 'ffffff' });
40
+ assert.equal(result.contentType, 'image/png');
41
+ });
42
+
43
+ test('throws on invalid dark color', async () => {
44
+ await assert.rejects(() => qrcode({ url: 'https://example.com', dark: 'xyz' }), /color/i);
45
+ });
46
+
47
+ test('throws on invalid light color', async () => {
48
+ await assert.rejects(() => qrcode({ url: 'https://example.com', light: 'nope' }), /color/i);
49
+ });
50
+
51
+ test('accepts correction level', async () => {
52
+ const result = await qrcode({ url: 'https://example.com', correction: 'H' });
53
+ assert.equal(result.contentType, 'image/png');
54
+ });
55
+
56
+ test('throws on invalid correction level', async () => {
57
+ await assert.rejects(() => qrcode({ url: 'https://example.com', correction: 'X' as 'L' }), /correction/);
58
+ });
59
+
60
+ test('accepts custom margin', async () => {
61
+ const result = await qrcode({ url: 'https://example.com', margin: 0 });
62
+ assert.equal(result.contentType, 'image/png');
63
+ });
64
+
65
+ test('throws on invalid margin', async () => {
66
+ await assert.rejects(() => qrcode({ url: 'https://example.com', margin: 99 }), /margin/);
67
+ });
68
+
69
+ test('throws on non-HTTPS icon URL', async () => {
70
+ await assert.rejects(
71
+ () => qrcode({ url: 'https://example.com', icon: 'http://example.com/icon.png' }),
72
+ /HTTPS/,
73
+ );
74
+ });
75
+
76
+ test('throws on unreachable icon URL', async () => {
77
+ await assert.rejects(
78
+ () => qrcode({ url: 'https://example.com', icon: 'https://localhost:1/nope.png' }),
79
+ /Failed to fetch icon|fetch/i,
80
+ );
13
81
  });
14
82
  });
@@ -36,7 +36,10 @@ describe('tic_tac_toe', () => {
36
36
  });
37
37
 
38
38
  test('throws on missing username', () => {
39
- assert.throws(() => tic_tac_toe('play', { move: '1-1', session: 's', game: 'G', storage: makeStorage() }), /username/);
39
+ assert.throws(
40
+ () => tic_tac_toe('play', { move: '1-1', session: 's', game: 'G', storage: makeStorage() }),
41
+ /username/,
42
+ );
40
43
  });
41
44
 
42
45
  test('throws on missing move', () => {
@@ -1,38 +0,0 @@
1
- export function random<T>(arr: T[]): T {
2
- return arr[Math.floor(Math.random() * arr.length)]!;
3
- }
4
-
5
- export function randomNumber(min: number, max: number): number {
6
- return Math.floor(Math.random() * (max - min + 1)) + min;
7
- }
8
-
9
- export function genIP(): string {
10
- return `${randomNumber(0, 255)}.${randomNumber(0, 255)}.${randomNumber(0, 255)}.${randomNumber(0, 255)}`;
11
- }
12
-
13
- export function formatDate(date: Date): string {
14
- return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().replace('Z', '');
15
- }
16
-
17
- export function envList(key: string): string[] | null {
18
- const v = process.env[key];
19
- return v && v !== 'undefined' ? v.split(' ') : null;
20
- }
21
-
22
- export function checkRateLimit(
23
- rateLimits: Record<string, number[]>,
24
- userId: string,
25
- timestamp: number,
26
- window = 10000,
27
- limit = 50,
28
- ): boolean {
29
- rateLimits[userId] = (rateLimits[userId] ?? []).filter((ts) => timestamp - ts < window);
30
-
31
- if (rateLimits[userId]!.length > limit) {
32
- const remainingTime = Math.ceil((rateLimits[userId]![0]! + window - timestamp) / 1000);
33
- throw new Error(`Rate limit exceeded. Try again in ${remainingTime} seconds.`);
34
- }
35
-
36
- rateLimits[userId]!.push(timestamp);
37
- return false;
38
- }