@dcl/sdk 7.20.2-22169778016.commit-030cbfe → 7.20.2-22231111352.commit-d2f6f0a
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/network/binary-message-bus.d.ts +3 -6
- package/network/binary-message-bus.js +5 -9
- package/network/index.d.ts +2 -8
- package/network/index.js +3 -16
- package/network/message-bus-sync.d.ts +1 -14
- package/network/message-bus-sync.js +103 -166
- package/network/state.js +5 -3
- package/package.json +6 -6
- package/src/network/binary-message-bus.ts +4 -9
- package/src/network/index.ts +3 -40
- package/src/network/message-bus-sync.ts +110 -180
- package/src/network/state.ts +4 -3
- package/atom.d.ts +0 -19
- package/atom.js +0 -83
- package/future.d.ts +0 -8
- package/future.js +0 -26
- package/network/chunking.d.ts +0 -5
- package/network/chunking.js +0 -38
- package/network/events/implementation.d.ts +0 -93
- package/network/events/implementation.js +0 -230
- package/network/events/index.d.ts +0 -42
- package/network/events/index.js +0 -43
- package/network/events/protocol.d.ts +0 -27
- package/network/events/protocol.js +0 -66
- package/network/events/registry.d.ts +0 -8
- package/network/events/registry.js +0 -3
- package/network/server/index.d.ts +0 -14
- package/network/server/index.js +0 -219
- package/network/server/utils.d.ts +0 -18
- package/network/server/utils.js +0 -135
- package/server/env-var.d.ts +0 -15
- package/server/env-var.js +0 -31
- package/server/index.d.ts +0 -2
- package/server/index.js +0 -3
- package/server/storage/constants.d.ts +0 -23
- package/server/storage/constants.js +0 -2
- package/server/storage/index.d.ts +0 -22
- package/server/storage/index.js +0 -29
- package/server/storage/player.d.ts +0 -43
- package/server/storage/player.js +0 -92
- package/server/storage/scene.d.ts +0 -38
- package/server/storage/scene.js +0 -90
- package/server/storage-url.d.ts +0 -10
- package/server/storage-url.js +0 -29
- package/server/utils.d.ts +0 -35
- package/server/utils.js +0 -56
- package/src/atom.ts +0 -98
- package/src/future.ts +0 -38
- package/src/network/chunking.ts +0 -45
- package/src/network/events/implementation.ts +0 -286
- package/src/network/events/index.ts +0 -48
- package/src/network/events/protocol.ts +0 -94
- package/src/network/events/registry.ts +0 -18
- package/src/network/server/index.ts +0 -301
- package/src/network/server/utils.ts +0 -189
- package/src/server/env-var.ts +0 -36
- package/src/server/index.ts +0 -2
- package/src/server/storage/constants.ts +0 -22
- package/src/server/storage/index.ts +0 -44
- package/src/server/storage/player.ts +0 -156
- package/src/server/storage/scene.ts +0 -149
- package/src/server/storage-url.ts +0 -34
- package/src/server/utils.ts +0 -73
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IEngine,
|
|
3
|
-
Entity,
|
|
4
|
-
CrdtMessageType,
|
|
5
|
-
CrdtMessageBody,
|
|
6
|
-
ProcessMessageResultType,
|
|
7
|
-
ComponentType,
|
|
8
|
-
PutNetworkComponentOperation
|
|
9
|
-
} from '@dcl/ecs'
|
|
10
|
-
import * as components from '@dcl/ecs/dist/components'
|
|
11
|
-
import { ReadWriteByteBuffer } from '@dcl/ecs/dist/serialization/ByteBuffer'
|
|
12
|
-
import { CommsMessage } from '../binary-message-bus'
|
|
13
|
-
import { chunkCrdtMessages } from '../chunking'
|
|
14
|
-
import * as utils from './utils'
|
|
15
|
-
import { AUTH_SERVER_PEER_ID, DEBUG_NETWORK_MESSAGES } from '../message-bus-sync'
|
|
16
|
-
import { type BinaryMessageBus } from '../binary-message-bus'
|
|
17
|
-
import {
|
|
18
|
-
LastWriteWinElementSetComponentDefinition,
|
|
19
|
-
GrowOnlyValueSetComponentDefinition,
|
|
20
|
-
ComponentDefinition,
|
|
21
|
-
InternalBaseComponent
|
|
22
|
-
} from '@dcl/ecs/dist/engine/component'
|
|
23
|
-
|
|
24
|
-
export const LIVEKIT_MAX_SIZE = 12
|
|
25
|
-
|
|
26
|
-
export interface ServerValidationConfig {
|
|
27
|
-
engine: IEngine
|
|
28
|
-
binaryMessageBus: ReturnType<typeof BinaryMessageBus>
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function createServerValidator(config: ServerValidationConfig) {
|
|
32
|
-
const { engine, binaryMessageBus } = config
|
|
33
|
-
|
|
34
|
-
// Initialize components for network operations and transform fixing
|
|
35
|
-
const NetworkEntity = components.NetworkEntity(engine)
|
|
36
|
-
const CreatedBy = components.CreatedBy(engine)
|
|
37
|
-
const NetworkParent = components.NetworkParent(engine)
|
|
38
|
-
|
|
39
|
-
// Type guard to check if component supports corrections (both LWW and GrowOnlySet)
|
|
40
|
-
function supportsCorrections<T>(
|
|
41
|
-
component: ComponentDefinition<T>
|
|
42
|
-
): component is LastWriteWinElementSetComponentDefinition<T> | GrowOnlyValueSetComponentDefinition<T> {
|
|
43
|
-
return (
|
|
44
|
-
(component.componentType === ComponentType.LastWriteWinElementSet ||
|
|
45
|
-
component.componentType === ComponentType.GrowOnlyValueSet) &&
|
|
46
|
-
'getCrdtState' in component
|
|
47
|
-
)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function findExistingNetworkEntity(message: utils.NetworkMessage): Entity | null {
|
|
51
|
-
// Look for existing network entity mapping (don't create new ones)
|
|
52
|
-
for (const [entityId, networkData] of engine.getEntitiesWith(NetworkEntity)) {
|
|
53
|
-
if (networkData.networkId === message.networkId && networkData.entityId === message.entityId) {
|
|
54
|
-
return entityId
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
// Return null if not found
|
|
58
|
-
return null
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function findOrCreateNetworkEntity(message: utils.NetworkMessage, sender: string, isServer: boolean): Entity {
|
|
62
|
-
// Look for existing network entity mapping first
|
|
63
|
-
const existingEntity = findExistingNetworkEntity(message)
|
|
64
|
-
|
|
65
|
-
if (existingEntity) {
|
|
66
|
-
return existingEntity
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Create new entity and network mapping
|
|
70
|
-
const newEntityId = engine.addEntity()
|
|
71
|
-
NetworkEntity.createOrReplace(newEntityId, {
|
|
72
|
-
networkId: message.networkId,
|
|
73
|
-
entityId: message.entityId
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
if (isServer) {
|
|
77
|
-
CreatedBy.createOrReplace(newEntityId, { address: sender })
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
DEBUG_NETWORK_MESSAGES() &&
|
|
81
|
-
console.log(`[DEBUG] Created new entity ${newEntityId} for network ${message.networkId}:${message.entityId}`)
|
|
82
|
-
return newEntityId
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function convertNetworkToRegularMessage(
|
|
86
|
-
networkMessage: utils.NetworkMessage,
|
|
87
|
-
localEntityId: Entity,
|
|
88
|
-
forceCorrections = false
|
|
89
|
-
): (CrdtMessageBody & { messageBuffer: Uint8Array }) | null {
|
|
90
|
-
const buffer = new ReadWriteByteBuffer()
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
// Use the well-tested networkMessageToLocal utility with transform fixing for Unity
|
|
94
|
-
const message = utils.networkMessageToLocal(
|
|
95
|
-
networkMessage,
|
|
96
|
-
localEntityId,
|
|
97
|
-
buffer,
|
|
98
|
-
NetworkParent,
|
|
99
|
-
forceCorrections
|
|
100
|
-
)
|
|
101
|
-
return { ...message, messageBuffer: buffer.toBinary() }
|
|
102
|
-
} catch (error) {
|
|
103
|
-
DEBUG_NETWORK_MESSAGES() && console.error('Error converting network message:', error)
|
|
104
|
-
return null
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function validateMessagePermissions(message: utils.RegularMessage, sender: string, _localEntityId: Entity): boolean {
|
|
109
|
-
// Basic checks
|
|
110
|
-
if (!sender || sender === AUTH_SERVER_PEER_ID) {
|
|
111
|
-
return false // Server shouldn't send messages to itself
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (message.type === CrdtMessageType.DELETE_ENTITY) {
|
|
115
|
-
// TODO: how to handle this case ?
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (message.type === CrdtMessageType.PUT_COMPONENT || message.type === CrdtMessageType.DELETE_COMPONENT) {
|
|
119
|
-
const component = engine.getComponent(message.componentId) as InternalBaseComponent<unknown>
|
|
120
|
-
const buf = 'data' in message ? new ReadWriteByteBuffer(message.data) : null
|
|
121
|
-
const value = buf ? component.schema.deserialize(buf) : null
|
|
122
|
-
const dryRunCRDT = component.__dry_run_updateFromCrdt(message)
|
|
123
|
-
const validCRDT = [
|
|
124
|
-
ProcessMessageResultType.StateUpdatedData,
|
|
125
|
-
ProcessMessageResultType.StateUpdatedTimestamp,
|
|
126
|
-
ProcessMessageResultType.EntityDeleted
|
|
127
|
-
].includes(dryRunCRDT)
|
|
128
|
-
const createdBy = CreatedBy.getOrNull(message.entityId)
|
|
129
|
-
const validMessage =
|
|
130
|
-
validCRDT &&
|
|
131
|
-
component.__run_validateBeforeChange(message.entityId, value, sender, createdBy?.address ?? AUTH_SERVER_PEER_ID)
|
|
132
|
-
|
|
133
|
-
return !!validMessage
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// For now, basic validation - in the future this will check component sync permissions
|
|
137
|
-
// TODO: Check if sender owns the entity
|
|
138
|
-
// TODO: Check component sync mode ('all' | 'owner' | 'server')
|
|
139
|
-
// TODO: Run component custom validation
|
|
140
|
-
return true
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function broadcastBatchedMessages(messages: utils.NetworkMessage[], excludeSender: string) {
|
|
144
|
-
if (messages.length === 0) return
|
|
145
|
-
|
|
146
|
-
// Build the complete buffer with all messages
|
|
147
|
-
const networkBuffer = new ReadWriteByteBuffer()
|
|
148
|
-
for (const message of messages) {
|
|
149
|
-
// Skip oversized messages upfront
|
|
150
|
-
if (message.messageBuffer.byteLength / 1024 > LIVEKIT_MAX_SIZE) {
|
|
151
|
-
console.error(
|
|
152
|
-
`Message too large (${message.messageBuffer.byteLength} bytes), skipping message from ${excludeSender}`
|
|
153
|
-
)
|
|
154
|
-
continue
|
|
155
|
-
}
|
|
156
|
-
networkBuffer.writeBuffer(message.messageBuffer, false)
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Use the chunking function to split into proper chunks
|
|
160
|
-
const chunks = chunkCrdtMessages(networkBuffer.toBinary(), LIVEKIT_MAX_SIZE)
|
|
161
|
-
|
|
162
|
-
for (const chunk of chunks) {
|
|
163
|
-
binaryMessageBus.emit(CommsMessage.CRDT, chunk)
|
|
164
|
-
}
|
|
165
|
-
DEBUG_NETWORK_MESSAGES() &&
|
|
166
|
-
console.log(`Total: ${messages.length} messages in ${chunks.length} chunks from ${excludeSender}`)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function sendCorrectionToSender(networkMessage: utils.NetworkMessage, sender: string, localEntityId: Entity) {
|
|
170
|
-
try {
|
|
171
|
-
// Only handle component messages (PUT/DELETE), not entity deletion
|
|
172
|
-
if (networkMessage.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
173
|
-
DEBUG_NETWORK_MESSAGES() && console.log('[AUTHORITATIVE] Cannot send authoritative message for entity deletion')
|
|
174
|
-
return
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Safe to access componentId and timestamp now
|
|
178
|
-
const component = engine.getComponent(networkMessage.componentId)
|
|
179
|
-
|
|
180
|
-
// Only proceed if component supports authoritative messages (LWW or GrowOnlySet)
|
|
181
|
-
if (!supportsCorrections(component)) {
|
|
182
|
-
DEBUG_NETWORK_MESSAGES() && console.log('[AUTHORITATIVE] Component does not support authoritative messages')
|
|
183
|
-
return
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const serverCRDTState = component.getCrdtState(localEntityId)
|
|
187
|
-
|
|
188
|
-
if (serverCRDTState) {
|
|
189
|
-
// Create authoritative message using PUT_COMPONENT_NETWORK
|
|
190
|
-
// Each client will convert this to AUTHORITATIVE_PUT_COMPONENT with proper entity mapping
|
|
191
|
-
const correctionBuffer = new ReadWriteByteBuffer()
|
|
192
|
-
PutNetworkComponentOperation.write(
|
|
193
|
-
networkMessage.entityId, // Use original network entity ID
|
|
194
|
-
serverCRDTState.timestamp,
|
|
195
|
-
networkMessage.componentId,
|
|
196
|
-
networkMessage.networkId,
|
|
197
|
-
serverCRDTState.data,
|
|
198
|
-
correctionBuffer
|
|
199
|
-
)
|
|
200
|
-
// Send authoritative message directly to the sender
|
|
201
|
-
binaryMessageBus.emit(CommsMessage.CRDT_AUTHORITATIVE, correctionBuffer.toBinary(), [sender])
|
|
202
|
-
|
|
203
|
-
DEBUG_NETWORK_MESSAGES() &&
|
|
204
|
-
console.log(
|
|
205
|
-
`[AUTHORITATIVE] Sent authoritative message to ${sender} for entity ${localEntityId} component ${networkMessage.componentId} with timestamp ${networkMessage.timestamp}`
|
|
206
|
-
)
|
|
207
|
-
}
|
|
208
|
-
} catch (error) {
|
|
209
|
-
DEBUG_NETWORK_MESSAGES() && console.error('Error sending correction:', error)
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return {
|
|
214
|
-
findExistingNetworkEntity,
|
|
215
|
-
// transform Network messages to CRDT Common Messages.
|
|
216
|
-
processClientMessages: function processClientMessages(value: Uint8Array, sender: string, forceCorrections = false) {
|
|
217
|
-
// console.log(`[CLIENT] Processing message from ${sender}, ${value.length} bytes`)
|
|
218
|
-
|
|
219
|
-
// Collect all regular messages in a single buffer for batched application
|
|
220
|
-
const combinedBuffer = new ReadWriteByteBuffer()
|
|
221
|
-
|
|
222
|
-
// Clients process network messages from server and convert them to regular messages
|
|
223
|
-
for (const message of utils.readMessages(value)) {
|
|
224
|
-
// Only process network messages in client message handler
|
|
225
|
-
if (utils.isNetworkMessage(message)) {
|
|
226
|
-
const networkMessage = message as utils.NetworkMessage
|
|
227
|
-
|
|
228
|
-
// Find or create network entity mapping
|
|
229
|
-
const localEntityId = findOrCreateNetworkEntity(networkMessage, sender, false)
|
|
230
|
-
|
|
231
|
-
// Convert network message to regular message or correction message
|
|
232
|
-
const regularMessage = convertNetworkToRegularMessage(networkMessage, localEntityId, forceCorrections)
|
|
233
|
-
|
|
234
|
-
if (regularMessage?.messageBuffer.byteLength) {
|
|
235
|
-
combinedBuffer.writeBuffer(regularMessage.messageBuffer, false)
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return combinedBuffer.toBinary()
|
|
240
|
-
},
|
|
241
|
-
// Sever Code: process message, handle permissions, and broadcast if needed.
|
|
242
|
-
processServerMessages: function processServerMessages(value: Uint8Array, sender: string) {
|
|
243
|
-
// console.log(`[SERVER] Processing message from ${sender}, ${value.length} bytes`)
|
|
244
|
-
|
|
245
|
-
// Collect all valid messages for batched broadcasting
|
|
246
|
-
const messagesToBroadcast: utils.NetworkMessage[] = []
|
|
247
|
-
const regularMessagesBuffer = new ReadWriteByteBuffer()
|
|
248
|
-
|
|
249
|
-
for (const message of utils.readMessages(value)) {
|
|
250
|
-
try {
|
|
251
|
-
// Only process network messages in server message handler
|
|
252
|
-
if (utils.isNetworkMessage(message)) {
|
|
253
|
-
const networkMessage = message as utils.NetworkMessage
|
|
254
|
-
// 1. Find or create network entity mapping
|
|
255
|
-
const localEntityId = findOrCreateNetworkEntity(networkMessage, sender, true)
|
|
256
|
-
|
|
257
|
-
// 2. Convert network message to regular message and collect for local application
|
|
258
|
-
const regularMessage = convertNetworkToRegularMessage(networkMessage, localEntityId)
|
|
259
|
-
|
|
260
|
-
// 3. Basic permission validation
|
|
261
|
-
if (!validateMessagePermissions(regularMessage as any, sender, localEntityId)) {
|
|
262
|
-
// Send correction back to sender with server's authoritative state
|
|
263
|
-
sendCorrectionToSender(networkMessage, sender, localEntityId)
|
|
264
|
-
continue
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// 4. Collect valid message for batched broadcasting
|
|
268
|
-
messagesToBroadcast.push(networkMessage)
|
|
269
|
-
|
|
270
|
-
if (regularMessage?.messageBuffer.byteLength) {
|
|
271
|
-
regularMessagesBuffer.writeBuffer(regularMessage.messageBuffer, false)
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
} catch (error) {
|
|
275
|
-
DEBUG_NETWORK_MESSAGES() && console.error('Error processing server message:', error)
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
// Batch broadcast all valid messages together
|
|
279
|
-
broadcastBatchedMessages(messagesToBroadcast, sender)
|
|
280
|
-
return regularMessagesBuffer.toBinary()
|
|
281
|
-
},
|
|
282
|
-
// engine changes that needs to be broadcasted.
|
|
283
|
-
convertRegularToNetworkMessage: function convertRegularToNetworkMessage(regularMessage: Uint8Array): Uint8Array[] {
|
|
284
|
-
const groupedBuffer = new ReadWriteByteBuffer()
|
|
285
|
-
|
|
286
|
-
// First pass: Convert all regular messages to network format and group them into one big buffer
|
|
287
|
-
for (const message of utils.readMessages(regularMessage)) {
|
|
288
|
-
// Only convert regular messages that have network data
|
|
289
|
-
const networkData = NetworkEntity.getOrNull(message.entityId)
|
|
290
|
-
|
|
291
|
-
if (networkData && !utils.isNetworkMessage(message)) {
|
|
292
|
-
utils.localMessageToNetwork(message, networkData, groupedBuffer)
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Second pass: Use the new chunking function that respects message boundaries
|
|
297
|
-
const totalData = groupedBuffer.toBinary()
|
|
298
|
-
return chunkCrdtMessages(totalData, LIVEKIT_MAX_SIZE)
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import { Entity } from '@dcl/ecs/dist/engine'
|
|
2
|
-
import { CrdtMessageProtocol, NetworkParent } from '@dcl/ecs'
|
|
3
|
-
import { ReceiveMessage } from '@dcl/ecs/dist/runtime/types'
|
|
4
|
-
import { ReceiveNetworkMessage } from '@dcl/ecs/dist/systems/crdt/types'
|
|
5
|
-
import { ByteBuffer, ReadWriteByteBuffer } from '@dcl/ecs/dist/serialization/ByteBuffer'
|
|
6
|
-
import { AuthoritativePutComponentOperation, PutComponentOperation } from '@dcl/ecs/dist/serialization/crdt'
|
|
7
|
-
import {
|
|
8
|
-
CrdtMessage,
|
|
9
|
-
CrdtMessageBody,
|
|
10
|
-
CrdtMessageHeader,
|
|
11
|
-
CrdtMessageType,
|
|
12
|
-
DeleteComponentMessage,
|
|
13
|
-
DeleteComponentNetworkMessage,
|
|
14
|
-
DeleteEntityMessage,
|
|
15
|
-
DeleteEntityNetworkMessage,
|
|
16
|
-
PutComponentMessage,
|
|
17
|
-
AuthoritativePutComponentMessage,
|
|
18
|
-
PutNetworkComponentMessage
|
|
19
|
-
} from '@dcl/ecs/dist/serialization/crdt/types'
|
|
20
|
-
import { DeleteComponent } from '@dcl/ecs/dist/serialization/crdt/deleteComponent'
|
|
21
|
-
import { DeleteEntity } from '@dcl/ecs/dist/serialization/crdt/deleteEntity'
|
|
22
|
-
import { INetowrkEntityType } from '@dcl/ecs/dist/components/types'
|
|
23
|
-
import { PutNetworkComponentOperation } from '@dcl/ecs/dist/serialization/crdt/network/putComponentNetwork'
|
|
24
|
-
import { DeleteComponentNetwork } from '@dcl/ecs/dist/serialization/crdt/network/deleteComponentNetwork'
|
|
25
|
-
import { DeleteEntityNetwork } from '@dcl/ecs/dist/serialization/crdt/network/deleteEntityNetwork'
|
|
26
|
-
import { TransformSchema, COMPONENT_ID as TransformComponentId } from '@dcl/ecs/dist/components/manual/Transform'
|
|
27
|
-
|
|
28
|
-
export type NetworkMessage = (
|
|
29
|
-
| PutNetworkComponentMessage
|
|
30
|
-
| DeleteComponentNetworkMessage
|
|
31
|
-
| DeleteEntityNetworkMessage
|
|
32
|
-
) & { messageBuffer: Uint8Array }
|
|
33
|
-
|
|
34
|
-
export type RegularMessage = (
|
|
35
|
-
| PutComponentMessage
|
|
36
|
-
| AuthoritativePutComponentMessage
|
|
37
|
-
| DeleteComponentMessage
|
|
38
|
-
| DeleteEntityMessage
|
|
39
|
-
) & {
|
|
40
|
-
messageBuffer: Uint8Array
|
|
41
|
-
}
|
|
42
|
-
export function readMessages(data: Uint8Array): (NetworkMessage | RegularMessage)[] {
|
|
43
|
-
const buffer = new ReadWriteByteBuffer(data)
|
|
44
|
-
const messages: (NetworkMessage | RegularMessage)[] = []
|
|
45
|
-
let header: CrdtMessageHeader | null
|
|
46
|
-
while ((header = CrdtMessageProtocol.getHeader(buffer))) {
|
|
47
|
-
const offset = buffer.currentReadOffset()
|
|
48
|
-
let message: CrdtMessage | undefined = undefined
|
|
49
|
-
|
|
50
|
-
// Network messages
|
|
51
|
-
if (header.type === CrdtMessageType.DELETE_COMPONENT_NETWORK) {
|
|
52
|
-
message = DeleteComponentNetwork.read(buffer)!
|
|
53
|
-
} else if (header.type === CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
54
|
-
message = PutNetworkComponentOperation.read(buffer)!
|
|
55
|
-
} else if (header.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
56
|
-
message = DeleteEntityNetwork.read(buffer)!
|
|
57
|
-
}
|
|
58
|
-
// Regular messages
|
|
59
|
-
else if (header.type === CrdtMessageType.PUT_COMPONENT) {
|
|
60
|
-
message = PutComponentOperation.read(buffer)!
|
|
61
|
-
} else if (header.type === CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT) {
|
|
62
|
-
message = AuthoritativePutComponentOperation.read(buffer)!
|
|
63
|
-
} else if (header.type === CrdtMessageType.DELETE_COMPONENT) {
|
|
64
|
-
message = DeleteComponent.read(buffer)!
|
|
65
|
-
} else if (header.type === CrdtMessageType.DELETE_ENTITY) {
|
|
66
|
-
message = DeleteEntity.read(buffer)!
|
|
67
|
-
} else {
|
|
68
|
-
// consume unknown messages
|
|
69
|
-
buffer.incrementReadOffset(header.length)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (message) {
|
|
73
|
-
messages.push({
|
|
74
|
-
...message,
|
|
75
|
-
messageBuffer: buffer.buffer().subarray(offset, buffer.currentReadOffset())
|
|
76
|
-
})
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return messages
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function isNetworkMessage(message: ReceiveMessage): message is ReceiveNetworkMessage {
|
|
83
|
-
return [
|
|
84
|
-
CrdtMessageType.DELETE_COMPONENT_NETWORK,
|
|
85
|
-
CrdtMessageType.DELETE_ENTITY_NETWORK,
|
|
86
|
-
CrdtMessageType.PUT_COMPONENT_NETWORK
|
|
87
|
-
].includes(message.type)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function networkMessageToLocal(
|
|
91
|
-
message: ReceiveNetworkMessage,
|
|
92
|
-
localEntityId: Entity,
|
|
93
|
-
destinationBuffer: ByteBuffer,
|
|
94
|
-
// Optional network parent component for transform fixing
|
|
95
|
-
networkParentComponent?: typeof NetworkParent,
|
|
96
|
-
// Force corrections - converts PUT_COMPONENT_NETWORK to authoritative_PUT_COMPONENT
|
|
97
|
-
forceCorrections = false
|
|
98
|
-
): CrdtMessageBody {
|
|
99
|
-
if (message.type === CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
100
|
-
let messageData = message.data
|
|
101
|
-
|
|
102
|
-
// Fix transform parent if needed for Unity/engine processing
|
|
103
|
-
if (message.componentId === TransformComponentId && networkParentComponent) {
|
|
104
|
-
const parentNetwork = networkParentComponent.getOrNull(localEntityId)
|
|
105
|
-
messageData = fixTransformParent(message, parentNetwork?.entityId)
|
|
106
|
-
}
|
|
107
|
-
if (forceCorrections) {
|
|
108
|
-
// Use AUTHORITATIVE_PUT_COMPONENT for forced state updates
|
|
109
|
-
AuthoritativePutComponentOperation.write(
|
|
110
|
-
localEntityId,
|
|
111
|
-
message.timestamp,
|
|
112
|
-
message.componentId,
|
|
113
|
-
messageData,
|
|
114
|
-
destinationBuffer
|
|
115
|
-
)
|
|
116
|
-
return {
|
|
117
|
-
type: CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT,
|
|
118
|
-
componentId: message.componentId,
|
|
119
|
-
timestamp: message.timestamp,
|
|
120
|
-
data: messageData,
|
|
121
|
-
entityId: localEntityId
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
// Normal PUT_COMPONENT conversion
|
|
125
|
-
PutComponentOperation.write(localEntityId, message.timestamp, message.componentId, messageData, destinationBuffer)
|
|
126
|
-
return {
|
|
127
|
-
type: CrdtMessageType.PUT_COMPONENT,
|
|
128
|
-
componentId: message.componentId,
|
|
129
|
-
timestamp: message.timestamp,
|
|
130
|
-
data: messageData,
|
|
131
|
-
entityId: localEntityId
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
} else if (message.type === CrdtMessageType.DELETE_COMPONENT_NETWORK) {
|
|
135
|
-
DeleteComponent.write(localEntityId, message.componentId, message.timestamp, destinationBuffer)
|
|
136
|
-
return {
|
|
137
|
-
type: CrdtMessageType.DELETE_COMPONENT,
|
|
138
|
-
componentId: message.componentId,
|
|
139
|
-
timestamp: message.timestamp,
|
|
140
|
-
entityId: localEntityId
|
|
141
|
-
}
|
|
142
|
-
} else if (message.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
143
|
-
DeleteEntity.write(localEntityId, destinationBuffer)
|
|
144
|
-
return {
|
|
145
|
-
type: CrdtMessageType.DELETE_ENTITY,
|
|
146
|
-
entityId: localEntityId
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
throw 1
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export function localMessageToNetwork(
|
|
153
|
-
message: ReceiveMessage,
|
|
154
|
-
network: INetowrkEntityType,
|
|
155
|
-
destinationBuffer: ByteBuffer
|
|
156
|
-
) {
|
|
157
|
-
if (message.type === CrdtMessageType.PUT_COMPONENT) {
|
|
158
|
-
PutNetworkComponentOperation.write(
|
|
159
|
-
network.entityId,
|
|
160
|
-
message.timestamp,
|
|
161
|
-
message.componentId,
|
|
162
|
-
network.networkId,
|
|
163
|
-
message.data,
|
|
164
|
-
destinationBuffer
|
|
165
|
-
)
|
|
166
|
-
} else if (message.type === CrdtMessageType.DELETE_COMPONENT) {
|
|
167
|
-
DeleteComponentNetwork.write(
|
|
168
|
-
network.entityId,
|
|
169
|
-
message.componentId,
|
|
170
|
-
message.timestamp,
|
|
171
|
-
network.networkId,
|
|
172
|
-
destinationBuffer
|
|
173
|
-
)
|
|
174
|
-
} else if (message.type === CrdtMessageType.DELETE_ENTITY) {
|
|
175
|
-
DeleteEntityNetwork.write(network.entityId, network.networkId, destinationBuffer)
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export function fixTransformParent(message: ReceiveMessage, parent?: Entity): Uint8Array {
|
|
180
|
-
const buffer = new ReadWriteByteBuffer()
|
|
181
|
-
const transform = 'data' in message && TransformSchema.deserialize(new ReadWriteByteBuffer(message.data))
|
|
182
|
-
|
|
183
|
-
if (!transform) throw new Error('Invalid parent transform')
|
|
184
|
-
|
|
185
|
-
// Generate new transform raw data with the parent
|
|
186
|
-
const newTransform = { ...transform, parent }
|
|
187
|
-
TransformSchema.serialize(newTransform, buffer)
|
|
188
|
-
return buffer.toBinary()
|
|
189
|
-
}
|
package/src/server/env-var.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { getStorageServerUrl } from './storage-url'
|
|
2
|
-
import { assertIsServer, wrapSignedFetch } from './utils'
|
|
3
|
-
|
|
4
|
-
const MODULE_NAME = 'EnvVar'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* EnvVar provides methods to fetch environment variables from the
|
|
8
|
-
* Server Side Storage service. This module only works when running
|
|
9
|
-
* on server-side scenes.
|
|
10
|
-
*/
|
|
11
|
-
export const EnvVar = {
|
|
12
|
-
/**
|
|
13
|
-
* Fetches a specific environment variable by key as plain text.
|
|
14
|
-
*
|
|
15
|
-
* @param key - The name of the environment variable to fetch
|
|
16
|
-
* @returns A promise that resolves to the plain text value, or empty string if not found
|
|
17
|
-
* @throws Error if not running on a server-side scene
|
|
18
|
-
*/
|
|
19
|
-
async get(key: string): Promise<string> {
|
|
20
|
-
assertIsServer(MODULE_NAME)
|
|
21
|
-
|
|
22
|
-
const baseUrl = await getStorageServerUrl()
|
|
23
|
-
const url = `${baseUrl}/env/${encodeURIComponent(key)}`
|
|
24
|
-
|
|
25
|
-
const [error, data] = await wrapSignedFetch<{ value: string }>({
|
|
26
|
-
url
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
if (error) {
|
|
30
|
-
console.error(`Failed to fetch environment variable '${key}': ${error}`)
|
|
31
|
-
return ''
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return data?.value ?? ''
|
|
35
|
-
}
|
|
36
|
-
}
|
package/src/server/index.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export const MODULE_NAME = 'Storage'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Options for getValues pagination and filtering.
|
|
5
|
-
*/
|
|
6
|
-
export interface GetValuesOptions {
|
|
7
|
-
prefix?: string
|
|
8
|
-
limit?: number
|
|
9
|
-
offset?: number
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Result of getValues with pagination metadata.
|
|
14
|
-
*/
|
|
15
|
-
export interface GetValuesResult {
|
|
16
|
-
/** Key-value entries for the current page. */
|
|
17
|
-
data: Array<{ key: string; value: unknown }>
|
|
18
|
-
pagination: {
|
|
19
|
-
offset: number
|
|
20
|
-
total: number
|
|
21
|
-
}
|
|
22
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { createSceneStorage, ISceneStorage } from './scene'
|
|
2
|
-
import { createPlayerStorage, IPlayerStorage } from './player'
|
|
3
|
-
|
|
4
|
-
// Re-export interfaces and types
|
|
5
|
-
export { GetValuesOptions, GetValuesResult } from './constants'
|
|
6
|
-
export { ISceneStorage } from './scene'
|
|
7
|
-
export { IPlayerStorage } from './player'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Storage interface with methods for scene-scoped and player-scoped storage.
|
|
11
|
-
*/
|
|
12
|
-
export interface IStorage extends ISceneStorage {
|
|
13
|
-
/** Player-scoped storage for key-value pairs */
|
|
14
|
-
player: IPlayerStorage
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Creates the Storage module with scene-scoped and player-scoped storage.
|
|
19
|
-
*/
|
|
20
|
-
const createStorage = (): IStorage => {
|
|
21
|
-
const sceneStorage = createSceneStorage()
|
|
22
|
-
const playerStorage = createPlayerStorage()
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
// Spread scene storage methods at top level
|
|
26
|
-
get: sceneStorage.get,
|
|
27
|
-
set: sceneStorage.set,
|
|
28
|
-
delete: sceneStorage.delete,
|
|
29
|
-
getValues: sceneStorage.getValues,
|
|
30
|
-
// Keep player as nested property
|
|
31
|
-
player: playerStorage
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Storage provides methods to store and retrieve key-value data from the
|
|
37
|
-
* Server Side Storage service.
|
|
38
|
-
*
|
|
39
|
-
* - Use Storage.get/set/delete/getValues for scene-scoped storage
|
|
40
|
-
* - Use Storage.player.get/set/delete/getValues for player-scoped storage
|
|
41
|
-
*
|
|
42
|
-
* This module only works when running on server-side scenes.
|
|
43
|
-
*/
|
|
44
|
-
export const Storage: IStorage = createStorage()
|