textiot
Version:
A framework for building web and native (IoT) Dapps on the IPFS network
126 lines (125 loc) • 5.08 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("../core/api");
const handlers_1 = require("../helpers/handlers");
const ksuid_1 = __importDefault(require("@therebel/ksuid"));
/**
* IPFS is an API module for working with an underlying IPFS peer
*
* @extends API
*/
class IPFS extends api_1.API {
/**
* Retrieves underlying IPFS peer ID
* @returns The underlying IPFS peer ID
*/
id() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`ipfs/id`);
return response.text();
});
}
/**
* Lists the set of peers to which this node is connected
*
* @param verbose Display all extra information
* @param latency Also list information about latency to each peer
* @param streams Also list information about open streams for each peer
* @param direction Also list information about the direction of connection
*/
peers(verbose, latency, streams, direction) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet('ipfs/swarm/peers', undefined, {
verbose: (!!verbose).toString(),
latency: (!!latency).toString(),
streams: (!!streams).toString(),
direction: (!!direction).toString()
});
return response.json();
});
}
/**
* Retrieves the data behind an IPFS CID path
*
* @param path IPFS/IPNS CID path
* @param key Key to decrypt the underlying data on-the-fly
* @returns The underlying data behind the given IPFS CID path
*/
cat(path, key) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`ipfs/cat/${path}`, undefined, { key: key || '' });
return response.blob();
});
}
/**
* Opens a new direct connection to a peer using an IPFS multiaddr
*
* @param addr Peer IPFS multiaddr
* @returns Whether the peer swarm connect was successfull
*/
connect(addr) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendPost(`ipfs/swarm/connect`, [addr]);
return response.status === 200;
});
}
/**
* Publishes a message to a given pubsub topic
*
* @param topic The topic to publish to
* @param data The payload of message to publish
* @returns Whether the publish was successfull
*/
pubsubPub(topic, data) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendPost(`ipfs/pubsub/pub/${topic}`, undefined, undefined, data, undefined, typeof data === 'string');
return response.status === 200;
});
}
/**
* Subscribes to messages on a given topic with GET
*
* @param topic The ipfs pubsub sub topic
* @returns A ReadableStream of ArrayBuffer
*/
pubsubSubGet(topic, queryId) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`ipfs/pubsub/sub/${topic}`, undefined, { queryId });
if (!response.body) {
throw Error('Empty response stream');
}
return handlers_1.readableNodeToWeb(response.body);
});
}
/**
* Subscribes to messages on a given topic with EventSource or GET
*
* @param topic The ipfs pubsub sub topic
* @param useEventSource Whether to use EventSource or GET
* @returns An object with queryId and EventSource or a ReadableStream of ArrayBuffer
*/
pubsubSub(topic, useEventSource) {
return __awaiter(this, void 0, void 0, function* () {
const ksuidFromAsync = yield ksuid_1.default.random();
const queryId = ksuidFromAsync.string;
const queryHandle = useEventSource ? this.sendEventSource(`ipfs/pubsub/sub/${topic}`, undefined, { events: true, queryId }) : yield this.pubsubSubGet(topic, queryId);
return {
queryId,
queryHandle
};
});
}
}
exports.default = IPFS;
;