diff --git a/packages/interface-ipfs-core/package.json b/packages/interface-ipfs-core/package.json index fb11b67754..2bb049a903 100644 --- a/packages/interface-ipfs-core/package.json +++ b/packages/interface-ipfs-core/package.json @@ -48,6 +48,7 @@ "is-ipfs": "^2.0.0", "iso-random-stream": "^1.1.1", "it-all": "^1.0.4", + "it-buffer-stream": "^1.0.5", "it-concat": "^1.0.1", "it-drain": "^1.0.3", "it-last": "^1.0.4", diff --git a/packages/interface-ipfs-core/src/add-all.js b/packages/interface-ipfs-core/src/add-all.js index bb3ea5a145..8dd06bc229 100644 --- a/packages/interface-ipfs-core/src/add-all.js +++ b/packages/interface-ipfs-core/src/add-all.js @@ -15,6 +15,7 @@ const { isNode } = require('ipfs-utils/src/env') const { getDescribe, getIt, expect } = require('./utils/mocha') const testTimeout = require('./utils/test-timeout') const uint8ArrayFromString = require('uint8arrays/from-string') +const bufferStream = require('it-buffer-stream') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -420,5 +421,84 @@ module.exports = (common, options) => { expect(files[0].cid.codec).to.equal('dag-pb') expect(files[0].size).to.equal(18) }) + + it('should support bidirectional streaming', async function () { + let progressInvoked + + const handler = (bytes, path) => { + progressInvoked = true + } + + const source = async function * () { + yield { + content: 'hello', + path: '/file' + } + + await new Promise((resolve) => { + const interval = setInterval(() => { + // we've received a progress result, that means we've received some + // data from the server before we're done sending data to the server + // so the streaming is bidirectional and we can finish up + if (progressInvoked) { + clearInterval(interval) + resolve() + } + }, 10) + }) + } + + await drain(ipfs.addAll(source(), { + progress: handler, + fileImportConcurrency: 1 + })) + + expect(progressInvoked).to.be.true() + }) + + it('should error during add-all stream', async function () { + const source = async function * () { + yield { + content: 'hello', + path: '/file' + } + + yield { + content: 'hello', + path: '/file' + } + } + + await expect(drain(ipfs.addAll(source(), { + fileImportConcurrency: 1, + chunker: 'rabin-2048--50' // invalid chunker parameters, validated after the stream starts moving + }))).to.eventually.be.rejectedWith(/Chunker parameter avg must be an integer/) + }) + + it('should add big files', async function () { + const totalSize = 1024 * 1024 * 200 + const chunkSize = 1024 * 1024 * 99 + + const source = async function * () { + yield { + path: '/dir/file-200mb-1', + content: bufferStream(totalSize, { + chunkSize + }) + } + + yield { + path: '/dir/file-200mb-2', + content: bufferStream(totalSize, { + chunkSize + }) + } + } + + const results = await all(ipfs.addAll(source())) + + expect(await ipfs.files.stat(`/ipfs/${results[0].cid}`)).to.have.property('size', totalSize) + expect(await ipfs.files.stat(`/ipfs/${results[1].cid}`)).to.have.property('size', totalSize) + }) }) } diff --git a/packages/ipfs-cli/tsconfig.json b/packages/ipfs-cli/tsconfig.json index 8fdab14fa1..f84b5f655c 100644 --- a/packages/ipfs-cli/tsconfig.json +++ b/packages/ipfs-cli/tsconfig.json @@ -9,14 +9,17 @@ ], "references": [ { - "path": "../ipfs-core-utils" + "path": "../ipfs-core" }, { - "path": "../ipfs-core" + "path": "../ipfs-core-utils" }, { "path": "../ipfs-http-client" }, + { + "path": "../ipfs-http-gateway" + }, { "path": "../ipfs-http-server" } diff --git a/packages/ipfs-core-utils/package.json b/packages/ipfs-core-utils/package.json index 12e4bdbf30..e68d3ac4e5 100644 --- a/packages/ipfs-core-utils/package.json +++ b/packages/ipfs-core-utils/package.json @@ -38,6 +38,7 @@ }, "license": "MIT", "dependencies": { + "any-signal": "^2.0.0", "blob-to-it": "^1.0.1", "browser-readablestream-to-it": "^1.0.1", "cids": "^1.0.0", @@ -48,6 +49,8 @@ "it-peekable": "^1.0.1", "multiaddr": "^8.0.0", "multiaddr-to-uri": "^6.0.0", + "parse-duration": "^0.4.4", + "timeout-abort-controller": "^1.1.1", "uint8arrays": "^1.1.0" }, "devDependencies": { diff --git a/packages/ipfs-core-utils/src/errors.js b/packages/ipfs-core-utils/src/errors.js new file mode 100644 index 0000000000..7894bfbefa --- /dev/null +++ b/packages/ipfs-core-utils/src/errors.js @@ -0,0 +1,12 @@ +'use strict' + +class TimeoutError extends Error { + constructor (message = 'request timed out') { + super(message) + this.name = 'TimeoutError' + this.code = TimeoutError.code + } +} + +TimeoutError.code = 'ERR_TIMEOUT' +exports.TimeoutError = TimeoutError diff --git a/packages/ipfs-core-utils/src/files/normalise-input/normalise-input.js b/packages/ipfs-core-utils/src/files/normalise-input/normalise-input.js index 9d89695a6c..4a6e8c6d82 100644 --- a/packages/ipfs-core-utils/src/files/normalise-input/normalise-input.js +++ b/packages/ipfs-core-utils/src/files/normalise-input/normalise-input.js @@ -8,7 +8,9 @@ const { isBytes, isBlob, isReadableStream, - isFileObject + isFileObject, + mtimeToObject, + modeToNumber } = require('./utils') // eslint-disable-next-line complexity @@ -105,7 +107,8 @@ module.exports = async function * normaliseInput (input, normaliseContent) { async function toFileObject (input, normaliseContent) { // @ts-ignore - Those properties don't exist on most input types const { path, mode, mtime, content } = input - const file = { path: path || '', mode, mtime } + + const file = { path: path || '', mode: modeToNumber(mode), mtime: mtimeToObject(mtime) } if (content) { file.content = await normaliseContent(content) diff --git a/packages/ipfs-core-utils/src/files/normalise-input/utils.js b/packages/ipfs-core-utils/src/files/normalise-input/utils.js index e1d0cb2b61..dabc6e5a6f 100644 --- a/packages/ipfs-core-utils/src/files/normalise-input/utils.js +++ b/packages/ipfs-core-utils/src/files/normalise-input/utils.js @@ -35,9 +35,94 @@ function isFileObject (obj) { const isReadableStream = (value) => value && typeof value.getReader === 'function' +/** + * @param {any} mtime + * @returns {{secs:number, nsecs:number}|undefined} + */ +function mtimeToObject (mtime) { + if (mtime == null) { + return undefined + } + + // Javascript Date + if (mtime instanceof Date) { + const ms = mtime.getTime() + const secs = Math.floor(ms / 1000) + + return { + secs: secs, + nsecs: (ms - (secs * 1000)) * 1000 + } + } + + // { secs, nsecs } + if (Object.prototype.hasOwnProperty.call(mtime, 'secs')) { + return { + secs: mtime.secs, + nsecs: mtime.nsecs + } + } + + // UnixFS TimeSpec + if (Object.prototype.hasOwnProperty.call(mtime, 'Seconds')) { + return { + secs: mtime.Seconds, + nsecs: mtime.FractionalNanoseconds + } + } + + // process.hrtime() + if (Array.isArray(mtime)) { + return { + secs: mtime[0], + nsecs: mtime[1] + } + } + /* + TODO: https://github.com/ipfs/aegir/issues/487 + + // process.hrtime.bigint() + if (typeof mtime === 'bigint') { + const secs = mtime / BigInt(1e9) + const nsecs = mtime - (secs * BigInt(1e9)) + + return { + secs: parseInt(secs), + nsecs: parseInt(nsecs) + } + } + */ +} + +/** + * @param {any} mode + * @returns {number|undefined} + */ +function modeToNumber (mode) { + if (mode == null) { + return undefined + } + + if (typeof mode === 'number') { + return mode + } + + mode = mode.toString() + + if (mode.substring(0, 1) === '0') { + // octal string + return parseInt(mode, 8) + } + + // decimal string + return parseInt(mode, 10) +} + module.exports = { isBytes, isBlob, isFileObject, - isReadableStream + isReadableStream, + mtimeToObject, + modeToNumber } diff --git a/packages/ipfs-core-utils/src/index.js b/packages/ipfs-core-utils/src/index.js index ccacec309b..89aa433f85 100644 --- a/packages/ipfs-core-utils/src/index.js +++ b/packages/ipfs-core-utils/src/index.js @@ -1 +1,7 @@ 'use strict' + +/** + * @template {any[]} ARGS + * @template R + * @typedef {(...args: ARGS) => R} Fn + */ diff --git a/packages/ipfs-core-utils/src/with-timeout-option.js b/packages/ipfs-core-utils/src/with-timeout-option.js new file mode 100644 index 0000000000..ace39cccce --- /dev/null +++ b/packages/ipfs-core-utils/src/with-timeout-option.js @@ -0,0 +1,106 @@ +/* eslint-disable no-unreachable */ +'use strict' + +const TimeoutController = require('timeout-abort-controller') +const { anySignal } = require('any-signal') +const parseDuration = require('parse-duration').default +const { TimeoutError } = require('./errors') + +/** + * @template {any[]} ARGS + * @template {Promise | AsyncIterable} R - The return type of `fn` + * @param {Fn} fn + * @param {number} [optionsArgIndex] + * @returns {Fn} + */ +function withTimeoutOption (fn, optionsArgIndex) { + // eslint-disable-next-line + return /** @returns {R} */(/** @type {ARGS} */...args) => { + const options = args[optionsArgIndex == null ? args.length - 1 : optionsArgIndex] + if (!options || !options.timeout) return fn(...args) + + const timeout = typeof options.timeout === 'string' + ? parseDuration(options.timeout) + : options.timeout + + const controller = new TimeoutController(timeout) + + options.signal = anySignal([options.signal, controller.signal]) + + const fnRes = fn(...args) + // eslint-disable-next-line promise/param-names + const timeoutPromise = new Promise((_resolve, reject) => { + controller.signal.addEventListener('abort', () => { + reject(new TimeoutError()) + }) + }) + + const start = Date.now() + + const maybeThrowTimeoutError = () => { + if (controller.signal.aborted) { + throw new TimeoutError() + } + + const timeTaken = Date.now() - start + + // if we have starved the event loop by adding microtasks, we could have + // timed out already but the TimeoutController will never know because it's + // setTimeout will not fire until we stop adding microtasks + if (timeTaken > timeout) { + controller.abort() + throw new TimeoutError() + } + } + + if (fnRes[Symbol.asyncIterator]) { + // @ts-ignore + return (async function * () { + const it = fnRes[Symbol.asyncIterator]() + + try { + while (true) { + const { value, done } = await Promise.race([it.next(), timeoutPromise]) + + if (done) { + break + } + + maybeThrowTimeoutError() + + yield value + } + } catch (err) { + maybeThrowTimeoutError() + + throw err + } finally { + controller.clear() + + if (it.return) { + it.return() + } + } + })() + } + + // @ts-ignore + return (async () => { + try { + const res = await Promise.race([fnRes, timeoutPromise]) + + maybeThrowTimeoutError() + + return res + } catch (err) { + maybeThrowTimeoutError() + + throw err + } finally { + controller.clear() + } + })() + } +} + +module.exports = withTimeoutOption diff --git a/packages/ipfs-core/package.json b/packages/ipfs-core/package.json index d2a257cd8c..2f51f07f3e 100644 --- a/packages/ipfs-core/package.json +++ b/packages/ipfs-core/package.json @@ -54,7 +54,6 @@ "dep-check": "aegir dep-check -i typescript -i interface-ipfs-core" }, "dependencies": { - "any-signal": "^2.0.0", "array-shuffle": "^1.0.1", "bignumber.js": "^9.0.0", "cbor": "^5.1.0", @@ -116,7 +115,6 @@ "parse-duration": "^0.4.4", "peer-id": "^0.14.1", "streaming-iterables": "^5.0.2", - "timeout-abort-controller": "^1.1.1", "uint8arrays": "^1.1.0" }, "devDependencies": { diff --git a/packages/ipfs-core/src/components/add-all/index.js b/packages/ipfs-core/src/components/add-all/index.js index 4798616df7..0c1c24435a 100644 --- a/packages/ipfs-core/src/components/add-all/index.js +++ b/packages/ipfs-core/src/components/add-all/index.js @@ -4,7 +4,7 @@ const importer = require('ipfs-unixfs-importer') const normaliseAddInput = require('ipfs-core-utils/src/files/normalise-input/index') const { parseChunkerString } = require('./utils') const { pipe } = require('it-pipe') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) /** diff --git a/packages/ipfs-core/src/components/bitswap/stat.js b/packages/ipfs-core/src/components/bitswap/stat.js index 237a54d80a..6197426058 100644 --- a/packages/ipfs-core/src/components/bitswap/stat.js +++ b/packages/ipfs-core/src/components/bitswap/stat.js @@ -2,7 +2,7 @@ const Big = require('bignumber.js').default const CID = require('cids') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/bitswap/unwant.js b/packages/ipfs-core/src/components/bitswap/unwant.js index 455696b23c..26af33e3c3 100644 --- a/packages/ipfs-core/src/components/bitswap/unwant.js +++ b/packages/ipfs-core/src/components/bitswap/unwant.js @@ -2,7 +2,7 @@ const CID = require('cids') const errCode = require('err-code') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/bitswap/wantlist-for-peer.js b/packages/ipfs-core/src/components/bitswap/wantlist-for-peer.js index 6f79ab8c9d..dff9d534e1 100644 --- a/packages/ipfs-core/src/components/bitswap/wantlist-for-peer.js +++ b/packages/ipfs-core/src/components/bitswap/wantlist-for-peer.js @@ -1,7 +1,7 @@ 'use strict' const PeerId = require('peer-id') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/bitswap/wantlist.js b/packages/ipfs-core/src/components/bitswap/wantlist.js index 94fed01ebb..0d43f816b5 100644 --- a/packages/ipfs-core/src/components/bitswap/wantlist.js +++ b/packages/ipfs-core/src/components/bitswap/wantlist.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/block/get.js b/packages/ipfs-core/src/components/block/get.js index 4f376fb4a5..d4c8825af2 100644 --- a/packages/ipfs-core/src/components/block/get.js +++ b/packages/ipfs-core/src/components/block/get.js @@ -1,7 +1,7 @@ 'use strict' const { cleanCid } = require('./utils') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/block/put.js b/packages/ipfs-core/src/components/block/put.js index 8303315b59..5e281e4700 100644 --- a/packages/ipfs-core/src/components/block/put.js +++ b/packages/ipfs-core/src/components/block/put.js @@ -4,7 +4,7 @@ const Block = require('ipld-block') const multihashing = require('multihashing-async') const CID = require('cids') const isIPFS = require('is-ipfs') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/block/rm.js b/packages/ipfs-core/src/components/block/rm.js index db7414e31d..8d2826f1c1 100644 --- a/packages/ipfs-core/src/components/block/rm.js +++ b/packages/ipfs-core/src/components/block/rm.js @@ -6,7 +6,7 @@ const { parallelMap, filter } = require('streaming-iterables') const { pipe } = require('it-pipe') const { PinTypes } = require('../pin/pin-manager') const { cleanCid } = require('./utils') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const BLOCK_RM_CONCURRENCY = 8 diff --git a/packages/ipfs-core/src/components/block/stat.js b/packages/ipfs-core/src/components/block/stat.js index 62f6c7ea3b..138cf1a6d7 100644 --- a/packages/ipfs-core/src/components/block/stat.js +++ b/packages/ipfs-core/src/components/block/stat.js @@ -1,7 +1,7 @@ 'use strict' const { cleanCid } = require('./utils') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/bootstrap/add.js b/packages/ipfs-core/src/components/bootstrap/add.js index a9f22c43a1..6b4dd623ff 100644 --- a/packages/ipfs-core/src/components/bootstrap/add.js +++ b/packages/ipfs-core/src/components/bootstrap/add.js @@ -1,7 +1,7 @@ 'use strict' const { isValidMultiaddr } = require('./utils') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {import('..').IPFSRepo} repo diff --git a/packages/ipfs-core/src/components/bootstrap/clear.js b/packages/ipfs-core/src/components/bootstrap/clear.js index 2637e4d345..1220ef8af7 100644 --- a/packages/ipfs-core/src/components/bootstrap/clear.js +++ b/packages/ipfs-core/src/components/bootstrap/clear.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const Multiaddr = require('multiaddr') /** diff --git a/packages/ipfs-core/src/components/bootstrap/list.js b/packages/ipfs-core/src/components/bootstrap/list.js index 421f16792f..5c445e4056 100644 --- a/packages/ipfs-core/src/components/bootstrap/list.js +++ b/packages/ipfs-core/src/components/bootstrap/list.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const Multiaddr = require('multiaddr') /** diff --git a/packages/ipfs-core/src/components/bootstrap/reset.js b/packages/ipfs-core/src/components/bootstrap/reset.js index 81a2dfc6bf..897c68a8eb 100644 --- a/packages/ipfs-core/src/components/bootstrap/reset.js +++ b/packages/ipfs-core/src/components/bootstrap/reset.js @@ -1,7 +1,7 @@ 'use strict' const defaultConfig = require('../../runtime/config-nodejs.js') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const Multiaddr = require('multiaddr') /** diff --git a/packages/ipfs-core/src/components/bootstrap/rm.js b/packages/ipfs-core/src/components/bootstrap/rm.js index 88300ea939..f96a2537f4 100644 --- a/packages/ipfs-core/src/components/bootstrap/rm.js +++ b/packages/ipfs-core/src/components/bootstrap/rm.js @@ -1,7 +1,7 @@ 'use strict' const { isValidMultiaddr } = require('./utils') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {import('..').IPFSRepo} repo diff --git a/packages/ipfs-core/src/components/cat.js b/packages/ipfs-core/src/components/cat.js index a2a793a6f0..6c47032286 100644 --- a/packages/ipfs-core/src/components/cat.js +++ b/packages/ipfs-core/src/components/cat.js @@ -1,7 +1,8 @@ 'use strict' const exporter = require('ipfs-unixfs-exporter') -const { normalizeCidPath, withTimeoutOption } = require('../utils') +const { normalizeCidPath } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/config.js b/packages/ipfs-core/src/components/config.js index b3df1c6f03..d3319ab055 100644 --- a/packages/ipfs-core/src/components/config.js +++ b/packages/ipfs-core/src/components/config.js @@ -1,7 +1,7 @@ 'use strict' const getDefaultConfig = require('../runtime/config-nodejs.js') -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const log = require('debug')('ipfs:core:config') /** diff --git a/packages/ipfs-core/src/components/dag/get.js b/packages/ipfs-core/src/components/dag/get.js index b0b305ea91..057ae4b4ae 100644 --- a/packages/ipfs-core/src/components/dag/get.js +++ b/packages/ipfs-core/src/components/dag/get.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const first = require('it-first') const last = require('it-last') const toCidAndPath = require('ipfs-core-utils/src/to-cid-and-path') diff --git a/packages/ipfs-core/src/components/dag/put.js b/packages/ipfs-core/src/components/dag/put.js index dc2e786f1d..54d45b8959 100644 --- a/packages/ipfs-core/src/components/dag/put.js +++ b/packages/ipfs-core/src/components/dag/put.js @@ -7,7 +7,7 @@ const multicodec = require('multicodec') * @returns {number} */ const nameToCodec = name => multicodec[name.toUpperCase().replace(/-/g, '_')] -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/dag/resolve.js b/packages/ipfs-core/src/components/dag/resolve.js index 30a7809e9f..51931d5d89 100644 --- a/packages/ipfs-core/src/components/dag/resolve.js +++ b/packages/ipfs-core/src/components/dag/resolve.js @@ -1,7 +1,7 @@ 'use strict' const CID = require('cids') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const toCidAndPath = require('ipfs-core-utils/src/to-cid-and-path') /** diff --git a/packages/ipfs-core/src/components/dag/tree.js b/packages/ipfs-core/src/components/dag/tree.js index 3beedd768e..e335d02b16 100644 --- a/packages/ipfs-core/src/components/dag/tree.js +++ b/packages/ipfs-core/src/components/dag/tree.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const toCidAndPath = require('ipfs-core-utils/src/to-cid-and-path') /** diff --git a/packages/ipfs-core/src/components/dht.js b/packages/ipfs-core/src/components/dht.js index f53865dabb..15c4caf6d0 100644 --- a/packages/ipfs-core/src/components/dht.js +++ b/packages/ipfs-core/src/components/dht.js @@ -3,7 +3,7 @@ const PeerId = require('peer-id') const CID = require('cids') const errCode = require('err-code') -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ libp2p, repo }) => { const { get, put, findProvs, findPeer, provide, query } = { diff --git a/packages/ipfs-core/src/components/dns.js b/packages/ipfs-core/src/components/dns.js index 0c77219e11..0d206989d5 100644 --- a/packages/ipfs-core/src/components/dns.js +++ b/packages/ipfs-core/src/components/dns.js @@ -2,7 +2,7 @@ // dns-nodejs gets replaced by dns-browser when webpacked/browserified const dns = require('../runtime/dns-nodejs') -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {string} domain diff --git a/packages/ipfs-core/src/components/files/chmod.js b/packages/ipfs-core/src/components/files/chmod.js index b23174ee78..01735d674a 100644 --- a/packages/ipfs-core/src/components/files/chmod.js +++ b/packages/ipfs-core/src/components/files/chmod.js @@ -19,7 +19,7 @@ const last = require('it-last') const cp = require('./cp') const rm = require('./rm') const persist = require('ipfs-unixfs-importer/src/utils/persist') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { flush: true, diff --git a/packages/ipfs-core/src/components/files/cp.js b/packages/ipfs-core/src/components/files/cp.js index aaee076f72..8936421a75 100644 --- a/packages/ipfs-core/src/components/files/cp.js +++ b/packages/ipfs-core/src/components/files/cp.js @@ -10,7 +10,7 @@ const addLink = require('./utils/add-link') const toMfsPath = require('./utils/to-mfs-path') const toSourcesAndDestination = require('./utils/to-sources-and-destination') const toTrail = require('./utils/to-trail') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { parents: false, diff --git a/packages/ipfs-core/src/components/files/flush.js b/packages/ipfs-core/src/components/files/flush.js index d3d57983a0..5df96ff0e1 100644 --- a/packages/ipfs-core/src/components/files/flush.js +++ b/packages/ipfs-core/src/components/files/flush.js @@ -1,7 +1,7 @@ 'use strict' const stat = require('./stat') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) const defaultOptions = { diff --git a/packages/ipfs-core/src/components/files/ls.js b/packages/ipfs-core/src/components/files/ls.js index 4610c34763..951074e86b 100644 --- a/packages/ipfs-core/src/components/files/ls.js +++ b/packages/ipfs-core/src/components/files/ls.js @@ -2,9 +2,7 @@ const exporter = require('ipfs-unixfs-exporter') const toMfsPath = require('./utils/to-mfs-path') -const { - withTimeoutOption -} = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {*} fsEntry diff --git a/packages/ipfs-core/src/components/files/mkdir.js b/packages/ipfs-core/src/components/files/mkdir.js index be6a59760a..22f0b8cfc1 100644 --- a/packages/ipfs-core/src/components/files/mkdir.js +++ b/packages/ipfs-core/src/components/files/mkdir.js @@ -10,7 +10,7 @@ const updateTree = require('./utils/update-tree') const addLink = require('./utils/add-link') const withMfsRoot = require('./utils/with-mfs-root') const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { parents: false, diff --git a/packages/ipfs-core/src/components/files/mv.js b/packages/ipfs-core/src/components/files/mv.js index 5f65e96e77..e58ad02963 100644 --- a/packages/ipfs-core/src/components/files/mv.js +++ b/packages/ipfs-core/src/components/files/mv.js @@ -3,7 +3,7 @@ const toSources = require('./utils/to-sources') const cp = require('./cp') const rm = require('./rm') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { parents: false, diff --git a/packages/ipfs-core/src/components/files/read.js b/packages/ipfs-core/src/components/files/read.js index e5cf791e51..ea697c85b7 100644 --- a/packages/ipfs-core/src/components/files/read.js +++ b/packages/ipfs-core/src/components/files/read.js @@ -4,7 +4,7 @@ const exporter = require('ipfs-unixfs-exporter') const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) const toMfsPath = require('./utils/to-mfs-path') const errCode = require('err-code') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { offset: 0, diff --git a/packages/ipfs-core/src/components/files/rm.js b/packages/ipfs-core/src/components/files/rm.js index 859203665c..d9677f99e8 100644 --- a/packages/ipfs-core/src/components/files/rm.js +++ b/packages/ipfs-core/src/components/files/rm.js @@ -7,7 +7,7 @@ const toSources = require('./utils/to-sources') const removeLink = require('./utils/remove-link') const toMfsPath = require('./utils/to-mfs-path') const toTrail = require('./utils/to-trail') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { recursive: false, diff --git a/packages/ipfs-core/src/components/files/stat.js b/packages/ipfs-core/src/components/files/stat.js index 6a038b57f5..4b83a664ff 100644 --- a/packages/ipfs-core/src/components/files/stat.js +++ b/packages/ipfs-core/src/components/files/stat.js @@ -5,7 +5,7 @@ const toMfsPath = require('./utils/to-mfs-path') const exporter = require('ipfs-unixfs-exporter') const log = require('debug')('ipfs:mfs:stat') const errCode = require('err-code') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { withLocal: false, diff --git a/packages/ipfs-core/src/components/files/touch.js b/packages/ipfs-core/src/components/files/touch.js index 77f2eda055..6d889ba07b 100644 --- a/packages/ipfs-core/src/components/files/touch.js +++ b/packages/ipfs-core/src/components/files/touch.js @@ -12,7 +12,7 @@ const updateMfsRoot = require('./utils/update-mfs-root') const { DAGNode } = require('ipld-dag-pb') const mc = require('multicodec') const mh = require('multihashing-async').multihash -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { /** @type {UnixTime|undefined} */ diff --git a/packages/ipfs-core/src/components/files/write.js b/packages/ipfs-core/src/components/files/write.js index 67b341406f..f65987c7a7 100644 --- a/packages/ipfs-core/src/components/files/write.js +++ b/packages/ipfs-core/src/components/files/write.js @@ -18,7 +18,7 @@ const { MFS_MAX_CHUNK_SIZE } = require('../../utils') const last = require('it-last') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const defaultOptions = { offset: 0, // the offset in the file to begin writing diff --git a/packages/ipfs-core/src/components/get.js b/packages/ipfs-core/src/components/get.js index 5763f89cb9..1b614136c9 100644 --- a/packages/ipfs-core/src/components/get.js +++ b/packages/ipfs-core/src/components/get.js @@ -2,7 +2,8 @@ const exporter = require('ipfs-unixfs-exporter') const errCode = require('err-code') -const { normalizeCidPath, mapFile, withTimeoutOption } = require('../utils') +const { normalizeCidPath, mapFile } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/id.js b/packages/ipfs-core/src/components/id.js index 226dcc239e..f319bf3e03 100644 --- a/packages/ipfs-core/src/components/id.js +++ b/packages/ipfs-core/src/components/id.js @@ -2,7 +2,7 @@ const pkgversion = require('../../package.json').version const multiaddr = require('multiaddr') -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const uint8ArrayToString = require('uint8arrays/to-string') /** diff --git a/packages/ipfs-core/src/components/key/export.js b/packages/ipfs-core/src/components/key/export.js index 0e136c7f70..e6766bfaa4 100644 --- a/packages/ipfs-core/src/components/key/export.js +++ b/packages/ipfs-core/src/components/key/export.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ keychain }) => { return withTimeoutOption((name, password, options = {}) => keychain.exportKey(name, password, options)) diff --git a/packages/ipfs-core/src/components/key/gen.js b/packages/ipfs-core/src/components/key/gen.js index e3eaea5ff0..76a270339e 100644 --- a/packages/ipfs-core/src/components/key/gen.js +++ b/packages/ipfs-core/src/components/key/gen.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ keychain }) => { return withTimeoutOption((name, options = {}) => { diff --git a/packages/ipfs-core/src/components/key/import.js b/packages/ipfs-core/src/components/key/import.js index 1aaba9559f..815e9063ec 100644 --- a/packages/ipfs-core/src/components/key/import.js +++ b/packages/ipfs-core/src/components/key/import.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ keychain }) => { return withTimeoutOption((name, pem, password, options) => keychain.importKey(name, pem, password, options)) diff --git a/packages/ipfs-core/src/components/key/info.js b/packages/ipfs-core/src/components/key/info.js index 209ee27eba..6000424535 100644 --- a/packages/ipfs-core/src/components/key/info.js +++ b/packages/ipfs-core/src/components/key/info.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ keychain }) => { return withTimeoutOption((name, options) => keychain.findKeyByName(name, options)) diff --git a/packages/ipfs-core/src/components/key/list.js b/packages/ipfs-core/src/components/key/list.js index 576132f7b6..62b7b13513 100644 --- a/packages/ipfs-core/src/components/key/list.js +++ b/packages/ipfs-core/src/components/key/list.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ keychain }) => { return withTimeoutOption((options) => keychain.listKeys(options)) diff --git a/packages/ipfs-core/src/components/key/rename.js b/packages/ipfs-core/src/components/key/rename.js index acd6a0c72f..be6e44ce4d 100644 --- a/packages/ipfs-core/src/components/key/rename.js +++ b/packages/ipfs-core/src/components/key/rename.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ keychain }) => { return withTimeoutOption(async (oldName, newName, options) => { diff --git a/packages/ipfs-core/src/components/key/rm.js b/packages/ipfs-core/src/components/key/rm.js index bd24943021..41b2b16d4b 100644 --- a/packages/ipfs-core/src/components/key/rm.js +++ b/packages/ipfs-core/src/components/key/rm.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ keychain }) => { return withTimeoutOption((name, options) => keychain.removeKey(name, options)) diff --git a/packages/ipfs-core/src/components/ls.js b/packages/ipfs-core/src/components/ls.js index e1cb13b2bd..81256669dc 100644 --- a/packages/ipfs-core/src/components/ls.js +++ b/packages/ipfs-core/src/components/ls.js @@ -2,7 +2,8 @@ const exporter = require('ipfs-unixfs-exporter') const errCode = require('err-code') -const { normalizeCidPath, mapFile, withTimeoutOption } = require('../utils') +const { normalizeCidPath, mapFile } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/name/publish.js b/packages/ipfs-core/src/components/name/publish.js index 217d2e41ca..bc87fb879d 100644 --- a/packages/ipfs-core/src/components/name/publish.js +++ b/packages/ipfs-core/src/components/name/publish.js @@ -9,7 +9,8 @@ const log = Object.assign(debug('ipfs:name:publish'), { error: debug('ipfs:name:publish:error') }) -const { OFFLINE_ERROR, normalizePath, withTimeoutOption } = require('../../utils') +const { OFFLINE_ERROR, normalizePath } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const { resolvePath } = require('./utils') /** diff --git a/packages/ipfs-core/src/components/name/pubsub/cancel.js b/packages/ipfs-core/src/components/name/pubsub/cancel.js index 1eb1b50985..f734efd2a1 100644 --- a/packages/ipfs-core/src/components/name/pubsub/cancel.js +++ b/packages/ipfs-core/src/components/name/pubsub/cancel.js @@ -1,7 +1,7 @@ 'use strict' const { getPubsubRouting } = require('./utils') -const { withTimeoutOption } = require('../../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/name/pubsub/state.js b/packages/ipfs-core/src/components/name/pubsub/state.js index f122da1396..a704de8a49 100644 --- a/packages/ipfs-core/src/components/name/pubsub/state.js +++ b/packages/ipfs-core/src/components/name/pubsub/state.js @@ -1,7 +1,7 @@ 'use strict' const { getPubsubRouting } = require('./utils') -const { withTimeoutOption } = require('../../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ ipns, options: constructorOptions }) => { /** diff --git a/packages/ipfs-core/src/components/name/pubsub/subs.js b/packages/ipfs-core/src/components/name/pubsub/subs.js index 6e84f8ec3c..a68b5eb05e 100644 --- a/packages/ipfs-core/src/components/name/pubsub/subs.js +++ b/packages/ipfs-core/src/components/name/pubsub/subs.js @@ -1,7 +1,7 @@ 'use strict' const { getPubsubRouting } = require('./utils') -const { withTimeoutOption } = require('../../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ ipns, options: constructorOptions }) => { /** diff --git a/packages/ipfs-core/src/components/name/resolve.js b/packages/ipfs-core/src/components/name/resolve.js index 05f74fb0f2..0589a28dfa 100644 --- a/packages/ipfs-core/src/components/name/resolve.js +++ b/packages/ipfs-core/src/components/name/resolve.js @@ -10,7 +10,8 @@ const log = Object.assign(debug('ipfs:name:resolve'), { error: debug('ipfs:name:resolve:error') }) -const { OFFLINE_ERROR, withTimeoutOption } = require('../../utils') +const { OFFLINE_ERROR } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * diff --git a/packages/ipfs-core/src/components/object/data.js b/packages/ipfs-core/src/components/object/data.js index 7143daf308..8e3bdfb84e 100644 --- a/packages/ipfs-core/src/components/object/data.js +++ b/packages/ipfs-core/src/components/object/data.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ ipld, preload }) => { const get = require('./get')({ ipld, preload }) diff --git a/packages/ipfs-core/src/components/object/get.js b/packages/ipfs-core/src/components/object/get.js index 70b8d6785c..669898b77e 100644 --- a/packages/ipfs-core/src/components/object/get.js +++ b/packages/ipfs-core/src/components/object/get.js @@ -2,7 +2,7 @@ const CID = require('cids') const errCode = require('err-code') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const uint8ArrayFromString = require('uint8arrays/from-string') function normalizeMultihash (multihash, enc) { diff --git a/packages/ipfs-core/src/components/object/links.js b/packages/ipfs-core/src/components/object/links.js index 8d9afb1929..d6a7b8d67c 100644 --- a/packages/ipfs-core/src/components/object/links.js +++ b/packages/ipfs-core/src/components/object/links.js @@ -3,7 +3,7 @@ const dagPB = require('ipld-dag-pb') const DAGLink = dagPB.DAGLink const CID = require('cids') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') function findLinks (node, links = []) { for (const key in node) { diff --git a/packages/ipfs-core/src/components/object/new.js b/packages/ipfs-core/src/components/object/new.js index f7e24fdbbc..315ae9efcb 100644 --- a/packages/ipfs-core/src/components/object/new.js +++ b/packages/ipfs-core/src/components/object/new.js @@ -4,7 +4,7 @@ const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode const multicodec = require('multicodec') const Unixfs = require('ipfs-unixfs') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ ipld, preload }) => { return withTimeoutOption(async function _new (options = {}) { diff --git a/packages/ipfs-core/src/components/object/patch/add-link.js b/packages/ipfs-core/src/components/object/patch/add-link.js index 77f0d26708..123a92a39d 100644 --- a/packages/ipfs-core/src/components/object/patch/add-link.js +++ b/packages/ipfs-core/src/components/object/patch/add-link.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ ipld, gcLock, preload }) => { const get = require('../get')({ ipld, preload }) diff --git a/packages/ipfs-core/src/components/object/patch/append-data.js b/packages/ipfs-core/src/components/object/patch/append-data.js index df31ca668c..3ddd9d700e 100644 --- a/packages/ipfs-core/src/components/object/patch/append-data.js +++ b/packages/ipfs-core/src/components/object/patch/append-data.js @@ -1,7 +1,7 @@ 'use strict' const { DAGNode } = require('ipld-dag-pb') -const { withTimeoutOption } = require('../../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const uint8ArrayConcat = require('uint8arrays/concat') module.exports = ({ ipld, gcLock, preload }) => { diff --git a/packages/ipfs-core/src/components/object/patch/rm-link.js b/packages/ipfs-core/src/components/object/patch/rm-link.js index 9d028017d0..ed24d8d168 100644 --- a/packages/ipfs-core/src/components/object/patch/rm-link.js +++ b/packages/ipfs-core/src/components/object/patch/rm-link.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ ipld, gcLock, preload }) => { const get = require('../get')({ ipld, preload }) diff --git a/packages/ipfs-core/src/components/object/patch/set-data.js b/packages/ipfs-core/src/components/object/patch/set-data.js index 9a9e4b3a1f..e556a8ea3c 100644 --- a/packages/ipfs-core/src/components/object/patch/set-data.js +++ b/packages/ipfs-core/src/components/object/patch/set-data.js @@ -1,7 +1,7 @@ 'use strict' const { DAGNode } = require('ipld-dag-pb') -const { withTimeoutOption } = require('../../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ ipld, gcLock, preload }) => { const get = require('../get')({ ipld, preload }) diff --git a/packages/ipfs-core/src/components/object/put.js b/packages/ipfs-core/src/components/object/put.js index cbbe4f2e23..65cf70fcb9 100644 --- a/packages/ipfs-core/src/components/object/put.js +++ b/packages/ipfs-core/src/components/object/put.js @@ -5,7 +5,7 @@ const DAGNode = dagPB.DAGNode const DAGLink = dagPB.DAGLink const mh = require('multihashing-async').multihash const multicodec = require('multicodec') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const uint8ArrayToString = require('uint8arrays/to-string') const uint8ArrayFromString = require('uint8arrays/from-string') diff --git a/packages/ipfs-core/src/components/object/stat.js b/packages/ipfs-core/src/components/object/stat.js index 511415c4c7..699c9b4d3e 100644 --- a/packages/ipfs-core/src/components/object/stat.js +++ b/packages/ipfs-core/src/components/object/stat.js @@ -1,7 +1,7 @@ 'use strict' const dagPB = require('ipld-dag-pb') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ ipld, preload }) => { const get = require('./get')({ ipld, preload }) diff --git a/packages/ipfs-core/src/components/pin/add-all.js b/packages/ipfs-core/src/components/pin/add-all.js index 24889323e3..09892c24e6 100644 --- a/packages/ipfs-core/src/components/pin/add-all.js +++ b/packages/ipfs-core/src/components/pin/add-all.js @@ -1,9 +1,10 @@ /* eslint max-nested-callbacks: ["error", 8] */ 'use strict' -const { resolvePath, withTimeoutOption } = require('../../utils') +const { resolvePath } = require('../../utils') const PinManager = require('./pin-manager') const { PinTypes } = PinManager +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** @type {(source:Source) => AsyncIterable} */ const normaliseInput = require('ipfs-core-utils/src/pins/normalise-input') diff --git a/packages/ipfs-core/src/components/pin/ls.js b/packages/ipfs-core/src/components/pin/ls.js index 179585d620..4764e32d94 100644 --- a/packages/ipfs-core/src/components/pin/ls.js +++ b/packages/ipfs-core/src/components/pin/ls.js @@ -4,7 +4,8 @@ const PinManager = require('./pin-manager') const { PinTypes } = PinManager const normaliseInput = require('ipfs-core-utils/src/pins/normalise-input') -const { resolvePath, withTimeoutOption } = require('../../utils') +const { resolvePath } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') function toPin (type, cid, metadata) { const output = { diff --git a/packages/ipfs-core/src/components/pin/rm-all.js b/packages/ipfs-core/src/components/pin/rm-all.js index 06f0887fe9..e02d2e7bfb 100644 --- a/packages/ipfs-core/src/components/pin/rm-all.js +++ b/packages/ipfs-core/src/components/pin/rm-all.js @@ -1,7 +1,8 @@ 'use strict' const normaliseInput = require('ipfs-core-utils/src/pins/normalise-input') -const { resolvePath, withTimeoutOption } = require('../../utils') +const { resolvePath } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const { PinTypes } = require('./pin-manager') /** diff --git a/packages/ipfs-core/src/components/ping.js b/packages/ipfs-core/src/components/ping.js index 24ed95dbff..a3285be652 100644 --- a/packages/ipfs-core/src/components/ping.js +++ b/packages/ipfs-core/src/components/ping.js @@ -3,7 +3,7 @@ const PeerId = require('peer-id') /** @type {{success:true, time:0, text: ''}} */ const basePacket = { success: true, time: 0, text: '' } -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/pubsub.js b/packages/ipfs-core/src/components/pubsub.js index 531a85e359..876594d5aa 100644 --- a/packages/ipfs-core/src/components/pubsub.js +++ b/packages/ipfs-core/src/components/pubsub.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const errCode = require('err-code') module.exports = ({ libp2p }) => { diff --git a/packages/ipfs-core/src/components/refs/index.js b/packages/ipfs-core/src/components/refs/index.js index 669fd0cbe1..15aee1a3d0 100644 --- a/packages/ipfs-core/src/components/refs/index.js +++ b/packages/ipfs-core/src/components/refs/index.js @@ -6,7 +6,7 @@ const { DAGNode } = require('ipld-dag-pb') const { normalizeCidPath } = require('../../utils') const { Errors } = require('interface-datastore') const ERR_NOT_FOUND = Errors.notFoundError().code -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const Format = { default: '', diff --git a/packages/ipfs-core/src/components/refs/local.js b/packages/ipfs-core/src/components/refs/local.js index 871c5440dd..92411408a1 100644 --- a/packages/ipfs-core/src/components/refs/local.js +++ b/packages/ipfs-core/src/components/refs/local.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = function ({ repo }) { return withTimeoutOption(async function * refsLocal (options = {}) { diff --git a/packages/ipfs-core/src/components/repo/gc.js b/packages/ipfs-core/src/components/repo/gc.js index c2b8b26afa..5a8bee05c8 100644 --- a/packages/ipfs-core/src/components/repo/gc.js +++ b/packages/ipfs-core/src/components/repo/gc.js @@ -2,7 +2,8 @@ const CID = require('cids') const log = require('debug')('ipfs:repo:gc') -const { MFS_ROOT_KEY, withTimeoutOption } = require('../../utils') +const { MFS_ROOT_KEY } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const { Errors } = require('interface-datastore') const ERR_NOT_FOUND = Errors.notFoundError().code const { parallelMerge, transform, map } = require('streaming-iterables') diff --git a/packages/ipfs-core/src/components/repo/stat.js b/packages/ipfs-core/src/components/repo/stat.js index 9194a00ded..3cf268254a 100644 --- a/packages/ipfs-core/src/components/repo/stat.js +++ b/packages/ipfs-core/src/components/repo/stat.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ repo }) => { return withTimeoutOption(async function stat (options) { diff --git a/packages/ipfs-core/src/components/repo/version.js b/packages/ipfs-core/src/components/repo/version.js index 4622f667b7..3d73e89045 100644 --- a/packages/ipfs-core/src/components/repo/version.js +++ b/packages/ipfs-core/src/components/repo/version.js @@ -1,7 +1,7 @@ 'use strict' const { repoVersion } = require('ipfs-repo') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ repo }) => { /** diff --git a/packages/ipfs-core/src/components/resolve.js b/packages/ipfs-core/src/components/resolve.js index 9b14d4516c..8e9672d6ab 100644 --- a/packages/ipfs-core/src/components/resolve.js +++ b/packages/ipfs-core/src/components/resolve.js @@ -3,7 +3,7 @@ const isIpfs = require('is-ipfs') const CID = require('cids') const { cidToString } = require('ipfs-core-utils/src/cid') -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') /** * @param {Object} config diff --git a/packages/ipfs-core/src/components/start.js b/packages/ipfs-core/src/components/start.js index 1f1f286eb2..d421707ef4 100644 --- a/packages/ipfs-core/src/components/start.js +++ b/packages/ipfs-core/src/components/start.js @@ -11,7 +11,7 @@ const routingConfig = require('../ipns/routing/config') const { AlreadyInitializedError, NotEnabledError } = require('../errors') const Components = require('./') const createMfsPreload = require('../mfs-preload') -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') const WEBSOCKET_STAR_PROTO_CODE = 479 diff --git a/packages/ipfs-core/src/components/stats/bw.js b/packages/ipfs-core/src/components/stats/bw.js index 3c2319187c..cb5b13fe7a 100644 --- a/packages/ipfs-core/src/components/stats/bw.js +++ b/packages/ipfs-core/src/components/stats/bw.js @@ -3,7 +3,7 @@ const Big = require('bignumber.js').default const parseDuration = require('parse-duration').default const errCode = require('err-code') -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') function getBandwidthStats (libp2p, opts) { let stats diff --git a/packages/ipfs-core/src/components/stop.js b/packages/ipfs-core/src/components/stop.js index 88e320aaea..10d03a8809 100644 --- a/packages/ipfs-core/src/components/stop.js +++ b/packages/ipfs-core/src/components/stop.js @@ -3,7 +3,7 @@ const defer = require('p-defer') const { NotStartedError, AlreadyInitializedError } = require('../errors') const Components = require('./') -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ apiManager, diff --git a/packages/ipfs-core/src/components/swarm/addrs.js b/packages/ipfs-core/src/components/swarm/addrs.js index cca0c83d32..42301f414c 100644 --- a/packages/ipfs-core/src/components/swarm/addrs.js +++ b/packages/ipfs-core/src/components/swarm/addrs.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ libp2p }) => { return withTimeoutOption(async function addrs (options) { // eslint-disable-line require-await diff --git a/packages/ipfs-core/src/components/swarm/connect.js b/packages/ipfs-core/src/components/swarm/connect.js index aafa951742..8f287505e1 100644 --- a/packages/ipfs-core/src/components/swarm/connect.js +++ b/packages/ipfs-core/src/components/swarm/connect.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ libp2p }) => { return withTimeoutOption(function connect (addr, options) { diff --git a/packages/ipfs-core/src/components/swarm/disconnect.js b/packages/ipfs-core/src/components/swarm/disconnect.js index 7d09d43edb..9ef3966606 100644 --- a/packages/ipfs-core/src/components/swarm/disconnect.js +++ b/packages/ipfs-core/src/components/swarm/disconnect.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ libp2p }) => { return withTimeoutOption(function disconnect (addr, options) { diff --git a/packages/ipfs-core/src/components/swarm/local-addrs.js b/packages/ipfs-core/src/components/swarm/local-addrs.js index 10b7f04cc3..51912f8259 100644 --- a/packages/ipfs-core/src/components/swarm/local-addrs.js +++ b/packages/ipfs-core/src/components/swarm/local-addrs.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ multiaddrs }) => { return withTimeoutOption(async function localAddrs () { // eslint-disable-line require-await diff --git a/packages/ipfs-core/src/components/swarm/peers.js b/packages/ipfs-core/src/components/swarm/peers.js index 4a0654cad7..a3e1199e95 100644 --- a/packages/ipfs-core/src/components/swarm/peers.js +++ b/packages/ipfs-core/src/components/swarm/peers.js @@ -1,6 +1,6 @@ 'use strict' -const { withTimeoutOption } = require('../../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ libp2p }) => { return withTimeoutOption(async function peers (options = {}) { // eslint-disable-line require-await diff --git a/packages/ipfs-core/src/components/version.js b/packages/ipfs-core/src/components/version.js index 72b304a62f..f9024bb657 100644 --- a/packages/ipfs-core/src/components/version.js +++ b/packages/ipfs-core/src/components/version.js @@ -1,7 +1,7 @@ 'use strict' const pkg = require('../../package.json') -const { withTimeoutOption } = require('../utils') +const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') // gitHead is defined in published versions const meta = { gitHead: '', ...pkg } diff --git a/packages/ipfs-core/src/errors.js b/packages/ipfs-core/src/errors.js index 465576b322..e15dec7260 100644 --- a/packages/ipfs-core/src/errors.js +++ b/packages/ipfs-core/src/errors.js @@ -54,14 +54,3 @@ class NotEnabledError extends Error { NotEnabledError.code = 'ERR_NOT_ENABLED' exports.NotEnabledError = NotEnabledError - -class TimeoutError extends Error { - constructor (message = 'request timed out') { - super(message) - this.name = 'TimeoutError' - this.code = TimeoutError.code - } -} - -TimeoutError.code = 'ERR_TIMEOUT' -exports.TimeoutError = TimeoutError diff --git a/packages/ipfs-core/src/utils.js b/packages/ipfs-core/src/utils.js index 18fcad2326..e2f7bce6d9 100644 --- a/packages/ipfs-core/src/utils.js +++ b/packages/ipfs-core/src/utils.js @@ -3,11 +3,7 @@ const isIpfs = require('is-ipfs') const CID = require('cids') -const TimeoutController = require('timeout-abort-controller') -const { anySignal } = require('any-signal') -const parseDuration = require('parse-duration').default const Key = require('interface-datastore').Key -const { TimeoutError } = require('./errors') const errCode = require('err-code') const toCidAndPath = require('ipfs-core-utils/src/to-cid-and-path') @@ -217,105 +213,7 @@ const mapFile = (file, options = {}) => { * @template {Record} ExtraOptions */ -/** - * @template {any[]} ARGS - * @template {Promise | AsyncIterable} R - The return type of `fn` - * @param {Fn} fn - * @param {number} [optionsArgIndex] - * @returns {Fn} - */ -function withTimeoutOption (fn, optionsArgIndex) { - // eslint-disable-next-line - return /** @returns {R} */(/** @type {ARGS} */...args) => { - const options = args[optionsArgIndex == null ? args.length - 1 : optionsArgIndex] - if (!options || !options.timeout) return fn(...args) - - const timeout = typeof options.timeout === 'string' - ? parseDuration(options.timeout) - : options.timeout - - const controller = new TimeoutController(timeout) - - options.signal = anySignal([options.signal, controller.signal]) - - const fnRes = fn(...args) - // eslint-disable-next-line promise/param-names - const timeoutPromise = new Promise((_resolve, reject) => { - controller.signal.addEventListener('abort', () => { - reject(new TimeoutError()) - }) - }) - - const start = Date.now() - - const maybeThrowTimeoutError = () => { - if (controller.signal.aborted) { - throw new TimeoutError() - } - - const timeTaken = Date.now() - start - - // if we have starved the event loop by adding microtasks, we could have - // timed out already but the TimeoutController will never know because it's - // setTimeout will not fire until we stop adding microtasks - if (timeTaken > timeout) { - controller.abort() - throw new TimeoutError() - } - } - - if (fnRes[Symbol.asyncIterator]) { - // @ts-ignore - return (async function * () { - const it = fnRes[Symbol.asyncIterator]() - - try { - while (true) { - const { value, done } = await Promise.race([it.next(), timeoutPromise]) - - if (done) { - break - } - - maybeThrowTimeoutError() - - yield value - } - } catch (err) { - maybeThrowTimeoutError() - - throw err - } finally { - controller.clear() - - if (it.return) { - it.return() - } - } - })() - } - - // @ts-ignore - return (async () => { - try { - const res = await Promise.race([fnRes, timeoutPromise]) - - maybeThrowTimeoutError() - - return res - } catch (err) { - maybeThrowTimeoutError() - - throw err - } finally { - controller.clear() - } - })() - } -} - exports.normalizePath = normalizePath exports.normalizeCidPath = normalizeCidPath exports.resolvePath = resolvePath exports.mapFile = mapFile -exports.withTimeoutOption = withTimeoutOption diff --git a/packages/ipfs-http-client/src/files/write.js b/packages/ipfs-http-client/src/files/write.js index 08b598397a..329834c0f5 100644 --- a/packages/ipfs-http-client/src/files/write.js +++ b/packages/ipfs-http-client/src/files/write.js @@ -1,7 +1,7 @@ 'use strict' const modeToString = require('../lib/mode-to-string') -const mtimeToObject = require('../lib/mtime-to-object') +const { mtimeToObject } = require('ipfs-core-utils/src/files/normalise-input/utils') const configure = require('../lib/configure') const multipartRequest = require('../lib/multipart-request') const toUrlSearchParams = require('../lib/to-url-search-params') diff --git a/packages/ipfs-http-client/src/lib/mtime-to-object.js b/packages/ipfs-http-client/src/lib/mtime-to-object.js deleted file mode 100644 index f7c4d3dbc9..0000000000 --- a/packages/ipfs-http-client/src/lib/mtime-to-object.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict' - -/** - * @param {any} mtime - * @returns {{secs:number, nsecs:number}|undefined} - */ -module.exports = function parseMtime (mtime) { - if (mtime == null) { - return undefined - } - - // Javascript Date - if (mtime instanceof Date) { - const ms = mtime.getTime() - const secs = Math.floor(ms / 1000) - - return { - secs: secs, - nsecs: (ms - (secs * 1000)) * 1000 - } - } - - // { secs, nsecs } - if (Object.prototype.hasOwnProperty.call(mtime, 'secs')) { - return { - secs: mtime.secs, - nsecs: mtime.nsecs - } - } - - // UnixFS TimeSpec - if (Object.prototype.hasOwnProperty.call(mtime, 'Seconds')) { - return { - secs: mtime.Seconds, - nsecs: mtime.FractionalNanoseconds - } - } - - // process.hrtime() - if (Array.isArray(mtime)) { - return { - secs: mtime[0], - nsecs: mtime[1] - } - } - /* - TODO: https://github.com/ipfs/aegir/issues/487 - - // process.hrtime.bigint() - if (typeof mtime === 'bigint') { - const secs = mtime / BigInt(1e9) - const nsecs = mtime - (secs * BigInt(1e9)) - - return { - secs: parseInt(secs), - nsecs: parseInt(nsecs) - } - } - */ -} diff --git a/packages/ipfs-http-client/src/lib/multipart-request.browser.js b/packages/ipfs-http-client/src/lib/multipart-request.browser.js index 417282a7d8..6c6ee87996 100644 --- a/packages/ipfs-http-client/src/lib/multipart-request.browser.js +++ b/packages/ipfs-http-client/src/lib/multipart-request.browser.js @@ -4,7 +4,6 @@ // version and fail. const normaliseInput = require('ipfs-core-utils/src/files/normalise-input/index.browser') const modeToString = require('./mode-to-string') -const mtimeToObject = require('./mtime-to-object') const { File, FormData } = require('ipfs-utils/src/globalthis') async function multipartRequest (source = '', abortController, headers = {}) { @@ -28,9 +27,8 @@ async function multipartRequest (source = '', abortController, headers = {}) { qs.push(`mode=${modeToString(mode)}`) } - const time = mtimeToObject(mtime) - if (time != null) { - const { secs, nsecs } = time + if ((mtime) != null) { + const { secs, nsecs } = (mtime) qs.push(`mtime=${secs}`) diff --git a/packages/ipfs-http-client/src/lib/multipart-request.node.js b/packages/ipfs-http-client/src/lib/multipart-request.node.js index d707f4f2b4..61d82c12e4 100644 --- a/packages/ipfs-http-client/src/lib/multipart-request.node.js +++ b/packages/ipfs-http-client/src/lib/multipart-request.node.js @@ -3,7 +3,6 @@ const normaliseInput = require('ipfs-core-utils/src/files/normalise-input') const { nanoid } = require('nanoid') const modeToString = require('./mode-to-string') -const mtimeToObject = require('./mtime-to-object') const merge = require('merge-options').bind({ ignoreUndefined: true }) const toStream = require('it-to-stream') @@ -36,9 +35,8 @@ async function multipartRequest (source = '', abortController, headers = {}, bou qs.push(`mode=${modeToString(mode)}`) } - const time = mtimeToObject(mtime) - if (time != null) { - const { secs, nsecs } = time + if (mtime != null) { + const { secs, nsecs } = mtime qs.push(`mtime=${secs}`) diff --git a/packages/ipfs-http-client/src/lib/to-url-search-params.js b/packages/ipfs-http-client/src/lib/to-url-search-params.js index 479dcbb840..2067b0be02 100644 --- a/packages/ipfs-http-client/src/lib/to-url-search-params.js +++ b/packages/ipfs-http-client/src/lib/to-url-search-params.js @@ -1,7 +1,7 @@ 'use strict' const modeToString = require('./mode-to-string') -const mtimeToObject = require('./mtime-to-object') +const { mtimeToObject } = require('ipfs-core-utils/src/files/normalise-input/utils') /** * @param {*} params diff --git a/packages/ipfs-http-server/src/index.js b/packages/ipfs-http-server/src/index.js index 5439be23d2..062814abf9 100644 --- a/packages/ipfs-http-server/src/index.js +++ b/packages/ipfs-http-server/src/index.js @@ -182,7 +182,7 @@ class HttpApi { const controller = new AbortController() request.app.signal = controller.signal - // abort the reqest if the client disconnects + // abort the request if the client disconnects request.events.once('disconnect', () => { controller.abort() }) diff --git a/packages/ipfs/test/interface-http-go.js b/packages/ipfs/test/interface-http-go.js index 7e091509f5..c3a60d6d0d 100644 --- a/packages/ipfs/test/interface-http-go.js +++ b/packages/ipfs/test/interface-http-go.js @@ -49,6 +49,14 @@ describe('interface-ipfs-core over ipfs-http-client tests against go-ipfs', () = { name: 'should override raw leaves when file is smaller than one block and metadata is present', reason: 'TODO not implemented in go-ipfs yet' + }, + { + name: 'should support bidirectional streaming', + reason: 'Not supported by http' + }, + { + name: 'should error during add-all stream', + reason: 'Not supported by http' } ] }) diff --git a/packages/ipfs/test/interface-http-js.js b/packages/ipfs/test/interface-http-js.js index 37fd0347ed..8126c739f3 100644 --- a/packages/ipfs/test/interface-http-js.js +++ b/packages/ipfs/test/interface-http-js.js @@ -15,16 +15,25 @@ describe('interface-ipfs-core over ipfs-http-client tests against js-ipfs', func }) tests.root(commonFactory, { - skip: isNode ? [{ - name: 'should fail when passed invalid input', - reason: 'node-fetch cannot detect errors in streaming bodies - https://github.com/node-fetch/node-fetch/issues/753' - }, { - name: 'should not add from an invalid url', - reason: 'node-fetch cannot detect errors in streaming bodies - https://github.com/node-fetch/node-fetch/issues/753' - }] : [{ - name: 'should add with mtime as hrtime', - reason: 'Not designed to run in the browser' - }] + skip: [ + { + name: 'should support bidirectional streaming', + reason: 'Not supported by http' + }, + { + name: 'should error during add-all stream', + reason: 'Not supported by http' + }] + .concat(isNode ? [{ + name: 'should fail when passed invalid input', + reason: 'node-fetch cannot detect errors in streaming bodies - https://github.com/node-fetch/node-fetch/issues/753' + }, { + name: 'should not add from an invalid url', + reason: 'node-fetch cannot detect errors in streaming bodies - https://github.com/node-fetch/node-fetch/issues/753' + }] : [{ + name: 'should add with mtime as hrtime', + reason: 'Not designed to run in the browser' + }]) }) tests.bitswap(commonFactory)