@wooksjs/http-proxy 0.2.13 → 0.2.15
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/dist/index.cjs +232 -15
- package/dist/index.mjs +232 -15
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var nodeFetchNative = require('node-fetch-native');
|
|
4
4
|
var eventHttp = require('@wooksjs/event-http');
|
|
5
|
+
var crypto = require('crypto');
|
|
5
6
|
|
|
6
7
|
/******************************************************************************
|
|
7
8
|
Copyright (c) Microsoft Corporation.
|
|
@@ -28,13 +29,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
28
29
|
});
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
const banner = () => `[${"@wooksjs/http-proxy"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
32
|
-
|
|
33
|
-
/* istanbul ignore file */
|
|
34
|
-
function warn(text) {
|
|
35
|
-
console.warn('[33m' + banner() + text + '[0m');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
32
|
class IterableRecords {
|
|
39
33
|
constructor() {
|
|
40
34
|
this.index = 0;
|
|
@@ -95,6 +89,230 @@ function applyProxyControls(records, controls, additionalBlockers) {
|
|
|
95
89
|
return result;
|
|
96
90
|
}
|
|
97
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Use existing event context
|
|
94
|
+
*
|
|
95
|
+
* !Must be called syncronously while context is reachable
|
|
96
|
+
*
|
|
97
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
98
|
+
*/
|
|
99
|
+
function useEventContext(expectedTypes) {
|
|
100
|
+
{
|
|
101
|
+
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function useEventId() {
|
|
106
|
+
const { store } = useEventContext();
|
|
107
|
+
const { init } = store('event');
|
|
108
|
+
const getId = () => init('id', () => crypto.randomUUID());
|
|
109
|
+
return { getId };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
class ProstoLogger {
|
|
113
|
+
constructor(options, topic = '') {
|
|
114
|
+
this.options = options;
|
|
115
|
+
this.topic = topic;
|
|
116
|
+
this.messages = [];
|
|
117
|
+
this.mappedLevels = new Map();
|
|
118
|
+
if (options === null || options === void 0 ? void 0 : options.levels) {
|
|
119
|
+
this.levels = options === null || options === void 0 ? void 0 : options.levels;
|
|
120
|
+
this.levels.forEach((type, level) => this.mappedLevels.set(type, level));
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
this.levels = defaultLevels;
|
|
124
|
+
this.mappedLevels = defaultMappedLevels;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
pushMessage(level, args, topic = '') {
|
|
128
|
+
var _a, _b, _c, _d, _e, _f;
|
|
129
|
+
if (typeof ((_a = this.options) === null || _a === void 0 ? void 0 : _a.level) === 'number' && level > this.options.level)
|
|
130
|
+
return;
|
|
131
|
+
if ((_b = this.options) === null || _b === void 0 ? void 0 : _b.parent)
|
|
132
|
+
return this.options.parent.pushMessage(level, args, topic);
|
|
133
|
+
const message = {
|
|
134
|
+
topic,
|
|
135
|
+
level,
|
|
136
|
+
type: this.levels[level] || '',
|
|
137
|
+
messages: args
|
|
138
|
+
.filter(a => (!(a instanceof Error) && !a.stack))
|
|
139
|
+
.map(a => textTypes.includes((typeof a)) ? String(a) : a instanceof Error ? `[Error]: ${a.message}` : safeStringify(a)),
|
|
140
|
+
timestamp: new Date(),
|
|
141
|
+
};
|
|
142
|
+
for (const a of args) {
|
|
143
|
+
if (a instanceof Error && a.stack) {
|
|
144
|
+
message.stack = message.stack || [];
|
|
145
|
+
message.stack.push(...(a.stack.split('\n')));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const mappedMessage = ((_c = this.options) === null || _c === void 0 ? void 0 : _c.mapper) ? this.options.mapper(message) : message;
|
|
149
|
+
if (typeof ((_d = this.options) === null || _d === void 0 ? void 0 : _d.persistLevel) === 'number' && level <= this.options.persistLevel) {
|
|
150
|
+
this.messages.push(mappedMessage);
|
|
151
|
+
}
|
|
152
|
+
if ((_e = this.options) === null || _e === void 0 ? void 0 : _e.transports) {
|
|
153
|
+
(_f = this.options) === null || _f === void 0 ? void 0 : _f.transports.forEach(t => {
|
|
154
|
+
if (typeof t === 'function') {
|
|
155
|
+
void t(mappedMessage);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
void t.handler(mappedMessage);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
fatal(...args) {
|
|
164
|
+
this.pushMessage(this.mappedLevels.get('fatal') || 0, args, this.topic);
|
|
165
|
+
}
|
|
166
|
+
error(...args) {
|
|
167
|
+
this.pushMessage(this.mappedLevels.get('error') || 1, args, this.topic);
|
|
168
|
+
}
|
|
169
|
+
warn(...args) {
|
|
170
|
+
this.pushMessage(this.mappedLevels.get('warn') || 2, args, this.topic);
|
|
171
|
+
}
|
|
172
|
+
log(...args) {
|
|
173
|
+
this.pushMessage(this.mappedLevels.get('log') || 3, args, this.topic);
|
|
174
|
+
}
|
|
175
|
+
info(...args) {
|
|
176
|
+
this.pushMessage(this.mappedLevels.get('info') || 4, args, this.topic);
|
|
177
|
+
}
|
|
178
|
+
debug(...args) {
|
|
179
|
+
this.pushMessage(this.mappedLevels.get('debug') || 5, args, this.topic);
|
|
180
|
+
}
|
|
181
|
+
trace(...args) {
|
|
182
|
+
this.pushMessage(this.mappedLevels.get('trace') || 6, args, this.topic);
|
|
183
|
+
}
|
|
184
|
+
createTopic(topic, level) {
|
|
185
|
+
return new ProstoLogger({
|
|
186
|
+
...(this.options || {}),
|
|
187
|
+
level,
|
|
188
|
+
parent: this,
|
|
189
|
+
}, topic);
|
|
190
|
+
}
|
|
191
|
+
getMessages() {
|
|
192
|
+
var _a;
|
|
193
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.parent)
|
|
194
|
+
return this.options.parent.getMessages();
|
|
195
|
+
return this.messages;
|
|
196
|
+
}
|
|
197
|
+
clear() {
|
|
198
|
+
this.messages = [];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const textTypes = ['string', 'number', 'boolean'];
|
|
202
|
+
const defaultLevels = [
|
|
203
|
+
'fatal',
|
|
204
|
+
'error',
|
|
205
|
+
'warn',
|
|
206
|
+
'log',
|
|
207
|
+
'info',
|
|
208
|
+
'debug',
|
|
209
|
+
'trace',
|
|
210
|
+
];
|
|
211
|
+
const defaultMappedLevels = new Map();
|
|
212
|
+
defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
|
|
213
|
+
function safeStringify(obj) {
|
|
214
|
+
const objType = getObjType(obj);
|
|
215
|
+
try {
|
|
216
|
+
return objType + ' ' + JSON.stringify(obj);
|
|
217
|
+
}
|
|
218
|
+
catch (e) {
|
|
219
|
+
return `[${typeof obj} ${objType}] (failed to stringify)`;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function getObjType(obj) {
|
|
223
|
+
var _a, _b;
|
|
224
|
+
return typeof obj === 'object' ? (_b = (_a = Object.getPrototypeOf(obj)) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name : '';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function createConsoleTransort(opts) {
|
|
228
|
+
return (message) => {
|
|
229
|
+
if (typeof (opts === null || opts === void 0 ? void 0 : opts.level) === 'undefined' || message.level <= (opts === null || opts === void 0 ? void 0 : opts.level)) {
|
|
230
|
+
const formatted = (opts === null || opts === void 0 ? void 0 : opts.format) ? opts.format(message) : message;
|
|
231
|
+
switch (message.level) {
|
|
232
|
+
case 0:
|
|
233
|
+
case 1:
|
|
234
|
+
console.error(formatted);
|
|
235
|
+
break;
|
|
236
|
+
case 2:
|
|
237
|
+
console.warn(formatted);
|
|
238
|
+
break;
|
|
239
|
+
case 3:
|
|
240
|
+
console.log(formatted);
|
|
241
|
+
break;
|
|
242
|
+
case 4:
|
|
243
|
+
console.info(formatted);
|
|
244
|
+
break;
|
|
245
|
+
case 5:
|
|
246
|
+
console.debug(formatted);
|
|
247
|
+
break;
|
|
248
|
+
case 6:
|
|
249
|
+
console.trace(formatted);
|
|
250
|
+
break;
|
|
251
|
+
default:
|
|
252
|
+
console.log(formatted);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
const coloredConsole = (m) => {
|
|
258
|
+
let color = '';
|
|
259
|
+
switch (m.level) {
|
|
260
|
+
case 0:
|
|
261
|
+
case 1:
|
|
262
|
+
color = '[31m';
|
|
263
|
+
break;
|
|
264
|
+
case 2:
|
|
265
|
+
color = '[33m';
|
|
266
|
+
break;
|
|
267
|
+
case 3:
|
|
268
|
+
color = '';
|
|
269
|
+
break;
|
|
270
|
+
case 4:
|
|
271
|
+
color = '[32m' + '[2m';
|
|
272
|
+
break;
|
|
273
|
+
case 5:
|
|
274
|
+
color = '[37m' + '[2m';
|
|
275
|
+
break;
|
|
276
|
+
case 6:
|
|
277
|
+
color = '[36m' + '[2m';
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
const topic = m.topic ? `[${m.topic}]` : '';
|
|
281
|
+
const type = (m.type && !skipTypes.includes(m.type)) ? `[${m.type.padEnd(5).toUpperCase()}]` : '';
|
|
282
|
+
const time = m.timestamp.toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '');
|
|
283
|
+
const stack = m.stack ? `\n${'[2m' + '[37m'}${m.stack.join('\n')}` : '';
|
|
284
|
+
return `${color}${topic}${type}[${time}] ${m.messages.join('\n')}${stack}${'[0m'}`;
|
|
285
|
+
};
|
|
286
|
+
const skipTypes = ['log', 'info', 'warn', 'error'];
|
|
287
|
+
|
|
288
|
+
class EventLogger extends ProstoLogger {
|
|
289
|
+
constructor(eventId, opts) {
|
|
290
|
+
const _opts = opts || {
|
|
291
|
+
level: 4,
|
|
292
|
+
};
|
|
293
|
+
if (!_opts.mapper) {
|
|
294
|
+
_opts.mapper = (msg) => (Object.assign(Object.assign({}, msg), { eventId }));
|
|
295
|
+
}
|
|
296
|
+
if (!_opts.transports) {
|
|
297
|
+
_opts.transports = [
|
|
298
|
+
createConsoleTransort({
|
|
299
|
+
format: coloredConsole,
|
|
300
|
+
}),
|
|
301
|
+
];
|
|
302
|
+
}
|
|
303
|
+
super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function useEventLogger(topic) {
|
|
308
|
+
const { getId } = useEventId();
|
|
309
|
+
const { store, getCtx } = useEventContext();
|
|
310
|
+
const { init } = store('event');
|
|
311
|
+
const ctx = getCtx();
|
|
312
|
+
const get = () => init('logger', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
|
|
313
|
+
return topic ? get().createTopic(topic) : get();
|
|
314
|
+
}
|
|
315
|
+
|
|
98
316
|
const reqHeadersToBlock = [
|
|
99
317
|
'connection',
|
|
100
318
|
'accept-encoding',
|
|
@@ -112,6 +330,7 @@ function useProxy() {
|
|
|
112
330
|
const { setHeader, headers: getSetHeaders } = eventHttp.useSetHeaders();
|
|
113
331
|
const { getCtx } = eventHttp.useHttpContext();
|
|
114
332
|
const { req } = getCtx().event;
|
|
333
|
+
const logger = useEventLogger('http-proxy');
|
|
115
334
|
const setHeadersObject = getSetHeaders();
|
|
116
335
|
return function proxy(target, opts) {
|
|
117
336
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -128,9 +347,8 @@ function useProxy() {
|
|
|
128
347
|
const method = (opts === null || opts === void 0 ? void 0 : opts.method) || req.method;
|
|
129
348
|
// actual request
|
|
130
349
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
console.log('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
350
|
+
logger.info(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
351
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
134
352
|
}
|
|
135
353
|
const resp = yield nodeFetchNative.fetch(url, {
|
|
136
354
|
method,
|
|
@@ -140,9 +358,8 @@ function useProxy() {
|
|
|
140
358
|
// preparing response
|
|
141
359
|
status.value = resp.status;
|
|
142
360
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
console.log(`${'[33m'}response headers:${'[39m'}`);
|
|
361
|
+
logger.info(`${resp.status} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
362
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
146
363
|
}
|
|
147
364
|
// preparing response headers
|
|
148
365
|
const resHeaders = (opts === null || opts === void 0 ? void 0 : opts.resHeaders) ? applyProxyControls(resp.headers.entries(), opts === null || opts === void 0 ? void 0 : opts.resHeaders, resHeadersToBlock) : null;
|
|
@@ -152,7 +369,7 @@ function useProxy() {
|
|
|
152
369
|
if (name) {
|
|
153
370
|
setHeader(name, value);
|
|
154
371
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
155
|
-
|
|
372
|
+
logger.info(`\t${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
156
373
|
}
|
|
157
374
|
}
|
|
158
375
|
}
|
|
@@ -163,7 +380,7 @@ function useProxy() {
|
|
|
163
380
|
if (name) {
|
|
164
381
|
setHeadersObject['set-cookie'].push(`${name}=${value}`);
|
|
165
382
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
166
|
-
|
|
383
|
+
logger.info(`\t${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
167
384
|
}
|
|
168
385
|
}
|
|
169
386
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { fetch } from 'node-fetch-native';
|
|
2
2
|
import { useStatus, useSetHeaders, useHttpContext } from '@wooksjs/event-http';
|
|
3
|
+
import { randomUUID } from 'crypto';
|
|
3
4
|
|
|
4
5
|
/******************************************************************************
|
|
5
6
|
Copyright (c) Microsoft Corporation.
|
|
@@ -26,13 +27,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
26
27
|
});
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
const banner = () => `[${"@wooksjs/http-proxy"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
30
|
-
|
|
31
|
-
/* istanbul ignore file */
|
|
32
|
-
function warn(text) {
|
|
33
|
-
console.warn('[33m' + banner() + text + '[0m');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
30
|
class IterableRecords {
|
|
37
31
|
constructor() {
|
|
38
32
|
this.index = 0;
|
|
@@ -93,6 +87,230 @@ function applyProxyControls(records, controls, additionalBlockers) {
|
|
|
93
87
|
return result;
|
|
94
88
|
}
|
|
95
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Use existing event context
|
|
92
|
+
*
|
|
93
|
+
* !Must be called syncronously while context is reachable
|
|
94
|
+
*
|
|
95
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
96
|
+
*/
|
|
97
|
+
function useEventContext(expectedTypes) {
|
|
98
|
+
{
|
|
99
|
+
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function useEventId() {
|
|
104
|
+
const { store } = useEventContext();
|
|
105
|
+
const { init } = store('event');
|
|
106
|
+
const getId = () => init('id', () => randomUUID());
|
|
107
|
+
return { getId };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
class ProstoLogger {
|
|
111
|
+
constructor(options, topic = '') {
|
|
112
|
+
this.options = options;
|
|
113
|
+
this.topic = topic;
|
|
114
|
+
this.messages = [];
|
|
115
|
+
this.mappedLevels = new Map();
|
|
116
|
+
if (options === null || options === void 0 ? void 0 : options.levels) {
|
|
117
|
+
this.levels = options === null || options === void 0 ? void 0 : options.levels;
|
|
118
|
+
this.levels.forEach((type, level) => this.mappedLevels.set(type, level));
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
this.levels = defaultLevels;
|
|
122
|
+
this.mappedLevels = defaultMappedLevels;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
pushMessage(level, args, topic = '') {
|
|
126
|
+
var _a, _b, _c, _d, _e, _f;
|
|
127
|
+
if (typeof ((_a = this.options) === null || _a === void 0 ? void 0 : _a.level) === 'number' && level > this.options.level)
|
|
128
|
+
return;
|
|
129
|
+
if ((_b = this.options) === null || _b === void 0 ? void 0 : _b.parent)
|
|
130
|
+
return this.options.parent.pushMessage(level, args, topic);
|
|
131
|
+
const message = {
|
|
132
|
+
topic,
|
|
133
|
+
level,
|
|
134
|
+
type: this.levels[level] || '',
|
|
135
|
+
messages: args
|
|
136
|
+
.filter(a => (!(a instanceof Error) && !a.stack))
|
|
137
|
+
.map(a => textTypes.includes((typeof a)) ? String(a) : a instanceof Error ? `[Error]: ${a.message}` : safeStringify(a)),
|
|
138
|
+
timestamp: new Date(),
|
|
139
|
+
};
|
|
140
|
+
for (const a of args) {
|
|
141
|
+
if (a instanceof Error && a.stack) {
|
|
142
|
+
message.stack = message.stack || [];
|
|
143
|
+
message.stack.push(...(a.stack.split('\n')));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const mappedMessage = ((_c = this.options) === null || _c === void 0 ? void 0 : _c.mapper) ? this.options.mapper(message) : message;
|
|
147
|
+
if (typeof ((_d = this.options) === null || _d === void 0 ? void 0 : _d.persistLevel) === 'number' && level <= this.options.persistLevel) {
|
|
148
|
+
this.messages.push(mappedMessage);
|
|
149
|
+
}
|
|
150
|
+
if ((_e = this.options) === null || _e === void 0 ? void 0 : _e.transports) {
|
|
151
|
+
(_f = this.options) === null || _f === void 0 ? void 0 : _f.transports.forEach(t => {
|
|
152
|
+
if (typeof t === 'function') {
|
|
153
|
+
void t(mappedMessage);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
void t.handler(mappedMessage);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
fatal(...args) {
|
|
162
|
+
this.pushMessage(this.mappedLevels.get('fatal') || 0, args, this.topic);
|
|
163
|
+
}
|
|
164
|
+
error(...args) {
|
|
165
|
+
this.pushMessage(this.mappedLevels.get('error') || 1, args, this.topic);
|
|
166
|
+
}
|
|
167
|
+
warn(...args) {
|
|
168
|
+
this.pushMessage(this.mappedLevels.get('warn') || 2, args, this.topic);
|
|
169
|
+
}
|
|
170
|
+
log(...args) {
|
|
171
|
+
this.pushMessage(this.mappedLevels.get('log') || 3, args, this.topic);
|
|
172
|
+
}
|
|
173
|
+
info(...args) {
|
|
174
|
+
this.pushMessage(this.mappedLevels.get('info') || 4, args, this.topic);
|
|
175
|
+
}
|
|
176
|
+
debug(...args) {
|
|
177
|
+
this.pushMessage(this.mappedLevels.get('debug') || 5, args, this.topic);
|
|
178
|
+
}
|
|
179
|
+
trace(...args) {
|
|
180
|
+
this.pushMessage(this.mappedLevels.get('trace') || 6, args, this.topic);
|
|
181
|
+
}
|
|
182
|
+
createTopic(topic, level) {
|
|
183
|
+
return new ProstoLogger({
|
|
184
|
+
...(this.options || {}),
|
|
185
|
+
level,
|
|
186
|
+
parent: this,
|
|
187
|
+
}, topic);
|
|
188
|
+
}
|
|
189
|
+
getMessages() {
|
|
190
|
+
var _a;
|
|
191
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.parent)
|
|
192
|
+
return this.options.parent.getMessages();
|
|
193
|
+
return this.messages;
|
|
194
|
+
}
|
|
195
|
+
clear() {
|
|
196
|
+
this.messages = [];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const textTypes = ['string', 'number', 'boolean'];
|
|
200
|
+
const defaultLevels = [
|
|
201
|
+
'fatal',
|
|
202
|
+
'error',
|
|
203
|
+
'warn',
|
|
204
|
+
'log',
|
|
205
|
+
'info',
|
|
206
|
+
'debug',
|
|
207
|
+
'trace',
|
|
208
|
+
];
|
|
209
|
+
const defaultMappedLevels = new Map();
|
|
210
|
+
defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
|
|
211
|
+
function safeStringify(obj) {
|
|
212
|
+
const objType = getObjType(obj);
|
|
213
|
+
try {
|
|
214
|
+
return objType + ' ' + JSON.stringify(obj);
|
|
215
|
+
}
|
|
216
|
+
catch (e) {
|
|
217
|
+
return `[${typeof obj} ${objType}] (failed to stringify)`;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function getObjType(obj) {
|
|
221
|
+
var _a, _b;
|
|
222
|
+
return typeof obj === 'object' ? (_b = (_a = Object.getPrototypeOf(obj)) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name : '';
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function createConsoleTransort(opts) {
|
|
226
|
+
return (message) => {
|
|
227
|
+
if (typeof (opts === null || opts === void 0 ? void 0 : opts.level) === 'undefined' || message.level <= (opts === null || opts === void 0 ? void 0 : opts.level)) {
|
|
228
|
+
const formatted = (opts === null || opts === void 0 ? void 0 : opts.format) ? opts.format(message) : message;
|
|
229
|
+
switch (message.level) {
|
|
230
|
+
case 0:
|
|
231
|
+
case 1:
|
|
232
|
+
console.error(formatted);
|
|
233
|
+
break;
|
|
234
|
+
case 2:
|
|
235
|
+
console.warn(formatted);
|
|
236
|
+
break;
|
|
237
|
+
case 3:
|
|
238
|
+
console.log(formatted);
|
|
239
|
+
break;
|
|
240
|
+
case 4:
|
|
241
|
+
console.info(formatted);
|
|
242
|
+
break;
|
|
243
|
+
case 5:
|
|
244
|
+
console.debug(formatted);
|
|
245
|
+
break;
|
|
246
|
+
case 6:
|
|
247
|
+
console.trace(formatted);
|
|
248
|
+
break;
|
|
249
|
+
default:
|
|
250
|
+
console.log(formatted);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
const coloredConsole = (m) => {
|
|
256
|
+
let color = '';
|
|
257
|
+
switch (m.level) {
|
|
258
|
+
case 0:
|
|
259
|
+
case 1:
|
|
260
|
+
color = '[31m';
|
|
261
|
+
break;
|
|
262
|
+
case 2:
|
|
263
|
+
color = '[33m';
|
|
264
|
+
break;
|
|
265
|
+
case 3:
|
|
266
|
+
color = '';
|
|
267
|
+
break;
|
|
268
|
+
case 4:
|
|
269
|
+
color = '[32m' + '[2m';
|
|
270
|
+
break;
|
|
271
|
+
case 5:
|
|
272
|
+
color = '[37m' + '[2m';
|
|
273
|
+
break;
|
|
274
|
+
case 6:
|
|
275
|
+
color = '[36m' + '[2m';
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
const topic = m.topic ? `[${m.topic}]` : '';
|
|
279
|
+
const type = (m.type && !skipTypes.includes(m.type)) ? `[${m.type.padEnd(5).toUpperCase()}]` : '';
|
|
280
|
+
const time = m.timestamp.toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '');
|
|
281
|
+
const stack = m.stack ? `\n${'[2m' + '[37m'}${m.stack.join('\n')}` : '';
|
|
282
|
+
return `${color}${topic}${type}[${time}] ${m.messages.join('\n')}${stack}${'[0m'}`;
|
|
283
|
+
};
|
|
284
|
+
const skipTypes = ['log', 'info', 'warn', 'error'];
|
|
285
|
+
|
|
286
|
+
class EventLogger extends ProstoLogger {
|
|
287
|
+
constructor(eventId, opts) {
|
|
288
|
+
const _opts = opts || {
|
|
289
|
+
level: 4,
|
|
290
|
+
};
|
|
291
|
+
if (!_opts.mapper) {
|
|
292
|
+
_opts.mapper = (msg) => (Object.assign(Object.assign({}, msg), { eventId }));
|
|
293
|
+
}
|
|
294
|
+
if (!_opts.transports) {
|
|
295
|
+
_opts.transports = [
|
|
296
|
+
createConsoleTransort({
|
|
297
|
+
format: coloredConsole,
|
|
298
|
+
}),
|
|
299
|
+
];
|
|
300
|
+
}
|
|
301
|
+
super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function useEventLogger(topic) {
|
|
306
|
+
const { getId } = useEventId();
|
|
307
|
+
const { store, getCtx } = useEventContext();
|
|
308
|
+
const { init } = store('event');
|
|
309
|
+
const ctx = getCtx();
|
|
310
|
+
const get = () => init('logger', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
|
|
311
|
+
return topic ? get().createTopic(topic) : get();
|
|
312
|
+
}
|
|
313
|
+
|
|
96
314
|
const reqHeadersToBlock = [
|
|
97
315
|
'connection',
|
|
98
316
|
'accept-encoding',
|
|
@@ -110,6 +328,7 @@ function useProxy() {
|
|
|
110
328
|
const { setHeader, headers: getSetHeaders } = useSetHeaders();
|
|
111
329
|
const { getCtx } = useHttpContext();
|
|
112
330
|
const { req } = getCtx().event;
|
|
331
|
+
const logger = useEventLogger('http-proxy');
|
|
113
332
|
const setHeadersObject = getSetHeaders();
|
|
114
333
|
return function proxy(target, opts) {
|
|
115
334
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -126,9 +345,8 @@ function useProxy() {
|
|
|
126
345
|
const method = (opts === null || opts === void 0 ? void 0 : opts.method) || req.method;
|
|
127
346
|
// actual request
|
|
128
347
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
console.log('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
348
|
+
logger.info(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
349
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
132
350
|
}
|
|
133
351
|
const resp = yield fetch(url, {
|
|
134
352
|
method,
|
|
@@ -138,9 +356,8 @@ function useProxy() {
|
|
|
138
356
|
// preparing response
|
|
139
357
|
status.value = resp.status;
|
|
140
358
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
console.log(`${'[33m'}response headers:${'[39m'}`);
|
|
359
|
+
logger.info(`${resp.status} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
360
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
144
361
|
}
|
|
145
362
|
// preparing response headers
|
|
146
363
|
const resHeaders = (opts === null || opts === void 0 ? void 0 : opts.resHeaders) ? applyProxyControls(resp.headers.entries(), opts === null || opts === void 0 ? void 0 : opts.resHeaders, resHeadersToBlock) : null;
|
|
@@ -150,7 +367,7 @@ function useProxy() {
|
|
|
150
367
|
if (name) {
|
|
151
368
|
setHeader(name, value);
|
|
152
369
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
153
|
-
|
|
370
|
+
logger.info(`\t${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
154
371
|
}
|
|
155
372
|
}
|
|
156
373
|
}
|
|
@@ -161,7 +378,7 @@ function useProxy() {
|
|
|
161
378
|
if (name) {
|
|
162
379
|
setHeadersObject['set-cookie'].push(`${name}=${value}`);
|
|
163
380
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
164
|
-
|
|
381
|
+
logger.info(`\t${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
165
382
|
}
|
|
166
383
|
}
|
|
167
384
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-proxy",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.15",
|
|
4
4
|
"description": "Proxy Wooks composable",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@wooksjs/event-http": "0.2.
|
|
34
|
+
"@wooksjs/event-http": "0.2.15"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"node-fetch-native": "^1.0.1"
|