Skip to content

Commit

Permalink
fix: generate types from JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed May 14, 2024
1 parent 29439f4 commit b3d842f
Show file tree
Hide file tree
Showing 37 changed files with 3,643 additions and 2,545 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ tmp/**/*
.idea
.idea/**/*
coverage
dist/
.tap
types/
.tap
72 changes: 55 additions & 17 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,78 @@
import EventEmitter from 'events';
import {uriStrict as validateUriStrict, name as validateName } from'@podium/schemas';
import {
uriStrict as validateUriStrict,
name as validateName,
} from '@podium/schemas';
import Metrics from '@metrics/client';
import abslog from 'abslog';
import Cache from 'ttl-mem-cache';
import http from 'http';
import https from 'https';

import Resource from './resource.js';
import State from './state.js';

const inspect = Symbol.for('nodejs.util.inspect.custom');

const HTTP_AGENT_OPTIONS = {
keepAlive: true,
maxSockets: 10,
maxFreeSockets: 10,
timeout: 60000,
keepAliveMsecs: 30000,
};

const HTTPS_AGENT_OPTIONS = {
...HTTP_AGENT_OPTIONS,
maxCachedSessions: 10,
};

const REJECT_UNAUTHORIZED = true;

const HTTP_AGENT = new http.Agent(HTTP_AGENT_OPTIONS);

const HTTPS_AGENT = new https.Agent(HTTPS_AGENT_OPTIONS);

const RETRIES = 4;

const TIMEOUT = 1000; // 1 seconds

const MAX_AGE = Infinity;

/**
* @typedef {import('./resource.js').default} PodiumClientResource
* @typedef {import('./resource.js').PodiumClientResourceOptions} PodiumClientResourceOptions
* @typedef {import('@podium/schemas').PodletManifestSchema} PodletManifest
*/

/**
* @typedef {object} PodiumClientOptions
* @property {string} name
* @property {import('abslog').AbstractLoggerOptions} [logger]
* @property {number} [retries=4]
* @property {number} [timeout=1000] In milliseconds
* @property {number} [maxAge=Infinity]
* @property {boolean} [rejectUnauthorized=true]
* @property {number} [resolveThreshold]
* @property {number} [resolveMax]
* @property {import('http').Agent} [httpAgent]
* @property {import('https').Agent} [httpsAgent]
*/

/**
* @typedef {object} RegisterOptions
* @property {string} name
* @property {string} uri To the podlet's `manifest.json`
* @property {number} [retries=4]
* @property {number} [timeout=1000] In milliseconds
* @property {boolean} [throwable=false]
* @property {boolean} [redirectable=false]
*/

export default class PodiumClient extends EventEmitter {
#resources;
#registry;
#metrics;
#histogram;
#options;
#state;

/**
* @constructor
* @param {PodiumClientOptions} options
*/
// @ts-expect-error Deliberate default empty options for better error messages
constructor(options = {}) {
super();
const log = abslog(options.logger);
Expand All @@ -69,7 +99,7 @@ export default class PodiumClient extends EventEmitter {
resolveThreshold: options.resolveThreshold,
resolveMax: options.resolveMax,
});
this.#state.on('state', state => {
this.#state.on('state', (state) => {
this.emit('state', state);
});

Expand All @@ -79,7 +109,7 @@ export default class PodiumClient extends EventEmitter {
changefeed: true,
ttl: options.maxAge,
});
this.#registry.on('error', error => {
this.#registry.on('error', (error) => {
log.error(
'Error emitted by the registry in @podium/client module',
error,
Expand All @@ -91,15 +121,15 @@ export default class PodiumClient extends EventEmitter {
});

this.#metrics = new Metrics();
this.#metrics.on('error', error => {
this.#metrics.on('error', (error) => {
log.error(
'Error emitted by metric stream in @podium/client module',
error,
);
});

this[Symbol.iterator] = () => ({
items: Array.from(this.#resources).map(item => item[1]),
items: Array.from(this.#resources).map((item) => item[1]),
next: function next() {
return {
done: this.items.length === 0,
Expand Down Expand Up @@ -128,7 +158,13 @@ export default class PodiumClient extends EventEmitter {
return this.#state.status;
}

register(options = {}) {
/**
* Register a podlet so you can fetch its contents later.
*
* @param {RegisterOptions} options
* @returns {PodiumClientResource}
*/
register(options) {
if (validateName(options.name).error)
throw new Error(
`The value, "${options.name}", for the required argument "name" on the .register() method is not defined or not valid.`,
Expand Down Expand Up @@ -188,7 +224,9 @@ export default class PodiumClient extends EventEmitter {

// Don't return this
await Promise.all(
Array.from(this.#resources).map(resource => resource[1].refresh()),
Array.from(this.#resources).map((resource) =>
resource[1].refresh(),
),
);

end();
Expand All @@ -204,4 +242,4 @@ export default class PodiumClient extends EventEmitter {
get [Symbol.toStringTag]() {
return 'PodiumClient';
}
};
}
Loading

0 comments on commit b3d842f

Please sign in to comment.