forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrare_data.zig
393 lines (332 loc) · 12.2 KB
/
rare_data.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const EditorContext = @import("../open.zig").EditorContext;
const Blob = JSC.WebCore.Blob;
const default_allocator = @import("root").bun.default_allocator;
const Output = @import("root").bun.Output;
const RareData = @This();
const Syscall = bun.sys;
const JSC = @import("root").bun.JSC;
const std = @import("std");
const BoringSSL = @import("root").bun.BoringSSL;
const bun = @import("root").bun;
const FDImpl = bun.FDImpl;
const Environment = bun.Environment;
const WebSocketClientMask = @import("../http/websocket_http_client.zig").Mask;
const UUID = @import("./uuid.zig");
const Async = bun.Async;
const StatWatcherScheduler = @import("./node/node_fs_stat_watcher.zig").StatWatcherScheduler;
const IPC = @import("./ipc.zig");
const uws = @import("root").bun.uws;
boring_ssl_engine: ?*BoringSSL.ENGINE = null,
editor_context: EditorContext = EditorContext{},
stderr_store: ?*Blob.Store = null,
stdin_store: ?*Blob.Store = null,
stdout_store: ?*Blob.Store = null,
entropy_cache: ?*EntropyCache = null,
hot_map: ?HotMap = null,
// TODO: make this per JSGlobalObject instead of global
// This does not handle ShadowRealm correctly!
tail_cleanup_hook: ?*CleanupHook = null,
cleanup_hook: ?*CleanupHook = null,
file_polls_: ?*Async.FilePoll.Store = null,
global_dns_data: ?*JSC.DNS.GlobalData = null,
spawn_ipc_usockets_context: ?*uws.SocketContext = null,
mime_types: ?bun.http.MimeType.Map = null,
node_fs_stat_watcher_scheduler: ?*StatWatcherScheduler = null,
listening_sockets_for_watch_mode: std.ArrayListUnmanaged(bun.FileDescriptor) = .{},
listening_sockets_for_watch_mode_lock: bun.Lock = bun.Lock.init(),
temp_pipe_read_buffer: ?*PipeReadBuffer = null,
const PipeReadBuffer = [256 * 1024]u8;
pub fn pipeReadBuffer(this: *RareData) *PipeReadBuffer {
return this.temp_pipe_read_buffer orelse {
this.temp_pipe_read_buffer = default_allocator.create(PipeReadBuffer) catch bun.outOfMemory();
return this.temp_pipe_read_buffer.?;
};
}
pub fn addListeningSocketForWatchMode(this: *RareData, socket: bun.FileDescriptor) void {
this.listening_sockets_for_watch_mode_lock.lock();
defer this.listening_sockets_for_watch_mode_lock.unlock();
this.listening_sockets_for_watch_mode.append(bun.default_allocator, socket) catch {};
}
pub fn removeListeningSocketForWatchMode(this: *RareData, socket: bun.FileDescriptor) void {
this.listening_sockets_for_watch_mode_lock.lock();
defer this.listening_sockets_for_watch_mode_lock.unlock();
if (std.mem.indexOfScalar(bun.FileDescriptor, this.listening_sockets_for_watch_mode.items, socket)) |i| {
_ = this.listening_sockets_for_watch_mode.swapRemove(i);
}
}
pub fn closeAllListenSocketsForWatchMode(this: *RareData) void {
this.listening_sockets_for_watch_mode_lock.lock();
defer this.listening_sockets_for_watch_mode_lock.unlock();
for (this.listening_sockets_for_watch_mode.items) |socket| {
_ = Syscall.close(socket);
}
this.listening_sockets_for_watch_mode = .{};
}
pub fn hotMap(this: *RareData, allocator: std.mem.Allocator) *HotMap {
if (this.hot_map == null) {
this.hot_map = HotMap.init(allocator);
}
return &this.hot_map.?;
}
pub fn mimeTypeFromString(this: *RareData, allocator: std.mem.Allocator, str: []const u8) ?bun.http.MimeType {
if (this.mime_types == null) {
this.mime_types = bun.http.MimeType.createHashTable(
allocator,
) catch @panic("Out of memory");
}
return this.mime_types.?.get(str);
}
pub const HotMap = struct {
_map: bun.StringArrayHashMap(Entry),
const HTTPServer = JSC.API.HTTPServer;
const HTTPSServer = JSC.API.HTTPSServer;
const DebugHTTPServer = JSC.API.DebugHTTPServer;
const DebugHTTPSServer = JSC.API.DebugHTTPSServer;
const TCPSocket = JSC.API.TCPSocket;
const TLSSocket = JSC.API.TLSSocket;
const Listener = JSC.API.Listener;
const Entry = bun.TaggedPointerUnion(.{
HTTPServer,
HTTPSServer,
DebugHTTPServer,
DebugHTTPSServer,
TCPSocket,
TLSSocket,
Listener,
});
pub fn init(allocator: std.mem.Allocator) HotMap {
return .{
._map = bun.StringArrayHashMap(Entry).init(allocator),
};
}
pub fn get(this: *HotMap, key: []const u8, comptime Type: type) ?*Type {
var entry = this._map.get(key) orelse return null;
return entry.get(Type);
}
pub fn getEntry(this: *HotMap, key: []const u8) ?Entry {
return this._map.get(key) orelse return null;
}
pub fn insert(this: *HotMap, key: []const u8, ptr: anytype) void {
const entry = this._map.getOrPut(key) catch @panic("Out of memory");
if (entry.found_existing) {
@panic("HotMap already contains key");
}
entry.key_ptr.* = this._map.allocator.dupe(u8, key) catch @panic("Out of memory");
entry.value_ptr.* = Entry.init(ptr);
}
pub fn remove(this: *HotMap, key: []const u8) void {
const entry = this._map.getEntry(key) orelse return;
bun.default_allocator.free(entry.key_ptr.*);
_ = this._map.orderedRemove(key);
}
};
pub fn filePolls(this: *RareData, vm: *JSC.VirtualMachine) *Async.FilePoll.Store {
return this.file_polls_ orelse {
this.file_polls_ = vm.allocator.create(Async.FilePoll.Store) catch unreachable;
this.file_polls_.?.* = Async.FilePoll.Store.init(vm.allocator);
return this.file_polls_.?;
};
}
pub fn nextUUID(this: *RareData) UUID {
if (this.entropy_cache == null) {
this.entropy_cache = default_allocator.create(EntropyCache) catch unreachable;
this.entropy_cache.?.init();
}
const bytes = this.entropy_cache.?.get();
return UUID.initWith(&bytes);
}
pub fn entropySlice(this: *RareData, len: usize) []u8 {
if (this.entropy_cache == null) {
this.entropy_cache = default_allocator.create(EntropyCache) catch unreachable;
this.entropy_cache.?.init();
}
return this.entropy_cache.?.slice(len);
}
pub const EntropyCache = struct {
pub const buffered_uuids_count = 16;
pub const size = buffered_uuids_count * 128;
cache: [size]u8 = undefined,
index: usize = 0,
pub fn init(instance: *EntropyCache) void {
instance.fill();
}
pub fn fill(this: *EntropyCache) void {
bun.rand(&this.cache);
this.index = 0;
}
pub fn slice(this: *EntropyCache, len: usize) []u8 {
if (len > this.cache.len) {
return &[_]u8{};
}
if (this.index + len > this.cache.len) {
this.fill();
}
const result = this.cache[this.index..][0..len];
this.index += len;
return result;
}
pub fn get(this: *EntropyCache) [16]u8 {
if (this.index + 16 > this.cache.len) {
this.fill();
}
const result = this.cache[this.index..][0..16].*;
this.index += 16;
return result;
}
};
pub const CleanupHook = struct {
next: ?*CleanupHook = null,
ctx: ?*anyopaque,
func: Function,
globalThis: *JSC.JSGlobalObject,
pub fn eql(self: CleanupHook, other: CleanupHook) bool {
return self.ctx == other.ctx and self.func == other.func and self.globalThis == other.globalThis;
}
pub fn execute(self: CleanupHook) void {
self.func(self.ctx);
}
pub fn from(
globalThis: *JSC.JSGlobalObject,
ctx: ?*anyopaque,
func: CleanupHook.Function,
) CleanupHook {
return .{
.next = null,
.ctx = ctx,
.func = func,
.globalThis = globalThis,
};
}
pub const Function = *const fn (?*anyopaque) callconv(.C) void;
};
pub fn pushCleanupHook(
this: *RareData,
globalThis: *JSC.JSGlobalObject,
ctx: ?*anyopaque,
func: CleanupHook.Function,
) void {
const hook = JSC.VirtualMachine.get().allocator.create(CleanupHook) catch unreachable;
hook.* = CleanupHook.from(globalThis, ctx, func);
if (this.cleanup_hook == null) {
this.cleanup_hook = hook;
this.tail_cleanup_hook = hook;
} else {
this.cleanup_hook.?.next = hook;
}
}
pub fn boringEngine(rare: *RareData) *BoringSSL.ENGINE {
return rare.boring_ssl_engine orelse brk: {
rare.boring_ssl_engine = BoringSSL.ENGINE_new();
break :brk rare.boring_ssl_engine.?;
};
}
pub fn stderr(rare: *RareData) *Blob.Store {
bun.Analytics.Features.@"Bun.stderr" += 1;
return rare.stderr_store orelse brk: {
const store = default_allocator.create(Blob.Store) catch unreachable;
var mode: bun.Mode = 0;
const fd = if (Environment.isWindows) FDImpl.fromUV(2).encode() else bun.STDERR_FD;
switch (Syscall.fstat(fd)) {
.result => |stat| {
mode = @intCast(stat.mode);
},
.err => {},
}
store.* = Blob.Store{
.ref_count = 2,
.allocator = default_allocator,
.data = .{
.file = Blob.FileStore{
.pathlike = .{
.fd = fd,
},
.is_atty = Output.stderr_descriptor_type == .terminal,
.mode = mode,
},
},
};
rare.stderr_store = store;
break :brk store;
};
}
pub fn stdout(rare: *RareData) *Blob.Store {
bun.Analytics.Features.@"Bun.stdout" += 1;
return rare.stdout_store orelse brk: {
const store = default_allocator.create(Blob.Store) catch unreachable;
var mode: bun.Mode = 0;
const fd = if (Environment.isWindows) FDImpl.fromUV(1).encode() else bun.STDOUT_FD;
switch (Syscall.fstat(fd)) {
.result => |stat| {
mode = @intCast(stat.mode);
},
.err => {},
}
store.* = Blob.Store{
.ref_count = 2,
.allocator = default_allocator,
.data = .{
.file = Blob.FileStore{
.pathlike = .{
.fd = fd,
},
.is_atty = Output.stdout_descriptor_type == .terminal,
.mode = mode,
},
},
};
rare.stdout_store = store;
break :brk store;
};
}
pub fn stdin(rare: *RareData) *Blob.Store {
bun.Analytics.Features.@"Bun.stdin" += 1;
return rare.stdin_store orelse brk: {
const store = default_allocator.create(Blob.Store) catch unreachable;
var mode: bun.Mode = 0;
const fd = if (Environment.isWindows) FDImpl.fromUV(0).encode() else bun.STDIN_FD;
switch (Syscall.fstat(fd)) {
.result => |stat| {
mode = @intCast(stat.mode);
},
.err => {},
}
store.* = Blob.Store{
.allocator = default_allocator,
.ref_count = 2,
.data = .{
.file = Blob.FileStore{
.pathlike = .{
.fd = fd,
},
.is_atty = if (bun.STDIN_FD.isValid()) std.os.isatty(bun.STDIN_FD.cast()) else false,
.mode = mode,
},
},
};
rare.stdin_store = store;
break :brk store;
};
}
const Subprocess = @import("./api/bun/subprocess.zig").Subprocess;
pub fn spawnIPCContext(rare: *RareData, vm: *JSC.VirtualMachine) *uws.SocketContext {
if (rare.spawn_ipc_usockets_context) |ctx| {
return ctx;
}
const opts: uws.us_socket_context_options_t = .{};
const ctx = uws.us_create_socket_context(0, vm.event_loop_handle.?, @sizeOf(usize), opts).?;
IPC.Socket.configure(ctx, true, *Subprocess, Subprocess.IPCHandler);
rare.spawn_ipc_usockets_context = ctx;
return ctx;
}
pub fn globalDNSResolver(rare: *RareData, vm: *JSC.VirtualMachine) *JSC.DNS.DNSResolver {
if (rare.global_dns_data == null) {
rare.global_dns_data = JSC.DNS.GlobalData.init(vm.allocator, vm);
}
return &rare.global_dns_data.?.resolver;
}
pub fn nodeFSStatWatcherScheduler(rare: *RareData, vm: *JSC.VirtualMachine) *StatWatcherScheduler {
return rare.node_fs_stat_watcher_scheduler orelse {
rare.node_fs_stat_watcher_scheduler = StatWatcherScheduler.init(vm.allocator, vm);
return rare.node_fs_stat_watcher_scheduler.?;
};
}