From 321f284e3031fc689742f3841ef8d842cef80034 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Tue, 15 Dec 2020 01:13:39 -0800 Subject: [PATCH 1/2] feat: export IPFS type --- examples/types-use-ipfs-from-ts/src/main.ts | 21 ++++++++++------ .../types-use-ipfs-from-typed-js/src/main.js | 24 +++++++++++++++---- packages/ipfs-core/src/index.js | 8 +++++-- packages/ipfs/src/index.js | 6 +++-- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/examples/types-use-ipfs-from-ts/src/main.ts b/examples/types-use-ipfs-from-ts/src/main.ts index c066d324df..29e1741602 100644 --- a/examples/types-use-ipfs-from-ts/src/main.ts +++ b/examples/types-use-ipfs-from-ts/src/main.ts @@ -1,7 +1,8 @@ -import IPFS from 'ipfs' +import { IPFS, create } from 'ipfs' +import CID from 'cids' -export default async function main () { - const node = await IPFS.create() +export default async function main() { + const node = await create() const version = await node.version() console.log('Version:', version.version) @@ -14,15 +15,21 @@ export default async function main () { console.log('Added file:', file.path, file.cid.toString()) try { file.cid.toUpperCase() - } catch(error) { + } catch (error) { } + const content = await readFile(node, file.cid) + + console.log('Added file contents:', content) +} + +const readFile = async (ipfs: IPFS, cid: CID): Promise => { const decoder = new TextDecoder() let content = '' - for await (const chunk of node.cat(file.cid)) { - content += decoder.decode(chunk) + for await (const chunk of ipfs.cat(cid)) { + content += decoder.decode(chunk) } - console.log('Added file contents:', content) + return content } diff --git a/examples/types-use-ipfs-from-typed-js/src/main.js b/examples/types-use-ipfs-from-typed-js/src/main.js index 79af577bcb..0d3cbe74a5 100644 --- a/examples/types-use-ipfs-from-typed-js/src/main.js +++ b/examples/types-use-ipfs-from-typed-js/src/main.js @@ -1,4 +1,8 @@ -const IPFS = require('ipfs') +const { create } = require('ipfs') +/** + * @typedef {import('ipfs').IPFS} IPFS + * @typedef {import('cids')} CID + */ async function main () { const node = await IPFS.create() @@ -18,13 +22,23 @@ async function main () { } - const decoder = new TextDecoder() + const content = await readFile(node, file.cid) + + console.log('Added file contents:', content) +} + +/** + * @param {IPFS} ipfs + * @param {CID} cid + * @returns {Promise} + */ +const readFile = async (ipfs, cid) => { + const decoder = new TextDecoder() let content = '' - for await (const chunk of node.cat(file.cid)) { + for await (const chunk of ipfs.cat(cid)) { content += decoder.decode(chunk) } - - console.log('Added file contents:', content) + return content } main() diff --git a/packages/ipfs-core/src/index.js b/packages/ipfs-core/src/index.js index bef8d923e3..f52ec102c6 100644 --- a/packages/ipfs-core/src/index.js +++ b/packages/ipfs-core/src/index.js @@ -11,10 +11,14 @@ const multicodec = require('multicodec') const multihashing = require('multihashing-async') const multihash = multihashing.multihash const CID = require('cids') -const IPFS = require('./components') +const { create } = require('./components') + +/** + * @typedef {import('./components')} IPFS + */ module.exports = { - create: IPFS.create, + create, crypto, isIPFS, CID, diff --git a/packages/ipfs/src/index.js b/packages/ipfs/src/index.js index 0ca85a967e..378329035b 100644 --- a/packages/ipfs/src/index.js +++ b/packages/ipfs/src/index.js @@ -1,5 +1,7 @@ 'use strict' -const IPFS = require('ipfs-core') +/** + * @typedef {import('ipfs-core/src/components')} IPFS + */ -module.exports = IPFS +module.exports = { ...require('ipfs-core') } From 7685b3c6f7313daa4aff7755e8a76bd0d28a7fdf Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 15 Dec 2020 11:24:46 +0000 Subject: [PATCH 2/2] chore: make sure types example tests are run and fix tests --- examples/types-use-ipfs-from-ts/package.json | 2 +- examples/types-use-ipfs-from-ts/src/main.ts | 1 + examples/types-use-ipfs-from-typed-js/package.json | 2 +- examples/types-use-ipfs-from-typed-js/src/main.js | 11 ++++++----- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/types-use-ipfs-from-ts/package.json b/examples/types-use-ipfs-from-ts/package.json index 06d6482147..f36edbd714 100644 --- a/examples/types-use-ipfs-from-ts/package.json +++ b/examples/types-use-ipfs-from-ts/package.json @@ -1,5 +1,5 @@ { - "name": "types-use-ipfs-from-ts", + "name": "example-types-use-ipfs-from-ts", "private": true, "dependencies": { "ipfs": "^0.52.1" diff --git a/examples/types-use-ipfs-from-ts/src/main.ts b/examples/types-use-ipfs-from-ts/src/main.ts index 29e1741602..a6763cade9 100644 --- a/examples/types-use-ipfs-from-ts/src/main.ts +++ b/examples/types-use-ipfs-from-ts/src/main.ts @@ -14,6 +14,7 @@ export default async function main() { console.log('Added file:', file.path, file.cid.toString()) try { + // @ts-expect-error CID has no toUpperCase method file.cid.toUpperCase() } catch (error) { diff --git a/examples/types-use-ipfs-from-typed-js/package.json b/examples/types-use-ipfs-from-typed-js/package.json index 87eb9000f5..c5912b4e55 100644 --- a/examples/types-use-ipfs-from-typed-js/package.json +++ b/examples/types-use-ipfs-from-typed-js/package.json @@ -1,5 +1,5 @@ { - "name": "types-use-ipfs-from-typed-js", + "name": "example-types-use-ipfs-from-typed-js", "private": true, "dependencies": { "ipfs": "^0.52.1" diff --git a/examples/types-use-ipfs-from-typed-js/src/main.js b/examples/types-use-ipfs-from-typed-js/src/main.js index 0d3cbe74a5..0ff2166786 100644 --- a/examples/types-use-ipfs-from-typed-js/src/main.js +++ b/examples/types-use-ipfs-from-typed-js/src/main.js @@ -1,11 +1,11 @@ const { create } = require('ipfs') /** - * @typedef {import('ipfs').IPFS} IPFS + * @typedef {import('ipfs').IPFS} IPFS * @typedef {import('cids')} CID */ async function main () { - const node = await IPFS.create() + const node = await create() const version = await node.version() console.log('Version:', version.version) @@ -17,6 +17,7 @@ async function main () { console.log('Added file:', file.path, file.cid.toString()) try { + // @ts-expect-error CID has no toUpperCase method file.cid.toUpperCase() } catch(error) { @@ -28,15 +29,15 @@ async function main () { } /** - * @param {IPFS} ipfs - * @param {CID} cid + * @param {IPFS} ipfs + * @param {CID} cid * @returns {Promise} */ const readFile = async (ipfs, cid) => { const decoder = new TextDecoder() let content = '' for await (const chunk of ipfs.cat(cid)) { - content += decoder.decode(chunk) + content += decoder.decode(chunk) } return content }