wirejs-deploy-amplify-basic 0.0.116-realtime → 0.0.118-realtime
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/dist/client/realtime.js
CHANGED
|
@@ -7,18 +7,23 @@
|
|
|
7
7
|
*/
|
|
8
8
|
const connections = new Map();
|
|
9
9
|
/**
|
|
10
|
+
* Channel subscription ID look-up table.
|
|
11
|
+
*
|
|
10
12
|
* `${URL}#${channel}` -> subscription ID
|
|
11
13
|
*/
|
|
12
|
-
const
|
|
14
|
+
const channelSubIdLUT = new Map();
|
|
13
15
|
/**
|
|
14
16
|
* subscription ID -> subscriber
|
|
15
17
|
*/
|
|
16
|
-
const
|
|
18
|
+
const subIdSubscribers = new Map();
|
|
17
19
|
/**
|
|
18
20
|
* subcription ID -> connection state
|
|
19
21
|
*
|
|
20
|
-
* For when new
|
|
22
|
+
* For when new subIdSubscribers are added, we'll want to broadcast the current
|
|
21
23
|
* state of the connection.
|
|
24
|
+
*
|
|
25
|
+
* "closed"-typed states are not represented here, as they are removed from the map
|
|
26
|
+
* when the WebSocket connection is closed.
|
|
22
27
|
*/
|
|
23
28
|
const subscriptionState = new Map();
|
|
24
29
|
/**
|
|
@@ -40,23 +45,41 @@ function getAuthProtocol(authorization) {
|
|
|
40
45
|
const header = getBase64URLEncoded(authorization);
|
|
41
46
|
return `header-${header}`;
|
|
42
47
|
}
|
|
43
|
-
|
|
48
|
+
function urlString(url) {
|
|
49
|
+
if (!url.startsWith('ws://') && !url.startsWith('wss://')) {
|
|
50
|
+
throw new Error(`Invalid WebSocket URL: ${url}`);
|
|
51
|
+
}
|
|
52
|
+
return url;
|
|
53
|
+
}
|
|
54
|
+
function fullChannelNameString(url, channel) {
|
|
55
|
+
if (!channel || typeof channel !== 'string') {
|
|
56
|
+
throw new Error(`Invalid channel name: ${channel}`);
|
|
57
|
+
}
|
|
44
58
|
const fullChannelName = `${url}#${channel}`;
|
|
59
|
+
return fullChannelName;
|
|
60
|
+
}
|
|
61
|
+
function subscriptionIdString(id) {
|
|
62
|
+
return id;
|
|
63
|
+
}
|
|
64
|
+
export function subscribe(url, channel, token, authHost, subscriber) {
|
|
65
|
+
const urlKey = urlString(url);
|
|
66
|
+
const fullChannelName = fullChannelNameString(urlKey, channel);
|
|
45
67
|
const authorization = {
|
|
46
68
|
Authorization: token,
|
|
47
69
|
host: authHost,
|
|
48
70
|
};
|
|
49
|
-
if (!connections.has(
|
|
71
|
+
if (!connections.has(urlKey)) {
|
|
50
72
|
const ws = new WebSocket(url, [
|
|
51
73
|
'aws-appsync-event-ws',
|
|
52
74
|
getAuthProtocol(authorization)
|
|
53
75
|
]);
|
|
54
|
-
connections.set(
|
|
76
|
+
connections.set(urlKey, ws);
|
|
55
77
|
ws.onmessage = event => {
|
|
56
78
|
const data = JSON.parse(event.data);
|
|
79
|
+
const sid = subscriptionIdString(data.id);
|
|
57
80
|
if (data.type === 'data') {
|
|
58
81
|
const eventData = JSON.parse(data.event);
|
|
59
|
-
for (const subscriber of
|
|
82
|
+
for (const subscriber of subIdSubscribers.get(sid) || []) {
|
|
60
83
|
try {
|
|
61
84
|
subscriber.onmessage(eventData);
|
|
62
85
|
}
|
|
@@ -66,8 +89,8 @@ export function subscribe(url, channel, token, authHost, subscriber) {
|
|
|
66
89
|
}
|
|
67
90
|
}
|
|
68
91
|
else if (data.type === 'subscribe_success') {
|
|
69
|
-
subscriptionState.set(
|
|
70
|
-
for (const subscriber of
|
|
92
|
+
subscriptionState.set(sid, 'open');
|
|
93
|
+
for (const subscriber of subIdSubscribers.get(sid) || []) {
|
|
71
94
|
try {
|
|
72
95
|
subscriber.onopen?.();
|
|
73
96
|
}
|
|
@@ -77,36 +100,38 @@ export function subscribe(url, channel, token, authHost, subscriber) {
|
|
|
77
100
|
}
|
|
78
101
|
}
|
|
79
102
|
};
|
|
80
|
-
const notifyClosed = () => {
|
|
81
|
-
const subscriptionIds = Array.from(
|
|
103
|
+
const notifyClosed = (reason) => {
|
|
104
|
+
const subscriptionIds = Array.from(channelSubIdLUT.entries())
|
|
82
105
|
.filter(([urlChannel, _subId]) => urlChannel.startsWith(`${url}#`))
|
|
83
|
-
.map(([_urlChannel, subId]) => subId);
|
|
106
|
+
.map(([_urlChannel, subId]) => subscriptionIdString(subId));
|
|
84
107
|
for (const subscriptionId of subscriptionIds) {
|
|
85
|
-
|
|
86
|
-
const subs = subscribers.get(subscriptionId);
|
|
108
|
+
const subs = subIdSubscribers.get(subscriptionId);
|
|
87
109
|
if (subs) {
|
|
88
110
|
for (const subscriber of subs) {
|
|
89
111
|
if (subscriber.onclose) {
|
|
90
112
|
try {
|
|
91
|
-
subscriber.onclose();
|
|
113
|
+
subscriber.onclose(reason);
|
|
92
114
|
}
|
|
93
115
|
catch (error) {
|
|
94
116
|
console.error('Error in subscriber onclose:', error);
|
|
95
117
|
}
|
|
96
118
|
}
|
|
97
119
|
}
|
|
98
|
-
subscribers.delete(subscriptionId);
|
|
99
120
|
}
|
|
121
|
+
subIdSubscribers.delete(subscriptionId);
|
|
122
|
+
subscriptionState.delete(subscriptionId);
|
|
100
123
|
}
|
|
101
|
-
|
|
124
|
+
channelSubIdLUT.delete(fullChannelName);
|
|
125
|
+
connections.delete(urlKey);
|
|
126
|
+
console.debug('closed', ws);
|
|
102
127
|
};
|
|
103
|
-
ws.onclose = () => notifyClosed();
|
|
104
|
-
ws.onerror = () => notifyClosed();
|
|
128
|
+
ws.onclose = () => notifyClosed('closed');
|
|
129
|
+
ws.onerror = () => notifyClosed('error');
|
|
105
130
|
}
|
|
106
|
-
if (!
|
|
107
|
-
const subscriptionId = crypto.randomUUID();
|
|
131
|
+
if (!channelSubIdLUT.has(fullChannelName)) {
|
|
132
|
+
const subscriptionId = subscriptionIdString(crypto.randomUUID());
|
|
108
133
|
subscriptionState.set(subscriptionId, 'connecting');
|
|
109
|
-
const ws = connections.get(
|
|
134
|
+
const ws = connections.get(urlKey);
|
|
110
135
|
const subscribe = () => {
|
|
111
136
|
ws.send(JSON.stringify({
|
|
112
137
|
id: subscriptionId,
|
|
@@ -114,8 +139,8 @@ export function subscribe(url, channel, token, authHost, subscriber) {
|
|
|
114
139
|
channel,
|
|
115
140
|
authorization
|
|
116
141
|
}));
|
|
117
|
-
|
|
118
|
-
|
|
142
|
+
channelSubIdLUT.set(fullChannelName, subscriptionId);
|
|
143
|
+
subIdSubscribers.set(subscriptionId, [subscriber]);
|
|
119
144
|
};
|
|
120
145
|
if (ws.readyState === WebSocket.OPEN) {
|
|
121
146
|
subscribe();
|
|
@@ -125,36 +150,42 @@ export function subscribe(url, channel, token, authHost, subscriber) {
|
|
|
125
150
|
}
|
|
126
151
|
}
|
|
127
152
|
else {
|
|
128
|
-
const subscriptionId =
|
|
129
|
-
|
|
153
|
+
const subscriptionId = channelSubIdLUT.get(fullChannelName);
|
|
154
|
+
subIdSubscribers.get(subscriptionId).push(subscriber);
|
|
130
155
|
if (subscriptionState.get(subscriptionId) === 'open') {
|
|
131
156
|
subscriber.onopen?.();
|
|
132
157
|
}
|
|
133
|
-
else if (subscriptionState.get(subscriptionId) === 'closed') {
|
|
134
|
-
subscriber.onclose?.();
|
|
135
|
-
}
|
|
136
158
|
}
|
|
137
159
|
}
|
|
138
160
|
export function unsubscribe(url, channel, subscriber) {
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
161
|
+
const urlKey = urlString(url);
|
|
162
|
+
const ws = connections.get(urlKey);
|
|
163
|
+
const fullChannelName = fullChannelNameString(urlKey, channel);
|
|
164
|
+
const subId = channelSubIdLUT.get(fullChannelName);
|
|
165
|
+
const subs = subId && subIdSubscribers.get(subId);
|
|
166
|
+
const sub = subs ? subs.find(s => s === subscriber) : undefined;
|
|
167
|
+
if (sub && subs) {
|
|
168
|
+
sub.onclose?.('unsubscribed');
|
|
169
|
+
const i = subs.indexOf(sub);
|
|
170
|
+
if (i > -1)
|
|
171
|
+
subs.splice(i, 1);
|
|
172
|
+
}
|
|
173
|
+
if (subs && subs.length === 0) {
|
|
174
|
+
// No subIdSubscribers left for this channel. We can unsubscribe from channel.
|
|
175
|
+
ws?.send(JSON.stringify({
|
|
176
|
+
id: subId,
|
|
177
|
+
type: 'unsubscribe',
|
|
178
|
+
}));
|
|
179
|
+
subscriptionState.delete(subId);
|
|
180
|
+
subIdSubscribers.delete(subId);
|
|
181
|
+
channelSubIdLUT.delete(fullChannelName);
|
|
182
|
+
}
|
|
183
|
+
const socketSubs = Array.from(channelSubIdLUT.keys())
|
|
184
|
+
.filter(k => k.startsWith(`${url}#`));
|
|
185
|
+
if (socketSubs.length === 0) {
|
|
186
|
+
// No channels left for this URL. We can close the WebSocket connection.
|
|
187
|
+
ws?.close();
|
|
188
|
+
connections.delete(urlKey);
|
|
189
|
+
console.debug('closed', ws);
|
|
159
190
|
}
|
|
160
191
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wirejs-deploy-amplify-basic",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.118-realtime",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"recursive-copy": "^2.0.14",
|
|
42
42
|
"rimraf": "^6.0.1",
|
|
43
43
|
"wirejs-dom": "^1.0.41",
|
|
44
|
-
"wirejs-resources": "^0.1.
|
|
44
|
+
"wirejs-resources": "^0.1.86-realtime"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@aws-amplify/backend": "^1.14.0",
|