[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,
};
} catch (err: unknown) {
console.error(err)
console.error(err);
return undefined;
}
}

View file

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

View file

@ -4,67 +4,64 @@
* - https://github.com/apollographql/apollo-client/blob/main/src/utilities/common/responseIterator.ts
*/
import type { Response as NodeResponse } from "node-fetch";
import { Readable as NodeReadableStream } from "stream";
import type { Response as NodeResponse } from 'node-fetch';
import { Readable as NodeReadableStream } from 'stream';
interface NodeStreamIterator<T> {
interface NodeStreamIterator<T> {
next(): Promise<IteratorResult<T, boolean | undefined>>;
[Symbol.asyncIterator]?(): AsyncIterator<T>;
}
}
interface PromiseIterator<T> {
interface PromiseIterator<T> {
next(): Promise<IteratorResult<T, ArrayBuffer | undefined>>;
[Symbol.asyncIterator]?(): AsyncIterator<T>;
}
}
interface ReaderIterator<T> {
interface ReaderIterator<T> {
next(): Promise<ReadableStreamDefaultReadResult<T>>;
[Symbol.asyncIterator]?(): AsyncIterator<T>;
}
}
const canUseSymbol =
typeof Symbol === 'function' &&
typeof Symbol.for === 'function';
const canUseSymbol = 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 value != null && value.constructor != null &&
typeof value.constructor.isBuffer === 'function' && value.constructor.isBuffer(value)
}
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 isAsyncIterableIterator(
value: any
): value is AsyncIterableIterator<any> {
return !!(
canUseAsyncIteratorSymbol &&
(value as AsyncIterableIterator<any>)[Symbol.asyncIterator]
function isBuffer(value: any): value is Buffer {
return (
value != null &&
value.constructor != null &&
typeof value.constructor.isBuffer === 'function' &&
value.constructor.isBuffer(value)
);
}
}
function isStreamableBlob(value: any): value is Blob {
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 isAsyncIterableIterator(value: any): value is AsyncIterableIterator<any> {
return !!(
canUseAsyncIteratorSymbol && (value as AsyncIterableIterator<any>)[Symbol.asyncIterator]
);
}
function isStreamableBlob(value: any): value is Blob {
return !!(value as Blob).stream;
}
}
function isBlob(value: any): value is Blob {
function isBlob(value: any): value is Blob {
return !!(value as Blob).arrayBuffer;
}
}
function isNodeReadableStream(value: any): value is NodeReadableStream {
function isNodeReadableStream(value: any): value is NodeReadableStream {
return !!(value as NodeReadableStream).pipe;
}
}
function readerIterator<T>(
reader: ReadableStreamDefaultReader<T>
): AsyncIterableIterator<T> {
function readerIterator<T>(reader: ReadableStreamDefaultReader<T>): AsyncIterableIterator<T> {
const iterator: ReaderIterator<T> = {
next() {
return reader.read();
@ -79,11 +76,9 @@
}
return iterator as AsyncIterableIterator<T>;
}
}
function promiseIterator<T = ArrayBuffer>(
promise: Promise<ArrayBuffer>
): AsyncIterableIterator<T> {
function promiseIterator<T = ArrayBuffer>(promise: Promise<ArrayBuffer>): AsyncIterableIterator<T> {
let resolved = false;
const iterator: PromiseIterator<T> = {
@ -111,11 +106,9 @@
}
return iterator as AsyncIterableIterator<T>;
}
}
function nodeStreamIterator<T>(
stream: NodeReadableStream
): AsyncIterableIterator<T> {
function nodeStreamIterator<T>(stream: NodeReadableStream): AsyncIterableIterator<T> {
let cleanup: (() => void) | null = null;
let error: Error | null = null;
let done = false;
@ -159,17 +152,17 @@
cleanup = function () {
cleanup = null;
stream.removeListener("data", onData);
stream.removeListener("error", onError);
stream.removeListener("end", onEnd);
stream.removeListener("finish", onEnd);
stream.removeListener("close", onEnd);
stream.removeListener('data', onData);
stream.removeListener('error', onError);
stream.removeListener('end', onEnd);
stream.removeListener('finish', onEnd);
stream.removeListener('close', onEnd);
};
stream.on("data", onData);
stream.on("error", onError);
stream.on("end", onEnd);
stream.on("finish", onEnd);
stream.on("close", onEnd);
stream.on('data', onData);
stream.on('error', onError);
stream.on('end', onEnd);
stream.on('finish', onEnd);
stream.on('close', onEnd);
function getNext(): Promise<IteratorResult<T, boolean | undefined>> {
return new Promise(function (resolve, reject) {
@ -193,11 +186,9 @@
}
return iterator as AsyncIterableIterator<T>;
}
}
function asyncIterator<T>(
source: AsyncIterableIterator<T>
): AsyncIterableIterator<T> {
function asyncIterator<T>(source: AsyncIterableIterator<T>): AsyncIterableIterator<T> {
const iterator = source[Symbol.asyncIterator]();
return {
next(): Promise<IteratorResult<T, boolean>> {
@ -207,11 +198,11 @@
return this;
},
};
}
}
export function responseIterator<T>(
export function responseIterator<T>(
response: Response | NodeResponse | Buffer
): AsyncIterableIterator<T> {
): AsyncIterableIterator<T> {
let body: unknown = response;
if (isNodeResponse(response)) body = response.body;
@ -225,17 +216,12 @@
// this errors without casting to ReadableStream<T>
// because Blob.stream() returns a NodeJS ReadableStream
if (isStreamableBlob(body)) {
return readerIterator<T>(
(body.stream() as unknown as ReadableStream<T>).getReader()
);
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."
);
}
throw new Error('Unknown body type for responseIterator. Please pass a streamable response.');
}