@famir/http-tools 0.0.2 → 0.0.4

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 (61) hide show
  1. package/README.md +1 -2
  2. package/dist/body.d.ts +6 -6
  3. package/dist/body.js +4 -4
  4. package/dist/body.js.map +1 -1
  5. package/dist/body.test.d.ts +1 -0
  6. package/dist/body.test.js +175 -0
  7. package/dist/body.test.js.map +1 -0
  8. package/dist/cheerio.d.ts +3 -4
  9. package/dist/cheerio.js +1 -4
  10. package/dist/cheerio.js.map +1 -1
  11. package/dist/content-type.d.ts +2 -6
  12. package/dist/content-type.js +3 -8
  13. package/dist/content-type.js.map +1 -1
  14. package/dist/cookies.d.ts +1 -13
  15. package/dist/cookies.js +3 -3
  16. package/dist/cookies.js.map +1 -1
  17. package/dist/cookies.test.d.ts +1 -0
  18. package/dist/cookies.test.js +365 -0
  19. package/dist/cookies.test.js.map +1 -0
  20. package/dist/headers.d.ts +5 -6
  21. package/dist/headers.js +5 -2
  22. package/dist/headers.js.map +1 -1
  23. package/dist/headers.test.d.ts +1 -0
  24. package/dist/headers.test.js +314 -0
  25. package/dist/headers.test.js.map +1 -0
  26. package/dist/isbot.js.map +1 -1
  27. package/dist/message.d.ts +11 -19
  28. package/dist/message.js +39 -37
  29. package/dist/message.js.map +1 -1
  30. package/dist/message.test.d.ts +1 -0
  31. package/dist/message.test.js +138 -0
  32. package/dist/message.test.js.map +1 -0
  33. package/dist/method.d.ts +2 -2
  34. package/dist/method.js +2 -1
  35. package/dist/method.js.map +1 -1
  36. package/dist/method.test.d.ts +1 -0
  37. package/dist/method.test.js +89 -0
  38. package/dist/method.test.js.map +1 -0
  39. package/dist/query-string.d.ts +4 -6
  40. package/dist/query-string.js +2 -7
  41. package/dist/query-string.js.map +1 -1
  42. package/dist/rewrite-url.js.map +1 -1
  43. package/dist/rewrite-url.test.d.ts +1 -0
  44. package/dist/rewrite-url.test.js +322 -0
  45. package/dist/rewrite-url.test.js.map +1 -0
  46. package/dist/status.d.ts +2 -2
  47. package/dist/status.js.map +1 -1
  48. package/dist/status.test.d.ts +1 -0
  49. package/dist/status.test.js +106 -0
  50. package/dist/status.test.js.map +1 -0
  51. package/dist/transforms.d.ts +3 -3
  52. package/dist/transforms.js.map +1 -1
  53. package/dist/ua-parser.d.ts +2 -1
  54. package/dist/ua-parser.js.map +1 -1
  55. package/dist/url.d.ts +7 -7
  56. package/dist/url.js +7 -7
  57. package/dist/url.js.map +1 -1
  58. package/dist/url.test.d.ts +1 -0
  59. package/dist/url.test.js +201 -0
  60. package/dist/url.test.js.map +1 -0
  61. package/package.json +16 -11
@@ -0,0 +1,138 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { HttpMessage } from './message.js';
4
+ describe('HttpMessage', () => {
5
+ describe('create', () => {
6
+ it('should create normal message', () => {
7
+ const msg = HttpMessage.create('normal');
8
+ assert.strictEqual(msg.type, 'normal-simple');
9
+ });
10
+ it('should create websocket message', () => {
11
+ const msg = HttpMessage.create('websocket');
12
+ assert.strictEqual(msg.type, 'websocket');
13
+ });
14
+ it('should throw on unknown type', () => {
15
+ assert.throws(() => HttpMessage.create('unknown'), /not known/);
16
+ });
17
+ });
18
+ describe('id', () => {
19
+ it('should have unique id', () => {
20
+ const msg1 = HttpMessage.create('normal');
21
+ const msg2 = HttpMessage.create('normal');
22
+ assert.notStrictEqual(msg1.id, msg2.id);
23
+ });
24
+ });
25
+ describe('ready', () => {
26
+ it('should not be ready initially', () => {
27
+ const msg = HttpMessage.create('normal');
28
+ assert.strictEqual(msg.isReady, false);
29
+ });
30
+ it('should mark as ready', () => {
31
+ const msg = HttpMessage.create('normal');
32
+ msg.ready();
33
+ assert.strictEqual(msg.isReady, true);
34
+ });
35
+ it('should prevent type change when ready', () => {
36
+ const msg = HttpMessage.create('normal');
37
+ msg.ready();
38
+ assert.throws(() => msg.setType('normal-stream-request'), /ready/);
39
+ });
40
+ });
41
+ describe('content-types', () => {
42
+ it('should add content types', () => {
43
+ const msg = HttpMessage.create('normal');
44
+ msg.addContentTypes('json', ['application/json']);
45
+ assert.strictEqual(msg.isContentType('json', { type: 'application/json', parameters: {} }), true);
46
+ });
47
+ it('should not allow add when ready', () => {
48
+ const msg = HttpMessage.create('normal');
49
+ msg.ready();
50
+ assert.throws(() => msg.addContentTypes('json', []), /ready/);
51
+ });
52
+ });
53
+ describe('rewrite url content types', () => {
54
+ it('should add rewrite url content types', () => {
55
+ const msg = HttpMessage.create('normal');
56
+ msg.addRewriteUrlContentTypes(['text/html']);
57
+ assert.strictEqual(msg.isRewriteUrlContentType({ type: 'text/html', parameters: {} }), true);
58
+ });
59
+ });
60
+ describe('connection metadata', () => {
61
+ it('should merge connection data', () => {
62
+ const msg = HttpMessage.create('normal');
63
+ msg.mergeConnection({ clientIp: '127.0.0.1' });
64
+ assert.strictEqual(msg.connection['clientIp'], '127.0.0.1');
65
+ });
66
+ it('should skip null values in merge', () => {
67
+ const msg = HttpMessage.create('normal');
68
+ msg.mergeConnection({ clientIp: '127.0.0.1', serverIp: null });
69
+ assert.strictEqual(msg.connection['clientIp'], '127.0.0.1');
70
+ assert.strictEqual(msg.connection['serverIp'], undefined);
71
+ });
72
+ });
73
+ describe('errors', () => {
74
+ it('should add error with path', () => {
75
+ const msg = HttpMessage.create('normal');
76
+ const err = new Error('test error');
77
+ msg.addError(err, 'request', 'parser');
78
+ assert.strictEqual(msg.errors.length, 1);
79
+ });
80
+ it('should track multiple errors', () => {
81
+ const msg = HttpMessage.create('normal');
82
+ msg.addError(new Error('error1'), 'req');
83
+ msg.addError(new Error('error2'), 'res');
84
+ assert.strictEqual(msg.errors.length, 2);
85
+ });
86
+ });
87
+ describe('interceptors', () => {
88
+ it('should add request head interceptor', () => {
89
+ const msg = HttpMessage.create('normal');
90
+ msg.addRequestHeadInterceptor('test', () => { });
91
+ assert.ok(msg);
92
+ });
93
+ it('should add request body interceptor', () => {
94
+ const msg = HttpMessage.create('normal');
95
+ msg.addRequestBodyInterceptor('test', () => { });
96
+ assert.ok(msg);
97
+ });
98
+ it('should run request head interceptors', () => {
99
+ const msg = HttpMessage.create('normal');
100
+ let called = false;
101
+ msg.addRequestHeadInterceptor('test', () => {
102
+ called = true;
103
+ });
104
+ msg.ready();
105
+ msg.runRequestHeadInterceptors();
106
+ assert.strictEqual(called, true);
107
+ });
108
+ it('should catch interceptor errors', () => {
109
+ const msg = HttpMessage.create('normal');
110
+ msg.addRequestHeadInterceptor('test', () => {
111
+ throw new Error('interceptor error');
112
+ });
113
+ msg.ready();
114
+ msg.runRequestHeadInterceptors();
115
+ assert.strictEqual(msg.errors.length, 1);
116
+ });
117
+ });
118
+ describe('url utilities', () => {
119
+ it('should check absolute urls', () => {
120
+ const msg = HttpMessage.create('normal');
121
+ assert.strictEqual(msg.isAbsoluteUrl('http://example.com'), true);
122
+ assert.strictEqual(msg.isAbsoluteUrl('https://example.com'), true);
123
+ assert.strictEqual(msg.isAbsoluteUrl('//example.com'), true);
124
+ assert.strictEqual(msg.isAbsoluteUrl('/path'), false);
125
+ });
126
+ });
127
+ describe('transforms', () => {
128
+ it('should add request transform', () => {
129
+ const msg = HttpMessage.create('normal');
130
+ assert.strictEqual(msg.getRequestTransforms().length, 0);
131
+ });
132
+ it('should add response transform', () => {
133
+ const msg = HttpMessage.create('normal');
134
+ assert.strictEqual(msg.getResponseTransforms().length, 0);
135
+ });
136
+ });
137
+ });
138
+ //# sourceMappingURL=message.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.test.js","sourceRoot":"","sources":["../src/message.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE1C,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;Q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CAAC,aAAa,CAAC,oBAAoB,CAAC,EAAE,IAAI,CAAC,CAAA;YACjE,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,aAAa,CAAC,qBAAqB,CAAC,EAAE,IAAI,CAAC,CAAA;YAClE,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,aAAa,CAAC,eAAe,CAAC,EAAE,IAAI,CAAC,CAAA;YAC5D,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,KAAK,CAAC,CAAA;QACvD,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;QAC1B,EAAE,CAAC,8BAA8B,EAAE,GAAG,EAAE;YACtC,MAAM,GAAG,GAAG,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;YACxC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,oBAAoB,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;QAC1D,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,+BAA+B,EAAE,GAAG,EAAE;YACvC,MAAM,GAAG,GAAG,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;YACxC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,qBAAqB,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;QAC3D,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
package/dist/method.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { HttpMethod } from '@famir/common';
1
+ import { HttpMethod } from '@famir/http-proto';
2
2
  export declare class HttpMethodWrap {
3
3
  #private;
4
4
  static fromScratch(): HttpMethodWrap;
@@ -13,5 +13,5 @@ export declare class HttpMethodWrap {
13
13
  get(): HttpMethod;
14
14
  set(method: HttpMethod): this;
15
15
  is(arg: HttpMethod | HttpMethod[]): boolean;
16
- protected sureNotFrozen(name: string): void;
16
+ private sureNotFrozen;
17
17
  }
package/dist/method.js CHANGED
@@ -1,4 +1,5 @@
1
- import { arrayIncludes, HTTP_METHODS } from '@famir/common';
1
+ import { arrayIncludes } from '@famir/common';
2
+ import { HTTP_METHODS } from '@famir/http-proto';
2
3
  export class HttpMethodWrap {
3
4
  static fromScratch() {
4
5
  return new HttpMethodWrap('GET');
@@ -1 +1 @@
1
- {"version":3,"file":"method.js","sourceRoot":"","sources":["../src/method.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAc,MAAM,eAAe,CAAA;AAKvE,MAAM,OAAO,cAAc;IAIzB,MAAM,CAAC,WAAW;QAChB,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,CAAA;IAClC,CAAC;IAKD,MAAM,CAAC,OAAO,CAAC,GAAoC;QACjD,IAAI,GAAG,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;QACvC,CAAC;QAED,OAAO,cAAc,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IAC9C,CAAC;IAKD,MAAM,CAAC,UAAU,CAAC,KAAa;QAC7B,MAAM,SAAS,GAAG,KAAK,CAAC,WAAW,EAAE,CAAA;QAErC,IAAI,CAAC,aAAa,CAAC,YAAY,EAAE,SAAS,CAAC,EAAE,CAAC;YAC5C,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;QACrC,CAAC;QAED,OAAO,IAAI,cAAc,CAAC,SAAS,CAAC,CAAA;IACtC,CAAC;IAED,OAAO,CAAY;IAEnB,YAAY,MAAkB;QAC5B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;IACvB,CAAC;IAKD,KAAK;QACH,OAAO,IAAI,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACzC,CAAC;IAED,SAAS,GAAY,KAAK,CAAA;IAK1B,IAAI,QAAQ;QACV,OAAO,IAAI,CAA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@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,89 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { HttpMethodWrap } from './method.js';
4
+ describe('HttpMethodWrap', () => {
5
+ describe('fromScratch', () => {
6
+ it('should create wrapper from scratch', () => {
7
+ const method = HttpMethodWrap.fromScratch();
8
+ assert.strictEqual(method.get(), 'GET');
9
+ });
10
+ });
11
+ describe('fromReq', () => {
12
+ it('should create wrapper from request object', () => {
13
+ const req = { method: 'PosT' };
14
+ const method = HttpMethodWrap.fromReq(req);
15
+ assert.strictEqual(method.get(), 'POST');
16
+ });
17
+ it('should throw if request method is not defined', () => {
18
+ const req = { method: undefined };
19
+ assert.throws(() => HttpMethodWrap.fromReq(req), /not defined/);
20
+ });
21
+ });
22
+ describe('fromString', () => {
23
+ it('should create wrapper from string', () => {
24
+ const method = HttpMethodWrap.fromString('PuT');
25
+ assert.strictEqual(method.get(), 'PUT');
26
+ });
27
+ it('should throw on unknown method', () => {
28
+ assert.throws(() => HttpMethodWrap.fromString('INVALID'), /not known/);
29
+ });
30
+ });
31
+ describe('clone', () => {
32
+ it('should clone wrapper', () => {
33
+ const original = new HttpMethodWrap('POST');
34
+ const cloned = original.clone();
35
+ assert.deepStrictEqual(cloned, original);
36
+ assert.notStrictEqual(cloned, original);
37
+ });
38
+ it('should clone has independent state', () => {
39
+ const original = new HttpMethodWrap('HEAD');
40
+ const cloned = original.clone();
41
+ assert.strictEqual(cloned.get(), 'HEAD');
42
+ cloned.set('GET');
43
+ assert.strictEqual(original.get(), 'HEAD');
44
+ assert.strictEqual(cloned.get(), 'GET');
45
+ });
46
+ });
47
+ describe('freeze', () => {
48
+ it('should freeze and prevent modifications', () => {
49
+ const method = HttpMethodWrap.fromScratch();
50
+ method.freeze();
51
+ assert.strictEqual(method.isFrozen, true);
52
+ assert.throws(() => method.set('POST'), /frozen/);
53
+ });
54
+ it('should return this for chaining', () => {
55
+ const method = HttpMethodWrap.fromScratch();
56
+ const that = method.freeze();
57
+ assert.strictEqual(that, method);
58
+ });
59
+ });
60
+ describe('get/set', () => {
61
+ it('should get method value', () => {
62
+ const method = new HttpMethodWrap('PATCH');
63
+ assert.strictEqual(method.get(), 'PATCH');
64
+ });
65
+ it('should set and get method value', () => {
66
+ const method = HttpMethodWrap.fromScratch();
67
+ method.set('DELETE');
68
+ assert.strictEqual(method.get(), 'DELETE');
69
+ });
70
+ it('should return this for chaining', () => {
71
+ const method = HttpMethodWrap.fromScratch();
72
+ const that = method.set('OPTIONS');
73
+ assert.strictEqual(that, method);
74
+ });
75
+ });
76
+ describe('is check', () => {
77
+ it('should check single method', () => {
78
+ const method = new HttpMethodWrap('GET');
79
+ assert.strictEqual(method.is('GET'), true);
80
+ assert.strictEqual(method.is('POST'), false);
81
+ });
82
+ it('should check multiple methods', () => {
83
+ const method = new HttpMethodWrap('HEAD');
84
+ assert.strictEqual(method.is(['GET', 'HEAD']), true);
85
+ assert.strictEqual(method.is(['PATCH', 'PUT']), false);
86
+ });
87
+ });
88
+ });
89
+ //# sourceMappingURL=method.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"method.test.js","sourceRoot":"","sources":["../src/method.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE5C,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC9B,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;QAC3B,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;YAC5C,MAAM,MAAM,GAAG,cAAc,CAAC,WAAW,EAAE,CAAA;YAC3C,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,EAAE,EAAE,KAAK,CAAC,CAAA;QACzC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,QAAQ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EAAE,GAAG,EAAE;YACvC,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAA;YACzC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,CAAA;YACpD,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;QACxD,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -1,6 +1,4 @@
1
- import qs from 'qs';
2
- export type HttpQueryString = Record<string, unknown>;
3
- export type HttpParseQueryStringOptions = qs.IParseBaseOptions;
4
- export type HttpFormatQueryStringOptions = qs.IStringifyBaseOptions;
5
- export declare function parseQueryString(value: string, options: HttpParseQueryStringOptions): HttpQueryString;
6
- export declare function formatQueryString(queryString: HttpQueryString, options: HttpFormatQueryStringOptions): string;
1
+ export { IParseBaseOptions as ParseQueryStringOptions } from 'qs';
2
+ export { IStringifyBaseOptions as FormatQueryStringOptions } from 'qs';
3
+ export { parse as parseQueryString } from 'qs';
4
+ export { stringify as formatQueryString } from 'qs';
@@ -1,8 +1,3 @@
1
- import qs from 'qs';
2
- export function parseQueryString(value, options) {
3
- return qs.parse(value.trim(), options);
4
- }
5
- export function formatQueryString(queryString, options) {
6
- return qs.stringify(queryString, options);
7
- }
1
+ export { parse as parseQueryString } from 'qs';
2
+ export { stringify as formatQueryString } from 'qs';
8
3
  //# sourceMappingURL=query-string.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"query-string.js","sourceRoot":"","sources":["../src/query-string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AASnB,MAAM,UAAU,gBAAgB,CAC9B,KAAa,EACb,OAAoC;IAEpC,OAAO,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,OAAO,CAAC,CAAA;AACxC,CAAC;AAKD,MAAM,UAAU,iBAAiB,CAC/B,WAA4B,EAC5B,OAAqC;IAErC,OAAO,EAAE,CAAC,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;AAC3C,CAAC"}
1
+ {"version":3,"file":"query-string.js","sourceRoot":"","sources":["../src/query-string.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,KAAK,IAAI,gBAAgB,EAAE,MAAM,IAAI,CAAA;AAM9C,OAAO,EAAE,SAAS,IAAI,iBAAiB,EAAE,MAAM,IAAI,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"rewrite-url.js","sourceRoot":"","sources":["../src/rewrite-url.ts"],"names":[],"mappings":"AAYA,MAAM,UAAU,UAAU,CACxB,IAAY,EACZ,GAAY,EACZ,OAA2B,EAC3B,OAA2B;IAE3B,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;QACzB,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,GAAG,GAAG;YAChC,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,WAAW,CAAC;YAC3C,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,YAAY,CAAC,CAAA;QAE7C,MAAM,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAA;QAC7C,MAAM,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAA;QAE7C,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,GAAG;YAC5B,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,SAAS,CAAC;YACvC,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;QAEzC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EAAE,SAAS,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AAG;YAC5B,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,SAAS,CAAC;YACvC,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;QAEzC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EAAE,SAAS,CAAC,EAAE,EAAE;YACzC,MAAM,OAAO,GAAG,SAAS;gBACvB,CAAC,CAAC,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;gBACzC,CAAC,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YAEjC,MAAM,WAAW,GAAG,SAAS;gBAC3B,CAAC,CAAC,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;gBACzC,CAAC,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YAEjC,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;QAC9C,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,OAAO,IAAI,CAAA;AACb,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,322 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { rewriteUrl } from './rewrite-url.js';
4
+ describe('rewriteUrl', () => {
5
+ describe('basic rewriting (forward)', () => {
6
+ it('should rewrite with protocol and separator', () => {
7
+ const text = 'Check http://donor.com/path and https://donor.com/secure';
8
+ const target = {
9
+ donorSecure: false,
10
+ donorHost: 'donor.com',
11
+ mirrorSecure: true,
12
+ mirrorHost: 'mirror.com',
13
+ };
14
+ const schemes = [['://', true]];
15
+ const result = rewriteUrl(text, false, [target], schemes);
16
+ assert.strictEqual(result.includes('https://mirror.com/path'), true);
17
+ });
18
+ it.skip('should rewrite without protocol', () => {
19
+ const text = 'Urls: //donor.com/path and //donor.com/other';
20
+ const target = {
21
+ donorSecure: false,
22
+ donorHost: 'donor.com',
23
+ mirrorSecure: false,
24
+ mirrorHost: 'mirror.com',
25
+ };
26
+ const schemes = [['://', false]];
27
+ const result = rewriteUrl(text, false, [target], schemes);
28
+ assert.strictEqual(result.includes('//mirror.com/path'), true);
29
+ });
30
+ it('should preserve URLs not matching target', () => {
31
+ const text = 'http://other.com/path and http://donor.com/path';
32
+ const target = {
33
+ donorSecure: false,
34
+ donorHost: 'donor.com',
35
+ mirrorSecure: false,
36
+ mirrorHost: 'mirror.com',
37
+ };
38
+ const schemes = [['://', true]];
39
+ const result = rewriteUrl(text, false, [target], schemes);
40
+ assert.strictEqual(result.includes('http://other.com/path'), true);
41
+ assert.strictEqual(result.includes('http://mirror.com/path'), true);
42
+ });
43
+ });
44
+ describe('reverse rewriting', () => {
45
+ it('should reverse rewrite mirror to donor', () => {
46
+ const text = 'Response from https://mirror.com/api';
47
+ const target = {
48
+ donorSecure: false,
49
+ donorHost: 'donor.com',
50
+ mirrorSecure: true,
51
+ mirrorHost: 'mirror.com',
52
+ };
53
+ const schemes = [['://', true]];
54
+ const result = rewriteUrl(text, true, [target], schemes);
55
+ assert.strictEqual(result.includes('http://donor.com/api'), true);
56
+ });
57
+ it('should reverse with protocol changes', () => {
58
+ const text = 'Content has https://mirror.example.org/resource';
59
+ const target = {
60
+ donorSecure: true,
61
+ donorHost: 'original.com',
62
+ mirrorSecure: true,
63
+ mirrorHost: 'mirror.example.org',
64
+ };
65
+ const schemes = [['://', true]];
66
+ const result = rewriteUrl(text, true, [target], schemes);
67
+ assert.strictEqual(result.includes('https://original.com/resource'), true);
68
+ });
69
+ });
70
+ describe('multiple schemes', () => {
71
+ it('should rewrite with multiple separators', () => {
72
+ const text = 'http://donor.com and //donor.com and data:url';
73
+ const target = {
74
+ donorSecure: false,
75
+ donorHost: 'donor.com',
76
+ mirrorSecure: false,
77
+ mirrorHost: 'mirror.com',
78
+ };
79
+ const schemes = [
80
+ ['://', true],
81
+ ['//', false],
82
+ ];
83
+ const result = rewriteUrl(text, false, [target], schemes);
84
+ assert.strictEqual(result.includes('http://mirror.com'), true);
85
+ assert.strictEqual(result.includes('//mirror.com'), true);
86
+ });
87
+ it('should handle encoded separators', () => {
88
+ const text = 'Data: %3A%2F%2Fdonor.com and %2F%2Fdonor.com';
89
+ const target = {
90
+ donorSecure: false,
91
+ donorHost: 'donor.com',
92
+ mirrorSecure: false,
93
+ mirrorHost: 'mirror.com',
94
+ };
95
+ const schemes = [
96
+ ['%3A%2F%2F', true],
97
+ ['%2F%2F', false],
98
+ ];
99
+ const result = rewriteUrl(text, false, [target], schemes);
100
+ assert.strictEqual(result.includes('%3A%2F%2Fmirror.com'), true);
101
+ assert.strictEqual(result.includes('%2F%2Fmirror.com'), true);
102
+ });
103
+ it.skip('should handle unicode escaped separators', () => {
104
+ const text = 'Escaped: \\u003A\\u002F\\u002Fdonor.com';
105
+ const target = {
106
+ donorSecure: false,
107
+ donorHost: 'donor.com',
108
+ mirrorSecure: false,
109
+ mirrorHost: 'mirror.com',
110
+ };
111
+ const schemes = [['\\u003A\\u002F\\u002F', true]];
112
+ const result = rewriteUrl(text, false, [target], schemes);
113
+ assert.strictEqual(result.includes('\\u003A\\u002F\\u002Fmirror.com'), true);
114
+ });
115
+ });
116
+ describe('multiple targets', () => {
117
+ it('should rewrite multiple targets sequentially', () => {
118
+ const text = 'http://donor1.com/path1 and http://donor2.com/path2';
119
+ const targets = [
120
+ {
121
+ donorSecure: false,
122
+ donorHost: 'donor1.com',
123
+ mirrorSecure: false,
124
+ mirrorHost: 'mirror1.com',
125
+ },
126
+ {
127
+ donorSecure: false,
128
+ donorHost: 'donor2.com',
129
+ mirrorSecure: false,
130
+ mirrorHost: 'mirror2.com',
131
+ },
132
+ ];
133
+ const schemes = [['://', true]];
134
+ const result = rewriteUrl(text, false, targets, schemes);
135
+ assert.strictEqual(result.includes('http://mirror1.com/path1'), true);
136
+ assert.strictEqual(result.includes('http://mirror2.com/path2'), true);
137
+ });
138
+ it('should handle overlapping targets independently', () => {
139
+ const text = 'http://sub.donor.com/api';
140
+ const targets = [
141
+ {
142
+ donorSecure: false,
143
+ donorHost: 'donor.com',
144
+ mirrorSecure: false,
145
+ mirrorHost: 'mirror.com',
146
+ },
147
+ {
148
+ donorSecure: false,
149
+ donorHost: 'sub.donor.com',
150
+ mirrorSecure: false,
151
+ mirrorHost: 'sub.mirror.com',
152
+ },
153
+ ];
154
+ const schemes = [['://', true]];
155
+ const result = rewriteUrl(text, false, targets, schemes);
156
+ assert.strictEqual(result.includes('mirror'), true);
157
+ });
158
+ });
159
+ describe('protocol switching', () => {
160
+ it('should switch from http to https', () => {
161
+ const text = 'http://donor.com/secure';
162
+ const target = {
163
+ donorSecure: false,
164
+ donorHost: 'donor.com',
165
+ mirrorSecure: true,
166
+ mirrorHost: 'mirror.com',
167
+ };
168
+ const schemes = [['://', true]];
169
+ const result = rewriteUrl(text, false, [target], schemes);
170
+ assert.strictEqual(result.includes('https://mirror.com/secure'), true);
171
+ });
172
+ it('should switch from https to http', () => {
173
+ const text = 'https://donor.com/public';
174
+ const target = {
175
+ donorSecure: true,
176
+ donorHost: 'donor.com',
177
+ mirrorSecure: false,
178
+ mirrorHost: 'mirror.com',
179
+ };
180
+ const schemes = [['://', true]];
181
+ const result = rewriteUrl(text, false, [target], schemes);
182
+ assert.strictEqual(result.includes('http://mirror.com/public'), true);
183
+ });
184
+ it('should maintain same protocol when both secure', () => {
185
+ const text = 'https://donor.com/path';
186
+ const target = {
187
+ donorSecure: true,
188
+ donorHost: 'donor.com',
189
+ mirrorSecure: true,
190
+ mirrorHost: 'mirror.com',
191
+ };
192
+ const schemes = [['://', true]];
193
+ const result = rewriteUrl(text, false, [target], schemes);
194
+ assert.strictEqual(result.includes('https://mirror.com/path'), true);
195
+ });
196
+ it('should maintain same protocol when both insecure', () => {
197
+ const text = 'http://donor.com/path';
198
+ const target = {
199
+ donorSecure: false,
200
+ donorHost: 'donor.com',
201
+ mirrorSecure: false,
202
+ mirrorHost: 'mirror.com',
203
+ };
204
+ const schemes = [['://', true]];
205
+ const result = rewriteUrl(text, false, [target], schemes);
206
+ assert.strictEqual(result.includes('http://mirror.com/path'), true);
207
+ });
208
+ });
209
+ describe('edge cases', () => {
210
+ it('should handle empty text', () => {
211
+ const result = rewriteUrl('', false, [], []);
212
+ assert.strictEqual(result, '');
213
+ });
214
+ it('should handle empty targets', () => {
215
+ const text = 'http://donor.com/path';
216
+ const result = rewriteUrl(text, false, [], [['://', true]]);
217
+ assert.strictEqual(result, text);
218
+ });
219
+ it('should handle empty schemes', () => {
220
+ const text = 'http://donor.com/path';
221
+ const target = {
222
+ donorSecure: false,
223
+ donorHost: 'donor.com',
224
+ mirrorSecure: false,
225
+ mirrorHost: 'mirror.com',
226
+ };
227
+ const result = rewriteUrl(text, false, [target], []);
228
+ assert.strictEqual(result, text);
229
+ });
230
+ it('should handle subdomains', () => {
231
+ const text = 'https://api.donor.com/v1 and https://donor.com';
232
+ const target = {
233
+ donorSecure: true,
234
+ donorHost: 'donor.com',
235
+ mirrorSecure: true,
236
+ mirrorHost: 'mirror.com',
237
+ };
238
+ const schemes = [['://', true]];
239
+ const result = rewriteUrl(text, false, [target], schemes);
240
+ assert.strictEqual(result.includes('api.donor.com'), true);
241
+ assert.strictEqual(result.includes('https://mirror.com'), true);
242
+ });
243
+ it('should handle ports in URLs', () => {
244
+ const text = 'http://donor.com:8080/path';
245
+ const target = {
246
+ donorSecure: false,
247
+ donorHost: 'donor.com',
248
+ mirrorSecure: false,
249
+ mirrorHost: 'mirror.com',
250
+ };
251
+ const schemes = [['://', true]];
252
+ const result = rewriteUrl(text, false, [target], schemes);
253
+ assert.strictEqual(result.includes('http://mirror.com:8080'), true);
254
+ });
255
+ it('should handle case-sensitive matching', () => {
256
+ const text = 'http://Donor.com and http://donor.com';
257
+ const target = {
258
+ donorSecure: false,
259
+ donorHost: 'donor.com',
260
+ mirrorSecure: false,
261
+ mirrorHost: 'mirror.com',
262
+ };
263
+ const schemes = [['://', true]];
264
+ const result = rewriteUrl(text, false, [target], schemes);
265
+ assert.strictEqual(result.includes('Donor.com'), true);
266
+ assert.strictEqual(result.includes('http://mirror.com'), true);
267
+ });
268
+ it('should handle multiple occurrences of same URL', () => {
269
+ const text = 'Link1: http://donor.com/a, Link2: http://donor.com/b, Link3: http://donor.com/c';
270
+ const target = {
271
+ donorSecure: false,
272
+ donorHost: 'donor.com',
273
+ mirrorSecure: false,
274
+ mirrorHost: 'mirror.com',
275
+ };
276
+ const schemes = [['://', true]];
277
+ const result = rewriteUrl(text, false, [target], schemes);
278
+ const count = (result.match(/mirror\.com/g) || []).length;
279
+ assert.strictEqual(count, 3);
280
+ });
281
+ it('should preserve query strings and fragments', () => {
282
+ const text = 'http://donor.com/path?key=value#section';
283
+ const target = {
284
+ donorSecure: false,
285
+ donorHost: 'donor.com',
286
+ mirrorSecure: false,
287
+ mirrorHost: 'mirror.com',
288
+ };
289
+ const schemes = [['://', true]];
290
+ const result = rewriteUrl(text, false, [target], schemes);
291
+ assert.strictEqual(result.includes('http://mirror.com/path?key=value#section'), true);
292
+ });
293
+ });
294
+ describe('protocol flag in schemes', () => {
295
+ it('should include protocol when withProto is true', () => {
296
+ const text = 'http://donor.com/path';
297
+ const target = {
298
+ donorSecure: false,
299
+ donorHost: 'donor.com',
300
+ mirrorSecure: false,
301
+ mirrorHost: 'mirror.com',
302
+ };
303
+ const schemes = [['://', true]];
304
+ const result = rewriteUrl(text, false, [target], schemes);
305
+ assert.strictEqual(result.includes('http://mirror.com'), true);
306
+ });
307
+ it.skip('should exclude protocol when withProto is false', () => {
308
+ const text = '//donor.com/path';
309
+ const target = {
310
+ donorSecure: false,
311
+ donorHost: 'donor.com',
312
+ mirrorSecure: false,
313
+ mirrorHost: 'mirror.com',
314
+ };
315
+ const schemes = [['://', false]];
316
+ const result = rewriteUrl(text, false, [target], schemes);
317
+ assert.strictEqual(result.includes('//mirror.com'), true);
318
+ assert.strictEqual(result.includes('http://'), false);
319
+ });
320
+ });
321
+ });
322
+ //# sourceMappingURL=rewrite-url.test.js.map