Skip to content

Commit

Permalink
WIP: Prototype of IPFS client wired to worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed May 6, 2020
1 parent 3461121 commit f2994c7
Show file tree
Hide file tree
Showing 5 changed files with 484 additions and 0 deletions.
187 changes: 187 additions & 0 deletions packages/ipfs-message-port-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
{
"name": "ipfs-message-port-client",
"version": "44.0.3",
"description": "A client library for the IPFS across message port",
"keywords": [
"ipfs"
],
"homepage": "https://github.com/ipfs/js-ipfs/tree/master/packages/ipfs-message-port-client#readme",
"bugs": "https://github.com/ipfs/js-ipfs/issues",
"license": "(Apache-2.0 OR MIT)",
"leadMaintainer": "Alex Potsides <[email protected]>",
"files": [
"src",
"dist"
],
"main": "src/index.js",
"browser": {
"./src/lib/to-stream.js": "./src/lib/to-stream.browser.js",
"ipfs-utils/src/files/glob-source": false
},
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/js-ipfs.git"
},
"scripts": {
"test": "cross-env ECHO_SERVER_PORT=37490 aegir test",
"test:node": "cross-env ECHO_SERVER_PORT=37491 aegir test -t node",
"test:browser": "cross-env ECHO_SERVER_PORT=37492 aegir test -t browser",
"test:webworker": "cross-env ECHO_SERVER_PORT=37493 aegir test -t webworker",
"test:electron-main": "cross-env ECHO_SERVER_PORT=37494 aegir test -t electron-main",
"test:electron-renderer": "cross-env ECHO_SERVER_PORT=37495 aegir test -t electron-renderer",
"test:chrome": "cross-env ECHO_SERVER_PORT=37496 aegir test -t browser -t webworker -- --browsers ChromeHeadless",
"test:firefox": "cross-env ECHO_SERVER_PORT=37497 aegir test -t browser -t webworker -- --browsers FirefoxHeadless",
"lint": "aegir lint",
"build": "aegir build",
"coverage": "npx nyc -r html npm run test:node -- --bail",
"clean": "rm -rf ./dist",
"dep-check": "aegir dep-check"
},
"dependencies": {
"abort-controller": "^3.0.0",
"bignumber.js": "^9.0.0",
"bs58": "^4.0.1",
"buffer": "^5.4.2",
"cids": "^0.8.0",
"debug": "^4.1.0",
"form-data": "^3.0.0",
"ipfs-block": "^0.8.1",
"ipfs-core-utils": "^0.2.2",
"ipfs-utils": "^2.2.2",
"ipld-dag-cbor": "^0.15.1",
"ipld-dag-pb": "^0.18.3",
"ipld-raw": "^4.0.1",
"iso-url": "^0.4.7",
"it-tar": "^1.2.1",
"it-to-buffer": "^1.0.0",
"it-to-stream": "^0.1.1",
"merge-options": "^2.0.0",
"multiaddr": "^7.2.1",
"multiaddr-to-uri": "^5.1.0",
"multibase": "^0.7.0",
"multicodec": "^1.0.0",
"multihashes": "^0.4.14",
"nanoid": "^3.0.2",
"node-fetch": "^2.6.0",
"parse-duration": "^0.1.2",
"stream-to-it": "^0.2.0"
},
"devDependencies": {
"aegir": "^21.10.1",
"browser-process-platform": "^0.1.1",
"cross-env": "^7.0.0",
"go-ipfs-dep": "0.4.23-3",
"interface-ipfs-core": "^0.134.3",
"ipfsd-ctl": "^3.0.0",
"it-all": "^1.0.1",
"it-concat": "^1.0.0",
"it-pipe": "^1.1.0",
"nock": "^12.0.3"
},
"engines": {
"node": ">=10.3.0",
"npm": ">=3.0.0"
},
"contributors": [
"Alan Shaw <[email protected]>",
"Alan Shaw <[email protected]>",
"Alex Mingoia <[email protected]>",
"Alex Potsides <[email protected]>",
"Antonio Tenorio-Fornés <[email protected]>",
"Bruno Barbieri <[email protected]>",
"Clemo <[email protected]>",
"Connor Keenan <[email protected]>",
"Daniel Constantin <[email protected]>",
"Danny <[email protected]>",
"David Braun <[email protected]>",
"David Dias <[email protected]>",
"Dietrich Ayala <[email protected]>",
"Diogo Silva <[email protected]>",
"Dmitriy Ryajov <[email protected]>",
"Dmitry Nikulin <[email protected]>",
"Donatas Stundys <[email protected]>",
"Fil <[email protected]>",
"Filip Š <[email protected]>",
"Francisco Baio Dias <[email protected]>",
"Friedel Ziegelmayer <[email protected]>",
"Gar <[email protected]>",
"Gavin McDermott <[email protected]>",
"Gopalakrishna Palem <[email protected]>",
"Greenkeeper <[email protected]>",
"Haad <[email protected]>",
"Harlan T Wood <[email protected]>",
"Harlan T Wood <[email protected]>",
"Henrique Dias <[email protected]>",
"Holodisc <[email protected]>",
"Hugo Dias <[email protected]>",
"Hugo Dias <[email protected]>",
"JGAntunes <[email protected]>",
"Jacob Heun <[email protected]>",
"James Halliday <[email protected]>",
"Jason Carver <[email protected]>",
"Jason Papakostas <[email protected]>",
"Jeff Downie <[email protected]>",
"Jeromy <[email protected]>",
"Jeromy <[email protected]>",
"Jim Pick <[email protected]>",
"Joe Turgeon <[email protected]>",
"Jonathan <[email protected]>",
"Juan Batiz-Benet <[email protected]>",
"Kevin Wang <[email protected]>",
"Kristoffer Ström <[email protected]>",
"Marcin Rataj <[email protected]>",
"Matt Bell <[email protected]>",
"Matt Ober <[email protected]>",
"Maxime Lathuilière <[email protected]>",
"Michael Bradley <[email protected]>",
"Michael Muré <[email protected]>",
"Michael Muré <[email protected]>",
"Mikeal Rogers <[email protected]>",
"Mitar <[email protected]>",
"Mithgol <[email protected]>",
"Mohamed Abdulaziz <[email protected]>",
"Nitin Patel <[email protected]>",
"Nuno Nogueira <[email protected]>",
"Níckolas Goline <[email protected]>",
"Oli Evans <[email protected]>",
"Orie Steele <[email protected]>",
"Paul Cowgill <[email protected]>",
"Pedro Santos <[email protected]>",
"Pedro Santos <[email protected]>",
"Pedro Teixeira <[email protected]>",
"Pete Thomas <[email protected]>",
"Richard Littauer <[email protected]>",
"Richard Schneider <[email protected]>",
"Roman Khafizianov <[email protected]>",
"SeungWon <[email protected]>",
"Stephen Whitmore <[email protected]>",
"Tara Vancil <[email protected]>",
"Teri Chadbourne <[email protected]>",
"Travis Person <[email protected]>",
"Travis Person <[email protected]>",
"Vasco Santos <[email protected]>",
"Vasco Santos <[email protected]>",
"Victor Bjelkholm <[email protected]>",
"Volker Mische <[email protected]>",
"Zhiyuan Lin <[email protected]>",
"dirkmc <[email protected]>",
"dmitriy ryajov <[email protected]>",
"elsehow <[email protected]>",
"ethers <[email protected]>",
"greenkeeper[bot] <23040076+greenkeeper[bot]@users.noreply.github.com>",
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>",
"haad <[email protected]>",
"kumavis <[email protected]>",
"leekt216 <[email protected]>",
"nginnever <[email protected]>",
"noah the goodra <[email protected]>",
"phillmac <[email protected]>",
"priecint <[email protected]>",
"samuli <[email protected]>",
"sarthak khandelwal <[email protected]>",
"shunkin <[email protected]>",
"victorbjelkholm <[email protected]>",
"Łukasz Magiera <[email protected]>",
"Łukasz Magiera <[email protected]>"
]
}
49 changes: 49 additions & 0 deletions packages/ipfs-message-port-client/src/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'

const CID = require('cids')
const normaliseInput = require('ipfs-core-utils/src/files/normalise-input')

class Add {
/**
* @type require("./index").Connection
*/
connection
return async function * add (input, options = {}) {
const {progress, timeout, signal} = options
const entries = normaliseInput(input)
this.connection.fetch({ type: "add", entry })


for await (const {path, content} of entries) {
for await (const chunk of content) {
const buffer = await collectAsBuffer(content)
const response = await this.connection.fetch({ "add", path, content:buffer transfer:[buffer]}, signal)
if (signal && signal.aborted) {
throw new AbortError()
}
yield decode(response.data)
}
}
}
}

const decode ({ name, hash, size, mode, mtime, mtimeNsecs }) {
const output = {
path: name,
cid: new CID(hash),
size: parseInt(size)
}

if (mode != null) {
output.mode = parseInt(mode, 8)
}

if (mtime != null) {
output.mtime = {
secs: mtime,
nsecs: mtimeNsecs || 0
}
}

return output
}
113 changes: 113 additions & 0 deletions packages/ipfs-message-port-client/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict'
/* eslint-env browser */
// @ts-ignore
const { Buffer } = require('buffer')
const CID = require('cids')
const multiaddr = require('multiaddr')
const multibase = require('multibase')
const multicodec = require('multicodec')
const multihash = require('multihashes')
const globSource = require('ipfs-utils/src/files/glob-source')
const urlSource = require('ipfs-utils/src/files/url-source')

const Add = require('./add')

class AbortError extends Error {

}


class Connection {
/** @type {MessagePort} */
port
/** @type {number} */
nextID

/** @type {{[string]: {resolve(any):void, reject(AbortError):void} }} */
pending

/**
*
* @param {MessagePort} port
*/
constructor(port) {
this.port = port
this.nextID = 0
}
/**
*
* @param {Object} request
* @param {AbortSignal} [signal]
* @returns {Promise<any>}
*/
fetch(request, signal) {
return new Promise((resolve, reject) => {
const id = this.nextID++
if (signal) {
if (signal.aborted) {
return reject(new AbortError())
} else {
signal.addEventListener("abort", () => this.cancel(id), { once:true })
}
}

this.pending[id] = {resolve, reject}
this.port.postMessage({
id,
type: "request",
request
}, request.transfer)
})
}
cancel(id) {
const pending = this.pending[id]
if (pending) {
delete this.pending[id]
this.port.postMessage({
id,
type: "abort"
})
pending.reject(new AbortError())
}
}
handleEvent(event) {
const { id, response } = event.data
const pending = this.pending[id]
if (pending) {
delete this.pending[id]
pending.resolve(response)
}
}
start() {
this.port.addEventListener("message", this)
this.port.start()
}
stop() {
this.port.removeEventListener("message", this)
this.port.postMessage({ type: "stop" })
const {pending} = this
for (const id in pending) {
this.cancel(id)
}
}
}

/**
*
*/
class IPFSClient {
/** @type {MessagePort} **/
connection: Connection

/**
* @param {ClientOptions} options
*/
constructor(options) {
this.connection = new Connection(options.port)
}
}
Object.assign(IPFSClient.prototype, Add.prototype)

// Object.assign(ipfsClient, { Buffer, CID, multiaddr, multibase, multicodec, multihash, globSource, urlSource })

module.exports = IPFSClient
18 changes: 18 additions & 0 deletions packages/ipfs-message-port-client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"alwaysStrict": true,
"rootDirs": [
"src"
],
"outDir": "./dist/"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
],
"compileOnSave": false
}
Loading

0 comments on commit f2994c7

Please sign in to comment.