aboutsummaryrefslogtreecommitdiff
path: root/src/node_module_bundle.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-07-30 00:50:29 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-07-30 00:50:29 -0700
commit7245f90b2dd686acacb9c8ee03f5d8fb05e09aeb (patch)
tree120d6d093ba609f93f1514fde16c0a3402855336 /src/node_module_bundle.zig
parentba743d776a6a417bdf42755b62a7868b15ebfe73 (diff)
downloadbun-7245f90b2dd686acacb9c8ee03f5d8fb05e09aeb.tar.gz
bun-7245f90b2dd686acacb9c8ee03f5d8fb05e09aeb.tar.zst
bun-7245f90b2dd686acacb9c8ee03f5d8fb05e09aeb.zip
little bit of errors, little bit of bytecode caching. neither finished
Former-commit-id: c774c395136d58330aa7cad7e9fa434bcef7d5c6
Diffstat (limited to 'src/node_module_bundle.zig')
-rw-r--r--src/node_module_bundle.zig36
1 files changed, 33 insertions, 3 deletions
diff --git a/src/node_module_bundle.zig b/src/node_module_bundle.zig
index 98b9067c0..94bbe5258 100644
--- a/src/node_module_bundle.zig
+++ b/src/node_module_bundle.zig
@@ -1,6 +1,7 @@
const schema = @import("./api/schema.zig");
const Api = schema.Api;
const std = @import("std");
+const Fs = @import("./fs.zig");
usingnamespace @import("global.zig");
pub fn modulesIn(bundle: *const Api.JavascriptBundle, pkg: *const Api.JavascriptBundledPackage) []const Api.JavascriptBundledModule {
@@ -16,13 +17,20 @@ const PackageIDMap = std.AutoHashMap(BundledPackageHash, BundledPackageID);
const PackageNameMap = std.StringHashMap([]BundledPackageID);
+pub const AllocatedString = struct {
+ str: string,
+ len: u32,
+ allocator: *std.mem.Allocator,
+};
+
pub const NodeModuleBundle = struct {
container: Api.JavascriptBundleContainer,
bundle: Api.JavascriptBundle,
allocator: *std.mem.Allocator,
bytes_ptr: []u8 = undefined,
- bytes: []u8 = undefined,
+ bytes: []u8 = &[_]u8{},
fd: FileDescriptorType = 0,
+ code_end_pos: u32 = 0,
// Lookup packages by ID - hash(name@version)
package_id_map: PackageIDMap,
@@ -33,6 +41,10 @@ pub const NodeModuleBundle = struct {
// This is stored as a single pre-allocated, flat array so we can avoid dynamic allocations.
package_name_ids_ptr: []BundledPackageID = &([_]BundledPackageID{}),
+ code_string: ?AllocatedString = null,
+
+ bytecode_cache_fetcher: Fs.BytecodeCacheFetcher = Fs.BytecodeCacheFetcher{},
+
pub const magic_bytes = "#!/usr/bin/env speedy\n\n";
threadlocal var jsbundle_prefix: [magic_bytes.len + 5]u8 = undefined;
@@ -41,6 +53,23 @@ pub const NodeModuleBundle = struct {
return this.package_name_map.contains("react-refresh");
}
+ pub inline fn fetchByteCodeCache(this: *NodeModuleBundle, basename: string, fs: *Fs.FileSystem.RealFS) ?StoredFileDescriptorType {
+ return this.bytecode_cache_fetcher.fetch(basename, fs);
+ }
+
+ pub fn readCodeAsStringSlow(this: *NodeModuleBundle, allocator: *std.mem.Allocator) !string {
+ if (this.code_string) |code| {
+ return code.str;
+ }
+
+ var file = std.fs.File{ .handle = this.fd };
+
+ var buf = try allocator.alloc(u8, this.code_end_pos);
+ const count = try file.preadAll(buf, this.codeStartOffset());
+ this.code_string = AllocatedString{ .str = buf[0..count], .len = @truncate(u32, buf.len), .allocator = allocator };
+ return this.code_string.?.str;
+ }
+
pub fn loadPackageMap(this: *NodeModuleBundle) !void {
this.package_name_map = PackageNameMap.init(this.allocator);
this.package_id_map = PackageIDMap.init(this.allocator);
@@ -268,12 +297,13 @@ pub const NodeModuleBundle = struct {
var read_bytes = file_bytes[0..read_count];
var reader = schema.Reader.init(read_bytes, allocator);
var container = try Api.JavascriptBundleContainer.decode(&reader);
-
var bundle = NodeModuleBundle{
.allocator = allocator,
.container = container,
.bundle = container.bundle.?,
.fd = stream.handle,
+ // sorry you can't have 4 GB of node_modules
+ .code_end_pos = @truncate(u32, file_end) - @intCast(u32, jsbundle_prefix.len),
.bytes = read_bytes,
.bytes_ptr = file_bytes,
.package_id_map = undefined,
@@ -346,7 +376,7 @@ pub const NodeModuleBundle = struct {
Output.prettyln(indent ++ "<b>{d:6} packages", .{this.bundle.packages.len});
}
- pub fn codeStartOffset(this: *const NodeModuleBundle) u32 {
+ pub inline fn codeStartOffset(this: *const NodeModuleBundle) u32 {
return @intCast(u32, jsbundle_prefix.len);
}