[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 { 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 {
return !!(value as NodeResponse).body; return !!(value as NodeResponse).body;
} }
function isReadableStream(value: any): value is ReadableStream<any> { function isReadableStream(value: any): value is ReadableStream<any> {
return !!(value as ReadableStream<any>).getReader; return !!(value as ReadableStream<any>).getReader;
} }
function isAsyncIterableIterator( function isAsyncIterableIterator(value: any): value is AsyncIterableIterator<any> {
value: any return !!(
): value is AsyncIterableIterator<any> { canUseAsyncIteratorSymbol && (value as AsyncIterableIterator<any>)[Symbol.asyncIterator]
return !!( );
canUseAsyncIteratorSymbol && }
(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>( function readerIterator<T>(reader: ReadableStreamDefaultReader<T>): AsyncIterableIterator<T> {
reader: ReadableStreamDefaultReader<T> const iterator: ReaderIterator<T> = {
): AsyncIterableIterator<T> { next() {
const iterator: ReaderIterator<T> = { return reader.read();
next() { },
return reader.read(); };
},
};
if (canUseAsyncIteratorSymbol) { if (canUseAsyncIteratorSymbol) {
iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> { iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> {
//@ts-ignore //@ts-ignore
return this; return this;
}; };
} }
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> let resolved = false;
): AsyncIterableIterator<T> {
let resolved = false;
const iterator: PromiseIterator<T> = { const iterator: PromiseIterator<T> = {
next(): Promise<IteratorResult<T, ArrayBuffer | undefined>> { next(): Promise<IteratorResult<T, ArrayBuffer | undefined>> {
if (resolved) if (resolved)
return Promise.resolve({ return Promise.resolve({
value: undefined, value: undefined,
done: true, done: true,
}); });
resolved = true; resolved = true;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
promise promise
.then(function (value) { .then(function (value) {
resolve({ value: value as unknown as T, done: false }); resolve({ value: value as unknown as T, done: false });
}) })
.catch(reject); .catch(reject);
}); });
}, },
}; };
if (canUseAsyncIteratorSymbol) { if (canUseAsyncIteratorSymbol) {
iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> { iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> {
return this; return this;
}; };
} }
return iterator as AsyncIterableIterator<T>; return iterator as AsyncIterableIterator<T>;
} }
function nodeStreamIterator<T>( function nodeStreamIterator<T>(stream: NodeReadableStream): AsyncIterableIterator<T> {
stream: NodeReadableStream let cleanup: (() => void) | null = null;
): AsyncIterableIterator<T> { let error: Error | null = null;
let cleanup: (() => void) | null = null; let done = false;
let error: Error | null = null; const data: unknown[] = [];
let done = false;
const data: unknown[] = [];
const waiting: [ const waiting: [
( (
value: value:
| IteratorResult<T, boolean | undefined> | IteratorResult<T, boolean | undefined>
| PromiseLike<IteratorResult<T, boolean | undefined>> | PromiseLike<IteratorResult<T, boolean | undefined>>
) => void, ) => void,
(reason?: any) => void (reason?: any) => void
][] = []; ][] = [];
function onData(chunk: any) { function onData(chunk: any) {
if (error) return; if (error) return;
if (waiting.length) { if (waiting.length) {
const shiftedArr = waiting.shift(); const shiftedArr = waiting.shift();
if (Array.isArray(shiftedArr) && shiftedArr[0]) { if (Array.isArray(shiftedArr) && shiftedArr[0]) {
return shiftedArr[0]({ value: chunk, done: false }); return shiftedArr[0]({ value: chunk, done: false });
} }
} }
data.push(chunk); data.push(chunk);
} }
function onError(err: Error) { function onError(err: Error) {
error = err; error = err;
const all = waiting.slice(); const all = waiting.slice();
all.forEach(function (pair) { all.forEach(function (pair) {
pair[1](err); pair[1](err);
}); });
!cleanup || cleanup(); !cleanup || cleanup();
} }
function onEnd() { function onEnd() {
done = true; done = true;
const all = waiting.slice(); const all = waiting.slice();
all.forEach(function (pair) { all.forEach(function (pair) {
pair[0]({ value: undefined, done: true }); pair[0]({ value: undefined, done: true });
}); });
!cleanup || cleanup(); !cleanup || cleanup();
} }
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) {
if (error) return reject(error); if (error) return reject(error);
if (data.length) return resolve({ value: data.shift() as T, done: false }); if (data.length) return resolve({ value: data.shift() as T, done: false });
if (done) return resolve({ value: undefined, done: true }); if (done) return resolve({ value: undefined, done: true });
waiting.push([resolve, reject]); waiting.push([resolve, reject]);
}); });
} }
const iterator: NodeStreamIterator<T> = { const iterator: NodeStreamIterator<T> = {
next(): Promise<IteratorResult<T, boolean | undefined>> { next(): Promise<IteratorResult<T, boolean | undefined>> {
return getNext(); return getNext();
}, },
}; };
if (canUseAsyncIteratorSymbol) { if (canUseAsyncIteratorSymbol) {
iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> { iterator[Symbol.asyncIterator] = function (): AsyncIterator<T> {
return this; return this;
}; };
} }
return iterator as AsyncIterableIterator<T>; return iterator as AsyncIterableIterator<T>;
} }
function asyncIterator<T>( function asyncIterator<T>(source: AsyncIterableIterator<T>): AsyncIterableIterator<T> {
source: AsyncIterableIterator<T> const iterator = source[Symbol.asyncIterator]();
): AsyncIterableIterator<T> { return {
const iterator = source[Symbol.asyncIterator](); next(): Promise<IteratorResult<T, boolean>> {
return { return iterator.next();
next(): Promise<IteratorResult<T, boolean>> { },
return iterator.next(); [Symbol.asyncIterator](): AsyncIterableIterator<T> {
}, return this;
[Symbol.asyncIterator](): AsyncIterableIterator<T> { },
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;
if (isBuffer(body)) body = NodeReadableStream.from(body); if (isBuffer(body)) body = NodeReadableStream.from(body);
if (isAsyncIterableIterator(body)) return asyncIterator<T>(body); if (isAsyncIterableIterator(body)) return asyncIterator<T>(body);
if (isReadableStream(body)) return readerIterator<T>(body.getReader()); if (isReadableStream(body)) return readerIterator<T>(body.getReader());
// 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(
"Unknown body type for responseIterator. Please pass a streamable response."
);
}
throw new Error('Unknown body type for responseIterator. Please pass a streamable response.');
}