feat(rest): use undici (#7747)

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
Co-authored-by: ckohen <chaikohen@gmail.com>
This commit is contained in:
Khafra
2022-05-12 16:49:15 -04:00
committed by GitHub
parent 4515a1ea80
commit d1ec8c37ff
19 changed files with 964 additions and 605 deletions

View File

@@ -0,0 +1,66 @@
import { Blob } from 'node:buffer';
import { resolveBody, parseHeader } from '../src/lib/utils/utils';
test('GIVEN string parseHeader returns string', () => {
const header = 'application/json';
expect(parseHeader(header)).toBe(header);
});
test('GIVEN string[] parseHeader returns string', () => {
const header = ['application/json', 'wait sorry I meant text/html'];
expect(parseHeader(header)).toBe(header.join(';'));
});
test('GIVEN undefined parseHeader return undefined', () => {
expect(parseHeader(undefined)).toBeUndefined();
});
test('resolveBody', async () => {
await expect(resolveBody(null)).resolves.toBe(null);
await expect(resolveBody(undefined)).resolves.toBe(null);
await expect(resolveBody('Hello')).resolves.toBe('Hello');
await expect(resolveBody(new Uint8Array([1, 2, 3]))).resolves.toStrictEqual(new Uint8Array([1, 2, 3]));
// ArrayBuffers gets resolved to Uint8Array
await expect(resolveBody(new ArrayBuffer(8))).resolves.toStrictEqual(new Uint8Array(new ArrayBuffer(8)));
const urlSearchParams = new URLSearchParams([['a', 'b']]);
await expect(resolveBody(urlSearchParams)).resolves.toBe(urlSearchParams.toString());
const dataView = new DataView(new ArrayBuffer(8));
await expect(resolveBody(dataView)).resolves.toStrictEqual(new Uint8Array(new ArrayBuffer(8)));
const blob = new Blob(['hello']);
await expect(resolveBody(blob)).resolves.toStrictEqual(new Uint8Array(await blob.arrayBuffer()));
const iterable: Iterable<Uint8Array> = {
*[Symbol.iterator]() {
for (let i = 0; i < 3; i++) {
yield new Uint8Array([1, 2, 3]);
}
},
};
await expect(resolveBody(iterable)).resolves.toStrictEqual(new Uint8Array([1, 2, 3, 1, 2, 3, 1, 2, 3]));
const asyncIterable: AsyncIterable<Uint8Array> = {
[Symbol.asyncIterator]() {
let i = 0;
return {
next() {
if (i < 3) {
i++;
return Promise.resolve({ value: new Uint8Array([1, 2, 3]), done: false });
}
return Promise.resolve({ value: undefined, done: true });
},
};
},
};
await expect(resolveBody(asyncIterable)).resolves.toStrictEqual(Buffer.from([1, 2, 3, 1, 2, 3, 1, 2, 3]));
// unknown type
// @ts-expect-error This test is ensuring that this throws
await expect(resolveBody(true)).rejects.toThrow(TypeError);
});