Webstrates Node API
The Webstrates Node API offers a high-level API to connect to a Webstrates server and open webstrates documents. It further offers event callbacks, which will be called when a document does not exist on the server, a document is opened or a document changed. This API is intended to be used by developers who want to contribute tools to ease the development or alteration of webstrates like the Webstrates Editor Extension for Visual Studio Code.
Add webstrates node-api to your npm project.
$> npm install webstrates --save
Look at the example folder or here for a working client-cli example! Alternatively, try out the following step-by-step example.
Documentation by Example
- Create a W3C compatible websocket.
const W3CWebSocket = w3cwebsocket; /** * Create a Web socket connection using the host address to connect. The host address * could also contain the port. * * @param * @param * @returns */ { // https://github.com/theturtle32/WebSocket-Node/blob/19108bbfd7d94a5cd02dbff3495eafee9e901ca4/docs/W3CWebSocket.md return // requestUrl port > -1 ? `:/ws/` : `/ws/` // requestedProtocols undefined // origin undefined // headers // cookie: "session=XXX" // requestOptions undefined // clientConfig maxReceivedFrameSize: 1024 * 1024 * 20 // 20 MB ;} // Connect to webstrate.cs.au.dk --> Do not forget ws:// prefix.var websocket = ;
- Connect to Webstrates server.
var webstrates = ; ... var client = websocket; client; client; client;
- Open webstrate document (e.g., with webstrate id node-api).
// Set auto connect parameter to false in order to add event listeners to// the document.var document = client; document; document; document; document; // Finally connect to document// This might be deprecated once webstrates node-api supports triggering events on// event listeners that have been registered after connection to sharedb document// established.document;
- Add listeners to receive document content and document updates.
... document; document; ...
- Filter or change webstrate document before onUpdate and onUpdateOp callbacks are triggered.
... /** * Filters out the <head>...</head> from a document and returns it as a new jsonML * document. * * @param * @returns */ { var newJsonML = jsonML; if jsonML && jsonMLlength > 2 for var i = 2; i < jsonMLlength; i++ if jsonMLi0 === "head" continue; newJsonML; return newJsonML;} /** * Changes the head tag element name to neck and returns the changed jsonML * document. * * @param * @returns */ { if jsonML && jsonMLlength > 2 for var i = 2; i < jsonMLlength; i++ if jsonMLi0 === "head" jsonMLi0 = "neck"; return jsonML;} document; document; document; document; ...
- Disconnect from document and also disconnect from Webstrates server.
... // Close connection to document.document; ... // CLose client connection. This also closes the websocket connection.client; ...
Developers
How to release a new version
Use the npm version
command to release a new version. It will run npm test
before version is released
and only continues if all tests succeed. It further will create a git tag named after the new version and
push the tag to the remote repository. Travis CI will run again all tests and deploy the new version to
npmjs repository.
To release a new version use:
$> npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
For example:
$> npm version patch
More details on npm version
command at npmjs.org.
License
This work is licenced under the Apache License, Version 2.0.