@pagepocket/lib 0.7.1 → 0.8.1

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.
Files changed (261) hide show
  1. package/README.md +7 -6
  2. package/dist/build-snapshot-from-bundle.d.ts +23 -0
  3. package/dist/build-snapshot-from-bundle.js +68 -0
  4. package/dist/builtin-blacklist.js +3 -6
  5. package/dist/bundle/from-network-store.d.ts +10 -0
  6. package/dist/bundle/from-network-store.js +26 -0
  7. package/dist/bundle/types.d.ts +32 -0
  8. package/dist/bundle/types.js +2 -0
  9. package/dist/capture/index.d.ts +14 -0
  10. package/dist/capture/index.js +86 -0
  11. package/dist/capture/memory-content-store.d.ts +4 -0
  12. package/dist/capture/memory-content-store.js +42 -0
  13. package/dist/capture/types.d.ts +61 -0
  14. package/dist/capture/types.js +2 -0
  15. package/dist/content-store.js +3 -8
  16. package/dist/content-type.d.ts +1 -1
  17. package/dist/content-type.js +2 -28
  18. package/dist/core/_impl/completion.d.ts +4 -0
  19. package/dist/core/_impl/completion.js +29 -0
  20. package/dist/core/_impl/content-store.d.ts +21 -0
  21. package/dist/core/_impl/content-store.js +91 -0
  22. package/dist/core/_impl/debug.d.ts +1 -0
  23. package/dist/core/_impl/debug.js +16 -0
  24. package/dist/core/_impl/inflight-tracker.d.ts +19 -0
  25. package/dist/core/_impl/inflight-tracker.js +48 -0
  26. package/dist/core/_impl/pagepocket.d.ts +27 -0
  27. package/dist/core/_impl/pagepocket.js +155 -0
  28. package/dist/core/capture/_impl/memory-content-store.d.ts +4 -0
  29. package/dist/core/capture/_impl/memory-content-store.js +42 -0
  30. package/dist/core/capture/_impl/types.d.ts +61 -0
  31. package/dist/core/capture/_impl/types.js +2 -0
  32. package/dist/core/capture/internal/memory-content-store.d.ts +4 -0
  33. package/dist/core/capture/internal/memory-content-store.js +42 -0
  34. package/dist/core/capture/internal/types.d.ts +61 -0
  35. package/dist/core/capture/internal/types.js +2 -0
  36. package/dist/core/capture/memory-content-store.d.ts +4 -0
  37. package/dist/core/capture/memory-content-store.js +38 -0
  38. package/dist/core/capture/types.d.ts +61 -0
  39. package/dist/core/capture/types.js +1 -0
  40. package/dist/core/completion.d.ts +4 -0
  41. package/dist/core/completion.js +23 -0
  42. package/dist/core/content-store.d.ts +21 -0
  43. package/dist/core/content-store.js +54 -0
  44. package/dist/core/debug.d.ts +1 -0
  45. package/dist/core/debug.js +12 -0
  46. package/dist/core/file-tree-merge.d.ts +2 -0
  47. package/dist/core/file-tree-merge.js +27 -0
  48. package/dist/core/file-tree.d.ts +36 -0
  49. package/dist/core/file-tree.js +1 -0
  50. package/dist/core/inflight-tracker.d.ts +19 -0
  51. package/dist/core/inflight-tracker.js +44 -0
  52. package/dist/core/internal/completion.d.ts +4 -0
  53. package/dist/core/internal/completion.js +29 -0
  54. package/dist/core/internal/content-store.d.ts +21 -0
  55. package/dist/core/internal/content-store.js +91 -0
  56. package/dist/core/internal/debug.d.ts +1 -0
  57. package/dist/core/internal/debug.js +16 -0
  58. package/dist/core/internal/inflight-tracker.d.ts +19 -0
  59. package/dist/core/internal/inflight-tracker.js +48 -0
  60. package/dist/core/internal/pagepocket.d.ts +27 -0
  61. package/dist/core/internal/pagepocket.js +155 -0
  62. package/dist/core/pagepocket.d.ts +38 -0
  63. package/dist/core/pagepocket.js +57 -0
  64. package/dist/core/plugin/_impl/context.d.ts +47 -0
  65. package/dist/core/plugin/_impl/context.js +142 -0
  66. package/dist/core/plugin/_impl/runner.d.ts +12 -0
  67. package/dist/core/plugin/_impl/runner.js +232 -0
  68. package/dist/core/plugin/_impl/types.d.ts +108 -0
  69. package/dist/core/plugin/_impl/types.js +2 -0
  70. package/dist/core/plugin/context.d.ts +47 -0
  71. package/dist/core/plugin/context.js +205 -0
  72. package/dist/core/plugin/internal/context.d.ts +47 -0
  73. package/dist/core/plugin/internal/context.js +142 -0
  74. package/dist/core/plugin/internal/runner.d.ts +12 -0
  75. package/dist/core/plugin/internal/runner.js +232 -0
  76. package/dist/core/plugin/internal/types.d.ts +108 -0
  77. package/dist/core/plugin/internal/types.js +2 -0
  78. package/dist/core/plugin/runner-utils.d.ts +9 -0
  79. package/dist/core/plugin/runner-utils.js +29 -0
  80. package/dist/core/plugin/runner.d.ts +12 -0
  81. package/dist/core/plugin/runner.js +118 -0
  82. package/dist/core/plugin/types.d.ts +117 -0
  83. package/dist/core/plugin/types.js +1 -0
  84. package/dist/core/runtime/types.d.ts +14 -0
  85. package/dist/core/runtime/types.js +2 -0
  86. package/dist/css-rewrite.js +1 -5
  87. package/dist/debug.d.ts +0 -1
  88. package/dist/debug.js +3 -5
  89. package/dist/files/types.d.ts +41 -0
  90. package/dist/files/types.js +2 -0
  91. package/dist/hack-html.js +20 -13
  92. package/dist/hackers/index.d.ts +1 -1
  93. package/dist/hackers/index.js +24 -27
  94. package/dist/hackers/preload-fetch.d.ts +1 -1
  95. package/dist/hackers/preload-fetch.js +1 -4
  96. package/dist/hackers/preload-xhr.d.ts +1 -1
  97. package/dist/hackers/preload-xhr.js +1 -4
  98. package/dist/hackers/replay-beacon.d.ts +1 -1
  99. package/dist/hackers/replay-beacon.js +1 -4
  100. package/dist/hackers/replay-block-text-fragment.d.ts +1 -1
  101. package/dist/hackers/replay-block-text-fragment.js +1 -4
  102. package/dist/hackers/replay-css-proxy.d.ts +1 -1
  103. package/dist/hackers/replay-css-proxy.js +9 -12
  104. package/dist/hackers/replay-dom-rewrite.d.ts +1 -1
  105. package/dist/hackers/replay-dom-rewrite.js +165 -154
  106. package/dist/hackers/replay-eventsource.d.ts +1 -1
  107. package/dist/hackers/replay-eventsource.js +1 -4
  108. package/dist/hackers/replay-fetch.d.ts +1 -1
  109. package/dist/hackers/replay-fetch.js +1 -4
  110. package/dist/hackers/replay-history-path.d.ts +1 -1
  111. package/dist/hackers/replay-history-path.js +1 -4
  112. package/dist/hackers/replay-svg-image.d.ts +1 -1
  113. package/dist/hackers/replay-svg-image.js +1 -4
  114. package/dist/hackers/replay-websocket.d.ts +1 -1
  115. package/dist/hackers/replay-websocket.js +1 -4
  116. package/dist/hackers/replay-xhr.d.ts +1 -1
  117. package/dist/hackers/replay-xhr.js +1 -4
  118. package/dist/hackers/types.js +1 -2
  119. package/dist/index.d.ts +29 -13
  120. package/dist/index.js +23 -44
  121. package/dist/kind-map.d.ts +68 -0
  122. package/dist/kind-map.js +58 -0
  123. package/dist/network-store.js +12 -1
  124. package/dist/pagepocket.d.ts +19 -4
  125. package/dist/pagepocket.js +36 -102
  126. package/dist/path-resolver.d.ts +1 -2
  127. package/dist/path-resolver.js +9 -16
  128. package/dist/plugin/builtins/build-snapshot-plugin.d.ts +5 -0
  129. package/dist/plugin/builtins/build-snapshot-plugin.js +84 -0
  130. package/dist/plugin/builtins/replace-elements-plugin.d.ts +8 -0
  131. package/dist/plugin/builtins/replace-elements-plugin.js +13 -0
  132. package/dist/plugin/builtins/to-directory-plugin.d.ts +7 -0
  133. package/dist/plugin/builtins/to-directory-plugin.js +20 -0
  134. package/dist/plugin/builtins/to-zip-plugin.d.ts +5 -0
  135. package/dist/plugin/builtins/to-zip-plugin.js +19 -0
  136. package/dist/plugin/context.d.ts +47 -0
  137. package/dist/plugin/context.js +142 -0
  138. package/dist/plugin/runner.d.ts +12 -0
  139. package/dist/plugin/runner.js +232 -0
  140. package/dist/plugin/types.d.ts +108 -0
  141. package/dist/plugin/types.js +2 -0
  142. package/dist/plugins/build-files-from-capture.d.ts +5 -0
  143. package/dist/plugins/build-files-from-capture.js +85 -0
  144. package/dist/plugins/build-warc.d.ts +5 -0
  145. package/dist/plugins/build-warc.js +225 -0
  146. package/dist/plugins/builtins/manifest.d.ts +2 -0
  147. package/dist/plugins/builtins/manifest.js +42 -0
  148. package/dist/plugins/builtins/snapshot-directory.d.ts +2 -0
  149. package/dist/plugins/builtins/snapshot-directory.js +24 -0
  150. package/dist/plugins/builtins/snapshot-zip.d.ts +2 -0
  151. package/dist/plugins/builtins/snapshot-zip.js +25 -0
  152. package/dist/plugins/capture-http-lighterceptor.d.ts +5 -0
  153. package/dist/plugins/capture-http-lighterceptor.js +85 -0
  154. package/dist/plugins/capture-http-puppeteer.d.ts +5 -0
  155. package/dist/plugins/capture-http-puppeteer.js +85 -0
  156. package/dist/plugins/host.d.ts +37 -0
  157. package/dist/plugins/host.js +105 -0
  158. package/dist/plugins/index.d.ts +6 -0
  159. package/dist/plugins/index.js +11 -0
  160. package/dist/plugins/ordering.d.ts +2 -0
  161. package/dist/plugins/ordering.js +19 -0
  162. package/dist/plugins/types.d.ts +51 -0
  163. package/dist/plugins/types.js +2 -0
  164. package/dist/preload.js +3 -7
  165. package/dist/replace-elements/actions.d.ts +5 -0
  166. package/dist/replace-elements/actions.js +86 -0
  167. package/dist/replace-elements/match.d.ts +5 -0
  168. package/dist/replace-elements/match.js +46 -0
  169. package/dist/replace-elements/normalize.d.ts +21 -0
  170. package/dist/replace-elements/normalize.js +50 -0
  171. package/dist/replace-elements.d.ts +1 -1
  172. package/dist/replace-elements.js +5 -185
  173. package/dist/replay/match-api.d.ts +10 -0
  174. package/dist/replay/match-api.js +162 -0
  175. package/dist/replay/templates/match-api-source.d.ts +1 -0
  176. package/dist/replay/templates/match-api-source.js +137 -0
  177. package/dist/replay/templates/replay-script-template.d.ts +5 -0
  178. package/dist/replay/templates/replay-script-template.js +337 -0
  179. package/dist/replay/templates/resource-proxy-script.d.ts +1 -0
  180. package/dist/replay/templates/resource-proxy-script.js +274 -0
  181. package/dist/replay-script.d.ts +3 -10
  182. package/dist/replay-script.js +11 -625
  183. package/dist/resource-filter.d.ts +1 -1
  184. package/dist/resource-filter.js +1 -5
  185. package/dist/resource-proxy/escape-percent.d.ts +1 -0
  186. package/dist/resource-proxy/escape-percent.js +12 -0
  187. package/dist/resource-proxy/multimap.d.ts +3 -0
  188. package/dist/resource-proxy/multimap.js +18 -0
  189. package/dist/resource-proxy/pathname-variants.d.ts +3 -0
  190. package/dist/resource-proxy/pathname-variants.js +54 -0
  191. package/dist/resource-proxy.d.ts +4 -2
  192. package/dist/resource-proxy.js +48 -117
  193. package/dist/resources.js +4 -42
  194. package/dist/rewrite-links/js-imports.d.ts +3 -0
  195. package/dist/rewrite-links/js-imports.js +56 -0
  196. package/dist/rewrite-links/link-rel.d.ts +2 -0
  197. package/dist/rewrite-links/link-rel.js +10 -0
  198. package/dist/rewrite-links/meta-refresh.d.ts +3 -0
  199. package/dist/rewrite-links/meta-refresh.js +22 -0
  200. package/dist/rewrite-links/skip.d.ts +1 -0
  201. package/dist/rewrite-links/skip.js +10 -0
  202. package/dist/rewrite-links/srcset.d.ts +3 -0
  203. package/dist/rewrite-links/srcset.js +63 -0
  204. package/dist/rewrite-links/url-resolve.d.ts +3 -0
  205. package/dist/rewrite-links/url-resolve.js +13 -0
  206. package/dist/rewrite-links.d.ts +3 -3
  207. package/dist/rewrite-links.js +31 -240
  208. package/dist/snapshot-builder/api.d.ts +3 -0
  209. package/dist/snapshot-builder/api.js +6 -0
  210. package/dist/snapshot-builder/build-snapshot.d.ts +3 -0
  211. package/dist/snapshot-builder/build-snapshot.js +138 -0
  212. package/dist/snapshot-builder/capture-index/index-capture.d.ts +13 -0
  213. package/dist/snapshot-builder/capture-index/index-capture.js +168 -0
  214. package/dist/snapshot-builder/capture-index/index.d.ts +2 -0
  215. package/dist/snapshot-builder/capture-index/index.js +1 -0
  216. package/dist/snapshot-builder/capture-index/types.d.ts +12 -0
  217. package/dist/snapshot-builder/capture-index/types.js +1 -0
  218. package/dist/snapshot-builder/capture-index.d.ts +12 -0
  219. package/dist/snapshot-builder/capture-index.js +173 -0
  220. package/dist/snapshot-builder/emit-document.d.ts +24 -0
  221. package/dist/snapshot-builder/emit-document.js +50 -0
  222. package/dist/snapshot-builder/grouping.d.ts +8 -0
  223. package/dist/snapshot-builder/grouping.js +87 -0
  224. package/dist/snapshot-builder/http.d.ts +6 -0
  225. package/dist/snapshot-builder/http.js +28 -0
  226. package/dist/snapshot-builder/index.d.ts +4 -0
  227. package/dist/snapshot-builder/index.js +2 -0
  228. package/dist/snapshot-builder/path-map.d.ts +3 -0
  229. package/dist/snapshot-builder/path-map.js +35 -0
  230. package/dist/snapshot-builder/resources-path.d.ts +23 -0
  231. package/dist/snapshot-builder/resources-path.js +47 -0
  232. package/dist/snapshot-builder/rewrite-resource.d.ts +18 -0
  233. package/dist/snapshot-builder/rewrite-resource.js +52 -0
  234. package/dist/snapshot-builder/types.d.ts +37 -0
  235. package/dist/snapshot-builder/types.js +2 -0
  236. package/dist/snapshot-builder.d.ts +12 -8
  237. package/dist/snapshot-builder.js +252 -27
  238. package/dist/types.d.ts +122 -78
  239. package/dist/types.js +4 -2
  240. package/dist/units/contracts-bridge.d.ts +76 -0
  241. package/dist/units/contracts-bridge.js +6 -0
  242. package/dist/units/index.d.ts +4 -0
  243. package/dist/units/index.js +2 -0
  244. package/dist/units/runner.d.ts +11 -0
  245. package/dist/units/runner.js +270 -0
  246. package/dist/units/types.d.ts +39 -0
  247. package/dist/units/types.js +1 -0
  248. package/dist/utils/streams.d.ts +2 -0
  249. package/dist/utils/streams.js +29 -0
  250. package/dist/utils.d.ts +35 -1
  251. package/dist/utils.js +107 -29
  252. package/dist/v3/contracts-bridge.d.ts +69 -0
  253. package/dist/v3/contracts-bridge.js +5 -0
  254. package/dist/v3/index.d.ts +4 -0
  255. package/dist/v3/index.js +2 -0
  256. package/dist/v3/runner.d.ts +20 -0
  257. package/dist/v3/runner.js +245 -0
  258. package/dist/v3/types.d.ts +39 -0
  259. package/dist/v3/types.js +1 -0
  260. package/dist/writers.js +3 -1
  261. package/package.json +11 -3
@@ -0,0 +1,19 @@
1
+ import type { NetworkEvent } from "@pagepocket/interceptor";
2
+ type InflightStats = {
3
+ inflightRequests: number;
4
+ lastNetworkTs: number;
5
+ totalRequests: number;
6
+ };
7
+ export declare class InflightTracker {
8
+ private inflight;
9
+ private inflightRequests;
10
+ private lastNetworkTs;
11
+ private totalRequests;
12
+ private shouldIgnore;
13
+ constructor(options?: {
14
+ shouldIgnore?: (event: NetworkEvent) => boolean;
15
+ });
16
+ handleEvent(event: NetworkEvent): InflightStats;
17
+ getStats(): InflightStats;
18
+ }
19
+ export {};
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InflightTracker = void 0;
4
+ class InflightTracker {
5
+ constructor(options) {
6
+ this.inflight = new Set();
7
+ this.inflightRequests = 0;
8
+ this.lastNetworkTs = Date.now();
9
+ this.totalRequests = 0;
10
+ this.shouldIgnore = options?.shouldIgnore ?? (() => false);
11
+ }
12
+ handleEvent(event) {
13
+ if (this.shouldIgnore(event)) {
14
+ return this.getStats();
15
+ }
16
+ if (event?.timestamp) {
17
+ this.lastNetworkTs = event.timestamp;
18
+ }
19
+ else {
20
+ this.lastNetworkTs = Date.now();
21
+ }
22
+ if (event?.type === "request") {
23
+ this.totalRequests += 1;
24
+ if (!this.inflight.has(event.requestId)) {
25
+ this.inflight.add(event.requestId);
26
+ this.inflightRequests += 1;
27
+ }
28
+ }
29
+ if (event?.type === "response" || event?.type === "failed") {
30
+ if (this.inflight.delete(event.requestId)) {
31
+ this.inflightRequests = Math.max(0, this.inflightRequests - 1);
32
+ }
33
+ }
34
+ return {
35
+ inflightRequests: this.inflightRequests,
36
+ lastNetworkTs: this.lastNetworkTs,
37
+ totalRequests: this.totalRequests
38
+ };
39
+ }
40
+ getStats() {
41
+ return {
42
+ inflightRequests: this.inflightRequests,
43
+ lastNetworkTs: this.lastNetworkTs,
44
+ totalRequests: this.totalRequests
45
+ };
46
+ }
47
+ }
48
+ exports.InflightTracker = InflightTracker;
@@ -0,0 +1,27 @@
1
+ import type { NetworkEvent } from "@pagepocket/interceptor";
2
+ import type { NetworkEventStream, PagePocketOptions } from "../../types";
3
+ import type { CaptureWithPluginsOptions, PagePocketCaptureResult } from "../plugin/types";
4
+ export declare class PagePocket {
5
+ private target;
6
+ private options;
7
+ private eventStream;
8
+ private constructor();
9
+ static fromURL(url: string, options?: PagePocketOptions): PagePocket;
10
+ static fromPuppeteerPage(page: unknown, options?: PagePocketOptions): PagePocket;
11
+ static fromCDPTab(tabId: number, options?: PagePocketOptions): PagePocket;
12
+ static fromHTMLString(htmlString: string, options: {
13
+ baseUrl: string;
14
+ url?: string;
15
+ } & PagePocketOptions): PagePocket;
16
+ interceptedRequestEvents(): NetworkEventStream;
17
+ /** @internal */
18
+ _emitNetworkEvent(event: NetworkEvent): void;
19
+ /** @internal */
20
+ _closeNetworkEventStream(): void;
21
+ static fromDocument(doc: unknown, options: {
22
+ baseUrl: string;
23
+ url?: string;
24
+ serialize?: (doc: unknown) => string;
25
+ } & PagePocketOptions): PagePocket;
26
+ capture(options: CaptureWithPluginsOptions): Promise<PagePocketCaptureResult>;
27
+ }
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PagePocket = void 0;
4
+ const runner_1 = require("../plugin/runner");
5
+ class AsyncEventQueue {
6
+ constructor() {
7
+ this.values = [];
8
+ this.pending = [];
9
+ this.closed = false;
10
+ }
11
+ push(value) {
12
+ if (this.closed) {
13
+ return;
14
+ }
15
+ if (this.pending.length > 0) {
16
+ const resolve = this.pending.shift();
17
+ resolve?.({ kind: "value", value });
18
+ return;
19
+ }
20
+ this.values.push({ kind: "value", value });
21
+ }
22
+ close() {
23
+ if (this.closed) {
24
+ return;
25
+ }
26
+ this.closed = true;
27
+ if (this.pending.length > 0) {
28
+ for (const resolve of this.pending) {
29
+ resolve({ kind: "close" });
30
+ }
31
+ this.pending = [];
32
+ return;
33
+ }
34
+ this.values.push({ kind: "close" });
35
+ }
36
+ async *iterate() {
37
+ while (true) {
38
+ if (this.values.length > 0) {
39
+ const item = this.values.shift();
40
+ if (!item || item.kind === "close") {
41
+ return;
42
+ }
43
+ yield item.value;
44
+ continue;
45
+ }
46
+ if (this.closed) {
47
+ return;
48
+ }
49
+ const item = await new Promise((resolve) => {
50
+ this.pending.push(resolve);
51
+ });
52
+ if (item.kind === "close") {
53
+ return;
54
+ }
55
+ yield item.value;
56
+ }
57
+ }
58
+ }
59
+ class BroadcastAsyncIterable {
60
+ constructor() {
61
+ this.queues = new Set();
62
+ this.closed = false;
63
+ }
64
+ subscribe() {
65
+ const queue = new AsyncEventQueue();
66
+ if (this.closed) {
67
+ queue.close();
68
+ }
69
+ this.queues.add(queue);
70
+ return (async function* (owner) {
71
+ try {
72
+ for await (const value of queue.iterate()) {
73
+ yield value;
74
+ }
75
+ }
76
+ finally {
77
+ owner.queues.delete(queue);
78
+ queue.close();
79
+ }
80
+ })(this);
81
+ }
82
+ emit(value) {
83
+ if (this.queues.size === 0) {
84
+ return;
85
+ }
86
+ for (const queue of this.queues) {
87
+ queue.push(value);
88
+ }
89
+ }
90
+ close() {
91
+ if (this.closed) {
92
+ return;
93
+ }
94
+ this.closed = true;
95
+ for (const queue of this.queues) {
96
+ queue.close();
97
+ }
98
+ this.queues.clear();
99
+ }
100
+ }
101
+ class PagePocket {
102
+ constructor(target, options) {
103
+ this.eventStream = new BroadcastAsyncIterable();
104
+ this.target = target;
105
+ this.options = options ?? {};
106
+ }
107
+ static fromURL(url, options) {
108
+ return new PagePocket({ kind: "url", url }, options);
109
+ }
110
+ static fromPuppeteerPage(page, options) {
111
+ return new PagePocket({ kind: "puppeteer-page", page }, options);
112
+ }
113
+ static fromCDPTab(tabId, options) {
114
+ return new PagePocket({ kind: "cdp-tab", tabId }, options);
115
+ }
116
+ static fromHTMLString(htmlString, options) {
117
+ const { baseUrl, url, ...rest } = options;
118
+ return new PagePocket({ kind: "html", htmlString, baseUrl, ...(url ? { url } : {}) }, rest);
119
+ }
120
+ interceptedRequestEvents() {
121
+ return this.eventStream.subscribe();
122
+ }
123
+ /** @internal */
124
+ _emitNetworkEvent(event) {
125
+ this.eventStream.emit(event);
126
+ }
127
+ /** @internal */
128
+ _closeNetworkEventStream() {
129
+ this.eventStream.close();
130
+ }
131
+ static fromDocument(doc, options) {
132
+ const serialize = options.serialize ??
133
+ ((value) => {
134
+ if (value && typeof value === "object" && "documentElement" in value) {
135
+ const el = value.documentElement;
136
+ if (el && typeof el.outerHTML === "string") {
137
+ return el.outerHTML;
138
+ }
139
+ }
140
+ throw new Error("fromDocument requires options.serialize(doc) or doc.documentElement.outerHTML");
141
+ });
142
+ const htmlString = serialize(doc);
143
+ const { baseUrl, url, serialize: _serialize, ...rest } = options;
144
+ return new PagePocket({ kind: "html", htmlString, baseUrl, ...(url ? { url } : {}) }, rest);
145
+ }
146
+ async capture(options) {
147
+ return (0, runner_1.captureWithPlugins)({
148
+ pocket: this,
149
+ pocketOptions: this.options,
150
+ target: this.target,
151
+ options
152
+ });
153
+ }
154
+ }
155
+ exports.PagePocket = PagePocket;
@@ -0,0 +1,4 @@
1
+ import type { ContentStore } from "../../../types";
2
+ export declare const createMemoryContentStore: (name?: string) => (ContentStore & {
3
+ _bytes: Map<string, Uint8Array>;
4
+ }) | ContentStore;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createMemoryContentStore = void 0;
4
+ const utils_1 = require("../../../utils");
5
+ const streams_1 = require("../../../utils/streams");
6
+ const createMemoryContentStore = (name = "memory") => {
7
+ const bytesById = new Map();
8
+ let nextId = 1;
9
+ const storeBytes = (bytes) => {
10
+ const id = String(nextId);
11
+ nextId += 1;
12
+ bytesById.set(id, bytes);
13
+ return id;
14
+ };
15
+ const getStoredBytes = (id) => {
16
+ const bytes = bytesById.get(id);
17
+ if (!bytes) {
18
+ throw new Error(`Missing memory content for ref id=${id}`);
19
+ }
20
+ return bytes;
21
+ };
22
+ return {
23
+ name,
24
+ _bytes: bytesById,
25
+ async put(body, _meta) {
26
+ const bytes = await (0, utils_1.toUint8Array)(body);
27
+ const id = storeBytes(bytes);
28
+ return { kind: "store-ref", id };
29
+ },
30
+ async open(ref) {
31
+ if (ref.kind === "memory") {
32
+ return (0, streams_1.streamFromUint8Array)(ref.data);
33
+ }
34
+ const bytes = getStoredBytes(ref.id);
35
+ return (0, streams_1.streamFromUint8Array)(bytes);
36
+ },
37
+ async dispose() {
38
+ bytesById.clear();
39
+ }
40
+ };
41
+ };
42
+ exports.createMemoryContentStore = createMemoryContentStore;
@@ -0,0 +1,61 @@
1
+ import type { ContentRef, ContentStore, ResourceType } from "../../../types";
2
+ export type Header = {
3
+ name: string;
4
+ value: string;
5
+ };
6
+ export type CaptureBodyRef = ContentRef;
7
+ export type CaptureCapabilities = {
8
+ requestHeaders: "sent" | "approx" | "none";
9
+ responseHeaders: "raw" | "approx" | "none";
10
+ requestBodies: boolean;
11
+ responseBodies: "decoded" | "wire" | "none";
12
+ httpVersion: boolean;
13
+ remoteIp: boolean;
14
+ headerOrderPreserved: boolean;
15
+ };
16
+ export type CaptureRequestEvent = {
17
+ type: "http.request";
18
+ requestId: string;
19
+ url: string;
20
+ method: string;
21
+ headers: Header[];
22
+ timestamp: number;
23
+ frameId?: string;
24
+ resourceType?: ResourceType;
25
+ initiator?: {
26
+ type?: string;
27
+ url?: string;
28
+ };
29
+ };
30
+ export type CaptureResponseEvent = {
31
+ type: "http.response";
32
+ requestId: string;
33
+ url: string;
34
+ status: number;
35
+ statusText?: string;
36
+ headers: Header[];
37
+ timestamp: number;
38
+ mimeType?: string;
39
+ fromDiskCache?: boolean;
40
+ fromServiceWorker?: boolean;
41
+ bodyRef?: CaptureBodyRef;
42
+ bodySize?: number;
43
+ };
44
+ export type CaptureFailedEvent = {
45
+ type: "http.failed";
46
+ requestId: string;
47
+ url: string;
48
+ errorText: string;
49
+ timestamp: number;
50
+ };
51
+ export type CaptureEvent = CaptureRequestEvent | CaptureResponseEvent | CaptureFailedEvent;
52
+ export type CaptureArtifacts = {
53
+ /** Ordered list of captured network events. */
54
+ events: CaptureEvent[];
55
+ /** Content store containing captured bodies referenced by events. */
56
+ contentStore: ContentStore;
57
+ capabilities: CaptureCapabilities;
58
+ };
59
+ export type CaptureContext = {
60
+ capture?: CaptureArtifacts;
61
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import type { ContentStore } from "../../../types";
2
+ export declare const createMemoryContentStore: (name?: string) => (ContentStore & {
3
+ _bytes: Map<string, Uint8Array>;
4
+ }) | ContentStore;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createMemoryContentStore = void 0;
4
+ const utils_1 = require("../../../utils");
5
+ const streams_1 = require("../../../utils/streams");
6
+ const createMemoryContentStore = (name = "memory") => {
7
+ const bytesById = new Map();
8
+ let nextId = 1;
9
+ const storeBytes = (bytes) => {
10
+ const id = String(nextId);
11
+ nextId += 1;
12
+ bytesById.set(id, bytes);
13
+ return id;
14
+ };
15
+ const getStoredBytes = (id) => {
16
+ const bytes = bytesById.get(id);
17
+ if (!bytes) {
18
+ throw new Error(`Missing memory content for ref id=${id}`);
19
+ }
20
+ return bytes;
21
+ };
22
+ return {
23
+ name,
24
+ _bytes: bytesById,
25
+ async put(body, _meta) {
26
+ const bytes = await (0, utils_1.toUint8Array)(body);
27
+ const id = storeBytes(bytes);
28
+ return { kind: "store-ref", id };
29
+ },
30
+ async open(ref) {
31
+ if (ref.kind === "memory") {
32
+ return (0, streams_1.streamFromUint8Array)(ref.data);
33
+ }
34
+ const bytes = getStoredBytes(ref.id);
35
+ return (0, streams_1.streamFromUint8Array)(bytes);
36
+ },
37
+ async dispose() {
38
+ bytesById.clear();
39
+ }
40
+ };
41
+ };
42
+ exports.createMemoryContentStore = createMemoryContentStore;
@@ -0,0 +1,61 @@
1
+ import type { ContentRef, ContentStore, ResourceType } from "../../../types";
2
+ export type Header = {
3
+ name: string;
4
+ value: string;
5
+ };
6
+ export type CaptureBodyRef = ContentRef;
7
+ export type CaptureCapabilities = {
8
+ requestHeaders: "sent" | "approx" | "none";
9
+ responseHeaders: "raw" | "approx" | "none";
10
+ requestBodies: boolean;
11
+ responseBodies: "decoded" | "wire" | "none";
12
+ httpVersion: boolean;
13
+ remoteIp: boolean;
14
+ headerOrderPreserved: boolean;
15
+ };
16
+ export type CaptureRequestEvent = {
17
+ type: "http.request";
18
+ requestId: string;
19
+ url: string;
20
+ method: string;
21
+ headers: Header[];
22
+ timestamp: number;
23
+ frameId?: string;
24
+ resourceType?: ResourceType;
25
+ initiator?: {
26
+ type?: string;
27
+ url?: string;
28
+ };
29
+ };
30
+ export type CaptureResponseEvent = {
31
+ type: "http.response";
32
+ requestId: string;
33
+ url: string;
34
+ status: number;
35
+ statusText?: string;
36
+ headers: Header[];
37
+ timestamp: number;
38
+ mimeType?: string;
39
+ fromDiskCache?: boolean;
40
+ fromServiceWorker?: boolean;
41
+ bodyRef?: CaptureBodyRef;
42
+ bodySize?: number;
43
+ };
44
+ export type CaptureFailedEvent = {
45
+ type: "http.failed";
46
+ requestId: string;
47
+ url: string;
48
+ errorText: string;
49
+ timestamp: number;
50
+ };
51
+ export type CaptureEvent = CaptureRequestEvent | CaptureResponseEvent | CaptureFailedEvent;
52
+ export type CaptureArtifacts = {
53
+ /** Ordered list of captured network events. */
54
+ events: CaptureEvent[];
55
+ /** Content store containing captured bodies referenced by events. */
56
+ contentStore: ContentStore;
57
+ capabilities: CaptureCapabilities;
58
+ };
59
+ export type CaptureContext = {
60
+ capture?: CaptureArtifacts;
61
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import type { ContentStore } from "../../types.js";
2
+ export declare const createMemoryContentStore: (name?: string) => (ContentStore & {
3
+ _bytes: Map<string, Uint8Array>;
4
+ }) | ContentStore;
@@ -0,0 +1,38 @@
1
+ import { streamFromUint8Array } from "../../utils/streams.js";
2
+ import { toUint8Array } from "../../utils.js";
3
+ export const createMemoryContentStore = (name = "memory") => {
4
+ const bytesById = new Map();
5
+ let nextId = 1;
6
+ const storeBytes = (bytes) => {
7
+ const id = String(nextId);
8
+ nextId += 1;
9
+ bytesById.set(id, bytes);
10
+ return id;
11
+ };
12
+ const getStoredBytes = (id) => {
13
+ const bytes = bytesById.get(id);
14
+ if (!bytes) {
15
+ throw new Error(`Missing memory content for ref id=${id}`);
16
+ }
17
+ return bytes;
18
+ };
19
+ return {
20
+ name,
21
+ _bytes: bytesById,
22
+ async put(body, _meta) {
23
+ const bytes = await toUint8Array(body);
24
+ const id = storeBytes(bytes);
25
+ return { kind: "store-ref", id };
26
+ },
27
+ async open(ref) {
28
+ if (ref.kind === "memory") {
29
+ return streamFromUint8Array(ref.data);
30
+ }
31
+ const bytes = getStoredBytes(ref.id);
32
+ return streamFromUint8Array(bytes);
33
+ },
34
+ async dispose() {
35
+ bytesById.clear();
36
+ }
37
+ };
38
+ };
@@ -0,0 +1,61 @@
1
+ import type { ContentRef, ContentStore, ResourceType } from "../../types.js";
2
+ export type Header = {
3
+ name: string;
4
+ value: string;
5
+ };
6
+ export type CaptureBodyRef = ContentRef;
7
+ export type CaptureCapabilities = {
8
+ requestHeaders: "sent" | "approx" | "none";
9
+ responseHeaders: "raw" | "approx" | "none";
10
+ requestBodies: boolean;
11
+ responseBodies: "decoded" | "wire" | "none";
12
+ httpVersion: boolean;
13
+ remoteIp: boolean;
14
+ headerOrderPreserved: boolean;
15
+ };
16
+ export type CaptureRequestEvent = {
17
+ type: "http.request";
18
+ requestId: string;
19
+ url: string;
20
+ method: string;
21
+ headers: Header[];
22
+ timestamp: number;
23
+ frameId?: string;
24
+ resourceType?: ResourceType;
25
+ initiator?: {
26
+ type?: string;
27
+ url?: string;
28
+ };
29
+ };
30
+ export type CaptureResponseEvent = {
31
+ type: "http.response";
32
+ requestId: string;
33
+ url: string;
34
+ status: number;
35
+ statusText?: string;
36
+ headers: Header[];
37
+ timestamp: number;
38
+ mimeType?: string;
39
+ fromDiskCache?: boolean;
40
+ fromServiceWorker?: boolean;
41
+ bodyRef?: CaptureBodyRef;
42
+ bodySize?: number;
43
+ };
44
+ export type CaptureFailedEvent = {
45
+ type: "http.failed";
46
+ requestId: string;
47
+ url: string;
48
+ errorText: string;
49
+ timestamp: number;
50
+ };
51
+ export type CaptureEvent = CaptureRequestEvent | CaptureResponseEvent | CaptureFailedEvent;
52
+ export type CaptureArtifacts = {
53
+ /** Ordered list of captured network events. */
54
+ events: CaptureEvent[];
55
+ /** Content store containing captured bodies referenced by events. */
56
+ contentStore: ContentStore;
57
+ capabilities: CaptureCapabilities;
58
+ };
59
+ export type CaptureContext = {
60
+ capture?: CaptureArtifacts;
61
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { CompletionStrategy } from "../types.js";
2
+ export declare const timeout: (ms: number) => CompletionStrategy;
3
+ export declare const networkIdle: (ms: number, checkInterval?: number) => CompletionStrategy;
4
+ export declare const normalizeCompletion: (completion?: CompletionStrategy | CompletionStrategy[]) => CompletionStrategy[];
@@ -0,0 +1,23 @@
1
+ import { sleep } from "../utils.js";
2
+ export const timeout = (ms) => ({
3
+ async wait() {
4
+ await sleep(ms);
5
+ }
6
+ });
7
+ export const networkIdle = (ms, checkInterval = 100) => ({
8
+ async wait(ctx) {
9
+ while (true) {
10
+ const stats = ctx.getStats();
11
+ const idleFor = ctx.now() - stats.lastNetworkTs;
12
+ if (stats.inflightRequests === 0 && idleFor >= ms) {
13
+ return;
14
+ }
15
+ await sleep(Math.min(checkInterval, ms));
16
+ }
17
+ }
18
+ });
19
+ export const normalizeCompletion = (completion) => {
20
+ if (!completion)
21
+ return [];
22
+ return Array.isArray(completion) ? completion : [completion];
23
+ };
@@ -0,0 +1,21 @@
1
+ import type { BodySource, ContentRef, ContentStore } from "../types.js";
2
+ type HybridContentStoreOptions = {
3
+ thresholdBytes?: number;
4
+ baseDir?: string;
5
+ };
6
+ export declare class HybridContentStore implements ContentStore {
7
+ name: string;
8
+ private thresholdBytes;
9
+ private baseDir;
10
+ private storedIds;
11
+ constructor(options?: HybridContentStoreOptions);
12
+ put(body: BodySource, meta: {
13
+ url: string;
14
+ mimeType?: string;
15
+ sizeHint?: number;
16
+ }): Promise<ContentRef>;
17
+ open(ref: ContentRef): Promise<ReadableStream<Uint8Array>>;
18
+ dispose(): Promise<void>;
19
+ private removeBaseDir;
20
+ }
21
+ export {};