goscript 0.1.0 → 0.1.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/README.md +267 -255
- package/cmd/goscript/cmd-test.go +286 -0
- package/cmd/goscript/cmd-test_test.go +76 -0
- package/cmd/goscript/cmd_compile.go +9 -0
- package/cmd/goscript/main.go +1 -0
- package/compiler/build-flags.go +38 -0
- package/compiler/compile-request.go +33 -0
- package/compiler/compiler.go +1 -1
- package/compiler/compliance_test.go +0 -10
- package/compiler/config.go +2 -0
- package/compiler/gotest/owner.go +24 -0
- package/compiler/gotest/package-result.go +69 -0
- package/compiler/gotest/request.go +210 -0
- package/compiler/gotest/result.go +28 -0
- package/compiler/gotest/runner.go +1225 -0
- package/compiler/gotest/runner_test.go +1271 -0
- package/compiler/gotest/test.go +9 -0
- package/compiler/index.test.ts +1 -1
- package/compiler/lowered-program.go +80 -21
- package/compiler/lowering.go +6754 -602
- package/compiler/override-facts.go +357 -0
- package/compiler/override-registry.go +52 -190
- package/compiler/override-registry_test.go +182 -0
- package/compiler/package-graph.go +50 -27
- package/compiler/package-graph_test.go +99 -9
- package/compiler/package-test-function.go +9 -0
- package/compiler/package-test-graph-package.go +40 -0
- package/compiler/package-test-graph-variant.go +129 -0
- package/compiler/package-test-graph.go +112 -0
- package/compiler/package-test-graph_test.go +202 -0
- package/compiler/runtime-contract.go +229 -29
- package/compiler/runtime-contract_test.go +44 -30
- package/compiler/semantic-model-types.go +25 -6
- package/compiler/semantic-model.go +819 -74
- package/compiler/semantic-model_test.go +104 -0
- package/compiler/service.go +10 -4
- package/compiler/skeleton_test.go +2777 -524
- package/compiler/tsworkspace/owner-process-unix_test.go +72 -0
- package/compiler/tsworkspace/owner.go +342 -0
- package/compiler/tsworkspace/owner_test.go +93 -0
- package/compiler/tsworkspace/result.go +17 -0
- package/compiler/tsworkspace/tool-process-other.go +14 -0
- package/compiler/tsworkspace/tool-process-unix.go +19 -0
- package/compiler/typescript-emitter.go +576 -86
- package/compiler/wasm/compile.go +1 -1
- package/compiler/wasm/compile_test.go +61 -11
- package/compiler/wasm_api.go +172 -7
- package/dist/gs/builtin/builtin.d.ts +40 -3
- package/dist/gs/builtin/builtin.js +430 -22
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/channel.d.ts +32 -10
- package/dist/gs/builtin/channel.js +119 -25
- package/dist/gs/builtin/channel.js.map +1 -1
- package/dist/gs/builtin/defer.d.ts +1 -0
- package/dist/gs/builtin/defer.js +12 -2
- package/dist/gs/builtin/defer.js.map +1 -1
- package/dist/gs/builtin/hostio.d.ts +9 -0
- package/dist/gs/builtin/hostio.js +25 -0
- package/dist/gs/builtin/hostio.js.map +1 -1
- package/dist/gs/builtin/map.js +40 -6
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/print.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +64 -10
- package/dist/gs/builtin/slice.js +619 -244
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/builtin/type.d.ts +7 -2
- package/dist/gs/builtin/type.js +128 -29
- package/dist/gs/builtin/type.js.map +1 -1
- package/dist/gs/builtin/varRef.d.ts +7 -0
- package/dist/gs/builtin/varRef.js +23 -0
- package/dist/gs/builtin/varRef.js.map +1 -1
- package/dist/gs/bytes/buffer.gs.js +74 -70
- package/dist/gs/bytes/buffer.gs.js.map +1 -1
- package/dist/gs/bytes/iter.gs.js +13 -13
- package/dist/gs/bytes/iter.gs.js.map +1 -1
- package/dist/gs/bytes/reader.gs.js +20 -18
- package/dist/gs/bytes/reader.gs.js.map +1 -1
- package/dist/gs/compress/zlib/index.d.ts +26 -0
- package/dist/gs/compress/zlib/index.js +168 -0
- package/dist/gs/compress/zlib/index.js.map +1 -0
- package/dist/gs/context/context.d.ts +6 -5
- package/dist/gs/context/context.js +17 -12
- package/dist/gs/context/context.js.map +1 -1
- package/dist/gs/crypto/ecdh/index.d.ts +52 -0
- package/dist/gs/crypto/ecdh/index.js +226 -0
- package/dist/gs/crypto/ecdh/index.js.map +1 -0
- package/dist/gs/crypto/ed25519/index.d.ts +34 -0
- package/dist/gs/crypto/ed25519/index.js +160 -0
- package/dist/gs/crypto/ed25519/index.js.map +1 -0
- package/dist/gs/crypto/internal/constanttime/index.d.ts +4 -0
- package/dist/gs/crypto/internal/constanttime/index.js +18 -0
- package/dist/gs/crypto/internal/constanttime/index.js.map +1 -0
- package/dist/gs/crypto/internal/fips140deps/byteorder/index.d.ts +1 -0
- package/dist/gs/crypto/internal/fips140deps/byteorder/index.js +2 -0
- package/dist/gs/crypto/internal/fips140deps/byteorder/index.js.map +1 -0
- package/dist/gs/crypto/internal/fips140deps/godebug/index.d.ts +1 -0
- package/dist/gs/crypto/internal/fips140deps/godebug/index.js +2 -0
- package/dist/gs/crypto/internal/fips140deps/godebug/index.js.map +1 -0
- package/dist/gs/crypto/rand/index.d.ts +2 -0
- package/dist/gs/crypto/rand/index.js +85 -0
- package/dist/gs/crypto/rand/index.js.map +1 -1
- package/dist/gs/crypto/sha256/index.d.ts +8 -0
- package/dist/gs/crypto/sha256/index.js +118 -0
- package/dist/gs/crypto/sha256/index.js.map +1 -0
- package/dist/gs/crypto/sha512/index.d.ts +14 -0
- package/dist/gs/crypto/sha512/index.js +129 -0
- package/dist/gs/crypto/sha512/index.js.map +1 -0
- package/dist/gs/embed/index.d.ts +7 -0
- package/dist/gs/embed/index.js +16 -0
- package/dist/gs/embed/index.js.map +1 -0
- package/dist/gs/encoding/json/index.d.ts +4 -0
- package/dist/gs/encoding/json/index.js +33 -0
- package/dist/gs/encoding/json/index.js.map +1 -1
- package/dist/gs/errors/errors.d.ts +4 -0
- package/dist/gs/errors/errors.js +108 -4
- package/dist/gs/errors/errors.js.map +1 -1
- package/dist/gs/fmt/fmt.d.ts +4 -4
- package/dist/gs/fmt/fmt.js +42 -11
- package/dist/gs/fmt/fmt.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +37 -2
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +245 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.d.ts +189 -0
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js +861 -0
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -0
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.d.ts +217 -0
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js +814 -0
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js.map +1 -0
- package/dist/gs/github.com/aperturerobotics/util/conc/index.d.ts +20 -0
- package/dist/gs/github.com/aperturerobotics/util/conc/index.js +134 -0
- package/dist/gs/github.com/aperturerobotics/util/conc/index.js.map +1 -0
- package/dist/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.js.map +1 -1
- package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.d.ts +3 -0
- package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js +50 -0
- package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js.map +1 -0
- package/dist/gs/github.com/klauspost/compress/internal/le/index.d.ts +9 -0
- package/dist/gs/github.com/klauspost/compress/internal/le/index.js +72 -0
- package/dist/gs/github.com/klauspost/compress/internal/le/index.js.map +1 -0
- package/dist/gs/github.com/mr-tron/base58/base58/index.d.ts +27 -0
- package/dist/gs/github.com/mr-tron/base58/base58/index.js +172 -0
- package/dist/gs/github.com/mr-tron/base58/base58/index.js.map +1 -0
- package/dist/gs/github.com/zeebo/blake3/internal/consts/index.d.ts +21 -0
- package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js +22 -0
- package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js.map +1 -0
- package/dist/gs/go/internal/scannerhooks/index.d.ts +3 -0
- package/dist/gs/go/internal/scannerhooks/index.js +5 -0
- package/dist/gs/go/internal/scannerhooks/index.js.map +1 -0
- package/dist/gs/go/scanner/index.d.ts +13 -0
- package/dist/gs/go/scanner/index.js +35 -0
- package/dist/gs/go/scanner/index.js.map +1 -1
- package/dist/gs/go/token/index.d.ts +156 -0
- package/dist/gs/go/token/index.js +507 -4
- package/dist/gs/go/token/index.js.map +1 -1
- package/dist/gs/hash/fnv/index.d.ts +57 -0
- package/dist/gs/hash/fnv/index.js +299 -0
- package/dist/gs/hash/fnv/index.js.map +1 -0
- package/dist/gs/hash/index.d.ts +17 -0
- package/dist/gs/hash/index.js +94 -0
- package/dist/gs/hash/index.js.map +1 -0
- package/dist/gs/internal/abi/index.d.ts +4 -0
- package/dist/gs/internal/abi/index.js +10 -0
- package/dist/gs/internal/abi/index.js.map +1 -1
- package/dist/gs/internal/bytealg/index.d.ts +2 -0
- package/dist/gs/internal/bytealg/index.js +14 -0
- package/dist/gs/internal/bytealg/index.js.map +1 -1
- package/dist/gs/internal/byteorder/index.d.ts +8 -2
- package/dist/gs/internal/byteorder/index.js +56 -25
- package/dist/gs/internal/byteorder/index.js.map +1 -1
- package/dist/gs/internal/godebug/index.d.ts +12 -0
- package/dist/gs/internal/godebug/index.js +30 -0
- package/dist/gs/internal/godebug/index.js.map +1 -0
- package/dist/gs/io/fs/index.d.ts +1 -0
- package/dist/gs/io/fs/index.js +1 -0
- package/dist/gs/io/fs/index.js.map +1 -1
- package/dist/gs/io/fs/readlink.d.ts +8 -0
- package/dist/gs/io/fs/readlink.js +60 -0
- package/dist/gs/io/fs/readlink.js.map +1 -0
- package/dist/gs/io/fs/walk.d.ts +3 -3
- package/dist/gs/io/fs/walk.js +7 -7
- package/dist/gs/io/fs/walk.js.map +1 -1
- package/dist/gs/io/io.d.ts +40 -6
- package/dist/gs/io/io.js +151 -26
- package/dist/gs/io/io.js.map +1 -1
- package/dist/gs/iter/iter.d.ts +3 -2
- package/dist/gs/iter/iter.js.map +1 -1
- package/dist/gs/maps/iter.d.ts +5 -5
- package/dist/gs/maps/iter.js +48 -21
- package/dist/gs/maps/iter.js.map +1 -1
- package/dist/gs/maps/maps.d.ts +6 -6
- package/dist/gs/maps/maps.js +1 -1
- package/dist/gs/maps/maps.js.map +1 -1
- package/dist/gs/math/bits/index.d.ts +13 -4
- package/dist/gs/math/bits/index.js +70 -48
- package/dist/gs/math/bits/index.js.map +1 -1
- package/dist/gs/math/const.gs.d.ts +5 -5
- package/dist/gs/math/const.gs.js +4 -4
- package/dist/gs/math/const.gs.js.map +1 -1
- package/dist/gs/mime/index.d.ts +1 -0
- package/dist/gs/mime/index.js +52 -0
- package/dist/gs/mime/index.js.map +1 -0
- package/dist/gs/net/http/httptest/index.d.ts +30 -0
- package/dist/gs/net/http/httptest/index.js +101 -0
- package/dist/gs/net/http/httptest/index.js.map +1 -0
- package/dist/gs/net/http/index.d.ts +131 -0
- package/dist/gs/net/http/index.js +307 -0
- package/dist/gs/net/http/index.js.map +1 -0
- package/dist/gs/net/http/pprof/index.d.ts +8 -0
- package/dist/gs/net/http/pprof/index.js +59 -0
- package/dist/gs/net/http/pprof/index.js.map +1 -0
- package/dist/gs/os/dir_unix.gs.js +2 -2
- package/dist/gs/os/dir_unix.gs.js.map +1 -1
- package/dist/gs/os/error.gs.js +9 -7
- package/dist/gs/os/error.gs.js.map +1 -1
- package/dist/gs/os/types_js.gs.js +95 -15
- package/dist/gs/os/types_js.gs.js.map +1 -1
- package/dist/gs/path/filepath/match.js +165 -3
- package/dist/gs/path/filepath/match.js.map +1 -1
- package/dist/gs/path/filepath/path.d.ts +8 -4
- package/dist/gs/path/filepath/path.js +192 -8
- package/dist/gs/path/filepath/path.js.map +1 -1
- package/dist/gs/path/path.d.ts +4 -1
- package/dist/gs/path/path.js +16 -4
- package/dist/gs/path/path.js.map +1 -1
- package/dist/gs/reflect/index.d.ts +4 -3
- package/dist/gs/reflect/index.js +3 -2
- package/dist/gs/reflect/index.js.map +1 -1
- package/dist/gs/reflect/iter.js +2 -2
- package/dist/gs/reflect/iter.js.map +1 -1
- package/dist/gs/reflect/map.js +29 -0
- package/dist/gs/reflect/map.js.map +1 -1
- package/dist/gs/reflect/type.d.ts +31 -9
- package/dist/gs/reflect/type.js +536 -43
- package/dist/gs/reflect/type.js.map +1 -1
- package/dist/gs/reflect/types.d.ts +1 -0
- package/dist/gs/reflect/types.js +3 -1
- package/dist/gs/reflect/types.js.map +1 -1
- package/dist/gs/reflect/value.d.ts +4 -1
- package/dist/gs/reflect/value.js +39 -1
- package/dist/gs/reflect/value.js.map +1 -1
- package/dist/gs/reflect/visiblefields.js +1 -1
- package/dist/gs/reflect/visiblefields.js.map +1 -1
- package/dist/gs/runtime/debug/index.d.ts +41 -0
- package/dist/gs/runtime/debug/index.js +66 -0
- package/dist/gs/runtime/debug/index.js.map +1 -0
- package/dist/gs/runtime/pprof/index.d.ts +20 -0
- package/dist/gs/runtime/pprof/index.js +85 -0
- package/dist/gs/runtime/pprof/index.js.map +1 -0
- package/dist/gs/runtime/runtime.d.ts +35 -3
- package/dist/gs/runtime/runtime.js +72 -0
- package/dist/gs/runtime/runtime.js.map +1 -1
- package/dist/gs/runtime/trace/index.d.ts +19 -0
- package/dist/gs/runtime/trace/index.js +64 -0
- package/dist/gs/runtime/trace/index.js.map +1 -0
- package/dist/gs/slices/slices.d.ts +42 -8
- package/dist/gs/slices/slices.js +425 -11
- package/dist/gs/slices/slices.js.map +1 -1
- package/dist/gs/sort/slice.gs.d.ts +5 -3
- package/dist/gs/sort/slice.gs.js +60 -22
- package/dist/gs/sort/slice.gs.js.map +1 -1
- package/dist/gs/sort/sort.gs.d.ts +4 -4
- package/dist/gs/sort/sort.gs.js +11 -8
- package/dist/gs/sort/sort.gs.js.map +1 -1
- package/dist/gs/strings/builder.d.ts +1 -1
- package/dist/gs/strings/builder.js +29 -19
- package/dist/gs/strings/builder.js.map +1 -1
- package/dist/gs/strings/iter.js +140 -75
- package/dist/gs/strings/iter.js.map +1 -1
- package/dist/gs/strings/replace.js +2 -2
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/strings/strings.js +52 -6
- package/dist/gs/strings/strings.js.map +1 -1
- package/dist/gs/sync/atomic/type.gs.d.ts +9 -8
- package/dist/gs/sync/atomic/type.gs.js +0 -2
- package/dist/gs/sync/atomic/type.gs.js.map +1 -1
- package/dist/gs/sync/sync.d.ts +8 -3
- package/dist/gs/sync/sync.js +66 -11
- package/dist/gs/sync/sync.js.map +1 -1
- package/dist/gs/syscall/constants.d.ts +36 -24
- package/dist/gs/syscall/constants.js +12 -0
- package/dist/gs/syscall/constants.js.map +1 -1
- package/dist/gs/syscall/errors.d.ts +117 -111
- package/dist/gs/syscall/errors.js +45 -0
- package/dist/gs/syscall/errors.js.map +1 -1
- package/dist/gs/syscall/fs.d.ts +37 -0
- package/dist/gs/syscall/fs.js +102 -0
- package/dist/gs/syscall/fs.js.map +1 -1
- package/dist/gs/syscall/js/index.d.ts +90 -0
- package/dist/gs/syscall/js/index.js +383 -0
- package/dist/gs/syscall/js/index.js.map +1 -0
- package/dist/gs/syscall/types.d.ts +26 -1
- package/dist/gs/syscall/types.js +45 -1
- package/dist/gs/syscall/types.js.map +1 -1
- package/dist/gs/testing/index.d.ts +1 -0
- package/dist/gs/testing/index.js +2 -0
- package/dist/gs/testing/index.js.map +1 -0
- package/dist/gs/testing/testing.d.ts +78 -0
- package/dist/gs/testing/testing.js +318 -0
- package/dist/gs/testing/testing.js.map +1 -0
- package/dist/gs/time/time.d.ts +41 -4
- package/dist/gs/time/time.js +227 -36
- package/dist/gs/time/time.js.map +1 -1
- package/dist/gs/unicode/unicode.d.ts +23 -1
- package/dist/gs/unicode/unicode.js +79 -10
- package/dist/gs/unicode/unicode.js.map +1 -1
- package/dist/gs/unicode/utf8/utf8.d.ts +4 -4
- package/dist/gs/unicode/utf8/utf8.js +24 -11
- package/dist/gs/unicode/utf8/utf8.js.map +1 -1
- package/dist/gs/unique/index.d.ts +11 -0
- package/dist/gs/unique/index.js +76 -0
- package/dist/gs/unique/index.js.map +1 -0
- package/go.mod +8 -8
- package/go.sum +14 -14
- package/gs/builtin/builtin.ts +585 -27
- package/gs/builtin/channel.ts +183 -29
- package/gs/builtin/defer.ts +13 -2
- package/gs/builtin/hostio.test.ts +1 -0
- package/gs/builtin/hostio.ts +38 -0
- package/gs/builtin/map.ts +46 -6
- package/gs/builtin/print.ts +12 -3
- package/gs/builtin/runtime-contract.test.ts +383 -10
- package/gs/builtin/slice.test.ts +70 -0
- package/gs/builtin/slice.ts +785 -265
- package/gs/builtin/type.ts +160 -41
- package/gs/builtin/varRef.ts +40 -1
- package/gs/bytes/buffer.gs.ts +74 -70
- package/gs/bytes/iter.gs.ts +13 -14
- package/gs/bytes/meta.json +8 -3
- package/gs/bytes/reader.gs.ts +20 -19
- package/gs/compress/zlib/index.test.ts +28 -0
- package/gs/compress/zlib/index.ts +200 -0
- package/gs/compress/zlib/meta.json +3 -0
- package/gs/context/context.test.ts +71 -0
- package/gs/context/context.ts +30 -29
- package/gs/crypto/ecdh/index.test.ts +43 -0
- package/gs/crypto/ecdh/index.ts +274 -0
- package/gs/crypto/ed25519/index.test.ts +41 -0
- package/gs/crypto/ed25519/index.ts +238 -0
- package/gs/crypto/ed25519/meta.json +13 -0
- package/gs/crypto/internal/constanttime/index.test.ts +25 -0
- package/gs/crypto/internal/constanttime/index.ts +22 -0
- package/gs/crypto/internal/fips140deps/byteorder/index.ts +1 -0
- package/gs/crypto/internal/fips140deps/godebug/index.ts +1 -0
- package/gs/crypto/rand/index.test.ts +89 -1
- package/gs/crypto/rand/index.ts +103 -1
- package/gs/crypto/rand/meta.json +4 -1
- package/gs/crypto/sha256/index.test.ts +78 -0
- package/gs/crypto/sha256/index.ts +150 -0
- package/gs/crypto/sha256/meta.json +9 -0
- package/gs/crypto/sha512/index.test.ts +31 -0
- package/gs/crypto/sha512/index.ts +161 -0
- package/gs/crypto/sha512/meta.json +11 -0
- package/gs/embed/index.ts +20 -0
- package/gs/embed/meta.json +5 -0
- package/gs/encoding/json/index.test.ts +39 -3
- package/gs/encoding/json/index.ts +45 -3
- package/gs/errors/errors.test.ts +85 -0
- package/gs/errors/errors.ts +132 -4
- package/gs/fmt/fmt.test.ts +3 -1
- package/gs/fmt/fmt.ts +55 -19
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +128 -1
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +342 -4
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +180 -0
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +1084 -0
- package/gs/github.com/aperturerobotics/starpc/srpc/index.test.ts +31 -0
- package/gs/github.com/aperturerobotics/starpc/srpc/index.ts +1233 -0
- package/gs/github.com/aperturerobotics/starpc/srpc/meta.json +46 -0
- package/gs/github.com/aperturerobotics/util/conc/index.test.ts +30 -0
- package/gs/github.com/aperturerobotics/util/conc/index.ts +172 -0
- package/gs/github.com/aperturerobotics/util/conc/meta.json +9 -0
- package/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.ts +1 -4
- package/gs/github.com/hack-pad/safejs/internal/catch/index.test.ts +35 -0
- package/gs/github.com/hack-pad/safejs/internal/catch/index.ts +65 -0
- package/gs/github.com/hack-pad/safejs/internal/catch/meta.json +9 -0
- package/gs/github.com/klauspost/compress/internal/le/index.test.ts +37 -0
- package/gs/github.com/klauspost/compress/internal/le/index.ts +115 -0
- package/gs/github.com/mr-tron/base58/base58/index.test.ts +70 -0
- package/gs/github.com/mr-tron/base58/base58/index.ts +231 -0
- package/gs/github.com/mr-tron/base58/base58/meta.json +3 -0
- package/gs/github.com/zeebo/blake3/internal/consts/index.test.ts +46 -0
- package/gs/github.com/zeebo/blake3/internal/consts/index.ts +26 -0
- package/gs/go/internal/scannerhooks/index.test.ts +14 -0
- package/gs/go/internal/scannerhooks/index.ts +9 -0
- package/gs/go/scanner/index.test.ts +22 -0
- package/gs/go/scanner/index.ts +47 -0
- package/gs/go/token/index.test.ts +47 -1
- package/gs/go/token/index.ts +583 -4
- package/gs/hash/fnv/index.test.ts +67 -0
- package/gs/hash/fnv/index.ts +351 -0
- package/gs/hash/fnv/meta.json +3 -0
- package/gs/hash/index.test.ts +37 -0
- package/gs/hash/index.ts +118 -0
- package/gs/hash/meta.json +5 -0
- package/gs/internal/abi/index.test.ts +18 -0
- package/gs/internal/abi/index.ts +14 -0
- package/gs/internal/bytealg/index.test.ts +18 -0
- package/gs/internal/bytealg/index.ts +16 -0
- package/gs/internal/byteorder/index.test.ts +39 -0
- package/gs/internal/byteorder/index.ts +100 -27
- package/gs/internal/godebug/index.test.ts +16 -0
- package/gs/internal/godebug/index.ts +35 -0
- package/gs/io/fs/index.ts +1 -0
- package/gs/io/fs/meta.json +5 -0
- package/gs/io/fs/readlink.test.ts +43 -0
- package/gs/io/fs/readlink.ts +69 -0
- package/gs/io/fs/walk.test.ts +61 -0
- package/gs/io/fs/walk.ts +17 -9
- package/gs/io/io.ts +177 -31
- package/gs/io/meta.json +10 -2
- package/gs/iter/iter.ts +8 -2
- package/gs/maps/iter.ts +75 -26
- package/gs/maps/maps.test.ts +23 -0
- package/gs/maps/maps.ts +13 -11
- package/gs/math/bits/index.test.ts +20 -0
- package/gs/math/bits/index.ts +107 -64
- package/gs/math/const.gs.test.ts +11 -5
- package/gs/math/const.gs.ts +5 -6
- package/gs/mime/index.ts +60 -0
- package/gs/net/http/httptest/index.test.ts +53 -0
- package/gs/net/http/httptest/index.ts +120 -0
- package/gs/net/http/index.test.ts +148 -0
- package/gs/net/http/index.ts +432 -0
- package/gs/net/http/meta.json +6 -0
- package/gs/net/http/pprof/index.test.ts +47 -0
- package/gs/net/http/pprof/index.ts +65 -0
- package/gs/os/dir_unix.gs.ts +2 -3
- package/gs/os/error.gs.ts +9 -10
- package/gs/os/error.test.ts +41 -0
- package/gs/os/file_unix_js.test.ts +55 -0
- package/gs/os/tempfile.gs.test.ts +37 -10
- package/gs/os/types_js.gs.ts +96 -17
- package/gs/path/filepath/match.test.ts +31 -12
- package/gs/path/filepath/match.ts +181 -3
- package/gs/path/filepath/meta.json +6 -0
- package/gs/path/filepath/path.test.ts +80 -0
- package/gs/path/filepath/path.ts +244 -11
- package/gs/path/path.ts +20 -5
- package/gs/reflect/field.test.ts +63 -0
- package/gs/reflect/index.ts +5 -1
- package/gs/reflect/iter.ts +2 -2
- package/gs/reflect/map.test.ts +42 -1
- package/gs/reflect/map.ts +39 -0
- package/gs/reflect/type.ts +728 -65
- package/gs/reflect/typefor.test.ts +100 -0
- package/gs/reflect/types.ts +3 -1
- package/gs/reflect/value.ts +50 -1
- package/gs/reflect/visiblefields.ts +1 -1
- package/gs/runtime/debug/index.test.ts +45 -0
- package/gs/runtime/debug/index.ts +96 -0
- package/gs/runtime/pprof/index.test.ts +36 -0
- package/gs/runtime/pprof/index.ts +104 -0
- package/gs/runtime/pprof/meta.json +6 -0
- package/gs/runtime/runtime.test.ts +19 -0
- package/gs/runtime/runtime.ts +98 -3
- package/gs/runtime/trace/index.test.ts +45 -0
- package/gs/runtime/trace/index.ts +97 -0
- package/gs/runtime/trace/meta.json +7 -0
- package/gs/slices/meta.json +2 -1
- package/gs/slices/slices.test.ts +180 -0
- package/gs/slices/slices.ts +502 -15
- package/gs/sort/meta.json +7 -0
- package/gs/sort/slice.gs.ts +85 -26
- package/gs/sort/slice.test.ts +40 -0
- package/gs/sort/sort.gs.ts +16 -13
- package/gs/strings/builder.test.ts +8 -0
- package/gs/strings/builder.ts +33 -20
- package/gs/strings/iter.test.ts +5 -7
- package/gs/strings/iter.ts +146 -71
- package/gs/strings/replace.test.ts +1 -4
- package/gs/strings/replace.ts +6 -6
- package/gs/strings/strings.test.ts +4 -0
- package/gs/strings/strings.ts +54 -6
- package/gs/sync/atomic/type.gs.ts +13 -14
- package/gs/sync/meta.json +3 -1
- package/gs/sync/sync.test.ts +69 -1
- package/gs/sync/sync.ts +72 -13
- package/gs/syscall/constants.ts +39 -24
- package/gs/syscall/errors.ts +165 -112
- package/gs/syscall/fs.ts +195 -0
- package/gs/syscall/js/index.ts +485 -0
- package/gs/syscall/js/meta.json +4 -0
- package/gs/syscall/net.test.ts +111 -0
- package/gs/syscall/types.ts +63 -2
- package/gs/testing/index.ts +1 -0
- package/gs/testing/meta.json +5 -0
- package/gs/testing/testing.test.ts +146 -0
- package/gs/testing/testing.ts +399 -0
- package/gs/time/meta.json +2 -2
- package/gs/time/time.test.ts +110 -0
- package/gs/time/time.ts +309 -57
- package/gs/unicode/unicode.test.ts +36 -0
- package/gs/unicode/unicode.ts +115 -9
- package/gs/unicode/utf8/utf8.test.ts +13 -0
- package/gs/unicode/utf8/utf8.ts +28 -16
- package/gs/unique/index.ts +98 -0
- package/package.json +3 -2
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import * as $ from '@goscript/builtin/index.js'
|
|
2
|
+
import * as errors from '@goscript/errors/index.js'
|
|
3
|
+
import * as io from '@goscript/io/index.js'
|
|
4
|
+
|
|
5
|
+
export type Resetter = {
|
|
6
|
+
Reset(r: io.Reader | null, dict: $.Bytes | null): Promise<$.GoError>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export let ErrChecksum = errors.New('zlib: invalid checksum')
|
|
10
|
+
export let ErrDictionary = errors.New('zlib: invalid dictionary')
|
|
11
|
+
export let ErrHeader = errors.New('zlib: invalid header')
|
|
12
|
+
|
|
13
|
+
export function __goscript_set_ErrChecksum(value: $.GoError): void {
|
|
14
|
+
ErrChecksum = value
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function __goscript_set_ErrDictionary(value: $.GoError): void {
|
|
18
|
+
ErrDictionary = value
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function __goscript_set_ErrHeader(value: $.GoError): void {
|
|
22
|
+
ErrHeader = value
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class zlibReader implements io.ReadCloser {
|
|
26
|
+
private offset = 0
|
|
27
|
+
|
|
28
|
+
constructor(private data: Uint8Array) {}
|
|
29
|
+
|
|
30
|
+
Read(p: $.Bytes): [number, $.GoError] {
|
|
31
|
+
if (this.offset >= this.data.length) {
|
|
32
|
+
return [0, io.EOF]
|
|
33
|
+
}
|
|
34
|
+
const out = $.bytesToUint8Array(p)
|
|
35
|
+
const n = Math.min(out.length, this.data.length - this.offset)
|
|
36
|
+
out.set(this.data.subarray(this.offset, this.offset + n))
|
|
37
|
+
this.offset += n
|
|
38
|
+
return [n, null]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
Close(): $.GoError {
|
|
42
|
+
return null
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class Writer {
|
|
47
|
+
private chunks: Uint8Array[] = []
|
|
48
|
+
private closed = false
|
|
49
|
+
|
|
50
|
+
constructor(private w: io.Writer | null) {}
|
|
51
|
+
|
|
52
|
+
Write(p: $.Bytes): [number, $.GoError] {
|
|
53
|
+
if (this.closed) {
|
|
54
|
+
return [0, errors.New('zlib: writer closed')]
|
|
55
|
+
}
|
|
56
|
+
const data = $.bytesToUint8Array(p)
|
|
57
|
+
this.chunks.push(data.slice())
|
|
58
|
+
return [data.length, null]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
Close(): $.GoError {
|
|
62
|
+
if (this.closed) {
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
65
|
+
this.closed = true
|
|
66
|
+
if (this.w == null) {
|
|
67
|
+
return errors.New('zlib: nil writer')
|
|
68
|
+
}
|
|
69
|
+
const compressed = deflate(concat(this.chunks))
|
|
70
|
+
const [, err] = this.w.Write(compressed)
|
|
71
|
+
return err
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
Flush(): $.GoError {
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
Reset(w: io.Writer | null): void {
|
|
79
|
+
this.w = w
|
|
80
|
+
this.chunks = []
|
|
81
|
+
this.closed = false
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function NewWriter(w: io.Writer | null): Writer {
|
|
86
|
+
return new Writer(w)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function NewWriterLevel(
|
|
90
|
+
w: io.Writer | null,
|
|
91
|
+
_level: number,
|
|
92
|
+
): [Writer | null, $.GoError] {
|
|
93
|
+
return [new Writer(w), null]
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function NewWriterLevelDict(
|
|
97
|
+
w: io.Writer | null,
|
|
98
|
+
_level: number,
|
|
99
|
+
_dict: $.Bytes | null,
|
|
100
|
+
): [Writer | null, $.GoError] {
|
|
101
|
+
return [new Writer(w), null]
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function NewReader(
|
|
105
|
+
r: io.Reader | null,
|
|
106
|
+
): [io.ReadCloser | null, $.GoError] {
|
|
107
|
+
return NewReaderDict(r, null)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function NewReaderDict(
|
|
111
|
+
r: io.Reader | null,
|
|
112
|
+
_dict: $.Bytes | null,
|
|
113
|
+
): [io.ReadCloser | null, $.GoError] {
|
|
114
|
+
if (r == null) {
|
|
115
|
+
return [null, errors.New('zlib: nil reader')]
|
|
116
|
+
}
|
|
117
|
+
const [data, readErr] = readAll(r)
|
|
118
|
+
if (readErr != null) {
|
|
119
|
+
return [null, readErr]
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
const out = inflate($.bytesToUint8Array(data))
|
|
123
|
+
return [new zlibReader(out), null]
|
|
124
|
+
} catch {
|
|
125
|
+
return [null, ErrHeader]
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function deflate(data: Uint8Array): Uint8Array {
|
|
130
|
+
const zlib = nodeZlib()
|
|
131
|
+
return new Uint8Array(zlib.deflateSync(data))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function inflate(data: Uint8Array): Uint8Array {
|
|
135
|
+
const zlib = nodeZlib()
|
|
136
|
+
return new Uint8Array(zlib.inflateSync(data))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function nodeZlib(): any {
|
|
140
|
+
const processObj = (globalThis as any).process
|
|
141
|
+
if (processObj && typeof processObj.getBuiltinModule === 'function') {
|
|
142
|
+
const mod = processObj.getBuiltinModule('zlib')
|
|
143
|
+
if (mod && typeof mod.deflateSync === 'function') {
|
|
144
|
+
return mod
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const requireFn = (() => {
|
|
148
|
+
try {
|
|
149
|
+
return Function(
|
|
150
|
+
"return typeof require !== 'undefined' ? require : null",
|
|
151
|
+
)() as ((specifier: string) => unknown) | null
|
|
152
|
+
} catch {
|
|
153
|
+
return null
|
|
154
|
+
}
|
|
155
|
+
})()
|
|
156
|
+
if (requireFn != null) {
|
|
157
|
+
for (const specifier of ['node:zlib', 'zlib']) {
|
|
158
|
+
try {
|
|
159
|
+
const mod = requireFn(specifier) as any
|
|
160
|
+
if (mod && typeof mod.deflateSync === 'function') {
|
|
161
|
+
return mod
|
|
162
|
+
}
|
|
163
|
+
} catch {
|
|
164
|
+
// Try the next fallback.
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
throw new Error('compress/zlib: node zlib module unavailable')
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function readAll(r: io.Reader): [Uint8Array, $.GoError] {
|
|
172
|
+
const chunks: Uint8Array[] = []
|
|
173
|
+
const buf = $.makeSlice<number>(32 * 1024, undefined, 'byte')
|
|
174
|
+
while (true) {
|
|
175
|
+
const [n, err] = r.Read(buf)
|
|
176
|
+
if (n > 0) {
|
|
177
|
+
chunks.push($.bytesToUint8Array($.goSlice(buf, 0, n)).slice())
|
|
178
|
+
}
|
|
179
|
+
if (err != null) {
|
|
180
|
+
if (err === io.EOF) {
|
|
181
|
+
return [concat(chunks), null]
|
|
182
|
+
}
|
|
183
|
+
return [new Uint8Array(0), err]
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function concat(chunks: Uint8Array[]): Uint8Array {
|
|
189
|
+
let total = 0
|
|
190
|
+
for (const chunk of chunks) {
|
|
191
|
+
total += chunk.length
|
|
192
|
+
}
|
|
193
|
+
const out = new Uint8Array(total)
|
|
194
|
+
let offset = 0
|
|
195
|
+
for (const chunk of chunks) {
|
|
196
|
+
out.set(chunk, offset)
|
|
197
|
+
offset += chunk.length
|
|
198
|
+
}
|
|
199
|
+
return out
|
|
200
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import * as $ from '@goscript/builtin/index.js'
|
|
4
|
+
|
|
5
|
+
import { AfterFunc, WithCancel, Background, WithValue } from './index.js'
|
|
6
|
+
|
|
7
|
+
async function nextMicrotask(): Promise<void> {
|
|
8
|
+
await new Promise<void>((resolve) => queueMicrotask(resolve))
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe('context override', () => {
|
|
12
|
+
it('matches generated struct keys by Go comparable value', () => {
|
|
13
|
+
class Key {
|
|
14
|
+
public _fields: Record<string, $.VarRef<unknown>>
|
|
15
|
+
|
|
16
|
+
constructor() {
|
|
17
|
+
this._fields = {}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ctx = WithValue(
|
|
22
|
+
Background(),
|
|
23
|
+
$.interfaceValue($.markAsStructValue(new Key()), 'example.key'),
|
|
24
|
+
'stored',
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
expect(
|
|
28
|
+
ctx.Value($.interfaceValue($.markAsStructValue(new Key()), 'example.key')),
|
|
29
|
+
).toBe('stored')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('runs AfterFunc after cancellation', async () => {
|
|
33
|
+
const [ctx, cancel] = WithCancel(Background())
|
|
34
|
+
let called = false
|
|
35
|
+
|
|
36
|
+
const stop = AfterFunc(ctx, () => {
|
|
37
|
+
called = true
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
cancel?.()
|
|
41
|
+
await nextMicrotask()
|
|
42
|
+
await nextMicrotask()
|
|
43
|
+
|
|
44
|
+
expect(called).toBe(true)
|
|
45
|
+
expect(stop()).toBe(false)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('stops AfterFunc before cancellation', async () => {
|
|
49
|
+
const [ctx, cancel] = WithCancel(Background())
|
|
50
|
+
let called = false
|
|
51
|
+
|
|
52
|
+
const stop = AfterFunc(ctx, () => {
|
|
53
|
+
called = true
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
expect(stop()).toBe(true)
|
|
57
|
+
cancel?.()
|
|
58
|
+
await nextMicrotask()
|
|
59
|
+
await nextMicrotask()
|
|
60
|
+
|
|
61
|
+
expect(called).toBe(false)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('accepts nil AfterFunc callbacks for type compatibility', () => {
|
|
65
|
+
const [ctx] = WithCancel(Background())
|
|
66
|
+
|
|
67
|
+
const stop = AfterFunc(ctx, null)
|
|
68
|
+
|
|
69
|
+
expect(stop()).toBe(true)
|
|
70
|
+
})
|
|
71
|
+
})
|
package/gs/context/context.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import * as $ from '@goscript/builtin/index.js'
|
|
2
|
+
import * as time from '@goscript/time/index.js'
|
|
2
3
|
|
|
3
4
|
export const Canceled = $.newError('context canceled')
|
|
4
5
|
|
|
5
6
|
export const DeadlineExceeded = $.newError('context deadline exceeded')
|
|
6
7
|
|
|
7
8
|
// Function types
|
|
8
|
-
export type CancelFunc = () => void
|
|
9
|
+
export type CancelFunc = (() => void) | null
|
|
9
10
|
export type CancelCauseFunc = (cause: $.GoError) => void
|
|
10
11
|
|
|
11
12
|
// Context interface matching Go's context.Context
|
|
12
13
|
export type Context = null | {
|
|
13
14
|
// Deadline returns the time when work done on behalf of this context should be canceled
|
|
14
|
-
Deadline(): [
|
|
15
|
+
Deadline(): [time.Time, boolean]
|
|
15
16
|
|
|
16
17
|
// Done returns a channel that's closed when work done on behalf of this context should be canceled
|
|
17
18
|
Done(): $.Channel<{}>
|
|
@@ -28,7 +29,7 @@ export type ContextNonNil = Exclude<Context, null>
|
|
|
28
29
|
|
|
29
30
|
// Base implementation for all contexts
|
|
30
31
|
abstract class baseContext implements ContextNonNil {
|
|
31
|
-
abstract Deadline(): [
|
|
32
|
+
abstract Deadline(): [time.Time, boolean]
|
|
32
33
|
abstract Done(): $.Channel<{}>
|
|
33
34
|
abstract Err(): $.GoError
|
|
34
35
|
abstract Value(key: any): any
|
|
@@ -42,8 +43,8 @@ class backgroundContext extends baseContext {
|
|
|
42
43
|
return backgroundContext.neverClosedChannel
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
Deadline(): [
|
|
46
|
-
return [
|
|
46
|
+
Deadline(): [time.Time, boolean] {
|
|
47
|
+
return [new time.Time(), false]
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
Done(): $.Channel<{}> {
|
|
@@ -73,7 +74,7 @@ class valueContext extends baseContext {
|
|
|
73
74
|
return this.parent
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
Deadline(): [
|
|
77
|
+
Deadline(): [time.Time, boolean] {
|
|
77
78
|
return this.parent.Deadline()
|
|
78
79
|
}
|
|
79
80
|
|
|
@@ -86,7 +87,7 @@ class valueContext extends baseContext {
|
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
Value(key: any): any {
|
|
89
|
-
if (this.key === key) {
|
|
90
|
+
if (this.key === key || $.comparableEqual(this.key, key)) {
|
|
90
91
|
return this.val
|
|
91
92
|
}
|
|
92
93
|
return this.parent.Value(key)
|
|
@@ -109,7 +110,7 @@ class cancelContext extends baseContext {
|
|
|
109
110
|
this.doneChannel = $.makeChannel<{}>(0, {}, 'both')
|
|
110
111
|
}
|
|
111
112
|
|
|
112
|
-
Deadline(): [
|
|
113
|
+
Deadline(): [time.Time, boolean] {
|
|
113
114
|
return this.parent.Deadline()
|
|
114
115
|
}
|
|
115
116
|
|
|
@@ -195,21 +196,20 @@ class cancelContext extends baseContext {
|
|
|
195
196
|
|
|
196
197
|
// Timer context with deadline
|
|
197
198
|
class timerContext extends cancelContext {
|
|
198
|
-
private deadline:
|
|
199
|
+
private deadline: time.Time
|
|
199
200
|
private timer: any
|
|
200
201
|
|
|
201
|
-
constructor(parent: ContextNonNil, deadline:
|
|
202
|
+
constructor(parent: ContextNonNil, deadline: time.Time) {
|
|
202
203
|
super(parent)
|
|
203
|
-
this.deadline = deadline
|
|
204
|
+
this.deadline = deadline.clone()
|
|
204
205
|
}
|
|
205
206
|
|
|
206
|
-
Deadline(): [
|
|
207
|
-
return [this.deadline, true]
|
|
207
|
+
Deadline(): [time.Time, boolean] {
|
|
208
|
+
return [this.deadline.clone(), true]
|
|
208
209
|
}
|
|
209
210
|
|
|
210
211
|
startTimer(): void {
|
|
211
|
-
const
|
|
212
|
-
const duration = this.deadline.getTime() - now
|
|
212
|
+
const duration = this.deadline.Sub(time.Now()) / 1000000
|
|
213
213
|
|
|
214
214
|
if (duration <= 0) {
|
|
215
215
|
// Already expired
|
|
@@ -237,8 +237,8 @@ class withoutCancelContext extends baseContext {
|
|
|
237
237
|
super()
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
-
Deadline(): [
|
|
241
|
-
return [
|
|
240
|
+
Deadline(): [time.Time, boolean] {
|
|
241
|
+
return [new time.Time(), false]
|
|
242
242
|
}
|
|
243
243
|
|
|
244
244
|
Done(): $.Channel<{}> {
|
|
@@ -305,7 +305,7 @@ export function WithCancelCause(
|
|
|
305
305
|
// WithDeadline returns a copy of parent with the deadline adjusted to be no later than d
|
|
306
306
|
export function WithDeadline(
|
|
307
307
|
parent: Context,
|
|
308
|
-
d:
|
|
308
|
+
d: time.Time,
|
|
309
309
|
): [ContextNonNil, CancelFunc] {
|
|
310
310
|
return WithDeadlineCause(parent, d, null)
|
|
311
311
|
}
|
|
@@ -313,7 +313,7 @@ export function WithDeadline(
|
|
|
313
313
|
// WithDeadlineCause is like WithDeadline but also sets the cause
|
|
314
314
|
export function WithDeadlineCause(
|
|
315
315
|
parent: Context,
|
|
316
|
-
d:
|
|
316
|
+
d: time.Time,
|
|
317
317
|
cause: $.GoError,
|
|
318
318
|
): [ContextNonNil, CancelFunc] {
|
|
319
319
|
if (parent === null) {
|
|
@@ -321,7 +321,7 @@ export function WithDeadlineCause(
|
|
|
321
321
|
}
|
|
322
322
|
// Check if parent deadline is already earlier
|
|
323
323
|
const [parentDeadline, ok] = parent.Deadline()
|
|
324
|
-
if (ok && parentDeadline
|
|
324
|
+
if (ok && (parentDeadline.Before(d) || parentDeadline.Equal(d))) {
|
|
325
325
|
// Parent deadline is already sooner
|
|
326
326
|
return WithCancel(parent)
|
|
327
327
|
}
|
|
@@ -343,7 +343,7 @@ export function WithTimeout(
|
|
|
343
343
|
parent: Context,
|
|
344
344
|
timeout: number,
|
|
345
345
|
): [ContextNonNil, CancelFunc] {
|
|
346
|
-
return WithDeadline(parent,
|
|
346
|
+
return WithDeadline(parent, time.Now().Add(timeout))
|
|
347
347
|
}
|
|
348
348
|
|
|
349
349
|
// WithTimeoutCause is like WithTimeout but also sets the cause
|
|
@@ -352,11 +352,7 @@ export function WithTimeoutCause(
|
|
|
352
352
|
timeout: number,
|
|
353
353
|
cause: $.GoError,
|
|
354
354
|
): [ContextNonNil, CancelFunc] {
|
|
355
|
-
return WithDeadlineCause(
|
|
356
|
-
parent,
|
|
357
|
-
new Date(Date.now() + timeout / 1000000),
|
|
358
|
-
cause,
|
|
359
|
-
)
|
|
355
|
+
return WithDeadlineCause(parent, time.Now().Add(timeout), cause)
|
|
360
356
|
}
|
|
361
357
|
|
|
362
358
|
// WithValue returns a copy of parent with the value associated with key
|
|
@@ -392,8 +388,8 @@ export function Cause(ctx: Context): $.GoError {
|
|
|
392
388
|
return c.Err()
|
|
393
389
|
}
|
|
394
390
|
|
|
395
|
-
// AfterFunc runs f in a separate goroutine after ctx is done
|
|
396
|
-
export function AfterFunc(ctx: Context, f: () => void): () => boolean {
|
|
391
|
+
// AfterFunc runs f in a separate goroutine after ctx is done.
|
|
392
|
+
export function AfterFunc(ctx: Context, f: (() => void) | null): () => boolean {
|
|
397
393
|
if (ctx === null) {
|
|
398
394
|
throw new Error('cannot create context from nil parent')
|
|
399
395
|
}
|
|
@@ -409,7 +405,12 @@ export function AfterFunc(ctx: Context, f: () => void): () => boolean {
|
|
|
409
405
|
if (!stopped) {
|
|
410
406
|
done = true
|
|
411
407
|
// Run in next tick to simulate goroutine
|
|
412
|
-
|
|
408
|
+
queueMicrotask(() => {
|
|
409
|
+
if (f === null) {
|
|
410
|
+
throw new Error('context: nil AfterFunc callback')
|
|
411
|
+
}
|
|
412
|
+
f()
|
|
413
|
+
})
|
|
413
414
|
}
|
|
414
415
|
})()
|
|
415
416
|
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { X25519 } from './index.js'
|
|
4
|
+
|
|
5
|
+
describe('crypto/ecdh override', () => {
|
|
6
|
+
it('matches the RFC 7748 X25519 test vector', () => {
|
|
7
|
+
const curve = X25519()
|
|
8
|
+
const [alice, aliceErr] = curve.NewPrivateKey(
|
|
9
|
+
hex('77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a'),
|
|
10
|
+
)
|
|
11
|
+
const [bobPub, bobPubErr] = curve.NewPublicKey(
|
|
12
|
+
hex('de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f'),
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
expect(aliceErr).toBeNull()
|
|
16
|
+
expect(bobPubErr).toBeNull()
|
|
17
|
+
expect(toHex(alice!.PublicKey().Bytes())).toBe(
|
|
18
|
+
'8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a',
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
const [shared, sharedErr] = alice!.ECDH(bobPub)
|
|
22
|
+
expect(sharedErr).toBeNull()
|
|
23
|
+
expect(toHex(shared)).toBe(
|
|
24
|
+
'4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742',
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('rejects low-order remote points', () => {
|
|
29
|
+
const [priv] = X25519().NewPrivateKey(new Uint8Array(32).fill(7))
|
|
30
|
+
const [zero] = X25519().NewPublicKey(new Uint8Array(32))
|
|
31
|
+
const [, err] = priv!.ECDH(zero)
|
|
32
|
+
|
|
33
|
+
expect(err?.Error()).toContain('low order point')
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
function hex(value: string): Uint8Array {
|
|
38
|
+
return new Uint8Array(value.match(/../g)!.map((byte) => Number.parseInt(byte, 16)))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function toHex(value: Uint8Array | null): string {
|
|
42
|
+
return Array.from(value ?? [], (byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
43
|
+
}
|