kubo-rpc-client-esm-cjs
Version:
A client library for the Kubo RPC API
49 lines • 2.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPut = void 0;
const cid_1 = require("multiformats/cid");
const multipart_request_1 = require("ipfs-core-utils/multipart-request");
const configure_js_1 = require("../lib/configure.js");
const to_url_search_params_js_1 = require("../lib/to-url-search-params.js");
const abort_signal_js_1 = require("../lib/abort-signal.js");
exports.createPut = (0, configure_js_1.configure)((api, configOptions) => {
/**
* @type {import('../types').BlockAPI["put"]}
* @see https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-block-put
*/
function put(data, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
// allow aborting requests on body errors
const controller = new AbortController();
const signal = (0, abort_signal_js_1.abortSignal)(controller.signal, options.signal);
let res;
try {
const response = yield api.post('block/put', Object.assign({ signal, searchParams: (0, to_url_search_params_js_1.toUrlSearchParams)(options) }, (yield (0, multipart_request_1.multipartRequest)([data], controller, options.headers))));
res = yield response.json();
}
catch ( /** @type {any} */err) {
// Retry with "protobuf"/"cbor" format for go-ipfs
// TODO: remove when https://github.com/ipfs/go-cid/issues/75 resolved
if (options.format === 'dag-pb') {
return put(data, Object.assign(Object.assign({}, options), { format: 'protobuf' }));
}
else if (options.format === 'dag-cbor') {
return put(data, Object.assign(Object.assign({}, options), { format: 'cbor' }));
}
throw err;
}
return cid_1.CID.parse(res.Key);
});
}
return put;
});
//# sourceMappingURL=put.js.map