Git Product home page Git Product logo

json1's Introduction

Standard operational transform types

We have a lovely buffet of operational transform types. Each type has many fine features, including thorough testing, browser support and documentation. Each type has its own project in this github organization.

These types have been finely aged in ShareJS's type labs, and now they're ready to enter the world and be used by everyone. We are rather proud of them.

Each type defines a set of standard methods for programatic use. You should be able to define a type using the spec then plug it directly into ShareJS (or other compatible collaborative editing systems) and use it immediately. The type defines how operations and documents are stored and manipulated, while a system like sharejs can decide where the data should be stored on disk, network protocols and all that jazz.

Available OT types

This repository contained three OT types. They were split to separate repositories:

This is the type you should use for normal plain-text editing. It can tranform operation with complexity N against operation with complexity M in O(N+M) time. This makes it much faster than ot-text-tp2 implementation.

This implementation features Transform Property 2 which makes it a good suit for peer-to-peer communication. Unfortunately the extra (unnecessary) complexity kills v8's optimizer and as a result ot-text-tp2 goes about 20x slower than the ot-text type. If you're using client-server library like ShareJS, you don't need TP2 property, so you should use simpler ot-text implementation,

This implementation is capable of transforming not only text but also JSON structures. It supports arbitrary inserts, deletes, and reparenting through the tree of a JSON object.

This is an OT implementation for collaboratively editing rich text documents. It was designed alongside QuillJS for editing those documents.

Javascript Spec

Each OT type exposes a single object with the following properties. Note that only name, create, apply and transform are strictly required, though most types should also include url and compose.

There is a simple example of a working type in example.js. For a more thorough example, take a look at the text type.

If you're publishing your library in npm (and you should!), the module should expose an object with a .type property (containing your type). So for example, require('ot-text').type.name contains the text type's name.

Standard properties

  • name: A user-readable name for the type. This is not guaranteed to be unique.
  • uri: (Optional, will be required soon) A canonical location for this type. The spec for the OT type should be at this address. Remember kids, Tim Berners-Lee says cool URLs don't change.
  • create([initialData]) -> snapshot: A function to create the initial document snapshot. Create may accept initial snapshot data as its only argument. Either the return value must be a valid target for JSON.stringify or you must specify serialize and deserialize functions (described below).
  • apply(snapshot, op) -> snapshot': Apply an operation to a document snapshot. Returns the changed snapshot. For performance, old document must not be used after this function call, so apply may reuse and return the current snapshot object.
  • transform(op1, op2, side) -> op1': Transform op1 by op2. Return the new op1. Side is either 'left' or 'right'. It exists to break ties, for example if two operations insert at the same position in a string. Both op1 and op2 must not be modified by transform. Transform must conform to Transform Property 1. That is, apply(apply(snapshot, op1), transform(op2, op1, 'left')) == apply(apply(snapshot, op2), transform(op1, op2, 'right')).
  • compose(op1, op2) -> op: (optional) Compose op1 and op2 to produce a new operation. The new operation must subsume the behaviour of op1 and op2. Specifically, apply(apply(snapshot, op1), op2) == apply(snapshot, compose(op1, op2)). Note: transforming by a composed operation is NOT guaranteed to produce the same result as transforming by each operation in order. This function is optional, but unless you have a good reason to do otherwise, you should provide a compose function for your type.

Optional properties

  • invertWithDoc(op, doc) -> op': (optional, RECOMMENDED) Invert the given operation using context from the document to which it can be applied. This method should generally be added to every type as a way to create undo operations. Formally given apply(snapshot, op) is valid, this creates an operation such that apply(apply(snapshot, op), invert(op, snapshot)) == snapshot. If invert(op) exists, invertWithDoc(op, _) == invert(op). 💣 NOTE: The document passed to invertWithDoc should be the document state before the operation is applied. Not after the operation has been applied.
  • invert(op) -> op': (optional) Invert the given operation. The original operation must not be edited in the process. If supplied, apply(apply(snapshot, op), invert(op)) == snapshot.
  • normalize(op) -> op': (optional) Normalize an operation, converting it to a canonical representation. normalize(normalize(op)) == normalize(op).
  • transformCursor(cursor, op, isOwnOp) -> cursor': (optional) transform the specified cursor by the provided operation, so that the cursor moves forward or backward as content is added or removed before (or at) the cursor position. isOwnOp defines how the cursor should be transformed against content inserted at the cursor position. If isOwnOp is true, the cursor is moved after the content inserted at the original cursor position. If isOwnOp is false, the cursor remains before the content inserted at the original cursor position.
  • serialize(snapshot) -> data: (optional) convert the document snapshot data into a form that may be passed to JSON.stringify. If you have a serialize function, you must have a deserialize function.
  • deserialize(data) -> snapshot: (optional) convert data generated by serialize back into its internal snapshot format. deserialize(serialize(snapshot)) == snapshot. If you have a deserialize function, you must have a serialize function.

Do I need serialize and deserialize? Maybe JSON.stringify is sufficiently customizable..?

TP2 Properties

If your OT type supports transform property 2, set the tp2 property to true and define a prune function.

Transform property 2 is an additional requirement on your transform function. Specifically, transform(op3, compose(op1, transform(op2, op1)) == transform(op3, compose(op2, transform(op1, op2)).

  • tp2: (optional) Boolean property. Make this truthy to declare that the type has tp2 support. Types with TP2 support must define prune.
  • prune(op, otherOp): The inverse of transform. Formally, apply(snapshot, op1) == apply(snapshot, prune(transform(op1, op2), op2)). Usually, prune will simply be the inverse of transform and prune(transform(op1, op2), op2) == op1.

CRDTs

Technically, CRDT types are a subset of OT types. Which is to say, they are OT types that don't need a transform function. As a result, anything that can handle these OT types should also be able to consume CRDTs. But I haven't tested it. If anyone wants to work with me to add CRDT support here, email me.


License

All code contributed to this repository is licensed under the standard MIT license:

Copyright 2011 ottypes library contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following condition:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

json1's People

Contributors

antoinelyset avatar colinthompson1 avatar curran avatar dependabot[bot] avatar josephg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json1's Issues

The problem

What is the difference between OT-json0 and OT-json1

Hope to increase the resolution of conflicts that replaceOp at the same time

doc

{
  startTime: 1
}

when A and B both submit op with replaceOp(['startTime'], 2) and replaceOp(['startTime'], 3) will conflict and throw error.

but can we add extra infomation to resolve this problem ? example we add a 'timestamp' or random number

op object

['startTime', { r: 2, weight: Date.now() }]
['startTime', { r: 3, weight: Date.now() }]

This can keeps the two operations consistent。we only simple compare weight to compose two operation.

"invalid JSON key" exception on makeInvertible

Context:
I encode colors as strings in json. To ensure, they are always consisted 6 character colors, I decided to use {r: oldColor} + {i: newColor} instead of {es: newColor}. (To my understanding from reading the documentation, two simultaneous {es: newColor} operations could lead to a longer string e.g. FF0000444444, correct?)

Call:
json1.makeInvertible(["color", {r: true}, {i: "FF0000"}], {color: "00FF00"}])

Throws:
Error: Invalid JSON key

Expected result:
["abc", {r: "00FF00"}, {i: "FF0000"}], {color: "00FF00"}]

Question about invert function

I saw this TODO in source code.

// My TODO list:
// - compose preserve invertable data
// - makeInvertable and invert()

is there any plan to implement the invert() method? Because I really need this to implement undo and redo functions in my editor.

Thanks for your great work.

How to implement?

I am working on adding collaboration to SlateJS.

It represents edits as a series of batched operations.

https://docs.slatejs.org/concepts/05-operations

What would be an efficient way to use json1 to add collab editing capability?

https://github.com/ianstormtaylor/slate/blob/master/packages/slate/src/interfaces/operation.ts

Do I have to manually map each operation to a json1 equivalent? Slate provides a document structure in the form of JSON but the ops are done through the editor, do I have to mirror the document structure in json1?

[Query] json0 vs json1

As a newbie to ottypes + sharedb, which library should be used json0 or json1(I have been working with json1)?

Apply should throw when path has out of bounds index (was: Composition and inserts in skipped position)

Hi,

Again thanks for this great library.
While testing the library with composition and inserts, I stumbled on this behavior.

Is this intended?

json1.type.apply(
  [0],
  json1.type.compose(
    json1.insertOp([2], { key: "value" }),
    json1.type.compose(
      json1.insertOp([2, "anotherKey"], "anotherValue"),
      json1.insertOp([2, "lastKey"], "lastValue")
    )
  )
);
=> 
[
  0,
  { key: 'value' },
  { key: 'value', anotherKey: 'anotherValue', lastKey: 'lastValue' }
]

Having { key: 'value' } at position 1 is creating a lot of confusion.

isNoop behaviour - should we strip embedded noops during processing?

Based on the discussion in this ShareDB issue it looks like it'd be useful to have an isNoop method in OT types.

JSON1 uses null as its noop sentinal value, which is fine and easy, but subtypes can also generate noops from transform and compose.

So:

  • isNoop(null) returns true
  • isNoop([{r:true}]) returns false

But what about:

  • isNoop([{es:[]}])? [] is a no-op in the text-unicode type, so this operation has no effect. Should we detect this sort of thing in isNoop?
  • normalize([{es:[]}])? Should this return [{es:[]}] or null?
  • compose([{es:['hi']}], [{es:[{d:'hi'}]}])? These two embedded operations cancel each other out. Should we do the naive thing and return [{es:[]}] or flatten the result out to null?
  • ... and the same in transform.

As I see it there's ways we could play this:

  1. isNoop must scan the operation recursively, looking for an operation component which does something.
  2. Embedded noops generated by subtypes are considered invalid. They get stripped in all methods that interact with operations. (So normalize([{es:[]}]) and all the other similar examples above return null)
  3. We ignore embedded noops, and a JSON operation which only has an embedded noop is not considered a noop in turn. This is a little weird.

Its a bit obscure no matter which way we play this, and I can't think of any serious consequences of preserving embedded noops or not. There's a little bit of runtime type checking which comes from applying an embedded edit (even if its null); but I don't think it matters much. I can't think of any other way this will affect the output of transform, compose or apply.

I'm leaning toward (2) and stripping any embedded noops we can detect in all cases.

Interest

I've been following this project since last year and am really excited about the possibilities this brings to general purpose OT.

It seems that there hasn't been much activity on this and I was wondering if you are still working on it in private or if you have lost interest and moved on to other things?

Compose does not preserve invertable operations

Bad case:

const json1 = require('ot-json1')

const doc = [{
    text: '-'
}]

const op1 = json1.editOp([0, 'text'], 'text-unicode', [{ d: '-' }])
const op2 = json1.removeOp([0], { text: '' })

const op = json1.type.compose(op1, op2)

const result = json1.type.apply(doc, op)

console.log(result) // []

const invertedOp = json1.type.invert(op)

const result2 = json1.type.apply(result, invertedOp)

console.log(result2) // [ { text: '' } ]

Expect result2 equal to doc ? But they are not equal.

transformPosition edge case ?

Hi Joseph!

As always, thanks for this library. I'm having a bit of trouble knowing if I encountered a bug or I just use the library badly. What do you think about this example?

const doc = {
  object: 'one',
  nodes: [
    {
      object: 'two',
      nodes: [
        {
          object: 'three',
          nodes: [
            {
              object: 'four',
              nodes: [{ object: 'five' }],
            },
          ],
        },
      ],
    },
  ],
}

const op = [
  'nodes',
  0,
  { r: true, d: 0 },
  'nodes',
  [0, { p: 0, i: { object: 'six' } }],
  [2, { i: { object: 'seven' } }],
]

const appliedDoc = {
  "object": "one",
  "nodes": [
    {
      "object": "three",
      "nodes": [
        {
          "object": "six"
        },
        {
          "object": "four",
          "nodes": [
            {
              "object": "five"
            }
          ]
        },
        {
          "object": "seven"
        }
      ]
    }
  ]
}

const path = ['nodes', 0, 'nodes' ,0, 'nodes', 0, 'nodes', 0]

json1.type.transformPosition(path, op)
// => [ 'nodes', 0, 'nodes', 0, 'nodes', 1 ] which seems buggy

Thanks for your time.

Browser Build

It would be useful to have a UMD browser build for this library.

It might be as simple as adding a flag to tsc here https://github.com/ottypes/json1/blob/master/Makefile#L20

Perhaps tsc --module=umd? Though I'm not sure how to specify the name of the browser global introduced.

FWIW I gave the ShareDB Client a browser build treatment that looks like this: https://github.com/vizhub-core/sharedb-client-browser#readme

One option is to do something similar for JSON1, or even bundle JSON1 into that build, but it would be neat if JSON1 had its own browser build "out of the box".

Thanks!

Computing a Diff

Greetings,

I'm evaluating feasibility of migrating from JSON0 to JSON1 for VizHub, a browser based collaborative code editor. One feature of the JSON0 ecosystem that I have been relying on heavily is computing a diff. I have been using kbadk/json0-ot-diff for this.

I'm opening this issue to ask, has anyone using JSON1 had any success trying to compute diffs between documents and generating JSON1 ops as output?

To be clear, I'm not proposing to extend this JSON1 codebase to compute diffs, that can live externally to this library. Just probing the space to see if anyone else is doing this. I may try to modify json0-ot-diff to work with JSON1, but wanted to first see if anyone else has thought about this.

Related issue json0-ot-diff: JSON1 OT Diff.

Thanks!

makeInvertable and invert()

I'm wondering, is the type invertable?

I see the following in

// - makeInvertable and invert()

// - makeInvertable and invert()

What would be some first steps to implementing/verifying this? I suppose we could start with some failing tests.

Also the related to the ideas from the spec:

(quoting from there)

The JSON1 OT type is designed to be optionally invertible. There are no constraints on the content of r: components, so the contents of the removed elements can be placed there.

To implement this, we need a (unwritten) function which takes an operation and a document snapshot and augments all r:X components with the content to be removed.

Then an invert function can be written which:

  • Swaps i and r components
  • Swaps p and d components
  • Move all edits to the item's source location
  • Invert all embedded edits using the embedded type

In the current implementation the extra information in r:X components will be lost if the operation is transformed.

Typo in readme

const op = [ json1.insertOp([], {title: '', contents: '', public: false}), json1.editOp(['title'], 'text-unicode', ['My cool blog entry']), json1.replaceOp(['public', false, true]) ].reduce(json1.type.compose, null)

Should be
const op = [ json1.insertOp([], {title: '', contents: '', public: false}), json1.editOp(['title'], 'text-unicode', ['My cool blog entry']), json1.replaceOp(['public'], false, true) ].reduce(json1.type.compose, null)

Question: how to work with data types that has a schema?

Hey! First of all, thanks for creating this awesome library!

I have a question about using this library for JSON data types that has to comply with a schema: on the server side, the server can reject changes that may break the schema, but what should the client do?

For example, if the schema imposes that an array can have at most 1 element. Both Alice and Bob now simultaneously insert an element to the array without knowing each other's change. The server may decide to reject Bob's change since Alice's request is handled first. On the client side, how should Bob handle server-sent update containing Alice's operation (when applied, will break the array length limit)? And if server rejected Bob's operation, should it send any operation regarding this rejection to Bob?

Verify immutability with Object.freeze

This issue is about improving the approach taken in the tests for checking immutability.

A quote from the original discussion of this idea in #8:

A better way to verify immutability would be to recursively call Object.freeze on all inputs to all the functions in all tests. The nice thing about that approach is that if any tests inappropriately modify data, the stack trace will point to the exact source of the error. If we do that, we probably shouldn't need special immutability test cases at all. --@josephg

`invert` and ShareDB

It seems that JSON1 and ShareDB will not play well together see here.

I don't know if we should rename invert to something else (which is a big breaking change) but I think I've reached a dead end.

What do you think we should do?

Some clarifications re: spec

Hi @josephg and welcome back :-)

I took a quick read-through of the spec and think the pick-up / put-down concept is really novel and clever. Here are some questions and thoughts:

Pick-up and put-down across ops
With regards to the pick-up 'slots' can you clarify whether a valid op may leave a sub-tree in suspense in the slot storage? In other words, can I have a pick-up operation to a named / numbered register that does not have a corresponding put-down operation in the same op?

It seems to me that allowing this sort of behaviour would just be asking for crazy corner-cases and challenges down the road. An alternative would be to encourage consumers of the api to create their own 'storage areas' within the json tree for disassociated trees if and when their app logic requires it.

So, assuming the behaviour is disallowed, that means some stronger validation can be created for ops and you avoid the issue of potential conflicts on 'pick-up' operations to the same slot.

List splicing vs overwriting
In your example of moving something between two items in a list, the item is inserted. Are the semantics to splice the item in or to overwrite the item at that position? It seems to me (perhaps naively) that in the former case, overwriting items will be non-trivial (or the pick-up/delete operation in a list context needs to be specialized to splice vs set to undefined or delete) while in the latter case, inserting items into the middle of a list will be non-trivial. What are your thoughts?

Security: Disallow __proto__ (and others?)

Currently the library allows any key path to be used to make and apply operations. If the operation is created by a user (which it usually will be), we should disallow obviously unsafe object keys from being assigned to.

The canonical example is assigning to obj.__proto__.

I propose we make __proto__ an invalid key for operations to contain. Are there others we should explicitly disallow?

Python conversion

There are several Python translations of the delta library from Quilljs, but there's no Python translation of an OT library to work on JSON objects.

Before I try reinventing the wheel, are you aware of any Python port of JSON1? Is that where you would recommend getting started?

Suggestion - use public ledger as content server

Hi Joseph.
What do you think about using Ethereum public ledger as the content server? Ethereum is a decentralised p2p public ledger - similar to Bitcoin, but one that allows to include turing complete scripts in transactions and thus allows sophisticated built-in support for smart contracts.
In this way - creating a new document will be similar to creating a new contract. And adding new content would be similar to sending transaction to this contract. Since the transactions are stored in public ledger - everyone can access them and thus read the document content.
I think this suggestion solves some of the issues:

  1. Monetisation - whoever wants to create a new document and/or add content pays for it in ether - and this mechanism is supported by Ethereum.
  2. User addresses - will be standard ethereum addresses.
    Also you don't have to host the content - it is hosted on ethereum blockchain, and paid by users, you just provide a front-end that can read the info from the ledger.
    Disclaimer: I am not familiar very deep with Ethereum technology - just read about it and tried the tutorial, so maybe it's harder that I think.

Is it possible to provide a base op for object value copying

The background is this, in the process of using slate, there is a basic operation called split_node,which is:

{
   type: 'split_node',
   position: 4,
   path: [0, 0],
   properties: { bold: true },
}

and original document like this:

[
  {
     type: 'paragraph',
     children: [{ text: 'some text!' }]
  }
]

The final expected result after the operation is as follows:

[
  {
     type: 'paragraph',
     children: [
         { text: 'some' }
         { text: ' text!' }
     ]
  }
]

In order to use ot-json1, I transform the origin path to [0, 'children', 0, 'text'].

My idea is to use editOp to truncate the previous result and use insertOp to insert the latter result, but I encountered a problem that there is no field in the original op that represents the entire string.

So my idea is to first copy the previous result to the position of the second result, and then use string truncation to achieve the final effect.

So I want to ask if it is possible to provide a basic copyOp(formPath, toPath) to achieve this effect? Or is there any way I haven't thought of, I am very willing to learn!

Theoretical Foundations for Contributions

As per my understanding ShareJS is a full collaboration system, complete with Transformation Functions and the Networking algorithm.

ottypes like json0 and json1 are the transformation function themselves extracted out of ShareJS?

So these are basically algorithms? I'd really like my team & I to contribute to these projects. We are literate is JS / TS / CofeeScript / ES6 etc. but in order to contribute to an algorithmic project such as this what can be some reference material?

Are the algorithms being used here out of one or more specific research papers?

Are there general OT algorithms in the wild? Other implementations / Academic study / research papers?

Are you creating your own algorithm here? If so, can you specify some mathematical / theoretical foundation?

It would be great if you could revert for json1 and ottypes/json0

Decaffeinate Tests

I noticed the following quote in #4 (comment), and thought I'd make an issue for it:

(And I'd like to port the tests away from coffeescript too!)

This issue can be closed when all the tests are ported to JavaScript.

I am thrilled to see this project at a steady slow burn. Really appreciate your long-term efforts on this!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.