@swiftbrowser/web 0.18.0 → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/blockedHosts.ts +3 -1
- package/src/corsProxy.ts +3 -1
- package/src/hostRequests.ts +9 -1
package/package.json
CHANGED
package/src/blockedHosts.ts
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
// TelemetryDeck signals): the app keeps working, those requests do not go out.
|
|
6
6
|
|
|
7
7
|
/** Whether `url` is on one of `hosts` (or a subdomain of one). */
|
|
8
|
+
import type { FetchLike } from './hostRequests';
|
|
9
|
+
|
|
8
10
|
export function isBlocked(url: string, hosts: readonly string[]): boolean {
|
|
9
11
|
let host: string;
|
|
10
12
|
try {
|
|
@@ -19,7 +21,7 @@ export function isBlocked(url: string, hosts: readonly string[]): boolean {
|
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
/** `fetchImpl`, failing requests to `hosts` as a network error does. */
|
|
22
|
-
export function withBlockedHosts(fetchImpl:
|
|
24
|
+
export function withBlockedHosts(fetchImpl: FetchLike, hosts: readonly string[]): FetchLike {
|
|
23
25
|
if (hosts.length === 0) return fetchImpl;
|
|
24
26
|
return async (input, init) => {
|
|
25
27
|
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
|
package/src/corsProxy.ts
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
// request that may change something (POST, PUT…) is never sent twice.
|
|
9
9
|
|
|
10
10
|
/** Whether `url` (absolute) is http(s) on another origin than `pageOrigin`. */
|
|
11
|
+
import type { FetchLike } from './hostRequests';
|
|
12
|
+
|
|
11
13
|
export function isCrossOrigin(url: string, pageOrigin: string): boolean {
|
|
12
14
|
try {
|
|
13
15
|
const target = new URL(url);
|
|
@@ -30,7 +32,7 @@ export function proxiedURL(proxy: string, target: string): string {
|
|
|
30
32
|
* to a page); an answer with any status is the server's and is kept. An
|
|
31
33
|
* aborted request is not retried.
|
|
32
34
|
*/
|
|
33
|
-
export function withCORSProxy(fetchImpl:
|
|
35
|
+
export function withCORSProxy(fetchImpl: FetchLike, proxy: string, pageOrigin: string): FetchLike {
|
|
34
36
|
return async (input, init) => {
|
|
35
37
|
try {
|
|
36
38
|
return await fetchImpl(input, init);
|
package/src/hostRequests.ts
CHANGED
|
@@ -95,8 +95,16 @@ export interface PickFilesResult {
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/** The browser facilities a request handler uses; tests substitute them. */
|
|
98
|
+
/**
|
|
99
|
+
* What the environment fetches with: the global `fetch`'s call, and no more.
|
|
100
|
+
* Not `typeof fetch`, which in Bun's types also carries `fetch.preconnect`,
|
|
101
|
+
* so a wrapper (the CORS proxy's, the blocked hosts', an app's own) would
|
|
102
|
+
* not be one.
|
|
103
|
+
*/
|
|
104
|
+
export type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
105
|
+
|
|
98
106
|
export interface HostRequestEnvironment {
|
|
99
|
-
fetch:
|
|
107
|
+
fetch: FetchLike;
|
|
100
108
|
/** Decodes an image and resolves with its natural size; rejects when it cannot be loaded. */
|
|
101
109
|
loadImage: (url: string) => Promise<ImageResult>;
|
|
102
110
|
/** The Web Share API (`navigator.share`), where the browser has it. */
|