[bun.js] 1/? Implement Response.file

This commit is contained in:
Jarred Sumner
2022-03-21 02:21:51 -07:00
parent 1f93de264f
commit fa343fa8ad
13 changed files with 1108 additions and 108 deletions

View File

@@ -297,12 +297,14 @@ pub fn IOTask(comptime Context: type) type {
const AsyncTransformTask = @import("./api/transpiler.zig").TransformTask.AsyncTransformTask;
const BunTimerTimeoutTask = Bun.Timer.Timeout.TimeoutTask;
const ReadFileTask = WebCore.Blob.Store.ReadFile.ReadFileTask;
// const PromiseTask = JSInternalPromise.Completion.PromiseTask;
pub const Task = TaggedPointerUnion(.{
FetchTasklet,
Microtask,
AsyncTransformTask,
BunTimerTimeoutTask,
ReadFileTask,
// PromiseTask,
// TimeoutTasklet,
});
@@ -478,6 +480,7 @@ pub const VirtualMachine = struct {
event_loop: *EventLoop = undefined,
ref_strings: JSC.RefString.Map = undefined,
file_blobs: JSC.WebCore.Blob.Store.Map,
source_mappings: SavedSourceMap = undefined,
response_objects_pool: ?*Response.Pool = null,
@@ -525,6 +528,11 @@ pub const VirtualMachine = struct {
transform_task.*.runFromJS();
finished += 1;
},
@field(Task.Tag, @typeName(ReadFileTask)) => {
var transform_task: *ReadFileTask = task.get(ReadFileTask).?;
transform_task.*.runFromJS();
finished += 1;
},
else => unreachable,
}
}
@@ -732,6 +740,7 @@ pub const VirtualMachine = struct {
.macro_entry_points = @TypeOf(VirtualMachine.vm.macro_entry_points).init(allocator),
.origin_timer = std.time.Timer.start() catch @panic("Please don't mess with timers."),
.ref_strings = JSC.RefString.Map.init(allocator),
.file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator),
};
VirtualMachine.vm.regular_event_loop.tasks = EventLoop.Queue.init(
default_allocator,
@@ -784,6 +793,21 @@ pub const VirtualMachine = struct {
_ = VirtualMachine.vm.ref_strings.remove(ref_string.hash);
}
pub fn getFileBlob(this: *VirtualMachine, pathlike: JSC.Node.PathOrFileDescriptor) ?*JSC.WebCore.Blob.Store {
const hash = pathlike.hash();
return this.file_blobs.get(hash);
}
pub fn putFileBlob(this: *VirtualMachine, pathlike: JSC.Node.PathOrFileDescriptor, store: *JSC.WebCore.Blob.Store) !void {
const hash = pathlike.hash();
try this.file_blobs.put(hash, store);
}
pub fn removeFileBlob(this: *VirtualMachine, pathlike: JSC.Node.PathOrFileDescriptor) void {
const hash = pathlike.hash();
_ = this.file_blobs.remove(hash);
}
pub fn refCountedResolvedSource(this: *VirtualMachine, code: []const u8, specifier: []const u8, source_url: []const u8, hash_: ?u32) ResolvedSource {
var source = this.refCountedString(code, hash_, true);