Files: ef1a2eaa163709173d29cc9f3fc446695e466fe7 / fetch.js
847 bytesRaw
1 | var BinaryXHR = require('binary-xhr') |
2 | var hasHash = /([A-Za-z0-9\/+]{43}=)\.sha256/ |
3 | var isUrl = /^https?:\/\// |
4 | |
5 | var u = require('./util') |
6 | |
7 | //before calling this, always check whether you alread have |
8 | //a file with this hash. |
9 | exports = module.exports = function (url, cb) { |
10 | var id = exports.isSecureUrl(secure_url) |
11 | if(!id) |
12 | return cb(new Error('is not a secure url:'+secure_url)) |
13 | |
14 | BinaryXHR(secure_url, function (err, data) { |
15 | if(err) |
16 | return cb(new Error('could not retrive secure url:'+err)) |
17 | if(!data || !data.length) |
18 | return cb(new Error('empty response from: '+secure_url)) |
19 | u.hash(data, function (err, _id) { |
20 | if(_id !== id) cb(u.HashError(_id, id)) |
21 | |
22 | cb(null, data, id) |
23 | }) |
24 | }) |
25 | } |
26 | |
27 | exports.isSecureUrl = function (string) { |
28 | var h = hasHash.exec(string) |
29 | return isUrl.test(string) && h && h[1] |
30 | } |
31 | |
32 | |
33 |
Built with git-ssb-web