goscript 0.1.1 → 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/cmd/goscript/cmd-test.go +104 -11
- package/cmd/goscript/cmd-test_test.go +1 -1
- package/cmd/goscript/cmd_compile.go +9 -0
- package/compiler/compile-request.go +31 -0
- package/compiler/compiler.go +1 -1
- package/compiler/compliance_test.go +0 -2
- package/compiler/config.go +2 -0
- package/compiler/gotest/package-result.go +2 -0
- package/compiler/gotest/request.go +85 -20
- package/compiler/gotest/runner.go +733 -96
- package/compiler/gotest/runner_test.go +647 -3
- package/compiler/lowered-program.go +9 -2
- package/compiler/lowering.go +2001 -345
- package/compiler/override-facts.go +77 -27
- package/compiler/override-registry.go +5 -4
- package/compiler/override-registry_test.go +135 -0
- package/compiler/package-graph_test.go +62 -7
- package/compiler/package-test-graph-variant.go +40 -16
- package/compiler/package-test-graph.go +0 -5
- package/compiler/package-test-graph_test.go +61 -3
- package/compiler/runtime-contract.go +40 -0
- package/compiler/semantic-model-types.go +16 -0
- package/compiler/semantic-model.go +336 -91
- package/compiler/semantic-model_test.go +50 -1
- package/compiler/service.go +9 -3
- package/compiler/skeleton_test.go +1921 -298
- package/compiler/tsworkspace/owner-process-unix_test.go +72 -0
- package/compiler/tsworkspace/owner.go +8 -0
- package/compiler/tsworkspace/tool-process-other.go +14 -0
- package/compiler/tsworkspace/tool-process-unix.go +19 -0
- package/compiler/typescript-emitter.go +122 -9
- package/dist/gs/builtin/builtin.d.ts +20 -1
- package/dist/gs/builtin/builtin.js +246 -26
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/channel.d.ts +24 -10
- package/dist/gs/builtin/channel.js +107 -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 +43 -9
- package/dist/gs/builtin/slice.js +437 -234
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/builtin/type.d.ts +2 -0
- package/dist/gs/builtin/type.js +47 -7
- package/dist/gs/builtin/type.js.map +1 -1
- package/dist/gs/builtin/varRef.d.ts +2 -0
- package/dist/gs/builtin/varRef.js.map +1 -1
- package/dist/gs/bytes/buffer.gs.js +28 -28
- 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/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 +1 -1
- package/dist/gs/context/context.js +8 -3
- 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/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/encoding/json/index.d.ts +3 -0
- package/dist/gs/encoding/json/index.js +15 -0
- package/dist/gs/encoding/json/index.js.map +1 -1
- package/dist/gs/errors/errors.js +29 -6
- package/dist/gs/errors/errors.js.map +1 -1
- package/dist/gs/fmt/fmt.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +7 -7
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +52 -18
- 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.js +56 -20
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.d.ts +57 -3
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js +366 -1
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js.map +1 -1
- 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.js +3 -2
- package/dist/gs/github.com/klauspost/compress/internal/le/index.js.map +1 -1
- 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/token/index.js +11 -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/io/fs/readlink.js +2 -6
- package/dist/gs/io/fs/readlink.js.map +1 -1
- package/dist/gs/io/fs/walk.js.map +1 -1
- 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/math/bits/index.js +14 -24
- package/dist/gs/math/bits/index.js.map +1 -1
- package/dist/gs/mime/index.js +3 -1
- package/dist/gs/mime/index.js.map +1 -1
- package/dist/gs/net/http/httptest/index.d.ts +20 -1
- package/dist/gs/net/http/httptest/index.js +83 -3
- package/dist/gs/net/http/httptest/index.js.map +1 -1
- package/dist/gs/net/http/index.d.ts +110 -6
- package/dist/gs/net/http/index.js +262 -16
- package/dist/gs/net/http/index.js.map +1 -1
- 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/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.map +1 -1
- package/dist/gs/path/filepath/path.d.ts +5 -3
- package/dist/gs/path/filepath/path.js +65 -10
- package/dist/gs/path/filepath/path.js.map +1 -1
- package/dist/gs/reflect/index.d.ts +3 -2
- package/dist/gs/reflect/index.js +2 -1
- 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 +26 -0
- package/dist/gs/reflect/map.js.map +1 -1
- package/dist/gs/reflect/type.d.ts +24 -5
- package/dist/gs/reflect/type.js +390 -38
- 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 +39 -0
- package/dist/gs/runtime/debug/index.js +58 -0
- package/dist/gs/runtime/debug/index.js.map +1 -1
- 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/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 +24 -9
- package/dist/gs/slices/slices.js +229 -24
- 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 +55 -17
- package/dist/gs/sort/slice.gs.js.map +1 -1
- package/dist/gs/strings/builder.js +26 -17
- 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/sync.d.ts +6 -3
- package/dist/gs/sync/sync.js +39 -11
- package/dist/gs/sync/sync.js.map +1 -1
- package/dist/gs/syscall/errors.d.ts +116 -112
- package/dist/gs/syscall/errors.js +38 -1
- package/dist/gs/syscall/errors.js.map +1 -1
- package/dist/gs/syscall/fs.d.ts +2 -8
- package/dist/gs/syscall/fs.js.map +1 -1
- package/dist/gs/syscall/js/index.js +20 -12
- package/dist/gs/syscall/js/index.js.map +1 -1
- package/dist/gs/syscall/types.d.ts +4 -1
- package/dist/gs/syscall/types.js.map +1 -1
- package/dist/gs/testing/testing.d.ts +4 -3
- package/dist/gs/testing/testing.js +21 -4
- package/dist/gs/testing/testing.js.map +1 -1
- package/dist/gs/time/time.js +22 -0
- package/dist/gs/time/time.js.map +1 -1
- package/dist/gs/unicode/unicode.js.map +1 -1
- package/dist/gs/unique/index.js +7 -2
- package/dist/gs/unique/index.js.map +1 -1
- package/go.mod +8 -8
- package/go.sum +14 -23
- package/gs/builtin/builtin.ts +364 -37
- package/gs/builtin/channel.ts +161 -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 +257 -10
- package/gs/builtin/slice.test.ts +70 -0
- package/gs/builtin/slice.ts +566 -255
- package/gs/builtin/type.ts +53 -9
- package/gs/builtin/varRef.ts +2 -0
- package/gs/bytes/buffer.gs.ts +28 -28
- package/gs/bytes/iter.gs.ts +13 -14
- 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 +31 -1
- package/gs/context/context.ts +9 -4
- 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/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/encoding/json/index.test.ts +25 -3
- package/gs/encoding/json/index.ts +21 -3
- package/gs/errors/errors.test.ts +4 -1
- package/gs/errors/errors.ts +32 -8
- package/gs/fmt/fmt.test.ts +3 -1
- package/gs/fmt/fmt.ts +1 -5
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +62 -7
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +78 -36
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +32 -11
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +122 -43
- package/gs/github.com/aperturerobotics/starpc/srpc/index.test.ts +31 -0
- package/gs/github.com/aperturerobotics/starpc/srpc/index.ts +518 -4
- package/gs/github.com/aperturerobotics/starpc/srpc/meta.json +6 -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 +2 -1
- package/gs/github.com/klauspost/compress/internal/le/index.ts +6 -5
- 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/token/index.ts +17 -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/byteorder/index.test.ts +6 -6
- package/gs/io/fs/readlink.ts +40 -48
- package/gs/io/fs/walk.ts +10 -2
- package/gs/io/io.ts +4 -1
- package/gs/iter/iter.ts +8 -2
- package/gs/maps/iter.ts +69 -26
- package/gs/maps/maps.test.ts +23 -0
- package/gs/maps/maps.ts +6 -6
- package/gs/math/bits/index.test.ts +20 -0
- package/gs/math/bits/index.ts +15 -28
- package/gs/mime/index.ts +8 -2
- package/gs/net/http/httptest/index.test.ts +53 -0
- package/gs/net/http/httptest/index.ts +98 -3
- package/gs/net/http/index.test.ts +129 -1
- package/gs/net/http/index.ts +370 -19
- 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/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 +94 -15
- package/gs/path/filepath/match.ts +4 -1
- package/gs/path/filepath/meta.json +6 -0
- package/gs/path/filepath/path.test.ts +57 -2
- package/gs/path/filepath/path.ts +91 -12
- package/gs/reflect/field.test.ts +63 -0
- package/gs/reflect/index.ts +4 -1
- package/gs/reflect/iter.ts +2 -2
- package/gs/reflect/map.test.ts +24 -2
- package/gs/reflect/map.ts +35 -0
- package/gs/reflect/type.ts +543 -60
- 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 +22 -1
- package/gs/runtime/debug/index.ts +88 -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/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 +86 -0
- package/gs/slices/slices.ts +284 -37
- package/gs/sort/slice.gs.ts +73 -23
- package/gs/sort/slice.test.ts +40 -0
- package/gs/strings/builder.test.ts +8 -0
- package/gs/strings/builder.ts +29 -17
- 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/sync.test.ts +57 -1
- package/gs/sync/sync.ts +45 -13
- package/gs/syscall/errors.ts +158 -115
- package/gs/syscall/fs.ts +8 -8
- package/gs/syscall/js/index.ts +49 -22
- package/gs/syscall/net.test.ts +26 -0
- package/gs/syscall/types.ts +7 -2
- package/gs/testing/testing.test.ts +56 -0
- package/gs/testing/testing.ts +27 -10
- package/gs/time/meta.json +2 -2
- package/gs/time/time.test.ts +4 -0
- package/gs/time/time.ts +33 -2
- package/gs/unicode/unicode.test.ts +14 -3
- package/gs/unicode/unicode.ts +1 -5
- package/gs/unique/index.ts +9 -2
- package/package.json +3 -3
package/gs/io/fs/readlink.ts
CHANGED
|
@@ -9,56 +9,48 @@ export type ReadLinkFS =
|
|
|
9
9
|
Lstat(name: string): [FileInfo, $.GoError]
|
|
10
10
|
} & FS)
|
|
11
11
|
|
|
12
|
-
$.registerInterfaceType(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
name: '
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
name: 'Error',
|
|
30
|
-
args: [],
|
|
31
|
-
returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }],
|
|
32
|
-
},
|
|
33
|
-
],
|
|
34
|
-
},
|
|
12
|
+
$.registerInterfaceType('ReadLinkFS', null, [
|
|
13
|
+
{
|
|
14
|
+
name: 'ReadLink',
|
|
15
|
+
args: [{ name: 'name', type: { kind: $.TypeKind.Basic, name: 'string' } }],
|
|
16
|
+
returns: [
|
|
17
|
+
{ type: { kind: $.TypeKind.Basic, name: 'string' } },
|
|
18
|
+
{
|
|
19
|
+
type: {
|
|
20
|
+
kind: $.TypeKind.Interface,
|
|
21
|
+
name: 'GoError',
|
|
22
|
+
methods: [
|
|
23
|
+
{
|
|
24
|
+
name: 'Error',
|
|
25
|
+
args: [],
|
|
26
|
+
returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }],
|
|
27
|
+
},
|
|
28
|
+
],
|
|
35
29
|
},
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
{
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
],
|
|
56
|
-
},
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'Lstat',
|
|
35
|
+
args: [{ name: 'name', type: { kind: $.TypeKind.Basic, name: 'string' } }],
|
|
36
|
+
returns: [
|
|
37
|
+
{ type: 'FileInfo' },
|
|
38
|
+
{
|
|
39
|
+
type: {
|
|
40
|
+
kind: $.TypeKind.Interface,
|
|
41
|
+
name: 'GoError',
|
|
42
|
+
methods: [
|
|
43
|
+
{
|
|
44
|
+
name: 'Error',
|
|
45
|
+
args: [],
|
|
46
|
+
returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }],
|
|
47
|
+
},
|
|
48
|
+
],
|
|
57
49
|
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
)
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
])
|
|
62
54
|
|
|
63
55
|
export function ReadLink(fsys: FS, name: string): [string, $.GoError] {
|
|
64
56
|
const { value: sym, ok } = $.typeAssert<ReadLinkFS>(fsys, 'ReadLinkFS')
|
package/gs/io/fs/walk.ts
CHANGED
|
@@ -12,7 +12,11 @@ export let SkipDir: $.GoError = errors.New('skip this directory')
|
|
|
12
12
|
export let SkipAll: $.GoError = errors.New('skip everything and stop the walk')
|
|
13
13
|
|
|
14
14
|
export type WalkDirFunc =
|
|
15
|
-
| ((
|
|
15
|
+
| ((
|
|
16
|
+
path: string,
|
|
17
|
+
d: DirEntry,
|
|
18
|
+
err: $.GoError,
|
|
19
|
+
) => $.GoError | Promise<$.GoError>)
|
|
16
20
|
| null
|
|
17
21
|
|
|
18
22
|
// walkDir recursively descends path, calling walkDirFn.
|
|
@@ -79,7 +83,11 @@ export async function walkDir(
|
|
|
79
83
|
//
|
|
80
84
|
// WalkDir does not follow symbolic links found in directories,
|
|
81
85
|
// but if root itself is a symbolic link, its target will be walked.
|
|
82
|
-
export async function WalkDir(
|
|
86
|
+
export async function WalkDir(
|
|
87
|
+
fsys: FS,
|
|
88
|
+
root: string,
|
|
89
|
+
fn: WalkDirFunc,
|
|
90
|
+
): Promise<$.GoError> {
|
|
83
91
|
let [info, err] = Stat(fsys, root)
|
|
84
92
|
if (err != null) {
|
|
85
93
|
err = await fn!(root, null, err)
|
package/gs/io/io.ts
CHANGED
|
@@ -547,7 +547,10 @@ export async function ReadAtLeast(
|
|
|
547
547
|
}
|
|
548
548
|
|
|
549
549
|
// ReadFull reads exactly len(buf) bytes from r into buf
|
|
550
|
-
export async function ReadFull(
|
|
550
|
+
export async function ReadFull(
|
|
551
|
+
r: Reader,
|
|
552
|
+
buf: $.Bytes,
|
|
553
|
+
): Promise<[number, $.GoError]> {
|
|
551
554
|
return await ReadAtLeast(r, buf, $.len(buf))
|
|
552
555
|
}
|
|
553
556
|
|
package/gs/iter/iter.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
// TypeScript implementation of Go's iter package
|
|
2
2
|
// This provides iterator types and functions
|
|
3
3
|
|
|
4
|
+
export type YieldResult = boolean | globalThis.Promise<boolean>
|
|
5
|
+
|
|
4
6
|
// Seq is an iterator over sequences of individual values
|
|
5
|
-
export type Seq<V> = (
|
|
7
|
+
export type Seq<V> = (
|
|
8
|
+
_yield: ((value: V) => YieldResult) | null,
|
|
9
|
+
) => void | globalThis.Promise<void>
|
|
6
10
|
|
|
7
11
|
// Seq2 is an iterator over sequences of pairs of values
|
|
8
|
-
export type Seq2<K, V> = (
|
|
12
|
+
export type Seq2<K, V> = (
|
|
13
|
+
_yield: ((key: K, value: V) => YieldResult) | null,
|
|
14
|
+
) => void | globalThis.Promise<void>
|
|
9
15
|
|
|
10
16
|
// Pull converts the "push-style" iterator sequence seq into a "pull-style" iterator
|
|
11
17
|
// Returns a function that returns the next value and a boolean indicating if iteration should continue
|
package/gs/maps/iter.ts
CHANGED
|
@@ -5,51 +5,39 @@ import * as iter from '@goscript/iter/index.js'
|
|
|
5
5
|
// All returns an iterator over key-value pairs from m.
|
|
6
6
|
// The iteration order is not specified and is not guaranteed
|
|
7
7
|
// to be the same from one call to the next.
|
|
8
|
-
export function All<K extends $.Comparable, V>(
|
|
8
|
+
export function All<K extends $.Comparable | null, V>(
|
|
9
9
|
m: Map<K, V> | null,
|
|
10
10
|
): iter.Seq2<K, V> {
|
|
11
|
-
return (_yield: ((p0: K, p1: V) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
11
|
+
return (_yield: ((p0: K, p1: V) => iter.YieldResult) | null):
|
|
12
|
+
| void
|
|
13
|
+
| globalThis.Promise<void> => iteratePairs(m?.entries() ?? [], _yield)
|
|
18
14
|
}
|
|
19
15
|
|
|
20
16
|
// Keys returns an iterator over keys in m.
|
|
21
17
|
// The iteration order is not specified and is not guaranteed
|
|
22
18
|
// to be the same from one call to the next.
|
|
23
|
-
export function Keys<K extends $.Comparable, V>(
|
|
19
|
+
export function Keys<K extends $.Comparable | null, V>(
|
|
24
20
|
m: Map<K, V> | null,
|
|
25
21
|
): iter.Seq<K> {
|
|
26
|
-
return (_yield: ((p0: K) =>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
22
|
+
return (_yield: ((p0: K) => iter.YieldResult) | null):
|
|
23
|
+
| void
|
|
24
|
+
| globalThis.Promise<void> => iterateValues(mapKeys(m), _yield)
|
|
33
25
|
}
|
|
34
26
|
|
|
35
27
|
// Values returns an iterator over values in m.
|
|
36
28
|
// The iteration order is not specified and is not guaranteed
|
|
37
29
|
// to be the same from one call to the next.
|
|
38
|
-
export function Values<K extends $.Comparable, V>(
|
|
30
|
+
export function Values<K extends $.Comparable | null, V>(
|
|
39
31
|
m: Map<K, V> | null,
|
|
40
32
|
): iter.Seq<V> {
|
|
41
|
-
return (_yield: ((p0: V) =>
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
33
|
+
return (_yield: ((p0: V) => iter.YieldResult) | null):
|
|
34
|
+
| void
|
|
35
|
+
| globalThis.Promise<void> => iterateValues(mapValues(m), _yield)
|
|
48
36
|
}
|
|
49
37
|
|
|
50
38
|
// Insert adds the key-value pairs from seq to m.
|
|
51
39
|
// If a key in seq already exists in m, its value will be overwritten.
|
|
52
|
-
export function Insert<K extends $.Comparable, V>(
|
|
40
|
+
export function Insert<K extends $.Comparable | null, V>(
|
|
53
41
|
m: Map<K, V>,
|
|
54
42
|
seq: iter.Seq2<K, V>,
|
|
55
43
|
): void {
|
|
@@ -64,10 +52,65 @@ export function Insert<K extends $.Comparable, V>(
|
|
|
64
52
|
|
|
65
53
|
// Collect collects key-value pairs from seq into a new map
|
|
66
54
|
// and returns it.
|
|
67
|
-
export function Collect<K extends $.Comparable, V>(
|
|
55
|
+
export function Collect<K extends $.Comparable | null, V>(
|
|
68
56
|
seq: iter.Seq2<K, V>,
|
|
69
57
|
): Map<K, V> {
|
|
70
58
|
let m = $.makeMap<K, V>()
|
|
71
59
|
Insert(m, seq)
|
|
72
60
|
return m
|
|
73
61
|
}
|
|
62
|
+
|
|
63
|
+
function mapKeys<K extends $.Comparable | null, V>(m: Map<K, V> | null): K[] {
|
|
64
|
+
return Array.from(m?.keys() ?? [])
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function mapValues<K extends $.Comparable | null, V>(m: Map<K, V> | null): V[] {
|
|
68
|
+
return Array.from(m?.values() ?? [])
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function iterateValues<T>(
|
|
72
|
+
values: Iterable<T>,
|
|
73
|
+
yieldValue: ((value: T) => iter.YieldResult) | null,
|
|
74
|
+
): void | globalThis.Promise<void> {
|
|
75
|
+
const items = Array.from(values)
|
|
76
|
+
const walk = (idx: number): void | globalThis.Promise<void> => {
|
|
77
|
+
for (; idx < items.length; idx++) {
|
|
78
|
+
const keepGoing = yieldValue!(items[idx])
|
|
79
|
+
if (keepGoing instanceof Promise) {
|
|
80
|
+
return keepGoing.then((next) => {
|
|
81
|
+
if (next) {
|
|
82
|
+
return walk(idx + 1)
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
if (!keepGoing) {
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return walk(0)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function iteratePairs<K, V>(
|
|
95
|
+
entries: Iterable<[K, V]>,
|
|
96
|
+
yieldValue: ((key: K, value: V) => iter.YieldResult) | null,
|
|
97
|
+
): void | globalThis.Promise<void> {
|
|
98
|
+
const items = Array.from(entries)
|
|
99
|
+
const walk = (idx: number): void | globalThis.Promise<void> => {
|
|
100
|
+
for (; idx < items.length; idx++) {
|
|
101
|
+
const [key, value] = items[idx]
|
|
102
|
+
const keepGoing = yieldValue!(key, value)
|
|
103
|
+
if (keepGoing instanceof Promise) {
|
|
104
|
+
return keepGoing.then((next) => {
|
|
105
|
+
if (next) {
|
|
106
|
+
return walk(idx + 1)
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
if (!keepGoing) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return walk(0)
|
|
116
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { Clone, Values } from './index.js'
|
|
4
|
+
|
|
5
|
+
describe('maps overrides', () => {
|
|
6
|
+
it('accept nilable comparable keys', () => {
|
|
7
|
+
const source = new Map<object | null, number>()
|
|
8
|
+
const key = { name: 'type' }
|
|
9
|
+
source.set(null, 1)
|
|
10
|
+
source.set(key, 2)
|
|
11
|
+
|
|
12
|
+
const cloned = Clone(source)
|
|
13
|
+
expect(cloned?.get(null)).toBe(1)
|
|
14
|
+
expect(cloned?.get(key)).toBe(2)
|
|
15
|
+
|
|
16
|
+
const values: number[] = []
|
|
17
|
+
Values(cloned)((value) => {
|
|
18
|
+
values.push(value)
|
|
19
|
+
return true
|
|
20
|
+
})
|
|
21
|
+
expect(values.sort()).toEqual([1, 2])
|
|
22
|
+
})
|
|
23
|
+
})
|
package/gs/maps/maps.ts
CHANGED
|
@@ -4,7 +4,7 @@ import * as _ from '@goscript/unsafe/index.js'
|
|
|
4
4
|
|
|
5
5
|
// Equal reports whether two maps contain the same key/value pairs.
|
|
6
6
|
// Values are compared using ==.
|
|
7
|
-
export function Equal<K extends $.Comparable, V extends $.Comparable>(
|
|
7
|
+
export function Equal<K extends $.Comparable | null, V extends $.Comparable | null>(
|
|
8
8
|
m1: Map<K, V>,
|
|
9
9
|
m2: Map<K, V>,
|
|
10
10
|
): boolean {
|
|
@@ -22,7 +22,7 @@ export function Equal<K extends $.Comparable, V extends $.Comparable>(
|
|
|
22
22
|
|
|
23
23
|
// EqualFunc is like Equal, but compares values using eq.
|
|
24
24
|
// Keys are still compared with ==.
|
|
25
|
-
export function EqualFunc<K extends $.Comparable, V1, V2>(
|
|
25
|
+
export function EqualFunc<K extends $.Comparable | null, V1, V2>(
|
|
26
26
|
m1: Map<K, V1>,
|
|
27
27
|
m2: Map<K, V2>,
|
|
28
28
|
eq: ((p0: V1, p1: V2) => boolean) | null,
|
|
@@ -40,7 +40,7 @@ export function EqualFunc<K extends $.Comparable, V1, V2>(
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
// clone returns a shallow copy of the map.
|
|
43
|
-
export function clone<K extends $.Comparable, V>(
|
|
43
|
+
export function clone<K extends $.Comparable | null, V>(
|
|
44
44
|
m: Map<K, V> | null,
|
|
45
45
|
): Map<K, V> | null {
|
|
46
46
|
if (m == null) {
|
|
@@ -55,7 +55,7 @@ export function clone<K extends $.Comparable, V>(
|
|
|
55
55
|
|
|
56
56
|
// Clone returns a copy of m. This is a shallow clone:
|
|
57
57
|
// the new keys and values are set using ordinary assignment.
|
|
58
|
-
export function Clone<K extends $.Comparable, V>(
|
|
58
|
+
export function Clone<K extends $.Comparable | null, V>(
|
|
59
59
|
m: Map<K, V> | null,
|
|
60
60
|
): Map<K, V> | null {
|
|
61
61
|
// Preserve nil in case it matters.
|
|
@@ -69,7 +69,7 @@ export function Clone<K extends $.Comparable, V>(
|
|
|
69
69
|
// When a key in src is already present in dst,
|
|
70
70
|
// the value in dst will be overwritten by the value associated
|
|
71
71
|
// with the key in src.
|
|
72
|
-
export function Copy<K extends $.Comparable, V>(
|
|
72
|
+
export function Copy<K extends $.Comparable | null, V>(
|
|
73
73
|
dst: Map<K, V> | null,
|
|
74
74
|
src: Map<K, V> | null,
|
|
75
75
|
): void {
|
|
@@ -79,7 +79,7 @@ export function Copy<K extends $.Comparable, V>(
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
// DeleteFunc deletes any key/value pairs from m for which del returns true.
|
|
82
|
-
export function DeleteFunc<K extends $.Comparable, V>(
|
|
82
|
+
export function DeleteFunc<K extends $.Comparable | null, V>(
|
|
83
83
|
m: Map<K, V>,
|
|
84
84
|
del: ((p0: K, p1: V) => boolean) | null,
|
|
85
85
|
): void {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { Mul64 } from './index.js'
|
|
4
|
+
|
|
5
|
+
describe('math/bits override', () => {
|
|
6
|
+
it('returns the full 128-bit product from Mul64', () => {
|
|
7
|
+
expect(Mul64(0xffffffffffffffffn, 0xffffffffffffffffn)).toEqual([
|
|
8
|
+
0xfffffffffffffffen,
|
|
9
|
+
1n,
|
|
10
|
+
])
|
|
11
|
+
expect(Mul64(0x1fffffffffffffn, 0x1fffffffffffffn)).toEqual([
|
|
12
|
+
0x3ffffffffffn,
|
|
13
|
+
0xffc0000000000001n,
|
|
14
|
+
])
|
|
15
|
+
expect(Mul64(0x1fffffffffffff, 0x1fffffffffffff)).toEqual([
|
|
16
|
+
0x3ffffffffff,
|
|
17
|
+
0xffc0000000000001n,
|
|
18
|
+
])
|
|
19
|
+
})
|
|
20
|
+
})
|
package/gs/math/bits/index.ts
CHANGED
|
@@ -17,7 +17,9 @@ function useBigIntResult(...values: Word64[]): boolean {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
function word64Result(value: bigint, useBigInt: boolean): Word64 {
|
|
20
|
-
return useBigInt
|
|
20
|
+
return useBigInt || value > BigInt(Number.MAX_SAFE_INTEGER) ?
|
|
21
|
+
value
|
|
22
|
+
: Number(value)
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
// --- Leading zeros ---
|
|
@@ -209,20 +211,16 @@ export function Reverse64(x: Word64): Word64 {
|
|
|
209
211
|
((word & 0x0000ffff0000ffffn) << 16n)
|
|
210
212
|
// Swap 8-bit chunks
|
|
211
213
|
word =
|
|
212
|
-
((word & 0xff00ff00ff00ff00n) >> 8n) |
|
|
213
|
-
((word & 0x00ff00ff00ff00ffn) << 8n)
|
|
214
|
+
((word & 0xff00ff00ff00ff00n) >> 8n) | ((word & 0x00ff00ff00ff00ffn) << 8n)
|
|
214
215
|
// Swap 4-bit chunks
|
|
215
216
|
word =
|
|
216
|
-
((word & 0xf0f0f0f0f0f0f0f0n) >> 4n) |
|
|
217
|
-
((word & 0x0f0f0f0f0f0f0f0fn) << 4n)
|
|
217
|
+
((word & 0xf0f0f0f0f0f0f0f0n) >> 4n) | ((word & 0x0f0f0f0f0f0f0f0fn) << 4n)
|
|
218
218
|
// Swap 2-bit chunks
|
|
219
219
|
word =
|
|
220
|
-
((word & 0xccccccccccccccccn) >> 2n) |
|
|
221
|
-
((word & 0x3333333333333333n) << 2n)
|
|
220
|
+
((word & 0xccccccccccccccccn) >> 2n) | ((word & 0x3333333333333333n) << 2n)
|
|
222
221
|
// Swap 1-bit chunks
|
|
223
222
|
word =
|
|
224
|
-
((word & 0xaaaaaaaaaaaaaaaan) >> 1n) |
|
|
225
|
-
((word & 0x5555555555555555n) << 1n)
|
|
223
|
+
((word & 0xaaaaaaaaaaaaaaaan) >> 1n) | ((word & 0x5555555555555555n) << 1n)
|
|
226
224
|
|
|
227
225
|
return word64Result(word & uint64Mask, useBigInt)
|
|
228
226
|
}
|
|
@@ -307,25 +305,14 @@ export function Mul64(x: Word64, y: Word64): [Word64, Word64] {
|
|
|
307
305
|
const useBigInt = useBigIntResult(x, y)
|
|
308
306
|
x = toUint64(x)
|
|
309
307
|
y = toUint64(y)
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
// Multiply parts
|
|
319
|
-
const p00 = x0 * y0
|
|
320
|
-
const p01 = x0 * y1
|
|
321
|
-
const p10 = x1 * y0
|
|
322
|
-
const p11 = x1 * y1
|
|
323
|
-
|
|
324
|
-
// Combine results
|
|
325
|
-
const lo = p00 + ((p01 + p10) << 32n)
|
|
326
|
-
const hi = p11 + ((p01 + p10) >> 32n) + (lo < p00 ? 1n : 0n)
|
|
327
|
-
|
|
328
|
-
return [word64Result(hi & uint64Mask, useBigInt), word64Result(lo & uint64Mask, useBigInt)]
|
|
308
|
+
const product = x * y
|
|
309
|
+
const lo = product & uint64Mask
|
|
310
|
+
const hi = product >> 64n
|
|
311
|
+
|
|
312
|
+
return [
|
|
313
|
+
word64Result(hi & uint64Mask, useBigInt),
|
|
314
|
+
word64Result(lo, useBigInt),
|
|
315
|
+
]
|
|
329
316
|
}
|
|
330
317
|
|
|
331
318
|
// --- Division functions ---
|
package/gs/mime/index.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
export function FormatMediaType(
|
|
1
|
+
export function FormatMediaType(
|
|
2
|
+
t: string,
|
|
3
|
+
param: Map<string, string> | Record<string, string> | null,
|
|
4
|
+
): string {
|
|
2
5
|
if (!isToken(t)) {
|
|
3
6
|
return ''
|
|
4
7
|
}
|
|
5
8
|
let out = t
|
|
6
|
-
const entries =
|
|
9
|
+
const entries =
|
|
10
|
+
param instanceof Map ?
|
|
11
|
+
Array.from(param.entries())
|
|
12
|
+
: Object.entries(param ?? {})
|
|
7
13
|
entries.sort(([a], [b]) => a.localeCompare(b))
|
|
8
14
|
for (const [key, value] of entries) {
|
|
9
15
|
if (!isToken(key)) {
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import * as $ from '@goscript/builtin/index.js'
|
|
4
|
+
|
|
5
|
+
import { Handler, Header_Get, Header_Set, MethodGet, NewRequest, StatusPartialContent } from '../index.js'
|
|
6
|
+
import { NewServer, NewUnstartedServer, Server_Start } from './index.js'
|
|
7
|
+
|
|
8
|
+
describe('net/http/httptest override', () => {
|
|
9
|
+
it('exports server helpers for typechecked server tests', () => {
|
|
10
|
+
const handler: Handler = {
|
|
11
|
+
ServeHTTP: () => undefined,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const srv = NewServer(handler)
|
|
15
|
+
expect(srv.URL).toBe('http://127.0.0.1')
|
|
16
|
+
expect(srv.Client()).toBeTruthy()
|
|
17
|
+
expect(srv.Config().Handler).toBe(handler)
|
|
18
|
+
expect(Server_Start(NewUnstartedServer(handler))?.Error()).toBe(
|
|
19
|
+
'net/http/httptest: Server.Start is not implemented in GoScript',
|
|
20
|
+
)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('routes Client.Do through the in-memory server handler', async () => {
|
|
24
|
+
const srv = NewServer({
|
|
25
|
+
ServeHTTP(w, r) {
|
|
26
|
+
Header_Set(w!.Header(), 'Content-Range', 'bytes 0-3/4')
|
|
27
|
+
const range = Header_Get($.pointerValue(r)!.Header, 'Range')
|
|
28
|
+
if (range !== 'bytes=0-3') {
|
|
29
|
+
w!.WriteHeader(400)
|
|
30
|
+
w!.Write($.stringToBytes(range))
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
w!.WriteHeader(StatusPartialContent)
|
|
34
|
+
w!.Write($.stringToBytes('data'))
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
const [req, reqErr] = NewRequest(MethodGet, srv.URL + '/pack.kvf', null)
|
|
38
|
+
expect(reqErr).toBeNull()
|
|
39
|
+
Header_Set(req!.Header, 'Range', 'bytes=0-3')
|
|
40
|
+
|
|
41
|
+
const [resp, err] = await srv.Client().Do(req)
|
|
42
|
+
|
|
43
|
+
expect(err).toBeNull()
|
|
44
|
+
expect(resp?.StatusCode).toBe(StatusPartialContent)
|
|
45
|
+
expect(Header_Get(resp!.Header, 'Content-Range')).toBe('bytes 0-3/4')
|
|
46
|
+
const buf = new Uint8Array(4)
|
|
47
|
+
const [n, readErr] = resp!.Body!.Read(buf)
|
|
48
|
+
expect(readErr).toBeNull()
|
|
49
|
+
expect(n).toBe(4)
|
|
50
|
+
expect(Buffer.from(buf).toString('utf8')).toBe('data')
|
|
51
|
+
expect(resp!.Body!.Close()).toBeNull()
|
|
52
|
+
})
|
|
53
|
+
})
|
|
@@ -1,25 +1,120 @@
|
|
|
1
1
|
import * as $ from '@goscript/builtin/index.js'
|
|
2
|
+
import * as bytes from '@goscript/bytes/index.js'
|
|
3
|
+
import * as errors from '@goscript/errors/index.js'
|
|
2
4
|
import * as http from '@goscript/net/http/index.js'
|
|
5
|
+
import * as io from '@goscript/io/index.js'
|
|
6
|
+
|
|
7
|
+
class responseBody implements io.ReadCloser {
|
|
8
|
+
private reader: bytes.Reader
|
|
9
|
+
|
|
10
|
+
constructor(data: $.Bytes) {
|
|
11
|
+
this.reader = bytes.NewReader(Uint8Array.from(data ?? []))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public Read(p: $.Bytes): [number, $.GoError] {
|
|
15
|
+
return this.reader.Read(p)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public Close(): $.GoError {
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
}
|
|
3
22
|
|
|
4
23
|
export class ResponseRecorder implements http.ResponseWriter {
|
|
5
24
|
public Code = 200
|
|
6
|
-
public Body
|
|
25
|
+
public Body = new bytes.Buffer()
|
|
7
26
|
private headerMap = new http.Header()
|
|
27
|
+
private wroteHeader = false
|
|
8
28
|
|
|
9
29
|
public Header(): http.Header {
|
|
10
30
|
return this.headerMap
|
|
11
31
|
}
|
|
12
32
|
|
|
13
33
|
public Write(p: $.Slice<number>): [number, $.GoError] {
|
|
14
|
-
|
|
15
|
-
|
|
34
|
+
if (!this.wroteHeader) {
|
|
35
|
+
this.WriteHeader(http.StatusOK)
|
|
36
|
+
}
|
|
37
|
+
return this.Body.Write(p)
|
|
16
38
|
}
|
|
17
39
|
|
|
18
40
|
public WriteHeader(statusCode: number): void {
|
|
41
|
+
if (this.wroteHeader) {
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
this.wroteHeader = true
|
|
19
45
|
this.Code = statusCode
|
|
20
46
|
}
|
|
47
|
+
|
|
48
|
+
public Result(): http.Response {
|
|
49
|
+
return new http.Response({
|
|
50
|
+
Body: new responseBody(this.Body.Bytes()),
|
|
51
|
+
Header: this.headerMap,
|
|
52
|
+
StatusCode: this.Code,
|
|
53
|
+
})
|
|
54
|
+
}
|
|
21
55
|
}
|
|
22
56
|
|
|
23
57
|
export function NewRecorder(): ResponseRecorder {
|
|
24
58
|
return new ResponseRecorder()
|
|
25
59
|
}
|
|
60
|
+
|
|
61
|
+
export function NewRequest(method: string, target: string, body: io.Reader | null): http.Request {
|
|
62
|
+
const [req, err] = http.NewRequest(method, target, body)
|
|
63
|
+
if (err != null || req == null) {
|
|
64
|
+
throw err ?? errors.New('net/http/httptest: NewRequest returned nil request')
|
|
65
|
+
}
|
|
66
|
+
return req
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class Server {
|
|
70
|
+
public URL: string
|
|
71
|
+
private handler: http.Handler | null
|
|
72
|
+
|
|
73
|
+
constructor(init?: Partial<Server> & { Handler?: http.Handler | null }) {
|
|
74
|
+
this.URL = init?.URL ?? 'http://127.0.0.1'
|
|
75
|
+
this.handler = init?.Handler ?? null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public Client(): http.Client {
|
|
79
|
+
return new http.Client({ Transport: new serverTransport(this) })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public Close(): void {}
|
|
83
|
+
|
|
84
|
+
public Config(): http.Server {
|
|
85
|
+
return new http.Server({ Handler: this.handler })
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public ServeHTTP(w: http.ResponseWriter | null, r: http.Request | $.VarRef<http.Request> | null): void | Promise<void> {
|
|
89
|
+
return this.handler?.ServeHTTP(w, r)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
class serverTransport implements http.RoundTripper {
|
|
94
|
+
constructor(private server: Server) {}
|
|
95
|
+
|
|
96
|
+
public RoundTrip(req: http.Request | $.VarRef<http.Request> | null): [http.Response | null, $.GoError] {
|
|
97
|
+
const request = $.pointerValue<http.Request | null>(req)
|
|
98
|
+
if (request == null) {
|
|
99
|
+
return [null, errors.New('net/http: nil Request')]
|
|
100
|
+
}
|
|
101
|
+
const recorder = NewRecorder()
|
|
102
|
+
const served = this.server.ServeHTTP(recorder, request)
|
|
103
|
+
if (served instanceof Promise) {
|
|
104
|
+
return [null, errors.New('net/http/httptest: async handlers are not supported by Client.Do')]
|
|
105
|
+
}
|
|
106
|
+
return [recorder.Result(), null]
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function NewServer(handler: http.Handler | null): Server {
|
|
111
|
+
return new Server({ Handler: handler })
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function NewUnstartedServer(handler: http.Handler | null): Server {
|
|
115
|
+
return new Server({ Handler: handler })
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function Server_Start(_s: Server | $.VarRef<Server> | null): $.GoError {
|
|
119
|
+
return errors.New('net/http/httptest: Server.Start is not implemented in GoScript')
|
|
120
|
+
}
|