Skip to content

Commit

Permalink
Merge pull request #1377 from opencomponents/remove-got
Browse files Browse the repository at this point in the history
remove got, replace with undici
  • Loading branch information
ricardo-devis-agullo authored Sep 10, 2024
2 parents f8a1a49 + 2604eef commit 89ed832
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 526 deletions.
306 changes: 10 additions & 296 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@types/errorhandler": "^1.5.3",
"@types/express": "^4.17.21",
"@types/fs-extra": "^11.0.4",
"@types/got": "^9.6.12",
"@types/livereload": "^0.9.5",
"@types/morgan": "^1.9.9",
"@types/multer": "^1.4.12",
Expand Down Expand Up @@ -90,10 +89,8 @@
"form-data": "^4.0.0",
"fs-extra": "^11.2.0",
"getport": "^0.1.0",
"got": "^11.8.5",
"livereload": "^0.9.3",
"lodash.isequal": "^4.5.0",
"minimal-request": "^3.0.0",
"morgan": "^1.10.0",
"multer": "^1.4.3",
"nice-cache": "^0.0.5",
Expand All @@ -119,7 +116,8 @@
"serialize-error": "^8.1.0",
"targz": "^1.0.1",
"try-require": "^1.2.1",
"undici": "^6.19.8",
"universalify": "^2.0.0",
"yargs": "^17.7.2"
}
}
}
18 changes: 10 additions & 8 deletions src/cli/domain/registry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import fs from 'fs-extra';
import got from 'got';
import { request } from 'undici';

import * as urlBuilder from '../../registry/domain/url-builder';
import settings from '../../resources/settings';
Expand Down Expand Up @@ -32,9 +32,11 @@ export default function registry(opts: RegistryOptions = {}) {
registry += '/';
}
try {
const apiResponse: { type: string } = await got(registry, {
headers: requestsHeaders
}).json();
const apiResponse: { type: string } = (await (
await request(registry, {
headers: requestsHeaders
})
).body.json()) as any;

if (!apiResponse) throw 'oc registry not available';
if (apiResponse.type !== 'oc-registry') throw 'not a valid oc registry';
Expand Down Expand Up @@ -73,15 +75,15 @@ export default function registry(opts: RegistryOptions = {}) {
}
},
getApiComponentByHref(href: string): Promise<Component> {
return got(href + settings.registry.componentInfoPath, {
return request(href + settings.registry.componentInfoPath, {
headers: requestsHeaders
}).json();
}).then((x) => x.body.json() as any);
},
async getComponentPreviewUrlByUrl(componentHref: string): Promise<string> {
const res: { requestVersion: string; href: string } = await got(
const res: { requestVersion: string; href: string } = await request(
componentHref,
{ headers: requestsHeaders }
).json();
).then((x) => x.body.json() as any);

const parsed = urlParser.parse(res);

Expand Down
25 changes: 0 additions & 25 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,3 @@ declare module 'nice-cache' {

export = Cache;
}

declare module 'minimal-request' {
function request<T>(
opts: {
method?: string;
body?: unknown;
url: string;
headers?: Record<string, string | null | undefined | string[]>;
json?: boolean;
},
cb: (
err: Error | number | null,
body: T,
details: {
response: {
headers: Record<string, string>;
};
}
) => void
): void;

const req: Request;

export = request;
}
98 changes: 58 additions & 40 deletions src/registry/routes/helpers/get-component-fallback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { IncomingHttpHeaders } from 'node:http';
import url from 'node:url';
import type { Request, Response } from 'express';
import request from 'minimal-request';
import { type Dispatcher, request } from 'undici';
import type { Component, Config } from '../../../types';
import * as urlBuilder from '../../domain/url-builder';
import type { GetComponentResult } from './get-component';
Expand Down Expand Up @@ -34,31 +33,21 @@ function getComponentFallbackForViewType(
conf.fallbackRegistryUrl
);

return request(
{
method: 'get',
url: path,
headers: {
...req.headers,
host: url.parse(conf.fallbackRegistryUrl).host,
accept: 'application/json'
}
},
(fallbackErr, fallbackResponse: string) => {
if (fallbackErr === 304) {
return res.status(304).send('');
}

if (fallbackErr) {
return callback(
{
registryError: registryError,
fallbackError: fallbackErr
},
undefined as any
);
return request(path, {
method: 'GET',
headers: {
...req.headers,
host: new URL(conf.fallbackRegistryUrl).host,
accept: 'application/json'
}
})
.then((response) => {
if (response.statusCode !== 200) {
throw response;
}

return response.body.text();
})
.then((fallbackResponse) => {
try {
return callback(null, JSON.parse(fallbackResponse));
} catch (parseError) {
Expand All @@ -70,8 +59,22 @@ function getComponentFallbackForViewType(
undefined as any
);
}
}
);
})
.catch(async (response: Dispatcher.ResponseData) => {
if (response.statusCode === 304) {
return res.status(304).send('');
}

return callback(
{
registryError: registryError,
fallbackError: await response.body
.text()
.catch(() => response.statusCode)
},
undefined as any
);
});
}

export function getComponent(
Expand All @@ -80,28 +83,43 @@ export function getComponent(
component: { name: string; version: string; parameters: IncomingHttpHeaders },
callback: (result: GetComponentResult) => void
): void {
request(
{
method: 'post',
url: fallbackRegistryUrl,
headers: { ...headers, host: url.parse(fallbackRegistryUrl).host },
json: true,
body: { components: [component] }
request(fallbackRegistryUrl, {
method: 'POST',
headers: {
...headers,
host: new URL(fallbackRegistryUrl).host,
'Content-Type': 'application/json'
},
(err, res: GetComponentResult[]) => {
if (err || !res || res.length === 0) {
body: JSON.stringify({ components: [component] })
})
.then((response) => {
if (response.statusCode !== 200) {
throw response;
}
return response.body.json() as any;
})
.then((res: GetComponentResult[]) => {
if (!res || res.length === 0) {
return callback({
status: 404,
response: {
code: 'NOT_FOUND',
error: err as any
error: 'Component not found'
}
});
}

return callback(res[0]);
}
);
})
.catch(async (err: Dispatcher.ResponseData) => {
return callback({
status: 404,
response: {
code: 'NOT_FOUND',
error: await err.body.text().catch(() => err.statusCode)
}
});
});
}

export function getComponentPreview(
Expand Down
7 changes: 3 additions & 4 deletions src/registry/routes/helpers/is-url-discoverable.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import got from 'got';
import { request } from 'undici';

export default async function isUrlDiscoverable(
url: string
): Promise<{ isDiscoverable: boolean }> {
try {
const res = await got(url, {
headers: { accept: 'text/html' }
});
const res = await request(url, { headers: { accept: 'text/html' } });

const isHtml = !!res.headers['content-type']?.includes('text/html');

return {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/put.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import FormData from 'form-data';
import fs from 'fs-extra';
import got from 'got';
import { request } from 'undici';

async function put(
urlPath: string,
Expand All @@ -19,17 +19,17 @@ async function put(
form.append(fileName, fs.createReadStream(file));
}

const res = await got(urlPath, {
const res = await request(urlPath, {
headers: { ...headers, ...form.getHeaders() },
method: 'PUT',
body: form
});

if (res.statusCode !== 200) {
throw res.body;
throw res.body.text();
}

return res.body;
return res.body.text();
}

export default put;
10 changes: 5 additions & 5 deletions test/acceptance/registry-ui.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const expect = require('chai').expect;
const oc = require('../../dist/index');
const path = require('node:path');
const got = require('got');
const { request } = require('undici');

describe('registry (ui interface)', () => {
let registry;
Expand All @@ -11,9 +11,9 @@ describe('registry (ui interface)', () => {

const next = (promise, done) => {
promise
.then((r) => {
.then(async (r) => {
headers = r.headers;
result = r.body;
result = await r.body.text();
})
.catch((e) => {
error = e;
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('registry (ui interface)', () => {
describe('GET / with Accept: text/html', () => {
before((done) => {
next(
got('http://localhost:3030', {
request('http://localhost:3030', {
headers: { accept: 'text/html' }
}),
done
Expand All @@ -64,7 +64,7 @@ describe('registry (ui interface)', () => {
describe('GET /oc-client/~info with Accept: text/html', () => {
before((done) => {
next(
got('http://localhost:3030/oc-client/~info', {
request('http://localhost:3030/oc-client/~info', {
headers: { accept: 'text/html' }
}),
done
Expand Down
Loading

0 comments on commit 89ed832

Please sign in to comment.