braid-text 0.2.14 → 0.2.16

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/README.md CHANGED
@@ -180,17 +180,17 @@ simpleton = simpleton_client(url, options)
180
180
  ## Testing
181
181
 
182
182
  ### to run unit tests:
183
- first run the demo server as usual:
183
+ first run the test server:
184
184
 
185
185
  npm install
186
- node server-demo.js
186
+ node test/server.js
187
187
 
188
- then open http://localhost:8888/test.html, and the boxes should turn green as the tests pass.
188
+ then open http://localhost:8889/test.html, and the boxes should turn green as the tests pass.
189
189
 
190
190
  ### to run fuzz tests:
191
191
 
192
192
  npm install
193
- node test.js
193
+ node test/test.js
194
194
 
195
195
  if the last output line looks like this, good:
196
196
 
package/index.js CHANGED
@@ -24,11 +24,7 @@ braid_text.serve = async (req, res, options = {}) => {
24
24
  ...options // Override with all options passed in
25
25
  }
26
26
 
27
- // free CORS
28
- res.setHeader("Access-Control-Allow-Origin", "*")
29
- res.setHeader("Access-Control-Allow-Methods", "*")
30
- res.setHeader("Access-Control-Allow-Headers", "*")
31
- res.setHeader("Access-Control-Expose-Headers", "*")
27
+ braid_text.free_cors(res)
32
28
 
33
29
  function my_end(statusCode, x) {
34
30
  res.statusCode = statusCode
@@ -40,8 +36,9 @@ braid_text.serve = async (req, res, options = {}) => {
40
36
  resource = await get_resource(options.key)
41
37
 
42
38
  braidify(req, res)
39
+ if (res.is_multiplexer) return
43
40
  } catch (e) {
44
- return my_end(400, "The server failed to process this request. The error generated was: " + e)
41
+ return my_end(500, "The server failed to process this request. The error generated was: " + e)
45
42
  }
46
43
 
47
44
  let peer = req.headers["peer"]
@@ -83,7 +80,7 @@ braid_text.serve = async (req, res, options = {}) => {
83
80
  try {
84
81
  x = await braid_text.get(resource, { version: req.version, parents: req.parents })
85
82
  } catch (e) {
86
- return my_end(400, "The server failed to get something. The error generated was: " + e)
83
+ return my_end(500, "The server failed to get something. The error generated was: " + e)
87
84
  }
88
85
 
89
86
  res.setHeader("Version", x.version.map((x) => JSON.stringify(x)).join(", "))
@@ -119,7 +116,7 @@ braid_text.serve = async (req, res, options = {}) => {
119
116
  try {
120
117
  return await braid_text.get(resource, options)
121
118
  } catch (e) {
122
- return my_end(400, "The server failed to get something. The error generated was: " + e)
119
+ return my_end(500, "The server failed to get something. The error generated was: " + e)
123
120
  }
124
121
  }
125
122
  }
@@ -195,7 +192,7 @@ braid_text.serve = async (req, res, options = {}) => {
195
192
  // - cons: typically implies that the request was missing an http conditional field like If-Match. that is to say, it implies that the request is missing a precondition, not that the server is missing a precondition
196
193
  return done_my_turn(425, e.message)
197
194
  } else {
198
- return done_my_turn(400, "The server failed to apply this version. The error generated was: " + e)
195
+ return done_my_turn(500, "The server failed to apply this version. The error generated was: " + e)
199
196
  }
200
197
  }
201
198
 
@@ -222,17 +219,14 @@ braid_text.get = async (key, options) => {
222
219
  let resource = (typeof key == 'string') ? await get_resource(key) : key
223
220
 
224
221
  if (!options.subscribe) {
225
- let doc = resource.doc
226
- if (options.version || options.parents) doc = dt_get(doc, options.version || options.parents)
227
-
228
- let ret = {
229
- version: doc.getRemoteVersion().map((x) => x.join("-")).sort(),
230
- body: doc.get()
231
- }
232
-
233
- if (options.version || options.parents) doc.free()
234
-
235
- return ret
222
+ return options.version || options.parents ?
223
+ {
224
+ version: options.version || options.parents,
225
+ body: dt_get_string(resource.doc, options.version || options.parents)
226
+ } : {
227
+ version: resource.doc.getRemoteVersion().map((x) => x.join("-")).sort(),
228
+ body: resource.doc.get()
229
+ }
236
230
  } else {
237
231
  if (options.merge_type != "dt") {
238
232
  let version = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
@@ -594,6 +588,13 @@ braid_text.list = async () => {
594
588
  } catch (e) { return [] }
595
589
  }
596
590
 
591
+ braid_text.free_cors = res => {
592
+ res.setHeader("Access-Control-Allow-Origin", "*")
593
+ res.setHeader("Access-Control-Allow-Methods", "*")
594
+ res.setHeader("Access-Control-Allow-Headers", "*")
595
+ res.setHeader("Access-Control-Expose-Headers", "*")
596
+ }
597
+
597
598
  async function get_resource(key) {
598
599
  let cache = braid_text.cache
599
600
  if (!cache[key]) cache[key] = new Promise(async done => {
@@ -786,14 +787,18 @@ async function file_sync(key, process_delta, get_init) {
786
787
  //////////////////////////////////////////////////////////////////
787
788
 
788
789
  function dt_len(doc, version) {
789
- let bytes = doc.toBytes()
790
- let oplog = OpLog.fromBytes(bytes)
791
- let [_agents, versions, _parentss] = dt_parse([...bytes])
790
+ return count_code_points(dt_get_string(doc, version))
791
+ }
792
792
 
793
- let frontier = new Set(version)
793
+ function dt_get_string(doc, version) {
794
+ var bytes = doc.toBytes()
795
+ var oplog = OpLog.fromBytes(bytes)
796
+ var [_agents, versions, _parentss] = dt_parse([...bytes])
794
797
 
795
- let local_version = []
796
- for (let i = 0; i < versions.length; i++) {
798
+ var frontier = new Set(version)
799
+
800
+ var local_version = []
801
+ for (var i = 0; i < versions.length; i++) {
797
802
  var v = versions[i].join("-")
798
803
  if (frontier.has(v)) {
799
804
  local_version.push(i)
@@ -803,13 +808,13 @@ function dt_len(doc, version) {
803
808
 
804
809
  if (frontier.size) throw new Error(`version not found: ${version}`)
805
810
 
806
- let b = new Branch()
811
+ var b = new Branch()
807
812
  b.merge(oplog, new Uint32Array(local_version))
808
- let len = count_code_points(b.get())
813
+ var s = b.get()
809
814
  b.free()
810
815
 
811
816
  oplog.free()
812
- return len
817
+ return s
813
818
  }
814
819
 
815
820
  function dt_get(doc, version, agent = null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",
package/server-demo.js CHANGED
@@ -12,9 +12,8 @@ var server = require("http").createServer(async (req, res) => {
12
12
  console.log(`${req.method} ${req.url}`)
13
13
 
14
14
  // Free the CORS
15
- free_the_cors(req, res)
16
- if (req.method === 'OPTIONS') return
17
-
15
+ braid_text.free_cors(res)
16
+ if (req.method === 'OPTIONS') return res.end()
18
17
 
19
18
  if (req.url.endsWith("?editor")) {
20
19
  res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
@@ -34,36 +33,6 @@ var server = require("http").createServer(async (req, res) => {
34
33
  return
35
34
  }
36
35
 
37
- if (req.url.startsWith('/test.html')) {
38
- let parts = req.url.split(/[\?&=]/g)
39
-
40
- if (parts[1] === 'check') {
41
- res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-cache" })
42
- return res.end(JSON.stringify({
43
- checking: parts[2],
44
- result: (await braid_text.get(parts[2])) != null
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
- }
60
- }
61
-
62
- res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
63
- require("fs").createReadStream("./test.html").pipe(res)
64
- return
65
- }
66
-
67
36
  // TODO: uncomment out the code below to add /pages endpoint,
68
37
  // which displays all the currently used keys
69
38
  //
@@ -103,21 +72,3 @@ var server = require("http").createServer(async (req, res) => {
103
72
  server.listen(port, () => {
104
73
  console.log(`server started on port ${port}`)
105
74
  })
106
-
107
-
108
- // Free the CORS!
109
- function free_the_cors (req, res) {
110
- res.setHeader('Range-Request-Allow-Methods', 'PATCH, PUT')
111
- res.setHeader('Range-Request-Allow-Units', 'json')
112
- res.setHeader("Patches", "OK")
113
- var free_the_cors = {
114
- "Access-Control-Allow-Origin": "*",
115
- "Access-Control-Allow-Methods": "OPTIONS, HEAD, GET, PUT, UNSUBSCRIBE",
116
- "Access-Control-Allow-Headers": "subscribe, client, version, parents, merge-type, content-type, content-range, patches, cache-control, peer"
117
- }
118
- Object.entries(free_the_cors).forEach(x => res.setHeader(x[0], x[1]))
119
- if (req.method === 'OPTIONS') {
120
- res.writeHead(200)
121
- res.end()
122
- }
123
- }
package/test/server.js ADDED
@@ -0,0 +1,50 @@
1
+
2
+ var port = process.argv[2] || 8889
3
+
4
+ var braid_text = require("../index.js")
5
+ braid_text.db_folder = null
6
+
7
+ var server = require("http").createServer(async (req, res) => {
8
+ console.log(`${req.method} ${req.url}`)
9
+
10
+ // Free the CORS
11
+ braid_text.free_cors(res)
12
+ if (req.method === 'OPTIONS') return
13
+
14
+ if (req.url.startsWith('/test.html')) {
15
+ let parts = req.url.split(/[\?&=]/g)
16
+
17
+ if (parts[1] === 'check') {
18
+ res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-cache" })
19
+ return res.end(JSON.stringify({
20
+ checking: parts[2],
21
+ result: (await braid_text.get(parts[2])) != null
22
+ }))
23
+ } else if (parts[1] === 'dt_create_bytes_big_name') {
24
+ try {
25
+ braid_text.dt_create_bytes('x'.repeat(1000000) + '-0', [], 0, 0, 'hi')
26
+ return res.end(JSON.stringify({ ok: true }))
27
+ } catch (e) {
28
+ return res.end(JSON.stringify({ ok: false, error: '' + e }))
29
+ }
30
+ } else if (parts[1] === 'dt_create_bytes_many_names') {
31
+ try {
32
+ braid_text.dt_create_bytes('hi-0', new Array(1000000).fill(0).map((x, i) => `x${i}-0`), 0, 0, 'hi')
33
+ return res.end(JSON.stringify({ ok: true }))
34
+ } catch (e) {
35
+ return res.end(JSON.stringify({ ok: false, error: '' + e }))
36
+ }
37
+ }
38
+
39
+ res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
40
+ require("fs").createReadStream(`${__dirname}/test.html`).pipe(res)
41
+ return
42
+ }
43
+
44
+ // Now serve the collaborative text!
45
+ braid_text.serve(req, res)
46
+ })
47
+
48
+ server.listen(port, () => {
49
+ console.log(`serving: http://localhost:${port}/test.html`)
50
+ })
@@ -1,6 +1,6 @@
1
1
 
2
2
  let { Doc } = require("diamond-types-node")
3
- let braid_text = require('./index.js')
3
+ let braid_text = require('../index.js')
4
4
  let {dt_get, dt_get_patches, dt_parse, dt_create_bytes} = braid_text
5
5
 
6
6
  process.on("unhandledRejection", (x) =>
@@ -23,7 +23,7 @@ async function main() {
23
23
  for (let t = 0; t < 10000; t++) {
24
24
  let seed = base + t
25
25
  // for (let t = 0; t < 1; t++) {
26
- // let seed = 7572861
26
+ // let seed = 7630348
27
27
 
28
28
  og_log(`t = ${t}, seed = ${seed}, best_n = ${best_n} @ ${best_seed}`)
29
29
  Math.randomSeed(seed)
File without changes