Commit Graph

4409 Commits

Author SHA1 Message Date
Pierre Tachoire
a24fcc6a5c use session arg to load cookies from file 2026-04-15 10:29:53 -04:00
Pierre Tachoire
cc4bd417d2 save cookies at the end of fetch 2026-04-15 10:09:14 -04:00
Pierre Tachoire
de3404dfc5 fix zig syntax 2026-04-15 10:09:13 -04:00
Matt Van Horn
35991a1b32 refactor: split --cookies-file into --cookie/--cookie-jar per curl convention
Split the single --cookies-file flag into two flags following curl's
convention as requested by @krichprollsch:

- --cookie (read-only): loads cookies at startup for fetch, mcp, and
  serve/CDP commands
- --cookie-jar (write-only): saves cookies on exit for fetch and mcp
  only (CDP cookie-jar deferred per maintainer guidance)

Add cookie integration to MCP server (load in init, save in deinit)
and CDP session creation (load only). The serve command now rejects
--cookie-jar with a clear error message.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 10:09:13 -04:00
Matt Van Horn
494ef345a1 fix: resolve Zig 0.15 compilation errors in cookies and config
- Escape braces in help text format string to avoid std.debug.print
  interpreting them as format specifiers
- Use writer.interface for std.Io.Writer methods (writeAll, stringify)
  instead of calling them directly on fs.File.Writer
- Replace writer.flush() with writer.end() per codebase convention

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 10:09:13 -04:00
Matt Van Horn
242851249d fix: use file.writer() instead of std.io.bufferedWriter
std.io.bufferedWriter doesn't exist in Zig 0.15.2. Use the
file.writer(&buf) pattern that matches the rest of the codebase.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 10:09:13 -04:00
Matt Van Horn
4d384dfe01 feat: add --cookies-file flag for session persistence
Add a --cookies-file CLI option that loads cookies from a JSON file
at startup and saves them back on exit. This enables AI agents to
maintain login sessions across multiple Lightpanda invocations.

The cookie format matches CDP Network.Cookie (compatible with
Puppeteer's page.cookies() export):
  [{"name":"sid","value":"abc","domain":".example.com","path":"/",
    "expires":1234567890,"secure":true,"httpOnly":true}]

Closes #335

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 10:09:12 -04:00
Karl Seguin
55791932f8 quiet a couple test warnings 2026-04-15 19:30:46 +08:00
Karl Seguin
8ab5f1b21f Improve finalizer code
2 small changes:
1 - Ensure that isolated world identity is always reset. Not clear how we can
have identity without a context, but very little harm in doing it this way.

2 - Clear finalizer_callback map _before_ finalizing an instance. Finalizing
an instance could (a) release an arena (they almost all do) and (b) create an
object with the newly released arena. I don't think anything does that now, but
they could. That object could even be passed into v8 during finalization. In
which case, we'd temporarily have 2 "live" instances (one being finalized, one
jsut created) at the same address. Don't think we have any code that does this,
but switching the order (remove from map, then finalize) protects against this
address re-use.

1 big change:
Not really big, but more likely to actually fix things. A finalizer can still
be called _after_ we've cleared the finalizer callback. This can happen if
v8 has queued the finalizer prior to us clearing it. We do see some evidence
that this might be an issue, as many extra releaseRefs are happening in
the message loop or microtasks. This makes that scenario safer. First, it moves
the finalizer identity to a dedicated MemoryPool that can outlive the page.
Second, it uses the finalizer_callback map itself to tell whether or not
anything has to happen.
2026-04-15 19:06:10 +08:00
Karl Seguin
513af82751 On page reset, reset IsolatedWorld identity
This is an attempt to fix reference counting issues. When Session.replacePage
is called, isolated worlds survive. This appears to be the correct behavior, but
it means that their identity outlives the page reset, which can result in a
use-after-free. The idea is that, on reset, IsolatedWorld persist, but their
identity is cleared.
2026-04-15 17:40:33 +08:00
Karl Seguin
5dc059cbb3 maintain isolated world name 2026-04-15 17:16:42 +08:00
Karl Seguin
dd7fbf17ed fix default-ness 2026-04-15 16:11:35 +08:00
Karl Seguin
0b72826cab Fix Page.createIsolatedWorld
Depends on https://github.com/lightpanda-io/zig-v8-fork/pull/170

Gets the correct executeContextId from the v8 inspector.
2026-04-15 16:02:24 +08:00
Karl Seguin
9654bc9afe Return promise on media.play() 2026-04-15 10:27:00 +08:00
Karl Seguin
16686c13af Add safety check around cache get
It should be impossible for a internal cache get to have an incorrect # of
internal fields. But we're seeing this exact scenario in production.
https://github.com/lightpanda-io/browser/pull/1991 was meant to help with this,
but you can do some pretty weird things in JavaScript and it's possible there's
some combination of JavaScript which still allows calling these methods on a
different receiver.
2026-04-15 09:02:38 +08:00
Karl Seguin
1f94312086 Merge pull request #2161 from lightpanda-io/crash_fix
Various crash fixes
2026-04-15 07:09:14 +08:00
Karl Seguin
beb6069ce5 Merge pull request #2160 from lightpanda-io/fix_fetch_cleanup_order
Remove unnecessary flag clear
2026-04-15 07:09:02 +08:00
Karl Seguin
5de092f662 Merge pull request #2159 from lightpanda-io/document_font_lifecycle
acquire reference on document font
2026-04-15 07:08:52 +08:00
Karl Seguin
5c33c8fc3c Merge pull request #2158 from lightpanda-io/websocket_tweaks
WS.close returns DOMException
2026-04-15 07:08:37 +08:00
Karl Seguin
ada235a8c8 Various crash fixes
1. Double buffer to_load list so that load callback which register more loadable
elements don't invalid the list while we're iterating

2. Switch to debug-only assertion for opaque origin. Not clear how this
assertion is failing, but isn't worth failing release builds for it.

3. on worker terminate, don't remove worker from page tracking. This results in
a leaking context, which causes numerous problems.

4. On page.init error, cleanly shutdown context
2026-04-15 01:14:26 +08:00
Karl Seguin
506f52bea2 Remove unnecessary flag clear
Fetch is owned by response.arena (a) we need to clear the flag before freeing
the arena and (b) there's no point in clearing the flag at all, since the
memory is freed.
2026-04-15 00:06:36 +08:00
Karl Seguin
2087aa7aac acquire reference on document font 2026-04-14 22:53:03 +08:00
Karl Seguin
eb0af793c2 WS.close returns DOMException
Cleanup WS after sending disconnetion events

Mapping support for []f32 and []f64

Default max WS connections 8 -> 64
2026-04-14 21:58:43 +08:00
Pierre Tachoire
28614479ae Merge pull request #2153 from lightpanda-io/cdp-useragent
cdp: Emulation.setUserAgentOverride implementation
2026-04-14 13:53:19 +02:00
Halil Durak
c42e242897 Fetch.init: prefer isSameOrigin for comparison 2026-04-14 13:12:48 +03:00
Halil Durak
f9fc858212 Page.isSameOrigin: !bool -> bool 2026-04-14 13:12:48 +03:00
Halil Durak
e698028e3a Fetch: cookie jar should only be included for include and same origin requests 2026-04-14 13:12:48 +03:00
Karl Seguin
5226df9968 Tweak tests
De-duplicate user agent validation
2026-04-14 17:11:54 +08:00
Karl Seguin
b088c42980 update workers for better cdp integration 2026-04-14 12:42:07 +08:00
Karl Seguin
1adb371902 fix rebase 2026-04-14 12:24:10 +08:00
Karl Seguin
4c02959596 update workers to use new arena pool 2026-04-14 12:24:09 +08:00
Karl Seguin
197de3dc0c Limit Worker API to supported types
In order for an API to be supported by workers, their dependency on *Page has
to be removed. To keep this PR smaller, we're only converting a minimum number
of APIs from Page to Execution. All other APIs should not be exposed to the
worker (better to get a FormData undefined than to try to segfault trying to
execute FormData without a page).
2026-04-14 12:24:09 +08:00
Karl Seguin
d1d561f5c1 Worker.close() 2026-04-14 12:24:09 +08:00
Karl Seguin
3830ccfdf2 support worker navigating using object url blobs 2026-04-14 12:24:09 +08:00
Karl Seguin
b3a8a7454e add error callbacks for workers 2026-04-14 12:24:09 +08:00
Karl Seguin
5e7f891546 Add unhandledPromiseRejection for Worker
Allow *Session to be injected into WebAPI for more generic webapis between
page and workers.
2026-04-14 12:24:09 +08:00
Karl Seguin
0247b21483 Improve event dispatching between/from workers 2026-04-14 12:24:08 +08:00
Karl Seguin
ce6b01f7f6 Allow structuredClone to target a different context 2026-04-14 12:24:08 +08:00
Karl Seguin
fb844a17b6 Worker WebAPI 2026-04-14 12:24:08 +08:00
Karl Seguin
951986b655 Create Worker context in snapshot
Turns out you can embed multiple contexts within a snapshot. So our snapshot
now contains the Page context (as before) but also the Worker context. This
gives us the performance benefit of snapshots and makes context creation for
pages and workers much more similar.
2026-04-14 12:24:08 +08:00
Karl Seguin
7ca3aa61e9 Make context work with Page of WGS
A context can be created for either a Page or a Worker. This removes the
Context.page field and replaces it with a Context.global union.
2026-04-14 12:24:08 +08:00
Karl Seguin
cd2bb28c6c Tweak snapshot for workers
We'll have two types of contexts: one for pages and one for workers. They'll
[probably] both be js.Context, but they'll have distinct FunctionTemplates
attached to their global. The Worker template will only contain a small subset
of the main Page's types, along with 1 or 2 of its own specific ones.

The Snapshot now creates the templates for both, so that the Env contains the
function templates for both contexts. Furthermore, having a "merged" view like
this ensures that the env.template[N] indices are consistent between the two.

However, the snapshot only attaches the Page-specific types to the snapshot
context. This allows the Page-context to be created as-is (e.g. efficiently).
The worker context will be created lazily, on demand, but from the templates
loaded into the env (since, again, the env contains templates for both).
2026-04-14 12:24:08 +08:00
Karl Seguin
4e385a3d13 Introduce Execution
A Worker has no page. So any API that is accessible to a worker cannot take
a *Page parameter. Such APIs will now take a js.Execution which the context
will own and create from the Page (or from the WorkerGlobalScope when that's
created).

To test this, in addition to introducing the Execution, this change also updates
URLSearchParams which is accessible to Worker (and the Page obviously). This
change is obviously viral..if URLSearchParams no longer has a *Page but instead
has an *Execution, then any function it calls must also be updated.

So some APIs will take a *Page (those only accessible from a Page) and some will
take an *Execution (those accessible from a Page or Worker). I'm ok with that.

A lot of private/internal functions take a *Page, because it's simple, but all
they want is a call_arena or something. We'll try to update those as much as
possible. The Page/Execution being injected from the bridge is convenient, but
we should be more specific for internal calls and pass only what's needed.
2026-04-14 12:24:06 +08:00
Karl Seguin
f2a2acc1aa Merge pull request #2134 from lightpanda-io/cache_public_default
Cache-Control is public by default
2026-04-14 12:22:25 +08:00
Pierre Tachoire
a114106790 cdp: accept LID- as requestId prefix 2026-04-13 23:46:37 +02:00
Karl Seguin
1ee7641f54 Merge pull request #2138 from lightpanda-io/range_error
Map a zig error.RangeError to a proper JS RangeError
2026-04-13 22:50:35 +08:00
Karl Seguin
12a0fb3fb6 Merge pull request #2137 from lightpanda-io/http_abrupt_close
Handle http response with closed socket
2026-04-13 22:50:18 +08:00
Karl Seguin
2f2f74ef4b Merge pull request #2135 from lightpanda-io/mo_cleanup
On Page cleanup, capture next linked list node _before_ releasing MO
2026-04-13 22:49:59 +08:00
Karl Seguin
9a05fd8af4 Merge pull request #2154 from lightpanda-io/cdp_request_id
Improve loaderId and requestId compatibility
2026-04-13 22:49:27 +08:00
Karl Seguin
ce811c1b48 Merge pull request #2142 from lightpanda-io/formdata_encoding
Encode form data based on the form (or documents) encoding.
2026-04-13 19:51:17 +08:00