mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 03:18:53 +00:00
134
src/javascript/jsc/api/router.zig
Normal file
134
src/javascript/jsc/api/router.zig
Normal file
@@ -0,0 +1,134 @@
|
||||
usingnamespace @import("../base.zig");
|
||||
const std = @import("std");
|
||||
const Api = @import("../../../api/schema.zig").Api;
|
||||
const FilesystemRouter = @import("../../../router.zig");
|
||||
const JavaScript = @import("../javascript.zig");
|
||||
|
||||
pub const Router = struct {
|
||||
match: FilesystemRouter.RouteMap.MatchedRoute,
|
||||
file_path_str: js.JSStringRef = null,
|
||||
pathname_str: js.JSStringRef = null,
|
||||
|
||||
pub const Class = NewClass(
|
||||
Router,
|
||||
"Router",
|
||||
.{
|
||||
.finalize = finalize,
|
||||
},
|
||||
.{
|
||||
.@"pathname" = .{
|
||||
.get = getPathname,
|
||||
.ro = true,
|
||||
.ts = .{
|
||||
.@"return" = "string",
|
||||
.@"tsdoc" = "URL path as appears in a web browser's address bar",
|
||||
},
|
||||
},
|
||||
.@"filepath" = .{
|
||||
.get = getPageFilePath,
|
||||
.ro = true,
|
||||
.ts = .{
|
||||
.@"return" = "string",
|
||||
.@"tsdoc" =
|
||||
\\Project-relative filesystem path to the route file
|
||||
\\
|
||||
\\@example
|
||||
\\
|
||||
\\```tsx
|
||||
\\const PageComponent = (await import(route.filepath)).default;
|
||||
\\ReactDOMServer.renderToString(<PageComponent query={route.query} />);
|
||||
\\```
|
||||
,
|
||||
},
|
||||
},
|
||||
.@"route" = .{
|
||||
.@"get" = getRoute,
|
||||
.ro = true,
|
||||
},
|
||||
.query = .{
|
||||
.@"get" = getQuery,
|
||||
.ro = true,
|
||||
},
|
||||
.pageFilePath = .{
|
||||
.@"get" = getPageFilePath,
|
||||
.ro = true,
|
||||
},
|
||||
},
|
||||
false,
|
||||
false,
|
||||
);
|
||||
|
||||
pub fn getPageFilePath(
|
||||
this: *Router,
|
||||
ctx: js.JSContextRef,
|
||||
thisObject: js.JSObjectRef,
|
||||
prop: js.JSStringRef,
|
||||
exception: js.ExceptionRef,
|
||||
) js.JSValueRef {
|
||||
if (this.file_path_str == null) {
|
||||
this.file_path_str = js.JSStringCreateWithUTF8CString(this.match.file_path[0.. :0]);
|
||||
}
|
||||
|
||||
return js.JSValueMakeString(ctx, this.file_path_str);
|
||||
}
|
||||
|
||||
pub fn finalize(
|
||||
this: *Router,
|
||||
ctx: js.JSObjectRef,
|
||||
) void {
|
||||
// this.deinit();
|
||||
}
|
||||
|
||||
pub fn requirePage(
|
||||
this: *Router,
|
||||
ctx: js.JSContextRef,
|
||||
function: js.JSObjectRef,
|
||||
thisObject: js.JSObjectRef,
|
||||
arguments: []const js.JSValueRef,
|
||||
exception: js.ExceptionRef,
|
||||
) js.JSValueRef {}
|
||||
|
||||
pub fn getPathname(
|
||||
this: *Router,
|
||||
ctx: js.JSContextRef,
|
||||
thisObject: js.JSObjectRef,
|
||||
prop: js.JSStringRef,
|
||||
exception: js.ExceptionRef,
|
||||
) js.JSValueRef {
|
||||
if (this.pathname_str == null) {
|
||||
this.pathname_str = js.JSStringCreateWithUTF8CString(this.match.pathname[0.. :0]);
|
||||
}
|
||||
|
||||
return js.JSValueMakeString(ctx, this.pathname_str);
|
||||
}
|
||||
|
||||
pub fn getAsPath(
|
||||
this: *Router,
|
||||
ctx: js.JSContextRef,
|
||||
thisObject: js.JSObjectRef,
|
||||
prop: js.JSStringRef,
|
||||
exception: js.ExceptionRef,
|
||||
) js.JSValueRef {
|
||||
return js.JSValueMakeString(ctx, Properties.Refs.default);
|
||||
}
|
||||
|
||||
pub fn getRoute(
|
||||
this: *Router,
|
||||
ctx: js.JSContextRef,
|
||||
thisObject: js.JSObjectRef,
|
||||
prop: js.JSStringRef,
|
||||
exception: js.ExceptionRef,
|
||||
) js.JSValueRef {
|
||||
return js.JSValueMakeString(ctx, Properties.Refs.default);
|
||||
}
|
||||
|
||||
pub fn getQuery(
|
||||
this: *Router,
|
||||
ctx: js.JSContextRef,
|
||||
thisObject: js.JSObjectRef,
|
||||
prop: js.JSStringRef,
|
||||
exception: js.ExceptionRef,
|
||||
) js.JSValueRef {
|
||||
return js.JSValueMakeString(ctx, Properties.Refs.default);
|
||||
}
|
||||
};
|
||||
@@ -33,7 +33,7 @@ pub const To = struct {
|
||||
return function;
|
||||
}
|
||||
|
||||
pub fn Finalize(n
|
||||
pub fn Finalize(
|
||||
comptime ZigContextType: type,
|
||||
comptime ctxfn: fn (
|
||||
this: *ZigContextType,
|
||||
|
||||
@@ -768,6 +768,8 @@ pub const Module = struct {
|
||||
return null;
|
||||
}
|
||||
|
||||
var module = this;
|
||||
|
||||
var total_len: usize = 0;
|
||||
for (arguments) |argument| {
|
||||
const len = js.JSStringGetLength(argument);
|
||||
@@ -785,7 +787,6 @@ pub const Module = struct {
|
||||
const end = js.JSStringGetUTF8CString(argument, require_buf.list.items.ptr, require_buf.list.items.len);
|
||||
total_len += end;
|
||||
const import_path = require_buf.list.items[0 .. end - 1];
|
||||
var module = this;
|
||||
|
||||
if (this.vm.bundler.linker.resolver.resolve(module.path.name.dirWithTrailingSlash(), import_path, .require)) |resolved| {
|
||||
var load_result = Module.loadFromResolveResult(this.vm, ctx, resolved, exception) catch |err| {
|
||||
@@ -835,7 +836,7 @@ pub const Module = struct {
|
||||
for (arguments) |argument| {
|
||||
const end = js.JSStringGetUTF8CString(argument, remainder.ptr, total_len - used_len);
|
||||
used_len += end;
|
||||
remainder[end - 1] = ",";
|
||||
remainder[end - 1] = ',';
|
||||
remainder = remainder[end..];
|
||||
}
|
||||
|
||||
@@ -1126,7 +1127,7 @@ pub const Module = struct {
|
||||
}
|
||||
var module: *Module = undefined;
|
||||
|
||||
if (needs_reload) {
|
||||
if (reload_pending) {
|
||||
module = vm.require_cache.get(hash).?;
|
||||
} else {
|
||||
module = try vm.allocator.create(Module);
|
||||
@@ -1134,12 +1135,12 @@ pub const Module = struct {
|
||||
}
|
||||
|
||||
errdefer {
|
||||
if (!needs_reload) {
|
||||
if (!reload_pending) {
|
||||
vm.allocator.destroy(module);
|
||||
}
|
||||
}
|
||||
|
||||
if (needs_reload) {
|
||||
if (reload_pending) {
|
||||
try Module.load(
|
||||
module,
|
||||
vm,
|
||||
@@ -1345,7 +1346,7 @@ pub const EventListenerMixin = struct {
|
||||
fetch,
|
||||
err,
|
||||
|
||||
const SizeMatcher = strings.ExactSizeMatcher("fetch".len);
|
||||
const SizeMatcher = strings.ExactSizeMatcher(8);
|
||||
|
||||
pub fn match(str: string) ?EventType {
|
||||
return switch (SizeMatcher.match(str)) {
|
||||
|
||||
@@ -2,7 +2,7 @@ usingnamespace @import("../base.zig");
|
||||
const std = @import("std");
|
||||
const Api = @import("../../../api/schema.zig").Api;
|
||||
const http = @import("../../../http.zig");
|
||||
pub const JavaScript = @import("../javascript.zig");
|
||||
const JavaScript = @import("../javascript.zig");
|
||||
pub const Response = struct {
|
||||
pub const Class = NewClass(
|
||||
Response,
|
||||
@@ -1129,5 +1129,4 @@ pub const FetchEvent = struct {
|
||||
) js.JSValueRef {
|
||||
return js.JSValueMakeUndefined(ctx);
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user