Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make client work in the browser #76

Merged
merged 10 commits into from
Oct 23, 2021
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
"homepage": "https://github.com/cabal-club/cabal-client",
"bugs": "https://github.com/cabal-club/cabal-client/issues",
"main": "index.js",
"browser": {
"mkdirp": false,
"random-access-file": "random-access-web"
},
"scripts": {
"test": "tape test/*.js",
"lint": "standard",
"doc": "jsdoc2md --files src/client.js src/cabal-details.js --param-list-format list --separators --partial doc-gen/scope.hbs --helper doc-gen/helpers.js > api.md"
"doc": "jsdoc2md --files src/client.js src/cabal-details.js --param-list-format list --separators --partial doc-gen/scope.hbs --helper doc-gen/helpers.js > api.md",
"build": "browserify boop.js > bundle.js"
cblgh marked this conversation as resolved.
Show resolved Hide resolved
},
"keywords": [],
"dependencies": {
"cabal-core": "^13.2.0",
"cabal-core": "^14.0.0",
"collect-stream": "^1.2.1",
"dat-dns": "^4.1.2",
"debug": "^4.1.1",
Expand All @@ -25,7 +30,8 @@
"memdb": "^1.3.1",
"mkdirp": "^1.0.4",
"monotonic-timestamp": "0.0.9",
"paperslip": "^3.0.0",
"random-access-file": "^2.2.0",
"paperslip": "^3.1.0",
"pump": "^3.0.0",
"qrcode": "^1.4.4",
"random-access-memory": "^3.1.1",
Expand Down
24 changes: 17 additions & 7 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const memdb = require('memdb')
const level = require('level')
const path = require('path')
const mkdirp = require('mkdirp')
const raf = require("random-access-file") // note! raf is replaced with random-access-web if cabal-client is browserified
const os = require('os')
const defaultCommands = require('./commands')
const paperslip = require("paperslip")
Expand All @@ -34,6 +35,8 @@ class Client {
config: {
temp: true,
dbdir: null,
storage: null,
swarm: null, // typically null or passed hyperswarm-web options
preferredPort: 0 // use cabal-core's default port
}
}
Expand Down Expand Up @@ -132,6 +135,9 @@ class Client {
stream.once('error', (err) => { reject(err) })
})
} else {
if (Cabal.isHypercoreKey(Client.scrubKey(name))) {
return Promise.resolve(name)
}
return this.cabalDns.resolveName(name).then((key) => {
if (key === null) return null
if (!cb) return key
Expand All @@ -144,9 +150,10 @@ class Client {
* Create a new cabal.
* @returns {Promise} a promise that resolves into a `CabalDetails` instance.
*/
createCabal (cb) {
createCabal (cb, opts) {
opts = opts || {}
const key = Client.generateKey()
return this.addCabal(key, cb)
return this.addCabal(key, opts, cb)
}

/**
Expand All @@ -173,15 +180,18 @@ class Client {
cabalPromise = this.resolveName(key.trim()).then((resolvedKey) => {
// discard uri scheme and search params of cabal key, if present. returns 64 chr hex string
const scrubbedKey = Client.scrubKey(resolvedKey)
// TODO: export cabal-core's isHypercoreKey() and use here & verify that scrubbedKey is 64 ch hex string
if (resolvedKey === null) {

// verify that scrubbedKey is 64 ch hex string
if (resolvedKey === null || !Cabal.isHypercoreKey(scrubbedKey)) {
dnsFailed = true
return
}
let { temp, dbdir, preferredPort } = this.config

let { temp, dbdir, preferredPort, storage } = this.config
preferredPort = preferredPort || 0
dbdir = dbdir || path.join(Client.getCabalDirectory(), 'archives')
const storage = temp ? ram : path.join(dbdir, scrubbedKey)
// if opts.config.storage passed in, use it. otherwise use decent defaults
storage = storage || temp ? ram : raf(path.join(dbdir, scrubbedKey))
if (!temp) try { mkdirp.sync(path.join(dbdir, scrubbedKey, 'views')) } catch (e) {}
var db = temp ? memdb() : level(path.join(dbdir, scrubbedKey, 'views'))

Expand Down Expand Up @@ -216,7 +226,7 @@ class Client {
aliases: this.aliases
}, done)
this.cabals.set(cabal, details)
if (!opts.noSwarm) cabal.swarm()
if (!opts.noSwarm) cabal.swarm(this.config.swarm)
function done () {
details._emitUpdate('init')
cb()
Expand Down