@koderlabs/tasks-sdk-web-network 0.1.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/LICENSE +179 -0
- package/README.md +9 -0
- package/dist/index.cjs +253 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +42 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +227 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
PROPRIETARY SOFTWARE LICENSE — ALL RIGHTS RESERVED
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 KoderLabs. All rights reserved.
|
|
4
|
+
Author: Jawaid Gadiwala <jawaidgadiwala@gmail.com>
|
|
5
|
+
|
|
6
|
+
================================================================================
|
|
7
|
+
1. NO LICENSE GRANTED BY DEFAULT
|
|
8
|
+
================================================================================
|
|
9
|
+
|
|
10
|
+
This software, including all source code, object code, documentation,
|
|
11
|
+
configuration files, build artifacts, test fixtures, and any associated
|
|
12
|
+
materials (collectively, the "Software"), is the proprietary and confidential
|
|
13
|
+
property of KoderLabs.
|
|
14
|
+
|
|
15
|
+
NO license, right, title, interest, or permission of any kind — express,
|
|
16
|
+
implied, statutory, by estoppel, by exhaustion, by patent exhaustion, or by
|
|
17
|
+
any other legal theory — is granted to any person or entity by:
|
|
18
|
+
|
|
19
|
+
(a) viewing the Software on any registry, repository, mirror, CDN,
|
|
20
|
+
cache, or other distribution channel (including but not limited to
|
|
21
|
+
npmjs.com, GitHub, GitLab, or any private registry);
|
|
22
|
+
(b) downloading, cloning, or otherwise obtaining a copy of the Software;
|
|
23
|
+
(c) the act of the Software being technically accessible due to a registry
|
|
24
|
+
requirement, mirror, or third-party distribution;
|
|
25
|
+
(d) any prior course of dealing, custom, or industry practice.
|
|
26
|
+
|
|
27
|
+
The ability to access the Software does NOT imply any permission to use it.
|
|
28
|
+
A separate, signed, written license agreement executed by an authorised
|
|
29
|
+
representative of KoderLabs is the ONLY mechanism by which any rights may be
|
|
30
|
+
granted.
|
|
31
|
+
|
|
32
|
+
================================================================================
|
|
33
|
+
2. PROHIBITED ACTIVITIES (NON-EXHAUSTIVE)
|
|
34
|
+
================================================================================
|
|
35
|
+
|
|
36
|
+
Without prior signed written permission from KoderLabs, the following are
|
|
37
|
+
expressly PROHIBITED and constitute infringement, breach of contract, and
|
|
38
|
+
unauthorised use:
|
|
39
|
+
|
|
40
|
+
(a) Copying the Software in whole or in part, in any medium;
|
|
41
|
+
(b) Modifying, adapting, translating, porting, or creating derivative works
|
|
42
|
+
of the Software;
|
|
43
|
+
(c) Distributing, republishing, mirroring, hosting, transmitting,
|
|
44
|
+
sublicensing, leasing, lending, renting, selling, offering for sale,
|
|
45
|
+
bartering, gifting, or otherwise transferring the Software or any
|
|
46
|
+
portion of it;
|
|
47
|
+
(d) Forking the Software's repository, whether on GitHub, GitLab, Bitbucket,
|
|
48
|
+
Codeberg, Sourcehut, or any other version-control hosting service;
|
|
49
|
+
(e) Reverse engineering, decompiling, disassembling, deobfuscating,
|
|
50
|
+
extracting source from compiled or minified artifacts, or attempting to
|
|
51
|
+
derive the source code, algorithms, or trade secrets;
|
|
52
|
+
(f) Removing, altering, or obscuring any copyright, trademark, license,
|
|
53
|
+
attribution, or proprietary notice;
|
|
54
|
+
(g) Using the Software, in whole or in part, to train, fine-tune,
|
|
55
|
+
evaluate, or benchmark any machine-learning model, embedding model, or
|
|
56
|
+
AI system;
|
|
57
|
+
(h) Using the Software to provide a hosted service, SaaS offering,
|
|
58
|
+
managed offering, or any form of public or commercial offering;
|
|
59
|
+
(i) Using the Software in any production, staging, development, evaluation,
|
|
60
|
+
or testing capacity, whether commercial or non-commercial;
|
|
61
|
+
(j) Bypassing, disabling, or attempting to circumvent any technical
|
|
62
|
+
protection measure (license check, telemetry, signature verification,
|
|
63
|
+
etc.) embedded in the Software;
|
|
64
|
+
(k) Combining or integrating the Software with any work licensed under a
|
|
65
|
+
copyleft licence (including but not limited to GPL, AGPL, LGPL, MPL,
|
|
66
|
+
EPL) in a manner that would purport to relicense the Software;
|
|
67
|
+
(l) Filing, prosecuting, or threatening patent litigation against KoderLabs
|
|
68
|
+
or its customers based on any feature, design, or behaviour of the
|
|
69
|
+
Software ("defensive termination" — any such action automatically and
|
|
70
|
+
immediately terminates any rights granted to the litigant elsewhere).
|
|
71
|
+
|
|
72
|
+
================================================================================
|
|
73
|
+
3. NO IMPLIED RIGHT TO INTERNAL USE
|
|
74
|
+
================================================================================
|
|
75
|
+
|
|
76
|
+
The Software is NOT licensed for internal evaluation, internal production,
|
|
77
|
+
internal testing, or any other internal use unless and until a separate
|
|
78
|
+
written commercial licence is executed. Possession of a copy of the Software,
|
|
79
|
+
by whatever means, confers no right to execute, run, deploy, or use it.
|
|
80
|
+
|
|
81
|
+
================================================================================
|
|
82
|
+
4. NO PATENT, TRADEMARK, OR OTHER IP GRANT
|
|
83
|
+
================================================================================
|
|
84
|
+
|
|
85
|
+
No patent licence, trademark licence, trade-secret disclosure, design-right
|
|
86
|
+
licence, or any other intellectual-property licence is granted under this
|
|
87
|
+
notice. KoderLabs retains all such rights.
|
|
88
|
+
|
|
89
|
+
================================================================================
|
|
90
|
+
5. CONFIDENTIALITY
|
|
91
|
+
================================================================================
|
|
92
|
+
|
|
93
|
+
The Software contains trade secrets and confidential information of
|
|
94
|
+
KoderLabs. Any party in possession of the Software shall (a) treat it as
|
|
95
|
+
confidential, (b) take reasonable measures to prevent unauthorised access or
|
|
96
|
+
disclosure, and (c) not disclose, publish, or make available the Software or
|
|
97
|
+
any portion thereof to any third party.
|
|
98
|
+
|
|
99
|
+
================================================================================
|
|
100
|
+
6. AUTOMATIC TERMINATION
|
|
101
|
+
================================================================================
|
|
102
|
+
|
|
103
|
+
Any rights that may exist under a separate written agreement terminate
|
|
104
|
+
AUTOMATICALLY AND IMMEDIATELY upon any breach of this notice, without notice
|
|
105
|
+
and without judicial action. Upon termination, the breaching party shall
|
|
106
|
+
immediately destroy all copies of the Software in its possession or control
|
|
107
|
+
and certify destruction in writing to KoderLabs.
|
|
108
|
+
|
|
109
|
+
================================================================================
|
|
110
|
+
7. NO WARRANTY
|
|
111
|
+
================================================================================
|
|
112
|
+
|
|
113
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
114
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
115
|
+
FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, ACCURACY, RELIABILITY,
|
|
116
|
+
TITLE, AVAILABILITY, OR THAT THE SOFTWARE WILL OPERATE UNINTERRUPTED OR
|
|
117
|
+
ERROR-FREE.
|
|
118
|
+
|
|
119
|
+
================================================================================
|
|
120
|
+
8. NO LIABILITY
|
|
121
|
+
================================================================================
|
|
122
|
+
|
|
123
|
+
IN NO EVENT SHALL KODERLABS OR ITS AFFILIATES, OFFICERS, EMPLOYEES, AGENTS,
|
|
124
|
+
LICENSORS, OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
|
|
125
|
+
LIABILITY (INCLUDING DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
126
|
+
CONSEQUENTIAL, PUNITIVE, LOST PROFITS, LOST DATA, OR BUSINESS INTERRUPTION),
|
|
127
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT
|
|
128
|
+
LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE
|
|
129
|
+
SOFTWARE OR THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF
|
|
130
|
+
THE POSSIBILITY OF SUCH DAMAGE. IN NO EVENT SHALL KODERLABS' TOTAL CUMULATIVE
|
|
131
|
+
LIABILITY EXCEED ONE U.S. DOLLAR (USD 1.00).
|
|
132
|
+
|
|
133
|
+
================================================================================
|
|
134
|
+
9. EQUITABLE RELIEF
|
|
135
|
+
================================================================================
|
|
136
|
+
|
|
137
|
+
The parties acknowledge that any breach of this notice would cause
|
|
138
|
+
irreparable harm to KoderLabs for which monetary damages would be inadequate.
|
|
139
|
+
KoderLabs is therefore entitled, in addition to all other remedies available
|
|
140
|
+
at law, to specific performance and injunctive relief without the need to
|
|
141
|
+
post bond.
|
|
142
|
+
|
|
143
|
+
================================================================================
|
|
144
|
+
10. GOVERNING LAW AND VENUE
|
|
145
|
+
================================================================================
|
|
146
|
+
|
|
147
|
+
This notice shall be governed by and construed in accordance with the laws
|
|
148
|
+
of the Islamic Republic of Pakistan, without regard to its conflict-of-laws
|
|
149
|
+
principles. Any dispute arising out of or in connection with this notice
|
|
150
|
+
shall be subject to the exclusive jurisdiction of the courts of Karachi,
|
|
151
|
+
Pakistan.
|
|
152
|
+
|
|
153
|
+
The United Nations Convention on Contracts for the International Sale of
|
|
154
|
+
Goods shall not apply.
|
|
155
|
+
|
|
156
|
+
================================================================================
|
|
157
|
+
11. SEVERABILITY AND ENTIRE NOTICE
|
|
158
|
+
================================================================================
|
|
159
|
+
|
|
160
|
+
If any provision of this notice is held invalid or unenforceable, the
|
|
161
|
+
remaining provisions shall continue in full force and effect. This notice,
|
|
162
|
+
together with any separate signed written licence executed between KoderLabs
|
|
163
|
+
and a licensee, constitutes the entire agreement concerning the Software and
|
|
164
|
+
supersedes all prior or contemporaneous understandings.
|
|
165
|
+
|
|
166
|
+
================================================================================
|
|
167
|
+
12. CONTACT
|
|
168
|
+
================================================================================
|
|
169
|
+
|
|
170
|
+
For licensing inquiries, audit requests, or notice of suspected
|
|
171
|
+
infringement, contact:
|
|
172
|
+
|
|
173
|
+
KoderLabs
|
|
174
|
+
Attn: Jawaid Gadiwala
|
|
175
|
+
Email: jawaidgadiwala@gmail.com
|
|
176
|
+
|
|
177
|
+
================================================================================
|
|
178
|
+
END OF LICENCE
|
|
179
|
+
================================================================================
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Proprietary Software
|
|
2
|
+
|
|
3
|
+
KoderLabs proprietary. All rights reserved.
|
|
4
|
+
|
|
5
|
+
See the bundled `LICENSE` for terms. No grant of any rights, express or
|
|
6
|
+
implied, is conferred by access to or possession of this package. A
|
|
7
|
+
separate signed written licence from KoderLabs is required for any use.
|
|
8
|
+
|
|
9
|
+
Licensing inquiries: jawaidgadiwala@gmail.com
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
NetworkRing: () => NetworkRing,
|
|
25
|
+
networkIntegration: () => networkIntegration
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/ring.ts
|
|
30
|
+
var NetworkRing = class {
|
|
31
|
+
static {
|
|
32
|
+
__name(this, "NetworkRing");
|
|
33
|
+
}
|
|
34
|
+
buf = [];
|
|
35
|
+
max;
|
|
36
|
+
constructor(max = 20) {
|
|
37
|
+
this.max = max;
|
|
38
|
+
}
|
|
39
|
+
push(entry) {
|
|
40
|
+
this.buf.push(entry);
|
|
41
|
+
if (this.buf.length > this.max) {
|
|
42
|
+
this.buf.shift();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
getAll() {
|
|
46
|
+
return [
|
|
47
|
+
...this.buf
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
size() {
|
|
51
|
+
return this.buf.length;
|
|
52
|
+
}
|
|
53
|
+
clear() {
|
|
54
|
+
this.buf.length = 0;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/sanitize.ts
|
|
59
|
+
var REDACTED_HEADERS = /* @__PURE__ */ new Set([
|
|
60
|
+
"authorization",
|
|
61
|
+
"cookie",
|
|
62
|
+
"set-cookie",
|
|
63
|
+
"x-auth-token"
|
|
64
|
+
]);
|
|
65
|
+
function builtinRequestSanitizer(entry) {
|
|
66
|
+
const sanitizedHeaders = {};
|
|
67
|
+
for (const [k, v] of Object.entries(entry.requestHeaders)) {
|
|
68
|
+
if (REDACTED_HEADERS.has(k.toLowerCase())) {
|
|
69
|
+
sanitizedHeaders[k] = "[Filtered]";
|
|
70
|
+
} else {
|
|
71
|
+
sanitizedHeaders[k] = v;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
...entry,
|
|
76
|
+
requestHeaders: sanitizedHeaders
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
__name(builtinRequestSanitizer, "builtinRequestSanitizer");
|
|
80
|
+
function chainSanitizers(entry, ...fns) {
|
|
81
|
+
let current = entry;
|
|
82
|
+
for (const fn of fns) {
|
|
83
|
+
if (!current) return null;
|
|
84
|
+
current = fn(current);
|
|
85
|
+
}
|
|
86
|
+
return current;
|
|
87
|
+
}
|
|
88
|
+
__name(chainSanitizers, "chainSanitizers");
|
|
89
|
+
function shouldIgnoreUrl(url, ignoreUrls = []) {
|
|
90
|
+
for (const pattern of ignoreUrls) {
|
|
91
|
+
if (typeof pattern === "string" && url.startsWith(pattern)) return true;
|
|
92
|
+
if (pattern instanceof RegExp && pattern.test(url)) return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
__name(shouldIgnoreUrl, "shouldIgnoreUrl");
|
|
97
|
+
|
|
98
|
+
// src/fetch-patch.ts
|
|
99
|
+
function patchFetch(ring, opts) {
|
|
100
|
+
if (typeof globalThis.fetch !== "function") return () => {
|
|
101
|
+
};
|
|
102
|
+
const originalFetch = globalThis.fetch.bind(globalThis);
|
|
103
|
+
globalThis.fetch = /* @__PURE__ */ __name(async function patchedFetch(input, init) {
|
|
104
|
+
const url = input instanceof Request ? input.url : String(input);
|
|
105
|
+
const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
|
|
106
|
+
if (shouldIgnoreUrl(url, opts.ignoreUrls)) {
|
|
107
|
+
return originalFetch(input, init);
|
|
108
|
+
}
|
|
109
|
+
const start = Date.now();
|
|
110
|
+
let response;
|
|
111
|
+
let statusCode = null;
|
|
112
|
+
try {
|
|
113
|
+
response = await originalFetch(input, init);
|
|
114
|
+
statusCode = response.status;
|
|
115
|
+
} catch (err) {
|
|
116
|
+
const entry2 = buildEntry(url, method, null, start, opts, init);
|
|
117
|
+
recordEntry(ring, entry2, opts);
|
|
118
|
+
throw err;
|
|
119
|
+
}
|
|
120
|
+
const entry = buildEntry(url, method, statusCode, start, opts, init);
|
|
121
|
+
recordEntry(ring, entry, opts);
|
|
122
|
+
return response;
|
|
123
|
+
}, "patchedFetch");
|
|
124
|
+
return () => {
|
|
125
|
+
globalThis.fetch = originalFetch;
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
__name(patchFetch, "patchFetch");
|
|
129
|
+
function buildEntry(url, method, statusCode, start, opts, init) {
|
|
130
|
+
const rawHeaders = {};
|
|
131
|
+
if (init?.headers) {
|
|
132
|
+
const headers = init.headers instanceof Headers ? init.headers : new Headers(init.headers);
|
|
133
|
+
headers.forEach((v, k) => {
|
|
134
|
+
rawHeaders[k] = v;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
method,
|
|
139
|
+
url,
|
|
140
|
+
statusCode,
|
|
141
|
+
duration: Date.now() - start,
|
|
142
|
+
requestBody: opts.recordRequestBody ? init?.body ?? null : null,
|
|
143
|
+
responseBody: null,
|
|
144
|
+
requestHeaders: rawHeaders,
|
|
145
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
__name(buildEntry, "buildEntry");
|
|
149
|
+
function recordEntry(ring, entry, opts) {
|
|
150
|
+
let sanitized = builtinRequestSanitizer(entry);
|
|
151
|
+
if (opts.requestSanitizer) {
|
|
152
|
+
sanitized = chainSanitizers(sanitized, opts.requestSanitizer);
|
|
153
|
+
}
|
|
154
|
+
if (opts.responseSanitizer) {
|
|
155
|
+
sanitized = chainSanitizers(sanitized, opts.responseSanitizer);
|
|
156
|
+
}
|
|
157
|
+
if (sanitized) ring.push(sanitized);
|
|
158
|
+
}
|
|
159
|
+
__name(recordEntry, "recordEntry");
|
|
160
|
+
|
|
161
|
+
// src/xhr-patch.ts
|
|
162
|
+
function patchXHR(ring, opts) {
|
|
163
|
+
if (typeof XMLHttpRequest === "undefined") return () => {
|
|
164
|
+
};
|
|
165
|
+
const OriginalXHR = XMLHttpRequest;
|
|
166
|
+
const originalOpen = XMLHttpRequest.prototype.open;
|
|
167
|
+
const originalSend = XMLHttpRequest.prototype.send;
|
|
168
|
+
const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
|
|
169
|
+
XMLHttpRequest.prototype.open = function(method, url, async = true, user, password) {
|
|
170
|
+
this.__it_method = method.toUpperCase();
|
|
171
|
+
this.__it_url = String(url);
|
|
172
|
+
this.__it_headers = {};
|
|
173
|
+
this.__it_start = 0;
|
|
174
|
+
return originalOpen.call(this, method, url, async, user, password);
|
|
175
|
+
};
|
|
176
|
+
XMLHttpRequest.prototype.setRequestHeader = function(name, value) {
|
|
177
|
+
if (this.__it_headers) {
|
|
178
|
+
this.__it_headers[name] = value;
|
|
179
|
+
}
|
|
180
|
+
return originalSetRequestHeader.call(this, name, value);
|
|
181
|
+
};
|
|
182
|
+
XMLHttpRequest.prototype.send = function(body) {
|
|
183
|
+
const xhrUrl = this.__it_url ?? "";
|
|
184
|
+
const method = this.__it_method ?? "GET";
|
|
185
|
+
const headers = this.__it_headers ?? {};
|
|
186
|
+
if (!shouldIgnoreUrl(xhrUrl, opts.ignoreUrls)) {
|
|
187
|
+
this.__it_start = Date.now();
|
|
188
|
+
this.addEventListener("loadend", () => {
|
|
189
|
+
const entry = {
|
|
190
|
+
method,
|
|
191
|
+
url: xhrUrl,
|
|
192
|
+
statusCode: this.status || null,
|
|
193
|
+
duration: Date.now() - this.__it_start,
|
|
194
|
+
requestBody: opts.recordRequestBody ? body ?? null : null,
|
|
195
|
+
responseBody: opts.recordResponseBody ? this.responseText ?? null : null,
|
|
196
|
+
requestHeaders: headers,
|
|
197
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
198
|
+
};
|
|
199
|
+
recordEntry2(ring, entry, opts);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return originalSend.call(this, body);
|
|
203
|
+
};
|
|
204
|
+
return () => {
|
|
205
|
+
XMLHttpRequest.prototype.open = originalOpen;
|
|
206
|
+
XMLHttpRequest.prototype.send = originalSend;
|
|
207
|
+
XMLHttpRequest.prototype.setRequestHeader = originalSetRequestHeader;
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
__name(patchXHR, "patchXHR");
|
|
211
|
+
function recordEntry2(ring, entry, opts) {
|
|
212
|
+
let sanitized = builtinRequestSanitizer(entry);
|
|
213
|
+
if (opts.requestSanitizer) sanitized = chainSanitizers(sanitized, opts.requestSanitizer);
|
|
214
|
+
if (opts.responseSanitizer && sanitized) sanitized = chainSanitizers(sanitized, opts.responseSanitizer);
|
|
215
|
+
if (sanitized) ring.push(sanitized);
|
|
216
|
+
}
|
|
217
|
+
__name(recordEntry2, "recordEntry");
|
|
218
|
+
|
|
219
|
+
// src/index.ts
|
|
220
|
+
function networkIntegration(opts = {}) {
|
|
221
|
+
const ring = new NetworkRing(opts.maxEntries ?? 20);
|
|
222
|
+
const teardowns = [];
|
|
223
|
+
const integration = {
|
|
224
|
+
name: "network",
|
|
225
|
+
setup(_client) {
|
|
226
|
+
teardowns.push(patchFetch(ring, opts));
|
|
227
|
+
teardowns.push(patchXHR(ring, opts));
|
|
228
|
+
try {
|
|
229
|
+
const errorsModule = globalThis.__instanttasks_errors_module__;
|
|
230
|
+
if (errorsModule?._registerNetworkRing) {
|
|
231
|
+
errorsModule._registerNetworkRing(() => ring.getAll());
|
|
232
|
+
}
|
|
233
|
+
} catch {
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
teardown() {
|
|
237
|
+
for (const fn of teardowns) fn();
|
|
238
|
+
teardowns.length = 0;
|
|
239
|
+
ring.clear();
|
|
240
|
+
},
|
|
241
|
+
getEntries() {
|
|
242
|
+
return ring.getAll();
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
return integration;
|
|
246
|
+
}
|
|
247
|
+
__name(networkIntegration, "networkIntegration");
|
|
248
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
249
|
+
0 && (module.exports = {
|
|
250
|
+
NetworkRing,
|
|
251
|
+
networkIntegration
|
|
252
|
+
});
|
|
253
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/ring.ts","../src/sanitize.ts","../src/fetch-patch.ts","../src/xhr-patch.ts"],"sourcesContent":["import type { ClientInterface, Integration } from '@koderlabs/tasks-sdk';\nimport type { NetworkIntegrationOptions } from './types';\nimport { NetworkRing } from './ring';\nimport { patchFetch } from './fetch-patch';\nimport { patchXHR } from './xhr-patch';\n\nexport type { NetworkEntry, NetworkIntegrationOptions } from './types';\nexport { NetworkRing } from './ring';\n\nexport function networkIntegration(opts: NetworkIntegrationOptions = {}): Integration & {\n getEntries(): ReturnType<NetworkRing['getAll']>;\n} {\n const ring = new NetworkRing(opts.maxEntries ?? 20);\n const teardowns: Array<() => void> = [];\n\n const integration = {\n name: 'network',\n\n setup(_client: ClientInterface) {\n teardowns.push(patchFetch(ring, opts));\n teardowns.push(patchXHR(ring, opts));\n\n // Register ring accessor with errors integration if loaded\n try {\n // Dynamic import to avoid hard dep cycle when errors integration is absent\n const errorsModule = (globalThis as any).__instanttasks_errors_module__;\n if (errorsModule?._registerNetworkRing) {\n errorsModule._registerNetworkRing(() => ring.getAll());\n }\n } catch {\n // errors integration not loaded — fine\n }\n },\n\n teardown() {\n for (const fn of teardowns) fn();\n teardowns.length = 0;\n ring.clear();\n },\n\n getEntries() {\n return ring.getAll();\n },\n };\n\n return integration;\n}\n","import type { NetworkEntry } from './types';\n\nexport class NetworkRing {\n private readonly buf: NetworkEntry[] = [];\n private readonly max: number;\n\n constructor(max = 20) {\n this.max = max;\n }\n\n push(entry: NetworkEntry): void {\n this.buf.push(entry);\n if (this.buf.length > this.max) {\n this.buf.shift();\n }\n }\n\n getAll(): NetworkEntry[] {\n return [...this.buf];\n }\n\n size(): number {\n return this.buf.length;\n }\n\n clear(): void {\n this.buf.length = 0;\n }\n}\n","import type { NetworkEntry } from './types';\n\n/** Header names that are always redacted. */\nconst REDACTED_HEADERS = new Set(['authorization', 'cookie', 'set-cookie', 'x-auth-token']);\n\n/**\n * Builtin sanitizer: strips Authorization, Cookie, Set-Cookie headers\n * and ensures request/response bodies are dropped unless explicitly opted in.\n */\nexport function builtinRequestSanitizer(entry: NetworkEntry): NetworkEntry {\n const sanitizedHeaders: Record<string, string> = {};\n for (const [k, v] of Object.entries(entry.requestHeaders)) {\n if (REDACTED_HEADERS.has(k.toLowerCase())) {\n sanitizedHeaders[k] = '[Filtered]';\n } else {\n sanitizedHeaders[k] = v;\n }\n }\n return { ...entry, requestHeaders: sanitizedHeaders };\n}\n\n/**\n * Chain multiple sanitizers. Returns null if any returns null.\n */\nexport function chainSanitizers(\n entry: NetworkEntry,\n ...fns: Array<(e: NetworkEntry) => NetworkEntry | null>\n): NetworkEntry | null {\n let current: NetworkEntry | null = entry;\n for (const fn of fns) {\n if (!current) return null;\n current = fn(current);\n }\n return current;\n}\n\nexport function shouldIgnoreUrl(\n url: string,\n ignoreUrls: Array<string | RegExp> = [],\n): boolean {\n for (const pattern of ignoreUrls) {\n if (typeof pattern === 'string' && url.startsWith(pattern)) return true;\n if (pattern instanceof RegExp && pattern.test(url)) return true;\n }\n return false;\n}\n","import type { NetworkEntry, NetworkIntegrationOptions } from './types';\nimport { NetworkRing } from './ring';\nimport { builtinRequestSanitizer, chainSanitizers, shouldIgnoreUrl } from './sanitize';\n\nexport function patchFetch(ring: NetworkRing, opts: NetworkIntegrationOptions): () => void {\n if (typeof globalThis.fetch !== 'function') return () => {};\n\n const originalFetch = globalThis.fetch.bind(globalThis);\n\n globalThis.fetch = async function patchedFetch(\n input: RequestInfo | URL,\n init?: RequestInit,\n ): Promise<Response> {\n const url = input instanceof Request ? input.url : String(input);\n const method = (init?.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase();\n\n if (shouldIgnoreUrl(url, opts.ignoreUrls)) {\n return originalFetch(input, init);\n }\n\n const start = Date.now();\n let response: Response;\n let statusCode: number | null = null;\n\n try {\n response = await originalFetch(input, init);\n statusCode = response.status;\n } catch (err) {\n const entry = buildEntry(url, method, null, start, opts, init);\n recordEntry(ring, entry, opts);\n throw err;\n }\n\n const entry = buildEntry(url, method, statusCode, start, opts, init);\n recordEntry(ring, entry, opts);\n return response;\n };\n\n return () => {\n globalThis.fetch = originalFetch;\n };\n}\n\nfunction buildEntry(\n url: string,\n method: string,\n statusCode: number | null,\n start: number,\n opts: NetworkIntegrationOptions,\n init?: RequestInit,\n): NetworkEntry {\n const rawHeaders: Record<string, string> = {};\n if (init?.headers) {\n const headers = init.headers instanceof Headers\n ? init.headers\n : new Headers(init.headers as HeadersInit);\n headers.forEach((v, k) => { rawHeaders[k] = v; });\n }\n\n return {\n method,\n url,\n statusCode,\n duration: Date.now() - start,\n requestBody: opts.recordRequestBody ? (init?.body ?? null) : null,\n responseBody: null, // populated only if recordResponseBody + response cloned\n requestHeaders: rawHeaders,\n timestamp: new Date().toISOString(),\n };\n}\n\nfunction recordEntry(ring: NetworkRing, entry: NetworkEntry, opts: NetworkIntegrationOptions): void {\n let sanitized: NetworkEntry | null = builtinRequestSanitizer(entry);\n if (opts.requestSanitizer) {\n sanitized = chainSanitizers(sanitized, opts.requestSanitizer);\n }\n if (opts.responseSanitizer) {\n sanitized = chainSanitizers(sanitized!, opts.responseSanitizer);\n }\n if (sanitized) ring.push(sanitized);\n}\n","import type { NetworkEntry, NetworkIntegrationOptions } from './types';\nimport { NetworkRing } from './ring';\nimport { builtinRequestSanitizer, chainSanitizers, shouldIgnoreUrl } from './sanitize';\n\nexport function patchXHR(ring: NetworkRing, opts: NetworkIntegrationOptions): () => void {\n if (typeof XMLHttpRequest === 'undefined') return () => {};\n\n const OriginalXHR = XMLHttpRequest;\n\n // We extend XMLHttpRequest by monkey-patching open + send\n const originalOpen = XMLHttpRequest.prototype.open;\n const originalSend = XMLHttpRequest.prototype.send;\n const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;\n\n XMLHttpRequest.prototype.open = function (\n method: string,\n url: string | URL,\n async = true,\n user?: string | null,\n password?: string | null,\n ) {\n (this as any).__it_method = method.toUpperCase();\n (this as any).__it_url = String(url);\n (this as any).__it_headers = {};\n (this as any).__it_start = 0;\n return originalOpen.call(this, method, url, async, user, password);\n };\n\n XMLHttpRequest.prototype.setRequestHeader = function (name: string, value: string) {\n if ((this as any).__it_headers) {\n (this as any).__it_headers[name] = value;\n }\n return originalSetRequestHeader.call(this, name, value);\n };\n\n XMLHttpRequest.prototype.send = function (body?: Document | XMLHttpRequestBodyInit | null) {\n const xhrUrl: string = (this as any).__it_url ?? '';\n const method: string = (this as any).__it_method ?? 'GET';\n const headers: Record<string, string> = (this as any).__it_headers ?? {};\n\n if (!shouldIgnoreUrl(xhrUrl, opts.ignoreUrls)) {\n (this as any).__it_start = Date.now();\n\n this.addEventListener('loadend', () => {\n const entry: NetworkEntry = {\n method,\n url: xhrUrl,\n statusCode: this.status || null,\n duration: Date.now() - (this as any).__it_start,\n requestBody: opts.recordRequestBody ? body ?? null : null,\n responseBody: opts.recordResponseBody ? this.responseText ?? null : null,\n requestHeaders: headers,\n timestamp: new Date().toISOString(),\n };\n recordEntry(ring, entry, opts);\n });\n }\n\n return originalSend.call(this, body);\n };\n\n return () => {\n XMLHttpRequest.prototype.open = originalOpen;\n XMLHttpRequest.prototype.send = originalSend;\n XMLHttpRequest.prototype.setRequestHeader = originalSetRequestHeader;\n };\n}\n\nfunction recordEntry(ring: NetworkRing, entry: NetworkEntry, opts: NetworkIntegrationOptions): void {\n let sanitized: NetworkEntry | null = builtinRequestSanitizer(entry);\n if (opts.requestSanitizer) sanitized = chainSanitizers(sanitized, opts.requestSanitizer);\n if (opts.responseSanitizer && sanitized) sanitized = chainSanitizers(sanitized, opts.responseSanitizer);\n if (sanitized) ring.push(sanitized);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAEA;;;;;;;;ACAO,IAAMA,cAAN,MAAMA;EAAb,OAAaA;;;EACMC,MAAsB,CAAA;EACtBC;EAEjB,YAAYA,MAAM,IAAI;AACpB,SAAKA,MAAMA;EACb;EAEAC,KAAKC,OAA2B;AAC9B,SAAKH,IAAIE,KAAKC,KAAAA;AACd,QAAI,KAAKH,IAAII,SAAS,KAAKH,KAAK;AAC9B,WAAKD,IAAIK,MAAK;IAChB;EACF;EAEAC,SAAyB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;MACF,QAAQ;MAER;IACF;IAEAC,WAAAA;AACE,iBAAWC,MAAMd,UAAWc,IAAAA;AAC5Bd,gBAAUe,SAAS;AACnBlB,WAAKmB,MAAK;IACZ;IAEAC,aAAAA;AACE,aAAOpB,KAAKe,OAAM;IACpB;EACF;AAEA,SAAOX;AACT;AArCgBN;","names":["NetworkRing","buf","max","push","entry","length","shift","getAll","size","clear","REDACTED_HEADERS","Set","builtinRequestSanitizer","entry","sanitizedHeaders","k","v","Object","entries","requestHeaders","has","toLowerCase","chainSanitizers","fns","current","fn","shouldIgnoreUrl","url","ignoreUrls","pattern","startsWith","RegExp","test","patchFetch","ring","opts","globalThis","fetch","originalFetch","bind","patchedFetch","input","init","url","Request","String","method","toUpperCase","shouldIgnoreUrl","ignoreUrls","start","Date","now","response","statusCode","status","err","entry","buildEntry","recordEntry","rawHeaders","headers","Headers","forEach","v","k","duration","requestBody","recordRequestBody","body","responseBody","requestHeaders","timestamp","toISOString","sanitized","builtinRequestSanitizer","requestSanitizer","chainSanitizers","responseSanitizer","push","patchXHR","ring","opts","XMLHttpRequest","OriginalXHR","originalOpen","prototype","open","originalSend","send","originalSetRequestHeader","setRequestHeader","method","url","async","user","password","__it_method","toUpperCase","__it_url","String","__it_headers","__it_start","call","name","value","body","xhrUrl","headers","shouldIgnoreUrl","ignoreUrls","Date","now","addEventListener","entry","statusCode","status","duration","requestBody","recordRequestBody","responseBody","recordResponseBody","responseText","requestHeaders","timestamp","toISOString","recordEntry","sanitized","builtinRequestSanitizer","requestSanitizer","chainSanitizers","responseSanitizer","push","networkIntegration","opts","ring","NetworkRing","maxEntries","teardowns","integration","name","setup","_client","push","patchFetch","patchXHR","errorsModule","globalThis","__instanttasks_errors_module__","_registerNetworkRing","getAll","teardown","fn","length","clear","getEntries"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Integration } from '@koderlabs/tasks-sdk';
|
|
2
|
+
|
|
3
|
+
interface NetworkEntry {
|
|
4
|
+
method: string;
|
|
5
|
+
url: string;
|
|
6
|
+
statusCode: number | null;
|
|
7
|
+
duration: number | null;
|
|
8
|
+
requestBody: unknown | null;
|
|
9
|
+
responseBody: unknown | null;
|
|
10
|
+
requestHeaders: Record<string, string>;
|
|
11
|
+
timestamp: string;
|
|
12
|
+
}
|
|
13
|
+
interface NetworkIntegrationOptions {
|
|
14
|
+
/** Max number of requests to keep in the ring. Default 20. */
|
|
15
|
+
maxEntries?: number;
|
|
16
|
+
/** Called to sanitize request before recording. Return null to drop. */
|
|
17
|
+
requestSanitizer?: (entry: NetworkEntry) => NetworkEntry | null;
|
|
18
|
+
/** Called to sanitize response before recording. Return null to drop. */
|
|
19
|
+
responseSanitizer?: (entry: NetworkEntry) => NetworkEntry | null;
|
|
20
|
+
/** Whether to record request bodies. Default false (PII guard). */
|
|
21
|
+
recordRequestBody?: boolean;
|
|
22
|
+
/** Whether to record response bodies. Default false. */
|
|
23
|
+
recordResponseBody?: boolean;
|
|
24
|
+
/** URL patterns (string prefix or RegExp) to exclude from capture. */
|
|
25
|
+
ignoreUrls?: Array<string | RegExp>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare class NetworkRing {
|
|
29
|
+
private readonly buf;
|
|
30
|
+
private readonly max;
|
|
31
|
+
constructor(max?: number);
|
|
32
|
+
push(entry: NetworkEntry): void;
|
|
33
|
+
getAll(): NetworkEntry[];
|
|
34
|
+
size(): number;
|
|
35
|
+
clear(): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare function networkIntegration(opts?: NetworkIntegrationOptions): Integration & {
|
|
39
|
+
getEntries(): ReturnType<NetworkRing['getAll']>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { type NetworkEntry, type NetworkIntegrationOptions, NetworkRing, networkIntegration };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Integration } from '@koderlabs/tasks-sdk';
|
|
2
|
+
|
|
3
|
+
interface NetworkEntry {
|
|
4
|
+
method: string;
|
|
5
|
+
url: string;
|
|
6
|
+
statusCode: number | null;
|
|
7
|
+
duration: number | null;
|
|
8
|
+
requestBody: unknown | null;
|
|
9
|
+
responseBody: unknown | null;
|
|
10
|
+
requestHeaders: Record<string, string>;
|
|
11
|
+
timestamp: string;
|
|
12
|
+
}
|
|
13
|
+
interface NetworkIntegrationOptions {
|
|
14
|
+
/** Max number of requests to keep in the ring. Default 20. */
|
|
15
|
+
maxEntries?: number;
|
|
16
|
+
/** Called to sanitize request before recording. Return null to drop. */
|
|
17
|
+
requestSanitizer?: (entry: NetworkEntry) => NetworkEntry | null;
|
|
18
|
+
/** Called to sanitize response before recording. Return null to drop. */
|
|
19
|
+
responseSanitizer?: (entry: NetworkEntry) => NetworkEntry | null;
|
|
20
|
+
/** Whether to record request bodies. Default false (PII guard). */
|
|
21
|
+
recordRequestBody?: boolean;
|
|
22
|
+
/** Whether to record response bodies. Default false. */
|
|
23
|
+
recordResponseBody?: boolean;
|
|
24
|
+
/** URL patterns (string prefix or RegExp) to exclude from capture. */
|
|
25
|
+
ignoreUrls?: Array<string | RegExp>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare class NetworkRing {
|
|
29
|
+
private readonly buf;
|
|
30
|
+
private readonly max;
|
|
31
|
+
constructor(max?: number);
|
|
32
|
+
push(entry: NetworkEntry): void;
|
|
33
|
+
getAll(): NetworkEntry[];
|
|
34
|
+
size(): number;
|
|
35
|
+
clear(): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare function networkIntegration(opts?: NetworkIntegrationOptions): Integration & {
|
|
39
|
+
getEntries(): ReturnType<NetworkRing['getAll']>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { type NetworkEntry, type NetworkIntegrationOptions, NetworkRing, networkIntegration };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/ring.ts
|
|
5
|
+
var NetworkRing = class {
|
|
6
|
+
static {
|
|
7
|
+
__name(this, "NetworkRing");
|
|
8
|
+
}
|
|
9
|
+
buf = [];
|
|
10
|
+
max;
|
|
11
|
+
constructor(max = 20) {
|
|
12
|
+
this.max = max;
|
|
13
|
+
}
|
|
14
|
+
push(entry) {
|
|
15
|
+
this.buf.push(entry);
|
|
16
|
+
if (this.buf.length > this.max) {
|
|
17
|
+
this.buf.shift();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
getAll() {
|
|
21
|
+
return [
|
|
22
|
+
...this.buf
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
size() {
|
|
26
|
+
return this.buf.length;
|
|
27
|
+
}
|
|
28
|
+
clear() {
|
|
29
|
+
this.buf.length = 0;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/sanitize.ts
|
|
34
|
+
var REDACTED_HEADERS = /* @__PURE__ */ new Set([
|
|
35
|
+
"authorization",
|
|
36
|
+
"cookie",
|
|
37
|
+
"set-cookie",
|
|
38
|
+
"x-auth-token"
|
|
39
|
+
]);
|
|
40
|
+
function builtinRequestSanitizer(entry) {
|
|
41
|
+
const sanitizedHeaders = {};
|
|
42
|
+
for (const [k, v] of Object.entries(entry.requestHeaders)) {
|
|
43
|
+
if (REDACTED_HEADERS.has(k.toLowerCase())) {
|
|
44
|
+
sanitizedHeaders[k] = "[Filtered]";
|
|
45
|
+
} else {
|
|
46
|
+
sanitizedHeaders[k] = v;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
...entry,
|
|
51
|
+
requestHeaders: sanitizedHeaders
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
__name(builtinRequestSanitizer, "builtinRequestSanitizer");
|
|
55
|
+
function chainSanitizers(entry, ...fns) {
|
|
56
|
+
let current = entry;
|
|
57
|
+
for (const fn of fns) {
|
|
58
|
+
if (!current) return null;
|
|
59
|
+
current = fn(current);
|
|
60
|
+
}
|
|
61
|
+
return current;
|
|
62
|
+
}
|
|
63
|
+
__name(chainSanitizers, "chainSanitizers");
|
|
64
|
+
function shouldIgnoreUrl(url, ignoreUrls = []) {
|
|
65
|
+
for (const pattern of ignoreUrls) {
|
|
66
|
+
if (typeof pattern === "string" && url.startsWith(pattern)) return true;
|
|
67
|
+
if (pattern instanceof RegExp && pattern.test(url)) return true;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
__name(shouldIgnoreUrl, "shouldIgnoreUrl");
|
|
72
|
+
|
|
73
|
+
// src/fetch-patch.ts
|
|
74
|
+
function patchFetch(ring, opts) {
|
|
75
|
+
if (typeof globalThis.fetch !== "function") return () => {
|
|
76
|
+
};
|
|
77
|
+
const originalFetch = globalThis.fetch.bind(globalThis);
|
|
78
|
+
globalThis.fetch = /* @__PURE__ */ __name(async function patchedFetch(input, init) {
|
|
79
|
+
const url = input instanceof Request ? input.url : String(input);
|
|
80
|
+
const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
|
|
81
|
+
if (shouldIgnoreUrl(url, opts.ignoreUrls)) {
|
|
82
|
+
return originalFetch(input, init);
|
|
83
|
+
}
|
|
84
|
+
const start = Date.now();
|
|
85
|
+
let response;
|
|
86
|
+
let statusCode = null;
|
|
87
|
+
try {
|
|
88
|
+
response = await originalFetch(input, init);
|
|
89
|
+
statusCode = response.status;
|
|
90
|
+
} catch (err) {
|
|
91
|
+
const entry2 = buildEntry(url, method, null, start, opts, init);
|
|
92
|
+
recordEntry(ring, entry2, opts);
|
|
93
|
+
throw err;
|
|
94
|
+
}
|
|
95
|
+
const entry = buildEntry(url, method, statusCode, start, opts, init);
|
|
96
|
+
recordEntry(ring, entry, opts);
|
|
97
|
+
return response;
|
|
98
|
+
}, "patchedFetch");
|
|
99
|
+
return () => {
|
|
100
|
+
globalThis.fetch = originalFetch;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
__name(patchFetch, "patchFetch");
|
|
104
|
+
function buildEntry(url, method, statusCode, start, opts, init) {
|
|
105
|
+
const rawHeaders = {};
|
|
106
|
+
if (init?.headers) {
|
|
107
|
+
const headers = init.headers instanceof Headers ? init.headers : new Headers(init.headers);
|
|
108
|
+
headers.forEach((v, k) => {
|
|
109
|
+
rawHeaders[k] = v;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
method,
|
|
114
|
+
url,
|
|
115
|
+
statusCode,
|
|
116
|
+
duration: Date.now() - start,
|
|
117
|
+
requestBody: opts.recordRequestBody ? init?.body ?? null : null,
|
|
118
|
+
responseBody: null,
|
|
119
|
+
requestHeaders: rawHeaders,
|
|
120
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
__name(buildEntry, "buildEntry");
|
|
124
|
+
function recordEntry(ring, entry, opts) {
|
|
125
|
+
let sanitized = builtinRequestSanitizer(entry);
|
|
126
|
+
if (opts.requestSanitizer) {
|
|
127
|
+
sanitized = chainSanitizers(sanitized, opts.requestSanitizer);
|
|
128
|
+
}
|
|
129
|
+
if (opts.responseSanitizer) {
|
|
130
|
+
sanitized = chainSanitizers(sanitized, opts.responseSanitizer);
|
|
131
|
+
}
|
|
132
|
+
if (sanitized) ring.push(sanitized);
|
|
133
|
+
}
|
|
134
|
+
__name(recordEntry, "recordEntry");
|
|
135
|
+
|
|
136
|
+
// src/xhr-patch.ts
|
|
137
|
+
function patchXHR(ring, opts) {
|
|
138
|
+
if (typeof XMLHttpRequest === "undefined") return () => {
|
|
139
|
+
};
|
|
140
|
+
const OriginalXHR = XMLHttpRequest;
|
|
141
|
+
const originalOpen = XMLHttpRequest.prototype.open;
|
|
142
|
+
const originalSend = XMLHttpRequest.prototype.send;
|
|
143
|
+
const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
|
|
144
|
+
XMLHttpRequest.prototype.open = function(method, url, async = true, user, password) {
|
|
145
|
+
this.__it_method = method.toUpperCase();
|
|
146
|
+
this.__it_url = String(url);
|
|
147
|
+
this.__it_headers = {};
|
|
148
|
+
this.__it_start = 0;
|
|
149
|
+
return originalOpen.call(this, method, url, async, user, password);
|
|
150
|
+
};
|
|
151
|
+
XMLHttpRequest.prototype.setRequestHeader = function(name, value) {
|
|
152
|
+
if (this.__it_headers) {
|
|
153
|
+
this.__it_headers[name] = value;
|
|
154
|
+
}
|
|
155
|
+
return originalSetRequestHeader.call(this, name, value);
|
|
156
|
+
};
|
|
157
|
+
XMLHttpRequest.prototype.send = function(body) {
|
|
158
|
+
const xhrUrl = this.__it_url ?? "";
|
|
159
|
+
const method = this.__it_method ?? "GET";
|
|
160
|
+
const headers = this.__it_headers ?? {};
|
|
161
|
+
if (!shouldIgnoreUrl(xhrUrl, opts.ignoreUrls)) {
|
|
162
|
+
this.__it_start = Date.now();
|
|
163
|
+
this.addEventListener("loadend", () => {
|
|
164
|
+
const entry = {
|
|
165
|
+
method,
|
|
166
|
+
url: xhrUrl,
|
|
167
|
+
statusCode: this.status || null,
|
|
168
|
+
duration: Date.now() - this.__it_start,
|
|
169
|
+
requestBody: opts.recordRequestBody ? body ?? null : null,
|
|
170
|
+
responseBody: opts.recordResponseBody ? this.responseText ?? null : null,
|
|
171
|
+
requestHeaders: headers,
|
|
172
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
173
|
+
};
|
|
174
|
+
recordEntry2(ring, entry, opts);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
return originalSend.call(this, body);
|
|
178
|
+
};
|
|
179
|
+
return () => {
|
|
180
|
+
XMLHttpRequest.prototype.open = originalOpen;
|
|
181
|
+
XMLHttpRequest.prototype.send = originalSend;
|
|
182
|
+
XMLHttpRequest.prototype.setRequestHeader = originalSetRequestHeader;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
__name(patchXHR, "patchXHR");
|
|
186
|
+
function recordEntry2(ring, entry, opts) {
|
|
187
|
+
let sanitized = builtinRequestSanitizer(entry);
|
|
188
|
+
if (opts.requestSanitizer) sanitized = chainSanitizers(sanitized, opts.requestSanitizer);
|
|
189
|
+
if (opts.responseSanitizer && sanitized) sanitized = chainSanitizers(sanitized, opts.responseSanitizer);
|
|
190
|
+
if (sanitized) ring.push(sanitized);
|
|
191
|
+
}
|
|
192
|
+
__name(recordEntry2, "recordEntry");
|
|
193
|
+
|
|
194
|
+
// src/index.ts
|
|
195
|
+
function networkIntegration(opts = {}) {
|
|
196
|
+
const ring = new NetworkRing(opts.maxEntries ?? 20);
|
|
197
|
+
const teardowns = [];
|
|
198
|
+
const integration = {
|
|
199
|
+
name: "network",
|
|
200
|
+
setup(_client) {
|
|
201
|
+
teardowns.push(patchFetch(ring, opts));
|
|
202
|
+
teardowns.push(patchXHR(ring, opts));
|
|
203
|
+
try {
|
|
204
|
+
const errorsModule = globalThis.__instanttasks_errors_module__;
|
|
205
|
+
if (errorsModule?._registerNetworkRing) {
|
|
206
|
+
errorsModule._registerNetworkRing(() => ring.getAll());
|
|
207
|
+
}
|
|
208
|
+
} catch {
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
teardown() {
|
|
212
|
+
for (const fn of teardowns) fn();
|
|
213
|
+
teardowns.length = 0;
|
|
214
|
+
ring.clear();
|
|
215
|
+
},
|
|
216
|
+
getEntries() {
|
|
217
|
+
return ring.getAll();
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
return integration;
|
|
221
|
+
}
|
|
222
|
+
__name(networkIntegration, "networkIntegration");
|
|
223
|
+
export {
|
|
224
|
+
NetworkRing,
|
|
225
|
+
networkIntegration
|
|
226
|
+
};
|
|
227
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ring.ts","../src/sanitize.ts","../src/fetch-patch.ts","../src/xhr-patch.ts","../src/index.ts"],"sourcesContent":["import type { NetworkEntry } from './types';\n\nexport class NetworkRing {\n private readonly buf: NetworkEntry[] = [];\n private readonly max: number;\n\n constructor(max = 20) {\n this.max = max;\n }\n\n push(entry: NetworkEntry): void {\n this.buf.push(entry);\n if (this.buf.length > this.max) {\n this.buf.shift();\n }\n }\n\n getAll(): NetworkEntry[] {\n return [...this.buf];\n }\n\n size(): number {\n return this.buf.length;\n }\n\n clear(): void {\n this.buf.length = 0;\n }\n}\n","import type { NetworkEntry } from './types';\n\n/** Header names that are always redacted. */\nconst REDACTED_HEADERS = new Set(['authorization', 'cookie', 'set-cookie', 'x-auth-token']);\n\n/**\n * Builtin sanitizer: strips Authorization, Cookie, Set-Cookie headers\n * and ensures request/response bodies are dropped unless explicitly opted in.\n */\nexport function builtinRequestSanitizer(entry: NetworkEntry): NetworkEntry {\n const sanitizedHeaders: Record<string, string> = {};\n for (const [k, v] of Object.entries(entry.requestHeaders)) {\n if (REDACTED_HEADERS.has(k.toLowerCase())) {\n sanitizedHeaders[k] = '[Filtered]';\n } else {\n sanitizedHeaders[k] = v;\n }\n }\n return { ...entry, requestHeaders: sanitizedHeaders };\n}\n\n/**\n * Chain multiple sanitizers. Returns null if any returns null.\n */\nexport function chainSanitizers(\n entry: NetworkEntry,\n ...fns: Array<(e: NetworkEntry) => NetworkEntry | null>\n): NetworkEntry | null {\n let current: NetworkEntry | null = entry;\n for (const fn of fns) {\n if (!current) return null;\n current = fn(current);\n }\n return current;\n}\n\nexport function shouldIgnoreUrl(\n url: string,\n ignoreUrls: Array<string | RegExp> = [],\n): boolean {\n for (const pattern of ignoreUrls) {\n if (typeof pattern === 'string' && url.startsWith(pattern)) return true;\n if (pattern instanceof RegExp && pattern.test(url)) return true;\n }\n return false;\n}\n","import type { NetworkEntry, NetworkIntegrationOptions } from './types';\nimport { NetworkRing } from './ring';\nimport { builtinRequestSanitizer, chainSanitizers, shouldIgnoreUrl } from './sanitize';\n\nexport function patchFetch(ring: NetworkRing, opts: NetworkIntegrationOptions): () => void {\n if (typeof globalThis.fetch !== 'function') return () => {};\n\n const originalFetch = globalThis.fetch.bind(globalThis);\n\n globalThis.fetch = async function patchedFetch(\n input: RequestInfo | URL,\n init?: RequestInit,\n ): Promise<Response> {\n const url = input instanceof Request ? input.url : String(input);\n const method = (init?.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase();\n\n if (shouldIgnoreUrl(url, opts.ignoreUrls)) {\n return originalFetch(input, init);\n }\n\n const start = Date.now();\n let response: Response;\n let statusCode: number | null = null;\n\n try {\n response = await originalFetch(input, init);\n statusCode = response.status;\n } catch (err) {\n const entry = buildEntry(url, method, null, start, opts, init);\n recordEntry(ring, entry, opts);\n throw err;\n }\n\n const entry = buildEntry(url, method, statusCode, start, opts, init);\n recordEntry(ring, entry, opts);\n return response;\n };\n\n return () => {\n globalThis.fetch = originalFetch;\n };\n}\n\nfunction buildEntry(\n url: string,\n method: string,\n statusCode: number | null,\n start: number,\n opts: NetworkIntegrationOptions,\n init?: RequestInit,\n): NetworkEntry {\n const rawHeaders: Record<string, string> = {};\n if (init?.headers) {\n const headers = init.headers instanceof Headers\n ? init.headers\n : new Headers(init.headers as HeadersInit);\n headers.forEach((v, k) => { rawHeaders[k] = v; });\n }\n\n return {\n method,\n url,\n statusCode,\n duration: Date.now() - start,\n requestBody: opts.recordRequestBody ? (init?.body ?? null) : null,\n responseBody: null, // populated only if recordResponseBody + response cloned\n requestHeaders: rawHeaders,\n timestamp: new Date().toISOString(),\n };\n}\n\nfunction recordEntry(ring: NetworkRing, entry: NetworkEntry, opts: NetworkIntegrationOptions): void {\n let sanitized: NetworkEntry | null = builtinRequestSanitizer(entry);\n if (opts.requestSanitizer) {\n sanitized = chainSanitizers(sanitized, opts.requestSanitizer);\n }\n if (opts.responseSanitizer) {\n sanitized = chainSanitizers(sanitized!, opts.responseSanitizer);\n }\n if (sanitized) ring.push(sanitized);\n}\n","import type { NetworkEntry, NetworkIntegrationOptions } from './types';\nimport { NetworkRing } from './ring';\nimport { builtinRequestSanitizer, chainSanitizers, shouldIgnoreUrl } from './sanitize';\n\nexport function patchXHR(ring: NetworkRing, opts: NetworkIntegrationOptions): () => void {\n if (typeof XMLHttpRequest === 'undefined') return () => {};\n\n const OriginalXHR = XMLHttpRequest;\n\n // We extend XMLHttpRequest by monkey-patching open + send\n const originalOpen = XMLHttpRequest.prototype.open;\n const originalSend = XMLHttpRequest.prototype.send;\n const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;\n\n XMLHttpRequest.prototype.open = function (\n method: string,\n url: string | URL,\n async = true,\n user?: string | null,\n password?: string | null,\n ) {\n (this as any).__it_method = method.toUpperCase();\n (this as any).__it_url = String(url);\n (this as any).__it_headers = {};\n (this as any).__it_start = 0;\n return originalOpen.call(this, method, url, async, user, password);\n };\n\n XMLHttpRequest.prototype.setRequestHeader = function (name: string, value: string) {\n if ((this as any).__it_headers) {\n (this as any).__it_headers[name] = value;\n }\n return originalSetRequestHeader.call(this, name, value);\n };\n\n XMLHttpRequest.prototype.send = function (body?: Document | XMLHttpRequestBodyInit | null) {\n const xhrUrl: string = (this as any).__it_url ?? '';\n const method: string = (this as any).__it_method ?? 'GET';\n const headers: Record<string, string> = (this as any).__it_headers ?? {};\n\n if (!shouldIgnoreUrl(xhrUrl, opts.ignoreUrls)) {\n (this as any).__it_start = Date.now();\n\n this.addEventListener('loadend', () => {\n const entry: NetworkEntry = {\n method,\n url: xhrUrl,\n statusCode: this.status || null,\n duration: Date.now() - (this as any).__it_start,\n requestBody: opts.recordRequestBody ? body ?? null : null,\n responseBody: opts.recordResponseBody ? this.responseText ?? null : null,\n requestHeaders: headers,\n timestamp: new Date().toISOString(),\n };\n recordEntry(ring, entry, opts);\n });\n }\n\n return originalSend.call(this, body);\n };\n\n return () => {\n XMLHttpRequest.prototype.open = originalOpen;\n XMLHttpRequest.prototype.send = originalSend;\n XMLHttpRequest.prototype.setRequestHeader = originalSetRequestHeader;\n };\n}\n\nfunction recordEntry(ring: NetworkRing, entry: NetworkEntry, opts: NetworkIntegrationOptions): void {\n let sanitized: NetworkEntry | null = builtinRequestSanitizer(entry);\n if (opts.requestSanitizer) sanitized = chainSanitizers(sanitized, opts.requestSanitizer);\n if (opts.responseSanitizer && sanitized) sanitized = chainSanitizers(sanitized, opts.responseSanitizer);\n if (sanitized) ring.push(sanitized);\n}\n","import type { ClientInterface, Integration } from '@koderlabs/tasks-sdk';\nimport type { NetworkIntegrationOptions } from './types';\nimport { NetworkRing } from './ring';\nimport { patchFetch } from './fetch-patch';\nimport { patchXHR } from './xhr-patch';\n\nexport type { NetworkEntry, NetworkIntegrationOptions } from './types';\nexport { NetworkRing } from './ring';\n\nexport function networkIntegration(opts: NetworkIntegrationOptions = {}): Integration & {\n getEntries(): ReturnType<NetworkRing['getAll']>;\n} {\n const ring = new NetworkRing(opts.maxEntries ?? 20);\n const teardowns: Array<() => void> = [];\n\n const integration = {\n name: 'network',\n\n setup(_client: ClientInterface) {\n teardowns.push(patchFetch(ring, opts));\n teardowns.push(patchXHR(ring, opts));\n\n // Register ring accessor with errors integration if loaded\n try {\n // Dynamic import to avoid hard dep cycle when errors integration is absent\n const errorsModule = (globalThis as any).__instanttasks_errors_module__;\n if (errorsModule?._registerNetworkRing) {\n errorsModule._registerNetworkRing(() => ring.getAll());\n }\n } catch {\n // errors integration not loaded — fine\n }\n },\n\n teardown() {\n for (const fn of teardowns) fn();\n teardowns.length = 0;\n ring.clear();\n },\n\n getEntries() {\n return ring.getAll();\n },\n };\n\n return integration;\n}\n"],"mappings":";;;;AAEO,IAAMA,cAAN,MAAMA;EAAb,OAAaA;;;EACMC,MAAsB,CAAA;EACtBC;EAEjB,YAAYA,MAAM,IAAI;AACpB,SAAKA,MAAMA;EACb;EAEAC,KAAKC,OAA2B;AAC9B,SAAKH,IAAIE,KAAKC,KAAAA;AACd,QAAI,KAAKH,IAAII,SAAS,KAAKH,KAAK;AAC9B,WAAKD,IAAIK,MAAK;IAChB;EACF;EAEAC,SAAyB;AACvB,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AQ;MAER;IACF;IAEAC,WAAAA;AACE,iBAAWC,MAAMd,UAAWc,IAAAA;AAC5Bd,gBAAUe,SAAS;AACnBlB,WAAKmB,MAAK;IACZ;IAEAC,aAAAA;AACE,aAAOpB,KAAKe,OAAM;IACpB;EACF;AAEA,SAAOX;AACT;AArCgBN;","names":["NetworkRing","buf","max","push","entry","length","shift","getAll","size","clear","REDACTED_HEADERS","Set","builtinRequestSanitizer","entry","sanitizedHeaders","k","v","Object","entries","requestHeaders","has","toLowerCase","chainSanitizers","fns","current","fn","shouldIgnoreUrl","url","ignoreUrls","pattern","startsWith","RegExp","test","patchFetch","ring","opts","globalThis","fetch","originalFetch","bind","patchedFetch","input","init","url","Request","String","method","toUpperCase","shouldIgnoreUrl","ignoreUrls","start","Date","now","response","statusCode","status","err","entry","buildEntry","recordEntry","rawHeaders","headers","Headers","forEach","v","k","duration","requestBody","recordRequestBody","body","responseBody","requestHeaders","timestamp","toISOString","sanitized","builtinRequestSanitizer","requestSanitizer","chainSanitizers","responseSanitizer","push","patchXHR","ring","opts","XMLHttpRequest","OriginalXHR","originalOpen","prototype","open","originalSend","send","originalSetRequestHeader","setRequestHeader","method","url","async","user","password","__it_method","toUpperCase","__it_url","String","__it_headers","__it_start","call","name","value","body","xhrUrl","headers","shouldIgnoreUrl","ignoreUrls","Date","now","addEventListener","entry","statusCode","status","duration","requestBody","recordRequestBody","responseBody","recordResponseBody","responseText","requestHeaders","timestamp","toISOString","recordEntry","sanitized","builtinRequestSanitizer","requestSanitizer","chainSanitizers","responseSanitizer","push","networkIntegration","opts","ring","NetworkRing","maxEntries","teardowns","integration","name","setup","_client","push","patchFetch","patchXHR","errorsModule","globalThis","__instanttasks_errors_module__","_registerNetworkRing","getAll","teardown","fn","length","clear","getEntries"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@koderlabs/tasks-sdk-web-network",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Browser network capture for the InstantTasks SDK — fetch + XMLHttpRequest.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"instanttasks",
|
|
7
|
+
"sdk",
|
|
8
|
+
"browser",
|
|
9
|
+
"network"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/jawaidgadiwala/instant-tasks/tree/main/packages/sdk/web-network#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/jawaidgadiwala/instant-tasks/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/jawaidgadiwala/instant-tasks.git",
|
|
18
|
+
"directory": "packages/sdk/web-network"
|
|
19
|
+
},
|
|
20
|
+
"license": "UNLICENSED",
|
|
21
|
+
"author": {
|
|
22
|
+
"name": "Jawaid Gadiwala",
|
|
23
|
+
"email": "jawaidgadiwala@gmail.com",
|
|
24
|
+
"url": "https://koderlabs.com"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "./dist/index.cjs",
|
|
28
|
+
"module": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.js",
|
|
34
|
+
"require": "./dist/index.cjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"sideEffects": false,
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@koderlabs/tasks-sdk": "0.1.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"tsup": "^8.3.0",
|
|
47
|
+
"typescript": "^5.5.0",
|
|
48
|
+
"vitest": "^2.1.0"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=20"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "restricted"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsup",
|
|
58
|
+
"dev": "tsup --watch",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"test:watch": "vitest"
|
|
61
|
+
}
|
|
62
|
+
}
|