Comparing version 0.1.16 to 0.1.17
var peer = Math.random().toString(36).substr(2) | ||
// *************************** | ||
@@ -39,2 +38,4 @@ // http | ||
options = options || {} | ||
// Now we know where the `options` are specified, let's set headers. | ||
@@ -46,3 +47,3 @@ if (!options.headers) | ||
if (options.subscribe) | ||
options.headers.subscribe = 'keep-alive' | ||
options.headers.subscribe = 'true' | ||
@@ -158,6 +159,3 @@ // Always add the `peer` header | ||
if (params.subscribe) | ||
params.headers.set('subscribe', | ||
(typeof params.subscribe === 'number' | ||
? 'keep-alive=' + params.subscribe | ||
: 'keep-alive')) | ||
params.headers.set('subscribe', 'true') | ||
@@ -632,2 +630,1 @@ // Prepare patches | ||
} | ||
@@ -5,2 +5,8 @@ var assert = require('assert') | ||
function generate_patches(res, patches) { | ||
for (let patch of patches) { | ||
assert(typeof patch.unit === 'string') | ||
assert(typeof patch.range === 'string') | ||
assert(typeof patch.content === 'string') | ||
} | ||
// This will return something like: | ||
@@ -18,7 +24,7 @@ // Patches: n | ||
for (let patch of patches) | ||
result += ` | ||
content-length: ${patch.content.length} | ||
content-range: ${patch.unit} ${patch.range} | ||
${patch.content} | ||
result += `\r | ||
content-length: ${patch.content.length}\r | ||
content-range: ${patch.unit} ${patch.range}\r | ||
\r | ||
${patch.content}\r | ||
` | ||
@@ -123,14 +129,6 @@ return result | ||
// Parse the subscribe header as one of these forms: | ||
// | ||
// keep-alive | ||
// keep-alive=number | ||
// | ||
// Parse the subscribe header | ||
var subscribe = req.headers.subscribe | ||
if (subscribe) { | ||
let match = req.headers.subscribe.match(/keep-alive(=(\w+))?/) | ||
if (match) | ||
subscribe = | ||
match[2] ? {keep_alive: parseInt(match[2])} : {keep_alive: true} | ||
} | ||
if (subscribe === 'true') | ||
subscribe = true | ||
@@ -143,3 +141,3 @@ // Define convenience variables | ||
// Add the braidly request/response helper methods | ||
res.sendVersion = (stuff) => send_version(res, stuff) | ||
res.sendVersion = (stuff) => send_version(res, stuff, req.url, peer) | ||
req.patches = () => new Promise( | ||
@@ -193,3 +191,3 @@ (done, err) => parse_patches(req, (patches) => done(patches)) | ||
function send_version(res, data) { | ||
function send_version(res, data, url, peer) { | ||
var {version, parents, patches, body} = data | ||
@@ -210,3 +208,3 @@ | ||
console.log('sending version', {version, parents, patches, body, | ||
console.log('sending version', {url, peer, version, parents, patches, body, | ||
subscription: res.isSubscription}) | ||
@@ -213,0 +211,0 @@ |
{ | ||
"name": "braidify", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"description": "Synchronization for the Web (reference implementation)", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -5,33 +5,24 @@ # Braidify | ||
- The Braid protocol is a simple extension to HTTP. | ||
- The Braidify library is a simple extension to Javascript HTTP libraries. | ||
- [npm package](https://www.npmjs.com/package/braidify) now available for testing | ||
- Edit the source via the [braidjs](https://github.com/braid-org/braidjs) monorepo | ||
- Reference implementation for [Braid-HTTP 03](https://github.com/braid-org/braid-spec/blob/master/draft-toomim-httpbis-braid-http-03.txt) specification | ||
Implements the | ||
[Braid-HTTP 03](https://github.com/braid-org/braid-spec/blob/master/draft-toomim-httpbis-braid-http-03.txt) | ||
specification. | ||
### Purpose | ||
Whereas [Braid](https://braid.org) is *"a few simple extensions to HTTP that | ||
add synchronization"*; the `braidify` library is *"a few simple extensions to | ||
HTTP libraries that add Braid synchronization"*. | ||
## Use it in Browsers | ||
Braidify currently supports Braid in the following libraries: | ||
Braidify will polyfill your browser's existing `fetch()` function. | ||
Install it with a script tag: | ||
```html | ||
<script src="braidify-client.js"></script> | ||
``` | ||
Or an import statement: | ||
```javascript | ||
import {fetch} from 'braidify-client.js' | ||
require('braidify').fetch // Browser fetch() API and require('node-fetch') | ||
require('braidify').http // Nodejs require('http') and require('https') | ||
``` | ||
You can then get the full experience by (optionally) replacing your browser's | ||
`fetch()` with the polyfill: | ||
We would love to support your favorite library, too. | ||
```html | ||
<script> fetch = braidify.fetch </script> | ||
``` | ||
Let's see how to use it: | ||
### Browser `fetch()` | ||
@@ -41,17 +32,15 @@ ```html | ||
<script> | ||
fetch = braidify.fetch // Optional | ||
fetch( | ||
'https://braid.org/chat', | ||
{subscribe: {keep_alive: true}}, | ||
).andThen(version => { | ||
console.log('We got a new version!', version) | ||
// { | ||
// version: "me", | ||
// parents: ["mom", "dad"], | ||
// patches: [{unit: "json", range: ".foo", content: "3"}] | ||
// body: "3" | ||
// } | ||
// // Version will contain either patches *or* body | ||
}) | ||
fetch( | ||
'https://braid.org/chat', | ||
{subscribe: {keep_alive: true}}, | ||
).andThen(version => { | ||
console.log('We got a new version!', version) | ||
// { | ||
// version: "me", | ||
// parents: ["mom", "dad"], | ||
// patches: [{unit: "json", range: ".foo", content: "3"}] | ||
// body: "3" | ||
// } | ||
// // Version will contain either patches *or* body | ||
}) | ||
</script> | ||
@@ -106,25 +95,5 @@ ``` | ||
## Use it in Node | ||
## Nodejs client with `fetch()` | ||
In node, first install braidify: | ||
```shell | ||
npm install braidify | ||
``` | ||
Then you can | ||
```javascript | ||
require('braidify').fetch // Browser fetch() API and require('node-fetch') | ||
require('braidify').http // Nodejs require('http') and require('https') | ||
``` | ||
We would love to support your favorite library, too. Make requests on | ||
[Github](https://github.com/braid-org/braidjs/issues). | ||
### Nodejs client with `fetch()` | ||
```javascript | ||
var fetch = require('braidify').fetch | ||
@@ -149,3 +118,3 @@ // or: | ||
### Nodejs client with `require('http')` | ||
## Nodejs client with `require('http')` | ||
@@ -199,3 +168,3 @@ ```javascript | ||
### Nodejs server using `require('express')` | ||
## Nodejs server using `require('express')` | ||
@@ -245,3 +214,3 @@ On the server using express: | ||
### Nodejs server with `require('http')` | ||
## Nodejs server with `require('http')` | ||
@@ -248,0 +217,0 @@ On the server using regular `require('http')`: |
36632
726
238