Skip to content

Commit

Permalink
Run chomp prettier on codebase (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
novucs authored Jan 6, 2025
1 parent a06f147 commit 56db9b0
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 220 deletions.
42 changes: 21 additions & 21 deletions src/common/fetch-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ export interface WrappedResponse {
export type FetchFn = (
url: URL | string,
...args: any[]
) => Promise<WrappedResponse | globalThis.Response>
) => Promise<WrappedResponse | globalThis.Response>;

export type WrappedFetch = ((
url: URL | string,
...args: any[]
) => Promise<WrappedResponse | globalThis.Response>) & {
arrayBuffer: (url: URL | string, ...args: any[]) => Promise<ArrayBuffer | null>,
text: (url: URL | string, ...args: any[]) => Promise<string | null>
) => Promise<WrappedResponse | globalThis.Response>) & {
arrayBuffer: (
url: URL | string,
...args: any[]
) => Promise<ArrayBuffer | null>;
text: (url: URL | string, ...args: any[]) => Promise<string | null>;
};

let retryCount = 5, poolSize = 100;
let retryCount = 5,
poolSize = 100;

export function setRetryCount(count: number) {
retryCount = count;
Expand Down Expand Up @@ -58,8 +62,7 @@ export function wrappedFetch(fetch: FetchFn): WrappedFetch {
try {
var res = await fetch(url, ...args);
} catch (e) {
if (retries++ >= retryCount)
throw e;
if (retries++ >= retryCount) throw e;
continue;
}
switch (res.status) {
Expand All @@ -75,12 +78,12 @@ export function wrappedFetch(fetch: FetchFn): WrappedFetch {
try {
return await res.arrayBuffer();
} catch (e) {
if (retries++ >= retryCount &&
e.code === "ERR_SOCKET_TIMEOUT" ||
e.code === "ETIMEOUT" ||
e.code === "ECONNRESET" ||
e.code === 'FETCH_ERROR') {

if (
(retries++ >= retryCount && e.code === "ERR_SOCKET_TIMEOUT") ||
e.code === "ETIMEOUT" ||
e.code === "ECONNRESET" ||
e.code === "FETCH_ERROR"
) {
}
}
}
Expand All @@ -90,8 +93,7 @@ export function wrappedFetch(fetch: FetchFn): WrappedFetch {
};
wrappedFetch.text = async function (url, ...args) {
const arrayBuffer = await this.arrayBuffer(url, ...args);
if (!arrayBuffer)
return null;
if (!arrayBuffer) return null;
return new TextDecoder().decode(arrayBuffer);
};
return wrappedFetch;
Expand All @@ -100,12 +102,10 @@ export function wrappedFetch(fetch: FetchFn): WrappedFetch {
// restrict in-flight fetches to a pool of 100
let p = [];
let c = 0;
function pushFetchPool () {
if (++c > poolSize)
return new Promise(r => p.push(r));
function pushFetchPool() {
if (++c > poolSize) return new Promise((r) => p.push(r));
}
function popFetchPool () {
function popFetchPool() {
c--;
if (p.length)
p.shift()();
if (p.length) p.shift()();
}
46 changes: 23 additions & 23 deletions src/common/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-ignore
import { fetch as fetchImpl, clearCache } from '#fetch';
import { fetch as fetchImpl, clearCache } from "#fetch";

export interface WrappedResponse {
url: string;
Expand All @@ -15,17 +15,21 @@ export interface WrappedResponse {
export type FetchFn = (
url: URL | string,
...args: any[]
) => Promise<WrappedResponse | globalThis.Response>
) => Promise<WrappedResponse | globalThis.Response>;

export type WrappedFetch = ((
url: URL | string,
...args: any[]
) => Promise<WrappedResponse | globalThis.Response>) & {
arrayBuffer: (url: URL | string, ...args: any[]) => Promise<ArrayBuffer | null>,
text: (url: URL | string, ...args: any[]) => Promise<string | null>
) => Promise<WrappedResponse | globalThis.Response>) & {
arrayBuffer: (
url: URL | string,
...args: any[]
) => Promise<ArrayBuffer | null>;
text: (url: URL | string, ...args: any[]) => Promise<string | null>;
};

let retryCount = 5, poolSize = 100;
let retryCount = 5,
poolSize = 100;

export function setRetryCount(count: number) {
retryCount = count;
Expand All @@ -44,7 +48,7 @@ export function setFetch(fetch: typeof globalThis.fetch | WrappedFetch) {
_fetch = fetch as WrappedFetch;
}

export { clearCache, _fetch as fetch }
export { clearCache, _fetch as fetch };

/**
* Wraps a fetch request with pooling, and retry logic on exceptions (emfile / network errors).
Expand Down Expand Up @@ -75,8 +79,7 @@ function wrappedFetch(fetch: FetchFn): WrappedFetch {
try {
var res = await fetch(url, ...args);
} catch (e) {
if (retries++ >= retryCount)
throw e;
if (retries++ >= retryCount) throw e;
continue;
}
switch (res.status) {
Expand All @@ -92,12 +95,12 @@ function wrappedFetch(fetch: FetchFn): WrappedFetch {
try {
return await res.arrayBuffer();
} catch (e) {
if (retries++ >= retryCount &&
e.code === "ERR_SOCKET_TIMEOUT" ||
e.code === "ETIMEOUT" ||
e.code === "ECONNRESET" ||
e.code === 'FETCH_ERROR') {

if (
(retries++ >= retryCount && e.code === "ERR_SOCKET_TIMEOUT") ||
e.code === "ETIMEOUT" ||
e.code === "ECONNRESET" ||
e.code === "FETCH_ERROR"
) {
}
}
}
Expand All @@ -107,8 +110,7 @@ function wrappedFetch(fetch: FetchFn): WrappedFetch {
};
wrappedFetch.text = async function (url, ...args) {
const arrayBuffer = await this.arrayBuffer(url, ...args);
if (!arrayBuffer)
return null;
if (!arrayBuffer) return null;
return new TextDecoder().decode(arrayBuffer);
};
return wrappedFetch;
Expand All @@ -117,12 +119,10 @@ function wrappedFetch(fetch: FetchFn): WrappedFetch {
// restrict in-flight fetches to a pool of 100
let p = [];
let c = 0;
function pushFetchPool () {
if (++c > poolSize)
return new Promise(r => p.push(r));
function pushFetchPool() {
if (++c > poolSize) return new Promise((r) => p.push(r));
}
function popFetchPool () {
function popFetchPool() {
c--;
if (p.length)
p.shift()();
if (p.length) p.shift()();
}
17 changes: 11 additions & 6 deletions src/common/integrity.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
let _nodeCrypto;

export async function getIntegrityNodeLegacy(buf: Uint8Array | string): Promise<`sha384-${string}`> {
const hash = (_nodeCrypto || (_nodeCrypto = (await (0, eval)('import("node:crypto")')))).createHash("sha384");
export async function getIntegrityNodeLegacy(
buf: Uint8Array | string
): Promise<`sha384-${string}`> {
const hash = (
_nodeCrypto || (_nodeCrypto = await (0, eval)('import("node:crypto")'))
).createHash("sha384");
hash.update(buf);
return `sha384-${hash.digest("base64")}`;
}

export let getIntegrity = async function getIntegrity(buf: Uint8Array | string): Promise<`sha384-${string}`> {
export let getIntegrity = async function getIntegrity(
buf: Uint8Array | string
): Promise<`sha384-${string}`> {
const data = typeof buf === "string" ? new TextEncoder().encode(buf) : buf;
const hashBuffer = await crypto.subtle.digest("SHA-384", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashBase64 = btoa(String.fromCharCode(...hashArray));
return `sha384-${hashBase64}`;
}
};

if (typeof crypto === 'undefined')
getIntegrity = getIntegrityNodeLegacy;
if (typeof crypto === "undefined") getIntegrity = getIntegrityNodeLegacy;
Loading

0 comments on commit 56db9b0

Please sign in to comment.