This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
66 lines (56 loc) · 1.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict'
const toUrlString = require('ipfs-core-utils/src/to-url-string')
const loadServices = require('./utils/load-services')
const { grpc } = require('@improbable-eng/grpc-web')
/**
* @typedef {import('./types').Options} Options
*/
const service = loadServices()
/** @type {Record<string, string>} */
const protocols = {
'ws://': 'http://',
'wss://': 'https://'
}
/**
* @param {{ url: string }} opts
*/
function normaliseUrls (opts) {
Object.keys(protocols).forEach(protocol => {
if (opts.url.startsWith(protocol)) {
opts.url = protocols[protocol] + opts.url.substring(protocol.length)
}
})
}
/**
* @param {Options} [opts]
*/
function create (opts = { url: '' }) {
const options = {
...opts,
url: toUrlString(opts.url)
}
// @improbable-eng/grpc-web requires http:// protocol URLs, not ws://
normaliseUrls(options)
const client = {
// @ts-ignore - TODO: fix after https://github.com/ipfs/js-ipfs/issues/3594
addAll: require('./core-api/add-all')(grpc, service.Root.add, options),
// @ts-ignore - TODO: fix after https://github.com/ipfs/js-ipfs/issues/3594
id: require('./core-api/id')(grpc, service.Root.id, options),
files: {
// @ts-ignore - TODO: fix after https://github.com/ipfs/js-ipfs/issues/3594
ls: require('./core-api/files/ls')(grpc, service.MFS.ls, options),
// @ts-ignore - TODO: fix after https://github.com/ipfs/js-ipfs/issues/3594
write: require('./core-api/files/write')(grpc, service.MFS.write, options)
},
pubsub: {
// @ts-ignore - TODO: fix after https://github.com/ipfs/js-ipfs/issues/3594
subscribe: require('./core-api/pubsub/subscribe')(grpc, service.PubSub.subscribe, options),
// @ts-ignore - TODO: fix after https://github.com/ipfs/js-ipfs/issues/3594
unsubscribe: require('./core-api/pubsub/unsubscribe')(grpc, service.PubSub.unsubscribe, options)
}
}
return client
}
module.exports = {
create
}