Use canonicalJson

This commit is contained in:
Mitra Ardron 2018-09-15 17:28:18 +10:00
parent 49113f78e2
commit 596a7a360b
6 changed files with 16 additions and 10 deletions

View File

@ -6,6 +6,8 @@ process.env.GUN_ENV = "false";
const Gun = require('gun/gun.js'); // TODO-GUN switchback to gun/gun at some point to get minimized version
require('gun/lib/path.js');
const debuggun = require('debug')('dweb-transports:gun');
const stringify = require('canonical-json');
// Other Dweb modules
const errors = require('./Errors'); // Standard Dweb Errors
@ -200,7 +202,7 @@ class TransportGUN extends Transport {
// Logged by Transports
console.assert(url && sig.urls.length && sig.signature && sig.signedby.length, "TransportGUN.p_rawadd args", url, sig);
this.connection(url)
.set( JSON.stringify( sig.preflight( Object.assign({}, sig))));
.set( stringify( sig.preflight( Object.assign({}, sig))));
}
// noinspection JSCheckFunctionSignatures
@ -253,13 +255,13 @@ class TransportGUN extends Transport {
*/
let table = this.connection(url);
if (typeof keyvalues === "string") {
table.path(keyvalues).put(JSON.stringify(value));
table.path(keyvalues).put(stringify(value));
} else {
// Store all key-value pairs without destroying any other key/value pairs previously set
console.assert(!Array.isArray(keyvalues), "TransportGUN - shouldnt be passsing an array as the keyvalues");
table.put(
Object.keys(keyvalues).reduce(
function(previous, key) { previous[key] = JSON.stringify(keyvalues[key]); return previous; },
function(previous, key) { previous[key] = stringify(keyvalues[key]); return previous; },
{}
))
}

View File

@ -4,6 +4,7 @@ const httptools = require('./httptools'); // Expose some of the httptools so tha
const Url = require('url');
const stream = require('readable-stream');
const debughttp = require('debug')('dweb-transports:http');
const stringify = require('canonical-json');
defaulthttpoptions = {
@ -129,7 +130,7 @@ class TransportHTTP extends Transport {
p_rawadd(url, sig) {
// Logged by Transports
if (!url || !sig) throw new errors.CodingError("TransportHTTP.p_rawadd: invalid parms", url, sig);
let value = JSON.stringify(sig.preflight(Object.assign({},sig)))+"\n";
let value = stringify(sig.preflight(Object.assign({},sig)))+"\n";
return httptools.p_POST(this._url(url, servercommands.rawadd), "application/json", value); // Returns immediately
}
@ -258,10 +259,10 @@ class TransportHTTP extends Transport {
// Logged by Transports
//debughttp("p_set %o %o %o", url, keyvalues, value);
if (typeof keyvalues === "string") {
let kv = JSON.stringify([{key: keyvalues, value: value}]);
let kv = stringify([{key: keyvalues, value: value}]);
await httptools.p_POST(this._url(url, servercommands.set), "application/json", kv); // Returns immediately
} else {
let kv = JSON.stringify(Object.keys(keyvalues).map((k) => ({"key": k, "value": keyvalues[k]})));
let kv = stringify(Object.keys(keyvalues).map((k) => ({"key": k, "value": keyvalues[k]})));
await httptools.p_POST(this._url(url, servercommands.set), "application/json", kv); // Returns immediately
}
}

View File

@ -318,7 +318,7 @@ class TransportIPFS extends Transport {
return TransportIPFS.urlFrom(res);
}
/* OLD WAY Based on https://github.com/ipfs/js-ipfs/pull/1231/files TODO-IPFS repurpose this to add byte range to fetch
/* OLD WAY Based on https://github.com/ipfs/js-ipfs/pull/1231/files TODO-IPFS repurpose this to add byte range to new function fetch calling p_rawfetch
async p_offsetStream(stream, links, startByte, endByte) {
let streamPosition = 0
@ -364,7 +364,7 @@ class TransportIPFS extends Transport {
const self = this;
function crs(opts) { // This is a synchronous function
// Return a readable stream that provides the bytes between offsets "start" and "end" inclusive
console.log("opts=",JSON.stringify(opts));
debug("opts=%o", opts);
// Can replace rest of crs with this when https://github.com/ipfs/js-ipfs/pull/1231/files lands (hopefully v0.28.3)
// return self.ipfs.catReadableStream(mh, opts ? opts.start : 0, opts && opts.end) ? opts.end+1 : undefined)
if (!opts) return throughstream; //TODO-STREAM unclear why called without opts - take this out when figured out

View File

@ -5,6 +5,7 @@ Y Lists have listeners and generate events - see docs at ...
*/
const Url = require('url');
const debugyjs = require('debug')('dweb-transports:yjs');
const stringify = require('canonical-json');
//const Y = require('yjs/dist/y.js'); // Explicity require of dist/y.js to get around a webpack warning but causes different error in YJS
const Y = require('yjs'); // Explicity require of dist/y.js to get around a webpack warning
@ -260,7 +261,7 @@ class TransportYJS extends Transport {
*/
let y = await this.p_connection(url);
if (typeof keyvalues === "string") {
y.share.map.set(keyvalues, JSON.stringify(value));
y.share.map.set(keyvalues, stringify(value));
} else {
Object.keys(keyvalues).map((key) => y.share.map.set(key, keyvalues[key]));
}

View File

@ -11,6 +11,7 @@
"url": "https://github.com/internetarchive/dweb-transports/issues"
},
"dependencies": {
"canonical-json": "latest",
"cids": "latest",
"gun": "^0.9.99993",
"ipfs": "^0.31.7",

View File

@ -1,5 +1,6 @@
const IPFS = require('ipfs');
const stringify = require('canonical-json');
var ipfs;
const CID = require('cids');
const unixFs = require('ipfs-unixfs');
@ -80,7 +81,7 @@ function check_result(name, buff, expected, expectfailure) {
if ( (typeof(expected) === "number") && (expected !== buff.length)) {
console.log(name, "Expected block length", expected, "but got", buff.length, expectfailure ? "Note this was expected to fail." : "");
//console.log(buff); // Normally leave commented out - will be long if looking at 250k file !
} else if ((typeof(expected) !== "number") && (JSON.stringify(expected) !== JSON.stringify(buff))) {
} else if ((typeof(expected) !== "number") && (stringify(expected) !== stringify(buff))) {
console.log(name, "Expected:", expected.constructor.name, expected, "got", buff.constructor.name, buff, expectfailure ? "Note this was expected to fail." : "");
} else {
console.log(name, "Retrieved successfully");