mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-06-12 09:41:37 +00:00
chore: use libp2p-interfaces (#98)
This commit is contained in:
@ -42,7 +42,7 @@
|
|||||||
"class-is": "^1.1.0",
|
"class-is": "^1.1.0",
|
||||||
"debug": "^4.1.1",
|
"debug": "^4.1.1",
|
||||||
"err-code": "^2.0.0",
|
"err-code": "^2.0.0",
|
||||||
"it-ws": "vasco-santos/it-ws#v2.1.1-rc.0",
|
"it-ws": "vasco-santos/it-ws#v2.1.1-rc.1",
|
||||||
"libp2p-utils": "~0.1.0",
|
"libp2p-utils": "~0.1.0",
|
||||||
"mafmt": "^7.0.0",
|
"mafmt": "^7.0.0",
|
||||||
"multiaddr": "^7.1.0",
|
"multiaddr": "^7.1.0",
|
||||||
@ -55,7 +55,7 @@
|
|||||||
"bl": "^4.0.0",
|
"bl": "^4.0.0",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"interface-transport": "^0.7.0",
|
"libp2p-interfaces": "^0.2.0",
|
||||||
"it-goodbye": "^2.0.1",
|
"it-goodbye": "^2.0.1",
|
||||||
"it-pipe": "^1.0.1",
|
"it-pipe": "^1.0.1",
|
||||||
"streaming-iterables": "^4.1.0"
|
"streaming-iterables": "^4.1.0"
|
||||||
|
@ -12,7 +12,7 @@ log.error = debug('libp2p:websockets:socket:error')
|
|||||||
|
|
||||||
// Convert a stream into a MultiaddrConnection
|
// Convert a stream into a MultiaddrConnection
|
||||||
// https://github.com/libp2p/interface-transport#multiaddrconnection
|
// https://github.com/libp2p/interface-transport#multiaddrconnection
|
||||||
module.exports = (socket, options = {}) => {
|
module.exports = (stream, options = {}) => {
|
||||||
const maConn = {
|
const maConn = {
|
||||||
async sink (source) {
|
async sink (source) {
|
||||||
if (options.signal) {
|
if (options.signal) {
|
||||||
@ -20,7 +20,7 @@ module.exports = (socket, options = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await socket.sink((async function * () {
|
await stream.sink((async function * () {
|
||||||
for await (const chunk of source) {
|
for await (const chunk of source) {
|
||||||
// Convert BufferList to Buffer
|
// Convert BufferList to Buffer
|
||||||
yield Buffer.isBuffer(chunk) ? chunk : chunk.slice()
|
yield Buffer.isBuffer(chunk) ? chunk : chunk.slice()
|
||||||
@ -33,15 +33,15 @@ module.exports = (socket, options = {}) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
source: options.signal ? abortable(socket.source, options.signal) : socket.source,
|
source: options.signal ? abortable(stream.source, options.signal) : stream.source,
|
||||||
|
|
||||||
conn: socket,
|
conn: stream,
|
||||||
|
|
||||||
localAddr: options.localAddr || (socket.localAddress && socket.localPort
|
localAddr: options.localAddr || (stream.localAddress && stream.localPort
|
||||||
? toMultiaddr(socket.localAddress, socket.localPort) : undefined),
|
? toMultiaddr(stream.localAddress, stream.localPort) : undefined),
|
||||||
|
|
||||||
// If the remote address was passed, use it - it may have the peer ID encapsulated
|
// If the remote address was passed, use it - it may have the peer ID encapsulated
|
||||||
remoteAddr: options.remoteAddr || toMultiaddr(socket.remoteAddress, socket.remotePort),
|
remoteAddr: options.remoteAddr || toMultiaddr(stream.remoteAddress, stream.remotePort),
|
||||||
|
|
||||||
timeline: { open: Date.now() },
|
timeline: { open: Date.now() },
|
||||||
|
|
||||||
@ -49,18 +49,27 @@ module.exports = (socket, options = {}) => {
|
|||||||
const start = Date.now()
|
const start = Date.now()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await pTimeout(socket.close(), CLOSE_TIMEOUT)
|
await pTimeout(stream.close(), CLOSE_TIMEOUT)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const { host, port } = maConn.remoteAddr.toOptions()
|
const { host, port } = maConn.remoteAddr.toOptions()
|
||||||
log('timeout closing socket to %s:%s after %dms, destroying it manually',
|
log('timeout closing stream to %s:%s after %dms, destroying it manually',
|
||||||
host, port, Date.now() - start)
|
host, port, Date.now() - start)
|
||||||
|
|
||||||
socket.destroy()
|
stream.destroy()
|
||||||
} finally {
|
} finally {
|
||||||
maConn.timeline.close = Date.now()
|
maConn.timeline.close = Date.now()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stream.socket.once && stream.socket.once('close', () => {
|
||||||
|
// In instances where `close` was not explicitly called,
|
||||||
|
// such as an iterable stream ending, ensure we have set the close
|
||||||
|
// timeline
|
||||||
|
if (!maConn.timeline.close) {
|
||||||
|
maConn.timeline.close = Date.now()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return maConn
|
return maConn
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const tests = require('interface-transport')
|
const tests = require('libp2p-interfaces/src/transport/tests')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
const WS = require('../src')
|
const WS = require('../src')
|
||||||
|
Reference in New Issue
Block a user