Skip to content

Commit

Permalink
📦 NEW: Support options.opaque = object
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 5, 2022
1 parent f07c9f8 commit cc1c854
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 42 deletions.
5 changes: 5 additions & 0 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ export class HttpClient extends EventEmitter {
headers['x-urllib-retry'] = `${requestContext.retries}/${args.retry}`;
}

let opaque = args.opaque;
try {
const requestOptions: UndiciRquestOptions = {
method,
keepalive: true,
maxRedirections: args.maxRedirects ?? 10,
headersTimeout,
bodyTimeout,
opaque,
};
if (args.followRedirect === false) {
requestOptions.maxRedirections = 0;
Expand Down Expand Up @@ -310,6 +312,7 @@ export class HttpClient extends EventEmitter {

requestOptions.headers = headers;
const response = await undiciRequest(requestUrl, requestOptions);
opaque = response.opaque;
if (args.timing) {
res.timing.waiting = performanceTime(requestStartTime);
}
Expand Down Expand Up @@ -389,6 +392,7 @@ export class HttpClient extends EventEmitter {
}

const clientResponse: HttpClientResponse = {
opaque,
data,
status: res.status,
headers: res.headers,
Expand Down Expand Up @@ -418,6 +422,7 @@ export class HttpClient extends EventEmitter {
} else if (err.name === 'BodyTimeoutError') {
err = new HttpClientRequestTimeoutError(bodyTimeout, { cause: e });
}
err.opaque = opaque;
err.status = res.status;
err.headers = res.headers;
err.res = res;
Expand Down
2 changes: 2 additions & 0 deletions src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,6 @@ export type RequestOptions = {
* It will retry when status >= 500 by default. Request error is not included.
*/
isRetry?: (response: HttpClientResponse) => boolean;
/** Default: `null` */
opaque?: unknown;
};
1 change: 1 addition & 0 deletions src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type ReadableWithMeta = Readable & {
};

export type HttpClientResponse = {
opaque: unknown;
data: any
status: number;
headers: IncomingHttpHeaders;
Expand Down
42 changes: 0 additions & 42 deletions test-old/timing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,48 +69,6 @@ describe('timing.test.js', function() {
});
});

it('should dns cache on https', function(done) {
urllib.request(config.npmRegistry + '/urllib', {
timing: true,
timeout: 10000,
// disable keepalive
httpsAgent: false,
}, function(err, data, res) {
assert(!err);
assert(data);
assert(res.timing);
console.log(res.timing);
// if (/^v0\.10\.\d+$/.test(process.version)) {
// // socket lookup event wont fire on 0.10
// assert(res.timing.dnslookup === 0);
// } else {
// assert(res.timing.dnslookup < firstDnsLookup);
// }
done();
});
});

it('should dns cache on http', function(done) {
urllib.request(config.npmRegistry + '/urllib', {
timing: true,
timeout: 10000,
// disable keepalive
agent: false,
}, function(err, data, res) {
assert(!err);
assert(data);
assert(res.timing);
console.log(res.timing);
// if (/^v0\.10\.\d+$/.test(process.version)) {
// // socket lookup event wont fire on 0.10
// assert(res.timing.dnslookup === 0);
// } else {
// assert(res.timing.dnslookup < firstDnsLookup);
// }
done();
});
});

// FIXME: why https request not support options.lookup
it.skip('should custom dns lookup work on https', function(done) {
done = pedding(2, done);
Expand Down
48 changes: 48 additions & 0 deletions test/options.opaque.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { strict as assert } from 'assert';
import urllib from '../src';
import { startServer } from './fixtures/server';

describe('options.opaque.test.ts', () => {
let close: any;
let _url: string;
beforeAll(async () => {
const { closeServer, url } = await startServer();
close = closeServer;
_url = url;
});

afterAll(async () => {
await close();
});

it('should opaque work', async () => {
const response = await urllib.request(_url, {
dataType: 'json',
opaque: {
traceId: 'some random id here',
},
});
assert.equal(response.status, 200);
assert.deepEqual(response.opaque, {
traceId: 'some random id here',
});
});

it('should opaque work on error request', async () => {
await assert.rejects(async () => {
await urllib.request(`${_url}socket.end.error`, {
opaque: {
traceId: 'some random id here',
},
});
}, (err: any) => {
// console.error(err);
assert.equal(err.res.status, 200);
assert.equal(err.name, 'HTTPParserError');
assert.deepEqual(err.opaque, {
traceId: 'some random id here',
});
return true;
});
});
});

0 comments on commit cc1c854

Please sign in to comment.