Skip to content

Commit d040405

Browse files
committed
Remove TCP Mode / Server
Websocket handler now interacts directly with cdp.
1 parent 00d332c commit d040405

File tree

10 files changed

+122
-742
lines changed

10 files changed

+122
-742
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
url = https://github.com/lightpanda-io/zig-async-io.git/
3131
[submodule "vendor/websocket.zig"]
3232
path = vendor/websocket.zig
33-
url = https://github.com/lightpanda-io/websocket.zig.git/
33+
url = https://github.com/karlseguin/websocket.zig.git/
3434
branch = lightpanda

src/browser/loader.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ test "basic url get" {
8484
var loader = Loader.init(alloc);
8585
defer loader.deinit();
8686

87-
var result = try loader.get(alloc, "https://en.wikipedia.org/wiki/Main_Page");
87+
const uri = try std.Uri.parse("https://en.wikipedia.org/wiki/Main_Page");
88+
var result = try loader.get(alloc, uri);
8889
defer result.deinit();
8990

9091
try std.testing.expect(result.req.response.status == std.http.Status.ok);

src/cdp/browser.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const Methods = enum {
3232
setDownloadBehavior,
3333
getWindowForTarget,
3434
setWindowBounds,
35+
close,
3536
};
3637

3738
pub fn browser(
@@ -47,6 +48,10 @@ pub fn browser(
4748
.setDownloadBehavior => setDownloadBehavior(alloc, msg, ctx),
4849
.getWindowForTarget => getWindowForTarget(alloc, msg, ctx),
4950
.setWindowBounds => setWindowBounds(alloc, msg, ctx),
51+
.close => {
52+
ctx.state.close = true;
53+
return "";
54+
}
5055
};
5156
}
5257

src/cdp/cdp.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub fn dispatch(
119119
}
120120

121121
pub const State = struct {
122+
close: bool = false,
122123
executionContextId: u32 = 0,
123124
contextID: ?[]const u8 = null,
124125
sessionID: SessionID = .CONTEXTSESSIONID0497A05C95417CF4,

src/cdp/runtime.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ fn sendInspector(
131131
const buf = try alloc.alloc(u8, msg.json.len + 1);
132132
defer alloc.free(buf);
133133
_ = std.mem.replace(u8, msg.json, "\"awaitPromise\":true", "\"awaitPromise\":false", buf);
134-
ctx.sendInspector(buf);
134+
try ctx.sendInspector(buf);
135135
return "";
136136
}
137137
}
138138

139-
ctx.sendInspector(msg.json);
139+
try ctx.sendInspector(msg.json);
140140

141141
if (msg.id == null) return "";
142142

src/handler.zig

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/main.zig

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const websocket = @import("websocket");
2424

2525
const Browser = @import("browser/browser.zig").Browser;
2626
const server = @import("server.zig");
27-
const handler = @import("handler.zig");
2827
const MaxSize = @import("msg.zig").MaxSize;
2928

3029
const parser = @import("netsurf");
@@ -86,11 +85,9 @@ const CliMode = union(CliModeTag) {
8685
const Server = struct {
8786
execname: []const u8 = undefined,
8887
args: *std.process.ArgIterator = undefined,
89-
addr: std.net.Address = undefined,
9088
host: []const u8 = Host,
9189
port: u16 = Port,
9290
timeout: u8 = Timeout,
93-
tcp: bool = false, // undocumented TCP mode
9491

9592
// default options
9693
const Host = "127.0.0.1";
@@ -160,10 +157,6 @@ const CliMode = union(CliModeTag) {
160157
return printUsageExit(execname, 1);
161158
}
162159
}
163-
if (std.mem.eql(u8, "--tcp", opt)) {
164-
_server.tcp = true;
165-
continue;
166-
}
167160

168161
// unknown option
169162
if (std.mem.startsWith(u8, opt, "--")) {
@@ -186,10 +179,6 @@ const CliMode = union(CliModeTag) {
186179
if (default_mode == .server) {
187180

188181
// server mode
189-
_server.addr = std.net.Address.parseIp4(_server.host, _server.port) catch |err| {
190-
log.err("address (host:port) {any}\n", .{err});
191-
return printUsageExit(execname, 1);
192-
};
193182
_server.execname = execname;
194183
_server.args = args;
195184
return CliMode{ .server = _server };
@@ -249,50 +238,19 @@ pub fn main() !void {
249238
.server => |opts| {
250239

251240
// Stream server
252-
const addr = blk: {
253-
if (opts.tcp) {
254-
break :blk opts.addr;
255-
} else {
256-
const unix_path = "/tmp/lightpanda";
257-
std.fs.deleteFileAbsolute(unix_path) catch {}; // file could not exists
258-
break :blk try std.net.Address.initUnix(unix_path);
259-
}
260-
};
261-
const socket = server.listen(addr) catch |err| {
262-
log.err("Server listen error: {any}\n", .{err});
263-
return printUsageExit(opts.execname, 1);
264-
};
265-
defer std.posix.close(socket);
266-
log.debug("Server opts: listening internally on {any}...", .{addr});
267-
268-
const timeout = std.time.ns_per_s * @as(u64, opts.timeout);
269241

270242
// loop
271243
var loop = try jsruntime.Loop.init(alloc);
272244
defer loop.deinit();
273245

274-
// TCP server mode
275-
if (opts.tcp) {
276-
return server.handle(alloc, &loop, socket, null, timeout);
277-
}
278-
279-
// start stream server in separate thread
280-
var stream = handler.Stream{
281-
.ws_host = opts.host,
282-
.ws_port = opts.port,
283-
.addr = addr,
284-
};
285-
const cdp_thread = try std.Thread.spawn(
286-
.{ .allocator = alloc },
287-
server.handle,
288-
.{ alloc, &loop, socket, &stream, timeout },
289-
);
246+
const vm = jsruntime.VM.init();
247+
defer vm.deinit();
290248

291249
// Websocket server
292-
var ws = try websocket.Server(handler.Handler).init(alloc, .{
250+
var ws = try websocket.Server(server.Client).init(alloc, .{
293251
.port = opts.port,
294252
.address = opts.host,
295-
.max_message_size = MaxSize + 14, // overhead websocket
253+
.max_message_size = 256 * 1024 + 14, // + 14 is the max websocket header len
296254
.max_conn = 1,
297255
.handshake = .{
298256
.timeout = 3,
@@ -304,8 +262,18 @@ pub fn main() !void {
304262
});
305263
defer ws.deinit();
306264

307-
try ws.listen(&stream);
308-
cdp_thread.join();
265+
266+
try ws.listen(.{
267+
.vm = vm,
268+
.loop = &loop,
269+
.allocator = alloc,
270+
// The websocket.zig fork has a hard-coded hack for handling
271+
// puppeteer's request to /json/version. The hack relies on
272+
// having these 2 fields to generating a response. These should
273+
// be removed when the hack is removed.
274+
.ws_host = opts.host,
275+
.ws_port = opts.port,
276+
});
309277
},
310278

311279
.fetch => |opts| {

0 commit comments

Comments
 (0)