Skip to content

Commit

Permalink
Merge pull request #286 from jaredwray/request---upgrading-get-stream…
Browse files Browse the repository at this point in the history
…-to-8.0.1

request - upgrading get-stream to 8.0.1
  • Loading branch information
jaredwray committed Jan 19, 2024
2 parents a736035 + eed322a commit 05b8450
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['16', '18']
node: ['16', '18', '20']
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion packages/request/package.json
Expand Up @@ -37,7 +37,7 @@
},
"dependencies": {
"@types/http-cache-semantics": "^4.0.4",
"get-stream": "^6.0.1",
"get-stream": "^8.0.1",
"http-cache-semantics": "^4.1.1",
"keyv": "^4.5.4",
"mimic-response": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/request/src/index.ts
Expand Up @@ -4,7 +4,7 @@ import crypto from 'node:crypto';
import stream, {PassThrough as PassThroughStream} from 'node:stream';
import {IncomingMessage} from 'node:http';
import normalizeUrl from 'normalize-url';
import getStream from 'get-stream';
import {getStreamAsBuffer} from 'get-stream';
import CachePolicy from 'http-cache-semantics';
import Response from 'responselike';
import Keyv from 'keyv';
Expand Down Expand Up @@ -127,7 +127,7 @@ class CacheableRequest {
clonedResponse = cloneResponse(response);
(async () => {
try {
const bodyPromise = getStream.buffer(response);
const bodyPromise = getStreamAsBuffer(response);
await Promise.race([
requestErrorPromise,
new Promise(resolve => response.once('end', resolve)), // eslint-disable-line no-promise-executor-return
Expand Down
13 changes: 7 additions & 6 deletions packages/request/test/cache.test.ts
Expand Up @@ -657,32 +657,33 @@ test('decompresses cached responses', async () => {
expect(value.body).toBe('{"foo":"bar"}');
});

test('cache remote address', async () => {
test('cache status message', async () => {
const endpoint = '/etag';
const cache = new Map();
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
cacheableRequest.addHook(onResponse, (value: CacheValue, response: any) => {
if (response.connection) {
value.remoteAddress = response.connection.remoteAddress;
if (response) {
value.statusMessage = response.statusMessage;
}

return value;
});
const response: any = await cacheableRequestHelper(s.url + endpoint);
const cacheValue = JSON.parse(await cache.get(`cacheable-request:GET:${s.url + endpoint}`));
expect(cacheValue.value.remoteAddress).toBeDefined();
expect(cacheValue.value.statusMessage).toBe('OK');
expect(response.statusCode).toBe(200);
cacheableRequest.removeHook(onResponse);
});

test('do not cache remote address', async () => {
test('do not cache status message', async () => {
const endpoint = '/etag';
const cache = new Map();
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const response: any = await cacheableRequestHelper(s.url + endpoint);
const cacheValue = JSON.parse(await cache.get(`cacheable-request:GET:${s.url + endpoint}`));
expect(cacheValue.value.remoteAddress).toBeUndefined();
expect(cacheValue.value.statusMessage).toBeUndefined();
expect(response.statusCode).toBe(200);
});
test('socket within keepAlive Agent has been free\'d after cache revalidation', async () => {
Expand Down

0 comments on commit 05b8450

Please sign in to comment.