@dcl/sdk 7.25.1-30310486734.commit-5ffe873 → 7.25.1-30365680769.commit-9c1b99d

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 (78) hide show
  1. package/composite-provider.js +4 -9
  2. package/internal/utf8.d.ts +15 -0
  3. package/internal/utf8.js +172 -0
  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 +1 -14
  9. package/network/message-bus-sync.js +103 -161
  10. package/network/state.js +5 -3
  11. package/package.json +6 -7
  12. package/src/composite-provider.ts +3 -10
  13. package/src/internal/utf8.ts +167 -0
  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 +110 -174
  17. package/src/network/state.ts +4 -3
  18. package/src/text-codec.ts +59 -12
  19. package/text-codec.d.ts +23 -8
  20. package/text-codec.js +55 -13
  21. package/atom.d.ts +0 -19
  22. package/atom.js +0 -83
  23. package/future.d.ts +0 -8
  24. package/future.js +0 -26
  25. package/network/chunking.d.ts +0 -5
  26. package/network/chunking.js +0 -38
  27. package/network/events/implementation.d.ts +0 -93
  28. package/network/events/implementation.js +0 -221
  29. package/network/events/index.d.ts +0 -42
  30. package/network/events/index.js +0 -43
  31. package/network/events/protocol.d.ts +0 -27
  32. package/network/events/protocol.js +0 -66
  33. package/network/events/registry.d.ts +0 -8
  34. package/network/events/registry.js +0 -3
  35. package/network/server/index.d.ts +0 -14
  36. package/network/server/index.js +0 -219
  37. package/network/server/utils.d.ts +0 -18
  38. package/network/server/utils.js +0 -135
  39. package/server/env-var.d.ts +0 -15
  40. package/server/env-var.js +0 -31
  41. package/server/index.d.ts +0 -2
  42. package/server/index.js +0 -3
  43. package/server/storage/constants.d.ts +0 -79
  44. package/server/storage/constants.js +0 -19
  45. package/server/storage/index.d.ts +0 -45
  46. package/server/storage/index.js +0 -51
  47. package/server/storage/player.d.ts +0 -45
  48. package/server/storage/player.js +0 -198
  49. package/server/storage/scene.d.ts +0 -40
  50. package/server/storage/scene.js +0 -188
  51. package/server/storage/value-cache.d.ts +0 -1
  52. package/server/storage/value-cache.js +0 -52
  53. package/server/storage/write-queue.d.ts +0 -1
  54. package/server/storage/write-queue.js +0 -71
  55. package/server/storage-url.d.ts +0 -13
  56. package/server/storage-url.js +0 -42
  57. package/server/utils.d.ts +0 -35
  58. package/server/utils.js +0 -61
  59. package/src/atom.ts +0 -98
  60. package/src/ethereum-provider/text-encoding.d.ts +0 -4
  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 -301
  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 -111
  72. package/src/server/storage/index.ts +0 -74
  73. package/src/server/storage/player.ts +0 -289
  74. package/src/server/storage/scene.ts +0 -270
  75. package/src/server/storage/value-cache.ts +0 -92
  76. package/src/server/storage/write-queue.ts +0 -131
  77. package/src/server/storage-url.ts +0 -49
  78. package/src/server/utils.ts +0 -76
@@ -1,92 +0,0 @@
1
- import { DEFAULT_STORAGE_CONFIG, StorageConfigState } from './constants'
2
-
3
- /**
4
- * A cached fact about a key's server-side state.
5
- * @internal
6
- */
7
- export interface CacheEntry {
8
- /**
9
- * Serialized `{ value }` body; absent for negative entries. Parsed per read
10
- * hit so callers never share object references, and compared verbatim for
11
- * write dedup (exact equality — no hash-collision risk).
12
- */
13
- body?: string
14
- /** True when the server confirmed the key does not exist (GET 404 or successful DELETE). */
15
- absent?: boolean
16
- }
17
-
18
- /**
19
- * Bounded, lazily-expiring cache of key states, backing both write dedup
20
- * (skip storage writes whose value is already known to be stored) and read
21
- * caching (serve get() without a network request within the TTL).
22
- * @internal
23
- */
24
- export interface ValueCache {
25
- /** Returns the entry if present and fresh; lazily evicts expired entries. */
26
- get(key: string): CacheEntry | undefined
27
- /** Stores or refreshes a known value (serialized body); overwrites negative entries. */
28
- set(key: string, entry: { body: string }): void
29
- /** Stores a confirmed-absent (negative) entry, replacing any value entry. */
30
- setAbsent(key: string): void
31
- delete(key: string): void
32
- }
33
-
34
- /**
35
- * Creates the bounded value cache shared by a storage scope.
36
- * @internal
37
- */
38
- export function createValueCache(config: StorageConfigState): ValueCache {
39
- const entries = new Map<string, CacheEntry & { storedAt: number }>()
40
-
41
- function insert(key: string, entry: CacheEntry): void {
42
- // Delete + re-insert moves refreshed keys to the end of the Map's
43
- // insertion order, so eviction below drops the least-recently-written.
44
- entries.delete(key)
45
- entries.set(key, { ...entry, storedAt: Date.now() })
46
-
47
- // Guard against misconfiguration: a negative bound would loop forever on
48
- // an empty map, and a NaN bound would silently disable eviction.
49
- const maxEntries = Number.isFinite(config.cacheMaxEntries)
50
- ? Math.max(0, config.cacheMaxEntries)
51
- : DEFAULT_STORAGE_CONFIG.cacheMaxEntries
52
-
53
- while (entries.size > maxEntries) {
54
- entries.delete(entries.keys().next().value!)
55
- }
56
- }
57
-
58
- return {
59
- get(key: string): CacheEntry | undefined {
60
- const entry = entries.get(key)
61
- if (!entry) return undefined
62
-
63
- // Guard against misconfiguration, mirroring cacheMaxEntries: a NaN
64
- // bound would silently disable expiry (NaN comparisons are false).
65
- // Negative values need no guard — they just expire everything.
66
- const maxAgeMs = Number.isFinite(config.cacheMaxAgeMs)
67
- ? config.cacheMaxAgeMs
68
- : DEFAULT_STORAGE_CONFIG.cacheMaxAgeMs
69
-
70
- // Lazy max-age expiry: storedAt is never refreshed on hits, so the age
71
- // bounds the time since the last actual network confirmation.
72
- if (Date.now() - entry.storedAt > maxAgeMs) {
73
- entries.delete(key)
74
- return undefined
75
- }
76
-
77
- return entry
78
- },
79
-
80
- set(key: string, entry: { body: string }): void {
81
- insert(key, entry)
82
- },
83
-
84
- setAbsent(key: string): void {
85
- insert(key, { absent: true })
86
- },
87
-
88
- delete(key: string): void {
89
- entries.delete(key)
90
- }
91
- }
92
- }
@@ -1,131 +0,0 @@
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
- }
@@ -1,49 +0,0 @@
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
- }
@@ -1,76 +0,0 @@
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
- }