Skip to content

Commit

Permalink
updated http-client to 1.0.0-beta.2
Browse files Browse the repository at this point in the history
  • Loading branch information
leftieFriele committed Nov 6, 2024
1 parent 5367455 commit ef639c7
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 41 deletions.
12 changes: 2 additions & 10 deletions lib/resolver.content.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,9 @@ export default class PodletClientContentResolver {
*/
// @ts-expect-error Deliberate default empty options for better error messages
constructor(options = {}) {
this.#http =
options.http ||
new HttpClient({
timeout: options.timeout,
throwOn400: false,
throwOn500: false,
});
const name = options.clientName;
this.#log = abslog(options.logger);
this.#http = options.http || new HttpClient({ logger: this.#log });
this.#metrics = new Metrics();
this.#histogram = this.#metrics.histogram({
name: 'podium_client_resolver_content_resolve',
Expand Down Expand Up @@ -167,9 +161,7 @@ export default class PodletClientContentResolver {
} = await this.#http.request({
origin: url.origin,
path: url.pathname,
method: reqOptions.method,
headers: reqOptions.headers,
throwable: reqOptions.throwable,
...reqOptions,
});

const parsedAssetObjects = parseLinkHeaders(hdrs.link);
Expand Down
13 changes: 9 additions & 4 deletions lib/resolver.fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Metrics from '@metrics/client';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import HTTP from './http.js';
import { parseLinkHeaders, filterAssets } from './utils.js';
import { AssetJs, AssetCss } from '@podium/utils';
import HttpClient from '@podium/http-client';

const currentDirectory = dirname(fileURLToPath(import.meta.url));

Expand All @@ -20,7 +20,7 @@ const UA_STRING = `${pkg.name} ${pkg.version}`;
/**
* @typedef {object} PodletClientFallbackResolverOptions
* @property {string} clientName
* @property {import('./http.js').default} [http]
* @property {import('@podium/http-client').default} [http]
* @property {import('abslog').AbstractLoggerOptions} [logger]
*/

Expand All @@ -36,9 +36,9 @@ export default class PodletClientFallbackResolver {
*/
// @ts-expect-error Deliberate default empty options for better error messages
constructor(options = {}) {
this.#http = options.http || new HTTP();
const name = options.clientName;
this.#log = abslog(options.logger);
this.#http = options.http || new HttpClient({ logger: this.#log });
this.#metrics = new Metrics();
this.#histogram = this.#metrics.histogram({
name: 'podium_client_resolver_fallback_resolve',
Expand Down Expand Up @@ -114,11 +114,16 @@ export default class PodletClientFallbackResolver {
this.#log.debug(
`start reading fallback content from remote resource - resource: ${outgoing.name} - url: ${outgoing.fallbackUri}`,
);
const url = new URL(outgoing.fallbackUri);
const {
statusCode,
body,
headers: resHeaders,
} = await this.#http.request(outgoing.fallbackUri, reqOptions);
} = await this.#http.request({
origin: url.origin,
path: url.pathname,
...reqOptions,
});

const parsedAssetObjects = parseLinkHeaders(resHeaders.link);

Expand Down
19 changes: 8 additions & 11 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Metrics from '@metrics/client';
import abslog from 'abslog';
import assert from 'assert';
import HttpClient from '@podium/http-client';
import HTTP from './http.js';
import Manifest from './resolver.manifest.js';
import Fallback from './resolver.fallback.js';
import Content from './resolver.content.js';
Expand Down Expand Up @@ -35,21 +34,18 @@ export default class PodletClientResolver {
);

const log = abslog(options.logger);
const http = new HTTP();
const httpClient = new HttpClient({
clientName: options.clientName,
logger: log,
});
this.#cache = new Cache(registry, options);
this.#manifest = new Manifest({
clientName: options.clientName,
logger: options.logger,
http,
});
this.#fallback = new Fallback({ ...options, http });
this.#content = new Content({
...options,
http: new HttpClient({
throwOn400: false,
throwOn500: false,
}),
http: httpClient,
});
this.#fallback = new Fallback({ ...options, http: httpClient });
this.#content = new Content({ ...options, http: httpClient });
this.#metrics = new Metrics();

this.#metrics.on('error', (error) => {
Expand All @@ -59,6 +55,7 @@ export default class PodletClientResolver {
);
});

httpClient.metrics.pipe(this.#metrics);
this.#content.metrics.pipe(this.#metrics);
this.#fallback.metrics.pipe(this.#metrics);
this.#manifest.metrics.pipe(this.#metrics);
Expand Down
23 changes: 12 additions & 11 deletions lib/resolver.manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as utils from '@podium/utils';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import HTTP from './http.js';
import HttpClient from '@podium/http-client';

const currentDirectory = dirname(fileURLToPath(import.meta.url));

Expand All @@ -22,7 +22,7 @@ const UA_STRING = `${pkg.name} ${pkg.version}`;
* @typedef {object} PodletClientManifestResolverOptions
* @property {string} clientName
* @property {import('abslog').AbstractLoggerOptions} [logger]
* @property {import('./http.js').default} [http]
* @property {import('@podium/http-client').default} [http]
*/

export default class PodletClientManifestResolver {
Expand All @@ -37,9 +37,9 @@ export default class PodletClientManifestResolver {
*/
// @ts-expect-error Deliberate default empty options for better error messages
constructor(options = {}) {
this.#http = options.http || new HTTP();
const name = options.clientName;
this.#log = abslog(options.logger);
this.#http = options.http || new HttpClient({ logger: this.#log });
this.#metrics = new Metrics();
this.#histogram = this.#metrics.histogram({
name: 'podium_client_resolver_manifest_resolve',
Expand Down Expand Up @@ -97,10 +97,12 @@ export default class PodletClientManifestResolver {
);

try {
const response = await this.#http.request(
outgoing.manifestUri,
reqOptions,
);
const url = new URL(outgoing.manifestUri);
const response = await this.#http.request({
origin: url.origin,
path: url.pathname,
...reqOptions,
});
const { statusCode, headers: hdrs, body } = response;

// Remote responds but with an http error code
Expand All @@ -120,11 +122,10 @@ export default class PodletClientManifestResolver {
await body.text();
return outgoing;
}

// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
const schema = await body.json();
const manifest = validateManifest(
/** @type {import("@podium/schemas").PodletManifestSchema} */ (
await body.json()
),
/** @type {import("@podium/schemas").PodletManifestSchema} */ schema,
);

// Manifest validation error
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"dependencies": {
"@hapi/boom": "10.0.1",
"@metrics/client": "2.5.3",
"@podium/http-client": "1.0.1-beta.1",
"@podium/http-client": "1.0.0-beta.4",
"@podium/schemas": "5.1.0",
"@podium/utils": "5.3.2",
"abslog": "2.4.4",
Expand Down
4 changes: 3 additions & 1 deletion tests/integration.basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ tap.test('integration basic - metrics stream objects created', async (t) => {
const client = new Client({ name: 'clientName' });

const metrics = [];
client.metrics.on('data', (metric) => metrics.push(metric));
client.metrics.on('data', (metric) => {
if (metric.name !== 'http_client_breaker_events') metrics.push(metric);
});
client.metrics.on('end', async () => {
t.equal(metrics.length, 3);
t.equal(metrics[0].name, 'podium_client_resolver_manifest_resolve');
Expand Down
1 change: 0 additions & 1 deletion tests/resolver.content.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
PodletServer,
HttpServer,
} from '@podium/test-utils';
import HttpClient from "@podium/http-client/lib/http-client.js";
import HttpOutgoing from '../lib/http-outgoing.js';
import Content from '../lib/resolver.content.js';

Expand Down
2 changes: 0 additions & 2 deletions tests/resolver.manifest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,7 @@ tap.test(
{},
new HttpIncoming({ headers }),
);

await manifest.resolve(outgoing);

t.equal(
outgoing.manifest.proxy[0].target,
'http://does.not.mather.com/api/bar',
Expand Down
43 changes: 43 additions & 0 deletions tests/resolver.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable no-unused-vars */
import tap from 'tap';
import TtlMemCache from 'ttl-mem-cache';
import { PodletServer } from '@podium/test-utils';
import { HttpIncoming } from '@podium/utils';
import Resolver from '../lib/resolver.js';
import HttpOutgoing from '../lib/http-outgoing.js';

tap.test('resolver() - object tag - should be PodletClientResolver', (t) => {
const resolver = new Resolver(new TtlMemCache());
Expand All @@ -22,3 +25,43 @@ tap.test(
t.end();
},
);
tap.test(
'resolver() - emit metrics from the underlying HttpClient',
async (t) => {
const outgoing = new HttpOutgoing({
name: 'test',
timeout: 1000,
uri: 'http://some.org',
maxAge: Infinity,
});

const resolver = new Resolver(new TtlMemCache());

function verify(stream) {
return new Promise((resolve) => {
const metrics = [];
stream.on('data', (metric) => metrics.push(metric));
stream.on('end', async () => {
t.equal(metrics.length, 2);

t.equal(metrics[0].name, 'http_client_breaker_events');
t.equal(metrics[0].type, 2);
t.same(metrics[0].labels[0], {
name: 'name',
value: 'success',
});
t.equal(
metrics[1].name,
'podium_client_resolver_manifest_resolve',
);
t.equal(metrics[1].type, 5);
resolve();
});
});
}
await resolver.resolve(outgoing);
resolver.metrics.push(null);
await verify(resolver.metrics);
t.end();
},
);

0 comments on commit ef639c7

Please sign in to comment.