Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: move types around
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Nov 20, 2020
1 parent 2ee0f16 commit 1bdb8ae
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
4 changes: 1 addition & 3 deletions packages/ipfs-grpc-client/src/core-api/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ module.exports = function grpcId (grpc, service, opts = {}) {
opts = opts || {}

async function id (options = {}) {
const request = {
peerId: options.peerId
}
const request = {}

const res = await unaryToPromise(grpc, service, request, {
host: opts.url,
Expand Down
3 changes: 3 additions & 0 deletions packages/ipfs-grpc-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"package.json"
],
"references": [
{
"path": "../ipfs-core"
},
{
"path": "../ipfs-core-utils"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/ipfs-grpc-server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ module.exports = async function createServer (ipfs, options = {}) {

const socket = options.socket || await webSocketServer(ipfs, options)

socket.on('message', async ({ path, metadata, channel }) => {
socket.on('error', (error) => debug(error))

socket.on('data', async ({ path, metadata, channel }) => {
// @ts-ignore
const handler = server.handlers.get(path)

Expand Down
38 changes: 23 additions & 15 deletions packages/ipfs-grpc-server/src/utils/web-socket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const coerce = require('coercer')
const { camelCase } = require('change-case')

/**
* @param {Buffer} buf - e.g. 'fooBar'
* @returns {object} - e.g. 'foo-bar'
* @param {Buffer} buf - e.g. `Buffer.from('foo-bar: baz\r\n')`
* @returns {object} - e.g. `{ foorBar: 'baz' }`
**/
const fromHeaders = (buf) => {
const headers = buf.toString('utf8')
Expand All @@ -27,20 +27,35 @@ const fromHeaders = (buf) => {
return coerce(headers)
}

class Messages extends EventEmitter {
constructor (wss) {
super()

this._wss = wss
this.multiaddr = ''
}

async stop () {
return new Promise((resolve) => {
this._wss.close(() => resolve())
})
}
}

module.exports = async (ipfs, options = {}) => {
const config = await ipfs.config.getAll()
const grpcAddr = config.Addresses.RPC
const [,, host, , port] = grpcAddr.split('/')

debug(`starting ws server on ${host}:${port}`)

const emitter = new EventEmitter()

const wss = new WebSocketServer({
host,
port
})

const messages = new Messages(wss)

wss.on('connection', function connection (ws, request) {
ws.on('error', error => debug(`WebSocket Error: ${error.stack}`))

Expand All @@ -49,28 +64,21 @@ module.exports = async (ipfs, options = {}) => {
const metadata = fromHeaders(buf)
const channel = new WebSocketMessageChannel(ws)

emitter.emit('message', {
messages.emit('data', {
path,
metadata,
channel
})
})
})

wss.on('error', error => debug(`WebSocket Server Error: ${error.stack}`))
wss.on('error', error => messages.emit('error', error))

return new Promise((resolve) => {
wss.on('listening', () => {
// @ts-ignore
emitter.stop = () => {
return new Promise((resolve) => {
wss.close(() => resolve())
})
}
// @ts-ignore
emitter.multiaddr = `/ip4/${wss.address().address}/tcp/${wss.address().port}/ws`
messages.multiaddr = `/ip4/${wss.address().address}/tcp/${wss.address().port}/ws`

resolve(emitter)
resolve(messages)
})
})
}
2 changes: 1 addition & 1 deletion packages/ipfs-grpc-server/test/utils/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = ({ ipfs, options }) => {
send: (path, metadata) => {
const channel = createChannel()

socket.emit('message', { path, metadata, channel })
socket.emit('data', { path, metadata, channel })

return channel
}
Expand Down

0 comments on commit 1bdb8ae

Please sign in to comment.