@dcl/sdk 7.26.0 → 7.26.1-31714079767.commit-96e9a29

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 (69) hide show
  1. package/atom.d.ts +19 -0
  2. package/atom.js +83 -0
  3. package/future.d.ts +8 -0
  4. package/future.js +26 -0
  5. package/network/binary-message-bus.d.ts +6 -3
  6. package/network/binary-message-bus.js +9 -5
  7. package/network/chunking.d.ts +5 -0
  8. package/network/chunking.js +38 -0
  9. package/network/events/implementation.d.ts +93 -0
  10. package/network/events/implementation.js +221 -0
  11. package/network/events/index.d.ts +42 -0
  12. package/network/events/index.js +43 -0
  13. package/network/events/protocol.d.ts +27 -0
  14. package/network/events/protocol.js +66 -0
  15. package/network/events/registry.d.ts +8 -0
  16. package/network/events/registry.js +3 -0
  17. package/network/index.d.ts +8 -2
  18. package/network/index.js +16 -3
  19. package/network/message-bus-sync.d.ts +16 -3
  20. package/network/message-bus-sync.js +161 -103
  21. package/network/server/index.d.ts +14 -0
  22. package/network/server/index.js +219 -0
  23. package/network/server/utils.d.ts +18 -0
  24. package/network/server/utils.js +135 -0
  25. package/network/state.js +3 -5
  26. package/package.json +6 -6
  27. package/server/env-var.d.ts +15 -0
  28. package/server/env-var.js +31 -0
  29. package/server/index.d.ts +2 -0
  30. package/server/index.js +3 -0
  31. package/server/storage/constants.d.ts +79 -0
  32. package/server/storage/constants.js +19 -0
  33. package/server/storage/index.d.ts +45 -0
  34. package/server/storage/index.js +51 -0
  35. package/server/storage/player.d.ts +45 -0
  36. package/server/storage/player.js +198 -0
  37. package/server/storage/scene.d.ts +40 -0
  38. package/server/storage/scene.js +188 -0
  39. package/server/storage/value-cache.d.ts +1 -0
  40. package/server/storage/value-cache.js +52 -0
  41. package/server/storage/write-queue.d.ts +1 -0
  42. package/server/storage/write-queue.js +71 -0
  43. package/server/storage-url.d.ts +13 -0
  44. package/server/storage-url.js +42 -0
  45. package/server/utils.d.ts +35 -0
  46. package/server/utils.js +61 -0
  47. package/src/atom.ts +98 -0
  48. package/src/future.ts +38 -0
  49. package/src/network/binary-message-bus.ts +9 -4
  50. package/src/network/chunking.ts +45 -0
  51. package/src/network/events/implementation.ts +271 -0
  52. package/src/network/events/index.ts +48 -0
  53. package/src/network/events/protocol.ts +94 -0
  54. package/src/network/events/registry.ts +18 -0
  55. package/src/network/index.ts +40 -3
  56. package/src/network/message-bus-sync.ts +176 -112
  57. package/src/network/server/index.ts +301 -0
  58. package/src/network/server/utils.ts +189 -0
  59. package/src/network/state.ts +3 -4
  60. package/src/server/env-var.ts +36 -0
  61. package/src/server/index.ts +12 -0
  62. package/src/server/storage/constants.ts +111 -0
  63. package/src/server/storage/index.ts +74 -0
  64. package/src/server/storage/player.ts +289 -0
  65. package/src/server/storage/scene.ts +270 -0
  66. package/src/server/storage/value-cache.ts +92 -0
  67. package/src/server/storage/write-queue.ts +131 -0
  68. package/src/server/storage-url.ts +49 -0
  69. package/src/server/utils.ts +76 -0
@@ -0,0 +1,131 @@
1
+ /**
2
+ * A pending write operation. `body` is the serialized PUT payload, or null
3
+ * for a DELETE. Callers coalesced into the op share its promise.
4
+ * @internal
5
+ */
6
+ interface PendingOp {
7
+ body: string | null
8
+ execute: (body: string | null) => Promise<boolean>
9
+ promise: Promise<boolean>
10
+ resolve: (result: boolean) => void
11
+ }
12
+
13
+ interface KeyState {
14
+ /** The op currently on the network. */
15
+ active: PendingOp
16
+ /** At most one queued op; later writes replace its payload (latest wins). */
17
+ queued?: PendingOp
18
+ }
19
+
20
+ /**
21
+ * Serializes writes per key so the service commits them in issue order.
22
+ * Overlapping PUTs from the single scene server would otherwise race: the
23
+ * server keeps whichever request it processes last, while the local cache
24
+ * keeps whichever response arrives last — either can disagree with the last
25
+ * set() issued. With at most one in-flight op per key and a single queued
26
+ * "latest value" slot, the server's final state always matches the last
27
+ * write issued, and N rapid writes collapse into at most 2 network calls.
28
+ * @internal
29
+ */
30
+ export interface WriteQueue {
31
+ /**
32
+ * Body of the latest issued write for the key (the queued op if present,
33
+ * else the in-flight one): a string for a PUT, null for a DELETE,
34
+ * undefined when no write is pending.
35
+ */
36
+ pending(key: string): string | null | undefined
37
+ /** True while any write for the key is in flight or queued. */
38
+ isPending(key: string): boolean
39
+ /**
40
+ * Issues a write. If one is in flight, the new op is queued — replacing any
41
+ * already-queued op, whose callers then follow this op's outcome (their
42
+ * value was superseded before it could ever be observed). An op identical
43
+ * to the queued one joins it; `joinActive` additionally allows joining an
44
+ * identical in-flight op (only valid for dedup-tolerant callers, since that
45
+ * op was issued before this call).
46
+ */
47
+ enqueue(
48
+ key: string,
49
+ body: string | null,
50
+ execute: (body: string | null) => Promise<boolean>,
51
+ joinActive: boolean
52
+ ): Promise<boolean>
53
+ }
54
+
55
+ /**
56
+ * Creates the per-key write serializer shared by a storage scope.
57
+ * @internal
58
+ */
59
+ export function createWriteQueue(): WriteQueue {
60
+ const keys = new Map<string, KeyState>()
61
+
62
+ function makeOp(body: string | null, execute: PendingOp['execute']): PendingOp {
63
+ let resolve!: (result: boolean) => void
64
+ const promise = new Promise<boolean>((r) => (resolve = r))
65
+ return { body, execute, promise, resolve }
66
+ }
67
+
68
+ async function drain(key: string, state: KeyState): Promise<void> {
69
+ for (;;) {
70
+ const op = state.active
71
+ let result = false
72
+ try {
73
+ result = await op.execute(op.body)
74
+ } catch {
75
+ // Executors report failures via their boolean result; a throw is
76
+ // unexpected but must not wedge the queue.
77
+ }
78
+ op.resolve(result)
79
+
80
+ if (state.queued) {
81
+ state.active = state.queued
82
+ state.queued = undefined
83
+ } else {
84
+ keys.delete(key)
85
+ return
86
+ }
87
+ }
88
+ }
89
+
90
+ return {
91
+ pending(key: string): string | null | undefined {
92
+ const state = keys.get(key)
93
+ if (!state) return undefined
94
+ return (state.queued ?? state.active).body
95
+ },
96
+
97
+ isPending(key: string): boolean {
98
+ return keys.has(key)
99
+ },
100
+
101
+ enqueue(key: string, body: string | null, execute: PendingOp['execute'], joinActive: boolean): Promise<boolean> {
102
+ const state = keys.get(key)
103
+
104
+ if (!state) {
105
+ const op = makeOp(body, execute)
106
+ const newState: KeyState = { active: op }
107
+ keys.set(key, newState)
108
+ void drain(key, newState)
109
+ return op.promise
110
+ }
111
+
112
+ if (state.queued) {
113
+ // A queued op has not started, so it is issued "after" this caller
114
+ // either way: join it when identical, supersede it otherwise.
115
+ if (state.queued.body !== body) {
116
+ state.queued.body = body
117
+ state.queued.execute = execute
118
+ }
119
+ return state.queued.promise
120
+ }
121
+
122
+ if (joinActive && state.active.body === body) {
123
+ return state.active.promise
124
+ }
125
+
126
+ const op = makeOp(body, execute)
127
+ state.queued = op
128
+ return op.promise
129
+ }
130
+ }
131
+ }
@@ -0,0 +1,49 @@
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
+ async function resolveStorageServerUrl(): Promise<string> {
7
+ const { realmInfo } = await getRealm({})
8
+
9
+ if (!realmInfo) {
10
+ throw new Error('Unable to retrieve realm information')
11
+ }
12
+
13
+ // Local development / preview mode
14
+ if (realmInfo.isPreview) {
15
+ return realmInfo.baseUrl
16
+ }
17
+
18
+ // Staging / testing environment
19
+ if (realmInfo.baseUrl.includes('.zone')) {
20
+ return STORAGE_SERVER_ZONE
21
+ }
22
+
23
+ // Production environment
24
+ return STORAGE_SERVER_ORG
25
+ }
26
+
27
+ let memoizedUrl: Promise<string> | null = null
28
+
29
+ /**
30
+ * Determines the correct storage server URL based on the current realm.
31
+ *
32
+ * - If `isPreview` is true, uses the realm's baseUrl (localhost)
33
+ * - If the realm's baseUrl contains `.zone`, uses storage.decentraland.zone
34
+ * - Otherwise, uses storage.decentraland.org (production)
35
+ *
36
+ * The realm never changes mid-session, so the result is memoized; a failed
37
+ * resolution is not memoized so transient getRealm errors can be retried.
38
+ *
39
+ * @returns The storage server base URL
40
+ */
41
+ export function getStorageServerUrl(): Promise<string> {
42
+ if (!memoizedUrl) {
43
+ memoizedUrl = resolveStorageServerUrl()
44
+ memoizedUrl.catch(() => {
45
+ memoizedUrl = null
46
+ })
47
+ }
48
+ return memoizedUrl
49
+ }
@@ -0,0 +1,76 @@
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
+ let body: T
66
+ try {
67
+ // JSON.parse throws synchronously, so it can't be wrapped with tryCatch (the
68
+ // throw would happen while evaluating the argument, rejecting this promise).
69
+ body = JSON.parse(response.body || '{}')
70
+ } catch {
71
+ console.error(`Failed to parse response from ${signedFetchBody.url}`)
72
+ return ['Failed to parse response', null, response.status]
73
+ }
74
+
75
+ return [null, (body ?? {}) as T, response.status]
76
+ }