arn-browser 0.1.35 → 0.1.36

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arn-browser",
3
- "version": "0.1.35",
3
+ "version": "0.1.36",
4
4
  "description": "A lightweight, browser autmation helper.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -4,38 +4,6 @@ import { Browser, BrowserContext, Page } from "playwright-core";
4
4
  // ROUTING & CACHE TYPES
5
5
  // ============================================================================
6
6
 
7
- /**
8
- * Supported browser resource types for interception.
9
- *
10
- * - `"document"` — HTML pages
11
- * - `"stylesheet"` — CSS files
12
- * - `"image"` — Images (png, jpg, svg, etc.)
13
- * - `"media"` — Audio/Video
14
- * - `"font"` — Web fonts (woff, woff2, ttf, etc.)
15
- * - `"script"` — JavaScript files
16
- * - `"xhr"` — XMLHttpRequest calls
17
- * - `"fetch"` — Fetch API calls
18
- */
19
- export type ResourceType = "document" | "stylesheet" | "image" | "media" | "font" | "script" | "xhr" | "fetch";
20
-
21
- /** Control object returned by pwRoute for runtime resource management */
22
- export interface RouteControl {
23
- /**
24
- * Add a resource type to intercept via Superagent at runtime.
25
- *
26
- * Supported: `"document"` | `"stylesheet"` | `"image"` | `"media"` | `"font"` | `"script"` | `"xhr"` | `"fetch"`
27
- */
28
- addResource(type: ResourceType): void;
29
- /**
30
- * Remove a resource type from Superagent interception at runtime.
31
- *
32
- * Supported: `"document"` | `"stylesheet"` | `"image"` | `"media"` | `"font"` | `"script"` | `"xhr"` | `"fetch"`
33
- */
34
- removeResource(type: ResourceType): void;
35
- /** Get currently intercepted resource types */
36
- getResources(): ResourceType[];
37
- }
38
-
39
7
  /**
40
8
  * Options for the pwRoute function.
41
9
  */
@@ -94,47 +62,12 @@ export interface PwRouteOptions {
94
62
  * Playwright network stack will be used instead.
95
63
  */
96
64
  skipGotPatterns?: string[];
97
-
98
- /**
99
- * Additional resource types to intercept via Superagent.
100
- * These are added on top of the defaults: `["stylesheet", "script", "font"]`
101
- *
102
- * - `"document"` — HTML pages
103
- * - `"stylesheet"` — CSS files
104
- * - `"image"` — Images (png, jpg, svg, etc.)
105
- * - `"media"` — Audio/Video
106
- * - `"font"` — Web fonts (woff, woff2, ttf, etc.)
107
- * - `"script"` — JavaScript files
108
- * - `"xhr"` — XMLHttpRequest calls
109
- * - `"fetch"` — Fetch API calls
110
- *
111
- * @example `gotAddResources: ["document", "xhr"]`
112
- */
113
- gotAddResources?: ResourceType[];
114
-
115
- /**
116
- * Resource types to exclude from Superagent interception.
117
- * These are removed from the defaults: `["stylesheet", "script", "font"]`
118
- *
119
- * - `"document"` — HTML pages
120
- * - `"stylesheet"` — CSS files
121
- * - `"image"` — Images (png, jpg, svg, etc.)
122
- * - `"media"` — Audio/Video
123
- * - `"font"` — Web fonts (woff, woff2, ttf, etc.)
124
- * - `"script"` — JavaScript files
125
- * - `"xhr"` — XMLHttpRequest calls
126
- * - `"fetch"` — Fetch API calls
127
- *
128
- * @example `gotRemoveResources: ["font"]`
129
- */
130
- gotRemoveResources?: ResourceType[];
131
65
  }
132
66
 
133
67
  /**
134
68
  * Sets up request interception, caching, and ad-blocking on a Playwright page or context.
135
- * Returns a control object for runtime resource management.
136
69
  */
137
- export function pwRoute(options: PwRouteOptions): Promise<RouteControl>;
70
+ export function pwRoute(options: PwRouteOptions): Promise<void>;
138
71
 
139
72
  /**
140
73
  * Starts logging cache statistics to the console at a set interval.
@@ -76,7 +76,7 @@ function createProxyAgent(proxyUrl) {
76
76
  * Only keeps essential headers needed for the server to respond correctly.
77
77
  * Everything else (cookies, auth, fingerprint hints, HTTP/2 pseudo-headers, etc.) is stripped.
78
78
  */
79
- function sanitizeRequestHeaders(headers, logger, url, resourceType) {
79
+ function sanitizeRequestHeaders(headers, logger, url) {
80
80
  // Allowlist of essential request headers to keep
81
81
  const allowedHeaders = new Set([
82
82
  "accept",
@@ -93,16 +93,6 @@ function sanitizeRequestHeaders(headers, logger, url, resourceType) {
93
93
  "sec-fetch-site",
94
94
  ]);
95
95
 
96
- // For document/xhr/fetch types, preserve session-critical headers
97
- const sessionTypes = new Set(["document", "xhr", "fetch"]);
98
- if (sessionTypes.has(resourceType)) {
99
- allowedHeaders.add("cookie");
100
- allowedHeaders.add("authorization");
101
- allowedHeaders.add("origin");
102
- allowedHeaders.add("content-type");
103
- allowedHeaders.add("x-requested-with");
104
- }
105
-
106
96
  const cleaned = {};
107
97
  const stripped = [];
108
98
 
@@ -170,7 +160,7 @@ function sanitizeResponseHeaders(headers, logger, url) {
170
160
  * @param {boolean} stripLogger - Whether to log stripped headers
171
161
  * @returns {Promise<Object>} - The response object containing status, headers, and body
172
162
  */
173
- async function fetchWithClient(useCache, url, requestHeaders, method, useFullUrl, logger, proxyAgent, stripHeaders, stripLogger, resourceType) {
163
+ async function fetchWithClient(useCache, url, requestHeaders, method, useFullUrl, logger, proxyAgent, stripHeaders, stripLogger) {
174
164
  // Determine the cache key based on configuration
175
165
  let mainUrl = new URL(url).origin + new URL(url).pathname;
176
166
  if (useFullUrl) {
@@ -190,7 +180,7 @@ async function fetchWithClient(useCache, url, requestHeaders, method, useFullUrl
190
180
 
191
181
  try {
192
182
  // Sanitize outgoing request headers if stripHeaders is enabled
193
- const finalHeaders = stripHeaders ? sanitizeRequestHeaders(requestHeaders, stripLogger, url, resourceType) : requestHeaders;
183
+ const finalHeaders = stripHeaders ? sanitizeRequestHeaders(requestHeaders, stripLogger, url) : requestHeaders;
194
184
 
195
185
  // Fetch the resource using superagent
196
186
  // buffer(true) ensures we get the raw binary data (essential for images/fonts)
@@ -248,8 +238,6 @@ async function fetchWithClient(useCache, url, requestHeaders, method, useFullUrl
248
238
  * @param {Object} options.m4w_send_on_message - Custom handler data for Doublelist messages
249
239
  * @param {Array<string>} options.allowImagePatterns - Array of strings/patterns. If a URL contains any of these, it will NOT be blocked even if blockImage is true.
250
240
  * @param {Array<string>} options.skipGotPatterns - Array of strings/patterns. If a URL contains any of these, it will skip the custom Superagent fetch.
251
- * @param {Array<string>} options.gotAddResources - Additional resource types to intercept via Superagent, e.g. ["document", "xhr", "fetch"]
252
- * @param {Array<string>} options.gotRemoveResources - Resource types to exclude from Superagent interception, e.g. ["font"]
253
241
  */
254
242
  export async function pwRoute({
255
243
  context = null,
@@ -267,8 +255,6 @@ export async function pwRoute({
267
255
  m4w_send_on_message = null,
268
256
  allowImagePatterns = [], // Default empty, merged inside
269
257
  skipGotPatterns = [], // Default empty, merged inside
270
- gotAddResources = [], // Default empty, add to intercepted types
271
- gotRemoveResources = [], // Default empty, remove from intercepted types
272
258
  }) {
273
259
  // Validation: Ensure we have a target to attach the route to
274
260
  if (!context && !page) {
@@ -303,25 +289,16 @@ export async function pwRoute({
303
289
  }
304
290
  }
305
291
 
306
- // Supported resource types reference (for documentation)
307
- // "document", "stylesheet", "image", "media", "font", "script",
308
- // "texttrack", "xhr", "fetch", "eventsource", "websocket", "manifest", "other"
309
-
310
292
  // Define resource types to intercept for custom fetching (useGot)
311
- // Start with defaults, add user extras, then remove user exclusions
312
- const defaultResourceTypes = ["stylesheet", "script", "font"];
313
- const interceptedResourceTypes = new Set([...defaultResourceTypes, ...gotAddResources]);
314
- for (const type of gotRemoveResources) {
315
- interceptedResourceTypes.delete(type);
316
- }
293
+ const interceptedResourceTypes = ["stylesheet", "script", "font"];
317
294
 
318
295
  // Create proxy agent once (reused for all requests in this route)
319
296
  const proxyUrl = formatProxyUrl(proxy);
320
297
  const proxyAgent = createProxyAgent(proxyUrl);
321
298
 
322
299
  // If images are NOT blocked, we generally want to intercept/cache them too.
323
- if (!blockImage && !gotRemoveResources.includes("image")) {
324
- interceptedResourceTypes.add("image");
300
+ if (!blockImage) {
301
+ interceptedResourceTypes.push("image");
325
302
  }
326
303
 
327
304
  // Set up the global route interception
@@ -447,7 +424,7 @@ export async function pwRoute({
447
424
  // ============================================================
448
425
  // Group 6: Resource Interception (Custom Fetch/Cache)
449
426
  // ============================================================
450
- if (useGot && interceptedResourceTypes.has(resourceType) && !url.startsWith("data:")) {
427
+ if (useGot && interceptedResourceTypes.includes(resourceType) && !url.startsWith("data:")) {
451
428
  // Check against the normalized host list (defaults + user input)
452
429
  let shouldSkipGot = false;
453
430
  try {
@@ -467,8 +444,7 @@ export async function pwRoute({
467
444
  logger,
468
445
  proxyAgent,
469
446
  stripGotHeaders,
470
- stripGotLogger,
471
- resourceType
447
+ stripGotLogger
472
448
  );
473
449
 
474
450
  if (response) {
@@ -492,20 +468,4 @@ export async function pwRoute({
492
468
  await route.continue();
493
469
  return;
494
470
  });
495
-
496
- // Return control object for runtime resource management
497
- return {
498
- /** Add a resource type to intercept via Superagent at runtime */
499
- addResource(type) {
500
- interceptedResourceTypes.add(type);
501
- },
502
- /** Remove a resource type from Superagent interception at runtime */
503
- removeResource(type) {
504
- interceptedResourceTypes.delete(type);
505
- },
506
- /** Get currently intercepted resource types */
507
- getResources() {
508
- return [...interceptedResourceTypes];
509
- },
510
- };
511
471
  }
@@ -4,38 +4,6 @@ import { Browser, Page } from "puppeteer-core";
4
4
  // ROUTING & CACHE TYPES
5
5
  // ============================================================================
6
6
 
7
- /**
8
- * Supported browser resource types for interception.
9
- *
10
- * - `"document"` — HTML pages
11
- * - `"stylesheet"` — CSS files
12
- * - `"image"` — Images (png, jpg, svg, etc.)
13
- * - `"media"` — Audio/Video
14
- * - `"font"` — Web fonts (woff, woff2, ttf, etc.)
15
- * - `"script"` — JavaScript files
16
- * - `"xhr"` — XMLHttpRequest calls
17
- * - `"fetch"` — Fetch API calls
18
- */
19
- export type ResourceType = "document" | "stylesheet" | "image" | "media" | "font" | "script" | "xhr" | "fetch";
20
-
21
- /** Control object returned by ppRoute for runtime resource management */
22
- export interface RouteControl {
23
- /**
24
- * Add a resource type to intercept via Superagent at runtime.
25
- *
26
- * Supported: `"document"` | `"stylesheet"` | `"image"` | `"media"` | `"font"` | `"script"` | `"xhr"` | `"fetch"`
27
- */
28
- addResource(type: ResourceType): void;
29
- /**
30
- * Remove a resource type from Superagent interception at runtime.
31
- *
32
- * Supported: `"document"` | `"stylesheet"` | `"image"` | `"media"` | `"font"` | `"script"` | `"xhr"` | `"fetch"`
33
- */
34
- removeResource(type: ResourceType): void;
35
- /** Get currently intercepted resource types */
36
- getResources(): ResourceType[];
37
- }
38
-
39
7
  /**
40
8
  * Options for the ppRoute function.
41
9
  */
@@ -91,47 +59,12 @@ export interface PpRouteOptions {
91
59
  * Playwright network stack will be used instead.
92
60
  */
93
61
  skipGotPatterns?: string[];
94
-
95
- /**
96
- * Additional resource types to intercept via Superagent.
97
- * These are added on top of the defaults: `["stylesheet", "script", "font"]`
98
- *
99
- * - `"document"` — HTML pages
100
- * - `"stylesheet"` — CSS files
101
- * - `"image"` — Images (png, jpg, svg, etc.)
102
- * - `"media"` — Audio/Video
103
- * - `"font"` — Web fonts (woff, woff2, ttf, etc.)
104
- * - `"script"` — JavaScript files
105
- * - `"xhr"` — XMLHttpRequest calls
106
- * - `"fetch"` — Fetch API calls
107
- *
108
- * @example `gotAddResources: ["document", "xhr"]`
109
- */
110
- gotAddResources?: ResourceType[];
111
-
112
- /**
113
- * Resource types to exclude from Superagent interception.
114
- * These are removed from the defaults: `["stylesheet", "script", "font"]`
115
- *
116
- * - `"document"` — HTML pages
117
- * - `"stylesheet"` — CSS files
118
- * - `"image"` — Images (png, jpg, svg, etc.)
119
- * - `"media"` — Audio/Video
120
- * - `"font"` — Web fonts (woff, woff2, ttf, etc.)
121
- * - `"script"` — JavaScript files
122
- * - `"xhr"` — XMLHttpRequest calls
123
- * - `"fetch"` — Fetch API calls
124
- *
125
- * @example `gotRemoveResources: ["font"]`
126
- */
127
- gotRemoveResources?: ResourceType[];
128
62
  }
129
63
 
130
64
  /**
131
65
  * Sets up request interception, caching, and ad-blocking on a Puppeteer page.
132
- * Returns a control object for runtime resource management.
133
66
  */
134
- export function ppRoute(options: PpRouteOptions): Promise<RouteControl>;
67
+ export function ppRoute(options: PpRouteOptions): Promise<void>;
135
68
 
136
69
  /**
137
70
  * Starts logging cache statistics to the console at a set interval.
@@ -76,7 +76,7 @@ function createProxyAgent(proxyUrl) {
76
76
  * Only keeps essential headers needed for the server to respond correctly.
77
77
  * Everything else (cookies, auth, fingerprint hints, HTTP/2 pseudo-headers, etc.) is stripped.
78
78
  */
79
- function sanitizeRequestHeaders(headers, logger, url, resourceType) {
79
+ function sanitizeRequestHeaders(headers, logger, url) {
80
80
  // Allowlist of essential request headers to keep
81
81
  const allowedHeaders = new Set([
82
82
  "accept",
@@ -93,16 +93,6 @@ function sanitizeRequestHeaders(headers, logger, url, resourceType) {
93
93
  "sec-fetch-site",
94
94
  ]);
95
95
 
96
- // For document/xhr/fetch types, preserve session-critical headers
97
- const sessionTypes = new Set(["document", "xhr", "fetch"]);
98
- if (sessionTypes.has(resourceType)) {
99
- allowedHeaders.add("cookie");
100
- allowedHeaders.add("authorization");
101
- allowedHeaders.add("origin");
102
- allowedHeaders.add("content-type");
103
- allowedHeaders.add("x-requested-with");
104
- }
105
-
106
96
  const cleaned = {};
107
97
  const stripped = [];
108
98
 
@@ -173,7 +163,7 @@ function sanitizeResponseHeaders(headers, logger, url) {
173
163
  * @param {boolean} stripLogger - Whether to log stripped headers
174
164
  * @returns {Promise<Object>} - The response object containing status, headers, and body
175
165
  */
176
- async function fetchWithClient(useCache, url, requestHeaders, method, useFullUrl, logger, proxyAgent, stripHeaders, stripLogger, resourceType) {
166
+ async function fetchWithClient(useCache, url, requestHeaders, method, useFullUrl, logger, proxyAgent, stripHeaders, stripLogger) {
177
167
  // Determine the cache key based on configuration
178
168
  let mainUrl = new URL(url).origin + new URL(url).pathname;
179
169
  if (useFullUrl) {
@@ -193,7 +183,7 @@ async function fetchWithClient(useCache, url, requestHeaders, method, useFullUrl
193
183
 
194
184
  try {
195
185
  // Sanitize outgoing request headers if stripHeaders is enabled
196
- const finalHeaders = stripHeaders ? sanitizeRequestHeaders(requestHeaders, stripLogger, url, resourceType) : requestHeaders;
186
+ const finalHeaders = stripHeaders ? sanitizeRequestHeaders(requestHeaders, stripLogger, url) : requestHeaders;
197
187
 
198
188
  // Fetch the resource using superagent
199
189
  // buffer(true) ensures we get the raw binary data (essential for images/fonts)
@@ -250,8 +240,6 @@ async function fetchWithClient(useCache, url, requestHeaders, method, useFullUrl
250
240
  * @param {Object} options.m4w_send_on_message - Custom handler data for Doublelist messages
251
241
  * @param {Array<string>} options.allowImagePatterns - Array of strings/patterns. If a URL contains any of these, it will NOT be blocked even if blockImage is true.
252
242
  * @param {Array<string>} options.skipGotPatterns - Array of strings/patterns. If a URL contains any of these, it will skip the custom Superagent fetch.
253
- * @param {Array<string>} options.gotAddResources - Additional resource types to intercept via Superagent, e.g. ["document", "xhr", "fetch"]
254
- * @param {Array<string>} options.gotRemoveResources - Resource types to exclude from Superagent interception, e.g. ["font"]
255
243
  */
256
244
  export async function ppRoute({
257
245
  page = null,
@@ -268,8 +256,6 @@ export async function ppRoute({
268
256
  m4w_send_on_message = null,
269
257
  allowImagePatterns = [], // Default empty, merged inside
270
258
  skipGotPatterns = [], // Default empty, merged inside
271
- gotAddResources = [], // Default empty, add to intercepted types
272
- gotRemoveResources = [], // Default empty, remove from intercepted types
273
259
  }) {
274
260
  // Validation: Ensure we have a page
275
261
  if (!page) {
@@ -303,25 +289,16 @@ export async function ppRoute({
303
289
  }
304
290
  }
305
291
 
306
- // Supported resource types reference (for documentation)
307
- // "document", "stylesheet", "image", "media", "font", "script",
308
- // "texttrack", "xhr", "fetch", "eventsource", "websocket", "manifest", "other"
309
-
310
292
  // Define resource types to intercept for custom fetching (useGot)
311
- // Start with defaults, add user extras, then remove user exclusions
312
- const defaultResourceTypes = ["stylesheet", "script", "font"];
313
- const interceptedResourceTypes = new Set([...defaultResourceTypes, ...gotAddResources]);
314
- for (const type of gotRemoveResources) {
315
- interceptedResourceTypes.delete(type);
316
- }
293
+ const interceptedResourceTypes = ["stylesheet", "script", "font"];
317
294
 
318
295
  // Create proxy agent once (reused for all requests in this route)
319
296
  const proxyUrl = formatProxyUrl(proxy);
320
297
  const proxyAgent = createProxyAgent(proxyUrl);
321
298
 
322
299
  // If images are NOT blocked, we generally want to intercept/cache them too.
323
- if (!blockImage && !gotRemoveResources.includes("image")) {
324
- interceptedResourceTypes.add("image");
300
+ if (!blockImage) {
301
+ interceptedResourceTypes.push("image");
325
302
  }
326
303
 
327
304
  // Enable request interception in Puppeteer
@@ -453,7 +430,7 @@ export async function ppRoute({
453
430
  // ============================================================
454
431
  // Group 6: Resource Interception (Custom Fetch/Cache)
455
432
  // ============================================================
456
- if (useGot && interceptedResourceTypes.has(resourceType) && !url.startsWith("data:")) {
433
+ if (useGot && interceptedResourceTypes.includes(resourceType) && !url.startsWith("data:")) {
457
434
  // Check against the normalized host list (defaults + user input)
458
435
  let shouldSkipGot = false;
459
436
  try {
@@ -473,8 +450,7 @@ export async function ppRoute({
473
450
  logger,
474
451
  proxyAgent,
475
452
  stripGotHeaders,
476
- stripGotLogger,
477
- resourceType
453
+ stripGotLogger
478
454
  );
479
455
 
480
456
  if (response) {
@@ -498,20 +474,4 @@ export async function ppRoute({
498
474
  await request.continue();
499
475
  return;
500
476
  });
501
-
502
- // Return control object for runtime resource management
503
- return {
504
- /** Add a resource type to intercept via Superagent at runtime */
505
- addResource(type) {
506
- interceptedResourceTypes.add(type);
507
- },
508
- /** Remove a resource type from Superagent interception at runtime */
509
- removeResource(type) {
510
- interceptedResourceTypes.delete(type);
511
- },
512
- /** Get currently intercepted resource types */
513
- getResources() {
514
- return [...interceptedResourceTypes];
515
- },
516
- };
517
477
  }