[ci] format

This commit is contained in:
matthewp 2022-11-16 21:58:37 +00:00 committed by fredkbot
parent e2fb0c4ff2
commit c18c618a09
3 changed files with 223 additions and 237 deletions

View file

@ -74,7 +74,7 @@ async function loadRemoteImage(src: string) {
expires: Date.now() + expires, expires: Date.now() + expires,
}; };
} catch (err: unknown) { } catch (err: unknown) {
console.error(err) console.error(err);
return undefined; return undefined;
} }
} }

View file

@ -15,7 +15,7 @@ async function loadRemoteImage(src: URL) {
return Buffer.from(await res.arrayBuffer()); return Buffer.from(await res.arrayBuffer());
} catch (err: unknown) { } catch (err: unknown) {
console.error(err) console.error(err);
return undefined; return undefined;
} }
} }

View file

@ -4,238 +4,224 @@
* - https://github.com/apollographql/apollo-client/blob/main/src/utilities/common/responseIterator.ts * - https://github.com/apollographql/apollo-client/blob/main/src/utilities/common/responseIterator.ts
*/ */
import type { Response as NodeResponse } from "node-fetch"; import type { Response as NodeResponse } from 'node-fetch';
import { Readable as NodeReadableStream } from "stream"; import { Readable as NodeReadableStream } from 'stream';
interface NodeStreamIterator<T> { interface NodeStreamIterator<T> {
next(): Promise<IteratorResult<T, boolean | undefined>>; next(): Promise<IteratorResult<T, boolean | undefined>>;
[Symbol.asyncIterator]?(): AsyncIterator<T>; [Symbol.asyncIterator]?(): AsyncIterator<T>;
} }
interface PromiseIterator<T> { interface PromiseIterator<T> {
next(): Promise<IteratorResult<T, ArrayBuffer | undefined>>; next(): Promise<IteratorResult<T, ArrayBuffer | undefined>>;
[Symbol.asyncIterator]?(): AsyncIterator<T>; [Symbol.asyncIterator]?(): AsyncIterator<T>;
} }
interface ReaderIterator<T> { interface ReaderIterator<T> {
next(): Promise<ReadableStreamDefaultReadResult<T>>; next(): Promise<ReadableStreamDefaultReadResult<T>>;
[Symbol.asyncIterator]?(): AsyncIterator<T>; [Symbol.asyncIterator]?(): AsyncIterator<T>;
} }
const canUseSymbol = const canUseSymbol = typeof Symbol === 'function' && typeof Symbol.for === 'function';
typeof Symbol === 'function' &&
typeof Symbol.for === 'function'; const canUseAsyncIteratorSymbol = canUseSymbol && Symbol.asyncIterator;
const canUseAsyncIteratorSymbol = canUseSymbol && Symbol.asyncIterator; function isBuffer(value: any): value is Buffer {
return (
function isBuffer(value: any): value is Buffer { value != null &&
return value != null && value.constructor != null && value.constructor != null &&
typeof value.constructor.isBuffer === 'function' && value.constructor.isBuffer(value) typeof value.constructor.isBuffer === 'function' &&
} value.constructor.isBuffer(value)
);
function isNodeResponse(value: any): value is NodeResponse { }
return !!(value as NodeResponse).body;
} function isNodeResponse(value: any): value is NodeResponse {
return !!(value as NodeResponse).body;
function isReadableStream(value: any): value is ReadableStream<any> { }
return !!(value as ReadableStream<any>).getReader;
} function isReadableStream(value: any): value is ReadableStream<any> {
return !!(value as ReadableStream<any>).getReader;
function isAsyncIterableIterator( }
value: any
): value is AsyncIterableIterator<any> { function isAsyncIterableIterator(value: any): value is AsyncIterableIterator<any> {
return !!( return !!(
canUseAsyncIteratorSymbol && canUseAsyncIteratorSymbol && (value as AsyncIterableIterator<any>)[Symbol.asyncIterator]
(value as AsyncIterableIterator<any>)[Symbol.asyncIterator] );
); }
}
function isStreamableBlob(value: any): value is Blob {
function isStreamableBlob(value: any): value is Blob { return !!(value as Blob).stream;
return !!(value as Blob).stream; }
}
function isBlob(value: any): value is Blob {
function isBlob(value: any): value is Blob { return !!(value as Blob).arrayBuffer;
return !!(value as Blob).arrayBuffer; }
}
function isNodeReadableStream(value: any): value is NodeReadableStream {
function isNodeReadableStream(value: any): value is NodeReadableStream { return !!(value as NodeReadableStream).pipe;
return !!(value as NodeReadableStream).pipe; }
}
function readerIterator<T>(reader: ReadableStreamDefaultReader<T>): AsyncIterableIterator<T> {
function readerIterator<T>( const iterator: ReaderIterator<T> = {
reader: ReadableStreamDefaultReader<T> next() {
): AsyncIterableIterator<T> { return reader.read();
const iterator: ReaderIterator<T> = { },
next() { };
return reader.read();
}, if (canUseAsyncIteratorSymbol) {
}; iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> {
//@ts-ignore
if (canUseAsyncIteratorSymbol) { return this;
iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> { };
//@ts-ignore }
return this;
}; return iterator as AsyncIterableIterator<T>;
} }
return iterator as AsyncIterableIterator<T>; function promiseIterator<T = ArrayBuffer>(promise: Promise<ArrayBuffer>): AsyncIterableIterator<T> {
} let resolved = false;
function promiseIterator<T = ArrayBuffer>( const iterator: PromiseIterator<T> = {
promise: Promise<ArrayBuffer> next(): Promise<IteratorResult<T, ArrayBuffer | undefined>> {
): AsyncIterableIterator<T> { if (resolved)
let resolved = false; return Promise.resolve({
value: undefined,
const iterator: PromiseIterator<T> = { done: true,
next(): Promise<IteratorResult<T, ArrayBuffer | undefined>> { });
if (resolved) resolved = true;
return Promise.resolve({ return new Promise(function (resolve, reject) {
value: undefined, promise
done: true, .then(function (value) {
}); resolve({ value: value as unknown as T, done: false });
resolved = true; })
return new Promise(function (resolve, reject) { .catch(reject);
promise });
.then(function (value) { },
resolve({ value: value as unknown as T, done: false }); };
})
.catch(reject); if (canUseAsyncIteratorSymbol) {
}); iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> {
}, return this;
}; };
}
if (canUseAsyncIteratorSymbol) {
iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> { return iterator as AsyncIterableIterator<T>;
return this; }
};
} function nodeStreamIterator<T>(stream: NodeReadableStream): AsyncIterableIterator<T> {
let cleanup: (() => void) | null = null;
return iterator as AsyncIterableIterator<T>; let error: Error | null = null;
} let done = false;
const data: unknown[] = [];
function nodeStreamIterator<T>(
stream: NodeReadableStream const waiting: [
): AsyncIterableIterator<T> { (
let cleanup: (() => void) | null = null; value:
let error: Error | null = null; | IteratorResult<T, boolean | undefined>
let done = false; | PromiseLike<IteratorResult<T, boolean | undefined>>
const data: unknown[] = []; ) => void,
(reason?: any) => void
const waiting: [ ][] = [];
(
value: function onData(chunk: any) {
| IteratorResult<T, boolean | undefined> if (error) return;
| PromiseLike<IteratorResult<T, boolean | undefined>> if (waiting.length) {
) => void, const shiftedArr = waiting.shift();
(reason?: any) => void if (Array.isArray(shiftedArr) && shiftedArr[0]) {
][] = []; return shiftedArr[0]({ value: chunk, done: false });
}
function onData(chunk: any) { }
if (error) return; data.push(chunk);
if (waiting.length) { }
const shiftedArr = waiting.shift(); function onError(err: Error) {
if (Array.isArray(shiftedArr) && shiftedArr[0]) { error = err;
return shiftedArr[0]({ value: chunk, done: false }); const all = waiting.slice();
} all.forEach(function (pair) {
} pair[1](err);
data.push(chunk); });
} !cleanup || cleanup();
function onError(err: Error) { }
error = err; function onEnd() {
const all = waiting.slice(); done = true;
all.forEach(function (pair) { const all = waiting.slice();
pair[1](err); all.forEach(function (pair) {
}); pair[0]({ value: undefined, done: true });
!cleanup || cleanup(); });
} !cleanup || cleanup();
function onEnd() { }
done = true;
const all = waiting.slice(); cleanup = function () {
all.forEach(function (pair) { cleanup = null;
pair[0]({ value: undefined, done: true }); stream.removeListener('data', onData);
}); stream.removeListener('error', onError);
!cleanup || cleanup(); stream.removeListener('end', onEnd);
} stream.removeListener('finish', onEnd);
stream.removeListener('close', onEnd);
cleanup = function () { };
cleanup = null; stream.on('data', onData);
stream.removeListener("data", onData); stream.on('error', onError);
stream.removeListener("error", onError); stream.on('end', onEnd);
stream.removeListener("end", onEnd); stream.on('finish', onEnd);
stream.removeListener("finish", onEnd); stream.on('close', onEnd);
stream.removeListener("close", onEnd);
}; function getNext(): Promise<IteratorResult<T, boolean | undefined>> {
stream.on("data", onData); return new Promise(function (resolve, reject) {
stream.on("error", onError); if (error) return reject(error);
stream.on("end", onEnd); if (data.length) return resolve({ value: data.shift() as T, done: false });
stream.on("finish", onEnd); if (done) return resolve({ value: undefined, done: true });
stream.on("close", onEnd); waiting.push([resolve, reject]);
});
function getNext(): Promise<IteratorResult<T, boolean | undefined>> { }
return new Promise(function (resolve, reject) {
if (error) return reject(error); const iterator: NodeStreamIterator<T> = {
if (data.length) return resolve({ value: data.shift() as T, done: false }); next(): Promise<IteratorResult<T, boolean | undefined>> {
if (done) return resolve({ value: undefined, done: true }); return getNext();
waiting.push([resolve, reject]); },
}); };
}
if (canUseAsyncIteratorSymbol) {
const iterator: NodeStreamIterator<T> = { iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> {
next(): Promise<IteratorResult<T, boolean | undefined>> { return this;
return getNext(); };
}, }
};
return iterator as AsyncIterableIterator<T>;
if (canUseAsyncIteratorSymbol) { }
iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> {
return this; function asyncIterator<T>(source: AsyncIterableIterator<T>): AsyncIterableIterator<T> {
}; const iterator = source[Symbol.asyncIterator]();
} return {
next(): Promise<IteratorResult<T, boolean>> {
return iterator as AsyncIterableIterator<T>; return iterator.next();
} },
[Symbol.asyncIterator](): AsyncIterableIterator<T> {
function asyncIterator<T>( return this;
source: AsyncIterableIterator<T> },
): AsyncIterableIterator<T> { };
const iterator = source[Symbol.asyncIterator](); }
return {
next(): Promise<IteratorResult<T, boolean>> { export function responseIterator<T>(
return iterator.next(); response: Response | NodeResponse | Buffer
}, ): AsyncIterableIterator<T> {
[Symbol.asyncIterator](): AsyncIterableIterator<T> { let body: unknown = response;
return this;
}, if (isNodeResponse(response)) body = response.body;
};
} if (isBuffer(body)) body = NodeReadableStream.from(body);
export function responseIterator<T>( if (isAsyncIterableIterator(body)) return asyncIterator<T>(body);
response: Response | NodeResponse | Buffer
): AsyncIterableIterator<T> { if (isReadableStream(body)) return readerIterator<T>(body.getReader());
let body: unknown = response;
// this errors without casting to ReadableStream<T>
if (isNodeResponse(response)) body = response.body; // because Blob.stream() returns a NodeJS ReadableStream
if (isStreamableBlob(body)) {
if (isBuffer(body)) body = NodeReadableStream.from(body); return readerIterator<T>((body.stream() as unknown as ReadableStream<T>).getReader());
}
if (isAsyncIterableIterator(body)) return asyncIterator<T>(body);
if (isBlob(body)) return promiseIterator<T>(body.arrayBuffer());
if (isReadableStream(body)) return readerIterator<T>(body.getReader());
if (isNodeReadableStream(body)) return nodeStreamIterator<T>(body);
// this errors without casting to ReadableStream<T>
// because Blob.stream() returns a NodeJS ReadableStream throw new Error('Unknown body type for responseIterator. Please pass a streamable response.');
if (isStreamableBlob(body)) { }
return readerIterator<T>(
(body.stream() as unknown as ReadableStream<T>).getReader()
);
}
if (isBlob(body)) return promiseIterator<T>(body.arrayBuffer());
if (isNodeReadableStream(body)) return nodeStreamIterator<T>(body);
throw new Error(
"Unknown body type for responseIterator. Please pass a streamable response."
);
}