Skip to content

Commit

Permalink
fix: support request uds and tcp at the same time (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed May 24, 2023
1 parent 318816f commit 3583219
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"formstream": "^1.1.1",
"mime-types": "^2.1.35",
"pump": "^3.0.0",
"undici": "^5.22.1"
"undici": "^5.22.1",
"ylru": "^1.3.2"
},
"devDependencies": {
"@types/busboy": "^1.5.0",
Expand All @@ -87,7 +88,7 @@
"selfsigned": "^2.0.1",
"tar-stream": "^2.2.0",
"typescript": "^5.0.4",
"vitest": "^0.30.1"
"vitest": "^0.31.1"
},
"engines": {
"node": ">= 14.19.3"
Expand Down
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import LRU from 'ylru';
import { HttpClient, HEADER_USER_AGENT } from './HttpClient';
import { RequestOptions, RequestURL } from './Request';

let httpclient: HttpClient;
const domainSocketHttpclients = new LRU(50);
export async function request<T = any>(url: RequestURL, options?: RequestOptions) {
if (!httpclient) {
if (options?.socketPath) {
httpclient = new HttpClient({
if (options?.socketPath) {
let domainSocketHttpclient = domainSocketHttpclients.get<HttpClient>(options.socketPath);
if (!domainSocketHttpclient) {
domainSocketHttpclient = new HttpClient({
connect: { socketPath: options.socketPath },
});
} else {
httpclient = new HttpClient({});
domainSocketHttpclients.set(options.socketPath, domainSocketHttpclient);
}
return await domainSocketHttpclient.request<T>(url, options);
}

if (!httpclient) {
httpclient = new HttpClient({});
}
return await httpclient.request<T>(url, options);
}
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/socket_server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { createServer, Server } from 'node:http';

const socketPath = '/tmp/urllib.unix.sock';
const socketPathPrefix = '/tmp/urllib.unix.sock';
let index = 0;
export async function startServer(): Promise<{
server: Server,
url: string,
socketPath: string,
closeServer: any,
}> {
const socketPath = `${socketPathPrefix}_${index++}`;
const unixSocketServer = createServer();

unixSocketServer.on('request', (req, res) => {
Expand Down
46 changes: 43 additions & 3 deletions test/options.socketPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,66 @@ describe.skipIf(os.platform() === 'win32')('options.socketPath.test.ts', () => {
let close: any;
let _url: string;
let _socketPath: string;
let server2: any;
beforeAll(async () => {
const { url, closeServer, socketPath } = await startServer();
close = closeServer;
_url = url;
_socketPath = socketPath;

server2 = await startServer();
});

afterAll(async () => {
await close?.();
await close();
await server2.closeServer();
});

it('should request socket successfully', async () => {
const result = await urllib.request(_url, {
let result = await urllib.request(_url, {
socketPath: _socketPath,
contentType: 'json',
dataType: 'json',
});

assert.deepStrictEqual(result.data, { a: 1 });
result = await urllib.request(_url, {
socketPath: _socketPath,
contentType: 'json',
dataType: 'json',
});
assert.deepStrictEqual(result.data, { a: 1 });
result = await urllib.request(_url, {
socketPath: _socketPath,
contentType: 'json',
dataType: 'json',
});
assert.deepStrictEqual(result.data, { a: 1 });
assert(result.res.socket.handledResponses > 1);

result = await urllib.request('http://unix/api/v1', {
socketPath: server2.socketPath,
contentType: 'json',
dataType: 'json',
});
assert.deepStrictEqual(result.data, { a: 1 });
assert.equal(result.url, 'http://unix/api/v1');

result = await urllib.request(_url, {
socketPath: _socketPath,
contentType: 'json',
dataType: 'json',
});
assert.deepStrictEqual(result.data, { a: 1 });
assert.equal(result.url, _url);

// request normal tcp should work
const host = process.env.CI ? 'registry.npmjs.org' : 'registry.npmmirror.com';
const url = `${host}/urllib/latest`;
const result2 = await urllib.request(url, {
dataType: 'json',
});
assert.equal(result2.status, 200);
assert.equal(result2.data.name, 'urllib');
});
});

0 comments on commit 3583219

Please sign in to comment.