@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.
Files changed (63) 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 +14 -1
  20. package/network/message-bus-sync.js +152 -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 +23 -0
  32. package/server/storage/constants.js +2 -0
  33. package/server/storage/index.d.ts +22 -0
  34. package/server/storage/index.js +29 -0
  35. package/server/storage/player.d.ts +43 -0
  36. package/server/storage/player.js +92 -0
  37. package/server/storage/scene.d.ts +38 -0
  38. package/server/storage/scene.js +90 -0
  39. package/server/storage-url.d.ts +10 -0
  40. package/server/storage-url.js +29 -0
  41. package/server/utils.d.ts +35 -0
  42. package/server/utils.js +56 -0
  43. package/src/atom.ts +98 -0
  44. package/src/future.ts +38 -0
  45. package/src/network/binary-message-bus.ts +9 -4
  46. package/src/network/chunking.ts +45 -0
  47. package/src/network/events/implementation.ts +271 -0
  48. package/src/network/events/index.ts +48 -0
  49. package/src/network/events/protocol.ts +94 -0
  50. package/src/network/events/registry.ts +18 -0
  51. package/src/network/index.ts +40 -3
  52. package/src/network/message-bus-sync.ts +166 -110
  53. package/src/network/server/index.ts +301 -0
  54. package/src/network/server/utils.ts +189 -0
  55. package/src/network/state.ts +3 -4
  56. package/src/server/env-var.ts +36 -0
  57. package/src/server/index.ts +2 -0
  58. package/src/server/storage/constants.ts +22 -0
  59. package/src/server/storage/index.ts +44 -0
  60. package/src/server/storage/player.ts +156 -0
  61. package/src/server/storage/scene.ts +149 -0
  62. package/src/server/storage-url.ts +34 -0
  63. 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
+ }