@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.
@@ -1,36 +1,28 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
2
 
3
- 'use strict';
4
-
5
- const { PassThrough } = require('readable-stream');
6
- const assert = require('assert');
7
- const utils = require('./utils');
8
-
9
- const _rejectUnauthorized = Symbol('podium:httpoutgoing:rejectUnauthorized');
10
- const _killRecursions = Symbol('podium:httpoutgoing:killrecursions');
11
- const _killThreshold = Symbol('podium:httpoutgoing:killthreshold');
12
- const _redirectable = Symbol('podium:httpoutgoing:redirectable');
13
- const _reqOptions = Symbol('podium:httpoutgoing:reqoptions');
14
- const _resolveCss = Symbol('podium:httpoutgoing:resolvecss');
15
- const _resolveJs = Symbol('podium:httpoutgoing:resolvejs');
16
- const _throwable = Symbol('podium:httpoutgoing:throwable');
17
- const _manifest = Symbol('podium:httpoutgoing:manifest');
18
- const _incoming = Symbol('podium:httpoutgoing:incoming');
19
- const _redirect = Symbol('podium:httpoutgoing:redirect');
20
- const _timeout = Symbol('podium:httpoutgoing:timeout');
21
- const _success = Symbol('podium:httpoutgoing:success');
22
- const _headers = Symbol('podium:httpoutgoing:headers');
23
- const _maxAge = Symbol('podium:httpoutgoing:maxage');
24
- const _status = Symbol('podium:httpoutgoing:status');
25
- const _name = Symbol('podium:httpoutgoing:name');
26
- const _uri = Symbol('podium:httpoutgoing:uri');
27
-
28
- const PodletClientHttpOutgoing = class PodletClientHttpOutgoing extends PassThrough {
3
+ import { PassThrough } from 'stream';
4
+ import assert from 'assert';
5
+
6
+ export default class PodletClientHttpOutgoing extends PassThrough {
7
+ #rejectUnauthorized;
8
+ #killRecursions;
9
+ #killThreshold;
10
+ #redirectable;
11
+ #reqOptions;
12
+ #throwable;
13
+ #manifest;
14
+ #incoming;
15
+ #redirect;
16
+ #timeout;
17
+ #success;
18
+ #headers;
19
+ #maxAge;
20
+ #status;
21
+ #name;
22
+ #uri;
29
23
  constructor(
30
24
  {
31
25
  rejectUnauthorized = true,
32
- resolveCss = false,
33
- resolveJs = false,
34
26
  throwable = false,
35
27
  redirectable = false,
36
28
  retries = 4,
@@ -50,47 +42,44 @@ const PodletClientHttpOutgoing = class PodletClientHttpOutgoing extends PassThro
50
42
  );
51
43
 
52
44
  // If requests to https sites should reject on unsigned sertificates
53
- this[_rejectUnauthorized] = rejectUnauthorized;
45
+ this.#rejectUnauthorized = rejectUnauthorized;
54
46
 
55
47
  // A HttpIncoming object
56
- this[_incoming] = utils.validateIncoming(incoming);
48
+ this.#incoming = incoming;
57
49
 
58
50
  // Kill switch for breaking the recursive promise chain
59
51
  // in case it is never able to completely resolve
60
- this[_killRecursions] = 0;
61
- this[_killThreshold] = retries;
52
+ this.#killRecursions = 0;
53
+ this.#killThreshold = retries;
62
54
 
63
55
  // Options to be appended to the content request
64
- this[_reqOptions] = {
56
+ this.#reqOptions = {
65
57
  pathname: '',
66
58
  query: {},
67
59
  headers: {},
68
60
  ...reqOptions,
69
61
  };
70
62
 
71
- // If relative CSS/JS paths should be resolved into an absolute
72
- // path based on the URI to the podlets manifest
73
- this[_resolveCss] = resolveCss;
74
- this[_resolveJs] = resolveJs;
75
-
76
63
  // In the case of failure, should the resource throw or not
77
- this[_throwable] = throwable;
64
+ this.#throwable = throwable;
78
65
 
79
66
  // Manifest which is either retrieved from the registry or
80
67
  // remote podlet (in other words its not saved in registry yet)
81
- this[_manifest] = {
68
+ this.#manifest = {
82
69
  _fallback: '',
83
70
  };
84
71
 
85
72
  // How long before a request should time out
86
- this[_timeout] = timeout;
73
+ this.#timeout = timeout;
87
74
 
88
75
  // Done indicator to break the promise chain
89
76
  // Set to true when content is served
90
- this[_success] = false;
77
+ this.#success = false;
78
+
79
+ this.#headers = {};
91
80
 
92
81
  // How long the manifest should be cached before refetched
93
- this[_maxAge] = maxAge;
82
+ this.#maxAge = maxAge;
94
83
 
95
84
  // What status the manifest is in. This is used to tell what actions need to
96
85
  // be performed throughout the resolving process to complete a request.
@@ -100,149 +89,140 @@ const PodletClientHttpOutgoing = class PodletClientHttpOutgoing extends PassThro
100
89
  // "fresh" - the manifest has been fetched but is not stored in cache yet
101
90
  // "cached" - the manifest was retrieved from cache
102
91
  // "stale" - the manifest is outdated, a new manifest needs to be fetched
103
- this[_status] = 'empty';
92
+ this.#status = 'empty';
104
93
 
105
94
  // Name of the resource (name given to client)
106
- this[_name] = name;
95
+ this.#name = name;
107
96
 
108
97
  // URI to manifest
109
- this[_uri] = uri;
98
+ this.#uri = uri;
110
99
 
111
100
  // Whether the user can handle redirects manually.
112
- this[_redirectable] = redirectable;
101
+ this.#redirectable = redirectable;
113
102
 
114
103
  // When redirectable is true, this object should be populated with redirect information
115
104
  // such that a user can perform manual redirection
116
- this[_redirect] = null;
117
- }
118
-
119
- get [Symbol.toStringTag]() {
120
- return 'PodletClientHttpOutgoing';
105
+ this.#redirect = null;
121
106
  }
122
107
 
123
108
  get rejectUnauthorized() {
124
- return this[_rejectUnauthorized];
109
+ return this.#rejectUnauthorized;
125
110
  }
126
111
 
127
112
  get reqOptions() {
128
- return this[_reqOptions];
129
- }
130
-
131
- get resolveCss() {
132
- return this[_resolveCss];
133
- }
134
-
135
- get resolveJs() {
136
- return this[_resolveJs];
113
+ return this.#reqOptions;
137
114
  }
138
115
 
139
116
  get throwable() {
140
- return this[_throwable];
117
+ return this.#throwable;
141
118
  }
142
119
 
143
120
  get manifest() {
144
- return this[_manifest];
121
+ return this.#manifest;
145
122
  }
146
123
 
147
124
  set manifest(obj) {
148
- this[_manifest] = obj;
125
+ this.#manifest = obj;
149
126
  }
150
127
 
151
128
  get fallback() {
152
- return this[_manifest]._fallback;
129
+ return this.#manifest._fallback;
153
130
  }
154
131
 
155
132
  set fallback(value) {
156
- this[_manifest]._fallback = value;
133
+ this.#manifest._fallback = value;
157
134
  }
158
135
 
159
136
  get timeout() {
160
- return this[_timeout];
137
+ return this.#timeout;
161
138
  }
162
139
 
163
140
  get success() {
164
- return this[_success];
141
+ return this.#success;
165
142
  }
166
143
 
167
144
  set success(value) {
168
- this[_success] = value;
145
+ this.#success = value;
169
146
  }
170
147
 
171
148
  get context() {
172
- return this[_incoming].context;
149
+ return this.#incoming.context;
173
150
  }
174
151
 
175
152
  get headers() {
176
- return this[_headers];
153
+ return this.#headers;
177
154
  }
178
155
 
179
156
  set headers(value) {
180
- this[_headers] = value;
157
+ this.#headers = value;
181
158
  }
182
159
 
183
160
  get maxAge() {
184
- return this[_maxAge];
161
+ return this.#maxAge;
185
162
  }
186
163
 
187
164
  set maxAge(value) {
188
- this[_maxAge] = value;
165
+ this.#maxAge = value;
189
166
  }
190
167
 
191
168
  get status() {
192
- return this[_status];
169
+ return this.#status;
193
170
  }
194
171
 
195
172
  set status(value) {
196
- this[_status] = value;
173
+ this.#status = value;
197
174
  }
198
175
 
199
176
  get name() {
200
- return this[_name];
177
+ return this.#name;
201
178
  }
202
179
 
203
180
  get manifestUri() {
204
- return this[_uri];
181
+ return this.#uri;
205
182
  }
206
183
 
207
184
  get fallbackUri() {
208
- return this[_manifest].fallback;
185
+ return this.#manifest.fallback;
209
186
  }
210
187
 
211
188
  get contentUri() {
212
- return this[_manifest].content;
189
+ return this.#manifest.content;
213
190
  }
214
191
 
215
192
  get kill() {
216
- return this[_killRecursions] === this[_killThreshold];
193
+ return this.#killRecursions === this.#killThreshold;
217
194
  }
218
195
 
219
196
  get recursions() {
220
- return this[_killRecursions];
197
+ return this.#killRecursions;
221
198
  }
222
199
 
223
200
  set recursions(value) {
224
- this[_killRecursions] = value;
201
+ this.#killRecursions = value;
225
202
  }
226
203
 
227
204
  get redirect() {
228
- return this[_redirect];
205
+ return this.#redirect;
229
206
  }
230
207
 
231
208
  set redirect(value) {
232
- this[_redirect] = value;
209
+ this.#redirect = value;
233
210
  }
234
211
 
235
212
  get redirectable() {
236
- return this[_redirectable];
213
+ return this.#redirectable;
237
214
  }
238
215
 
239
216
  set redirectable(value) {
240
- this[_redirectable] = value;
217
+ this.#redirectable = value;
241
218
  }
242
219
 
243
220
  pushFallback() {
244
- this.push(this[_manifest]._fallback);
221
+ this.push(this.#manifest._fallback);
245
222
  this.push(null);
246
223
  }
224
+
225
+ get [Symbol.toStringTag]() {
226
+ return 'PodletClientHttpOutgoing';
227
+ }
247
228
  };
248
- module.exports = PodletClientHttpOutgoing;
@@ -1,40 +1,30 @@
1
1
  /* eslint-disable no-plusplus */
2
2
  /* eslint-disable no-param-reassign */
3
3
 
4
- 'use strict';
4
+ import clonedeep from 'lodash.clonedeep';
5
+ import abslog from 'abslog';
6
+ import assert from 'assert';
5
7
 
6
- const clonedeep = require('lodash.clonedeep');
7
- const abslog = require('abslog');
8
- const assert = require('assert');
9
-
10
- module.exports = class PodletClientCacheResolver {
8
+ export default class PodletClientCacheResolver {
9
+ #registry;
10
+ #log;
11
11
  constructor(registry, options = {}) {
12
12
  assert(
13
13
  registry,
14
14
  'you must pass a "registry" object to the PodletClientCacheResolver constructor',
15
15
  );
16
-
17
- Object.defineProperty(this, 'registry', {
18
- value: registry,
19
- });
20
-
21
- Object.defineProperty(this, 'log', {
22
- value: abslog(options.logger),
23
- });
24
- }
25
-
26
- get [Symbol.toStringTag]() {
27
- return 'PodletClientCacheResolver';
16
+ this.#registry = registry;
17
+ this.#log = abslog(options.logger);
28
18
  }
29
19
 
30
20
  load(outgoing) {
31
21
  return new Promise(resolve => {
32
22
  if (outgoing.status !== 'stale') {
33
- const cached = this.registry.get(outgoing.name);
23
+ const cached = this.#registry.get(outgoing.name);
34
24
  if (cached) {
35
25
  outgoing.manifest = clonedeep(cached);
36
26
  outgoing.status = 'cached';
37
- this.log.debug(
27
+ this.#log.debug(
38
28
  `loaded manifest from cache - resource: ${outgoing.name}`,
39
29
  );
40
30
  }
@@ -46,12 +36,12 @@ module.exports = class PodletClientCacheResolver {
46
36
  save(outgoing) {
47
37
  return new Promise(resolve => {
48
38
  if (outgoing.status === 'fresh') {
49
- this.registry.set(
39
+ this.#registry.set(
50
40
  outgoing.name,
51
41
  outgoing.manifest,
52
42
  outgoing.maxAge,
53
43
  );
54
- this.log.debug(
44
+ this.#log.debug(
55
45
  `saved manifest to cache - resource: ${outgoing.name}`,
56
46
  );
57
47
  }
@@ -61,4 +51,8 @@ module.exports = class PodletClientCacheResolver {
61
51
  resolve(outgoing);
62
52
  });
63
53
  }
54
+
55
+ get [Symbol.toStringTag]() {
56
+ return 'PodletClientCacheResolver';
57
+ }
64
58
  };
@@ -1,70 +1,64 @@
1
1
  /* eslint-disable no-param-reassign */
2
2
 
3
- 'use strict';
4
-
5
- const { pipeline } = require('readable-stream');
6
- const Metrics = require('@metrics/client');
7
- const request = require('request');
8
- const abslog = require('abslog');
9
- const putils = require('@podium/utils');
10
- const { Boom, badGateway } = require('@hapi/boom');
11
- const Response = require('./response');
12
- const utils = require('./utils');
13
- const pkg = require('../package.json');
3
+ import { pipeline } from 'stream';
4
+ import Metrics from '@metrics/client';
5
+ import request from 'request';
6
+ import abslog from 'abslog';
7
+ import * as putils from '@podium/utils';
8
+ import { Boom, badGateway } from '@hapi/boom';
14
9
 
15
- const UA_STRING = `${pkg.name} ${pkg.version}`;
16
-
17
- module.exports = class PodletClientContentResolver {
18
- constructor(options = {}) {
19
- Object.defineProperty(this, 'log', {
20
- value: abslog(options.logger),
21
- });
22
-
23
- Object.defineProperty(this, 'clientName', {
24
- enumerable: false,
25
- value: options.clientName,
26
- });
27
-
28
- Object.defineProperty(this, 'metrics', {
29
- enumerable: true,
30
- value: new Metrics(),
31
- });
10
+ import { join, dirname } from 'path';
11
+ import { fileURLToPath } from 'url';
12
+ import fs from 'fs';
13
+ import * as utils from './utils.js';
14
+ import Response from './response.js';
32
15
 
33
- Object.defineProperty(this, 'httpsAgent', {
34
- value: options.httpsAgent,
35
- });
16
+ const currentDirectory = dirname(fileURLToPath(import.meta.url));
36
17
 
37
- Object.defineProperty(this, 'httpAgent', {
38
- value: options.httpAgent,
39
- });
18
+ const pkgJson = fs.readFileSync(join(currentDirectory, '../package.json'), 'utf-8');
19
+ const pkg = JSON.parse(pkgJson);
40
20
 
41
- this.metrics.on('error', error => {
42
- this.log.error(
43
- 'Error emitted by metric stream in @podium/client module',
44
- error,
45
- );
46
- });
21
+ const UA_STRING = `${pkg.name} ${pkg.version}`;
47
22
 
48
- this.histogram = this.metrics.histogram({
23
+ export default class PodletClientContentResolver {
24
+ #log;
25
+ #metrics;
26
+ #histogram;
27
+ #httpAgent;
28
+ #httpsAgent;
29
+ constructor(options = {}) {
30
+ const name = options.clientName;
31
+ this.#log = abslog(options.logger);
32
+ this.#httpAgent = options.httpAgent;
33
+ this.#httpsAgent = options.httpsAgent;
34
+ this.#metrics = new Metrics();
35
+ this.#histogram = this.#metrics.histogram({
49
36
  name: 'podium_client_resolver_content_resolve',
50
37
  description: 'Time taken for success/failure of content request',
51
38
  labels: {
52
- name: this.clientName,
39
+ name,
53
40
  status: null,
54
41
  podlet: null,
55
42
  },
56
43
  buckets: [0.001, 0.01, 0.1, 0.5, 1, 2, 10],
57
44
  });
45
+
46
+ this.#metrics.on('error', error => {
47
+ this.#log.error(
48
+ 'Error emitted by metric stream in @podium/client module',
49
+ error,
50
+ );
51
+ });
58
52
  }
59
53
 
60
- get [Symbol.toStringTag]() {
61
- return 'PodletClientContentResolver';
54
+ get metrics() {
55
+ return this.#metrics;
62
56
  }
63
57
 
64
58
  resolve(outgoing) {
65
59
  return new Promise((resolve, reject) => {
66
60
  if (outgoing.kill && outgoing.throwable) {
67
- this.log.warn(
61
+ this.#log.warn(
68
62
  `recursion detected - failed to resolve fetching of podlet ${outgoing.recursions} times - throwing - resource: ${outgoing.name} - url: ${outgoing.manifestUri}`,
69
63
  );
70
64
  reject(
@@ -76,7 +70,7 @@ module.exports = class PodletClientContentResolver {
76
70
  }
77
71
 
78
72
  if (outgoing.kill) {
79
- this.log.warn(
73
+ this.#log.warn(
80
74
  `recursion detected - failed to resolve fetching of podlet ${outgoing.recursions} times - serving fallback - resource: ${outgoing.name} - url: ${outgoing.manifestUri}`,
81
75
  );
82
76
  outgoing.success = true;
@@ -86,7 +80,7 @@ module.exports = class PodletClientContentResolver {
86
80
  }
87
81
 
88
82
  if (outgoing.status === 'empty' && outgoing.throwable) {
89
- this.log.warn(
83
+ this.#log.warn(
90
84
  `no manifest available - cannot read content - throwing - resource: ${outgoing.name} - url: ${outgoing.manifestUri}`,
91
85
  );
92
86
  reject(
@@ -96,7 +90,7 @@ module.exports = class PodletClientContentResolver {
96
90
  }
97
91
 
98
92
  if (outgoing.status === 'empty') {
99
- this.log.warn(
93
+ this.#log.warn(
100
94
  `no manifest available - cannot read content - serving fallback - resource: ${outgoing.name} - url: ${outgoing.manifestUri}`,
101
95
  );
102
96
  outgoing.success = true;
@@ -117,8 +111,8 @@ module.exports = class PodletClientContentResolver {
117
111
  timeout: outgoing.timeout,
118
112
  method: 'GET',
119
113
  agent: outgoing.contentUri.startsWith('https://')
120
- ? this.httpsAgent
121
- : this.httpAgent,
114
+ ? this.#httpsAgent
115
+ : this.#httpAgent,
122
116
  uri: putils.uriBuilder(
123
117
  outgoing.reqOptions.pathname,
124
118
  outgoing.contentUri,
@@ -131,13 +125,13 @@ module.exports = class PodletClientContentResolver {
131
125
  reqOptions.followRedirect = false;
132
126
  }
133
127
 
134
- const timer = this.histogram.timer({
128
+ const timer = this.#histogram.timer({
135
129
  labels: {
136
130
  podlet: outgoing.name,
137
131
  },
138
132
  });
139
133
 
140
- this.log.debug(
134
+ this.#log.debug(
141
135
  `start reading content from remote resource - manifest version is ${outgoing.manifest.version} - resource: ${outgoing.name} - url: ${outgoing.contentUri}`,
142
136
  );
143
137
 
@@ -153,7 +147,7 @@ module.exports = class PodletClientContentResolver {
153
147
  },
154
148
  });
155
149
 
156
- this.log.warn(
150
+ this.#log.debug(
157
151
  `remote resource responded with non 200 http status code for content - code: ${response.statusCode} - resource: ${outgoing.name} - url: ${outgoing.contentUri}`,
158
152
  );
159
153
 
@@ -176,7 +170,7 @@ module.exports = class PodletClientContentResolver {
176
170
  },
177
171
  });
178
172
 
179
- this.log.warn(
173
+ this.#log.warn(
180
174
  `remote resource responded with non 200 http status code for content - code: ${response.statusCode} - resource: ${outgoing.name} - url: ${outgoing.contentUri}`,
181
175
  );
182
176
  outgoing.success = true;
@@ -192,7 +186,7 @@ module.exports = class PodletClientContentResolver {
192
186
  ? response.headers['podlet-version']
193
187
  : undefined;
194
188
 
195
- this.log.debug(
189
+ this.#log.debug(
196
190
  `got head response from remote resource for content - header version is ${response.headers['podlet-version']} - resource: ${outgoing.name} - url: ${outgoing.contentUri}`,
197
191
  );
198
192
 
@@ -206,7 +200,7 @@ module.exports = class PodletClientContentResolver {
206
200
  },
207
201
  });
208
202
 
209
- this.log.info(
203
+ this.#log.info(
210
204
  `podlet version number in header differs from cached version number - aborting request to remote resource for content - resource: ${outgoing.name} - url: ${outgoing.contentUri}`,
211
205
  );
212
206
  r.abort();
@@ -234,9 +228,9 @@ module.exports = class PodletClientContentResolver {
234
228
  }),
235
229
  );
236
230
 
237
- pipeline(r, outgoing, err => {
231
+ pipeline([r, outgoing], (err) => {
238
232
  if (err) {
239
- this.log.warn('error while piping content stream', err);
233
+ this.#log.warn('error while piping content stream', err);
240
234
  }
241
235
  });
242
236
  });
@@ -250,7 +244,7 @@ module.exports = class PodletClientContentResolver {
250
244
  },
251
245
  });
252
246
 
253
- this.log.warn(
247
+ this.#log.warn(
254
248
  `could not create network connection to remote resource when trying to request content - resource: ${outgoing.name} - url: ${outgoing.contentUri}`,
255
249
  );
256
250
  reject(
@@ -268,7 +262,7 @@ module.exports = class PodletClientContentResolver {
268
262
  },
269
263
  });
270
264
 
271
- this.log.warn(
265
+ this.#log.warn(
272
266
  `could not create network connection to remote resource when trying to request content - resource: ${outgoing.name} - url: ${outgoing.contentUri}`,
273
267
  );
274
268
 
@@ -285,7 +279,7 @@ module.exports = class PodletClientContentResolver {
285
279
  },
286
280
  });
287
281
 
288
- this.log.debug(
282
+ this.#log.debug(
289
283
  `successfully read content from remote resource - resource: ${outgoing.name} - url: ${outgoing.contentUri}`,
290
284
  );
291
285
 
@@ -293,4 +287,8 @@ module.exports = class PodletClientContentResolver {
293
287
  });
294
288
  });
295
289
  }
290
+
291
+ get [Symbol.toStringTag]() {
292
+ return 'PodletClientContentResolver';
293
+ }
296
294
  };