New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

datastore-core

Package Overview
Dependencies
Maintainers
3
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

datastore-core - npm Package Compare versions

Comparing version 2.0.1 to 3.0.0

dist/src/index.d.ts

14

CHANGELOG.md

@@ -0,1 +1,15 @@

# [3.0.0](https://github.com/ipfs/js-datastore-core/compare/v2.0.1...v3.0.0) (2021-01-22)
### Bug Fixes
* fix ShardingDatastore creation ([#48](https://github.com/ipfs/js-datastore-core/issues/48)) ([21b48e7](https://github.com/ipfs/js-datastore-core/commit/21b48e7ccc965a1422f8a0f7eadbd83715b5cac6))
### Features
* ts types, github ci and clean up ([#39](https://github.com/ipfs/js-datastore-core/issues/39)) ([bee45ae](https://github.com/ipfs/js-datastore-core/commit/bee45ae0b778171d0919e135ea8affcf2d6de635))
## [2.0.1](https://github.com/ipfs/js-datastore-core/compare/v2.0.0...v2.0.1) (2020-11-09)

@@ -2,0 +16,0 @@

27

package.json
{
"name": "datastore-core",
"version": "2.0.1",
"description": "Wrapper implmentation for interface-datastore",
"leadMaintainer": "Pedro Teixeira <pedro@protocol.ai>",
"version": "3.0.0",
"description": "Wrapper implementation for interface-datastore",
"leadMaintainer": "Alex Potsides <alex.potsides@protocol.ai>",
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"prepare": "aegir build --no-bundle",
"test": "aegir test",

@@ -12,3 +14,2 @@ "test:node": "aegir test -t node",

"test:webworker": "aegir test -t webworker",
"build": "aegir build",
"lint": "aegir lint",

@@ -42,16 +43,16 @@ "release": "aegir release",

"devDependencies": {
"aegir": "^28.1.0",
"async-iterator-all": "^1.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
"@types/debug": "^4.1.5",
"aegir": "^30.3.0",
"it-all": "^1.0.4"
},
"dependencies": {
"debug": "^4.1.1",
"interface-datastore": "^2.0.0",
"ipfs-utils": "^4.0.1"
"interface-datastore": "^3.0.1"
},
"engines": {
"node": ">=6.0.0",
"npm": ">=3.0.0"
"node": ">=12.0.0"
},
"eslintConfig": {
"extends": "ipfs"
},
"contributors": [

@@ -61,6 +62,6 @@ "achingbrain <alex@achingbrain.net>",

"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Hugo Dias <hugomrdias@gmail.com>",
"Hector Sanjuan <code@hector.link>",
"Zane Starr <zcstarr@zaner.attlocal.net>",
"Jacob Heun <jacobheun@gmail.com>",
"Hugo Dias <hugomrdias@gmail.com>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",

@@ -67,0 +68,0 @@ "Pedro Teixeira <i@pgte.me>",

@@ -1,2 +0,2 @@

# js-datastore-core
# js-datastore-core <!-- omit in toc -->

@@ -6,27 +6,19 @@ [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)

[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![Build Status](https://flat.badgen.net/travis/ipfs/js-datastore-core)](https://travis-ci.com/ipfs/js-datastore-core)
[![Codecov](https://codecov.io/gh/ipfs/js-datastore-core/branch/master/graph/badge.svg)](https://codecov.io/gh/ipfs/js-datastore-core)
[![Dependency Status](https://david-dm.org/ipfs/js-datastore-core.svg?style=flat-square)](https://david-dm.org/ipfs/js-datastore-core)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D8.0.0-orange.svg?style=flat-square)
[![codecov](https://img.shields.io/codecov/c/github/ipfs/js-datastore-core.svg?style=flat-square)](https://codecov.io/gh/ipfs/js-datastore-core)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/ipfs/js-datastore-core/ci?label=ci&style=flat-square)](https://github.com/ipfs/js-datastore-core/actions?query=branch%3Amaster+workflow%3Aci+)
> Wrapping implementations for [interface-datastore](https://github.com/ipfs/interface-datastore).
## Lead Maintainer
## Lead Maintainer <!-- omit in toc -->
[Alex Potsides](https://github.com/achingbrain)
## Table of Contents
## Table of Contents <!-- omit in toc -->
- [js-datastore-core](#js-datastore-core)
- [Lead Maintainer](#lead-maintainer)
- [Table of Contents](#table-of-contents)
- [Implementations](#implementations)
- [Install](#install)
- [Usage](#usage)
- [Wrapping Stores](#wrapping-stores)
- [Contribute](#contribute)
- [License](#license)
- [Implementations](#implementations)
- [Install](#install)
- [Usage](#usage)
- [Wrapping Stores](#wrapping-stores)
- [Contribute](#contribute)
- [License](#license)

@@ -33,0 +25,0 @@ ## Implementations

@@ -10,7 +10,14 @@ 'use strict'

exports.KeytransformDatastore = KeytransformDatastore
exports.ShardingDatastore = ShardingDatastore
exports.MountDatastore = MountDatastore
exports.TieredDatastore = TieredDatastore
exports.NamespaceDatastore = NamespaceDatastore
exports.shard = shard
/**
* @typedef {import("./types").Shard } Shard
* @typedef {import("./types").KeyTransform } KeyTransform
*/
module.exports = {
KeytransformDatastore,
ShardingDatastore,
MountDatastore,
TieredDatastore,
NamespaceDatastore,
shard
}

@@ -5,2 +5,10 @@ 'use strict'

const map = utils.map
/**
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('interface-datastore').Options} Options
* @typedef {import('interface-datastore').Batch} Batch
* @typedef {import('interface-datastore').Query} Query
* @typedef {import('interface-datastore').Key} Key
* @typedef {import('./types').KeyTransform} KeyTransform
*/

@@ -11,4 +19,10 @@ /**

* keys, reversing them, etc.
*
* @implements {Datastore}
*/
class KeyTransformDatastore extends Adapter {
/**
* @param {Datastore} child
* @param {KeyTransform} transform
*/
constructor (child, transform) {

@@ -25,2 +39,7 @@ super()

/**
* @param {Key} key
* @param {Uint8Array} val
* @param {Options} [options]
*/
put (key, val, options) {

@@ -30,2 +49,6 @@ return this.child.put(this.transform.convert(key), val, options)

/**
* @param {Key} key
* @param {Options} [options]
*/
get (key, options) {

@@ -35,2 +58,6 @@ return this.child.get(this.transform.convert(key), options)

/**
* @param {Key} key
* @param {Options} [options]
*/
has (key, options) {

@@ -40,2 +67,6 @@ return this.child.has(this.transform.convert(key), options)

/**
* @param {Key} key
* @param {Options} [options]
*/
delete (key, options) {

@@ -45,2 +76,5 @@ return this.child.delete(this.transform.convert(key), options)

/**
* @returns {Batch}
*/
batch () {

@@ -61,2 +95,6 @@ const b = this.child.batch()

/**
* @param {Query} q
* @param {Options} [options]
*/
query (q, options) {

@@ -63,0 +101,0 @@ return map(this.child.query(q, options), e => {

@@ -16,6 +16,25 @@ /* @flow */

/**
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('interface-datastore').Options} Options
* @typedef {import('interface-datastore').Batch} Batch
* @typedef {import('interface-datastore').Query} Query
* @typedef {import('interface-datastore').Pair} Pair
*/
/**
* @template TEntry
* @typedef {import('./types').AwaitIterable<TEntry>} AwaitIterable
*/
/**
* A datastore that can combine multiple stores inside various
* key prefixs.
*
* @implements {Datastore}
*/
class MountDatastore extends Adapter {
/**
*
* @param {Array<{prefix: Key, datastore: Datastore}>} mounts
*/
constructor (mounts) {

@@ -27,4 +46,4 @@ super()

open () {
return Promise.all(this.mounts.map((m) => m.datastore.open()))
async open () {
await Promise.all(this.mounts.map((m) => m.datastore.open()))
}

@@ -37,3 +56,3 @@

* @param {Key} key
* @returns {{Datastore, Key, Key}}
* @returns {{datastore: Datastore, mountpoint: Key, rest: Key} | undefined}
*/

@@ -53,2 +72,7 @@ _lookup (key) {

/**
* @param {Key} key
* @param {Uint8Array} value
* @param {Options} [options]
*/
put (key, value, options) {

@@ -63,2 +87,6 @@ const match = this._lookup(key)

/**
* @param {Key} key
* @param {Options} [options]
*/
get (key, options) {

@@ -72,6 +100,10 @@ const match = this._lookup(key)

/**
* @param {Key} key
* @param {Options} [options]
*/
has (key, options) {
const match = this._lookup(key)
if (match == null) {
return false
return Promise.resolve(false)
}

@@ -81,2 +113,6 @@ return match.datastore.has(match.rest, options)

/**
* @param {Key} key
* @param {Options} [options]
*/
delete (key, options) {

@@ -91,4 +127,4 @@ const match = this._lookup(key)

close () {
return Promise.all(this.mounts.map((m) => {
async close () {
await Promise.all(this.mounts.map((m) => {
return m.datastore.close()

@@ -98,4 +134,11 @@ }))

/**
* @returns {Batch}
*/
batch () {
/** @type {Record<string, Batch>} */
const batchMounts = {}
/**
* @param {Key} key
*/
const lookup = (key) => {

@@ -127,4 +170,4 @@ const match = this._lookup(key)

},
commit: (options) => {
return Promise.all(Object.keys(batchMounts).map(p => batchMounts[p].commit(options)))
commit: async (options) => {
await Promise.all(Object.keys(batchMounts).map(p => batchMounts[p].commit(options)))
}

@@ -134,2 +177,6 @@ }

/**
* @param {Query} q
* @param {Options} [options]
*/
query (q, options) {

@@ -163,3 +210,3 @@ const qs = this.mounts.map(m => {

let i = 0
it = filter(it, () => i++ >= q.offset)
it = filter(it, () => i++ >= /** @type {number} */ (q.offset))
}

@@ -172,2 +219,6 @@ if (q.limit != null) it = take(it, q.limit)

/**
* @param {ArrayLike<AwaitIterable<Pair>>} iterable
* @returns {AsyncIterable<Pair>}
*/
function _many (iterable) {

@@ -174,0 +225,0 @@ return (async function * () {

@@ -5,2 +5,9 @@ 'use strict'

const KeytransformDatastore = require('./keytransform')
/**
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('interface-datastore').Query} Query
* @typedef {import('interface-datastore').Options} Options
* @typedef {import('interface-datastore').Batch} Batch
* @typedef {import('./types').KeyTransform} KeyTransform
*/

@@ -17,2 +24,6 @@ /**

class NamespaceDatastore extends KeytransformDatastore {
/**
* @param {Datastore} child
* @param {Key} prefix
*/
constructor (child, prefix) {

@@ -39,3 +50,7 @@ super(child, {

query (q) {
/**
* @param {Query} q
* @param {Options} [options]
*/
query (q, options) {
if (q.prefix && this.prefix.toString() !== '/') {

@@ -46,3 +61,3 @@ return super.query(Object.assign({}, q, {

}
return super.query(q)
return super.query(q, options)
}

@@ -49,0 +64,0 @@ }

@@ -1,44 +0,47 @@

/* @flow */
'use strict'
const Key = require('interface-datastore').Key
const { utf8Decoder } = require('../src/utils')
const { Key, utils: { utf8Decoder } } = require('interface-datastore')
const readme = require('./shard-readme')
// eslint-disable-next-line
/*:: import type {Datastore, Callback} from 'interface-datastore'
/**
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('./types').Shard} Shard
*/
export interface ShardV1 {
name: string;
param: number;
fun(string): string;
toString(): string;
}
*/
const PREFIX = '/repo/flatfs/shard/'
const SHARDING_FN = 'SHARDING'
const README_FN = '_README'
const PREFIX = exports.PREFIX = '/repo/flatfs/shard/'
const SHARDING_FN = exports.SHARDING_FN = 'SHARDING'
exports.README_FN = '_README'
class Shard {
/* :: name: string */
/* :: param: number */
/* :: _padding: string */
constructor (param /* : number */) {
/**
* @implements {Shard}
*/
class ShardBase {
/**
* @param {any} param
*/
constructor (param) {
this.param = param
this.name = 'base'
this._padding = ''
}
fun (str /* : string */) /* : string */ {
throw new Error('implement me')
/**
* @param {string} s
*/
fun (s) {
return 'implement me'
}
toString () /* : string */ {
toString () {
return `${PREFIX}v1/${this.name}/${this.param}`
}
}
class Prefix extends Shard {
constructor (prefixLen /* : number */) {
/**
* @implements {Shard}
*/
class Prefix extends ShardBase {
/**
* @param {number} prefixLen
*/
constructor (prefixLen) {
super(prefixLen)

@@ -49,3 +52,6 @@ this._padding = ''.padStart(prefixLen, '_')

fun (noslash /* : string */) /* : string */ {
/**
* @param {string} noslash
*/
fun (noslash) {
return (noslash + this._padding).slice(0, this.param)

@@ -55,4 +61,7 @@ }

class Suffix extends Shard {
constructor (suffixLen /* : number */) {
class Suffix extends ShardBase {
/**
* @param {number} suffixLen
*/
constructor (suffixLen) {
super(suffixLen)

@@ -63,3 +72,6 @@ this._padding = ''.padStart(suffixLen, '_')

fun (noslash /* : string */) /* : string */ {
/**
* @param {string} noslash
*/
fun (noslash) {
const s = this._padding + noslash

@@ -70,4 +82,7 @@ return s.slice(s.length - this.param)

class NextToLast extends Shard {
constructor (suffixLen /* : number */) {
class NextToLast extends ShardBase {
/**
* @param {number} suffixLen
*/
constructor (suffixLen) {
super(suffixLen)

@@ -78,3 +93,6 @@ this._padding = ''.padStart(suffixLen + 1, '_')

fun (noslash /* : string */) /* : string */ {
/**
* @param {string} noslash
*/
fun (noslash) {
const s = this._padding + noslash

@@ -90,5 +108,5 @@ const offset = s.length - this.param - 1

* @param {string} str
* @returns {ShardV1}
* @returns {Shard}
*/
function parseShardFun (str /* : string */) {
function parseShardFun (str) {
str = str.trim()

@@ -131,4 +149,9 @@

exports.readShardFun = async (path /* : string */, store) /* : Promise<ShardV1> */ => {
/**
* @param {string | Uint8Array} path
* @param {Datastore} store
*/
const readShardFun = async (path, store) => {
const key = new Key(path).child(new Key(SHARDING_FN))
// @ts-ignore
const get = typeof store.getRaw === 'function' ? store.getRaw.bind(store) : store.get.bind(store)

@@ -139,6 +162,12 @@ const res = await get(key)

exports.readme = readme
exports.parseShardFun = parseShardFun
exports.Prefix = Prefix
exports.Suffix = Suffix
exports.NextToLast = NextToLast
module.exports = {
readme,
parseShardFun,
readShardFun,
Prefix,
Suffix,
NextToLast,
README_FN,
SHARDING_FN,
PREFIX
}
'use strict'
const { Adapter, Key } = require('interface-datastore')
const { Adapter, Key, utils: { utf8Encoder } } = require('interface-datastore')
const sh = require('./shard')
const KeytransformStore = require('./keytransform')
const { utf8Encoder } = require('../src/utils')
const shardKey = new Key(sh.SHARDING_FN)
const shardReadmeKey = new Key(sh.README_FN)
/**
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('interface-datastore').Options} Options
* @typedef {import('interface-datastore').Batch} Batch
* @typedef {import('interface-datastore').Query} Query
* @typedef {import('interface-datastore').Pair} Pair
* @typedef {import('./types').Shard} Shard
*
*/
/**
* @template TValue
* @typedef {import('./types').Await<TValue> } Await
*/

@@ -18,2 +30,6 @@ /**

class ShardingDatastore extends Adapter {
/**
* @param {Datastore} store
* @param {Shard} shard
*/
constructor (store, shard) {

@@ -29,6 +45,11 @@ super()

open () {
return this.child.open()
async open () {
await this.child.open()
this.shard = await ShardingDatastore.create(this.child, this.shard)
}
/**
* @param {Key} key
*/
_convertKey (key) {

@@ -44,2 +65,5 @@ const s = key.toString()

/**
* @param {Key} key
*/
_invertKey (key) {

@@ -53,2 +77,7 @@ const s = key.toString()

/**
* @deprecated
* @param {Datastore} store
* @param {Shard} shard
*/
static async createOrOpen (store, shard) {

@@ -63,2 +92,6 @@ try {

/**
* @deprecated
* @param {Datastore} store
*/
static async open (store) {

@@ -69,17 +102,34 @@ const shard = await sh.readShardFun('/', store)

/**
* @param {Datastore} store
* @param {Shard} shard
*/
static async create (store, shard) {
const exists = await store.has(shardKey)
if (!exists) {
const hasShard = await store.has(shardKey)
if (!hasShard) {
// @ts-ignore i have no idea what putRaw is or saw any implementation
const put = typeof store.putRaw === 'function' ? store.putRaw.bind(store) : store.put.bind(store)
return Promise.all([put(shardKey, utf8Encoder.encode(shard.toString() + '\n')),
put(shardReadmeKey, utf8Encoder.encode(sh.readme))])
await Promise.all([
put(shardKey, utf8Encoder.encode(shard.toString() + '\n')),
put(shardReadmeKey, utf8Encoder.encode(sh.readme))
])
return shard
}
// test shards
const diskShard = await sh.readShardFun('/', store)
const a = (diskShard || '').toString()
const b = shard.toString()
if (a !== b) throw new Error(`specified fun ${b} does not match repo shard fun ${a}`)
throw new Error('datastore exists')
if (a !== b) {
throw new Error(`specified fun ${b} does not match repo shard fun ${a}`)
}
return diskShard
}
/**
* @param {Key} key
* @param {Uint8Array} val
* @param {Options} [options]
*/
put (key, val, options) {

@@ -89,2 +139,6 @@ return this.child.put(key, val, options)

/**
* @param {Key} key
* @param {Options} [options]
*/
get (key, options) {

@@ -94,2 +148,6 @@ return this.child.get(key, options)

/**
* @param {Key} key
* @param {Options} [options]
*/
has (key, options) {

@@ -99,2 +157,6 @@ return this.child.has(key, options)

/**
* @param {Key} key
* @param {Options} [options]
*/
delete (key, options) {

@@ -108,2 +170,6 @@ return this.child.delete(key, options)

/**
* @param {Query} q
* @param {Options} [options]
*/
query (q, options) {

@@ -114,4 +180,8 @@ const tq = {

limit: q.limit,
/** @type Array<(items: Pair[]) => Await<Pair[]>> */
orders: [],
filters: [
/** @type {(item: Pair) => boolean} */
e => e.key.toString() !== shardKey.toString(),
/** @type {(item: Pair) => boolean} */
e => e.key.toString() !== shardReadmeKey.toString()

@@ -121,5 +191,6 @@ ]

if (q.prefix != null) {
const { prefix } = q
if (prefix != null) {
tq.filters.push((e) => {
return this._invertKey(e.key).toString().startsWith(q.prefix)
return this._invertKey(e.key).toString().startsWith(prefix)
})

@@ -129,2 +200,3 @@ }

if (q.filters != null) {
// @ts-ignore - can't find a way to easily type this
const filters = q.filters.map((f) => (e) => {

@@ -131,0 +203,0 @@ return f(Object.assign({}, e, {

@@ -5,2 +5,9 @@ 'use strict'

const log = require('debug')('datastore:core:tiered')
/**
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('interface-datastore').Options} Options
* @typedef {import('interface-datastore').Batch} Batch
* @typedef {import('interface-datastore').Query} Query
* @typedef {import('interface-datastore').Key} Key
*/

@@ -15,2 +22,5 @@ /**

class TieredDatastore extends Adapter {
/**
* @param {Datastore[]} stores
*/
constructor (stores) {

@@ -30,2 +40,6 @@ super()

/**
* @param {Key} key
* @param {Uint8Array} value
*/
async put (key, value) {

@@ -39,2 +53,6 @@ try {

/**
* @param {Key} key
* @param {Options} [options]
*/
async get (key, options) {

@@ -52,2 +70,6 @@ for (const store of this.stores) {

/**
* @param {Key} key
* @param {Options} [options]
*/
async has (key, options) {

@@ -63,2 +85,6 @@ for (const s of this.stores) {

/**
* @param {Key} key
* @param {Options} [options]
*/
async delete (key, options) {

@@ -76,2 +102,5 @@ try {

/**
* @returns {Batch}
*/
batch () {

@@ -95,2 +124,6 @@ const batches = this.stores.map(store => store.batch())

/**
* @param {Query} q
* @param {Options} [options]
*/
query (q, options) {

@@ -97,0 +130,0 @@ return this.stores[this.stores.length - 1].query(q, options)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc