@dcl/sdk 7.29.1-36863326504.commit-aa54c50 → 7.30.0

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 (80) hide show
  1. package/explorer-ui/index.d.ts +118 -0
  2. package/explorer-ui/index.js +235 -0
  3. package/internal/transports/rendererTransport.js +2 -5
  4. package/network/binary-message-bus.d.ts +3 -6
  5. package/network/binary-message-bus.js +5 -9
  6. package/network/index.d.ts +2 -8
  7. package/network/index.js +3 -16
  8. package/network/message-bus-sync.d.ts +3 -16
  9. package/network/message-bus-sync.js +103 -164
  10. package/network/state.js +5 -3
  11. package/package.json +6 -6
  12. package/src/explorer-ui/index.ts +398 -0
  13. package/src/internal/transports/rendererTransport.ts +1 -4
  14. package/src/network/binary-message-bus.ts +4 -9
  15. package/src/network/index.ts +3 -40
  16. package/src/network/message-bus-sync.ts +112 -180
  17. package/src/network/state.ts +4 -3
  18. package/atom.d.ts +0 -19
  19. package/atom.js +0 -83
  20. package/future.d.ts +0 -8
  21. package/future.js +0 -26
  22. package/network/chunking.d.ts +0 -5
  23. package/network/chunking.js +0 -38
  24. package/network/events/implementation.d.ts +0 -93
  25. package/network/events/implementation.js +0 -221
  26. package/network/events/index.d.ts +0 -42
  27. package/network/events/index.js +0 -43
  28. package/network/events/protocol.d.ts +0 -27
  29. package/network/events/protocol.js +0 -66
  30. package/network/events/registry.d.ts +0 -8
  31. package/network/events/registry.js +0 -3
  32. package/network/server/index.d.ts +0 -14
  33. package/network/server/index.js +0 -256
  34. package/network/server/utils.d.ts +0 -18
  35. package/network/server/utils.js +0 -135
  36. package/server/env-var.d.ts +0 -15
  37. package/server/env-var.js +0 -31
  38. package/server/index.d.ts +0 -2
  39. package/server/index.js +0 -3
  40. package/server/storage/constants.d.ts +0 -80
  41. package/server/storage/constants.js +0 -19
  42. package/server/storage/error-message.d.ts +0 -1
  43. package/server/storage/error-message.js +0 -9
  44. package/server/storage/index.d.ts +0 -54
  45. package/server/storage/index.js +0 -60
  46. package/server/storage/player.d.ts +0 -60
  47. package/server/storage/player.js +0 -244
  48. package/server/storage/scene.d.ts +0 -55
  49. package/server/storage/scene.js +0 -232
  50. package/server/storage/serialize.d.ts +0 -1
  51. package/server/storage/serialize.js +0 -135
  52. package/server/storage/value-cache.d.ts +0 -1
  53. package/server/storage/value-cache.js +0 -67
  54. package/server/storage/write-queue.d.ts +0 -1
  55. package/server/storage/write-queue.js +0 -113
  56. package/server/storage-url.d.ts +0 -13
  57. package/server/storage-url.js +0 -42
  58. package/server/utils.d.ts +0 -35
  59. package/server/utils.js +0 -69
  60. package/src/atom.ts +0 -98
  61. package/src/future.ts +0 -38
  62. package/src/network/chunking.ts +0 -45
  63. package/src/network/events/implementation.ts +0 -271
  64. package/src/network/events/index.ts +0 -48
  65. package/src/network/events/protocol.ts +0 -94
  66. package/src/network/events/registry.ts +0 -18
  67. package/src/network/server/index.ts +0 -341
  68. package/src/network/server/utils.ts +0 -189
  69. package/src/server/env-var.ts +0 -36
  70. package/src/server/index.ts +0 -12
  71. package/src/server/storage/constants.ts +0 -112
  72. package/src/server/storage/error-message.ts +0 -8
  73. package/src/server/storage/index.ts +0 -83
  74. package/src/server/storage/player.ts +0 -345
  75. package/src/server/storage/scene.ts +0 -324
  76. package/src/server/storage/serialize.ts +0 -132
  77. package/src/server/storage/value-cache.ts +0 -119
  78. package/src/server/storage/write-queue.ts +0 -174
  79. package/src/server/storage-url.ts +0 -49
  80. package/src/server/utils.ts +0 -83
@@ -1,8 +0,0 @@
1
- /**
2
- * The message of a thrown value, or `fallback` when it carries none.
3
- * @internal
4
- */
5
- export function errorMessage(error: unknown, fallback = 'unknown error'): string {
6
- const message = error instanceof Error ? error.message : typeof error === 'string' ? error : ''
7
- return message || fallback
8
- }
@@ -1,83 +0,0 @@
1
- import { createStorageConfig, StorageOptions } from './constants'
2
- import { createSceneStorage, ISceneStorage } from './scene'
3
- import { createPlayerStorage, IPlayerStorage } from './player'
4
-
5
- // Re-export interfaces and types
6
- export { GetOptions, GetValuesOptions, GetValuesResult, SetOptions, StorageOptions } from './constants'
7
- export { ISceneStorage } from './scene'
8
- export { IPlayerStorage } from './player'
9
-
10
- /**
11
- * Storage interface with methods for scene-scoped and player-scoped storage.
12
- */
13
- export interface IStorage extends ISceneStorage {
14
- /** Player-scoped storage for key-value pairs */
15
- player: IPlayerStorage
16
-
17
- /**
18
- * Sets module-wide defaults for both scene-scoped and player-scoped storage.
19
- * Merges the given partial options into the current configuration.
20
- * Note: cacheMaxEntries applies per scope (scene and player each keep their
21
- * own cache) and bounds entry count, not bytes (entries hold serialized
22
- * value bodies).
23
- */
24
- configure(options: StorageOptions): void
25
- }
26
-
27
- /**
28
- * Creates the Storage module with scene-scoped and player-scoped storage.
29
- */
30
- const createStorage = (): IStorage => {
31
- const config = createStorageConfig()
32
- const sceneStorage = createSceneStorage(config)
33
- const playerStorage = createPlayerStorage(config)
34
-
35
- return {
36
- // Spread scene storage methods at top level
37
- get: sceneStorage.get,
38
- set: sceneStorage.set,
39
- delete: sceneStorage.delete,
40
- getValues: sceneStorage.getValues,
41
- // Keep player as nested property
42
- player: playerStorage,
43
- configure(options: StorageOptions): void {
44
- for (const [key, value] of Object.entries(options)) {
45
- if (value !== undefined) (config as unknown as Record<string, unknown>)[key] = value
46
- }
47
- }
48
- }
49
- }
50
-
51
- /**
52
- * Storage provides methods to store and retrieve key-value data from the
53
- * Server Side Storage service.
54
- *
55
- * - Use Storage.get/set/delete/getValues for scene-scoped storage
56
- * - Use Storage.player.get/set/delete/getValues for player-scoped storage
57
- * - Use Storage.configure to change module-wide defaults (e.g. skipIfUnchanged, cacheReads)
58
- *
59
- * A failure is never reported as absence. A read that fails throws; get() resolves
60
- * null only for a confirmed absence (a 404, or a delete that landed) or a stored
61
- * JSON null, and getValues() returns an empty page only for a genuinely empty
62
- * listing. A delete that fails throws, and resolves false only for a confirmed 404.
63
- * set() reports failure through its boolean result, and throws for a value that
64
- * cannot be JSON-serialized. A get() issued while writes to its key are pending
65
- * waits for them, so it reflects them; getValues() does not wait. Nothing is
66
- * retried — that is the caller's to decide.
67
- *
68
- * Reads are cached by default: get() serves values known from a network
69
- * round-trip within the last cacheMaxAgeMs (including confirmed "not found"
70
- * results) without hitting the service, and concurrent gets for the same key
71
- * share one request. Overlapping writes to the same key are serialized so the
72
- * service commits them in issue order, and rapid writes coalesce to the
73
- * latest value; a set()/delete() settles once its own write, or the newer
74
- * write that replaced it, lands or fails. By default (skipIfUnchanged)
75
- * an unchanged set is skipped, but only when a previous confirmed round-trip
76
- * proved the exact value is already stored. Out-of-band writers (e.g. CLI storage commands)
77
- * may not be visible for up to cacheMaxAgeMs; use get(key, { fresh: true })
78
- * for an authoritative read or Storage.configure({ cacheReads: false }) to
79
- * disable read caching.
80
- *
81
- * This module only works when running on server-side scenes.
82
- */
83
- export const Storage: IStorage = createStorage()
@@ -1,345 +0,0 @@
1
- import { getStorageServerUrl } from '../storage-url'
2
- import { assertIsServer, wrapSignedFetch } from '../utils'
3
- import {
4
- createStorageConfig,
5
- GetOptions,
6
- GetValuesOptions,
7
- GetValuesResult,
8
- MODULE_NAME,
9
- SetOptions,
10
- StorageConfigState
11
- } from './constants'
12
- import { createValueCache } from './value-cache'
13
- import { errorMessage } from './error-message'
14
- import { assertPlayerAddress, assertStorageKey, serializeStorageValue } from './serialize'
15
- import { createWriteQueue, SupersedingWriteFailed } from './write-queue'
16
-
17
- /**
18
- * Player-scoped storage interface for key-value pairs from the Server Side Storage service.
19
- * This is NOT filesystem storage - data is stored in the remote storage service.
20
- */
21
- export interface IPlayerStorage {
22
- /**
23
- * Retrieves a value from a player's storage by key from the Server Side Storage service.
24
- *
25
- * By default (cacheReads), values read or written during the last cacheMaxAgeMs
26
- * are served from a local cache without a network request, including confirmed
27
- * "not found" results. Concurrent gets for the same player and key share one
28
- * request. Out-of-band writers (e.g. CLI storage commands) may not be visible
29
- * for up to cacheMaxAgeMs — pass { fresh: true } to force a network read.
30
- * A read first waits for writes to the same key that were pending when it was
31
- * issued, so it reflects every write issued before it.
32
- * @param address - The player's wallet address
33
- * @param key - The key to retrieve
34
- * @param options - Optional { fresh } to bypass the read cache
35
- * @returns A promise that resolves to the parsed JSON value, or null if not found
36
- * @throws Error if the read fails, so a failure is never mistaken for a missing key
37
- * @throws TypeError if the address or key is invalid
38
- */
39
- get<T = unknown>(address: string, key: string, options?: GetOptions): Promise<T | null>
40
-
41
- /**
42
- * Stores a value in a player's storage in the Server Side Storage service.
43
- * @param address - The player's wallet address
44
- * @param key - The key to store the value under
45
- * @param value - The value to store (will be JSON serialized)
46
- * @param options - Optional { skipIfUnchanged } to skip the network write when the value is already stored
47
- * @returns true once stored, or once a newer write that replaced this one lands
48
- * (rapid writes coalesce); false if the write, or its replacement, fails
49
- * @throws TypeError if the address or key is invalid, or the value cannot be stored unchanged: undefined,
50
- * circular, a BigInt, or containing a function, a symbol, a non-finite number, an undefined array
51
- * element or hole, a Map, Set, typed array or other built-in with no JSON form, or text with an
52
- * unpaired surrogate or a NUL character. An undefined object property is dropped; a Date is
53
- * stored as its ISO string and a class instance as its own enumerable data.
54
- */
55
- set<T = unknown>(address: string, key: string, value: T, options?: SetOptions): Promise<boolean>
56
-
57
- /**
58
- * Deletes a value from a player's storage in the Server Side Storage service.
59
- * @param address - The player's wallet address
60
- * @param key - The key to delete
61
- * @returns true once applied, or once a newer write that replaced it lands;
62
- * false only for a confirmed 404
63
- * @throws Error if the delete, or its replacement, fails; a failure is never reported as absence
64
- * @throws TypeError if the address or key is invalid
65
- */
66
- delete(address: string, key: string): Promise<boolean>
67
-
68
- /**
69
- * Returns key-value entries from a player's storage, optionally filtered by prefix.
70
- * Supports pagination via limit and offset.
71
- * @param address - The player's wallet address
72
- * @param options - Optional { prefix, limit, offset } for filtering and pagination.
73
- * @returns A promise that resolves to { data, pagination: { offset, total } } for pagination UI
74
- * @throws Error if the read fails, so a failure is never mistaken for an empty page
75
- * @throws TypeError if the address is invalid
76
- */
77
- getValues(address: string, options?: GetValuesOptions): Promise<GetValuesResult>
78
- }
79
-
80
- /**
81
- * Creates player-scoped storage that provides methods to interact with
82
- * player-specific key-value pairs from the Server Side Storage service.
83
- * This module only works when running on server-side scenes.
84
- * @internal
85
- */
86
- export const createPlayerStorage = (config: StorageConfigState = createStorageConfig()): IPlayerStorage => {
87
- const cache = createValueCache(config)
88
- // Each in-flight GET is tracked by a wrapper object whose identity marks
89
- // ownership: a landed write detaches the pending GET so its stale response is not cached.
90
- const inflightGets = new Map<string, { promise: Promise<unknown> }>()
91
-
92
- // Lowercased so checksummed callers share an entry.
93
- const cacheKey = (address: string, key: string) => JSON.stringify([address.toLowerCase(), key])
94
-
95
- // Writes to the same player key are serialized (and rapid ones coalesced to
96
- // the latest value) so the service commits them in issue order — overlapping
97
- // PUTs would otherwise leave both the kept value and the cached value to
98
- // response-order chance. Keyed by the same case-insensitive cache key.
99
- const writes = createWriteQueue()
100
-
101
- async function executeSet(address: string, key: string, ck: string, body: string): Promise<boolean> {
102
- let baseUrl: string
103
- try {
104
- baseUrl = await getStorageServerUrl()
105
- } catch (error) {
106
- cache.delete(ck)
107
- console.error(`Failed to set player storage value '${key}' for '${address}': ${errorMessage(error)}`)
108
- return false
109
- }
110
- const url = `${baseUrl}/players/${encodeURIComponent(address)}/values/${encodeURIComponent(key)}`
111
-
112
- const [error] = await wrapSignedFetch({
113
- url,
114
- init: {
115
- method: 'PUT',
116
- headers: {
117
- 'content-type': 'application/json'
118
- },
119
- body
120
- }
121
- })
122
-
123
- // Either way the entry changed server-side (or may have): detach any
124
- // overlapping in-flight GET so its stale response is not cached.
125
- inflightGets.delete(ck)
126
-
127
- if (error) {
128
- // The PUT may have reached the server, so the cached body is no
129
- // longer reliable.
130
- cache.delete(ck)
131
- return false
132
- }
133
-
134
- cache.set(ck, { body })
135
- return true
136
- }
137
-
138
- async function executeDelete(address: string, key: string, ck: string): Promise<boolean> {
139
- let baseUrl: string
140
- try {
141
- baseUrl = await getStorageServerUrl()
142
- } catch (error) {
143
- cache.delete(ck)
144
- throw new Error(`Failed to delete player storage value '${key}' for '${address}': ${errorMessage(error)}`)
145
- }
146
- const url = `${baseUrl}/players/${encodeURIComponent(address)}/values/${encodeURIComponent(key)}`
147
-
148
- const [error, , status] = await wrapSignedFetch({
149
- url,
150
- init: {
151
- method: 'DELETE',
152
- headers: {}
153
- }
154
- })
155
-
156
- // Detach again: a GET may have started while the DELETE was in flight.
157
- inflightGets.delete(ck)
158
-
159
- if (error) {
160
- // A 404 still confirms the key is absent server-side.
161
- if (status === 404) {
162
- cache.setAbsent(ck)
163
- return false
164
- }
165
- // The DELETE may have reached the server, so the cached body is unreliable.
166
- cache.delete(ck)
167
- throw new Error(`Failed to delete player storage value '${key}' for '${address}': ${errorMessage(error)}`)
168
- }
169
-
170
- cache.setAbsent(ck)
171
- return true
172
- }
173
-
174
- return {
175
- async get<T = unknown>(address: string, key: string, options?: GetOptions): Promise<T | null> {
176
- assertIsServer(MODULE_NAME)
177
- assertPlayerAddress(address, 'Storage.player.get()')
178
- assertStorageKey(key, 'Storage.player.get()')
179
-
180
- const ck = cacheKey(address, key)
181
-
182
- if (writes.isPending(ck)) await writes.settled(ck)
183
-
184
- if (config.cacheReads && !options?.fresh) {
185
- const entry = cache.get(ck)
186
- if (entry?.absent) return null
187
- // Parse per hit so each caller gets a fresh object (no shared mutation).
188
- if (entry?.body !== undefined) return JSON.parse(entry.body).value as T
189
- }
190
-
191
- // Coalesce concurrent gets (even fresh ones: an in-flight response is
192
- // milliseconds old, not TTL-stale) into a single network request.
193
- const joined = inflightGets.get(ck)
194
- if (joined) return joined.promise as Promise<T | null>
195
-
196
- const inflight = {} as { promise: Promise<T | null> }
197
- inflight.promise = (async () => {
198
- try {
199
- let baseUrl: string
200
- try {
201
- baseUrl = await getStorageServerUrl()
202
- } catch (error) {
203
- throw new Error(`Failed to get player storage value '${key}' for '${address}': ${errorMessage(error)}`)
204
- }
205
- const url = `${baseUrl}/players/${encodeURIComponent(address)}/values/${encodeURIComponent(key)}`
206
-
207
- const [error, data, status] = await wrapSignedFetch<{ value: T }>({ url })
208
-
209
- const isOwner = inflightGets.get(ck) === inflight
210
-
211
- if (error) {
212
- // A confirmed 404 is a first-class "absent" outcome, not a failure.
213
- if (status === 404) {
214
- if (isOwner) cache.setAbsent(ck)
215
- return null
216
- }
217
- throw new Error(`Failed to get player storage value '${key}' for '${address}': ${errorMessage(error)}`)
218
- }
219
-
220
- // wrapSignedFetch parses an empty 2xx body as {}: a missing value is a fault, not an absence.
221
- if (!data || data.value === undefined) {
222
- throw new Error(`Failed to get player storage value '${key}' for '${address}': response carried no value`)
223
- }
224
-
225
- // Same serialization shape as set()'s PUT body, so a read followed by
226
- // an unchanged write can be skipped.
227
- const body = JSON.stringify({ value: data.value })
228
- if (isOwner) cache.set(ck, { body })
229
- return data.value
230
- } finally {
231
- if (inflightGets.get(ck) === inflight) inflightGets.delete(ck)
232
- }
233
- })()
234
-
235
- inflightGets.set(ck, inflight)
236
- return inflight.promise
237
- },
238
-
239
- async set<T = unknown>(address: string, key: string, value: T, options?: SetOptions): Promise<boolean> {
240
- assertIsServer(MODULE_NAME)
241
- assertPlayerAddress(address, 'Storage.player.set()')
242
- assertStorageKey(key, 'Storage.player.set()')
243
-
244
- const ck = cacheKey(address, key)
245
- const body = serializeStorageValue(value, `Storage.player.set('${address}', '${key}')`)
246
- const skipIfUnchanged = options?.skipIfUnchanged ?? config.skipIfUnchanged
247
-
248
- // Dedup against confirmed state only while no write is pending — a
249
- // pending write makes the cache momentarily stale; enqueue() coalesces
250
- // against pending writes instead.
251
- if (skipIfUnchanged && writes.pending(ck) === undefined && cache.get(ck)?.body === body) {
252
- return true
253
- }
254
-
255
- return writes.enqueue(ck, body, (b) => executeSet(address, key, ck, b as string), skipIfUnchanged)
256
- },
257
-
258
- async delete(address: string, key: string): Promise<boolean> {
259
- assertIsServer(MODULE_NAME)
260
- assertPlayerAddress(address, 'Storage.player.delete()')
261
- assertStorageKey(key, 'Storage.player.delete()')
262
-
263
- const ck = cacheKey(address, key)
264
-
265
- return writes
266
- .enqueue(ck, null, () => executeDelete(address, key, ck), true)
267
- .catch((error: unknown) => {
268
- if (error instanceof SupersedingWriteFailed) {
269
- throw new Error(`Failed to delete player storage value '${key}' for '${address}': ${error.message}`)
270
- }
271
- throw error
272
- })
273
- },
274
-
275
- async getValues(address: string, options?: GetValuesOptions): Promise<GetValuesResult> {
276
- assertIsServer(MODULE_NAME)
277
- assertPlayerAddress(address, 'Storage.player.getValues()')
278
-
279
- const { prefix, limit, offset } = options ?? {}
280
- let baseUrl: string
281
- try {
282
- baseUrl = await getStorageServerUrl()
283
- } catch (error) {
284
- throw new Error(`Failed to get player storage values for '${address}': ${errorMessage(error)}`)
285
- }
286
- const parts: string[] = []
287
-
288
- if (!!prefix) {
289
- parts.push(`prefix=${encodeURIComponent(prefix)}`)
290
- }
291
-
292
- if (!!limit) {
293
- parts.push(`limit=${limit}`)
294
- }
295
-
296
- if (!!offset) {
297
- parts.push(`offset=${offset}`)
298
- }
299
-
300
- const query = parts.join('&')
301
- const url = query
302
- ? `${baseUrl}/players/${encodeURIComponent(address)}/values?${query}`
303
- : `${baseUrl}/players/${encodeURIComponent(address)}/values`
304
-
305
- const watcher = cache.watch()
306
- const [error, response] = await wrapSignedFetch<GetValuesResult>({ url })
307
- watcher.stop()
308
-
309
- if (error) {
310
- throw new Error(`Failed to get player storage values for '${address}': ${errorMessage(error)}`)
311
- }
312
-
313
- const data = response?.data
314
- if (!Array.isArray(data)) {
315
- throw new Error(`Failed to get player storage values for '${address}': response carried no data array`)
316
- }
317
- if (data.some((entry) => typeof entry?.key !== 'string' || entry.value === undefined)) {
318
- throw new Error(`Failed to get player storage values for '${address}': response carried a malformed entry`)
319
- }
320
-
321
- // Seed the per-key cache so subsequent get()/set() on returned keys can
322
- // skip the network. Only keys with no live entry and no pending write
323
- // are seeded: existing per-key state comes from a confirmed operation
324
- // that this page snapshot — whose request started earlier — must not
325
- // clobber with stale data. Absence is never seeded (prefix/pagination
326
- // make it non-authoritative). A page larger than cacheMaxEntries churns
327
- // the cache; entries repopulate lazily.
328
- for (const entry of data) {
329
- const ck = cacheKey(address, entry.key)
330
- if (watcher.mutated(ck) || writes.isPending(ck) || inflightGets.has(ck)) continue
331
- if (cache.get(ck) === undefined) {
332
- cache.set(ck, { body: JSON.stringify({ value: entry.value }) })
333
- }
334
- }
335
-
336
- const requestedOffset = offset ?? 0
337
- const pagination = {
338
- offset: response!.pagination?.offset ?? requestedOffset,
339
- total: response!.pagination?.total ?? data.length
340
- }
341
-
342
- return { data, pagination }
343
- }
344
- }
345
- }