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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
pub usingnamespace @import("./webcore/response.zig");
pub usingnamespace @import("./webcore/encoding.zig");
pub usingnamespace @import("./webcore/streams.zig");
const JSC = @import("../jsc.zig");
const std = @import("std");
const bun = @import("../global.zig");
pub const Lifetime = enum {
clone,
transfer,
share,
/// When reading from a fifo like STDIN/STDERR
temporary,
};
pub const Alert = struct {
pub const Class = JSC.NewClass(
void,
.{ .name = "alert" },
.{
.@"call" = .{ .rfn = call },
},
.{},
);
/// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-alert
pub fn call(
// this
_: void,
ctx: JSC.C.JSContextRef,
// function
_: JSC.C.JSObjectRef,
// thisObject
_: JSC.C.JSObjectRef,
arguments: []const JSC.C.JSValueRef,
_: JSC.C.ExceptionRef,
) JSC.C.JSValueRef {
var output = bun.Output.writer();
const has_message = arguments.len != 0;
// 2. If the method was invoked with no arguments, then let message be the empty string; otherwise, let message be the method's first argument.
if (has_message) {
const allocator = std.heap.stackFallback(2048, bun.default_allocator).get();
const message = arguments[0].?.value().toSlice(ctx.ptr(), allocator);
defer message.deinit();
// 3. Set message to the result of normalizing newlines given message.
// * We skip step 3 because they are already done in most terminals by default.
// 4. Set message to the result of optionally truncating message.
// * We just don't do this because it's not necessary.
// 5. Show message to the user, treating U+000A LF as a line break.
output.writeAll(message.slice()) catch {
// 1. If we cannot show simple dialogs for this, then return.
return JSC.JSValue.jsUndefined().asObjectRef();
};
}
output.writeAll(if (has_message) " [Enter] " else "Alert [Enter] ") catch {
// 1. If we cannot show simple dialogs for this, then return.
return JSC.JSValue.jsUndefined().asObjectRef();
};
// 6. Invoke WebDriver BiDi user prompt opened with this, "alert", and message.
// * Not pertinent to use their complex system in a server context.
bun.Output.flush();
// 7. Optionally, pause while waiting for the user to acknowledge the message.
var stdin = std.io.getStdIn();
var reader = stdin.reader();
while (true) {
const byte = reader.readByte() catch break;
if (byte == '\n') break;
}
// 8. Invoke WebDriver BiDi user prompt closed with this and true.
// * Again, not necessary in a server context.
return JSC.JSValue.jsUndefined().asObjectRef();
}
};
pub const Confirm = struct {
pub const Class = JSC.NewClass(
void,
.{ .name = "confirm" },
.{
.@"call" = .{ .rfn = call },
},
.{},
);
/// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-confirm
pub fn call(
// this
_: void,
ctx: JSC.C.JSContextRef,
// function
_: JSC.C.JSObjectRef,
// thisObject
_: JSC.C.JSObjectRef,
arguments: []const JSC.C.JSValueRef,
_: JSC.C.ExceptionRef,
) JSC.C.JSValueRef {
var output = bun.Output.writer();
const has_message = arguments.len != 0;
if (has_message) {
const allocator = std.heap.stackFallback(1024, bun.default_allocator).get();
// 2. Set message to the result of normalizing newlines given message.
// * Not pertinent to a server runtime so we will just let the terminal handle this.
// 3. Set message to the result of optionally truncating message.
// * Not necessary so we won't do it.
const message = arguments[0].?.value().toSlice(ctx.ptr(), allocator);
defer message.deinit();
output.writeAll(message.slice()) catch {
// 1. If we cannot show simple dialogs for this, then return false.
return JSC.JSValue.jsBoolean(false).asObjectRef();
};
}
// 4. Show message to the user, treating U+000A LF as a line break,
// and ask the user to respond with a positive or negative
// response.
output.writeAll(if (has_message) " [y/N] " else "Confirm [y/N] ") catch {
// 1. If we cannot show simple dialogs for this, then return false.
return JSC.JSValue.jsBoolean(false).asObjectRef();
};
// 5. Invoke WebDriver BiDi user prompt opened with this, "confirm", and message.
// * Not relevant in a server context.
bun.Output.flush();
// 6. Pause until the user responds either positively or negatively.
var stdin = std.io.getStdIn();
var reader = stdin.reader();
const first_byte = reader.readByte() catch {
return JSC.JSValue.jsBoolean(false).asObjectRef();
};
// 7. Invoke WebDriver BiDi user prompt closed with this, and true if
// the user responded positively or false otherwise.
// * Not relevant in a server context.
switch (first_byte) {
'\n' => return JSC.JSValue.jsBoolean(false).asObjectRef(),
'y', 'Y' => {
const next_byte = reader.readByte() catch {
// They may have said yes, but the stdin is invalid.
return JSC.JSValue.jsBoolean(false).asObjectRef();
};
if (next_byte == '\n') {
// 8. If the user responded positively, return true;
// otherwise, the user responded negatively: return false.
return JSC.JSValue.jsBoolean(true).asObjectRef();
}
},
else => {},
}
while (reader.readByte()) |b| {
if (b == '\n') break;
} else |_| {}
// 8. If the user responded positively, return true; otherwise, the user
// responded negatively: return false.
return JSC.JSValue.jsBoolean(false).asObjectRef();
}
};
pub const Prompt = struct {
pub const Class = JSC.NewClass(
void,
.{ .name = "prompt" },
.{
.@"call" = .{ .rfn = call },
},
.{},
);
/// Adapted from `std.io.Reader.readUntilDelimiterArrayList` to only append
/// and assume capacity.
pub fn readUntilDelimiterArrayListAppendAssumeCapacity(
reader: anytype,
array_list: *std.ArrayList(u8),
delimiter: u8,
max_size: usize,
) !void {
while (true) {
if (array_list.items.len == max_size) {
return error.StreamTooLong;
}
var byte: u8 = try reader.readByte();
if (byte == delimiter) {
return;
}
array_list.appendAssumeCapacity(byte);
}
}
/// Adapted from `std.io.Reader.readUntilDelimiterArrayList` to always append
/// and not resize.
fn readUntilDelimiterArrayListInfinity(
reader: anytype,
array_list: *std.ArrayList(u8),
delimiter: u8,
) !void {
while (true) {
var byte: u8 = try reader.readByte();
if (byte == delimiter) {
return;
}
try array_list.append(byte);
}
}
/// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-prompt
pub fn call(
// this
_: void,
ctx: JSC.C.JSContextRef,
// function
_: JSC.C.JSObjectRef,
// thisObject
_: JSC.C.JSObjectRef,
arguments: []const JSC.C.JSValueRef,
_: JSC.C.ExceptionRef,
) JSC.C.JSValueRef {
var allocator = std.heap.stackFallback(2048, bun.default_allocator).get();
var output = bun.Output.writer();
const has_message = arguments.len != 0;
const has_default = arguments.len >= 2;
// 4. Set default to the result of optionally truncating default.
// * We don't really need to do this.
const default = if (has_default) arguments[1] else JSC.JSValue.jsNull().asObjectRef();
if (has_message) {
// 2. Set message to the result of normalizing newlines given message.
// * Not pertinent to a server runtime so we will just let the terminal handle this.
// 3. Set message to the result of optionally truncating message.
// * Not necessary so we won't do it.
const message = arguments[0].?.value().toSlice(ctx.ptr(), allocator);
defer message.deinit();
output.writeAll(message.slice()) catch {
// 1. If we cannot show simple dialogs for this, then return null.
return JSC.JSValue.jsNull().asObjectRef();
};
}
// 4. Set default to the result of optionally truncating default.
// 5. Show message to the user, treating U+000A LF as a line break,
// and ask the user to either respond with a string value or
// abort. The response must be defaulted to the value given by
// default.
output.writeAll(if (has_message) " " else "Prompt ") catch {
// 1. If we cannot show simple dialogs for this, then return false.
return JSC.JSValue.jsBoolean(false).asObjectRef();
};
if (has_default) {
const default_string = arguments[1].?.value().toSlice(ctx.ptr(), allocator);
defer default_string.deinit();
output.print("[{s}] ", .{default_string.slice()}) catch {
// 1. If we cannot show simple dialogs for this, then return false.
return JSC.JSValue.jsBoolean(false).asObjectRef();
};
}
// 6. Invoke WebDriver BiDi user prompt opened with this, "prompt" and message.
// * Not relevant in a server context.
bun.Output.flush();
// 7. Pause while waiting for the user's response.
var stdin = std.io.getStdIn();
var reader = stdin.reader();
const first_byte = reader.readByte() catch {
// 8. Let result be null if the user aborts, or otherwise the string
// that the user responded with.
return JSC.JSValue.jsNull().asObjectRef();
};
if (first_byte == '\n') {
// 8. Let result be null if the user aborts, or otherwise the string
// that the user responded with.
return default;
}
var input = std.ArrayList(u8).initCapacity(allocator, 2048) catch {
// 8. Let result be null if the user aborts, or otherwise the string
// that the user responded with.
return JSC.JSValue.jsNull().asObjectRef();
};
defer input.deinit();
input.appendAssumeCapacity(first_byte);
// All of this code basically just first tries to load the input into a
// buffer of size 2048. If that is too small, then increase the buffer
// size to 4096. If that is too small, then just dynamically allocate
// the rest.
readUntilDelimiterArrayListAppendAssumeCapacity(reader, &input, '\n', 2048) catch |e| {
if (e != error.StreamTooLong) {
// 8. Let result be null if the user aborts, or otherwise the string
// that the user responded with.
return JSC.JSValue.jsNull().asObjectRef();
}
input.ensureTotalCapacity(4096) catch {
// 8. Let result be null if the user aborts, or otherwise the string
// that the user responded with.
return JSC.JSValue.jsNull().asObjectRef();
};
readUntilDelimiterArrayListAppendAssumeCapacity(reader, &input, '\n', 4096) catch |e2| {
if (e2 != error.StreamTooLong) {
// 8. Let result be null if the user aborts, or otherwise the string
// that the user responded with.
return JSC.JSValue.jsNull().asObjectRef();
}
readUntilDelimiterArrayListInfinity(reader, &input, '\n') catch {
// 8. Let result be null if the user aborts, or otherwise the string
// that the user responded with.
return JSC.JSValue.jsNull().asObjectRef();
};
};
};
// 8. Let result be null if the user aborts, or otherwise the string
// that the user responded with.
var result = JSC.ZigString.init(input.items);
result.markUTF8();
// 9. Invoke WebDriver BiDi user prompt closed with this, false if
// result is null or true otherwise, and result.
// * Too complex for server context.
// 9. Return result.
return result.toValueGC(ctx.ptr()).asObjectRef();
}
};
pub const Crypto = struct {
const UUID = @import("./uuid.zig");
const BoringSSL = @import("boringssl");
pub const Class = JSC.NewClass(
void,
.{ .name = "crypto" },
.{
.getRandomValues = JSC.DOMCall("Crypto", @This(), "getRandomValues", JSC.JSValue, JSC.DOMEffect.top),
.randomUUID = JSC.DOMCall("Crypto", @This(), "randomUUID", *JSC.JSString, JSC.DOMEffect.top),
},
.{},
);
pub const Prototype = JSC.NewClass(
void,
.{ .name = "Crypto" },
.{
.call = .{
.rfn = call,
},
},
.{},
);
pub fn getRandomValues(
globalThis: *JSC.JSGlobalObject,
_: JSC.JSValue,
arguments: []const JSC.JSValue,
) JSC.JSValue {
if (arguments.len == 0) {
globalThis.throwInvalidArguments("Expected typed array but got nothing", .{});
return JSC.JSValue.jsUndefined();
}
var array_buffer = arguments[0].asArrayBuffer(globalThis) orelse {
globalThis.throwInvalidArguments("Expected typed array but got {s}", .{@tagName(arguments[0].jsType())});
return JSC.JSValue.jsUndefined();
};
var slice = array_buffer.byteSlice();
randomData(globalThis, slice.ptr, slice.len);
return arguments[0];
}
pub fn getRandomValuesWithoutTypeChecks(
globalThis: *JSC.JSGlobalObject,
_: *anyopaque,
array: *JSC.JSUint8Array,
) callconv(.C) JSC.JSValue {
var slice = array.slice();
randomData(globalThis, slice.ptr, slice.len);
return @intToEnum(JSC.JSValue, @bitCast(i64, @ptrToInt(array)));
}
fn randomData(
globalThis: *JSC.JSGlobalObject,
ptr: [*]u8,
len: usize,
) void {
var slice = ptr[0..len];
switch (slice.len) {
0 => {},
// 512 bytes or less we reuse from the same cache as UUID generation.
1...JSC.RareData.EntropyCache.size / 8 => {
std.mem.copy(u8, slice, globalThis.bunVM().rareData().entropySlice(slice.len));
},
else => {
bun.rand(slice);
},
}
}
pub fn randomUUID(
globalThis: *JSC.JSGlobalObject,
_: JSC.JSValue,
_: []const JSC.JSValue,
) JSC.JSValue {
var out: [36]u8 = undefined;
const uuid: UUID = .{
.bytes = globalThis.bunVM().rareData().nextUUID(),
};
uuid.print(&out);
return JSC.ZigString.init(&out).toValueGC(globalThis);
}
pub fn randomUUIDWithoutTypeChecks(
globalThis: *JSC.JSGlobalObject,
_: *anyopaque,
) callconv(.C) JSC.JSValue {
var out: [36]u8 = undefined;
const uuid: UUID = .{
.bytes = globalThis.bunVM().rareData().nextUUID(),
};
uuid.print(&out);
return JSC.ZigString.init(&out).toValueGC(globalThis);
}
pub fn call(
// this
_: void,
_: JSC.C.JSContextRef,
// function
_: JSC.C.JSObjectRef,
// thisObject
_: JSC.C.JSObjectRef,
_: []const JSC.C.JSValueRef,
_: JSC.C.ExceptionRef,
) JSC.C.JSValueRef {
return JSC.JSValue.jsUndefined().asObjectRef();
}
};
|