Skip to content
Permalink
Browse files

Copy from old dweb-transport repo

  • Loading branch information...
mitra42 committed Jun 5, 2018
0 parents commit c59842a61c02befe5db5a6971e44b6dca5600a85
Showing with 1,302 additions and 0 deletions.
  1. +28 −0 .gitignore
  2. +13 −0 Errors.js
  3. +100 −0 HashStore.js
  4. +661 −0 LICENSE
  5. +69 −0 MirrorCollection.js
  6. 0 MirrorConfig.js
  7. +10 −0 MirrorFS.js
  8. +68 −0 MirrorItemFromStream.js
  9. +17 −0 MirrorUI.js
  10. +103 −0 README.md
  11. +135 −0 index.html
  12. +49 −0 mirroring.js
  13. +45 −0 package.json
  14. +4 −0 test.js
@@ -0,0 +1,28 @@
/node_modules

.sync
Icon?
npm-debug.log
dbtestjs
.cache
archive_minchanges/build
archive/node_modules
leveldb_dweb
mirror/testleveldb.Testtable
sw_client_bundle.js
sw_server_bundle.js
test_serviceworker_bundle.js
*-bundle.js
examples/includes
examples/images/cc
WaybackLogoSmall.png
loading.gif
gift.png
examples/fonts
archive.html
archive_bundle.js
bootloader.html
dweb-archive-styles.css
archivesw.html
package-lock.json
mirror/node_modules
@@ -0,0 +1,13 @@

errors = {};

// Use this when the code logic has been broken - e.g. something is called with an undefined parameter, its preferable to console.assert
// Typically this is an error, that should have been caught higher up.
class IntentionallyUnimplementedError extends Error {
constructor(message) {
super(message || "Intentionally unimplemented");
this.name = "IntentionallyUnimplementedError"
}
}

errors.IntentionallyUnimplementedError = IntentionallyUnimplementedError;
@@ -0,0 +1,100 @@
const level = require('level');
const errors = require("./Errors.js"); // ToBeImplementedError


class HashStore {
/*
A generic Hash Store built on top of level,
TODO - this could probably build on top of Redis as well.
*/
constructor() {
throw new errors.IntentionallyUnimplementedError("There is no meaningful constructor for HashStore")
}
static async init(config) {
this.config = config;
this.tables = {}
}
static db(table) {
if (!this.tables[table]) {
this.tables[table] = level(`${this.config.dir}${table}`);
}
return this.tables[table]; // Note file might not be open yet, if not any put/get/del will be queued by level till its ready
}
static async put(table, key, val) {
if (typeof key === "object") {
await this.db(table).batch(Object.keys(key).map(k => {return {type: "put", key: k, value: key[k]};}));
} else {
await this.db(table).put(key, val);
}
}
static async get(table, key) {
let tab = this.db(table);
//return await tab.get(key); // Fails ...see https://github.com/Level/level/issues/97
// Promisified get because of bug reported above.
return new Promise((resolve, reject) => tab.get(key, function(err, val) {
if (err && err.notFound) resolve(undefined);
if (err) { reject(err) }
resolve(val);
}));
}
static async del(table, key) {
if (typeof key === "object") { // Delete all keys in object
await this.db(table).batch(Object.keys(key).map(k => {return {type: "del", key: k};}));
} else {
await this.db(table).del(key);
}
}
static async map(table, cb, {end=undefined}={}) {
// cb(data) => data.key, data.value
// Returns a stream so can add further .on
// UNTESTED
return this.db(table)
.createReadStream()
.on('data', cb );
}
static async keys(table) {
let keys=[];
let db = this.db(table); //syncronous
return await new Promise(function(resolve, reject) {
try {
db
.createKeyStream()
// Note close comes after End
.on('data', (key) => keys.push(key))
.on('end', () => resolve(keys)) // Gets to end of stream
.on('close', () => resolve(keys)) // Gets to end of stream, or closed from outside
.on('error', (err) => { console.error('Error in stream from',table); reject(err)});
} catch (err) {
reject(err);
}
}
);
}
static async test() {
try {
await this.init({dir: "testleveldb."});
await this.put("Testtable", "testkey", "testval");
let res = await this.get("Testtable", "testkey");
console.assert(res === "testval");
await this.put("Testtable", {A: "AAA", B: "BBB"});
res = await this.get("Testtable", "A");
console.log("Get after put obj, res=", res)
console.assert(res === "AAA");
res = await this.get("Testtable", "B");
console.assert(res === "BBB");
res = await this.del("Testtable", "A");
res = await this.get("Testtable", "A");
console.log("Get after del res=", res)
console.assert(res === undefined);
res = await this.keys("Testtable");
console.log("DONE",res)
// Now test batches
// Now test map
} catch (err) {
console.log("Error caught in HashStore.test", err);
}
}
}
exports = module.exports = HashStore;

0 comments on commit c59842a

Please sign in to comment.
You can’t perform that action at this time.