@wooksjs/http-proxy 0.4.1 → 0.4.3

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