goscript 0.1.4 → 0.2.1
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 +5 -2
- package/cmd/go_js_wasm_exec/main.go +201 -0
- package/cmd/go_js_wasm_exec/main_test.go +83 -0
- package/cmd/goscript/{cmd_compile.go → cmd-compile.go} +7 -0
- package/cmd/goscript/cmd-test.go +14 -0
- package/cmd/goscript/cmd-test_test.go +1 -1
- package/cmd/goscript-wasm/main.go +38 -6
- package/compiler/compile-request.go +12 -9
- package/compiler/compliance_test.go +0 -1
- package/compiler/config.go +2 -0
- package/compiler/diagnostic.go +104 -12
- package/compiler/diagnostic_test.go +106 -0
- package/compiler/gotest/request.go +28 -0
- package/compiler/gotest/runner.go +354 -44
- package/compiler/gotest/runner_test.go +293 -1
- package/compiler/gotest/testdata/browserapi/browserapi_test.go +20 -0
- package/compiler/gotest/testdata/browserapi/go.mod +3 -0
- package/compiler/index.test.ts +23 -0
- package/compiler/lowered-program.go +33 -24
- package/compiler/lowering.go +746 -194
- package/compiler/lowering_bench_test.go +42 -27
- package/compiler/lowering_internal_test.go +18 -0
- package/compiler/override-facts.go +15 -0
- package/compiler/override-parity-verifier.go +450 -0
- package/compiler/override-parity.go +122 -0
- package/compiler/override-registry_test.go +559 -0
- package/compiler/protobuf-ts-binding.go +567 -0
- package/compiler/protobuf-ts-binding_test.go +402 -0
- package/compiler/runtime-contract.go +4 -0
- package/compiler/runtime-contract_test.go +2 -0
- package/compiler/semantic-model-types.go +9 -4
- package/compiler/semantic-model.go +282 -70
- package/compiler/semantic-model_test.go +82 -1
- package/compiler/service.go +21 -1
- package/compiler/skeleton_test.go +118 -10
- package/compiler/typescript-emitter.go +128 -13
- package/compiler/wasm/compile_test.go +37 -4
- package/compiler/{wasm_api.go → wasm-api.go} +57 -7
- package/dist/gs/builtin/hostio.js +5 -0
- package/dist/gs/builtin/hostio.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +13 -2
- package/dist/gs/builtin/slice.js +187 -6
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/builtin/type.d.ts +13 -5
- package/dist/gs/builtin/type.js +153 -60
- package/dist/gs/builtin/type.js.map +1 -1
- package/dist/gs/builtin/varRef.d.ts +11 -0
- package/dist/gs/builtin/varRef.js +57 -2
- package/dist/gs/builtin/varRef.js.map +1 -1
- package/dist/gs/bytes/buffer.gs.js +1 -1
- package/dist/gs/bytes/buffer.gs.js.map +1 -1
- package/dist/gs/bytes/reader.gs.js +1 -1
- package/dist/gs/bytes/reader.gs.js.map +1 -1
- package/dist/gs/compress/zlib/index.d.ts +10 -3
- package/dist/gs/compress/zlib/index.js +50 -16
- package/dist/gs/compress/zlib/index.js.map +1 -1
- package/dist/gs/encoding/json/index.d.ts +114 -0
- package/dist/gs/encoding/json/index.js +544 -36
- package/dist/gs/encoding/json/index.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +101 -0
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +589 -0
- 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 +1 -0
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js +17 -11
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/errors.js +54 -30
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -1
- package/dist/gs/go/scanner/index.d.ts +2 -0
- package/dist/gs/go/scanner/index.js +29 -5
- package/dist/gs/go/scanner/index.js.map +1 -1
- package/dist/gs/go/token/index.js +22 -6
- package/dist/gs/go/token/index.js.map +1 -1
- package/dist/gs/hash/index.d.ts +6 -0
- package/dist/gs/hash/index.js +20 -0
- package/dist/gs/hash/index.js.map +1 -1
- package/dist/gs/internal/byteorder/index.js +2 -2
- package/dist/gs/internal/byteorder/index.js.map +1 -1
- package/dist/gs/internal/goarch/index.d.ts +43 -3
- package/dist/gs/internal/goarch/index.js +42 -10
- package/dist/gs/internal/goarch/index.js.map +1 -1
- package/dist/gs/io/fs/fs.js +26 -14
- package/dist/gs/io/fs/fs.js.map +1 -1
- package/dist/gs/io/fs/readdir.js +4 -2
- package/dist/gs/io/fs/readdir.js.map +1 -1
- package/dist/gs/io/fs/sub.js +8 -1
- package/dist/gs/io/fs/sub.js.map +1 -1
- package/dist/gs/io/io.d.ts +2 -0
- package/dist/gs/io/io.js.map +1 -1
- package/dist/gs/math/bits/index.d.ts +5 -0
- package/dist/gs/math/bits/index.js +16 -4
- package/dist/gs/math/bits/index.js.map +1 -1
- package/dist/gs/mime/index.d.ts +16 -0
- package/dist/gs/mime/index.js +315 -6
- package/dist/gs/mime/index.js.map +1 -1
- package/dist/gs/net/http/httptest/index.d.ts +12 -0
- package/dist/gs/net/http/httptest/index.js +85 -6
- package/dist/gs/net/http/httptest/index.js.map +1 -1
- package/dist/gs/net/http/index.d.ts +300 -5
- package/dist/gs/net/http/index.js +1598 -58
- package/dist/gs/net/http/index.js.map +1 -1
- package/dist/gs/os/dir_unix.gs.js +1 -1
- package/dist/gs/os/dir_unix.gs.js.map +1 -1
- package/dist/gs/os/error.gs.js +1 -1
- package/dist/gs/os/error.gs.js.map +1 -1
- package/dist/gs/os/exec.gs.d.ts +1 -0
- package/dist/gs/os/exec.gs.js +4 -8
- package/dist/gs/os/exec.gs.js.map +1 -1
- package/dist/gs/os/exec_posix.gs.js +1 -1
- package/dist/gs/os/exec_posix.gs.js.map +1 -1
- package/dist/gs/os/index.d.ts +1 -1
- package/dist/gs/os/index.js +1 -1
- package/dist/gs/os/index.js.map +1 -1
- package/dist/gs/os/proc.gs.d.ts +4 -0
- package/dist/gs/os/proc.gs.js +12 -6
- package/dist/gs/os/proc.gs.js.map +1 -1
- package/dist/gs/os/root_js.gs.js +1 -1
- package/dist/gs/os/root_js.gs.js.map +1 -1
- package/dist/gs/os/types.gs.js +1 -1
- package/dist/gs/os/types.gs.js.map +1 -1
- package/dist/gs/os/types_js.gs.js +1 -1
- package/dist/gs/os/types_js.gs.js.map +1 -1
- package/dist/gs/os/types_unix.gs.js +1 -1
- package/dist/gs/os/types_unix.gs.js.map +1 -1
- package/dist/gs/path/path.js +11 -7
- package/dist/gs/path/path.js.map +1 -1
- package/dist/gs/reflect/index.d.ts +5 -4
- package/dist/gs/reflect/index.js +4 -3
- package/dist/gs/reflect/index.js.map +1 -1
- package/dist/gs/reflect/map.js +15 -0
- package/dist/gs/reflect/map.js.map +1 -1
- package/dist/gs/reflect/type.d.ts +25 -6
- package/dist/gs/reflect/type.js +1475 -228
- package/dist/gs/reflect/type.js.map +1 -1
- package/dist/gs/reflect/types.d.ts +14 -6
- package/dist/gs/reflect/types.js +35 -1
- package/dist/gs/reflect/types.js.map +1 -1
- package/dist/gs/reflect/value.d.ts +1 -0
- package/dist/gs/reflect/value.js +83 -41
- package/dist/gs/reflect/value.js.map +1 -1
- package/dist/gs/reflect/visiblefields.js +4 -140
- package/dist/gs/reflect/visiblefields.js.map +1 -1
- package/dist/gs/runtime/pprof/index.d.ts +8 -2
- package/dist/gs/runtime/pprof/index.js +50 -30
- package/dist/gs/runtime/pprof/index.js.map +1 -1
- package/dist/gs/runtime/runtime.js +5 -4
- package/dist/gs/runtime/runtime.js.map +1 -1
- package/dist/gs/runtime/trace/index.js +5 -19
- package/dist/gs/runtime/trace/index.js.map +1 -1
- package/dist/gs/strconv/atoi.gs.js +1 -1
- package/dist/gs/strconv/atoi.gs.js.map +1 -1
- package/dist/gs/strconv/complex.gs.d.ts +3 -0
- package/dist/gs/strconv/complex.gs.js +148 -0
- package/dist/gs/strconv/complex.gs.js.map +1 -0
- package/dist/gs/strconv/index.d.ts +1 -0
- package/dist/gs/strconv/index.js +1 -0
- package/dist/gs/strconv/index.js.map +1 -1
- package/dist/gs/strings/builder.js +1 -1
- package/dist/gs/strings/reader.js +9 -5
- package/dist/gs/strings/reader.js.map +1 -1
- package/dist/gs/strings/replace.js +15 -7
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/strings/strings.d.ts +5 -0
- package/dist/gs/strings/strings.js +57 -5
- package/dist/gs/strings/strings.js.map +1 -1
- package/dist/gs/sync/atomic/doc_64.gs.js +7 -6
- package/dist/gs/sync/atomic/doc_64.gs.js.map +1 -1
- package/dist/gs/sync/atomic/type.gs.js +9 -9
- package/dist/gs/sync/atomic/type.gs.js.map +1 -1
- package/dist/gs/sync/atomic/value.gs.js +2 -2
- package/dist/gs/sync/atomic/value.gs.js.map +1 -1
- package/dist/gs/syscall/env.js +22 -14
- package/dist/gs/syscall/env.js.map +1 -1
- package/dist/gs/testing/testing.js +55 -13
- package/dist/gs/testing/testing.js.map +1 -1
- package/dist/gs/time/time.d.ts +24 -1
- package/dist/gs/time/time.js +43 -3
- package/dist/gs/time/time.js.map +1 -1
- package/dist/gs/unique/index.js +7 -1
- package/dist/gs/unique/index.js.map +1 -1
- package/go.mod +3 -3
- package/go.sum +16 -0
- package/gs/builtin/hostio.test.ts +16 -0
- package/gs/builtin/hostio.ts +7 -0
- package/gs/builtin/runtime-contract.test.ts +246 -21
- package/gs/builtin/slice.ts +269 -24
- package/gs/builtin/type.ts +226 -59
- package/gs/builtin/varRef.ts +85 -2
- package/gs/bytes/buffer.gs.ts +1 -1
- package/gs/bytes/reader.gs.ts +1 -1
- package/gs/compress/zlib/index.test.ts +62 -1
- package/gs/compress/zlib/index.ts +53 -16
- package/gs/compress/zlib/parity.json +51 -0
- package/gs/encoding/json/index.test.ts +360 -6
- package/gs/encoding/json/index.ts +679 -38
- package/gs/encoding/json/parity.json +81 -0
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +373 -3
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +893 -1
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +18 -0
- package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +17 -11
- package/gs/github.com/pkg/errors/errors.ts +54 -30
- package/gs/go/scanner/index.test.ts +39 -56
- package/gs/go/scanner/index.ts +33 -5
- package/gs/go/scanner/parity.json +27 -0
- package/gs/go/token/index.ts +22 -6
- package/gs/hash/index.test.ts +20 -33
- package/gs/hash/index.ts +28 -0
- package/gs/hash/parity.json +21 -0
- package/gs/internal/byteorder/index.test.ts +2 -2
- package/gs/internal/byteorder/index.ts +2 -2
- package/gs/internal/goarch/index.test.ts +32 -0
- package/gs/internal/goarch/index.ts +45 -13
- package/gs/internal/goarch/parity.json +144 -0
- package/gs/io/fs/fs.ts +26 -14
- package/gs/io/fs/readdir.ts +4 -4
- package/gs/io/fs/sub.ts +8 -1
- package/gs/io/io.ts +1 -0
- package/gs/io/parity.json +162 -0
- package/gs/math/bits/index.test.ts +14 -1
- package/gs/math/bits/index.ts +23 -4
- package/gs/math/bits/parity.json +156 -0
- package/gs/mime/index.test.ts +90 -0
- package/gs/mime/index.ts +369 -6
- package/gs/mime/parity.json +36 -0
- package/gs/net/http/httptest/index.test.ts +98 -2
- package/gs/net/http/httptest/index.ts +101 -6
- package/gs/net/http/httptest/parity.json +15 -0
- package/gs/net/http/index.test.ts +781 -12
- package/gs/net/http/index.ts +1860 -139
- package/gs/net/http/meta.json +16 -1
- package/gs/net/http/parity.json +193 -0
- package/gs/os/dir_unix.gs.ts +1 -1
- package/gs/os/error.gs.ts +1 -1
- package/gs/os/exec.gs.ts +4 -8
- package/gs/os/exec_posix.gs.ts +1 -1
- package/gs/os/index.test.ts +9 -0
- package/gs/os/index.ts +1 -0
- package/gs/os/parity.json +9 -0
- package/gs/os/proc.gs.ts +18 -5
- package/gs/os/proc.test.ts +26 -0
- package/gs/os/root_js.gs.ts +1 -1
- package/gs/os/types.gs.ts +1 -1
- package/gs/os/types_js.gs.ts +1 -1
- package/gs/os/types_unix.gs.ts +1 -1
- package/gs/path/path.ts +11 -7
- package/gs/reflect/field.test.ts +37 -15
- package/gs/reflect/function-types.test.ts +518 -22
- package/gs/reflect/index.ts +8 -6
- package/gs/reflect/map.ts +20 -0
- package/gs/reflect/meta.json +6 -4
- package/gs/reflect/parity.json +234 -0
- package/gs/reflect/sliceat.test.ts +156 -0
- package/gs/reflect/structof.test.ts +401 -0
- package/gs/reflect/type.ts +1961 -317
- package/gs/reflect/typefor.test.ts +530 -10
- package/gs/reflect/types.ts +43 -18
- package/gs/reflect/value.ts +105 -45
- package/gs/reflect/visiblefields.ts +5 -168
- package/gs/runtime/parity.json +24 -0
- package/gs/runtime/pprof/index.test.ts +29 -7
- package/gs/runtime/pprof/index.ts +56 -30
- package/gs/runtime/pprof/parity.json +27 -0
- package/gs/runtime/runtime.test.ts +3 -1
- package/gs/runtime/runtime.ts +4 -3
- package/gs/runtime/trace/index.test.ts +5 -3
- package/gs/runtime/trace/index.ts +8 -20
- package/gs/runtime/trace/parity.json +36 -0
- package/gs/strconv/atoi.gs.ts +1 -1
- package/gs/strconv/complex.gs.ts +174 -0
- package/gs/strconv/complex.test.ts +65 -0
- package/gs/strconv/index.ts +1 -0
- package/gs/strconv/parity.json +120 -0
- package/gs/strings/builder.ts +1 -1
- package/gs/strings/parity.json +186 -0
- package/gs/strings/reader.ts +9 -5
- package/gs/strings/replace.ts +15 -7
- package/gs/strings/strings.test.ts +22 -2
- package/gs/strings/strings.ts +64 -6
- package/gs/sync/atomic/doc_64.gs.ts +6 -7
- package/gs/sync/atomic/doc_64.test.ts +43 -0
- package/gs/sync/atomic/type.gs.ts +9 -9
- package/gs/sync/atomic/value.gs.ts +2 -2
- package/gs/syscall/env.ts +29 -14
- package/gs/testing/testing.test.ts +67 -0
- package/gs/testing/testing.ts +87 -19
- package/gs/time/parity.json +225 -0
- package/gs/time/time.test.ts +20 -2
- package/gs/time/time.ts +49 -7
- package/gs/unique/index.ts +7 -1
- package/package.json +4 -2
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.d.ts +0 -217
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js +0 -926
- package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js.map +0 -1
- package/gs/github.com/aperturerobotics/starpc/srpc/index.test.ts +0 -38
- package/gs/github.com/aperturerobotics/starpc/srpc/index.ts +0 -1361
- package/gs/github.com/aperturerobotics/starpc/srpc/meta.json +0 -46
|
@@ -1,1361 +0,0 @@
|
|
|
1
|
-
import * as $ from '@goscript/builtin/index.js'
|
|
2
|
-
import * as context from '@goscript/context/index.js'
|
|
3
|
-
import * as io from '@goscript/io/index.js'
|
|
4
|
-
import * as protobuf_go_lite from '@goscript/github.com/aperturerobotics/protobuf-go-lite/index.js'
|
|
5
|
-
import type {
|
|
6
|
-
Client as StarpcClient,
|
|
7
|
-
Mux as StarpcMux,
|
|
8
|
-
Server as StarpcServer,
|
|
9
|
-
} from 'starpc'
|
|
10
|
-
|
|
11
|
-
export type Message = protobuf_go_lite.Message
|
|
12
|
-
export type TypeScriptClient = StarpcClient
|
|
13
|
-
export type TypeScriptMux = StarpcMux
|
|
14
|
-
export type TypeScriptServer = StarpcServer
|
|
15
|
-
|
|
16
|
-
type MaybePromise<T> = T | Promise<T>
|
|
17
|
-
|
|
18
|
-
export const ErrReset = $.newError('stream reset')
|
|
19
|
-
export const ErrUnimplemented = $.newError('unimplemented')
|
|
20
|
-
export const ErrCompleted = $.newError(
|
|
21
|
-
'unexpected packet after rpc was completed',
|
|
22
|
-
)
|
|
23
|
-
export const ErrUnrecognizedPacket = $.newError('unrecognized packet type')
|
|
24
|
-
export const ErrEmptyPacket = $.newError('invalid empty packet')
|
|
25
|
-
export const ErrInvalidMessage = $.newError('invalid message')
|
|
26
|
-
export const ErrEmptyMethodID = $.newError('method id empty')
|
|
27
|
-
export const ErrEmptyServiceID = $.newError('service id empty')
|
|
28
|
-
export const ErrNoAvailableClients = $.newError('no available rpc clients')
|
|
29
|
-
export const ErrNilWriter = $.newError('writer cannot be nil')
|
|
30
|
-
|
|
31
|
-
export class RawMessage implements Message {
|
|
32
|
-
private data: $.Slice<number>
|
|
33
|
-
private copyData: boolean
|
|
34
|
-
|
|
35
|
-
constructor(init?: Partial<{ data?: $.Slice<number>; copy?: boolean }>) {
|
|
36
|
-
this.data = init?.data ?? null
|
|
37
|
-
this.copyData = init?.copy ?? false
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public GetData(): $.Slice<number> {
|
|
41
|
-
return this.data
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public SetData(data: $.Slice<number>): void {
|
|
45
|
-
if (!this.copyData) {
|
|
46
|
-
this.data = data
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
const next = new Uint8Array($.len(data))
|
|
50
|
-
$.copy(next, data)
|
|
51
|
-
this.data = next
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public Clear(): void {
|
|
55
|
-
this.data = $.goSlice(this.data, 0, 0)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public Reset(): void {
|
|
59
|
-
this.data = null
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public MarshalVT(): [$.Slice<number>, $.GoError] {
|
|
63
|
-
if (!this.copyData) {
|
|
64
|
-
return [this.data, null]
|
|
65
|
-
}
|
|
66
|
-
const next = new Uint8Array($.len(this.data))
|
|
67
|
-
$.copy(next, this.data)
|
|
68
|
-
return [next, null]
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public UnmarshalVT(data: $.Slice<number>): $.GoError {
|
|
72
|
-
this.SetData(data)
|
|
73
|
-
return null
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public SizeVT(): number {
|
|
77
|
-
return $.len(this.data)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public MarshalToSizedBufferVT(dAtA: $.Slice<number>): [number, $.GoError] {
|
|
81
|
-
if ($.len(dAtA) !== $.len(this.data)) {
|
|
82
|
-
return [0, $.newError('invalid buffer length')]
|
|
83
|
-
}
|
|
84
|
-
$.copy(dAtA, this.data)
|
|
85
|
-
return [$.len(dAtA), null]
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public clone(): RawMessage {
|
|
89
|
-
const cloned = new RawMessage({ copy: this.copyData })
|
|
90
|
-
cloned.SetData(this.data)
|
|
91
|
-
return $.markAsStructValue(cloned)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function NewRawMessage(
|
|
96
|
-
data: $.Slice<number>,
|
|
97
|
-
copy: boolean,
|
|
98
|
-
): RawMessage {
|
|
99
|
-
return new RawMessage({ data, copy })
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export interface Client {
|
|
103
|
-
ExecCall(
|
|
104
|
-
ctx: context.Context,
|
|
105
|
-
service: string,
|
|
106
|
-
method: string,
|
|
107
|
-
input: Message | null,
|
|
108
|
-
output: Message | null,
|
|
109
|
-
): Promise<$.GoError>
|
|
110
|
-
NewStream(
|
|
111
|
-
ctx: context.Context,
|
|
112
|
-
service: string,
|
|
113
|
-
method: string,
|
|
114
|
-
firstMsg: Message | null,
|
|
115
|
-
): Promise<[Stream | null, $.GoError]>
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
$.registerInterfaceType('srpc.Client', null, [
|
|
119
|
-
{
|
|
120
|
-
name: 'ExecCall',
|
|
121
|
-
args: [
|
|
122
|
-
{ name: 'ctx', type: 'context.Context' },
|
|
123
|
-
{ name: 'service', type: { kind: $.TypeKind.Basic, name: 'string' } },
|
|
124
|
-
{ name: 'method', type: { kind: $.TypeKind.Basic, name: 'string' } },
|
|
125
|
-
{ name: 'in', type: 'srpc.Message' },
|
|
126
|
-
{ name: 'out', type: 'srpc.Message' },
|
|
127
|
-
],
|
|
128
|
-
returns: [{ name: '_r0', type: 'error' }],
|
|
129
|
-
},
|
|
130
|
-
{
|
|
131
|
-
name: 'NewStream',
|
|
132
|
-
args: [
|
|
133
|
-
{ name: 'ctx', type: 'context.Context' },
|
|
134
|
-
{ name: 'service', type: { kind: $.TypeKind.Basic, name: 'string' } },
|
|
135
|
-
{ name: 'method', type: { kind: $.TypeKind.Basic, name: 'string' } },
|
|
136
|
-
{ name: 'firstMsg', type: 'srpc.Message' },
|
|
137
|
-
],
|
|
138
|
-
returns: [
|
|
139
|
-
{ name: '_r0', type: 'srpc.Stream' },
|
|
140
|
-
{ name: '_r1', type: 'error' },
|
|
141
|
-
],
|
|
142
|
-
},
|
|
143
|
-
])
|
|
144
|
-
|
|
145
|
-
export class ClientSet implements Client {
|
|
146
|
-
private clients: $.Slice<Client | null>
|
|
147
|
-
|
|
148
|
-
constructor(init?: Partial<{ clients?: $.Slice<Client | null> }>) {
|
|
149
|
-
this.clients = init?.clients ?? null
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
public async ExecCall(
|
|
153
|
-
ctx: context.Context,
|
|
154
|
-
service: string,
|
|
155
|
-
method: string,
|
|
156
|
-
input: Message | null,
|
|
157
|
-
output: Message | null,
|
|
158
|
-
): Promise<$.GoError> {
|
|
159
|
-
return this.execCall(ctx, (client) =>
|
|
160
|
-
client.ExecCall(ctx, service, method, input, output),
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
public async NewStream(
|
|
165
|
-
ctx: context.Context,
|
|
166
|
-
service: string,
|
|
167
|
-
method: string,
|
|
168
|
-
firstMsg: Message | null,
|
|
169
|
-
): Promise<[Stream | null, $.GoError]> {
|
|
170
|
-
let stream: Stream | null = null
|
|
171
|
-
const err = await this.execCall(ctx, async (client) => {
|
|
172
|
-
const [next, callErr] = await client.NewStream(
|
|
173
|
-
ctx,
|
|
174
|
-
service,
|
|
175
|
-
method,
|
|
176
|
-
firstMsg,
|
|
177
|
-
)
|
|
178
|
-
stream = next
|
|
179
|
-
return callErr
|
|
180
|
-
})
|
|
181
|
-
return [stream, err]
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
private async execCall(
|
|
185
|
-
ctx: context.Context,
|
|
186
|
-
call: (client: Client) => MaybePromise<$.GoError>,
|
|
187
|
-
): Promise<$.GoError> {
|
|
188
|
-
let anyClient = false
|
|
189
|
-
for (let i = 0; i < $.len(this.clients); i++) {
|
|
190
|
-
const client = this.clients![i]
|
|
191
|
-
if (client == null) {
|
|
192
|
-
continue
|
|
193
|
-
}
|
|
194
|
-
anyClient = true
|
|
195
|
-
const err = await call(client)
|
|
196
|
-
if (err == null) {
|
|
197
|
-
return null
|
|
198
|
-
}
|
|
199
|
-
if (err === context.Canceled && contextErr(ctx) === context.Canceled) {
|
|
200
|
-
return context.Canceled
|
|
201
|
-
}
|
|
202
|
-
if (err.Error() === ErrUnimplemented!.Error()) {
|
|
203
|
-
continue
|
|
204
|
-
}
|
|
205
|
-
return err
|
|
206
|
-
}
|
|
207
|
-
return anyClient ? ErrUnimplemented : ErrNoAvailableClients
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export function NewClientSet(clients: $.Slice<Client | null>): ClientSet {
|
|
212
|
-
return new ClientSet({ clients })
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export class PrefixClient implements Client {
|
|
216
|
-
private client: Client | null
|
|
217
|
-
private serviceIDPrefixes: $.Slice<string>
|
|
218
|
-
|
|
219
|
-
constructor(
|
|
220
|
-
clientOrInit?:
|
|
221
|
-
| Client
|
|
222
|
-
| Partial<{ client?: Client | null; serviceIDPrefixes?: $.Slice<string> }>
|
|
223
|
-
| null,
|
|
224
|
-
serviceIDPrefixes?: $.Slice<string>,
|
|
225
|
-
) {
|
|
226
|
-
if (
|
|
227
|
-
clientOrInit != null &&
|
|
228
|
-
typeof clientOrInit === 'object' &&
|
|
229
|
-
'client' in clientOrInit
|
|
230
|
-
) {
|
|
231
|
-
this.client = clientOrInit.client ?? null
|
|
232
|
-
this.serviceIDPrefixes = clientOrInit.serviceIDPrefixes ?? null
|
|
233
|
-
return
|
|
234
|
-
}
|
|
235
|
-
this.client = clientOrInit as Client | null
|
|
236
|
-
this.serviceIDPrefixes = serviceIDPrefixes ?? null
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
public async ExecCall(
|
|
240
|
-
ctx: context.Context,
|
|
241
|
-
service: string,
|
|
242
|
-
method: string,
|
|
243
|
-
input: Message | null,
|
|
244
|
-
output: Message | null,
|
|
245
|
-
): Promise<$.GoError> {
|
|
246
|
-
const [stripped, err] = this.stripCheckServiceIDPrefix(service)
|
|
247
|
-
if (err != null) {
|
|
248
|
-
return err
|
|
249
|
-
}
|
|
250
|
-
if (this.client == null) {
|
|
251
|
-
return ErrNoAvailableClients
|
|
252
|
-
}
|
|
253
|
-
return this.client.ExecCall(ctx, stripped, method, input, output)
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
public async NewStream(
|
|
257
|
-
ctx: context.Context,
|
|
258
|
-
service: string,
|
|
259
|
-
method: string,
|
|
260
|
-
firstMsg: Message | null,
|
|
261
|
-
): Promise<[Stream | null, $.GoError]> {
|
|
262
|
-
const [stripped, err] = this.stripCheckServiceIDPrefix(service)
|
|
263
|
-
if (err != null) {
|
|
264
|
-
return [null, err]
|
|
265
|
-
}
|
|
266
|
-
if (this.client == null) {
|
|
267
|
-
return [null, ErrNoAvailableClients]
|
|
268
|
-
}
|
|
269
|
-
return this.client.NewStream(ctx, stripped, method, firstMsg)
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
private stripCheckServiceIDPrefix(service: string): [string, $.GoError] {
|
|
273
|
-
if ($.len(this.serviceIDPrefixes) === 0) {
|
|
274
|
-
return [service, null]
|
|
275
|
-
}
|
|
276
|
-
const [stripped, matched] = CheckStripPrefix(
|
|
277
|
-
service,
|
|
278
|
-
this.serviceIDPrefixes,
|
|
279
|
-
)
|
|
280
|
-
if (matched === '') {
|
|
281
|
-
return [service, ErrUnimplemented]
|
|
282
|
-
}
|
|
283
|
-
return [stripped, null]
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export function NewPrefixClient(
|
|
288
|
-
client: Client | null,
|
|
289
|
-
serviceIDPrefixes: $.Slice<string>,
|
|
290
|
-
): PrefixClient {
|
|
291
|
-
return new PrefixClient(client, serviceIDPrefixes)
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export interface Stream {
|
|
295
|
-
Context(): context.Context
|
|
296
|
-
MsgSend(msg: Message | null): MaybePromise<$.GoError>
|
|
297
|
-
MsgRecv(msg: Message | null): MaybePromise<$.GoError>
|
|
298
|
-
CloseSend(): MaybePromise<$.GoError>
|
|
299
|
-
Close(): MaybePromise<$.GoError>
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
export interface StreamRecv<T> extends Stream {
|
|
303
|
-
Recv(): MaybePromise<[T, $.GoError]>
|
|
304
|
-
RecvTo(value: T): MaybePromise<$.GoError>
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
export interface StreamSend<T> extends Stream {
|
|
308
|
-
Send(value: T): MaybePromise<$.GoError>
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
export interface StreamSendAndClose<T> extends StreamSend<T> {
|
|
312
|
-
SendAndClose(value: T): MaybePromise<$.GoError>
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
class emptyStream implements Stream {
|
|
316
|
-
private ctx: context.Context
|
|
317
|
-
private closed = false
|
|
318
|
-
|
|
319
|
-
constructor(ctx: context.Context = context.Background()) {
|
|
320
|
-
this.ctx = ctx
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
public Context(): context.Context {
|
|
324
|
-
return this.ctx
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
public MsgSend(_msg: Message | null): $.GoError {
|
|
328
|
-
return this.closed ? ErrCompleted : null
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
public MsgRecv(_msg: Message | null): $.GoError {
|
|
332
|
-
return this.closed ? io.EOF : io.EOF
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
public CloseSend(): $.GoError {
|
|
336
|
-
this.closed = true
|
|
337
|
-
return null
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
public Close(): $.GoError {
|
|
341
|
-
this.closed = true
|
|
342
|
-
return null
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
class memoryStream implements Stream {
|
|
347
|
-
private sent: Message | null = null
|
|
348
|
-
private sentQueue: (Message | null)[] = []
|
|
349
|
-
private waiters: ((msg: Message | null) => void)[] = []
|
|
350
|
-
private closed = false
|
|
351
|
-
private recvConsumed = false
|
|
352
|
-
|
|
353
|
-
constructor(
|
|
354
|
-
private ctx: context.Context,
|
|
355
|
-
private recv: Message | null,
|
|
356
|
-
) {}
|
|
357
|
-
|
|
358
|
-
public Context(): context.Context {
|
|
359
|
-
return this.ctx
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
public MsgSend(msg: Message | null): $.GoError {
|
|
363
|
-
this.sent = msg
|
|
364
|
-
const waiter = this.waiters.shift()
|
|
365
|
-
if (waiter != null) {
|
|
366
|
-
waiter(msg)
|
|
367
|
-
return null
|
|
368
|
-
}
|
|
369
|
-
this.sentQueue.push(msg)
|
|
370
|
-
return null
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
public MsgRecv(msg: Message | null): MaybePromise<$.GoError> {
|
|
374
|
-
if (!this.recvConsumed) {
|
|
375
|
-
this.recvConsumed = true
|
|
376
|
-
if (msg != null && this.recv != null) {
|
|
377
|
-
Object.assign(msg, this.recv)
|
|
378
|
-
}
|
|
379
|
-
return null
|
|
380
|
-
}
|
|
381
|
-
const next = this.sentQueue.shift()
|
|
382
|
-
if (next !== undefined) {
|
|
383
|
-
if (msg != null && next != null) {
|
|
384
|
-
Object.assign(msg, next)
|
|
385
|
-
}
|
|
386
|
-
return null
|
|
387
|
-
}
|
|
388
|
-
if (this.closed) {
|
|
389
|
-
return io.EOF
|
|
390
|
-
}
|
|
391
|
-
return new Promise<$.GoError>((resolve) => {
|
|
392
|
-
this.waiters.push((sent) => {
|
|
393
|
-
if (msg != null && sent != null) {
|
|
394
|
-
Object.assign(msg, sent)
|
|
395
|
-
}
|
|
396
|
-
resolve(null)
|
|
397
|
-
})
|
|
398
|
-
})
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
public CloseSend(): $.GoError {
|
|
402
|
-
return null
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
public Close(): $.GoError {
|
|
406
|
-
this.closed = true
|
|
407
|
-
for (const waiter of this.waiters.splice(0)) {
|
|
408
|
-
waiter(null)
|
|
409
|
-
}
|
|
410
|
-
return null
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
public CopySentTo(output: Message | null): void {
|
|
414
|
-
if (output != null && this.sent != null) {
|
|
415
|
-
Object.assign(output, this.sent)
|
|
416
|
-
return
|
|
417
|
-
}
|
|
418
|
-
const next = this.sentQueue.shift()
|
|
419
|
-
if (output != null && next != null) {
|
|
420
|
-
Object.assign(output, next)
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
class streamQueue {
|
|
426
|
-
private queue: (Message | null)[] = []
|
|
427
|
-
private waiters: ((msg: Message | null, err: $.GoError) => void)[] = []
|
|
428
|
-
private closed = false
|
|
429
|
-
|
|
430
|
-
public send(msg: Message | null): $.GoError {
|
|
431
|
-
if (this.closed) {
|
|
432
|
-
return ErrCompleted
|
|
433
|
-
}
|
|
434
|
-
const waiter = this.waiters.shift()
|
|
435
|
-
if (waiter != null) {
|
|
436
|
-
waiter(msg, null)
|
|
437
|
-
return null
|
|
438
|
-
}
|
|
439
|
-
this.queue.push(msg)
|
|
440
|
-
return null
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
public recv(msg: Message | null): MaybePromise<$.GoError> {
|
|
444
|
-
const next = this.queue.shift()
|
|
445
|
-
if (next !== undefined) {
|
|
446
|
-
if (msg != null && next != null) {
|
|
447
|
-
Object.assign(msg, next)
|
|
448
|
-
}
|
|
449
|
-
return null
|
|
450
|
-
}
|
|
451
|
-
if (this.closed) {
|
|
452
|
-
return io.EOF
|
|
453
|
-
}
|
|
454
|
-
return new Promise<$.GoError>((resolve) => {
|
|
455
|
-
this.waiters.push((sent, err) => {
|
|
456
|
-
if (msg != null && sent != null) {
|
|
457
|
-
Object.assign(msg, sent)
|
|
458
|
-
}
|
|
459
|
-
resolve(err)
|
|
460
|
-
})
|
|
461
|
-
})
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
public close(): $.GoError {
|
|
465
|
-
this.closed = true
|
|
466
|
-
for (const waiter of this.waiters.splice(0)) {
|
|
467
|
-
waiter(null, io.EOF)
|
|
468
|
-
}
|
|
469
|
-
return null
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
class pairedMemoryStream implements Stream {
|
|
474
|
-
constructor(
|
|
475
|
-
private ctx: context.Context,
|
|
476
|
-
private incoming: streamQueue,
|
|
477
|
-
private outgoing: streamQueue,
|
|
478
|
-
) {}
|
|
479
|
-
|
|
480
|
-
public Context(): context.Context {
|
|
481
|
-
return this.ctx
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
public MsgSend(msg: Message | null): $.GoError {
|
|
485
|
-
return this.outgoing.send(msg)
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
public MsgRecv(msg: Message | null): MaybePromise<$.GoError> {
|
|
489
|
-
return this.incoming.recv(msg)
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
public CloseSend(): $.GoError {
|
|
493
|
-
return this.outgoing.close()
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
public Close(): $.GoError {
|
|
497
|
-
const incomingErr = this.incoming.close()
|
|
498
|
-
const outgoingErr = this.outgoing.close()
|
|
499
|
-
return incomingErr ?? outgoingErr
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
class streamWithClose implements Stream {
|
|
504
|
-
constructor(
|
|
505
|
-
private stream: Stream,
|
|
506
|
-
private closeFn: () => MaybePromise<$.GoError>,
|
|
507
|
-
) {}
|
|
508
|
-
|
|
509
|
-
public Context(): context.Context {
|
|
510
|
-
return this.stream.Context()
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
public MsgSend(msg: Message | null): MaybePromise<$.GoError> {
|
|
514
|
-
return this.stream.MsgSend(msg)
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
public MsgRecv(msg: Message | null): MaybePromise<$.GoError> {
|
|
518
|
-
return this.stream.MsgRecv(msg)
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
public CloseSend(): MaybePromise<$.GoError> {
|
|
522
|
-
return this.stream.CloseSend()
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
public async Close(): Promise<$.GoError> {
|
|
526
|
-
const err = await this.stream.Close()
|
|
527
|
-
const closeErr = await this.closeFn()
|
|
528
|
-
return err ?? closeErr
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
export function NewStreamWithClose(
|
|
533
|
-
stream: Stream | null,
|
|
534
|
-
close: () => MaybePromise<$.GoError>,
|
|
535
|
-
): Stream {
|
|
536
|
-
return new streamWithClose(stream ?? new emptyStream(), close)
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
export interface Invoker {
|
|
540
|
-
InvokeMethod(
|
|
541
|
-
serviceID: string,
|
|
542
|
-
methodID: string,
|
|
543
|
-
stream: Stream | null,
|
|
544
|
-
): MaybePromise<[boolean, $.GoError]>
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
export interface QueryableInvoker {
|
|
548
|
-
HasService(serviceID: string): boolean
|
|
549
|
-
HasServiceMethod(serviceID: string, methodID: string): boolean
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
export type InvokerFunc = (
|
|
553
|
-
serviceID: string,
|
|
554
|
-
methodID: string,
|
|
555
|
-
stream: Stream | null,
|
|
556
|
-
) => [boolean, $.GoError] | Promise<[boolean, $.GoError]>
|
|
557
|
-
|
|
558
|
-
export async function InvokeMethod(
|
|
559
|
-
fn: InvokerFunc,
|
|
560
|
-
serviceID: string,
|
|
561
|
-
methodID: string,
|
|
562
|
-
stream: Stream | null,
|
|
563
|
-
): Promise<[boolean, $.GoError]> {
|
|
564
|
-
if (fn == null) {
|
|
565
|
-
return [false, null]
|
|
566
|
-
}
|
|
567
|
-
return fn(serviceID, methodID, stream)
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
export async function InvokerFunc_InvokeMethod(
|
|
571
|
-
fn: InvokerFunc,
|
|
572
|
-
serviceID: string,
|
|
573
|
-
methodID: string,
|
|
574
|
-
stream: Stream | null,
|
|
575
|
-
): Promise<[boolean, $.GoError]> {
|
|
576
|
-
return InvokeMethod(fn, serviceID, methodID, stream)
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
export type InvokerSlice = $.Slice<Invoker | null>
|
|
580
|
-
|
|
581
|
-
export async function InvokerSlice_InvokeMethod(
|
|
582
|
-
s: InvokerSlice,
|
|
583
|
-
serviceID: string,
|
|
584
|
-
methodID: string,
|
|
585
|
-
stream: Stream | null,
|
|
586
|
-
): Promise<[boolean, $.GoError]> {
|
|
587
|
-
for (let i = 0; i < $.len(s); i++) {
|
|
588
|
-
const invoker = s![i]
|
|
589
|
-
if (invoker == null) {
|
|
590
|
-
continue
|
|
591
|
-
}
|
|
592
|
-
const [handled, err] = await invoker.InvokeMethod(
|
|
593
|
-
serviceID,
|
|
594
|
-
methodID,
|
|
595
|
-
stream,
|
|
596
|
-
)
|
|
597
|
-
if (handled || err != null) {
|
|
598
|
-
return [true, err]
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
return [false, null]
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
export interface Handler extends Invoker {
|
|
605
|
-
GetServiceID(): string
|
|
606
|
-
GetMethodIDs(): $.Slice<string>
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
type HandlerMap = Map<string, Handler>
|
|
610
|
-
|
|
611
|
-
export interface Mux extends Invoker, QueryableInvoker {
|
|
612
|
-
Register(handler: Handler | null): $.GoError
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
class defaultMux implements Mux {
|
|
616
|
-
private fallback: $.Slice<Invoker | null>
|
|
617
|
-
private services = new Map<string, HandlerMap>()
|
|
618
|
-
|
|
619
|
-
constructor(fallback: $.Slice<Invoker | null>) {
|
|
620
|
-
this.fallback = fallback
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
public Register(handler: Handler | null): $.GoError {
|
|
624
|
-
if (handler == null) {
|
|
625
|
-
return ErrUnimplemented
|
|
626
|
-
}
|
|
627
|
-
const serviceID = handler.GetServiceID()
|
|
628
|
-
if (serviceID === '') {
|
|
629
|
-
return ErrEmptyServiceID
|
|
630
|
-
}
|
|
631
|
-
let methods = this.services.get(serviceID)
|
|
632
|
-
if (methods === undefined) {
|
|
633
|
-
methods = new Map()
|
|
634
|
-
this.services.set(serviceID, methods)
|
|
635
|
-
}
|
|
636
|
-
const methodIDs = handler.GetMethodIDs()
|
|
637
|
-
for (let i = 0; i < $.len(methodIDs); i++) {
|
|
638
|
-
const methodID = methodIDs![i]
|
|
639
|
-
if (methodID !== '') {
|
|
640
|
-
methods.set(methodID, handler)
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
return null
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
public HasService(serviceID: string): boolean {
|
|
647
|
-
return serviceID !== '' && (this.services.get(serviceID)?.size ?? 0) !== 0
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
public HasServiceMethod(serviceID: string, methodID: string): boolean {
|
|
651
|
-
if (serviceID === '' || methodID === '') {
|
|
652
|
-
return false
|
|
653
|
-
}
|
|
654
|
-
return this.services.get(serviceID)?.has(methodID) ?? false
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
public async InvokeMethod(
|
|
658
|
-
serviceID: string,
|
|
659
|
-
methodID: string,
|
|
660
|
-
stream: Stream | null,
|
|
661
|
-
): Promise<[boolean, $.GoError]> {
|
|
662
|
-
let handler: Handler | undefined
|
|
663
|
-
if (serviceID === '') {
|
|
664
|
-
for (const methods of this.services.values()) {
|
|
665
|
-
handler = methods.get(methodID)
|
|
666
|
-
if (handler !== undefined) {
|
|
667
|
-
break
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
} else {
|
|
671
|
-
handler = this.services.get(serviceID)?.get(methodID)
|
|
672
|
-
}
|
|
673
|
-
if (handler !== undefined) {
|
|
674
|
-
return handler.InvokeMethod(serviceID, methodID, stream)
|
|
675
|
-
}
|
|
676
|
-
for (let i = 0; i < $.len(this.fallback); i++) {
|
|
677
|
-
const invoker = this.fallback![i]
|
|
678
|
-
if (invoker == null) {
|
|
679
|
-
continue
|
|
680
|
-
}
|
|
681
|
-
const [handled, err] = await invoker.InvokeMethod(
|
|
682
|
-
serviceID,
|
|
683
|
-
methodID,
|
|
684
|
-
stream,
|
|
685
|
-
)
|
|
686
|
-
if (handled || err != null) {
|
|
687
|
-
return [handled, err]
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
return [false, null]
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
export function NewMux(...fallbackInvokers: (Invoker | null)[]): Mux {
|
|
695
|
-
return new defaultMux(fallbackInvokers)
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
export class PrefixInvoker implements Invoker {
|
|
699
|
-
private inv: Invoker | null
|
|
700
|
-
private serviceIDPrefixes: $.Slice<string>
|
|
701
|
-
|
|
702
|
-
constructor(
|
|
703
|
-
invOrInit?:
|
|
704
|
-
| Invoker
|
|
705
|
-
| Partial<{ inv?: Invoker | null; serviceIDPrefixes?: $.Slice<string> }>
|
|
706
|
-
| null,
|
|
707
|
-
serviceIDPrefixes?: $.Slice<string>,
|
|
708
|
-
) {
|
|
709
|
-
if (
|
|
710
|
-
invOrInit != null &&
|
|
711
|
-
typeof invOrInit === 'object' &&
|
|
712
|
-
'inv' in invOrInit
|
|
713
|
-
) {
|
|
714
|
-
this.inv = invOrInit.inv ?? null
|
|
715
|
-
this.serviceIDPrefixes = invOrInit.serviceIDPrefixes ?? null
|
|
716
|
-
return
|
|
717
|
-
}
|
|
718
|
-
this.inv = invOrInit as Invoker | null
|
|
719
|
-
this.serviceIDPrefixes = serviceIDPrefixes ?? null
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
public async InvokeMethod(
|
|
723
|
-
serviceID: string,
|
|
724
|
-
methodID: string,
|
|
725
|
-
stream: Stream | null,
|
|
726
|
-
): Promise<[boolean, $.GoError]> {
|
|
727
|
-
if ($.len(this.serviceIDPrefixes) !== 0) {
|
|
728
|
-
const [strippedID, matchedPrefix] = CheckStripPrefix(
|
|
729
|
-
serviceID,
|
|
730
|
-
this.serviceIDPrefixes,
|
|
731
|
-
)
|
|
732
|
-
if (matchedPrefix === '') {
|
|
733
|
-
return [false, null]
|
|
734
|
-
}
|
|
735
|
-
serviceID = strippedID
|
|
736
|
-
}
|
|
737
|
-
if (this.inv == null) {
|
|
738
|
-
return [false, null]
|
|
739
|
-
}
|
|
740
|
-
return this.inv.InvokeMethod(serviceID, methodID, stream)
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
export function NewPrefixInvoker(
|
|
745
|
-
inv: Invoker | null,
|
|
746
|
-
serviceIDPrefixes: $.Slice<string>,
|
|
747
|
-
): PrefixInvoker {
|
|
748
|
-
return new PrefixInvoker(inv, serviceIDPrefixes)
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
export function CheckStripPrefix(
|
|
752
|
-
serviceID: string,
|
|
753
|
-
serviceIDPrefixes: $.Slice<string>,
|
|
754
|
-
): [string, string] {
|
|
755
|
-
for (let i = 0; i < $.len(serviceIDPrefixes); i++) {
|
|
756
|
-
const prefix = serviceIDPrefixes![i]
|
|
757
|
-
if (prefix !== '' && serviceID.startsWith(prefix)) {
|
|
758
|
-
return [serviceID.slice(prefix.length), prefix]
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
return [serviceID, '']
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
export class Server {
|
|
765
|
-
private invoker: Invoker | null
|
|
766
|
-
|
|
767
|
-
constructor(init?: Partial<{ invoker?: Invoker | null }>) {
|
|
768
|
-
this.invoker = init?.invoker ?? null
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
public GetInvoker(): Invoker | null {
|
|
772
|
-
return this.invoker
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
public HandleStream(
|
|
776
|
-
_ctx: context.Context,
|
|
777
|
-
_rwc: io.ReadWriteCloser | null,
|
|
778
|
-
): void {}
|
|
779
|
-
|
|
780
|
-
public AcceptMuxedConn(
|
|
781
|
-
_ctx: context.Context,
|
|
782
|
-
_conn: MuxedConn | null,
|
|
783
|
-
): $.GoError {
|
|
784
|
-
return null
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
export function NewServer(invoker: Invoker | null): Server {
|
|
789
|
-
return new Server({ invoker })
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
export interface PacketWriter {
|
|
793
|
-
WritePacket(packet: Packet | null): MaybePromise<$.GoError>
|
|
794
|
-
Close(): MaybePromise<$.GoError>
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
export type OpenStreamFunc = ((
|
|
798
|
-
ctx: context.Context,
|
|
799
|
-
msgHandler: PacketDataHandler,
|
|
800
|
-
closeHandler: CloseHandler,
|
|
801
|
-
) => MaybePromise<[PacketWriter | null, $.GoError]>) & {
|
|
802
|
-
__server?: Server
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
class transportClient implements Client {
|
|
806
|
-
constructor(private openStream: OpenStreamFunc | null) {}
|
|
807
|
-
|
|
808
|
-
public async ExecCall(
|
|
809
|
-
ctx: context.Context,
|
|
810
|
-
service: string,
|
|
811
|
-
method: string,
|
|
812
|
-
input: Message | null,
|
|
813
|
-
output: Message | null,
|
|
814
|
-
): Promise<$.GoError> {
|
|
815
|
-
if (this.openStream == null) {
|
|
816
|
-
return ErrNoAvailableClients
|
|
817
|
-
}
|
|
818
|
-
const writerResult = await this.openStream(
|
|
819
|
-
ctx,
|
|
820
|
-
() => null,
|
|
821
|
-
() => undefined,
|
|
822
|
-
)
|
|
823
|
-
if (writerResult == null) {
|
|
824
|
-
return ErrNoAvailableClients
|
|
825
|
-
}
|
|
826
|
-
const writer = writerResult[0]
|
|
827
|
-
const err = writerResult[1]
|
|
828
|
-
if (err != null) {
|
|
829
|
-
return err
|
|
830
|
-
}
|
|
831
|
-
if (input != null) {
|
|
832
|
-
const [data, marshalErr] = input.MarshalVT()
|
|
833
|
-
if (marshalErr != null) {
|
|
834
|
-
return marshalErr
|
|
835
|
-
}
|
|
836
|
-
const writeErr = await writer?.WritePacket(
|
|
837
|
-
NewCallStartPacket(service, method, data, $.len(data) === 0),
|
|
838
|
-
)
|
|
839
|
-
if (writeErr != null) {
|
|
840
|
-
return writeErr
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
if (output != null) {
|
|
844
|
-
output.Reset()
|
|
845
|
-
}
|
|
846
|
-
return (await writer?.Close()) ?? null
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
public async NewStream(
|
|
850
|
-
ctx: context.Context,
|
|
851
|
-
service: string,
|
|
852
|
-
method: string,
|
|
853
|
-
firstMsg: Message | null,
|
|
854
|
-
): Promise<[Stream | null, $.GoError]> {
|
|
855
|
-
if (this.openStream == null) {
|
|
856
|
-
return [null, ErrNoAvailableClients]
|
|
857
|
-
}
|
|
858
|
-
const writerResult = await this.openStream(
|
|
859
|
-
ctx,
|
|
860
|
-
() => null,
|
|
861
|
-
() => undefined,
|
|
862
|
-
)
|
|
863
|
-
if (writerResult == null) {
|
|
864
|
-
return [null, ErrNoAvailableClients]
|
|
865
|
-
}
|
|
866
|
-
const writer = writerResult[0]
|
|
867
|
-
const err = writerResult[1]
|
|
868
|
-
if (err != null) {
|
|
869
|
-
return [null, err]
|
|
870
|
-
}
|
|
871
|
-
if (firstMsg != null) {
|
|
872
|
-
const [data, marshalErr] = firstMsg.MarshalVT()
|
|
873
|
-
if (marshalErr != null) {
|
|
874
|
-
return [null, marshalErr]
|
|
875
|
-
}
|
|
876
|
-
const writeErr = await writer?.WritePacket(
|
|
877
|
-
NewCallStartPacket(service, method, data, $.len(data) === 0),
|
|
878
|
-
)
|
|
879
|
-
if (writeErr != null) {
|
|
880
|
-
return [null, writeErr]
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
return [
|
|
884
|
-
NewStreamWithClose(new emptyStream(ctx), () => writer?.Close() ?? null),
|
|
885
|
-
null,
|
|
886
|
-
]
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
export function NewClient(openStream: OpenStreamFunc | null): Client {
|
|
891
|
-
if (openStream?.__server != null) {
|
|
892
|
-
return NewClientWithInvoker(openStream.__server.GetInvoker())
|
|
893
|
-
}
|
|
894
|
-
return new transportClient(openStream)
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
class invokerClient implements Client {
|
|
898
|
-
constructor(
|
|
899
|
-
private invoker: Invoker | null,
|
|
900
|
-
private contextFn:
|
|
901
|
-
| ((ctx: context.Context) => context.Context)
|
|
902
|
-
| null = null,
|
|
903
|
-
) {}
|
|
904
|
-
|
|
905
|
-
public async ExecCall(
|
|
906
|
-
ctx: context.Context,
|
|
907
|
-
service: string,
|
|
908
|
-
method: string,
|
|
909
|
-
input: Message | null,
|
|
910
|
-
output: Message | null,
|
|
911
|
-
): Promise<$.GoError> {
|
|
912
|
-
if (this.invoker == null) {
|
|
913
|
-
return ErrNoAvailableClients
|
|
914
|
-
}
|
|
915
|
-
const stream = new memoryStream(
|
|
916
|
-
this.contextFn == null ? ctx : this.contextFn(ctx),
|
|
917
|
-
input,
|
|
918
|
-
)
|
|
919
|
-
const [handled, err] = await this.invoker.InvokeMethod(
|
|
920
|
-
service,
|
|
921
|
-
method,
|
|
922
|
-
stream,
|
|
923
|
-
)
|
|
924
|
-
if (err != null) {
|
|
925
|
-
return err
|
|
926
|
-
}
|
|
927
|
-
if (!handled) {
|
|
928
|
-
return ErrUnimplemented
|
|
929
|
-
}
|
|
930
|
-
stream.CopySentTo(output)
|
|
931
|
-
return null
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
public async NewStream(
|
|
935
|
-
ctx: context.Context,
|
|
936
|
-
service: string,
|
|
937
|
-
method: string,
|
|
938
|
-
firstMsg: Message | null,
|
|
939
|
-
): Promise<[Stream | null, $.GoError]> {
|
|
940
|
-
if (this.invoker == null) {
|
|
941
|
-
return [null, ErrNoAvailableClients]
|
|
942
|
-
}
|
|
943
|
-
const streamCtx = this.contextFn == null ? ctx : this.contextFn(ctx)
|
|
944
|
-
const clientInput = new streamQueue()
|
|
945
|
-
const serverInput = new streamQueue()
|
|
946
|
-
const clientStream = new pairedMemoryStream(
|
|
947
|
-
streamCtx,
|
|
948
|
-
clientInput,
|
|
949
|
-
serverInput,
|
|
950
|
-
)
|
|
951
|
-
const serverStream = new pairedMemoryStream(
|
|
952
|
-
streamCtx,
|
|
953
|
-
serverInput,
|
|
954
|
-
clientInput,
|
|
955
|
-
)
|
|
956
|
-
if (firstMsg != null) {
|
|
957
|
-
const err = serverInput.send(firstMsg)
|
|
958
|
-
if (err != null) {
|
|
959
|
-
return [null, err]
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
const pending = Promise.resolve(
|
|
963
|
-
this.invoker.InvokeMethod(service, method, serverStream),
|
|
964
|
-
)
|
|
965
|
-
pending.then(([handled, err]) => {
|
|
966
|
-
if (!handled || err != null) {
|
|
967
|
-
clientStream.Close()
|
|
968
|
-
return
|
|
969
|
-
}
|
|
970
|
-
serverStream.CloseSend()
|
|
971
|
-
})
|
|
972
|
-
return [clientStream, null]
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
|
|
976
|
-
export function NewClientWithInvoker(
|
|
977
|
-
invoker: Invoker | null,
|
|
978
|
-
contextFn: ((ctx: context.Context) => context.Context) | null = null,
|
|
979
|
-
): Client {
|
|
980
|
-
return new invokerClient(invoker, contextFn)
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
export type PacketHandler =
|
|
984
|
-
| ((pkt: Packet | null) => MaybePromise<$.GoError>)
|
|
985
|
-
| null
|
|
986
|
-
export type PacketDataHandler =
|
|
987
|
-
| ((data: $.Slice<number>) => MaybePromise<$.GoError>)
|
|
988
|
-
| null
|
|
989
|
-
export type CloseHandler = ((closeErr: $.GoError) => void) | null
|
|
990
|
-
|
|
991
|
-
class streamWithContext implements Stream {
|
|
992
|
-
constructor(
|
|
993
|
-
private stream: Stream,
|
|
994
|
-
private ctx: context.Context,
|
|
995
|
-
) {}
|
|
996
|
-
|
|
997
|
-
public Context(): context.Context {
|
|
998
|
-
return this.ctx
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
public MsgSend(msg: Message | null): MaybePromise<$.GoError> {
|
|
1002
|
-
return this.stream.MsgSend(msg)
|
|
1003
|
-
}
|
|
1004
|
-
|
|
1005
|
-
public MsgRecv(msg: Message | null): MaybePromise<$.GoError> {
|
|
1006
|
-
return this.stream.MsgRecv(msg)
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
public CloseSend(): MaybePromise<$.GoError> {
|
|
1010
|
-
return this.stream.CloseSend()
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
public Close(): MaybePromise<$.GoError> {
|
|
1014
|
-
return this.stream.Close()
|
|
1015
|
-
}
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
export function NewStreamWithContext(
|
|
1019
|
-
stream: Stream | null,
|
|
1020
|
-
ctx: context.Context,
|
|
1021
|
-
): Stream {
|
|
1022
|
-
return new streamWithContext(stream ?? new emptyStream(ctx), ctx)
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
export class ServerRPC {
|
|
1026
|
-
constructor(
|
|
1027
|
-
private ctx: context.Context,
|
|
1028
|
-
private invoker: Invoker | null,
|
|
1029
|
-
private writer: PacketWriter | null,
|
|
1030
|
-
) {}
|
|
1031
|
-
|
|
1032
|
-
public async HandlePacketData(data: $.Slice<number>): Promise<$.GoError> {
|
|
1033
|
-
const pkt = new Packet()
|
|
1034
|
-
const err = pkt.UnmarshalVT(data)
|
|
1035
|
-
if (err != null) {
|
|
1036
|
-
return err
|
|
1037
|
-
}
|
|
1038
|
-
return this.HandlePacket(pkt)
|
|
1039
|
-
}
|
|
1040
|
-
|
|
1041
|
-
public async HandlePacket(pkt: Packet | null): Promise<$.GoError> {
|
|
1042
|
-
if (pkt == null) {
|
|
1043
|
-
return null
|
|
1044
|
-
}
|
|
1045
|
-
const body = pkt.GetBody()
|
|
1046
|
-
if (body instanceof Packet_CallStart) {
|
|
1047
|
-
return this.HandleCallStart(body.CallStart)
|
|
1048
|
-
}
|
|
1049
|
-
return null
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
public async HandleCallStart(pkt: CallStart | null): Promise<$.GoError> {
|
|
1053
|
-
if (pkt == null || this.invoker == null) {
|
|
1054
|
-
return ErrUnimplemented
|
|
1055
|
-
}
|
|
1056
|
-
const stream = new emptyStream(this.ctx)
|
|
1057
|
-
const [handled, err] = await this.invoker.InvokeMethod(
|
|
1058
|
-
pkt.GetRpcService(),
|
|
1059
|
-
pkt.GetRpcMethod(),
|
|
1060
|
-
stream,
|
|
1061
|
-
)
|
|
1062
|
-
const callErr = err ?? (handled ? null : ErrUnimplemented)
|
|
1063
|
-
await this.writer?.WritePacket(
|
|
1064
|
-
NewCallDataPacket(null, false, true, callErr),
|
|
1065
|
-
)
|
|
1066
|
-
return callErr
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
public HandleStreamClose(_closeErr: $.GoError): void {}
|
|
1070
|
-
|
|
1071
|
-
public Wait(_ctx: context.Context): $.GoError {
|
|
1072
|
-
return null
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
export function NewServerRPC(
|
|
1077
|
-
ctx: context.Context,
|
|
1078
|
-
invoker: Invoker | null,
|
|
1079
|
-
writer: PacketWriter | null,
|
|
1080
|
-
): ServerRPC {
|
|
1081
|
-
return new ServerRPC(ctx, invoker, writer)
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
export class PacketReadWriter implements PacketWriter {
|
|
1085
|
-
constructor(private rw: io.ReadWriteCloser | null) {}
|
|
1086
|
-
|
|
1087
|
-
public Write(data: $.Slice<number>): [number, $.GoError] {
|
|
1088
|
-
if (this.rw == null) {
|
|
1089
|
-
return [0, ErrNilWriter]
|
|
1090
|
-
}
|
|
1091
|
-
return this.rw.Write(data)
|
|
1092
|
-
}
|
|
1093
|
-
|
|
1094
|
-
public WritePacket(_packet: Packet | null): $.GoError {
|
|
1095
|
-
return this.rw == null ? ErrNilWriter : null
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
public ReadPump(_cb: PacketDataHandler, closed: CloseHandler): void {
|
|
1099
|
-
if (closed != null) {
|
|
1100
|
-
closed(null)
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
public ReadToHandler(_cb: PacketDataHandler): $.GoError {
|
|
1105
|
-
return null
|
|
1106
|
-
}
|
|
1107
|
-
|
|
1108
|
-
public Close(): $.GoError {
|
|
1109
|
-
return this.rw?.Close() ?? null
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
|
|
1113
|
-
export function NewPacketReadWriter(
|
|
1114
|
-
rw: io.ReadWriteCloser | null,
|
|
1115
|
-
): PacketReadWriter {
|
|
1116
|
-
return new PacketReadWriter(rw)
|
|
1117
|
-
}
|
|
1118
|
-
|
|
1119
|
-
export function NewServerPipe(server: Server | null): OpenStreamFunc {
|
|
1120
|
-
const openStream = ((
|
|
1121
|
-
_ctx: context.Context,
|
|
1122
|
-
_msgHandler: PacketDataHandler,
|
|
1123
|
-
_closeHandler: CloseHandler,
|
|
1124
|
-
): [PacketWriter | null, $.GoError] => [
|
|
1125
|
-
new closedPacketWriter(),
|
|
1126
|
-
null,
|
|
1127
|
-
]) as OpenStreamFunc
|
|
1128
|
-
if (server != null) {
|
|
1129
|
-
openStream.__server = server
|
|
1130
|
-
}
|
|
1131
|
-
return openStream
|
|
1132
|
-
}
|
|
1133
|
-
|
|
1134
|
-
export class MuxedConn {
|
|
1135
|
-
constructor(
|
|
1136
|
-
public rwc: any = null,
|
|
1137
|
-
public outbound = false,
|
|
1138
|
-
) {}
|
|
1139
|
-
|
|
1140
|
-
public Close(): $.GoError {
|
|
1141
|
-
return this.rwc?.Close() ?? null
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
|
|
1145
|
-
class closedPacketWriter implements PacketWriter {
|
|
1146
|
-
public WritePacket(_packet: Packet | null): $.GoError {
|
|
1147
|
-
return ErrUnimplemented
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
public Close(): $.GoError {
|
|
1151
|
-
return null
|
|
1152
|
-
}
|
|
1153
|
-
}
|
|
1154
|
-
|
|
1155
|
-
export function NewOpenStreamWithMuxedConn(_conn: MuxedConn): OpenStreamFunc {
|
|
1156
|
-
return () => [new closedPacketWriter(), null]
|
|
1157
|
-
}
|
|
1158
|
-
|
|
1159
|
-
export function NewMuxedConn(
|
|
1160
|
-
rwc: any,
|
|
1161
|
-
outbound: boolean,
|
|
1162
|
-
_yamuxConf: unknown,
|
|
1163
|
-
): [MuxedConn | null, $.GoError] {
|
|
1164
|
-
return [new MuxedConn(rwc, outbound), null]
|
|
1165
|
-
}
|
|
1166
|
-
|
|
1167
|
-
export function NewWebSocketConn(
|
|
1168
|
-
_ctx: context.Context,
|
|
1169
|
-
conn: unknown,
|
|
1170
|
-
isServer: boolean,
|
|
1171
|
-
yamuxConf: unknown,
|
|
1172
|
-
): [MuxedConn | null, $.GoError] {
|
|
1173
|
-
return NewMuxedConn(conn, !isServer, yamuxConf)
|
|
1174
|
-
}
|
|
1175
|
-
|
|
1176
|
-
export function NewClientWithMuxedConn(_conn: MuxedConn | null): Client {
|
|
1177
|
-
return NewClientWithInvoker(null)
|
|
1178
|
-
}
|
|
1179
|
-
|
|
1180
|
-
export function NewMuxedConnWithRwc(
|
|
1181
|
-
_ctx: context.Context,
|
|
1182
|
-
_rwc: io.ReadWriteCloser | null,
|
|
1183
|
-
_outbound: boolean,
|
|
1184
|
-
_yamuxConf: unknown,
|
|
1185
|
-
): [MuxedConn | null, $.GoError] {
|
|
1186
|
-
return [new MuxedConn(_rwc, _outbound), null]
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1189
|
-
class clientInvoker implements Invoker {
|
|
1190
|
-
constructor(private client: Client | null) {}
|
|
1191
|
-
|
|
1192
|
-
public async InvokeMethod(
|
|
1193
|
-
serviceID: string,
|
|
1194
|
-
methodID: string,
|
|
1195
|
-
stream: Stream | null,
|
|
1196
|
-
): Promise<[boolean, $.GoError]> {
|
|
1197
|
-
if (this.client == null || stream == null) {
|
|
1198
|
-
return [false, null]
|
|
1199
|
-
}
|
|
1200
|
-
const [remote, err] = await this.client.NewStream(
|
|
1201
|
-
stream.Context(),
|
|
1202
|
-
serviceID,
|
|
1203
|
-
methodID,
|
|
1204
|
-
null,
|
|
1205
|
-
)
|
|
1206
|
-
await remote?.Close()
|
|
1207
|
-
return [true, err]
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
|
|
1211
|
-
export function NewClientInvoker(client: Client | null): Invoker {
|
|
1212
|
-
return new clientInvoker(client)
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
|
-
export class Packet {
|
|
1216
|
-
public Body: unknown = null
|
|
1217
|
-
|
|
1218
|
-
public GetBody(): unknown {
|
|
1219
|
-
return this.Body
|
|
1220
|
-
}
|
|
1221
|
-
|
|
1222
|
-
public MarshalVT(): [$.Slice<number>, $.GoError] {
|
|
1223
|
-
return [new Uint8Array(), null]
|
|
1224
|
-
}
|
|
1225
|
-
|
|
1226
|
-
public UnmarshalVT(_data: $.Slice<number>): $.GoError {
|
|
1227
|
-
return null
|
|
1228
|
-
}
|
|
1229
|
-
|
|
1230
|
-
public SizeVT(): number {
|
|
1231
|
-
return 0
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
|
-
public MarshalToSizedBufferVT(_data: $.Slice<number>): [number, $.GoError] {
|
|
1235
|
-
return [0, null]
|
|
1236
|
-
}
|
|
1237
|
-
|
|
1238
|
-
public Reset(): void {
|
|
1239
|
-
this.Body = null
|
|
1240
|
-
}
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
export class CallStart {
|
|
1244
|
-
public RpcService = ''
|
|
1245
|
-
public RpcMethod = ''
|
|
1246
|
-
public Data: $.Slice<number> = null
|
|
1247
|
-
public DataIsZero = false
|
|
1248
|
-
|
|
1249
|
-
public GetRpcService(): string {
|
|
1250
|
-
return this.RpcService
|
|
1251
|
-
}
|
|
1252
|
-
|
|
1253
|
-
public GetRpcMethod(): string {
|
|
1254
|
-
return this.RpcMethod
|
|
1255
|
-
}
|
|
1256
|
-
|
|
1257
|
-
public GetData(): $.Slice<number> {
|
|
1258
|
-
return this.Data
|
|
1259
|
-
}
|
|
1260
|
-
|
|
1261
|
-
public GetDataIsZero(): boolean {
|
|
1262
|
-
return this.DataIsZero
|
|
1263
|
-
}
|
|
1264
|
-
}
|
|
1265
|
-
|
|
1266
|
-
export class CallData {
|
|
1267
|
-
public Data: $.Slice<number> = null
|
|
1268
|
-
public DataIsZero = false
|
|
1269
|
-
public Complete = false
|
|
1270
|
-
public Error = ''
|
|
1271
|
-
|
|
1272
|
-
public GetData(): $.Slice<number> {
|
|
1273
|
-
return this.Data
|
|
1274
|
-
}
|
|
1275
|
-
|
|
1276
|
-
public GetDataIsZero(): boolean {
|
|
1277
|
-
return this.DataIsZero
|
|
1278
|
-
}
|
|
1279
|
-
|
|
1280
|
-
public GetComplete(): boolean {
|
|
1281
|
-
return this.Complete
|
|
1282
|
-
}
|
|
1283
|
-
|
|
1284
|
-
public GetError(): string {
|
|
1285
|
-
return this.Error
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
|
|
1289
|
-
export class Packet_CallStart {
|
|
1290
|
-
constructor(public CallStart: CallStart | null = null) {}
|
|
1291
|
-
}
|
|
1292
|
-
|
|
1293
|
-
export class Packet_CallData {
|
|
1294
|
-
constructor(public CallData: CallData | null = null) {}
|
|
1295
|
-
}
|
|
1296
|
-
|
|
1297
|
-
export class Packet_CallCancel {
|
|
1298
|
-
constructor(public CallCancel = true) {}
|
|
1299
|
-
}
|
|
1300
|
-
|
|
1301
|
-
export function NewPacketDataHandler(
|
|
1302
|
-
handler: PacketHandler,
|
|
1303
|
-
): PacketDataHandler {
|
|
1304
|
-
return (data) => {
|
|
1305
|
-
const pkt = new Packet()
|
|
1306
|
-
const err = pkt.UnmarshalVT(data)
|
|
1307
|
-
if (err != null) {
|
|
1308
|
-
return err
|
|
1309
|
-
}
|
|
1310
|
-
return handler?.(pkt) ?? null
|
|
1311
|
-
}
|
|
1312
|
-
}
|
|
1313
|
-
|
|
1314
|
-
export function NewCallStartPacket(
|
|
1315
|
-
service: string,
|
|
1316
|
-
method: string,
|
|
1317
|
-
data: $.Slice<number>,
|
|
1318
|
-
dataIsZero: boolean,
|
|
1319
|
-
): Packet {
|
|
1320
|
-
const pkt = new Packet()
|
|
1321
|
-
pkt.Body = new Packet_CallStart(
|
|
1322
|
-
Object.assign(new CallStart(), {
|
|
1323
|
-
RpcService: service,
|
|
1324
|
-
RpcMethod: method,
|
|
1325
|
-
Data: data,
|
|
1326
|
-
DataIsZero: dataIsZero,
|
|
1327
|
-
}),
|
|
1328
|
-
)
|
|
1329
|
-
return pkt
|
|
1330
|
-
}
|
|
1331
|
-
|
|
1332
|
-
export function NewCallDataPacket(
|
|
1333
|
-
data: $.Slice<number>,
|
|
1334
|
-
dataIsZero: boolean,
|
|
1335
|
-
complete: boolean,
|
|
1336
|
-
err: $.GoError,
|
|
1337
|
-
): Packet {
|
|
1338
|
-
const pkt = new Packet()
|
|
1339
|
-
pkt.Body = new Packet_CallData(
|
|
1340
|
-
Object.assign(new CallData(), {
|
|
1341
|
-
Data: data,
|
|
1342
|
-
DataIsZero: dataIsZero,
|
|
1343
|
-
Complete: complete || err != null,
|
|
1344
|
-
Error: err?.Error() ?? '',
|
|
1345
|
-
}),
|
|
1346
|
-
)
|
|
1347
|
-
return pkt
|
|
1348
|
-
}
|
|
1349
|
-
|
|
1350
|
-
export function NewCallCancelPacket(): Packet {
|
|
1351
|
-
const pkt = new Packet()
|
|
1352
|
-
pkt.Body = new Packet_CallCancel(true)
|
|
1353
|
-
return pkt
|
|
1354
|
-
}
|
|
1355
|
-
|
|
1356
|
-
function contextErr(ctx: context.Context): $.GoError {
|
|
1357
|
-
if (ctx == null) {
|
|
1358
|
-
return null
|
|
1359
|
-
}
|
|
1360
|
-
return ctx.Err()
|
|
1361
|
-
}
|