Newer canonicaljson

This commit is contained in:
Mitra Ardron 2019-08-26 15:48:49 +10:00
parent bf59edca99
commit 00ee80de01
7 changed files with 28 additions and 26 deletions

View File

@ -24,8 +24,7 @@ require('gun/lib/store.js');
require('gun/lib/rindexed.js');
const debuggun = require('debug')('dweb-transports:gun');
const stringify = require('canonical-json');
const canonicaljson = require('@stratumn/canonicaljson');
// Other Dweb modules
const errors = require('./Errors'); // Standard Dweb Errors
@ -225,7 +224,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( stringify( sig.preflight( Object.assign({}, sig))));
.set( canonicaljson.stringify( sig.preflight( Object.assign({}, sig))));
}
// noinspection JSCheckFunctionSignatures
@ -278,13 +277,13 @@ class TransportGUN extends Transport {
*/
let table = this.connection(url);
if (typeof keyvalues === "string") {
table.path(keyvalues).put(stringify(value));
table.path(keyvalues).put(canonicaljson.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] = stringify(keyvalues[key]); return previous; },
function(previous, key) { previous[key] = canonicaljson.stringify(keyvalues[key]); return previous; },
{}
))
}

View File

@ -4,7 +4,8 @@ const httptools = require('./httptools'); // Expose some of the httptools so tha
const Url = require('url');
const stream = require('readable-stream');
const debug = require('debug')('dweb-transports:http');
const stringify = require('canonical-json');
const canonicaljson = require('@stratumn/canonicaljson');
defaulthttpoptions = {
@ -184,7 +185,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);
const data = stringify(sig.preflight(Object.assign({},sig)))+"\n";
const data = canonicaljson.stringify(sig.preflight(Object.assign({},sig)))+"\n";
return httptools.p_POST(this._url(url, servercommands.rawadd), {data, contenttype: "application/json"}); // Returns immediately
}
@ -316,10 +317,10 @@ class TransportHTTP extends Transport {
// Logged by Transports
//debug("p_set %o %o %o", url, keyvalues, value);
if (typeof keyvalues === "string") {
let data = stringify([{key: keyvalues, value: value}]);
let data = canonicaljson.stringify([{key: keyvalues, value: value}]);
await httptools.p_POST(this._url(url, servercommands.set), {data, contenttype: "application/json"}); // Returns immediately
} else {
let data = stringify(Object.keys(keyvalues).map((k) => ({"key": k, "value": keyvalues[k]})));
let data = canonicaljson.stringify(Object.keys(keyvalues).map((k) => ({"key": k, "value": keyvalues[k]})));
await httptools.p_POST(this._url(url, servercommands.set), {data, contenttype: "application/json"}); // Returns immediately
}
}

View File

@ -8,7 +8,7 @@ if( typeof window === 'undefined' ) {
} else {
WOLK = require("wolkjs").WOLK;
}
const stringify = require('canonical-json');
const canonicaljson = require('@stratumn/canonicaljson');
const debug = require('debug')('dweb-transports:wolk');
// Other Dweb modules
@ -141,7 +141,7 @@ class TransportWOLK extends Transport {
//TODO: use this.wolk.parseWolkUrl eventually
var wolkurl = this.parseWolkUrl(url)
/*
console.log("WOLK p_rawfetch url: " + JSON.stringify(wolkurl));
console.log("WOLK p_rawfetch url: " + canonicaljson.stringify(wolkurl));
console.log("WOLK owner: " + wolkurl.owner);
console.log("WOLK bucket: " + wolkurl.bucket);
console.log("WOLK key: " + wolkurl.path);
@ -155,7 +155,7 @@ class TransportWOLK extends Transport {
return this.wolk.getKey(wolkurl.owner, wolkurl.bucket, wolkurl.path, "latest")
.then(function(responseData) {
//TODO-WOLK: error checking
//debug("Response: %s", JSON.stringify(responseData)); //Commented as could be big
//debug("Response: %s", canonicaljson.stringify(responseData)); //Commented as could be big
return responseData;
})
.catch( (err) => {
@ -180,7 +180,7 @@ class TransportWOLK extends Transport {
*/
var wolkurl = this.parseWolkUrl(url)
/*
console.log("WOLK p_set url: " + JSON.stringify(wolkurl));
console.log("WOLK p_set url: " + canonoicaljson.stringify(wolkurl));
console.log("WOLK owner: " + wolkurl.owner);
console.log("WOLK bucket: " + wolkurl.bucket);
console.log("WOLK key: " + wolkurl.path);
@ -188,7 +188,7 @@ class TransportWOLK extends Transport {
console.log("WOLK urltype: " + wolkurl.urltype);
*/
if (typeof keyvalues === "string") {
return this.wolk.setKey(wolkurl.owner, wolkurl.bucket, keyvalues, stringify(value))
return this.wolk.setKey(wolkurl.owner, wolkurl.bucket, keyvalues, canonicaljson.stringify(value))
.then( (hash) => {
return hash;
})
@ -204,7 +204,7 @@ class TransportWOLK extends Transport {
table.put(
Object.keys(keyvalues).reduce(
function(previous, key) {
previous[key] = stringify(keyvalues[key]);
previous[key] = canonicaljson.stringify(keyvalues[key]);
return previous;
},
{}
@ -217,7 +217,7 @@ class TransportWOLK extends Transport {
async p_get(url, keys) {
var wolkurl = this.parseWolkUrl(url)
debug("Getting url: %s", JSON.stringify(wolkurl));
debug("Getting url: %s", canonicaljson.stringify(wolkurl));
/*
console.log("WOLK owner: " + wolkurl.owner);
console.log("WOLK bucket: " + wolkurl.bucket);

View File

@ -5,7 +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 canonicaljson = require('@stratumn/canonicaljson');
//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
@ -262,7 +262,7 @@ class TransportYJS extends Transport {
*/
let y = await this.p_connection(url);
if (typeof keyvalues === "string") {
y.share.map.set(keyvalues, stringify(value));
y.share.map.set(keyvalues, canonicaljson.stringify(value));
} else {
Object.keys(keyvalues).map((key) => y.share.map.set(key, keyvalues[key]));
}

View File

@ -11,7 +11,7 @@
"url": "https://github.com/internetarchive/dweb-transports/issues"
},
"dependencies": {
"canonical-json": "^0.0.4",
"@stratumn/canonicaljson": "^1.0.3",
"cids": "^0.7.1",
"debug": "^4.1.1",
"gun": "^0.2019.726",

View File

@ -1,6 +1,6 @@
const IPFS = require('ipfs');
const stringify = require('canonical-json');
const canonicaljson = require('@stratumn/canonicaljson');
var ipfs;
const CID = require('cids');
const unixFs = require('ipfs-unixfs');
@ -81,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") && (stringify(expected) !== stringify(buff))) {
} else if ((typeof(expected) !== "number") && (canonicaljson.stringify(expected) !== canonicaljson.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");

View File

@ -289,6 +289,13 @@
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea"
integrity sha512-9NET910DNaIPngYnLLPeg+Ogzqsi9uM4mSboU5y6p8S5DzMTVEsJZrawi+BoDNUVBa2DhJqQYUFvMDfgU062LQ==
"@stratumn/canonicaljson@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@stratumn/canonicaljson/-/canonicaljson-1.0.3.tgz#4130525b7a264eeefabb5eadbe9e4d616c2f1ca7"
integrity sha512-OTP6z1+aJuKw9M3WKWLVf0yxjfzkCq3Uw2iMhwjQ02s5SSgTRxUqPtX1gJXj1RhSZiABLd9uI0Rj02N4gCNvpA==
dependencies:
bignumber.js "^8.1.1"
"@szmarczak/http-timer@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@szmarczak/http-timer/-/http-timer-1.1.2.tgz#b1665e2c461a2cd92f4c1bbf50d5454de0d4b421"
@ -1634,11 +1641,6 @@ camelcase@^5.0.0, camelcase@^5.3.1:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320"
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
canonical-json@^0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/canonical-json/-/canonical-json-0.0.4.tgz#6579c072c3db5c477ec41dc978fbf2b8f41074a3"
integrity sha1-ZXnAcsPbXEd+xB3JePvyuPQQdKM=
caseless@~0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"