@pagepocket/lib 0.5.1 → 0.6.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/README.md CHANGED
@@ -17,7 +17,7 @@ import { PagePocket } from "@pagepocket/lib";
17
17
  import { CdpAdapter } from "@pagepocket/cdp-adapter";
18
18
 
19
19
  const interceptor = new CdpAdapter();
20
- const snapshot = await PagePocket.fromURL("https://example.com").capture({
20
+ const snapshot = await PagePocket.fromTarget({ kind: "cdp-tab", tabId: 123 }).capture({
21
21
  interceptor
22
22
  });
23
23
 
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PagePocket = void 0;
4
- const content_store_1 = require("./content-store");
5
4
  const completion_1 = require("./completion");
5
+ const content_store_1 = require("./content-store");
6
+ const network_store_1 = require("./network-store");
6
7
  const path_resolver_1 = require("./path-resolver");
7
8
  const resource_filter_1 = require("./resource-filter");
8
9
  const snapshot_builder_1 = require("./snapshot-builder");
9
- const network_store_1 = require("./network-store");
10
10
  class PagePocket {
11
11
  constructor(target, options) {
12
12
  this.target = target;
@@ -29,9 +29,7 @@ class PagePocket {
29
29
  const rewriteCSS = options?.rewriteCSS ?? true;
30
30
  const limits = options?.limits;
31
31
  const completionStrategies = (0, completion_1.normalizeCompletion)(options?.completion);
32
- const completion = completionStrategies.length > 0
33
- ? completionStrategies
34
- : [(0, completion_1.networkIdle)(1000), (0, completion_1.timeout)(5000)];
32
+ const completion = completionStrategies.length > 0 ? completionStrategies : [(0, completion_1.networkIdle)(1000), (0, completion_1.timeout)(5000)];
35
33
  const store = new network_store_1.NetworkStore({
36
34
  contentStore,
37
35
  filter,
@@ -43,9 +43,7 @@ const createDefaultPathResolver = () => {
43
43
  const pathname = normalizePathname(parsed?.pathname || "/");
44
44
  const queryHash = `${parsed?.search || ""}${parsed?.hash || ""}`;
45
45
  const suffix = queryHash ? `__ppq_${(0, utils_1.hashString)(queryHash)}` : "";
46
- const basePath = input.isCrossOrigin
47
- ? `/external_resources${pathname}`
48
- : `${pathname}`;
46
+ const basePath = input.isCrossOrigin ? `/external_resources${pathname}` : `${pathname}`;
49
47
  let resolvedPath = suffix ? withSuffix(basePath, suffix) : basePath;
50
48
  const collisionKey = resolvedPath;
51
49
  const existingUrl = usedPaths.get(collisionKey);
@@ -1,14 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createDefaultResourceFilter = void 0;
4
- const DEFAULT_ALLOWED = new Set([
5
- "document",
6
- "stylesheet",
7
- "script",
8
- "image",
9
- "font",
10
- "media"
11
- ]);
4
+ const DEFAULT_ALLOWED = new Set(["document", "stylesheet", "script", "image", "font", "media"]);
12
5
  const isSkippableUrl = (url) => url.startsWith("data:") ||
13
6
  url.startsWith("blob:") ||
14
7
  url.startsWith("mailto:") ||
@@ -1,5 +1,5 @@
1
- import type { ContentStore, PageSnapshot, PathResolver } from "./types";
2
1
  import type { ApiEntry, StoredResource } from "./network-store";
2
+ import type { ContentStore, PageSnapshot, PathResolver } from "./types";
3
3
  type BuildOptions = {
4
4
  entryUrl: string;
5
5
  createdAt: number;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildSnapshot = void 0;
4
- const path_resolver_1 = require("./path-resolver");
5
- const snapshot_1 = require("./snapshot");
6
4
  const css_rewrite_1 = require("./css-rewrite");
5
+ const path_resolver_1 = require("./path-resolver");
7
6
  const rewrite_links_1 = require("./rewrite-links");
7
+ const snapshot_1 = require("./snapshot");
8
8
  const utils_1 = require("./utils");
9
9
  const streamToUint8Array = async (stream) => {
10
10
  const reader = stream.getReader();
@@ -80,7 +80,7 @@ const groupResources = (input) => {
80
80
  });
81
81
  }
82
82
  const primaryGroup = primaryDoc
83
- ? groups.get(primaryDoc.request.frameId ?? primaryDoc.request.requestId) ?? null
83
+ ? (groups.get(primaryDoc.request.frameId ?? primaryDoc.request.requestId) ?? null)
84
84
  : null;
85
85
  const groupByUrl = new Map();
86
86
  for (const group of groups.values()) {
@@ -92,7 +92,9 @@ const groupResources = (input) => {
92
92
  }
93
93
  const frameId = resource.request.frameId;
94
94
  const byFrame = frameId ? groups.get(frameId) : undefined;
95
- const byInitiator = resource.request.initiator?.url ? groupByUrl.get(resource.request.initiator.url) : undefined;
95
+ const byInitiator = resource.request.initiator?.url
96
+ ? groupByUrl.get(resource.request.initiator.url)
97
+ : undefined;
96
98
  const target = byFrame ?? byInitiator ?? primaryGroup ?? Array.from(groups.values())[0];
97
99
  if (target) {
98
100
  target.resources.push(resource);
@@ -101,7 +103,9 @@ const groupResources = (input) => {
101
103
  for (const entry of input.apiEntries) {
102
104
  const frameId = entry.request.frameId;
103
105
  const byFrame = frameId ? groups.get(frameId) : undefined;
104
- const byInitiator = entry.request.initiator?.url ? groupByUrl.get(entry.request.initiator.url) : undefined;
106
+ const byInitiator = entry.request.initiator?.url
107
+ ? groupByUrl.get(entry.request.initiator.url)
108
+ : undefined;
105
109
  const target = byFrame ?? byInitiator ?? primaryGroup ?? Array.from(groups.values())[0];
106
110
  if (target) {
107
111
  target.apiEntries.push(entry);
package/dist/types.d.ts CHANGED
@@ -64,9 +64,6 @@ export type InterceptTarget = {
64
64
  } | {
65
65
  kind: "cdp-tab";
66
66
  tabId: number;
67
- } | {
68
- kind: "cdp-session";
69
- session: unknown;
70
67
  };
71
68
  export type InterceptOptions = Record<string, unknown>;
72
69
  export type NavigateOptions = Record<string, unknown>;
package/dist/utils.js CHANGED
@@ -17,7 +17,7 @@ const hashString = (value) => {
17
17
  exports.hashString = hashString;
18
18
  const stripLeadingSlash = (value) => value.replace(/^\/+/, "");
19
19
  exports.stripLeadingSlash = stripLeadingSlash;
20
- const ensureLeadingSlash = (value) => value.startsWith("/") ? value : `/${value}`;
20
+ const ensureLeadingSlash = (value) => (value.startsWith("/") ? value : `/${value}`);
21
21
  exports.ensureLeadingSlash = ensureLeadingSlash;
22
22
  const sanitizePosixPath = (value) => {
23
23
  const parts = value.split("/").filter(Boolean);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pagepocket/lib",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Library for rewriting HTML snapshots and inlining local resources.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -12,18 +12,22 @@
12
12
  "license": "ISC",
13
13
  "dependencies": {
14
14
  "cheerio": "^1.0.0-rc.12",
15
- "@pagepocket/uni-fs": "0.5.1"
15
+ "@pagepocket/uni-fs": "0.6.0"
16
16
  },
17
17
  "devDependencies": {
18
+ "@playwright/test": "^1.50.1",
18
19
  "@types/node": "^20.11.30",
20
+ "esbuild": "^0.25.1",
19
21
  "prettier": "^3.7.4",
20
22
  "tsx": "^4.19.3",
21
- "typescript": "^5.4.5"
23
+ "typescript": "^5.4.5",
24
+ "@pagepocket/cdp-adapter": "0.6.0"
22
25
  },
23
26
  "scripts": {
24
27
  "build": "tsc",
25
28
  "format": "prettier --write .",
26
29
  "format:check": "prettier --check .",
27
- "test": "tsx --test specs/**/*.test.ts"
30
+ "test": "tsx --test specs/**/*.test.ts",
31
+ "test:e2e": "playwright test -c e2e/playwright.config.ts"
28
32
  }
29
33
  }