Skip to content

Commit

Permalink
Merge branch 'main' into dylan/timeout-test
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway committed May 1, 2024
2 parents 5082ff2 + b48c673 commit 459928c
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 28 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ src/bun.js/WebKit
src/deps
test/snapshots
test/js/deno
test/node.js
src/react-refresh.js
*.min.js
18 changes: 15 additions & 3 deletions src/bun.js/api/bun/subprocess.zig
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub const Subprocess = struct {
};
}
};
process: *Process = undefined,
process: *Process,
stdin: Writable,
stdout: Readable,
stderr: Readable,
Expand Down Expand Up @@ -208,6 +208,7 @@ pub const Subprocess = struct {
is_sync: bool = false,
killed: bool = false,
has_stdin_destructor_called: bool = false,
finalized: bool = false,
};

pub const SignalCode = bun.SignalCode;
Expand Down Expand Up @@ -672,7 +673,13 @@ pub const Subprocess = struct {
this.flags.has_stdin_destructor_called = true;
this.weak_file_sink_stdin_ptr = null;

this.updateHasPendingActivity();
if (this.flags.finalized) {
// if the process has already been garbage collected, we can free the memory now
bun.default_allocator.destroy(this);
} else {
// otherwise update the pending activity flag
this.updateHasPendingActivity();
}
}

pub fn doSend(this: *Subprocess, global: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue {
Expand Down Expand Up @@ -1514,7 +1521,12 @@ pub const Subprocess = struct {

this.process.detach();
this.process.deref();
bun.default_allocator.destroy(this);

this.flags.finalized = true;
if (this.weak_file_sink_stdin_ptr == null) {
// if no file sink exists we can free immediately
bun.default_allocator.destroy(this);
}
}

pub fn getExited(
Expand Down
60 changes: 54 additions & 6 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,25 @@ pub const ServerConfig = struct {
}

if (obj.getTruthy(global, "requestCert")) |request_cert| {
result.request_cert = if (request_cert.asBoolean()) 1 else 0;
any = true;
if (request_cert.isBoolean()) {
result.request_cert = if (request_cert.asBoolean()) 1 else 0;
any = true;
} else {
global.throw("Expected requestCert to be a boolean", .{});
result.deinit();
return null;
}
}

if (obj.getTruthy(global, "rejectUnauthorized")) |reject_unauthorized| {
result.reject_unauthorized = if (reject_unauthorized.asBoolean()) 1 else 0;
any = true;
if (reject_unauthorized.isBoolean()) {
result.reject_unauthorized = if (reject_unauthorized.asBoolean()) 1 else 0;
any = true;
} else {
global.throw("Expected rejectUnauthorized to be a boolean", .{});
result.deinit();
return null;
}
}

if (obj.getTruthy(global, "ciphers")) |ssl_ciphers| {
Expand Down Expand Up @@ -720,8 +732,14 @@ pub const ServerConfig = struct {
}

if (obj.get(global, "lowMemoryMode")) |low_memory_mode| {
result.low_memory_mode = low_memory_mode.toBoolean();
any = true;
if (low_memory_mode.isBoolean() or low_memory_mode.isUndefined()) {
result.low_memory_mode = low_memory_mode.toBoolean();
any = true;
} else {
global.throw("Expected lowMemoryMode to be a boolean", .{});
result.deinit();
return null;
}
}
}

Expand Down Expand Up @@ -4191,6 +4209,11 @@ pub const ServerWebSocket = struct {
return .zero;
}

if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) {
globalThis.throw("publish expects compress to be a boolean", .{});
return .zero;
}

const compress = args.len > 1 and compress_value.toBoolean();

if (message_value.isEmptyOrUndefinedOrNull()) {
Expand Down Expand Up @@ -4268,6 +4291,11 @@ pub const ServerWebSocket = struct {
var topic_slice = topic_value.toSlice(globalThis, bun.default_allocator);
defer topic_slice.deinit();

if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) {
globalThis.throw("publishText expects compress to be a boolean", .{});
return .zero;
}

const compress = args.len > 1 and compress_value.toBoolean();

if (message_value.isEmptyOrUndefinedOrNull() or !message_value.isString()) {
Expand Down Expand Up @@ -4329,6 +4357,11 @@ pub const ServerWebSocket = struct {
return .zero;
}

if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) {
globalThis.throw("publishBinary expects compress to be a boolean", .{});
return .zero;
}

const compress = args.len > 1 and compress_value.toBoolean();

if (message_value.isEmptyOrUndefinedOrNull()) {
Expand Down Expand Up @@ -4497,6 +4530,11 @@ pub const ServerWebSocket = struct {
const message_value = args.ptr[0];
const compress_value = args.ptr[1];

if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) {
globalThis.throw("send expects compress to be a boolean", .{});
return .zero;
}

const compress = args.len > 1 and compress_value.toBoolean();

if (message_value.isEmptyOrUndefinedOrNull()) {
Expand Down Expand Up @@ -4566,6 +4604,11 @@ pub const ServerWebSocket = struct {
const message_value = args.ptr[0];
const compress_value = args.ptr[1];

if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) {
globalThis.throw("sendText expects compress to be a boolean", .{});
return .zero;
}

const compress = args.len > 1 and compress_value.toBoolean();

if (message_value.isEmptyOrUndefinedOrNull() or !message_value.isString()) {
Expand Down Expand Up @@ -4645,6 +4688,11 @@ pub const ServerWebSocket = struct {
const message_value = args.ptr[0];
const compress_value = args.ptr[1];

if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) {
globalThis.throw("sendBinary expects compress to be a boolean", .{});
return .zero;
}

const compress = args.len > 1 and compress_value.toBoolean();

const buffer = message_value.asArrayBuffer(globalThis) orelse {
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/BunWorkerGlobalScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MessagePortChannelProviderImpl;
class GlobalScope : public RefCounted<GlobalScope>, public EventTargetWithInlineData {
WTF_MAKE_ISO_ALLOCATED(GlobalScope);

uint32_t m_messageEventCount;
uint32_t m_messageEventCount{0};

static void onDidChangeListenerImpl(EventTarget&, const AtomString&, OnDidChangeListenerKind);

Expand Down
27 changes: 13 additions & 14 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,8 @@ pub const Arguments = struct {
pub const run_params = run_only_params ++ runtime_params_ ++ transpiler_params_ ++ base_params_;

const bunx_commands = [_]ParamType{
clap.parseParam("--silent Don't print the script command") catch unreachable,
clap.parseParam("-b, --bun Force a script or package to use Bun's runtime instead of Node.js (via symlinking node)") catch unreachable,
};
} ++ auto_only_params;

const build_only_params = [_]ParamType{
clap.parseParam("--compile Generate a standalone Bun executable containing your bundled code") catch unreachable,
Expand Down Expand Up @@ -251,18 +250,6 @@ pub const Arguments = struct {
};
pub const test_params = test_only_params ++ runtime_params_ ++ transpiler_params_ ++ base_params_;

fn printVersionAndExit() noreturn {
@setCold(true);
Output.writer().writeAll(Global.package_json_version ++ "\n") catch {};
Global.exit(0);
}

fn printRevisionAndExit() noreturn {
@setCold(true);
Output.writer().writeAll(Global.package_json_version_with_revision ++ "\n") catch {};
Global.exit(0);
}

pub fn loadConfigPath(allocator: std.mem.Allocator, auto_loaded: bool, config_path: [:0]const u8, ctx: Command.Context, comptime cmd: Command.Tag) !void {
var config_file = switch (bun.sys.openA(config_path, std.os.O.RDONLY, 0)) {
.result => |fd| fd.asFile(),
Expand Down Expand Up @@ -2341,3 +2328,15 @@ pub const Command = struct {
});
};
};

pub fn printVersionAndExit() noreturn {
@setCold(true);
Output.writer().writeAll(Global.package_json_version ++ "\n") catch {};
Global.exit(0);
}

pub fn printRevisionAndExit() noreturn {
@setCold(true);
Output.writer().writeAll(Global.package_json_version_with_revision ++ "\n") catch {};
Global.exit(0);
}
19 changes: 16 additions & 3 deletions src/cli/bunx_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const std = @import("std");
const Command = @import("../cli.zig").Command;
const cli = @import("../cli.zig");
const Command = cli.Command;
const Run = @import("./run_command.zig").RunCommand;

const debug = Output.scoped(.bunx, false);
Expand Down Expand Up @@ -220,6 +221,8 @@ pub const BunxCommand = struct {
var maybe_package_name: ?string = null;
var verbose_install = false;
var silent_install = false;
var has_version = false;
var has_revision = false;
{
var found_subcommand_name = false;

Expand All @@ -230,7 +233,11 @@ pub const BunxCommand = struct {
}

if (positional.len > 0 and positional[0] == '-') {
if (strings.eqlComptime(positional, "--verbose")) {
if (strings.eqlComptime(positional, "--version") or strings.eqlComptime(positional, "-v")) {
has_version = true;
} else if (strings.eqlComptime(positional, "--revision")) {
has_revision = true;
} else if (strings.eqlComptime(positional, "--verbose")) {
verbose_install = true;
} else if (strings.eqlComptime(positional, "--silent")) {
silent_install = true;
Expand All @@ -249,7 +256,13 @@ pub const BunxCommand = struct {

// check if package_name_for_update_request is empty string or " "
if (maybe_package_name == null or maybe_package_name.?.len == 0) {
exitWithUsage();
if (has_revision) {
cli.printRevisionAndExit();
} else if (has_version) {
cli.printVersionAndExit();
} else {
exitWithUsage();
}
}

const package_name = maybe_package_name.?;
Expand Down
2 changes: 1 addition & 1 deletion src/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ __ZN2v87Isolate10GetCurrentEv
__ZN2v87Isolate13TryGetCurrentEv
__ZN2v87Isolate17GetCurrentContextEv
__ZN4node25AddEnvironmentCleanupHookEPN2v87IsolateEPFvPvES3_
__ZN4node28RemoveEnvironmentCleanupHookEPN2v87IsolateEPFvPvES3_
__ZN4node28RemoveEnvironmentCleanupHookEPN2v87IsolateEPFvPvES3_
67 changes: 67 additions & 0 deletions test/cli/install/bunx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,70 @@ it("should work for github repository with committish", async () => {
expect(out.trim()).toContain("hello bun!");
expect(exited).toBe(0);
});

it.each(["--version", "-v"])("should print the version using %s and exit", async flag => {
const subprocess = spawn({
cmd: [bunExe(), "x", flag],
cwd: x_dir,
stdout: "pipe",
stdin: "inherit",
stderr: "pipe",
env,
});

let [err, out, exited] = await Promise.all([
new Response(subprocess.stderr).text(),
new Response(subprocess.stdout).text(),
subprocess.exited,
]);

expect(err).not.toContain("error:");
expect(err).not.toContain("panic:");
expect(out.trim()).toContain(Bun.version);
expect(exited).toBe(0);
});

it("should print the revision and exit", async () => {
const subprocess = spawn({
cmd: [bunExe(), "x", "--revision"],
cwd: x_dir,
stdout: "pipe",
stdin: "inherit",
stderr: "pipe",
env,
});

let [err, out, exited] = await Promise.all([
new Response(subprocess.stderr).text(),
new Response(subprocess.stdout).text(),
subprocess.exited,
]);

expect(err).not.toContain("error:");
expect(err).not.toContain("panic:");
expect(out.trim()).toContain(Bun.version);
expect(out.trim()).toContain(Bun.revision.slice(0, 7));
expect(exited).toBe(0);
});

it("should pass --version to the package if specified", async () => {
const subprocess = spawn({
cmd: [bunExe(), "x", "esbuild", "--version"],
cwd: x_dir,
stdout: "pipe",
stdin: "inherit",
stderr: "pipe",
env,
});

let [err, out, exited] = await Promise.all([
new Response(subprocess.stderr).text(),
new Response(subprocess.stdout).text(),
subprocess.exited,
]);

expect(err).not.toContain("error:");
expect(err).not.toContain("panic:");
expect(out.trim()).not.toContain(Bun.version);
expect(exited).toBe(0);
});
43 changes: 43 additions & 0 deletions test/js/bun/http/serve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1481,3 +1481,46 @@ if (process.platform === "linux")
});
}).toThrow("permission denied 0.0.0.0:1003");
});

describe("should error with invalid options", async () => {
it("requestCert", () => {
expect(() => {
Bun.serve({
port: 0,
fetch(req) {
return new Response("hi");
},
tls: {
requestCert: "invalid",
},
});
}).toThrow("Expected requestCert to be a boolean");
});
it("rejectUnauthorized", () => {
expect(() => {
Bun.serve({
port: 0,
fetch(req) {
return new Response("hi");
},
tls: {
rejectUnauthorized: "invalid",
},
});
}).toThrow("Expected rejectUnauthorized to be a boolean");
});
it("lowMemoryMode", () => {
expect(() => {
Bun.serve({
port: 0,
fetch(req) {
return new Response("hi");
},
tls: {
rejectUnauthorized: true,
lowMemoryMode: "invalid",
},
});
}).toThrow("Expected lowMemoryMode to be a boolean");
});
});
Loading

0 comments on commit 459928c

Please sign in to comment.