Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
peers: expose('ipfs.pubsub.peers', pre(
opts.pre('pubsub.peers'),
(...args) => getIpfs().pubsub.peers(...args)
), opts),
ls: expose('ipfs.pubsub.ls', pre(
// FIXME: The interface-ipfs-core tests call ls straight after unsubscribe.
// Unsubscribe in js-ipfs is synchronous but it HAS to be async in the
// proxy because window.postMessage is asynchronous.
(...args) => new Promise((resolve) => setTimeout(() => resolve(args))),
opts.pre('pubsub.ls'),
(...args) => getIpfs().pubsub.ls(...args)
), opts)
}
// Clean up any subscriptions on close
api.subscribe.close = pre(
(...args) => {
subs.forEach((sub) => getIpfs().pubsub.unsubscribe(sub.topic, sub.rpc.stubFn))
return args
},
api.subscribe.close
)
return api
}
export default function (getIpfs, opts) {
return {
mv: expose('ipfs.files.mv', pre(
opts.pre('files.mv'),
post(
(...args) => getIpfs().files.mv(...args),
() => null
)
), opts)
}
}
export default function (getIpfs, opts) {
return {
ls: expose('ipfs.files.ls', pre(
opts.pre('files.ls'),
(...args) => getIpfs().files.ls(...args)
), opts)
}
}
export default function (getIpfs, opts) {
return {
cp: expose('ipfs.files.cp', pre(
opts.pre('files.cp'),
post(
(...args) => getIpfs().files.cp(...args),
() => null
)
), opts)
}
}
export default function (getIpfs, opts) {
return {
rm: expose('ipfs.files.rm', pre(
opts.pre('files.rm'),
post(
(...args) => getIpfs().files.rm(...args),
() => null
)
), opts)
}
}
export default function (getIpfs, opts) {
return {
mkdir: expose('ipfs.files.mkdir', pre(
opts.pre('files.mkdir'),
post(
(...args) => getIpfs().files.mkdir(...args),
() => null
)
), opts)
}
}
subscribe: function (topic, options, handler, cb) {
let sub
if (typeof options === 'function') {
cb = handler
handler = options
options = {}
}
const stub = pre(
(...args) => {
const handlerIndex = args.length === 3 ? 2 : 1
const fnName = `ipfs.pubsub.subscribe.handler.${shortid()}`
sub = {
topic,
handler,
rpc: {
fnName,
exposedFn: expose(fnName, pre(
(...args) => {
if (args[0]) {
if (isBufferJson(args[0].data)) {
args[0].data = bufferFromJson(args[0].data)
}
sub.rpc.exposedFn.close()
subs.splice(subs.indexOf(sub), 1)
throw err
})
}
)
if (cb) {
stub(topic, options, handler)
.then((res) => process.nextTick(() => cb(null, res)))
.catch((err) => process.nextTick(() => cb(err)))
} else {
return stub(topic, options, handler)
}
},
unsubscribe: pre(
(...args) => {
const topic = args[0]
const sub = subs.find((s) => s.topic === topic && s.handler === args[1])
if (sub) {
args[1] = functionToJson(sub.rpc.fnName)
sub.rpc.exposedFn.close()
subs.splice(subs.indexOf(sub), 1)
}
return args
},
caller('ipfs.pubsub.unsubscribe', opts)
),
peers: callbackify.variadic(caller('ipfs.pubsub.peers', opts)),
ls: callbackify.variadic(caller('ipfs.pubsub.ls', opts)),
(...args) => {
const topic = args[0]
const handlerIndex = args.length === 3 ? 2 : 1
if (isFunctionJson(args[handlerIndex])) {
const stubFn = pre(
(...args) => {
if (args[0]) {
args[0] = Object.assign({}, args[0])
if (isBuffer(args[0].data)) {
args[0].data = bufferToJson(args[0].data)
}
if (isBuffer(args[0].seqno)) {
args[0].seqno = bufferToJson(args[0].seqno)
}
}
return args
},
caller(args[handlerIndex].name, opts)