@pagepocket/lighterceptor-adapter 0.4.2 → 0.5.0

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/index.d.ts CHANGED
@@ -1,12 +1,17 @@
1
- import type { NetworkInterceptorAdapter, SnapshotData } from "@pagepocket/lib";
1
+ import type { InterceptSession, InterceptTarget, NetworkEventHandlers, NetworkInterceptorAdapter } from "@pagepocket/lib";
2
2
  import { type LighterceptorOptions } from "@pagepocket/lighterceptor";
3
- type LighterceptorAdapterOptions = LighterceptorOptions & {
4
- title?: string;
5
- };
3
+ type LighterceptorAdapterOptions = LighterceptorOptions;
6
4
  export declare class LighterceptorAdapter implements NetworkInterceptorAdapter {
5
+ readonly name = "lighterceptor";
6
+ readonly capabilities: {
7
+ canGetResponseBody: boolean;
8
+ canStreamResponseBody: boolean;
9
+ canGetRequestBody: boolean;
10
+ providesResourceType: boolean;
11
+ };
7
12
  private options;
8
13
  constructor(options?: LighterceptorAdapterOptions);
9
- run(url: string): Promise<SnapshotData>;
14
+ start(target: InterceptTarget, handlers: NetworkEventHandlers): Promise<InterceptSession>;
10
15
  }
11
16
  export {};
12
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,yBAAyB,EACzB,YAAY,EACb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAErF,KAAK,2BAA2B,GAAG,oBAAoB,GAAG;IACxD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,qBAAa,oBAAqB,YAAW,yBAAyB;IACpE,OAAO,CAAC,OAAO,CAA8B;gBAEjC,OAAO,GAAE,2BAAgC;IAI/C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CAkB9C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EACf,oBAAoB,EACpB,yBAAyB,EAK1B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAErF,KAAK,2BAA2B,GAAG,oBAAoB,CAAC;AAsExD,qBAAa,oBAAqB,YAAW,yBAAyB;IACpE,QAAQ,CAAC,IAAI,mBAAmB;IAChC,QAAQ,CAAC,YAAY;;;;;MAKnB;IAEF,OAAO,CAAC,OAAO,CAA8B;gBAEjC,OAAO,GAAE,2BAAgC;IAI/C,KAAK,CACT,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,oBAAoB,GAC7B,OAAO,CAAC,gBAAgB,CAAC;CAkE7B"}
package/dist/index.js CHANGED
@@ -1,22 +1,135 @@
1
1
  import { Lighterceptor } from "@pagepocket/lighterceptor";
2
+ const decodeBase64 = (input) => {
3
+ const bufferCtor = globalThis.Buffer;
4
+ if (bufferCtor) {
5
+ return new Uint8Array(bufferCtor.from(input, "base64"));
6
+ }
7
+ const binary = atob(input);
8
+ const bytes = new Uint8Array(binary.length);
9
+ for (let i = 0; i < binary.length; i += 1) {
10
+ bytes[i] = binary.charCodeAt(i);
11
+ }
12
+ return bytes;
13
+ };
14
+ const encodeUtf8 = (input) => new TextEncoder().encode(input);
15
+ const getHeaderValue = (headers, name) => {
16
+ const target = name.toLowerCase();
17
+ for (const key in headers) {
18
+ if (key.toLowerCase() === target) {
19
+ return headers[key];
20
+ }
21
+ }
22
+ return undefined;
23
+ };
24
+ const inferResourceType = (source, headers) => {
25
+ if (source === "fetch")
26
+ return "fetch";
27
+ if (source === "xhr")
28
+ return "xhr";
29
+ if (source === "css")
30
+ return "stylesheet";
31
+ if (source === "img")
32
+ return "image";
33
+ const contentType = (getHeaderValue(headers, "content-type") || "").toLowerCase();
34
+ if (contentType.includes("text/html"))
35
+ return "document";
36
+ if (contentType.includes("text/css"))
37
+ return "stylesheet";
38
+ if (contentType.includes("javascript"))
39
+ return "script";
40
+ if (contentType.startsWith("image/"))
41
+ return "image";
42
+ if (contentType.startsWith("font/") || contentType.includes("woff"))
43
+ return "font";
44
+ if (contentType.startsWith("audio/") || contentType.startsWith("video/"))
45
+ return "media";
46
+ return undefined;
47
+ };
48
+ const toRequestEvent = (record) => {
49
+ const headers = record.headers ?? {};
50
+ const headersForType = record.responseHeaders && Object.keys(record.responseHeaders).length > 0
51
+ ? record.responseHeaders
52
+ : headers;
53
+ const resourceType = inferResourceType(record.source, headersForType);
54
+ const event = {
55
+ type: "request",
56
+ requestId: record.requestId,
57
+ url: record.url,
58
+ method: record.method,
59
+ headers,
60
+ resourceType: resourceType,
61
+ timestamp: record.timestamp
62
+ };
63
+ return event;
64
+ };
2
65
  export class LighterceptorAdapter {
3
66
  constructor(options = {}) {
67
+ this.name = "lighterceptor";
68
+ this.capabilities = {
69
+ canGetResponseBody: true,
70
+ canStreamResponseBody: false,
71
+ canGetRequestBody: false,
72
+ providesResourceType: true
73
+ };
4
74
  this.options = options;
5
75
  }
6
- async run(url) {
7
- const { title, ...lighterceptorOptions } = this.options;
8
- const result = await new Lighterceptor(url, {
76
+ async start(target, handlers) {
77
+ if (target.kind !== "url") {
78
+ throw new Error("LighterceptorAdapter only supports target.kind === 'url'.");
79
+ }
80
+ const lighterceptor = new Lighterceptor(target.url, {
9
81
  recursion: true,
10
- ...lighterceptorOptions
11
- }).run();
12
- const capturedNetworkRecords = (result.networkRecords ?? []);
82
+ ...this.options
83
+ });
84
+ const result = await lighterceptor.run();
85
+ const networkRecords = result.networkRecords ?? [];
86
+ let sequence = 0;
87
+ for (const record of networkRecords) {
88
+ const requestId = `${record.url}:${record.timestamp}:${sequence++}`;
89
+ const requestEvent = toRequestEvent({
90
+ url: record.url,
91
+ method: record.method || "GET",
92
+ source: record.source,
93
+ timestamp: record.timestamp,
94
+ responseHeaders: record.response?.headers,
95
+ requestId
96
+ });
97
+ handlers.onEvent(requestEvent);
98
+ if (record.response) {
99
+ const headers = record.response.headers || {};
100
+ const resourceType = inferResourceType(record.source, headers);
101
+ const responseEvent = {
102
+ type: "response",
103
+ requestId: requestEvent.requestId,
104
+ url: record.url,
105
+ status: record.response.status,
106
+ statusText: record.response.statusText,
107
+ headers,
108
+ mimeType: getHeaderValue(headers, "content-type"),
109
+ timestamp: record.timestamp,
110
+ body: {
111
+ kind: "buffer",
112
+ data: record.response.bodyEncoding === "base64"
113
+ ? decodeBase64(record.response.body)
114
+ : encodeUtf8(record.response.body)
115
+ }
116
+ };
117
+ handlers.onEvent(responseEvent);
118
+ continue;
119
+ }
120
+ if (record.error) {
121
+ const failedEvent = {
122
+ type: "failed",
123
+ requestId: requestEvent.requestId,
124
+ url: record.url,
125
+ errorText: record.error,
126
+ timestamp: record.timestamp
127
+ };
128
+ handlers.onEvent(failedEvent);
129
+ }
130
+ }
13
131
  return {
14
- url,
15
- title: title ?? result.title ?? "snapshot",
16
- capturedAt: result.capturedAt ?? new Date().toISOString(),
17
- fetchXhrRecords: [],
18
- networkRecords: capturedNetworkRecords,
19
- resources: []
132
+ async stop() { }
20
133
  };
21
134
  }
22
135
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAA6B,MAAM,2BAA2B,CAAC;AAMrF,MAAM,OAAO,oBAAoB;IAG/B,YAAY,UAAuC,EAAE;QACnD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW;QACnB,MAAM,EAAE,KAAK,EAAE,GAAG,oBAAoB,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QACxD,MAAM,MAAM,GAAG,MAAM,IAAI,aAAa,CAAC,GAAG,EAAE;YAC1C,SAAS,EAAE,IAAI;YACf,GAAG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EAAE;wBACJ,IAAI,EAAE,QAAQ;wBACd,IAAI,EACF,MAAM,CAAC,QAAQ,CAAC,YAAY,KAAK,QAAQ;4BACvC,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;4BACpC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;qBACvC;iBACF,CAAC;gBACF,QAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;gBAChC,SAAS;YACX,CAAC;YAED,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjB,MAAM,WAAW,GAA8B;oBAC7C,IAAI,EAAE,QAAQ;oBACd,SAAS,EAAE,YAAY,CAAC,SAAS;oBACjC,GAAG,EAAE,MAAM,CAAC,GAAG;oBACf,SAAS,EAAE,MAAM,CAAC,KAAK;oBACvB,SAAS,EAAE,MAAM,CAAC,SAAS;iBAC5B,CAAC;gBACF,QAAQ,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;QAED,OAAO;YACL,KAAK,CAAC,IAAI,KAAI,CAAC;SAChB,CAAC;IACJ,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pagepocket/lighterceptor-adapter",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "description": "Lighterceptor adapter for PagePocket network interception.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -12,8 +12,8 @@
12
12
  "dist"
13
13
  ],
14
14
  "dependencies": {
15
- "@pagepocket/lib": "0.4.2",
16
- "@pagepocket/lighterceptor": "0.4.2"
15
+ "@pagepocket/lib": "0.5.0",
16
+ "@pagepocket/lighterceptor": "0.5.0"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/node": "^20.17.12",