@procedures/http 0.52.2 → 0.53.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/src/host-fetch.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  /**
2
- * A `fetch` for GET requests that goes through the host policy (audit pass 2, O55).
2
+ * A `fetch` for GET and HEAD requests that goes through the host policy (audit pass 2, O55).
3
3
  *
4
- * A procedure of another package that reads a URL of its caller uses this function in place of
5
- * the global `fetch`: `weather.forecast`, and the modules of `@procedures/geo` and
4
+ * A procedure of another package uses this function in place of the global `fetch` for a URL of
5
+ * its caller. `weather.forecast` uses it, and so do the modules of `@procedures/geo` and
6
6
  * `@procedures/ics` that a host makes with it. The request has the same checks as `http.request`:
7
7
  *
8
8
  * - The host of the URL and of each redirect must pass the host policy. The default policy
9
9
  * refuses loopback, private, link-local and the other special addresses.
10
- * - The connection uses only the addresses that the check allowed (DNS rebinding).
10
+ * - The connection uses only the addresses that passed the check (DNS rebinding).
11
11
  * - The body of the response has the size limit of the settings.
12
+ * - A `HEAD` request reads only the headers. Thus a caller that needs the headers of a large page,
13
+ * for example for `frameRefusal`, does not read its body.
12
14
  *
13
15
  * A refusal is `FORBIDDEN` with the reason `host-not-allowed` (`isHostNotAllowed`). When the
14
16
  * signal of the caller aborts, the function throws the reason of the signal, as `fetch` does.
@@ -23,6 +25,8 @@ const LONGEST_MS = 86_400_000;
23
25
 
24
26
  /** The options of one request. */
25
27
  export interface HostFetchInit {
28
+ /** The method of the request. The default is `GET`. */
29
+ readonly method?: "GET" | "HEAD" | undefined;
26
30
  /** The headers of the request. */
27
31
  readonly headers?: Readonly<Record<string, string>> | undefined;
28
32
  /** The signal that stops the request. */
@@ -35,15 +39,17 @@ export interface HostFetchResponse {
35
39
  readonly status: number;
36
40
  /** The URL of the response: the last URL after the redirects. */
37
41
  readonly url: string;
42
+ /** The headers of the response, with names in lowercase. A header that repeats, for example `set-cookie`, is a list. */
43
+ readonly headers: Readonly<Record<string, string | readonly string[]>>;
38
44
  text(): Promise<string>;
39
45
  json(): Promise<unknown>;
40
46
  }
41
47
 
42
- /** A `fetch` for GET requests through the host policy. */
48
+ /** A `fetch` for GET and HEAD requests through the host policy. */
43
49
  export type HostFetch = (url: string, init?: HostFetchInit) => Promise<HostFetchResponse>;
44
50
 
45
51
  /**
46
- * This function gives a `fetch` for GET requests through the host policy of the options. The
52
+ * This function gives a `fetch` for GET and HEAD requests through the host policy of the options. The
47
53
  * options are the options of `createHttpModule()`. Without options, the policy is the default
48
54
  * policy: each public host, and no other host. A bad entry of `allow` throws here.
49
55
  *
@@ -57,7 +63,7 @@ export function createHostFetch(options: HttpModuleOptions = {}): HostFetch {
57
63
  let output;
58
64
  try {
59
65
  output = await httpRequest(
60
- { url, method: "GET", ...(init.headers !== undefined ? { headers: { ...init.headers } } : {}), responseType: "text", timeoutMs: LONGEST_MS },
66
+ { url, method: init.method ?? "GET", ...(init.headers !== undefined ? { headers: { ...init.headers } } : {}), responseType: "text", timeoutMs: LONGEST_MS },
61
67
  { signal: init.signal },
62
68
  settings,
63
69
  );
@@ -70,6 +76,7 @@ export function createHostFetch(options: HttpModuleOptions = {}): HostFetch {
70
76
  ok: output.ok,
71
77
  status: output.status,
72
78
  url: output.url,
79
+ headers: output.headers,
73
80
  text: async () => text,
74
81
  json: async () => JSON.parse(text) as unknown,
75
82
  };
package/src/index.ts CHANGED
@@ -11,3 +11,6 @@ export { isPublicAddress, parseAddress, specialClass } from "./address.js";
11
11
  export { HttpError, type HttpErrorCode } from "./errors.js";
12
12
  export { DEFAULT_MAX_BYTES, DEFAULT_ITEM_BYTES, DEFAULT_TIMEOUT_MS, DEFAULT_SETTINGS, type HttpSettings } from "./exchange.js";
13
13
  export { ndjsonLines, sseEvents, type NdjsonLine, type ServerSentEvent } from "./parsers.js";
14
+ // The frame rules of a response: frame-ancestors of CSP3 and X-Frame-Options
15
+ export { frameRefusal, parseCsp, sourceAllows, sourceListAllows } from "./frame.js";
16
+ export type { CspPolicy, FrameOrigins, FrameRefusal, HeaderSource } from "./frame.js";