Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: incorporate changes from ipfs@0.48
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Jul 21, 2020
1 parent f462640 commit 2302b2f
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 157 deletions.
10 changes: 1 addition & 9 deletions examples/browser-sharing-node-across-tabs/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const main = async () => {

const uploader = async (ipfs) => {
document.body.outerHTML += '<div>Adding "hello world!" to shared IPFS node</div>'
const entry = await add(ipfs, new Blob(['hello world!'], { type: "text/plain" }))
const entry = await ipfs.add(ipfs, new Blob(['hello world!'], { type: "text/plain" }))
const path = `/ipfs/${entry.cid}/`
document.body.outerHTML += `<div class="ipfs-add">File was added:
<a target="_blank" href="${new URL(`#${path}`, location)}">${path}</a>
Expand All @@ -42,12 +42,4 @@ const viewer = async (ipfs, path) => {
}
}

const add = async (ipfs, blob) => {
let result = null
for await (const entry of ipfs.add(blob)) {
result = entry
}
return result
}

onload = main
19 changes: 11 additions & 8 deletions packages/interface-ipfs-core/src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ const { Buffer } = require('buffer')
const { fixtures } = require('./utils')
const { Readable } = require('readable-stream')
const { supportsFileReader } = require('ipfs-utils/src/supports')
const urlSource = require('ipfs-utils/src/files/url-source')
const urlSources = require('ipfs-utils/src/files/url-source')
const last = require('it-last')
const { isNode } = require('ipfs-utils/src/env')
const { getDescribe, getIt, expect } = require('./utils/mocha')
const testTimeout = require('./utils/test-timeout')
const echoUrl = (text) => `${process.env.ECHO_SERVER}/download?data=${encodeURIComponent(text)}`
const redirectUrl = (url) => `${process.env.ECHO_SERVER}/redirect?to=${encodeURI(url)}`

const urlSource = (url) => last(urlSources(url))

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
Expand Down Expand Up @@ -242,7 +245,7 @@ module.exports = (common, options) => {
const url = echoUrl(text)

const [result, expectedResult] = await Promise.all([
ipfs.add(urlSource(url)),
ipfs.add(await urlSource(url)),
ipfs.add(Buffer.from(text))
])

Expand All @@ -257,7 +260,7 @@ module.exports = (common, options) => {
const url = echoUrl(text)

const [result, expectedResult] = await Promise.all([
ipfs.add(urlSource(redirectUrl(url))),
ipfs.add(await urlSource(redirectUrl(url))),
ipfs.add(Buffer.from(text))
])

Expand All @@ -271,7 +274,7 @@ module.exports = (common, options) => {
const text = `TEST${Math.random()}`
const url = echoUrl(text)

const res = await ipfs.add(urlSource(url), { onlyHash: true })
const res = await ipfs.add(await urlSource(url), { onlyHash: true })

await expect(ipfs.object.get(res.cid, { timeout: 500 }))
.to.eventually.be.rejected()
Expand All @@ -284,7 +287,7 @@ module.exports = (common, options) => {
const addOpts = { wrapWithDirectory: true }

const [result, expectedResult] = await Promise.all([
ipfs.add(urlSource(url), addOpts),
ipfs.add(await urlSource(url), addOpts),
ipfs.add({ path: 'download', content: Buffer.from(filename) }, addOpts)
])
expect(result.err).to.not.exist()
Expand All @@ -298,8 +301,8 @@ module.exports = (common, options) => {
const addOpts = { wrapWithDirectory: true }

const [result, expectedResult] = await Promise.all([
ipfs.add(urlSource(url), addOpts),
ipfs.add([{ path: 'download', content: Buffer.from(filename) }], addOpts)
ipfs.add(await urlSource(url), addOpts),
ipfs.add({ path: 'download', content: Buffer.from(filename) }, addOpts)
])

expect(result.err).to.not.exist()
Expand All @@ -308,7 +311,7 @@ module.exports = (common, options) => {
})

it('should not add from an invalid url', () => {
return expect(ipfs.add(urlSource('123http://invalid'))).to.eventually.be.rejected()
return expect(last(ipfs.addAll(urlSources('123http://invalid')))).to.eventually.be.rejected()
})

it('should respect raw leaves when file is smaller than one block and no metadata is present', async () => {
Expand Down
27 changes: 17 additions & 10 deletions packages/ipfs-message-port-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
"name": "ipfs-message-port-client",
"version": "0.0.1",
"description": "IPFS client library for accessing IPFS node over message port",
"keywords": ["ipfs", "message-port", "worker"],
"keywords": [
"ipfs",
"message-port",
"worker"
],
"homepage": "https://github.com/ipfs/js-ipfs/tree/master/packages/ipfs-message-port-client#readme",
"bugs": "https://github.com/ipfs/js-ipfs/issues",
"license": "(Apache-2.0 OR MIT)",
"leadMaintainer": "Alex Potsides <alex@achingbrain.net>",
"files": ["src", "dist"],
"files": [
"src",
"dist"
],
"main": "src/index.js",
"browser": {},
"repository": {
Expand All @@ -27,22 +34,22 @@
"dep-check": "aegir dep-check"
},
"dependencies": {
"cids": "^0.8.0"
"cids": "^0.8.3"
},
"devDependencies": {
"ipfs-message-port-protocol": "~0.0.1",
"ipfs-message-port-server": "~0.0.1",
"ipld-dag-pb": "^0.19.0",
"ipfs": "^0.46.0",
"it-all": "^1.0.1",
"it-drain": "^1.0.1",
"aegir": "^22.0.0",
"ipfs": "^0.48.0",
"aegir": "^23.0.0",
"cross-env": "^7.0.0",
"interface-ipfs-core": "^0.137.0"
"interface-ipfs-core": "^0.138.0"
},
"engines": {
"node": ">=10.3.0",
"npm": ">=3.0.0"
},
"contributors": ["Irakli Gozalishvili <contact@gozala.io>"]
}
"contributors": [
"Irakli Gozalishvili <dev@gozala.io>"
]
}
126 changes: 106 additions & 20 deletions packages/ipfs-message-port-client/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const {
* @typedef {import('ipfs-message-port-protocol/src/data').Time} Time
* @typedef {import('ipfs-message-port-protocol/src/data').UnixFSTime} UnixFSTime
* @typedef {import('ipfs-message-port-protocol/src/dag').EncodedCID} EncodedCID
* @typedef {import('ipfs-message-port-server/src/core').AddInput} EncodedAddInput
* @typedef {import('ipfs-message-port-server/src/core').SingleFileInput} EncodedAddInput
* @typedef {import('ipfs-message-port-server/src/core').MultiFileInput} EncodedAddAllInput
* @typedef {import('ipfs-message-port-server/src/core').FileInput} FileInput
* @typedef {import('ipfs-message-port-server/src/core').FileContent} EncodedFileContent
*
Expand All @@ -29,7 +30,7 @@ const {
*
* @typedef {ArrayBuffer|ArrayBufferView} Bytes
*
* @typedef {Blob|Bytes|string|Iterable<number>|Iterable<Bytes>|AsyncIterable<Bytes>|ReadableStream} FileContent
* @typedef {Blob|Bytes|string|Iterable<number>|Iterable<Bytes>|AsyncIterable<Bytes>} FileContent
*
* @typedef {Object} FileObject
* @property {string} [path]
Expand All @@ -38,12 +39,9 @@ const {
* @property {UnixFSTime} [mtime]
*
*
* @typedef {Blob|Bytes|string|FileObject|Iterable<Number>|Iterable<Bytes>|AsyncIterable<Bytes>|ReadableStream} SingleFileInput
*
* @typedef {Iterable<Blob>|Iterable<string>|Iterable<FileObject>|AsyncIterable<Blob>|AsyncIterable<string>|AsyncIterable<FileObject>} MultiFileInput
*
* @typedef {SingleFileInput | MultiFileInput} AddInput
* @typedef {Blob|Bytes|string|FileObject|Iterable<number>|Iterable<Bytes>|AsyncIterable<Bytes>|ReadableStream} AddInput
*
* @typedef {Iterable<Blob|string|FileObject>|AsyncIterable<Blob|string|FileObject>} AddAllInput
*/

/**
Expand Down Expand Up @@ -71,7 +69,7 @@ class CoreClient extends Client {
* `transfer: [input.buffer]` which would allow transferring it instead of
* copying.
*
* @param {AddInput} input
* @param {AddAllInput} input
* @param {Object} [options]
* @param {string} [options.chunker="size-262144"]
* @param {number} [options.cidVersion=0]
Expand All @@ -96,7 +94,50 @@ class CoreClient extends Client {
* @property {number} size
* @property {Time} mtime
*/
async * add (input, options = {}) {
async * addAll (input, options = {}) {
const { timeout, signal } = options
const transfer = [...(options.transfer || [])]
const progress = options.progress
? encodeCallback(options.progress, transfer)
: undefined

const result = await this.remote.addAll({
...options,
input: encodeAddAllInput(input, transfer),
progress,
transfer,
timeout,
signal
})
yield * decodeIterable(result.data, decodeAddedData)
}

/**
* Add file to IPFS.
*
* If you pass binary data like `Uint8Array` it is recommended to provide
* `transfer: [input.buffer]` which would allow transferring it instead of
* copying.
*
* @param {AddInput} input
* @param {Object} [options]
* @param {string} [options.chunker="size-262144"]
* @param {number} [options.cidVersion=0]
* @param {boolean} [options.enableShardingExperiment]
* @param {string} [options.hashAlg="sha2-256"]
* @param {boolean} [options.onlyHash=false]
* @param {boolean} [options.pin=true]
* @param {function(number):void} [options.progress]
* @param {boolean} [options.rawLeaves=false]
* @param {number} [options.shardSplitThreshold=1000]
* @param {boolean} [options.trickle=false]
* @param {boolean} [options.wrapWithDirectory=false]
* @param {number} [options.timeout]
* @param {Transferable[]} [options.transfer]
* @param {AbortSignal} [options.signal]
* @returns {Promise<AddedData>}
*/
async add (input, options = {}) {
const { timeout, signal } = options
const transfer = [...(options.transfer || [])]
const progress = options.progress
Expand All @@ -111,7 +152,8 @@ class CoreClient extends Client {
timeout,
signal
})
yield * decodeIterable(result.data, decodeAddedData)

return decodeAddedData(result.data)
}

/**
Expand Down Expand Up @@ -151,7 +193,7 @@ const decodeAddedData = ({ path, cid, mode, mtime, size }) => {
* @param {T} v
* @returns {T}
*/
const identity = v => v
const identity = (v) => v

/**
* Encodes input passed to the `ipfs.add` via the best possible strategy for the
Expand All @@ -175,15 +217,18 @@ const encodeAddInput = (input, transfer) => {
} else {
// If input is (async) iterable or `ReadableStream` or "FileObject" it will
// be encoded via own specific encoder.

const iterable = asIterable(input)
if (iterable) {
return encodeIterable(iterable, encodeIterableContent, transfer)
}

const asyncIterable = asAsyncIterable(input)
if (asyncIterable) {
return encodeIterable(asyncIterable, encodeAsyncIterableContent, transfer)
return encodeIterable(
asyncIterable,
encodeAsyncIterableContent,
transfer
)
}

const readableStream = asReadableStream(input)
Expand All @@ -204,6 +249,43 @@ const encodeAddInput = (input, transfer) => {
}
}

/**
* Encodes input passed to the `ipfs.add` via the best possible strategy for the
* given input.
*
* @param {AddAllInput} input
* @param {Transferable[]} transfer
* @returns {EncodedAddAllInput}
*/
const encodeAddAllInput = (input, transfer) => {
// If input is (async) iterable or `ReadableStream` or "FileObject" it will
// be encoded via own specific encoder.
const iterable = asIterable(input)
if (iterable) {
return encodeIterable(iterable, encodeIterableContent, transfer)
}

const asyncIterable = asAsyncIterable(input)
if (asyncIterable) {
return encodeIterable(
asyncIterable,
encodeAsyncIterableContent,
transfer
)
}

const readableStream = asReadableStream(input)
if (readableStream) {
return encodeIterable(
iterateReadableStream(readableStream),
encodeAsyncIterableContent,
transfer
)
}

throw TypeError('Unexpected input: ' + typeof input)
}

/**
* Function encodes individual item of some `AsyncIterable` by choosing most
* effective strategy.
Expand Down Expand Up @@ -291,7 +373,11 @@ const encodeFileContent = (content, transfer) => {

const asyncIterable = asAsyncIterable(content)
if (asyncIterable) {
return encodeIterable(asyncIterable, encodeAsyncIterableContent, transfer)
return encodeIterable(
asyncIterable,
encodeAsyncIterableContent,
transfer
)
}

const readableStream = asReadableStream(content)
Expand Down Expand Up @@ -331,10 +417,10 @@ const iterateReadableStream = async function * (stream) {
* Pattern matches given input as `Iterable<I>` and returns back either matched
* iterable or `null`.
* @template I
* @param {Iterable<I>|AddInput} input
* @param {Iterable<I>|AddInput|AddAllInput} input
* @returns {Iterable<I>|null}
*/
const asIterable = input => {
const asIterable = (input) => {
/** @type {*} */
const object = input
if (object && typeof object[Symbol.iterator] === 'function') {
Expand All @@ -348,10 +434,10 @@ const asIterable = input => {
* Pattern matches given `input` as `AsyncIterable<I>` and returns back either
* matched `AsyncIterable` or `null`.
* @template I
* @param {AsyncIterable<I>|AddInput} input
* @param {AsyncIterable<I>|AddInput|AddAllInput} input
* @returns {AsyncIterable<I>|null}
*/
const asAsyncIterable = input => {
const asAsyncIterable = (input) => {
/** @type {*} */
const object = input
if (object && typeof object[Symbol.asyncIterator] === 'function') {
Expand All @@ -368,7 +454,7 @@ const asAsyncIterable = input => {
* @param {any} input
* @returns {ReadableStream<Uint8Array>|null}
*/
const asReadableStream = input => {
const asReadableStream = (input) => {
if (input && typeof input.getReader === 'function') {
return input
} else {
Expand All @@ -382,7 +468,7 @@ const asReadableStream = input => {
* @param {*} input
* @returns {FileObject|null}
*/
const asFileObject = input => {
const asFileObject = (input) => {
if (typeof input === 'object' && (input.path || input.content)) {
return input
} else {
Expand Down
Loading

0 comments on commit 2302b2f

Please sign in to comment.