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