Skip to content

Commit

Permalink
wip: working standalone hapi v18, wip one from js-ipfs
Browse files Browse the repository at this point in the history
Second checkpoint: I took js-ipfs `0.35.0-pre.0` for a spin.
It ships with [hapi.js](https://github.com/hapijs) v18, HTTP server used
for exposing Gateway.

Done:

- [x] start raw `http` server (`http.createServer`)
- [x] start raw `hapi` server (`Hapi.Server`) (screenshot below)
    - after some troubleshooting I managed to start raw Hapi server
      but got internal error for all responses
- [x] return valid response from  `hapi` server running in browser extension
    - got it to work by replacing `url` with
      [iso-url](https://www.npmjs.com/package/iso-url) during webpack build

Next:

- [ ] start embedded js-ipfs with Gateway exposed by embedded Hapi server
    - right now `new Ipfs(..)` fails due to `TypeError: this._dht.on is
      not a function`,
      it seems libp2p does not disable DHT fully when we polyfill
      `net`/`dgram` using `chrome.sockets`
- [ ] start js-ipfs-http-client
    - does not work due to `TypeError: res.req.getHeaders is not
    a function` – probably `http` API mismatch between expected and one
    provided by chrome.sockets polyfil
  • Loading branch information
lidel committed Feb 27, 2019
1 parent 4f9b383 commit 6ffb723
Show file tree
Hide file tree
Showing 5 changed files with 592 additions and 214 deletions.
80 changes: 46 additions & 34 deletions add-on/src/lib/ipfs-client/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,70 @@ process.hrtime = require('browser-process-hrtime')
const Ipfs = require('ipfs')
const { optionDefaults } = require('../options')
const http = require('http') // courtesy of chrome-net

const Hapi = require('hapi')
const Hapi = require('hapi') // courtesy of js-ipfs

let node = null
let httpServer = null
let hapiServer = null

exports.init = function init (opts) {
console.log('[ipfs-companion] Embedded ipfs init')

node = new Ipfs(
JSON.parse(opts.ipfsNodeConfig || optionDefaults.ipfsNodeConfig)
)

// find first free port and start http server
// BRAVE TESTS FIRST
// TODO: remove after experiments are done
// =======================================
// [x] start raw http server (http.createServer)
// [x] start raw Hapi server (Hapi.Server)
// [x] return response
// [ ] start js-ipfs with Gateway exposed by embedded Hapi server
// - right now fails due to `TypeError: this._dht.on is not a function`,
// but we are on the right track
// =======================================
// TEST RAW require('http') SERVER
if (!httpServer) {
let port = 9091
httpServer = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello from ipfs-companion exposing HTTP via chrome.sockets in Brave :-)\n')
})
httpServer.listen(port, '127.0.0.1')
console.log(`[ipfs-companion] started demo HTTP server on http://127.0.0.1:${port}`)
console.log(`[ipfs-companion] require('http') HTTP server on http://127.0.0.1:${port}`)
}

// find first free port and start http server
// =======================================
// TEST require('hapi') HTTP SERVER (same as in js-ipfs)
if (!hapiServer) {
let port = 9092
let options = {
host: '127.0.0.1',
port,
debug: {
log: ['*'],
request: ['*']
}
}
hapiServer = new Hapi.Server(options)
hapiServer.connection({
host: '127.0.0.1',
port
})

hapiServer.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
console.log('[ipfs-companion] hapiServer processing request', request)
return reply('Hello')
}
})

hapiServer.start((err) => {
if (err) console.error(`[ipfs-companion] Failed to start Hapi`, err)
console.log(`[ipfs-companion] started demo Hapi server on http://127.0.0.1:${port}`)
})
const initHapi = async () => {
// hapi v18 (js-ipfs >=v0.35.0-pre.0)
hapiServer = new Hapi.Server(options)
await hapiServer.route({
method: 'GET',
path: '/',
handler: (request, h) => {
console.log('[ipfs-companion] hapiServer processing request', request)
return 'Hello from ipfs-companion+Hapi.js exposing HTTP via chrome.sockets in Brave :-)'
}
})
// await hapiServer.register({
// })
await hapiServer.start()
console.log(`[ipfs-companion] require('hapi') HTTP server running at: ${hapiServer.info.uri}`)
}
initHapi()
}
// =======================================
// Resume regular startup
console.log('[ipfs-companion] Embedded ipfs init')

node = new Ipfs(
JSON.parse(opts.ipfsNodeConfig || optionDefaults.ipfsNodeConfig)
)

if (node.isOnline()) {
return Promise.resolve(node)
Expand All @@ -80,14 +90,16 @@ exports.destroy = async function () {
httpServer = null
}
if (hapiServer) {
hapiServer.stop({ timeout: 1000 }).then(function (err) {
try {
await hapiServer.stop({ timeout: 1000 })
} catch (err) {
if (err) {
console.error(`[ipfs-companion] failed to stop hapi`, err)
} else {
console.log('[ipfs-companion] hapi server stopped')
}
})
httpServer = null
}
hapiServer = null
}

await node.stop()
Expand Down
6 changes: 2 additions & 4 deletions add-on/src/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

exports.optionDefaults = Object.freeze({
active: true, // global ON/OFF switch, overrides everything else
ipfsNodeType: 'external', // or 'embedded'
ipfsNodeType: 'embedded', // Brave should default to js-ipfs: https://github.com/ipfs-shipyard/ipfs-companion/issues/664
ipfsNodeConfig: JSON.stringify({
config: {
Addresses: {
// Swarm: [],
Swarm: [
],
Swarm: [],
// API: '/ip4/127.0.0.1/tcp/5002',
Gateway: '/ip4/127.0.0.1/tcp/9090'
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"filesize": "3.6.1",
"http-dns": "3.0.1",
"http-node": "1.2.0",
"ipfs": "0.34.0",
"ipfs": "0.35.0-pre.0",
"ipfs-css": "0.12.0",
"ipfs-http-client": "28.1.1",
"ipfs-http-response": "0.2.1",
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const commonConfig = {
resolve: {
extensions: ['.js', '.json'],
alias: {
'url': 'iso-url',
'http': 'http-node',
'dns': 'http-dns',
'dgram': 'chrome-dgram',
Expand Down
Loading

0 comments on commit 6ffb723

Please sign in to comment.