This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add preload tests for ipfs.files.add and make fixes
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
- Loading branch information
Showing
11 changed files
with
336 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,51 @@ | ||
const get = require('lodash/get') | ||
'use strict' | ||
|
||
const setImmediate = require('async/setImmediate') | ||
const each = require('async/each') | ||
const toUri = require('multiaddr-to-uri') | ||
const debug = require('debug') | ||
const preload = require('./runtime/preload-nodejs') | ||
|
||
const log = debug('jsipfs:preload') | ||
log.error = debug('jsipfs:preload:error') | ||
|
||
// Tools like IPFS Companion redirect requests to IPFS gateways to your local | ||
// gateway. This is a hint to those tools that they shouldn't redirect these | ||
// requests as they will effectively disable the preloading. | ||
const redirectOptOutHint = 'x-ipfs-preload' | ||
|
||
module.exports = self => { | ||
const enabled = get(self._options, 'preload.enabled') | ||
const gateways = get(self._options, 'preload.addresses', []) | ||
.map(address => address.gateway) | ||
.filter(Boolean) | ||
module.exports = (options) => { | ||
options = options || {} | ||
options.enabled = !!options.enabled | ||
options.gateways = options.gateways || [] | ||
|
||
if (!enabled || !gateways.length) { | ||
if (!options.enabled || !options.gateways.length) { | ||
return (_, callback) => { | ||
if (!callback) return | ||
setImmediate(() => callback()) | ||
if (callback) { | ||
setImmediate(() => callback()) | ||
} | ||
} | ||
} | ||
|
||
const noop = (err) => { | ||
if (err) log.error(err) | ||
} | ||
|
||
return (cid, callback) => { | ||
each(gateways, (gatewayAddr, cb) => { | ||
callback = callback || noop | ||
|
||
each(options.gateways, (gatewayAddr, cb) => { | ||
let gatewayUri | ||
|
||
try { | ||
gatewayUri = toUri(gatewayAddr) | ||
gatewayUri = gatewayUri.startsWith('http') ? gatewayUri : `http://${gatewayUri}` | ||
} catch (err) { | ||
return cb(err) | ||
} | ||
|
||
const preloadUrl = `${gatewayUri}/ipfs/${cid.toBaseEncodedString()}#${redirectOptOutHint}` | ||
|
||
preload(preloadUrl, cb) | ||
const url = `${gatewayUri}/ipfs/${cid.toBaseEncodedString()}#${redirectOptOutHint}` | ||
preload(url, cb) | ||
}, callback) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.