braid-text 0.2.6 → 0.2.8
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 +1 -1
- package/index.js +23 -9
- package/package.json +1 -1
- package/server-demo.js +5 -13
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ Now open these URLs in your browser:
|
|
|
35
35
|
|
|
36
36
|
Or try opening the URL in [Braid-Chrome](https://github.com/braid-org/braid-chrome), or [another Braid client](https://bloop.monster/simpleditor), to edit it directly!
|
|
37
37
|
|
|
38
|
-
Check out the `server-demo.js` file to see examples for how to add access control, and a `/pages` endpoint to show all the edited pages.
|
|
38
|
+
Check out the `server-demo.js` file to see examples for how to add simple access control, where a user need only enter a password into a cookie in the javascript console like: `document.cookie = 'password'`; and a `/pages` endpoint to show all the edited pages.
|
|
39
39
|
|
|
40
40
|
## General Use on Server
|
|
41
41
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
let { Doc } = require("diamond-types-node")
|
|
2
|
+
let { Doc, OpLog, Branch } = require("diamond-types-node")
|
|
3
3
|
let braidify = require("braid-http").http_server
|
|
4
4
|
let fs = require("fs")
|
|
5
5
|
|
|
@@ -349,15 +349,8 @@ braid_text.put = async (key, options) => {
|
|
|
349
349
|
let parents = resource.doc.getRemoteVersion().map((x) => x.join("-")).sort()
|
|
350
350
|
let og_parents = options_parents || parents
|
|
351
351
|
|
|
352
|
-
function get_len() {
|
|
353
|
-
let d = dt_get(resource.doc, og_parents)
|
|
354
|
-
let len = d.len()
|
|
355
|
-
d.free()
|
|
356
|
-
return len
|
|
357
|
-
}
|
|
358
|
-
|
|
359
352
|
let max_pos = resource.length_cache.get('' + og_parents) ??
|
|
360
|
-
(v_eq(parents, og_parents) ? resource.doc.len() :
|
|
353
|
+
(v_eq(parents, og_parents) ? resource.doc.len() : dt_len(resource.doc, og_parents))
|
|
361
354
|
|
|
362
355
|
if (body != null) {
|
|
363
356
|
patches = [{
|
|
@@ -825,6 +818,27 @@ async function file_sync(key, process_delta, get_init) {
|
|
|
825
818
|
//////////////////////////////////////////////////////////////////
|
|
826
819
|
//////////////////////////////////////////////////////////////////
|
|
827
820
|
|
|
821
|
+
function dt_len(doc, version) {
|
|
822
|
+
let bytes = doc.toBytes()
|
|
823
|
+
let oplog = OpLog.fromBytes(bytes)
|
|
824
|
+
let [_agents, versions, _parentss] = dt_parse([...bytes])
|
|
825
|
+
|
|
826
|
+
let frontier = {}
|
|
827
|
+
version.forEach((x) => frontier[x] = true)
|
|
828
|
+
|
|
829
|
+
let local_version = []
|
|
830
|
+
for (let i = 0; i < versions.length; i++)
|
|
831
|
+
if (frontier[versions[i].join("-")]) local_version.push(i)
|
|
832
|
+
|
|
833
|
+
let b = new Branch()
|
|
834
|
+
b.merge(oplog, new Uint32Array(local_version))
|
|
835
|
+
let len = count_code_points(b.get())
|
|
836
|
+
b.free()
|
|
837
|
+
|
|
838
|
+
oplog.free()
|
|
839
|
+
return len
|
|
840
|
+
}
|
|
841
|
+
|
|
828
842
|
function dt_get(doc, version, agent = null) {
|
|
829
843
|
if (dt_get.last_doc) dt_get.last_doc.free()
|
|
830
844
|
|
package/package.json
CHANGED
package/server-demo.js
CHANGED
|
@@ -77,22 +77,14 @@ var server = require("http").createServer(async (req, res) => {
|
|
|
77
77
|
// return
|
|
78
78
|
// }
|
|
79
79
|
|
|
80
|
-
// TODO: uncomment
|
|
81
|
-
//
|
|
80
|
+
// TODO: uncomment out the code below to add basic access control,
|
|
81
|
+
// where a user logs in by openning the javascript console
|
|
82
|
+
// and entering: document.cookie = 'fake_password'
|
|
82
83
|
//
|
|
83
84
|
// var admin_pass = "fake_password"
|
|
84
|
-
//
|
|
85
|
-
// if (req.url === '/login_' + admin_pass) {
|
|
86
|
-
// res.writeHead(200, {
|
|
87
|
-
// "Content-Type": "text/plain",
|
|
88
|
-
// "Set-Cookie": `admin_pass=${admin_pass}; Path=/`,
|
|
89
|
-
// });
|
|
90
|
-
// res.end("Logged in successfully");
|
|
91
|
-
// return;
|
|
92
|
-
// }
|
|
93
|
-
//
|
|
85
|
+
//
|
|
94
86
|
// if (req.method == "PUT" || req.method == "POST" || req.method == "PATCH") {
|
|
95
|
-
// if (!req.headers.cookie?.
|
|
87
|
+
// if (!req.headers.cookie?.split(/;/).map(x => x.trim()).some(x => x === admin_pass)) {
|
|
96
88
|
// console.log("Blocked PUT:", { cookie: req.headers.cookie })
|
|
97
89
|
// res.statusCode = 401
|
|
98
90
|
// return res.end()
|