@ariva-mds/mds 2.0.0 → 2.2.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/lib/cjs/index.js CHANGED
@@ -1,17 +1,156 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.goodBye = exports.helloWorld = void 0;
4
- function helloWorld() {
5
- const message = 'Hello World from my example modern npm package!';
6
- return message;
6
+ exports.MdsConnection = exports.MarketstateUpdate = exports.MdsAuthdata = void 0;
7
+ const reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket"));
8
+ const rxjs_1 = require("rxjs");
9
+ class MdsAuthdata {
10
+ constructor(token) {
11
+ this.token = token;
12
+ }
7
13
  }
8
- exports.helloWorld = helloWorld;
9
- function goodBye() {
10
- const message = 'Goodbye from my example modern npm package!';
11
- return message;
14
+ exports.MdsAuthdata = MdsAuthdata;
15
+ class MarketstateUpdate {
16
+ constructor(state, update) {
17
+ this.state = state;
18
+ this.update = update;
19
+ }
12
20
  }
13
- exports.goodBye = goodBye;
14
- exports.default = {
15
- helloWorld,
16
- goodBye,
17
- };
21
+ exports.MarketstateUpdate = MarketstateUpdate;
22
+ class MdsConnection {
23
+ constructor(websocketUrl, authdataCallback, options = {}) {
24
+ this.counter = 0;
25
+ this.runningRequestsObservable = new Map();
26
+ this.runningRequestsPromise = new Map();
27
+ this.authdataCallback = authdataCallback;
28
+ this.websocketUrl = websocketUrl;
29
+ this.websocket = new reconnecting_websocket_1.default(this.websocketUrl, [], options.wsOptions);
30
+ this.stayAuthenticated();
31
+ this.websocket.onopen = function () {
32
+ console.log("connected");
33
+ };
34
+ this.websocket.onclose = function () {
35
+ console.log("close");
36
+ };
37
+ const outer = this;
38
+ this.websocket.onmessage = function (e) {
39
+ outer.processWebsocketMessageEvent(e);
40
+ };
41
+ }
42
+ heartbeat() {
43
+ return this.observable({ 'heartbeat': {} })
44
+ .pipe((0, rxjs_1.map)((x) => x.dataHeartbeat));
45
+ }
46
+ marketstates(marketstateQueries) {
47
+ const full = new Map();
48
+ return this.marketstateUpdates(marketstateQueries)
49
+ .pipe((0, rxjs_1.map)((update) => {
50
+ let existingEntry = full.get(update.marketstateId);
51
+ if (existingEntry) {
52
+ for (let key in update) {
53
+ existingEntry[key] = update[key];
54
+ }
55
+ }
56
+ else {
57
+ existingEntry = update;
58
+ full.set(update.marketstateId, existingEntry);
59
+ }
60
+ return new MarketstateUpdate(existingEntry, update);
61
+ }));
62
+ }
63
+ marketstateUpdates(marketstateQueries) {
64
+ return this.observable({ 'subscribeMarketstates': { 'marketstateQueries': marketstateQueries } })
65
+ .pipe((0, rxjs_1.map)((x) => x.dataMarketstate));
66
+ }
67
+ marketstatesStates(marketstateQueries) {
68
+ return this.marketstates(marketstateQueries).pipe((0, rxjs_1.map)((x) => x.state));
69
+ }
70
+ priority(sources) {
71
+ return this.promise({ 'priority': { 'sources': sources } });
72
+ }
73
+ stayAuthenticated() {
74
+ const outer = this;
75
+ new rxjs_1.Observable((subscriber) => {
76
+ this.authdataCallback().then((mdsAuthdata) => outer.observable({ 'subscribeAuthentication': mdsAuthdata })).then((observable) => observable.subscribe({
77
+ next(x) {
78
+ subscriber.next(x);
79
+ },
80
+ complete() {
81
+ subscriber.complete();
82
+ },
83
+ error() {
84
+ subscriber.complete();
85
+ }
86
+ }));
87
+ })
88
+ .pipe((0, rxjs_1.repeat)({ delay: 2000 }))
89
+ .subscribe((c) => console.log("auth outer " + c));
90
+ }
91
+ generateNextRequestId() {
92
+ return "request-" + this.counter++;
93
+ }
94
+ observable(req) {
95
+ const requestId = this.generateNextRequestId();
96
+ req.requestId = requestId;
97
+ const outer = this;
98
+ const observable = new rxjs_1.Observable((subscriber) => {
99
+ outer.runningRequestsObservable.set(requestId, subscriber);
100
+ outer.websocket.send(JSON.stringify(req));
101
+ // Provide a way of canceling and disposing the resources
102
+ return function unsubscribe() {
103
+ if (outer.runningRequestsObservable.has(requestId)) {
104
+ outer.runningRequestsObservable.delete(requestId);
105
+ outer.websocket.send(JSON.stringify({
106
+ 'cancel': { 'requestId': requestId }
107
+ }));
108
+ }
109
+ };
110
+ });
111
+ return observable;
112
+ }
113
+ promise(req) {
114
+ const requestId = this.generateNextRequestId();
115
+ req.requestId = requestId;
116
+ const outer = this;
117
+ const promise = new Promise((resolve, reject) => {
118
+ outer.runningRequestsPromise.set(requestId, { 'resolve': resolve, 'reject': reject });
119
+ outer.websocket.send(JSON.stringify(req));
120
+ });
121
+ return promise;
122
+ }
123
+ processWebsocketMessageEvent(e) {
124
+ const msg = JSON.parse(e.data);
125
+ const promise = this.runningRequestsPromise.get(msg.requestId);
126
+ const subscriber = this.runningRequestsObservable.get(msg.requestId);
127
+ if (promise) {
128
+ if (msg.isError) {
129
+ promise.reject(msg.errorMessage);
130
+ this.runningRequestsPromise.delete(msg.requestId);
131
+ }
132
+ else if (msg.isComplete == true) {
133
+ promise.resolve(msg);
134
+ this.runningRequestsPromise.delete(msg.requestId);
135
+ }
136
+ else {
137
+ promise.reject("strange message " + msg);
138
+ this.runningRequestsPromise.delete(msg.requestId);
139
+ }
140
+ }
141
+ else if (subscriber) {
142
+ if (msg.isError) {
143
+ this.runningRequestsObservable.delete(msg.requestId);
144
+ subscriber.error(msg.errorMessage);
145
+ }
146
+ else if (msg.isComplete == true) {
147
+ this.runningRequestsObservable.delete(msg.requestId);
148
+ subscriber.complete();
149
+ }
150
+ else {
151
+ subscriber.next(msg);
152
+ }
153
+ }
154
+ }
155
+ }
156
+ exports.MdsConnection = MdsConnection;
@@ -1,8 +1,34 @@
1
- export declare function helloWorld(): string;
2
- export declare function goodBye(): string;
3
- declare const _default: {
4
- helloWorld: typeof helloWorld;
5
- goodBye: typeof goodBye;
1
+ import { Observable } from 'rxjs';
2
+ import { Options as ReconnectingWebSocketOptions } from 'reconnecting-websocket';
3
+ export declare class MdsAuthdata {
4
+ token: string;
5
+ constructor(token: string);
6
+ }
7
+ export declare class MarketstateUpdate {
8
+ state: any;
9
+ update: any;
10
+ constructor(state: any, update: any);
11
+ }
12
+ export type Options = {
13
+ wsOptions?: ReconnectingWebSocketOptions;
6
14
  };
7
- export default _default;
15
+ export declare class MdsConnection {
16
+ private websocketUrl;
17
+ private websocket;
18
+ private counter;
19
+ private readonly runningRequestsObservable;
20
+ private readonly runningRequestsPromise;
21
+ private readonly authdataCallback;
22
+ constructor(websocketUrl: string, authdataCallback: () => Promise<MdsAuthdata>, options?: Options);
23
+ heartbeat(): Observable<String>;
24
+ marketstates(marketstateQueries: String[]): Observable<MarketstateUpdate>;
25
+ marketstateUpdates(marketstateQueries: String[]): Observable<any>;
26
+ marketstatesStates(marketstateQueries: String[]): Observable<any>;
27
+ priority(sources: string[]): Promise<any>;
28
+ private stayAuthenticated;
29
+ private generateNextRequestId;
30
+ private observable;
31
+ private promise;
32
+ private processWebsocketMessageEvent;
33
+ }
8
34
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,WAGzB;AAED,wBAAgB,OAAO,WAGtB;;;;;AAED,wBAGE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAM,UAAU,EAAqB,MAAM,MAAM,CAAC;AACzD,OAAO,EAAC,OAAO,IAAI,4BAA4B,EAAC,MAAM,wBAAwB,CAAC;AAE/E,qBAAa,WAAW;IAEb,KAAK,EAAE,MAAM,CAAC;gBAET,KAAK,EAAE,MAAM;CAI5B;AAED,qBAAa,iBAAiB;IAEnB,KAAK,EAAE,GAAG,CAAC;IACX,MAAM,EAAG,GAAG,CAAA;gBAEP,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG;CAKtC;AACD,MAAM,MAAM,OAAO,GAAG;IAClB,SAAS,CAAC,EAAE,4BAA4B,CAAC;CAC5C,CAAC;AAEF,qBAAa,aAAa;IAEtB,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,SAAS,CAAwB;IAEzC,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAA2C;IACrF,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAA+B;IAEtE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA6B;gBAElD,YAAY,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,EAAE,OAAO,GAAE,OAAY;IAsB9F,SAAS,IAAI,UAAU,CAAC,MAAM,CAAC;IAK/B,YAAY,CAAC,kBAAkB,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,iBAAiB,CAAC;IAkBzE,kBAAkB,CAAC,kBAAkB,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,CAAC;IAKjE,kBAAkB,CAAC,kBAAkB,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,CAAC;IAMjE,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC;IAQhD,OAAO,CAAC,iBAAiB;IA4BzB,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,UAAU;IA0BlB,OAAO,CAAC,OAAO;IAiBf,OAAO,CAAC,4BAA4B;CA6BvC"}
package/lib/esm/index.mjs CHANGED
@@ -1,12 +1,153 @@
1
- export function helloWorld() {
2
- const message = 'Hello World from my example modern npm package!';
3
- return message;
1
+ import ReconnectingWebSocket from 'reconnecting-websocket';
2
+ import { map, Observable, repeat } from 'rxjs';
3
+ export class MdsAuthdata {
4
+ token;
5
+ constructor(token) {
6
+ this.token = token;
7
+ }
4
8
  }
5
- export function goodBye() {
6
- const message = 'Goodbye from my example modern npm package!';
7
- return message;
9
+ export class MarketstateUpdate {
10
+ state;
11
+ update;
12
+ constructor(state, update) {
13
+ this.state = state;
14
+ this.update = update;
15
+ }
16
+ }
17
+ export class MdsConnection {
18
+ websocketUrl;
19
+ websocket;
20
+ counter = 0;
21
+ runningRequestsObservable = new Map();
22
+ runningRequestsPromise = new Map();
23
+ authdataCallback;
24
+ constructor(websocketUrl, authdataCallback, options = {}) {
25
+ this.authdataCallback = authdataCallback;
26
+ this.websocketUrl = websocketUrl;
27
+ this.websocket = new ReconnectingWebSocket(this.websocketUrl, [], options.wsOptions);
28
+ this.stayAuthenticated();
29
+ this.websocket.onopen = function () {
30
+ console.log("connected");
31
+ };
32
+ this.websocket.onclose = function () {
33
+ console.log("close");
34
+ };
35
+ const outer = this;
36
+ this.websocket.onmessage = function (e) {
37
+ outer.processWebsocketMessageEvent(e);
38
+ };
39
+ }
40
+ heartbeat() {
41
+ return this.observable({ 'heartbeat': {} })
42
+ .pipe(map((x) => x.dataHeartbeat));
43
+ }
44
+ marketstates(marketstateQueries) {
45
+ const full = new Map();
46
+ return this.marketstateUpdates(marketstateQueries)
47
+ .pipe(map((update) => {
48
+ let existingEntry = full.get(update.marketstateId);
49
+ if (existingEntry) {
50
+ for (let key in update) {
51
+ existingEntry[key] = update[key];
52
+ }
53
+ }
54
+ else {
55
+ existingEntry = update;
56
+ full.set(update.marketstateId, existingEntry);
57
+ }
58
+ return new MarketstateUpdate(existingEntry, update);
59
+ }));
60
+ }
61
+ marketstateUpdates(marketstateQueries) {
62
+ return this.observable({ 'subscribeMarketstates': { 'marketstateQueries': marketstateQueries } })
63
+ .pipe(map((x) => x.dataMarketstate));
64
+ }
65
+ marketstatesStates(marketstateQueries) {
66
+ return this.marketstates(marketstateQueries).pipe(map((x) => x.state));
67
+ }
68
+ priority(sources) {
69
+ return this.promise({ 'priority': { 'sources': sources } });
70
+ }
71
+ stayAuthenticated() {
72
+ const outer = this;
73
+ new Observable((subscriber) => {
74
+ this.authdataCallback().then((mdsAuthdata) => outer.observable({ 'subscribeAuthentication': mdsAuthdata })).then((observable) => observable.subscribe({
75
+ next(x) {
76
+ subscriber.next(x);
77
+ },
78
+ complete() {
79
+ subscriber.complete();
80
+ },
81
+ error() {
82
+ subscriber.complete();
83
+ }
84
+ }));
85
+ })
86
+ .pipe(repeat({ delay: 2000 }))
87
+ .subscribe((c) => console.log("auth outer " + c));
88
+ }
89
+ generateNextRequestId() {
90
+ return "request-" + this.counter++;
91
+ }
92
+ observable(req) {
93
+ const requestId = this.generateNextRequestId();
94
+ req.requestId = requestId;
95
+ const outer = this;
96
+ const observable = new Observable((subscriber) => {
97
+ outer.runningRequestsObservable.set(requestId, subscriber);
98
+ outer.websocket.send(JSON.stringify(req));
99
+ // Provide a way of canceling and disposing the resources
100
+ return function unsubscribe() {
101
+ if (outer.runningRequestsObservable.has(requestId)) {
102
+ outer.runningRequestsObservable.delete(requestId);
103
+ outer.websocket.send(JSON.stringify({
104
+ 'cancel': { 'requestId': requestId }
105
+ }));
106
+ }
107
+ };
108
+ });
109
+ return observable;
110
+ }
111
+ promise(req) {
112
+ const requestId = this.generateNextRequestId();
113
+ req.requestId = requestId;
114
+ const outer = this;
115
+ const promise = new Promise((resolve, reject) => {
116
+ outer.runningRequestsPromise.set(requestId, { 'resolve': resolve, 'reject': reject });
117
+ outer.websocket.send(JSON.stringify(req));
118
+ });
119
+ return promise;
120
+ }
121
+ processWebsocketMessageEvent(e) {
122
+ const msg = JSON.parse(e.data);
123
+ const promise = this.runningRequestsPromise.get(msg.requestId);
124
+ const subscriber = this.runningRequestsObservable.get(msg.requestId);
125
+ if (promise) {
126
+ if (msg.isError) {
127
+ promise.reject(msg.errorMessage);
128
+ this.runningRequestsPromise.delete(msg.requestId);
129
+ }
130
+ else if (msg.isComplete == true) {
131
+ promise.resolve(msg);
132
+ this.runningRequestsPromise.delete(msg.requestId);
133
+ }
134
+ else {
135
+ promise.reject("strange message " + msg);
136
+ this.runningRequestsPromise.delete(msg.requestId);
137
+ }
138
+ }
139
+ else if (subscriber) {
140
+ if (msg.isError) {
141
+ this.runningRequestsObservable.delete(msg.requestId);
142
+ subscriber.error(msg.errorMessage);
143
+ }
144
+ else if (msg.isComplete == true) {
145
+ this.runningRequestsObservable.delete(msg.requestId);
146
+ subscriber.complete();
147
+ }
148
+ else {
149
+ subscriber.next(msg);
150
+ }
151
+ }
152
+ }
8
153
  }
9
- export default {
10
- helloWorld,
11
- goodBye,
12
- };
@@ -1,8 +1,34 @@
1
- export declare function helloWorld(): string;
2
- export declare function goodBye(): string;
3
- declare const _default: {
4
- helloWorld: typeof helloWorld;
5
- goodBye: typeof goodBye;
1
+ import { Observable } from 'rxjs';
2
+ import { Options as ReconnectingWebSocketOptions } from 'reconnecting-websocket';
3
+ export declare class MdsAuthdata {
4
+ token: string;
5
+ constructor(token: string);
6
+ }
7
+ export declare class MarketstateUpdate {
8
+ state: any;
9
+ update: any;
10
+ constructor(state: any, update: any);
11
+ }
12
+ export type Options = {
13
+ wsOptions?: ReconnectingWebSocketOptions;
6
14
  };
7
- export default _default;
15
+ export declare class MdsConnection {
16
+ private websocketUrl;
17
+ private websocket;
18
+ private counter;
19
+ private readonly runningRequestsObservable;
20
+ private readonly runningRequestsPromise;
21
+ private readonly authdataCallback;
22
+ constructor(websocketUrl: string, authdataCallback: () => Promise<MdsAuthdata>, options?: Options);
23
+ heartbeat(): Observable<String>;
24
+ marketstates(marketstateQueries: String[]): Observable<MarketstateUpdate>;
25
+ marketstateUpdates(marketstateQueries: String[]): Observable<any>;
26
+ marketstatesStates(marketstateQueries: String[]): Observable<any>;
27
+ priority(sources: string[]): Promise<any>;
28
+ private stayAuthenticated;
29
+ private generateNextRequestId;
30
+ private observable;
31
+ private promise;
32
+ private processWebsocketMessageEvent;
33
+ }
8
34
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,WAGzB;AAED,wBAAgB,OAAO,WAGtB;;;;;AAED,wBAGE"}
1
+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AAC,qBAAqB;IAI7B,OAAO,CAAC,UAAU;IA0BlB,OAAO,CAAC,OAAO;IAiBf,OAAO,CAAC,4BAA4B;CA6BvC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ariva-mds/mds",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "Stock market data",
5
5
  "license": "MIT",
6
6
  "main": "./lib/cjs/index.js",
@@ -13,7 +13,8 @@
13
13
  "build": "npm run clean && npm run build:esm && npm run build:cjs",
14
14
  "build:esm": "tsc -p ./configs/tsconfig.esm.json && mv lib/esm/index.js lib/esm/index.mjs",
15
15
  "build:cjs": "tsc -p ./configs/tsconfig.cjs.json",
16
- "prepack": "npm run build"
16
+ "prepack": "npm run build",
17
+ "test": "mocha"
17
18
  },
18
19
  "files": [
19
20
  "lib/**/*"
@@ -32,6 +33,18 @@
32
33
  }
33
34
  },
34
35
  "dependencies": {
35
- "typescript": "^4.9.5"
36
+ "node-fetch": "^3.3.0",
37
+ "reconnecting-websocket": "^4.4.0",
38
+ "rxjs": "^7.8.0",
39
+ "typescript": "^4.9.5",
40
+ "ws": "^8.12.0"
41
+ },
42
+ "devDependencies": {
43
+ "@types/chai": "^4.3.4",
44
+ "@types/mocha": "^10.0.1",
45
+ "@types/node": "^18.11.18",
46
+ "chai": "^4.3.7",
47
+ "mocha": "^10.2.0",
48
+ "ts-node": "^10.9.1"
36
49
  }
37
50
  }