@arrirpc/client 0.80.1 → 0.80.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/dist/index.cjs +0 -159
- package/dist/index.d.cts +2 -59
- package/dist/index.d.mts +2 -59
- package/dist/index.d.ts +2 -59
- package/dist/index.mjs +1 -153
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -3,11 +3,6 @@
|
|
|
3
3
|
const schema = require('@arrirpc/schema');
|
|
4
4
|
const ofetch = require('ofetch');
|
|
5
5
|
const eventSourcePlus = require('event-source-plus');
|
|
6
|
-
const NodeWebsocket = require('ws');
|
|
7
|
-
|
|
8
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
9
|
-
|
|
10
|
-
const NodeWebsocket__default = /*#__PURE__*/_interopDefaultCompat(NodeWebsocket);
|
|
11
6
|
|
|
12
7
|
function isArriError(input) {
|
|
13
8
|
if (typeof input !== "object" || input === null) {
|
|
@@ -293,157 +288,6 @@ function arriSseRequest(opts, options) {
|
|
|
293
288
|
return controller;
|
|
294
289
|
}
|
|
295
290
|
|
|
296
|
-
function isBrowser() {
|
|
297
|
-
return typeof window !== "undefined";
|
|
298
|
-
}
|
|
299
|
-
function connectWebsocket(url, protocol) {
|
|
300
|
-
if (isBrowser()) {
|
|
301
|
-
return new WebSocket(url, protocol);
|
|
302
|
-
}
|
|
303
|
-
return new NodeWebsocket__default(url, {
|
|
304
|
-
protocol
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
async function arriWsRequest(opts, retryCount = 0) {
|
|
308
|
-
let url = opts.url.replace("http://", "ws://").replace("https://", "wss://");
|
|
309
|
-
const headers = await getHeaders(opts.headers);
|
|
310
|
-
if (headers) {
|
|
311
|
-
if (opts.clientVersion) headers["client-version"] = opts.clientVersion;
|
|
312
|
-
const queryParts = [];
|
|
313
|
-
for (const key of Object.keys(headers)) {
|
|
314
|
-
queryParts.push(`${key}=${headers[key]}`);
|
|
315
|
-
}
|
|
316
|
-
url += `?${queryParts.join("&")}`;
|
|
317
|
-
}
|
|
318
|
-
try {
|
|
319
|
-
const controller = new WsController({
|
|
320
|
-
url,
|
|
321
|
-
responseFromJson: opts.responseFromJson,
|
|
322
|
-
responseFromString: opts.responseFromString,
|
|
323
|
-
serializer: opts.serializer ?? ((_) => ""),
|
|
324
|
-
onOpen: opts.onOpen,
|
|
325
|
-
onClose: opts.onClose,
|
|
326
|
-
onMessage: opts.onMessage,
|
|
327
|
-
onErrorMessage: opts.onError,
|
|
328
|
-
onConnectionError: opts.onConnectionError
|
|
329
|
-
});
|
|
330
|
-
return controller;
|
|
331
|
-
} catch (err) {
|
|
332
|
-
console.error(err);
|
|
333
|
-
if (opts.onConnectionError) {
|
|
334
|
-
opts.onConnectionError(err);
|
|
335
|
-
}
|
|
336
|
-
return arriWsRequest(opts, retryCount + 1);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
class WsController {
|
|
340
|
-
url;
|
|
341
|
-
_ws;
|
|
342
|
-
_serializer;
|
|
343
|
-
_parser;
|
|
344
|
-
onMessage;
|
|
345
|
-
onErrorMessage;
|
|
346
|
-
onConnectionError;
|
|
347
|
-
onOpen;
|
|
348
|
-
onClose;
|
|
349
|
-
constructor(opts) {
|
|
350
|
-
this.url = opts.url;
|
|
351
|
-
this._serializer = opts.serializer;
|
|
352
|
-
this._parser = opts.responseFromString;
|
|
353
|
-
this.onOpen = opts.onOpen;
|
|
354
|
-
this.onClose = opts.onClose;
|
|
355
|
-
this.onErrorMessage = opts.onErrorMessage;
|
|
356
|
-
this.onConnectionError = opts.onConnectionError;
|
|
357
|
-
this.onMessage = opts.onMessage;
|
|
358
|
-
}
|
|
359
|
-
connect() {
|
|
360
|
-
this._ws = connectWebsocket(this.url);
|
|
361
|
-
this._ws.onopen = () => {
|
|
362
|
-
this.onOpen?.();
|
|
363
|
-
};
|
|
364
|
-
this._ws.onclose = () => {
|
|
365
|
-
this.onClose?.();
|
|
366
|
-
};
|
|
367
|
-
this._ws.onmessage = (event) => {
|
|
368
|
-
this._handleMessage(event);
|
|
369
|
-
};
|
|
370
|
-
this._ws.onerror = (event) => {
|
|
371
|
-
this.onConnectionError?.(
|
|
372
|
-
new ArriErrorInstance({
|
|
373
|
-
code: "errno" in event.error && typeof event.error.errno === "number" ? event.error.errno : 0,
|
|
374
|
-
message: event.error instanceof Error ? event.error.message : `Error connecting to ${this.url}`,
|
|
375
|
-
data: event.error,
|
|
376
|
-
stack: event.error instanceof Error ? event.error.stack : void 0
|
|
377
|
-
})
|
|
378
|
-
);
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
readyState() {
|
|
382
|
-
return this._ws?.readyState ?? -1;
|
|
383
|
-
}
|
|
384
|
-
send(message) {
|
|
385
|
-
const payload = this._serializer(message);
|
|
386
|
-
this._ws?.send(payload);
|
|
387
|
-
}
|
|
388
|
-
close() {
|
|
389
|
-
this._ws?.close();
|
|
390
|
-
}
|
|
391
|
-
_handleMessage(msg) {
|
|
392
|
-
if (typeof msg.data !== "string") {
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
const response = parsedWsResponse(msg.data);
|
|
396
|
-
switch (response.event) {
|
|
397
|
-
case "error": {
|
|
398
|
-
if (!this.onErrorMessage) {
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
const err = ArriErrorInstance.fromJson(response.data);
|
|
402
|
-
this.onErrorMessage(err);
|
|
403
|
-
break;
|
|
404
|
-
}
|
|
405
|
-
case "message": {
|
|
406
|
-
if (!this.onMessage) {
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
const msg2 = this._parser(response.data);
|
|
410
|
-
this.onMessage(msg2);
|
|
411
|
-
break;
|
|
412
|
-
}
|
|
413
|
-
default:
|
|
414
|
-
console.warn("Invalid response from server", msg);
|
|
415
|
-
break;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
function parsedWsResponse(input) {
|
|
420
|
-
const lines = input.split("\n");
|
|
421
|
-
let event = "unknown";
|
|
422
|
-
let data = "";
|
|
423
|
-
for (const line of lines) {
|
|
424
|
-
const trimmedLine = line.trim();
|
|
425
|
-
if (trimmedLine.startsWith("event: ")) {
|
|
426
|
-
switch (trimmedLine.substring(6).trim()) {
|
|
427
|
-
case "error":
|
|
428
|
-
event = "error";
|
|
429
|
-
break;
|
|
430
|
-
case "message":
|
|
431
|
-
event = "message";
|
|
432
|
-
break;
|
|
433
|
-
}
|
|
434
|
-
continue;
|
|
435
|
-
}
|
|
436
|
-
if (trimmedLine.startsWith("data: ")) {
|
|
437
|
-
data = trimmedLine.substring(5).trim();
|
|
438
|
-
continue;
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return {
|
|
442
|
-
event,
|
|
443
|
-
data
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
|
-
|
|
447
291
|
exports.createFetch = ofetch.createFetch;
|
|
448
292
|
exports.ofetch = ofetch.ofetch;
|
|
449
293
|
exports.ArriErrorInstance = ArriErrorInstance;
|
|
@@ -459,14 +303,11 @@ exports.UINT16_MAX = UINT16_MAX;
|
|
|
459
303
|
exports.UINT32_MAX = UINT32_MAX;
|
|
460
304
|
exports.UINT64_MAX = UINT64_MAX;
|
|
461
305
|
exports.UINT8_MAX = UINT8_MAX;
|
|
462
|
-
exports.WsController = WsController;
|
|
463
306
|
exports.arriRequest = arriRequest;
|
|
464
307
|
exports.arriSafeRequest = arriSafeRequest;
|
|
465
308
|
exports.arriSseRequest = arriSseRequest;
|
|
466
|
-
exports.arriWsRequest = arriWsRequest;
|
|
467
309
|
exports.isArriError = isArriError;
|
|
468
310
|
exports.isObject = isObject;
|
|
469
|
-
exports.parsedWsResponse = parsedWsResponse;
|
|
470
311
|
exports.serializeString = serializeString;
|
|
471
312
|
Object.prototype.hasOwnProperty.call(eventSourcePlus, '__proto__') &&
|
|
472
313
|
!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
|
package/dist/index.d.cts
CHANGED
|
@@ -129,62 +129,5 @@ interface SseOptions<TData> {
|
|
|
129
129
|
}
|
|
130
130
|
declare function arriSseRequest<TType, TParams extends Record<any, any> | undefined = undefined>(opts: ArriRequestConfig<TType, TParams>, options: SseOptions<TType>): EventSourceController;
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
serializer: (input: TParams) => string;
|
|
135
|
-
responseFromJson: (input: Record<string, unknown>) => TResponse;
|
|
136
|
-
responseFromString: (input: string) => TResponse;
|
|
137
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
138
|
-
onErrorMessage?: WsErrorHook;
|
|
139
|
-
onConnectionError?: WsErrorHook;
|
|
140
|
-
onOpen?: () => any;
|
|
141
|
-
onClose?: () => any;
|
|
142
|
-
}
|
|
143
|
-
interface WsOptions<TResponse> {
|
|
144
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
145
|
-
onError?: WsErrorHook;
|
|
146
|
-
onConnectionError?: WsErrorHook;
|
|
147
|
-
onOpen?: () => any;
|
|
148
|
-
onClose?: () => any;
|
|
149
|
-
}
|
|
150
|
-
interface ArriWsRequestOptions<TParams = any, TResponse = any> {
|
|
151
|
-
url: string;
|
|
152
|
-
headers?: EventSourcePlusOptions['headers'];
|
|
153
|
-
params?: TParams;
|
|
154
|
-
responseFromJson: (input: Record<string, unknown>) => TResponse;
|
|
155
|
-
responseFromString: (input: string) => TResponse;
|
|
156
|
-
serializer: (input: TParams) => string;
|
|
157
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
158
|
-
onError?: WsErrorHook;
|
|
159
|
-
onConnectionError?: WsErrorHook;
|
|
160
|
-
onOpen?: () => any;
|
|
161
|
-
onClose?: () => any;
|
|
162
|
-
clientVersion?: string;
|
|
163
|
-
}
|
|
164
|
-
declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): Promise<WsController<TParams, TResponse>>;
|
|
165
|
-
type WsErrorHook = (err: ArriErrorInstance) => void;
|
|
166
|
-
type WsMessageHook<TResponse> = (msg: TResponse) => any;
|
|
167
|
-
declare class WsController<TParams, TResponse> {
|
|
168
|
-
url: string;
|
|
169
|
-
private _ws?;
|
|
170
|
-
private readonly _serializer;
|
|
171
|
-
private readonly _parser;
|
|
172
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
173
|
-
onErrorMessage?: WsErrorHook;
|
|
174
|
-
onConnectionError?: WsErrorHook;
|
|
175
|
-
onOpen?: () => any;
|
|
176
|
-
onClose?: () => any;
|
|
177
|
-
constructor(opts: WsControllerOptions<TParams, TResponse>);
|
|
178
|
-
connect(): void;
|
|
179
|
-
readyState(): number;
|
|
180
|
-
send(message: TParams): void;
|
|
181
|
-
close(): void;
|
|
182
|
-
private _handleMessage;
|
|
183
|
-
}
|
|
184
|
-
declare function parsedWsResponse(input: string): {
|
|
185
|
-
event: 'message' | 'error' | 'unknown';
|
|
186
|
-
data: string;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
export { ArriErrorInstance, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN, INT8_MAX, INT8_MIN, UINT16_MAX, UINT32_MAX, UINT64_MAX, UINT8_MAX, WsController, arriRequest, arriSafeRequest, arriSseRequest, arriWsRequest, isArriError, isObject, parsedWsResponse, serializeString };
|
|
190
|
-
export type { ArriEnumValidator, ArriError, ArriModelValidator, ArriRequestConfig, ArriRequestOptions, SafeResponse, SseEvent, SseOptions, WsOptions };
|
|
132
|
+
export { ArriErrorInstance, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN, INT8_MAX, INT8_MIN, UINT16_MAX, UINT32_MAX, UINT64_MAX, UINT8_MAX, arriRequest, arriSafeRequest, arriSseRequest, isArriError, isObject, serializeString };
|
|
133
|
+
export type { ArriEnumValidator, ArriError, ArriModelValidator, ArriRequestConfig, ArriRequestOptions, SafeResponse, SseEvent, SseOptions };
|
package/dist/index.d.mts
CHANGED
|
@@ -129,62 +129,5 @@ interface SseOptions<TData> {
|
|
|
129
129
|
}
|
|
130
130
|
declare function arriSseRequest<TType, TParams extends Record<any, any> | undefined = undefined>(opts: ArriRequestConfig<TType, TParams>, options: SseOptions<TType>): EventSourceController;
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
serializer: (input: TParams) => string;
|
|
135
|
-
responseFromJson: (input: Record<string, unknown>) => TResponse;
|
|
136
|
-
responseFromString: (input: string) => TResponse;
|
|
137
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
138
|
-
onErrorMessage?: WsErrorHook;
|
|
139
|
-
onConnectionError?: WsErrorHook;
|
|
140
|
-
onOpen?: () => any;
|
|
141
|
-
onClose?: () => any;
|
|
142
|
-
}
|
|
143
|
-
interface WsOptions<TResponse> {
|
|
144
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
145
|
-
onError?: WsErrorHook;
|
|
146
|
-
onConnectionError?: WsErrorHook;
|
|
147
|
-
onOpen?: () => any;
|
|
148
|
-
onClose?: () => any;
|
|
149
|
-
}
|
|
150
|
-
interface ArriWsRequestOptions<TParams = any, TResponse = any> {
|
|
151
|
-
url: string;
|
|
152
|
-
headers?: EventSourcePlusOptions['headers'];
|
|
153
|
-
params?: TParams;
|
|
154
|
-
responseFromJson: (input: Record<string, unknown>) => TResponse;
|
|
155
|
-
responseFromString: (input: string) => TResponse;
|
|
156
|
-
serializer: (input: TParams) => string;
|
|
157
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
158
|
-
onError?: WsErrorHook;
|
|
159
|
-
onConnectionError?: WsErrorHook;
|
|
160
|
-
onOpen?: () => any;
|
|
161
|
-
onClose?: () => any;
|
|
162
|
-
clientVersion?: string;
|
|
163
|
-
}
|
|
164
|
-
declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): Promise<WsController<TParams, TResponse>>;
|
|
165
|
-
type WsErrorHook = (err: ArriErrorInstance) => void;
|
|
166
|
-
type WsMessageHook<TResponse> = (msg: TResponse) => any;
|
|
167
|
-
declare class WsController<TParams, TResponse> {
|
|
168
|
-
url: string;
|
|
169
|
-
private _ws?;
|
|
170
|
-
private readonly _serializer;
|
|
171
|
-
private readonly _parser;
|
|
172
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
173
|
-
onErrorMessage?: WsErrorHook;
|
|
174
|
-
onConnectionError?: WsErrorHook;
|
|
175
|
-
onOpen?: () => any;
|
|
176
|
-
onClose?: () => any;
|
|
177
|
-
constructor(opts: WsControllerOptions<TParams, TResponse>);
|
|
178
|
-
connect(): void;
|
|
179
|
-
readyState(): number;
|
|
180
|
-
send(message: TParams): void;
|
|
181
|
-
close(): void;
|
|
182
|
-
private _handleMessage;
|
|
183
|
-
}
|
|
184
|
-
declare function parsedWsResponse(input: string): {
|
|
185
|
-
event: 'message' | 'error' | 'unknown';
|
|
186
|
-
data: string;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
export { ArriErrorInstance, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN, INT8_MAX, INT8_MIN, UINT16_MAX, UINT32_MAX, UINT64_MAX, UINT8_MAX, WsController, arriRequest, arriSafeRequest, arriSseRequest, arriWsRequest, isArriError, isObject, parsedWsResponse, serializeString };
|
|
190
|
-
export type { ArriEnumValidator, ArriError, ArriModelValidator, ArriRequestConfig, ArriRequestOptions, SafeResponse, SseEvent, SseOptions, WsOptions };
|
|
132
|
+
export { ArriErrorInstance, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN, INT8_MAX, INT8_MIN, UINT16_MAX, UINT32_MAX, UINT64_MAX, UINT8_MAX, arriRequest, arriSafeRequest, arriSseRequest, isArriError, isObject, serializeString };
|
|
133
|
+
export type { ArriEnumValidator, ArriError, ArriModelValidator, ArriRequestConfig, ArriRequestOptions, SafeResponse, SseEvent, SseOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -129,62 +129,5 @@ interface SseOptions<TData> {
|
|
|
129
129
|
}
|
|
130
130
|
declare function arriSseRequest<TType, TParams extends Record<any, any> | undefined = undefined>(opts: ArriRequestConfig<TType, TParams>, options: SseOptions<TType>): EventSourceController;
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
serializer: (input: TParams) => string;
|
|
135
|
-
responseFromJson: (input: Record<string, unknown>) => TResponse;
|
|
136
|
-
responseFromString: (input: string) => TResponse;
|
|
137
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
138
|
-
onErrorMessage?: WsErrorHook;
|
|
139
|
-
onConnectionError?: WsErrorHook;
|
|
140
|
-
onOpen?: () => any;
|
|
141
|
-
onClose?: () => any;
|
|
142
|
-
}
|
|
143
|
-
interface WsOptions<TResponse> {
|
|
144
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
145
|
-
onError?: WsErrorHook;
|
|
146
|
-
onConnectionError?: WsErrorHook;
|
|
147
|
-
onOpen?: () => any;
|
|
148
|
-
onClose?: () => any;
|
|
149
|
-
}
|
|
150
|
-
interface ArriWsRequestOptions<TParams = any, TResponse = any> {
|
|
151
|
-
url: string;
|
|
152
|
-
headers?: EventSourcePlusOptions['headers'];
|
|
153
|
-
params?: TParams;
|
|
154
|
-
responseFromJson: (input: Record<string, unknown>) => TResponse;
|
|
155
|
-
responseFromString: (input: string) => TResponse;
|
|
156
|
-
serializer: (input: TParams) => string;
|
|
157
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
158
|
-
onError?: WsErrorHook;
|
|
159
|
-
onConnectionError?: WsErrorHook;
|
|
160
|
-
onOpen?: () => any;
|
|
161
|
-
onClose?: () => any;
|
|
162
|
-
clientVersion?: string;
|
|
163
|
-
}
|
|
164
|
-
declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): Promise<WsController<TParams, TResponse>>;
|
|
165
|
-
type WsErrorHook = (err: ArriErrorInstance) => void;
|
|
166
|
-
type WsMessageHook<TResponse> = (msg: TResponse) => any;
|
|
167
|
-
declare class WsController<TParams, TResponse> {
|
|
168
|
-
url: string;
|
|
169
|
-
private _ws?;
|
|
170
|
-
private readonly _serializer;
|
|
171
|
-
private readonly _parser;
|
|
172
|
-
onMessage?: WsMessageHook<TResponse>;
|
|
173
|
-
onErrorMessage?: WsErrorHook;
|
|
174
|
-
onConnectionError?: WsErrorHook;
|
|
175
|
-
onOpen?: () => any;
|
|
176
|
-
onClose?: () => any;
|
|
177
|
-
constructor(opts: WsControllerOptions<TParams, TResponse>);
|
|
178
|
-
connect(): void;
|
|
179
|
-
readyState(): number;
|
|
180
|
-
send(message: TParams): void;
|
|
181
|
-
close(): void;
|
|
182
|
-
private _handleMessage;
|
|
183
|
-
}
|
|
184
|
-
declare function parsedWsResponse(input: string): {
|
|
185
|
-
event: 'message' | 'error' | 'unknown';
|
|
186
|
-
data: string;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
export { ArriErrorInstance, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN, INT8_MAX, INT8_MIN, UINT16_MAX, UINT32_MAX, UINT64_MAX, UINT8_MAX, WsController, arriRequest, arriSafeRequest, arriSseRequest, arriWsRequest, isArriError, isObject, parsedWsResponse, serializeString };
|
|
190
|
-
export type { ArriEnumValidator, ArriError, ArriModelValidator, ArriRequestConfig, ArriRequestOptions, SafeResponse, SseEvent, SseOptions, WsOptions };
|
|
132
|
+
export { ArriErrorInstance, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN, INT8_MAX, INT8_MIN, UINT16_MAX, UINT32_MAX, UINT64_MAX, UINT8_MAX, arriRequest, arriSafeRequest, arriSseRequest, isArriError, isObject, serializeString };
|
|
133
|
+
export type { ArriEnumValidator, ArriError, ArriModelValidator, ArriRequestConfig, ArriRequestOptions, SafeResponse, SseEvent, SseOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,6 @@ import { ofetch, FetchError } from 'ofetch';
|
|
|
3
3
|
export { createFetch, ofetch } from 'ofetch';
|
|
4
4
|
import { EventSourcePlus } from 'event-source-plus';
|
|
5
5
|
export * from 'event-source-plus';
|
|
6
|
-
import NodeWebsocket from 'ws';
|
|
7
6
|
|
|
8
7
|
function isArriError(input) {
|
|
9
8
|
if (typeof input !== "object" || input === null) {
|
|
@@ -289,155 +288,4 @@ function arriSseRequest(opts, options) {
|
|
|
289
288
|
return controller;
|
|
290
289
|
}
|
|
291
290
|
|
|
292
|
-
|
|
293
|
-
return typeof window !== "undefined";
|
|
294
|
-
}
|
|
295
|
-
function connectWebsocket(url, protocol) {
|
|
296
|
-
if (isBrowser()) {
|
|
297
|
-
return new WebSocket(url, protocol);
|
|
298
|
-
}
|
|
299
|
-
return new NodeWebsocket(url, {
|
|
300
|
-
protocol
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
async function arriWsRequest(opts, retryCount = 0) {
|
|
304
|
-
let url = opts.url.replace("http://", "ws://").replace("https://", "wss://");
|
|
305
|
-
const headers = await getHeaders(opts.headers);
|
|
306
|
-
if (headers) {
|
|
307
|
-
if (opts.clientVersion) headers["client-version"] = opts.clientVersion;
|
|
308
|
-
const queryParts = [];
|
|
309
|
-
for (const key of Object.keys(headers)) {
|
|
310
|
-
queryParts.push(`${key}=${headers[key]}`);
|
|
311
|
-
}
|
|
312
|
-
url += `?${queryParts.join("&")}`;
|
|
313
|
-
}
|
|
314
|
-
try {
|
|
315
|
-
const controller = new WsController({
|
|
316
|
-
url,
|
|
317
|
-
responseFromJson: opts.responseFromJson,
|
|
318
|
-
responseFromString: opts.responseFromString,
|
|
319
|
-
serializer: opts.serializer ?? ((_) => ""),
|
|
320
|
-
onOpen: opts.onOpen,
|
|
321
|
-
onClose: opts.onClose,
|
|
322
|
-
onMessage: opts.onMessage,
|
|
323
|
-
onErrorMessage: opts.onError,
|
|
324
|
-
onConnectionError: opts.onConnectionError
|
|
325
|
-
});
|
|
326
|
-
return controller;
|
|
327
|
-
} catch (err) {
|
|
328
|
-
console.error(err);
|
|
329
|
-
if (opts.onConnectionError) {
|
|
330
|
-
opts.onConnectionError(err);
|
|
331
|
-
}
|
|
332
|
-
return arriWsRequest(opts, retryCount + 1);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
class WsController {
|
|
336
|
-
url;
|
|
337
|
-
_ws;
|
|
338
|
-
_serializer;
|
|
339
|
-
_parser;
|
|
340
|
-
onMessage;
|
|
341
|
-
onErrorMessage;
|
|
342
|
-
onConnectionError;
|
|
343
|
-
onOpen;
|
|
344
|
-
onClose;
|
|
345
|
-
constructor(opts) {
|
|
346
|
-
this.url = opts.url;
|
|
347
|
-
this._serializer = opts.serializer;
|
|
348
|
-
this._parser = opts.responseFromString;
|
|
349
|
-
this.onOpen = opts.onOpen;
|
|
350
|
-
this.onClose = opts.onClose;
|
|
351
|
-
this.onErrorMessage = opts.onErrorMessage;
|
|
352
|
-
this.onConnectionError = opts.onConnectionError;
|
|
353
|
-
this.onMessage = opts.onMessage;
|
|
354
|
-
}
|
|
355
|
-
connect() {
|
|
356
|
-
this._ws = connectWebsocket(this.url);
|
|
357
|
-
this._ws.onopen = () => {
|
|
358
|
-
this.onOpen?.();
|
|
359
|
-
};
|
|
360
|
-
this._ws.onclose = () => {
|
|
361
|
-
this.onClose?.();
|
|
362
|
-
};
|
|
363
|
-
this._ws.onmessage = (event) => {
|
|
364
|
-
this._handleMessage(event);
|
|
365
|
-
};
|
|
366
|
-
this._ws.onerror = (event) => {
|
|
367
|
-
this.onConnectionError?.(
|
|
368
|
-
new ArriErrorInstance({
|
|
369
|
-
code: "errno" in event.error && typeof event.error.errno === "number" ? event.error.errno : 0,
|
|
370
|
-
message: event.error instanceof Error ? event.error.message : `Error connecting to ${this.url}`,
|
|
371
|
-
data: event.error,
|
|
372
|
-
stack: event.error instanceof Error ? event.error.stack : void 0
|
|
373
|
-
})
|
|
374
|
-
);
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
readyState() {
|
|
378
|
-
return this._ws?.readyState ?? -1;
|
|
379
|
-
}
|
|
380
|
-
send(message) {
|
|
381
|
-
const payload = this._serializer(message);
|
|
382
|
-
this._ws?.send(payload);
|
|
383
|
-
}
|
|
384
|
-
close() {
|
|
385
|
-
this._ws?.close();
|
|
386
|
-
}
|
|
387
|
-
_handleMessage(msg) {
|
|
388
|
-
if (typeof msg.data !== "string") {
|
|
389
|
-
return;
|
|
390
|
-
}
|
|
391
|
-
const response = parsedWsResponse(msg.data);
|
|
392
|
-
switch (response.event) {
|
|
393
|
-
case "error": {
|
|
394
|
-
if (!this.onErrorMessage) {
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
const err = ArriErrorInstance.fromJson(response.data);
|
|
398
|
-
this.onErrorMessage(err);
|
|
399
|
-
break;
|
|
400
|
-
}
|
|
401
|
-
case "message": {
|
|
402
|
-
if (!this.onMessage) {
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
const msg2 = this._parser(response.data);
|
|
406
|
-
this.onMessage(msg2);
|
|
407
|
-
break;
|
|
408
|
-
}
|
|
409
|
-
default:
|
|
410
|
-
console.warn("Invalid response from server", msg);
|
|
411
|
-
break;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
function parsedWsResponse(input) {
|
|
416
|
-
const lines = input.split("\n");
|
|
417
|
-
let event = "unknown";
|
|
418
|
-
let data = "";
|
|
419
|
-
for (const line of lines) {
|
|
420
|
-
const trimmedLine = line.trim();
|
|
421
|
-
if (trimmedLine.startsWith("event: ")) {
|
|
422
|
-
switch (trimmedLine.substring(6).trim()) {
|
|
423
|
-
case "error":
|
|
424
|
-
event = "error";
|
|
425
|
-
break;
|
|
426
|
-
case "message":
|
|
427
|
-
event = "message";
|
|
428
|
-
break;
|
|
429
|
-
}
|
|
430
|
-
continue;
|
|
431
|
-
}
|
|
432
|
-
if (trimmedLine.startsWith("data: ")) {
|
|
433
|
-
data = trimmedLine.substring(5).trim();
|
|
434
|
-
continue;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return {
|
|
438
|
-
event,
|
|
439
|
-
data
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
export { ArriErrorInstance, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN, INT8_MAX, INT8_MIN, UINT16_MAX, UINT32_MAX, UINT64_MAX, UINT8_MAX, WsController, arriRequest, arriSafeRequest, arriSseRequest, arriWsRequest, isArriError, isObject, parsedWsResponse, serializeString };
|
|
291
|
+
export { ArriErrorInstance, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN, INT8_MAX, INT8_MIN, UINT16_MAX, UINT32_MAX, UINT64_MAX, UINT8_MAX, arriRequest, arriSafeRequest, arriSseRequest, isArriError, isObject, serializeString };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arrirpc/client",
|
|
3
|
-
"version": "0.80.
|
|
3
|
+
"version": "0.80.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"event-source-plus": "^0.1.11",
|
|
26
26
|
"ofetch": "^1.4.1",
|
|
27
27
|
"ws": "^8.18.2",
|
|
28
|
-
"@arrirpc/schema": "0.80.
|
|
28
|
+
"@arrirpc/schema": "0.80.2"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/ws": "^8.18.1"
|