@symbo.ls/sdk 2.31.37 → 2.32.1

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.
@@ -26,6 +26,8 @@ var import_RootStateManager = require("../state/RootStateManager.js");
26
26
  var import_rootEventBus = require("../state/rootEventBus.js");
27
27
  var import_validation = require("../utils/validation.js");
28
28
  var import_utils = require("@domql/utils");
29
+ var import_jsonDiff = require("../utils/jsonDiff.js");
30
+ var import_ordering = require("../utils/ordering.js");
29
31
  class CollabService extends import_BaseService.BaseService {
30
32
  constructor(config) {
31
33
  super(config);
@@ -125,53 +127,49 @@ class CollabService extends import_BaseService.BaseService {
125
127
  if (this._client) {
126
128
  await this.disconnect();
127
129
  }
128
- try {
129
- this._client = new import_CollabClient.CollabClient({
130
- jwt,
131
- projectId,
132
- branch,
133
- live: Boolean(pro)
134
- });
135
- await new Promise((resolve) => {
136
- var _a2, _b2;
137
- if ((_a2 = this._client.socket) == null ? void 0 : _a2.connected) {
138
- resolve();
139
- } else {
140
- (_b2 = this._client.socket) == null ? void 0 : _b2.once("connect", resolve);
141
- }
142
- });
143
- (_b = this._client.socket) == null ? void 0 : _b.on("ops", ({ changes }) => {
144
- console.log(`ops event`);
145
- this._stateManager.applyChanges(changes, { fromSocket: true });
146
- });
147
- (_c = this._client.socket) == null ? void 0 : _c.on("commit", ({ version }) => {
148
- if (version) {
149
- this._stateManager.setVersion(version);
150
- }
151
- import_rootEventBus.rootBus.emit("checkpoint:done", { version, origin: "auto" });
152
- });
153
- (_d = this._client.socket) == null ? void 0 : _d.on("clients", this._handleClientsEvent.bind(this));
154
- (_e = this._client.socket) == null ? void 0 : _e.on(
155
- "bundle:done",
156
- this._handleBundleDoneEvent.bind(this)
157
- );
158
- (_f = this._client.socket) == null ? void 0 : _f.on(
159
- "bundle:error",
160
- this._handleBundleErrorEvent.bind(this)
161
- );
162
- if (this._pendingOps.length) {
163
- console.log(
164
- `[CollabService] Flushing ${this._pendingOps.length} offline operation batch(es)`
165
- );
166
- this._pendingOps.forEach(({ tuples }) => {
167
- this.socket.emit("ops", { changes: tuples, ts: Date.now() });
168
- });
169
- this._pendingOps.length = 0;
130
+ this._client = new import_CollabClient.CollabClient({
131
+ jwt,
132
+ projectId,
133
+ branch,
134
+ live: Boolean(pro)
135
+ });
136
+ await new Promise((resolve) => {
137
+ var _a2, _b2;
138
+ if ((_a2 = this._client.socket) == null ? void 0 : _a2.connected) {
139
+ resolve();
140
+ } else {
141
+ (_b2 = this._client.socket) == null ? void 0 : _b2.once("connect", resolve);
170
142
  }
171
- this._connected = true;
172
- } catch (err) {
173
- throw err;
143
+ });
144
+ (_b = this._client.socket) == null ? void 0 : _b.on("ops", ({ changes }) => {
145
+ console.log(`ops event`);
146
+ this._stateManager.applyChanges(changes, { fromSocket: true });
147
+ });
148
+ (_c = this._client.socket) == null ? void 0 : _c.on("commit", ({ version }) => {
149
+ if (version) {
150
+ this._stateManager.setVersion(version);
151
+ }
152
+ import_rootEventBus.rootBus.emit("checkpoint:done", { version, origin: "auto" });
153
+ });
154
+ (_d = this._client.socket) == null ? void 0 : _d.on("clients", this._handleClientsEvent.bind(this));
155
+ (_e = this._client.socket) == null ? void 0 : _e.on(
156
+ "bundle:done",
157
+ this._handleBundleDoneEvent.bind(this)
158
+ );
159
+ (_f = this._client.socket) == null ? void 0 : _f.on(
160
+ "bundle:error",
161
+ this._handleBundleErrorEvent.bind(this)
162
+ );
163
+ if (this._pendingOps.length) {
164
+ console.log(
165
+ `[CollabService] Flushing ${this._pendingOps.length} offline operation batch(es)`
166
+ );
167
+ this._pendingOps.forEach(({ changes, orders }) => {
168
+ this.socket.emit("ops", { changes, orders, ts: Date.now() });
169
+ });
170
+ this._pendingOps.length = 0;
174
171
  }
172
+ this._connected = true;
175
173
  }
176
174
  disconnect() {
177
175
  var _a;
@@ -209,24 +207,128 @@ class CollabService extends import_BaseService.BaseService {
209
207
  }
210
208
  /* ---------- data helpers ---------- */
211
209
  updateData(tuples, options = {}) {
212
- var _a;
210
+ var _a, _b;
213
211
  this._ensureStateManager();
214
212
  const { isUndo = false, isRedo = false } = options;
215
213
  if (!isUndo && !isRedo && !this._isUndoRedo) {
216
214
  this._trackForUndo(tuples, options);
217
215
  }
216
+ const processedTuples = (() => {
217
+ var _a2;
218
+ try {
219
+ const root = (_a2 = this._stateManager) == null ? void 0 : _a2.root;
220
+ const isPlainObject = (o) => o && typeof o === "object" && !Array.isArray(o);
221
+ const getByPath = (state2, path) => {
222
+ if (!state2 || typeof state2.getByPath !== "function") {
223
+ return null;
224
+ }
225
+ try {
226
+ return state2.getByPath(path);
227
+ } catch {
228
+ return null;
229
+ }
230
+ };
231
+ const expandTuple = (t) => {
232
+ const [action, path, value] = t || [];
233
+ const isSchemaPath = Array.isArray(path) && path[0] === "schema";
234
+ if (action === "delete" || isSchemaPath) {
235
+ return [t];
236
+ }
237
+ const canConsiderExpansion = action === "update" && Array.isArray(path) && (path.length === 1 || path.length === 2) && isPlainObject(value);
238
+ if (!canConsiderExpansion) {
239
+ return [t];
240
+ }
241
+ const prev = getByPath(root, path) || {};
242
+ const next = value || {};
243
+ if (!isPlainObject(prev) || !isPlainObject(next)) {
244
+ return [t];
245
+ }
246
+ const ops = (0, import_jsonDiff.diffJson)(prev, next, []);
247
+ if (!ops.length) {
248
+ return [];
249
+ }
250
+ const arr = [];
251
+ for (let j = 0; j < ops.length; j++) {
252
+ const op = ops[j];
253
+ const fullPath = [...path, ...op.path];
254
+ const last = fullPath[fullPath.length - 1];
255
+ if (op.action === "set") {
256
+ arr.push(["update", fullPath, op.value]);
257
+ } else if (op.action === "del") {
258
+ if (last !== "__order") {
259
+ arr.push(["delete", fullPath]);
260
+ }
261
+ }
262
+ }
263
+ return arr;
264
+ };
265
+ const minimizeTuples = (inputTuples) => {
266
+ const out = [];
267
+ for (let i = 0; i < inputTuples.length; i++) {
268
+ const expanded = expandTuple(inputTuples[i]);
269
+ for (let k = 0; k < expanded.length; k++) {
270
+ const tuple = expanded[k];
271
+ const isDelete = Array.isArray(tuple) && tuple[0] === "delete";
272
+ const isOrderKey = isDelete && Array.isArray(tuple[1]) && tuple[1][tuple[1].length - 1] === "__order";
273
+ if (!isOrderKey) {
274
+ out.push(tuple);
275
+ }
276
+ }
277
+ }
278
+ console.log(`Minimized tuples`, out);
279
+ return out;
280
+ };
281
+ console.log(`Processing tuples`, tuples);
282
+ return minimizeTuples(tuples);
283
+ } catch (err) {
284
+ console.warn(
285
+ "[CollabService] Minimal diff expansion failed \u2013 using original tuples",
286
+ err
287
+ );
288
+ return tuples;
289
+ }
290
+ })();
218
291
  if (options.append && options.append.length) {
219
- tuples.push(...options.append);
292
+ processedTuples.push(...options.append);
220
293
  }
221
294
  this._stateManager.applyChanges(tuples, { ...options });
222
- tuples = (0, import_utils.deepStringifyFunctions)(tuples, []);
295
+ const state = (_a = this._stateManager) == null ? void 0 : _a.root;
296
+ const el = state == null ? void 0 : state.__element;
297
+ const orders = (0, import_ordering.computeOrdersForTuples)(state, processedTuples);
298
+ const stringifiedGranularTuples = (el == null ? void 0 : el.call) ? el.call(
299
+ "deepStringifyFunctions",
300
+ processedTuples,
301
+ Array.isArray(processedTuples) ? [] : {}
302
+ ) : (0, import_utils.deepStringifyFunctions)(
303
+ processedTuples,
304
+ Array.isArray(processedTuples) ? [] : {}
305
+ );
306
+ const stringifiedTuples = (el == null ? void 0 : el.call) ? el.call(
307
+ "deepStringifyFunctions",
308
+ tuples,
309
+ Array.isArray(tuples) ? [] : {}
310
+ ) : (0, import_utils.deepStringifyFunctions)(
311
+ tuples,
312
+ Array.isArray(tuples) ? [] : {}
313
+ );
223
314
  if (!this.isConnected()) {
224
315
  console.warn("[CollabService] Not connected, queuing real-time update");
225
- this._pendingOps.push({ tuples, options });
316
+ this._pendingOps.push({ changes: stringifiedTuples, granularChanges: stringifiedGranularTuples, orders, options });
226
317
  return;
227
318
  }
228
- if ((_a = this.socket) == null ? void 0 : _a.connected) {
229
- this.socket.emit("ops", { changes: tuples, ts: Date.now() });
319
+ if ((_b = this.socket) == null ? void 0 : _b.connected) {
320
+ console.log("[CollabService] Sending operations to the backend", {
321
+ changes: stringifiedTuples,
322
+ granularChanges: stringifiedGranularTuples,
323
+ orders,
324
+ ts: Date.now()
325
+ });
326
+ this.socket.emit("ops", {
327
+ changes: stringifiedTuples,
328
+ granularChanges: stringifiedGranularTuples,
329
+ orders,
330
+ ts: Date.now()
331
+ });
230
332
  }
231
333
  return { success: true };
232
334
  }
@@ -21,6 +21,7 @@ __export(ProjectService_exports, {
21
21
  });
22
22
  module.exports = __toCommonJS(ProjectService_exports);
23
23
  var import_BaseService = require("./BaseService.js");
24
+ var import_ordering = require("../utils/ordering.js");
24
25
  class ProjectService extends import_BaseService.BaseService {
25
26
  // ==================== PROJECT METHODS ====================
26
27
  async createProject(projectData) {
@@ -552,6 +553,8 @@ class ProjectService extends import_BaseService.BaseService {
552
553
  throw new Error("Changes must be an array");
553
554
  }
554
555
  const { message, branch = "main", type = "patch" } = options;
556
+ const state = this._context && this._context.state;
557
+ const derivedOrders = options.orders || (state ? (0, import_ordering.computeOrdersForTuples)(state, changes) : []);
555
558
  try {
556
559
  const response = await this._request(`/projects/${projectId}/changes`, {
557
560
  method: "POST",
@@ -559,7 +562,8 @@ class ProjectService extends import_BaseService.BaseService {
559
562
  changes,
560
563
  message,
561
564
  branch,
562
- type
565
+ type,
566
+ ...derivedOrders && derivedOrders.length ? { orders: derivedOrders } : {}
563
567
  }),
564
568
  methodName: "applyProjectChanges"
565
569
  });
@@ -265,8 +265,8 @@ class ScreenshotService extends import_BaseService.BaseService {
265
265
  waitAfterRecreateMs = 2e4,
266
266
  recreate = {
267
267
  process_pages: true,
268
- process_components: true,
269
- process_descriptions: true,
268
+ process_components: false,
269
+ process_descriptions: false,
270
270
  force: false,
271
271
  priority: 5
272
272
  },
@@ -0,0 +1,276 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var ordering_exports = {};
19
+ __export(ordering_exports, {
20
+ computeOrdersForTuples: () => computeOrdersForTuples,
21
+ computeOrdersFromState: () => computeOrdersFromState,
22
+ getParentPathsFromTuples: () => getParentPathsFromTuples
23
+ });
24
+ module.exports = __toCommonJS(ordering_exports);
25
+ function isObjectLike(val) {
26
+ return val && typeof val === "object" && !Array.isArray(val);
27
+ }
28
+ function normalizePath(path) {
29
+ if (Array.isArray(path)) {
30
+ return path;
31
+ }
32
+ if (typeof path === "string") {
33
+ return [path];
34
+ }
35
+ return [];
36
+ }
37
+ function getParentPathsFromTuples(tuples = []) {
38
+ const seen = /* @__PURE__ */ new Set();
39
+ const parents = [];
40
+ const META_KEYS = /* @__PURE__ */ new Set([
41
+ "style",
42
+ "class",
43
+ "text",
44
+ "html",
45
+ "content",
46
+ "data",
47
+ "attr",
48
+ "state",
49
+ "scope",
50
+ "props",
51
+ "define",
52
+ "on",
53
+ "extend",
54
+ "extends",
55
+ "childExtend",
56
+ "childExtends",
57
+ "childProps",
58
+ "children",
59
+ "component",
60
+ "context",
61
+ "tag",
62
+ "key",
63
+ "__order",
64
+ "if"
65
+ ]);
66
+ for (let i = 0; i < tuples.length; i++) {
67
+ const tuple = tuples[i];
68
+ if (!Array.isArray(tuple) || tuple.length < 2) {
69
+ continue;
70
+ }
71
+ const path = normalizePath(tuple[1]);
72
+ if (!path.length) {
73
+ continue;
74
+ }
75
+ if (path[0] === "schema") {
76
+ continue;
77
+ }
78
+ const immediateParent = path.slice(0, -1);
79
+ if (immediateParent.length) {
80
+ const key = JSON.stringify(immediateParent);
81
+ if (!seen.has(key)) {
82
+ seen.add(key);
83
+ parents.push(immediateParent);
84
+ }
85
+ }
86
+ const last = path[path.length - 1];
87
+ if (META_KEYS.has(last) && path.length >= 2) {
88
+ const containerParent = path.slice(0, -2);
89
+ if (containerParent.length) {
90
+ const key2 = JSON.stringify(containerParent);
91
+ if (!seen.has(key2)) {
92
+ seen.add(key2);
93
+ parents.push(containerParent);
94
+ }
95
+ }
96
+ }
97
+ for (let j = 0; j < path.length; j++) {
98
+ const seg = path[j];
99
+ if (!META_KEYS.has(seg)) {
100
+ continue;
101
+ }
102
+ const containerParent2 = path.slice(0, j);
103
+ if (!containerParent2.length) {
104
+ continue;
105
+ }
106
+ const key3 = JSON.stringify(containerParent2);
107
+ if (!seen.has(key3)) {
108
+ seen.add(key3);
109
+ parents.push(containerParent2);
110
+ }
111
+ }
112
+ }
113
+ return parents;
114
+ }
115
+ function computeOrdersFromState(root, parentPaths = []) {
116
+ if (!root || typeof root.getByPath !== "function") {
117
+ return [];
118
+ }
119
+ const orders = [];
120
+ const EXCLUDE_KEYS = /* @__PURE__ */ new Set(["__order"]);
121
+ for (let i = 0; i < parentPaths.length; i++) {
122
+ const parentPath = parentPaths[i];
123
+ const obj = (() => {
124
+ try {
125
+ return root.getByPath(parentPath);
126
+ } catch {
127
+ return null;
128
+ }
129
+ })();
130
+ if (!isObjectLike(obj)) {
131
+ continue;
132
+ }
133
+ const keys = Object.keys(obj).filter((k) => !EXCLUDE_KEYS.has(k));
134
+ orders.push({ path: parentPath, keys });
135
+ }
136
+ return orders;
137
+ }
138
+ function normaliseSchemaCode(code) {
139
+ if (typeof code !== "string" || !code.length) {
140
+ return "";
141
+ }
142
+ return code.replaceAll("/////n", "\n").replaceAll("/////tilde", "`");
143
+ }
144
+ function parseExportedObject(code) {
145
+ const src = normaliseSchemaCode(code);
146
+ if (!src) {
147
+ return null;
148
+ }
149
+ const body = src.replace(/^\s*export\s+default\s*/u, "return ");
150
+ try {
151
+ return new Function(body)();
152
+ } catch {
153
+ return null;
154
+ }
155
+ }
156
+ function extractTopLevelKeysFromCode(code) {
157
+ const obj = parseExportedObject(code);
158
+ if (!obj || typeof obj !== "object") {
159
+ return [];
160
+ }
161
+ return Object.keys(obj);
162
+ }
163
+ function computeOrdersForTuples(root, tuples = []) {
164
+ const pendingChildrenByContainer = /* @__PURE__ */ new Map();
165
+ for (let i = 0; i < tuples.length; i++) {
166
+ const t = tuples[i];
167
+ if (!Array.isArray(t)) {
168
+ continue;
169
+ }
170
+ const [action, path] = t;
171
+ const p = normalizePath(path);
172
+ if (!Array.isArray(p) || p.length < 3) {
173
+ continue;
174
+ }
175
+ if (p[0] === "schema") {
176
+ continue;
177
+ }
178
+ const [typeName, containerKey, childKey] = p;
179
+ const containerPath = [typeName, containerKey];
180
+ const key = JSON.stringify(containerPath);
181
+ if (!pendingChildrenByContainer.has(key)) {
182
+ pendingChildrenByContainer.set(key, /* @__PURE__ */ new Set());
183
+ }
184
+ if (action === "update" || action === "set") {
185
+ pendingChildrenByContainer.get(key).add(childKey);
186
+ }
187
+ }
188
+ const preferredOrderMap = /* @__PURE__ */ new Map();
189
+ for (let i = 0; i < tuples.length; i++) {
190
+ const t = tuples[i];
191
+ if (!Array.isArray(t)) {
192
+ continue;
193
+ }
194
+ const [action, path, value] = t;
195
+ const p = normalizePath(path);
196
+ if (action !== "update" || !Array.isArray(p) || p.length < 3) {
197
+ continue;
198
+ }
199
+ if (p[0] !== "schema") {
200
+ continue;
201
+ }
202
+ const [, type, key] = p;
203
+ const containerPath = [type, key];
204
+ const uses = value && Array.isArray(value.uses) ? value.uses : null;
205
+ const code = value && value.code;
206
+ const obj = (() => {
207
+ try {
208
+ return root && typeof root.getByPath === "function" ? root.getByPath(containerPath) : null;
209
+ } catch {
210
+ return null;
211
+ }
212
+ })();
213
+ if (!obj) {
214
+ continue;
215
+ }
216
+ const present = new Set(Object.keys(obj));
217
+ const EXCLUDE_KEYS = /* @__PURE__ */ new Set(["__order"]);
218
+ const codeKeys = extractTopLevelKeysFromCode(code);
219
+ let resolved = [];
220
+ const pendingKey = JSON.stringify(containerPath);
221
+ const pendingChildren = pendingChildrenByContainer.get(pendingKey) || /* @__PURE__ */ new Set();
222
+ const eligible = /* @__PURE__ */ new Set([...present, ...pendingChildren]);
223
+ if (Array.isArray(codeKeys) && codeKeys.length) {
224
+ resolved = codeKeys.filter((k) => eligible.has(k) && !EXCLUDE_KEYS.has(k));
225
+ }
226
+ if (Array.isArray(uses) && uses.length) {
227
+ for (let u = 0; u < uses.length; u++) {
228
+ const keyName = uses[u];
229
+ if (eligible.has(keyName) && !EXCLUDE_KEYS.has(keyName) && !resolved.includes(keyName)) {
230
+ resolved.push(keyName);
231
+ }
232
+ }
233
+ }
234
+ if (pendingChildren.size) {
235
+ for (const child of pendingChildren) {
236
+ if (!EXCLUDE_KEYS.has(child) && !resolved.includes(child)) {
237
+ resolved.push(child);
238
+ }
239
+ }
240
+ }
241
+ if (resolved.length) {
242
+ preferredOrderMap.set(JSON.stringify(containerPath), { path: containerPath, keys: resolved });
243
+ }
244
+ }
245
+ const parents = getParentPathsFromTuples(tuples);
246
+ const orders = [];
247
+ const seen = /* @__PURE__ */ new Set();
248
+ preferredOrderMap.forEach((v) => {
249
+ const k = JSON.stringify(v.path);
250
+ if (!seen.has(k)) {
251
+ seen.add(k);
252
+ orders.push(v);
253
+ }
254
+ });
255
+ const fallbackOrders = computeOrdersFromState(root, parents);
256
+ for (let i = 0; i < fallbackOrders.length; i++) {
257
+ const v = fallbackOrders[i];
258
+ const k = JSON.stringify(v.path);
259
+ if (seen.has(k)) {
260
+ continue;
261
+ }
262
+ const pending = pendingChildrenByContainer.get(k);
263
+ if (pending && pending.size) {
264
+ const existing = new Set(v.keys);
265
+ for (const child of pending) {
266
+ if (existing.has(child)) {
267
+ continue;
268
+ }
269
+ v.keys.push(child);
270
+ }
271
+ }
272
+ seen.add(k);
273
+ orders.push(v);
274
+ }
275
+ return orders;
276
+ }