@celljs/rpc 3.0.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/LICENSE +21 -0
- package/README.md +1 -0
- package/cell.yml +8 -0
- package/lib/common/annotation/autorpc.d.ts +6 -0
- package/lib/common/annotation/autorpc.d.ts.map +1 -0
- package/lib/common/annotation/autorpc.js +19 -0
- package/lib/common/annotation/autorpc.js.map +1 -0
- package/lib/common/annotation/detached/autorpc.d.ts +3 -0
- package/lib/common/annotation/detached/autorpc.d.ts.map +1 -0
- package/lib/common/annotation/detached/autorpc.js +15 -0
- package/lib/common/annotation/detached/autorpc.js.map +1 -0
- package/lib/common/annotation/detached/index.d.ts +2 -0
- package/lib/common/annotation/detached/index.d.ts.map +1 -0
- package/lib/common/annotation/detached/index.js +18 -0
- package/lib/common/annotation/detached/index.js.map +1 -0
- package/lib/common/annotation/index.d.ts +3 -0
- package/lib/common/annotation/index.d.ts.map +1 -0
- package/lib/common/annotation/index.js +19 -0
- package/lib/common/annotation/index.js.map +1 -0
- package/lib/common/annotation/rpc.d.ts +20 -0
- package/lib/common/annotation/rpc.d.ts.map +1 -0
- package/lib/common/annotation/rpc.js +41 -0
- package/lib/common/annotation/rpc.js.map +1 -0
- package/lib/common/channal/channel-protocol.d.ts +52 -0
- package/lib/common/channal/channel-protocol.d.ts.map +1 -0
- package/lib/common/channal/channel-protocol.js +38 -0
- package/lib/common/channal/channel-protocol.js.map +1 -0
- package/lib/common/channal/http-channel.d.ts +9 -0
- package/lib/common/channal/http-channel.d.ts.map +1 -0
- package/lib/common/channal/http-channel.js +26 -0
- package/lib/common/channal/http-channel.js.map +1 -0
- package/lib/common/channal/index.d.ts +3 -0
- package/lib/common/channal/index.d.ts.map +1 -0
- package/lib/common/channal/index.js +19 -0
- package/lib/common/channal/index.js.map +1 -0
- package/lib/common/constants.d.ts +2 -0
- package/lib/common/constants.d.ts.map +1 -0
- package/lib/common/constants.js +5 -0
- package/lib/common/constants.js.map +1 -0
- package/lib/common/converter/converter-protocol.d.ts +7 -0
- package/lib/common/converter/converter-protocol.d.ts.map +1 -0
- package/lib/common/converter/converter-protocol.js +6 -0
- package/lib/common/converter/converter-protocol.js.map +1 -0
- package/lib/common/converter/index.d.ts +2 -0
- package/lib/common/converter/index.d.ts.map +1 -0
- package/lib/common/converter/index.js +18 -0
- package/lib/common/converter/index.js.map +1 -0
- package/lib/common/endpoint/endpoint-protocol.d.ts +5 -0
- package/lib/common/endpoint/endpoint-protocol.d.ts.map +1 -0
- package/lib/common/endpoint/endpoint-protocol.js +5 -0
- package/lib/common/endpoint/endpoint-protocol.js.map +1 -0
- package/lib/common/endpoint/endpoint-resolver.d.ts +13 -0
- package/lib/common/endpoint/endpoint-resolver.d.ts.map +1 -0
- package/lib/common/endpoint/endpoint-resolver.js +61 -0
- package/lib/common/endpoint/endpoint-resolver.js.map +1 -0
- package/lib/common/endpoint/index.d.ts +3 -0
- package/lib/common/endpoint/index.d.ts.map +1 -0
- package/lib/common/endpoint/index.js +19 -0
- package/lib/common/endpoint/index.js.map +1 -0
- package/lib/common/error/error.d.ts +6 -0
- package/lib/common/error/error.d.ts.map +1 -0
- package/lib/common/error/error.js +12 -0
- package/lib/common/error/error.js.map +1 -0
- package/lib/common/error/index.d.ts +2 -0
- package/lib/common/error/index.d.ts.map +1 -0
- package/lib/common/error/index.js +18 -0
- package/lib/common/error/index.js.map +1 -0
- package/lib/common/factory/connection-factory.d.ts +10 -0
- package/lib/common/factory/connection-factory.d.ts.map +1 -0
- package/lib/common/factory/connection-factory.js +28 -0
- package/lib/common/factory/connection-factory.js.map +1 -0
- package/lib/common/factory/index.d.ts +5 -0
- package/lib/common/factory/index.d.ts.map +1 -0
- package/lib/common/factory/index.js +21 -0
- package/lib/common/factory/index.js.map +1 -0
- package/lib/common/factory/proxy-factory.d.ts +57 -0
- package/lib/common/factory/proxy-factory.d.ts.map +1 -0
- package/lib/common/factory/proxy-factory.js +182 -0
- package/lib/common/factory/proxy-factory.js.map +1 -0
- package/lib/common/factory/reader.d.ts +16 -0
- package/lib/common/factory/reader.d.ts.map +1 -0
- package/lib/common/factory/reader.js +48 -0
- package/lib/common/factory/reader.js.map +1 -0
- package/lib/common/factory/writer.d.ts +10 -0
- package/lib/common/factory/writer.d.ts.map +1 -0
- package/lib/common/factory/writer.js +25 -0
- package/lib/common/factory/writer.js.map +1 -0
- package/lib/common/handler/connection-handler.d.ts +7 -0
- package/lib/common/handler/connection-handler.d.ts.map +1 -0
- package/lib/common/handler/connection-handler.js +8 -0
- package/lib/common/handler/connection-handler.js.map +1 -0
- package/lib/common/handler/handler-protocol.d.ts +7 -0
- package/lib/common/handler/handler-protocol.d.ts.map +1 -0
- package/lib/common/handler/handler-protocol.js +5 -0
- package/lib/common/handler/handler-protocol.js.map +1 -0
- package/lib/common/handler/index.d.ts +3 -0
- package/lib/common/handler/index.d.ts.map +1 -0
- package/lib/common/handler/index.js +19 -0
- package/lib/common/handler/index.js.map +1 -0
- package/lib/common/index.d.ts +12 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +28 -0
- package/lib/common/index.js.map +1 -0
- package/lib/common/module.d.ts +4 -0
- package/lib/common/module.d.ts.map +1 -0
- package/lib/common/module.js +22 -0
- package/lib/common/module.js.map +1 -0
- package/lib/common/processor/client-config-processor.d.ts +6 -0
- package/lib/common/processor/client-config-processor.d.ts.map +1 -0
- package/lib/common/processor/client-config-processor.js +20 -0
- package/lib/common/processor/client-config-processor.js.map +1 -0
- package/lib/common/processor/index.d.ts +3 -0
- package/lib/common/processor/index.d.ts.map +1 -0
- package/lib/common/processor/index.js +19 -0
- package/lib/common/processor/index.js.map +1 -0
- package/lib/common/processor/processor-protocol.d.ts +6 -0
- package/lib/common/processor/processor-protocol.d.ts.map +1 -0
- package/lib/common/processor/processor-protocol.js +5 -0
- package/lib/common/processor/processor-protocol.js.map +1 -0
- package/lib/common/proxy/delegating-http-proxy-creator.d.ts +24 -0
- package/lib/common/proxy/delegating-http-proxy-creator.d.ts.map +1 -0
- package/lib/common/proxy/delegating-http-proxy-creator.js +89 -0
- package/lib/common/proxy/delegating-http-proxy-creator.js.map +1 -0
- package/lib/common/proxy/http-proxy-creator.d.ts +43 -0
- package/lib/common/proxy/http-proxy-creator.d.ts.map +1 -0
- package/lib/common/proxy/http-proxy-creator.js +137 -0
- package/lib/common/proxy/http-proxy-creator.js.map +1 -0
- package/lib/common/proxy/index.d.ts +5 -0
- package/lib/common/proxy/index.d.ts.map +1 -0
- package/lib/common/proxy/index.js +21 -0
- package/lib/common/proxy/index.js.map +1 -0
- package/lib/common/proxy/proxy-protocol.d.ts +22 -0
- package/lib/common/proxy/proxy-protocol.d.ts.map +1 -0
- package/lib/common/proxy/proxy-protocol.js +6 -0
- package/lib/common/proxy/proxy-protocol.js.map +1 -0
- package/lib/common/proxy/proxy-provider.d.ts +10 -0
- package/lib/common/proxy/proxy-provider.d.ts.map +1 -0
- package/lib/common/proxy/proxy-provider.js +43 -0
- package/lib/common/proxy/proxy-provider.js.map +1 -0
- package/lib/common/utils/converter-util.d.ts +8 -0
- package/lib/common/utils/converter-util.d.ts.map +1 -0
- package/lib/common/utils/converter-util.js +32 -0
- package/lib/common/utils/converter-util.js.map +1 -0
- package/lib/common/utils/index.d.ts +4 -0
- package/lib/common/utils/index.d.ts.map +1 -0
- package/lib/common/utils/index.js +20 -0
- package/lib/common/utils/index.js.map +1 -0
- package/lib/common/utils/logger.d.ts +9 -0
- package/lib/common/utils/logger.d.ts.map +1 -0
- package/lib/common/utils/logger.js +22 -0
- package/lib/common/utils/logger.js.map +1 -0
- package/lib/common/utils/rpc-util.d.ts +13 -0
- package/lib/common/utils/rpc-util.d.ts.map +1 -0
- package/lib/common/utils/rpc-util.js +46 -0
- package/lib/common/utils/rpc-util.js.map +1 -0
- package/lib/node/channel/channel-manager.d.ts +10 -0
- package/lib/node/channel/channel-manager.d.ts.map +1 -0
- package/lib/node/channel/channel-manager.js +59 -0
- package/lib/node/channel/channel-manager.js.map +1 -0
- package/lib/node/channel/channel-middleware.d.ts +8 -0
- package/lib/node/channel/channel-middleware.d.ts.map +1 -0
- package/lib/node/channel/channel-middleware.js +40 -0
- package/lib/node/channel/channel-middleware.js.map +1 -0
- package/lib/node/channel/channel-protocol.d.ts +13 -0
- package/lib/node/channel/channel-protocol.d.ts.map +1 -0
- package/lib/node/channel/channel-protocol.js +9 -0
- package/lib/node/channel/channel-protocol.js.map +1 -0
- package/lib/node/channel/channel-strategy.d.ts +10 -0
- package/lib/node/channel/channel-strategy.d.ts.map +1 -0
- package/lib/node/channel/channel-strategy.js +64 -0
- package/lib/node/channel/channel-strategy.js.map +1 -0
- package/lib/node/channel/index.d.ts +5 -0
- package/lib/node/channel/index.d.ts.map +1 -0
- package/lib/node/channel/index.js +21 -0
- package/lib/node/channel/index.js.map +1 -0
- package/lib/node/handler/handler-adapter.d.ts +14 -0
- package/lib/node/handler/handler-adapter.d.ts.map +1 -0
- package/lib/node/handler/handler-adapter.js +55 -0
- package/lib/node/handler/handler-adapter.js.map +1 -0
- package/lib/node/handler/handler-protocol.d.ts +2 -0
- package/lib/node/handler/handler-protocol.d.ts.map +1 -0
- package/lib/node/handler/handler-protocol.js +5 -0
- package/lib/node/handler/handler-protocol.js.map +1 -0
- package/lib/node/handler/index.d.ts +3 -0
- package/lib/node/handler/index.d.ts.map +1 -0
- package/lib/node/handler/index.js +19 -0
- package/lib/node/handler/index.js.map +1 -0
- package/lib/node/index.d.ts +3 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +19 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +4 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +45 -0
- package/src/common/annotation/autorpc.ts +17 -0
- package/src/common/annotation/detached/autorpc.ts +12 -0
- package/src/common/annotation/detached/index.ts +1 -0
- package/src/common/annotation/index.ts +2 -0
- package/src/common/annotation/rpc.ts +41 -0
- package/src/common/channal/channel-protocol.ts +82 -0
- package/src/common/channal/http-channel.ts +30 -0
- package/src/common/channal/index.ts +2 -0
- package/src/common/constants.ts +1 -0
- package/src/common/converter/converter-protocol.ts +8 -0
- package/src/common/converter/index.ts +1 -0
- package/src/common/endpoint/endpoint-protocol.ts +5 -0
- package/src/common/endpoint/endpoint-resolver.ts +46 -0
- package/src/common/endpoint/index.ts +2 -0
- package/src/common/error/error.ts +7 -0
- package/src/common/error/index.ts +1 -0
- package/src/common/factory/connection-factory.ts +21 -0
- package/src/common/factory/index.ts +4 -0
- package/src/common/factory/proxy-factory.ts +219 -0
- package/src/common/factory/reader.ts +50 -0
- package/src/common/factory/writer.ts +26 -0
- package/src/common/handler/connection-handler.ts +7 -0
- package/src/common/handler/handler-protocol.ts +8 -0
- package/src/common/handler/index.ts +2 -0
- package/src/common/index.ts +11 -0
- package/src/common/module.ts +21 -0
- package/src/common/processor/client-config-processor.ts +12 -0
- package/src/common/processor/index.ts +2 -0
- package/src/common/processor/processor-protocol.ts +7 -0
- package/src/common/proxy/delegating-http-proxy-creator.ts +68 -0
- package/src/common/proxy/http-proxy-creator.ts +179 -0
- package/src/common/proxy/index.ts +5 -0
- package/src/common/proxy/proxy-protocol.ts +26 -0
- package/src/common/proxy/proxy-provider.ts +29 -0
- package/src/common/utils/converter-util.ts +29 -0
- package/src/common/utils/index.ts +3 -0
- package/src/common/utils/logger.ts +23 -0
- package/src/common/utils/rpc-util.ts +46 -0
- package/src/node/channel/channel-manager.ts +44 -0
- package/src/node/channel/channel-middleware.ts +27 -0
- package/src/node/channel/channel-protocol.ts +21 -0
- package/src/node/channel/channel-strategy.ts +56 -0
- package/src/node/channel/index.ts +4 -0
- package/src/node/handler/handler-adapter.ts +38 -0
- package/src/node/handler/handler-protocol.ts +1 -0
- package/src/node/handler/index.ts +2 -0
- package/src/node/index.ts +2 -0
- package/src/node/module.ts +4 -0
- package/src/package.spec.ts +4 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Prioritizeable, Component, Autowired } from '@celljs/core';
|
|
2
|
+
import { ErrorConverter } from '../converter';
|
|
3
|
+
import { JsonRpcProxy } from '../factory';
|
|
4
|
+
import { ProxyProvider, ProxyCreator } from './proxy-protocol';
|
|
5
|
+
|
|
6
|
+
@Component(ProxyProvider)
|
|
7
|
+
export class ProxyProviderImpl implements ProxyProvider {
|
|
8
|
+
|
|
9
|
+
constructor(
|
|
10
|
+
@Autowired(ProxyCreator)
|
|
11
|
+
protected readonly proxyCreators: ProxyCreator[]
|
|
12
|
+
) { }
|
|
13
|
+
|
|
14
|
+
provide<T extends object>(path: string, errorConverters?: ErrorConverter[], target?: object): JsonRpcProxy<T> {
|
|
15
|
+
return this.prioritize(path)[0].create(path, errorConverters, target);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
protected prioritize(path: string): ProxyCreator[] {
|
|
19
|
+
const prioritized = Prioritizeable.prioritizeAllSync(this.proxyCreators, proxyCreator => {
|
|
20
|
+
try {
|
|
21
|
+
return proxyCreator.support(path);
|
|
22
|
+
} catch {
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return prioritized.map(p => p.value);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ContainerUtil, Container } from '@celljs/core';
|
|
2
|
+
import { ErrorConverter, GlobalConverter } from '../converter';
|
|
3
|
+
import { RpcUtil } from './rpc-util';
|
|
4
|
+
|
|
5
|
+
export namespace ConverterUtil {
|
|
6
|
+
|
|
7
|
+
export function getErrorConverterIgnoreNoMatching<T>(task: () => T): T | undefined {
|
|
8
|
+
try {
|
|
9
|
+
return task();
|
|
10
|
+
} catch (error) {
|
|
11
|
+
if (!error?.message.startsWith('No matching bindings found for serviceIdentifier: Symbol(ErrorConverter)')) {
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getGlobalErrorConverters(container?: Container) {
|
|
18
|
+
return getErrorConverterIgnoreNoMatching(() =>
|
|
19
|
+
container ? container.getAllNamed<ErrorConverter>(ErrorConverter, GlobalConverter) :
|
|
20
|
+
ContainerUtil.getAllNamed<ErrorConverter>(ErrorConverter, GlobalConverter)) || [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getErrorConverters(serviceIdentifier: any, container?: Container) {
|
|
24
|
+
const name = RpcUtil.toName(serviceIdentifier);
|
|
25
|
+
return getErrorConverterIgnoreNoMatching(() =>
|
|
26
|
+
container ? container.getNamed<ErrorConverter>(ErrorConverter, name) :
|
|
27
|
+
ContainerUtil.getNamed<ErrorConverter>(ErrorConverter, name));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Logger as ILoger } from 'vscode-jsonrpc';
|
|
2
|
+
|
|
3
|
+
export class ConsoleLogger implements ILoger {
|
|
4
|
+
public error(message: string): void {
|
|
5
|
+
console.error(message);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
public warn(message: string): void {
|
|
9
|
+
console.warn(message);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public info(message: string): void {
|
|
13
|
+
console.info(message);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public log(message: string): void {
|
|
17
|
+
console.log(message);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public debug(message: string): void {
|
|
21
|
+
console.debug(message);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ContainerUtil, ComponentId, UrlUtil } from '@celljs/core';
|
|
2
|
+
import { ID_KEY, RPC } from '../annotation';
|
|
3
|
+
import { HttpProxyCreator, HttpProxyCreatorOptions, MergeOptions } from '../proxy/http-proxy-creator';
|
|
4
|
+
import { ProxyCreator } from '../proxy/proxy-protocol';
|
|
5
|
+
|
|
6
|
+
let defaultProxyCreator: ProxyCreator | undefined;
|
|
7
|
+
|
|
8
|
+
export namespace RpcUtil {
|
|
9
|
+
export function get<T extends object>(rpcServiceIdentifier: ComponentId<T>): T {
|
|
10
|
+
if (defaultProxyCreator) {
|
|
11
|
+
return defaultProxyCreator.create<T>(toPath(rpcServiceIdentifier));
|
|
12
|
+
}
|
|
13
|
+
return ContainerUtil.getTagged(RPC, ID_KEY, rpcServiceIdentifier);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getDefaultProxyCreator(endpoint: string, options?: Partial<HttpProxyCreatorOptions<Partial<MergeOptions>>>) {
|
|
17
|
+
return defaultProxyCreator;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function setDefaultProxyCreator(endpoint: string, options?: Partial<HttpProxyCreatorOptions<Partial<MergeOptions>>>) {
|
|
21
|
+
defaultProxyCreator = createProxyCreator(endpoint, options);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createProxyCreator(endpoint: string, options?: Partial<HttpProxyCreatorOptions<Partial<MergeOptions>>>) {
|
|
25
|
+
return new HttpProxyCreator({ ...options, endpointResolver: {
|
|
26
|
+
resolve(serviceIdentifier: string) {
|
|
27
|
+
return Promise.resolve(UrlUtil.join(endpoint, serviceIdentifier));
|
|
28
|
+
}
|
|
29
|
+
}});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function create<T extends object>(endpoint: string, rpcServiceIdentifier: ComponentId<T>,
|
|
33
|
+
options?: Partial<HttpProxyCreatorOptions<Partial<MergeOptions>>>): T {
|
|
34
|
+
return createProxyCreator(endpoint, options).create<T>(toPath(rpcServiceIdentifier));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function toPath(serviceIdentifier: any) {
|
|
38
|
+
return typeof serviceIdentifier !== 'function' ? serviceIdentifier.toString() : serviceIdentifier.name || serviceIdentifier.toString();
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function toName(serviceIdentifier: any) {
|
|
43
|
+
return typeof serviceIdentifier !== 'function' ? <symbol | string>serviceIdentifier : serviceIdentifier.name || serviceIdentifier.toString();
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Context, NotFoundError } from '@celljs/web/lib/node';
|
|
2
|
+
import { ConnectionHandler, ConnnectionFactory, Channel, ConsoleLogger } from '../../common';
|
|
3
|
+
import { Component, Autowired, Deferred, Optional } from '@celljs/core';
|
|
4
|
+
import { CURRENT_CHANNEL_STRATEGY_REQUEST_KEY, ChannelStrategy } from './channel-protocol';
|
|
5
|
+
|
|
6
|
+
// tslint:disable:no-any
|
|
7
|
+
@Component()
|
|
8
|
+
export class ChannelManager {
|
|
9
|
+
|
|
10
|
+
protected _handlers = new Map<string, ConnectionHandler>();
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
@Autowired(ConnectionHandler) @Optional()
|
|
14
|
+
protected readonly handlers: ConnectionHandler[],
|
|
15
|
+
@Autowired(ConnnectionFactory) protected connnectionFactory: ConnnectionFactory<Channel>
|
|
16
|
+
) {
|
|
17
|
+
for (const handler of handlers) {
|
|
18
|
+
this._handlers.set(handler.path, handler);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async handleChannels(): Promise<void> {
|
|
23
|
+
const channelStrategy: ChannelStrategy = Context.getAttr(CURRENT_CHANNEL_STRATEGY_REQUEST_KEY);
|
|
24
|
+
Context.getResponse().body = new Deferred<any>();
|
|
25
|
+
for (const message of await channelStrategy.getMessages()) {
|
|
26
|
+
const { id, path } = message as any;
|
|
27
|
+
if (path) {
|
|
28
|
+
const handler = this._handlers.get(this.getRealPath(path));
|
|
29
|
+
if (handler) {
|
|
30
|
+
const channel = await channelStrategy.createChannel(id);
|
|
31
|
+
handler.onConnection(this.connnectionFactory.create(channel, new ConsoleLogger()));
|
|
32
|
+
channel.handleMessage(message);
|
|
33
|
+
}
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
throw new NotFoundError(`Cannot find a service for the path: ${path}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
protected getRealPath(path: string): string {
|
|
41
|
+
return <string>path.split(':').pop();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Middleware, Context } from '@celljs/web/lib/node';
|
|
2
|
+
import { Component, Autowired } from '@celljs/core';
|
|
3
|
+
import { ChannelStrategy, CHANNEL_MIDDLEWARE_PRIORITY, CURRENT_CHANNEL_STRATEGY_REQUEST_KEY } from './channel-protocol';
|
|
4
|
+
import { HttpHeaders, MediaType } from '@celljs/http';
|
|
5
|
+
|
|
6
|
+
@Component(Middleware)
|
|
7
|
+
export class ChannelMiddleware implements Middleware {
|
|
8
|
+
|
|
9
|
+
@Autowired(ChannelStrategy)
|
|
10
|
+
protected readonly channelStrategies: ChannelStrategy[];
|
|
11
|
+
|
|
12
|
+
async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
|
|
13
|
+
for (const channelStrategy of this.channelStrategies) {
|
|
14
|
+
if (await channelStrategy.support()) {
|
|
15
|
+
Context.setAttr(CURRENT_CHANNEL_STRATEGY_REQUEST_KEY, channelStrategy);
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
ctx.response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8);
|
|
21
|
+
|
|
22
|
+
await next();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
readonly priority = CHANNEL_MIDDLEWARE_PRIORITY;
|
|
26
|
+
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Channel } from '../../common';
|
|
2
|
+
|
|
3
|
+
export const ChannelStrategy = Symbol('ChannelStrategy');
|
|
4
|
+
|
|
5
|
+
export const CHANNEL_MIDDLEWARE_PRIORITY = 2100;
|
|
6
|
+
|
|
7
|
+
export const CURRENT_CHANNEL_STRATEGY_REQUEST_KEY = 'CurrentChannelStrategyRequest';
|
|
8
|
+
export const CURRENT_MESSAGE_COUNT_REQUEST_KEY = 'CurrentMessageCountRequest';
|
|
9
|
+
export const CURRENT_RESPONSE_MESSAGE_REQUEST_KEY = 'CurrentResponseMessageRequest';
|
|
10
|
+
|
|
11
|
+
export interface ChannelStrategy {
|
|
12
|
+
|
|
13
|
+
getMessages(): Promise<Channel.Message[]>;
|
|
14
|
+
|
|
15
|
+
handleMessage(message: any): Promise<void>;
|
|
16
|
+
|
|
17
|
+
createChannel(id: number): Promise<Channel>
|
|
18
|
+
|
|
19
|
+
support(): Promise<boolean>;
|
|
20
|
+
|
|
21
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Channel, HttpChannel } from '../../common/channal';
|
|
2
|
+
import { ChannelStrategy, CURRENT_MESSAGE_COUNT_REQUEST_KEY, CURRENT_RESPONSE_MESSAGE_REQUEST_KEY } from './channel-protocol';
|
|
3
|
+
import { Component } from '@celljs/core';
|
|
4
|
+
import { Context } from '@celljs/web/lib/node';
|
|
5
|
+
|
|
6
|
+
@Component(ChannelStrategy)
|
|
7
|
+
export class HttpChannelStrategy implements ChannelStrategy {
|
|
8
|
+
async getMessages(): Promise<Channel.Message[]> {
|
|
9
|
+
let message = await Context.getRequest().body;
|
|
10
|
+
if (!Array.isArray(message)) {
|
|
11
|
+
message = [ message ];
|
|
12
|
+
} else {
|
|
13
|
+
const parsed: Channel.Message[] = [];
|
|
14
|
+
for (const m of message) {
|
|
15
|
+
parsed.push(JSON.parse(m));
|
|
16
|
+
}
|
|
17
|
+
message = parsed;
|
|
18
|
+
}
|
|
19
|
+
Context.setAttr(CURRENT_MESSAGE_COUNT_REQUEST_KEY, message.length);
|
|
20
|
+
return message;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async createChannel(id: number): Promise<Channel> {
|
|
24
|
+
return new HttpChannel(id, async content => {
|
|
25
|
+
this.handleMessage(content);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async handleMessage(message: string): Promise<void> {
|
|
30
|
+
if (!this.consumeMessage(message)) {
|
|
31
|
+
const deferred = Context.getResponse().body;
|
|
32
|
+
const messages = Context.getAttr<string[]>(CURRENT_RESPONSE_MESSAGE_REQUEST_KEY);
|
|
33
|
+
deferred.resolve(messages.length > 1 ? JSON.stringify(messages) : message);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected consumeMessage(message: string) {
|
|
38
|
+
const messageCount = Context.getAttr<number>(CURRENT_MESSAGE_COUNT_REQUEST_KEY);
|
|
39
|
+
Context.setAttr(CURRENT_MESSAGE_COUNT_REQUEST_KEY, messageCount - 1);
|
|
40
|
+
let messages = Context.getAttr<string[]>(CURRENT_RESPONSE_MESSAGE_REQUEST_KEY);
|
|
41
|
+
if (!messages) {
|
|
42
|
+
messages = [];
|
|
43
|
+
Context.setAttr(CURRENT_RESPONSE_MESSAGE_REQUEST_KEY, messages);
|
|
44
|
+
}
|
|
45
|
+
messages.push(message);
|
|
46
|
+
if (messageCount - 1 <= 0) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async support(): Promise<boolean> {
|
|
53
|
+
return Context.getCurrent() instanceof Context;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Component, Autowired, Value, Deferred } from '@celljs/core';
|
|
2
|
+
import { ChannelManager } from '../channel';
|
|
3
|
+
import { RPC_HANDLER_ADAPTER_PRIORITY } from './handler-protocol';
|
|
4
|
+
import { RequestMatcher, HandlerAdapter, Context } from '@celljs/web/lib/node';
|
|
5
|
+
import { PathResolver } from '@celljs/web';
|
|
6
|
+
import { RPC_PATH } from '../../common';
|
|
7
|
+
|
|
8
|
+
export const PATH_PARMAS_ATTR = 'pathParams';
|
|
9
|
+
|
|
10
|
+
@Component(HandlerAdapter)
|
|
11
|
+
export class RpcHandlerAdapter implements HandlerAdapter {
|
|
12
|
+
readonly priority = RPC_HANDLER_ADAPTER_PRIORITY;
|
|
13
|
+
|
|
14
|
+
@Autowired(ChannelManager)
|
|
15
|
+
protected readonly channelManager: ChannelManager;
|
|
16
|
+
|
|
17
|
+
@Autowired(RequestMatcher)
|
|
18
|
+
protected readonly requestMatcher: RequestMatcher;
|
|
19
|
+
|
|
20
|
+
@Autowired(PathResolver)
|
|
21
|
+
protected readonly pathResolver: PathResolver;
|
|
22
|
+
|
|
23
|
+
@Value(RPC_PATH)
|
|
24
|
+
protected readonly rpcPath: string;
|
|
25
|
+
|
|
26
|
+
async handle(): Promise<void> {
|
|
27
|
+
await this.channelManager.handleChannels();
|
|
28
|
+
const response = Context.getResponse();
|
|
29
|
+
if (response.body instanceof Deferred) {
|
|
30
|
+
response.body = await response.body.promise;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async canHandle(): Promise<boolean> {
|
|
35
|
+
return this.requestMatcher.match(await this.pathResolver.resolve(this.rpcPath));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const RPC_HANDLER_ADAPTER_PRIORITY = 1000;
|