@dcl/sdk 7.20.4 → 7.20.5-22638270380.commit-3f48d5e
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 +221 -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 +152 -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 +23 -0
- package/server/storage/constants.js +2 -0
- package/server/storage/index.d.ts +22 -0
- package/server/storage/index.js +29 -0
- package/server/storage/player.d.ts +43 -0
- package/server/storage/player.js +92 -0
- package/server/storage/scene.d.ts +38 -0
- package/server/storage/scene.js +90 -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 +271 -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 +166 -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 +22 -0
- package/src/server/storage/index.ts +44 -0
- package/src/server/storage/player.ts +156 -0
- package/src/server/storage/scene.ts +149 -0
- package/src/server/storage-url.ts +34 -0
- package/src/server/utils.ts +73 -0
|
@@ -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
|
+
}
|