braid-text 0.3.21 → 0.3.23

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
@@ -69,11 +69,13 @@ http_server.on("request", (req, res) => {
69
69
  - `res`: Outgoing HTTP response object.
70
70
  - `options`: <small style="color:lightgrey">[optional]</small> An object containing additional options:
71
71
  - `key`: <small style="color:lightgrey">[optional]</small> ID of text resource to sync with. Defaults to `req.url`.
72
- - `put_cb`: <small style="color:lightgrey">[optional]</small> Callback invoked after a PUT changes a resource. Signature: `(key, val, old_val, patches)` where:
72
+ - `put_cb`: <small style="color:lightgrey">[optional]</small> Callback invoked after a PUT changes a resource. Signature: `(key, val, {old_val, patches, version, parents})` where:
73
73
  - `key` - The resource key
74
74
  - `val` - The new document text after the PUT
75
75
  - `old_val` - The document text before the PUT
76
76
  - `patches` - Array of patches applied (each `{unit, range, content}`), or `null` for full-body replacements
77
+ - `version` - The version after the PUT
78
+ - `parents` - The version prior to the PUT
77
79
  - This is the main method of this library, and does all the work to handle Braid-HTTP `GET` and `PUT` requests concerned with a specific text resource.
78
80
 
79
81
  `await braid_text.get(key)`
@@ -12,6 +12,8 @@
12
12
  //
13
13
  // url: resource endpoint
14
14
  //
15
+ // headers: custom headers that get forwarded through into the fetch
16
+ //
15
17
  // on_patches?: (patches) => void
16
18
  // processes incoming patches by applying them to the UI/textarea.
17
19
  // Patches are guaranteed to be in-order and non-overlapping.
@@ -104,7 +106,8 @@ function simpleton_client(url, {
104
106
  get_patches,
105
107
  get_state,
106
108
  content_type,
107
-
109
+ headers: user_headers, // The user can pass in custom headers
110
+ // that are forwared into the fetch
108
111
  on_error,
109
112
  on_res,
110
113
  on_online,
@@ -141,7 +144,8 @@ function simpleton_client(url, {
141
144
  retry: () => true,
142
145
  parents: () => client_version.length ? client_version : null,
143
146
  onSubscriptionStatus: status => on_online && on_online(status.online),
144
- headers: { "Merge-Type": "simpleton",
147
+ headers: { ...user_headers,
148
+ "Merge-Type": "simpleton",
145
149
  ...content_type && {Accept: content_type} },
146
150
  }).then(res => {
147
151
  if (on_res) on_res(res)
@@ -328,6 +332,7 @@ function simpleton_client(url, {
328
332
  peer, version, parents, patches,
329
333
  retry: (res) => res.status !== 550,
330
334
  headers: {
335
+ ...user_headers,
331
336
  "Merge-Type": "simpleton",
332
337
  ...send_digests && {
333
338
  "Repr-Digest": await get_digest(client_state) },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.3.21",
3
+ "version": "0.3.23",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",
@@ -9,8 +9,10 @@
9
9
  "test": "node test/test.js"
10
10
  },
11
11
  "main": "server.js",
12
+ "types": "server.d.ts",
12
13
  "files": [
13
14
  "server.js",
15
+ "server.d.ts",
14
16
  "simpleton-client.js",
15
17
  "client/simpleton-sync.js",
16
18
  "client/cursor-sync.js",
package/server.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { IncomingMessage, ServerResponse } from 'http'
2
+
3
+ interface PutCallbackParams {
4
+ old_val: string
5
+ patches: Array<{ unit: string; range: string; content: string }> | null
6
+ version: string[]
7
+ parents: string[]
8
+ }
9
+
10
+ interface ServeOptions {
11
+ key?: string
12
+ put_cb?: (key: string, val: string, params: PutCallbackParams) => void
13
+ }
14
+
15
+ interface GetResult {
16
+ version: string[]
17
+ body: string | Uint8Array
18
+ }
19
+
20
+ interface Resource {
21
+ key: string
22
+ val: string
23
+ version: string[]
24
+ }
25
+
26
+ interface BraidText {
27
+ verbose: boolean
28
+ db_folder: string | null
29
+ cors: boolean
30
+ cache: Record<string, Promise<Resource>>
31
+
32
+ serve(req: IncomingMessage, res: ServerResponse, options?: ServeOptions): Promise<void>
33
+ get(key: string): Promise<string | null>
34
+ get(key: string, options: Record<string, any>): Promise<GetResult>
35
+ put(key: string, options: Record<string, any>): Promise<{ change_count: number }>
36
+ delete(key: string): Promise<void>
37
+ list(): Promise<string[]>
38
+ sync(a: string, b: string | URL, options?: Record<string, any>): Promise<void>
39
+ free_cors(res: ServerResponse): void
40
+ create_braid_text(): BraidText
41
+ }
42
+
43
+ declare const braidText: BraidText
44
+ export = braidText
package/server.js CHANGED
@@ -345,7 +345,7 @@ function create_braid_text() {
345
345
  braid_text.serve = async (req, res, options = {}) => {
346
346
  options = {
347
347
  key: req.url.split('?')[0], // Default key
348
- put_cb: (key, val, old_val, patches) => { }, // Default callback when a PUT changes a key
348
+ put_cb: (key, val, params) => { }, // Default callback when a PUT changes a key
349
349
  ...options // Override with all options passed in
350
350
  }
351
351
 
@@ -556,6 +556,7 @@ function create_braid_text() {
556
556
  }
557
557
 
558
558
  var old_val = resource.val
559
+ var old_version = resource.version
559
560
  var put_patches = patches ? patches.map(p => ({unit: p.unit, range: p.range, content: p.content})) : null
560
561
  var {change_count} = await braid_text.put(resource, { peer, version: req.version, parents: req.parents, patches, body, merge_type })
561
562
 
@@ -577,7 +578,7 @@ function create_braid_text() {
577
578
 
578
579
  res.setHeader("Version", get_current_version())
579
580
 
580
- options.put_cb(options.key, resource.val, old_val, put_patches)
581
+ options.put_cb(options.key, resource.val, {old_val, patches: put_patches, version: resource.version, parents: old_version})
581
582
  } catch (e) {
582
583
  console.log(`${req.method} ERROR: ${e.stack}`)
583
584
  return done_my_turn(500, "The server failed to apply this version. The error generated was: " + e)