Skip to content

Commit 344a302

Browse files
committed
feat: 🎸 reduce size of client-side bundle
1 parent c4009d6 commit 344a302

File tree

19 files changed

+100
-63
lines changed

19 files changed

+100
-63
lines changed

src/__demos__/json-crdt-server/routes/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {LevelStore} from '../services/blocks/store/level/LevelStore';
1010
import {ClassicLevel} from 'classic-level';
1111
import {Store} from '../services/blocks/store/types';
1212
import type {RouteDeps} from './types';
13+
import {TypedRpcError} from '../../../common/rpc/caller/error/typed';
1314

1415
export const createRouter = (services: Services) => {
1516
const router = ObjectValue.create(system);
@@ -28,10 +29,10 @@ export const createCaller = (services: Services = new Services()) => {
2829
router,
2930
wrapInternalError: (error: unknown) => {
3031
if (error instanceof RpcValue) return error;
31-
if (error instanceof RpcError) return RpcError.value(error);
32+
if (error instanceof RpcError) return TypedRpcError.value(error);
3233
// tslint:disable-next-line:no-console
3334
console.error(error);
34-
return RpcError.valueFrom(error);
35+
return TypedRpcError.valueFrom(error);
3536
},
3637
});
3738
return {router, caller, services};

src/common/codec/binary/BinaryRpcMessageCodec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {RpcMessageFormat} from '../constants';
22
import {decode} from './decode';
3-
import * as msg from '../../messages';
3+
import type * as msg from '../../messages';
44
import type {Uint8ArrayCut} from '@jsonjoy.com/util/lib/buffers/Uint8ArrayCut';
55
import type {JsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/types';
66
import type {RpcMessageCodec} from '../types';

src/common/codec/compact/CompactRpcMessageCodec.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {RpcMessageFormat} from '../constants';
2-
import {RpcError, RpcErrorCodes} from '../../rpc/caller/error';
2+
import {RpcError, RpcErrorCodes} from '../../rpc/caller/error/RpcError';
33
import * as msg from '../../messages';
44
import {CompactMessageType} from './constants';
55
import {RpcValue} from '../../messages/Value';
6+
import {TypedRpcError} from '../../rpc/caller/error/typed';
67
import type {JsonEncoder} from '@jsonjoy.com/json-pack/lib/json/JsonEncoder';
78
import type {RpcMessageCodec} from '../types';
89
import type {JsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/types';
@@ -47,7 +48,7 @@ const fromJson = (arr: unknown | unknown[] | types.CompactMessage): msg.Reactive
4748
return new msg.NotificationMessage(arr[1], new RpcValue(arr[2], undefined));
4849
}
4950
}
50-
throw RpcError.value(RpcError.validation('Unknown message type'));
51+
throw TypedRpcError.value(RpcError.validation('Unknown message type'));
5152
};
5253

5354
const encodeCompactWithNameAndPayload = (
@@ -68,7 +69,10 @@ const encodeCompactWithNameAndPayload = (
6869
if (value.type) value.type.encoder(codec.format)(value.data, encoder);
6970
else encoder.writeAny(value.data);
7071
}
71-
} else if (typeof (encoder as any as JsonEncoder).writeStartArr === 'function' && typeof (encoder as any as JsonEncoder).writeArrSeparator === 'function') {
72+
} else if (
73+
typeof (encoder as any as JsonEncoder).writeStartArr === 'function' &&
74+
typeof (encoder as any as JsonEncoder).writeArrSeparator === 'function'
75+
) {
7276
const jsonEncoder = encoder as any as JsonEncoder;
7377
const value = msg.value;
7478
jsonEncoder.writeStartArr();
@@ -105,7 +109,10 @@ const encodeCompactWithPayload = (
105109
value.type.encoder(codec.format)(value.data, encoder);
106110
} else encoder.writeAny(value.data);
107111
}
108-
} else if (typeof (encoder as any as JsonEncoder).writeStartArr === 'function' && typeof (encoder as any as JsonEncoder).writeArrSeparator === 'function') {
112+
} else if (
113+
typeof (encoder as any as JsonEncoder).writeStartArr === 'function' &&
114+
typeof (encoder as any as JsonEncoder).writeArrSeparator === 'function'
115+
) {
109116
const jsonEncoder = encoder as any as JsonEncoder;
110117
const value = msg.value;
111118
jsonEncoder.writeStartArr();
@@ -140,7 +147,10 @@ export class CompactRpcMessageCodec implements RpcMessageCodec {
140147
if (value.type) value.type.encoder(codec.format)(value.data, encoder);
141148
else encoder.writeAny(value.data);
142149
}
143-
} else if (typeof (encoder as any as JsonEncoder).writeStartArr === 'function' && typeof (encoder as any as JsonEncoder).writeArrSeparator === 'function') {
150+
} else if (
151+
typeof (encoder as any as JsonEncoder).writeStartArr === 'function' &&
152+
typeof (encoder as any as JsonEncoder).writeArrSeparator === 'function'
153+
) {
144154
const jsonEncoder = encoder as any as JsonEncoder;
145155
const value = message.value;
146156
jsonEncoder.writeStartArr();
@@ -183,7 +193,10 @@ export class CompactRpcMessageCodec implements RpcMessageCodec {
183193
const length = batch.length;
184194
binaryEncoder.writeArrHdr(length);
185195
for (let i = 0; i < length; i++) this.encodeMessage(jsonCodec, batch[i]);
186-
} else if (typeof (encoder as any as JsonEncoder).writeStartArr === 'function' && typeof (encoder as any as JsonEncoder).writeArrSeparator === 'function') {
196+
} else if (
197+
typeof (encoder as any as JsonEncoder).writeStartArr === 'function' &&
198+
typeof (encoder as any as JsonEncoder).writeArrSeparator === 'function'
199+
) {
187200
const jsonEncoder = encoder as any as JsonEncoder;
188201
const length = batch.length;
189202
const last = length - 1;

src/common/codec/compact/__tests__/smoke-tests.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
ResponseUnsubscribeMessage,
1515
} from '../../../messages';
1616
import {RpcValue} from '../../../messages/Value';
17-
import {RpcError} from '../../../rpc/caller/error';
17+
import {TypedRpcError} from '../../../rpc/caller/error/typed';
1818

1919
const writer = new Writer(8 * Math.round(Math.random() * 100));
2020
const codecs = new Codecs(writer);
@@ -84,7 +84,7 @@ for (const jsonCodec of codecList) {
8484
});
8585

8686
test('Response Error typed', () => {
87-
const value = RpcError.internalErrorValue(null);
87+
const value = TypedRpcError.internalErrorValue(null);
8888
const message = new ResponseErrorMessage(123, value);
8989
const encoded = codec.encode(jsonCodec, [message]);
9090
const decoded1 = jsonCodec.decoder.read(encoded);

src/common/codec/json-rpc-2/JsonRpc2RpcMessageCodec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {EncodingFormat} from '@jsonjoy.com/json-pack/lib/constants';
22
import {TlvBinaryJsonEncoder} from '@jsonjoy.com/json-pack/lib/types';
33
import {JsonJsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/json';
44
import {RpcMessageFormat} from '../constants';
5-
import {RpcError} from '../../rpc/caller/error';
5+
import {RpcError} from '../../rpc/caller/error/RpcError';
66
import {RpcValue} from '../../messages/Value';
77
import * as msg from '../../messages';
88
import * as schema from './schema';

src/common/rpc/RpcMessageBatchProcessor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as msg from '../messages';
2-
import {RpcError, RpcErrorValue} from './caller';
2+
import {RpcErrorValue} from './caller';
33
import {validateId, validateMethod} from './validation';
4+
import {TypedRpcError} from './caller/error/typed';
45
import type {RpcCaller} from './caller/RpcCaller';
56

67
export type IncomingBatchMessage =
@@ -59,7 +60,7 @@ export class RpcMessageBatchProcessor<Ctx = unknown> {
5960
}
6061
return result;
6162
} catch (error) {
62-
const value = RpcError.internalErrorValue(error);
63+
const value = TypedRpcError.internalErrorValue(error);
6364
return [new msg.ResponseErrorMessage(-1, value)];
6465
}
6566
}

src/common/rpc/RpcMessageStreamProcessor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as msg from '../messages';
22
import {TimedQueue} from '../util/TimedQueue';
3-
import {RpcErrorCodes, RpcError} from './caller/error';
3+
import {RpcErrorCodes, RpcError} from './caller/error/RpcError';
44
import {RpcValue} from '../messages/Value';
55
import {subscribeCompleteObserver} from '../util/subscribeCompleteObserver';
66
import type {RpcCaller} from './caller/RpcCaller';
77
import type {Call, RpcApiMap} from './caller/types';
8+
import {TypedRpcError} from './caller/error/typed';
89

910
type Send = (messages: (msg.ReactiveRpcServerMessage | msg.NotificationMessage)[]) => void;
1011

@@ -130,7 +131,7 @@ export class RpcMessageStreamProcessor<Ctx = unknown> {
130131
this.send = <any>(() => {});
131132
for (const call of this.activeStreamCalls.values()) {
132133
try {
133-
call.req$.error(RpcError.valueFromCode(reason));
134+
call.req$.error(TypedRpcError.valueFromCode(reason));
134135
call.stop$.next(null);
135136
} catch (error) {
136137
// tslint:disable-next-line no-console
@@ -145,7 +146,7 @@ export class RpcMessageStreamProcessor<Ctx = unknown> {
145146
}
146147

147148
private sendError(id: number, code: RpcErrorCodes): void {
148-
const data = RpcError.valueFromCode(code);
149+
const data = TypedRpcError.valueFromCode(code);
149150
this.sendErrorMessage(id, data);
150151
}
151152

src/common/rpc/caller/ObjectValueCaller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RpcError} from './error';
1+
import {RpcError} from './error/RpcError';
22
import {RpcCaller, type RpcApiCallerOptions} from './RpcCaller';
33
import {type AbstractType, FunctionStreamingType, FunctionType} from 'json-joy/lib/json-type/type/classes';
44
import {printTree} from 'sonic-forest/lib/print/printTree';
@@ -90,7 +90,7 @@ export class ObjectValueCaller<V extends ObjectValue<ObjectType<any>>, Ctx = unk
9090
const error: any = validator(req);
9191
if (error) {
9292
const message = error.message + (Array.isArray(error?.path) ? ' Path: /' + error.path.join('/') : '');
93-
throw RpcError.value(RpcError.validation(message, error));
93+
throw RpcError.validation(message, error);
9494
}
9595
};
9696
method =

src/common/rpc/caller/RpcCaller.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {firstValueFrom, from, Observable, Subject} from 'rxjs';
22
import {catchError, finalize, first, map, mergeWith, share, switchMap, take, takeUntil, tap} from 'rxjs/operators';
3-
import {RpcError, RpcErrorCodes, RpcErrorValue} from './error';
3+
import {RpcError, RpcErrorCodes, RpcErrorValue} from './error/RpcError';
4+
import {TypedRpcError} from './error/typed';
45
import {RpcValue} from '../../messages/Value';
56
import {StaticRpcMethod} from '../methods/StaticRpcMethod';
67
import {BufferSubject} from '../../../util/rx/BufferSubject';
@@ -22,9 +23,9 @@ export interface RpcApiCallerOptions<Ctx = unknown> {
2223
wrapInternalError?: (error: unknown) => unknown;
2324
}
2425

25-
const INVALID_REQUEST_ERROR_VALUE = RpcError.value(RpcError.badRequest());
26+
const INVALID_REQUEST_ERROR_VALUE = TypedRpcError.value(RpcError.badRequest());
2627

27-
const defaultWrapInternalError = (error: unknown) => RpcError.valueFrom(error);
28+
const defaultWrapInternalError = (error: unknown) => TypedRpcError.valueFrom(error);
2829

2930
/**
3031
* Implements methods to call Reactive-RPC methods on the server.
@@ -50,7 +51,7 @@ export class RpcCaller<Ctx = unknown> {
5051

5152
public getMethodStrict(name: string): StaticRpcMethod<Ctx> | StreamingRpcMethod<Ctx> {
5253
const method = this.getMethod(name);
53-
if (!method) throw RpcError.valueFromCode(RpcErrorCodes.METHOD_NOT_FOUND);
54+
if (!method) throw TypedRpcError.valueFromCode(RpcErrorCodes.METHOD_NOT_FOUND);
5455
return method;
5556
}
5657

@@ -70,7 +71,7 @@ export class RpcCaller<Ctx = unknown> {
7071
}
7172

7273
protected wrapValidationError(error: unknown): RpcErrorValue {
73-
return RpcError.valueFrom(error, INVALID_REQUEST_ERROR_VALUE);
74+
return TypedRpcError.valueFrom(error, INVALID_REQUEST_ERROR_VALUE);
7475
}
7576

7677
/**
@@ -209,13 +210,13 @@ export class RpcCaller<Ctx = unknown> {
209210
error$.complete();
210211
}),
211212
catchError((error) => {
212-
throw RpcError.valueFrom(error);
213+
throw TypedRpcError.valueFrom(error);
213214
}),
214215
);
215216

216217
return {req$, reqUnsubscribe$, stop$, res$: $resWithErrorsFormatted.pipe(takeUntil(stop$))};
217218
} catch (error) {
218-
const errorFormatted = RpcError.valueFrom(error);
219+
const errorFormatted = TypedRpcError.valueFrom(error);
219220
req$.error(errorFormatted);
220221
const res$ = new Subject<RpcValue>();
221222
res$.error(errorFormatted);

src/common/rpc/caller/TypeRouterCaller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {RpcError} from './error';
1+
import {RpcError} from './error/RpcError';
22
import {RpcCaller, type RpcApiCallerOptions} from './RpcCaller';
33
import {type AbstractType, FunctionStreamingType, FunctionType} from 'json-joy/lib/json-type/type/classes';
44
import {StaticRpcMethod, type StaticRpcMethodOptions} from '../methods/StaticRpcMethod';
55
import {StreamingRpcMethod, type StreamingRpcMethodOptions} from '../methods/StreamingRpcMethod';
6+
import {TypedRpcError} from './error/typed';
67
import type {Schema, SchemaOf, TypeOf, TypeSystem} from 'json-joy/lib/json-type';
78
import type {TypeRouter} from 'json-joy/lib/json-type/system/TypeRouter';
89
import type {RpcValue} from '../../messages/Value';
@@ -44,7 +45,7 @@ export class TypeRouterCaller<Router extends TypeRouter<any>, Ctx = unknown> ext
4445
const error = validator(req);
4546
if (error) {
4647
const message = error.message + (Array.isArray(error?.path) ? ' Path: /' + error.path.join('/') : '');
47-
throw RpcError.value(RpcError.validation(message, error));
48+
throw TypedRpcError.value(RpcError.validation(message, error));
4849
}
4950
};
5051
method =

0 commit comments

Comments
 (0)