Code Monkey home page Code Monkey logo

twister-lib-js's Introduction

twister-lib-js

A Twister Library in JavaScript

Scope of this Project

twister-lib-js handles all querying and manipulation of the Twister P2P network, given the availability of a (remote or local) twisterd JSON-RPC endpoint. This includes managing the network resource, by bundleing queries and by caching. This also includes the ability sign posts and to encrypt and decrypt direct messages locally.

twister-lib-js should be compilable for as many platforms as possible including:

  • All popular Browsers (for web apps as well as firefoxOS)
  • node-js (for server-side functionality)
  • The iOS Javascript VM (for building native iOS apps)
  • The Android Javascript VM (for building native android apps)

A techdemo of twister-lib-js combined with react-js can be found at http://github.com/Tschaul/twister-react

Implementation Status

Resource query manipulate (client side wallet) manipulate (server side wallet)
Posts
Replies
Retwists
Profile
Avatar
Followings
Mentions -
Hashtags -
Promoted Posts - -
Direct Messages - -
Group Messages - - -

Todo

Next Version

  • Implement methods to create or import accounts.
  • Implement group chats

At Some Point

  • Implement bind-methods (e.g. bindProfile(...) ) that repeatedly queries the resource and invokes a callback function for every update.
  • When posting a new resource revision (status, profile, avatar...) add the updated resource to the cache but flag it as "dirty" and rollback if the resource is not confirmed after a certain time.
  • Implement get...Promise(...) functions (e.g. getProfilePromise(...) ) that work with https://github.com/yortus/asyncawait to avoid callback hells
  • Implement code specific error functions (e.g. "errorfunc_32052" catches errors with code 32052)

Usage

In a Node Project

From inside the project folder run

npm install twister-lib-js

Then inside your code import the library using

Twister = require('twister-lib-js');

In a Webapp

Download the twister-lib.js file into you project folder. Link to it inside html using

<script src="path/to/twister-lib.js"></script>

Code Examples

Display the content of the latest post of user tschaul:

Twister.getUser("tschaul").doStatus(function(post){
  console.log(post.getContent());  
});

For more code examples in tutorial form, see /examples

Error Codes

twister-lib-js passes through all JSON-RPC errors. Internal errors are thrown in the same format with codes ranging between 32050 and 32099:

  • 32050: DHT resource signature could not be verified.
  • 32051: Unknown query setting was requested.
  • 32052: DHT resource is empty. (Only thrown for status, post, profile and avatar resources.)
  • 32060: Post signature could not be verified.
  • 32061: Public key not available on server.
  • 32062: Signature of retwisted post could not be verified.
  • 32063: Post could not be decrypted.
  • 32064: Private key is in conflict with public key.
  • 32080: Unsupported wallet type.
  • 32081: No wallet users found on the server.
  • 32082: Torrent inactive. Activate torrent first!
  • 32090: Host not reachable.
  • 32091: Request was not processed successfully (http error: HTTP_ERROR_CODE).
  • 32092: An error occurred while parsing the JSON response body.

Change Log

0.3.0

  • New query setting queryId. Can be used to mark multiple queries with a common id. With the Twister.onQueryComplete(...) function a handler can be registered that is triggered when the last query is completed. see examples/021_deep_fetching.js

0.2.1

  • Better handling of http/connection errors. Error codes: 32090-32092

0.2.0

  • Resources can now remove themselves from the cache using their own trim() function.
  • New function Twister.trimCache(timestamp) deletes every resource from the cache that is older than the timestamp, or has not been accessed since the timestamp. Needed to manage localStorage size in browsers.
  • Resources with invalid signatures now get removed from the cache.

twister-lib-js's People

Contributors

tschaul avatar erkan-yilmaz avatar gabrielhesposito avatar flipflopsimsommer avatar

Stargazers

Maher Zaidoun avatar  avatar Mulili Nzuki avatar Basil avatar  avatar Tikhon Bernstam avatar Corey Henderson avatar

Watchers

James Cloos avatar  avatar  avatar

twister-lib-js's Issues

How to get a list of resources?

Hello!

I'm trying to create a simple wrapper in Node.js that people might access from over the internet to read data from the Twister network (so only the 'get' part, not the 'set'), to make, for instance, an embeddable Twister Updates widget that shows the last posts from a certain user.

What I am having trouble with, is what to do when calling a function like Twister.getUser(name).doLatestPostsUntil(), I end up with a recursive callback structure. It gets very hard to create a list from this to return to the browser. The basic example works: I push() the post to an array, and when I'm at the final post I want, I return the contents of the array to the user.

But what if I want to append the URL of the user's avatar in the results as well?
I end up with recursive function calls that are very hard to understand and control.

What am I doing wrong?

Sincerely,

~Qqwy

Throw an error on bad JSON return

I tried this library out, but had problems with a syntax error that was thrown. It took me quite a bit of debugging before I figured out that the JSON.parse method on (in this case) r.283 of twisterResource.js was not placed within a try/catch statement.

What happened was, my RPC info was wrong, so the server answered with a 401 error instead of proper JSON. It would be great to display/throw a custom error message in this case, I think.

other than that: Great work! The examples are wonderful :-).

Implement bind-methods

Implement bind-methods (e.g. bindProfile(...) ) that repeatedly queries the resource and invokes a callback function for every update.

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.