@amaster.ai/vite-plugins 1.0.0-beta.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 +21 -0
- package/README.md +183 -0
- package/dist/index.cjs +876 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +866 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,876 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var path = require('path');
|
|
5
|
+
var crypto = require('crypto');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
10
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
11
|
+
|
|
12
|
+
// src/browser-logs.ts
|
|
13
|
+
function browserLogsPlugin() {
|
|
14
|
+
let logFilePath = "";
|
|
15
|
+
const injectedScript = `
|
|
16
|
+
<script>
|
|
17
|
+
(function() {
|
|
18
|
+
'use strict';
|
|
19
|
+
|
|
20
|
+
// Log API path (provided by Vite dev server)
|
|
21
|
+
var LOG_API_PATH = '/__browser__';
|
|
22
|
+
|
|
23
|
+
// Write queue to ensure sequential writes
|
|
24
|
+
var writeQueue = [];
|
|
25
|
+
var isWriting = false;
|
|
26
|
+
|
|
27
|
+
// Process write queue to ensure sequential writes
|
|
28
|
+
function processWriteQueue() {
|
|
29
|
+
if (isWriting || writeQueue.length === 0) return;
|
|
30
|
+
|
|
31
|
+
isWriting = true;
|
|
32
|
+
var entry = writeQueue.shift();
|
|
33
|
+
var logText = JSON.stringify(entry);
|
|
34
|
+
|
|
35
|
+
fetch(LOG_API_PATH, {
|
|
36
|
+
method: 'POST',
|
|
37
|
+
headers: { 'Content-Type': 'application/json' },
|
|
38
|
+
body: logText
|
|
39
|
+
})
|
|
40
|
+
.then(function(response) {
|
|
41
|
+
isWriting = false;
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
console.warn('[BrowserLogs] Failed to write log:', response.status);
|
|
44
|
+
}
|
|
45
|
+
// Continue processing next item in queue
|
|
46
|
+
processWriteQueue();
|
|
47
|
+
})
|
|
48
|
+
.catch(function(error) {
|
|
49
|
+
console.warn('[BrowserLogs] Failed to write log:', error.message);
|
|
50
|
+
// On failure, put log back to queue head for retry
|
|
51
|
+
writeQueue.unshift(entry);
|
|
52
|
+
isWriting = false;
|
|
53
|
+
// Retry after delay
|
|
54
|
+
setTimeout(processWriteQueue, 1000);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Limit headers object size
|
|
59
|
+
function truncateHeaders(headers) {
|
|
60
|
+
if (!headers) return headers;
|
|
61
|
+
var jsonStr = JSON.stringify(headers);
|
|
62
|
+
if (jsonStr.length <= MAX_HEADER_SIZE) return headers;
|
|
63
|
+
return '[Headers truncated, size: ' + jsonStr.length + ']';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Truncate oversized log entries
|
|
67
|
+
function truncateLogEntry(entry) {
|
|
68
|
+
var jsonStr = JSON.stringify(entry);
|
|
69
|
+
if (jsonStr.length <= MAX_LOG_ENTRY_SIZE) {
|
|
70
|
+
return entry;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// If oversized, progressively truncate non-critical fields
|
|
74
|
+
var truncated = Object.assign({}, entry);
|
|
75
|
+
|
|
76
|
+
// 1. Truncate response body first
|
|
77
|
+
if (truncated.responseBody && typeof truncated.responseBody === 'string') {
|
|
78
|
+
truncated.responseBody = truncated.responseBody.substring(0, 500) + '... [truncated]';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 2. Truncate request body
|
|
82
|
+
if (truncated.requestBody && typeof truncated.requestBody === 'string') {
|
|
83
|
+
truncated.requestBody = truncated.requestBody.substring(0, 500) + '... [truncated]';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 3. Truncate message
|
|
87
|
+
if (truncated.message && typeof truncated.message === 'string' && truncated.message.length > 1000) {
|
|
88
|
+
truncated.message = truncated.message.substring(0, 1000) + '... [truncated]';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 4. Truncate stack
|
|
92
|
+
if (truncated.stack && Array.isArray(truncated.stack) && truncated.stack.length > 3) {
|
|
93
|
+
truncated.stack = truncated.stack.slice(0, 3);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Add truncation marker
|
|
97
|
+
truncated._truncated = true;
|
|
98
|
+
truncated._originalSize = jsonStr.length;
|
|
99
|
+
|
|
100
|
+
return truncated;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Add log and send immediately (ensure order)
|
|
104
|
+
function addLog(entry) {
|
|
105
|
+
var truncatedEntry = truncateLogEntry(entry);
|
|
106
|
+
writeQueue.push(truncatedEntry);
|
|
107
|
+
processWriteQueue();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ============================================
|
|
111
|
+
// Console log collection
|
|
112
|
+
// ============================================
|
|
113
|
+
var originalConsole = {
|
|
114
|
+
log: console.log,
|
|
115
|
+
info: console.info,
|
|
116
|
+
warn: console.warn,
|
|
117
|
+
error: console.error,
|
|
118
|
+
debug: console.debug
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
function getStackTrace() {
|
|
122
|
+
var stack = new Error().stack || '';
|
|
123
|
+
var lines = stack.split('\\n').slice(3);
|
|
124
|
+
// Limit stack lines
|
|
125
|
+
return lines.slice(0, MAX_STACK_LINES).map(function(line) { return line.trim(); });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function formatMessage(args) {
|
|
129
|
+
return Array.from(args).map(function(arg) {
|
|
130
|
+
if (arg === null) return 'null';
|
|
131
|
+
if (arg === undefined) return 'undefined';
|
|
132
|
+
if (typeof arg === 'object') {
|
|
133
|
+
try {
|
|
134
|
+
return JSON.stringify(arg);
|
|
135
|
+
} catch (e) {
|
|
136
|
+
return String(arg);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return String(arg);
|
|
140
|
+
}).join(' ');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function createLogEntry(level, args) {
|
|
144
|
+
return {
|
|
145
|
+
type: 'console',
|
|
146
|
+
timestamp: new Date().toISOString(),
|
|
147
|
+
level: level,
|
|
148
|
+
message: formatMessage(args),
|
|
149
|
+
stack: getStackTrace()
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Check if message should be filtered (contains [vite] text)
|
|
154
|
+
function shouldFilterConsoleLog(args) {
|
|
155
|
+
for (var i = 0; i < args.length; i++) {
|
|
156
|
+
var arg = args[i];
|
|
157
|
+
if (typeof arg === 'string' && arg.indexOf('[vite]') !== -1) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function wrapConsoleMethod(method, level) {
|
|
165
|
+
return function() {
|
|
166
|
+
var args = arguments;
|
|
167
|
+
// Filter out logs containing [vite]
|
|
168
|
+
if (shouldFilterConsoleLog(args)) {
|
|
169
|
+
return originalConsole[method].apply(console, args);
|
|
170
|
+
}
|
|
171
|
+
var entry = createLogEntry(level, args);
|
|
172
|
+
addLog(entry);
|
|
173
|
+
return originalConsole[method].apply(console, args);
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
console.log = wrapConsoleMethod('log', 'log');
|
|
178
|
+
console.info = wrapConsoleMethod('info', 'info');
|
|
179
|
+
console.warn = wrapConsoleMethod('warn', 'warn');
|
|
180
|
+
console.error = wrapConsoleMethod('error', 'error');
|
|
181
|
+
console.debug = wrapConsoleMethod('debug', 'debug');
|
|
182
|
+
|
|
183
|
+
// ============================================
|
|
184
|
+
// Network request collection (exclude SSE and log API requests)
|
|
185
|
+
// ============================================
|
|
186
|
+
|
|
187
|
+
var MAX_BODY_SIZE = 2000;
|
|
188
|
+
var MAX_LOG_ENTRY_SIZE = 3000; // Max single log entry length
|
|
189
|
+
var MAX_HEADER_SIZE = 500; // Max single header object length
|
|
190
|
+
var MAX_STACK_LINES = 5; // Max stack lines to keep
|
|
191
|
+
const FILTERED_URLS = ['/__browser__', '/builtin']; // Filter out log API and Vite built-in requests
|
|
192
|
+
|
|
193
|
+
function isSSERequest(url, headers) {
|
|
194
|
+
var sseUrlPatterns = ['/events', '/sse', '/stream', 'text/event-stream'];
|
|
195
|
+
var urlStr = String(url).toLowerCase();
|
|
196
|
+
for (var i = 0; i < sseUrlPatterns.length; i++) {
|
|
197
|
+
if (urlStr.indexOf(sseUrlPatterns[i]) !== -1) {
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (headers) {
|
|
203
|
+
if (typeof headers.get === 'function') {
|
|
204
|
+
var accept = headers.get('Accept') || headers.get('accept');
|
|
205
|
+
if (accept && accept.indexOf('text/event-stream') !== -1) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
} else if (typeof headers === 'object') {
|
|
209
|
+
for (var key in headers) {
|
|
210
|
+
if (key.toLowerCase() === 'accept' && headers[key].indexOf('text/event-stream') !== -1) {
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function shouldFilterRequest(url) {
|
|
221
|
+
return FILTERED_URLS.some(function(filteredUrl) {
|
|
222
|
+
return String(url).indexOf(filteredUrl) !== -1;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function formatRequestBody(body) {
|
|
227
|
+
if (!body) return null;
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
if (typeof body === 'string') {
|
|
231
|
+
return body.substring(0, MAX_BODY_SIZE);
|
|
232
|
+
}
|
|
233
|
+
if (body instanceof FormData) {
|
|
234
|
+
var formDataObj = {};
|
|
235
|
+
body.forEach(function(value, key) {
|
|
236
|
+
if (value instanceof File) {
|
|
237
|
+
formDataObj[key] = '[File: ' + value.name + ', size: ' + value.size + ']';
|
|
238
|
+
} else {
|
|
239
|
+
formDataObj[key] = String(value).substring(0, 1000);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
return JSON.stringify(formDataObj);
|
|
243
|
+
}
|
|
244
|
+
if (body instanceof Blob) {
|
|
245
|
+
return '[Blob: size=' + body.size + ', type=' + body.type + ']';
|
|
246
|
+
}
|
|
247
|
+
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
|
|
248
|
+
return '[Binary: size=' + body.byteLength + ']';
|
|
249
|
+
}
|
|
250
|
+
if (body instanceof URLSearchParams) {
|
|
251
|
+
return body.toString().substring(0, MAX_BODY_SIZE);
|
|
252
|
+
}
|
|
253
|
+
return String(body).substring(0, MAX_BODY_SIZE);
|
|
254
|
+
} catch (e) {
|
|
255
|
+
return '[Error reading body: ' + e.message + ']';
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function formatResponseBody(response, responseType) {
|
|
260
|
+
if (!response) return null;
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
if (responseType === '' || responseType === 'text') {
|
|
264
|
+
return String(response).substring(0, MAX_BODY_SIZE);
|
|
265
|
+
}
|
|
266
|
+
if (responseType === 'json') {
|
|
267
|
+
return JSON.stringify(response).substring(0, MAX_BODY_SIZE);
|
|
268
|
+
}
|
|
269
|
+
if (responseType === 'document') {
|
|
270
|
+
return '[Document]';
|
|
271
|
+
}
|
|
272
|
+
if (responseType === 'blob') {
|
|
273
|
+
return '[Blob: size=' + response.size + ']';
|
|
274
|
+
}
|
|
275
|
+
if (responseType === 'arraybuffer') {
|
|
276
|
+
return '[ArrayBuffer: size=' + response.byteLength + ']';
|
|
277
|
+
}
|
|
278
|
+
return String(response).substring(0, MAX_BODY_SIZE);
|
|
279
|
+
} catch (e) {
|
|
280
|
+
return '[Error reading response: ' + e.message + ']';
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Intercept XMLHttpRequest
|
|
285
|
+
var originalXHROpen = XMLHttpRequest.prototype.open;
|
|
286
|
+
var originalXHRSend = XMLHttpRequest.prototype.send;
|
|
287
|
+
var originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
|
|
288
|
+
|
|
289
|
+
XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
|
|
290
|
+
this.__requestInfo__ = {
|
|
291
|
+
method: method,
|
|
292
|
+
url: url,
|
|
293
|
+
startTime: null,
|
|
294
|
+
headers: {}
|
|
295
|
+
};
|
|
296
|
+
return originalXHROpen.apply(this, arguments);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
XMLHttpRequest.prototype.setRequestHeader = function(name, value) {
|
|
300
|
+
if (this.__requestInfo__ && this.__requestInfo__.headers) {
|
|
301
|
+
this.__requestInfo__.headers[name] = value;
|
|
302
|
+
}
|
|
303
|
+
return originalXHRSetRequestHeader.apply(this, arguments);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
XMLHttpRequest.prototype.send = function(body) {
|
|
307
|
+
var xhr = this;
|
|
308
|
+
var requestInfo = xhr.__requestInfo__;
|
|
309
|
+
|
|
310
|
+
if (requestInfo) {
|
|
311
|
+
// Skip SSE requests and filtered requests
|
|
312
|
+
if (isSSERequest(requestInfo.url, requestInfo.headers) || shouldFilterRequest(requestInfo.url)) {
|
|
313
|
+
return originalXHRSend.apply(this, arguments);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
requestInfo.startTime = Date.now();
|
|
317
|
+
requestInfo.requestBody = formatRequestBody(body);
|
|
318
|
+
|
|
319
|
+
xhr.addEventListener('loadend', function() {
|
|
320
|
+
var contentType = xhr.getResponseHeader('Content-Type') || '';
|
|
321
|
+
if (contentType.indexOf('text/event-stream') !== -1) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
var responseHeaders = {};
|
|
326
|
+
var allHeaders = xhr.getAllResponseHeaders();
|
|
327
|
+
if (allHeaders) {
|
|
328
|
+
allHeaders.split('\\r\\n').forEach(function(line) {
|
|
329
|
+
var parts = line.split(': ');
|
|
330
|
+
if (parts.length === 2) {
|
|
331
|
+
responseHeaders[parts[0]] = parts[1];
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
var entry = {
|
|
337
|
+
type: 'request',
|
|
338
|
+
timestamp: new Date().toISOString(),
|
|
339
|
+
requestType: 'xhr',
|
|
340
|
+
method: requestInfo.method,
|
|
341
|
+
url: requestInfo.url,
|
|
342
|
+
status: xhr.status,
|
|
343
|
+
statusText: xhr.statusText,
|
|
344
|
+
duration: Date.now() - requestInfo.startTime,
|
|
345
|
+
requestHeaders: truncateHeaders(requestInfo.headers),
|
|
346
|
+
requestBody: requestInfo.requestBody,
|
|
347
|
+
responseHeaders: truncateHeaders(responseHeaders),
|
|
348
|
+
responseType: xhr.responseType,
|
|
349
|
+
responseBody: formatResponseBody(xhr.response, xhr.responseType),
|
|
350
|
+
responseSize: xhr.response ? (typeof xhr.response === 'string' ? xhr.response.length : (xhr.response.byteLength || xhr.response.size || null)) : null
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
addLog(entry);
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return originalXHRSend.apply(this, arguments);
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// Intercept Fetch API
|
|
361
|
+
var originalFetch = window.fetch;
|
|
362
|
+
|
|
363
|
+
function headersToObject(headers) {
|
|
364
|
+
var obj = {};
|
|
365
|
+
if (!headers) return obj;
|
|
366
|
+
|
|
367
|
+
if (typeof headers.forEach === 'function') {
|
|
368
|
+
headers.forEach(function(value, key) {
|
|
369
|
+
obj[key] = value;
|
|
370
|
+
});
|
|
371
|
+
} else if (typeof headers === 'object') {
|
|
372
|
+
for (var key in headers) {
|
|
373
|
+
obj[key] = headers[key];
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return obj;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
window.fetch = function(input, init) {
|
|
380
|
+
var startTime = Date.now();
|
|
381
|
+
var method = (init && init.method) || 'GET';
|
|
382
|
+
var url = typeof input === 'string' ? input : input.url;
|
|
383
|
+
var headers = init && init.headers;
|
|
384
|
+
|
|
385
|
+
// Skip SSE requests and filtered requests
|
|
386
|
+
if (isSSERequest(url, headers) || shouldFilterRequest(url)) {
|
|
387
|
+
return originalFetch.apply(this, arguments);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
var requestHeaders = headersToObject(headers);
|
|
391
|
+
var requestBody = formatRequestBody(init && init.body);
|
|
392
|
+
|
|
393
|
+
return originalFetch.apply(this, arguments)
|
|
394
|
+
.then(function(response) {
|
|
395
|
+
var contentType = response.headers.get('Content-Type') || '';
|
|
396
|
+
if (contentType.indexOf('text/event-stream') !== -1) {
|
|
397
|
+
return response;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
var clonedResponse = response.clone();
|
|
401
|
+
var responseHeaders = headersToObject(response.headers);
|
|
402
|
+
|
|
403
|
+
clonedResponse.text().then(function(bodyText) {
|
|
404
|
+
var entry = {
|
|
405
|
+
type: 'request',
|
|
406
|
+
timestamp: new Date().toISOString(),
|
|
407
|
+
requestType: 'fetch',
|
|
408
|
+
method: method,
|
|
409
|
+
url: url,
|
|
410
|
+
status: response.status,
|
|
411
|
+
statusText: response.statusText,
|
|
412
|
+
duration: Date.now() - startTime,
|
|
413
|
+
requestHeaders: truncateHeaders(requestHeaders),
|
|
414
|
+
requestBody: requestBody,
|
|
415
|
+
responseHeaders: truncateHeaders(responseHeaders),
|
|
416
|
+
responseBody: bodyText ? bodyText.substring(0, MAX_BODY_SIZE) : null,
|
|
417
|
+
responseSize: bodyText ? bodyText.length : null,
|
|
418
|
+
ok: response.ok
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
addLog(entry);
|
|
422
|
+
}).catch(function(e) {
|
|
423
|
+
var entry = {
|
|
424
|
+
type: 'request',
|
|
425
|
+
timestamp: new Date().toISOString(),
|
|
426
|
+
requestType: 'fetch',
|
|
427
|
+
method: method,
|
|
428
|
+
url: url,
|
|
429
|
+
status: response.status,
|
|
430
|
+
statusText: response.statusText,
|
|
431
|
+
duration: Date.now() - startTime,
|
|
432
|
+
requestHeaders: truncateHeaders(requestHeaders),
|
|
433
|
+
requestBody: requestBody,
|
|
434
|
+
responseHeaders: truncateHeaders(responseHeaders),
|
|
435
|
+
responseBody: '[Unable to read: ' + e.message + ']',
|
|
436
|
+
responseSize: null,
|
|
437
|
+
ok: response.ok
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
addLog(entry);
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
return response;
|
|
444
|
+
})
|
|
445
|
+
.catch(function(error) {
|
|
446
|
+
var entry = {
|
|
447
|
+
type: 'request',
|
|
448
|
+
timestamp: new Date().toISOString(),
|
|
449
|
+
requestType: 'fetch',
|
|
450
|
+
method: method,
|
|
451
|
+
url: url,
|
|
452
|
+
status: 0,
|
|
453
|
+
statusText: 'Network Error',
|
|
454
|
+
duration: Date.now() - startTime,
|
|
455
|
+
requestHeaders: truncateHeaders(requestHeaders),
|
|
456
|
+
requestBody: requestBody,
|
|
457
|
+
responseHeaders: null,
|
|
458
|
+
responseBody: null,
|
|
459
|
+
error: error.message
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
addLog(entry);
|
|
463
|
+
throw error;
|
|
464
|
+
});
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
// ============================================
|
|
468
|
+
// Global error capture
|
|
469
|
+
// ============================================
|
|
470
|
+
window.addEventListener('error', function(event) {
|
|
471
|
+
var entry = {
|
|
472
|
+
type: 'error',
|
|
473
|
+
timestamp: new Date().toISOString(),
|
|
474
|
+
level: 'error',
|
|
475
|
+
message: event.message,
|
|
476
|
+
stack: event.error ? event.error.stack : 'at ' + event.filename + ':' + event.lineno + ':' + event.colno
|
|
477
|
+
};
|
|
478
|
+
addLog(entry);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
window.addEventListener('unhandledrejection', function(event) {
|
|
482
|
+
var entry = {
|
|
483
|
+
type: 'error',
|
|
484
|
+
timestamp: new Date().toISOString(),
|
|
485
|
+
level: 'error',
|
|
486
|
+
message: 'Unhandled Promise Rejection: ' + (event.reason ? (event.reason.message || String(event.reason)) : 'Unknown'),
|
|
487
|
+
stack: event.reason && event.reason.stack ? event.reason.stack : ''
|
|
488
|
+
};
|
|
489
|
+
addLog(entry);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
// ============================================
|
|
493
|
+
// Send remaining logs on page unload
|
|
494
|
+
// ============================================
|
|
495
|
+
window.addEventListener('beforeunload', function() {
|
|
496
|
+
if (writeQueue.length > 0) {
|
|
497
|
+
// Use sendBeacon to ensure logs are sent
|
|
498
|
+
writeQueue.forEach(function(entry) {
|
|
499
|
+
navigator.sendBeacon(LOG_API_PATH, JSON.stringify(entry));
|
|
500
|
+
});
|
|
501
|
+
writeQueue = [];
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
// ============================================
|
|
506
|
+
// Provide manual flush method
|
|
507
|
+
// ============================================
|
|
508
|
+
window.__flushBrowserLogs__ = function() {
|
|
509
|
+
return new Promise(function(resolve) {
|
|
510
|
+
// Wait for queue to finish processing
|
|
511
|
+
function checkQueue() {
|
|
512
|
+
if (writeQueue.length === 0 && !isWriting) {
|
|
513
|
+
resolve();
|
|
514
|
+
} else {
|
|
515
|
+
setTimeout(checkQueue, 100);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
checkQueue();
|
|
519
|
+
});
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
// Provide method to get queue status
|
|
523
|
+
window.__getBrowserLogsStatus__ = function() {
|
|
524
|
+
return {
|
|
525
|
+
queueLength: writeQueue.length,
|
|
526
|
+
isWriting: isWriting
|
|
527
|
+
};
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
originalConsole.log('[BrowserLogs] Log collection started');
|
|
531
|
+
})();
|
|
532
|
+
</script>`;
|
|
533
|
+
return {
|
|
534
|
+
name: "vite-plugin-browser-logs",
|
|
535
|
+
configResolved(config) {
|
|
536
|
+
const root = config.root || process.cwd();
|
|
537
|
+
logFilePath = path__default.default.join(root, "browser.log");
|
|
538
|
+
},
|
|
539
|
+
configureServer(devServer) {
|
|
540
|
+
devServer.middlewares.use((req, res, next) => {
|
|
541
|
+
if (req.url === "/__browser__" && req.method === "POST") {
|
|
542
|
+
const origin = req.headers.origin || "*";
|
|
543
|
+
let body = "";
|
|
544
|
+
req.on("data", (chunk) => {
|
|
545
|
+
body += chunk.toString();
|
|
546
|
+
});
|
|
547
|
+
req.on("end", () => {
|
|
548
|
+
try {
|
|
549
|
+
const logDir = path__default.default.dirname(logFilePath);
|
|
550
|
+
if (!fs__default.default.existsSync(logDir)) {
|
|
551
|
+
fs__default.default.mkdirSync(logDir, { recursive: true });
|
|
552
|
+
}
|
|
553
|
+
fs__default.default.appendFileSync(logFilePath, `${body}
|
|
554
|
+
`, "utf-8");
|
|
555
|
+
res.writeHead(200, {
|
|
556
|
+
"Content-Type": "application/json",
|
|
557
|
+
"Access-Control-Allow-Origin": origin,
|
|
558
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
559
|
+
"Access-Control-Allow-Headers": "Content-Type"
|
|
560
|
+
});
|
|
561
|
+
res.end(JSON.stringify({ success: true }));
|
|
562
|
+
} catch (error) {
|
|
563
|
+
console.error("[BrowserLogs] Write error:", error);
|
|
564
|
+
res.writeHead(500, {
|
|
565
|
+
"Content-Type": "application/json",
|
|
566
|
+
"Access-Control-Allow-Origin": origin,
|
|
567
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
568
|
+
"Access-Control-Allow-Headers": "Content-Type"
|
|
569
|
+
});
|
|
570
|
+
res.end(JSON.stringify({ success: false, error: String(error) }));
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
} else if (req.url === "/__browser__" && req.method === "OPTIONS") {
|
|
574
|
+
const origin = req.headers.origin || "*";
|
|
575
|
+
res.writeHead(204, {
|
|
576
|
+
"Access-Control-Allow-Origin": origin,
|
|
577
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
578
|
+
"Access-Control-Allow-Headers": "Content-Type",
|
|
579
|
+
"Access-Control-Max-Age": "86400"
|
|
580
|
+
});
|
|
581
|
+
res.end();
|
|
582
|
+
} else if (req.url === "/__browser__") {
|
|
583
|
+
const origin = req.headers.origin || "*";
|
|
584
|
+
res.writeHead(405, {
|
|
585
|
+
"Content-Type": "application/json",
|
|
586
|
+
"Access-Control-Allow-Origin": origin
|
|
587
|
+
});
|
|
588
|
+
res.end(JSON.stringify({ error: "Method not allowed" }));
|
|
589
|
+
} else {
|
|
590
|
+
next();
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
console.log("[BrowserLogs] Logs will be written to:", logFilePath);
|
|
594
|
+
},
|
|
595
|
+
transformIndexHtml(html) {
|
|
596
|
+
return html.replace(/<head([^>]*)>/i, `<head$1>${injectedScript}`);
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
function generateUniqueId(filePath, position) {
|
|
601
|
+
const key = `${filePath}:${position}`;
|
|
602
|
+
return crypto.createHash("md5").update(key).digest("hex").substring(0, 12);
|
|
603
|
+
}
|
|
604
|
+
var HTML_TAGS = /* @__PURE__ */ new Set([
|
|
605
|
+
"a",
|
|
606
|
+
"abbr",
|
|
607
|
+
"address",
|
|
608
|
+
"area",
|
|
609
|
+
"article",
|
|
610
|
+
"aside",
|
|
611
|
+
"audio",
|
|
612
|
+
"b",
|
|
613
|
+
"base",
|
|
614
|
+
"bdi",
|
|
615
|
+
"bdo",
|
|
616
|
+
"blockquote",
|
|
617
|
+
"body",
|
|
618
|
+
"br",
|
|
619
|
+
"button",
|
|
620
|
+
"canvas",
|
|
621
|
+
"caption",
|
|
622
|
+
"cite",
|
|
623
|
+
"code",
|
|
624
|
+
"col",
|
|
625
|
+
"colgroup",
|
|
626
|
+
"data",
|
|
627
|
+
"datalist",
|
|
628
|
+
"dd",
|
|
629
|
+
"del",
|
|
630
|
+
"details",
|
|
631
|
+
"dfn",
|
|
632
|
+
"dialog",
|
|
633
|
+
"div",
|
|
634
|
+
"dl",
|
|
635
|
+
"dt",
|
|
636
|
+
"em",
|
|
637
|
+
"embed",
|
|
638
|
+
"fieldset",
|
|
639
|
+
"figcaption",
|
|
640
|
+
"figure",
|
|
641
|
+
"footer",
|
|
642
|
+
"form",
|
|
643
|
+
"h1",
|
|
644
|
+
"h2",
|
|
645
|
+
"h3",
|
|
646
|
+
"h4",
|
|
647
|
+
"h5",
|
|
648
|
+
"h6",
|
|
649
|
+
"head",
|
|
650
|
+
"header",
|
|
651
|
+
"hgroup",
|
|
652
|
+
"hr",
|
|
653
|
+
"html",
|
|
654
|
+
"i",
|
|
655
|
+
"iframe",
|
|
656
|
+
"img",
|
|
657
|
+
"input",
|
|
658
|
+
"ins",
|
|
659
|
+
"kbd",
|
|
660
|
+
"label",
|
|
661
|
+
"legend",
|
|
662
|
+
"li",
|
|
663
|
+
"link",
|
|
664
|
+
"main",
|
|
665
|
+
"map",
|
|
666
|
+
"mark",
|
|
667
|
+
"meta",
|
|
668
|
+
"meter",
|
|
669
|
+
"nav",
|
|
670
|
+
"noscript",
|
|
671
|
+
"object",
|
|
672
|
+
"ol",
|
|
673
|
+
"optgroup",
|
|
674
|
+
"option",
|
|
675
|
+
"output",
|
|
676
|
+
"p",
|
|
677
|
+
"param",
|
|
678
|
+
"picture",
|
|
679
|
+
"pre",
|
|
680
|
+
"progress",
|
|
681
|
+
"q",
|
|
682
|
+
"rp",
|
|
683
|
+
"rt",
|
|
684
|
+
"ruby",
|
|
685
|
+
"s",
|
|
686
|
+
"samp",
|
|
687
|
+
"script",
|
|
688
|
+
"section",
|
|
689
|
+
"select",
|
|
690
|
+
"small",
|
|
691
|
+
"source",
|
|
692
|
+
"span",
|
|
693
|
+
"strong",
|
|
694
|
+
"style",
|
|
695
|
+
"sub",
|
|
696
|
+
"summary",
|
|
697
|
+
"sup",
|
|
698
|
+
"svg",
|
|
699
|
+
"table",
|
|
700
|
+
"tbody",
|
|
701
|
+
"td",
|
|
702
|
+
"template",
|
|
703
|
+
"textarea",
|
|
704
|
+
"tfoot",
|
|
705
|
+
"th",
|
|
706
|
+
"thead",
|
|
707
|
+
"time",
|
|
708
|
+
"title",
|
|
709
|
+
"tr",
|
|
710
|
+
"track",
|
|
711
|
+
"u",
|
|
712
|
+
"ul",
|
|
713
|
+
"var",
|
|
714
|
+
"video",
|
|
715
|
+
"wbr"
|
|
716
|
+
]);
|
|
717
|
+
function componentIdPlugin() {
|
|
718
|
+
let isDev = false;
|
|
719
|
+
return {
|
|
720
|
+
name: "vite-plugin-component-id",
|
|
721
|
+
enforce: "pre",
|
|
722
|
+
configResolved(config) {
|
|
723
|
+
isDev = config.command === "serve";
|
|
724
|
+
},
|
|
725
|
+
transform(code, id) {
|
|
726
|
+
if (!isDev) {
|
|
727
|
+
return null;
|
|
728
|
+
}
|
|
729
|
+
if (!/\.(tsx|jsx)$/.test(id)) {
|
|
730
|
+
return null;
|
|
731
|
+
}
|
|
732
|
+
if (id.includes("node_modules")) {
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
if (!/<[a-z]/.test(code)) {
|
|
736
|
+
return null;
|
|
737
|
+
}
|
|
738
|
+
try {
|
|
739
|
+
let transformedCode = code;
|
|
740
|
+
const jsxOpenTagRegex = /<([a-z][\da-z]*)(?=[\s/>])/g;
|
|
741
|
+
let match;
|
|
742
|
+
const matches = [];
|
|
743
|
+
while ((match = jsxOpenTagRegex.exec(code)) !== null) {
|
|
744
|
+
const tag = match[1];
|
|
745
|
+
if (!tag) continue;
|
|
746
|
+
const tagStartIndex = match.index;
|
|
747
|
+
const tagEndIndex = match.index + match[0].length;
|
|
748
|
+
if (!HTML_TAGS.has(tag)) {
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
let checkIndex = tagEndIndex;
|
|
752
|
+
let foundClosing = false;
|
|
753
|
+
let hasComponentId = false;
|
|
754
|
+
while (checkIndex < code.length && !foundClosing) {
|
|
755
|
+
const char = code[checkIndex];
|
|
756
|
+
if (char === ">") {
|
|
757
|
+
foundClosing = true;
|
|
758
|
+
const tagContent = code.substring(tagStartIndex, checkIndex);
|
|
759
|
+
if (tagContent.includes("data-node-component-id")) {
|
|
760
|
+
hasComponentId = true;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
checkIndex++;
|
|
764
|
+
}
|
|
765
|
+
if (hasComponentId) {
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
matches.push({
|
|
769
|
+
index: tagStartIndex,
|
|
770
|
+
tag,
|
|
771
|
+
tagEndIndex
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
let currentCode = code;
|
|
775
|
+
for (let i = matches.length - 1; i >= 0; i--) {
|
|
776
|
+
const item = matches[i];
|
|
777
|
+
if (!item) continue;
|
|
778
|
+
const { index, tagEndIndex } = item;
|
|
779
|
+
const uniqueId = generateUniqueId(id, index);
|
|
780
|
+
const before = currentCode.substring(0, tagEndIndex);
|
|
781
|
+
const after = currentCode.substring(tagEndIndex);
|
|
782
|
+
transformedCode = `${before} data-node-component-id="${uniqueId}"${after}`;
|
|
783
|
+
currentCode = transformedCode;
|
|
784
|
+
}
|
|
785
|
+
return {
|
|
786
|
+
code: transformedCode,
|
|
787
|
+
map: null
|
|
788
|
+
};
|
|
789
|
+
} catch {
|
|
790
|
+
return null;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// src/editor-bridge.ts
|
|
797
|
+
var clickHandlerScript = `<script>
|
|
798
|
+
document.addEventListener("click", (e) => {
|
|
799
|
+
const element = e.target;
|
|
800
|
+
const noJumpOut = document.body.classList.contains("forbid-jump-out")
|
|
801
|
+
if (element.hasAttribute("data-link-href") && !noJumpOut) {
|
|
802
|
+
const href = element.getAttribute("data-link-href");
|
|
803
|
+
const target = element.getAttribute("data-link-target");
|
|
804
|
+
if (href) {
|
|
805
|
+
if (target === "_blank") {
|
|
806
|
+
window.open(href, "_blank");
|
|
807
|
+
} else {
|
|
808
|
+
window.location.href = href;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
} else if(noJumpOut && element.tagName === "A") {
|
|
812
|
+
e.preventDefault();
|
|
813
|
+
}
|
|
814
|
+
});
|
|
815
|
+
</script>`;
|
|
816
|
+
function editorBridgePlugin(options) {
|
|
817
|
+
let isDev = false;
|
|
818
|
+
const bridgePath = options?.bridgeScriptPath || "/scripts/bridge.js";
|
|
819
|
+
return {
|
|
820
|
+
name: "vite-plugin-editor-bridge",
|
|
821
|
+
configResolved(config) {
|
|
822
|
+
isDev = config.command === "serve";
|
|
823
|
+
},
|
|
824
|
+
transformIndexHtml(html) {
|
|
825
|
+
const devScript = isDev ? `<script type="module" src="${bridgePath}"></script>` : "";
|
|
826
|
+
const scriptsToInject = `${clickHandlerScript + devScript}</body>`;
|
|
827
|
+
return html.replace("</body>", scriptsToInject);
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
// src/routes-expose.ts
|
|
833
|
+
function routesExposePlugin(options) {
|
|
834
|
+
let isDev = false;
|
|
835
|
+
const routesPath = options?.routesFilePath || "src/routes.tsx";
|
|
836
|
+
return {
|
|
837
|
+
name: "vite-plugin-routes-expose",
|
|
838
|
+
enforce: "post",
|
|
839
|
+
configResolved(config) {
|
|
840
|
+
isDev = config.command === "serve";
|
|
841
|
+
},
|
|
842
|
+
transform(code, id) {
|
|
843
|
+
if (!isDev) {
|
|
844
|
+
return null;
|
|
845
|
+
}
|
|
846
|
+
if (!id.endsWith(routesPath)) {
|
|
847
|
+
return null;
|
|
848
|
+
}
|
|
849
|
+
try {
|
|
850
|
+
if (code.includes("window.__APP_ROUTES__")) {
|
|
851
|
+
return null;
|
|
852
|
+
}
|
|
853
|
+
const transformedCode = `${code}
|
|
854
|
+
|
|
855
|
+
// Development mode: Expose routes to window.__APP_ROUTES__
|
|
856
|
+
if (typeof window !== 'undefined') {
|
|
857
|
+
window.__APP_ROUTES__ = typeof routes !== 'undefined' && Array.isArray(routes) ? routes : [];
|
|
858
|
+
}
|
|
859
|
+
`;
|
|
860
|
+
return {
|
|
861
|
+
code: transformedCode,
|
|
862
|
+
map: null
|
|
863
|
+
};
|
|
864
|
+
} catch {
|
|
865
|
+
return null;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
exports.browserLogsPlugin = browserLogsPlugin;
|
|
872
|
+
exports.componentIdPlugin = componentIdPlugin;
|
|
873
|
+
exports.editorBridgePlugin = editorBridgePlugin;
|
|
874
|
+
exports.routesExposePlugin = routesExposePlugin;
|
|
875
|
+
//# sourceMappingURL=index.cjs.map
|
|
876
|
+
//# sourceMappingURL=index.cjs.map
|