oorja 1.5.1 → 1.6.3

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.
Files changed (46) hide show
  1. package/README.md +4 -7
  2. package/lib/commands/signout.js +2 -2
  3. package/lib/commands/teletype/index.js +3 -3
  4. package/lib/lib/config.d.ts +5 -6
  5. package/lib/lib/config.js +12 -7
  6. package/lib/lib/encryption.d.ts +1 -1
  7. package/lib/lib/encryption.js +12 -7
  8. package/lib/lib/index.js +1 -0
  9. package/lib/lib/oorja/index.js +14 -13
  10. package/lib/lib/oorja/preflight.d.ts +1 -1
  11. package/lib/lib/oorja/preflight.js +17 -11
  12. package/lib/lib/surya/index.d.ts +2 -2
  13. package/lib/lib/surya/index.js +15 -13
  14. package/lib/lib/surya/types.d.ts +10 -10
  15. package/lib/lib/surya/vendor/phoenix/ajax.d.ts +8 -0
  16. package/lib/lib/surya/vendor/phoenix/ajax.js +85 -0
  17. package/lib/lib/surya/vendor/phoenix/channel.d.ts +154 -0
  18. package/lib/lib/surya/vendor/phoenix/channel.js +311 -0
  19. package/lib/lib/surya/vendor/phoenix/constants.d.ts +33 -0
  20. package/lib/lib/surya/vendor/phoenix/constants.js +32 -0
  21. package/lib/lib/surya/vendor/phoenix/index.d.ts +199 -0
  22. package/lib/lib/surya/vendor/phoenix/index.js +207 -0
  23. package/lib/lib/surya/vendor/phoenix/longpoll.d.ts +12 -0
  24. package/lib/lib/surya/vendor/phoenix/longpoll.js +129 -0
  25. package/lib/lib/surya/vendor/phoenix/presence.d.ts +44 -0
  26. package/lib/lib/surya/vendor/phoenix/presence.js +155 -0
  27. package/lib/lib/surya/vendor/phoenix/push.d.ts +57 -0
  28. package/lib/lib/surya/vendor/phoenix/push.js +125 -0
  29. package/lib/lib/surya/vendor/phoenix/serializer.d.ts +53 -0
  30. package/lib/lib/surya/vendor/phoenix/serializer.js +102 -0
  31. package/lib/lib/surya/vendor/phoenix/socket.d.ts +222 -0
  32. package/lib/lib/surya/vendor/phoenix/socket.js +544 -0
  33. package/lib/lib/surya/vendor/phoenix/timer.d.ts +25 -0
  34. package/lib/lib/surya/vendor/phoenix/timer.js +43 -0
  35. package/lib/lib/surya/vendor/phoenix/utils.d.ts +1 -0
  36. package/lib/lib/surya/vendor/phoenix/utils.js +15 -0
  37. package/lib/lib/teletype/auxiliary.d.ts +1 -1
  38. package/lib/lib/teletype/auxiliary.js +8 -4
  39. package/lib/lib/teletype/index.d.ts +1 -1
  40. package/lib/lib/teletype/index.js +13 -12
  41. package/lib/lib/utils.js +2 -1
  42. package/oclif.manifest.json +1 -1
  43. package/package.json +18 -10
  44. package/NOTICE.txt +0 -5
  45. package/lib/lib/surya/vendor/phoenix.d.ts +0 -486
  46. package/lib/lib/surya/vendor/phoenix.js +0 -1299
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // @ts-nocheck
4
+ /**
5
+ * Initializes the Presence
6
+ * @param {Channel} channel - The Channel
7
+ * @param {Object} opts - The options,
8
+ * for example `{events: {state: "state", diff: "diff"}}`
9
+ */
10
+ class Presence {
11
+ constructor(channel, opts = {}) {
12
+ let events = opts.events || { state: "presence_state", diff: "presence_diff" };
13
+ this.state = {};
14
+ this.pendingDiffs = [];
15
+ this.channel = channel;
16
+ this.joinRef = null;
17
+ this.caller = {
18
+ onJoin: function () { },
19
+ onLeave: function () { },
20
+ onSync: function () { }
21
+ };
22
+ this.channel.on(events.state, newState => {
23
+ let { onJoin, onLeave, onSync } = this.caller;
24
+ this.joinRef = this.channel.joinRef();
25
+ this.state = Presence.syncState(this.state, newState, onJoin, onLeave);
26
+ this.pendingDiffs.forEach(diff => {
27
+ this.state = Presence.syncDiff(this.state, diff, onJoin, onLeave);
28
+ });
29
+ this.pendingDiffs = [];
30
+ onSync();
31
+ });
32
+ this.channel.on(events.diff, diff => {
33
+ let { onJoin, onLeave, onSync } = this.caller;
34
+ if (this.inPendingSyncState()) {
35
+ this.pendingDiffs.push(diff);
36
+ }
37
+ else {
38
+ this.state = Presence.syncDiff(this.state, diff, onJoin, onLeave);
39
+ onSync();
40
+ }
41
+ });
42
+ }
43
+ onJoin(callback) { this.caller.onJoin = callback; }
44
+ onLeave(callback) { this.caller.onLeave = callback; }
45
+ onSync(callback) { this.caller.onSync = callback; }
46
+ list(by) { return Presence.list(this.state, by); }
47
+ inPendingSyncState() {
48
+ return !this.joinRef || (this.joinRef !== this.channel.joinRef());
49
+ }
50
+ // lower-level public static API
51
+ /**
52
+ * Used to sync the list of presences on the server
53
+ * with the client's state. An optional `onJoin` and `onLeave` callback can
54
+ * be provided to react to changes in the client's local presences across
55
+ * disconnects and reconnects with the server.
56
+ *
57
+ * @returns {Presence}
58
+ */
59
+ static syncState(currentState, newState, onJoin, onLeave) {
60
+ let state = this.clone(currentState);
61
+ let joins = {};
62
+ let leaves = {};
63
+ this.map(state, (key, presence) => {
64
+ if (!newState[key]) {
65
+ leaves[key] = presence;
66
+ }
67
+ });
68
+ this.map(newState, (key, newPresence) => {
69
+ let currentPresence = state[key];
70
+ if (currentPresence) {
71
+ let newRefs = newPresence.metas.map(m => m.phx_ref);
72
+ let curRefs = currentPresence.metas.map(m => m.phx_ref);
73
+ let joinedMetas = newPresence.metas.filter(m => curRefs.indexOf(m.phx_ref) < 0);
74
+ let leftMetas = currentPresence.metas.filter(m => newRefs.indexOf(m.phx_ref) < 0);
75
+ if (joinedMetas.length > 0) {
76
+ joins[key] = newPresence;
77
+ joins[key].metas = joinedMetas;
78
+ }
79
+ if (leftMetas.length > 0) {
80
+ leaves[key] = this.clone(currentPresence);
81
+ leaves[key].metas = leftMetas;
82
+ }
83
+ }
84
+ else {
85
+ joins[key] = newPresence;
86
+ }
87
+ });
88
+ return this.syncDiff(state, { joins: joins, leaves: leaves }, onJoin, onLeave);
89
+ }
90
+ /**
91
+ *
92
+ * Used to sync a diff of presence join and leave
93
+ * events from the server, as they happen. Like `syncState`, `syncDiff`
94
+ * accepts optional `onJoin` and `onLeave` callbacks to react to a user
95
+ * joining or leaving from a device.
96
+ *
97
+ * @returns {Presence}
98
+ */
99
+ static syncDiff(state, diff, onJoin, onLeave) {
100
+ let { joins, leaves } = this.clone(diff);
101
+ if (!onJoin) {
102
+ onJoin = function () { };
103
+ }
104
+ if (!onLeave) {
105
+ onLeave = function () { };
106
+ }
107
+ this.map(joins, (key, newPresence) => {
108
+ let currentPresence = state[key];
109
+ state[key] = this.clone(newPresence);
110
+ if (currentPresence) {
111
+ let joinedRefs = state[key].metas.map(m => m.phx_ref);
112
+ let curMetas = currentPresence.metas.filter(m => joinedRefs.indexOf(m.phx_ref) < 0);
113
+ state[key].metas.unshift(...curMetas);
114
+ }
115
+ onJoin(key, currentPresence, newPresence);
116
+ });
117
+ this.map(leaves, (key, leftPresence) => {
118
+ let currentPresence = state[key];
119
+ if (!currentPresence) {
120
+ return;
121
+ }
122
+ let refsToRemove = leftPresence.metas.map(m => m.phx_ref);
123
+ currentPresence.metas = currentPresence.metas.filter(p => {
124
+ return refsToRemove.indexOf(p.phx_ref) < 0;
125
+ });
126
+ onLeave(key, currentPresence, leftPresence);
127
+ if (currentPresence.metas.length === 0) {
128
+ delete state[key];
129
+ }
130
+ });
131
+ return state;
132
+ }
133
+ /**
134
+ * Returns the array of presences, with selected metadata.
135
+ *
136
+ * @param {Object} presences
137
+ * @param {Function} chooser
138
+ *
139
+ * @returns {Presence}
140
+ */
141
+ static list(presences, chooser) {
142
+ if (!chooser) {
143
+ chooser = function (key, pres) { return pres; };
144
+ }
145
+ return this.map(presences, (key, presence) => {
146
+ return chooser(key, presence);
147
+ });
148
+ }
149
+ // private
150
+ static map(obj, func) {
151
+ return Object.getOwnPropertyNames(obj).map(key => func(key, obj[key]));
152
+ }
153
+ static clone(obj) { return JSON.parse(JSON.stringify(obj)); }
154
+ }
155
+ exports.default = Presence;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Initializes the Push
3
+ * @param {Channel} channel - The Channel
4
+ * @param {string} event - The event, for example `"phx_join"`
5
+ * @param {Object} payload - The payload, for example `{user_id: 123}`
6
+ * @param {number} timeout - The push timeout in milliseconds
7
+ */
8
+ export default class Push {
9
+ constructor(channel: any, event: any, payload: any, timeout: any);
10
+ /**
11
+ *
12
+ * @param {number} timeout
13
+ */
14
+ resend(timeout: any): void;
15
+ /**
16
+ *
17
+ */
18
+ send(): void;
19
+ /**
20
+ *
21
+ * @param {*} status
22
+ * @param {*} callback
23
+ */
24
+ receive(status: any, callback: any): this;
25
+ /**
26
+ * @private
27
+ */
28
+ reset(): void;
29
+ /**
30
+ * @private
31
+ */
32
+ matchReceive({ status, response, _ref }: {
33
+ status: any;
34
+ response: any;
35
+ _ref: any;
36
+ }): void;
37
+ /**
38
+ * @private
39
+ */
40
+ cancelRefEvent(): void;
41
+ /**
42
+ * @private
43
+ */
44
+ cancelTimeout(): void;
45
+ /**
46
+ * @private
47
+ */
48
+ startTimeout(): void;
49
+ /**
50
+ * @private
51
+ */
52
+ hasReceived(status: any): any;
53
+ /**
54
+ * @private
55
+ */
56
+ trigger(status: any, response: any): void;
57
+ }
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // @ts-nocheck
4
+ /**
5
+ * Initializes the Push
6
+ * @param {Channel} channel - The Channel
7
+ * @param {string} event - The event, for example `"phx_join"`
8
+ * @param {Object} payload - The payload, for example `{user_id: 123}`
9
+ * @param {number} timeout - The push timeout in milliseconds
10
+ */
11
+ class Push {
12
+ constructor(channel, event, payload, timeout) {
13
+ this.channel = channel;
14
+ this.event = event;
15
+ this.payload = payload || function () { return {}; };
16
+ this.receivedResp = null;
17
+ this.timeout = timeout;
18
+ this.timeoutTimer = null;
19
+ this.recHooks = [];
20
+ this.sent = false;
21
+ }
22
+ /**
23
+ *
24
+ * @param {number} timeout
25
+ */
26
+ resend(timeout) {
27
+ this.timeout = timeout;
28
+ this.reset();
29
+ this.send();
30
+ }
31
+ /**
32
+ *
33
+ */
34
+ send() {
35
+ if (this.hasReceived("timeout")) {
36
+ return;
37
+ }
38
+ this.startTimeout();
39
+ this.sent = true;
40
+ this.channel.socket.push({
41
+ topic: this.channel.topic,
42
+ event: this.event,
43
+ payload: this.payload(),
44
+ ref: this.ref,
45
+ join_ref: this.channel.joinRef()
46
+ });
47
+ }
48
+ /**
49
+ *
50
+ * @param {*} status
51
+ * @param {*} callback
52
+ */
53
+ receive(status, callback) {
54
+ if (this.hasReceived(status)) {
55
+ callback(this.receivedResp.response);
56
+ }
57
+ this.recHooks.push({ status, callback });
58
+ return this;
59
+ }
60
+ /**
61
+ * @private
62
+ */
63
+ reset() {
64
+ this.cancelRefEvent();
65
+ this.ref = null;
66
+ this.refEvent = null;
67
+ this.receivedResp = null;
68
+ this.sent = false;
69
+ }
70
+ /**
71
+ * @private
72
+ */
73
+ matchReceive({ status, response, _ref }) {
74
+ this.recHooks.filter(h => h.status === status)
75
+ .forEach(h => h.callback(response));
76
+ }
77
+ /**
78
+ * @private
79
+ */
80
+ cancelRefEvent() {
81
+ if (!this.refEvent) {
82
+ return;
83
+ }
84
+ this.channel.off(this.refEvent);
85
+ }
86
+ /**
87
+ * @private
88
+ */
89
+ cancelTimeout() {
90
+ clearTimeout(this.timeoutTimer);
91
+ this.timeoutTimer = null;
92
+ }
93
+ /**
94
+ * @private
95
+ */
96
+ startTimeout() {
97
+ if (this.timeoutTimer) {
98
+ this.cancelTimeout();
99
+ }
100
+ this.ref = this.channel.socket.makeRef();
101
+ this.refEvent = this.channel.replyEventName(this.ref);
102
+ this.channel.on(this.refEvent, payload => {
103
+ this.cancelRefEvent();
104
+ this.cancelTimeout();
105
+ this.receivedResp = payload;
106
+ this.matchReceive(payload);
107
+ });
108
+ this.timeoutTimer = setTimeout(() => {
109
+ this.trigger("timeout", {});
110
+ }, this.timeout);
111
+ }
112
+ /**
113
+ * @private
114
+ */
115
+ hasReceived(status) {
116
+ return this.receivedResp && this.receivedResp.status === status;
117
+ }
118
+ /**
119
+ * @private
120
+ */
121
+ trigger(status, response) {
122
+ this.channel.trigger(this.refEvent, { status, response });
123
+ }
124
+ }
125
+ exports.default = Push;
@@ -0,0 +1,53 @@
1
+ declare const _default: {
2
+ HEADER_LENGTH: number;
3
+ META_LENGTH: number;
4
+ KINDS: {
5
+ push: number;
6
+ reply: number;
7
+ broadcast: number;
8
+ };
9
+ encode(msg: any, callback: any): any;
10
+ decode(rawPayload: any, callback: any): any;
11
+ binaryEncode(message: any): ArrayBufferLike;
12
+ binaryDecode(buffer: any): {
13
+ join_ref: any;
14
+ ref: null;
15
+ topic: any;
16
+ event: any;
17
+ payload: any;
18
+ } | {
19
+ join_ref: any;
20
+ ref: any;
21
+ topic: any;
22
+ event: string;
23
+ payload: {
24
+ status: any;
25
+ response: any;
26
+ };
27
+ } | undefined;
28
+ decodePush(buffer: any, view: any, decoder: any): {
29
+ join_ref: any;
30
+ ref: null;
31
+ topic: any;
32
+ event: any;
33
+ payload: any;
34
+ };
35
+ decodeReply(buffer: any, view: any, decoder: any): {
36
+ join_ref: any;
37
+ ref: any;
38
+ topic: any;
39
+ event: string;
40
+ payload: {
41
+ status: any;
42
+ response: any;
43
+ };
44
+ };
45
+ decodeBroadcast(buffer: any, view: any, decoder: any): {
46
+ join_ref: null;
47
+ ref: null;
48
+ topic: any;
49
+ event: any;
50
+ payload: any;
51
+ };
52
+ };
53
+ export default _default;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // @ts-nocheck
4
+ /* The default serializer for encoding and decoding messages */
5
+ const constants_1 = require("./constants");
6
+ exports.default = {
7
+ HEADER_LENGTH: 1,
8
+ META_LENGTH: 4,
9
+ KINDS: { push: 0, reply: 1, broadcast: 2 },
10
+ encode(msg, callback) {
11
+ if (msg.payload.constructor === ArrayBuffer) {
12
+ return callback(this.binaryEncode(msg));
13
+ }
14
+ else {
15
+ let payload = [msg.join_ref, msg.ref, msg.topic, msg.event, msg.payload];
16
+ return callback(JSON.stringify(payload));
17
+ }
18
+ },
19
+ decode(rawPayload, callback) {
20
+ if (rawPayload.constructor === ArrayBuffer) {
21
+ return callback(this.binaryDecode(rawPayload));
22
+ }
23
+ else {
24
+ let [join_ref, ref, topic, event, payload] = JSON.parse(rawPayload);
25
+ return callback({ join_ref, ref, topic, event, payload });
26
+ }
27
+ },
28
+ // private
29
+ binaryEncode(message) {
30
+ let { join_ref, ref, event, topic, payload } = message;
31
+ let metaLength = this.META_LENGTH + join_ref.length + ref.length + topic.length + event.length;
32
+ let header = new ArrayBuffer(this.HEADER_LENGTH + metaLength);
33
+ let view = new DataView(header);
34
+ let offset = 0;
35
+ view.setUint8(offset++, this.KINDS.push); // kind
36
+ view.setUint8(offset++, join_ref.length);
37
+ view.setUint8(offset++, ref.length);
38
+ view.setUint8(offset++, topic.length);
39
+ view.setUint8(offset++, event.length);
40
+ Array.from(join_ref, char => view.setUint8(offset++, char.charCodeAt(0)));
41
+ Array.from(ref, char => view.setUint8(offset++, char.charCodeAt(0)));
42
+ Array.from(topic, char => view.setUint8(offset++, char.charCodeAt(0)));
43
+ Array.from(event, char => view.setUint8(offset++, char.charCodeAt(0)));
44
+ var combined = new Uint8Array(header.byteLength + payload.byteLength);
45
+ combined.set(new Uint8Array(header), 0);
46
+ combined.set(new Uint8Array(payload), header.byteLength);
47
+ return combined.buffer;
48
+ },
49
+ binaryDecode(buffer) {
50
+ let view = new DataView(buffer);
51
+ let kind = view.getUint8(0);
52
+ let decoder = new TextDecoder();
53
+ switch (kind) {
54
+ case this.KINDS.push: return this.decodePush(buffer, view, decoder);
55
+ case this.KINDS.reply: return this.decodeReply(buffer, view, decoder);
56
+ case this.KINDS.broadcast: return this.decodeBroadcast(buffer, view, decoder);
57
+ }
58
+ },
59
+ decodePush(buffer, view, decoder) {
60
+ let joinRefSize = view.getUint8(1);
61
+ let topicSize = view.getUint8(2);
62
+ let eventSize = view.getUint8(3);
63
+ let offset = this.HEADER_LENGTH + this.META_LENGTH - 1; // pushes have no ref
64
+ let joinRef = decoder.decode(buffer.slice(offset, offset + joinRefSize));
65
+ offset = offset + joinRefSize;
66
+ let topic = decoder.decode(buffer.slice(offset, offset + topicSize));
67
+ offset = offset + topicSize;
68
+ let event = decoder.decode(buffer.slice(offset, offset + eventSize));
69
+ offset = offset + eventSize;
70
+ let data = buffer.slice(offset, buffer.byteLength);
71
+ return { join_ref: joinRef, ref: null, topic: topic, event: event, payload: data };
72
+ },
73
+ decodeReply(buffer, view, decoder) {
74
+ let joinRefSize = view.getUint8(1);
75
+ let refSize = view.getUint8(2);
76
+ let topicSize = view.getUint8(3);
77
+ let eventSize = view.getUint8(4);
78
+ let offset = this.HEADER_LENGTH + this.META_LENGTH;
79
+ let joinRef = decoder.decode(buffer.slice(offset, offset + joinRefSize));
80
+ offset = offset + joinRefSize;
81
+ let ref = decoder.decode(buffer.slice(offset, offset + refSize));
82
+ offset = offset + refSize;
83
+ let topic = decoder.decode(buffer.slice(offset, offset + topicSize));
84
+ offset = offset + topicSize;
85
+ let event = decoder.decode(buffer.slice(offset, offset + eventSize));
86
+ offset = offset + eventSize;
87
+ let data = buffer.slice(offset, buffer.byteLength);
88
+ let payload = { status: event, response: data };
89
+ return { join_ref: joinRef, ref: ref, topic: topic, event: constants_1.CHANNEL_EVENTS.reply, payload: payload };
90
+ },
91
+ decodeBroadcast(buffer, view, decoder) {
92
+ let topicSize = view.getUint8(1);
93
+ let eventSize = view.getUint8(2);
94
+ let offset = this.HEADER_LENGTH + 2;
95
+ let topic = decoder.decode(buffer.slice(offset, offset + topicSize));
96
+ offset = offset + topicSize;
97
+ let event = decoder.decode(buffer.slice(offset, offset + eventSize));
98
+ offset = offset + eventSize;
99
+ let data = buffer.slice(offset, buffer.byteLength);
100
+ return { join_ref: null, ref: null, topic: topic, event: event, payload: data };
101
+ }
102
+ };