Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: ci
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx aegir lint
- uses: gozala/[email protected]
- run: npx aegir build --no-bundle
- run: npx aegir dep-check
test-node:
needs: check
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node: [14, 16]
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm run pretest
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
- uses: codecov/codecov-action@v1
test-electron-main:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npm run pretest
- run: npx xvfb-maybe aegir test -t electron-main --bail -f dist/cjs/node-test/*js
test-electron-renderer:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npm run pretest
- run: npx xvfb-maybe aegir test -t electron-renderer --bail -f dist/cjs/browser-test/*js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ typings/
# while testing npm5
package-lock.json
yarn.lock
types
33 changes: 0 additions & 33 deletions .travis.yml

This file was deleted.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@
## Usage

```js
const DatastorePubsub = require('datastore-pubsub')
import { PubsubDatastore } from 'datastore-pubsub'

const dsPubsub = new DatastorePubsub(pubsub, datastore, peerId, validator)
const dsPubsub = new PubsubDatastore(pubsub, datastore, peerId, validator)
```

## API

#### Setup

```js
new DatastorePubsub(pubsub, datastore, peerId, validator, subscriptionKeyFn)
new PubsubDatastore(pubsub, datastore, peerId, validator, subscriptionKeyFn)
```

Creates a DatastorePubsub instance.
Expand Down
43 changes: 26 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@
"description": "Responsible for providing an interface-datastore compliant api to pubsub",
"leadMaintainer": "Vasco Santos <[email protected]>",
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"type": "module",
"types": "types/src/index.d.ts",
"files": [
"*",
"!**/*.tsbuildinfo"
],
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"scripts": {
"prepare": "npm run build",
"build": "aegir build --no-bundle",
"clean": "rimraf dist types",
"prepare": "aegir build --no-bundle && cp -R types dist",
"lint": "aegir ts -p check && aegir lint",
"release": "aegir release --target node",
"release-minor": "aegir release --target node --type minor",
"release-major": "aegir release --target node --type major",
"test": "aegir test -t node",
"test:node": "aegir test -t node"
"build": "aegir build",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"pretest": "aegir build --esm-tests",
"test": "aegir test",
"dep-check": "aegir dep-check -i rimraf"
},
"pre-push": [
"lint"
],
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/js-datastore-pubsub.git"
Expand All @@ -27,20 +37,17 @@
"datastore",
"pubsub"
],
"files": [
"dist",
"src"
],
"author": "Vasco Santos <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/ipfs/js-datastore-pubsub/issues"
},
"homepage": "https://github.com/ipfs/js-datastore-pubsub#readme",
"dependencies": {
"datastore-core": "^6.0.7",
"debug": "^4.2.0",
"err-code": "^3.0.1",
"interface-datastore": "^5.1.1",
"interface-datastore": "^6.0.2",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
Expand All @@ -55,7 +62,9 @@
"libp2p-record": "^0.10.0",
"p-wait-for": "^3.1.0",
"peer-id": "^0.15.0",
"sinon": "^11.1.1"
"rimraf": "^3.0.2",
"sinon": "^11.1.1",
"util": "^0.12.4"
},
"contributors": [
"Vasco Santos <[email protected]>",
Expand Down
17 changes: 7 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'
import { Key } from 'interface-datastore'
import { BaseDatastore } from 'datastore-core'
import { encodeBase32, keyToTopic, topicToKey } from './utils.js'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import errcode from 'err-code'
import debug from 'debug'

const { Key, Adapter } = require('interface-datastore')
const { encodeBase32, keyToTopic, topicToKey } = require('./utils')
const { equals: uint8ArrayEquals } = require('uint8arrays/equals')

const errcode = require('err-code')
const debug = require('debug')
const log = Object.assign(debug('datastore-pubsub:publisher'), {
error: debug('datastore-pubsub:publisher:error')
})
Expand All @@ -19,7 +18,7 @@ const log = Object.assign(debug('datastore-pubsub:publisher'), {

// DatastorePubsub is responsible for providing an api for pubsub to be used as a datastore with
// [TieredDatastore]{@link https://github.com/ipfs/js-datastore-core/blob/master/src/tiered.js}
class DatastorePubsub extends Adapter {
export class PubsubDatastore extends BaseDatastore {
/**
* Creates an instance of DatastorePubsub.
*
Expand Down Expand Up @@ -314,5 +313,3 @@ class DatastorePubsub extends Adapter {
log(`record for ${keyToTopic(key)} was stored in the datastore`)
}
}

exports = module.exports = DatastorePubsub
2 changes: 1 addition & 1 deletion src/types.d.ts → src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValidateFn, SelectFn } from 'libp2p-interfaces/src/types'
import type { ValidateFn, SelectFn } from 'libp2p-interfaces/src/types'

export interface SubscriptionKeyFn { (key: Uint8Array): Promise<Uint8Array> | Uint8Array }
export interface Validator {
Expand Down
20 changes: 6 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const errcode = require('err-code')
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
import errcode from 'err-code'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'

/**
* @typedef {import('interface-datastore').Key} Key
Expand All @@ -13,7 +11,7 @@ const namespace = '/record/'
/**
* @param {Uint8Array} buf
*/
function encodeBase32 (buf) {
export function encodeBase32 (buf) {
return uint8ArrayToString(buf, 'base32')
}

Expand All @@ -22,7 +20,7 @@ function encodeBase32 (buf) {
*
* @param {Uint8Array | string} key
*/
function keyToTopic (key) {
export function keyToTopic (key) {
// Record-store keys are arbitrary binary. However, pubsub requires UTF-8 string topic IDs
// Encodes to "/record/base64url(key)"
if (typeof key === 'string' || key instanceof String) {
Expand All @@ -39,7 +37,7 @@ function keyToTopic (key) {
*
* @param {string} topic
*/
function topicToKey (topic) {
export function topicToKey (topic) {
if (topic.substring(0, namespace.length) !== namespace) {
throw errcode(new Error('topic received is not from a record'), 'ERR_TOPIC_IS_NOT_FROM_RECORD_NAMESPACE')
}
Expand All @@ -48,9 +46,3 @@ function topicToKey (topic) {

return uint8ArrayFromString(key, 'base64url')
}

module.exports = {
encodeBase32,
keyToTopic,
topicToKey
}
Loading