Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit 7000f5c

Browse files
authored
Merge pull request #881 from 3box/feat/iframe-cache
feat: iframe shared ipfs and orbitdb cache/repo
2 parents 6bc9433 + 025f0ba commit 7000f5c

File tree

5 files changed

+105
-33
lines changed

5 files changed

+105
-33
lines changed

package-lock.json

Lines changed: 55 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"homepage": "https://github.com/3box/3box-js#readme",
4646
"dependencies": {
4747
"3box-orbitdb-plugins": "^2.1.2",
48+
"3box-shared-cache": "^1.1.0",
4849
"3id-blockchain-utils": "^0.4.0",
4950
"3id-connect": "0.1.0",
5051
"3id-resolver": "^1.0.0",
@@ -63,6 +64,7 @@
6364
"ipfs-repo": "^3.0.2",
6465
"is-ipfs": "^1.0.3",
6566
"js-sha256": "^0.9.0",
67+
"levelup": "^4.4.0",
6668
"libp2p-pubsub": "^0.4.6",
6769
"lodash.merge": "^4.6.2",
6870
"muport-did-resolver": "^1.0.2",

src/3box.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ const IPFSRepo = require('ipfs-repo')
1717
const LevelStore = require('datastore-level')
1818
const didJWT = require('did-jwt')
1919
const { ThreeIdConnect } = require('3id-connect')
20+
const { IframeCache } = require('3box-shared-cache')
2021

2122
const PINNING_NODE = config.pinning_node
2223
const ADDRESS_SERVER_URL = config.address_server_url
2324
const IPFS_OPTIONS = config.ipfs_options
2425
const RENDEZVOUS_ADDRESS = config.rendezvous_address
25-
const IFRAME_STORE_URL = 'https://connect.3box.io/v1/index.html'
26+
const THREEID_CONNECT_URL = config.threeid_connect_url
27+
const IFRAME_CACHE_URL = config.iframe_cache_url
2628
const supportAlert = 'This site uses local data storage to give you control of your data. Please enable web APIs like localstorage, indexxeddb, etc in your browser settings.'
2729

28-
let globalIPFS, globalIPFSPromise, threeIdConnect
30+
let globalIPFS, globalIPFSPromise, threeIdConnect, iframeCacheService, cacheSupportedPromise
2931

3032
const browserHuh = typeof window !== 'undefined' && typeof document !== 'undefined'
31-
if (browserHuh) require('./modernizr.js')
32-
if (browserHuh) threeIdConnect = new ThreeIdConnect(IFRAME_STORE_URL)
33+
if (browserHuh) {
34+
require('./modernizr.js')
35+
threeIdConnect = new ThreeIdConnect(THREEID_CONNECT_URL)
36+
iframeCacheService = new IframeCache(IFRAME_CACHE_URL)
37+
cacheSupportedPromise = iframeCacheService.connect()
38+
}
3339
/**
3440
* @extends BoxApi
3541
*/
@@ -68,7 +74,17 @@ class Box extends BoxApi {
6874
}
6975

7076
async _init (opts) {
71-
this.replicator = await Replicator.create(this._ipfs, opts)
77+
const replicatorOpts = { ...opts }
78+
79+
if (opts.iframeCache && iframeCacheService) {
80+
const cacheSupported = await cacheSupportedPromise
81+
82+
if (cacheSupported) {
83+
replicatorOpts.cacheProxy = iframeCacheService.getOrbitStorageProxyFactory()
84+
}
85+
}
86+
87+
this.replicator = await Replicator.create(this._ipfs, replicatorOpts)
7288

7389
if (opts.ghostPinbot) {
7490
this._ghostPinbot = opts.ghostPinbot
@@ -125,10 +141,12 @@ class Box extends BoxApi {
125141
* @param {String} opts.addressServer URL of the Address Server
126142
* @param {String} opts.ghostPinbot MultiAddress of a Ghost Pinbot node
127143
* @param {String} opts.supportCheck Gives browser alert if 3boxjs/ipfs not supported in browser env, defaults to true. You can also set to false to implement your own alert and call Box.support to check if supported.
144+
* @param {Boolean} opts.iframeCache Enable iframe cache for ipfs/orbit, defaults to true
128145
* @return {Box} the 3Box session instance
129146
*/
130147
static async create (provider, opts = {}) {
131148
if (opts.supportCheck !== false && browserHuh) await this._supportAlert()
149+
opts.iframeCache = typeof opts.iframeCache === 'boolean' ? opts.iframeCache : true
132150
const ipfs = await Box.getIPFS(opts)
133151
const box = new Box(provider, ipfs, opts)
134152
await box._setProvider(provider)
@@ -570,7 +588,7 @@ class Box extends BoxApi {
570588
}
571589

572590
if (!globalIPFS && !globalIPFSPromise) {
573-
globalIPFSPromise = initIPFS(opts.ipfs, opts.iframeStore, opts.ipfsOptions)
591+
globalIPFSPromise = initIPFS(opts.ipfs, opts.iframeStore, opts.ipfsOptions, opts.iframeCache)
574592
}
575593
if (browserHuh) window.globalIPFSPromise = globalIPFSPromise
576594

@@ -590,7 +608,7 @@ class Box extends BoxApi {
590608
}
591609
}
592610

593-
function initIPFSRepo () {
611+
async function initIPFSRepo (iframeCache) {
594612
let repoOpts = {}
595613
let ipfsRootPath
596614

@@ -599,7 +617,19 @@ function initIPFSRepo () {
599617
const sessionID = utils.randInt(10000)
600618
ipfsRootPath = 'ipfs/root/' + sessionID
601619
const levelInstance = new LevelStore(ipfsRootPath)
602-
repoOpts = { storageBackends: { root: () => levelInstance } }
620+
621+
repoOpts = {
622+
storageBackends: {
623+
root: () => levelInstance
624+
}
625+
}
626+
627+
if (iframeCache && iframeCacheService) {
628+
const cacheSupported = await cacheSupportedPromise
629+
if (cacheSupported) {
630+
repoOpts.storageBackends.blocks = iframeCacheService.getIpfsStorageProxy()
631+
}
632+
}
603633
}
604634

605635
const repo = new IPFSRepo('ipfs', repoOpts)
@@ -610,17 +640,16 @@ function initIPFSRepo () {
610640
}
611641
}
612642

613-
async function initIPFS (ipfs, iframeStore, ipfsOptions) {
614-
// if (!ipfs && !ipfsProxy) throw new Error('No IPFS object configured and no default available for environment')
615-
if (!!ipfs && iframeStore) console.warn('Warning: iframeStore true, orbit db cache in iframe, but the given ipfs object is being used, and may not be running in same iframe.')
643+
async function initIPFS (ipfs, iframeStore, ipfsOptions, iframeCache) {
644+
if (!!ipfs && iframeCache) console.warn('Warning: Caching in iframe is true, but the given ipfs object that is being used may not be using the iframe cache')
616645
if (ipfs) {
617646
return ipfs
618647
} else {
619-
// await iframeLoadedPromise
620648
// return ipfsProxy
621649
let ipfsRepo
622650
if (!ipfsOptions) {
623-
ipfsRepo = initIPFSRepo()
651+
if (ipfsOptions && iframeCache) console.warn('Warning: Caching in iframe is true, but received ipfs options which may not include the proper repo configuration for using the iframe cache')
652+
ipfsRepo = await initIPFSRepo(iframeCache)
624653
ipfsOptions = Object.assign(IPFS_OPTIONS, { repo: ipfsRepo.repo })
625654
}
626655
ipfs = await IPFS.create(ipfsOptions)

src/config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const Log = require('ipfs-log')
2-
const IFRAME_STORE_VERSION = '0.0.3'
32

43
const RENDEZVOUS_DISABLE = !(
54
!process.env.RENDEZVOUS_DISABLE ||
@@ -12,8 +11,8 @@ module.exports = {
1211
// pinning_node: process.env.PINNING_NODE || '/dnsaddr/ipfs-dev.3box.io/tcp/443/wss/ipfs/QmZipMZjcYTjnyk4WQuV1HB5XUM98hBy3MpJmPTsVoMvW8',
1312
pinning_room: process.env.PINNING_ROOM || '3box-pinning',
1413
rendezvous_address: RENDEZVOUS_DISABLE ? '' : (process.env.RENDEZVOUS_ADDRESS || '/dns4/p2p.3box.io/tcp/9091/wss/p2p-webrtc-star/'),
15-
iframe_store_version: process.env.IFRAME_STORE_VERSION || IFRAME_STORE_VERSION,
16-
iframe_store_url: process.env.IFRAME_STORE_URL || `https://iframe.3box.io/${IFRAME_STORE_VERSION}/iframe.html`,
14+
iframe_cache_url: process.env.IFRAME_CACHE_URL || 'https://cache.3box.io',
15+
threeid_connect_url: process.env.THREEID_CONNECT_URL || 'https://connect.3box.io/v1/index.html',
1716
ipfs_options: {
1817
preload: { enabled: false },
1918
config: {

src/replicator.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ class Replicator {
101101
// orbitdb/ipfs id which can change on page load
102102
const cachePath = path.join(opts.orbitPath || './orbitdb', '/cache')
103103
const levelDown = OdbStorage(null, {})
104-
const cacheStorage = await levelDown.createStore(cachePath)
105-
const cache = new OdbCache(cacheStorage)
104+
const cacheProxy = opts.cacheProxy
105+
? await opts.cacheProxy(cachePath)
106+
: await levelDown.createStore(cachePath)
106107

108+
const cache = new OdbCache(cacheProxy)
107109
const keystorePath = path.join(opts.orbitPath || './orbitdb', '/keystore')
108110
const keyStorage = await levelDown.createStore(keystorePath)
109111
const keystore = new OdbKeystore(keyStorage)

0 commit comments

Comments
 (0)