Skip to content

Commit 674915a

Browse files
authored
fix: change set-cookie type define to string | string[] (#471)
closes #467
1 parent c297a06 commit 674915a

10 files changed

Lines changed: 58 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ src/**/*.d.ts
1414
test/fixtures/ts/*.js
1515
test/fixtures/ts-esm/*.js
1616
.eslintcache
17-
.tshy/
17+
.tshy*

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"mime-types": "^2.1.35",
7777
"pump": "^3.0.0",
7878
"qs": "^6.11.2",
79+
"type-fest": "^4.3.1",
7980
"undici": "^5.22.1",
8081
"ylru": "^1.3.2"
8182
},

src/HttpClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import diagnosticsChannel from 'node:diagnostics_channel';
22
import { EventEmitter } from 'node:events';
33
import { LookupFunction } from 'node:net';
44
import { STATUS_CODES } from 'node:http';
5-
import type { IncomingHttpHeaders } from 'node:http';
65
import { debuglog } from 'node:util';
76
import {
87
createGunzip,
@@ -31,6 +30,7 @@ import pump from 'pump';
3130
// Compatible with old style formstream
3231
import FormStream from 'formstream';
3332
import { HttpAgent, CheckAddressFunction } from './HttpAgent.js';
33+
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
3434
import { RequestURL, RequestOptions, HttpMethod, RequestMeta } from './Request.js';
3535
import { RawResponseWithMeta, HttpClientResponse, SocketInfo } from './Response.js';
3636
import { parseJSON, sleep, digestAuthHeader, globalId, performanceTime, isReadable } from './utils.js';

src/IncomingHttpHeaders.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Except } from 'type-fest';
2+
import type { IncomingHttpHeaders as HTTPIncomingHttpHeaders } from 'node:http';
3+
4+
// fix set-cookie type define https://github.com/nodejs/undici/pull/1893
5+
export interface IncomingHttpHeaders extends Except<HTTPIncomingHttpHeaders, 'set-cookie'> {
6+
'set-cookie'?: string | string[];
7+
}

src/Request.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { Readable, Writable } from 'node:stream';
2-
import type { IncomingHttpHeaders } from 'node:http';
1+
import type { Readable, Writable } from 'node:stream';
32
import type { Dispatcher } from 'undici';
4-
import type {
5-
HttpClientResponse,
6-
} from './Response.js';
3+
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
4+
import type { HttpClientResponse } from './Response.js';
75

86
export type HttpMethod = Dispatcher.HttpMethod;
97

src/Response.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Readable } from 'node:stream';
2-
import { IncomingHttpHeaders } from 'node:http';
1+
import type { Readable } from 'node:stream';
2+
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
33

44
export type SocketInfo = {
55
id: number;

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ export {
4646
FixJSONCtlCharsHandler, FixJSONCtlChars,
4747
} from './Request.js';
4848

49-
export { SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse } from './Response.js';
49+
export {
50+
SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse,
51+
} from './Response.js';
52+
export {
53+
IncomingHttpHeaders,
54+
} from './IncomingHttpHeaders.js';
55+
5056

5157
export default {
5258
request,

test/fixtures/server.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ export async function startServer(options?: {
133133
return res.end('<h1>hello</h1>');
134134
}
135135

136+
if (pathname === '/set-one-cookie') {
137+
res.setHeader('set-cookie', 'foo=bar; path=/');
138+
res.setHeader('content-type', 'text/html');
139+
return res.end('<h1>hello set-cookie</h1>');
140+
}
141+
142+
if (pathname === '/set-two-cookie') {
143+
res.setHeader('Set-Cookie', [ 'foo=bar; path=/', 'hello=world; path=/' ]);
144+
res.setHeader('content-type', 'text/html');
145+
return res.end('<h1>hello set-cookie</h1>');
146+
}
147+
136148
if (pathname === '/redirect') {
137149
res.setHeader('Location', '/redirect-to-url');
138150
res.statusCode = 302;

test/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ describe('index.test.ts', () => {
3030
assert(!response.redirected);
3131
});
3232

33+
it('should response set-cookie as a string', async () => {
34+
const response = await urllib.request(`${_url}set-one-cookie`);
35+
assert.equal(response.status, 200);
36+
assert.equal(typeof response.headers['set-cookie'], 'string');
37+
assert.equal(response.headers['set-cookie'], 'foo=bar; path=/');
38+
assert.equal(response.headers['Set-Cookie'], undefined);
39+
assert.equal(response.headers['content-type'], 'text/html');
40+
assert.equal(response.headers['content-length'], '25');
41+
});
42+
43+
it('should response set-cookie as an array string', async () => {
44+
const response = await urllib.request(`${_url}set-two-cookie`);
45+
assert.equal(response.status, 200);
46+
assert(Array.isArray(response.headers['set-cookie']));
47+
assert.equal(typeof response.headers['set-cookie'], 'object');
48+
assert.deepEqual(response.headers['set-cookie'], [
49+
'foo=bar; path=/',
50+
'hello=world; path=/',
51+
]);
52+
assert.equal(response.headers['Set-Cookie'], undefined);
53+
});
54+
3355
it('should request with T response data', async () => {
3456
type HelloData = {
3557
hello: string;

test/mts/src/index.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { request } from "urllib";
1+
import { request, IncomingHttpHeaders } from "urllib";
22
const responseObj = await request("test");
33

44
type IsAny<T, Y, N> = 0 extends (1 & T) ? Y : N;
55
(x: IsAny<number, true, never>) => x; // never
66
(x: IsAny<any, true, never>) => x; // true
77

88
(x: IsAny<typeof responseObj, true, never>) => x; // true
9+
console.log(responseObj.headers as IncomingHttpHeaders);

0 commit comments

Comments
 (0)