Skip to content

Remove TCP Mode / Server #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
url = https://github.com/lightpanda-io/zig-async-io.git/
[submodule "vendor/websocket.zig"]
path = vendor/websocket.zig
url = https://github.com/lightpanda-io/websocket.zig.git/
url = https://github.com/karlseguin/websocket.zig.git/
branch = lightpanda
3 changes: 2 additions & 1 deletion src/browser/loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ test "basic url get" {
var loader = Loader.init(alloc);
defer loader.deinit();

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

try std.testing.expect(result.req.response.status == std.http.Status.ok);
Expand Down
5 changes: 5 additions & 0 deletions src/cdp/browser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const Methods = enum {
setDownloadBehavior,
getWindowForTarget,
setWindowBounds,
close,
};

pub fn browser(
Expand All @@ -47,6 +48,10 @@ pub fn browser(
.setDownloadBehavior => setDownloadBehavior(alloc, msg, ctx),
.getWindowForTarget => getWindowForTarget(alloc, msg, ctx),
.setWindowBounds => setWindowBounds(alloc, msg, ctx),
.close => {
ctx.state.close = true;
return "";
}
};
}

Expand Down
1 change: 1 addition & 0 deletions src/cdp/cdp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub fn dispatch(
}

pub const State = struct {
close: bool = false,
executionContextId: u32 = 0,
contextID: ?[]const u8 = null,
sessionID: SessionID = .CONTEXTSESSIONID0497A05C95417CF4,
Expand Down
4 changes: 2 additions & 2 deletions src/cdp/runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ fn sendInspector(
const buf = try alloc.alloc(u8, msg.json.len + 1);
defer alloc.free(buf);
_ = std.mem.replace(u8, msg.json, "\"awaitPromise\":true", "\"awaitPromise\":false", buf);
ctx.sendInspector(buf);
try ctx.sendInspector(buf);
return "";
}
}

ctx.sendInspector(msg.json);
try ctx.sendInspector(msg.json);

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

Expand Down
95 changes: 0 additions & 95 deletions src/handler.zig

This file was deleted.

64 changes: 16 additions & 48 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const websocket = @import("websocket");

const Browser = @import("browser/browser.zig").Browser;
const server = @import("server.zig");
const handler = @import("handler.zig");
const MaxSize = @import("msg.zig").MaxSize;

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

// default options
const Host = "127.0.0.1";
Expand Down Expand Up @@ -160,10 +157,6 @@ const CliMode = union(CliModeTag) {
return printUsageExit(execname, 1);
}
}
if (std.mem.eql(u8, "--tcp", opt)) {
_server.tcp = true;
continue;
}

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

// server mode
_server.addr = std.net.Address.parseIp4(_server.host, _server.port) catch |err| {
log.err("address (host:port) {any}\n", .{err});
return printUsageExit(execname, 1);
};
_server.execname = execname;
_server.args = args;
return CliMode{ .server = _server };
Expand Down Expand Up @@ -249,50 +238,19 @@ pub fn main() !void {
.server => |opts| {

// Stream server
const addr = blk: {
if (opts.tcp) {
break :blk opts.addr;
} else {
const unix_path = "/tmp/lightpanda";
std.fs.deleteFileAbsolute(unix_path) catch {}; // file could not exists
break :blk try std.net.Address.initUnix(unix_path);
}
};
const socket = server.listen(addr) catch |err| {
log.err("Server listen error: {any}\n", .{err});
return printUsageExit(opts.execname, 1);
};
defer std.posix.close(socket);
log.debug("Server opts: listening internally on {any}...", .{addr});

const timeout = std.time.ns_per_s * @as(u64, opts.timeout);

// loop
var loop = try jsruntime.Loop.init(alloc);
defer loop.deinit();

// TCP server mode
if (opts.tcp) {
return server.handle(alloc, &loop, socket, null, timeout);
}

// start stream server in separate thread
var stream = handler.Stream{
.ws_host = opts.host,
.ws_port = opts.port,
.addr = addr,
};
const cdp_thread = try std.Thread.spawn(
.{ .allocator = alloc },
server.handle,
.{ alloc, &loop, socket, &stream, timeout },
);
const vm = jsruntime.VM.init();
defer vm.deinit();

// Websocket server
var ws = try websocket.Server(handler.Handler).init(alloc, .{
var ws = try websocket.Server(server.Client).init(alloc, .{
.port = opts.port,
.address = opts.host,
.max_message_size = MaxSize + 14, // overhead websocket
.max_message_size = 256 * 1024 + 14, // + 14 is the max websocket header len
.max_conn = 1,
.handshake = .{
.timeout = 3,
Expand All @@ -304,8 +262,18 @@ pub fn main() !void {
});
defer ws.deinit();

try ws.listen(&stream);
cdp_thread.join();

try ws.listen(.{
.vm = vm,
.loop = &loop,
.allocator = alloc,
// The websocket.zig fork has a hard-coded hack for handling
// puppeteer's request to /json/version. The hack relies on
// having these 2 fields to generating a response. These should
// be removed when the hack is removed.
.ws_host = opts.host,
.ws_port = opts.port,
});
},

.fetch => |opts| {
Expand Down
Loading
Loading