rivetkit 2.3.11-rc.7 → 2.3.11-rc.9

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 (58) hide show
  1. package/dist/browser/client.js +1 -1
  2. package/dist/browser/client.js.map +1 -1
  3. package/dist/browser/inspector/client.js +1 -1
  4. package/dist/browser/inspector/client.js.map +1 -1
  5. package/dist/tsup/agent-os/index.cjs +1 -1
  6. package/dist/tsup/agent-os/index.cjs.map +1 -1
  7. package/dist/tsup/agent-os/index.js +1 -1
  8. package/dist/tsup/agent-os/index.js.map +1 -1
  9. package/dist/tsup/{chunk-2EPCTV3V.cjs → chunk-6SBPWTXJ.cjs} +9 -9
  10. package/dist/tsup/{chunk-2EPCTV3V.cjs.map → chunk-6SBPWTXJ.cjs.map} +1 -1
  11. package/dist/tsup/{chunk-OJUVQ2F4.cjs → chunk-BZWKY6WO.cjs} +138 -138
  12. package/dist/tsup/{chunk-OJUVQ2F4.cjs.map → chunk-BZWKY6WO.cjs.map} +1 -1
  13. package/dist/tsup/{chunk-RZFEJKIS.js → chunk-C5TORAVP.js} +2 -2
  14. package/dist/tsup/{chunk-OISWZDAF.js → chunk-HOZ3ZLZ2.js} +3 -3
  15. package/dist/tsup/{chunk-BC7SLXCY.cjs → chunk-IMRDHZM2.cjs} +4 -4
  16. package/dist/tsup/{chunk-BC7SLXCY.cjs.map → chunk-IMRDHZM2.cjs.map} +1 -1
  17. package/dist/tsup/{chunk-HODZKZZE.js → chunk-IOLZ3ONU.js} +3 -3
  18. package/dist/tsup/{chunk-XAGDGH4O.js → chunk-LZ4TSBIP.js} +2 -2
  19. package/dist/tsup/{chunk-XAGDGH4O.js.map → chunk-LZ4TSBIP.js.map} +1 -1
  20. package/dist/tsup/{chunk-5TWP5HIG.js → chunk-QVCO6DTZ.js} +3 -3
  21. package/dist/tsup/{chunk-2HVKCDDH.cjs → chunk-T56IZ42M.cjs} +2 -2
  22. package/dist/tsup/{chunk-2HVKCDDH.cjs.map → chunk-T56IZ42M.cjs.map} +1 -1
  23. package/dist/tsup/{chunk-M35WIZ7C.cjs → chunk-T5XVTVQY.cjs} +3 -3
  24. package/dist/tsup/{chunk-M35WIZ7C.cjs.map → chunk-T5XVTVQY.cjs.map} +1 -1
  25. package/dist/tsup/{chunk-OG3KYH7X.cjs → chunk-V5CVATRU.cjs} +7 -7
  26. package/dist/tsup/{chunk-OG3KYH7X.cjs.map → chunk-V5CVATRU.cjs.map} +1 -1
  27. package/dist/tsup/{chunk-B7N2TMLF.js → chunk-XOOESJY7.js} +2 -2
  28. package/dist/tsup/client/mod.cjs +5 -5
  29. package/dist/tsup/client/mod.js +4 -4
  30. package/dist/tsup/common/log.cjs +2 -2
  31. package/dist/tsup/common/log.js +1 -1
  32. package/dist/tsup/common/websocket.cjs +3 -3
  33. package/dist/tsup/common/websocket.js +2 -2
  34. package/dist/tsup/db/drizzle.cjs +2 -2
  35. package/dist/tsup/db/drizzle.js +1 -1
  36. package/dist/tsup/db/mod.cjs +2 -2
  37. package/dist/tsup/db/mod.js +1 -1
  38. package/dist/tsup/inspector/mod.cjs +4 -4
  39. package/dist/tsup/inspector/mod.js +3 -3
  40. package/dist/tsup/mod.cjs +171 -73
  41. package/dist/tsup/mod.cjs.map +1 -1
  42. package/dist/tsup/mod.js +111 -13
  43. package/dist/tsup/mod.js.map +1 -1
  44. package/dist/tsup/test/mod.cjs +6 -6
  45. package/dist/tsup/test/mod.js +4 -4
  46. package/dist/tsup/utils.cjs +2 -2
  47. package/dist/tsup/utils.js +1 -1
  48. package/dist/tsup/workflow/mod.cjs +6 -6
  49. package/dist/tsup/workflow/mod.cjs.map +1 -1
  50. package/dist/tsup/workflow/mod.js +3 -3
  51. package/package.json +8 -8
  52. package/src/registry/native.ts +45 -8
  53. package/src/registry/write-through-proxy.ts +101 -0
  54. /package/dist/tsup/{chunk-RZFEJKIS.js.map → chunk-C5TORAVP.js.map} +0 -0
  55. /package/dist/tsup/{chunk-OISWZDAF.js.map → chunk-HOZ3ZLZ2.js.map} +0 -0
  56. /package/dist/tsup/{chunk-HODZKZZE.js.map → chunk-IOLZ3ONU.js.map} +0 -0
  57. /package/dist/tsup/{chunk-5TWP5HIG.js.map → chunk-QVCO6DTZ.js.map} +0 -0
  58. /package/dist/tsup/{chunk-B7N2TMLF.js.map → chunk-XOOESJY7.js.map} +0 -0
@@ -38,3 +38,104 @@ export function createWriteThroughProxy<T>(
38
38
  },
39
39
  ) as T;
40
40
  }
41
+
42
+ /**
43
+ * Returns the raw target behind an `@rivetkit/on-change` proxy, following
44
+ * chains of proxies wrapping proxies until reaching a plain value.
45
+ */
46
+ function unwrapProxy(value: unknown): unknown {
47
+ let current = value;
48
+ while (current !== null && typeof current === "object") {
49
+ const target = onChange.target(current as Record<string, any>);
50
+ if (target === current) {
51
+ break;
52
+ }
53
+ current = target;
54
+ }
55
+ return current;
56
+ }
57
+
58
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
59
+ const proto = Object.getPrototypeOf(value as object);
60
+ return proto === Object.prototype || proto === null;
61
+ }
62
+
63
+ function unwrapDeep(value: unknown, seen: Set<object>): unknown {
64
+ const unwrapped = unwrapProxy(value);
65
+ if (!unwrapped || typeof unwrapped !== "object") {
66
+ return unwrapped;
67
+ }
68
+ if (seen.has(unwrapped)) {
69
+ return unwrapped;
70
+ }
71
+ seen.add(unwrapped);
72
+
73
+ if (Array.isArray(unwrapped)) {
74
+ for (let i = 0; i < unwrapped.length; i++) {
75
+ const child = unwrapDeep(unwrapped[i], seen);
76
+ if (child !== unwrapped[i]) {
77
+ unwrapped[i] = child;
78
+ }
79
+ }
80
+ return unwrapped;
81
+ }
82
+
83
+ if (unwrapped instanceof Map) {
84
+ const replacements: [unknown, unknown, unknown][] = [];
85
+ for (const [key, child] of unwrapped.entries()) {
86
+ const nextKey = unwrapDeep(key, seen);
87
+ const nextChild = unwrapDeep(child, seen);
88
+ if (nextKey !== key || nextChild !== child) {
89
+ replacements.push([key, nextKey, nextChild]);
90
+ }
91
+ }
92
+ for (const [key, nextKey, nextChild] of replacements) {
93
+ if (nextKey !== key) {
94
+ unwrapped.delete(key);
95
+ }
96
+ unwrapped.set(nextKey, nextChild);
97
+ }
98
+ return unwrapped;
99
+ }
100
+
101
+ if (unwrapped instanceof Set) {
102
+ const replacements: [unknown, unknown][] = [];
103
+ for (const child of unwrapped.values()) {
104
+ const next = unwrapDeep(child, seen);
105
+ if (next !== child) {
106
+ replacements.push([child, next]);
107
+ }
108
+ }
109
+ for (const [child, next] of replacements) {
110
+ unwrapped.delete(child);
111
+ unwrapped.add(next);
112
+ }
113
+ return unwrapped;
114
+ }
115
+
116
+ if (isPlainObject(unwrapped)) {
117
+ for (const key of Object.keys(unwrapped)) {
118
+ const child = unwrapDeep(unwrapped[key], seen);
119
+ if (child !== unwrapped[key]) {
120
+ unwrapped[key] = child;
121
+ }
122
+ }
123
+ }
124
+
125
+ return unwrapped;
126
+ }
127
+
128
+ /**
129
+ * Strips every `@rivetkit/on-change` proxy out of a value in place, including
130
+ * proxies nested inside plain objects, arrays, `Map`s, and `Set`s.
131
+ *
132
+ * A read of `c.state` or `conn.state` hands back a deep write-through proxy,
133
+ * so an update written as `c.state = { ...c.state, foo }` produces a plain root
134
+ * object whose children are still proxies. Persisting that value as-is makes
135
+ * the next read wrap proxies in another proxy layer, and each layer multiplies
136
+ * the work of traversing the state, so repeated spread updates degrade
137
+ * exponentially. Unwrapping before persisting keeps stored state proxy-free.
138
+ */
139
+ export function unwrapWriteThroughProxy<T>(value: T): T {
140
+ return unwrapDeep(value, new Set()) as T;
141
+ }