在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ipfs-shipyard/ipfs-provider开源软件地址:https://github.com/ipfs-shipyard/ipfs-provider开源编程语言:JavaScript 100.0%开源软件介绍:ipfs-provider
Installvia NPM$ npm install ipfs-provider via prebuilt browser bundles<!-- remember to include js-ipfs (core) and/or js-ipfs-http-client, if they are used -->
<script src="https://cdn.jsdelivr.net/npm/ipfs-core/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ipfs-http-client/dist/index.min.js"></script>
<!-- prebuilt and minified bundle -->
<script src="https://cdn.jsdelivr.net/npm/ipfs-provider/dist/index.min.js"></script>
<script>
const { getIpfs, providers } = window.IpfsProvider
const { ipfs, provider, apiAddress } = await getIpfs({
loadHttpClientModule: () => window.IpfsHttpClient,
loadJsIpfsModule: () => window.IpfsCore,
providers: [ /* see Usage below */ ]
})
</script> Note: when using prebuilt bundles in production use explicit versions and SRI hashes. Details here. Usageconst { getIpfs, providers } = require('ipfs-provider')
const { httpClient, jsIpfs } = providers
const { ipfs, provider, apiAddress } = await getIpfs({
// when httpClient provider is used multiple times
// define its constructor once, at the top level
loadHttpClientModule: () => require('ipfs-http-client'),
// note this is an array, providers are tried in order:
providers: [
// try various HTTP endpoints (best-effort),
httpClient({
// (1) try multiaddr of a local node
apiAddress: '/ip4/127.0.0.1/tcp/5001'
}),
httpClient(), // (2) try "/api/v0/" on the same Origin as the page
httpClient({
// (3) try arbitrary API from URL string
apiAddress: 'https://some.example.com:8080'
}),
httpClient({
// (4) try API defined by a custom http client config
apiAddress: {
host: 'apis.example.com',
port: '443',
protocol: 'https',
apiPath: 'custom/path/to/api/v0',
headers: {
authorization: 'Basic dXNlcjpwYXNz'
}
}
}),
// (5) final fallback to spawning embedded js-ipfs running in-page
jsIpfs({
// js-ipfs package is used only once, as a last resort
loadJsIpfsModule: () => require('ipfs-core'),
options: { } // pass config: https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/docs/MODULE.md#ipfscreateoptions
})
]
})
for await (const file of ipfs.add("text")) {
if (file && file.cid) {
console.log(`successfully stored at ${file.cid}`)
} else {
console.error('unable to ipfs.add', file)
}
}
ExamplesSee ProvidersYou can customize the order of the providers by passing a different array order to the For example, if you want to try const { getIpfs, providers } = require('ipfs-provider')
const { httpClient, jsIpfs } = providers
const { ipfs, provider } = await getIpfs({
providers: [
httpClient(),
jsIpfs()
]
}) Customizing connection testconst { ipfs, provider } = await getIpfs({
providers: [ /* array of providers to try in order */ ],
connectionTest: () => { /* boolean function to test the connection to IPFS, default one tries to ipfs.get the CID of an empty directory */ },
})
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论