@parhelia/preview-ticket 0.0.0-stage → 1.0.12951
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/README.md +59 -2
- package/dist/alpaca/index.d.ts +101 -0
- package/dist/alpaca/index.d.ts.map +1 -0
- package/dist/alpaca/index.js +200 -0
- package/dist/client.d.ts +36 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +125 -0
- package/dist/config.d.ts +52 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +51 -0
- package/dist/constants.d.ts +80 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +79 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +46 -0
- package/dist/cookie.d.ts +7 -0
- package/dist/cookie.d.ts.map +1 -0
- package/dist/cookie.js +29 -0
- package/dist/errors.d.ts +91 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +114 -0
- package/dist/fetch/index.d.ts +51 -0
- package/dist/fetch/index.d.ts.map +1 -0
- package/dist/fetch/index.js +44 -0
- package/dist/gateway.d.ts +17 -0
- package/dist/gateway.d.ts.map +1 -0
- package/dist/gateway.js +41 -0
- package/dist/headers.d.ts +4 -0
- package/dist/headers.d.ts.map +1 -0
- package/dist/headers.js +40 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/kit.d.ts +54 -0
- package/dist/kit.d.ts.map +1 -0
- package/dist/kit.js +347 -0
- package/dist/next/app.d.ts +8 -0
- package/dist/next/app.d.ts.map +1 -0
- package/dist/next/app.js +14 -0
- package/dist/next/index.d.ts +58 -0
- package/dist/next/index.d.ts.map +1 -0
- package/dist/next/index.js +121 -0
- package/dist/redact.d.ts +3 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +11 -0
- package/dist/refusal.d.ts +20 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +26 -0
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +21 -0
- package/dist/signing.d.ts +44 -0
- package/dist/signing.d.ts.map +1 -0
- package/dist/signing.js +94 -0
- package/dist/store.d.ts +59 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +69 -0
- package/package.json +66 -4
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names this package shares with Parhelia's per-person preview routes. The .NET side's one
|
|
3
|
+
* definition is the `Parhelia.Preview.Protocol` library (`PreviewProtocol`, used by Parhelia and
|
|
4
|
+
* .NET heads); a shared fixture of the preview protocol pins both equal. A rename changes this
|
|
5
|
+
* file, `client.ts`, the fixture and the library.
|
|
6
|
+
*/
|
|
7
|
+
/** The frame-URL query parameter Parhelia appends the one-time exchange code in. */
|
|
8
|
+
export declare const PREVIEW_CODE_PARAMETER = "parhelia_preview";
|
|
9
|
+
/**
|
|
10
|
+
* The head's handle cookie. Fixed on purpose: Parhelia's rendering proxy passes exactly this
|
|
11
|
+
* cookie name to an opted-in head and drops every other cookie whose name contains "parhelia".
|
|
12
|
+
*/
|
|
13
|
+
export declare const PREVIEW_HANDLE_COOKIE = "parhelia-preview";
|
|
14
|
+
/** The request header a head sends its ticket in on every gateway read. */
|
|
15
|
+
export declare const PREVIEW_TICKET_HEADER = "X-Parhelia-Preview-Ticket";
|
|
16
|
+
/** The header naming a refusal on every refused preview answer (Parhelia's and this package's). */
|
|
17
|
+
export declare const PREVIEW_REFUSAL_HEADER = "Parhelia-Preview-Refusal";
|
|
18
|
+
/**
|
|
19
|
+
* The preview protocol's version: 2 since a head signs its redemption with its key and proves it per ticket on gateway
|
|
20
|
+
* reads (`signing.ts`) — the key never travels. Parhelia refuses version 1's bearer redemption by name
|
|
21
|
+
* (`RedemptionRefusals.Unsigned`).
|
|
22
|
+
*/
|
|
23
|
+
export declare const PREVIEW_PROTOCOL_VERSION = 2;
|
|
24
|
+
/** The signed redemption's headers (`signRedemption`): the key id, the timestamp, the nonce, the target origin, the signature. */
|
|
25
|
+
export declare const PREVIEW_KEY_ID_HEADER = "Parhelia-Preview-Key-Id";
|
|
26
|
+
export declare const PREVIEW_TIMESTAMP_HEADER = "Parhelia-Preview-Timestamp";
|
|
27
|
+
export declare const PREVIEW_NONCE_HEADER = "Parhelia-Preview-Nonce";
|
|
28
|
+
export declare const PREVIEW_ORIGIN_HEADER = "Parhelia-Preview-Origin";
|
|
29
|
+
export declare const PREVIEW_SIGNATURE_HEADER = "Parhelia-Preview-Signature";
|
|
30
|
+
/** The first line of a redemption's canonical string. */
|
|
31
|
+
export declare const REDEMPTION_SCHEME = "PARHELIA-PREVIEW-REDEEM-2";
|
|
32
|
+
/** The first line of a gateway credential's canonical string. */
|
|
33
|
+
export declare const GATEWAY_SCHEME = "PARHELIA-PREVIEW-GATEWAY-2";
|
|
34
|
+
/** How far a redemption's timestamp may lie from Parhelia's clock, either way (seconds). */
|
|
35
|
+
export declare const SIGNATURE_SKEW_SECONDS = 120;
|
|
36
|
+
/**
|
|
37
|
+
* The header a share-link review guest's signed review scope travels in (Parhelia's gateway answers and the requests it
|
|
38
|
+
* forwards to a CMS-side package). This package sends and reads none; it is a protocol name all the same, pinned by
|
|
39
|
+
* `protocol.test.ts` (`PreviewProtocol.ReviewScopeHeader` in .NET).
|
|
40
|
+
*/
|
|
41
|
+
export declare const PREVIEW_REVIEW_SCOPE_HEADER = "Parhelia-Review-Scope";
|
|
42
|
+
/**
|
|
43
|
+
* The JWS `typ` of the review scope that travels in `PREVIEW_REVIEW_SCOPE_HEADER`: the scope's format name. Format 2 carries the
|
|
44
|
+
* locales per page (a page's locale bounds that page and its subitems only; `*` is every locale of that one page). A format
|
|
45
|
+
* whose meaning changes gets a new type, and a verifier accepts exactly its own, so a site and a Parhelia on different formats
|
|
46
|
+
* refuse guests by name. This package verifies no scope; the name is pinned with the .NET library's `ReviewScopeToken.Type` by
|
|
47
|
+
* `protocol.test.ts` (`reviewScopeType` in `fixtures/preview-protocol.json`).
|
|
48
|
+
*/
|
|
49
|
+
export declare const PREVIEW_REVIEW_SCOPE_TYPE = "parhelia-review-scope-v2+jwt";
|
|
50
|
+
/**
|
|
51
|
+
* The query parameter Parhelia puts on every canvas frame URL (`parhelia=1`). Only a request
|
|
52
|
+
* carrying it is checked for a lost handle cookie; every other request is left alone.
|
|
53
|
+
*/
|
|
54
|
+
export declare const PARHELIA_FRAME_MARKER_PARAMETER = "parhelia";
|
|
55
|
+
/** The ticket's absolute cap on the Parhelia side (the sign-in's 8 h); the handle cookie lives as long. */
|
|
56
|
+
export declare const DEFAULT_SESSION_CAP_SECONDS: number;
|
|
57
|
+
/** How long a redirected frame URL is remembered to recognise a browser that dropped the handle cookie. */
|
|
58
|
+
export declare const DEFAULT_COOKIE_CHECK_SECONDS: number;
|
|
59
|
+
/** How long the head waits for Parhelia to redeem a code. */
|
|
60
|
+
export declare const DEFAULT_REDEMPTION_TIMEOUT_MS = 5000;
|
|
61
|
+
/** Environment variables the default configuration reads (at request time, never at import). */
|
|
62
|
+
export declare const ENV_PARHELIA_URL = "PARHELIA_PREVIEW_URL";
|
|
63
|
+
/** Setup plan E: the one name for the Parhelia origin; the gateway URL when `PARHELIA_PREVIEW_URL` is unset. */
|
|
64
|
+
export declare const ENV_PARHELIA_ORIGIN = "PARHELIA_ORIGIN";
|
|
65
|
+
export declare const ENV_PREVIEW_KEY = "PARHELIA_PREVIEW_KEY";
|
|
66
|
+
export declare const ENV_ALLOW_HTTP = "PARHELIA_PREVIEW_ALLOW_HTTP";
|
|
67
|
+
/** The store id a session's reads go to through the gateway (one of the host entry's `previewStores`). */
|
|
68
|
+
export declare const ENV_PREVIEW_STORE = "PARHELIA_PREVIEW_STORE";
|
|
69
|
+
/** The store this head renders (`@parhelia/next/identity`); the preview store when `PARHELIA_PREVIEW_STORE` is unset. */
|
|
70
|
+
export declare const ENV_STORE = "PARHELIA_STORE";
|
|
71
|
+
/**
|
|
72
|
+
* The request header Parhelia's rendering proxy stamps on every request it forwards to a head whose host entry declares an
|
|
73
|
+
* `editorSecret` or opts in to the per-person preview (`previewTicket=on`; without a secret the value is `proxied`, a
|
|
74
|
+
* statement, not a secret), and drops when a browser sends it. This package reads only whether it is present: it tells a proxied
|
|
75
|
+
* request from one the browser sent the head directly. Checking its value is the head's (`@parhelia/next/server`).
|
|
76
|
+
* Lower case, as Node keys incoming headers; the name is `editorSignalHeader` of `fixtures/preview-protocol.json`
|
|
77
|
+
* (`PreviewProtocol.EditorSignalHeader` in .NET), pinned by `protocol.test.ts`.
|
|
78
|
+
*/
|
|
79
|
+
export declare const EDITOR_SIGNAL_HEADER = "x-parhelia-editor";
|
|
80
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,oFAAoF;AACpF,eAAO,MAAM,sBAAsB,qBAAqB,CAAC;AAEzD;;;GAGG;AACH,eAAO,MAAM,qBAAqB,qBAAqB,CAAC;AAExD,2EAA2E;AAC3E,eAAO,MAAM,qBAAqB,8BAA8B,CAAC;AAEjE,mGAAmG;AACnG,eAAO,MAAM,sBAAsB,6BAA6B,CAAC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,IAAI,CAAC;AAE1C,kIAAkI;AAClI,eAAO,MAAM,qBAAqB,4BAA4B,CAAC;AAC/D,eAAO,MAAM,wBAAwB,+BAA+B,CAAC;AACrE,eAAO,MAAM,oBAAoB,2BAA2B,CAAC;AAC7D,eAAO,MAAM,qBAAqB,4BAA4B,CAAC;AAC/D,eAAO,MAAM,wBAAwB,+BAA+B,CAAC;AAErE,yDAAyD;AACzD,eAAO,MAAM,iBAAiB,8BAA8B,CAAC;AAE7D,iEAAiE;AACjE,eAAO,MAAM,cAAc,+BAA+B,CAAC;AAE3D,4FAA4F;AAC5F,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAE1C;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,0BAA0B,CAAC;AAEnE;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,iCAAiC,CAAC;AAExE;;;GAGG;AACH,eAAO,MAAM,+BAA+B,aAAa,CAAC;AAE1D,2GAA2G;AAC3G,eAAO,MAAM,2BAA2B,QAAc,CAAC;AAEvD,2GAA2G;AAC3G,eAAO,MAAM,4BAA4B,QAAU,CAAC;AAEpD,6DAA6D;AAC7D,eAAO,MAAM,6BAA6B,OAAO,CAAC;AAElD,gGAAgG;AAChG,eAAO,MAAM,gBAAgB,yBAAyB,CAAC;AACvD,gHAAgH;AAChH,eAAO,MAAM,mBAAmB,oBAAoB,CAAC;AACrD,eAAO,MAAM,eAAe,yBAAyB,CAAC;AACtD,eAAO,MAAM,cAAc,gCAAgC,CAAC;AAC5D,0GAA0G;AAC1G,eAAO,MAAM,iBAAiB,2BAA2B,CAAC;AAC1D,yHAAyH;AACzH,eAAO,MAAM,SAAS,mBAAmB,CAAC;AAE1C;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names this package shares with Parhelia's per-person preview routes. The .NET side's one
|
|
3
|
+
* definition is the `Parhelia.Preview.Protocol` library (`PreviewProtocol`, used by Parhelia and
|
|
4
|
+
* .NET heads); a shared fixture of the preview protocol pins both equal. A rename changes this
|
|
5
|
+
* file, `client.ts`, the fixture and the library.
|
|
6
|
+
*/
|
|
7
|
+
/** The frame-URL query parameter Parhelia appends the one-time exchange code in. */
|
|
8
|
+
export const PREVIEW_CODE_PARAMETER = "parhelia_preview";
|
|
9
|
+
/**
|
|
10
|
+
* The head's handle cookie. Fixed on purpose: Parhelia's rendering proxy passes exactly this
|
|
11
|
+
* cookie name to an opted-in head and drops every other cookie whose name contains "parhelia".
|
|
12
|
+
*/
|
|
13
|
+
export const PREVIEW_HANDLE_COOKIE = "parhelia-preview";
|
|
14
|
+
/** The request header a head sends its ticket in on every gateway read. */
|
|
15
|
+
export const PREVIEW_TICKET_HEADER = "X-Parhelia-Preview-Ticket";
|
|
16
|
+
/** The header naming a refusal on every refused preview answer (Parhelia's and this package's). */
|
|
17
|
+
export const PREVIEW_REFUSAL_HEADER = "Parhelia-Preview-Refusal";
|
|
18
|
+
/**
|
|
19
|
+
* The preview protocol's version: 2 since a head signs its redemption with its key and proves it per ticket on gateway
|
|
20
|
+
* reads (`signing.ts`) — the key never travels. Parhelia refuses version 1's bearer redemption by name
|
|
21
|
+
* (`RedemptionRefusals.Unsigned`).
|
|
22
|
+
*/
|
|
23
|
+
export const PREVIEW_PROTOCOL_VERSION = 2;
|
|
24
|
+
/** The signed redemption's headers (`signRedemption`): the key id, the timestamp, the nonce, the target origin, the signature. */
|
|
25
|
+
export const PREVIEW_KEY_ID_HEADER = "Parhelia-Preview-Key-Id";
|
|
26
|
+
export const PREVIEW_TIMESTAMP_HEADER = "Parhelia-Preview-Timestamp";
|
|
27
|
+
export const PREVIEW_NONCE_HEADER = "Parhelia-Preview-Nonce";
|
|
28
|
+
export const PREVIEW_ORIGIN_HEADER = "Parhelia-Preview-Origin";
|
|
29
|
+
export const PREVIEW_SIGNATURE_HEADER = "Parhelia-Preview-Signature";
|
|
30
|
+
/** The first line of a redemption's canonical string. */
|
|
31
|
+
export const REDEMPTION_SCHEME = "PARHELIA-PREVIEW-REDEEM-2";
|
|
32
|
+
/** The first line of a gateway credential's canonical string. */
|
|
33
|
+
export const GATEWAY_SCHEME = "PARHELIA-PREVIEW-GATEWAY-2";
|
|
34
|
+
/** How far a redemption's timestamp may lie from Parhelia's clock, either way (seconds). */
|
|
35
|
+
export const SIGNATURE_SKEW_SECONDS = 120;
|
|
36
|
+
/**
|
|
37
|
+
* The header a share-link review guest's signed review scope travels in (Parhelia's gateway answers and the requests it
|
|
38
|
+
* forwards to a CMS-side package). This package sends and reads none; it is a protocol name all the same, pinned by
|
|
39
|
+
* `protocol.test.ts` (`PreviewProtocol.ReviewScopeHeader` in .NET).
|
|
40
|
+
*/
|
|
41
|
+
export const PREVIEW_REVIEW_SCOPE_HEADER = "Parhelia-Review-Scope";
|
|
42
|
+
/**
|
|
43
|
+
* The JWS `typ` of the review scope that travels in `PREVIEW_REVIEW_SCOPE_HEADER`: the scope's format name. Format 2 carries the
|
|
44
|
+
* locales per page (a page's locale bounds that page and its subitems only; `*` is every locale of that one page). A format
|
|
45
|
+
* whose meaning changes gets a new type, and a verifier accepts exactly its own, so a site and a Parhelia on different formats
|
|
46
|
+
* refuse guests by name. This package verifies no scope; the name is pinned with the .NET library's `ReviewScopeToken.Type` by
|
|
47
|
+
* `protocol.test.ts` (`reviewScopeType` in `fixtures/preview-protocol.json`).
|
|
48
|
+
*/
|
|
49
|
+
export const PREVIEW_REVIEW_SCOPE_TYPE = "parhelia-review-scope-v2+jwt";
|
|
50
|
+
/**
|
|
51
|
+
* The query parameter Parhelia puts on every canvas frame URL (`parhelia=1`). Only a request
|
|
52
|
+
* carrying it is checked for a lost handle cookie; every other request is left alone.
|
|
53
|
+
*/
|
|
54
|
+
export const PARHELIA_FRAME_MARKER_PARAMETER = "parhelia";
|
|
55
|
+
/** The ticket's absolute cap on the Parhelia side (the sign-in's 8 h); the handle cookie lives as long. */
|
|
56
|
+
export const DEFAULT_SESSION_CAP_SECONDS = 8 * 60 * 60;
|
|
57
|
+
/** How long a redirected frame URL is remembered to recognise a browser that dropped the handle cookie. */
|
|
58
|
+
export const DEFAULT_COOKIE_CHECK_SECONDS = 15 * 60;
|
|
59
|
+
/** How long the head waits for Parhelia to redeem a code. */
|
|
60
|
+
export const DEFAULT_REDEMPTION_TIMEOUT_MS = 5000;
|
|
61
|
+
/** Environment variables the default configuration reads (at request time, never at import). */
|
|
62
|
+
export const ENV_PARHELIA_URL = "PARHELIA_PREVIEW_URL";
|
|
63
|
+
/** Setup plan E: the one name for the Parhelia origin; the gateway URL when `PARHELIA_PREVIEW_URL` is unset. */
|
|
64
|
+
export const ENV_PARHELIA_ORIGIN = "PARHELIA_ORIGIN";
|
|
65
|
+
export const ENV_PREVIEW_KEY = "PARHELIA_PREVIEW_KEY";
|
|
66
|
+
export const ENV_ALLOW_HTTP = "PARHELIA_PREVIEW_ALLOW_HTTP";
|
|
67
|
+
/** The store id a session's reads go to through the gateway (one of the host entry's `previewStores`). */
|
|
68
|
+
export const ENV_PREVIEW_STORE = "PARHELIA_PREVIEW_STORE";
|
|
69
|
+
/** The store this head renders (`@parhelia/next/identity`); the preview store when `PARHELIA_PREVIEW_STORE` is unset. */
|
|
70
|
+
export const ENV_STORE = "PARHELIA_STORE";
|
|
71
|
+
/**
|
|
72
|
+
* The request header Parhelia's rendering proxy stamps on every request it forwards to a head whose host entry declares an
|
|
73
|
+
* `editorSecret` or opts in to the per-person preview (`previewTicket=on`; without a secret the value is `proxied`, a
|
|
74
|
+
* statement, not a secret), and drops when a browser sends it. This package reads only whether it is present: it tells a proxied
|
|
75
|
+
* request from one the browser sent the head directly. Checking its value is the head's (`@parhelia/next/server`).
|
|
76
|
+
* Lower case, as Node keys incoming headers; the name is `editorSignalHeader` of `fixtures/preview-protocol.json`
|
|
77
|
+
* (`PreviewProtocol.EditorSignalHeader` in .NET), pinned by `protocol.test.ts`.
|
|
78
|
+
*/
|
|
79
|
+
export const EDITOR_SIGNAL_HEADER = "x-parhelia-editor";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type PreviewRefusal } from "./errors.js";
|
|
2
|
+
/** The referrer policy of every frame the editor opens with a code (the editor's origin as `Referer`, never a path or the code). */
|
|
3
|
+
export declare const FRAME_REFERRER_POLICY = "strict-origin";
|
|
4
|
+
/**
|
|
5
|
+
* Why a code presented with these request headers is not redeemed for the editor at `editorOrigin`, or undefined when it
|
|
6
|
+
* may be (the module notes). `editorOrigin` is only needed for a directly framed request.
|
|
7
|
+
*/
|
|
8
|
+
export declare const redemptionContextRefusal: (headers: Headers, editorOrigin: string | undefined) => PreviewRefusal | undefined;
|
|
9
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAiBA,OAAO,EAA8B,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAG9E,oIAAoI;AACpI,eAAO,MAAM,qBAAqB,kBAAkB,CAAC;AAErD;;;GAGG;AACH,eAAO,MAAM,wBAAwB,YAC1B,OAAO,gBACF,MAAM,GAAG,SAAS,KAC/B,cAAc,GAAG,SAsCnB,CAAC"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The editor-bound redemption rule: where a preview code may be redeemed at all (the preview protocol's
|
|
3
|
+
* `redemptionContext`; the .NET twin is `PreviewRedemptionContext` in `Parhelia.Preview.Protocol`). A code that signs a browser in as someone else is login CSRF: a link or another site's page could plant
|
|
4
|
+
* one person's session in another person's browser. The browser's own fetch metadata, which no page can set, says where a
|
|
5
|
+
* request comes from; a head checks it before it redeems, and refuses by name without calling Parhelia or setting a cookie.
|
|
6
|
+
*
|
|
7
|
+
* - `Sec-Fetch-Dest: iframe`, or `preview-frame-required`: a code opened as a page, typed, or sent without fetch metadata
|
|
8
|
+
* (which cannot tell a frame from a page) is refused.
|
|
9
|
+
* - Through Parhelia's rendering proxy (the editor signal header present: a browser cannot put it on a navigation) the head
|
|
10
|
+
* is served from the editor's own origin: `Sec-Fetch-Site: same-origin`, or `preview-frame-required`.
|
|
11
|
+
* - Framed directly: `Sec-Fetch-Site: cross-site` (a frame of the head's own site, `same-site` or `same-origin`, is
|
|
12
|
+
* `preview-editor-same-site` — a partitioned cookie is keyed by the top-level site only, so the session would reach the
|
|
13
|
+
* site's own tabs), and `Referer` naming the configured editor origin, or `preview-editor-origin-required` (another
|
|
14
|
+
* site's page that framed the head with a code it obtained, or no `Referer` at all: fail closed). The editor opens coded
|
|
15
|
+
* frames with the referrer policy {@link FRAME_REFERRER_POLICY}, so the header carries its origin only.
|
|
16
|
+
*/
|
|
17
|
+
import { ENV_PARHELIA_ORIGIN, EDITOR_SIGNAL_HEADER } from "./constants.js";
|
|
18
|
+
import { HeadPreviewStates, refusal } from "./errors.js";
|
|
19
|
+
import { previewOrigin } from "./signing.js";
|
|
20
|
+
/** The referrer policy of every frame the editor opens with a code (the editor's origin as `Referer`, never a path or the code). */
|
|
21
|
+
export const FRAME_REFERRER_POLICY = "strict-origin";
|
|
22
|
+
/**
|
|
23
|
+
* Why a code presented with these request headers is not redeemed for the editor at `editorOrigin`, or undefined when it
|
|
24
|
+
* may be (the module notes). `editorOrigin` is only needed for a directly framed request.
|
|
25
|
+
*/
|
|
26
|
+
export const redemptionContextRefusal = (headers, editorOrigin) => {
|
|
27
|
+
const value = (name) => headers.get(name)?.trim().toLowerCase();
|
|
28
|
+
if (value("sec-fetch-dest") !== "iframe")
|
|
29
|
+
return refusal(HeadPreviewStates.FrameRequired, "A preview code is redeemed only in the frame of Parhelia's editor; open the page from the editor.");
|
|
30
|
+
const site = value("sec-fetch-site");
|
|
31
|
+
if (headers.has(EDITOR_SIGNAL_HEADER))
|
|
32
|
+
return site === "same-origin"
|
|
33
|
+
? undefined
|
|
34
|
+
: refusal(HeadPreviewStates.FrameRequired, "A preview code through Parhelia's rendering proxy is redeemed only in a frame of the editor's own page.");
|
|
35
|
+
if (site === "same-site" || site === "same-origin")
|
|
36
|
+
return refusal(HeadPreviewStates.EditorSameSite, "The editor that framed this page is on the site's own address; per-person preview needs the editor on another site.");
|
|
37
|
+
if (site !== "cross-site")
|
|
38
|
+
return refusal(HeadPreviewStates.FrameRequired, "A preview code is redeemed only in the frame of Parhelia's editor; open the page from the editor.");
|
|
39
|
+
const editor = editorOrigin ? previewOrigin(editorOrigin) : undefined;
|
|
40
|
+
if (!editor)
|
|
41
|
+
return refusal(HeadPreviewStates.HeadNotConfigured, `The head has no ${ENV_PARHELIA_ORIGIN} configured: a directly framed preview code is redeemed only for the editor at that origin.`);
|
|
42
|
+
const referer = headers.get("referer");
|
|
43
|
+
return referer && previewOrigin(referer) === editor
|
|
44
|
+
? undefined
|
|
45
|
+
: refusal(HeadPreviewStates.EditorOriginRequired, "This frame was not opened by the Parhelia editor this site is set up for.");
|
|
46
|
+
};
|
package/dist/cookie.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** The exact `Set-Cookie` value for a handle. */
|
|
2
|
+
export declare const handleCookie: (handle: string, path: string, maxAgeSeconds: number) => string;
|
|
3
|
+
/** A cookie path: the head's base path, or `/`. Anything unsafe for the attribute becomes `/`. */
|
|
4
|
+
export declare const cookiePath: (path: string | undefined) => string;
|
|
5
|
+
/** Every value of the handle cookie in a `Cookie` header, in the browser's order. */
|
|
6
|
+
export declare const handleCookieValues: (cookieHeader: string | null | undefined) => string[];
|
|
7
|
+
//# sourceMappingURL=cookie.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../src/cookie.ts"],"names":[],"mappings":"AAOA,iDAAiD;AACjD,eAAO,MAAM,YAAY,WACf,MAAM,QACR,MAAM,iBACG,MAAM,KACpB,MAC8J,CAAC;AAElK,kGAAkG;AAClG,eAAO,MAAM,UAAU,SAAU,MAAM,GAAG,SAAS,KAAG,MAIrD,CAAC;AAEF,qFAAqF;AACrF,eAAO,MAAM,kBAAkB,iBACf,MAAM,GAAG,IAAI,GAAG,SAAS,KACtC,MAAM,EAUR,CAAC"}
|
package/dist/cookie.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The head's handle cookie: `HttpOnly; Secure; SameSite=None; Partitioned`.
|
|
3
|
+
* `Partitioned` keys it to the editor's top-level site, so it is sent only while the head is framed
|
|
4
|
+
* by the editor; `SameSite=None; Secure` lets a cross-site frame (a direct-mode head) carry it.
|
|
5
|
+
*/
|
|
6
|
+
import { PREVIEW_HANDLE_COOKIE } from "./constants.js";
|
|
7
|
+
/** The exact `Set-Cookie` value for a handle. */
|
|
8
|
+
export const handleCookie = (handle, path, maxAgeSeconds) => `${PREVIEW_HANDLE_COOKIE}=${handle}; Path=${cookiePath(path)}; Max-Age=${Math.max(0, Math.floor(maxAgeSeconds))}; HttpOnly; Secure; SameSite=None; Partitioned`;
|
|
9
|
+
/** A cookie path: the head's base path, or `/`. Anything unsafe for the attribute becomes `/`. */
|
|
10
|
+
export const cookiePath = (path) => {
|
|
11
|
+
if (!path || !path.startsWith("/") || /[;\s\x00-\x1f\x7f,]/.test(path))
|
|
12
|
+
return "/";
|
|
13
|
+
return path;
|
|
14
|
+
};
|
|
15
|
+
/** Every value of the handle cookie in a `Cookie` header, in the browser's order. */
|
|
16
|
+
export const handleCookieValues = (cookieHeader) => {
|
|
17
|
+
if (!cookieHeader)
|
|
18
|
+
return [];
|
|
19
|
+
const values = [];
|
|
20
|
+
for (const pair of cookieHeader.split(";")) {
|
|
21
|
+
const equals = pair.indexOf("=");
|
|
22
|
+
if (equals < 0)
|
|
23
|
+
continue;
|
|
24
|
+
if (pair.slice(0, equals).trim() !== PREVIEW_HANDLE_COOKIE)
|
|
25
|
+
continue;
|
|
26
|
+
values.push(pair.slice(equals + 1).trim());
|
|
27
|
+
}
|
|
28
|
+
return values;
|
|
29
|
+
};
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The named states a per-person preview can end in on the head. Parhelia's own refusals
|
|
3
|
+
* (`preview-code-*`, `preview-ticket-*`, `preview-throttled`, ...) pass through unchanged; the
|
|
4
|
+
* ones below are the head's own. None of them ever falls back to the head's site-wide credential.
|
|
5
|
+
*/
|
|
6
|
+
export declare const HeadPreviewStates: {
|
|
7
|
+
/** The frame URL carried no usable code (malformed, or more than one). Parhelia was not called. */
|
|
8
|
+
readonly CodeInvalid: "preview-code-invalid";
|
|
9
|
+
/** The head has no Parhelia URL or preview key configured (or an insecure URL). */
|
|
10
|
+
readonly HeadNotConfigured: "preview-head-not-configured";
|
|
11
|
+
/** Parhelia could not be reached to redeem the code (network, timeout, a redirect). */
|
|
12
|
+
readonly RedemptionUnreachable: "preview-redemption-unreachable";
|
|
13
|
+
/** Parhelia answered the redemption with something that is not a ticket. */
|
|
14
|
+
readonly RedemptionMalformed: "preview-redemption-malformed";
|
|
15
|
+
/** Parhelia refused the redemption without a recognisable name. */
|
|
16
|
+
readonly RedemptionRefused: "preview-redemption-refused";
|
|
17
|
+
/** The browser presented a handle this head does not know (expired, evicted, another instance's store). */
|
|
18
|
+
readonly SessionLost: "preview-session-lost";
|
|
19
|
+
/** The browser did not send back the handle cookie right after the redemption (third-party cookies blocked). */
|
|
20
|
+
readonly CookieBlocked: "preview-cookie-blocked";
|
|
21
|
+
/** The head's ticket store failed. */
|
|
22
|
+
readonly StoreUnavailable: "preview-store-unavailable";
|
|
23
|
+
/** The in-memory store was used where the middleware and the render cannot share it (Edge runtime). */
|
|
24
|
+
readonly StoreNotShared: "preview-store-not-shared";
|
|
25
|
+
/** A code arrived outside a frame (opened as a page, no fetch metadata), or a proxied one not from the editor's own origin. Parhelia was not called. */
|
|
26
|
+
readonly FrameRequired: "preview-frame-required";
|
|
27
|
+
/** A directly framed code arrived from a page of the head's own site: the editor must be on another site. Parhelia was not called. */
|
|
28
|
+
readonly EditorSameSite: "preview-editor-same-site";
|
|
29
|
+
/** A directly framed code arrived from a page that is not the configured editor's (another Referer, or none). Parhelia was not called. */
|
|
30
|
+
readonly EditorOriginRequired: "preview-editor-origin-required";
|
|
31
|
+
};
|
|
32
|
+
export type HeadPreviewState = (typeof HeadPreviewStates)[keyof typeof HeadPreviewStates];
|
|
33
|
+
/**
|
|
34
|
+
* Parhelia's refusals of a redemption whose signature it does not accept (each 401; the .NET twin is
|
|
35
|
+
* `PreviewRedemptionRefusals`). A head passes them through like every other refusal Parhelia names.
|
|
36
|
+
*/
|
|
37
|
+
export declare const RedemptionRefusals: {
|
|
38
|
+
/** No signature: the version 1 bearer form, or none. Parhelia accepts only signed redemptions. */
|
|
39
|
+
readonly Unsigned: "preview-redemption-unsigned";
|
|
40
|
+
/** A signature header is malformed, or the signature is not the request's under the named key. */
|
|
41
|
+
readonly SignatureInvalid: "preview-signature-invalid";
|
|
42
|
+
/** The timestamp lies too far from Parhelia's clock. */
|
|
43
|
+
readonly SignatureStale: "preview-signature-stale";
|
|
44
|
+
/** The nonce was presented before under the same key. */
|
|
45
|
+
readonly SignatureReplayed: "preview-signature-replayed";
|
|
46
|
+
/** No opted-in host entry has the named key. */
|
|
47
|
+
readonly KeyUnknown: "preview-key-unknown";
|
|
48
|
+
/** The request was signed for another origin than this Parhelia accepts redemptions at. */
|
|
49
|
+
readonly Origin: "preview-signature-origin";
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* A refusal name is `preview-` (a preview refusal) or `review-scope-` (a review-scope refusal)
|
|
53
|
+
* plus lower-case words; anything else is not echoed.
|
|
54
|
+
*/
|
|
55
|
+
export declare const isRefusalName: (value: unknown) => value is string;
|
|
56
|
+
/** The HTTP status a head answers a named state with. */
|
|
57
|
+
export declare const statusOfState: (code: string, fallback?: number) => number;
|
|
58
|
+
/** One named preview refusal as the head keeps and shows it. Never carries a code, ticket or handle. */
|
|
59
|
+
export interface PreviewRefusal {
|
|
60
|
+
readonly code: string;
|
|
61
|
+
readonly status: number;
|
|
62
|
+
/** Parhelia's words (or the head's), for the person; plain text. */
|
|
63
|
+
readonly message: string;
|
|
64
|
+
}
|
|
65
|
+
export declare const refusal: (code: string, message: string, status?: number) => PreviewRefusal;
|
|
66
|
+
/**
|
|
67
|
+
* Thrown by `previewHeaders()` / `readPreviewSession()` when a preview session exists but cannot
|
|
68
|
+
* serve this render. The head's error path renders; nothing renders with the site-wide credential.
|
|
69
|
+
*/
|
|
70
|
+
export declare class ParheliaPreviewError extends Error {
|
|
71
|
+
readonly code: string;
|
|
72
|
+
readonly status: number;
|
|
73
|
+
/** The words for the person, without the code prefix. */
|
|
74
|
+
readonly detail: string;
|
|
75
|
+
constructor(state: PreviewRefusal);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Thrown by a head's delivery client for a read of its CMS refused with a status other than not found, with the name the
|
|
79
|
+
* answer gave (the gateway's refusal header, the CMS's error) or a head-side name. The read may be the published site's or the
|
|
80
|
+
* editing person's (through Parhelia's preview gateway): only inside a preview session is it a named preview state
|
|
81
|
+
* (`deliveryRefusal`); outside one it is a plain failure of the render. That is why it is no {@link ParheliaPreviewError},
|
|
82
|
+
* which a head shows as a preview state wherever it is thrown.
|
|
83
|
+
*/
|
|
84
|
+
export declare class DeliveryReadRefused extends Error {
|
|
85
|
+
readonly status: number;
|
|
86
|
+
readonly code: string;
|
|
87
|
+
/** The words for the person, without the code prefix. */
|
|
88
|
+
readonly detail: string;
|
|
89
|
+
constructor(status: number, code: string, detail: string);
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,iBAAiB;IAC5B,mGAAmG;aACnG,WAAW,EAAE,sBAAsB;IACnC,mFAAmF;aACnF,iBAAiB,EAAE,6BAA6B;IAChD,uFAAuF;aACvF,qBAAqB,EAAE,gCAAgC;IACvD,4EAA4E;aAC5E,mBAAmB,EAAE,8BAA8B;IACnD,mEAAmE;aACnE,iBAAiB,EAAE,4BAA4B;IAC/C,2GAA2G;aAC3G,WAAW,EAAE,sBAAsB;IACnC,gHAAgH;aAChH,aAAa,EAAE,wBAAwB;IACvC,sCAAsC;aACtC,gBAAgB,EAAE,2BAA2B;IAC7C,uGAAuG;aACvG,cAAc,EAAE,0BAA0B;IAC1C,wJAAwJ;aACxJ,aAAa,EAAE,wBAAwB;IACvC,sIAAsI;aACtI,cAAc,EAAE,0BAA0B;IAC1C,0IAA0I;aAC1I,oBAAoB,EAAE,gCAAgC;CAC9C,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAC1B,CAAC,OAAO,iBAAiB,CAAC,CAAC,MAAM,OAAO,iBAAiB,CAAC,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,kBAAkB;IAC7B,kGAAkG;aAClG,QAAQ,EAAE,6BAA6B;IACvC,kGAAkG;aAClG,gBAAgB,EAAE,2BAA2B;IAC7C,wDAAwD;aACxD,cAAc,EAAE,yBAAyB;IACzC,yDAAyD;aACzD,iBAAiB,EAAE,4BAA4B;IAC/C,gDAAgD;aAChD,UAAU,EAAE,qBAAqB;IACjC,2FAA2F;aAC3F,MAAM,EAAE,0BAA0B;CAC1B,CAAC;AAiBX;;;GAGG;AACH,eAAO,MAAM,aAAa,UAAW,OAAO,KAAG,KAAK,IAAI,MAGpC,CAAC;AAErB,yDAAyD;AACzD,eAAO,MAAM,aAAa,SAAU,MAAM,wBAAmB,MAC5B,CAAC;AAElC,wGAAwG;AACxG,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,eAAO,MAAM,OAAO,SACZ,MAAM,WACH,MAAM,WACN,MAAM,KACd,cAID,CAAC;AAEH;;;GAGG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,YAAY,KAAK,EAAE,cAAc,EAQhC;CACF;AAED;;;;;;GAMG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,YAAY,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAMvD;CACF"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The named states a per-person preview can end in on the head. Parhelia's own refusals
|
|
3
|
+
* (`preview-code-*`, `preview-ticket-*`, `preview-throttled`, ...) pass through unchanged; the
|
|
4
|
+
* ones below are the head's own. None of them ever falls back to the head's site-wide credential.
|
|
5
|
+
*/
|
|
6
|
+
export const HeadPreviewStates = {
|
|
7
|
+
/** The frame URL carried no usable code (malformed, or more than one). Parhelia was not called. */
|
|
8
|
+
CodeInvalid: "preview-code-invalid",
|
|
9
|
+
/** The head has no Parhelia URL or preview key configured (or an insecure URL). */
|
|
10
|
+
HeadNotConfigured: "preview-head-not-configured",
|
|
11
|
+
/** Parhelia could not be reached to redeem the code (network, timeout, a redirect). */
|
|
12
|
+
RedemptionUnreachable: "preview-redemption-unreachable",
|
|
13
|
+
/** Parhelia answered the redemption with something that is not a ticket. */
|
|
14
|
+
RedemptionMalformed: "preview-redemption-malformed",
|
|
15
|
+
/** Parhelia refused the redemption without a recognisable name. */
|
|
16
|
+
RedemptionRefused: "preview-redemption-refused",
|
|
17
|
+
/** The browser presented a handle this head does not know (expired, evicted, another instance's store). */
|
|
18
|
+
SessionLost: "preview-session-lost",
|
|
19
|
+
/** The browser did not send back the handle cookie right after the redemption (third-party cookies blocked). */
|
|
20
|
+
CookieBlocked: "preview-cookie-blocked",
|
|
21
|
+
/** The head's ticket store failed. */
|
|
22
|
+
StoreUnavailable: "preview-store-unavailable",
|
|
23
|
+
/** The in-memory store was used where the middleware and the render cannot share it (Edge runtime). */
|
|
24
|
+
StoreNotShared: "preview-store-not-shared",
|
|
25
|
+
/** A code arrived outside a frame (opened as a page, no fetch metadata), or a proxied one not from the editor's own origin. Parhelia was not called. */
|
|
26
|
+
FrameRequired: "preview-frame-required",
|
|
27
|
+
/** A directly framed code arrived from a page of the head's own site: the editor must be on another site. Parhelia was not called. */
|
|
28
|
+
EditorSameSite: "preview-editor-same-site",
|
|
29
|
+
/** A directly framed code arrived from a page that is not the configured editor's (another Referer, or none). Parhelia was not called. */
|
|
30
|
+
EditorOriginRequired: "preview-editor-origin-required",
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Parhelia's refusals of a redemption whose signature it does not accept (each 401; the .NET twin is
|
|
34
|
+
* `PreviewRedemptionRefusals`). A head passes them through like every other refusal Parhelia names.
|
|
35
|
+
*/
|
|
36
|
+
export const RedemptionRefusals = {
|
|
37
|
+
/** No signature: the version 1 bearer form, or none. Parhelia accepts only signed redemptions. */
|
|
38
|
+
Unsigned: "preview-redemption-unsigned",
|
|
39
|
+
/** A signature header is malformed, or the signature is not the request's under the named key. */
|
|
40
|
+
SignatureInvalid: "preview-signature-invalid",
|
|
41
|
+
/** The timestamp lies too far from Parhelia's clock. */
|
|
42
|
+
SignatureStale: "preview-signature-stale",
|
|
43
|
+
/** The nonce was presented before under the same key. */
|
|
44
|
+
SignatureReplayed: "preview-signature-replayed",
|
|
45
|
+
/** No opted-in host entry has the named key. */
|
|
46
|
+
KeyUnknown: "preview-key-unknown",
|
|
47
|
+
/** The request was signed for another origin than this Parhelia accepts redemptions at. */
|
|
48
|
+
Origin: "preview-signature-origin",
|
|
49
|
+
};
|
|
50
|
+
const defaultStatus = {
|
|
51
|
+
[HeadPreviewStates.CodeInvalid]: 400,
|
|
52
|
+
[HeadPreviewStates.HeadNotConfigured]: 500,
|
|
53
|
+
[HeadPreviewStates.RedemptionUnreachable]: 502,
|
|
54
|
+
[HeadPreviewStates.RedemptionMalformed]: 502,
|
|
55
|
+
[HeadPreviewStates.RedemptionRefused]: 403,
|
|
56
|
+
[HeadPreviewStates.SessionLost]: 401,
|
|
57
|
+
[HeadPreviewStates.CookieBlocked]: 401,
|
|
58
|
+
[HeadPreviewStates.StoreUnavailable]: 503,
|
|
59
|
+
[HeadPreviewStates.StoreNotShared]: 500,
|
|
60
|
+
[HeadPreviewStates.FrameRequired]: 403,
|
|
61
|
+
[HeadPreviewStates.EditorSameSite]: 403,
|
|
62
|
+
[HeadPreviewStates.EditorOriginRequired]: 403,
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* A refusal name is `preview-` (a preview refusal) or `review-scope-` (a review-scope refusal)
|
|
66
|
+
* plus lower-case words; anything else is not echoed.
|
|
67
|
+
*/
|
|
68
|
+
export const isRefusalName = (value) => typeof value === "string" &&
|
|
69
|
+
/^(?:preview|review-scope)-[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value) &&
|
|
70
|
+
value.length <= 64;
|
|
71
|
+
/** The HTTP status a head answers a named state with. */
|
|
72
|
+
export const statusOfState = (code, fallback = 403) => defaultStatus[code] ?? fallback;
|
|
73
|
+
export const refusal = (code, message, status) => ({
|
|
74
|
+
code,
|
|
75
|
+
status: status ?? statusOfState(code),
|
|
76
|
+
message,
|
|
77
|
+
});
|
|
78
|
+
/**
|
|
79
|
+
* Thrown by `previewHeaders()` / `readPreviewSession()` when a preview session exists but cannot
|
|
80
|
+
* serve this render. The head's error path renders; nothing renders with the site-wide credential.
|
|
81
|
+
*/
|
|
82
|
+
export class ParheliaPreviewError extends Error {
|
|
83
|
+
code;
|
|
84
|
+
status;
|
|
85
|
+
/** The words for the person, without the code prefix. */
|
|
86
|
+
detail;
|
|
87
|
+
constructor(state) {
|
|
88
|
+
super(`Per-person preview is unavailable (${state.code}).${state.message ? " " + state.message : ""}`);
|
|
89
|
+
this.name = "ParheliaPreviewError";
|
|
90
|
+
this.code = state.code;
|
|
91
|
+
this.status = state.status;
|
|
92
|
+
this.detail = state.message;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Thrown by a head's delivery client for a read of its CMS refused with a status other than not found, with the name the
|
|
97
|
+
* answer gave (the gateway's refusal header, the CMS's error) or a head-side name. The read may be the published site's or the
|
|
98
|
+
* editing person's (through Parhelia's preview gateway): only inside a preview session is it a named preview state
|
|
99
|
+
* (`deliveryRefusal`); outside one it is a plain failure of the render. That is why it is no {@link ParheliaPreviewError},
|
|
100
|
+
* which a head shows as a preview state wherever it is thrown.
|
|
101
|
+
*/
|
|
102
|
+
export class DeliveryReadRefused extends Error {
|
|
103
|
+
status;
|
|
104
|
+
code;
|
|
105
|
+
/** The words for the person, without the code prefix. */
|
|
106
|
+
detail;
|
|
107
|
+
constructor(status, code, detail) {
|
|
108
|
+
super(`${code} (${status}): ${detail}`);
|
|
109
|
+
this.name = "DeliveryReadRefused";
|
|
110
|
+
this.status = status;
|
|
111
|
+
this.code = code;
|
|
112
|
+
this.detail = detail;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@parhelia/preview-ticket/fetch` — per-person preview for a head whose server is a Web fetch handler,
|
|
3
|
+
* `(request: Request) => Response | Promise<Response>`: TanStack Start's server entry, React Router, Hono, srvx, Bun, Deno,
|
|
4
|
+
* a Worker. No framework import: the preview step wraps the handler, and the head's server code reads the session from
|
|
5
|
+
* the same request.
|
|
6
|
+
*
|
|
7
|
+
* // src/server.ts (TanStack Start)
|
|
8
|
+
* import handler from "@tanstack/react-start/server-entry";
|
|
9
|
+
* import { withParheliaPreview } from "@parhelia/preview-ticket/fetch";
|
|
10
|
+
* export default { fetch: withParheliaPreview((request) => handler.fetch(request), { basePath }) };
|
|
11
|
+
*
|
|
12
|
+
* // a server function or loader of the same process
|
|
13
|
+
* const session = await readPreviewSession(getRequest());
|
|
14
|
+
*
|
|
15
|
+
* The answers keep the core's relative `Location` (`./segment?query`): a browser resolves it against the URL it asked
|
|
16
|
+
* for, which below Parhelia's rendering proxy is the proxied one, not the head's own.
|
|
17
|
+
*/
|
|
18
|
+
import type { PreviewKitOptions } from "../config.js";
|
|
19
|
+
import { type PreviewHeaders } from "../headers.js";
|
|
20
|
+
import { type CookieSource, type PreviewKit, type PreviewSession } from "../kit.js";
|
|
21
|
+
/** A Web fetch handler. */
|
|
22
|
+
export type FetchHandler = (request: Request) => Response | Promise<Response>;
|
|
23
|
+
export interface ParheliaPreviewWrapOptions {
|
|
24
|
+
/** The head's base path below the rendering proxy (`/parhelia/proxy/<name>`): the handle cookie's path. */
|
|
25
|
+
readonly basePath?: string;
|
|
26
|
+
}
|
|
27
|
+
export interface ParheliaPreviewFetch {
|
|
28
|
+
readonly kit: PreviewKit;
|
|
29
|
+
/**
|
|
30
|
+
* Runs the preview step before `handler`: the 303 that removes a code from the URL or a named refusal is answered
|
|
31
|
+
* without calling it; a live session's response is made `private, no-store`; any other request reaches it untouched.
|
|
32
|
+
*/
|
|
33
|
+
wrap(handler: FetchHandler, options?: ParheliaPreviewWrapOptions): (request: Request) => Promise<Response>;
|
|
34
|
+
/** The live session of a request, or null; throws `ParheliaPreviewError` when a session cannot serve. */
|
|
35
|
+
readPreviewSession(source: CookieSource): Promise<PreviewSession | null>;
|
|
36
|
+
/** The ticket header map of a request (`{}` without a session); throws `ParheliaPreviewError` when a session cannot serve. */
|
|
37
|
+
previewHeaders(source: CookieSource): Promise<PreviewHeaders>;
|
|
38
|
+
}
|
|
39
|
+
export declare const createParheliaPreviewFetch: (options?: PreviewKitOptions) => ParheliaPreviewFetch;
|
|
40
|
+
/** The instance configured from `PARHELIA_PREVIEW_URL` / `PARHELIA_PREVIEW_KEY` with the shared in-memory store. */
|
|
41
|
+
export declare const defaultParheliaPreviewFetch: () => ParheliaPreviewFetch;
|
|
42
|
+
/** Wraps a fetch handler with the default configuration. */
|
|
43
|
+
export declare const withParheliaPreview: (handler: FetchHandler, options?: ParheliaPreviewWrapOptions) => ((request: Request) => Promise<Response>);
|
|
44
|
+
/** The live session of a request, default configuration. */
|
|
45
|
+
export declare const readPreviewSession: (source: CookieSource) => Promise<PreviewSession | null>;
|
|
46
|
+
/** The ticket header map of a request, default configuration. */
|
|
47
|
+
export declare const previewHeaders: (source: CookieSource) => Promise<PreviewHeaders>;
|
|
48
|
+
export type { CookieSource, PreviewSession } from "../kit.js";
|
|
49
|
+
export type { PreviewHeaders } from "../headers.js";
|
|
50
|
+
export type { PreviewKitOptions } from "../config.js";
|
|
51
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAoB,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,cAAc,EACpB,MAAM,WAAW,CAAC;AAEnB,2BAA2B;AAC3B,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAE9E,MAAM,WAAW,0BAA0B;IACzC,2GAA2G;IAC3G,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC;IACzB;;;OAGG;IACH,IAAI,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,0BAA0B,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC3G,yGAAyG;IACzG,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IACzE,8HAA8H;IAC9H,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;CAC/D;AAED,eAAO,MAAM,0BAA0B,aAC5B,iBAAiB,KACzB,oBAgBF,CAAC;AAIF,oHAAoH;AACpH,eAAO,MAAM,2BAA2B,QAAO,oBACK,CAAC;AAErD,4DAA4D;AAC5D,eAAO,MAAM,mBAAmB,YACrB,YAAY,YACX,0BAA0B,KACnC,CAAC,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CACW,CAAC;AAEvD,4DAA4D;AAC5D,eAAO,MAAM,kBAAkB,WACrB,YAAY,KACnB,OAAO,CAAC,cAAc,GAAG,IAAI,CAC0B,CAAC;AAE3D,iEAAiE;AACjE,eAAO,MAAM,cAAc,WAAY,YAAY,KAAG,OAAO,CAAC,cAAc,CACtB,CAAC;AAoBvD,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC9D,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { previewHeaderMap } from "../headers.js";
|
|
2
|
+
import { createPreviewKit, NO_STORE_HEADERS, } from "../kit.js";
|
|
3
|
+
export const createParheliaPreviewFetch = (options = {}) => {
|
|
4
|
+
const kit = createPreviewKit(options);
|
|
5
|
+
return {
|
|
6
|
+
kit,
|
|
7
|
+
wrap: (handler, { basePath } = {}) => async (request) => {
|
|
8
|
+
const decision = await kit.handle(request, { basePath: basePath || undefined });
|
|
9
|
+
if (decision.kind === "answer")
|
|
10
|
+
return decision.response;
|
|
11
|
+
const response = await handler(request);
|
|
12
|
+
return decision.kind === "live" ? withNoStore(response) : response;
|
|
13
|
+
},
|
|
14
|
+
readPreviewSession: (source) => kit.readSession(source),
|
|
15
|
+
previewHeaders: async (source) => (await kit.readSession(source))?.headers ?? previewHeaderMap(),
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
let defaultInstance;
|
|
19
|
+
/** The instance configured from `PARHELIA_PREVIEW_URL` / `PARHELIA_PREVIEW_KEY` with the shared in-memory store. */
|
|
20
|
+
export const defaultParheliaPreviewFetch = () => (defaultInstance ??= createParheliaPreviewFetch());
|
|
21
|
+
/** Wraps a fetch handler with the default configuration. */
|
|
22
|
+
export const withParheliaPreview = (handler, options) => defaultParheliaPreviewFetch().wrap(handler, options);
|
|
23
|
+
/** The live session of a request, default configuration. */
|
|
24
|
+
export const readPreviewSession = (source) => defaultParheliaPreviewFetch().readPreviewSession(source);
|
|
25
|
+
/** The ticket header map of a request, default configuration. */
|
|
26
|
+
export const previewHeaders = (source) => defaultParheliaPreviewFetch().previewHeaders(source);
|
|
27
|
+
const withNoStore = (response) => {
|
|
28
|
+
try {
|
|
29
|
+
for (const [name, value] of Object.entries(NO_STORE_HEADERS))
|
|
30
|
+
response.headers.set(name, value);
|
|
31
|
+
return response;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// Immutable headers (Response.redirect, a fetched response): copy the response.
|
|
35
|
+
const headers = new Headers(response.headers);
|
|
36
|
+
for (const [name, value] of Object.entries(NO_STORE_HEADERS))
|
|
37
|
+
headers.set(name, value);
|
|
38
|
+
return new Response(response.body, {
|
|
39
|
+
status: response.status,
|
|
40
|
+
statusText: response.statusText,
|
|
41
|
+
headers,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gateway a live session reads through. A read carries the session's `credential` in the CMS SDK's own credential slot
|
|
3
|
+
* (bearer token, API key) and the session's `headers`.
|
|
4
|
+
*/
|
|
5
|
+
export interface PreviewGateway {
|
|
6
|
+
/** `{parhelia}/parhelia/preview/{store}`: the CMS's own path is appended unchanged. */
|
|
7
|
+
readonly baseUrl: string;
|
|
8
|
+
/** The store id the reads go to. */
|
|
9
|
+
readonly storeId: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The gateway from the environment (default `process.env`, read at call time). Throws {@link ParheliaPreviewError}
|
|
13
|
+
* `preview-head-not-configured` (500) when the URL, the key or the store is missing, or the URL is not acceptable — a
|
|
14
|
+
* named state for the page to show, never a render with the head's own credential.
|
|
15
|
+
*/
|
|
16
|
+
export declare const previewGatewayFromEnv: (env?: Readonly<Record<string, string | undefined>>) => PreviewGateway;
|
|
17
|
+
//# sourceMappingURL=gateway.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.d.ts","sourceRoot":"","sources":["../src/gateway.ts"],"names":[],"mappings":"AAqBA;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,uFAAuF;IACvF,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,oCAAoC;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,SAC3B,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,KAIhD,cAgBF,CAAC"}
|