@dcl/sdk 7.20.0 → 7.20.1-21975617794.commit-7189140
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/atom.d.ts +19 -0
- package/atom.js +83 -0
- package/future.d.ts +8 -0
- package/future.js +26 -0
- package/network/binary-message-bus.d.ts +6 -3
- package/network/binary-message-bus.js +9 -5
- package/network/chunking.d.ts +5 -0
- package/network/chunking.js +38 -0
- package/network/events/implementation.d.ts +93 -0
- package/network/events/implementation.js +230 -0
- package/network/events/index.d.ts +42 -0
- package/network/events/index.js +43 -0
- package/network/events/protocol.d.ts +27 -0
- package/network/events/protocol.js +66 -0
- package/network/events/registry.d.ts +8 -0
- package/network/events/registry.js +3 -0
- package/network/index.d.ts +8 -2
- package/network/index.js +16 -3
- package/network/message-bus-sync.d.ts +14 -1
- package/network/message-bus-sync.js +166 -103
- package/network/server/index.d.ts +14 -0
- package/network/server/index.js +219 -0
- package/network/server/utils.d.ts +18 -0
- package/network/server/utils.js +135 -0
- package/network/state.js +3 -5
- package/package.json +6 -6
- package/server/env-var.d.ts +15 -0
- package/server/env-var.js +31 -0
- package/server/index.d.ts +2 -0
- package/server/index.js +3 -0
- package/server/storage/constants.d.ts +1 -0
- package/server/storage/constants.js +2 -0
- package/server/storage/index.d.ts +21 -0
- package/server/storage/index.js +28 -0
- package/server/storage/player.d.ts +34 -0
- package/server/storage/player.js +61 -0
- package/server/storage/scene.d.ts +30 -0
- package/server/storage/scene.js +61 -0
- package/server/storage-url.d.ts +10 -0
- package/server/storage-url.js +29 -0
- package/server/utils.d.ts +35 -0
- package/server/utils.js +56 -0
- package/src/atom.ts +98 -0
- package/src/future.ts +38 -0
- package/src/network/binary-message-bus.ts +9 -4
- package/src/network/chunking.ts +45 -0
- package/src/network/events/implementation.ts +286 -0
- package/src/network/events/index.ts +48 -0
- package/src/network/events/protocol.ts +94 -0
- package/src/network/events/registry.ts +18 -0
- package/src/network/index.ts +40 -3
- package/src/network/message-bus-sync.ts +180 -110
- package/src/network/server/index.ts +301 -0
- package/src/network/server/utils.ts +189 -0
- package/src/network/state.ts +3 -4
- package/src/server/env-var.ts +36 -0
- package/src/server/index.ts +2 -0
- package/src/server/storage/constants.ts +1 -0
- package/src/server/storage/index.ts +42 -0
- package/src/server/storage/player.ts +106 -0
- package/src/server/storage/scene.ts +102 -0
- package/src/server/storage-url.ts +34 -0
- package/src/server/utils.ts +73 -0
|
@@ -0,0 +1,189 @@
|
|
|
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/network/state.ts
CHANGED
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
UiTransform,
|
|
26
26
|
ComponentDefinition
|
|
27
27
|
} from '@dcl/ecs'
|
|
28
|
-
import { LIVEKIT_MAX_SIZE } from '
|
|
28
|
+
import { LIVEKIT_MAX_SIZE } from './server'
|
|
29
29
|
|
|
30
30
|
export const NOT_SYNC_COMPONENTS: ComponentDefinition<unknown>[] = [
|
|
31
31
|
VideoEvent,
|
|
@@ -72,9 +72,9 @@ export function engineToCrdt(engine: IEngine): Uint8Array[] {
|
|
|
72
72
|
if (!shouldSyncComponent(itComponentDefinition)) {
|
|
73
73
|
continue
|
|
74
74
|
}
|
|
75
|
+
|
|
75
76
|
itComponentDefinition.dumpCrdtStateToBuffer(crdtBuffer, (entity) => {
|
|
76
|
-
|
|
77
|
-
return isNetworkEntity
|
|
77
|
+
return NetworkEntity.has(entity)
|
|
78
78
|
})
|
|
79
79
|
}
|
|
80
80
|
|
|
@@ -96,7 +96,6 @@ export function engineToCrdt(engine: IEngine): Uint8Array[] {
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
// If the message itself is larger than the limit, we need to handle it specially
|
|
99
|
-
// For now, we'll skip it to prevent infinite loops
|
|
100
99
|
if (messageSize / 1024 > LIVEKIT_MAX_SIZE) {
|
|
101
100
|
console.error(
|
|
102
101
|
`Message too large (${messageSize} bytes), skipping component ${message.componentId} for entity ${message.entityId}`
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const MODULE_NAME = 'Storage'
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { createSceneStorage, ISceneStorage } from './scene'
|
|
2
|
+
import { createPlayerStorage, IPlayerStorage } from './player'
|
|
3
|
+
|
|
4
|
+
// Re-export interfaces
|
|
5
|
+
export { ISceneStorage } from './scene'
|
|
6
|
+
export { IPlayerStorage } from './player'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Storage interface with methods for scene-scoped and player-scoped storage.
|
|
10
|
+
*/
|
|
11
|
+
export interface IStorage extends ISceneStorage {
|
|
12
|
+
/** Player-scoped storage for key-value pairs */
|
|
13
|
+
player: IPlayerStorage
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates the Storage module with scene-scoped and player-scoped storage.
|
|
18
|
+
*/
|
|
19
|
+
const createStorage = (): IStorage => {
|
|
20
|
+
const sceneStorage = createSceneStorage()
|
|
21
|
+
const playerStorage = createPlayerStorage()
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
// Spread scene storage methods at top level
|
|
25
|
+
get: sceneStorage.get,
|
|
26
|
+
set: sceneStorage.set,
|
|
27
|
+
delete: sceneStorage.delete,
|
|
28
|
+
// Keep player as nested property
|
|
29
|
+
player: playerStorage
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Storage provides methods to store and retrieve key-value data from the
|
|
35
|
+
* Server Side Storage service.
|
|
36
|
+
*
|
|
37
|
+
* - Use Storage.get/set/delete for scene-scoped storage
|
|
38
|
+
* - Use Storage.player.get/set/delete for player-scoped storage
|
|
39
|
+
*
|
|
40
|
+
* This module only works when running on server-side scenes.
|
|
41
|
+
*/
|
|
42
|
+
export const Storage: IStorage = createStorage()
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { getStorageServerUrl } from '../storage-url'
|
|
2
|
+
import { assertIsServer, wrapSignedFetch } from '../utils'
|
|
3
|
+
import { MODULE_NAME } from './constants'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Player-scoped storage interface for key-value pairs from the Server Side Storage service.
|
|
7
|
+
* This is NOT filesystem storage - data is stored in the remote storage service.
|
|
8
|
+
*/
|
|
9
|
+
export interface IPlayerStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves a value from a player's storage by key from the Server Side Storage service.
|
|
12
|
+
* @param address - The player's wallet address
|
|
13
|
+
* @param key - The key to retrieve
|
|
14
|
+
* @returns A promise that resolves to the parsed JSON value, or null if not found
|
|
15
|
+
*/
|
|
16
|
+
get<T = unknown>(address: string, key: string): Promise<T | null>
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Stores a value in a player's storage in the Server Side Storage service.
|
|
20
|
+
* @param address - The player's wallet address
|
|
21
|
+
* @param key - The key to store the value under
|
|
22
|
+
* @param value - The value to store (will be JSON serialized)
|
|
23
|
+
* @returns A promise that resolves to true if successful, false otherwise
|
|
24
|
+
*/
|
|
25
|
+
set<T = unknown>(address: string, key: string, value: T): Promise<boolean>
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Deletes a value from a player's storage in the Server Side Storage service.
|
|
29
|
+
* @param address - The player's wallet address
|
|
30
|
+
* @param key - The key to delete
|
|
31
|
+
* @returns A promise that resolves to true if deleted, false if not found
|
|
32
|
+
*/
|
|
33
|
+
delete(address: string, key: string): Promise<boolean>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates player-scoped storage that provides methods to interact with
|
|
38
|
+
* player-specific key-value pairs from the Server Side Storage service.
|
|
39
|
+
* This module only works when running on server-side scenes.
|
|
40
|
+
*/
|
|
41
|
+
export const createPlayerStorage = (): IPlayerStorage => {
|
|
42
|
+
return {
|
|
43
|
+
async get<T = unknown>(address: string, key: string): Promise<T | null> {
|
|
44
|
+
assertIsServer(MODULE_NAME)
|
|
45
|
+
|
|
46
|
+
const baseUrl = await getStorageServerUrl()
|
|
47
|
+
const url = `${baseUrl}/players/${encodeURIComponent(address)}/values/${encodeURIComponent(key)}`
|
|
48
|
+
|
|
49
|
+
const [error, data] = await wrapSignedFetch<{ value: T }>({ url })
|
|
50
|
+
|
|
51
|
+
if (error) {
|
|
52
|
+
console.error(`Failed to get player storage value '${key}' for '${address}': ${error}`)
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return data?.value ?? null
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
async set<T = unknown>(address: string, key: string, value: T): Promise<boolean> {
|
|
60
|
+
assertIsServer(MODULE_NAME)
|
|
61
|
+
|
|
62
|
+
const baseUrl = await getStorageServerUrl()
|
|
63
|
+
const url = `${baseUrl}/players/${encodeURIComponent(address)}/values/${encodeURIComponent(key)}`
|
|
64
|
+
|
|
65
|
+
const [error] = await wrapSignedFetch({
|
|
66
|
+
url,
|
|
67
|
+
init: {
|
|
68
|
+
method: 'PUT',
|
|
69
|
+
headers: {
|
|
70
|
+
'content-type': 'application/json'
|
|
71
|
+
},
|
|
72
|
+
body: JSON.stringify({ value })
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
if (error) {
|
|
77
|
+
console.error(`Failed to set player storage value '${key}' for '${address}': ${error}`)
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return true
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
async delete(address: string, key: string): Promise<boolean> {
|
|
85
|
+
assertIsServer(MODULE_NAME)
|
|
86
|
+
|
|
87
|
+
const baseUrl = await getStorageServerUrl()
|
|
88
|
+
const url = `${baseUrl}/players/${encodeURIComponent(address)}/values/${encodeURIComponent(key)}`
|
|
89
|
+
|
|
90
|
+
const [error] = await wrapSignedFetch({
|
|
91
|
+
url,
|
|
92
|
+
init: {
|
|
93
|
+
method: 'DELETE',
|
|
94
|
+
headers: {}
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
if (error) {
|
|
99
|
+
console.error(`Failed to delete player storage value '${key}' for '${address}': ${error}`)
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return true
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { getStorageServerUrl } from '../storage-url'
|
|
2
|
+
import { assertIsServer, wrapSignedFetch } from '../utils'
|
|
3
|
+
import { MODULE_NAME } from './constants'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Scene-scoped storage interface for key-value pairs from the Server Side Storage service.
|
|
7
|
+
* This is NOT filesystem storage - data is stored in the remote storage service.
|
|
8
|
+
*/
|
|
9
|
+
export interface ISceneStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves a value from scene storage by key from the Server Side Storage service.
|
|
12
|
+
* @param key - The key to retrieve
|
|
13
|
+
* @returns A promise that resolves to the parsed JSON value, or null if not found
|
|
14
|
+
*/
|
|
15
|
+
get<T = unknown>(key: string): Promise<T | null>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Stores a value in scene storage in the Server Side Storage service.
|
|
19
|
+
* @param key - The key to store the value under
|
|
20
|
+
* @param value - The value to store (will be JSON serialized)
|
|
21
|
+
*/
|
|
22
|
+
set<T = unknown>(key: string, value: T): Promise<boolean>
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Deletes a value from scene storage in the Server Side Storage service.
|
|
26
|
+
* @param key - The key to delete
|
|
27
|
+
* @returns A promise that resolves to true if deleted, false if not found
|
|
28
|
+
*/
|
|
29
|
+
delete(key: string): Promise<boolean>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates scene-scoped storage that provides methods to interact with
|
|
34
|
+
* scene-specific key-value pairs from the Server Side Storage service.
|
|
35
|
+
* This module only works when running on server-side scenes.
|
|
36
|
+
*/
|
|
37
|
+
export const createSceneStorage = (): ISceneStorage => {
|
|
38
|
+
return {
|
|
39
|
+
async get<T = unknown>(key: string): Promise<T | null> {
|
|
40
|
+
assertIsServer(MODULE_NAME)
|
|
41
|
+
|
|
42
|
+
const baseUrl = await getStorageServerUrl()
|
|
43
|
+
const url = `${baseUrl}/values/${encodeURIComponent(key)}`
|
|
44
|
+
|
|
45
|
+
const [error, data] = await wrapSignedFetch<{ value: T }>({ url })
|
|
46
|
+
|
|
47
|
+
if (error) {
|
|
48
|
+
console.error(`Failed to get storage value '${key}': ${error}`)
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return data?.value ?? null
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
async set<T = unknown>(key: string, value: T): Promise<boolean> {
|
|
56
|
+
assertIsServer(MODULE_NAME)
|
|
57
|
+
|
|
58
|
+
const baseUrl = await getStorageServerUrl()
|
|
59
|
+
const url = `${baseUrl}/values/${encodeURIComponent(key)}`
|
|
60
|
+
|
|
61
|
+
const [error] = await wrapSignedFetch({
|
|
62
|
+
url,
|
|
63
|
+
init: {
|
|
64
|
+
method: 'PUT',
|
|
65
|
+
headers: {
|
|
66
|
+
'content-type': 'application/json'
|
|
67
|
+
},
|
|
68
|
+
body: JSON.stringify({ value })
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
if (error) {
|
|
73
|
+
console.error(`Failed to set storage value '${key}': ${error}`)
|
|
74
|
+
return false
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return true
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
async delete(key: string): Promise<boolean> {
|
|
81
|
+
assertIsServer(MODULE_NAME)
|
|
82
|
+
|
|
83
|
+
const baseUrl = await getStorageServerUrl()
|
|
84
|
+
const url = `${baseUrl}/values/${encodeURIComponent(key)}`
|
|
85
|
+
|
|
86
|
+
const [error] = await wrapSignedFetch({
|
|
87
|
+
url,
|
|
88
|
+
init: {
|
|
89
|
+
method: 'DELETE',
|
|
90
|
+
headers: {}
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
if (error) {
|
|
95
|
+
console.error(`Failed to delete storage value '${key}': ${error}`)
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return true
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getRealm } from '~system/Runtime'
|
|
2
|
+
|
|
3
|
+
const STORAGE_SERVER_ORG = 'https://storage.decentraland.org'
|
|
4
|
+
const STORAGE_SERVER_ZONE = 'https://storage.decentraland.zone'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Determines the correct storage server URL based on the current realm.
|
|
8
|
+
*
|
|
9
|
+
* - If `isPreview` is true, uses the realm's baseUrl (localhost)
|
|
10
|
+
* - If the realm's baseUrl contains `.zone`, uses storage.decentraland.zone
|
|
11
|
+
* - Otherwise, uses storage.decentraland.org (production)
|
|
12
|
+
*
|
|
13
|
+
* @returns The storage server base URL
|
|
14
|
+
*/
|
|
15
|
+
export async function getStorageServerUrl(): Promise<string> {
|
|
16
|
+
const { realmInfo } = await getRealm({})
|
|
17
|
+
|
|
18
|
+
if (!realmInfo) {
|
|
19
|
+
throw new Error('Unable to retrieve realm information')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Local development / preview mode
|
|
23
|
+
if (realmInfo.isPreview) {
|
|
24
|
+
return realmInfo.baseUrl
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Staging / testing environment
|
|
28
|
+
if (realmInfo.baseUrl.includes('.zone')) {
|
|
29
|
+
return STORAGE_SERVER_ZONE
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Production environment
|
|
33
|
+
return STORAGE_SERVER_ORG
|
|
34
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { signedFetch, SignedFetchRequest } from '~system/SignedFetch'
|
|
2
|
+
import { isServer } from '../network'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Validates that the code is running on a server-side scene.
|
|
6
|
+
* Throws an error if called from a client-side context.
|
|
7
|
+
*
|
|
8
|
+
* @param moduleName - The name of the module for the error message
|
|
9
|
+
* @throws Error if not running on a server-side scene
|
|
10
|
+
*/
|
|
11
|
+
export function assertIsServer(moduleName: string): void {
|
|
12
|
+
if (!isServer()) {
|
|
13
|
+
throw new Error(`${moduleName} is only available on server-side scenes`)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Result type for operations that can fail.
|
|
19
|
+
* Returns a tuple of [error, null] on failure or [null, data] on success.
|
|
20
|
+
*/
|
|
21
|
+
export type Result<T, E = string> = [E, null] | [null, T]
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Extended result type that includes HTTP status code information.
|
|
25
|
+
*/
|
|
26
|
+
export type FetchResult<T> = [string, null, number?] | [null, T, number]
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Wraps a promise to catch errors and return a Result tuple.
|
|
30
|
+
* This allows for cleaner error handling without try-catch blocks.
|
|
31
|
+
*
|
|
32
|
+
* @param promise - The promise to wrap
|
|
33
|
+
* @returns A tuple of [error, null] on failure or [null, data] on success
|
|
34
|
+
*/
|
|
35
|
+
export async function tryCatch<T, E = Error>(promise: Promise<T>): Promise<Result<T, E>> {
|
|
36
|
+
try {
|
|
37
|
+
const data = await promise
|
|
38
|
+
return [null, data]
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return [error as E, null]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Wraps signedFetch with automatic error handling and JSON parsing.
|
|
46
|
+
* Returns a FetchResult tuple with parsed JSON data or error message and status code.
|
|
47
|
+
*
|
|
48
|
+
* @param signedFetchBody - The signedFetch request configuration
|
|
49
|
+
* @returns A tuple of [error, null, statusCode?] on failure or [null, data, statusCode] on success
|
|
50
|
+
*/
|
|
51
|
+
export async function wrapSignedFetch<T = unknown>(signedFetchBody: SignedFetchRequest): Promise<FetchResult<T>> {
|
|
52
|
+
const [error, response] = await tryCatch(signedFetch(signedFetchBody))
|
|
53
|
+
|
|
54
|
+
if (error) {
|
|
55
|
+
console.error(`Error in ${signedFetchBody.url} endpoint`, { error })
|
|
56
|
+
return [error.message, null, undefined]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
const errorMessage = `${response.status} ${response.statusText}`
|
|
61
|
+
console.error(`Error in ${signedFetchBody.url} endpoint`, { response })
|
|
62
|
+
return [errorMessage, null, response.status]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const [parseError, body] = await tryCatch<T>(JSON.parse(response.body || '{}'))
|
|
66
|
+
|
|
67
|
+
if (parseError) {
|
|
68
|
+
console.error(`Failed to parse response from ${signedFetchBody.url}`)
|
|
69
|
+
return ['Failed to parse response', null, response.status]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return [null, (body ?? {}) as T, response.status]
|
|
73
|
+
}
|