@ethersphere/bee-js
Version:
Javascript client for Bee
50 lines (49 loc) • 1.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.subscribe = exports.send = void 0;
const cafe_utility_1 = require("cafe-utility");
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
const headers_1 = require("../utils/headers");
const http_1 = require("../utils/http");
const endpoint = 'pss';
/**
* Send to recipient or target with Postal Service for Swarm
*
* @param requestOptions Options for making requests
* @param topic Topic name
* @param target Target message address prefix
* @param data
* @param postageBatchId Postage BatchId that will be assigned to sent message
* @param recipient Recipient public key
*
*/
async function send(requestOptions, topic, target, data, postageBatchId, recipient) {
await (0, http_1.http)(requestOptions, {
method: 'post',
url: `${endpoint}/send/${topic}/${target}`,
data,
responseType: 'json',
params: { recipient },
headers: (0, headers_1.prepareRequestHeaders)(postageBatchId),
});
}
exports.send = send;
/**
* Subscribe for messages on the given topic
*
* @param url Bee node URL
* @param topic Topic name
*/
function subscribe(url, topic, headers) {
const wsUrl = url.replace(/^http/i, 'ws');
if (cafe_utility_1.System.whereAmI() === 'browser') {
return new isomorphic_ws_1.default(`${wsUrl}/${endpoint}/subscribe/${topic.toHex()}`);
}
return new isomorphic_ws_1.default(`${wsUrl}/${endpoint}/subscribe/${topic.toHex()}`, {
headers,
});
}
exports.subscribe = subscribe;