Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options to http client #30

Merged
merged 5 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: ci
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm ci
- run: npm run build
- run: npm run lint
- run: npx aegir dep-check
- uses: ipfs/aegir/actions/bundle-size@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node: [12, 14]
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

7 changes: 1 addition & 6 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ Let us know if you find any issue or if you want to contribute and add an exampl

## Examples

- [`ipfs-provider` in the browser with `browserify`](./browser-browserify)
<!-- TODO - [ipfs-provider in the browser with Parcel.js](./browser-parceljs) -->
<!-- TODO - [ipfs-provider in the browser with Vue](./browser-vue) -->
<!-- TODO - [ipfs-provider in the browser with WebPack](./browser-webpack) -->
<!-- TODO - [ipfs-provider in the browser with a `<script>` tag](./browser-script-tag) -->
- [Bundle `ipfs-provider`, `js-ipfs-http-client` and `js-ipfs` with `esbuild`](./browser-esbuild)

## See also

- [`js-ipfs-http-client` examples](https://github.com/ipfs/js-ipfs/tree/master/packages/ipfs-http-client/examples)
- [`js-ipfs` examples and tutorials](https://github.com/ipfs/js-ipfs/tree/master/examples)
29 changes: 0 additions & 29 deletions examples/browser-browserify/package.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bundle.js
package-lock.json
bundle.js.map
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Bundle `ipfs-provider`, `js-ipfs-http-client` and `js-ipfs` with Browserify!
# Bundle `ipfs-provider`, `js-ipfs-http-client` and `js-ipfs` with esbuild

> In this example, you will find a boilerplate you can use to guide yourself
into bundling everything with browserify, so that you can use it in your own web app
into bundling everything with [esbuild](https://esbuild.github.io) (similar to browserify), so that you can use it in your own web app
for a robust IPFS fallback.

## Run this example

Make sure `ipfs-provider` is built first, as this demo uses local version of `ipfs-provider` at `file:../../`

```console
$ npm install # all dependencies
$ npm ci
$ npm start
```

Expand All @@ -23,8 +23,7 @@ After loading `http://localhost:8888` you should see something similar to the fo

Let's unpack what happened in the above example:

1. 🔴 there was no `window.ipfs` (no [ipfs-companion](https://github.com/ipfs/ipfs-companion), or disabled by user)
2. 🔴 test request to local API (`/ip4/127.0.0.1/tcp/5001`) was blocked due to [CORS protection](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
3. 🔴 `/api/v0/` on the same Origin as the page did not exist
1. 🔴 test request to local API (`/ip4/127.0.0.1/tcp/5001`) was blocked due to [CORS protection](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
2. 🔴 `/api/v0/` on the same Origin as the page did not exist
3. 🔴 explicitly defined remote API was offline (`http://dev.local:8080`)
4. 💚 final fallback worked: spawning embedded [js-ipfs](https://github.com/ipfs/js-ipfs/tree/master/packages/ipfs) was executed successfully 🚀✨
28 changes: 28 additions & 0 deletions examples/browser-esbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "bundle-esbuild",
"version": "1.0.0",
"description": "Bundle ipfs-provider with esbuild",
"main": "index.js",
"private": true,
"scripts": {
"bundle": "esbuild src/index.js --bundle --minify --sourcemap --define:process.env.NODE_ENV=\\\"production\\\" --outfile=public/bundle.js",
"serve": "http-server public -a 127.0.0.1 -p 8888 -c-1",
"start": "npm run bundle && npm run serve"
},
"keywords": [],
"license": "MIT",
"devDependencies": {
"esbuild": "0.11.2",
"http-server": "0.12.3",
"ipfs-provider": "file:../../"
},
"dependencies": {
"ipfs-core": "0.5.4",
"ipfs-http-client": "49.0.4"
},
"browser": {
"util": false,
"ipfs-core": "ipfs-core/dist/index.min.js",
"ipfs-http-client": "ipfs-http-client/dist/index.min.js"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const Buffer = require('buffer/').Buffer
const { getIpfs, providers } = require('ipfs-provider')
const { httpClient, jsIpfs, windowIpfs } = providers
const { httpClient, jsIpfs } = providers

document.addEventListener('DOMContentLoaded', async () => {
console.log('IPFS starting..')
const { ipfs, provider, apiAddress } = await getIpfs({
// HTTP client library can be defined globally to keep code minimal
// when httpClient provider is used multiple times
Expand All @@ -13,9 +13,6 @@ document.addEventListener('DOMContentLoaded', async () => {
// then http apis (if up),
// and finally fallback to spawning embedded js-ipfs
providers: [
windowIpfs({
permissions: { commands: ['files.add', 'files.cat'] }
}),
httpClient({
// try multiaddr of a local node
apiAddress: '/ip4/127.0.0.1/tcp/5001'
Expand All @@ -27,8 +24,10 @@ document.addEventListener('DOMContentLoaded', async () => {
}),
jsIpfs({
// js-ipfs package is used only once, here
loadJsIpfsModule: () => require('ipfs'), // note require instead of
options: { } // pass config: https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/docs/MODULE.md#ipfscreateoptions
loadJsIpfsModule: () => require('ipfs-core'), // note require instead of
options: {
init: { alogorithm: 'ed25519' }
}
})
]
})
Expand All @@ -54,9 +53,14 @@ document.addEventListener('DOMContentLoaded', async () => {
for await (const chunk of ipfs.cat(cid)) {
chunks.push(chunk)
}
const data = Buffer.concat(chunks).toString()

// merge all chunks into single byte buffer (does not matter here, as text
// entered by user will be short enough to fit in a single chunk, but makes
// this example useful for bigger files)
const data = new Uint8Array(chunks.reduce((acc, curr) => [...acc, ...curr], []))

document.getElementById('cid').innerText = cid
document.getElementById('content').innerText = data
document.getElementById('content').innerText = new TextDecoder().decode(data)
document.getElementById('output').setAttribute('style', 'display: block')
}

Expand Down
Loading