@wooksjs/http-proxy 0.2.13 → 0.2.14
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 +229 -15
- package/dist/index.mjs +229 -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,227 @@ 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
|
+
return this.messages;
|
|
193
|
+
}
|
|
194
|
+
clear() {
|
|
195
|
+
this.messages = [];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const textTypes = ['string', 'number', 'boolean'];
|
|
199
|
+
const defaultLevels = [
|
|
200
|
+
'fatal',
|
|
201
|
+
'error',
|
|
202
|
+
'warn',
|
|
203
|
+
'log',
|
|
204
|
+
'info',
|
|
205
|
+
'debug',
|
|
206
|
+
'trace',
|
|
207
|
+
];
|
|
208
|
+
const defaultMappedLevels = new Map();
|
|
209
|
+
defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
|
|
210
|
+
function safeStringify(obj) {
|
|
211
|
+
const objType = getObjType(obj);
|
|
212
|
+
try {
|
|
213
|
+
return objType + ' ' + JSON.stringify(obj);
|
|
214
|
+
}
|
|
215
|
+
catch (e) {
|
|
216
|
+
return `[${typeof obj} ${objType}] (failed to stringify)`;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function getObjType(obj) {
|
|
220
|
+
var _a, _b;
|
|
221
|
+
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 : '';
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function createConsoleTransort(opts) {
|
|
225
|
+
return (message) => {
|
|
226
|
+
if (typeof (opts === null || opts === void 0 ? void 0 : opts.level) === 'undefined' || message.level <= (opts === null || opts === void 0 ? void 0 : opts.level)) {
|
|
227
|
+
const formatted = (opts === null || opts === void 0 ? void 0 : opts.format) ? opts.format(message) : message;
|
|
228
|
+
switch (message.level) {
|
|
229
|
+
case 0:
|
|
230
|
+
case 1:
|
|
231
|
+
console.error(formatted);
|
|
232
|
+
break;
|
|
233
|
+
case 2:
|
|
234
|
+
console.warn(formatted);
|
|
235
|
+
break;
|
|
236
|
+
case 3:
|
|
237
|
+
console.log(formatted);
|
|
238
|
+
break;
|
|
239
|
+
case 4:
|
|
240
|
+
console.info(formatted);
|
|
241
|
+
break;
|
|
242
|
+
case 5:
|
|
243
|
+
console.debug(formatted);
|
|
244
|
+
break;
|
|
245
|
+
case 6:
|
|
246
|
+
console.trace(formatted);
|
|
247
|
+
break;
|
|
248
|
+
default:
|
|
249
|
+
console.log(formatted);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
const coloredConsole = (m) => {
|
|
255
|
+
let color = '';
|
|
256
|
+
switch (m.level) {
|
|
257
|
+
case 0:
|
|
258
|
+
case 1:
|
|
259
|
+
color = '[31m';
|
|
260
|
+
break;
|
|
261
|
+
case 2:
|
|
262
|
+
color = '[33m';
|
|
263
|
+
break;
|
|
264
|
+
case 3:
|
|
265
|
+
color = '';
|
|
266
|
+
break;
|
|
267
|
+
case 4:
|
|
268
|
+
color = '[32m' + '[2m';
|
|
269
|
+
break;
|
|
270
|
+
case 5:
|
|
271
|
+
color = '[37m' + '[2m';
|
|
272
|
+
break;
|
|
273
|
+
case 6:
|
|
274
|
+
color = '[36m' + '[2m';
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
const topic = m.topic ? `[${m.topic}]` : '';
|
|
278
|
+
const type = (m.type && !skipTypes.includes(m.type)) ? `[${m.type.padEnd(5).toUpperCase()}]` : '';
|
|
279
|
+
const time = m.timestamp.toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '');
|
|
280
|
+
const stack = m.stack ? `\n${'[2m' + '[37m'}${m.stack.join('\n')}` : '';
|
|
281
|
+
return `${color}${topic}${type}[${time}] ${m.messages.join('\n')}${stack}${'[0m'}`;
|
|
282
|
+
};
|
|
283
|
+
const skipTypes = ['log', 'info', 'warn', 'error'];
|
|
284
|
+
|
|
285
|
+
class EventLogger extends ProstoLogger {
|
|
286
|
+
constructor(eventId, opts) {
|
|
287
|
+
const _opts = opts || {
|
|
288
|
+
level: 4,
|
|
289
|
+
};
|
|
290
|
+
if (!_opts.mapper) {
|
|
291
|
+
_opts.mapper = (msg) => (Object.assign(Object.assign({}, msg), { eventId }));
|
|
292
|
+
}
|
|
293
|
+
if (!_opts.transports) {
|
|
294
|
+
_opts.transports = [
|
|
295
|
+
createConsoleTransort({
|
|
296
|
+
format: (message) => coloredConsole(message),
|
|
297
|
+
}),
|
|
298
|
+
];
|
|
299
|
+
}
|
|
300
|
+
super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function useEventLogger(topic) {
|
|
305
|
+
const { getId } = useEventId();
|
|
306
|
+
const { store, getCtx } = useEventContext();
|
|
307
|
+
const { init } = store('event');
|
|
308
|
+
const ctx = getCtx();
|
|
309
|
+
const get = () => init('logger', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
|
|
310
|
+
return topic ? get().createTopic(topic) : get();
|
|
311
|
+
}
|
|
312
|
+
|
|
98
313
|
const reqHeadersToBlock = [
|
|
99
314
|
'connection',
|
|
100
315
|
'accept-encoding',
|
|
@@ -112,6 +327,7 @@ function useProxy() {
|
|
|
112
327
|
const { setHeader, headers: getSetHeaders } = eventHttp.useSetHeaders();
|
|
113
328
|
const { getCtx } = eventHttp.useHttpContext();
|
|
114
329
|
const { req } = getCtx().event;
|
|
330
|
+
const logger = useEventLogger('http-proxy');
|
|
115
331
|
const setHeadersObject = getSetHeaders();
|
|
116
332
|
return function proxy(target, opts) {
|
|
117
333
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -128,9 +344,8 @@ function useProxy() {
|
|
|
128
344
|
const method = (opts === null || opts === void 0 ? void 0 : opts.method) || req.method;
|
|
129
345
|
// actual request
|
|
130
346
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
console.log('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
347
|
+
logger.info(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
348
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
134
349
|
}
|
|
135
350
|
const resp = yield nodeFetchNative.fetch(url, {
|
|
136
351
|
method,
|
|
@@ -140,9 +355,8 @@ function useProxy() {
|
|
|
140
355
|
// preparing response
|
|
141
356
|
status.value = resp.status;
|
|
142
357
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
console.log(`${'[33m'}response headers:${'[39m'}`);
|
|
358
|
+
logger.info(`${resp.status} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
359
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
146
360
|
}
|
|
147
361
|
// preparing response headers
|
|
148
362
|
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 +366,7 @@ function useProxy() {
|
|
|
152
366
|
if (name) {
|
|
153
367
|
setHeader(name, value);
|
|
154
368
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
155
|
-
|
|
369
|
+
logger.info(`\t${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
156
370
|
}
|
|
157
371
|
}
|
|
158
372
|
}
|
|
@@ -163,7 +377,7 @@ function useProxy() {
|
|
|
163
377
|
if (name) {
|
|
164
378
|
setHeadersObject['set-cookie'].push(`${name}=${value}`);
|
|
165
379
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
166
|
-
|
|
380
|
+
logger.info(`\t${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
167
381
|
}
|
|
168
382
|
}
|
|
169
383
|
}
|
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,227 @@ 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
|
+
return this.messages;
|
|
191
|
+
}
|
|
192
|
+
clear() {
|
|
193
|
+
this.messages = [];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const textTypes = ['string', 'number', 'boolean'];
|
|
197
|
+
const defaultLevels = [
|
|
198
|
+
'fatal',
|
|
199
|
+
'error',
|
|
200
|
+
'warn',
|
|
201
|
+
'log',
|
|
202
|
+
'info',
|
|
203
|
+
'debug',
|
|
204
|
+
'trace',
|
|
205
|
+
];
|
|
206
|
+
const defaultMappedLevels = new Map();
|
|
207
|
+
defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
|
|
208
|
+
function safeStringify(obj) {
|
|
209
|
+
const objType = getObjType(obj);
|
|
210
|
+
try {
|
|
211
|
+
return objType + ' ' + JSON.stringify(obj);
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
return `[${typeof obj} ${objType}] (failed to stringify)`;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function getObjType(obj) {
|
|
218
|
+
var _a, _b;
|
|
219
|
+
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 : '';
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function createConsoleTransort(opts) {
|
|
223
|
+
return (message) => {
|
|
224
|
+
if (typeof (opts === null || opts === void 0 ? void 0 : opts.level) === 'undefined' || message.level <= (opts === null || opts === void 0 ? void 0 : opts.level)) {
|
|
225
|
+
const formatted = (opts === null || opts === void 0 ? void 0 : opts.format) ? opts.format(message) : message;
|
|
226
|
+
switch (message.level) {
|
|
227
|
+
case 0:
|
|
228
|
+
case 1:
|
|
229
|
+
console.error(formatted);
|
|
230
|
+
break;
|
|
231
|
+
case 2:
|
|
232
|
+
console.warn(formatted);
|
|
233
|
+
break;
|
|
234
|
+
case 3:
|
|
235
|
+
console.log(formatted);
|
|
236
|
+
break;
|
|
237
|
+
case 4:
|
|
238
|
+
console.info(formatted);
|
|
239
|
+
break;
|
|
240
|
+
case 5:
|
|
241
|
+
console.debug(formatted);
|
|
242
|
+
break;
|
|
243
|
+
case 6:
|
|
244
|
+
console.trace(formatted);
|
|
245
|
+
break;
|
|
246
|
+
default:
|
|
247
|
+
console.log(formatted);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const coloredConsole = (m) => {
|
|
253
|
+
let color = '';
|
|
254
|
+
switch (m.level) {
|
|
255
|
+
case 0:
|
|
256
|
+
case 1:
|
|
257
|
+
color = '[31m';
|
|
258
|
+
break;
|
|
259
|
+
case 2:
|
|
260
|
+
color = '[33m';
|
|
261
|
+
break;
|
|
262
|
+
case 3:
|
|
263
|
+
color = '';
|
|
264
|
+
break;
|
|
265
|
+
case 4:
|
|
266
|
+
color = '[32m' + '[2m';
|
|
267
|
+
break;
|
|
268
|
+
case 5:
|
|
269
|
+
color = '[37m' + '[2m';
|
|
270
|
+
break;
|
|
271
|
+
case 6:
|
|
272
|
+
color = '[36m' + '[2m';
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
const topic = m.topic ? `[${m.topic}]` : '';
|
|
276
|
+
const type = (m.type && !skipTypes.includes(m.type)) ? `[${m.type.padEnd(5).toUpperCase()}]` : '';
|
|
277
|
+
const time = m.timestamp.toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '');
|
|
278
|
+
const stack = m.stack ? `\n${'[2m' + '[37m'}${m.stack.join('\n')}` : '';
|
|
279
|
+
return `${color}${topic}${type}[${time}] ${m.messages.join('\n')}${stack}${'[0m'}`;
|
|
280
|
+
};
|
|
281
|
+
const skipTypes = ['log', 'info', 'warn', 'error'];
|
|
282
|
+
|
|
283
|
+
class EventLogger extends ProstoLogger {
|
|
284
|
+
constructor(eventId, opts) {
|
|
285
|
+
const _opts = opts || {
|
|
286
|
+
level: 4,
|
|
287
|
+
};
|
|
288
|
+
if (!_opts.mapper) {
|
|
289
|
+
_opts.mapper = (msg) => (Object.assign(Object.assign({}, msg), { eventId }));
|
|
290
|
+
}
|
|
291
|
+
if (!_opts.transports) {
|
|
292
|
+
_opts.transports = [
|
|
293
|
+
createConsoleTransort({
|
|
294
|
+
format: (message) => coloredConsole(message),
|
|
295
|
+
}),
|
|
296
|
+
];
|
|
297
|
+
}
|
|
298
|
+
super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function useEventLogger(topic) {
|
|
303
|
+
const { getId } = useEventId();
|
|
304
|
+
const { store, getCtx } = useEventContext();
|
|
305
|
+
const { init } = store('event');
|
|
306
|
+
const ctx = getCtx();
|
|
307
|
+
const get = () => init('logger', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
|
|
308
|
+
return topic ? get().createTopic(topic) : get();
|
|
309
|
+
}
|
|
310
|
+
|
|
96
311
|
const reqHeadersToBlock = [
|
|
97
312
|
'connection',
|
|
98
313
|
'accept-encoding',
|
|
@@ -110,6 +325,7 @@ function useProxy() {
|
|
|
110
325
|
const { setHeader, headers: getSetHeaders } = useSetHeaders();
|
|
111
326
|
const { getCtx } = useHttpContext();
|
|
112
327
|
const { req } = getCtx().event;
|
|
328
|
+
const logger = useEventLogger('http-proxy');
|
|
113
329
|
const setHeadersObject = getSetHeaders();
|
|
114
330
|
return function proxy(target, opts) {
|
|
115
331
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -126,9 +342,8 @@ function useProxy() {
|
|
|
126
342
|
const method = (opts === null || opts === void 0 ? void 0 : opts.method) || req.method;
|
|
127
343
|
// actual request
|
|
128
344
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
console.log('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
345
|
+
logger.info(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
346
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
132
347
|
}
|
|
133
348
|
const resp = yield fetch(url, {
|
|
134
349
|
method,
|
|
@@ -138,9 +353,8 @@ function useProxy() {
|
|
|
138
353
|
// preparing response
|
|
139
354
|
status.value = resp.status;
|
|
140
355
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
console.log(`${'[33m'}response headers:${'[39m'}`);
|
|
356
|
+
logger.info(`${resp.status} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
357
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
144
358
|
}
|
|
145
359
|
// preparing response headers
|
|
146
360
|
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 +364,7 @@ function useProxy() {
|
|
|
150
364
|
if (name) {
|
|
151
365
|
setHeader(name, value);
|
|
152
366
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
153
|
-
|
|
367
|
+
logger.info(`\t${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
154
368
|
}
|
|
155
369
|
}
|
|
156
370
|
}
|
|
@@ -161,7 +375,7 @@ function useProxy() {
|
|
|
161
375
|
if (name) {
|
|
162
376
|
setHeadersObject['set-cookie'].push(`${name}=${value}`);
|
|
163
377
|
if (opts === null || opts === void 0 ? void 0 : opts.debug) {
|
|
164
|
-
|
|
378
|
+
logger.info(`\t${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
165
379
|
}
|
|
166
380
|
}
|
|
167
381
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-proxy",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
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.14"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"node-fetch-native": "^1.0.1"
|