Skip to content

Commit

Permalink
Allow browser fetch option overrides (#3096).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jun 30, 2022
1 parent d9046dd commit c309df8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/web/src.ts/browser-geturl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export async function getUrl(href: string, options?: Options): Promise<GetUrlRes
request.referrer = "client"; // no-referrer, *client
};

if (options.fetchOptions != null) {
const opts = options.fetchOptions;
if (opts.mode) { request.mode = <RequestMode>(opts.mode); }
if (opts.cache) { request.cache = <RequestCache>(opts.cache); }
if (opts.credentials) { request.credentials = <RequestCredentials>(opts.credentials); }
if (opts.redirect) { request.redirect = <RequestRedirect>(opts.redirect); }
if (opts.referrer) { request.referrer = opts.referrer; }
}

const response = await fetch(href, request);
const body = await response.arrayBuffer();

Expand Down
6 changes: 6 additions & 0 deletions packages/web/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type ConnectionInfo = {
throttleCallback?: (attempt: number, url: string) => Promise<boolean>,

skipFetchSetup?: boolean;
fetchOptions?: Record<string, string>;
errorPassThrough?: boolean;

timeout?: number,
Expand Down Expand Up @@ -158,7 +159,12 @@ export function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo,
if (connection.skipFetchSetup != null) {
options.skipFetchSetup = !!connection.skipFetchSetup;
}

if (connection.fetchOptions != null) {
options.fetchOptions = shallowCopy(connection.fetchOptions);
}
}

const reData = new RegExp("^data:([a-z0-9-]+/[a-z0-9-]+);base64,(.*)$", "i");
const dataMatch = ((url) ? url.match(reData): null);
if (dataMatch) {
Expand Down
1 change: 1 addition & 0 deletions packages/web/src.ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export type Options = {
body?: Uint8Array;
headers?: { [ key: string] : string };
skipFetchSetup?: boolean;
fetchOptions?: Record<string, string>;
};

0 comments on commit c309df8

Please sign in to comment.