File tree 6 files changed +49
-20
lines changed
types-use-ipfs-from-typed-js
6 files changed +49
-20
lines changed Original file line number Diff line number Diff line change 1
1
{
2
- "name" : " types-use-ipfs-from-ts" ,
2
+ "name" : " example- types-use-ipfs-from-ts" ,
3
3
"private" : true ,
4
4
"dependencies" : {
5
5
"ipfs" : " ^0.52.1"
Original file line number Diff line number Diff line change 1
- import IPFS from 'ipfs'
1
+ import { IPFS , create } from 'ipfs'
2
+ import CID from 'cids'
2
3
3
- export default async function main ( ) {
4
- const node = await IPFS . create ( )
4
+ export default async function main ( ) {
5
+ const node = await create ( )
5
6
const version = await node . version ( )
6
7
7
8
console . log ( 'Version:' , version . version )
@@ -13,16 +14,23 @@ export default async function main () {
13
14
14
15
console . log ( 'Added file:' , file . path , file . cid . toString ( ) )
15
16
try {
17
+ // @ts -expect-error CID has no toUpperCase method
16
18
file . cid . toUpperCase ( )
17
- } catch ( error ) {
19
+ } catch ( error ) {
18
20
19
21
}
20
22
23
+ const content = await readFile ( node , file . cid )
24
+
25
+ console . log ( 'Added file contents:' , content )
26
+ }
27
+
28
+ const readFile = async ( ipfs : IPFS , cid : CID ) : Promise < string > => {
21
29
const decoder = new TextDecoder ( )
22
30
let content = ''
23
- for await ( const chunk of node . cat ( file . cid ) ) {
24
- content += decoder . decode ( chunk )
31
+ for await ( const chunk of ipfs . cat ( cid ) ) {
32
+ content += decoder . decode ( chunk )
25
33
}
26
34
27
- console . log ( 'Added file contents:' , content )
35
+ return content
28
36
}
Original file line number Diff line number Diff line change 1
1
{
2
- "name" : " types-use-ipfs-from-typed-js" ,
2
+ "name" : " example- types-use-ipfs-from-typed-js" ,
3
3
"private" : true ,
4
4
"dependencies" : {
5
5
"ipfs" : " ^0.52.1"
Original file line number Diff line number Diff line change 1
- const IPFS = require ( 'ipfs' )
1
+ const { create } = require ( 'ipfs' )
2
+ /**
3
+ * @typedef {import('ipfs').IPFS } IPFS
4
+ * @typedef {import('cids') } CID
5
+ */
2
6
3
7
async function main ( ) {
4
- const node = await IPFS . create ( )
8
+ const node = await create ( )
5
9
const version = await node . version ( )
6
10
7
11
console . log ( 'Version:' , version . version )
@@ -13,18 +17,29 @@ async function main () {
13
17
14
18
console . log ( 'Added file:' , file . path , file . cid . toString ( ) )
15
19
try {
20
+ // @ts -expect-error CID has no toUpperCase method
16
21
file . cid . toUpperCase ( )
17
22
} catch ( error ) {
18
23
19
24
}
20
25
21
- const decoder = new TextDecoder ( )
22
- let content = ''
23
- for await ( const chunk of node . cat ( file . cid ) ) {
24
- content += decoder . decode ( chunk )
25
- }
26
+ const content = await readFile ( node , file . cid )
26
27
27
28
console . log ( 'Added file contents:' , content )
28
29
}
29
30
31
+ /**
32
+ * @param {IPFS } ipfs
33
+ * @param {CID } cid
34
+ * @returns {Promise<string> }
35
+ */
36
+ const readFile = async ( ipfs , cid ) => {
37
+ const decoder = new TextDecoder ( )
38
+ let content = ''
39
+ for await ( const chunk of ipfs . cat ( cid ) ) {
40
+ content += decoder . decode ( chunk )
41
+ }
42
+ return content
43
+ }
44
+
30
45
main ( )
Original file line number Diff line number Diff line change @@ -11,10 +11,14 @@ const multicodec = require('multicodec')
11
11
const multihashing = require ( 'multihashing-async' )
12
12
const multihash = multihashing . multihash
13
13
const CID = require ( 'cids' )
14
- const IPFS = require ( './components' )
14
+ const { create } = require ( './components' )
15
+
16
+ /**
17
+ * @typedef {import('./components') } IPFS
18
+ */
15
19
16
20
module . exports = {
17
- create : IPFS . create ,
21
+ create,
18
22
crypto,
19
23
isIPFS,
20
24
CID ,
Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
3
- const IPFS = require ( 'ipfs-core' )
3
+ /**
4
+ * @typedef {import('ipfs-core/src/components') } IPFS
5
+ */
4
6
5
- module . exports = IPFS
7
+ module . exports = { ... require ( 'ipfs-core' ) }
You can’t perform that action at this time.
0 commit comments