Skip to content

Commit d473997

Browse files
chore: update IPFS HTTP client (#1268)
* chore: update IPFS HTTP client * prettier * remove * chore(dependencies): updated changesets for modified dependencies * fixxx? * prettier * use latest ipfs image * trying * prettier * validate if exists * refactor * prettier * we want the hash * make it work * prettier * chore(dependencies): updated changesets for modified dependencies --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 4e1d988 commit d473997

File tree

7 files changed

+358
-686
lines changed

7 files changed

+358
-686
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphprotocol/graph-cli": patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`[email protected]` ↗︎](https://www.npmjs.com/package/ipfs-http-client/v/55.0.0) (from `34.0.0`, in `dependencies`)
6+
- Updated dependency [`[email protected]` ↗︎](https://www.npmjs.com/package/jayson/v/4.0.0) (from `3.7.0`, in `dependencies`)

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,5 @@
3535
"eslint": "^8.31.0",
3636
"jest": "26.6.3",
3737
"prettier": "^2.8.2"
38-
},
39-
"pnpm": {
40-
"overrides": {
41-
"[email protected]>concat-stream": "github:mihirgupta0900/concat-stream#master"
42-
}
4338
}
4439
}

packages/cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
"gluegun": "https://github.com/edgeandnode/gluegun#v4.3.1-pin-colors-dep",
4141
"graphql": "15.5.0",
4242
"immutable": "4.2.1",
43-
"ipfs-http-client": "34.0.0",
44-
"jayson": "3.7.0",
43+
"ipfs-http-client": "55.0.0",
44+
"jayson": "4.0.0",
4545
"js-yaml": "3.14.1",
4646
"prettier": "1.19.1",
4747
"request": "2.88.2",

packages/cli/src/command-helpers/compiler.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { URL } from 'url';
22
import * as toolbox from 'gluegun';
3-
import ipfsHttpClient from 'ipfs-http-client';
3+
import { create } from 'ipfs-http-client';
44
import Compiler from '../compiler';
55
import Protocol from '../protocols';
66

@@ -27,26 +27,17 @@ export function createCompiler(
2727
protocol,
2828
}: CreateCompilerOptions,
2929
) {
30-
// Parse the IPFS URL
31-
let url;
30+
// Validate the IPFS URL (if a node address was provided)
3231
try {
33-
url = ipfs ? new URL(ipfs) : undefined;
32+
if (ipfs) new URL(ipfs);
3433
} catch (e) {
3534
toolbox.print.error(`Invalid IPFS URL: ${ipfs}
3635
The IPFS URL must be of the following format: http(s)://host[:port]/[path]`);
3736
return null;
3837
}
3938

4039
// Connect to the IPFS node (if a node address was provided)
41-
ipfs = ipfs
42-
? ipfsHttpClient({
43-
protocol: url?.protocol.replace(/[:]+$/, ''),
44-
host: url?.hostname,
45-
port: url?.port,
46-
'api-path': url?.pathname.replace(/\/$/, '') + '/api/v0/',
47-
headers,
48-
})
49-
: undefined;
40+
ipfs = ipfs ? create({ url: ipfs, headers }) : undefined;
5041

5142
return new Compiler({
5243
ipfs,

packages/cli/src/command-helpers/ipfs-http-client.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/cli/src/compiler/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Protocol from '../protocols';
1212
import Subgraph from '../subgraph';
1313
import Watcher from '../watcher';
1414
import * as asc from './asc';
15+
import type { IPFSHTTPClient } from 'ipfs-http-client';
1516

1617
const compilerDebug = debug('graph-cli:compiler');
1718

@@ -26,7 +27,7 @@ interface CompilerOptions {
2627
}
2728

2829
export default class Compiler {
29-
private ipfs: any;
30+
private ipfs: IPFSHTTPClient;
3031
private sourceDir: string;
3132
private blockIpfsMethods?: RegExpMatchArray;
3233
private libsDirs: string[];
@@ -678,9 +679,18 @@ export default class Compiler {
678679

679680
async _uploadToIPFS(file: { path: string; content: Buffer }) {
680681
try {
681-
const hash = (await this.ipfs.add([file]))[0].hash;
682-
await this.ipfs.pin.add(hash);
683-
return hash;
682+
const files = this.ipfs.addAll([file]);
683+
684+
// We get back async iterable
685+
const filesIterator = files[Symbol.asyncIterator]();
686+
// We only care about the first item, since that is the file, rest could be directories
687+
const { value } = await filesIterator.next();
688+
689+
// we grab the file and pin it
690+
const uploadedFile = value as Awaited<ReturnType<typeof this.ipfs.add>>;
691+
await this.ipfs.pin.add(uploadedFile.cid);
692+
693+
return uploadedFile.cid.toString();
684694
} catch (e) {
685695
throw Error(`Failed to upload file to IPFS: ${e.message}`);
686696
}

0 commit comments

Comments
 (0)