@powersync/service-rsocket-router 0.0.6

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 ADDED
@@ -0,0 +1,67 @@
1
+ # Functional Source License, Version 1.1, Apache 2.0 Future License
2
+
3
+ ## Abbreviation
4
+
5
+ FSL-1.1-Apache-2.0
6
+
7
+ ## Notice
8
+
9
+ Copyright 2023-2024 Journey Mobile, Inc.
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under these Terms and Conditions, as indicated by our inclusion of these Terms and Conditions with the Software.
20
+
21
+ ### License Grant
22
+
23
+ Subject to your compliance with this License Grant and the Patents, Redistribution and Trademark clauses below, we hereby grant you the right to use, copy, modify, create derivative works, publicly perform, publicly display and redistribute the Software for any Permitted Purpose identified below.
24
+
25
+ ### Permitted Purpose
26
+
27
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use means making the Software available to others in a commercial product or service that:
28
+
29
+ 1. substitutes for the Software;
30
+ 2. substitutes for any other product or service we offer using the Software that exists as of the date we make the Software available; or
31
+ 3. offers the same or substantially similar functionality as the Software.
32
+
33
+ Permitted Purposes specifically include using the Software:
34
+
35
+ 1. for your internal use and access;
36
+ 2. for non-commercial education;
37
+ 3. for non-commercial research; and
38
+ 4. in connection with professional services that you provide to a licensee using the Software in accordance with these Terms and Conditions.
39
+
40
+ ### Patents
41
+
42
+ To the extent your use for a Permitted Purpose would necessarily infringe our patents, the license grant above includes a license under our patents. If you make a claim against any party that the Software infringes or contributes to the infringement of any patent, then your patent license to the Software ends immediately.
43
+
44
+ ### Redistribution
45
+
46
+ The Terms and Conditions apply to all copies, modifications and derivatives of the Software.
47
+ If you redistribute any copies, modifications or derivatives of the Software, you must include a copy of or a link to these Terms and Conditions and not remove any copyright notices provided in or with the Software.
48
+
49
+ ### Disclaimer
50
+
51
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
52
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
53
+
54
+ ### Trademarks
55
+
56
+ Except for displaying the License Details and identifying us as the origin of the Software, you have no right under these Terms and Conditions to use our trademarks, trade names, service marks or product names.
57
+
58
+ ## Grant of Future License
59
+
60
+ We hereby irrevocably grant you an additional license to use the Software under the Apache License, Version 2.0 that is effective on the second anniversary of the date we make the Software available. On or after that date, you may use the Software under the Apache License, Version 2.0, in which case the following will apply:
61
+
62
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
63
+ You may obtain a copy of the License at
64
+
65
+ http://www.apache.org/licenses/LICENSE-2.0
66
+
67
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # RSocket Micro Router
2
+
3
+ This package wraps the RSocket library to a router format similar to the Journey Micro router.
@@ -0,0 +1,4 @@
1
+ export * from './utils/BaseObserver.js';
2
+ export * from './router/ReactiveSocketRouter.js';
3
+ export * from './router/SocketRouterListener.js';
4
+ export * from './router/types.js';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './utils/BaseObserver.js';
2
+ export * from './router/ReactiveSocketRouter.js';
3
+ export * from './router/SocketRouterListener.js';
4
+ export * from './router/types.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1,20 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import * as http from 'http';
3
+ import { Payload } from 'rsocket-core';
4
+ import { SocketRouterObserver } from './SocketRouterListener.js';
5
+ import { CommonParams, IReactiveStream, IReactiveStreamInput, ReactiveSocketRouterOptions, SocketResponder } from './types.js';
6
+ export declare class ReactiveSocketRouter<C> {
7
+ protected options?: ReactiveSocketRouterOptions<C> | undefined;
8
+ constructor(options?: ReactiveSocketRouterOptions<C> | undefined);
9
+ reactiveStream<I, O>(path: string, stream: IReactiveStreamInput<I, O, C>): IReactiveStream<I, O, C>;
10
+ /**
11
+ * Apply a set of subscriptions to a raw http server. The specified path is used to tell
12
+ * the server on which path it should handle WebSocket upgrades
13
+ */
14
+ applyWebSocketEndpoints<I>(server: http.Server, params: CommonParams<C>): Promise<import("rsocket-core").Closeable>;
15
+ }
16
+ export declare function handleReactiveStream<Context>(context: Context, request: {
17
+ payload: Payload;
18
+ initialN: number;
19
+ responder: SocketResponder;
20
+ }, observer: SocketRouterObserver, params: CommonParams<Context>): Promise<void>;
@@ -0,0 +1,126 @@
1
+ /**
2
+ * This is a small Router wrapper which uses the RSocket lib
3
+ * to expose reactive websocket stream in an interface similar to
4
+ * other journey micro routers.
5
+ */
6
+ import * as micro from '@journeyapps-platform/micro';
7
+ import { RSocketServer } from 'rsocket-core';
8
+ import { WebsocketServerTransport } from 'rsocket-websocket-server';
9
+ import * as ws from 'ws';
10
+ import { SocketRouterObserver } from './SocketRouterListener.js';
11
+ import { RS_ENDPOINT_TYPE } from './types.js';
12
+ export class ReactiveSocketRouter {
13
+ constructor(options) {
14
+ this.options = options;
15
+ }
16
+ reactiveStream(path, stream) {
17
+ return {
18
+ ...stream,
19
+ type: RS_ENDPOINT_TYPE.STREAM,
20
+ path: path
21
+ };
22
+ }
23
+ /**
24
+ * Apply a set of subscriptions to a raw http server. The specified path is used to tell
25
+ * the server on which path it should handle WebSocket upgrades
26
+ */
27
+ applyWebSocketEndpoints(server, params) {
28
+ /**
29
+ * Use upgraded connections from the existing server.
30
+ * This follows a similar pattern to the Journey Micro
31
+ * web sockets router.
32
+ */
33
+ const wss = new ws.WebSocketServer({ noServer: true });
34
+ server.on('upgrade', (request, socket, head) => {
35
+ wss.handleUpgrade(request, socket, head, (ws) => {
36
+ wss.emit('connection', ws, request);
37
+ });
38
+ });
39
+ server.on('close', () => wss.close());
40
+ const transport = new WebsocketServerTransport({
41
+ wsCreator: () => wss
42
+ });
43
+ const rSocketServer = new RSocketServer({
44
+ transport,
45
+ acceptor: {
46
+ accept: async (payload) => {
47
+ // Throwing an exception in this context will be returned to the client side request
48
+ if (!payload.metadata) {
49
+ // Meta data is required for endpoint handler path matching
50
+ throw new micro.errors.AuthorizationError('No context meta data provided');
51
+ }
52
+ const context = await params.contextProvider(payload.metadata);
53
+ return {
54
+ // RequestStream is currently the only supported connection type
55
+ requestStream: (payload, initialN, responder) => {
56
+ const observer = new SocketRouterObserver();
57
+ handleReactiveStream(context, { payload, initialN, responder }, observer, params).catch((ex) => {
58
+ micro.logger.error(ex);
59
+ responder.onError(ex);
60
+ responder.onComplete();
61
+ });
62
+ return {
63
+ cancel: () => {
64
+ observer.triggerCancel();
65
+ },
66
+ onExtension: () => observer.triggerExtension(),
67
+ request: (n) => observer.triggerRequest(n)
68
+ };
69
+ }
70
+ };
71
+ }
72
+ }
73
+ });
74
+ Promise.resolve().then(() => {
75
+ // RSocket listens for this event before accepting connections
76
+ wss.emit('listening');
77
+ });
78
+ return rSocketServer.bind();
79
+ }
80
+ }
81
+ export async function handleReactiveStream(context, request, observer, params) {
82
+ const { payload, responder, initialN } = request;
83
+ const { metadata } = payload;
84
+ const exitWithError = (error) => {
85
+ responder.onError(error);
86
+ responder.onComplete();
87
+ };
88
+ if (!metadata) {
89
+ return exitWithError(new micro.errors.ValidationError('Metadata is not provided'));
90
+ }
91
+ const meta = await params.metaDecoder(metadata);
92
+ const { path } = meta;
93
+ const route = params.endpoints.find((e) => e.path == path && e.type == RS_ENDPOINT_TYPE.STREAM);
94
+ if (!route) {
95
+ return exitWithError(new micro.errors.ResourceNotFound('route', `No route for ${path} is configured`));
96
+ }
97
+ const { handler, authorize, validator, decoder = params.payloadDecoder } = route;
98
+ const requestPayload = await decoder(payload.data || undefined);
99
+ if (validator) {
100
+ const isValid = validator.validate(requestPayload);
101
+ if (!isValid.valid) {
102
+ return exitWithError(new micro.errors.ValidationError(isValid.errors));
103
+ }
104
+ }
105
+ if (authorize) {
106
+ const isAuthorized = await authorize({ params: requestPayload, context, observer, responder });
107
+ if (!isAuthorized.authorized) {
108
+ return exitWithError(new micro.errors.AuthorizationError(isAuthorized.errors));
109
+ }
110
+ }
111
+ try {
112
+ await handler({
113
+ params: requestPayload,
114
+ context,
115
+ observer,
116
+ responder,
117
+ initialN
118
+ });
119
+ }
120
+ catch (ex) {
121
+ micro.logger.error(ex);
122
+ responder.onError(ex);
123
+ responder.onComplete();
124
+ }
125
+ }
126
+ //# sourceMappingURL=ReactiveSocketRouter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReactiveSocketRouter.js","sourceRoot":"","sources":["../../src/router/ReactiveSocketRouter.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,KAAK,MAAM,6BAA6B,CAAC;AAErD,OAAO,EAAW,aAAa,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAIL,gBAAgB,EAGjB,MAAM,YAAY,CAAC;AAEpB,MAAM,OAAO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AAC,CAAC;QACzE,CAAC;IACH,CAAC;IAED,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,YAAY,GAAG,MAAM,SAAS,CAAC,EAAE,MAAM,EAAE,cAAc,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,CAAC;QAC/F,IAAI,CAAC,YAAY,CAAC,UAAU,EAAE,CAAC;YAC7B,OAAO,aAAa,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,kBAAkB,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;QACjF,CAAC;IACH,CAAC;IAED,IAAI,CAAC;QACH,MAAM,OAAO,CAAC;YACZ,MAAM,EAAE,cAAc;YACtB,OAAO;YACP,QAAQ;YACR,SAAS;YACT,QAAQ;SACT,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,EAAE,EAAE,CAAC;QACZ,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACvB,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACtB,SAAS,CAAC,UAAU,EAAE,CAAC;IACzB,CAAC;AACH,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { BaseObserver } from '../utils/BaseObserver.js';
2
+ export interface SocketRouterListener {
3
+ cancel: () => void;
4
+ onExtension: () => void;
5
+ request: (n: number) => void;
6
+ }
7
+ export declare class SocketRouterObserver extends BaseObserver<SocketRouterListener> {
8
+ triggerCancel(): void;
9
+ triggerExtension(): void;
10
+ triggerRequest(n: number): void;
11
+ }
@@ -0,0 +1,13 @@
1
+ import { BaseObserver } from '../utils/BaseObserver.js';
2
+ export class SocketRouterObserver extends BaseObserver {
3
+ triggerCancel() {
4
+ this.iterateListeners((l) => l.cancel?.());
5
+ }
6
+ triggerExtension() {
7
+ this.iterateListeners((l) => l.onExtension?.());
8
+ }
9
+ triggerRequest(n) {
10
+ this.iterateListeners((l) => l.request?.(n));
11
+ }
12
+ }
13
+ //# sourceMappingURL=SocketRouterListener.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SocketRouterListener.js","sourceRoot":"","sources":["../../src/router/SocketRouterListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAQxD,MAAM,OAAO,oBAAqB,SAAQ,YAAkC;IAC1E,aAAa;QACX,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,gBAAgB;QACd,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;IAClD,CAAC;IAED,cAAc,CAAC,CAAS;QACtB,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/C,CAAC;CACF"}
@@ -0,0 +1,37 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import * as micro_router from '@journeyapps-platform/micro-router';
3
+ import * as t from 'ts-codec';
4
+ import { OnExtensionSubscriber, OnNextSubscriber, OnTerminalSubscriber } from 'rsocket-core';
5
+ import { SocketRouterObserver } from './SocketRouterListener.js';
6
+ export declare enum RS_ENDPOINT_TYPE {
7
+ STREAM = "stream"
8
+ }
9
+ export declare const RSocketRequestMeta: t.ObjectCodec<{
10
+ path: t.IdentityCodec<t.CodecType.String>;
11
+ }>;
12
+ export type RequestMeta = t.Decoded<typeof RSocketRequestMeta>;
13
+ export type ReactiveSocketRouterOptions<C> = {};
14
+ export type SocketResponder = OnTerminalSubscriber & OnNextSubscriber & OnExtensionSubscriber;
15
+ export type CommonStreamPayload = {
16
+ observer: SocketRouterObserver;
17
+ responder: SocketResponder;
18
+ };
19
+ export type ReactiveStreamPayload<O> = CommonStreamPayload & {
20
+ initialN: number;
21
+ };
22
+ export type IReactiveStream<I = any, O = any, C = any> = micro_router.Endpoint<I, O, C, micro_router.EndpointHandlerPayload<I, C> & CommonStreamPayload, micro_router.EndpointHandler<micro_router.EndpointHandlerPayload<I, C> & ReactiveStreamPayload<O>, undefined>> & {
23
+ type: RS_ENDPOINT_TYPE.STREAM;
24
+ /**
25
+ * Decodes raw payload buffer to [I].
26
+ * Falls back to router level decoder if not specified.
27
+ */
28
+ decoder?: (rawData?: Buffer) => Promise<I>;
29
+ };
30
+ export type IReactiveStreamInput<I, O, C> = Omit<IReactiveStream<I, O, C>, 'path' | 'type'>;
31
+ export type ReactiveEndpoint = IReactiveStream;
32
+ export type CommonParams<C> = {
33
+ endpoints: Array<ReactiveEndpoint>;
34
+ contextProvider: (metaData: Buffer) => Promise<C>;
35
+ metaDecoder: (meta: Buffer) => Promise<RequestMeta>;
36
+ payloadDecoder: (rawData?: Buffer) => Promise<any>;
37
+ };
@@ -0,0 +1,10 @@
1
+ import * as t from 'ts-codec';
2
+ export var RS_ENDPOINT_TYPE;
3
+ (function (RS_ENDPOINT_TYPE) {
4
+ // Other methods are supported by RSocket, but are not yet mapped here
5
+ RS_ENDPOINT_TYPE["STREAM"] = "stream";
6
+ })(RS_ENDPOINT_TYPE || (RS_ENDPOINT_TYPE = {}));
7
+ export const RSocketRequestMeta = t.object({
8
+ path: t.string
9
+ });
10
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/router/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAI9B,MAAM,CAAN,IAAY,gBAGX;AAHD,WAAY,gBAAgB;IAC1B,sEAAsE;IACtE,qCAAiB,CAAA;AACnB,CAAC,EAHW,gBAAgB,KAAhB,gBAAgB,QAG3B;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC;IACzC,IAAI,EAAE,CAAC,CAAC,MAAM;CACf,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ export declare class BaseObserver<T> {
2
+ protected listeners: {
3
+ [id: string]: Partial<T>;
4
+ };
5
+ constructor();
6
+ registerListener(listener: Partial<T>): () => void;
7
+ iterateListeners(cb: (listener: Partial<T>) => any): void;
8
+ iterateAsyncListeners(cb: (listener: Partial<T>) => Promise<any>): Promise<void>;
9
+ }
@@ -0,0 +1,24 @@
1
+ import { v4 as uuid } from 'uuid';
2
+ export class BaseObserver {
3
+ constructor() {
4
+ this.listeners = {};
5
+ }
6
+ registerListener(listener) {
7
+ const id = uuid();
8
+ this.listeners[id] = listener;
9
+ return () => {
10
+ delete this.listeners[id];
11
+ };
12
+ }
13
+ iterateListeners(cb) {
14
+ for (let i in this.listeners) {
15
+ cb(this.listeners[i]);
16
+ }
17
+ }
18
+ async iterateAsyncListeners(cb) {
19
+ for (let i in this.listeners) {
20
+ await cb(this.listeners[i]);
21
+ }
22
+ }
23
+ }
24
+ //# sourceMappingURL=BaseObserver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BaseObserver.js","sourceRoot":"","sources":["../../src/utils/BaseObserver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,MAAM,OAAO,YAAY;IAGvB;QACE,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;IACtB,CAAC;IAED,gBAAgB,CAAC,QAAoB;QACnC,MAAM,EAAE,GAAG,IAAI,EAAE,CAAC;QAClB,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC;QAC9B,OAAO,GAAG,EAAE;YACV,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC;IAED,gBAAgB,CAAC,EAAiC;QAChD,KAAK,IAAI,CAAC,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YAC7B,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,qBAAqB,CAAC,EAA0C;QACpE,KAAK,IAAI,CAAC,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YAC7B,MAAM,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@powersync/service-rsocket-router",
3
+ "repository": "https://github.com/powersync-ja/powersync-service",
4
+ "version": "0.0.6",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "license": "FSL-1.1-Apache-2.0",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "files": [
12
+ "dist/**/*"
13
+ ],
14
+ "type": "module",
15
+ "dependencies": {
16
+ "rsocket-core": "1.0.0-alpha.3",
17
+ "rsocket-websocket-server": "1.0.0-alpha.3",
18
+ "@journeyapps-platform/micro": "^16.1.0",
19
+ "@journeyapps-platform/micro-authorizers": "^5.2.0",
20
+ "@journeyapps-platform/micro-migrate": "^3.1.0",
21
+ "@journeyapps-platform/micro-router": "^7.0.0",
22
+ "ts-codec": "^1.2.2",
23
+ "uuid": "^9.0.1",
24
+ "ws": "~8.2.3"
25
+ },
26
+ "devDependencies": {
27
+ "@types/uuid": "^9.0.4",
28
+ "@types/ws": "~8.2.0",
29
+ "bson": "^6.6.0",
30
+ "typescript": "^5.2.2",
31
+ "vitest": "^0.34.6"
32
+ },
33
+ "scripts": {
34
+ "clean": "rm -r ./dist && tsc -b --clean",
35
+ "build": "tsc -b",
36
+ "test": "vitest"
37
+ }
38
+ }