kubo-rpc-client-esm-cjs
Version:
A client library for the Kubo RPC API
86 lines • 4.09 kB
JavaScript
;
/* eslint-env mocha */
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("aegir/chai");
const from_string_1 = require("uint8arrays/from-string");
const p_defer_1 = __importDefault(require("p-defer"));
const factory_js_1 = require("./utils/factory.js");
const f = (0, factory_js_1.factory)();
describe('.pubsub', function () {
this.timeout(20 * 1000);
describe('.subscribe', function () {
/** @type {import('../src/types.js').IPFS} */
let ipfs;
/** @type {any} */
let ctl;
beforeEach(function () {
return __awaiter(this, void 0, void 0, function* () {
this.timeout(30 * 1000); // slow CI
ctl = yield yield f.spawn({
args: ['--enable-pubsub-experiment']
});
ipfs = ctl.api;
});
});
afterEach(function () { return f.clean(); });
it('.onError when connection is closed', function () {
return __awaiter(this, void 0, void 0, function* () {
const topic = 'gossipboom';
let messageCount = 0;
const onError = (0, p_defer_1.default)();
yield ipfs.pubsub.subscribe(topic, message => {
messageCount++;
if (messageCount === 2) {
// Stop the daemon
ctl.stop().catch();
}
}, {
onError: onError.resolve
});
yield ipfs.pubsub.publish(topic, (0, from_string_1.fromString)('hello'));
yield ipfs.pubsub.publish(topic, (0, from_string_1.fromString)('bye'));
yield (0, chai_1.expect)(onError.promise).to.eventually.be.fulfilled().and.to.be.instanceOf(Error);
});
});
it('does not call onError when aborted', function () {
return __awaiter(this, void 0, void 0, function* () {
const controller = new AbortController();
const topic = 'gossipabort';
const messages = [];
const onError = (0, p_defer_1.default)();
const onReceived = (0, p_defer_1.default)();
yield ipfs.pubsub.subscribe(topic, message => {
messages.push(message);
if (messages.length === 2) {
onReceived.resolve();
}
}, {
onError: onError.resolve,
signal: controller.signal
});
yield ipfs.pubsub.publish(topic, (0, from_string_1.fromString)('hello'));
yield ipfs.pubsub.publish(topic, (0, from_string_1.fromString)('bye'));
yield onReceived.promise;
controller.abort();
// Stop the daemon
yield ctl.stop();
// Just to make sure no error is caused by above line
setTimeout(onError.resolve, 200, 'aborted');
yield (0, chai_1.expect)(onError.promise).to.eventually.be.fulfilled().and.to.equal('aborted');
});
});
});
});
//# sourceMappingURL=pubsub.spec.js.map