@synonymdev/feeds
Advanced tools
Comparing version 1.0.0-alpha.2 to 1.0.0-alpha.3
68
index.js
@@ -9,3 +9,2 @@ const os = require('os') | ||
const Hyperdrive = require('hyperdrive') | ||
const deepEqual = require('deep-equal') | ||
@@ -26,2 +25,3 @@ const DEFAULT_PATH = path.join(os.homedir(), '.slashtags-feeds') | ||
this.header = header | ||
this.drives = [] | ||
} | ||
@@ -33,2 +33,8 @@ | ||
async close () { | ||
// close the drives (one at a time) | ||
for (let i = 0; i < this.drives.length; i += 1) { | ||
await this.drives[i].hyperdrive.close() | ||
} | ||
this.drives = [] | ||
await this.corestore.close() | ||
@@ -62,3 +68,4 @@ return this.swarm.destroy() | ||
await this._ensureHeader(drive) | ||
const headerContent = Buffer.from(JSON.stringify(this.header)) | ||
await this.ensureFile(feedID, Feeds.HEADER_PATH, headerContent) | ||
@@ -71,23 +78,15 @@ return { | ||
async _ensureHeader (drive) { | ||
const batch = drive.batch() | ||
const existing = await batch.get(Feeds.HEADER_PATH).then((buf) => { | ||
return buf && JSON.parse(buf.toString()) | ||
}) | ||
const skip = existing && deepEqual(existing, this.header) | ||
if (skip) return batch.flush() | ||
_drive (feedID) { | ||
const drive = this.drives.find((d) => d.feedID === feedID) | ||
if (drive) { | ||
return drive.hyperdrive | ||
} | ||
await batch.put( | ||
Feeds.HEADER_PATH, | ||
Buffer.from(JSON.stringify(this.header)) | ||
) | ||
await batch.flush() | ||
} | ||
_drive (feedID) { | ||
// don't have this one yet, make it | ||
const namespace = this.corestore.namespace(feedID) | ||
const encryptionKey = hash(namespace._namespace) | ||
const drive = new Hyperdrive(namespace, { encryptionKey }) | ||
const hyperdrive = new Hyperdrive(namespace, { encryptionKey }) | ||
this.drives.push({ feedID, hyperdrive }) | ||
return drive | ||
return hyperdrive | ||
} | ||
@@ -123,2 +122,33 @@ | ||
/** | ||
* Deletes an old file that is not needed any more | ||
* @param {} feedID | ||
* @param {*} key | ||
*/ | ||
async deleteFile (feedID, key) { | ||
const drive = await this._drive(feedID) | ||
await drive.del(key) | ||
} | ||
/** | ||
* Ensures a file exists and writes it if missing or out of date | ||
* Returns true if the file was missing and needed to be written | ||
* @param {string} feedID | ||
* @param {string} key | ||
* @param {SerializableItem} value | ||
*/ | ||
async ensureFile (feedID, key, data) { | ||
const drive = await this._drive(feedID) | ||
const batch = drive.batch() | ||
const existing = await batch.get(key) | ||
if (existing && existing.equals(data)) { | ||
await batch.flush() | ||
return false | ||
} | ||
await batch.put(key, data) | ||
await batch.flush() | ||
return true | ||
} | ||
/** | ||
* Delete the feed from storage | ||
@@ -125,0 +155,0 @@ * @param {string} feedID |
{ | ||
"name": "@synonymdev/feeds", | ||
"version": "1.0.0-alpha.2", | ||
"version": "1.0.0-alpha.3", | ||
"scripts": { | ||
@@ -10,6 +10,4 @@ "test": "standard --fix && node test/all.js" | ||
"corestore": "^6.0.1-alpha.19", | ||
"deep-equal": "^2.0.5", | ||
"hyperdrive": "github:Nazeh/hyperdrive-next#pass-options", | ||
"hyperswarm": "^4.2.0", | ||
"just-compare": "^2.2.2", | ||
"sodium-universal": "^3.1.0" | ||
@@ -22,2 +20,2 @@ }, | ||
} | ||
} | ||
} |
@@ -40,3 +40,3 @@ const Feeds = require('../index.js') | ||
feed.key.toString('hex'), | ||
'3ea919d3dd3fc7ba82d24df6a04ca972dcac66a2470e6fbea2008437072214a1' | ||
'b5fd1f1df9204f61ab6f5741a1471f14ee1541c833f2233258e4ee3f59a65dbe' | ||
) | ||
@@ -50,3 +50,3 @@ t.is( | ||
feed2.key.toString('hex'), | ||
'c09510a75f0296cc935b9dc3d39bda113d5d9552751722e5ce4e3d2326446173' | ||
'7843365d5054795fd61238f8fd1d6c534ca7abd17913705911b3a074ce45171d' | ||
) | ||
@@ -53,0 +53,0 @@ t.is( |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11589
5
322
- Removeddeep-equal@^2.0.5
- Removedjust-compare@^2.2.2
- Removedarray-buffer-byte-length@1.0.1(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removeddeep-equal@2.2.3(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-get-iterator@1.1.3(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-bigints@1.0.2(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedinternal-slot@1.0.7(transitive)
- Removedis-arguments@1.1.1(transitive)
- Removedis-array-buffer@3.0.4(transitive)
- Removedis-bigint@1.0.4(transitive)
- Removedis-boolean-object@1.1.2(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-map@2.0.3(transitive)
- Removedis-number-object@1.0.7(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedis-set@2.0.3(transitive)
- Removedis-shared-array-buffer@1.0.3(transitive)
- Removedis-string@1.0.7(transitive)
- Removedis-symbol@1.0.4(transitive)
- Removedis-weakmap@2.0.2(transitive)
- Removedis-weakset@2.0.3(transitive)
- Removedisarray@2.0.5(transitive)
- Removedjust-compare@2.3.0(transitive)
- Removedobject-inspect@1.13.3(transitive)
- Removedobject-is@1.1.6(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.5(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedregexp.prototype.flags@1.5.3(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedstop-iteration-iterator@1.0.0(transitive)
- Removedwhich-boxed-primitive@1.0.2(transitive)
- Removedwhich-collection@1.0.2(transitive)
- Removedwhich-typed-array@1.1.15(transitive)