[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,8 +4,8 @@
* - 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>>;
@ -22,15 +22,17 @@
[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' &&
value.constructor.isBuffer(value)
);
} }
function isNodeResponse(value: any): value is NodeResponse { function isNodeResponse(value: any): value is NodeResponse {
@ -41,12 +43,9 @@
return !!(value as ReadableStream<any>).getReader; return !!(value as ReadableStream<any>).getReader;
} }
function isAsyncIterableIterator( function isAsyncIterableIterator(value: any): value is AsyncIterableIterator<any> {
value: any
): value is AsyncIterableIterator<any> {
return !!( return !!(
canUseAsyncIteratorSymbol && canUseAsyncIteratorSymbol && (value as AsyncIterableIterator<any>)[Symbol.asyncIterator]
(value as AsyncIterableIterator<any>)[Symbol.asyncIterator]
); );
} }
@ -62,9 +61,7 @@
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();
@ -81,9 +78,7 @@
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> = {
@ -113,9 +108,7 @@
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) {
@ -195,9 +188,7 @@
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>> {
@ -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."
);
} }