@podium/client 4.5.22 → 5.0.0-next.10

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/lib/resource.js CHANGED
@@ -1,24 +1,22 @@
1
- /* eslint-disable no-underscore-dangle */
2
1
  /* eslint-disable no-param-reassign */
3
2
 
4
- 'use strict';
3
+ import Metrics from '@metrics/client';
4
+ import stream from 'stream';
5
+ import abslog from 'abslog';
6
+ import assert from 'assert';
5
7
 
6
- const Metrics = require('@metrics/client');
7
- const stream = require('readable-stream');
8
- const abslog = require('abslog');
9
- const assert = require('assert');
10
- const util = require('util');
8
+ import HttpOutgoing from './http-outgoing.js';
9
+ import Response from './response.js';
10
+ import Resolver from './resolver.js';
11
+ import * as utils from './utils.js';
11
12
 
12
- const HttpOutgoing = require('./http-outgoing');
13
- const Response = require('./response');
14
- const Resolver = require('./resolver');
13
+ const inspect = Symbol.for('nodejs.util.inspect.custom');
15
14
 
16
- const _resolver = Symbol('podium:client:resource:resolver');
17
- const _options = Symbol('podium:client:resource:options');
18
- const _metrics = Symbol('podium:client:resource:metrics');
19
- const _state = Symbol('podium:client:resource:state');
20
-
21
- const PodiumClientResource = class PodiumClientResource {
15
+ export default class PodiumClientResource {
16
+ #resolver;
17
+ #options;
18
+ #metrics;
19
+ #state;
22
20
  constructor(registry, state, options = {}) {
23
21
  assert(
24
22
  registry,
@@ -32,37 +30,38 @@ const PodiumClientResource = class PodiumClientResource {
32
30
 
33
31
  const log = abslog(options.logger);
34
32
 
35
- this[_resolver] = new Resolver(registry, options);
36
- this[_options] = options;
37
- this[_metrics] = new Metrics();
38
- this[_state] = state;
33
+ this.#resolver = new Resolver(registry, options);
34
+ this.#options = options;
35
+ this.#metrics = new Metrics();
36
+ this.#state = state;
39
37
 
40
- this[_metrics].on('error', error => {
38
+ this.#metrics.on('error', error => {
41
39
  log.error(
42
40
  'Error emitted by metric stream in @podium/client module',
43
41
  error,
44
42
  );
45
43
  });
46
44
 
47
- this[_resolver].metrics.pipe(this[_metrics]);
45
+ this.#resolver.metrics.pipe(this.#metrics);
48
46
  }
49
47
 
50
48
  get metrics() {
51
- return this[_metrics];
49
+ return this.#metrics;
52
50
  }
53
51
 
54
52
  get name() {
55
- return this[_options].name;
53
+ return this.#options.name;
56
54
  }
57
55
 
58
56
  get uri() {
59
- return this[_options].uri;
57
+ return this.#options.uri;
60
58
  }
61
59
 
62
60
  async fetch(incoming = {}, reqOptions = {}) {
63
- const outgoing = new HttpOutgoing(this[_options], reqOptions, incoming);
61
+ if (!utils.validateIncoming(incoming)) throw new TypeError('you must pass an instance of "HttpIncoming" as the first argument to the .fetch() method');
62
+ const outgoing = new HttpOutgoing(this.#options, reqOptions, incoming);
64
63
 
65
- this[_state].setInitializingState();
64
+ this.#state.setInitializingState();
66
65
 
67
66
  const chunks = [];
68
67
  const to = new stream.Writable({
@@ -72,9 +71,11 @@ const PodiumClientResource = class PodiumClientResource {
72
71
  },
73
72
  });
74
73
 
75
- stream.pipeline(outgoing, to);
74
+ stream.pipeline([outgoing, to], () => {
75
+ // noop
76
+ });
76
77
 
77
- const { manifest, headers, redirect } = await this[_resolver].resolve(
78
+ const { manifest, headers, redirect } = await this.#resolver.resolve(
78
79
  outgoing,
79
80
  );
80
81
 
@@ -92,34 +93,28 @@ const PodiumClientResource = class PodiumClientResource {
92
93
  }
93
94
 
94
95
  stream(incoming = {}, reqOptions = {}) {
95
- const outgoing = new HttpOutgoing(this[_options], reqOptions, incoming);
96
- this[_state].setInitializingState();
97
- this[_resolver].resolve(outgoing).catch((err) => {
98
- outgoing.emit('error', err);
99
- });
96
+ if (!utils.validateIncoming(incoming)) throw new TypeError('you must pass an instance of "HttpIncoming" as the first argument to the .stream() method');
97
+ const outgoing = new HttpOutgoing(this.#options, reqOptions, incoming);
98
+ this.#state.setInitializingState();
99
+ this.#resolver.resolve(outgoing);
100
100
  return outgoing;
101
101
  }
102
102
 
103
103
  refresh(incoming = {}, reqOptions = {}) {
104
- const outgoing = new HttpOutgoing(this[_options], reqOptions, incoming);
105
- this[_state].setInitializingState();
106
- return this[_resolver].refresh(outgoing).then(obj => obj);
104
+ const outgoing = new HttpOutgoing(this.#options, reqOptions, incoming);
105
+ this.#state.setInitializingState();
106
+ return this.#resolver.refresh(outgoing).then(obj => obj);
107
107
  }
108
108
 
109
- get [Symbol.toStringTag]() {
110
- return 'PodiumClientResource';
109
+ [inspect]() {
110
+ return {
111
+ metrics: this.metrics,
112
+ name: this.name,
113
+ uri: this.uri,
114
+ };
111
115
  }
112
116
 
113
- [util.inspect.custom](depth, options) {
114
- return util.inspect(
115
- {
116
- metrics: this.metrics,
117
- name: this.name,
118
- uri: this.uri,
119
- },
120
- depth,
121
- options,
122
- );
117
+ get [Symbol.toStringTag]() {
118
+ return 'PodiumClientResource';
123
119
  }
124
120
  };
125
- module.exports = PodiumClientResource;
package/lib/response.js CHANGED
@@ -1,17 +1,11 @@
1
- /* eslint-disable no-underscore-dangle */
2
- /* eslint-disable import/order */
1
+ const inspect = Symbol.for('nodejs.util.inspect.custom');
3
2
 
4
- 'use strict';
5
-
6
- const util = require('util');
7
-
8
- const _redirect = Symbol('podium:client:response:redirect');
9
- const _content = Symbol('podium:client:response:content');
10
- const _headers = Symbol('podium:client:response:headers');
11
- const _css = Symbol('podium:client:response:css');
12
- const _js = Symbol('podium:client:response:js');
13
-
14
- const PodiumClientResponse = class PodiumClientResponse {
3
+ export default class PodiumClientResponse {
4
+ #redirect;
5
+ #content;
6
+ #headers;
7
+ #css;
8
+ #js;
15
9
  constructor({
16
10
  content = '',
17
11
  headers = {},
@@ -19,57 +13,66 @@ const PodiumClientResponse = class PodiumClientResponse {
19
13
  js = [],
20
14
  redirect = null,
21
15
  } = {}) {
22
- this[_redirect] = redirect;
23
- this[_content] = content;
24
- this[_headers] = headers;
25
- this[_css] = css;
26
- this[_js] = js;
16
+ this.#redirect = redirect;
17
+ this.#content = content;
18
+ this.#headers = headers;
19
+ this.#css = css;
20
+ this.#js = js;
27
21
  }
28
22
 
29
23
  get content() {
30
- return this[_content];
24
+ return this.#content;
31
25
  }
32
26
 
33
27
  get headers() {
34
- return this[_headers];
28
+ return this.#headers;
35
29
  }
36
30
 
37
31
  get css() {
38
- return this[_css];
32
+ return this.#css;
39
33
  }
40
34
 
41
35
  get js() {
42
- return this[_js];
36
+ return this.#js;
43
37
  }
44
38
 
45
39
  get redirect() {
46
- return this[_redirect];
47
- }
48
-
49
- get [Symbol.toStringTag]() {
50
- return 'PodiumClientResponse';
40
+ return this.#redirect;
51
41
  }
52
42
 
53
43
  toJSON() {
54
44
  return {
55
- redirect: this[_redirect],
56
- content: this[_content],
57
- headers: this[_headers],
58
- css: this[_css],
59
- js: this[_js],
45
+ redirect: this.redirect,
46
+ content: this.content,
47
+ headers: this.headers,
48
+ css: this.css,
49
+ js: this.js,
60
50
  };
61
51
  }
62
52
 
63
53
  toString() {
64
- return this[_content];
54
+ return this.content;
65
55
  }
66
56
 
67
57
  [Symbol.toPrimitive]() {
68
- return this[_content];
58
+ return this.content;
69
59
  }
70
60
 
71
- [util.inspect.custom](depth, options) {
72
- return util.inspect(this.toJSON(), depth, options);
61
+ [inspect]() {
62
+ return {
63
+ referrerpolicy: this.referrerpolicy,
64
+ crossorigin: this.crossorigin,
65
+ integrity: this.integrity,
66
+ nomodule: this.nomodule,
67
+ value: this.value,
68
+ async: this.async,
69
+ defer: this.defer,
70
+ type: this.type,
71
+ data: this.data,
72
+ };
73
+ }
74
+
75
+ get [Symbol.toStringTag]() {
76
+ return 'PodiumClientResponse';
73
77
  }
74
78
  };
75
- module.exports = PodiumClientResponse;
package/lib/state.js CHANGED
@@ -1,18 +1,13 @@
1
- /* eslint-disable no-underscore-dangle */
2
- /* eslint-disable import/order */
1
+ import EventEmitter from 'events';
3
2
 
4
- 'use strict';
3
+ const inspect = Symbol.for('nodejs.util.inspect.custom');
5
4
 
6
- const EventEmitter = require('events');
7
- const util = require('util');
8
-
9
- const _thresholdTimer = Symbol('podium:client:state:threshold:timer');
10
- const _threshold = Symbol('podium:client:state:threshold');
11
- const _maxTimer = Symbol('podium:client:state:max:timer');
12
- const _state = Symbol('podium:client:state:state');
13
- const _max = Symbol('podium:client:state:max');
14
-
15
- const PodiumClientState = class PodiumClientState extends EventEmitter {
5
+ export default class PodiumClientState extends EventEmitter {
6
+ #thresholdTimer;
7
+ #threshold;
8
+ #maxTimer;
9
+ #state;
10
+ #max;
16
11
  constructor({
17
12
  resolveThreshold = 10 * 1000,
18
13
  resolveMax = 4 * 60 * 1000,
@@ -23,22 +18,22 @@ const PodiumClientState = class PodiumClientState extends EventEmitter {
23
18
  'argument "resolveMax" must be larger than "resolveThreshold" argument',
24
19
  );
25
20
 
26
- this[_thresholdTimer] = undefined;
27
- this[_threshold] = resolveThreshold;
28
- this[_maxTimer] = undefined;
29
- this[_state] = 'instantiated';
30
- this[_max] = resolveMax;
21
+ this.#thresholdTimer = undefined;
22
+ this.#threshold = resolveThreshold;
23
+ this.#maxTimer = undefined;
24
+ this.#state = 'instantiated';
25
+ this.#max = resolveMax;
31
26
  }
32
27
 
33
28
  get status() {
34
- return this[_state];
29
+ return this.#state;
35
30
  }
36
31
 
37
32
  setInitializingState() {
38
33
  const state = 'initializing';
39
34
 
40
- if (this[_state] === 'instantiated') {
41
- this[_state] = state;
35
+ if (this.#state === 'instantiated') {
36
+ this.#state = state;
42
37
  this.emit('state', state);
43
38
  }
44
39
  }
@@ -46,10 +41,10 @@ const PodiumClientState = class PodiumClientState extends EventEmitter {
46
41
  setStableState() {
47
42
  const state = 'stable';
48
43
 
49
- clearTimeout(this[_maxTimer]);
44
+ clearTimeout(this.#maxTimer);
50
45
 
51
- if (this[_state] !== state) {
52
- this[_state] = state;
46
+ if (this.#state !== state) {
47
+ this.#state = state;
53
48
  this.emit('state', state);
54
49
  }
55
50
  }
@@ -57,8 +52,8 @@ const PodiumClientState = class PodiumClientState extends EventEmitter {
57
52
  setUnhealthyState() {
58
53
  const state = 'unhealthy';
59
54
 
60
- if (this[_state] !== state) {
61
- this[_state] = state;
55
+ if (this.#state !== state) {
56
+ this.#state = state;
62
57
  this.emit('state', state);
63
58
  }
64
59
  }
@@ -68,53 +63,54 @@ const PodiumClientState = class PodiumClientState extends EventEmitter {
68
63
  // timeout. This max timeout will be reset if a stable state is reached
69
64
  // again before it times out. If this times out, unhealthy state is
70
65
  // reached.
71
- if (this[_state] === 'stable' || this[_state] === 'initializing') {
72
- this[_maxTimer] = setTimeout(
66
+ if (this.#state === 'stable' || this.#state === 'initializing') {
67
+ this.#maxTimer = setTimeout(
73
68
  this.setUnhealthyState.bind(this),
74
- this[_max],
69
+ this.#max,
75
70
  );
76
- this[_maxTimer].unref();
71
+ this.#maxTimer.unref();
77
72
  }
78
73
 
79
74
  // If state is unhealthy, continue keeping it unhealthy.
80
- const state = this[_state] === 'unhealthy' ? 'unhealthy' : 'unstable';
75
+ const state = this.#state === 'unhealthy' ? 'unhealthy' : 'unstable';
81
76
 
82
77
  // Clear any possible previous threshold timeouts in case because we are
83
78
  // there was a call to this method within the threshold time.
84
- clearTimeout(this[_thresholdTimer]);
79
+ clearTimeout(this.#thresholdTimer);
85
80
 
86
- if (this[_state] !== state) {
87
- this[_state] = state;
81
+ if (this.#state !== state) {
82
+ this.#state = state;
88
83
  this.emit('state', state);
89
84
  }
90
85
 
91
86
  // Set a threshold timeout. If no further calls is done to this method a
92
87
  // the timeout will call method for setting stable state.
93
- this[_thresholdTimer] = setTimeout(
88
+ this.#thresholdTimer = setTimeout(
94
89
  this.setStableState.bind(this),
95
- this[_threshold],
90
+ this.#threshold,
96
91
  );
97
- this[_thresholdTimer].unref();
92
+ this.#thresholdTimer.unref();
98
93
  }
99
94
 
100
95
  reset() {
101
- clearTimeout(this[_thresholdTimer]);
102
- clearTimeout(this[_maxTimer]);
103
- this[_state] = 'instantiated';
96
+ clearTimeout(this.#thresholdTimer);
97
+ clearTimeout(this.#maxTimer);
98
+ this.#state = 'instantiated';
104
99
  }
105
100
 
106
- get [Symbol.toStringTag]() {
107
- return 'PodiumClientState';
101
+ toJSON() {
102
+ return {
103
+ status: this.status,
104
+ };
108
105
  }
109
106
 
110
- toJSON() {
107
+ [inspect]() {
111
108
  return {
112
109
  status: this.status,
113
110
  };
114
111
  }
115
112
 
116
- [util.inspect.custom](depth, options) {
117
- return util.inspect(this.toJSON(), depth, options);
113
+ get [Symbol.toStringTag]() {
114
+ return 'PodiumClientState';
118
115
  }
119
116
  };
120
- module.exports = PodiumClientState;
package/lib/utils.js CHANGED
@@ -1,7 +1,3 @@
1
- 'use strict';
2
-
3
- const { HttpIncoming } = require('@podium/utils');
4
-
5
1
  /**
6
2
  * Checks if a header Oject has a header.
7
3
  * Will return true if the header exist and are not an empty
@@ -13,7 +9,7 @@ const { HttpIncoming } = require('@podium/utils');
13
9
  * @returns {Boolean}
14
10
  */
15
11
 
16
- module.exports.isHeaderDefined = (headers, header) => {
12
+ export const isHeaderDefined = (headers, header) => {
17
13
  if (headers[header] === undefined || headers[header].trim() === '') {
18
14
  return false;
19
15
  }
@@ -30,44 +26,10 @@ module.exports.isHeaderDefined = (headers, header) => {
30
26
  * @returns {Boolean}
31
27
  */
32
28
 
33
- module.exports.hasManifestChange = item => {
29
+ export const hasManifestChange = item => {
34
30
  const oldVersion = item.oldVal ? item.oldVal.version : '';
35
31
  const newVersion = item.newVal ? item.newVal.version : '';
36
32
  return oldVersion !== newVersion;
37
33
  };
38
34
 
39
- /**
40
- * Check if a value is a HttpIncoming object or not. If not, it
41
- * assume the incoming value is a context
42
- *
43
- * @param {Object} incoming A object
44
- *
45
- * @returns {HttpIncoming}
46
- */
47
-
48
- function incomingDeprecated() {
49
- if (!incomingDeprecated.warned) {
50
- incomingDeprecated.warned = true;
51
- process.emitWarning(
52
- 'Passing an arbitrary value as the first argument to .fetch() and .stream() is deprecated. In a future version it will be required to pass in a HttpIncoming object as a the first argument to these methods. For further information and how to migrate, please see https://podium-lib.io/blog/2019/06/14/version-4.0.0#httpincoming-replaces-context-argument',
53
- 'DeprecationWarning',
54
- );
55
- }
56
- }
57
-
58
- module.exports.validateIncoming = (incoming = {}) => {
59
- if (
60
- Object.prototype.toString.call(incoming) ===
61
- '[object PodiumHttpIncoming]'
62
- ) {
63
- return incoming;
64
- }
65
-
66
- incomingDeprecated();
67
-
68
- const inc = new HttpIncoming({
69
- headers: {},
70
- });
71
- inc.context = incoming;
72
- return inc;
73
- };
35
+ export const validateIncoming = (incoming = {}) => (Object.prototype.toString.call(incoming) === '[object PodiumHttpIncoming]');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@podium/client",
3
- "version": "4.5.22",
4
- "main": "lib/client.js",
3
+ "version": "5.0.0-next.10",
4
+ "type": "module",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "micro services",
@@ -24,49 +24,45 @@
24
24
  "index.d.ts",
25
25
  "README.md",
26
26
  "LICENSE",
27
+ "dist",
27
28
  "lib"
28
29
  ],
30
+ "main": "./lib/client.js",
29
31
  "types": "index.d.ts",
30
32
  "scripts": {
31
33
  "lint": "eslint .",
32
34
  "lint:fix": "eslint --fix .",
33
- "test": "tap --no-cov test/*.js",
34
- "lint:format": "eslint --fix .",
35
- "precommit": "lint-staged"
35
+ "test": "tap --no-check-coverage"
36
36
  },
37
37
  "dependencies": {
38
- "@hapi/boom": "^9.0.0",
38
+ "@hapi/boom": "^10.0.0",
39
39
  "@metrics/client": "2.5.0",
40
- "@podium/schemas": "4.1.32",
41
- "@podium/utils": "4.4.36",
40
+ "@podium/schemas": "5.0.0-next.4",
41
+ "@podium/utils": "5.0.0-next.6",
42
42
  "abslog": "2.4.0",
43
43
  "http-cache-semantics": "^4.0.3",
44
44
  "lodash.clonedeep": "^4.5.0",
45
- "readable-stream": "^3.4.0",
46
45
  "request": "^2.88.0",
47
46
  "ttl-mem-cache": "4.1.0"
48
47
  },
49
48
  "devDependencies": {
50
- "@podium/test-utils": "2.4.1",
49
+ "@podium/test-utils": "2.5.2",
51
50
  "@semantic-release/changelog": "6.0.1",
52
- "@semantic-release/commit-analyzer": "9.0.2",
53
51
  "@semantic-release/git": "10.0.1",
54
- "@semantic-release/github": "8.0.2",
55
- "@semantic-release/npm": "8.0.3",
56
- "@semantic-release/release-notes-generator": "10.0.3",
57
- "@sinonjs/fake-timers": "9.1.0",
52
+ "@sinonjs/fake-timers": "9.1.2",
53
+ "@babel/eslint-parser": "7.18.9",
58
54
  "benchmark": "2.1.4",
59
- "eslint": "7.32.0",
60
- "eslint-config-airbnb-base": "14.2.1",
61
- "eslint-config-prettier": "8.4.0",
62
- "eslint-plugin-import": "2.25.4",
63
- "eslint-plugin-prettier": "3.4.1",
64
- "express": "4.17.3",
55
+ "eslint": "8.23.0",
56
+ "eslint-config-airbnb-base": "15.0.0",
57
+ "eslint-config-prettier": "8.5.0",
58
+ "eslint-plugin-import": "2.26.0",
59
+ "eslint-plugin-prettier": "4.2.1",
60
+ "express": "4.18.1",
65
61
  "get-stream": "6.0.1",
66
62
  "http-proxy": "1.18.1",
67
- "is-stream": "2.0.1",
68
- "prettier": "2.5.1",
69
- "semantic-release": "18.0.1",
70
- "tap": "15.1.6"
63
+ "is-stream": "3.0.0",
64
+ "prettier": "2.7.1",
65
+ "semantic-release": "19.0.5",
66
+ "tap": "16.3.0"
71
67
  }
72
68
  }