Skip to content

Commit

Permalink
feat(route): add request .frame and .serviceWorker (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperStrike authored Jul 22, 2022
1 parent ba4da7c commit d969256
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/WS/WSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default class WSClient {
});
body = await bodyBlob.arrayBuffer();
}
const request = new RouteRequest(meta, body ?? null);
const request = new RouteRequest(meta, body ?? null, this.ws);
const route = new Route(this.ws, meta.id, request);
const requestURL = new URL(meta.url, window.location.origin);
const handlers = this.routes.filter((r) => r.matches(requestURL));
Expand Down
15 changes: 15 additions & 0 deletions src/WS/WSServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,22 @@ export default class WSServer {
}
const bodyBuffer = request.postDataBuffer();
const hasBody = bodyBuffer !== null;
let frame: number | null;
try {
frame = this.createHandleID(request.frame());
} catch {
frame = null;
}
const sw = request.serviceWorker();
client.send(createRouteMeta({
id: this.routeList.length,
hasBody,
frame,
headersArray,
isNavigationRequest: request.isNavigationRequest(),
method: request.method(),
resourceType: request.resourceType(),
serviceWorker: sw ? this.createHandleID(sw) : null,
url: request.url(),
}));
if (hasBody) {
Expand Down Expand Up @@ -262,6 +271,12 @@ export default class WSServer {
result = e instanceof Error ? e : String(e);
}
}
if (resolveID === null) {
if (error) {
throw result;
}
return;
}
source.send(createHandleMeta({
action: 'resolve',
id,
Expand Down
10 changes: 10 additions & 0 deletions src/WS/handle/NodeHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ const finalizationRegistry = new FinalizationRegistry(({ id, ws }: {
});

export default class NodeHandle<T = unknown> extends Handle {
/**
* Share the handle ID with an object so that the matching handles will keep referencing the node
* target until all ID users are disposed or garbage-collected.
* @internal
*/
static share(id: number, ws: WebSocket, withObj: object) {
idReferenceCounts[id] = (idReferenceCounts[id] ?? 0) + 1;
finalizationRegistry.register(withObj, { id, ws });
}

constructor(
id: number,
private readonly ws: WebSocket,
Expand Down
3 changes: 3 additions & 0 deletions src/WS/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ export interface RouteMetaBase {
export interface RouteRequestMeta extends RouteMetaBase {
id: number;
hasBody: boolean;

frame: number | null;
headersArray: { name: string, value: string }[];
isNavigationRequest: boolean;
method: string;
resourceType: string;
serviceWorker: number | null;
url: string;
}

Expand Down
21 changes: 20 additions & 1 deletion src/WS/route/RouteRequest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type playwright from 'playwright-core';
import NodeHandle from '../handle/NodeHandle.js';
import { RouteRequestMeta } from '../message.js';
import { FallbackOverrides } from './Route.js';

export default class RouteRequest {
constructor(
private readonly requestMeta: RouteRequestMeta,
private readonly requestBody: ArrayBuffer | null,
) {}
private readonly ws: WebSocket,
) {
const { frame, serviceWorker } = requestMeta;
if (frame !== null) NodeHandle.share(frame, ws, this);
if (serviceWorker !== null) NodeHandle.share(serviceWorker, ws, this);
}

private fallbackOverrides: FallbackOverrides = {};

Expand Down Expand Up @@ -39,6 +46,13 @@ export default class RouteRequest {
return this.cachedAllHeaders;
}

frame() {
if (this.requestMeta.frame === null) {
throw new Error('Service Worker requests do not have an associated frame');
}
return new NodeHandle<playwright.Frame>(this.requestMeta.frame, this.ws);
}

headerValue(name: string) {
return this.allHeaders()[name.toLowerCase()];
}
Expand Down Expand Up @@ -111,6 +125,11 @@ export default class RouteRequest {
return this.requestMeta.resourceType;
}

serviceWorker() {
if (this.requestMeta.serviceWorker === null) return null;
return new NodeHandle<playwright.Worker>(this.requestMeta.serviceWorker, this.ws);
}

url() {
return this.fallbackOverrides.url ?? this.requestMeta.url;
}
Expand Down

0 comments on commit d969256

Please sign in to comment.