@parhelia/capture 0.4.13014 → 0.4.13062

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/dist/cmsJob.d.ts CHANGED
@@ -7,6 +7,10 @@ export declare const CONTROLLER_EVENTS = "parheliaCaptureEvent";
7
7
  export declare const RELAY_BINDING = "parheliaCaptureRelay";
8
8
  export declare const RELAY_DELIVER = "parheliaCaptureDeliver";
9
9
  export declare const CAPTURE_COOKIE = "parhelia-capture";
10
+ /** The preview protocol's names (`frontend/packages/preview-ticket/fixtures/preview-protocol.json`; a test pins them). */
11
+ export declare const PREVIEW_CODE_PARAMETER = "parhelia_preview";
12
+ export declare const PREVIEW_HANDLE_COOKIE = "parhelia-preview";
13
+ export declare const PREVIEW_REFUSAL_HEADER = "parhelia-preview-refusal";
10
14
  /** Stops animations, transitions and the caret before measurement (plan CS-D4 step 1); applied through re-validation. */
11
15
  export declare const PRECONDITION_CSS = "*,*::before,*::after{animation:none!important;transition:none!important;caret-color:transparent!important;scroll-behavior:auto!important}";
12
16
  export type JobOutcome = {
@@ -35,12 +39,9 @@ export type CmsJobOptions = {
35
39
  /** The `host:port` of an origin as the egress proxy sees it on CONNECT or in an absolute request URL. */
36
40
  export declare function hostPortOf(origin: string): string;
37
41
  export declare function runCmsJob(offer: CaptureJobOffer, options: CmsJobOptions): Promise<JobOutcome>;
38
- /**
39
- * DevOps 8782: a head that takes per-person preview keeps the session it opens on redemption in a `Secure` cookie (`HttpOnly;
40
- * Secure; SameSite=None; Partitioned`), and Chromium keeps no `Secure` cookie that a plain-http page sets, loopback aside. A
41
- * worker reaching Parhelia at an internal http origin (parhelia-test: `http://10.10.10.21:8882`, docs/capture-service.md) lost
42
- * that session at once, and the head's page rendered without it: no rendered identity, the capture refused. The job's browser
43
- * treats that one origin, Parhelia's own (where proxied heads load), as secure; every other origin stays as Chromium judges it.
44
- */
45
- export declare function secureOriginLaunchArgs(origin: string): string[];
42
+ /** The code a frame URL carries and the same URL without it (the rest of the query as spelled); null for a URL with none. */
43
+ export declare function previewCodeOf(url: string): {
44
+ code: string;
45
+ without: string;
46
+ } | null;
46
47
  //# sourceMappingURL=cmsJob.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cmsJob.d.ts","sourceRoot":"","sources":["../src/cmsJob.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,OAAO,EAA+B,MAAM,iBAAiB,CAAC;AAC5E,OAAO,EAAqC,KAAK,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3D,eAAO,MAAM,eAAe,iCAAiC,CAAC;AAC9D,eAAO,MAAM,kBAAkB,oBAAoB,CAAC;AACpD,eAAO,MAAM,iBAAiB,yBAAyB,CAAC;AACxD,eAAO,MAAM,aAAa,yBAAyB,CAAC;AACpD,eAAO,MAAM,aAAa,2BAA2B,CAAC;AACtD,eAAO,MAAM,cAAc,qBAAqB,CAAC;AAEjD,yHAAyH;AACzH,eAAO,MAAM,gBAAgB,8IACgH,CAAC;AAE9I,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GACnC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACtD;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,CAAC;AAE1B,MAAM,MAAM,aAAa,GAAG;IAC1B,uHAAuH;IACvH,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC7C,0DAA0D;IAC1D,YAAY,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;IACpC,8EAA8E;IAC9E,MAAM,EAAE,WAAW,CAAC;IACpB,GAAG,CAAC,E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AAC,MAAM,EAAE,OAAO,CAAA;CAAE,GACnC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACtD;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,CAAC;AAE1B,MAAM,MAAM,aAAa,GAAG;IAC1B,uHAAuH;IACvH,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC7C,0DAA0D;IAC1D,YAAY,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;IACpC,8EAA8E;IAC9E,MAAM,EAAE,WAAW,CAAC;IACpB,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IAC9C,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,CAAC;IAC5C,gHAAgH;IAChH,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB,CAAC;AAEF,yGAAyG;AACzG,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAIjD;AAED,wBAAsB,SAAS,CAAC,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC,CAgCnG;AAkKD,6HAA6H;AAC7H,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAOnF"}
package/dist/cmsJob.js CHANGED
@@ -5,8 +5,14 @@ export const CONTROLLER_EVENTS = "parheliaCaptureEvent";
5
5
  export const RELAY_BINDING = "parheliaCaptureRelay";
6
6
  export const RELAY_DELIVER = "parheliaCaptureDeliver";
7
7
  export const CAPTURE_COOKIE = "parhelia-capture";
8
+ /** The preview protocol's names (`frontend/packages/preview-ticket/fixtures/preview-protocol.json`; a test pins them). */
9
+ export const PREVIEW_CODE_PARAMETER = "parhelia_preview";
10
+ export const PREVIEW_HANDLE_COOKIE = "parhelia-preview";
11
+ export const PREVIEW_REFUSAL_HEADER = "parhelia-preview-refusal";
8
12
  /** Stops animations, transitions and the caret before measurement (plan CS-D4 step 1); applied through re-validation. */
9
13
  export const PRECONDITION_CSS = "*,*::before,*::after{animation:none!important;transition:none!important;caret-color:transparent!important;scroll-behavior:auto!important}";
14
+ const PREVIEW_REDEEM_TIMEOUT_MS = 15_000;
15
+ const PREVIEW_SESSION_TIMEOUT_MS = 3_000;
10
16
  /** The `host:port` of an origin as the egress proxy sees it on CONNECT or in an absolute request URL. */
11
17
  export function hostPortOf(origin) {
12
18
  const url = new URL(origin);
@@ -25,7 +31,7 @@ export async function runCmsJob(offer, options) {
25
31
  const proxy = await startEgressProxy({ allow, log: options.egressLog });
26
32
  let browser;
27
33
  try {
28
- browser = await options.launch([...proxyLaunchArgs(proxy.url), ...secureOriginLaunchArgs(origin)]);
34
+ browser = await options.launch(proxyLaunchArgs(proxy.url));
29
35
  const context = await browser.newContext({
30
36
  viewport: { width: 1280, height: 900 },
31
37
  deviceScaleFactor: 1,
@@ -49,17 +55,6 @@ export async function runCmsJob(offer, options) {
49
55
  await proxy.close();
50
56
  }
51
57
  }
52
- /**
53
- * DevOps 8782: a head that takes per-person preview keeps the session it opens on redemption in a `Secure` cookie (`HttpOnly;
54
- * Secure; SameSite=None; Partitioned`), and Chromium keeps no `Secure` cookie that a plain-http page sets, loopback aside. A
55
- * worker reaching Parhelia at an internal http origin (parhelia-test: `http://10.10.10.21:8882`, docs/capture-service.md) lost
56
- * that session at once, and the head's page rendered without it: no rendered identity, the capture refused. The job's browser
57
- * treats that one origin, Parhelia's own (where proxied heads load), as secure; every other origin stays as Chromium judges it.
58
- */
59
- export function secureOriginLaunchArgs(origin) {
60
- const url = new URL(origin);
61
- return url.protocol === "http:" ? [`--unsafely-treat-insecure-origin-as-secure=${url.origin}`] : [];
62
- }
63
58
  /** The capture cookie on the Parhelia origin only (plan CS-D7): HttpOnly, Secure per the origin's scheme, path `/`. */
64
59
  async function addCaptureCookie(context, origin, offer) {
65
60
  const url = new URL(origin);
@@ -145,8 +140,19 @@ async function drive(context, offer, options, controllerUrl, targetUrl, log, now
145
140
  if (loaded)
146
141
  refuse("the target loads once");
147
142
  loaded = true;
148
- const response = await target.goto(targetUrl, { waitUntil: "domcontentloaded" });
143
+ const preview = previewCodeOf(targetUrl);
144
+ if (preview) {
145
+ const failure = await redeemInHiddenFrame(context, controller, targetUrl, preview.code);
146
+ if (failure) {
147
+ log({ event: "preview-not-redeemed", job: offer.jobId, errorCode: failure.errorCode, message: failure.message });
148
+ end(failure);
149
+ return false;
150
+ }
151
+ log({ event: "preview-redeemed", job: offer.jobId });
152
+ }
153
+ const response = await target.goto(preview?.without ?? targetUrl, { waitUntil: "domcontentloaded" });
149
154
  // Diagnostics: what the head answered (the path only: a query may carry a one-time preview code) and the identity it states.
155
+ const previewRefusal = response?.headers()[PREVIEW_REFUSAL_HEADER];
150
156
  const refusals = [];
151
157
  for (let hop = response?.request().redirectedFrom(); hop; hop = hop.redirectedFrom()) {
152
158
  const refusal = (await hop.response())?.headers()["x-parhelia-capture-refusal"];
@@ -155,7 +161,13 @@ async function drive(context, offer, options, controllerUrl, targetUrl, log, now
155
161
  }
156
162
  log({ event: "target-loaded", job: offer.jobId, status: response?.status() ?? null, path: new URL(target.url()).pathname,
157
163
  ...(refusals.length ? { captureRefusals: refusals } : {}),
164
+ ...(previewRefusal ? { previewRefusal } : {}),
158
165
  renderedCoordinate: await target.evaluate(() => document.querySelector("script[data-parhelia-rendered-coordinate]")?.textContent?.slice(0, 500) ?? null).catch(() => null) });
166
+ // The head's refusal is the document now: nothing in it can state the page, and the capture must not read as a wrong identity.
167
+ if (previewRefusal) {
168
+ end(previewRefused(previewRefusal));
169
+ return false;
170
+ }
159
171
  if (!(await target.evaluate(() => !!window.__PARHELIA_EDITING_BRIDGE_RUNTIME__))) {
160
172
  // The head's own bridge bowed out at top level (an older bridge, a loader that skips top-level pages): the host's
161
173
  // current bridge, by content.
@@ -210,6 +222,83 @@ async function drive(context, offer, options, controllerUrl, targetUrl, log, now
210
222
  active = false;
211
223
  }
212
224
  }
225
+ /** The code a frame URL carries and the same URL without it (the rest of the query as spelled); null for a URL with none. */
226
+ export function previewCodeOf(url) {
227
+ const parsed = new URL(url);
228
+ const code = parsed.searchParams.get(PREVIEW_CODE_PARAMETER);
229
+ if (code === null)
230
+ return null;
231
+ const kept = parsed.search.slice(1).split("&").filter((pair) => pair.split("=")[0] !== PREVIEW_CODE_PARAMETER);
232
+ parsed.search = kept.join("&");
233
+ return { code, without: parsed.toString() };
234
+ }
235
+ function previewRefused(refusal) {
236
+ return {
237
+ kind: "failed",
238
+ errorCode: "capture-preview-refused",
239
+ message: `The page's head refused the capture's preview (${refusal}).` +
240
+ (refusal === "preview-editor-origin-required"
241
+ ? " The worker opened it from the origin it reaches Parhelia at, which is not the editor origin this head is configured for."
242
+ : ""),
243
+ };
244
+ }
245
+ /**
246
+ * Redeems `code` as a browser does: a frame of the controller page, which is the editor's origin, with the editor's referrer policy, so the
247
+ * head's own rule reads real fetch metadata. The frame is sandboxed without scripts: a proxied head is same-origin with the controller page,
248
+ * and its scripts must not reach the worker's bindings there. The answer of the code's request is all that is wanted; the frame goes at once.
249
+ * The session cookie lands in the partition of the controller's site, so it moves to the context's own jar, where the top-level page sends it.
250
+ * Answers the failure, or nothing when the session is ready.
251
+ */
252
+ async function redeemInHiddenFrame(context, controller, frameUrl, code) {
253
+ let stopListening = () => { };
254
+ const answered = new Promise((resolve) => {
255
+ const timer = setTimeout(() => resolve(null), PREVIEW_REDEEM_TIMEOUT_MS);
256
+ const listener = (response) => {
257
+ if (!response.request().isNavigationRequest() || new URL(response.url()).searchParams.get(PREVIEW_CODE_PARAMETER) !== code)
258
+ return;
259
+ resolve(response);
260
+ };
261
+ context.on("response", listener);
262
+ stopListening = () => {
263
+ clearTimeout(timer);
264
+ context.off("response", listener);
265
+ };
266
+ });
267
+ try {
268
+ await controller.evaluate((url) => {
269
+ const frame = document.createElement("iframe");
270
+ frame.setAttribute("data-parhelia-capture-redemption", "");
271
+ frame.setAttribute("sandbox", "");
272
+ frame.referrerPolicy = "strict-origin";
273
+ frame.style.cssText = "position:absolute;width:0;height:0;border:0";
274
+ frame.src = url;
275
+ document.documentElement.appendChild(frame);
276
+ }, frameUrl);
277
+ const response = await answered;
278
+ if (!response)
279
+ return { kind: "failed", errorCode: "capture-preview-unanswered", message: "The page's head did not answer the capture's preview code in time." };
280
+ const refusal = response.headers()[PREVIEW_REFUSAL_HEADER];
281
+ if (refusal)
282
+ return previewRefused(refusal);
283
+ const until = Date.now() + PREVIEW_SESSION_TIMEOUT_MS;
284
+ for (;;) {
285
+ const session = (await context.cookies()).find((cookie) => cookie.name === PREVIEW_HANDLE_COOKIE);
286
+ if (session) {
287
+ await context.clearCookies({ name: PREVIEW_HANDLE_COOKIE });
288
+ await context.addCookies([{ name: session.name, value: session.value, domain: session.domain, path: session.path, expires: session.expires,
289
+ httpOnly: session.httpOnly, secure: session.secure, sameSite: session.sameSite }]);
290
+ return undefined;
291
+ }
292
+ if (Date.now() >= until)
293
+ return { kind: "failed", errorCode: "capture-preview-no-session", message: `The page's head answered the capture's preview code (${response.status()}) and set no session cookie.` };
294
+ await new Promise((resolve) => setTimeout(resolve, 100));
295
+ }
296
+ }
297
+ finally {
298
+ stopListening();
299
+ await controller.evaluate(() => document.querySelector("iframe[data-parhelia-capture-redemption]")?.remove()).catch(() => undefined);
300
+ }
301
+ }
213
302
  /** One pass down the page for lazy images, then back to the top (plan CS-D4 step 1). */
214
303
  async function prescroll(target) {
215
304
  await target.evaluate(async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parhelia/capture",
3
- "version": "0.4.13014",
3
+ "version": "0.4.13062",
4
4
  "description": "Parhelia's capture sidecar: real Chromium screenshots of CMS pages without an open editor. Install with `npx @parhelia/capture install`.",
5
5
  "publishConfig": {
6
6
  "access": "public"