aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/api/bun.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-08-25 21:08:41 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-25 21:08:41 -0700
commitd98a93c3181426ea0565193303d3e63d3796231c (patch)
tree71a8d96bc4b75d902643ec877c03070cca8fcf45 /src/bun.js/api/bun.zig
parentf70bb2497b2406e89afec3ee8a36a3b10ef66334 (diff)
downloadbun-d98a93c3181426ea0565193303d3e63d3796231c.tar.gz
bun-d98a93c3181426ea0565193303d3e63d3796231c.tar.zst
bun-d98a93c3181426ea0565193303d3e63d3796231c.zip
Automatically hot reload Bun.serve() (#4344)
* Automatically hot reload Bun.serve() * Update doc * Update example --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/api/bun.zig')
-rw-r--r--src/bun.js/api/bun.zig60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index a9d7ed970..71b065993 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -2481,7 +2481,7 @@ pub fn serve(
callframe: *JSC.CallFrame,
) callconv(.C) JSC.JSValue {
const arguments = callframe.arguments(2).slice();
- const config: JSC.API.ServerConfig = brk: {
+ var config: JSC.API.ServerConfig = brk: {
var exception_ = [1]JSC.JSValueRef{null};
var exception = &exception_;
@@ -2497,6 +2497,40 @@ pub fn serve(
var exception_value: *JSC.JSValue = undefined;
+ if (config.allow_hot) {
+ if (globalObject.bunVM().hotMap()) |hot| {
+ if (config.id.len == 0) {
+ config.id = config.computeID(globalObject.allocator());
+ }
+
+ if (hot.getEntry(config.id)) |entry| {
+ switch (entry.tag()) {
+ @field(@TypeOf(entry.tag()), @typeName(JSC.API.HTTPServer)) => {
+ var server: *JSC.API.HTTPServer = entry.as(JSC.API.HTTPServer);
+ server.onReloadFromZig(&config, globalObject);
+ return server.thisObject;
+ },
+ @field(@TypeOf(entry.tag()), @typeName(JSC.API.DebugHTTPServer)) => {
+ var server: *JSC.API.DebugHTTPServer = entry.as(JSC.API.DebugHTTPServer);
+ server.onReloadFromZig(&config, globalObject);
+ return server.thisObject;
+ },
+ @field(@TypeOf(entry.tag()), @typeName(JSC.API.DebugHTTPSServer)) => {
+ var server: *JSC.API.DebugHTTPSServer = entry.as(JSC.API.DebugHTTPSServer);
+ server.onReloadFromZig(&config, globalObject);
+ return server.thisObject;
+ },
+ @field(@TypeOf(entry.tag()), @typeName(JSC.API.HTTPSServer)) => {
+ var server: *JSC.API.HTTPSServer = entry.as(JSC.API.HTTPSServer);
+ server.onReloadFromZig(&config, globalObject);
+ return server.thisObject;
+ },
+ else => {},
+ }
+ }
+ }
+ }
+
// Listen happens on the next tick!
// This is so we can return a Server object
if (config.ssl_config != null) {
@@ -2515,6 +2549,12 @@ pub fn serve(
obj.protect();
server.thisObject = obj;
+
+ if (config.allow_hot) {
+ if (globalObject.bunVM().hotMap()) |hot| {
+ hot.insert(config.id, server);
+ }
+ }
return obj;
} else {
var server = JSC.API.HTTPSServer.init(config, globalObject.ptr());
@@ -2530,6 +2570,12 @@ pub fn serve(
const obj = server.toJS(globalObject);
obj.protect();
server.thisObject = obj;
+
+ if (config.allow_hot) {
+ if (globalObject.bunVM().hotMap()) |hot| {
+ hot.insert(config.id, server);
+ }
+ }
return obj;
}
} else {
@@ -2547,6 +2593,12 @@ pub fn serve(
const obj = server.toJS(globalObject);
obj.protect();
server.thisObject = obj;
+
+ if (config.allow_hot) {
+ if (globalObject.bunVM().hotMap()) |hot| {
+ hot.insert(config.id, server);
+ }
+ }
return obj;
} else {
var server = JSC.API.HTTPServer.init(config, globalObject.ptr());
@@ -2563,6 +2615,12 @@ pub fn serve(
obj.protect();
server.thisObject = obj;
+
+ if (config.allow_hot) {
+ if (globalObject.bunVM().hotMap()) |hot| {
+ hot.insert(config.id, server);
+ }
+ }
return obj;
}
}