Deploy 12.1.0
This commit is contained in:
parent
cf3969dff6
commit
7744993dde
20
README.md
20
README.md
@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
# 
|
# 
|
||||||
|
|
||||||
Yjs is a framework for p2p shared editing on structured data like (rich-)text, json, and XML.
|
Yjs is a framework for offline-first p2p shared editing on structured data like text, richtext, json, or XML.
|
||||||
It is similar to [ShareJs] and [OpenCoweb], but easy to use.
|
It is fairly easy to get started, as Yjs hides most of the complexity of concurrent editing.
|
||||||
For additional information, demos, and tutorials visit [y-js.org](http://y-js.org/).
|
For additional information, demos, and tutorials visit [y-js.org](http://y-js.org/).
|
||||||
|
|
||||||
### Extensions
|
### Extensions
|
||||||
@ -91,17 +91,17 @@ bower i yjs y-memory y-webrtc y-array y-text
|
|||||||
```
|
```
|
||||||
|
|
||||||
Here is a simple example of a shared textarea
|
Here is a simple example of a shared textarea
|
||||||
```
|
```HTML
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<script src="./bower_components/yjs/y.js"></script>
|
<script src="./bower_components/yjs/y.js"></script>
|
||||||
|
<!-- Yjs automatically includes all missing dependencies (browser only) -->
|
||||||
<script>
|
<script>
|
||||||
Y({
|
Y({
|
||||||
db: {
|
db: {
|
||||||
name: 'memory' // use memory database adapter.
|
name: 'memory' // use memory database adapter.
|
||||||
// name: 'indexeddb'
|
// name: 'indexeddb' // use indexeddb database adapter instead for offline apps
|
||||||
// name: 'leveldb'
|
|
||||||
},
|
},
|
||||||
connector: {
|
connector: {
|
||||||
name: 'webrtc', // use webrtc connector
|
name: 'webrtc', // use webrtc connector
|
||||||
@ -117,9 +117,9 @@ Here is a simple example of a shared textarea
|
|||||||
// The Yjs instance `y` is available
|
// The Yjs instance `y` is available
|
||||||
// y.share.* contains the shared types
|
// y.share.* contains the shared types
|
||||||
|
|
||||||
// Bind the textarea to y.share.textarea
|
// Bind `y.share.textarea` to `<textarea/>`
|
||||||
y.share.textarea.bind(document.querySelector('textarea'))
|
y.share.textarea.bind(document.querySelector('textarea'))
|
||||||
}
|
})
|
||||||
</script>
|
</script>
|
||||||
<textarea></textarea>
|
<textarea></textarea>
|
||||||
</body>
|
</body>
|
||||||
@ -134,6 +134,9 @@ Report _any_ issues to the [Github issue page](https://github.com/y-js/yjs/issue
|
|||||||
# API
|
# API
|
||||||
|
|
||||||
### Y(options)
|
### Y(options)
|
||||||
|
* Y.extend(module1, module2, ..)
|
||||||
|
* Add extensions to Y
|
||||||
|
* `Y.extend(require('y-webrtc'))` has the same semantics as `require('y-webrtc')(Y)`
|
||||||
* options.db
|
* options.db
|
||||||
* Will be forwarded to the database adapter. Specify the database adaper on `options.db.name`.
|
* Will be forwarded to the database adapter. Specify the database adaper on `options.db.name`.
|
||||||
* Have a look at the used database adapter repository to see all available options.
|
* Have a look at the used database adapter repository to see all available options.
|
||||||
@ -259,6 +262,3 @@ Yjs is licensed under the [MIT License](./LICENSE).
|
|||||||
|
|
||||||
<yjs@dbis.rwth-aachen.de>
|
<yjs@dbis.rwth-aachen.de>
|
||||||
|
|
||||||
[ShareJs]: https://github.com/share/ShareJS
|
|
||||||
[OpenCoweb]: https://github.com/opencoweb/coweb/wiki
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "yjs",
|
"name": "yjs",
|
||||||
"version": "12.0.3",
|
"version": "12.1.0",
|
||||||
"homepage": "y-js.org",
|
"homepage": "y-js.org",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Kevin Jahns <kevin.jahns@rwth-aachen.de>"
|
"Kevin Jahns <kevin.jahns@rwth-aachen.de>"
|
||||||
|
96
y.es6
96
y.es6
@ -2,6 +2,9 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
function canRead (auth) { return auth === 'read' || auth === 'write' }
|
||||||
|
function canWrite (auth) { return auth === 'write' }
|
||||||
|
|
||||||
module.exports = function (Y/* :any */) {
|
module.exports = function (Y/* :any */) {
|
||||||
class AbstractConnector {
|
class AbstractConnector {
|
||||||
/* ::
|
/* ::
|
||||||
@ -55,6 +58,8 @@ module.exports = function (Y/* :any */) {
|
|||||||
this.syncStep2 = Promise.resolve()
|
this.syncStep2 = Promise.resolve()
|
||||||
this.broadcastOpBuffer = []
|
this.broadcastOpBuffer = []
|
||||||
this.protocolVersion = 11
|
this.protocolVersion = 11
|
||||||
|
this.authInfo = opts.auth || null
|
||||||
|
this.checkAuth = opts.checkAuth || function () { return Promise.resolve('write') } // default is everyone has write access
|
||||||
}
|
}
|
||||||
reconnect () {
|
reconnect () {
|
||||||
}
|
}
|
||||||
@ -88,6 +93,9 @@ module.exports = function (Y/* :any */) {
|
|||||||
onUserEvent (f) {
|
onUserEvent (f) {
|
||||||
this.userEventListeners.push(f)
|
this.userEventListeners.push(f)
|
||||||
}
|
}
|
||||||
|
removeUserEventListener (f) {
|
||||||
|
this.userEventListeners = this.userEventListeners.filter(g => { f !== g })
|
||||||
|
}
|
||||||
userLeft (user) {
|
userLeft (user) {
|
||||||
if (this.connections[user] != null) {
|
if (this.connections[user] != null) {
|
||||||
delete this.connections[user]
|
delete this.connections[user]
|
||||||
@ -164,7 +172,8 @@ module.exports = function (Y/* :any */) {
|
|||||||
type: 'sync step 1',
|
type: 'sync step 1',
|
||||||
stateSet: stateSet,
|
stateSet: stateSet,
|
||||||
deleteSet: deleteSet,
|
deleteSet: deleteSet,
|
||||||
protocolVersion: conn.protocolVersion
|
protocolVersion: conn.protocolVersion,
|
||||||
|
auth: conn.authInfo
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@ -218,7 +227,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
*/
|
*/
|
||||||
receiveMessage (sender/* :UserId */, message/* :Message */) {
|
receiveMessage (sender/* :UserId */, message/* :Message */) {
|
||||||
if (sender === this.userId) {
|
if (sender === this.userId) {
|
||||||
return
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
if (this.debug) {
|
if (this.debug) {
|
||||||
console.log(`receive ${sender} -> ${this.userId}: ${message.type}`, JSON.parse(JSON.stringify(message))) // eslint-disable-line
|
console.log(`receive ${sender} -> ${this.userId}: ${message.type}`, JSON.parse(JSON.stringify(message))) // eslint-disable-line
|
||||||
@ -233,14 +242,36 @@ module.exports = function (Y/* :any */) {
|
|||||||
type: 'sync stop',
|
type: 'sync stop',
|
||||||
protocolVersion: this.protocolVersion
|
protocolVersion: this.protocolVersion
|
||||||
})
|
})
|
||||||
return
|
return Promise.reject('Incompatible protocol version')
|
||||||
}
|
}
|
||||||
if (message.type === 'sync step 1') {
|
if (message.auth != null && this.connections[sender] != null) {
|
||||||
|
// authenticate using auth in message
|
||||||
|
var auth = this.checkAuth(message.auth, this.y)
|
||||||
|
this.connections[sender].auth = auth
|
||||||
|
auth.then(auth => {
|
||||||
|
for (var f of this.userEventListeners) {
|
||||||
|
f({
|
||||||
|
action: 'userAuthenticated',
|
||||||
|
user: sender,
|
||||||
|
auth: auth
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else if (this.connections[sender] != null && this.connections[sender].auth == null) {
|
||||||
|
// authenticate without otherwise
|
||||||
|
this.connections[sender].auth = this.checkAuth(null, this.y)
|
||||||
|
}
|
||||||
|
if (this.connections[sender] != null && this.connections[sender].auth != null) {
|
||||||
|
return this.connections[sender].auth.then((auth) => {
|
||||||
|
if (message.type === 'sync step 1' && canRead(auth)) {
|
||||||
let conn = this
|
let conn = this
|
||||||
let m = message
|
let m = message
|
||||||
|
|
||||||
this.y.db.requestTransaction(function *() {
|
this.y.db.requestTransaction(function *() {
|
||||||
var currentStateSet = yield* this.getStateSet()
|
var currentStateSet = yield* this.getStateSet()
|
||||||
|
if (canWrite(auth)) {
|
||||||
yield* this.applyDeleteSet(m.deleteSet)
|
yield* this.applyDeleteSet(m.deleteSet)
|
||||||
|
}
|
||||||
|
|
||||||
var ds = yield* this.getDeleteSet()
|
var ds = yield* this.getDeleteSet()
|
||||||
var ops = yield* this.getOperations(m.stateSet)
|
var ops = yield* this.getOperations(m.stateSet)
|
||||||
@ -249,7 +280,8 @@ module.exports = function (Y/* :any */) {
|
|||||||
os: ops,
|
os: ops,
|
||||||
stateSet: currentStateSet,
|
stateSet: currentStateSet,
|
||||||
deleteSet: ds,
|
deleteSet: ds,
|
||||||
protocolVersion: this.protocolVersion
|
protocolVersion: this.protocolVersion,
|
||||||
|
auth: this.authInfo
|
||||||
})
|
})
|
||||||
if (this.forwardToSyncingClients) {
|
if (this.forwardToSyncingClients) {
|
||||||
conn.syncingClients.push(sender)
|
conn.syncingClients.push(sender)
|
||||||
@ -268,7 +300,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
}
|
}
|
||||||
conn._setSyncedWith(sender)
|
conn._setSyncedWith(sender)
|
||||||
})
|
})
|
||||||
} else if (message.type === 'sync step 2') {
|
} else if (message.type === 'sync step 2' && canWrite(auth)) {
|
||||||
let conn = this
|
let conn = this
|
||||||
var broadcastHB = !this.broadcastedHB
|
var broadcastHB = !this.broadcastedHB
|
||||||
this.broadcastedHB = true
|
this.broadcastedHB = true
|
||||||
@ -303,7 +335,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
this.syncStep2.then(function () {
|
this.syncStep2.then(function () {
|
||||||
self._setSyncedWith(sender)
|
self._setSyncedWith(sender)
|
||||||
})
|
})
|
||||||
} else if (message.type === 'update') {
|
} else if (message.type === 'update' && canWrite(auth)) {
|
||||||
if (this.forwardToSyncingClients) {
|
if (this.forwardToSyncingClients) {
|
||||||
for (var client of this.syncingClients) {
|
for (var client of this.syncingClients) {
|
||||||
this.send(client, message)
|
this.send(client, message)
|
||||||
@ -319,6 +351,10 @@ module.exports = function (Y/* :any */) {
|
|||||||
}
|
}
|
||||||
this.y.db.apply(message.ops)
|
this.y.db.apply(message.ops)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return Promise.reject('Unable to deliver message')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_setSyncedWith (user) {
|
_setSyncedWith (user) {
|
||||||
var conn = this.connections[user]
|
var conn = this.connections[user]
|
||||||
@ -451,11 +487,21 @@ module.exports = function (Y) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
whenTransactionsFinished: function () {
|
whenTransactionsFinished: function () {
|
||||||
|
var self = this
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
// The connector first has to send the messages to the db.
|
||||||
|
// Wait for the checkAuth-function to resolve
|
||||||
|
// The test lib only has a simple checkAuth function: `() => Promise.resolve()`
|
||||||
|
// Just add a function to the event-queue, in order to wait for the event.
|
||||||
|
// TODO: this may be buggy in test applications (but it isn't be for real-life apps)
|
||||||
|
setTimeout(function () {
|
||||||
var ps = []
|
var ps = []
|
||||||
for (var name in this.users) {
|
for (var name in self.users) {
|
||||||
ps.push(this.users[name].y.db.whenTransactionsFinished())
|
ps.push(self.users[name].y.db.whenTransactionsFinished())
|
||||||
}
|
}
|
||||||
return Promise.all(ps)
|
Promise.all(ps).then(resolve, reject)
|
||||||
|
}, 0)
|
||||||
|
})
|
||||||
},
|
},
|
||||||
flushOne: function flushOne () {
|
flushOne: function flushOne () {
|
||||||
var bufs = []
|
var bufs = []
|
||||||
@ -481,8 +527,9 @@ module.exports = function (Y) {
|
|||||||
delete buff[sender]
|
delete buff[sender]
|
||||||
}
|
}
|
||||||
var user = globalRoom.users[userId]
|
var user = globalRoom.users[userId]
|
||||||
user.receiveMessage(m[0], m[1])
|
return user.receiveMessage(m[0], m[1]).then(function () {
|
||||||
return user.y.db.whenTransactionsFinished()
|
return user.y.db.whenTransactionsFinished()
|
||||||
|
}, function () {})
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -499,8 +546,7 @@ module.exports = function (Y) {
|
|||||||
}
|
}
|
||||||
globalRoom.whenTransactionsFinished().then(nextFlush)
|
globalRoom.whenTransactionsFinished().then(nextFlush)
|
||||||
} else {
|
} else {
|
||||||
setTimeout(function () {
|
c = globalRoom.flushOne()
|
||||||
var c = globalRoom.flushOne()
|
|
||||||
if (c) {
|
if (c) {
|
||||||
c.then(function () {
|
c.then(function () {
|
||||||
globalRoom.whenTransactionsFinished().then(nextFlush)
|
globalRoom.whenTransactionsFinished().then(nextFlush)
|
||||||
@ -508,7 +554,6 @@ module.exports = function (Y) {
|
|||||||
} else {
|
} else {
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
}, 0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
globalRoom.whenTransactionsFinished().then(nextFlush)
|
globalRoom.whenTransactionsFinished().then(nextFlush)
|
||||||
@ -534,7 +579,7 @@ module.exports = function (Y) {
|
|||||||
this.syncingClientDuration = 0
|
this.syncingClientDuration = 0
|
||||||
}
|
}
|
||||||
receiveMessage (sender, m) {
|
receiveMessage (sender, m) {
|
||||||
super.receiveMessage(sender, JSON.parse(JSON.stringify(m)))
|
return super.receiveMessage(sender, JSON.parse(JSON.stringify(m)))
|
||||||
}
|
}
|
||||||
send (userId, message) {
|
send (userId, message) {
|
||||||
var buffer = globalRoom.buffers[userId]
|
var buffer = globalRoom.buffers[userId]
|
||||||
@ -581,7 +626,7 @@ module.exports = function (Y) {
|
|||||||
if (buff[sender].length === 0) {
|
if (buff[sender].length === 0) {
|
||||||
delete buff[sender]
|
delete buff[sender]
|
||||||
}
|
}
|
||||||
this.receiveMessage(m[0], m[1])
|
yield this.receiveMessage(m[0], m[1])
|
||||||
}
|
}
|
||||||
yield self.whenTransactionsFinished()
|
yield self.whenTransactionsFinished()
|
||||||
})
|
})
|
||||||
@ -2696,7 +2741,7 @@ module.exports = function (Y /* : any*/) {
|
|||||||
try {
|
try {
|
||||||
this.eventListeners[i](event)
|
this.eventListeners[i](event)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('User events must not throw Errors!')
|
console.error('Your observer threw an error. This error was caught so that Yjs still can ensure data consistency! In order to debug this error you have to check "Pause On Caught Exceptions"', e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3439,8 +3484,8 @@ Y.extend = function (name, value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Y.requestModules = requestModules
|
Y.requestModules = requestModules
|
||||||
function requestModules (modules, sourceDir) {
|
function requestModules (modules) {
|
||||||
sourceDir = sourceDir || '/bower_components'
|
var sourceDir = Y.sourceDir || '/bower_components'
|
||||||
// determine if this module was compiled for es5 or es6 (y.js vs. y.es6)
|
// determine if this module was compiled for es5 or es6 (y.js vs. y.es6)
|
||||||
// if Insert.execute is a Function, then it isnt a generator..
|
// if Insert.execute is a Function, then it isnt a generator..
|
||||||
// then load the es5(.js) files..
|
// then load the es5(.js) files..
|
||||||
@ -3505,14 +3550,15 @@ type YOptions = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function Y (opts/* :YOptions */) /* :Promise<YConfig> */ {
|
function Y (opts/* :YOptions */) /* :Promise<YConfig> */ {
|
||||||
|
if (opts.sourceDir != null) {
|
||||||
|
Y.sourceDir = opts.sourceDir
|
||||||
|
}
|
||||||
opts.types = opts.types != null ? opts.types : []
|
opts.types = opts.types != null ? opts.types : []
|
||||||
var modules = [opts.db.name, opts.connector.name].concat(opts.types)
|
var modules = [opts.db.name, opts.connector.name].concat(opts.types)
|
||||||
for (var name in opts.share) {
|
for (var name in opts.share) {
|
||||||
modules.push(opts.share[name])
|
modules.push(opts.share[name])
|
||||||
}
|
}
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
setTimeout(function () {
|
|
||||||
Y.requestModules(modules, opts.sourceDir).then(function () {
|
|
||||||
if (opts == null) reject('An options object is expected! ')
|
if (opts == null) reject('An options object is expected! ')
|
||||||
else if (opts.connector == null) reject('You must specify a connector! (missing connector property)')
|
else if (opts.connector == null) reject('You must specify a connector! (missing connector property)')
|
||||||
else if (opts.connector.name == null) reject('You must specify connector name! (missing connector.name property)')
|
else if (opts.connector.name == null) reject('You must specify connector name! (missing connector.name property)')
|
||||||
@ -3520,15 +3566,21 @@ function Y (opts/* :YOptions */) /* :Promise<YConfig> */ {
|
|||||||
else if (opts.connector.name == null) reject('You must specify db name! (missing db.name property)')
|
else if (opts.connector.name == null) reject('You must specify db name! (missing db.name property)')
|
||||||
else if (opts.share == null) reject('You must specify a set of shared types!')
|
else if (opts.share == null) reject('You must specify a set of shared types!')
|
||||||
else {
|
else {
|
||||||
|
opts = Y.utils.copyObject(opts)
|
||||||
|
opts.connector = Y.utils.copyObject(opts.connector)
|
||||||
|
opts.db = Y.utils.copyObject(opts.db)
|
||||||
|
opts.share = Y.utils.copyObject(opts.share)
|
||||||
|
setTimeout(function () {
|
||||||
|
Y.requestModules(modules).then(function () {
|
||||||
var yconfig = new YConfig(opts)
|
var yconfig = new YConfig(opts)
|
||||||
yconfig.db.whenUserIdSet(function () {
|
yconfig.db.whenUserIdSet(function () {
|
||||||
yconfig.init(function () {
|
yconfig.init(function () {
|
||||||
resolve(yconfig)
|
resolve(yconfig)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}).catch(reject)
|
}).catch(reject)
|
||||||
}, 0)
|
}, 0)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user