Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
paperclover committed Feb 12, 2025
1 parent 9067732 commit 063ecfa
Show file tree
Hide file tree
Showing 21 changed files with 1,082 additions and 279 deletions.
75 changes: 40 additions & 35 deletions src/bake/DevServer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ pub fn attachRoutes(dev: *DevServer, server: anytype) !bool {
}

fn onNotFound(_: *DevServer, _: *Request, resp: anytype) void {
notFound(resp);
}

fn notFound(resp: anytype) void {
resp.corked(onNotFoundCorked, .{resp});
}

Expand All @@ -896,28 +900,28 @@ fn onNotFoundCorked(resp: anytype) void {
fn onJsRequest(dev: *DevServer, req: *Request, resp: AnyResponse) void {
const route_id = req.parameter(0);
if (!bun.strings.hasSuffixComptime(route_id, ".js"))
return req.setYield(true);
return notFound(resp);
const min_len = "-00000000FFFFFFFF.js".len;
if (route_id.len < min_len)
return req.setYield(true);
return notFound(resp);
const hex = route_id[route_id.len - min_len + 1 ..][0 .. @sizeOf(u64) * 2];
if (hex.len != @sizeOf(u64) * 2)
return req.setYield(true);
return notFound(resp);
const id = parseHexToInt(u64, hex) orelse
return req.setYield(true);
return notFound(resp);

const route_bundle_index: RouteBundle.Index = .init(@intCast(id & 0xFFFFFFFF));
const generation: u32 = @intCast(id >> 32);

if (route_bundle_index.get() >= dev.route_bundles.items.len)
return req.setYield(true);
return notFound(resp);

const route_bundle = dev.route_bundles.items[route_bundle_index.get()];
if (route_bundle.client_script_generation != generation or
route_bundle.server_state != .loaded)
{
bun.Output.debugWarn("TODO: Outdated JS Payload", .{});
return req.setYield(true);
return notFound(resp);
}

dev.onJsRequestWithBundle(route_bundle_index, resp, bun.http.Method.which(req.method()) orelse .POST);
Expand All @@ -926,15 +930,15 @@ fn onJsRequest(dev: *DevServer, req: *Request, resp: AnyResponse) void {
fn onAssetRequest(dev: *DevServer, req: *Request, resp: AnyResponse) void {
const param = req.parameter(0);
if (param.len < @sizeOf(u64) * 2)
return req.setYield(true);
return notFound(resp);
const hex = param[0 .. @sizeOf(u64) * 2];
var out: [@sizeOf(u64)]u8 = undefined;
assert((std.fmt.hexToBytes(&out, hex) catch
return req.setYield(true)).len == @sizeOf(u64));
return notFound(resp)).len == @sizeOf(u64));
const hash: u64 = @bitCast(out);
debug.log("onAssetRequest {} {s}", .{ hash, param });
const asset = dev.assets.get(hash) orelse
return req.setYield(true);
return notFound(resp);
req.setYield(false);
asset.on(resp);
}
Expand Down Expand Up @@ -1356,9 +1360,13 @@ fn getJavaScriptCodeForHTMLFile(
try bun.js_printer.writeJSONString(input_file_sources[index.get()].path.pretty, @TypeOf(w), w, .utf8);
try w.writeAll("(m) {\n ");
for (import_records[index.get()].slice()) |import| {
if (import.source_index.isValid() and
!loaders[import.source_index.get()].isJavaScriptLike())
continue; // ignore non-JavaScript imports
if (import.source_index.isValid()) {
if (!loaders[import.source_index.get()].isJavaScriptLike())
continue; // ignore non-JavaScript imports
} else {
if (!import.path.is_disabled)
continue;
}

try w.writeAll(" m.dynamicImport(");
try bun.js_printer.writeJSONString(import.path.pretty, @TypeOf(w), w, .utf8);
Expand Down Expand Up @@ -1831,7 +1839,7 @@ pub const HotUpdateContext = struct {
rc: *const HotUpdateContext,
comptime side: bake.Side,
i: bun.JSAst.Index,
) *IncrementalGraph(side).FileIndex {
) *IncrementalGraph(side).FileIndex.Optional {
const start = switch (side) {
.client => 0,
.server => rc.sources.len,
Expand Down Expand Up @@ -1903,6 +1911,7 @@ pub fn finalizeBundle(
}

const resolved_index_cache = try bv2.graph.allocator.alloc(u32, input_file_sources.len * 2);
@memset(resolved_index_cache, @intFromEnum(IncrementalGraph(.server).FileIndex.Optional.none));

var ctx: bun.bake.DevServer.HotUpdateContext = .{
.import_records = import_records,
Expand Down Expand Up @@ -2011,7 +2020,7 @@ pub fn finalizeBundle(
null, // HTML chunk does not have a source map.
false,
);
const client_index = ctx.getCachedIndex(.client, index).*;
const client_index = ctx.getCachedIndex(.client, index).*.unwrap() orelse @panic("unresolved index");
const route_bundle_index = dev.client_graph.htmlRouteBundleIndex(client_index);
const route_bundle = dev.routeBundlePtr(route_bundle_index);
assert(route_bundle.data.html.bundled_file == client_index);
Expand Down Expand Up @@ -3176,7 +3185,7 @@ pub fn IncrementalGraph(side: bake.Side) type {
g.stale_files.unset(gop.index);
}

ctx.getCachedIndex(side, index).* = FileIndex.init(@intCast(gop.index));
ctx.getCachedIndex(side, index).* = .init(FileIndex.init(@intCast(gop.index)));

switch (side) {
.client => {
Expand Down Expand Up @@ -3311,7 +3320,8 @@ pub fn IncrementalGraph(side: bake.Side) type {
temp_alloc: Allocator,
) bun.OOM!void {
const log = bun.Output.scoped(.processChunkDependencies, false);
const file_index: FileIndex = ctx.getCachedIndex(side, bundle_graph_index).*;
const file_index: FileIndex = ctx.getCachedIndex(side, bundle_graph_index).*.unwrap() orelse
@panic("unresolved index"); // do not process for failed chunks
log("index id={d} {}:", .{
file_index.get(),
bun.fmt.quote(g.bundled_files.keys()[file_index.get()]),
Expand Down Expand Up @@ -3425,22 +3435,20 @@ pub fn IncrementalGraph(side: bake.Side) type {

if (!import_record.source_index.isRuntime()) try_index_record: {
const key = import_record.path.keyForIncrementalGraph();
const imported_file_index = if (import_record.source_index.isInvalid())
FileIndex.init(@intCast(
g.bundled_files.getIndex(key) orelse break :try_index_record,
))
else
ctx.getCachedIndex(side, import_record.source_index).*;
const imported_file_index: FileIndex = brk: {
if (import_record.source_index.isValid()) {
if (ctx.getCachedIndex(side, import_record.source_index).*.unwrap()) |i| {
break :brk i;
}
}
break :brk .init(@intCast(
g.bundled_files.getIndex(key) orelse
break :try_index_record,
));
};

if (Environment.isDebug) {
if (imported_file_index.get() > g.bundled_files.count()) {
Output.debugWarn("Invalid mapped source index {x}. {} was not inserted into IncrementalGraph", .{
imported_file_index.get(),
bun.fmt.quote(key),
});
Output.flush();
continue;
}
bun.assert(imported_file_index.get() < g.bundled_files.count());
}

const gop = try quick_lookup.getOrPut(temp_alloc, imported_file_index);
Expand Down Expand Up @@ -3649,9 +3657,6 @@ pub fn IncrementalGraph(side: bake.Side) type {
try g.owner().incremental_result.failures_added.append(g.owner().allocator, fail);
return;
}

if (Environment.isDebug)
assert(!g.stale_files.isSet(file_index.get())); // should not be left stale
},
}

Expand Down Expand Up @@ -3770,7 +3775,7 @@ pub fn IncrementalGraph(side: bake.Side) type {

debug.log("Insert stale: {s}", .{abs_path});
const gop = try g.bundled_files.getOrPut(g.owner().allocator, abs_path);
const file_index = FileIndex.init(@intCast(gop.index));
const file_index: FileIndex = .init(@intCast(gop.index));

if (!gop.found_existing) {
gop.key_ptr.* = try bun.default_allocator.dupe(u8, abs_path);
Expand All @@ -3794,7 +3799,7 @@ pub fn IncrementalGraph(side: bake.Side) type {
},
}

ctx.getCachedIndex(.server, index).* = file_index;
ctx.getCachedIndex(.server, index).* = .init(file_index);
}

pub fn insertFailure(
Expand Down
2 changes: 0 additions & 2 deletions src/bake/client/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ export function decodeAndAppendError(r: DataViewReader) {
}

export function updateErrorOverlay() {
console.log(errors, updatedErrorOwners);

if (errors.size === 0) {
if (IS_ERROR_RUNTIME) {
location.reload();
Expand Down
4 changes: 2 additions & 2 deletions src/bake/client/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface WebSocketWrapper {

export function initWebSocket(
handlers: Record<number, (dv: DataView<ArrayBuffer>, ws: WebSocket) => void>,
url: string = "/_bun/hmr",
{ url = "/_bun/hmr", displayMessage = "Live-reloading socket" }: { url?: string; displayMessage?: string } = {},
): WebSocketWrapper {
let firstConnection = true;
let closed = false;
Expand All @@ -71,7 +71,7 @@ export function initWebSocket(
function onOpen() {
if (firstConnection) {
firstConnection = false;
console.info("[Bun] Hot-module-reloading socket connected, waiting for changes...");
console.info(`[Bun] ${displayMessage} connected, waiting for changes...`);
}
}

Expand Down
24 changes: 17 additions & 7 deletions src/bake/hmr-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export class HotModule<E = any> {
: (mod._ext_exports ??= { ...(typeof exports === "object" && exports), default: exports });

if (expectedImports && mod._state === State.Ready) {
// for (const key of expectedImports) {
// if (!(key in object)) {
// throw new SyntaxError(`The requested module '${id}' does not provide an export named '${key}'`);
// }
// }
for (const key of expectedImports) {
if (!(key in object)) {
throw new SyntaxError(`The requested module '${id}' does not provide an export named '${key}'`);
}
}
}
return object;
}
Expand Down Expand Up @@ -281,6 +281,7 @@ export function replaceModule(key: Id, load: ModuleLoadFunction): Promise<void>
}

export async function replaceModules(modules: any) {
let needsHardReload = false;
for (const k in modules) {
input_graph[k] = modules[k];
const mod = registry.get(k);
Expand All @@ -289,6 +290,13 @@ export async function replaceModules(modules: any) {
mod._state = State.Replacing;
mod.exports = {};
mod._ext_exports = undefined;
if (side === "client" && !config.refresh && !needsHardReload) {
// TODO: import meta hot
needsHardReload = true;
console.info("[Bun] Reloading because there was not an `import.meta.hot.accept` boundary");
location.reload();
return;
}
}
}
const promises: Promise<void>[] = [];
Expand All @@ -310,8 +318,10 @@ export async function replaceModules(modules: any) {
console.error(err);
}
}
if (side === "client" && refreshRuntime) {
refreshRuntime.performReactRefresh(window);
if (side === "client") {
if (refreshRuntime) {
refreshRuntime.performReactRefresh(window);
}
}
}

Expand Down
Loading

0 comments on commit 063ecfa

Please sign in to comment.