Skip to content

Commit 3583219

Browse files
authored
fix: support request uds and tcp at the same time (#451)
cnpm/rapid#10
1 parent 318816f commit 3583219

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"formstream": "^1.1.1",
6868
"mime-types": "^2.1.35",
6969
"pump": "^3.0.0",
70-
"undici": "^5.22.1"
70+
"undici": "^5.22.1",
71+
"ylru": "^1.3.2"
7172
},
7273
"devDependencies": {
7374
"@types/busboy": "^1.5.0",
@@ -87,7 +88,7 @@
8788
"selfsigned": "^2.0.1",
8889
"tar-stream": "^2.2.0",
8990
"typescript": "^5.0.4",
90-
"vitest": "^0.30.1"
91+
"vitest": "^0.31.1"
9192
},
9293
"engines": {
9394
"node": ">= 14.19.3"

src/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
import LRU from 'ylru';
12
import { HttpClient, HEADER_USER_AGENT } from './HttpClient';
23
import { RequestOptions, RequestURL } from './Request';
34

45
let httpclient: HttpClient;
6+
const domainSocketHttpclients = new LRU(50);
57
export async function request<T = any>(url: RequestURL, options?: RequestOptions) {
6-
if (!httpclient) {
7-
if (options?.socketPath) {
8-
httpclient = new HttpClient({
8+
if (options?.socketPath) {
9+
let domainSocketHttpclient = domainSocketHttpclients.get<HttpClient>(options.socketPath);
10+
if (!domainSocketHttpclient) {
11+
domainSocketHttpclient = new HttpClient({
912
connect: { socketPath: options.socketPath },
1013
});
11-
} else {
12-
httpclient = new HttpClient({});
14+
domainSocketHttpclients.set(options.socketPath, domainSocketHttpclient);
1315
}
16+
return await domainSocketHttpclient.request<T>(url, options);
17+
}
18+
19+
if (!httpclient) {
20+
httpclient = new HttpClient({});
1421
}
1522
return await httpclient.request<T>(url, options);
1623
}

test/fixtures/socket_server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { createServer, Server } from 'node:http';
22

3-
const socketPath = '/tmp/urllib.unix.sock';
3+
const socketPathPrefix = '/tmp/urllib.unix.sock';
4+
let index = 0;
45
export async function startServer(): Promise<{
56
server: Server,
67
url: string,
78
socketPath: string,
89
closeServer: any,
910
}> {
11+
const socketPath = `${socketPathPrefix}_${index++}`;
1012
const unixSocketServer = createServer();
1113

1214
unixSocketServer.on('request', (req, res) => {

test/options.socketPath.test.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,66 @@ describe.skipIf(os.platform() === 'win32')('options.socketPath.test.ts', () => {
88
let close: any;
99
let _url: string;
1010
let _socketPath: string;
11+
let server2: any;
1112
beforeAll(async () => {
1213
const { url, closeServer, socketPath } = await startServer();
1314
close = closeServer;
1415
_url = url;
1516
_socketPath = socketPath;
16-
17+
server2 = await startServer();
1718
});
1819

1920
afterAll(async () => {
20-
await close?.();
21+
await close();
22+
await server2.closeServer();
2123
});
2224

2325
it('should request socket successfully', async () => {
24-
const result = await urllib.request(_url, {
26+
let result = await urllib.request(_url, {
2527
socketPath: _socketPath,
2628
contentType: 'json',
2729
dataType: 'json',
2830
});
2931

3032
assert.deepStrictEqual(result.data, { a: 1 });
33+
result = await urllib.request(_url, {
34+
socketPath: _socketPath,
35+
contentType: 'json',
36+
dataType: 'json',
37+
});
38+
assert.deepStrictEqual(result.data, { a: 1 });
39+
result = await urllib.request(_url, {
40+
socketPath: _socketPath,
41+
contentType: 'json',
42+
dataType: 'json',
43+
});
44+
assert.deepStrictEqual(result.data, { a: 1 });
45+
assert(result.res.socket.handledResponses > 1);
46+
47+
result = await urllib.request('http://unix/api/v1', {
48+
socketPath: server2.socketPath,
49+
contentType: 'json',
50+
dataType: 'json',
51+
});
52+
assert.deepStrictEqual(result.data, { a: 1 });
53+
assert.equal(result.url, 'http://unix/api/v1');
54+
55+
result = await urllib.request(_url, {
56+
socketPath: _socketPath,
57+
contentType: 'json',
58+
dataType: 'json',
59+
});
60+
assert.deepStrictEqual(result.data, { a: 1 });
61+
assert.equal(result.url, _url);
62+
63+
// request normal tcp should work
64+
const host = process.env.CI ? 'registry.npmjs.org' : 'registry.npmmirror.com';
65+
const url = `${host}/urllib/latest`;
66+
const result2 = await urllib.request(url, {
67+
dataType: 'json',
68+
});
69+
assert.equal(result2.status, 200);
70+
assert.equal(result2.data.name, 'urllib');
3171
});
3272
});
3373

0 commit comments

Comments
 (0)