quickjs 0.13.0.pre → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +94 -69
- data/Rakefile +7 -0
- data/ext/quickjsrb/extconf.rb +7 -0
- data/ext/quickjsrb/quickjsrb.c +177 -43
- data/ext/quickjsrb/quickjsrb.h +6 -4
- data/ext/quickjsrb/quickjsrb_crypto.c +71 -0
- data/ext/quickjsrb/quickjsrb_crypto.h +6 -0
- data/ext/quickjsrb/quickjsrb_crypto_subtle.c +1001 -0
- data/ext/quickjsrb/quickjsrb_crypto_subtle.h +6 -0
- data/ext/quickjsrb/vendor/polyfill-intl-en.min.js +3 -11
- data/ext/quickjsrb/vendor/polyfill-url.min.js +1 -0
- data/lib/quickjs/crypto_key.rb +15 -0
- data/lib/quickjs/subtle_crypto.rb +493 -0
- data/lib/quickjs/version.rb +1 -1
- data/lib/quickjs.rb +3 -0
- data/polyfills/check-licenses.mjs +72 -0
- data/polyfills/package-lock.json +183 -154
- data/polyfills/package.json +9 -8
- data/polyfills/rolldown.config.mjs +8 -0
- data/polyfills/src/url.js +1089 -0
- data/sig/quickjs.rbs +6 -1
- metadata +11 -2
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#include "quickjsrb.h"
|
|
2
|
+
#include "quickjsrb_crypto.h"
|
|
3
|
+
#include "quickjsrb_crypto_subtle.h"
|
|
4
|
+
|
|
5
|
+
static VALUE r_secure_random()
|
|
6
|
+
{
|
|
7
|
+
return rb_const_get(rb_cObject, rb_intern("SecureRandom"));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static JSValue js_crypto_get_random_values(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
|
11
|
+
{
|
|
12
|
+
if (argc < 1)
|
|
13
|
+
return JS_ThrowTypeError(ctx, "Failed to execute 'getRandomValues': 1 argument required, but only 0 present.");
|
|
14
|
+
|
|
15
|
+
size_t byte_offset, byte_length, bytes_per_element;
|
|
16
|
+
JSValue j_buffer = JS_GetTypedArrayBuffer(ctx, argv[0], &byte_offset, &byte_length, &bytes_per_element);
|
|
17
|
+
if (JS_IsException(j_buffer))
|
|
18
|
+
return JS_EXCEPTION;
|
|
19
|
+
|
|
20
|
+
JSValue j_ctor = JS_GetPropertyStr(ctx, argv[0], "constructor");
|
|
21
|
+
JSValue j_name = JS_GetPropertyStr(ctx, j_ctor, "name");
|
|
22
|
+
const char *name = JS_ToCString(ctx, j_name);
|
|
23
|
+
int is_float = name && (strcmp(name, "Float16Array") == 0 ||
|
|
24
|
+
strcmp(name, "Float32Array") == 0 ||
|
|
25
|
+
strcmp(name, "Float64Array") == 0);
|
|
26
|
+
JS_FreeCString(ctx, name);
|
|
27
|
+
JS_FreeValue(ctx, j_name);
|
|
28
|
+
JS_FreeValue(ctx, j_ctor);
|
|
29
|
+
|
|
30
|
+
if (is_float)
|
|
31
|
+
{
|
|
32
|
+
JS_FreeValue(ctx, j_buffer);
|
|
33
|
+
return JS_ThrowTypeError(ctx, "Failed to execute 'getRandomValues': The provided ArrayBufferView value must not be of floating-point type.");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (byte_length > 65536)
|
|
37
|
+
{
|
|
38
|
+
JS_FreeValue(ctx, j_buffer);
|
|
39
|
+
return JS_ThrowRangeError(ctx, "Failed to execute 'getRandomValues': The ArrayBufferView's byte length exceeds the number of bytes of entropy available via this API (65536).");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
size_t buf_size;
|
|
43
|
+
uint8_t *buf = JS_GetArrayBuffer(ctx, &buf_size, j_buffer);
|
|
44
|
+
JS_FreeValue(ctx, j_buffer);
|
|
45
|
+
|
|
46
|
+
if (!buf)
|
|
47
|
+
return JS_ThrowTypeError(ctx, "Failed to execute 'getRandomValues': Failed to get ArrayBuffer.");
|
|
48
|
+
|
|
49
|
+
VALUE r_random_bytes = rb_funcall(r_secure_random(), rb_intern("random_bytes"), 1, SIZET2NUM(byte_length));
|
|
50
|
+
const uint8_t *random_buf = (const uint8_t *)RSTRING_PTR(r_random_bytes);
|
|
51
|
+
memcpy(buf + byte_offset, random_buf, byte_length);
|
|
52
|
+
|
|
53
|
+
return JS_DupValue(ctx, argv[0]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static JSValue js_crypto_random_uuid(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
|
57
|
+
{
|
|
58
|
+
VALUE r_uuid = rb_funcall(r_secure_random(), rb_intern("uuid"), 0);
|
|
59
|
+
return JS_NewString(ctx, StringValueCStr(r_uuid));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
void quickjsrb_init_crypto(JSContext *ctx, JSValue j_global)
|
|
63
|
+
{
|
|
64
|
+
JSValue j_crypto = JS_NewObject(ctx);
|
|
65
|
+
JS_SetPropertyStr(ctx, j_crypto, "getRandomValues",
|
|
66
|
+
JS_NewCFunction(ctx, js_crypto_get_random_values, "getRandomValues", 1));
|
|
67
|
+
JS_SetPropertyStr(ctx, j_crypto, "randomUUID",
|
|
68
|
+
JS_NewCFunction(ctx, js_crypto_random_uuid, "randomUUID", 0));
|
|
69
|
+
quickjsrb_init_crypto_subtle(ctx, j_crypto);
|
|
70
|
+
JS_SetPropertyStr(ctx, j_global, "crypto", j_crypto);
|
|
71
|
+
}
|