UNPKG

@ethersphere/bee-js

Version:
208 lines (207 loc) 11.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeTagUid = exports.prepareAllTagsOptions = exports.assertFileData = exports.assertData = exports.prepareTransactionOptions = exports.preparePostageBatchOptions = exports.prepareGsocMessageHandler = exports.preparePssMessageHandler = exports.isTag = exports.prepareCollectionUploadOptions = exports.prepareFileUploadOptions = exports.prepareRedundantUploadOptions = exports.prepareUploadOptions = exports.prepareDownloadOptions = exports.prepareBeeRequestOptions = exports.asNumberString = exports.isReadable = void 0; const cafe_utility_1 = require("cafe-utility"); const stream = __importStar(require("stream")); const types_1 = require("../types"); const file_1 = require("./file"); const typed_bytes_1 = require("./typed-bytes"); function isReadable(value) { return typeof stream.Readable !== 'undefined' && value instanceof stream.Readable; } exports.isReadable = isReadable; function asNumberString(value, options) { if (typeof value === 'bigint') { value = value.toString(); } return cafe_utility_1.Types.asIntegerString(value, options); } exports.asNumberString = asNumberString; function prepareBeeRequestOptions(value) { const object = cafe_utility_1.Types.asObject(value, { name: 'BeeRequestOptions' }); return { baseURL: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asString(x, { name: 'baseURL' }), object.baseURL), timeout: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'timeout', min: 0 }), object.timeout), headers: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asStringMap(x, { name: 'headers' }), object.headers), onRequest: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asFunction(x, { name: 'onRequest' }), object.onRequest), httpAgent: object.httpAgent, httpsAgent: object.httpsAgent, endlesslyRetry: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'endlesslyRetry' }), object.endlesslyRetry), }; } exports.prepareBeeRequestOptions = prepareBeeRequestOptions; function prepareDownloadOptions(value) { const object = cafe_utility_1.Types.asObject(value, { name: 'DownloadOptions' }); return { redundancyStrategy: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'redundancyStrategy' }), object.redundancyStrategy), fallback: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'fallback' }), object.fallback), timeoutMs: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'timeoutMs', min: 0 }), object.timeoutMs), actPublisher: cafe_utility_1.Types.asOptional(x => new typed_bytes_1.PublicKey(x), object.actPublisher), actHistoryAddress: cafe_utility_1.Types.asOptional(x => new typed_bytes_1.Reference(x), object.actHistoryAddress), actTimestamp: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asNumber(x, { name: 'actTimestamp' }), object.actTimestamp), }; } exports.prepareDownloadOptions = prepareDownloadOptions; function prepareUploadOptions(value, name = 'UploadOptions') { const object = cafe_utility_1.Types.asObject(value, { name }); return { act: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'act' }), object.act), actHistoryAddress: cafe_utility_1.Types.asOptional(x => new typed_bytes_1.Reference(x), object.actHistoryAddress), deferred: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'deferred' }), object.deferred), encrypt: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'encrypt' }), object.encrypt), pin: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'pin' }), object.pin), tag: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'tag', min: 0 }), object.tag), }; } exports.prepareUploadOptions = prepareUploadOptions; function prepareRedundantUploadOptions(value, name = 'UploadOptions') { const uploadOptions = prepareUploadOptions(value, name); const object = cafe_utility_1.Types.asObject(value, { name }); return { ...uploadOptions, redundancyLevel: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'redundancyLevel', min: 0 }), object.redundancyLevel), }; } exports.prepareRedundantUploadOptions = prepareRedundantUploadOptions; function prepareFileUploadOptions(value) { const uploadOptions = prepareUploadOptions(value, 'FileUploadOptions'); const object = cafe_utility_1.Types.asObject(value, { name: 'FileUploadOptions' }); return { ...uploadOptions, size: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'size', min: 0 }), object.size), contentType: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asString(x, { name: 'contentType' }), object.contentType), redundancyLevel: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'redundancyLevel', min: 0 }), object.redundancyLevel), }; } exports.prepareFileUploadOptions = prepareFileUploadOptions; function prepareCollectionUploadOptions(value) { const uploadOptions = prepareUploadOptions(value, 'CollectionUploadOptions'); const object = cafe_utility_1.Types.asObject(value, { name: 'CollectionUploadOptions' }); return { ...uploadOptions, errorDocument: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asString(x, { name: 'errorDocument' }), object.errorDocument), indexDocument: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asString(x, { name: 'indexDocument' }), object.indexDocument), redundancyLevel: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'redundancyLevel', min: 0 }), object.redundancyLevel), }; } exports.prepareCollectionUploadOptions = prepareCollectionUploadOptions; function isTag(value) { try { const object = cafe_utility_1.Types.asObject(value, { name: 'Tag' }); cafe_utility_1.Types.asInteger(object.uid, { name: 'Tag.uid' }); return true; } catch { return false; } } exports.isTag = isTag; function preparePssMessageHandler(value) { const object = cafe_utility_1.Types.asObject(value, { name: 'PssMessageHandler' }); return { onMessage: cafe_utility_1.Types.asFunction(object.onMessage, { name: 'onMessage' }), onError: cafe_utility_1.Types.asFunction(object.onError, { name: 'onError' }), }; } exports.preparePssMessageHandler = preparePssMessageHandler; function prepareGsocMessageHandler(value) { const object = cafe_utility_1.Types.asObject(value, { name: 'GsocMessageHandler' }); return { onMessage: cafe_utility_1.Types.asFunction(object.onMessage, { name: 'onMessage' }), onError: cafe_utility_1.Types.asFunction(object.onError, { name: 'onError' }), }; } exports.prepareGsocMessageHandler = prepareGsocMessageHandler; function preparePostageBatchOptions(value) { const object = cafe_utility_1.Types.asObject(value, { name: 'PostageBatchOptions' }); return { gasPrice: cafe_utility_1.Types.asOptional(x => asNumberString(x, { name: 'gasPrice' }), object.gasPrice), immutableFlag: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'immutableFlag' }), object.immutableFlag), label: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asString(x, { name: 'label' }), object.label), waitForUsable: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'waitForUsable' }), object.waitForUsable), waitForUsableTimeout: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'waitForUsableTimeout', min: 0 }), object.waitForUsableTimeout), }; } exports.preparePostageBatchOptions = preparePostageBatchOptions; function prepareTransactionOptions(value, name = 'TransactionOptions') { const object = cafe_utility_1.Types.asObject(value, { name }); return { gasLimit: cafe_utility_1.Types.asOptional(x => asNumberString(x, { name: 'gasLimit', min: 0n }), object.gasLimit), gasPrice: cafe_utility_1.Types.asOptional(x => asNumberString(x, { name: 'gasPrice', min: 0n }), object.gasPrice), }; } exports.prepareTransactionOptions = prepareTransactionOptions; /** * Check whether the given parameter is valid data to upload * @param value * @throws TypeError if not valid */ function assertData(value) { if (typeof value !== 'string' && !(value instanceof Uint8Array)) { throw new TypeError('Data must be either string or Uint8Array!'); } } exports.assertData = assertData; /** * Check whether the given parameter is a correct file representation to file upload. * @param value * @throws TypeError if not valid */ function assertFileData(value) { if (typeof value !== 'string' && !(value instanceof Uint8Array) && !(0, file_1.isFile)(value) && !isReadable(value)) { throw new TypeError('Data must be either string, Readable, Uint8Array or File!'); } } exports.assertFileData = assertFileData; /** * Checks whether optional options for AllTags query are valid * @param options */ function prepareAllTagsOptions(value) { const object = cafe_utility_1.Types.asObject(value, { name: 'AllTagsOptions' }); return { limit: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'limit', min: types_1.TAGS_LIMIT_MIN, max: types_1.TAGS_LIMIT_MAX }), object.limit), offset: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asInteger(x, { name: 'offset', min: 0 }), object.offset), }; } exports.prepareAllTagsOptions = prepareAllTagsOptions; /** * Utility functions that return Tag UID * @param tagUid */ function makeTagUid(tagUid) { if (tagUid === undefined || tagUid === null) { throw new TypeError(`Expected number | Tag | string from tagUid, got: ${tagUid}`); } if (isTag(tagUid)) { return tagUid.uid; } else if (typeof tagUid === 'number' || typeof tagUid === 'string') { return cafe_utility_1.Types.asNumber(tagUid, { name: 'tagUid', min: 0 }); } throw new TypeError(`Expected number | Tag | string from tagUid, got: ${tagUid}`); } exports.makeTagUid = makeTagUid;