mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-03-15 10:30:48 +00:00
Use canonicalJson
This commit is contained in:
parent
49113f78e2
commit
596a7a360b
@ -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
|
const Gun = require('gun/gun.js'); // TODO-GUN switchback to gun/gun at some point to get minimized version
|
||||||
require('gun/lib/path.js');
|
require('gun/lib/path.js');
|
||||||
const debuggun = require('debug')('dweb-transports:gun');
|
const debuggun = require('debug')('dweb-transports:gun');
|
||||||
|
const stringify = require('canonical-json');
|
||||||
|
|
||||||
|
|
||||||
// Other Dweb modules
|
// Other Dweb modules
|
||||||
const errors = require('./Errors'); // Standard Dweb Errors
|
const errors = require('./Errors'); // Standard Dweb Errors
|
||||||
@ -200,7 +202,7 @@ class TransportGUN extends Transport {
|
|||||||
// Logged by Transports
|
// Logged by Transports
|
||||||
console.assert(url && sig.urls.length && sig.signature && sig.signedby.length, "TransportGUN.p_rawadd args", url, sig);
|
console.assert(url && sig.urls.length && sig.signature && sig.signedby.length, "TransportGUN.p_rawadd args", url, sig);
|
||||||
this.connection(url)
|
this.connection(url)
|
||||||
.set( JSON.stringify( sig.preflight( Object.assign({}, sig))));
|
.set( stringify( sig.preflight( Object.assign({}, sig))));
|
||||||
}
|
}
|
||||||
|
|
||||||
// noinspection JSCheckFunctionSignatures
|
// noinspection JSCheckFunctionSignatures
|
||||||
@ -253,13 +255,13 @@ class TransportGUN extends Transport {
|
|||||||
*/
|
*/
|
||||||
let table = this.connection(url);
|
let table = this.connection(url);
|
||||||
if (typeof keyvalues === "string") {
|
if (typeof keyvalues === "string") {
|
||||||
table.path(keyvalues).put(JSON.stringify(value));
|
table.path(keyvalues).put(stringify(value));
|
||||||
} else {
|
} else {
|
||||||
// Store all key-value pairs without destroying any other key/value pairs previously set
|
// 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");
|
console.assert(!Array.isArray(keyvalues), "TransportGUN - shouldnt be passsing an array as the keyvalues");
|
||||||
table.put(
|
table.put(
|
||||||
Object.keys(keyvalues).reduce(
|
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; },
|
||||||
{}
|
{}
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ const httptools = require('./httptools'); // Expose some of the httptools so tha
|
|||||||
const Url = require('url');
|
const Url = require('url');
|
||||||
const stream = require('readable-stream');
|
const stream = require('readable-stream');
|
||||||
const debughttp = require('debug')('dweb-transports:http');
|
const debughttp = require('debug')('dweb-transports:http');
|
||||||
|
const stringify = require('canonical-json');
|
||||||
|
|
||||||
|
|
||||||
defaulthttpoptions = {
|
defaulthttpoptions = {
|
||||||
@ -129,7 +130,7 @@ class TransportHTTP extends Transport {
|
|||||||
p_rawadd(url, sig) {
|
p_rawadd(url, sig) {
|
||||||
// Logged by Transports
|
// Logged by Transports
|
||||||
if (!url || !sig) throw new errors.CodingError("TransportHTTP.p_rawadd: invalid parms", url, sig);
|
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
|
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
|
// Logged by Transports
|
||||||
//debughttp("p_set %o %o %o", url, keyvalues, value);
|
//debughttp("p_set %o %o %o", url, keyvalues, value);
|
||||||
if (typeof keyvalues === "string") {
|
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
|
await httptools.p_POST(this._url(url, servercommands.set), "application/json", kv); // Returns immediately
|
||||||
} else {
|
} 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
|
await httptools.p_POST(this._url(url, servercommands.set), "application/json", kv); // Returns immediately
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -318,7 +318,7 @@ class TransportIPFS extends Transport {
|
|||||||
return TransportIPFS.urlFrom(res);
|
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) {
|
async p_offsetStream(stream, links, startByte, endByte) {
|
||||||
let streamPosition = 0
|
let streamPosition = 0
|
||||||
@ -364,7 +364,7 @@ class TransportIPFS extends Transport {
|
|||||||
const self = this;
|
const self = this;
|
||||||
function crs(opts) { // This is a synchronous function
|
function crs(opts) { // This is a synchronous function
|
||||||
// Return a readable stream that provides the bytes between offsets "start" and "end" inclusive
|
// 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)
|
// 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)
|
// 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
|
if (!opts) return throughstream; //TODO-STREAM unclear why called without opts - take this out when figured out
|
||||||
|
@ -5,6 +5,7 @@ Y Lists have listeners and generate events - see docs at ...
|
|||||||
*/
|
*/
|
||||||
const Url = require('url');
|
const Url = require('url');
|
||||||
const debugyjs = require('debug')('dweb-transports:yjs');
|
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/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
|
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);
|
let y = await this.p_connection(url);
|
||||||
if (typeof keyvalues === "string") {
|
if (typeof keyvalues === "string") {
|
||||||
y.share.map.set(keyvalues, JSON.stringify(value));
|
y.share.map.set(keyvalues, stringify(value));
|
||||||
} else {
|
} else {
|
||||||
Object.keys(keyvalues).map((key) => y.share.map.set(key, keyvalues[key]));
|
Object.keys(keyvalues).map((key) => y.share.map.set(key, keyvalues[key]));
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
"url": "https://github.com/internetarchive/dweb-transports/issues"
|
"url": "https://github.com/internetarchive/dweb-transports/issues"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"canonical-json": "latest",
|
||||||
"cids": "latest",
|
"cids": "latest",
|
||||||
"gun": "^0.9.99993",
|
"gun": "^0.9.99993",
|
||||||
"ipfs": "^0.31.7",
|
"ipfs": "^0.31.7",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
const IPFS = require('ipfs');
|
const IPFS = require('ipfs');
|
||||||
|
|
||||||
|
const stringify = require('canonical-json');
|
||||||
var ipfs;
|
var ipfs;
|
||||||
const CID = require('cids');
|
const CID = require('cids');
|
||||||
const unixFs = require('ipfs-unixfs');
|
const unixFs = require('ipfs-unixfs');
|
||||||
@ -80,7 +81,7 @@ function check_result(name, buff, expected, expectfailure) {
|
|||||||
if ( (typeof(expected) === "number") && (expected !== buff.length)) {
|
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(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 !
|
//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." : "");
|
console.log(name, "Expected:", expected.constructor.name, expected, "got", buff.constructor.name, buff, expectfailure ? "Note this was expected to fail." : "");
|
||||||
} else {
|
} else {
|
||||||
console.log(name, "Retrieved successfully");
|
console.log(name, "Retrieved successfully");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user