@yorkie-js/react 0.6.17 → 0.6.18

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.
@@ -5,7 +5,7 @@ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
5
5
  import { jsx } from "react/jsx-runtime";
6
6
  import { createContext, useMemo, useState, useEffect, useContext, useCallback } from "react";
7
7
  const name$1 = "@yorkie-js/react";
8
- const version$1 = "0.6.17";
8
+ const version$1 = "0.6.18";
9
9
  const pkg$1 = {
10
10
  name: name$1,
11
11
  version: version$1
@@ -6456,6 +6456,10 @@ const _DocumentSummary = class _DocumentSummary extends Message {
6456
6456
  * @generated from field: yorkie.v1.DocSize document_size = 8;
6457
6457
  */
6458
6458
  __publicField(this, "documentSize");
6459
+ /**
6460
+ * @generated from field: string schema_key = 9;
6461
+ */
6462
+ __publicField(this, "schemaKey", "");
6459
6463
  /**
6460
6464
  * @generated from field: google.protobuf.Timestamp created_at = 4;
6461
6465
  */
@@ -6515,6 +6519,13 @@ __publicField(_DocumentSummary, "fields", proto3.util.newFieldList(() => [
6515
6519
  /* ScalarType.INT32 */
6516
6520
  },
6517
6521
  { no: 8, name: "document_size", kind: "message", T: DocSize },
6522
+ {
6523
+ no: 9,
6524
+ name: "schema_key",
6525
+ kind: "scalar",
6526
+ T: 9
6527
+ /* ScalarType.STRING */
6528
+ },
6518
6529
  { no: 4, name: "created_at", kind: "message", T: Timestamp },
6519
6530
  { no: 5, name: "accessed_at", kind: "message", T: Timestamp },
6520
6531
  { no: 6, name: "updated_at", kind: "message", T: Timestamp }
@@ -6570,9 +6581,9 @@ let Presence$1 = (_g = class extends Message {
6570
6581
  constructor(data) {
6571
6582
  super();
6572
6583
  /**
6573
- * @generated from field: repeated string data = 1;
6584
+ * @generated from field: map<string, string> data = 1;
6574
6585
  */
6575
- __publicField(this, "data", []);
6586
+ __publicField(this, "data", {});
6576
6587
  proto3.util.initPartial(data, this);
6577
6588
  }
6578
6589
  static fromBinary(bytes, options) {
@@ -6588,7 +6599,11 @@ let Presence$1 = (_g = class extends Message {
6588
6599
  return proto3.util.equals(_g, a, b);
6589
6600
  }
6590
6601
  }, __publicField(_g, "runtime", proto3), __publicField(_g, "typeName", "yorkie.v1.Presence"), __publicField(_g, "fields", proto3.util.newFieldList(() => [
6591
- { no: 1, name: "data", kind: "scalar", T: 9, repeated: true }
6602
+ { no: 1, name: "data", kind: "map", K: 9, V: {
6603
+ kind: "scalar",
6604
+ T: 9
6605
+ /* ScalarType.STRING */
6606
+ } }
6592
6607
  ])), _g);
6593
6608
  let Checkpoint$1 = (_h = class extends Message {
6594
6609
  constructor(data) {
@@ -6900,6 +6915,128 @@ __publicField(_DocSize, "fields", proto3.util.newFieldList(() => [
6900
6915
  { no: 2, name: "gc", kind: "message", T: DataSize }
6901
6916
  ]));
6902
6917
  let DocSize = _DocSize;
6918
+ const _Schema = class _Schema extends Message {
6919
+ constructor(data) {
6920
+ super();
6921
+ /**
6922
+ * @generated from field: string id = 1;
6923
+ */
6924
+ __publicField(this, "id", "");
6925
+ /**
6926
+ * @generated from field: string name = 2;
6927
+ */
6928
+ __publicField(this, "name", "");
6929
+ /**
6930
+ * @generated from field: int32 version = 3;
6931
+ */
6932
+ __publicField(this, "version", 0);
6933
+ /**
6934
+ * @generated from field: string body = 4;
6935
+ */
6936
+ __publicField(this, "body", "");
6937
+ /**
6938
+ * @generated from field: repeated yorkie.v1.Rule rules = 5;
6939
+ */
6940
+ __publicField(this, "rules", []);
6941
+ /**
6942
+ * @generated from field: google.protobuf.Timestamp created_at = 6;
6943
+ */
6944
+ __publicField(this, "createdAt");
6945
+ proto3.util.initPartial(data, this);
6946
+ }
6947
+ static fromBinary(bytes, options) {
6948
+ return new _Schema().fromBinary(bytes, options);
6949
+ }
6950
+ static fromJson(jsonValue, options) {
6951
+ return new _Schema().fromJson(jsonValue, options);
6952
+ }
6953
+ static fromJsonString(jsonString, options) {
6954
+ return new _Schema().fromJsonString(jsonString, options);
6955
+ }
6956
+ static equals(a, b) {
6957
+ return proto3.util.equals(_Schema, a, b);
6958
+ }
6959
+ };
6960
+ __publicField(_Schema, "runtime", proto3);
6961
+ __publicField(_Schema, "typeName", "yorkie.v1.Schema");
6962
+ __publicField(_Schema, "fields", proto3.util.newFieldList(() => [
6963
+ {
6964
+ no: 1,
6965
+ name: "id",
6966
+ kind: "scalar",
6967
+ T: 9
6968
+ /* ScalarType.STRING */
6969
+ },
6970
+ {
6971
+ no: 2,
6972
+ name: "name",
6973
+ kind: "scalar",
6974
+ T: 9
6975
+ /* ScalarType.STRING */
6976
+ },
6977
+ {
6978
+ no: 3,
6979
+ name: "version",
6980
+ kind: "scalar",
6981
+ T: 5
6982
+ /* ScalarType.INT32 */
6983
+ },
6984
+ {
6985
+ no: 4,
6986
+ name: "body",
6987
+ kind: "scalar",
6988
+ T: 9
6989
+ /* ScalarType.STRING */
6990
+ },
6991
+ { no: 5, name: "rules", kind: "message", T: Rule, repeated: true },
6992
+ { no: 6, name: "created_at", kind: "message", T: Timestamp }
6993
+ ]));
6994
+ let Schema = _Schema;
6995
+ const _Rule = class _Rule extends Message {
6996
+ constructor(data) {
6997
+ super();
6998
+ /**
6999
+ * @generated from field: string path = 1;
7000
+ */
7001
+ __publicField(this, "path", "");
7002
+ /**
7003
+ * @generated from field: string type = 2;
7004
+ */
7005
+ __publicField(this, "type", "");
7006
+ proto3.util.initPartial(data, this);
7007
+ }
7008
+ static fromBinary(bytes, options) {
7009
+ return new _Rule().fromBinary(bytes, options);
7010
+ }
7011
+ static fromJson(jsonValue, options) {
7012
+ return new _Rule().fromJson(jsonValue, options);
7013
+ }
7014
+ static fromJsonString(jsonString, options) {
7015
+ return new _Rule().fromJsonString(jsonString, options);
7016
+ }
7017
+ static equals(a, b) {
7018
+ return proto3.util.equals(_Rule, a, b);
7019
+ }
7020
+ };
7021
+ __publicField(_Rule, "runtime", proto3);
7022
+ __publicField(_Rule, "typeName", "yorkie.v1.Rule");
7023
+ __publicField(_Rule, "fields", proto3.util.newFieldList(() => [
7024
+ {
7025
+ no: 1,
7026
+ name: "path",
7027
+ kind: "scalar",
7028
+ T: 9
7029
+ /* ScalarType.STRING */
7030
+ },
7031
+ {
7032
+ no: 2,
7033
+ name: "type",
7034
+ kind: "scalar",
7035
+ T: 9
7036
+ /* ScalarType.STRING */
7037
+ }
7038
+ ]));
7039
+ let Rule = _Rule;
6903
7040
  const _ActivateClientRequest = class _ActivateClientRequest extends Message {
6904
7041
  constructor(data) {
6905
7042
  super();
@@ -7044,6 +7181,10 @@ const _AttachDocumentRequest = class _AttachDocumentRequest extends Message {
7044
7181
  * @generated from field: yorkie.v1.ChangePack change_pack = 2;
7045
7182
  */
7046
7183
  __publicField(this, "changePack");
7184
+ /**
7185
+ * @generated from field: string schema_key = 3;
7186
+ */
7187
+ __publicField(this, "schemaKey", "");
7047
7188
  proto3.util.initPartial(data, this);
7048
7189
  }
7049
7190
  static fromBinary(bytes, options) {
@@ -7069,7 +7210,14 @@ __publicField(_AttachDocumentRequest, "fields", proto3.util.newFieldList(() => [
7069
7210
  T: 9
7070
7211
  /* ScalarType.STRING */
7071
7212
  },
7072
- { no: 2, name: "change_pack", kind: "message", T: ChangePack$1 }
7213
+ { no: 2, name: "change_pack", kind: "message", T: ChangePack$1 },
7214
+ {
7215
+ no: 3,
7216
+ name: "schema_key",
7217
+ kind: "scalar",
7218
+ T: 9
7219
+ /* ScalarType.STRING */
7220
+ }
7073
7221
  ]));
7074
7222
  let AttachDocumentRequest = _AttachDocumentRequest;
7075
7223
  const _AttachDocumentResponse = class _AttachDocumentResponse extends Message {
@@ -7079,14 +7227,18 @@ const _AttachDocumentResponse = class _AttachDocumentResponse extends Message {
7079
7227
  * @generated from field: string document_id = 1;
7080
7228
  */
7081
7229
  __publicField(this, "documentId", "");
7230
+ /**
7231
+ * @generated from field: yorkie.v1.ChangePack change_pack = 2;
7232
+ */
7233
+ __publicField(this, "changePack");
7082
7234
  /**
7083
7235
  * @generated from field: int32 max_size_per_document = 3;
7084
7236
  */
7085
7237
  __publicField(this, "maxSizePerDocument", 0);
7086
7238
  /**
7087
- * @generated from field: yorkie.v1.ChangePack change_pack = 2;
7239
+ * @generated from field: repeated yorkie.v1.Rule schema_rules = 4;
7088
7240
  */
7089
- __publicField(this, "changePack");
7241
+ __publicField(this, "schemaRules", []);
7090
7242
  proto3.util.initPartial(data, this);
7091
7243
  }
7092
7244
  static fromBinary(bytes, options) {
@@ -7112,6 +7264,7 @@ __publicField(_AttachDocumentResponse, "fields", proto3.util.newFieldList(() =>
7112
7264
  T: 9
7113
7265
  /* ScalarType.STRING */
7114
7266
  },
7267
+ { no: 2, name: "change_pack", kind: "message", T: ChangePack$1 },
7115
7268
  {
7116
7269
  no: 3,
7117
7270
  name: "max_size_per_document",
@@ -7119,7 +7272,7 @@ __publicField(_AttachDocumentResponse, "fields", proto3.util.newFieldList(() =>
7119
7272
  T: 5
7120
7273
  /* ScalarType.INT32 */
7121
7274
  },
7122
- { no: 2, name: "change_pack", kind: "message", T: ChangePack$1 }
7275
+ { no: 4, name: "schema_rules", kind: "message", T: Rule, repeated: true }
7123
7276
  ]));
7124
7277
  let AttachDocumentResponse = _AttachDocumentResponse;
7125
7278
  const _DetachDocumentRequest = class _DetachDocumentRequest extends Message {
@@ -7890,6 +8043,7 @@ var Code = /* @__PURE__ */ ((Code2) => {
7890
8043
  Code2["ErrDocumentNotDetached"] = "ErrDocumentNotDetached";
7891
8044
  Code2["ErrDocumentRemoved"] = "ErrDocumentRemoved";
7892
8045
  Code2["ErrDocumentSizeExceedsLimit"] = "ErrDocumentSizeExceedsLimit";
8046
+ Code2["ErrDocumentSchemaValidationFailed"] = "ErrDocumentSchemaValidationFailed";
7893
8047
  Code2["ErrInvalidObjectKey"] = "ErrInvalidObjectKey";
7894
8048
  Code2["ErrInvalidArgument"] = "ErrInvalidArgument";
7895
8049
  Code2["ErrNotInitialized"] = "ErrNotInitialized";
@@ -15941,10 +16095,9 @@ class TreeStyleOperation extends Operation {
15941
16095
  }
15942
16096
  function toPresence(presence) {
15943
16097
  const pbPresence = new Presence$1();
15944
- const pbDataArray = pbPresence.data;
16098
+ const pbDataMap = pbPresence.data;
15945
16099
  for (const [key, value] of Object.entries(presence)) {
15946
- pbDataArray.push(key);
15947
- pbDataArray.push(JSON.stringify(value));
16100
+ pbDataMap[key] = JSON.stringify(value);
15948
16101
  }
15949
16102
  return pbPresence;
15950
16103
  }
@@ -16506,11 +16659,9 @@ function fromTimeTicket(pbTimeTicket) {
16506
16659
  }
16507
16660
  function fromPresence(pbPresence) {
16508
16661
  const data = {};
16509
- for (let i = 0; i < pbPresence.data.length; i += 2) {
16510
- const key = pbPresence.data[i];
16511
- const value = pbPresence.data[i + 1];
16662
+ Object.entries(pbPresence.data).forEach(([key, value]) => {
16512
16663
  data[key] = JSON.parse(value);
16513
- }
16664
+ });
16514
16665
  return data;
16515
16666
  }
16516
16667
  function fromPresenceChange(pbPresenceChange) {
@@ -16946,6 +17097,14 @@ function fromElement(pbElement) {
16946
17097
  throw new YorkieError(Code.ErrUnimplemented, `unimplemented element`);
16947
17098
  }
16948
17099
  }
17100
+ function fromSchemaRules(pbRules) {
17101
+ return pbRules.map((pbRule) => {
17102
+ return {
17103
+ path: pbRule.path,
17104
+ type: pbRule.type
17105
+ };
17106
+ });
17107
+ }
16949
17108
  function bytesToSnapshot(bytes) {
16950
17109
  if (!bytes) {
16951
17110
  return {
@@ -17043,7 +17202,8 @@ const converter = {
17043
17202
  bytesToChangeID,
17044
17203
  bytesToOperation,
17045
17204
  versionVectorToHex,
17046
- hexToVersionVector
17205
+ hexToVersionVector,
17206
+ fromSchemaRules
17047
17207
  };
17048
17208
  var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
17049
17209
  LogLevel2[LogLevel2["Trivial"] = 0] = "Trivial";
@@ -17203,6 +17363,156 @@ class Attachment {
17203
17363
  this.watchLoopTimerID = void 0;
17204
17364
  }
17205
17365
  }
17366
+ function validateYorkieRuleset(data, ruleset) {
17367
+ const errors = [];
17368
+ for (const rule of ruleset) {
17369
+ const value = getValueByPath(data, rule.path);
17370
+ const result = validateValue(value, rule);
17371
+ if (!result.valid) {
17372
+ for (const error of result.errors || []) {
17373
+ errors.push(error);
17374
+ }
17375
+ }
17376
+ }
17377
+ return {
17378
+ valid: errors.length === 0,
17379
+ errors
17380
+ };
17381
+ }
17382
+ function getValueByPath(obj, path) {
17383
+ if (!path.startsWith("$")) {
17384
+ throw new Error(`Path must start with $, got ${path}`);
17385
+ }
17386
+ const keys = path.split(".");
17387
+ let current = obj;
17388
+ for (let i = 1; i < keys.length; i++) {
17389
+ const key = keys[i];
17390
+ if (!(current instanceof CRDTObject)) {
17391
+ return void 0;
17392
+ }
17393
+ current = current.get(key);
17394
+ }
17395
+ return current;
17396
+ }
17397
+ function validateValue(value, rule) {
17398
+ switch (rule.type) {
17399
+ case "string":
17400
+ case "boolean":
17401
+ case "integer":
17402
+ case "double":
17403
+ case "long":
17404
+ case "date":
17405
+ case "bytes":
17406
+ case "null":
17407
+ return validatePrimitiveValue(value, rule);
17408
+ case "object":
17409
+ if (!(value instanceof CRDTObject)) {
17410
+ return {
17411
+ valid: false,
17412
+ errors: [
17413
+ {
17414
+ path: rule.path,
17415
+ message: `Expected object at path ${rule.path}`
17416
+ }
17417
+ ]
17418
+ };
17419
+ }
17420
+ break;
17421
+ case "array":
17422
+ if (!(value instanceof CRDTArray)) {
17423
+ return {
17424
+ valid: false,
17425
+ errors: [
17426
+ {
17427
+ path: rule.path,
17428
+ message: `Expected array at path ${rule.path}`
17429
+ }
17430
+ ]
17431
+ };
17432
+ }
17433
+ break;
17434
+ case "yorkie.Text":
17435
+ if (!(value instanceof CRDTText)) {
17436
+ return {
17437
+ valid: false,
17438
+ errors: [
17439
+ {
17440
+ path: rule.path,
17441
+ message: `Expected yorkie.Text at path ${rule.path}`
17442
+ }
17443
+ ]
17444
+ };
17445
+ }
17446
+ break;
17447
+ case "yorkie.Tree":
17448
+ if (!(value instanceof CRDTTree)) {
17449
+ return {
17450
+ valid: false,
17451
+ errors: [
17452
+ {
17453
+ path: rule.path,
17454
+ message: `Expected yorkie.Tree at path ${rule.path}`
17455
+ }
17456
+ ]
17457
+ };
17458
+ }
17459
+ break;
17460
+ case "yorkie.Counter":
17461
+ if (!(value instanceof CRDTCounter)) {
17462
+ return {
17463
+ valid: false,
17464
+ errors: [
17465
+ {
17466
+ path: rule.path,
17467
+ message: `Expected yorkie.Counter at path ${rule.path}`
17468
+ }
17469
+ ]
17470
+ };
17471
+ }
17472
+ break;
17473
+ default:
17474
+ throw new Error(`Unknown rule type: ${rule.type}`);
17475
+ }
17476
+ return {
17477
+ valid: true
17478
+ };
17479
+ }
17480
+ function getPrimitiveType(type) {
17481
+ switch (type) {
17482
+ case "null":
17483
+ return PrimitiveType.Null;
17484
+ case "boolean":
17485
+ return PrimitiveType.Boolean;
17486
+ case "integer":
17487
+ return PrimitiveType.Integer;
17488
+ case "long":
17489
+ return PrimitiveType.Long;
17490
+ case "double":
17491
+ return PrimitiveType.Double;
17492
+ case "string":
17493
+ return PrimitiveType.String;
17494
+ case "bytes":
17495
+ return PrimitiveType.Bytes;
17496
+ case "date":
17497
+ return PrimitiveType.Date;
17498
+ default:
17499
+ throw new Error(`Unknown primitive type: ${type}`);
17500
+ }
17501
+ }
17502
+ function validatePrimitiveValue(value, rule) {
17503
+ if (value instanceof Primitive && value.getType() === getPrimitiveType(rule.type)) {
17504
+ return { valid: true };
17505
+ }
17506
+ return {
17507
+ valid: false,
17508
+ errors: [
17509
+ {
17510
+ path: rule.path,
17511
+ message: `Expected ${rule.type} at path ${rule.path}`
17512
+ }
17513
+ ]
17514
+ };
17515
+ }
17206
17516
  const Noop = () => {
17207
17517
  };
17208
17518
  class ObserverProxy {
@@ -19663,6 +19973,7 @@ class Document {
19663
19973
  __publicField(this, "checkpoint");
19664
19974
  __publicField(this, "localChanges");
19665
19975
  __publicField(this, "maxSizeLimit");
19976
+ __publicField(this, "schemaRules");
19666
19977
  __publicField(this, "root");
19667
19978
  __publicField(this, "clone");
19668
19979
  __publicField(this, "eventStream");
@@ -19696,6 +20007,7 @@ class Document {
19696
20007
  this.checkpoint = InitialCheckpoint;
19697
20008
  this.localChanges = [];
19698
20009
  this.maxSizeLimit = 0;
20010
+ this.schemaRules = [];
19699
20011
  this.eventStream = createObservable((observer) => {
19700
20012
  this.eventStreamObserver = observer;
19701
20013
  });
@@ -19715,7 +20027,7 @@ class Document {
19715
20027
  * `update` executes the given updater to update this document.
19716
20028
  */
19717
20029
  update(updater, message) {
19718
- var _a2;
20030
+ var _a2, _b2, _c2;
19719
20031
  if (this.getStatus() === "removed") {
19720
20032
  throw new YorkieError(Code.ErrDocumentRemoved, `${this.key} is removed`);
19721
20033
  }
@@ -19746,7 +20058,21 @@ class Document {
19746
20058
  } finally {
19747
20059
  this.isUpdating = false;
19748
20060
  }
19749
- const size = totalDocSize((_a2 = this.clone) == null ? void 0 : _a2.root.getDocSize());
20061
+ const schemaRules = this.getSchemaRules();
20062
+ if (!context.isPresenceOnlyChange() && schemaRules.length > 0) {
20063
+ const result = validateYorkieRuleset(
20064
+ (_a2 = this.clone) == null ? void 0 : _a2.root.getObject(),
20065
+ schemaRules
20066
+ );
20067
+ if (!result.valid) {
20068
+ this.clone = void 0;
20069
+ throw new YorkieError(
20070
+ Code.ErrDocumentSchemaValidationFailed,
20071
+ `schema validation failed: ${(_b2 = result.errors) == null ? void 0 : _b2.map((error) => error.message).join(", ")}`
20072
+ );
20073
+ }
20074
+ }
20075
+ const size = totalDocSize((_c2 = this.clone) == null ? void 0 : _c2.root.getDocSize());
19750
20076
  if (!context.isPresenceOnlyChange() && this.maxSizeLimit > 0 && this.maxSizeLimit < size) {
19751
20077
  this.clone = void 0;
19752
20078
  throw new YorkieError(
@@ -20194,6 +20520,18 @@ class Document {
20194
20520
  setMaxSizePerDocument(size) {
20195
20521
  this.maxSizeLimit = size;
20196
20522
  }
20523
+ /**
20524
+ * `getSchemaRules` gets the schema rules of this document.
20525
+ */
20526
+ getSchemaRules() {
20527
+ return this.schemaRules;
20528
+ }
20529
+ /**
20530
+ * `setSchemaRules` sets the schema rules of this document.
20531
+ */
20532
+ setSchemaRules(rules) {
20533
+ this.schemaRules = rules;
20534
+ }
20197
20535
  /**
20198
20536
  * `garbageCollect` purges elements that were removed before the given time.
20199
20537
  *
@@ -20895,7 +21233,7 @@ function createAuthInterceptor(apiKey, token) {
20895
21233
  };
20896
21234
  }
20897
21235
  const name = "@yorkie-js/sdk";
20898
- const version = "0.6.17";
21236
+ const version = "0.6.18";
20899
21237
  const pkg = {
20900
21238
  name,
20901
21239
  version
@@ -21121,7 +21459,8 @@ class Client {
21121
21459
  const res = await this.rpcClient.attachDocument(
21122
21460
  {
21123
21461
  clientId: this.id,
21124
- changePack: converter.toChangePack(doc.createChangePack())
21462
+ changePack: converter.toChangePack(doc.createChangePack()),
21463
+ schemaKey: opts.schema
21125
21464
  },
21126
21465
  { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
21127
21466
  );
@@ -21129,6 +21468,9 @@ class Client {
21129
21468
  if (maxSize > 0) {
21130
21469
  doc.setMaxSizePerDocument(res.maxSizePerDocument);
21131
21470
  }
21471
+ if (res.schemaRules.length > 0) {
21472
+ doc.setSchemaRules(converter.fromSchemaRules(res.schemaRules));
21473
+ }
21132
21474
  const pack = converter.fromChangePack(res.changePack);
21133
21475
  doc.applyChangePack(pack);
21134
21476
  if (doc.getStatus() === DocStatus.Removed) {