braid-text 0.2.4 → 0.2.6

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
@@ -386,9 +386,12 @@ braid_text.put = async (key, options) => {
386
386
 
387
387
  let og_v = version?.[0] || `${(is_valid_actor(peer) && peer) || Math.random().toString(36).slice(2, 7)}-${change_count - 1}`
388
388
 
389
- // reduce the version sequence by the number of char-edits
390
389
  let v = decode_version(og_v)
391
390
 
391
+ resource.length_cache.put(`${v[0]}-${v[1]}`, patches.reduce((a, b) =>
392
+ a + (b.content_codepoints.length ? b.content_codepoints.length : -(b.range[1] - b.range[0])),
393
+ max_pos))
394
+
392
395
  // validate version: make sure we haven't seen it already
393
396
  if (v[1] <= (resource.actor_seqs[v[0]] ?? -1)) {
394
397
 
@@ -453,10 +456,7 @@ braid_text.put = async (key, options) => {
453
456
  }
454
457
  resource.actor_seqs[v[0]] = v[1]
455
458
 
456
- resource.length_cache.put(`${v[0]}-${v[1]}`, patches.reduce((a, b) =>
457
- a + (b.content_codepoints.length ? b.content_codepoints.length : -(b.range[1] - b.range[0])),
458
- max_pos))
459
-
459
+ // reduce the version sequence by the number of char-edits
460
460
  v = `${v[0]}-${v[1] + 1 - change_count}`
461
461
 
462
462
  let ps = og_parents
@@ -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)
@@ -1084,7 +1085,7 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1084
1085
  function write_string(byte_array, str) {
1085
1086
  let str_bytes = new TextEncoder().encode(str)
1086
1087
  write_varint(byte_array, str_bytes.length)
1087
- byte_array.push(...str_bytes)
1088
+ for (let x of str_bytes) byte_array.push(x)
1088
1089
  }
1089
1090
 
1090
1091
  version = decode_version(version)
@@ -1112,11 +1113,11 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1112
1113
 
1113
1114
  file_info.push(3)
1114
1115
  write_varint(file_info, agent_names.length)
1115
- file_info.push(...agent_names)
1116
+ for (let x of agent_names) file_info.push(x)
1116
1117
 
1117
1118
  bytes.push(1)
1118
1119
  write_varint(bytes, file_info.length)
1119
- bytes.push(...file_info)
1120
+ for (let x of file_info) bytes.push(x)
1120
1121
 
1121
1122
  let branch = []
1122
1123
 
@@ -1133,12 +1134,12 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1133
1134
 
1134
1135
  branch.push(12)
1135
1136
  write_varint(branch, frontier.length)
1136
- branch.push(...frontier)
1137
+ for (let x of frontier) branch.push(x)
1137
1138
  }
1138
1139
 
1139
1140
  bytes.push(10)
1140
1141
  write_varint(bytes, branch.length)
1141
- bytes.push(...branch)
1142
+ for (let x of branch) bytes.push(x)
1142
1143
 
1143
1144
  let patches = []
1144
1145
 
@@ -1164,7 +1165,7 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1164
1165
  let known_chunk = []
1165
1166
  write_varint(known_chunk, unicode_chars.length * 2 + 1)
1166
1167
  write_varint(inserted_content_bytes, known_chunk.length)
1167
- inserted_content_bytes.push(...known_chunk)
1168
+ for (let x of known_chunk) inserted_content_bytes.push(x)
1168
1169
 
1169
1170
  patches.push(24)
1170
1171
  write_varint(patches, inserted_content_bytes.length)
@@ -1228,7 +1229,7 @@ function dt_create_bytes(version, parents, pos, del, ins) {
1228
1229
 
1229
1230
  patches.push(23)
1230
1231
  write_varint(patches, parents_bytes.length)
1231
- patches.push(...parents_bytes)
1232
+ for (let x of parents_bytes) patches.push(x)
1232
1233
 
1233
1234
  // write in patches
1234
1235
  bytes.push(20)
@@ -1250,7 +1251,7 @@ function OpLog_remote_to_local(doc, frontier) {
1250
1251
 
1251
1252
  let local_version = []
1252
1253
 
1253
- let max_version = Math.max(-1, ...doc.getLocalVersion())
1254
+ let max_version = doc.getLocalVersion().reduce((a, b) => Math.max(a, b), -1)
1254
1255
  for (let i = 0; i <= max_version; i++) {
1255
1256
  if (map[doc.localToRemoteVersion([i])[0].join("-")]) {
1256
1257
  local_version.push(i)
@@ -1731,6 +1732,8 @@ function apply_patch(obj, range, content) {
1731
1732
  }
1732
1733
  }
1733
1734
 
1735
+ braid_text.get_resource = get_resource
1736
+
1734
1737
  braid_text.encode_filename = encode_filename
1735
1738
  braid_text.decode_filename = decode_filename
1736
1739
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
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 () => {