braid-text 0.2.14 → 0.2.15

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"]
@@ -594,6 +591,13 @@ braid_text.list = async () => {
594
591
  } catch (e) { return [] }
595
592
  }
596
593
 
594
+ braid_text.free_cors = res => {
595
+ res.setHeader("Access-Control-Allow-Origin", "*")
596
+ res.setHeader("Access-Control-Allow-Methods", "*")
597
+ res.setHeader("Access-Control-Allow-Headers", "*")
598
+ res.setHeader("Access-Control-Expose-Headers", "*")
599
+ }
600
+
597
601
  async function get_resource(key) {
598
602
  let cache = braid_text.cache
599
603
  if (!cache[key]) cache[key] = new Promise(async done => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.2.14",
3
+ "version": "0.2.15",
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(`server started on port ${port}`)
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) =>
File without changes