@nmtjs/http-client 0.15.0-beta.37 → 0.15.0-beta.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2024 Denis Ilchyshyn
1
+ Copyright (c) 2025 Denys Ilchyshyn
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -6,4 +6,4 @@
6
6
  - binary data streaming and event subscriptions
7
7
  - contract-based API
8
8
  - end-to-end type safety
9
- - CPU-intensive task execution on separate workers
9
+ - CPU-intensive task execution on separate workers
package/dist/index.d.ts CHANGED
@@ -1,22 +1,53 @@
1
- import type { ClientMessageType } from '@nmtjs/protocol';
2
- import type { BaseProtocol, ProtocolBaseClientCallOptions, ProtocolBaseTransformer, ProtocolClientCall, ProtocolSendMetadata } from '@nmtjs/protocol/client';
3
- import { ProtocolTransport, ProtocolTransportStatus } from '@nmtjs/protocol/client';
1
+ import type { ClientTransport, ClientTransportFactory, ClientTransportMessageOptions, ClientTransportRpcParams } from '@nmtjs/client';
2
+ import type { ProtocolVersion } from '@nmtjs/protocol';
3
+ import type { BaseClientFormat } from '@nmtjs/protocol/client';
4
+ import { ConnectionType } from '@nmtjs/protocol';
5
+ type DecodeBase64Function = (data: string) => ArrayBufferView;
4
6
  export type HttpClientTransportOptions = {
5
7
  /**
6
8
  * The origin of the server
7
9
  * @example 'http://localhost:3000'
8
10
  */
9
- origin: string;
11
+ url: string;
10
12
  debug?: boolean;
13
+ EventSource?: typeof EventSource;
14
+ decodeBase64?: DecodeBase64Function;
11
15
  };
12
- export declare class HttpClientTransport extends ProtocolTransport<HttpClientTransportOptions> {
13
- #private;
14
- protected readonly protocol: BaseProtocol;
15
- protected readonly options: HttpClientTransportOptions;
16
- status: ProtocolTransportStatus;
17
- constructor(protocol: BaseProtocol, options: HttpClientTransportOptions);
18
- call(procedure: string, payload: any, options: ProtocolBaseClientCallOptions, transformer: ProtocolBaseTransformer): Promise<ProtocolClientCall>;
19
- connect(auth: any): Promise<void>;
20
- disconnect(): Promise<void>;
21
- send(_messageType: ClientMessageType, _buffer: ArrayBuffer, _metadata: ProtocolSendMetadata): Promise<void>;
16
+ export declare class HttpTransportClient implements ClientTransport<ConnectionType.Unidirectional> {
17
+ protected readonly format: BaseClientFormat;
18
+ protected readonly protocol: ProtocolVersion;
19
+ protected options: HttpClientTransportOptions;
20
+ type: ConnectionType.Unidirectional;
21
+ decodeBase64: DecodeBase64Function;
22
+ constructor(format: BaseClientFormat, protocol: ProtocolVersion, options: HttpClientTransportOptions);
23
+ url({ procedure, application, payload }: {
24
+ procedure: string;
25
+ application?: string;
26
+ payload?: unknown;
27
+ }): URL;
28
+ call(client: ClientTransportRpcParams, rpc: {
29
+ callId: number;
30
+ procedure: string;
31
+ payload: unknown;
32
+ }, options: ClientTransportMessageOptions): Promise<{
33
+ type: "rpc_stream";
34
+ stream: ReadableStream<ArrayBufferView<ArrayBufferLike>>;
35
+ } | {
36
+ type: "blob";
37
+ metadata: {
38
+ type: string;
39
+ size: number | undefined;
40
+ filename: string | undefined;
41
+ };
42
+ source: ReadableStream<Uint8Array<ArrayBuffer>>;
43
+ result?: undefined;
44
+ } | {
45
+ metadata?: undefined;
46
+ source?: undefined;
47
+ type: "rpc";
48
+ result: Uint8Array<ArrayBuffer>;
49
+ }>;
22
50
  }
51
+ export type HttpTransportFactory = ClientTransportFactory<ConnectionType.Unidirectional, HttpClientTransportOptions, HttpTransportClient>;
52
+ export declare const HttpTransportFactory: HttpTransportFactory;
53
+ export {};
package/dist/index.js CHANGED
@@ -1,84 +1,137 @@
1
- import { ClientError } from '@nmtjs/client';
2
- import { ErrorCode, ProtocolBlob } from '@nmtjs/protocol';
3
- import { ProtocolServerBlobStream, ProtocolTransport, ProtocolTransportStatus, } from '@nmtjs/protocol/client';
4
- export class HttpClientTransport extends ProtocolTransport {
1
+ import { createFuture } from '@nmtjs/common';
2
+ import { ConnectionType, ErrorCode, ProtocolBlob } from '@nmtjs/protocol';
3
+ import { ProtocolError } from '@nmtjs/protocol/client';
4
+ const createDecodeBase64 = (customFn) => {
5
+ return (string) => {
6
+ if ('fromBase64' in Uint8Array &&
7
+ typeof Uint8Array.fromBase64 === 'function') {
8
+ return Uint8Array.fromBase64(string);
9
+ }
10
+ else if (typeof atob === 'function') {
11
+ return Uint8Array.from(atob(string), (c) => c.charCodeAt(0));
12
+ }
13
+ else if (customFn) {
14
+ return customFn(string);
15
+ }
16
+ else {
17
+ throw new Error('No base64 decoding function available');
18
+ }
19
+ };
20
+ };
21
+ const NEEMATA_BLOB_HEADER = 'X-Neemata-Blob';
22
+ export class HttpTransportClient {
23
+ format;
5
24
  protocol;
6
25
  options;
7
- #auth = null;
8
- status = ProtocolTransportStatus.CONNECTED;
9
- constructor(protocol, options) {
10
- super();
26
+ type = ConnectionType.Unidirectional;
27
+ decodeBase64;
28
+ constructor(format, protocol, options) {
29
+ this.format = format;
11
30
  this.protocol = protocol;
12
31
  this.options = options;
32
+ this.options = { debug: false, ...options };
33
+ this.decodeBase64 = createDecodeBase64(options.decodeBase64);
13
34
  }
14
- async call(procedure, payload, options, transformer) {
15
- const call = this.protocol.createCall(procedure, options);
16
- const headers = new Headers();
17
- headers.set('Content-Type', this.protocol.contentType);
18
- headers.set('Accept', this.protocol.contentType);
19
- if (this.#auth)
20
- headers.set('Authorization', this.#auth);
21
- const isBlob = payload instanceof ProtocolBlob;
22
- if (isBlob)
23
- headers.set('x-neemata-blob', 'true');
24
- const response = fetch(`${this.options.origin}/api/${procedure}`, {
25
- method: 'POST',
26
- headers,
27
- credentials: 'include',
28
- body: isBlob
29
- ? payload.source
30
- : this.protocol.format.encode(transformer.encodeRPC(procedure, payload)),
31
- signal: call.signal,
32
- // @ts-expect-error
33
- duplex: 'half',
34
- });
35
- response
36
- .catch((error) => Promise.reject(new Error('Network error', { cause: error })))
37
- .then((response) => {
38
- const isBlob = response.headers.get('x-neemata-blob') === 'true';
39
- if (isBlob) {
40
- const contentLength = response.headers.get('content-length');
41
- const size = contentLength
42
- ? Number.parseInt(contentLength, 10) || undefined
43
- : undefined;
44
- const type = response.headers.get('content-type') || 'application/octet-stream';
45
- const stream = new ProtocolServerBlobStream(-1, { size, type });
46
- response.body?.pipeThrough(stream);
47
- return stream;
35
+ url({ procedure, application, payload, }) {
36
+ const base = application ? `/${application}/${procedure}` : `/${procedure}`;
37
+ const url = new URL(base, this.options.url);
38
+ if (payload)
39
+ url.searchParams.set('payload', JSON.stringify(payload));
40
+ return url;
41
+ }
42
+ async call(client, rpc, options) {
43
+ const { procedure, payload } = rpc;
44
+ const requestHeaders = new Headers();
45
+ const url = this.url({ application: client.application, procedure });
46
+ if (client.auth)
47
+ requestHeaders.set('Authorization', client.auth);
48
+ let body;
49
+ if (payload instanceof ProtocolBlob) {
50
+ requestHeaders.set('Content-Type', payload.metadata.type);
51
+ requestHeaders.set(NEEMATA_BLOB_HEADER, 'true');
52
+ }
53
+ else {
54
+ requestHeaders.set('Content-Type', client.format.contentType);
55
+ const buffer = client.format.encode(payload);
56
+ body = buffer;
57
+ }
58
+ if (options._stream_response) {
59
+ const _constructor = this.options.EventSource
60
+ ? this.options.EventSource
61
+ : EventSource;
62
+ const source = new _constructor(url.toString(), { withCredentials: true });
63
+ const future = createFuture();
64
+ const { readable, writable } = new TransformStream();
65
+ const writer = writable.getWriter();
66
+ source.addEventListener('open', () => future.resolve({ type: 'rpc_stream', stream: readable }));
67
+ source.addEventListener('close', () => writable.close());
68
+ source.addEventListener('error', (event) => {
69
+ const error = new Error('Stream error', { cause: event });
70
+ future.reject(error);
71
+ writable.abort(error);
72
+ });
73
+ source.addEventListener('message', (event) => {
74
+ try {
75
+ const buffer = this.decodeBase64(event.data);
76
+ writer.write(buffer);
77
+ }
78
+ catch (cause) {
79
+ const error = new Error('Failed to decode stream message', { cause });
80
+ writable.abort(error);
81
+ source.close();
82
+ }
83
+ });
84
+ return future.promise;
85
+ }
86
+ else {
87
+ const response = await fetch(url.toString(), {
88
+ body,
89
+ method: 'POST',
90
+ headers: requestHeaders,
91
+ signal: options.signal,
92
+ credentials: 'include',
93
+ keepalive: true,
94
+ });
95
+ if (response.ok) {
96
+ const isBlob = !!response.headers.get(NEEMATA_BLOB_HEADER);
97
+ if (isBlob) {
98
+ const contentLength = response.headers.get('content-length');
99
+ const size = (contentLength && Number.parseInt(contentLength, 10)) || undefined;
100
+ const type = response.headers.get('content-type') || 'application/octet-stream';
101
+ const disposition = response.headers.get('content-disposition');
102
+ let filename;
103
+ if (disposition) {
104
+ const match = disposition.match(/filename="?([^"]+)"?/);
105
+ if (match)
106
+ filename = match[1];
107
+ }
108
+ return {
109
+ type: 'blob',
110
+ metadata: { type, size, filename },
111
+ source: response.body,
112
+ };
113
+ }
114
+ else {
115
+ return { type: 'rpc', result: await response.bytes() };
116
+ }
48
117
  }
49
118
  else {
50
- const body = response.arrayBuffer();
51
- return body.then((buffer) => {
52
- if (response.ok) {
53
- const decoded = this.protocol.format.decode(buffer);
54
- return transformer.decodeRPC(procedure, decoded);
55
- }
56
- else {
57
- if (buffer.byteLength === 0) {
58
- const error = new ClientError(ErrorCode.InternalServerError, `Empty response with ${response.status} status code`);
59
- return Promise.reject(error);
60
- }
61
- else {
62
- const payload = this.protocol.format.decode(buffer);
63
- const error = new ClientError(payload.code, payload.message, payload.data);
64
- return Promise.reject(error);
65
- }
66
- }
67
- });
119
+ try {
120
+ const buffer = await response.bytes();
121
+ const error = client.format.decode(buffer);
122
+ throw new ProtocolError(error.code || ErrorCode.ClientRequestError, error.message || response.statusText, error.data);
123
+ }
124
+ catch (cause) {
125
+ if (cause instanceof ProtocolError)
126
+ throw cause;
127
+ // If decoding fails, throw generic error with status info
128
+ throw new ProtocolError(ErrorCode.ClientRequestError, `HTTP ${response.status}: ${response.statusText}`);
129
+ }
68
130
  }
69
- })
70
- .then(call.resolve)
71
- .catch(call.reject);
72
- return call;
73
- }
74
- async connect(auth) {
75
- this.#auth = auth;
76
- this.emit('connected');
77
- }
78
- async disconnect() {
79
- this.emit('disconnected', 'client');
80
- }
81
- async send(_messageType, _buffer, _metadata) {
82
- throw new Error('Not supported');
131
+ }
83
132
  }
84
133
  }
134
+ export const HttpTransportFactory = (params, options) => {
135
+ return new HttpTransportClient(params.format, params.protocol, options);
136
+ };
137
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACzE,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAItD,MAAM,kBAAkB,GAAG,CACzB,QAA+B,EACT,EAAE,CAAC;IACzB,OAAO,CAAC,MAAc,EAAE,EAAE,CAAC;QACzB,IACE,YAAY,IAAI,UAAU;YAC1B,OAAO,UAAU,CAAC,UAAU,KAAK,UAAU,EAC3C,CAAC;YACD,OAAO,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;QACtC,CAAC;aAAM,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;YACtC,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;QAC9D,CAAC;aAAM,IAAI,QAAQ,EAAE,CAAC;YACpB,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAA;QACzB,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAA;QAC1D,CAAC;IA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CAAA;gBACH,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,IAAI,KAAK,YAAY,aAAa;wBAAE,MAAM,KAAK,CAAA;oBAC/C,0DAA0D;oBAC1D,MAAM,IAAI,aAAa,CACrB,SAAS,CAAC,kBAAkB,EAC5B,QAAQ,QAAQ,CAAC,MAAM,KAAK,QAAQ,CAAC,UAAU,EAAE,CAClD,CAAA;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IAAA,CACF;CACF;AAQD,MAAM,CAAC,MAAM,oBAAoB,GAAyB,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC;IAC7E,OAAO,IAAI,mBAAmB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;AAAA,CACxE,CAAA"}
package/package.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "name": "@nmtjs/http-client",
3
+ "description": "HTTP client transport for Neemata.",
3
4
  "type": "module",
4
5
  "exports": {
5
6
  ".": {
@@ -9,23 +10,23 @@
9
10
  }
10
11
  },
11
12
  "dependencies": {
12
- "@nmtjs/client": "0.15.0-beta.37",
13
- "@nmtjs/common": "0.15.0-beta.37",
14
- "@nmtjs/protocol": "0.15.0-beta.37"
13
+ "@nmtjs/client": "0.15.0-beta.39",
14
+ "@nmtjs/common": "0.15.0-beta.39",
15
+ "@nmtjs/protocol": "0.15.0-beta.39"
15
16
  },
16
17
  "peerDependencies": {
17
- "@nmtjs/client": "0.15.0-beta.37",
18
- "@nmtjs/protocol": "0.15.0-beta.37",
19
- "@nmtjs/common": "0.15.0-beta.37"
18
+ "@nmtjs/client": "0.15.0-beta.39",
19
+ "@nmtjs/common": "0.15.0-beta.39",
20
+ "@nmtjs/protocol": "0.15.0-beta.39"
20
21
  },
21
22
  "files": [
22
23
  "dist",
24
+ "src",
23
25
  "LICENSE.md",
24
26
  "README.md"
25
27
  ],
26
- "version": "0.15.0-beta.37",
28
+ "version": "0.15.0-beta.39",
27
29
  "scripts": {
28
- "build": "tsc",
29
- "type-check": "tsc --noEmit"
30
+ "clean-build": "rm -rf ./dist"
30
31
  }
31
32
  }
package/src/index.ts ADDED
@@ -0,0 +1,197 @@
1
+ import type {
2
+ ClientTransport,
3
+ ClientTransportFactory,
4
+ ClientTransportMessageOptions,
5
+ ClientTransportRpcParams,
6
+ } from '@nmtjs/client'
7
+ import type { ProtocolVersion } from '@nmtjs/protocol'
8
+ import type { BaseClientFormat } from '@nmtjs/protocol/client'
9
+ import { createFuture } from '@nmtjs/common'
10
+ import { ConnectionType, ErrorCode, ProtocolBlob } from '@nmtjs/protocol'
11
+ import { ProtocolError } from '@nmtjs/protocol/client'
12
+
13
+ type DecodeBase64Function = (data: string) => ArrayBufferView
14
+
15
+ const createDecodeBase64 = (
16
+ customFn?: DecodeBase64Function,
17
+ ): DecodeBase64Function => {
18
+ return (string: string) => {
19
+ if (
20
+ 'fromBase64' in Uint8Array &&
21
+ typeof Uint8Array.fromBase64 === 'function'
22
+ ) {
23
+ return Uint8Array.fromBase64(string)
24
+ } else if (typeof atob === 'function') {
25
+ return Uint8Array.from(atob(string), (c) => c.charCodeAt(0))
26
+ } else if (customFn) {
27
+ return customFn(string)
28
+ } else {
29
+ throw new Error('No base64 decoding function available')
30
+ }
31
+ }
32
+ }
33
+
34
+ const NEEMATA_BLOB_HEADER = 'X-Neemata-Blob'
35
+
36
+ export type HttpClientTransportOptions = {
37
+ /**
38
+ * The origin of the server
39
+ * @example 'http://localhost:3000'
40
+ */
41
+ url: string
42
+ debug?: boolean
43
+ EventSource?: typeof EventSource
44
+ decodeBase64?: DecodeBase64Function
45
+ }
46
+
47
+ export class HttpTransportClient
48
+ implements ClientTransport<ConnectionType.Unidirectional>
49
+ {
50
+ type: ConnectionType.Unidirectional = ConnectionType.Unidirectional
51
+ decodeBase64: DecodeBase64Function
52
+
53
+ constructor(
54
+ protected readonly format: BaseClientFormat,
55
+ protected readonly protocol: ProtocolVersion,
56
+ protected options: HttpClientTransportOptions,
57
+ ) {
58
+ this.options = { debug: false, ...options }
59
+ this.decodeBase64 = createDecodeBase64(options.decodeBase64)
60
+ }
61
+
62
+ url({
63
+ procedure,
64
+ application,
65
+ payload,
66
+ }: {
67
+ procedure: string
68
+ application?: string
69
+ payload?: unknown
70
+ }) {
71
+ const base = application ? `/${application}/${procedure}` : `/${procedure}`
72
+ const url = new URL(base, this.options.url)
73
+ if (payload) url.searchParams.set('payload', JSON.stringify(payload))
74
+ return url
75
+ }
76
+
77
+ async call(
78
+ client: ClientTransportRpcParams,
79
+ rpc: { callId: number; procedure: string; payload: unknown },
80
+ options: ClientTransportMessageOptions,
81
+ ) {
82
+ const { procedure, payload } = rpc
83
+ const requestHeaders = new Headers()
84
+
85
+ const url = this.url({ application: client.application, procedure })
86
+
87
+ if (client.auth) requestHeaders.set('Authorization', client.auth)
88
+
89
+ let body: any
90
+
91
+ if (payload instanceof ProtocolBlob) {
92
+ requestHeaders.set('Content-Type', payload.metadata.type)
93
+ requestHeaders.set(NEEMATA_BLOB_HEADER, 'true')
94
+ } else {
95
+ requestHeaders.set('Content-Type', client.format.contentType)
96
+ const buffer = client.format.encode(payload)
97
+ body = buffer
98
+ }
99
+
100
+ if (options._stream_response) {
101
+ const _constructor = this.options.EventSource
102
+ ? this.options.EventSource
103
+ : EventSource
104
+ const source = new _constructor(url.toString(), { withCredentials: true })
105
+ const future = createFuture<{
106
+ type: 'rpc_stream'
107
+ stream: ReadableStream<ArrayBufferView>
108
+ }>()
109
+ const { readable, writable } = new TransformStream()
110
+ const writer = writable.getWriter()
111
+ source.addEventListener('open', () =>
112
+ future.resolve({ type: 'rpc_stream', stream: readable }),
113
+ )
114
+ source.addEventListener('close', () => writable.close())
115
+ source.addEventListener('error', (event) => {
116
+ const error = new Error('Stream error', { cause: event })
117
+ future.reject(error)
118
+ writable.abort(error)
119
+ })
120
+ source.addEventListener('message', (event) => {
121
+ try {
122
+ const buffer = this.decodeBase64(event.data)
123
+ writer.write(buffer)
124
+ } catch (cause) {
125
+ const error = new Error('Failed to decode stream message', { cause })
126
+ writable.abort(error)
127
+ source.close()
128
+ }
129
+ })
130
+ return future.promise
131
+ } else {
132
+ const response = await fetch(url.toString(), {
133
+ body,
134
+ method: 'POST',
135
+ headers: requestHeaders,
136
+ signal: options.signal,
137
+ credentials: 'include',
138
+ keepalive: true,
139
+ })
140
+
141
+ if (response.ok) {
142
+ const isBlob = !!response.headers.get(NEEMATA_BLOB_HEADER)
143
+ if (isBlob) {
144
+ const contentLength = response.headers.get('content-length')
145
+ const size =
146
+ (contentLength && Number.parseInt(contentLength, 10)) || undefined
147
+ const type =
148
+ response.headers.get('content-type') || 'application/octet-stream'
149
+ const disposition = response.headers.get('content-disposition')
150
+ let filename: string | undefined
151
+ if (disposition) {
152
+ const match = disposition.match(/filename="?([^"]+)"?/)
153
+ if (match) filename = match[1]
154
+ }
155
+ return {
156
+ type: 'blob' as const,
157
+ metadata: { type, size, filename },
158
+ source: response.body!,
159
+ }
160
+ } else {
161
+ return { type: 'rpc' as const, result: await response.bytes() }
162
+ }
163
+ } else {
164
+ try {
165
+ const buffer = await response.bytes()
166
+ const error = client.format.decode(buffer) as {
167
+ code?: string
168
+ message?: string
169
+ data?: unknown
170
+ }
171
+ throw new ProtocolError(
172
+ error.code || ErrorCode.ClientRequestError,
173
+ error.message || response.statusText,
174
+ error.data,
175
+ )
176
+ } catch (cause) {
177
+ if (cause instanceof ProtocolError) throw cause
178
+ // If decoding fails, throw generic error with status info
179
+ throw new ProtocolError(
180
+ ErrorCode.ClientRequestError,
181
+ `HTTP ${response.status}: ${response.statusText}`,
182
+ )
183
+ }
184
+ }
185
+ }
186
+ }
187
+ }
188
+
189
+ export type HttpTransportFactory = ClientTransportFactory<
190
+ ConnectionType.Unidirectional,
191
+ HttpClientTransportOptions,
192
+ HttpTransportClient
193
+ >
194
+
195
+ export const HttpTransportFactory: HttpTransportFactory = (params, options) => {
196
+ return new HttpTransportClient(params.format, params.protocol, options)
197
+ }