@celljs/rpc 4.5.1 → 4.6.0
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 -4
- package/esm/common/annotation/autorpc.d.ts +1 -0
- package/esm/common/annotation/autorpc.js +3 -2
- package/esm/common/annotation/autorpc.js.map +1 -1
- package/esm/common/annotation/detached/autorpc.js +2 -2
- package/esm/common/annotation/detached/autorpc.js.map +1 -1
- package/esm/common/module.js +1 -1
- package/esm/common/module.js.map +1 -1
- package/esm/common/transport/websocket-transport.js +27 -11
- package/esm/common/transport/websocket-transport.js.map +1 -1
- package/esm/common/utils/converter-util.d.ts +2 -2
- package/esm/common/utils/converter-util.js +2 -6
- package/esm/common/utils/converter-util.js.map +1 -1
- package/esm/common/utils/rpc-util.d.ts +0 -2
- package/esm/common/utils/rpc-util.js +1 -7
- package/esm/common/utils/rpc-util.js.map +1 -1
- package/esm/node/server/rpc-invoker.d.ts +2 -1
- package/esm/node/server/rpc-invoker.js +7 -3
- package/esm/node/server/rpc-invoker.js.map +1 -1
- package/esm/node/test/websocket-fixture.js +0 -2
- package/esm/node/test/websocket-fixture.js.map +1 -1
- package/esm/node/websocket/websocket-connection.d.ts +2 -1
- package/esm/node/websocket/websocket-connection.js +2 -1
- package/esm/node/websocket/websocket-connection.js.map +1 -1
- package/lib/common/annotation/autorpc.d.ts +1 -0
- package/lib/common/annotation/autorpc.d.ts.map +1 -1
- package/lib/common/annotation/autorpc.js +3 -2
- package/lib/common/annotation/autorpc.js.map +1 -1
- package/lib/common/annotation/detached/autorpc.d.ts.map +1 -1
- package/lib/common/annotation/detached/autorpc.js +1 -1
- package/lib/common/annotation/detached/autorpc.js.map +1 -1
- package/lib/common/module.js +1 -1
- package/lib/common/module.js.map +1 -1
- package/lib/common/transport/websocket-transport.d.ts.map +1 -1
- package/lib/common/transport/websocket-transport.js +27 -11
- package/lib/common/transport/websocket-transport.js.map +1 -1
- package/lib/common/utils/converter-util.d.ts +2 -2
- package/lib/common/utils/converter-util.d.ts.map +1 -1
- package/lib/common/utils/converter-util.js +2 -6
- package/lib/common/utils/converter-util.js.map +1 -1
- package/lib/common/utils/rpc-util.d.ts +0 -2
- package/lib/common/utils/rpc-util.d.ts.map +1 -1
- package/lib/common/utils/rpc-util.js +0 -6
- package/lib/common/utils/rpc-util.js.map +1 -1
- package/lib/node/rpc-http.spec.js +0 -2
- package/lib/node/rpc-http.spec.js.map +1 -1
- package/lib/node/rpc-websocket.spec.d.ts.map +1 -1
- package/lib/node/rpc-websocket.spec.js +9 -6
- package/lib/node/rpc-websocket.spec.js.map +1 -1
- package/lib/node/server/rpc-invoker.d.ts +2 -1
- package/lib/node/server/rpc-invoker.d.ts.map +1 -1
- package/lib/node/server/rpc-invoker.js +6 -2
- package/lib/node/server/rpc-invoker.js.map +1 -1
- package/lib/node/test/websocket-fixture.d.ts.map +1 -1
- package/lib/node/test/websocket-fixture.js +0 -2
- package/lib/node/test/websocket-fixture.js.map +1 -1
- package/lib/node/websocket/websocket-connection.d.ts +2 -1
- package/lib/node/websocket/websocket-connection.d.ts.map +1 -1
- package/lib/node/websocket/websocket-connection.js +4 -3
- package/lib/node/websocket/websocket-connection.js.map +1 -1
- package/lib/package.spec.js +19 -2
- package/lib/package.spec.js.map +1 -1
- package/package.json +4 -4
- package/src/common/annotation/autorpc.ts +3 -2
- package/src/common/annotation/detached/autorpc.ts +2 -2
- package/src/common/module.ts +2 -2
- package/src/common/transport/websocket-transport.ts +28 -11
- package/src/common/utils/converter-util.ts +4 -7
- package/src/common/utils/rpc-util.ts +2 -8
- package/src/node/rpc-http.spec.ts +1 -3
- package/src/node/rpc-websocket.spec.ts +11 -7
- package/src/node/server/rpc-invoker.ts +7 -4
- package/src/node/test/websocket-fixture.ts +0 -2
- package/src/node/websocket/websocket-connection.ts +4 -2
- package/src/package.spec.ts +18 -3
|
@@ -100,10 +100,31 @@ export class WebSocketTransport implements RpcTransport {
|
|
|
100
100
|
const url = await this.options.url();
|
|
101
101
|
return new Promise<WebSocket | undefined>(resolve => {
|
|
102
102
|
let opened = false;
|
|
103
|
+
let failed = false;
|
|
103
104
|
const socket = new WebSocket(url);
|
|
104
|
-
//
|
|
105
|
-
const
|
|
105
|
+
// No WebSocket endpoint (yet): HTTP for a while, then try again.
|
|
106
|
+
const fail = () => {
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
if (failed) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
failed = true;
|
|
112
|
+
this.retryAt = Date.now() + this.delay;
|
|
113
|
+
this.delay = Math.min(this.delay * 2, WebSocketTransport.MAX_RECONNECT_DELAY);
|
|
114
|
+
resolve(undefined);
|
|
115
|
+
this.scheduleReconnect();
|
|
116
|
+
};
|
|
117
|
+
// A server that never answers the upgrade (Bun < 1.4 behind node:http) must not hold the calls;
|
|
118
|
+
// nor a client whose close() of a connecting socket does not fire close (the WebSocket of Node.js 22).
|
|
119
|
+
const timer = setTimeout(() => {
|
|
120
|
+
socket.close();
|
|
121
|
+
fail();
|
|
122
|
+
}, this.options.connectTimeout ?? 5000);
|
|
106
123
|
socket.onopen = () => {
|
|
124
|
+
if (failed) {
|
|
125
|
+
socket.close(1000);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
107
128
|
clearTimeout(timer);
|
|
108
129
|
opened = true;
|
|
109
130
|
this.socket = socket;
|
|
@@ -113,16 +134,12 @@ export class WebSocketTransport implements RpcTransport {
|
|
|
113
134
|
socket.onmessage = event => this.receive(socket, event.data);
|
|
114
135
|
socket.onerror = () => undefined;
|
|
115
136
|
socket.onclose = () => {
|
|
116
|
-
clearTimeout(timer);
|
|
117
137
|
if (!opened) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
this.delay = Math.min(this.delay * 2, WebSocketTransport.MAX_RECONNECT_DELAY);
|
|
121
|
-
resolve(undefined);
|
|
122
|
-
} else {
|
|
123
|
-
this.socket = undefined;
|
|
124
|
-
this.failPending();
|
|
138
|
+
fail();
|
|
139
|
+
return;
|
|
125
140
|
}
|
|
141
|
+
this.socket = undefined;
|
|
142
|
+
this.failPending();
|
|
126
143
|
this.scheduleReconnect();
|
|
127
144
|
};
|
|
128
145
|
});
|
|
@@ -168,4 +185,4 @@ export class WebSocketTransport implements RpcTransport {
|
|
|
168
185
|
});
|
|
169
186
|
}
|
|
170
187
|
}
|
|
171
|
-
}
|
|
188
|
+
}
|
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Container } from '@celljs/core';
|
|
2
2
|
import { ErrorConverter, GlobalConverter } from '../converter';
|
|
3
3
|
import { RpcUtil } from './rpc-util';
|
|
4
4
|
|
|
5
5
|
export namespace ConverterUtil {
|
|
6
6
|
|
|
7
|
-
export function getGlobalErrorConverters(container: Container
|
|
7
|
+
export function getGlobalErrorConverters(container: Container): ErrorConverter[] {
|
|
8
8
|
const query = { name: GlobalConverter };
|
|
9
9
|
return container.isBound(ErrorConverter, query) ? container.getAll<ErrorConverter>(ErrorConverter, query) : [];
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export function getErrorConverters(serviceIdentifier: any, container
|
|
12
|
+
export function getErrorConverters(serviceIdentifier: any, container: Container): ErrorConverter | undefined {
|
|
13
13
|
const query = { name: RpcUtil.toName(serviceIdentifier) };
|
|
14
|
-
|
|
15
|
-
return container.isBound(ErrorConverter, query) ? container.get<ErrorConverter>(ErrorConverter, query) : undefined;
|
|
16
|
-
}
|
|
17
|
-
return ContainerUtil.isBound(ErrorConverter, query) ? ContainerUtil.get<ErrorConverter>(ErrorConverter, query) : undefined;
|
|
14
|
+
return container.isBound(ErrorConverter, query) ? container.get<ErrorConverter>(ErrorConverter, query) : undefined;
|
|
18
15
|
}
|
|
19
16
|
}
|
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
import { ComponentId,
|
|
2
|
-
import { ID_KEY, RPC } from '../annotation/autorpc';
|
|
1
|
+
import { ComponentId, UrlUtil } from '@celljs/core';
|
|
3
2
|
import { HttpTransport, HttpTransportOptions } from '../transport/http-transport';
|
|
4
3
|
import { RpcProxy } from '../proxy/rpc-proxy';
|
|
5
4
|
|
|
6
5
|
export namespace RpcUtil {
|
|
7
6
|
|
|
8
|
-
/** The proxy of a service from the application's container (as `@Autorpc(id)` injects it). */
|
|
9
|
-
export function get<T extends object>(id: ComponentId<T>): T {
|
|
10
|
-
return ContainerUtil.get(RPC, { tags: { [ID_KEY]: id } });
|
|
11
|
-
}
|
|
12
|
-
|
|
13
7
|
/** A proxy of the service `id` at `endpoint` (the URL of `rpc.path`), over HTTP, without the container. */
|
|
14
8
|
export function create<T extends object>(endpoint: string, id: ComponentId<T>, options: Partial<HttpTransportOptions> = {}): T {
|
|
15
9
|
const transport = new HttpTransport({
|
|
@@ -32,4 +26,4 @@ export namespace RpcUtil {
|
|
|
32
26
|
export function toName(id: ComponentId): string | symbol {
|
|
33
27
|
return typeof id === 'function' ? id.name : (id as string | symbol);
|
|
34
28
|
}
|
|
35
|
-
}
|
|
29
|
+
}
|
|
@@ -11,7 +11,6 @@ import webNodeModule from '@celljs/web/lib/node/module';
|
|
|
11
11
|
import rpcCommonModule from '../common/module';
|
|
12
12
|
import rpcNodeModule from './module';
|
|
13
13
|
import { Container } from '@celljs/core/lib/common/container/container';
|
|
14
|
-
import { ContainerProvider } from '@celljs/core/lib/common/container/container-provider';
|
|
15
14
|
import { autoBind } from '@celljs/core/lib/common/container/auto-bind';
|
|
16
15
|
import { currentThis } from '@celljs/core/lib/common/utils';
|
|
17
16
|
import { Application } from '@celljs/core/lib/common/application/application-protocol';
|
|
@@ -57,7 +56,6 @@ beforeAll(async () => {
|
|
|
57
56
|
expect(WelcomeServerImpl).toBeDefined();
|
|
58
57
|
currentThis.cellProps = structuredClone(CONFIG);
|
|
59
58
|
const container = new Container(coreCommonStatic, coreNodeStatic, httpCommonModule, webCommonModule, webNodeModule, rpcCommonModule, rpcNodeModule, autoBind());
|
|
60
|
-
ContainerProvider.set(container);
|
|
61
59
|
await container.get<Application>(Application).start();
|
|
62
60
|
server = HttpServerUtil.createHttpServer(HttpServerUtil.createHandler(container));
|
|
63
61
|
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
@@ -123,4 +121,4 @@ describe('rpc over HTTP', () => {
|
|
|
123
121
|
expect(error).toBeInstanceOf(RpcError);
|
|
124
122
|
expect(error.status).toBe(404);
|
|
125
123
|
});
|
|
126
|
-
});
|
|
124
|
+
});
|
|
@@ -7,12 +7,11 @@ import coreNodeStatic from '@celljs/core/lib/node/static-module';
|
|
|
7
7
|
import httpCommonModule from '@celljs/http/lib/common/module';
|
|
8
8
|
import webCommonModule from '@celljs/web/lib/common/module';
|
|
9
9
|
import { Container } from '@celljs/core/lib/common/container/container';
|
|
10
|
-
import { ContainerProvider } from '@celljs/core/lib/common/container/container-provider';
|
|
11
10
|
import { autoBind } from '@celljs/core/lib/common/container/auto-bind';
|
|
12
11
|
import { currentThis } from '@celljs/core/lib/common/utils';
|
|
13
12
|
import { Application } from '@celljs/core/lib/common/application/application-protocol';
|
|
14
13
|
import rpcCommonModule from '../common/module';
|
|
15
|
-
import { HttpTransport, JsonRpc, RpcClient, RpcError, RpcProxy,
|
|
14
|
+
import { HttpTransport, ID_KEY, JsonRpc, RPC, RpcClient, RpcError, RpcProxy, WebSocketTransport } from '../common';
|
|
16
15
|
// Not ./websocket: its components would join the client container of this file.
|
|
17
16
|
import { WebSocketConnection } from './websocket/websocket-connection';
|
|
18
17
|
import { ChatClient, ChatServer, NotYours } from './test/websocket-fixture-protocol';
|
|
@@ -89,19 +88,24 @@ beforeAll(async () => {
|
|
|
89
88
|
rpc: { path: 'api', transport: 'websocket', defaultEndpoint: `${base}{rpcPath}/{serviceIdentifier}`, websocket: { endpoint: wsUrl, reconnectDelay: 100 } },
|
|
90
89
|
};
|
|
91
90
|
container = new Container(coreCommonStatic, coreNodeStatic, httpCommonModule, webCommonModule, rpcCommonModule, autoBind());
|
|
92
|
-
ContainerProvider.set(container);
|
|
93
91
|
await container.get<Application>(Application).start();
|
|
94
92
|
}, 30000);
|
|
95
93
|
|
|
94
|
+
/** The proxy `@Autorpc(id)` injects. */
|
|
95
|
+
function proxyOf<T>(id: symbol): T {
|
|
96
|
+
return container.get<T>(RPC, { tags: { [ID_KEY]: id } });
|
|
97
|
+
}
|
|
98
|
+
|
|
96
99
|
afterAll(async () => {
|
|
97
100
|
await container?.get<Application>(Application).stop();
|
|
98
101
|
child?.kill();
|
|
99
102
|
});
|
|
100
103
|
|
|
101
|
-
|
|
104
|
+
// The client side needs the WebSocket of the platform: browsers, Bun, Deno and Node.js 22 or later.
|
|
105
|
+
describe.skipIf(typeof WebSocket === 'undefined')('rpc over WebSocket', () => {
|
|
102
106
|
|
|
103
107
|
it('calls the services over one connection, which the server calls the @RpcClient components back over', async () => {
|
|
104
|
-
const proxy =
|
|
108
|
+
const proxy = proxyOf<ChatServer>(ChatServer);
|
|
105
109
|
expect(await proxy.echo('hello')).toBe('hello');
|
|
106
110
|
expect(await proxy.viaWebSocket()).toBe(true);
|
|
107
111
|
expect(await proxy.connections()).toBe(1);
|
|
@@ -110,7 +114,7 @@ describe('rpc over WebSocket', () => {
|
|
|
110
114
|
});
|
|
111
115
|
|
|
112
116
|
it('answers like HTTP: application errors with code and data, refusals with their message, internal errors without it', async () => {
|
|
113
|
-
const proxy =
|
|
117
|
+
const proxy = proxyOf<ChatServer>(ChatServer);
|
|
114
118
|
const application = await proxy.rejectApplication().catch(e => e);
|
|
115
119
|
expect(NotYours.is(application) || application.code === NotYours.code).toBe(true);
|
|
116
120
|
expect(application.data).toEqual({ id: '42' });
|
|
@@ -162,7 +166,7 @@ describe('rpc over WebSocket', () => {
|
|
|
162
166
|
const own = chat(transport);
|
|
163
167
|
await own.join();
|
|
164
168
|
// This client has no @RpcClient: the server's call fails with METHOD_NOT_FOUND.
|
|
165
|
-
const observer =
|
|
169
|
+
const observer = proxyOf<ChatServer>(ChatServer);
|
|
166
170
|
expect((await observer.callJoined('x')).code).toBe(JsonRpc.METHOD_NOT_FOUND);
|
|
167
171
|
transport.close();
|
|
168
172
|
let state = await observer.joinedState();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApplicationError, Autowired, Component, Logger, Optional } from '@celljs/core';
|
|
1
|
+
import { ApplicationError, Autowired, Component, Container, Logger, Optional } from '@celljs/core';
|
|
2
2
|
import { PipeManager } from '@celljs/web';
|
|
3
3
|
import { ErrorMessageResolver } from '@celljs/web/lib/node';
|
|
4
4
|
import { JsonRpc, RPC_SERVICE, RpcMethodUtil, RpcServiceMetadata, ConverterUtil } from '../../common';
|
|
@@ -27,6 +27,9 @@ export class RpcInvokerImpl implements RpcInvoker {
|
|
|
27
27
|
@Autowired(Logger)
|
|
28
28
|
protected readonly logger: Logger;
|
|
29
29
|
|
|
30
|
+
@Autowired(Container)
|
|
31
|
+
protected readonly container: Container;
|
|
32
|
+
|
|
30
33
|
protected services?: Map<string, RpcServiceMetadata>;
|
|
31
34
|
|
|
32
35
|
has(service: string): boolean {
|
|
@@ -82,8 +85,8 @@ export class RpcInvokerImpl implements RpcInvoker {
|
|
|
82
85
|
*/
|
|
83
86
|
protected toError(metadata: RpcServiceMetadata, method: string, error: unknown): [number, string, unknown] {
|
|
84
87
|
const service = metadata.path;
|
|
85
|
-
const own = ConverterUtil.getErrorConverters(metadata.id);
|
|
86
|
-
for (const converter of [...ConverterUtil.getGlobalErrorConverters(), ...(own ? [own] : [])]) {
|
|
88
|
+
const own = ConverterUtil.getErrorConverters(metadata.id, this.container);
|
|
89
|
+
for (const converter of [...ConverterUtil.getGlobalErrorConverters(this.container), ...(own ? [own] : [])]) {
|
|
87
90
|
const converted = converter.serialize(error);
|
|
88
91
|
if (converted) {
|
|
89
92
|
return [converted.code, converted.message, converted.data];
|
|
@@ -101,4 +104,4 @@ export class RpcInvokerImpl implements RpcInvoker {
|
|
|
101
104
|
}
|
|
102
105
|
return [JsonRpc.SERVER_ERROR, message, { status }];
|
|
103
106
|
}
|
|
104
|
-
}
|
|
107
|
+
}
|
|
@@ -7,7 +7,6 @@ import webCommonModule from '@celljs/web/lib/common/module';
|
|
|
7
7
|
import webNodeModule from '@celljs/web/lib/node/module';
|
|
8
8
|
import { Autowired, Component } from '@celljs/core';
|
|
9
9
|
import { Container } from '@celljs/core/lib/common/container/container';
|
|
10
|
-
import { ContainerProvider } from '@celljs/core/lib/common/container/container-provider';
|
|
11
10
|
import { autoBind } from '@celljs/core/lib/common/container/auto-bind';
|
|
12
11
|
import { currentThis } from '@celljs/core/lib/common/utils';
|
|
13
12
|
import { Application } from '@celljs/core/lib/common/application/application-protocol';
|
|
@@ -119,7 +118,6 @@ export async function startFixtureServer(): Promise<{ port: number; server: Serv
|
|
|
119
118
|
rpc: { path: 'api', transport: 'websocket', websocket: { maxPayload: 65536, heartbeat: 30000, callTimeout: 2000, allowedOrigins: ['https://app.example.com'] } },
|
|
120
119
|
};
|
|
121
120
|
const container = new Container(coreCommonStatic, coreNodeStatic, httpCommonModule, webCommonModule, webNodeModule, rpcCommonModule, rpcNodeModule, autoBind());
|
|
122
|
-
ContainerProvider.set(container);
|
|
123
121
|
await container.get<Application>(Application).start();
|
|
124
122
|
handler = HttpServerUtil.createHandler(container);
|
|
125
123
|
for (const aware of container.getAll<ServerAware>(ServerAware)) {
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import type { IncomingMessage } from 'node:http';
|
|
3
3
|
import type { Duplex } from 'node:stream';
|
|
4
|
-
|
|
4
|
+
// The emitter alone: a process with only WebSocket connections (ta exec-server) does not load the framework.
|
|
5
|
+
import { Emitter } from '@celljs/core/lib/common/utils/emitter';
|
|
6
|
+
import type { Event } from '@celljs/core/lib/common/utils/event';
|
|
5
7
|
import { WebSocketFrameParser, WebSocketFrames, WebSocketProtocolError } from './websocket-frames';
|
|
6
8
|
|
|
7
9
|
export interface WebSocketConnectionOptions {
|
|
@@ -197,4 +199,4 @@ export class WebSocketConnection {
|
|
|
197
199
|
this.onMessageEmitter.dispose();
|
|
198
200
|
this.onCloseEmitter.dispose();
|
|
199
201
|
}
|
|
200
|
-
}
|
|
202
|
+
}
|
package/src/package.spec.ts
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
|
-
import { describe, it } from 'bun:test';
|
|
2
|
-
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { currentThis } from '@celljs/core';
|
|
3
|
+
import { AssemblyTestUtil } from '@celljs/core/lib/common/test/assembly-test-util';
|
|
4
|
+
import m0 from '@celljs/core/lib/common/static-module';
|
|
5
|
+
import m1 from '@celljs/core/lib/node/static-module';
|
|
6
|
+
import m2 from '@celljs/http/lib/common/module';
|
|
7
|
+
import m3 from '@celljs/web/lib/common/module';
|
|
8
|
+
import m4 from '@celljs/web/lib/node/module';
|
|
9
|
+
import own0 from './common/module';
|
|
10
|
+
import own1 from './node/module';
|
|
3
11
|
|
|
4
|
-
|
|
12
|
+
currentThis.cellProps = {};
|
|
13
|
+
|
|
14
|
+
describe('@celljs/rpc', () => {
|
|
15
|
+
it('its DI module loads after those of its dependencies and every binding resolves', () => {
|
|
16
|
+
const { changed, errors } = AssemblyTestUtil.check([m0, m1, m2, m3, m4, own0], own1);
|
|
17
|
+
expect(errors).toEqual([]);
|
|
18
|
+
expect(changed).toBeGreaterThan(0);
|
|
19
|
+
});
|
|
5
20
|
});
|