Skip to content

Commit

Permalink
fix: change set-cookie type define to string | string[] (#471)
Browse files Browse the repository at this point in the history
closes #467
  • Loading branch information
fengmk2 authored Sep 19, 2023
1 parent c297a06 commit 674915a
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ src/**/*.d.ts
test/fixtures/ts/*.js
test/fixtures/ts-esm/*.js
.eslintcache
.tshy/
.tshy*
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"mime-types": "^2.1.35",
"pump": "^3.0.0",
"qs": "^6.11.2",
"type-fest": "^4.3.1",
"undici": "^5.22.1",
"ylru": "^1.3.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import diagnosticsChannel from 'node:diagnostics_channel';
import { EventEmitter } from 'node:events';
import { LookupFunction } from 'node:net';
import { STATUS_CODES } from 'node:http';
import type { IncomingHttpHeaders } from 'node:http';
import { debuglog } from 'node:util';
import {
createGunzip,
Expand Down Expand Up @@ -31,6 +30,7 @@ import pump from 'pump';
// Compatible with old style formstream
import FormStream from 'formstream';
import { HttpAgent, CheckAddressFunction } from './HttpAgent.js';
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
import { RequestURL, RequestOptions, HttpMethod, RequestMeta } from './Request.js';
import { RawResponseWithMeta, HttpClientResponse, SocketInfo } from './Response.js';
import { parseJSON, sleep, digestAuthHeader, globalId, performanceTime, isReadable } from './utils.js';
Expand Down
7 changes: 7 additions & 0 deletions src/IncomingHttpHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Except } from 'type-fest';
import type { IncomingHttpHeaders as HTTPIncomingHttpHeaders } from 'node:http';

// fix set-cookie type define https://github.com/nodejs/undici/pull/1893
export interface IncomingHttpHeaders extends Except<HTTPIncomingHttpHeaders, 'set-cookie'> {
'set-cookie'?: string | string[];
}
8 changes: 3 additions & 5 deletions src/Request.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Readable, Writable } from 'node:stream';
import type { IncomingHttpHeaders } from 'node:http';
import type { Readable, Writable } from 'node:stream';
import type { Dispatcher } from 'undici';
import type {
HttpClientResponse,
} from './Response.js';
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
import type { HttpClientResponse } from './Response.js';

export type HttpMethod = Dispatcher.HttpMethod;

Expand Down
4 changes: 2 additions & 2 deletions src/Response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Readable } from 'node:stream';
import { IncomingHttpHeaders } from 'node:http';
import type { Readable } from 'node:stream';
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';

export type SocketInfo = {
id: number;
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ export {
FixJSONCtlCharsHandler, FixJSONCtlChars,
} from './Request.js';

export { SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse } from './Response.js';
export {
SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse,
} from './Response.js';
export {
IncomingHttpHeaders,
} from './IncomingHttpHeaders.js';


export default {
request,
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ export async function startServer(options?: {
return res.end('<h1>hello</h1>');
}

if (pathname === '/set-one-cookie') {
res.setHeader('set-cookie', 'foo=bar; path=/');
res.setHeader('content-type', 'text/html');
return res.end('<h1>hello set-cookie</h1>');
}

if (pathname === '/set-two-cookie') {
res.setHeader('Set-Cookie', [ 'foo=bar; path=/', 'hello=world; path=/' ]);
res.setHeader('content-type', 'text/html');
return res.end('<h1>hello set-cookie</h1>');
}

if (pathname === '/redirect') {
res.setHeader('Location', '/redirect-to-url');
res.statusCode = 302;
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ describe('index.test.ts', () => {
assert(!response.redirected);
});

it('should response set-cookie as a string', async () => {
const response = await urllib.request(`${_url}set-one-cookie`);
assert.equal(response.status, 200);
assert.equal(typeof response.headers['set-cookie'], 'string');
assert.equal(response.headers['set-cookie'], 'foo=bar; path=/');
assert.equal(response.headers['Set-Cookie'], undefined);
assert.equal(response.headers['content-type'], 'text/html');
assert.equal(response.headers['content-length'], '25');
});

it('should response set-cookie as an array string', async () => {
const response = await urllib.request(`${_url}set-two-cookie`);
assert.equal(response.status, 200);
assert(Array.isArray(response.headers['set-cookie']));
assert.equal(typeof response.headers['set-cookie'], 'object');
assert.deepEqual(response.headers['set-cookie'], [
'foo=bar; path=/',
'hello=world; path=/',
]);
assert.equal(response.headers['Set-Cookie'], undefined);
});

it('should request with T response data', async () => {
type HelloData = {
hello: string;
Expand Down
3 changes: 2 additions & 1 deletion test/mts/src/index.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { request } from "urllib";
import { request, IncomingHttpHeaders } from "urllib";
const responseObj = await request("test");

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

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

0 comments on commit 674915a

Please sign in to comment.