visaljs 0.0.0-stage → 0.0.2
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.
- package/LICENSE +21 -0
- package/README.md +45 -2
- package/cli.js +8084 -0
- package/node/include/js_node.h +46 -0
- package/node/prelude/async_hooks.js +88 -0
- package/node/prelude/buffer.js +31 -0
- package/node/prelude/crypto.js +114 -0
- package/node/prelude/events.js +227 -0
- package/node/prelude/fs.js +79 -0
- package/node/prelude/http.js +536 -0
- package/node/prelude/http2.js +30 -0
- package/node/prelude/internal/crypto/asym.js +333 -0
- package/node/prelude/internal/crypto/cipher.js +205 -0
- package/node/prelude/internal/crypto/hash.js +137 -0
- package/node/prelude/internal/crypto/kdf.js +182 -0
- package/node/prelude/internal/crypto/keys.js +603 -0
- package/node/prelude/internal/crypto/random.js +118 -0
- package/node/prelude/internal/crypto/util.js +76 -0
- package/node/prelude/internal/errors.js +88 -0
- package/node/prelude/internal/querystring.js +105 -0
- package/node/prelude/module.js +53 -0
- package/node/prelude/net.js +51 -0
- package/node/prelude/os.js +129 -0
- package/node/prelude/path.js +257 -0
- package/node/prelude/process.js +21 -0
- package/node/prelude/querystring.js +469 -0
- package/node/prelude/stream.js +1327 -0
- package/node/prelude/stream_promises.js +7 -0
- package/node/prelude/stream_web.js +6 -0
- package/node/prelude/string_decoder.js +236 -0
- package/node/prelude/tty.js +18 -0
- package/node/prelude/url.js +1156 -0
- package/node/prelude/util.js +64 -0
- package/node/prelude/zlib.js +81 -0
- package/node/src/host_fs.c +126 -0
- package/node/src/host_http.c +486 -0
- package/node/src/host_os.c +505 -0
- package/node/src/host_sys.c +78 -0
- package/node/third_party/libuv/LICENSE +19 -0
- package/node/third_party/libuv/include/uv/aix.h +32 -0
- package/node/third_party/libuv/include/uv/bsd.h +34 -0
- package/node/third_party/libuv/include/uv/darwin.h +61 -0
- package/node/third_party/libuv/include/uv/errno.h +483 -0
- package/node/third_party/libuv/include/uv/linux.h +34 -0
- package/node/third_party/libuv/include/uv/os390.h +33 -0
- package/node/third_party/libuv/include/uv/posix.h +31 -0
- package/node/third_party/libuv/include/uv/sunos.h +44 -0
- package/node/third_party/libuv/include/uv/threadpool.h +37 -0
- package/node/third_party/libuv/include/uv/tree.h +521 -0
- package/node/third_party/libuv/include/uv/unix.h +512 -0
- package/node/third_party/libuv/include/uv/version.h +43 -0
- package/node/third_party/libuv/include/uv/win.h +698 -0
- package/node/third_party/libuv/include/uv.h +1990 -0
- package/node/third_party/libuv/src/fs-poll.c +290 -0
- package/node/third_party/libuv/src/heap-inl.h +245 -0
- package/node/third_party/libuv/src/idna.c +560 -0
- package/node/third_party/libuv/src/idna.h +31 -0
- package/node/third_party/libuv/src/inet.c +298 -0
- package/node/third_party/libuv/src/queue.h +90 -0
- package/node/third_party/libuv/src/random.c +123 -0
- package/node/third_party/libuv/src/strscpy.c +38 -0
- package/node/third_party/libuv/src/strscpy.h +39 -0
- package/node/third_party/libuv/src/strtok.c +52 -0
- package/node/third_party/libuv/src/strtok.h +27 -0
- package/node/third_party/libuv/src/thread-common.c +175 -0
- package/node/third_party/libuv/src/threadpool.c +419 -0
- package/node/third_party/libuv/src/timer.c +200 -0
- package/node/third_party/libuv/src/unix/async.c +418 -0
- package/node/third_party/libuv/src/unix/bsd-ifaddrs.c +164 -0
- package/node/third_party/libuv/src/unix/core.c +2104 -0
- package/node/third_party/libuv/src/unix/darwin-proctitle.c +176 -0
- package/node/third_party/libuv/src/unix/darwin-stub.h +97 -0
- package/node/third_party/libuv/src/unix/darwin-syscalls.h +17 -0
- package/node/third_party/libuv/src/unix/darwin.c +237 -0
- package/node/third_party/libuv/src/unix/dl.c +80 -0
- package/node/third_party/libuv/src/unix/fs.c +2303 -0
- package/node/third_party/libuv/src/unix/fsevents.c +906 -0
- package/node/third_party/libuv/src/unix/getaddrinfo.c +252 -0
- package/node/third_party/libuv/src/unix/getnameinfo.c +121 -0
- package/node/third_party/libuv/src/unix/internal.h +530 -0
- package/node/third_party/libuv/src/unix/kqueue.c +667 -0
- package/node/third_party/libuv/src/unix/linux.c +2761 -0
- package/node/third_party/libuv/src/unix/loop-watcher.c +68 -0
- package/node/third_party/libuv/src/unix/loop.c +240 -0
- package/node/third_party/libuv/src/unix/pipe.c +555 -0
- package/node/third_party/libuv/src/unix/poll.c +159 -0
- package/node/third_party/libuv/src/unix/process.c +1123 -0
- package/node/third_party/libuv/src/unix/procfs-exepath.c +45 -0
- package/node/third_party/libuv/src/unix/proctitle.c +157 -0
- package/node/third_party/libuv/src/unix/random-devurandom.c +93 -0
- package/node/third_party/libuv/src/unix/random-getentropy.c +57 -0
- package/node/third_party/libuv/src/unix/random-getrandom.c +86 -0
- package/node/third_party/libuv/src/unix/random-sysctl-linux.c +99 -0
- package/node/third_party/libuv/src/unix/signal.c +581 -0
- package/node/third_party/libuv/src/unix/stream.c +1567 -0
- package/node/third_party/libuv/src/unix/tcp.c +673 -0
- package/node/third_party/libuv/src/unix/thread.c +975 -0
- package/node/third_party/libuv/src/unix/tty.c +510 -0
- package/node/third_party/libuv/src/unix/udp.c +1434 -0
- package/node/third_party/libuv/src/uv-common.c +1051 -0
- package/node/third_party/libuv/src/uv-common.h +474 -0
- package/node/third_party/libuv/src/uv-data-getter-setters.c +119 -0
- package/node/third_party/libuv/src/version.c +45 -0
- package/node/third_party/llhttp/LICENSE +22 -0
- package/node/third_party/llhttp/api.c +509 -0
- package/node/third_party/llhttp/http.c +170 -0
- package/node/third_party/llhttp/llhttp.c +10099 -0
- package/node/third_party/llhttp/llhttp.h +907 -0
- package/package.json +25 -4
- package/runtime/include/js_atoms.gen.h +68 -0
- package/runtime/include/js_atoms.h +54 -0
- package/runtime/include/js_bigint.h +93 -0
- package/runtime/include/js_buffer.h +79 -0
- package/runtime/include/js_builtins.h +168 -0
- package/runtime/include/js_console.h +58 -0
- package/runtime/include/js_convert.h +22 -0
- package/runtime/include/js_core.def +188 -0
- package/runtime/include/js_crypto.h +58 -0
- package/runtime/include/js_error.h +37 -0
- package/runtime/include/js_exception.h +65 -0
- package/runtime/include/js_function.h +167 -0
- package/runtime/include/js_gc.h +195 -0
- package/runtime/include/js_generator.h +96 -0
- package/runtime/include/js_host.h +19 -0
- package/runtime/include/js_jobs.h +84 -0
- package/runtime/include/js_module.h +16 -0
- package/runtime/include/js_object.h +384 -0
- package/runtime/include/js_ops.h +111 -0
- package/runtime/include/js_promise.h +45 -0
- package/runtime/include/js_regexp.h +25 -0
- package/runtime/include/js_runtime.h +30 -0
- package/runtime/include/js_symbol.h +62 -0
- package/runtime/include/js_web.h +110 -0
- package/runtime/include/jscell.h +48 -0
- package/runtime/include/jscontext.h +240 -0
- package/runtime/include/jsnum.h +42 -0
- package/runtime/include/jsstring.h +161 -0
- package/runtime/include/jsvalue.h +142 -0
- package/runtime/src/arguments.c +115 -0
- package/runtime/src/array.c +1145 -0
- package/runtime/src/arraybuffer.c +608 -0
- package/runtime/src/async_iter.c +479 -0
- package/runtime/src/atoms.c +157 -0
- package/runtime/src/base64.c +107 -0
- package/runtime/src/bigint.c +1189 -0
- package/runtime/src/blob.c +371 -0
- package/runtime/src/buffer.c +1357 -0
- package/runtime/src/builtin_table.c +100 -0
- package/runtime/src/builtins.c +987 -0
- package/runtime/src/builtins_util.h +53 -0
- package/runtime/src/collections.c +912 -0
- package/runtime/src/components.c +28 -0
- package/runtime/src/console.c +637 -0
- package/runtime/src/convert.c +82 -0
- package/runtime/src/core_all.c +21 -0
- package/runtime/src/crypto.c +116 -0
- package/runtime/src/crypto_asym.c +644 -0
- package/runtime/src/crypto_cipher.c +671 -0
- package/runtime/src/crypto_hash.c +260 -0
- package/runtime/src/crypto_host.c +414 -0
- package/runtime/src/crypto_internal.h +304 -0
- package/runtime/src/crypto_kdf.c +143 -0
- package/runtime/src/crypto_key.c +171 -0
- package/runtime/src/crypto_pkey.c +794 -0
- package/runtime/src/crypto_subtle.c +1675 -0
- package/runtime/src/date.c +940 -0
- package/runtime/src/domexception.c +169 -0
- package/runtime/src/elements.c +312 -0
- package/runtime/src/elements.h +45 -0
- package/runtime/src/encoding.c +485 -0
- package/runtime/src/error.c +297 -0
- package/runtime/src/events.c +793 -0
- package/runtime/src/exception.c +56 -0
- package/runtime/src/fetch.c +772 -0
- package/runtime/src/function.c +530 -0
- package/runtime/src/gc.c +803 -0
- package/runtime/src/generator.c +218 -0
- package/runtime/src/global.c +119 -0
- package/runtime/src/headers.c +557 -0
- package/runtime/src/inspect.c +1792 -0
- package/runtime/src/internal.h +80 -0
- package/runtime/src/iterate.c +94 -0
- package/runtime/src/iterator.c +409 -0
- package/runtime/src/jobs.c +337 -0
- package/runtime/src/jsnum.c +353 -0
- package/runtime/src/json.c +1028 -0
- package/runtime/src/math.c +243 -0
- package/runtime/src/module.c +336 -0
- package/runtime/src/number.c +176 -0
- package/runtime/src/object.c +1265 -0
- package/runtime/src/ops.c +322 -0
- package/runtime/src/os.c +196 -0
- package/runtime/src/os.h +29 -0
- package/runtime/src/process.c +432 -0
- package/runtime/src/promise.c +721 -0
- package/runtime/src/proxy.c +570 -0
- package/runtime/src/reflect.c +194 -0
- package/runtime/src/regexp.c +1141 -0
- package/runtime/src/regexp_anchor.h +34 -0
- package/runtime/src/regexp_message.h +32 -0
- package/runtime/src/shape.c +294 -0
- package/runtime/src/shape.h +46 -0
- package/runtime/src/streams.c +1046 -0
- package/runtime/src/streams.h +47 -0
- package/runtime/src/string.c +364 -0
- package/runtime/src/string_builtins.c +837 -0
- package/runtime/src/symbol.c +143 -0
- package/runtime/src/test_host.c +59 -0
- package/runtime/src/typedarray.c +1330 -0
- package/runtime/src/tznames.gen.h +712 -0
- package/runtime/src/uri.c +169 -0
- package/runtime/src/url.c +2116 -0
- package/runtime/src/weak.c +486 -0
- package/runtime/src/web.c +214 -0
- package/runtime/src/wrapper.c +59 -0
- package/runtime/src/writable.c +1486 -0
- package/runtime/third_party/ada-idna/LICENSE-MIT +18 -0
- package/runtime/third_party/ada-idna/README.md +27 -0
- package/runtime/third_party/ada-idna/convert.mjs +83 -0
- package/runtime/third_party/ada-idna/idna.c +416 -0
- package/runtime/third_party/ada-idna/idna.h +15 -0
- package/runtime/third_party/ada-idna/idna_tables.h +2657 -0
- package/runtime/third_party/bearssl/LICENSE.txt +21 -0
- package/runtime/third_party/bearssl/README.txt +136 -0
- package/runtime/third_party/bearssl/VERSION +1 -0
- package/runtime/third_party/bearssl/inc/bearssl.h +183 -0
- package/runtime/third_party/bearssl/inc/bearssl_aead.h +1059 -0
- package/runtime/third_party/bearssl/inc/bearssl_block.h +2618 -0
- package/runtime/third_party/bearssl/inc/bearssl_ec.h +967 -0
- package/runtime/third_party/bearssl/inc/bearssl_hash.h +1346 -0
- package/runtime/third_party/bearssl/inc/bearssl_hmac.h +241 -0
- package/runtime/third_party/bearssl/inc/bearssl_kdf.h +284 -0
- package/runtime/third_party/bearssl/inc/bearssl_pem.h +294 -0
- package/runtime/third_party/bearssl/inc/bearssl_prf.h +150 -0
- package/runtime/third_party/bearssl/inc/bearssl_rand.h +397 -0
- package/runtime/third_party/bearssl/inc/bearssl_rsa.h +1655 -0
- package/runtime/third_party/bearssl/inc/bearssl_ssl.h +4296 -0
- package/runtime/third_party/bearssl/inc/bearssl_x509.h +1474 -0
- package/runtime/third_party/bearssl/src/aead/ccm.c +346 -0
- package/runtime/third_party/bearssl/src/aead/eax.c +525 -0
- package/runtime/third_party/bearssl/src/aead/gcm.c +318 -0
- package/runtime/third_party/bearssl/src/codec/ccopy.c +44 -0
- package/runtime/third_party/bearssl/src/codec/dec16be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec16le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec32be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec32le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec64be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec64le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc16be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc16le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc32be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc32le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc64be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc64le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/pemdec.c +526 -0
- package/runtime/third_party/bearssl/src/codec/pemdec.t0 +314 -0
- package/runtime/third_party/bearssl/src/codec/pemenc.c +173 -0
- package/runtime/third_party/bearssl/src/config.h +247 -0
- package/runtime/third_party/bearssl/src/ec/ec_all_m15.c +121 -0
- package/runtime/third_party/bearssl/src/ec/ec_all_m31.c +171 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_i15.c +398 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_i31.c +390 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m15.c +1478 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m31.c +800 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m62.c +605 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m64.c +831 -0
- package/runtime/third_party/bearssl/src/ec/ec_curve25519.c +46 -0
- package/runtime/third_party/bearssl/src/ec/ec_default.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ec_keygen.c +86 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m15.c +2124 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m31.c +1469 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m62.c +1765 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m64.c +1781 -0
- package/runtime/third_party/bearssl/src/ec/ec_prime_i15.c +824 -0
- package/runtime/third_party/bearssl/src/ec/ec_prime_i31.c +826 -0
- package/runtime/third_party/bearssl/src/ec/ec_pubkey.c +85 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp256r1.c +51 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp384r1.c +57 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp521r1.c +64 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_atr.c +134 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_asn1.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_raw.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_asn1.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_raw.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_bits.c +47 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_asn1.c +45 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_raw.c +174 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_asn1.c +48 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_raw.c +166 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_bits.c +47 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_asn1.c +45 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_raw.c +173 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_asn1.c +48 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_raw.c +165 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_rta.c +121 -0
- package/runtime/third_party/bearssl/src/hash/dig_oid.c +84 -0
- package/runtime/third_party/bearssl/src/hash/dig_size.c +50 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul.c +345 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul32.c +251 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul64.c +154 -0
- package/runtime/third_party/bearssl/src/hash/ghash_pclmul.c +389 -0
- package/runtime/third_party/bearssl/src/hash/ghash_pwr8.c +411 -0
- package/runtime/third_party/bearssl/src/hash/md5.c +208 -0
- package/runtime/third_party/bearssl/src/hash/md5sha1.c +141 -0
- package/runtime/third_party/bearssl/src/hash/mgf1.c +56 -0
- package/runtime/third_party/bearssl/src/hash/multihash.c +166 -0
- package/runtime/third_party/bearssl/src/hash/sha1.c +191 -0
- package/runtime/third_party/bearssl/src/hash/sha2big.c +285 -0
- package/runtime/third_party/bearssl/src/hash/sha2small.c +341 -0
- package/runtime/third_party/bearssl/src/inner.h +2577 -0
- package/runtime/third_party/bearssl/src/int/i15_add.c +46 -0
- package/runtime/third_party/bearssl/src/int/i15_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i15_decmod.c +124 -0
- package/runtime/third_party/bearssl/src/int/i15_decode.c +56 -0
- package/runtime/third_party/bearssl/src/int/i15_decred.c +100 -0
- package/runtime/third_party/bearssl/src/int/i15_encode.c +56 -0
- package/runtime/third_party/bearssl/src/int/i15_fmont.c +59 -0
- package/runtime/third_party/bearssl/src/int/i15_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i15_moddiv.c +465 -0
- package/runtime/third_party/bearssl/src/int/i15_modpow.c +50 -0
- package/runtime/third_party/bearssl/src/int/i15_modpow2.c +160 -0
- package/runtime/third_party/bearssl/src/int/i15_montmul.c +184 -0
- package/runtime/third_party/bearssl/src/int/i15_mulacc.c +61 -0
- package/runtime/third_party/bearssl/src/int/i15_muladd.c +173 -0
- package/runtime/third_party/bearssl/src/int/i15_ninv15.c +38 -0
- package/runtime/third_party/bearssl/src/int/i15_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i15_rshift.c +47 -0
- package/runtime/third_party/bearssl/src/int/i15_sub.c +46 -0
- package/runtime/third_party/bearssl/src/int/i15_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i31_add.c +46 -0
- package/runtime/third_party/bearssl/src/int/i31_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i31_decmod.c +124 -0
- package/runtime/third_party/bearssl/src/int/i31_decode.c +57 -0
- package/runtime/third_party/bearssl/src/int/i31_decred.c +103 -0
- package/runtime/third_party/bearssl/src/int/i31_encode.c +79 -0
- package/runtime/third_party/bearssl/src/int/i31_fmont.c +60 -0
- package/runtime/third_party/bearssl/src/int/i31_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i31_moddiv.c +488 -0
- package/runtime/third_party/bearssl/src/int/i31_modpow.c +65 -0
- package/runtime/third_party/bearssl/src/int/i31_modpow2.c +160 -0
- package/runtime/third_party/bearssl/src/int/i31_montmul.c +127 -0
- package/runtime/third_party/bearssl/src/int/i31_mulacc.c +74 -0
- package/runtime/third_party/bearssl/src/int/i31_muladd.c +157 -0
- package/runtime/third_party/bearssl/src/int/i31_ninv31.c +39 -0
- package/runtime/third_party/bearssl/src/int/i31_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i31_rshift.c +47 -0
- package/runtime/third_party/bearssl/src/int/i31_sub.c +46 -0
- package/runtime/third_party/bearssl/src/int/i31_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i32_add.c +51 -0
- package/runtime/third_party/bearssl/src/int/i32_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i32_decmod.c +77 -0
- package/runtime/third_party/bearssl/src/int/i32_decode.c +63 -0
- package/runtime/third_party/bearssl/src/int/i32_decred.c +107 -0
- package/runtime/third_party/bearssl/src/int/i32_div32.c +56 -0
- package/runtime/third_party/bearssl/src/int/i32_encode.c +72 -0
- package/runtime/third_party/bearssl/src/int/i32_fmont.c +60 -0
- package/runtime/third_party/bearssl/src/int/i32_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i32_modpow.c +65 -0
- package/runtime/third_party/bearssl/src/int/i32_montmul.c +69 -0
- package/runtime/third_party/bearssl/src/int/i32_mulacc.c +56 -0
- package/runtime/third_party/bearssl/src/int/i32_muladd.c +138 -0
- package/runtime/third_party/bearssl/src/int/i32_ninv32.c +39 -0
- package/runtime/third_party/bearssl/src/int/i32_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i32_sub.c +51 -0
- package/runtime/third_party/bearssl/src/int/i32_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i62_modpow2.c +493 -0
- package/runtime/third_party/bearssl/src/kdf/hkdf.c +107 -0
- package/runtime/third_party/bearssl/src/kdf/shake.c +590 -0
- package/runtime/third_party/bearssl/src/mac/hmac.c +122 -0
- package/runtime/third_party/bearssl/src/mac/hmac_ct.c +193 -0
- package/runtime/third_party/bearssl/src/rand/aesctr_drbg.c +206 -0
- package/runtime/third_party/bearssl/src/rand/hmac_drbg.c +157 -0
- package/runtime/third_party/bearssl/src/rand/sysrng.c +252 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_keygen.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_modulus.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_decrypt.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_encrypt.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_sign.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_vrfy.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_priv.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_privexp.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_sign.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_vrfy.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pub.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pubexp.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_keygen.c +583 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_modulus.c +99 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_priv.c +209 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_privexp.c +320 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pub.c +113 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pubexp.c +152 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen_inner.c +608 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_modulus.c +99 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_priv.c +203 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_privexp.c +318 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pub.c +106 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pubexp.c +152 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_priv.c +160 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pub.c +77 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_keygen.c +57 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_decrypt.c +61 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_encrypt.c +64 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_sign.c +57 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_vrfy.c +63 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_priv.c +223 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_sign.c +60 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_vrfy.c +64 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pub.c +125 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_oaep_pad.c +112 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_oaep_unpad.c +145 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_pad.c +100 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_unpad.c +121 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_pad.c +106 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_unpad.c +121 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_ssl_decrypt.c +52 -0
- package/runtime/third_party/bearssl/src/settings.c +306 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcdec.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_ctr.c +84 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_ctrcbc.c +142 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_dec.c +254 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_enc.c +157 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_common.c +112 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct.c +328 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64.c +398 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcdec.c +104 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcenc.c +81 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctr.c +114 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctrcbc.c +433 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_dec.c +159 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_enc.c +115 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcdec.c +111 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcenc.c +91 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctr.c +116 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctrcbc.c +422 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_dec.c +170 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_enc.c +112 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8.c +445 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcdec.c +670 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcenc.c +417 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctr.c +717 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctrcbc.c +946 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcdec.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_ctr.c +84 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_ctrcbc.c +142 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_dec.c +176 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_enc.c +129 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni.c +240 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcdec.c +223 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcenc.c +122 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctr.c +211 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctrcbc.c +596 -0
- package/runtime/third_party/bearssl/src/symcipher/chacha20_ct.c +106 -0
- package/runtime/third_party/bearssl/src/symcipher/chacha20_sse2.c +237 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct.c +411 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcdec.c +87 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcenc.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/des_support.c +166 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab.c +310 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcdec.c +85 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul.c +260 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul32.c +297 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmulq.c +475 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_i15.c +221 -0
- package/runtime/third_party/bearssl/src/x509/asn1.t0 +757 -0
- package/runtime/third_party/bearssl/src/x509/asn1enc.c +93 -0
- package/runtime/third_party/bearssl/src/x509/encode_ec_pk8der.c +110 -0
- package/runtime/third_party/bearssl/src/x509/encode_ec_rawder.c +161 -0
- package/runtime/third_party/bearssl/src/x509/encode_rsa_pk8der.c +97 -0
- package/runtime/third_party/bearssl/src/x509/encode_rsa_rawder.c +96 -0
- package/runtime/third_party/bearssl/src/x509/skey_decoder.c +653 -0
- package/runtime/third_party/bearssl/src/x509/skey_decoder.t0 +376 -0
- package/runtime/third_party/bearssl/src/x509/x509_decoder.c +776 -0
- package/runtime/third_party/bearssl/src/x509/x509_decoder.t0 +324 -0
- package/runtime/third_party/bearssl/src/x509/x509_knownkey.c +105 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal.c +1722 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal.t0 +1526 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal_full.c +59 -0
- package/runtime/third_party/quickjs-dtoa/LICENSE +22 -0
- package/runtime/third_party/quickjs-dtoa/README.md +23 -0
- package/runtime/third_party/quickjs-dtoa/dtoa.c +1640 -0
- package/runtime/third_party/quickjs-dtoa/dtoa.h +83 -0
- package/runtime/third_party/quickjs-dtoa/dtoa_compat.h +89 -0
- package/runtime/third_party/quickjs-regexp/LICENSE +22 -0
- package/runtime/third_party/quickjs-regexp/README.md +22 -0
- package/runtime/third_party/quickjs-regexp/cutils.c +641 -0
- package/runtime/third_party/quickjs-regexp/cutils.h +457 -0
- package/runtime/third_party/quickjs-regexp/libregexp-opcode.h +73 -0
- package/runtime/third_party/quickjs-regexp/libregexp.c +3448 -0
- package/runtime/third_party/quickjs-regexp/libregexp.h +65 -0
- package/runtime/third_party/quickjs-regexp/libunicode-table.h +5206 -0
- package/runtime/third_party/quickjs-regexp/libunicode.c +2124 -0
- package/runtime/third_party/quickjs-regexp/libunicode.h +196 -0
- package/runtime/third_party/v8-ieee754/LICENSE +61 -0
- package/runtime/third_party/v8-ieee754/README.md +22 -0
- package/runtime/third_party/v8-ieee754/convert.py +84 -0
- package/runtime/third_party/v8-ieee754/ieee754.c +3030 -0
- package/runtime/third_party/v8-ieee754/ieee754.h +27 -0
- package/runtime/tools/regexp_compile.c +90 -0
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
/* Request and Response (Fetch §5.4, §5.5), the fetch component (ADR 0012, layer 2), as Node's
|
|
2
|
+
* (undici's): their headers (the headers component), their bodies (the Body mixin, §5.3) and
|
|
3
|
+
* Node's errors and inspection. A body made from bytes (a string, a buffer source, a Blob,
|
|
4
|
+
* URLSearchParams) keeps them: text(), json(), arrayBuffer(), bytes() and blob() read them
|
|
5
|
+
* directly, and `body` makes a ReadableStream of them only when asked. A ReadableStream body is
|
|
6
|
+
* read through the streams component. fetch() itself (the client) isn't provided yet. */
|
|
7
|
+
#include <math.h>
|
|
8
|
+
#include <stdio.h>
|
|
9
|
+
#include <stdlib.h>
|
|
10
|
+
#include <string.h>
|
|
11
|
+
|
|
12
|
+
#include "builtins_util.h"
|
|
13
|
+
#include "internal.h"
|
|
14
|
+
#include "js_atoms.h"
|
|
15
|
+
#include "js_buffer.h"
|
|
16
|
+
#include "js_convert.h"
|
|
17
|
+
#include "js_exception.h"
|
|
18
|
+
#include "js_ops.h"
|
|
19
|
+
#include "js_promise.h"
|
|
20
|
+
#include "js_web.h"
|
|
21
|
+
#include "jscontext.h"
|
|
22
|
+
#include "jsstring.h"
|
|
23
|
+
|
|
24
|
+
/* A failure's -1, after the call that threw (the throwers' results must be used). */
|
|
25
|
+
static int fail(JSValue thrown) {
|
|
26
|
+
(void)thrown;
|
|
27
|
+
return -1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/* ---- Bodies (§5.3) -------------------------------------------------------------------------- */
|
|
31
|
+
|
|
32
|
+
typedef struct Body {
|
|
33
|
+
JSValue bytes; /* a Uint8Array of the body's bytes, or undefined (none, or a stream body) */
|
|
34
|
+
JSValue stream; /* the ReadableStream, once made (or given); undefined until then */
|
|
35
|
+
uint8_t has_body;
|
|
36
|
+
uint8_t used; /* consumed directly from `bytes` */
|
|
37
|
+
} Body;
|
|
38
|
+
|
|
39
|
+
typedef struct RequestObject {
|
|
40
|
+
JSObject obj;
|
|
41
|
+
Body body;
|
|
42
|
+
JSValue method, url, headers, signal;
|
|
43
|
+
uint8_t redirect; /* 0 follow, 1 error, 2 manual */
|
|
44
|
+
uint8_t keepalive;
|
|
45
|
+
} RequestObject;
|
|
46
|
+
|
|
47
|
+
typedef struct ResponseObject {
|
|
48
|
+
JSObject obj;
|
|
49
|
+
Body body;
|
|
50
|
+
JSValue status_text, headers, url;
|
|
51
|
+
uint16_t status;
|
|
52
|
+
uint8_t type; /* 0 default, 1 error, 2 basic… */
|
|
53
|
+
uint8_t redirected;
|
|
54
|
+
} ResponseObject;
|
|
55
|
+
|
|
56
|
+
static void body_visit(Body *b, JSTracer *trc) {
|
|
57
|
+
js_trace_value(trc, b->bytes);
|
|
58
|
+
js_trace_value(trc, b->stream);
|
|
59
|
+
}
|
|
60
|
+
static void request_visit(JSCell *c, JSTracer *trc) {
|
|
61
|
+
RequestObject *r = (RequestObject *)c;
|
|
62
|
+
body_visit(&r->body, trc);
|
|
63
|
+
js_trace_value(trc, r->method);
|
|
64
|
+
js_trace_value(trc, r->url);
|
|
65
|
+
js_trace_value(trc, r->headers);
|
|
66
|
+
js_trace_value(trc, r->signal);
|
|
67
|
+
}
|
|
68
|
+
static void response_visit(JSCell *c, JSTracer *trc) {
|
|
69
|
+
ResponseObject *r = (ResponseObject *)c;
|
|
70
|
+
body_visit(&r->body, trc);
|
|
71
|
+
js_trace_value(trc, r->status_text);
|
|
72
|
+
js_trace_value(trc, r->headers);
|
|
73
|
+
js_trace_value(trc, r->url);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static const JSClass request_class = { "Request", sizeof(RequestObject), NULL, NULL, NULL, NULL, request_visit, NULL, NULL };
|
|
77
|
+
static const JSClass response_class = { "Response", sizeof(ResponseObject), NULL, NULL, NULL, NULL, response_visit, NULL, NULL };
|
|
78
|
+
|
|
79
|
+
static RequestObject *as_request(JSValue v) {
|
|
80
|
+
return js_is_object(v) && js_as_object(v)->shape->cls == &request_class ? (RequestObject *)js_as_object(v) : NULL;
|
|
81
|
+
}
|
|
82
|
+
static ResponseObject *as_response(JSValue v) {
|
|
83
|
+
return js_is_object(v) && js_as_object(v)->shape->cls == &response_class ? (ResponseObject *)js_as_object(v) : NULL;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static void init_body(Body *b) {
|
|
87
|
+
b->bytes = JS_UNDEFINED;
|
|
88
|
+
b->stream = JS_UNDEFINED;
|
|
89
|
+
b->has_body = 0;
|
|
90
|
+
b->used = 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static JSValue u8_of(JSContext *ctx, const uint8_t *p, size_t n) {
|
|
94
|
+
JSValue u8 = js_new_uint8_array(ctx, n);
|
|
95
|
+
if (js_is_exception(u8) || !n) return u8;
|
|
96
|
+
uint8_t *data;
|
|
97
|
+
size_t len;
|
|
98
|
+
js_buffer_source_bytes(u8, &data, &len);
|
|
99
|
+
memcpy(data, p, n);
|
|
100
|
+
return u8;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static bool class_named(JSValue v, const char *name) {
|
|
104
|
+
return js_is_object(v) && strcmp(js_as_object(v)->shape->cls->name, name) == 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/* Extract a body (§5.3 "extract"): its bytes or stream, and the Content-Type it implies (EMPTY
|
|
108
|
+
* for none). 0, or -1. */
|
|
109
|
+
static int extract(JSContext *ctx, JSCell *owner, Body *b, JSValue init, JSValue *content_type) {
|
|
110
|
+
*content_type = JS_EMPTY;
|
|
111
|
+
b->has_body = 1;
|
|
112
|
+
uint8_t *p;
|
|
113
|
+
size_t n;
|
|
114
|
+
if (js_is_readable_stream(init)) {
|
|
115
|
+
if (js_readable_stream_locked(init) || js_readable_stream_disturbed(init))
|
|
116
|
+
return fail(js_throw_type_error(ctx, "Response body object should not be disturbed or locked"));
|
|
117
|
+
js_store(ctx, owner, &b->stream, init);
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
JSValue bytes;
|
|
121
|
+
if (js_is_blob(init)) {
|
|
122
|
+
const uint8_t *data;
|
|
123
|
+
size_t size;
|
|
124
|
+
JSValue type;
|
|
125
|
+
js_blob_bytes(init, &data, &size, &type);
|
|
126
|
+
bytes = u8_of(ctx, data, size);
|
|
127
|
+
if (js_string_length(type)) *content_type = type;
|
|
128
|
+
} else if (js_buffer_source_bytes(init, &p, &n)) {
|
|
129
|
+
bytes = u8_of(ctx, p, n);
|
|
130
|
+
} else {
|
|
131
|
+
bool params = class_named(init, "URLSearchParams");
|
|
132
|
+
JSValue s = js_to_string(ctx, init);
|
|
133
|
+
if (js_is_exception(s)) return -1;
|
|
134
|
+
JSStrView v = js_string_view(ctx, s);
|
|
135
|
+
size_t len = js_view_to_utf8(v, NULL, 0);
|
|
136
|
+
bytes = js_new_uint8_array(ctx, len);
|
|
137
|
+
if (js_is_exception(bytes)) return -1;
|
|
138
|
+
js_buffer_source_bytes(bytes, &p, &n);
|
|
139
|
+
js_view_to_utf8(v, (char *)p, len);
|
|
140
|
+
*content_type = js_new_string_cstr(ctx, params ? "application/x-www-form-urlencoded;charset=UTF-8" : "text/plain;charset=UTF-8");
|
|
141
|
+
}
|
|
142
|
+
if (js_is_exception(bytes)) return -1;
|
|
143
|
+
js_store(ctx, owner, &b->bytes, bytes);
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static bool body_used(const Body *b) {
|
|
148
|
+
return b->used || (!js_is_undefined(b->stream) && js_readable_stream_disturbed(b->stream));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* The body's stream (null for none): made from the bytes the first time it is asked for. */
|
|
152
|
+
static JSValue body_stream(JSContext *ctx, JSCell *owner, Body *b) {
|
|
153
|
+
if (!b->has_body) return JS_NULL;
|
|
154
|
+
if (js_is_undefined(b->stream)) {
|
|
155
|
+
JSValue s = js_readable_stream_of_bytes(ctx, b->used ? JS_UNDEFINED : b->bytes);
|
|
156
|
+
if (js_is_exception(s)) return s;
|
|
157
|
+
js_store(ctx, owner, &b->stream, s);
|
|
158
|
+
js_store(ctx, owner, &b->bytes, JS_UNDEFINED);
|
|
159
|
+
}
|
|
160
|
+
return b->stream;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static JSValue rejected(JSContext *ctx, JSValue reason) {
|
|
164
|
+
JSValue p = js_new_promise(ctx);
|
|
165
|
+
js_promise_reject_internal(ctx, p, reason);
|
|
166
|
+
return p;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static JSValue settled(JSContext *ctx, JSValue v) {
|
|
170
|
+
JSValue p = js_new_promise(ctx);
|
|
171
|
+
if (js_is_exception(v)) js_promise_reject_internal(ctx, p, js_take_exception(ctx));
|
|
172
|
+
else js_promise_resolve_internal(ctx, p, v);
|
|
173
|
+
return p;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
enum { AS_TEXT, AS_JSON, AS_ARRAY_BUFFER, AS_BYTES, AS_BLOB };
|
|
177
|
+
|
|
178
|
+
/* The body's bytes (a Uint8Array) as what a consumer asked for. */
|
|
179
|
+
static JSValue package(JSContext *ctx, JSValue bytes, int kind, JSValue mime) {
|
|
180
|
+
uint8_t *p;
|
|
181
|
+
size_t n;
|
|
182
|
+
js_buffer_source_bytes(bytes, &p, &n);
|
|
183
|
+
switch (kind) {
|
|
184
|
+
case AS_TEXT: return js_decode_utf8_bytes(ctx, p, n);
|
|
185
|
+
case AS_JSON: {
|
|
186
|
+
JSValue s = js_decode_utf8_bytes(ctx, p, n);
|
|
187
|
+
return js_is_exception(s) ? s : js_json_parse(ctx, s);
|
|
188
|
+
}
|
|
189
|
+
case AS_ARRAY_BUFFER: {
|
|
190
|
+
JSValue ab = js_new_array_buffer(ctx, n);
|
|
191
|
+
if (!js_is_exception(ab) && n) memcpy(js_as_array_buffer(ab)->data, p, n);
|
|
192
|
+
return ab;
|
|
193
|
+
}
|
|
194
|
+
case AS_BYTES: return bytes;
|
|
195
|
+
default: return js_new_blob(ctx, p, n, js_is_string(mime) ? mime : js_empty_string());
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/* A stream body's consumption: the closure's data is [kind, mime]. */
|
|
200
|
+
BUILTIN(consume_read) {
|
|
201
|
+
(void)this_val, (void)new_target;
|
|
202
|
+
JSValue data = js_closure_data(callee);
|
|
203
|
+
JSValue kind = js_get_elem(ctx, data, js_int32(0)), mime = js_get_elem(ctx, data, js_int32(1));
|
|
204
|
+
return package(ctx, ARG(0), (int)js_as_number(kind), mime);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
static JSValue unusable(JSContext *ctx) {
|
|
208
|
+
return js_new_error(ctx, JS_TYPE_ERROR, js_new_string_cstr(ctx, "Body is unusable: Body has already been read"));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/* Consume body (§5.3): a promise of the bytes as `kind`. */
|
|
212
|
+
static JSValue consume(JSContext *ctx, JSCell *owner, Body *b, JSValue headers, int kind) {
|
|
213
|
+
if (body_used(b) || (!js_is_undefined(b->stream) && js_readable_stream_locked(b->stream))) return rejected(ctx, unusable(ctx));
|
|
214
|
+
JSValue mime = JS_UNDEFINED;
|
|
215
|
+
if (kind == AS_BLOB) {
|
|
216
|
+
mime = js_headers_get(ctx, headers, "content-type");
|
|
217
|
+
if (js_is_exception(mime)) return rejected(ctx, js_take_exception(ctx));
|
|
218
|
+
}
|
|
219
|
+
if (!b->has_body) return settled(ctx, package(ctx, js_new_uint8_array(ctx, 0), kind, mime));
|
|
220
|
+
if (js_is_undefined(b->stream)) {
|
|
221
|
+
b->used = 1;
|
|
222
|
+
JSValue bytes = b->bytes;
|
|
223
|
+
js_store(ctx, owner, &b->bytes, JS_UNDEFINED);
|
|
224
|
+
return settled(ctx, package(ctx, bytes, kind, mime));
|
|
225
|
+
}
|
|
226
|
+
JSValue all = js_readable_stream_read_all(ctx, b->stream);
|
|
227
|
+
JSValue items[2] = { js_int32(kind), mime };
|
|
228
|
+
JSValue derived = js_new_promise(ctx);
|
|
229
|
+
js_promise_then_internal(ctx, all, js_new_closure_native(ctx, consume_read, 1, js_new_array_from(ctx, items, 2)), JS_UNDEFINED, derived);
|
|
230
|
+
return derived;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/* Clone a body (§5.3): bytes are shared (they never change), a stream is teed. 0, or -1. */
|
|
234
|
+
static int clone_body(JSContext *ctx, JSCell *from_owner, Body *from, JSCell *to_owner, Body *to) {
|
|
235
|
+
to->has_body = from->has_body;
|
|
236
|
+
if (!from->has_body) return 0;
|
|
237
|
+
if (js_is_undefined(from->stream)) {
|
|
238
|
+
js_store(ctx, to_owner, &to->bytes, from->bytes);
|
|
239
|
+
return 0;
|
|
240
|
+
}
|
|
241
|
+
JSValue pair = js_readable_stream_tee(ctx, from->stream);
|
|
242
|
+
if (js_is_exception(pair)) return -1;
|
|
243
|
+
js_store(ctx, from_owner, &from->stream, js_get_elem(ctx, pair, js_int32(0)));
|
|
244
|
+
js_store(ctx, to_owner, &to->stream, js_get_elem(ctx, pair, js_int32(1)));
|
|
245
|
+
return 0;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/* ---- Request (§5.4) ------------------------------------------------------------------------- */
|
|
249
|
+
|
|
250
|
+
static bool token_char(uint32_t c) {
|
|
251
|
+
if ((c | 32) >= 'a' && (c | 32) <= 'z') return true;
|
|
252
|
+
if (c >= '0' && c <= '9') return true;
|
|
253
|
+
return c < 0x80 && c && strchr("!#$%&'*+-.^_`|~", (int)c);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/* A method as Fetch normalizes it: DELETE, GET, HEAD, OPTIONS, POST and PUT uppercased. */
|
|
257
|
+
static JSValue normalize_method(JSContext *ctx, JSValue m) {
|
|
258
|
+
char s[64];
|
|
259
|
+
JSStrView v = js_string_view(ctx, m);
|
|
260
|
+
char msg[160];
|
|
261
|
+
bool ok = v.length > 0;
|
|
262
|
+
for (uint32_t i = 0; i < v.length && ok; i++) ok = token_char(js_view_at(v, i));
|
|
263
|
+
s[js_view_to_utf8(v, s, sizeof s - 1)] = '\0';
|
|
264
|
+
if (!ok) {
|
|
265
|
+
snprintf(msg, sizeof msg, "'%s' is not a valid HTTP method.", s);
|
|
266
|
+
return js_throw_type_error(ctx, "%s", msg);
|
|
267
|
+
}
|
|
268
|
+
char upper[64];
|
|
269
|
+
size_t n = strlen(s);
|
|
270
|
+
for (size_t i = 0; i <= n; i++) upper[i] = (char)(s[i] >= 'a' && s[i] <= 'z' ? s[i] - 32 : s[i]);
|
|
271
|
+
if (!strcmp(upper, "CONNECT") || !strcmp(upper, "TRACE") || !strcmp(upper, "TRACK"))
|
|
272
|
+
return js_throw_type_error(ctx, "'%s' HTTP method is unsupported.", s);
|
|
273
|
+
static const char *const normal[] = { "DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT" };
|
|
274
|
+
for (size_t i = 0; i < JS_COUNTOF(normal); i++)
|
|
275
|
+
if (!strcmp(upper, normal[i])) return js_new_string_cstr(ctx, normal[i]);
|
|
276
|
+
return m;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
static bool method_is(JSContext *ctx, JSValue m, const char *name) {
|
|
280
|
+
char s[16];
|
|
281
|
+
JSStrView v = js_string_view(ctx, m);
|
|
282
|
+
if (v.length >= sizeof s) return false;
|
|
283
|
+
s[js_view_to_utf8(v, s, sizeof s - 1)] = '\0';
|
|
284
|
+
return strcmp(s, name) == 0;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
static JSValue get_option(JSContext *ctx, JSValue init, const char *name) {
|
|
288
|
+
return js_is_object(init) ? js_get_value(ctx, init, js_atom_from_cstr(ctx, name)) : JS_UNDEFINED;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/* new Request(input, init) */
|
|
292
|
+
BUILTIN(request_ctor) {
|
|
293
|
+
(void)callee, (void)this_val;
|
|
294
|
+
if (js_is_undefined(new_target)) return js_throw_class_without_new(ctx, "_Request");
|
|
295
|
+
if (argc < 1) return js_throw_type_error(ctx, "Request constructor: 1 argument required, but 0 found.");
|
|
296
|
+
JSValue input = argv[0], init = ARG(1);
|
|
297
|
+
if (!js_is_nullish(init) && !js_is_object(init))
|
|
298
|
+
return js_throw_type_error(ctx, "Request constructor: Expected init (\"Argument 2\") to be an instance of RequestInit.");
|
|
299
|
+
RequestObject *src = as_request(input);
|
|
300
|
+
JSValue url, method = js_new_string_cstr(ctx, "GET"), headers_init = JS_UNDEFINED, signal = JS_UNDEFINED;
|
|
301
|
+
if (src) {
|
|
302
|
+
url = src->url;
|
|
303
|
+
method = src->method;
|
|
304
|
+
headers_init = src->headers;
|
|
305
|
+
signal = src->signal;
|
|
306
|
+
} else {
|
|
307
|
+
JSValue s = js_to_string(ctx, input);
|
|
308
|
+
if (js_is_exception(s)) return s;
|
|
309
|
+
JSValue u = js_parse_url_href(ctx, s);
|
|
310
|
+
if (js_is_exception(u)) return u;
|
|
311
|
+
if (js_is_undefined(u)) {
|
|
312
|
+
char text[256];
|
|
313
|
+
text[js_view_to_utf8(js_string_view(ctx, s), text, sizeof text - 1)] = '\0';
|
|
314
|
+
return js_throw_type_error(ctx, "Failed to parse URL from %s", text);
|
|
315
|
+
}
|
|
316
|
+
url = u;
|
|
317
|
+
}
|
|
318
|
+
JSValue m = get_option(ctx, init, "method");
|
|
319
|
+
if (js_is_exception(m)) return m;
|
|
320
|
+
if (!js_is_undefined(m)) {
|
|
321
|
+
JSValue ms = js_to_string(ctx, m);
|
|
322
|
+
if (js_is_exception(ms)) return ms;
|
|
323
|
+
method = normalize_method(ctx, ms);
|
|
324
|
+
if (js_is_exception(method)) return method;
|
|
325
|
+
}
|
|
326
|
+
JSValue h = get_option(ctx, init, "headers");
|
|
327
|
+
if (js_is_exception(h)) return h;
|
|
328
|
+
if (!js_is_undefined(h)) headers_init = h;
|
|
329
|
+
JSValue sig = get_option(ctx, init, "signal");
|
|
330
|
+
if (js_is_exception(sig)) return sig;
|
|
331
|
+
if (!js_is_undefined(sig)) {
|
|
332
|
+
if (!js_is_null(sig) && !js_is_abort_signal(sig))
|
|
333
|
+
return js_throw_type_error(ctx, "Failed to construct 'Request': member signal is not of type AbortSignal.");
|
|
334
|
+
signal = sig;
|
|
335
|
+
}
|
|
336
|
+
JSValue body = get_option(ctx, init, "body");
|
|
337
|
+
if (js_is_exception(body)) return body;
|
|
338
|
+
bool given_body = !js_is_nullish(body);
|
|
339
|
+
if (given_body && (method_is(ctx, method, "GET") || method_is(ctx, method, "HEAD")))
|
|
340
|
+
return js_throw_type_error(ctx, "Request with GET/HEAD method cannot have body.");
|
|
341
|
+
if (given_body && js_is_readable_stream(body)) {
|
|
342
|
+
JSValue duplex = get_option(ctx, init, "duplex");
|
|
343
|
+
if (js_is_exception(duplex)) return duplex;
|
|
344
|
+
if (js_is_undefined(duplex)) return js_throw_type_error(ctx, "RequestInit: duplex option is required when sending a body.");
|
|
345
|
+
}
|
|
346
|
+
JSValue redirect = get_option(ctx, init, "redirect");
|
|
347
|
+
if (js_is_exception(redirect)) return redirect;
|
|
348
|
+
JSValue keepalive = get_option(ctx, init, "keepalive");
|
|
349
|
+
if (js_is_exception(keepalive)) return keepalive;
|
|
350
|
+
|
|
351
|
+
JSValue proto = js_get_prototype_from_constructor(ctx, new_target, JS_INTRINSIC_REQUEST_PROTOTYPE);
|
|
352
|
+
if (js_is_exception(proto)) return proto;
|
|
353
|
+
RequestObject *r = (RequestObject *)js_alloc_object(ctx, JS_CELL_OBJECT, js_shape_root(ctx, &request_class, proto, 4));
|
|
354
|
+
init_body(&r->body);
|
|
355
|
+
js_init_slot(&r->method, method);
|
|
356
|
+
js_init_slot(&r->url, url);
|
|
357
|
+
js_init_slot(&r->headers, JS_UNDEFINED);
|
|
358
|
+
js_init_slot(&r->signal, JS_UNDEFINED);
|
|
359
|
+
r->redirect = 0;
|
|
360
|
+
r->keepalive = js_truthy(keepalive);
|
|
361
|
+
if (js_is_string(redirect)) r->redirect = method_is(ctx, redirect, "error") ? 1 : method_is(ctx, redirect, "manual") ? 2 : 0;
|
|
362
|
+
JSValue rv = js_object_value(&r->obj);
|
|
363
|
+
JSValue headers = js_new_headers_from(ctx, headers_init);
|
|
364
|
+
if (js_is_exception(headers)) return headers;
|
|
365
|
+
js_store(ctx, &r->obj.cell, &r->headers, headers);
|
|
366
|
+
/* the request's signal follows the given one (Fetch: "follow"), as undici's does */
|
|
367
|
+
JSValue own = js_is_abort_signal(signal) ? js_abort_signal_follow(ctx, signal) : js_new_abort_signal(ctx);
|
|
368
|
+
if (js_is_exception(own)) return own;
|
|
369
|
+
js_store(ctx, &r->obj.cell, &r->signal, own);
|
|
370
|
+
if (given_body) {
|
|
371
|
+
JSValue type;
|
|
372
|
+
if (extract(ctx, &r->obj.cell, &r->body, body, &type) < 0) return JS_EXCEPTION;
|
|
373
|
+
if (!js_is_empty(type)) {
|
|
374
|
+
JSValue has = js_headers_get(ctx, headers, "content-type");
|
|
375
|
+
if (js_is_exception(has)) return has;
|
|
376
|
+
if (js_is_null(has) && js_headers_append(ctx, headers, js_new_string_cstr(ctx, "content-type"), type) < 0) return JS_EXCEPTION;
|
|
377
|
+
}
|
|
378
|
+
} else if (src && src->body.has_body) { /* the input request's body moves to this one */
|
|
379
|
+
if (body_used(&src->body)) return js_throw_type_error(ctx, "Cannot construct a Request with a Request object that has already been used.");
|
|
380
|
+
r->body = src->body;
|
|
381
|
+
init_body(&src->body);
|
|
382
|
+
src->body.used = 1;
|
|
383
|
+
src->body.has_body = 1;
|
|
384
|
+
}
|
|
385
|
+
return rv;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
#define THIS_REQUEST() \
|
|
389
|
+
RequestObject *r = as_request(this_val); \
|
|
390
|
+
if (!r) return js_throw_invalid_this(ctx, "Request")
|
|
391
|
+
|
|
392
|
+
#define REQUEST_GETTER(fn, ...) \
|
|
393
|
+
BUILTIN(fn) { \
|
|
394
|
+
UNUSED_ARGS; \
|
|
395
|
+
THIS_REQUEST(); \
|
|
396
|
+
return (__VA_ARGS__); \
|
|
397
|
+
}
|
|
398
|
+
REQUEST_GETTER(request_method, r->method)
|
|
399
|
+
REQUEST_GETTER(request_url, r->url)
|
|
400
|
+
REQUEST_GETTER(request_headers, r->headers)
|
|
401
|
+
REQUEST_GETTER(request_signal, r->signal)
|
|
402
|
+
REQUEST_GETTER(request_destination, (void)r, js_empty_string())
|
|
403
|
+
REQUEST_GETTER(request_referrer, (void)r, js_new_string_cstr(ctx, "about:client"))
|
|
404
|
+
REQUEST_GETTER(request_referrer_policy, (void)r, js_empty_string())
|
|
405
|
+
REQUEST_GETTER(request_mode, (void)r, js_new_string_cstr(ctx, "cors"))
|
|
406
|
+
REQUEST_GETTER(request_credentials, (void)r, js_new_string_cstr(ctx, "same-origin"))
|
|
407
|
+
REQUEST_GETTER(request_cache, (void)r, js_new_string_cstr(ctx, "default"))
|
|
408
|
+
REQUEST_GETTER(request_redirect, js_new_string_cstr(ctx, r->redirect == 1 ? "error" : r->redirect == 2 ? "manual" : "follow"))
|
|
409
|
+
REQUEST_GETTER(request_integrity, (void)r, js_empty_string())
|
|
410
|
+
REQUEST_GETTER(request_keepalive, js_bool(r->keepalive))
|
|
411
|
+
REQUEST_GETTER(request_false, (void)r, JS_FALSE)
|
|
412
|
+
REQUEST_GETTER(request_duplex, (void)r, js_new_string_cstr(ctx, "half"))
|
|
413
|
+
REQUEST_GETTER(request_body, body_stream(ctx, &r->obj.cell, &r->body))
|
|
414
|
+
REQUEST_GETTER(request_body_used, js_bool(body_used(&r->body)))
|
|
415
|
+
|
|
416
|
+
BUILTIN(request_clone) {
|
|
417
|
+
UNUSED_ARGS;
|
|
418
|
+
THIS_REQUEST();
|
|
419
|
+
if (body_used(&r->body)) return js_throw_type_error(ctx, "Request.clone: Body has already been consumed.");
|
|
420
|
+
JSValue headers = js_new_headers_from(ctx, r->headers);
|
|
421
|
+
if (js_is_exception(headers)) return headers;
|
|
422
|
+
RequestObject *c = (RequestObject *)js_alloc_object(ctx, JS_CELL_OBJECT,
|
|
423
|
+
js_shape_root(ctx, &request_class, ctx->intrinsics[JS_INTRINSIC_REQUEST_PROTOTYPE], 4));
|
|
424
|
+
init_body(&c->body);
|
|
425
|
+
js_init_slot(&c->method, r->method);
|
|
426
|
+
js_init_slot(&c->url, r->url);
|
|
427
|
+
js_init_slot(&c->headers, headers);
|
|
428
|
+
js_init_slot(&c->signal, JS_UNDEFINED);
|
|
429
|
+
c->redirect = r->redirect;
|
|
430
|
+
c->keepalive = r->keepalive;
|
|
431
|
+
JSValue cv = js_object_value(&c->obj);
|
|
432
|
+
JSValue signal = js_abort_signal_follow(ctx, r->signal);
|
|
433
|
+
if (js_is_exception(signal)) return signal;
|
|
434
|
+
js_store(ctx, &c->obj.cell, &c->signal, signal);
|
|
435
|
+
return clone_body(ctx, &r->obj.cell, &r->body, &c->obj.cell, &c->body) < 0 ? JS_EXCEPTION : cv;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
static JSValue request_consume(JSContext *ctx, JSValue this_val, int kind) {
|
|
439
|
+
RequestObject *r = as_request(this_val);
|
|
440
|
+
if (!r) {
|
|
441
|
+
JSValue e = js_throw_invalid_this(ctx, "Request");
|
|
442
|
+
(void)e;
|
|
443
|
+
return rejected(ctx, js_take_exception(ctx));
|
|
444
|
+
}
|
|
445
|
+
return consume(ctx, &r->obj.cell, &r->body, r->headers, kind);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
#define CONSUMER(fn, consumer, kind) \
|
|
449
|
+
BUILTIN(fn) { \
|
|
450
|
+
UNUSED_ARGS; \
|
|
451
|
+
return consumer(ctx, this_val, kind); \
|
|
452
|
+
}
|
|
453
|
+
CONSUMER(request_text, request_consume, AS_TEXT)
|
|
454
|
+
CONSUMER(request_json, request_consume, AS_JSON)
|
|
455
|
+
CONSUMER(request_array_buffer, request_consume, AS_ARRAY_BUFFER)
|
|
456
|
+
CONSUMER(request_bytes, request_consume, AS_BYTES)
|
|
457
|
+
CONSUMER(request_blob, request_consume, AS_BLOB)
|
|
458
|
+
|
|
459
|
+
BUILTIN(form_data_unsupported) {
|
|
460
|
+
UNUSED_ARGS;
|
|
461
|
+
JSValue e = js_throw_node_error(ctx, JS_TYPE_ERROR, "ERR_VISALJS_UNSUPPORTED", "formData() is not supported yet");
|
|
462
|
+
(void)e;
|
|
463
|
+
return rejected(ctx, js_take_exception(ctx));
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
static void field(JSContext *ctx, JSValue o, const char *name, JSValue v) {
|
|
467
|
+
js_define_data(ctx, js_as_object(o), js_atom_from_cstr(ctx, name), v, JS_PROP_DEFAULT);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
BUILTIN(request_inspect) {
|
|
471
|
+
UNUSED_ARGS;
|
|
472
|
+
THIS_REQUEST();
|
|
473
|
+
JSValue o = js_new_object(ctx);
|
|
474
|
+
field(ctx, o, "method", r->method);
|
|
475
|
+
field(ctx, o, "url", r->url);
|
|
476
|
+
field(ctx, o, "headers", r->headers);
|
|
477
|
+
field(ctx, o, "destination", js_empty_string());
|
|
478
|
+
field(ctx, o, "referrer", js_new_string_cstr(ctx, "about:client"));
|
|
479
|
+
field(ctx, o, "referrerPolicy", js_empty_string());
|
|
480
|
+
field(ctx, o, "mode", js_new_string_cstr(ctx, "cors"));
|
|
481
|
+
field(ctx, o, "credentials", js_new_string_cstr(ctx, "same-origin"));
|
|
482
|
+
field(ctx, o, "cache", js_new_string_cstr(ctx, "default"));
|
|
483
|
+
field(ctx, o, "redirect", js_new_string_cstr(ctx, r->redirect == 1 ? "error" : r->redirect == 2 ? "manual" : "follow"));
|
|
484
|
+
field(ctx, o, "integrity", js_empty_string());
|
|
485
|
+
field(ctx, o, "keepalive", js_bool(r->keepalive));
|
|
486
|
+
field(ctx, o, "isReloadNavigation", JS_FALSE);
|
|
487
|
+
field(ctx, o, "isHistoryNavigation", JS_FALSE);
|
|
488
|
+
field(ctx, o, "signal", r->signal);
|
|
489
|
+
return js_inspect_as(ctx, this_val, "Request", o, ARG(0), ARG(1));
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/* ---- Response (§5.5) ------------------------------------------------------------------------ */
|
|
493
|
+
|
|
494
|
+
static ResponseObject *alloc_response(JSContext *ctx, JSValue proto) {
|
|
495
|
+
ResponseObject *r = (ResponseObject *)js_alloc_object(ctx, JS_CELL_OBJECT, js_shape_root(ctx, &response_class, proto, 4));
|
|
496
|
+
init_body(&r->body);
|
|
497
|
+
js_init_slot(&r->status_text, js_empty_string());
|
|
498
|
+
js_init_slot(&r->headers, JS_UNDEFINED);
|
|
499
|
+
js_init_slot(&r->url, js_empty_string());
|
|
500
|
+
r->status = 200;
|
|
501
|
+
r->type = 0;
|
|
502
|
+
r->redirected = 0;
|
|
503
|
+
return r;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
static bool null_body_status(double s) { return s == 101 || s == 103 || s == 204 || s == 205 || s == 304; }
|
|
507
|
+
|
|
508
|
+
/* Initialize a response (§5.5): status, statusText, headers, then the body. 0, or -1. */
|
|
509
|
+
static int initialize(JSContext *ctx, ResponseObject *r, JSValue init, JSValue body, bool has_body) {
|
|
510
|
+
double status = 200;
|
|
511
|
+
JSValue st = get_option(ctx, init, "status");
|
|
512
|
+
if (js_is_exception(st)) return -1;
|
|
513
|
+
if (!js_is_undefined(st) && js_to_number(ctx, &status, st) < 0) return -1;
|
|
514
|
+
status = isfinite(status) ? trunc(status) : 0;
|
|
515
|
+
if (status < 200 || status > 599)
|
|
516
|
+
return fail(js_throw_range_error(ctx, "init[\"status\"] must be in the range of 200 to 599, inclusive."));
|
|
517
|
+
JSValue text = get_option(ctx, init, "statusText");
|
|
518
|
+
if (js_is_exception(text)) return -1;
|
|
519
|
+
if (!js_is_undefined(text)) {
|
|
520
|
+
text = js_to_string(ctx, text);
|
|
521
|
+
if (js_is_exception(text)) return -1;
|
|
522
|
+
JSStrView v = js_string_view(ctx, text);
|
|
523
|
+
for (uint32_t i = 0; i < v.length; i++) {
|
|
524
|
+
uint32_t c = js_view_at(v, i);
|
|
525
|
+
if (c == '\r' || c == '\n' || c > 255) return fail(js_throw_type_error(ctx, "Invalid statusText"));
|
|
526
|
+
}
|
|
527
|
+
js_store(ctx, &r->obj.cell, &r->status_text, text);
|
|
528
|
+
}
|
|
529
|
+
r->status = (uint16_t)status;
|
|
530
|
+
JSValue h = get_option(ctx, init, "headers");
|
|
531
|
+
if (js_is_exception(h)) return -1;
|
|
532
|
+
JSValue headers = js_new_headers_from(ctx, h);
|
|
533
|
+
if (js_is_exception(headers)) return -1;
|
|
534
|
+
js_store(ctx, &r->obj.cell, &r->headers, headers);
|
|
535
|
+
if (has_body) {
|
|
536
|
+
if (null_body_status(status)) return fail(js_throw_type_error(ctx, "Response constructor: Invalid response status code %d", (int)status));
|
|
537
|
+
JSValue type;
|
|
538
|
+
if (extract(ctx, &r->obj.cell, &r->body, body, &type) < 0) return -1;
|
|
539
|
+
if (!js_is_empty(type)) {
|
|
540
|
+
JSValue has = js_headers_get(ctx, headers, "content-type");
|
|
541
|
+
if (js_is_exception(has)) return -1;
|
|
542
|
+
if (js_is_null(has) && js_headers_append(ctx, headers, js_new_string_cstr(ctx, "content-type"), type) < 0) return -1;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return 0;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/* new Response(body = null, init) */
|
|
549
|
+
BUILTIN(response_ctor) {
|
|
550
|
+
(void)callee, (void)this_val;
|
|
551
|
+
if (js_is_undefined(new_target)) return js_throw_class_without_new(ctx, "_Response");
|
|
552
|
+
JSValue init = ARG(1);
|
|
553
|
+
if (!js_is_nullish(init) && !js_is_object(init))
|
|
554
|
+
return js_throw_type_error(ctx, "Response constructor: Expected init (\"Argument 2\") to be an instance of ResponseInit.");
|
|
555
|
+
JSValue proto = js_get_prototype_from_constructor(ctx, new_target, JS_INTRINSIC_RESPONSE_PROTOTYPE);
|
|
556
|
+
if (js_is_exception(proto)) return proto;
|
|
557
|
+
ResponseObject *r = alloc_response(ctx, proto);
|
|
558
|
+
JSValue rv = js_object_value(&r->obj);
|
|
559
|
+
return initialize(ctx, r, init, ARG(0), !js_is_nullish(ARG(0))) < 0 ? JS_EXCEPTION : rv;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/* Response.error() */
|
|
563
|
+
BUILTIN(response_error) {
|
|
564
|
+
UNUSED_ARGS;
|
|
565
|
+
ResponseObject *r = alloc_response(ctx, ctx->intrinsics[JS_INTRINSIC_RESPONSE_PROTOTYPE]);
|
|
566
|
+
JSValue rv = js_object_value(&r->obj);
|
|
567
|
+
r->status = 0;
|
|
568
|
+
r->type = 1;
|
|
569
|
+
JSValue headers = js_new_headers_from(ctx, JS_UNDEFINED);
|
|
570
|
+
if (js_is_exception(headers)) return headers;
|
|
571
|
+
js_headers_set_guard(headers, JS_HEADERS_IMMUTABLE);
|
|
572
|
+
js_store(ctx, &r->obj.cell, &r->headers, headers);
|
|
573
|
+
return rv;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/* Response.json(data, init) */
|
|
577
|
+
BUILTIN(response_json_static) {
|
|
578
|
+
UNUSED_ARGS;
|
|
579
|
+
JSValue s = js_json_stringify(ctx, ARG(0), JS_UNDEFINED, JS_UNDEFINED, NULL);
|
|
580
|
+
if (js_is_exception(s)) return s;
|
|
581
|
+
if (js_is_undefined(s)) return js_throw_type_error(ctx, "Value is not JSON serializable");
|
|
582
|
+
ResponseObject *r = alloc_response(ctx, ctx->intrinsics[JS_INTRINSIC_RESPONSE_PROTOTYPE]);
|
|
583
|
+
JSValue rv = js_object_value(&r->obj);
|
|
584
|
+
if (initialize(ctx, r, ARG(1), s, true) < 0) return JS_EXCEPTION;
|
|
585
|
+
/* extract gave text/plain: JSON's type replaces it unless the init set one */
|
|
586
|
+
JSValue h = get_option(ctx, ARG(1), "headers");
|
|
587
|
+
JSValue given = JS_NULL;
|
|
588
|
+
if (!js_is_nullish(h)) {
|
|
589
|
+
JSValue probe = js_new_headers_from(ctx, h);
|
|
590
|
+
if (js_is_exception(probe)) return probe;
|
|
591
|
+
given = js_headers_get(ctx, probe, "content-type");
|
|
592
|
+
}
|
|
593
|
+
if (js_is_null(given)) {
|
|
594
|
+
JSValue fresh = js_new_headers_from(ctx, h);
|
|
595
|
+
if (js_is_exception(fresh)) return fresh;
|
|
596
|
+
if (js_headers_append(ctx, fresh, js_new_string_cstr(ctx, "content-type"), js_new_string_cstr(ctx, "application/json")) < 0) return JS_EXCEPTION;
|
|
597
|
+
js_store(ctx, &r->obj.cell, &r->headers, fresh);
|
|
598
|
+
}
|
|
599
|
+
return rv;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/* Response.redirect(url, status = 302) */
|
|
603
|
+
BUILTIN(response_redirect) {
|
|
604
|
+
UNUSED_ARGS;
|
|
605
|
+
JSValue s = js_to_string(ctx, ARG(0));
|
|
606
|
+
if (js_is_exception(s)) return s;
|
|
607
|
+
JSValue url = js_parse_url_href(ctx, s);
|
|
608
|
+
if (js_is_exception(url)) return url;
|
|
609
|
+
if (js_is_undefined(url)) {
|
|
610
|
+
char text[256];
|
|
611
|
+
text[js_view_to_utf8(js_string_view(ctx, s), text, sizeof text - 1)] = '\0';
|
|
612
|
+
return js_throw_type_error(ctx, "Failed to parse URL from %s", text);
|
|
613
|
+
}
|
|
614
|
+
double status = 302;
|
|
615
|
+
if (!js_is_undefined(ARG(1)) && js_to_number(ctx, &status, ARG(1)) < 0) return JS_EXCEPTION;
|
|
616
|
+
if (status != 301 && status != 302 && status != 303 && status != 307 && status != 308)
|
|
617
|
+
return js_throw_range_error(ctx, "Invalid status code %g", status);
|
|
618
|
+
ResponseObject *r = alloc_response(ctx, ctx->intrinsics[JS_INTRINSIC_RESPONSE_PROTOTYPE]);
|
|
619
|
+
JSValue rv = js_object_value(&r->obj);
|
|
620
|
+
r->status = (uint16_t)status;
|
|
621
|
+
JSValue headers = js_new_headers_from(ctx, JS_UNDEFINED);
|
|
622
|
+
if (js_is_exception(headers)) return headers;
|
|
623
|
+
if (js_headers_append(ctx, headers, js_new_string_cstr(ctx, "location"), url) < 0) return JS_EXCEPTION;
|
|
624
|
+
js_headers_set_guard(headers, JS_HEADERS_IMMUTABLE);
|
|
625
|
+
js_store(ctx, &r->obj.cell, &r->headers, headers);
|
|
626
|
+
return rv;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
#define THIS_RESPONSE() \
|
|
630
|
+
ResponseObject *r = as_response(this_val); \
|
|
631
|
+
if (!r) return js_throw_invalid_this(ctx, "Response")
|
|
632
|
+
|
|
633
|
+
#define RESPONSE_GETTER(fn, expr) \
|
|
634
|
+
BUILTIN(fn) { \
|
|
635
|
+
UNUSED_ARGS; \
|
|
636
|
+
THIS_RESPONSE(); \
|
|
637
|
+
return (expr); \
|
|
638
|
+
}
|
|
639
|
+
static const char *const TYPES[] = { "default", "error", "basic", "cors", "opaque", "opaqueredirect" };
|
|
640
|
+
RESPONSE_GETTER(response_type, js_new_string_cstr(ctx, TYPES[r->type]))
|
|
641
|
+
RESPONSE_GETTER(response_url, r->url)
|
|
642
|
+
RESPONSE_GETTER(response_redirected, js_bool(r->redirected))
|
|
643
|
+
RESPONSE_GETTER(response_status, js_int32(r->status))
|
|
644
|
+
RESPONSE_GETTER(response_ok, js_bool(r->status >= 200 && r->status <= 299))
|
|
645
|
+
RESPONSE_GETTER(response_status_text, r->status_text)
|
|
646
|
+
RESPONSE_GETTER(response_headers, r->headers)
|
|
647
|
+
RESPONSE_GETTER(response_body, body_stream(ctx, &r->obj.cell, &r->body))
|
|
648
|
+
RESPONSE_GETTER(response_body_used, js_bool(body_used(&r->body)))
|
|
649
|
+
|
|
650
|
+
BUILTIN(response_clone) {
|
|
651
|
+
UNUSED_ARGS;
|
|
652
|
+
THIS_RESPONSE();
|
|
653
|
+
if (body_used(&r->body)) return js_throw_type_error(ctx, "Response.clone: Body has already been consumed.");
|
|
654
|
+
JSValue headers = js_new_headers_from(ctx, r->headers);
|
|
655
|
+
if (js_is_exception(headers)) return headers;
|
|
656
|
+
ResponseObject *c = alloc_response(ctx, ctx->intrinsics[JS_INTRINSIC_RESPONSE_PROTOTYPE]);
|
|
657
|
+
JSValue cv = js_object_value(&c->obj);
|
|
658
|
+
c->status = r->status;
|
|
659
|
+
c->type = r->type;
|
|
660
|
+
c->redirected = r->redirected;
|
|
661
|
+
js_store(ctx, &c->obj.cell, &c->status_text, r->status_text);
|
|
662
|
+
js_store(ctx, &c->obj.cell, &c->url, r->url);
|
|
663
|
+
js_store(ctx, &c->obj.cell, &c->headers, headers);
|
|
664
|
+
return clone_body(ctx, &r->obj.cell, &r->body, &c->obj.cell, &c->body) < 0 ? JS_EXCEPTION : cv;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
static JSValue response_consume(JSContext *ctx, JSValue this_val, int kind) {
|
|
668
|
+
ResponseObject *r = as_response(this_val);
|
|
669
|
+
if (!r) {
|
|
670
|
+
JSValue e = js_throw_invalid_this(ctx, "Response");
|
|
671
|
+
(void)e;
|
|
672
|
+
return rejected(ctx, js_take_exception(ctx));
|
|
673
|
+
}
|
|
674
|
+
return consume(ctx, &r->obj.cell, &r->body, r->headers, kind);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
CONSUMER(response_text, response_consume, AS_TEXT)
|
|
678
|
+
CONSUMER(response_json, response_consume, AS_JSON)
|
|
679
|
+
CONSUMER(response_array_buffer, response_consume, AS_ARRAY_BUFFER)
|
|
680
|
+
CONSUMER(response_bytes, response_consume, AS_BYTES)
|
|
681
|
+
CONSUMER(response_blob, response_consume, AS_BLOB)
|
|
682
|
+
|
|
683
|
+
BUILTIN(response_inspect) {
|
|
684
|
+
UNUSED_ARGS;
|
|
685
|
+
THIS_RESPONSE();
|
|
686
|
+
JSValue o = js_new_object(ctx);
|
|
687
|
+
field(ctx, o, "status", js_int32(r->status));
|
|
688
|
+
field(ctx, o, "statusText", r->status_text);
|
|
689
|
+
field(ctx, o, "headers", r->headers);
|
|
690
|
+
JSValue body = body_stream(ctx, &r->obj.cell, &r->body);
|
|
691
|
+
if (js_is_exception(body)) return body;
|
|
692
|
+
field(ctx, o, "body", body);
|
|
693
|
+
field(ctx, o, "bodyUsed", js_bool(body_used(&r->body)));
|
|
694
|
+
field(ctx, o, "ok", js_bool(r->status >= 200 && r->status <= 299));
|
|
695
|
+
field(ctx, o, "redirected", js_bool(r->redirected));
|
|
696
|
+
field(ctx, o, "type", js_new_string_cstr(ctx, TYPES[r->type]));
|
|
697
|
+
field(ctx, o, "url", r->url);
|
|
698
|
+
return js_inspect_as(ctx, this_val, "Response", o, ARG(0), ARG(1));
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/* ---- Realm ---------------------------------------------------------------------------------- */
|
|
702
|
+
|
|
703
|
+
static const JSBuiltin request_proto[] = {
|
|
704
|
+
WEBIDL_ATTRIBUTE("method", request_method, NULL),
|
|
705
|
+
WEBIDL_ATTRIBUTE("url", request_url, NULL),
|
|
706
|
+
WEBIDL_ATTRIBUTE("headers", request_headers, NULL),
|
|
707
|
+
WEBIDL_ATTRIBUTE("destination", request_destination, NULL),
|
|
708
|
+
WEBIDL_ATTRIBUTE("referrer", request_referrer, NULL),
|
|
709
|
+
WEBIDL_ATTRIBUTE("referrerPolicy", request_referrer_policy, NULL),
|
|
710
|
+
WEBIDL_ATTRIBUTE("mode", request_mode, NULL),
|
|
711
|
+
WEBIDL_ATTRIBUTE("credentials", request_credentials, NULL),
|
|
712
|
+
WEBIDL_ATTRIBUTE("cache", request_cache, NULL),
|
|
713
|
+
WEBIDL_ATTRIBUTE("redirect", request_redirect, NULL),
|
|
714
|
+
WEBIDL_ATTRIBUTE("integrity", request_integrity, NULL),
|
|
715
|
+
WEBIDL_ATTRIBUTE("keepalive", request_keepalive, NULL),
|
|
716
|
+
WEBIDL_ATTRIBUTE("isReloadNavigation", request_false, NULL),
|
|
717
|
+
WEBIDL_ATTRIBUTE("isHistoryNavigation", request_false, NULL),
|
|
718
|
+
WEBIDL_ATTRIBUTE("signal", request_signal, NULL),
|
|
719
|
+
WEBIDL_ATTRIBUTE("body", request_body, NULL),
|
|
720
|
+
WEBIDL_ATTRIBUTE("bodyUsed", request_body_used, NULL),
|
|
721
|
+
WEBIDL_ATTRIBUTE("duplex", request_duplex, NULL),
|
|
722
|
+
WEBIDL_OPERATION("clone", request_clone, 0),
|
|
723
|
+
WEBIDL_OPERATION("blob", request_blob, 0),
|
|
724
|
+
WEBIDL_OPERATION("arrayBuffer", request_array_buffer, 0),
|
|
725
|
+
WEBIDL_OPERATION("text", request_text, 0),
|
|
726
|
+
WEBIDL_OPERATION("json", request_json, 0),
|
|
727
|
+
WEBIDL_OPERATION("formData", form_data_unsupported, 0),
|
|
728
|
+
WEBIDL_OPERATION("bytes", request_bytes, 0),
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
static const JSBuiltin response_statics[] = {
|
|
732
|
+
WEBIDL_OPERATION("error", response_error, 0),
|
|
733
|
+
WEBIDL_OPERATION("json", response_json_static, 1),
|
|
734
|
+
WEBIDL_OPERATION("redirect", response_redirect, 1),
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
static const JSBuiltin response_proto[] = {
|
|
738
|
+
WEBIDL_ATTRIBUTE("type", response_type, NULL),
|
|
739
|
+
WEBIDL_ATTRIBUTE("url", response_url, NULL),
|
|
740
|
+
WEBIDL_ATTRIBUTE("redirected", response_redirected, NULL),
|
|
741
|
+
WEBIDL_ATTRIBUTE("status", response_status, NULL),
|
|
742
|
+
WEBIDL_ATTRIBUTE("ok", response_ok, NULL),
|
|
743
|
+
WEBIDL_ATTRIBUTE("statusText", response_status_text, NULL),
|
|
744
|
+
WEBIDL_ATTRIBUTE("headers", response_headers, NULL),
|
|
745
|
+
WEBIDL_ATTRIBUTE("body", response_body, NULL),
|
|
746
|
+
WEBIDL_ATTRIBUTE("bodyUsed", response_body_used, NULL),
|
|
747
|
+
WEBIDL_OPERATION("clone", response_clone, 0),
|
|
748
|
+
WEBIDL_OPERATION("blob", response_blob, 0),
|
|
749
|
+
WEBIDL_OPERATION("arrayBuffer", response_array_buffer, 0),
|
|
750
|
+
WEBIDL_OPERATION("text", response_text, 0),
|
|
751
|
+
WEBIDL_OPERATION("json", response_json, 0),
|
|
752
|
+
WEBIDL_OPERATION("formData", form_data_unsupported, 0),
|
|
753
|
+
WEBIDL_OPERATION("bytes", response_bytes, 0),
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
static void install_fetch(JSContext *ctx, JSObject *global) {
|
|
757
|
+
JSValue object_proto = ctx->intrinsics[JS_INTRINSIC_OBJECT_PROTOTYPE];
|
|
758
|
+
js_define_web_class(ctx, global, "Request", request_ctor, 1, object_proto, JS_INTRINSIC_REQUEST_PROTOTYPE);
|
|
759
|
+
JSValue qp = ctx->intrinsics[JS_INTRINSIC_REQUEST_PROTOTYPE];
|
|
760
|
+
js_define_builtins(ctx, js_as_object(qp), request_proto, JS_COUNTOF(request_proto));
|
|
761
|
+
js_define_inspect(ctx, qp, request_inspect, "Request");
|
|
762
|
+
/* undici leaves this behind on Request.prototype (an enumerable, read-only undefined) */
|
|
763
|
+
js_define_data(ctx, js_as_object(qp), js_atom_from_cstr(ctx, "attribute"), JS_UNDEFINED, JS_PROP_ENUMERABLE);
|
|
764
|
+
|
|
765
|
+
JSValue response = js_define_web_class(ctx, global, "Response", response_ctor, 0, object_proto, JS_INTRINSIC_RESPONSE_PROTOTYPE);
|
|
766
|
+
js_define_builtins(ctx, js_as_object(response), response_statics, JS_COUNTOF(response_statics));
|
|
767
|
+
JSValue rp = ctx->intrinsics[JS_INTRINSIC_RESPONSE_PROTOTYPE];
|
|
768
|
+
js_define_builtins(ctx, js_as_object(rp), response_proto, JS_COUNTOF(response_proto));
|
|
769
|
+
js_define_inspect(ctx, rp, response_inspect, "Response");
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
const JSComponent js_component_fetch = { "fetch", install_fetch };
|