@podium/client 5.0.0-next.8 → 5.0.0-next.9

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/client.js DELETED
@@ -1,237 +0,0 @@
1
- 'use strict';
2
-
3
- var EventEmitter = require('events');
4
- var schemas = require('@podium/schemas');
5
- var Metrics = require('@metrics/client');
6
- var abslog = require('abslog');
7
- var Cache = require('ttl-mem-cache');
8
- var http = require('http');
9
- var https = require('https');
10
- var resource = require('./resource.js');
11
- var state = require('./state.js');
12
-
13
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
-
15
- function _interopNamespace(e) {
16
- if (e && e.__esModule) return e;
17
- var n = Object.create(null);
18
- if (e) {
19
- Object.keys(e).forEach(function (k) {
20
- if (k !== 'default') {
21
- var d = Object.getOwnPropertyDescriptor(e, k);
22
- Object.defineProperty(n, k, d.get ? d : {
23
- enumerable: true,
24
- get: function () {
25
- return e[k];
26
- }
27
- });
28
- }
29
- });
30
- }
31
- n['default'] = e;
32
- return Object.freeze(n);
33
- }
34
-
35
- var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
36
- var schemas__namespace = /*#__PURE__*/_interopNamespace(schemas);
37
- var Metrics__default = /*#__PURE__*/_interopDefaultLegacy(Metrics);
38
- var abslog__default = /*#__PURE__*/_interopDefaultLegacy(abslog);
39
- var Cache__default = /*#__PURE__*/_interopDefaultLegacy(Cache);
40
- var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
41
- var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
42
-
43
- const inspect = Symbol.for('nodejs.util.inspect.custom');
44
-
45
- const HTTP_AGENT_OPTIONS = {
46
- keepAlive: true,
47
- maxSockets: 10,
48
- maxFreeSockets: 10,
49
- timeout: 60000,
50
- keepAliveMsecs: 30000,
51
- };
52
-
53
- const HTTPS_AGENT_OPTIONS = {
54
- ...HTTP_AGENT_OPTIONS,
55
- maxCachedSessions: 10,
56
- };
57
-
58
- const HTTP_AGENT = new http__default['default'].Agent(HTTP_AGENT_OPTIONS);
59
-
60
- const HTTPS_AGENT = new https__default['default'].Agent(HTTPS_AGENT_OPTIONS);
61
-
62
- const RETRIES = 4;
63
-
64
- const TIMEOUT = 1000; // 1 seconds
65
-
66
- const MAX_AGE = Infinity;
67
-
68
- class PodiumClient extends EventEmitter__default['default'] {
69
- #resources;
70
- #registry;
71
- #metrics;
72
- #histogram;
73
- #options;
74
- #state;
75
- constructor(options = {}) {
76
- super();
77
- const log = abslog__default['default'](options.logger);
78
-
79
- if (schemas__namespace.name(options.name).error) {
80
- throw new Error(
81
- `The value, "${options.name}", for the required argument "name" on the Client constructor is not defined or not valid.`,
82
- );
83
- }
84
-
85
- this.#options = {
86
- name: '',
87
- retries: RETRIES,
88
- timeout: TIMEOUT,
89
- logger: options.logger,
90
- maxAge: MAX_AGE,
91
- httpAgent: HTTP_AGENT,
92
- httpsAgent: HTTPS_AGENT,
93
- ...options,
94
- };
95
-
96
- this.#state = new state({
97
- resolveThreshold: options.resolveThreshold,
98
- resolveMax: options.resolveMax,
99
- });
100
- this.#state.on('state', state => {
101
- this.emit('state', state);
102
- });
103
-
104
- this.#resources = new Map();
105
-
106
- this.#registry = new Cache__default['default']({
107
- changefeed: true,
108
- ttl: options.maxAge,
109
- });
110
- this.#registry.on('error', error => {
111
- log.error(
112
- 'Error emitted by the registry in @podium/client module',
113
- error,
114
- );
115
- });
116
-
117
- this.#registry.on('set', () => {
118
- this.#state.setUnstableState();
119
- });
120
-
121
- this.#metrics = new Metrics__default['default']();
122
- this.#metrics.on('error', error => {
123
- log.error(
124
- 'Error emitted by metric stream in @podium/client module',
125
- error,
126
- );
127
- });
128
-
129
- this[Symbol.iterator] = () => ({
130
- items: Array.from(this.#resources).map(item => item[1]),
131
- next: function next() {
132
- return {
133
- done: this.items.length === 0,
134
- value: this.items.shift(),
135
- };
136
- },
137
- });
138
-
139
- this.#histogram = this.#metrics.histogram({
140
- name: 'podium_client_refresh_manifests',
141
- description: 'Time taken for podium client to refresh manifests',
142
- labels: { name: this.#options.name },
143
- buckets: [0.001, 0.01, 0.1, 0.5, 1, 2, 10],
144
- });
145
- }
146
-
147
- get registry() {
148
- return this.#registry;
149
- }
150
-
151
- get metrics() {
152
- return this.#metrics;
153
- }
154
-
155
- get state() {
156
- return this.#state.status;
157
- }
158
-
159
- register(options = {}) {
160
- if (schemas__namespace.name(options.name).error)
161
- throw new Error(
162
- `The value, "${options.name}", for the required argument "name" on the .register() method is not defined or not valid.`,
163
- );
164
-
165
- if (schemas__namespace.uriStrict(options.uri).error)
166
- throw new Error(
167
- `The value, "${options.uri}", for the required argument "uri" on the .register() method is not defined or not valid.`,
168
- );
169
-
170
- if (this.#resources.has(options.name)) {
171
- throw new Error(
172
- `Resource with the name "${options.name}" has already been registered.`,
173
- );
174
- }
175
-
176
- const resourceOptions = {
177
- clientName: this.#options.name,
178
- retries: this.#options.retries,
179
- timeout: this.#options.timeout,
180
- logger: this.#options.logger,
181
- maxAge: this.#options.maxAge,
182
- agent: options.uri.startsWith('https://')
183
- ? this.#options.httpsAgent
184
- : this.#options.httpAgent,
185
- ...options,
186
- };
187
- const resource$1 = new resource(
188
- this.#registry,
189
- this.#state,
190
- resourceOptions,
191
- );
192
-
193
- resource$1.metrics.pipe(this.#metrics);
194
-
195
- Object.defineProperty(this, options.name, {
196
- get: () => this.#resources.get(options.name),
197
- set: () => {
198
- throw new Error('Cannot set read-only property.');
199
- },
200
- });
201
-
202
- this.#resources.set(options.name, resource$1);
203
- return resource$1;
204
- }
205
-
206
- dump() {
207
- return this.#registry.dump();
208
- }
209
-
210
- load(dump) {
211
- return this.#registry.load(dump);
212
- }
213
-
214
- async refreshManifests() {
215
- const end = this.#histogram.timer();
216
-
217
- // Don't return this
218
- await Promise.all(
219
- Array.from(this.#resources).map(resource => resource[1].refresh()),
220
- );
221
-
222
- end();
223
- }
224
-
225
- [inspect]() {
226
- return {
227
- metrics: this.metrics,
228
- state: this.state,
229
- };
230
- }
231
-
232
- get [Symbol.toStringTag]() {
233
- return 'PodiumClient';
234
- }
235
- }
236
-
237
- module.exports = PodiumClient;
@@ -1,227 +0,0 @@
1
- 'use strict';
2
-
3
- var stream = require('stream');
4
- var assert = require('assert');
5
-
6
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7
-
8
- var assert__default = /*#__PURE__*/_interopDefaultLegacy(assert);
9
-
10
- /* eslint-disable no-underscore-dangle */
11
-
12
- class PodletClientHttpOutgoing extends stream.PassThrough {
13
- #killRecursions;
14
- #killThreshold;
15
- #redirectable;
16
- #reqOptions;
17
- #throwable;
18
- #manifest;
19
- #incoming;
20
- #redirect;
21
- #timeout;
22
- #success;
23
- #headers;
24
- #maxAge;
25
- #status;
26
- #name;
27
- #uri;
28
- constructor(
29
- {
30
- throwable = false,
31
- redirectable = false,
32
- retries = 4,
33
- timeout,
34
- maxAge,
35
- name = '',
36
- uri,
37
- } = {},
38
- reqOptions,
39
- incoming,
40
- ) {
41
- super();
42
-
43
- assert__default['default'](
44
- uri,
45
- 'you must pass a URI in "options.uri" to the HttpOutgoing constructor',
46
- );
47
-
48
- // A HttpIncoming object
49
- this.#incoming = incoming;
50
-
51
- // Kill switch for breaking the recursive promise chain
52
- // in case it is never able to completely resolve
53
- this.#killRecursions = 0;
54
- this.#killThreshold = retries;
55
-
56
- // Options to be appended to the content request
57
- this.#reqOptions = {
58
- pathname: '',
59
- query: {},
60
- headers: {},
61
- ...reqOptions,
62
- };
63
-
64
- // In the case of failure, should the resource throw or not
65
- this.#throwable = throwable;
66
-
67
- // Manifest which is either retrieved from the registry or
68
- // remote podlet (in other words its not saved in registry yet)
69
- this.#manifest = {
70
- _fallback: '',
71
- };
72
-
73
- // How long before a request should time out
74
- this.#timeout = timeout;
75
-
76
- // Done indicator to break the promise chain
77
- // Set to true when content is served
78
- this.#success = false;
79
-
80
- this.#headers = {};
81
-
82
- // How long the manifest should be cached before refetched
83
- this.#maxAge = maxAge;
84
-
85
- // What status the manifest is in. This is used to tell what actions need to
86
- // be performed throughout the resolving process to complete a request.
87
- //
88
- // The different statuses can be:
89
- // "empty" - there is no manifest available - we are in process of fetching it
90
- // "fresh" - the manifest has been fetched but is not stored in cache yet
91
- // "cached" - the manifest was retrieved from cache
92
- // "stale" - the manifest is outdated, a new manifest needs to be fetched
93
- this.#status = 'empty';
94
-
95
- // Name of the resource (name given to client)
96
- this.#name = name;
97
-
98
- // URI to manifest
99
- this.#uri = uri;
100
-
101
- // Whether the user can handle redirects manually.
102
- this.#redirectable = redirectable;
103
-
104
- // When redirectable is true, this object should be populated with redirect information
105
- // such that a user can perform manual redirection
106
- this.#redirect = null;
107
- }
108
-
109
- get reqOptions() {
110
- return this.#reqOptions;
111
- }
112
-
113
- get throwable() {
114
- return this.#throwable;
115
- }
116
-
117
- get manifest() {
118
- return this.#manifest;
119
- }
120
-
121
- set manifest(obj) {
122
- this.#manifest = obj;
123
- }
124
-
125
- get fallback() {
126
- return this.#manifest._fallback;
127
- }
128
-
129
- set fallback(value) {
130
- this.#manifest._fallback = value;
131
- }
132
-
133
- get timeout() {
134
- return this.#timeout;
135
- }
136
-
137
- get success() {
138
- return this.#success;
139
- }
140
-
141
- set success(value) {
142
- this.#success = value;
143
- }
144
-
145
- get context() {
146
- return this.#incoming.context;
147
- }
148
-
149
- get headers() {
150
- return this.#headers;
151
- }
152
-
153
- set headers(value) {
154
- this.#headers = value;
155
- }
156
-
157
- get maxAge() {
158
- return this.#maxAge;
159
- }
160
-
161
- set maxAge(value) {
162
- this.#maxAge = value;
163
- }
164
-
165
- get status() {
166
- return this.#status;
167
- }
168
-
169
- set status(value) {
170
- this.#status = value;
171
- }
172
-
173
- get name() {
174
- return this.#name;
175
- }
176
-
177
- get manifestUri() {
178
- return this.#uri;
179
- }
180
-
181
- get fallbackUri() {
182
- return this.#manifest.fallback;
183
- }
184
-
185
- get contentUri() {
186
- return this.#manifest.content;
187
- }
188
-
189
- get kill() {
190
- return this.#killRecursions === this.#killThreshold;
191
- }
192
-
193
- get recursions() {
194
- return this.#killRecursions;
195
- }
196
-
197
- set recursions(value) {
198
- this.#killRecursions = value;
199
- }
200
-
201
- get redirect() {
202
- return this.#redirect;
203
- }
204
-
205
- set redirect(value) {
206
- this.#redirect = value;
207
- }
208
-
209
- get redirectable() {
210
- return this.#redirectable;
211
- }
212
-
213
- set redirectable(value) {
214
- this.#redirectable = value;
215
- }
216
-
217
- pushFallback() {
218
- this.push(this.#manifest._fallback);
219
- this.push(null);
220
- }
221
-
222
- get [Symbol.toStringTag]() {
223
- return 'PodletClientHttpOutgoing';
224
- }
225
- }
226
-
227
- module.exports = PodletClientHttpOutgoing;
@@ -1,67 +0,0 @@
1
- 'use strict';
2
-
3
- var clonedeep = require('lodash.clonedeep');
4
- var abslog = require('abslog');
5
- var assert = require('assert');
6
-
7
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
-
9
- var clonedeep__default = /*#__PURE__*/_interopDefaultLegacy(clonedeep);
10
- var abslog__default = /*#__PURE__*/_interopDefaultLegacy(abslog);
11
- var assert__default = /*#__PURE__*/_interopDefaultLegacy(assert);
12
-
13
- /* eslint-disable no-plusplus */
14
-
15
- class PodletClientCacheResolver {
16
- #registry;
17
- #log;
18
- constructor(registry, options = {}) {
19
- assert__default['default'](
20
- registry,
21
- 'you must pass a "registry" object to the PodletClientCacheResolver constructor',
22
- );
23
- this.#registry = registry;
24
- this.#log = abslog__default['default'](options.logger);
25
- }
26
-
27
- load(outgoing) {
28
- return new Promise(resolve => {
29
- if (outgoing.status !== 'stale') {
30
- const cached = this.#registry.get(outgoing.name);
31
- if (cached) {
32
- outgoing.manifest = clonedeep__default['default'](cached);
33
- outgoing.status = 'cached';
34
- this.#log.debug(
35
- `loaded manifest from cache - resource: ${outgoing.name}`,
36
- );
37
- }
38
- }
39
- resolve(outgoing);
40
- });
41
- }
42
-
43
- save(outgoing) {
44
- return new Promise(resolve => {
45
- if (outgoing.status === 'fresh') {
46
- this.#registry.set(
47
- outgoing.name,
48
- outgoing.manifest,
49
- outgoing.maxAge,
50
- );
51
- this.#log.debug(
52
- `saved manifest to cache - resource: ${outgoing.name}`,
53
- );
54
- }
55
-
56
- outgoing.recursions++;
57
-
58
- resolve(outgoing);
59
- });
60
- }
61
-
62
- get [Symbol.toStringTag]() {
63
- return 'PodletClientCacheResolver';
64
- }
65
- }
66
-
67
- module.exports = PodletClientCacheResolver;