@live-change/peer-connection-service 0.8.109 → 0.8.111

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 (3) hide show
  1. package/message.js +84 -10
  2. package/package.json +2 -2
  3. package/turn.js +1 -0
package/message.js CHANGED
@@ -23,6 +23,9 @@ const messageFields = {
23
23
  },
24
24
  data: {
25
25
  type: Object
26
+ },
27
+ sent: {
28
+ type: Date
26
29
  }
27
30
  }
28
31
 
@@ -40,13 +43,16 @@ const Message = definition.model({
40
43
  property: ['to', 'timestamp']
41
44
  },*/
42
45
  },
43
- crud: {
44
- deleteTrigger: true,
45
- writeOptions: {
46
- access: (params, {client, service}) => {
47
- return client.roles.includes('admin')
48
- }
49
- }
46
+ })
47
+
48
+ definition.event({
49
+ name: "MessageCreated",
50
+ async execute({ message, data }, { client, service }) {
51
+ if(data.id) delete data.id
52
+ await Message.create({
53
+ id: message,
54
+ ...data
55
+ })
50
56
  }
51
57
  })
52
58
 
@@ -110,7 +116,7 @@ definition.view({
110
116
 
111
117
  let lastMessageTime = new Map()
112
118
 
113
- async function postMessage(props, { client, service }, emit, conversation) {
119
+ function postMessage(props, { client, service }, emit, conversation) {
114
120
  console.log("POST MESSAGE", props)
115
121
  const channelId = props.to
116
122
  let lastTime = lastMessageTime.get(channelId)
@@ -151,15 +157,83 @@ definition.action({
151
157
  if(visibilityTest) return true
152
158
  const [fromType, fromId, fromSession] = from.split(':')
153
159
  const [toType, toId, toSession] = to.split(':')
154
- console.log("POST MESSAGE", fromType, fromId, fromSession, '=>', toType, toId, toSession, "BY", client)
160
+ //console.log("POST MESSAGE", fromType, fromId, fromSession, '=>', toType, toId, toSession, "BY", client)
155
161
  if(toType !== fromType || toId !== fromId) return false // different channel
156
162
  if(client.session !== fromSession) return false
157
163
  const hasRole = await clientHasAccessRoles(client, { objectType: toType, object: toId }, writerRoles)
158
164
  return hasRole
159
165
  },
166
+ queuedBy: (props) => props.from+':'+props.to, // without this, messages order can be changed
167
+ // and it will block ice connection state
160
168
  async execute(props, { client, service }, emit) {
161
- const result = await postMessage(props, { client, service }, emit)
169
+ console.error('postMessage is deprecated, use postMessages instead')
170
+ const result = postMessage(props, { client, service }, emit)
162
171
  console.log("MESSAGE POSTED!")
163
172
  return result
164
173
  }
165
174
  })
175
+
176
+
177
+ definition.action({
178
+ name: "postMessages",
179
+ properties: {
180
+ from: {
181
+ type: Peer,
182
+ validation: ['nonEmpty']
183
+ },
184
+ to: {
185
+ type: Peer,
186
+ validation: ['nonEmpty']
187
+ },
188
+ messages: {
189
+ type: Array,
190
+ of: {
191
+ type: Object,
192
+ messageFields
193
+ }
194
+ }
195
+ },
196
+ //queuedBy: (command) => `${command.toType}_${command.toId})`,
197
+ access: async ({ from, to }, context) => {
198
+ const { client, service, visibilityTest } = context
199
+ if(visibilityTest) return true
200
+ const [fromType, fromId, fromSession] = from.split(':')
201
+ const [toType, toId, toSession] = to.split(':')
202
+ // console.log("POST MESSAGE", fromType, fromId, fromSession, '=>', toType, toId, toSession, "BY", client)
203
+ if(toType !== fromType || toId !== fromId) return false // different channel
204
+ if(client.session !== fromSession) return false
205
+ const hasRole = await clientHasAccessRoles(client, { objectType: toType, object: toId }, writerRoles)
206
+ return hasRole
207
+ },
208
+ queuedBy: (props) => props.from+':'+props.to, // without this, messages order can be changed
209
+ // and it will block ice connection state
210
+ //waitForEvents: true,
211
+ async execute(props, { client, service }, emit) {
212
+ const lastMessages = await Message.rangeGet({
213
+ gte: `${props.to}_`,
214
+ lte: `${props.to}_\xFF\xFF\xFF\xFF`,
215
+ limit: 10,
216
+ reverse: true
217
+ })
218
+ let lastSent = lastMessages?.[0]?.sent
219
+ const messages = props.messages
220
+ for(const message of messages) {
221
+ message.from = props.from
222
+ message.to = props.to
223
+ const sent = new Date(message.sent).toISOString()
224
+ if(lastSent > sent) {
225
+ console.error("Message out of order", lastSent, '>', sent, "BY", props.from)
226
+ console.error("LAST MESSAGES", lastMessages)
227
+ console.error("RECEIVED MESSAGES", props.messages)
228
+ // process.exit(1)
229
+ throw new Error("Messages out of order")
230
+ }
231
+ lastSent = sent
232
+ }
233
+ for(const message of messages) {
234
+ postMessage(message, { client, service }, emit)
235
+ }
236
+ // console.log("MESSAGES POSTED!")
237
+ return 'ok'
238
+ }
239
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/peer-connection-service",
3
- "version": "0.8.109",
3
+ "version": "0.8.111",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -11,5 +11,5 @@
11
11
  },
12
12
  "author": "Michał Łaszczewski <michal@emikse.com>",
13
13
  "license": "BSD-3-Clause",
14
- "gitHead": "075bf3759fae6c40fb8e44d77d9f12c5c175e098"
14
+ "gitHead": "9b96afb2fc61ab3d2a5d143924e2c56d411280b4"
15
15
  }
package/turn.js CHANGED
@@ -28,6 +28,7 @@ function randomHexString(size) {
28
28
  }
29
29
 
30
30
  async function createTurnConfiguration({ client }) {
31
+ if(!urls || !secret) return null
31
32
  if(!urls) throw new Error('TURN urls not configured')
32
33
  if(!secret) throw new Error('TURN secret not configured')
33
34
  const expire = Date.now() / 1000 + turnExpireTime | 0