@erebos/api-bzz-base
Advanced tools
Comparing version 0.8.1 to 0.9.0
"use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
exports.__esModule = true; | ||
exports.getFeedTopic = exports.createFeedDigest = exports.bytesToHexValue = void 0; | ||
exports.createFeedDigest = createFeedDigest; | ||
exports.getFeedTopic = getFeedTopic; | ||
var _hex = _interopRequireWildcard(require("@erebos/hex")); | ||
var _hex = require("@erebos/hex"); | ||
@@ -18,11 +17,5 @@ var _keccak = require("@erebos/keccak256"); | ||
var bytesToHexValue = function bytesToHexValue(bytes) { | ||
return (0, _hex.hexValueType)('0x' + Buffer.from(bytes).toString('hex')); | ||
}; | ||
function createFeedDigest(meta, data) { | ||
var topicBuffer = (0, _hex.createHex)(meta.feed.topic).toBuffer(); | ||
exports.bytesToHexValue = bytesToHexValue; | ||
var createFeedDigest = function createFeedDigest(meta, data) { | ||
var topicBuffer = (0, _hex.default)(meta.feed.topic).toBuffer(); | ||
if (topicBuffer.length !== FEED_TOPIC_LENGTH) { | ||
@@ -32,3 +25,3 @@ throw new Error('Invalid topic length'); | ||
var userBuffer = (0, _hex.default)(meta.feed.user).toBuffer(); | ||
var userBuffer = (0, _hex.createHex)(meta.feed.user).toBuffer(); | ||
@@ -45,11 +38,9 @@ if (userBuffer.length !== FEED_USER_LENGTH) { | ||
levelBuffer.writeUInt8(meta.epoch.level, 0); | ||
var payload = Buffer.concat([headerBuffer, topicBuffer, userBuffer, timeBuffer, levelBuffer, (0, _hex.default)(data).toBuffer()]); | ||
var payload = Buffer.concat([headerBuffer, topicBuffer, userBuffer, timeBuffer, levelBuffer, (0, _hex.createHex)(data).toBuffer()]); | ||
return (0, _keccak.hash)(payload); | ||
}; | ||
} | ||
exports.createFeedDigest = createFeedDigest; | ||
function getFeedTopic(params) { | ||
var topicHex = (0, _hex.createHex)(params.topic || Buffer.alloc(FEED_TOPIC_LENGTH)); | ||
var getFeedTopic = function getFeedTopic(params) { | ||
var topicHex = (0, _hex.default)(params.topic || Buffer.alloc(FEED_TOPIC_LENGTH)); | ||
if (params.name == null) { | ||
@@ -61,8 +52,6 @@ return topicHex.value; | ||
var topic = topicHex.toBuffer(); | ||
var bytes = Array(FEED_TOPIC_LENGTH).fill().map(function (_, i) { | ||
var bytes = Array(FEED_TOPIC_LENGTH).fill(0).map(function (_, i) { | ||
return topic[i] ^ name[i]; | ||
}); | ||
return bytesToHexValue(bytes); | ||
}; | ||
exports.getFeedTopic = getFeedTopic; | ||
return (0, _hex.toHexValue)(bytes); | ||
} |
1097
cjs/index.js
"use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
@@ -15,8 +13,20 @@ | ||
resText: true, | ||
resSwarmHash: true | ||
resHex: true, | ||
resSwarmHash: true, | ||
BaseBzz: true | ||
}; | ||
exports.default = exports.resSwarmHash = exports.resText = exports.resJSON = exports.resOrError = exports.HTTPError = exports.getModeProtocol = exports.BZZ_MODE_PROTOCOLS = void 0; | ||
exports.getModeProtocol = getModeProtocol; | ||
exports.resOrError = resOrError; | ||
exports.resJSON = resJSON; | ||
exports.resText = resText; | ||
exports.resHex = resHex; | ||
exports.resSwarmHash = resSwarmHash; | ||
exports.BaseBzz = exports.HTTPError = exports.BZZ_MODE_PROTOCOLS = void 0; | ||
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); | ||
@@ -30,3 +40,3 @@ | ||
var _hex = _interopRequireWildcard(require("@erebos/hex")); | ||
var _hex = require("@erebos/hex"); | ||
@@ -60,8 +70,6 @@ var _rxjs = require("rxjs"); | ||
var getModeProtocol = function getModeProtocol(mode) { | ||
function getModeProtocol(mode) { | ||
return mode && BZZ_MODE_PROTOCOLS[mode] || BZZ_MODE_PROTOCOLS.default; | ||
}; | ||
} | ||
exports.getModeProtocol = getModeProtocol; | ||
var HTTPError = | ||
@@ -86,37 +94,127 @@ /*#__PURE__*/ | ||
var resOrError = function resOrError(res) { | ||
return res.ok ? Promise.resolve(res) : Promise.reject(new HTTPError(res.status, res.statusText)); | ||
}; | ||
function resOrError(res) { | ||
if (res.ok) { | ||
return res; | ||
} | ||
exports.resOrError = resOrError; | ||
throw new HTTPError(res.status, res.statusText); | ||
} | ||
var resJSON = function resJSON(res) { | ||
return resOrError(res).then(function (r) { | ||
return r.json(); | ||
}); | ||
}; | ||
function resJSON(_x) { | ||
return _resJSON.apply(this, arguments); | ||
} | ||
exports.resJSON = resJSON; | ||
function _resJSON() { | ||
_resJSON = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee19(res) { | ||
return _regenerator.default.wrap(function _callee19$(_context19) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
case 0: | ||
_context19.next = 2; | ||
return resOrError(res).json(); | ||
var resText = function resText(res) { | ||
return resOrError(res).then(function (r) { | ||
return r.text(); | ||
}); | ||
}; | ||
case 2: | ||
return _context19.abrupt("return", _context19.sent); | ||
exports.resText = resText; | ||
case 3: | ||
case "end": | ||
return _context19.stop(); | ||
} | ||
} | ||
}, _callee19); | ||
})); | ||
return _resJSON.apply(this, arguments); | ||
} | ||
var resSwarmHash = function resSwarmHash(res) { | ||
return resOrError(res).then(function (r) { | ||
return r.arrayBuffer(); | ||
}).then(function (value) { | ||
return Buffer.from(new Uint8Array(value)).toString('hex'); | ||
}); | ||
}; | ||
function resText(_x2) { | ||
return _resText.apply(this, arguments); | ||
} | ||
exports.resSwarmHash = resSwarmHash; | ||
function _resText() { | ||
_resText = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee20(res) { | ||
return _regenerator.default.wrap(function _callee20$(_context20) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
case 0: | ||
_context20.next = 2; | ||
return resOrError(res).text(); | ||
var defaultSignBytes = function defaultSignBytes() { | ||
case 2: | ||
return _context20.abrupt("return", _context20.sent); | ||
case 3: | ||
case "end": | ||
return _context20.stop(); | ||
} | ||
} | ||
}, _callee20); | ||
})); | ||
return _resText.apply(this, arguments); | ||
} | ||
function resHex(_x3) { | ||
return _resHex.apply(this, arguments); | ||
} | ||
function _resHex() { | ||
_resHex = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee21(res) { | ||
return _regenerator.default.wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context21.prev = _context21.next) { | ||
case 0: | ||
_context21.next = 2; | ||
return resText(res); | ||
case 2: | ||
return _context21.abrupt("return", _context21.sent); | ||
case 3: | ||
case "end": | ||
return _context21.stop(); | ||
} | ||
} | ||
}, _callee21); | ||
})); | ||
return _resHex.apply(this, arguments); | ||
} | ||
function resSwarmHash(_x4) { | ||
return _resSwarmHash.apply(this, arguments); | ||
} | ||
function _resSwarmHash() { | ||
_resSwarmHash = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee22(res) { | ||
var value; | ||
return _regenerator.default.wrap(function _callee22$(_context22) { | ||
while (1) { | ||
switch (_context22.prev = _context22.next) { | ||
case 0: | ||
_context22.next = 2; | ||
return resOrError(res).arrayBuffer(); | ||
case 2: | ||
value = _context22.sent; | ||
return _context22.abrupt("return", Buffer.from(new Uint8Array(value)).toString('hex')); | ||
case 4: | ||
case "end": | ||
return _context22.stop(); | ||
} | ||
} | ||
}, _callee22); | ||
})); | ||
return _resSwarmHash.apply(this, arguments); | ||
} | ||
function defaultSignBytes() { | ||
return Promise.reject(new Error('Missing `signBytes()` function')); | ||
}; | ||
} | ||
@@ -126,12 +224,13 @@ var BaseBzz = | ||
function () { | ||
function BaseBzz(config) { | ||
(0, _defineProperty2.default)(this, "_defaultTimeout", void 0); | ||
(0, _defineProperty2.default)(this, "_fetch", void 0); | ||
(0, _defineProperty2.default)(this, "_signBytes", void 0); | ||
(0, _defineProperty2.default)(this, "_url", void 0); | ||
function BaseBzz(fetch, config) { | ||
(0, _defineProperty2.default)(this, "defaultTimeout", void 0); | ||
(0, _defineProperty2.default)(this, "fetch", void 0); | ||
(0, _defineProperty2.default)(this, "signBytes", void 0); | ||
(0, _defineProperty2.default)(this, "url", void 0); | ||
var url = config.url, | ||
timeout = config.timeout; | ||
this._defaultTimeout = timeout ? timeout : 0; | ||
this._signBytes = config.signBytes || defaultSignBytes; | ||
this._url = url; | ||
this.fetch = fetch; | ||
this.defaultTimeout = timeout ? timeout : 0; | ||
this.signBytes = config.signBytes || defaultSignBytes; | ||
this.url = url; | ||
} | ||
@@ -141,3 +240,3 @@ | ||
_proto._fetchTimeout = function _fetchTimeout(url, options, params) { | ||
_proto.fetchTimeout = function fetchTimeout(url, options, params) { | ||
var _this2 = this; | ||
@@ -149,3 +248,3 @@ | ||
var timeout = options.timeout == null ? this._defaultTimeout : options.timeout; | ||
var timeout = options.timeout == null ? this.defaultTimeout : options.timeout; | ||
@@ -158,3 +257,3 @@ if (options.headers != null) { | ||
// No timeout | ||
return this._fetch(url, params); | ||
return this.fetch(url, params); | ||
} | ||
@@ -167,3 +266,3 @@ | ||
_this2._fetch(url, params).then(function (res) { | ||
_this2.fetch(url, params).then(function (res) { | ||
clearTimeout(timeoutID); | ||
@@ -175,6 +274,35 @@ resolve(res); | ||
_proto.sign = function sign(bytes, params) { | ||
return this._signBytes(bytes, params).then(_feed.bytesToHexValue); | ||
}; | ||
_proto.sign = | ||
/*#__PURE__*/ | ||
function () { | ||
var _sign = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee(bytes, params) { | ||
var signed; | ||
return _regenerator.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return this.signBytes(bytes, params); | ||
case 2: | ||
signed = _context.sent; | ||
return _context.abrupt("return", (0, _hex.toHexValue)(signed)); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
function sign(_x5, _x6) { | ||
return _sign.apply(this, arguments); | ||
} | ||
return sign; | ||
}(); | ||
_proto.getDownloadURL = function getDownloadURL(hash, options, raw) { | ||
@@ -190,3 +318,3 @@ if (options === void 0) { | ||
var protocol = raw ? BZZ_MODE_PROTOCOLS.raw : getModeProtocol(options.mode); | ||
var url = "" + this._url + protocol + hash + "/"; | ||
var url = "" + this.url + protocol + hash + "/"; | ||
@@ -214,3 +342,3 @@ if (options.path != null) { | ||
// Default URL to creation | ||
var url = this._url + BZZ_MODE_PROTOCOLS[raw ? 'raw' : 'default']; // Manifest update if hash is provided | ||
var url = this.url + BZZ_MODE_PROTOCOLS[raw ? 'raw' : 'default']; // Manifest update if hash is provided | ||
@@ -233,3 +361,3 @@ if (options.manifestHash != null) { | ||
_proto.getFeedURL = function getFeedURL(hashOrParams, flag) { | ||
var url = this._url + BZZ_MODE_PROTOCOLS.feed; | ||
var url = this.url + BZZ_MODE_PROTOCOLS.feed; | ||
var query = []; | ||
@@ -242,12 +370,10 @@ | ||
// feed params | ||
query = Object.keys(hashOrParams).reduce(function (acc, key) { | ||
// $FlowFixMe: hashOrParams type | ||
Object.keys(hashOrParams).forEach(function (key) { | ||
// @ts-ignore | ||
var value = hashOrParams[key]; | ||
if (value != null) { | ||
acc.push(key + "=" + value); | ||
query.push(key + "=" + value); | ||
} | ||
return acc; | ||
}, []); | ||
}); | ||
} | ||
@@ -262,69 +388,221 @@ | ||
_proto.hash = function hash(domain, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
_proto.hash = | ||
/*#__PURE__*/ | ||
function () { | ||
var _hash = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee2(domain, options) { | ||
var res; | ||
return _regenerator.default.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
return this._fetchTimeout(this._url + "bzz-hash:/" + domain, options).then(resText); | ||
}; | ||
_context2.next = 3; | ||
return this.fetchTimeout(this.url + "bzz-hash:/" + domain, options); | ||
_proto.list = function list(hash, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
case 3: | ||
res = _context2.sent; | ||
_context2.next = 6; | ||
return resHex(res); | ||
var url = this._url + "bzz-list:/" + hash + "/"; | ||
case 6: | ||
return _context2.abrupt("return", _context2.sent); | ||
if (options.path != null) { | ||
url += options.path; | ||
case 7: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
function hash(_x7, _x8) { | ||
return _hash.apply(this, arguments); | ||
} | ||
return this._fetchTimeout(url, options).then(resJSON); | ||
}; | ||
return hash; | ||
}(); | ||
_proto._download = function _download(hash, options) { | ||
var url = this.getDownloadURL(hash, options); | ||
return this._fetchTimeout(url, options).then(resOrError); | ||
}; | ||
_proto.list = | ||
/*#__PURE__*/ | ||
function () { | ||
var _list = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee3(hash, options) { | ||
var url, res; | ||
return _regenerator.default.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
_proto.download = function download(hash, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.url + "bzz-list:/" + hash + "/"; | ||
return this._download(hash, options); | ||
}; | ||
if (options.path != null) { | ||
url += options.path; | ||
} | ||
_proto._upload = function _upload(body, options, raw) { | ||
if (raw === void 0) { | ||
raw = false; | ||
_context3.next = 5; | ||
return this.fetchTimeout(url, options); | ||
case 5: | ||
res = _context3.sent; | ||
_context3.next = 8; | ||
return resJSON(res); | ||
case 8: | ||
return _context3.abrupt("return", _context3.sent); | ||
case 9: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this); | ||
})); | ||
function list(_x9, _x10) { | ||
return _list.apply(this, arguments); | ||
} | ||
var url = this.getUploadURL(options, raw); | ||
return this._fetchTimeout(url, options, { | ||
body: body, | ||
method: 'POST' | ||
}).then(resText).then(_hex.hexValueType); | ||
}; | ||
return list; | ||
}(); | ||
_proto.uploadFile = function uploadFile(data, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.download = | ||
/*#__PURE__*/ | ||
function () { | ||
var _download = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee4(hash, options) { | ||
var url, res; | ||
return _regenerator.default.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getDownloadURL(hash, options); | ||
_context4.next = 4; | ||
return this.fetchTimeout(url, options); | ||
case 4: | ||
res = _context4.sent; | ||
return _context4.abrupt("return", resOrError(res)); | ||
case 6: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
})); | ||
function download(_x11, _x12) { | ||
return _download.apply(this, arguments); | ||
} | ||
var body = typeof data === 'string' ? Buffer.from(data) : data; | ||
var raw = options.contentType == null; | ||
return download; | ||
}(); | ||
if (options.headers == null) { | ||
options.headers = {}; | ||
_proto.uploadBody = | ||
/*#__PURE__*/ | ||
function () { | ||
var _uploadBody = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee5(body, options, raw) { | ||
var url, res; | ||
return _regenerator.default.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
if (raw === void 0) { | ||
raw = false; | ||
} | ||
url = this.getUploadURL(options, raw); | ||
_context5.next = 4; | ||
return this.fetchTimeout(url, options, { | ||
body: body, | ||
method: 'POST' | ||
}); | ||
case 4: | ||
res = _context5.sent; | ||
_context5.next = 7; | ||
return resHex(res); | ||
case 7: | ||
return _context5.abrupt("return", _context5.sent); | ||
case 8: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
function uploadBody(_x13, _x14, _x15) { | ||
return _uploadBody.apply(this, arguments); | ||
} | ||
options.headers['content-length'] = body.length; | ||
return uploadBody; | ||
}(); | ||
if (options.headers != null && options.headers['content-type'] == null && !raw) { | ||
options.headers['content-type'] = options.contentType; | ||
_proto.uploadFile = | ||
/*#__PURE__*/ | ||
function () { | ||
var _uploadFile = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee6(data, options) { | ||
var body, raw; | ||
return _regenerator.default.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
body = typeof data === 'string' ? Buffer.from(data) : data; | ||
raw = options.contentType == null; | ||
if (options.headers == null) { | ||
options.headers = {}; | ||
} | ||
options.headers['content-length'] = body.length; | ||
if (options.headers != null && options.headers['content-type'] == null && !raw) { | ||
options.headers['content-type'] = options.contentType; | ||
} | ||
_context6.next = 8; | ||
return this.uploadBody(body, options, raw); | ||
case 8: | ||
return _context6.abrupt("return", _context6.sent); | ||
case 9: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
})); | ||
function uploadFile(_x16, _x17) { | ||
return _uploadFile.apply(this, arguments); | ||
} | ||
return this._upload(body, options, raw); | ||
}; | ||
return uploadFile; | ||
}(); | ||
@@ -335,86 +613,315 @@ _proto.uploadDirectory = function uploadDirectory(_directory, _options) { | ||
_proto.upload = function upload(data, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.upload = | ||
/*#__PURE__*/ | ||
function () { | ||
var _upload = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee7(data, options) { | ||
return _regenerator.default.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!(typeof data === 'string' || Buffer.isBuffer(data))) { | ||
_context7.next = 7; | ||
break; | ||
} | ||
_context7.next = 4; | ||
return this.uploadFile(data, options); | ||
case 4: | ||
_context7.t0 = _context7.sent; | ||
_context7.next = 10; | ||
break; | ||
case 7: | ||
_context7.next = 9; | ||
return this.uploadDirectory(data, options); | ||
case 9: | ||
_context7.t0 = _context7.sent; | ||
case 10: | ||
return _context7.abrupt("return", _context7.t0); | ||
case 11: | ||
case "end": | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee7, this); | ||
})); | ||
function upload(_x18, _x19) { | ||
return _upload.apply(this, arguments); | ||
} | ||
return typeof data === 'string' || Buffer.isBuffer(data) ? // $FlowFixMe: Flow doesn't understand type refinement with Buffer check | ||
this.uploadFile(data, options) : this.uploadDirectory(data, options); | ||
}; | ||
return upload; | ||
}(); | ||
_proto.deleteResource = function deleteResource(hash, path, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.deleteResource = | ||
/*#__PURE__*/ | ||
function () { | ||
var _deleteResource = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee8(hash, path, options) { | ||
var url, res; | ||
return _regenerator.default.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getUploadURL({ | ||
manifestHash: hash, | ||
path: path | ||
}); | ||
_context8.next = 4; | ||
return this.fetchTimeout(url, options, { | ||
method: 'DELETE' | ||
}); | ||
case 4: | ||
res = _context8.sent; | ||
_context8.next = 7; | ||
return resHex(res); | ||
case 7: | ||
return _context8.abrupt("return", _context8.sent); | ||
case 8: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
})); | ||
function deleteResource(_x20, _x21, _x22) { | ||
return _deleteResource.apply(this, arguments); | ||
} | ||
var url = this.getUploadURL({ | ||
manifestHash: hash, | ||
path: path | ||
}); | ||
return this._fetchTimeout(url, options, { | ||
method: 'DELETE' | ||
}).then(resText); | ||
}; | ||
return deleteResource; | ||
}(); | ||
_proto.createFeedManifest = function createFeedManifest(params, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.createFeedManifest = | ||
/*#__PURE__*/ | ||
function () { | ||
var _createFeedManifest = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee9(params, options) { | ||
var manifest; | ||
return _regenerator.default.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
manifest = { | ||
entries: [{ | ||
contentType: 'application/bzz-feed', | ||
mod_time: '0001-01-01T00:00:00Z', | ||
// eslint-disable-line @typescript-eslint/camelcase | ||
feed: { | ||
topic: (0, _feed.getFeedTopic)(params), | ||
user: params.user | ||
} | ||
}] | ||
}; | ||
_context9.next = 4; | ||
return this.uploadFile(JSON.stringify(manifest), options); | ||
case 4: | ||
return _context9.abrupt("return", _context9.sent); | ||
case 5: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee9, this); | ||
})); | ||
function createFeedManifest(_x23, _x24) { | ||
return _createFeedManifest.apply(this, arguments); | ||
} | ||
var manifest = { | ||
entries: [{ | ||
contentType: 'application/bzz-feed', | ||
mod_time: '0001-01-01T00:00:00Z', | ||
feed: { | ||
topic: (0, _feed.getFeedTopic)(params), | ||
user: params.user | ||
return createFeedManifest; | ||
}(); | ||
_proto.getFeedMetadata = | ||
/*#__PURE__*/ | ||
function () { | ||
var _getFeedMetadata = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee10(hashOrParams, options) { | ||
var url, res; | ||
return _regenerator.default.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getFeedURL(hashOrParams, 'meta'); | ||
_context10.next = 4; | ||
return this.fetchTimeout(url, options); | ||
case 4: | ||
res = _context10.sent; | ||
_context10.next = 7; | ||
return resJSON(res); | ||
case 7: | ||
return _context10.abrupt("return", _context10.sent); | ||
case 8: | ||
case "end": | ||
return _context10.stop(); | ||
} | ||
} | ||
}] | ||
}; | ||
return this.uploadFile(JSON.stringify(manifest), options).then(_hex.hexValueType); | ||
}; | ||
}, _callee10, this); | ||
})); | ||
_proto.getFeedMetadata = function getFeedMetadata(hashOrParams, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
function getFeedMetadata(_x25, _x26) { | ||
return _getFeedMetadata.apply(this, arguments); | ||
} | ||
var url = this.getFeedURL(hashOrParams, 'meta'); | ||
return this._fetchTimeout(url, options).then(resJSON); | ||
}; | ||
return getFeedMetadata; | ||
}(); | ||
_proto.getFeedChunk = function getFeedChunk(hashOrParams, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.getFeedChunk = | ||
/*#__PURE__*/ | ||
function () { | ||
var _getFeedChunk = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee11(hashOrParams, options) { | ||
var url, res; | ||
return _regenerator.default.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getFeedURL(hashOrParams); | ||
_context11.next = 4; | ||
return this.fetchTimeout(url, options); | ||
case 4: | ||
res = _context11.sent; | ||
return _context11.abrupt("return", resOrError(res)); | ||
case 6: | ||
case "end": | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee11, this); | ||
})); | ||
function getFeedChunk(_x27, _x28) { | ||
return _getFeedChunk.apply(this, arguments); | ||
} | ||
var url = this.getFeedURL(hashOrParams); | ||
return this._fetchTimeout(url, options).then(resOrError); | ||
}; | ||
return getFeedChunk; | ||
}(); | ||
_proto.getFeedContentHash = function getFeedContentHash(hashOrParams, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.getFeedContentHash = | ||
/*#__PURE__*/ | ||
function () { | ||
var _getFeedContentHash = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee12(hashOrParams, options) { | ||
var res; | ||
return _regenerator.default.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
_context12.next = 3; | ||
return this.getFeedChunk(hashOrParams, options); | ||
case 3: | ||
res = _context12.sent; | ||
_context12.next = 6; | ||
return resSwarmHash(res); | ||
case 6: | ||
return _context12.abrupt("return", _context12.sent); | ||
case 7: | ||
case "end": | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee12, this); | ||
})); | ||
function getFeedContentHash(_x29, _x30) { | ||
return _getFeedContentHash.apply(this, arguments); | ||
} | ||
return this.getFeedChunk(hashOrParams, options).then(resSwarmHash); | ||
}; | ||
return getFeedContentHash; | ||
}(); | ||
_proto.getFeedContent = function getFeedContent(hashOrParams, options) { | ||
var _this3 = this; | ||
_proto.getFeedContent = | ||
/*#__PURE__*/ | ||
function () { | ||
var _getFeedContent = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee13(hashOrParams, options) { | ||
var hash; | ||
return _regenerator.default.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
_context13.next = 3; | ||
return this.getFeedContentHash(hashOrParams, { | ||
headers: options.headers, | ||
timeout: options.timeout | ||
}); | ||
case 3: | ||
hash = _context13.sent; | ||
_context13.next = 6; | ||
return this.download(hash, options); | ||
case 6: | ||
return _context13.abrupt("return", _context13.sent); | ||
case 7: | ||
case "end": | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee13, this); | ||
})); | ||
function getFeedContent(_x31, _x32) { | ||
return _getFeedContent.apply(this, arguments); | ||
} | ||
return this.getFeedContentHash(hashOrParams, { | ||
headers: options.headers, | ||
timeout: options.timeout | ||
}).then(function (hash) { | ||
return _this3.download(hash, options); | ||
}); | ||
}; | ||
return getFeedContent; | ||
}(); | ||
_proto.pollFeedChunk = function pollFeedChunk(hashOrParams, options) { | ||
var _this4 = this, | ||
var _this3 = this, | ||
_merge; | ||
@@ -437,3 +944,3 @@ | ||
pipeline.push((0, _operators.flatMap)(function () { | ||
return _this4.getFeedChunk(hashOrParams, options); | ||
return _this3.getFeedChunk(hashOrParams, options); | ||
})); | ||
@@ -443,3 +950,3 @@ } else { | ||
pipeline.push((0, _operators.flatMap)(function () { | ||
return _this4._fetchTimeout(url, options).then(function (res) { | ||
return _this3.fetchTimeout(url, options).then(function (res) { | ||
if (res.status === 404) { | ||
@@ -462,4 +969,5 @@ return null; | ||
} | ||
} | ||
} // @ts-ignore | ||
return (_merge = _rxjs.merge.apply(void 0, [(0, _rxjs.interval)(options.interval)].concat(sources))).pipe.apply(_merge, pipeline); | ||
@@ -476,5 +984,7 @@ }; | ||
if (options.changedOnly) { | ||
// @ts-ignore | ||
pipeline.push((0, _operators.distinctUntilChanged)()); | ||
} | ||
} // @ts-ignore | ||
return (_this$pollFeedChunk = this.pollFeedChunk(hashOrParams, options)).pipe.apply(_this$pollFeedChunk, pipeline); | ||
@@ -484,76 +994,217 @@ }; | ||
_proto.pollFeedContent = function pollFeedContent(hashOrParams, options) { | ||
var _this5 = this; | ||
var _this4 = this; | ||
return this.pollFeedContentHash(hashOrParams, options).pipe((0, _operators.flatMap)(function (hash) { | ||
return hash === null ? Promise.resolve(null) : _this5.download(hash, options); | ||
return hash === null ? Promise.resolve(null) : _this4.download(hash, options); | ||
})); | ||
}; | ||
_proto.postSignedFeedChunk = function postSignedFeedChunk(params, body, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.postSignedFeedChunk = | ||
/*#__PURE__*/ | ||
function () { | ||
var _postSignedFeedChunk = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee14(params, body, options) { | ||
var url, res; | ||
return _regenerator.default.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getFeedURL(params); | ||
_context14.next = 4; | ||
return this.fetchTimeout(url, options, { | ||
method: 'POST', | ||
body: body | ||
}); | ||
case 4: | ||
res = _context14.sent; | ||
return _context14.abrupt("return", resOrError(res)); | ||
case 6: | ||
case "end": | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, this); | ||
})); | ||
function postSignedFeedChunk(_x33, _x34, _x35) { | ||
return _postSignedFeedChunk.apply(this, arguments); | ||
} | ||
var url = this.getFeedURL(params); | ||
return this._fetchTimeout(url, options, { | ||
method: 'POST', | ||
body: body | ||
}).then(resOrError); | ||
}; | ||
return postSignedFeedChunk; | ||
}(); | ||
_proto.postFeedChunk = function postFeedChunk(meta, data, options, signParams) { | ||
var _this6 = this; | ||
_proto.postFeedChunk = | ||
/*#__PURE__*/ | ||
function () { | ||
var _postFeedChunk = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee15(meta, data, options, signParams) { | ||
var body, digest, signature; | ||
return _regenerator.default.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
body = (0, _hex.createHex)(data).toBuffer(); | ||
digest = (0, _feed.createFeedDigest)(meta, body); | ||
_context15.next = 4; | ||
return this.sign(digest, signParams); | ||
var body = (0, _hex.default)(data).toBuffer(); | ||
var digest = (0, _feed.createFeedDigest)(meta, body); | ||
return this.sign(digest, signParams).then(function (signature) { | ||
var params = { | ||
user: meta.feed.user, | ||
topic: meta.feed.topic, | ||
time: meta.epoch.time, | ||
level: meta.epoch.level, | ||
signature: signature | ||
}; | ||
return _this6.postSignedFeedChunk(params, body, options); | ||
}); | ||
}; | ||
case 4: | ||
signature = _context15.sent; | ||
_context15.next = 7; | ||
return this.postSignedFeedChunk({ | ||
user: meta.feed.user, | ||
topic: meta.feed.topic, | ||
time: meta.epoch.time, | ||
level: meta.epoch.level, | ||
signature: signature | ||
}, body, options); | ||
_proto.setFeedChunk = function setFeedChunk(hashOrParams, data, options, signParams) { | ||
var _this7 = this; | ||
case 7: | ||
return _context15.abrupt("return", _context15.sent); | ||
return this.getFeedMetadata(hashOrParams, options).then(function (meta) { | ||
return _this7.postFeedChunk(meta, data, options, signParams); | ||
}); | ||
}; | ||
case 8: | ||
case "end": | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee15, this); | ||
})); | ||
_proto.setFeedContentHash = function setFeedContentHash(hashOrParams, contentHash, options, signParams) { | ||
var _this8 = this; | ||
function postFeedChunk(_x36, _x37, _x38, _x39) { | ||
return _postFeedChunk.apply(this, arguments); | ||
} | ||
return this.getFeedMetadata(hashOrParams, options).then(function (meta) { | ||
return _this8.postFeedChunk(meta, "0x" + contentHash, options, signParams); | ||
}); | ||
}; | ||
return postFeedChunk; | ||
}(); | ||
_proto.setFeedContent = function setFeedContent(hashOrParams, data, options, signParams) { | ||
var _this9 = this; | ||
_proto.setFeedChunk = | ||
/*#__PURE__*/ | ||
function () { | ||
var _setFeedChunk = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee16(hashOrParams, data, options, signParams) { | ||
var meta; | ||
return _regenerator.default.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
_context16.next = 2; | ||
return this.getFeedMetadata(hashOrParams, options); | ||
if (options === void 0) { | ||
options = {}; | ||
case 2: | ||
meta = _context16.sent; | ||
_context16.next = 5; | ||
return this.postFeedChunk(meta, data, options, signParams); | ||
case 5: | ||
return _context16.abrupt("return", _context16.sent); | ||
case 6: | ||
case "end": | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee16, this); | ||
})); | ||
function setFeedChunk(_x40, _x41, _x42, _x43) { | ||
return _setFeedChunk.apply(this, arguments); | ||
} | ||
var _options2 = options, | ||
_c = _options2.contentType, | ||
feedOptions = (0, _objectWithoutPropertiesLoose2.default)(_options2, ["contentType"]); | ||
return Promise.all([this.upload(data, options), this.getFeedMetadata(hashOrParams, feedOptions)]).then(function (_ref) { | ||
var hash = _ref[0], | ||
meta = _ref[1]; | ||
return _this9.postFeedChunk(meta, "0x" + hash, feedOptions, signParams).then(function () { | ||
return hash; | ||
}); | ||
}); | ||
}; | ||
return setFeedChunk; | ||
}(); | ||
_proto.setFeedContentHash = | ||
/*#__PURE__*/ | ||
function () { | ||
var _setFeedContentHash = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee17(hashOrParams, contentHash, options, signParams) { | ||
var meta; | ||
return _regenerator.default.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
_context17.next = 2; | ||
return this.getFeedMetadata(hashOrParams, options); | ||
case 2: | ||
meta = _context17.sent; | ||
_context17.next = 5; | ||
return this.postFeedChunk(meta, "0x" + contentHash, options, signParams); | ||
case 5: | ||
return _context17.abrupt("return", _context17.sent); | ||
case 6: | ||
case "end": | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee17, this); | ||
})); | ||
function setFeedContentHash(_x44, _x45, _x46, _x47) { | ||
return _setFeedContentHash.apply(this, arguments); | ||
} | ||
return setFeedContentHash; | ||
}(); | ||
_proto.setFeedContent = | ||
/*#__PURE__*/ | ||
function () { | ||
var _setFeedContent = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee18(hashOrParams, data, options, signParams) { | ||
var _options2, _c, feedOptions, _ref, hash, meta; | ||
return _regenerator.default.wrap(function _callee18$(_context18) { | ||
while (1) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
_options2 = options, _c = _options2.contentType, feedOptions = (0, _objectWithoutPropertiesLoose2.default)(_options2, ["contentType"]); | ||
_context18.next = 4; | ||
return Promise.all([this.upload(data, options), this.getFeedMetadata(hashOrParams, feedOptions)]); | ||
case 4: | ||
_ref = _context18.sent; | ||
hash = _ref[0]; | ||
meta = _ref[1]; | ||
_context18.next = 9; | ||
return this.postFeedChunk(meta, "0x" + hash, feedOptions, signParams); | ||
case 9: | ||
return _context18.abrupt("return", hash); | ||
case 10: | ||
case "end": | ||
return _context18.stop(); | ||
} | ||
} | ||
}, _callee18, this); | ||
})); | ||
function setFeedContent(_x48, _x49, _x50, _x51) { | ||
return _setFeedContent.apply(this, arguments); | ||
} | ||
return setFeedContent; | ||
}(); | ||
return BaseBzz; | ||
}(); | ||
exports.default = BaseBzz; | ||
exports.BaseBzz = BaseBzz; |
@@ -1,2 +0,2 @@ | ||
import createHex, { hexValueType } from '@erebos/hex'; | ||
import { createHex, toHexValue } from '@erebos/hex'; | ||
import { hash } from '@erebos/keccak256'; | ||
@@ -8,6 +8,3 @@ var FEED_TOPIC_LENGTH = 32; | ||
var FEED_HEADER_LENGTH = 8; | ||
export var bytesToHexValue = function bytesToHexValue(bytes) { | ||
return hexValueType('0x' + Buffer.from(bytes).toString('hex')); | ||
}; | ||
export var createFeedDigest = function createFeedDigest(meta, data) { | ||
export function createFeedDigest(meta, data) { | ||
var topicBuffer = createHex(meta.feed.topic).toBuffer(); | ||
@@ -33,4 +30,4 @@ | ||
return hash(payload); | ||
}; | ||
export var getFeedTopic = function getFeedTopic(params) { | ||
} | ||
export function getFeedTopic(params) { | ||
var topicHex = createHex(params.topic || Buffer.alloc(FEED_TOPIC_LENGTH)); | ||
@@ -44,6 +41,6 @@ | ||
var topic = topicHex.toBuffer(); | ||
var bytes = Array(FEED_TOPIC_LENGTH).fill().map(function (_, i) { | ||
var bytes = Array(FEED_TOPIC_LENGTH).fill(0).map(function (_, i) { | ||
return topic[i] ^ name[i]; | ||
}); | ||
return bytesToHexValue(bytes); | ||
}; | ||
return toHexValue(bytes); | ||
} |
1086
esm/index.js
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; | ||
import _regeneratorRuntime from "@babel/runtime/regenerator"; | ||
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; | ||
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; | ||
@@ -6,6 +8,6 @@ import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose"; | ||
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
import createHex, { hexValueType } from '@erebos/hex'; | ||
import { createHex, toHexValue } from '@erebos/hex'; | ||
import { interval, merge } from 'rxjs'; | ||
import { distinctUntilChanged, filter, flatMap } from 'rxjs/operators'; | ||
import { bytesToHexValue, createFeedDigest, getFeedTopic } from './feed'; | ||
import { createFeedDigest, getFeedTopic } from './feed'; | ||
export * from './feed'; | ||
@@ -19,5 +21,5 @@ export * from './types'; | ||
}; | ||
export var getModeProtocol = function getModeProtocol(mode) { | ||
export function getModeProtocol(mode) { | ||
return mode && BZZ_MODE_PROTOCOLS[mode] || BZZ_MODE_PROTOCOLS.default; | ||
}; | ||
} | ||
export var HTTPError = | ||
@@ -41,44 +43,145 @@ /*#__PURE__*/ | ||
}(_wrapNativeSuper(Error)); | ||
export var resOrError = function resOrError(res) { | ||
return res.ok ? Promise.resolve(res) : Promise.reject(new HTTPError(res.status, res.statusText)); | ||
}; | ||
export var resJSON = function resJSON(res) { | ||
return resOrError(res).then(function (r) { | ||
return r.json(); | ||
}); | ||
}; | ||
export var resText = function resText(res) { | ||
return resOrError(res).then(function (r) { | ||
return r.text(); | ||
}); | ||
}; | ||
export var resSwarmHash = function resSwarmHash(res) { | ||
return resOrError(res).then(function (r) { | ||
return r.arrayBuffer(); | ||
}).then(function (value) { | ||
return Buffer.from(new Uint8Array(value)).toString('hex'); | ||
}); | ||
}; | ||
export function resOrError(res) { | ||
if (res.ok) { | ||
return res; | ||
} | ||
var defaultSignBytes = function defaultSignBytes() { | ||
throw new HTTPError(res.status, res.statusText); | ||
} | ||
export function resJSON(_x) { | ||
return _resJSON.apply(this, arguments); | ||
} | ||
function _resJSON() { | ||
_resJSON = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee19(res) { | ||
return _regeneratorRuntime.wrap(function _callee19$(_context19) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
case 0: | ||
_context19.next = 2; | ||
return resOrError(res).json(); | ||
case 2: | ||
return _context19.abrupt("return", _context19.sent); | ||
case 3: | ||
case "end": | ||
return _context19.stop(); | ||
} | ||
} | ||
}, _callee19); | ||
})); | ||
return _resJSON.apply(this, arguments); | ||
} | ||
export function resText(_x2) { | ||
return _resText.apply(this, arguments); | ||
} | ||
function _resText() { | ||
_resText = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee20(res) { | ||
return _regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
case 0: | ||
_context20.next = 2; | ||
return resOrError(res).text(); | ||
case 2: | ||
return _context20.abrupt("return", _context20.sent); | ||
case 3: | ||
case "end": | ||
return _context20.stop(); | ||
} | ||
} | ||
}, _callee20); | ||
})); | ||
return _resText.apply(this, arguments); | ||
} | ||
export function resHex(_x3) { | ||
return _resHex.apply(this, arguments); | ||
} | ||
function _resHex() { | ||
_resHex = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee21(res) { | ||
return _regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context21.prev = _context21.next) { | ||
case 0: | ||
_context21.next = 2; | ||
return resText(res); | ||
case 2: | ||
return _context21.abrupt("return", _context21.sent); | ||
case 3: | ||
case "end": | ||
return _context21.stop(); | ||
} | ||
} | ||
}, _callee21); | ||
})); | ||
return _resHex.apply(this, arguments); | ||
} | ||
export function resSwarmHash(_x4) { | ||
return _resSwarmHash.apply(this, arguments); | ||
} | ||
function _resSwarmHash() { | ||
_resSwarmHash = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee22(res) { | ||
var value; | ||
return _regeneratorRuntime.wrap(function _callee22$(_context22) { | ||
while (1) { | ||
switch (_context22.prev = _context22.next) { | ||
case 0: | ||
_context22.next = 2; | ||
return resOrError(res).arrayBuffer(); | ||
case 2: | ||
value = _context22.sent; | ||
return _context22.abrupt("return", Buffer.from(new Uint8Array(value)).toString('hex')); | ||
case 4: | ||
case "end": | ||
return _context22.stop(); | ||
} | ||
} | ||
}, _callee22); | ||
})); | ||
return _resSwarmHash.apply(this, arguments); | ||
} | ||
function defaultSignBytes() { | ||
return Promise.reject(new Error('Missing `signBytes()` function')); | ||
}; | ||
} | ||
var BaseBzz = | ||
export var BaseBzz = | ||
/*#__PURE__*/ | ||
function () { | ||
function BaseBzz(config) { | ||
_defineProperty(this, "_defaultTimeout", void 0); | ||
function BaseBzz(fetch, config) { | ||
_defineProperty(this, "defaultTimeout", void 0); | ||
_defineProperty(this, "_fetch", void 0); | ||
_defineProperty(this, "fetch", void 0); | ||
_defineProperty(this, "_signBytes", void 0); | ||
_defineProperty(this, "signBytes", void 0); | ||
_defineProperty(this, "_url", void 0); | ||
_defineProperty(this, "url", void 0); | ||
var url = config.url, | ||
timeout = config.timeout; | ||
this._defaultTimeout = timeout ? timeout : 0; | ||
this._signBytes = config.signBytes || defaultSignBytes; | ||
this._url = url; | ||
this.fetch = fetch; | ||
this.defaultTimeout = timeout ? timeout : 0; | ||
this.signBytes = config.signBytes || defaultSignBytes; | ||
this.url = url; | ||
} | ||
@@ -88,3 +191,3 @@ | ||
_proto._fetchTimeout = function _fetchTimeout(url, options, params) { | ||
_proto.fetchTimeout = function fetchTimeout(url, options, params) { | ||
var _this2 = this; | ||
@@ -96,3 +199,3 @@ | ||
var timeout = options.timeout == null ? this._defaultTimeout : options.timeout; | ||
var timeout = options.timeout == null ? this.defaultTimeout : options.timeout; | ||
@@ -105,3 +208,3 @@ if (options.headers != null) { | ||
// No timeout | ||
return this._fetch(url, params); | ||
return this.fetch(url, params); | ||
} | ||
@@ -114,3 +217,3 @@ | ||
_this2._fetch(url, params).then(function (res) { | ||
_this2.fetch(url, params).then(function (res) { | ||
clearTimeout(timeoutID); | ||
@@ -122,6 +225,35 @@ resolve(res); | ||
_proto.sign = function sign(bytes, params) { | ||
return this._signBytes(bytes, params).then(bytesToHexValue); | ||
}; | ||
_proto.sign = | ||
/*#__PURE__*/ | ||
function () { | ||
var _sign = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee(bytes, params) { | ||
var signed; | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return this.signBytes(bytes, params); | ||
case 2: | ||
signed = _context.sent; | ||
return _context.abrupt("return", toHexValue(signed)); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
function sign(_x5, _x6) { | ||
return _sign.apply(this, arguments); | ||
} | ||
return sign; | ||
}(); | ||
_proto.getDownloadURL = function getDownloadURL(hash, options, raw) { | ||
@@ -137,3 +269,3 @@ if (options === void 0) { | ||
var protocol = raw ? BZZ_MODE_PROTOCOLS.raw : getModeProtocol(options.mode); | ||
var url = "" + this._url + protocol + hash + "/"; | ||
var url = "" + this.url + protocol + hash + "/"; | ||
@@ -161,3 +293,3 @@ if (options.path != null) { | ||
// Default URL to creation | ||
var url = this._url + BZZ_MODE_PROTOCOLS[raw ? 'raw' : 'default']; // Manifest update if hash is provided | ||
var url = this.url + BZZ_MODE_PROTOCOLS[raw ? 'raw' : 'default']; // Manifest update if hash is provided | ||
@@ -180,3 +312,3 @@ if (options.manifestHash != null) { | ||
_proto.getFeedURL = function getFeedURL(hashOrParams, flag) { | ||
var url = this._url + BZZ_MODE_PROTOCOLS.feed; | ||
var url = this.url + BZZ_MODE_PROTOCOLS.feed; | ||
var query = []; | ||
@@ -189,12 +321,10 @@ | ||
// feed params | ||
query = Object.keys(hashOrParams).reduce(function (acc, key) { | ||
// $FlowFixMe: hashOrParams type | ||
Object.keys(hashOrParams).forEach(function (key) { | ||
// @ts-ignore | ||
var value = hashOrParams[key]; | ||
if (value != null) { | ||
acc.push(key + "=" + value); | ||
query.push(key + "=" + value); | ||
} | ||
return acc; | ||
}, []); | ||
}); | ||
} | ||
@@ -209,69 +339,221 @@ | ||
_proto.hash = function hash(domain, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
_proto.hash = | ||
/*#__PURE__*/ | ||
function () { | ||
var _hash = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee2(domain, options) { | ||
var res; | ||
return _regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
return this._fetchTimeout(this._url + "bzz-hash:/" + domain, options).then(resText); | ||
}; | ||
_context2.next = 3; | ||
return this.fetchTimeout(this.url + "bzz-hash:/" + domain, options); | ||
_proto.list = function list(hash, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
case 3: | ||
res = _context2.sent; | ||
_context2.next = 6; | ||
return resHex(res); | ||
var url = this._url + "bzz-list:/" + hash + "/"; | ||
case 6: | ||
return _context2.abrupt("return", _context2.sent); | ||
if (options.path != null) { | ||
url += options.path; | ||
case 7: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
function hash(_x7, _x8) { | ||
return _hash.apply(this, arguments); | ||
} | ||
return this._fetchTimeout(url, options).then(resJSON); | ||
}; | ||
return hash; | ||
}(); | ||
_proto._download = function _download(hash, options) { | ||
var url = this.getDownloadURL(hash, options); | ||
return this._fetchTimeout(url, options).then(resOrError); | ||
}; | ||
_proto.list = | ||
/*#__PURE__*/ | ||
function () { | ||
var _list = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee3(hash, options) { | ||
var url, res; | ||
return _regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
_proto.download = function download(hash, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.url + "bzz-list:/" + hash + "/"; | ||
return this._download(hash, options); | ||
}; | ||
if (options.path != null) { | ||
url += options.path; | ||
} | ||
_proto._upload = function _upload(body, options, raw) { | ||
if (raw === void 0) { | ||
raw = false; | ||
_context3.next = 5; | ||
return this.fetchTimeout(url, options); | ||
case 5: | ||
res = _context3.sent; | ||
_context3.next = 8; | ||
return resJSON(res); | ||
case 8: | ||
return _context3.abrupt("return", _context3.sent); | ||
case 9: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this); | ||
})); | ||
function list(_x9, _x10) { | ||
return _list.apply(this, arguments); | ||
} | ||
var url = this.getUploadURL(options, raw); | ||
return this._fetchTimeout(url, options, { | ||
body: body, | ||
method: 'POST' | ||
}).then(resText).then(hexValueType); | ||
}; | ||
return list; | ||
}(); | ||
_proto.uploadFile = function uploadFile(data, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.download = | ||
/*#__PURE__*/ | ||
function () { | ||
var _download = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee4(hash, options) { | ||
var url, res; | ||
return _regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getDownloadURL(hash, options); | ||
_context4.next = 4; | ||
return this.fetchTimeout(url, options); | ||
case 4: | ||
res = _context4.sent; | ||
return _context4.abrupt("return", resOrError(res)); | ||
case 6: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
})); | ||
function download(_x11, _x12) { | ||
return _download.apply(this, arguments); | ||
} | ||
var body = typeof data === 'string' ? Buffer.from(data) : data; | ||
var raw = options.contentType == null; | ||
return download; | ||
}(); | ||
if (options.headers == null) { | ||
options.headers = {}; | ||
_proto.uploadBody = | ||
/*#__PURE__*/ | ||
function () { | ||
var _uploadBody = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee5(body, options, raw) { | ||
var url, res; | ||
return _regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
if (raw === void 0) { | ||
raw = false; | ||
} | ||
url = this.getUploadURL(options, raw); | ||
_context5.next = 4; | ||
return this.fetchTimeout(url, options, { | ||
body: body, | ||
method: 'POST' | ||
}); | ||
case 4: | ||
res = _context5.sent; | ||
_context5.next = 7; | ||
return resHex(res); | ||
case 7: | ||
return _context5.abrupt("return", _context5.sent); | ||
case 8: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
function uploadBody(_x13, _x14, _x15) { | ||
return _uploadBody.apply(this, arguments); | ||
} | ||
options.headers['content-length'] = body.length; | ||
return uploadBody; | ||
}(); | ||
if (options.headers != null && options.headers['content-type'] == null && !raw) { | ||
options.headers['content-type'] = options.contentType; | ||
_proto.uploadFile = | ||
/*#__PURE__*/ | ||
function () { | ||
var _uploadFile = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee6(data, options) { | ||
var body, raw; | ||
return _regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
body = typeof data === 'string' ? Buffer.from(data) : data; | ||
raw = options.contentType == null; | ||
if (options.headers == null) { | ||
options.headers = {}; | ||
} | ||
options.headers['content-length'] = body.length; | ||
if (options.headers != null && options.headers['content-type'] == null && !raw) { | ||
options.headers['content-type'] = options.contentType; | ||
} | ||
_context6.next = 8; | ||
return this.uploadBody(body, options, raw); | ||
case 8: | ||
return _context6.abrupt("return", _context6.sent); | ||
case 9: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
})); | ||
function uploadFile(_x16, _x17) { | ||
return _uploadFile.apply(this, arguments); | ||
} | ||
return this._upload(body, options, raw); | ||
}; | ||
return uploadFile; | ||
}(); | ||
@@ -282,86 +564,315 @@ _proto.uploadDirectory = function uploadDirectory(_directory, _options) { | ||
_proto.upload = function upload(data, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.upload = | ||
/*#__PURE__*/ | ||
function () { | ||
var _upload = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee7(data, options) { | ||
return _regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!(typeof data === 'string' || Buffer.isBuffer(data))) { | ||
_context7.next = 7; | ||
break; | ||
} | ||
_context7.next = 4; | ||
return this.uploadFile(data, options); | ||
case 4: | ||
_context7.t0 = _context7.sent; | ||
_context7.next = 10; | ||
break; | ||
case 7: | ||
_context7.next = 9; | ||
return this.uploadDirectory(data, options); | ||
case 9: | ||
_context7.t0 = _context7.sent; | ||
case 10: | ||
return _context7.abrupt("return", _context7.t0); | ||
case 11: | ||
case "end": | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee7, this); | ||
})); | ||
function upload(_x18, _x19) { | ||
return _upload.apply(this, arguments); | ||
} | ||
return typeof data === 'string' || Buffer.isBuffer(data) ? // $FlowFixMe: Flow doesn't understand type refinement with Buffer check | ||
this.uploadFile(data, options) : this.uploadDirectory(data, options); | ||
}; | ||
return upload; | ||
}(); | ||
_proto.deleteResource = function deleteResource(hash, path, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.deleteResource = | ||
/*#__PURE__*/ | ||
function () { | ||
var _deleteResource = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee8(hash, path, options) { | ||
var url, res; | ||
return _regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getUploadURL({ | ||
manifestHash: hash, | ||
path: path | ||
}); | ||
_context8.next = 4; | ||
return this.fetchTimeout(url, options, { | ||
method: 'DELETE' | ||
}); | ||
case 4: | ||
res = _context8.sent; | ||
_context8.next = 7; | ||
return resHex(res); | ||
case 7: | ||
return _context8.abrupt("return", _context8.sent); | ||
case 8: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
})); | ||
function deleteResource(_x20, _x21, _x22) { | ||
return _deleteResource.apply(this, arguments); | ||
} | ||
var url = this.getUploadURL({ | ||
manifestHash: hash, | ||
path: path | ||
}); | ||
return this._fetchTimeout(url, options, { | ||
method: 'DELETE' | ||
}).then(resText); | ||
}; | ||
return deleteResource; | ||
}(); | ||
_proto.createFeedManifest = function createFeedManifest(params, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.createFeedManifest = | ||
/*#__PURE__*/ | ||
function () { | ||
var _createFeedManifest = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee9(params, options) { | ||
var manifest; | ||
return _regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
manifest = { | ||
entries: [{ | ||
contentType: 'application/bzz-feed', | ||
mod_time: '0001-01-01T00:00:00Z', | ||
// eslint-disable-line @typescript-eslint/camelcase | ||
feed: { | ||
topic: getFeedTopic(params), | ||
user: params.user | ||
} | ||
}] | ||
}; | ||
_context9.next = 4; | ||
return this.uploadFile(JSON.stringify(manifest), options); | ||
case 4: | ||
return _context9.abrupt("return", _context9.sent); | ||
case 5: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee9, this); | ||
})); | ||
function createFeedManifest(_x23, _x24) { | ||
return _createFeedManifest.apply(this, arguments); | ||
} | ||
var manifest = { | ||
entries: [{ | ||
contentType: 'application/bzz-feed', | ||
mod_time: '0001-01-01T00:00:00Z', | ||
feed: { | ||
topic: getFeedTopic(params), | ||
user: params.user | ||
return createFeedManifest; | ||
}(); | ||
_proto.getFeedMetadata = | ||
/*#__PURE__*/ | ||
function () { | ||
var _getFeedMetadata = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee10(hashOrParams, options) { | ||
var url, res; | ||
return _regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getFeedURL(hashOrParams, 'meta'); | ||
_context10.next = 4; | ||
return this.fetchTimeout(url, options); | ||
case 4: | ||
res = _context10.sent; | ||
_context10.next = 7; | ||
return resJSON(res); | ||
case 7: | ||
return _context10.abrupt("return", _context10.sent); | ||
case 8: | ||
case "end": | ||
return _context10.stop(); | ||
} | ||
} | ||
}] | ||
}; | ||
return this.uploadFile(JSON.stringify(manifest), options).then(hexValueType); | ||
}; | ||
}, _callee10, this); | ||
})); | ||
_proto.getFeedMetadata = function getFeedMetadata(hashOrParams, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
function getFeedMetadata(_x25, _x26) { | ||
return _getFeedMetadata.apply(this, arguments); | ||
} | ||
var url = this.getFeedURL(hashOrParams, 'meta'); | ||
return this._fetchTimeout(url, options).then(resJSON); | ||
}; | ||
return getFeedMetadata; | ||
}(); | ||
_proto.getFeedChunk = function getFeedChunk(hashOrParams, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.getFeedChunk = | ||
/*#__PURE__*/ | ||
function () { | ||
var _getFeedChunk = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee11(hashOrParams, options) { | ||
var url, res; | ||
return _regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getFeedURL(hashOrParams); | ||
_context11.next = 4; | ||
return this.fetchTimeout(url, options); | ||
case 4: | ||
res = _context11.sent; | ||
return _context11.abrupt("return", resOrError(res)); | ||
case 6: | ||
case "end": | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee11, this); | ||
})); | ||
function getFeedChunk(_x27, _x28) { | ||
return _getFeedChunk.apply(this, arguments); | ||
} | ||
var url = this.getFeedURL(hashOrParams); | ||
return this._fetchTimeout(url, options).then(resOrError); | ||
}; | ||
return getFeedChunk; | ||
}(); | ||
_proto.getFeedContentHash = function getFeedContentHash(hashOrParams, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.getFeedContentHash = | ||
/*#__PURE__*/ | ||
function () { | ||
var _getFeedContentHash = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee12(hashOrParams, options) { | ||
var res; | ||
return _regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
_context12.next = 3; | ||
return this.getFeedChunk(hashOrParams, options); | ||
case 3: | ||
res = _context12.sent; | ||
_context12.next = 6; | ||
return resSwarmHash(res); | ||
case 6: | ||
return _context12.abrupt("return", _context12.sent); | ||
case 7: | ||
case "end": | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee12, this); | ||
})); | ||
function getFeedContentHash(_x29, _x30) { | ||
return _getFeedContentHash.apply(this, arguments); | ||
} | ||
return this.getFeedChunk(hashOrParams, options).then(resSwarmHash); | ||
}; | ||
return getFeedContentHash; | ||
}(); | ||
_proto.getFeedContent = function getFeedContent(hashOrParams, options) { | ||
var _this3 = this; | ||
_proto.getFeedContent = | ||
/*#__PURE__*/ | ||
function () { | ||
var _getFeedContent = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee13(hashOrParams, options) { | ||
var hash; | ||
return _regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
_context13.next = 3; | ||
return this.getFeedContentHash(hashOrParams, { | ||
headers: options.headers, | ||
timeout: options.timeout | ||
}); | ||
case 3: | ||
hash = _context13.sent; | ||
_context13.next = 6; | ||
return this.download(hash, options); | ||
case 6: | ||
return _context13.abrupt("return", _context13.sent); | ||
case 7: | ||
case "end": | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee13, this); | ||
})); | ||
function getFeedContent(_x31, _x32) { | ||
return _getFeedContent.apply(this, arguments); | ||
} | ||
return this.getFeedContentHash(hashOrParams, { | ||
headers: options.headers, | ||
timeout: options.timeout | ||
}).then(function (hash) { | ||
return _this3.download(hash, options); | ||
}); | ||
}; | ||
return getFeedContent; | ||
}(); | ||
_proto.pollFeedChunk = function pollFeedChunk(hashOrParams, options) { | ||
var _this4 = this, | ||
var _this3 = this, | ||
_merge; | ||
@@ -384,3 +895,3 @@ | ||
pipeline.push(flatMap(function () { | ||
return _this4.getFeedChunk(hashOrParams, options); | ||
return _this3.getFeedChunk(hashOrParams, options); | ||
})); | ||
@@ -390,3 +901,3 @@ } else { | ||
pipeline.push(flatMap(function () { | ||
return _this4._fetchTimeout(url, options).then(function (res) { | ||
return _this3.fetchTimeout(url, options).then(function (res) { | ||
if (res.status === 404) { | ||
@@ -409,4 +920,5 @@ return null; | ||
} | ||
} | ||
} // @ts-ignore | ||
return (_merge = merge.apply(void 0, [interval(options.interval)].concat(sources))).pipe.apply(_merge, pipeline); | ||
@@ -423,5 +935,7 @@ }; | ||
if (options.changedOnly) { | ||
// @ts-ignore | ||
pipeline.push(distinctUntilChanged()); | ||
} | ||
} // @ts-ignore | ||
return (_this$pollFeedChunk = this.pollFeedChunk(hashOrParams, options)).pipe.apply(_this$pollFeedChunk, pipeline); | ||
@@ -431,77 +945,215 @@ }; | ||
_proto.pollFeedContent = function pollFeedContent(hashOrParams, options) { | ||
var _this5 = this; | ||
var _this4 = this; | ||
return this.pollFeedContentHash(hashOrParams, options).pipe(flatMap(function (hash) { | ||
return hash === null ? Promise.resolve(null) : _this5.download(hash, options); | ||
return hash === null ? Promise.resolve(null) : _this4.download(hash, options); | ||
})); | ||
}; | ||
_proto.postSignedFeedChunk = function postSignedFeedChunk(params, body, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
_proto.postSignedFeedChunk = | ||
/*#__PURE__*/ | ||
function () { | ||
var _postSignedFeedChunk = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee14(params, body, options) { | ||
var url, res; | ||
return _regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
url = this.getFeedURL(params); | ||
_context14.next = 4; | ||
return this.fetchTimeout(url, options, { | ||
method: 'POST', | ||
body: body | ||
}); | ||
case 4: | ||
res = _context14.sent; | ||
return _context14.abrupt("return", resOrError(res)); | ||
case 6: | ||
case "end": | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, this); | ||
})); | ||
function postSignedFeedChunk(_x33, _x34, _x35) { | ||
return _postSignedFeedChunk.apply(this, arguments); | ||
} | ||
var url = this.getFeedURL(params); | ||
return this._fetchTimeout(url, options, { | ||
method: 'POST', | ||
body: body | ||
}).then(resOrError); | ||
}; | ||
return postSignedFeedChunk; | ||
}(); | ||
_proto.postFeedChunk = function postFeedChunk(meta, data, options, signParams) { | ||
var _this6 = this; | ||
_proto.postFeedChunk = | ||
/*#__PURE__*/ | ||
function () { | ||
var _postFeedChunk = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee15(meta, data, options, signParams) { | ||
var body, digest, signature; | ||
return _regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
body = createHex(data).toBuffer(); | ||
digest = createFeedDigest(meta, body); | ||
_context15.next = 4; | ||
return this.sign(digest, signParams); | ||
var body = createHex(data).toBuffer(); | ||
var digest = createFeedDigest(meta, body); | ||
return this.sign(digest, signParams).then(function (signature) { | ||
var params = { | ||
user: meta.feed.user, | ||
topic: meta.feed.topic, | ||
time: meta.epoch.time, | ||
level: meta.epoch.level, | ||
signature: signature | ||
}; | ||
return _this6.postSignedFeedChunk(params, body, options); | ||
}); | ||
}; | ||
case 4: | ||
signature = _context15.sent; | ||
_context15.next = 7; | ||
return this.postSignedFeedChunk({ | ||
user: meta.feed.user, | ||
topic: meta.feed.topic, | ||
time: meta.epoch.time, | ||
level: meta.epoch.level, | ||
signature: signature | ||
}, body, options); | ||
_proto.setFeedChunk = function setFeedChunk(hashOrParams, data, options, signParams) { | ||
var _this7 = this; | ||
case 7: | ||
return _context15.abrupt("return", _context15.sent); | ||
return this.getFeedMetadata(hashOrParams, options).then(function (meta) { | ||
return _this7.postFeedChunk(meta, data, options, signParams); | ||
}); | ||
}; | ||
case 8: | ||
case "end": | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee15, this); | ||
})); | ||
_proto.setFeedContentHash = function setFeedContentHash(hashOrParams, contentHash, options, signParams) { | ||
var _this8 = this; | ||
function postFeedChunk(_x36, _x37, _x38, _x39) { | ||
return _postFeedChunk.apply(this, arguments); | ||
} | ||
return this.getFeedMetadata(hashOrParams, options).then(function (meta) { | ||
return _this8.postFeedChunk(meta, "0x" + contentHash, options, signParams); | ||
}); | ||
}; | ||
return postFeedChunk; | ||
}(); | ||
_proto.setFeedContent = function setFeedContent(hashOrParams, data, options, signParams) { | ||
var _this9 = this; | ||
_proto.setFeedChunk = | ||
/*#__PURE__*/ | ||
function () { | ||
var _setFeedChunk = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee16(hashOrParams, data, options, signParams) { | ||
var meta; | ||
return _regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
_context16.next = 2; | ||
return this.getFeedMetadata(hashOrParams, options); | ||
if (options === void 0) { | ||
options = {}; | ||
case 2: | ||
meta = _context16.sent; | ||
_context16.next = 5; | ||
return this.postFeedChunk(meta, data, options, signParams); | ||
case 5: | ||
return _context16.abrupt("return", _context16.sent); | ||
case 6: | ||
case "end": | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee16, this); | ||
})); | ||
function setFeedChunk(_x40, _x41, _x42, _x43) { | ||
return _setFeedChunk.apply(this, arguments); | ||
} | ||
var _options2 = options, | ||
_c = _options2.contentType, | ||
feedOptions = _objectWithoutPropertiesLoose(_options2, ["contentType"]); | ||
return setFeedChunk; | ||
}(); | ||
return Promise.all([this.upload(data, options), this.getFeedMetadata(hashOrParams, feedOptions)]).then(function (_ref) { | ||
var hash = _ref[0], | ||
meta = _ref[1]; | ||
return _this9.postFeedChunk(meta, "0x" + hash, feedOptions, signParams).then(function () { | ||
return hash; | ||
}); | ||
}); | ||
}; | ||
_proto.setFeedContentHash = | ||
/*#__PURE__*/ | ||
function () { | ||
var _setFeedContentHash = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee17(hashOrParams, contentHash, options, signParams) { | ||
var meta; | ||
return _regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
_context17.next = 2; | ||
return this.getFeedMetadata(hashOrParams, options); | ||
case 2: | ||
meta = _context17.sent; | ||
_context17.next = 5; | ||
return this.postFeedChunk(meta, "0x" + contentHash, options, signParams); | ||
case 5: | ||
return _context17.abrupt("return", _context17.sent); | ||
case 6: | ||
case "end": | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee17, this); | ||
})); | ||
function setFeedContentHash(_x44, _x45, _x46, _x47) { | ||
return _setFeedContentHash.apply(this, arguments); | ||
} | ||
return setFeedContentHash; | ||
}(); | ||
_proto.setFeedContent = | ||
/*#__PURE__*/ | ||
function () { | ||
var _setFeedContent = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee18(hashOrParams, data, options, signParams) { | ||
var _options2, _c, feedOptions, _ref, hash, meta; | ||
return _regeneratorRuntime.wrap(function _callee18$(_context18) { | ||
while (1) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
_options2 = options, _c = _options2.contentType, feedOptions = _objectWithoutPropertiesLoose(_options2, ["contentType"]); | ||
_context18.next = 4; | ||
return Promise.all([this.upload(data, options), this.getFeedMetadata(hashOrParams, feedOptions)]); | ||
case 4: | ||
_ref = _context18.sent; | ||
hash = _ref[0]; | ||
meta = _ref[1]; | ||
_context18.next = 9; | ||
return this.postFeedChunk(meta, "0x" + hash, feedOptions, signParams); | ||
case 9: | ||
return _context18.abrupt("return", hash); | ||
case 10: | ||
case "end": | ||
return _context18.stop(); | ||
} | ||
} | ||
}, _callee18, this); | ||
})); | ||
function setFeedContent(_x48, _x49, _x50, _x51) { | ||
return _setFeedContent.apply(this, arguments); | ||
} | ||
return setFeedContent; | ||
}(); | ||
return BaseBzz; | ||
}(); | ||
export { BaseBzz as default }; | ||
}(); |
{ | ||
"name": "@erebos/api-bzz-base", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Base Bzz API", | ||
@@ -17,19 +17,20 @@ "repository": "git@github.com:MainframeHQ/erebos.git", | ||
"scripts": { | ||
"clean": "del cjs esm", | ||
"build:cjs": "BABEL_ENV='browser-cjs' babel src --out-dir cjs", | ||
"build:esm": "BABEL_ENV='browser-esm' babel src --out-dir esm", | ||
"clean": "del cjs esm types", | ||
"test:types": "tsc --noEmit -p tsconfig.build.json", | ||
"build:cjs": "BABEL_ENV='browser-cjs' babel src --out-dir cjs --extensions \".ts\"", | ||
"build:esm": "BABEL_ENV='browser-esm' babel src --out-dir esm --extensions \".ts\"", | ||
"build:js": "yarn build:cjs && yarn build:esm", | ||
"build:flow": "flow-copy-source src cjs && flow-copy-source src esm", | ||
"build": "yarn clean && yarn build:js && yarn build:flow", | ||
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json", | ||
"build": "yarn test:types && yarn clean && yarn build:js && yarn build:types", | ||
"prepublishOnly": "yarn build" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.4.5", | ||
"@erebos/hex": "^0.7.0", | ||
"@erebos/keccak256": "^0.7.0", | ||
"@babel/runtime": "^7.5.5", | ||
"@erebos/hex": "^0.9.0", | ||
"@erebos/keccak256": "^0.9.0", | ||
"rxjs": "^6.5.2" | ||
}, | ||
"devDependencies": { | ||
"flow-bin": "^0.101.0" | ||
"typescript": "3.5.2" | ||
} | ||
} |
@@ -1,200 +0,55 @@ | ||
import { hexInput, hexValue } from '@erebos/hex' | ||
import { Observable } from 'rxjs' | ||
export interface DirectoryData { | ||
[path: string]: { data: string | Buffer; contentType: string; size?: number } | ||
/// <reference types="node" /> | ||
import { hexInput, hexValue } from '@erebos/hex'; | ||
import { Observable } from 'rxjs'; | ||
import { BaseResponse, RequestInit, Fetch, BzzConfig, BzzMode, DirectoryData, DownloadOptions, FeedMetadata, FeedParams, FeedUpdateParams, FetchOptions, ListResult, PollOptions, PollContentHashOptions, PollContentOptions, SignBytesFunc, UploadOptions } from './types'; | ||
export * from './feed'; | ||
export * from './types'; | ||
export declare const BZZ_MODE_PROTOCOLS: { | ||
default: string; | ||
feed: string; | ||
immutable: string; | ||
raw: string; | ||
}; | ||
export declare function getModeProtocol(mode?: BzzMode): string; | ||
export declare class HTTPError extends Error { | ||
status: number; | ||
constructor(status: number, message: string); | ||
} | ||
export interface FileEntry { | ||
data: string | Buffer | ||
path: string | ||
size?: number | ||
export declare function resOrError<R extends BaseResponse>(res: R): R; | ||
export declare function resJSON<R extends BaseResponse, T = any>(res: R): Promise<T>; | ||
export declare function resText<R extends BaseResponse>(res: R): Promise<string>; | ||
export declare function resHex<R extends BaseResponse>(res: R): Promise<hexValue>; | ||
export declare function resSwarmHash<R extends BaseResponse>(res: R): Promise<string>; | ||
export declare class BaseBzz<Response extends BaseResponse> { | ||
protected defaultTimeout: number; | ||
protected fetch: Fetch<Response>; | ||
protected signBytes: SignBytesFunc; | ||
protected url: string; | ||
constructor(fetch: Fetch<Response>, config: BzzConfig); | ||
protected fetchTimeout(url: string, options: FetchOptions, params?: RequestInit): Promise<Response>; | ||
sign(bytes: Array<number>, params?: any): Promise<hexValue>; | ||
getDownloadURL(hash: string, options?: DownloadOptions, raw?: boolean): string; | ||
getUploadURL(options?: UploadOptions, raw?: boolean): string; | ||
getFeedURL(hashOrParams: string | FeedParams | FeedUpdateParams, flag?: 'meta'): string; | ||
hash(domain: string, options?: FetchOptions): Promise<hexValue>; | ||
list(hash: string, options?: DownloadOptions): Promise<ListResult>; | ||
download(hash: string, options?: DownloadOptions): Promise<Response>; | ||
protected uploadBody(body: any, options: UploadOptions, raw?: boolean): Promise<hexValue>; | ||
uploadFile(data: string | Buffer, options?: UploadOptions): Promise<hexValue>; | ||
uploadDirectory(_directory: DirectoryData, _options?: UploadOptions): Promise<hexValue>; | ||
upload(data: string | Buffer | DirectoryData, options?: UploadOptions): Promise<hexValue>; | ||
deleteResource(hash: string, path: string, options?: FetchOptions): Promise<hexValue>; | ||
createFeedManifest(params: FeedParams, options?: UploadOptions): Promise<hexValue>; | ||
getFeedMetadata(hashOrParams: string | FeedParams, options?: FetchOptions): Promise<FeedMetadata>; | ||
getFeedChunk(hashOrParams: string | FeedParams, options?: FetchOptions): Promise<Response>; | ||
getFeedContentHash(hashOrParams: string | FeedParams, options?: FetchOptions): Promise<string>; | ||
getFeedContent(hashOrParams: string | FeedParams, options?: DownloadOptions): Promise<Response>; | ||
pollFeedChunk(hashOrParams: string | FeedParams, options: PollOptions): Observable<Response>; | ||
pollFeedContentHash(hashOrParams: string | FeedParams, options: PollContentHashOptions): Observable<string | null>; | ||
pollFeedContent(hashOrParams: string | FeedParams, options: PollContentOptions): Observable<Response | null>; | ||
postSignedFeedChunk(params: FeedUpdateParams, body: Buffer, options?: FetchOptions): Promise<Response>; | ||
postFeedChunk(meta: FeedMetadata, data: hexInput, options?: FetchOptions, signParams?: any): Promise<Response>; | ||
setFeedChunk(hashOrParams: string | FeedParams, data: hexInput, options?: FetchOptions, signParams?: any): Promise<Response>; | ||
setFeedContentHash(hashOrParams: string | FeedParams, contentHash: string, options?: FetchOptions, signParams?: any): Promise<Response>; | ||
setFeedContent(hashOrParams: string | FeedParams, data: string | Buffer | DirectoryData, options?: UploadOptions, signParams?: any): Promise<hexValue>; | ||
} | ||
export interface ListEntry { | ||
hash: string | ||
path: string | ||
contentType: string | ||
size: number | ||
mod_time: string | ||
} | ||
export interface ListResult { | ||
common_prefixes?: Array<string> | ||
entries?: Array<ListEntry> | ||
} | ||
export interface FeedTopicParams { | ||
name?: string | ||
topic?: string | Buffer | ||
} | ||
export interface FeedMetadata { | ||
feed: { | ||
topic: hexValue | ||
user: hexValue | ||
} | ||
epoch: { | ||
time: number | ||
level: number | ||
} | ||
protocolVersion: number | ||
} | ||
export interface FetchOptions { | ||
headers?: Object | ||
timeout?: number | ||
} | ||
export interface FileOptions extends FetchOptions { | ||
contentType?: string | ||
path?: string | ||
} | ||
export type BzzMode = 'default' | 'immutable' | 'raw' | ||
export interface DownloadOptions extends FileOptions { | ||
mode?: BzzMode | ||
} | ||
export interface UploadOptions extends FileOptions { | ||
defaultPath?: string | ||
encrypt?: boolean | ||
manifestHash?: hexValue | string | ||
} | ||
export interface PollOptions extends FetchOptions { | ||
interval: number | ||
immediate?: boolean | ||
whenEmpty?: 'accept' | 'ignore' | 'error' | ||
trigger?: Observable<void> | ||
} | ||
export interface PollContentHashOptions extends PollOptions { | ||
changedOnly?: boolean | ||
} | ||
export interface PollContentOptions | ||
extends DownloadOptions, | ||
PollContentHashOptions {} | ||
export interface FeedParams { | ||
user: string | ||
level?: number | ||
name?: string | ||
time?: number | ||
topic?: string | ||
} | ||
export interface FeedUpdateParams { | ||
user: string | ||
level: number | ||
time: number | ||
topic: string | ||
signature: string | ||
} | ||
export type SignBytesFunc = ( | ||
digest: Array<number>, | ||
params?: any, | ||
) => Promise<Array<number>> | ||
export type BzzConfig = { | ||
signBytes?: SignBytesFunc | ||
timeout?: number | ||
url: string | ||
} | ||
export function createFeedDigest(meta: FeedMetadata, data: hexInput): Buffer | ||
export function getFeedTopic(params: FeedTopicParams): hexValue | ||
export class HTTPError extends Error { | ||
status: number | ||
constructor(status: number, message: string) | ||
} | ||
export default abstract class BaseBzz<T> { | ||
constructor(config: BzzConfig) | ||
sign(bytes: Array<number>, params?: any): Promise<hexValue> | ||
getDownloadURL(hash: string, options: DownloadOptions, raw?: boolean): string | ||
getUploadURL(options: UploadOptions, raw?: boolean): string | ||
getFeedURL( | ||
hashOrParams: string | FeedParams | FeedUpdateParams, | ||
flag?: 'meta', | ||
): string | ||
hash(domain: string, options?: FetchOptions): Promise<hexValue> | ||
list(hash: string, options?: DownloadOptions): Promise<ListResult> | ||
download(hash: string, options?: DownloadOptions): Promise<T> | ||
uploadFile(data: string | Buffer, options?: UploadOptions): Promise<hexValue> | ||
upload( | ||
data: string | Buffer | DirectoryData, | ||
options?: UploadOptions, | ||
): Promise<hexValue> | ||
deleteResource( | ||
hash: string, | ||
path: string, | ||
options?: FetchOptions, | ||
): Promise<hexValue> | ||
createFeedManifest( | ||
params: FeedParams, | ||
options?: UploadOptions, | ||
): Promise<hexValue> | ||
getFeedMetadata( | ||
hashOrParams: string | FeedParams, | ||
options?: FetchOptions, | ||
): Promise<FeedMetadata> | ||
getFeedChunk( | ||
hashOrParams: string | FeedParams, | ||
options?: FetchOptions, | ||
): Promise<T> | ||
getFeedContentHash( | ||
hashOrParams: string | FeedParams, | ||
options?: FetchOptions, | ||
): Promise<string | null> | ||
getFeedContent( | ||
hashOrParams: string | FeedParams, | ||
options?: DownloadOptions, | ||
): Promise<T | null> | ||
pollFeedChunk( | ||
hashOrParams: string | FeedParams, | ||
options: PollOptions, | ||
): Observable<T> | ||
pollFeedContentHash( | ||
hashOrParams: string | FeedParams, | ||
options: PollContentHashOptions, | ||
): Observable<string | null> | ||
pollFeedContent( | ||
hashOrParams: string | FeedParams, | ||
options: PollContentOptions, | ||
): Observable<T | null> | ||
postSignedFeedChunk( | ||
params: FeedUpdateParams, | ||
body: Buffer, | ||
options?: FetchOptions, | ||
): Promise<T> | ||
postFeedChunk( | ||
meta: FeedMetadata, | ||
data: hexInput, | ||
options?: FetchOptions, | ||
signParams?: any, | ||
): Promise<T> | ||
setFeedChunk( | ||
hashOrParams: string | FeedParams, | ||
data: hexInput, | ||
options?: FetchOptions, | ||
signParams?: any, | ||
): Promise<T> | ||
setFeedContentHash( | ||
hashOrParams: string | FeedParams, | ||
contentHash: string, | ||
options?: FetchOptions, | ||
signParams?: any, | ||
): Promise<T> | ||
setFeedContent( | ||
hashOrParams: string | FeedParams, | ||
data: string | Buffer | DirectoryData, | ||
options?: UploadOptions, | ||
signParams?: any, | ||
): Promise<hexValue> | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74156
2145
11
3
+ Added@erebos/hex@0.9.0(transitive)
+ Added@erebos/keccak256@0.9.0(transitive)
- Removed@erebos/hex@0.7.0(transitive)
- Removed@erebos/keccak256@0.7.0(transitive)
Updated@babel/runtime@^7.5.5
Updated@erebos/hex@^0.9.0
Updated@erebos/keccak256@^0.9.0