dsh-web-fetch-playwright 0.2.13 → 0.2.15

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.
@@ -0,0 +1,229 @@
1
+ /** Explicit CDP window allocation for session-owned Playwright contexts. */
2
+ import type { PlaywrightBrowser, PlaywrightContext, PlaywrightPage } from './types.ts'
3
+
4
+ interface CdpSession {
5
+ send(method: string, params?: Record<string, unknown>): Promise<any>
6
+ detach(): Promise<void>
7
+ }
8
+ interface WindowContext {
9
+ newCDPSession(page: PlaywrightPage): Promise<CdpSession>
10
+ pages(): PlaywrightPage[]
11
+ on(event: 'page', listener: (page: PlaywrightPage) => void): unknown
12
+ removeListener(event: 'page', listener: (page: PlaywrightPage) => void): unknown
13
+ }
14
+ interface WindowBrowser { newBrowserCDPSession(): Promise<CdpSession> }
15
+ interface TargetIdentity { targetId: string; browserContextId: string }
16
+
17
+ // Weak ownership plus close-event pruning prevents retaining completed sessions.
18
+ const ownership = new WeakMap<PlaywrightBrowser, Map<string, WeakRef<PlaywrightContext>>>()
19
+ // Never adopt an unconfirmed late tab after timing out, nor close sibling leases.
20
+ const unavailable = new WeakSet<PlaywrightContext>()
21
+ const CLEANUP_GRACE_MS = 100
22
+
23
+ function claim(browser: PlaywrightBrowser, context: PlaywrightContext, keys: string[]): void {
24
+ let owners = ownership.get(browser)
25
+ if (!owners) { owners = new Map(); ownership.set(browser, owners) }
26
+ for (const [key, reference] of owners) if (!reference.deref()) owners.delete(key)
27
+ for (const key of keys) {
28
+ const previous = owners.get(key)?.deref()
29
+ if (previous && previous !== context) throw new Error('session context or window identity is already owned by another session context')
30
+ }
31
+ const reference = new WeakRef(context)
32
+ const added = keys.filter(key => !owners!.has(key))
33
+ for (const key of added) owners.set(key, reference)
34
+ if (added.length) context.on?.('close', () => {
35
+ for (const key of added) if (owners!.get(key) === reference) owners!.delete(key)
36
+ })
37
+ }
38
+
39
+ /** Bound a promise without abandoning responsibility for resources arriving late. */
40
+ class SetupDeadline {
41
+ private readonly end: number
42
+ stopped = false
43
+ constructor(timeoutMs: number, private readonly expired: () => void) {
44
+ this.end = Date.now() + Math.max(1, timeoutMs)
45
+ }
46
+ run<T>(operation: () => Promise<T>, late?: (value: T) => Promise<unknown>): Promise<T> {
47
+ if (this.stopped) return Promise.reject(new Error('session window setup has finished'))
48
+ if (Date.now() >= this.end) {
49
+ this.expired()
50
+ return Promise.reject(new Error('timed out creating the dedicated session window'))
51
+ }
52
+ return new Promise<T>((resolve, reject) => {
53
+ let timedOut = false
54
+ const timer = setTimeout(() => {
55
+ timedOut = true
56
+ if (!this.stopped) this.expired()
57
+ reject(new Error('timed out creating the dedicated session window'))
58
+ }, Math.max(1, this.end - Date.now()))
59
+ void Promise.resolve().then(operation).then(value => {
60
+ clearTimeout(timer)
61
+ if (timedOut) { if (late) void Promise.resolve().then(() => late(value)).catch(() => {}); return }
62
+ resolve(value)
63
+ }, error => { clearTimeout(timer); if (!timedOut) reject(error) })
64
+ })
65
+ }
66
+ }
67
+
68
+ /** Cleanup must never hold a pool lock indefinitely, including detach itself. */
69
+ async function cleanup(operation: () => Promise<unknown>): Promise<void> {
70
+ let timer: ReturnType<typeof setTimeout> | undefined
71
+ await Promise.race([
72
+ Promise.resolve().then(operation).catch(() => {}),
73
+ new Promise<void>(resolve => { timer = setTimeout(resolve, CLEANUP_GRACE_MS) }),
74
+ ])
75
+ if (timer) clearTimeout(timer)
76
+ }
77
+
78
+ /**
79
+ * Caller serializes creation and release for this context. A bootstrap supplies
80
+ * its public CDP identity; the real first tab explicitly requests a NEW window.
81
+ * Subsequent tabs must match that window. Never falls back to the profile.
82
+ * On timeout only owned pages/targets are cleaned; existing leases stay alive.
83
+ */
84
+ export async function createSessionPage(
85
+ context: PlaywrightContext,
86
+ browser: PlaywrightBrowser,
87
+ timeoutMs: number,
88
+ ): Promise<PlaywrightPage> {
89
+ const ctx = context as PlaywrightContext & WindowContext
90
+ const owner = browser as PlaywrightBrowser & WindowBrowser
91
+ if (typeof owner.newBrowserCDPSession !== 'function' || typeof ctx.newCDPSession !== 'function'
92
+ || typeof ctx.pages !== 'function' || typeof ctx.on !== 'function' || typeof ctx.removeListener !== 'function') {
93
+ throw new Error('session window isolation requires Playwright Chromium CDP session and page event APIs')
94
+ }
95
+ if (browser.contexts?.()[0] === context) throw new Error('session window isolation must not use the default browser profile context')
96
+ if (unavailable.has(context)) throw new Error('session window context is unavailable after a setup timeout')
97
+ const deadline = new SetupDeadline(timeoutMs, () => unavailable.add(context))
98
+ const sessions = new Set<CdpSession>()
99
+ let root: CdpSession | undefined
100
+ let bootstrap: PlaywrightPage | undefined
101
+ let bootstrapTargetId: string | undefined
102
+ let created: PlaywrightPage | undefined
103
+ let targetId: string | undefined
104
+ let listener: ((page: PlaywrightPage) => void) | undefined
105
+ let succeeded = false
106
+ const detach = (session: CdpSession) => cleanup(() => session.detach())
107
+ const closePage = (page: PlaywrightPage) => cleanup(() => page.close())
108
+ const newSession = async (operation: () => Promise<CdpSession>) => {
109
+ const session = await deadline.run(operation, detach)
110
+ if (deadline.stopped) { await detach(session); throw new Error('session window setup has finished') }
111
+ sessions.add(session)
112
+ return session
113
+ }
114
+ const identify = async (page: PlaywrightPage): Promise<TargetIdentity> => {
115
+ const session = await newSession(() => ctx.newCDPSession(page))
116
+ let identificationFailed = false
117
+ try {
118
+ const { targetInfo } = await deadline.run(() => session.send('Target.getTargetInfo'))
119
+ if (!targetInfo?.targetId || !targetInfo.browserContextId) throw new Error('session window isolation requires a target and browser context identity')
120
+ return targetInfo
121
+ } catch (error) {
122
+ identificationFailed = true
123
+ throw error
124
+ } finally {
125
+ // Bounded by the SAME setup deadline; a stuck detach is not an unbounded
126
+ // finally. Keep it tracked so outer cleanup can retry best-effort.
127
+ try {
128
+ await deadline.run(() => session.detach())
129
+ sessions.delete(session)
130
+ } catch (error) { if (!identificationFailed) throw error }
131
+ }
132
+ }
133
+ const windowOf = async (id: string): Promise<number> => {
134
+ const { windowId } = await deadline.run(() => root!.send('Browser.getWindowForTarget', { targetId: id }))
135
+ if (!Number.isInteger(windowId)) throw new Error('session window isolation requires a browser window identity')
136
+ return windowId
137
+ }
138
+ // A late createTarget result arrives after the original root may be detached.
139
+ const closeLateTarget = async (value: { targetId?: string }) => {
140
+ if (!value?.targetId) return
141
+ const lateDeadline = new SetupDeadline(CLEANUP_GRACE_MS, () => {})
142
+ let session: CdpSession | undefined
143
+ try {
144
+ session = await lateDeadline.run(() => owner.newBrowserCDPSession(), detach)
145
+ await lateDeadline.run(() => session!.send('Target.closeTarget', { targetId: value.targetId }))
146
+ } finally { if (session) await detach(session) }
147
+ }
148
+ try {
149
+ root = await newSession(() => owner.newBrowserCDPSession())
150
+ const existing = ctx.pages()
151
+ if (existing.length) {
152
+ const identity = await identify(existing[0]!)
153
+ const expectedWindow = await windowOf(identity.targetId)
154
+ for (const page of existing.slice(1)) {
155
+ const other = await identify(page)
156
+ if (other.browserContextId !== identity.browserContextId || await windowOf(other.targetId) !== expectedWindow) {
157
+ throw new Error('session context contains tabs in multiple windows; refusing unsafe reuse')
158
+ }
159
+ }
160
+ created = await deadline.run(() => ctx.newPage(), closePage)
161
+ const actual = await identify(created)
162
+ targetId = actual.targetId
163
+ if (actual.browserContextId !== identity.browserContextId || await windowOf(actual.targetId) !== expectedWindow) {
164
+ throw new Error('Chromium did not place the session tab in its dedicated window')
165
+ }
166
+ claim(browser, context, [`context:${actual.browserContextId}`, `window:${expectedWindow}`])
167
+ succeeded = true
168
+ return created
169
+ }
170
+ bootstrap = await deadline.run(() => ctx.newPage(), closePage)
171
+ const identity = await identify(bootstrap)
172
+ bootstrapTargetId = identity.targetId
173
+ claim(browser, context, [`context:${identity.browserContextId}`])
174
+ const bootstrapWindow = await windowOf(identity.targetId)
175
+ // Register BEFORE createTarget. Events can precede its response; match the
176
+ // exact returned target id, never URL, event order, or context.pages()[0].
177
+ const candidates = new Map<string, { page: PlaywrightPage; contextId: string }>()
178
+ let resolvePage!: (page: PlaywrightPage) => void
179
+ let rejectPage!: (error: Error) => void
180
+ const pending = new Promise<PlaywrightPage>((resolve, reject) => { resolvePage = resolve; rejectPage = reject })
181
+ void pending.catch(() => {})
182
+ const match = () => {
183
+ const candidate = targetId ? candidates.get(targetId) : undefined
184
+ if (!candidate) return
185
+ if (candidate.contextId !== identity.browserContextId) rejectPage(new Error('created target belongs to the wrong browser context'))
186
+ else resolvePage(candidate.page)
187
+ }
188
+ listener = page => {
189
+ void identify(page).then(info => {
190
+ candidates.set(info.targetId, { page, contextId: info.browserContextId })
191
+ match()
192
+ }).catch(() => { /* unrelated pages may close during target discovery */ })
193
+ }
194
+ ctx.on('page', listener)
195
+ // Bind creation to the bootstrap PAGE session. A CDP gateway may map the
196
+ // Playwright context alias to another Chromium process while routing a
197
+ // browser-target session to its main process. Page sessions follow their
198
+ // actual target; getTargetInfo above supplies that backend's context ID.
199
+ const creator = await newSession(() => ctx.newCDPSession(bootstrap!))
200
+ const result = await deadline.run(() => creator.send('Target.createTarget', {
201
+ url: 'about:blank', browserContextId: identity.browserContextId, newWindow: true,
202
+ }), closeLateTarget)
203
+ if (typeof result.targetId !== 'string' || !result.targetId) throw new Error('CDP did not return a session window target id')
204
+ const explicitTargetId: string = result.targetId
205
+ targetId = explicitTargetId
206
+ match()
207
+ for (const page of ctx.pages()) listener(page)
208
+ created = await deadline.run(() => pending, closePage)
209
+ const explicitWindow = await windowOf(explicitTargetId)
210
+ if (explicitWindow === bootstrapWindow) throw new Error('Chromium ignored the dedicated session window request')
211
+ claim(browser, context, [`context:${identity.browserContextId}`, `window:${explicitWindow}`])
212
+ await deadline.run(() => bootstrap!.close())
213
+ bootstrap = undefined
214
+ succeeded = true
215
+ return created
216
+ } finally {
217
+ deadline.stopped = true
218
+ if (listener) ctx.removeListener('page', listener)
219
+ // All cleanup runs concurrently under one bounded grace, never closes the
220
+ // shared context/browser, and cannot replace the operation's original error.
221
+ await Promise.all([
222
+ ...(!succeeded && created ? [closePage(created)] : []),
223
+ ...(!succeeded && targetId && root ? [cleanup(() => root!.send('Target.closeTarget', { targetId }))] : []),
224
+ ...(bootstrap ? [closePage(bootstrap)] : []),
225
+ ...(bootstrap && bootstrapTargetId && root ? [cleanup(() => root!.send('Target.closeTarget', { targetId: bootstrapTargetId }))] : []),
226
+ ])
227
+ await Promise.all([...sessions].map(detach))
228
+ }
229
+ }
package/src/types.ts CHANGED
@@ -94,14 +94,19 @@ export interface PlaywrightPage {
94
94
  }
95
95
 
96
96
  /**
97
- * A browser context: fetch-owned and isolated (local backend, or CDP
98
- * `isolated` mode), or the remote browser's default context carrying its
99
- * real profile (CDP `profile` mode — never closed by a fetch).
97
+ * A browser context: fetch-owned and isolated, a pool-owned named context,
98
+ * or the remote browser's default context carrying its real profile. Named
99
+ * session contexts close when idle; global contexts are retained until teardown.
100
+ * The default context is never explicitly closed.
100
101
  */
101
102
  export interface PlaywrightContext {
102
103
  newPage(): Promise<PlaywrightPage>
104
+ /** Currently open tabs; used before releasing an idle session context. */
105
+ pages?(): PlaywrightPage[]
103
106
  route(glob: string, handler: (route: PlaywrightRoute) => Promise<void>): Promise<void>
104
107
  close(): Promise<void>
108
+ /** Invalidate cached named contexts when the remote context closes. */
109
+ on?(event: 'close', listener: () => void): unknown
105
110
  }
106
111
 
107
112
  /** A route interception decision. */