braid-text 0.2.3 → 0.2.5

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/index.js CHANGED
@@ -626,7 +626,8 @@ async function get_resource(key) {
626
626
  resource.need_defrag = false
627
627
 
628
628
  resource.actor_seqs = {}
629
- let max_version = Math.max(...resource.doc.getLocalVersion()) ?? -1
629
+
630
+ let max_version = resource.doc.getLocalVersion().reduce((a, b) => Math.max(a, b), -1)
630
631
  for (let i = 0; i <= max_version; i++) {
631
632
  let v = resource.doc.localToRemoteVersion([i])[0]
632
633
  resource.actor_seqs[v[0]] = Math.max(v[1], resource.actor_seqs[v[0]] ?? -1)
@@ -865,17 +866,28 @@ function dt_get(doc, version, agent = null) {
865
866
  ) {
866
867
  for (; i < I; i++) {
867
868
  let version = versions[i].join("-")
868
- if (!after_versions[version]) new_doc.mergeBytes(
869
+ if (after_versions[version]) continue
870
+ let og_i = i
871
+ let content = []
872
+ if (op_run.content?.[i - base_i]) content.push(op_run.content[i - base_i])
873
+ if (!!op_run.content === op_run.fwd)
874
+ while (i + 1 < I && !after_versions[versions[i + 1].join("-")]) {
875
+ i++
876
+ if (op_run.content?.[i - base_i]) content.push(op_run.content[i - base_i])
877
+ }
878
+ content = content.length ? content.join("") : null
879
+
880
+ new_doc.mergeBytes(
869
881
  dt_create_bytes(
870
882
  version,
871
- parentss[i].map((x) => x.join("-")),
883
+ parentss[og_i].map((x) => x.join("-")),
872
884
  op_run.fwd ?
873
885
  (op_run.content ?
874
- op_run.start + (i - base_i) :
886
+ op_run.start + (og_i - base_i) :
875
887
  op_run.start) :
876
888
  op_run.end - 1 - (i - base_i),
877
- op_run.content?.[i - base_i] != null ? 0 : 1,
878
- op_run.content?.[i - base_i]
889
+ op_run.content ? 0 : i - og_i + 1,
890
+ content
879
891
  )
880
892
  )
881
893
  }
@@ -1073,7 +1085,7 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1073
1085
  function write_string(byte_array, str) {
1074
1086
  let str_bytes = new TextEncoder().encode(str)
1075
1087
  write_varint(byte_array, str_bytes.length)
1076
- byte_array.push(...str_bytes)
1088
+ for (let x of str_bytes) byte_array.push(x)
1077
1089
  }
1078
1090
 
1079
1091
  version = decode_version(version)
@@ -1101,11 +1113,11 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1101
1113
 
1102
1114
  file_info.push(3)
1103
1115
  write_varint(file_info, agent_names.length)
1104
- file_info.push(...agent_names)
1116
+ for (let x of agent_names) file_info.push(x)
1105
1117
 
1106
1118
  bytes.push(1)
1107
1119
  write_varint(bytes, file_info.length)
1108
- bytes.push(...file_info)
1120
+ for (let x of file_info) bytes.push(x)
1109
1121
 
1110
1122
  let branch = []
1111
1123
 
@@ -1122,12 +1134,12 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1122
1134
 
1123
1135
  branch.push(12)
1124
1136
  write_varint(branch, frontier.length)
1125
- branch.push(...frontier)
1137
+ for (let x of frontier) branch.push(x)
1126
1138
  }
1127
1139
 
1128
1140
  bytes.push(10)
1129
1141
  write_varint(bytes, branch.length)
1130
- bytes.push(...branch)
1142
+ for (let x of branch) bytes.push(x)
1131
1143
 
1132
1144
  let patches = []
1133
1145
 
@@ -1153,7 +1165,7 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1153
1165
  let known_chunk = []
1154
1166
  write_varint(known_chunk, unicode_chars.length * 2 + 1)
1155
1167
  write_varint(inserted_content_bytes, known_chunk.length)
1156
- inserted_content_bytes.push(...known_chunk)
1168
+ for (let x of known_chunk) inserted_content_bytes.push(x)
1157
1169
 
1158
1170
  patches.push(24)
1159
1171
  write_varint(patches, inserted_content_bytes.length)
@@ -1217,7 +1229,7 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1217
1229
 
1218
1230
  patches.push(23)
1219
1231
  write_varint(patches, parents_bytes.length)
1220
- patches.push(...parents_bytes)
1232
+ for (let x of parents_bytes) patches.push(x)
1221
1233
 
1222
1234
  // write in patches
1223
1235
  bytes.push(20)
@@ -1239,7 +1251,7 @@ function OpLog_remote_to_local(doc, frontier) {
1239
1251
 
1240
1252
  let local_version = []
1241
1253
 
1242
- let max_version = Math.max(-1, ...doc.getLocalVersion())
1254
+ let max_version = doc.getLocalVersion().reduce((a, b) => Math.max(a, b), -1)
1243
1255
  for (let i = 0; i <= max_version; i++) {
1244
1256
  if (map[doc.localToRemoteVersion([i])[0].join("-")]) {
1245
1257
  local_version.push(i)
@@ -1568,7 +1580,7 @@ function decode_filename(encodedFilename) {
1568
1580
  function validate_version_array(x) {
1569
1581
  if (!Array.isArray(x)) throw new Error(`invalid version array: not an array`)
1570
1582
  x.sort()
1571
- for (xx of x) validate_actor_seq(xx)
1583
+ for (var xx of x) validate_actor_seq(xx)
1572
1584
  }
1573
1585
 
1574
1586
  function validate_actor_seq(x) {
@@ -1720,6 +1732,8 @@ function apply_patch(obj, range, content) {
1720
1732
  }
1721
1733
  }
1722
1734
 
1735
+ braid_text.get_resource = get_resource
1736
+
1723
1737
  braid_text.encode_filename = encode_filename
1724
1738
  braid_text.decode_filename = decode_filename
1725
1739
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidjs",
package/server-demo.js CHANGED
@@ -37,12 +37,26 @@ var server = require("http").createServer(async (req, res) => {
37
37
  if (req.url.startsWith('/test.html')) {
38
38
  let parts = req.url.split(/[\?&=]/g)
39
39
 
40
- if (parts[1] == 'check') {
40
+ if (parts[1] === 'check') {
41
41
  res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-cache" })
42
42
  return res.end(JSON.stringify({
43
43
  checking: parts[2],
44
44
  result: (await braid_text.get(parts[2])) != null
45
45
  }))
46
+ } else if (parts[1] === 'dt_create_bytes_big_name') {
47
+ try {
48
+ braid_text.dt_create_bytes('x'.repeat(1000000) + '-0', [], 0, 0, 'hi')
49
+ return res.end(JSON.stringify({ ok: true }))
50
+ } catch (e) {
51
+ return res.end(JSON.stringify({ ok: false, error: '' + e }))
52
+ }
53
+ } else if (parts[1] === 'dt_create_bytes_many_names') {
54
+ try {
55
+ braid_text.dt_create_bytes('hi-0', new Array(1000000).fill(0).map((x, i) => `x${i}-0`), 0, 0, 'hi')
56
+ return res.end(JSON.stringify({ ok: true }))
57
+ } catch (e) {
58
+ return res.end(JSON.stringify({ ok: false, error: '' + e }))
59
+ }
46
60
  }
47
61
 
48
62
  res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
package/test.html CHANGED
@@ -61,6 +61,50 @@ async function runTest(testName, testFunction, expectedResult) {
61
61
  }
62
62
  }
63
63
 
64
+ runTest(
65
+ "test subscribing starting at a version using simpleton",
66
+ async () => {
67
+ let key = 'test-' + Math.random().toString(36).slice(2)
68
+
69
+ let r = await braid_fetch(`/${key}`, {
70
+ method: 'PUT',
71
+ version: ['hi-1'],
72
+ parents: [],
73
+ body: 'xx'
74
+ })
75
+ if (!r.ok) throw 'got: ' + r.statusCode
76
+
77
+ let r2 = await braid_fetch(`/${key}`, {
78
+ version: ['hi-0'],
79
+ subscribe: true
80
+ })
81
+ return await new Promise(async (done, fail) => {
82
+ r2.subscribe(update => {
83
+ done(JSON.stringify(update.parents))
84
+ }, fail)
85
+ })
86
+ },
87
+ JSON.stringify([ "hi-0" ])
88
+ )
89
+
90
+ runTest(
91
+ "test dt_create_bytes with big agent name",
92
+ async () => {
93
+ let x = await (await fetch(`/test.html?dt_create_bytes_big_name`)).json()
94
+ return JSON.stringify(x)
95
+ },
96
+ JSON.stringify({ok: true})
97
+ )
98
+
99
+ runTest(
100
+ "test dt_create_bytes with many agent names",
101
+ async () => {
102
+ let x = await (await fetch(`/test.html?dt_create_bytes_many_names`)).json()
103
+ return JSON.stringify(x)
104
+ },
105
+ JSON.stringify({ok: true})
106
+ )
107
+
64
108
  runTest(
65
109
  "test deleting a resource",
66
110
  async () => {
package/test.js CHANGED
@@ -16,6 +16,7 @@ async function main() {
16
16
  let best_seed = NaN
17
17
  let best_n = Infinity
18
18
  let base = Math.floor(Math.random() * 10000000)
19
+ let st = Date.now()
19
20
 
20
21
  let og_log = console.log
21
22
  console.log = () => {}
@@ -113,6 +114,7 @@ async function main() {
113
114
  }
114
115
  }
115
116
  og_log(`best_seed = ${best_seed}, best_n = ${best_n}`)
117
+ og_log(`time = ${Date.now() - st}`)
116
118
  }
117
119
 
118
120
  function make_random_edit(doc) {