UNPKG

textiot

Version:

A framework for building web and native (IoT) Dapps on the IPFS network

101 lines (100 loc) 3.41 kB
"use strict"; 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.pubsubSub = exports.pubsubPub = exports.dataAtPath = exports.peers = exports.connect = exports.peerId = void 0; const react_native_1 = require("react-native"); const buffer_1 = require("buffer"); const model_1 = require("./model"); const { IpfsBridge } = react_native_1.NativeModules; /** * Get node's IPFS peerId. * ```typescript * Textile.ipfs.peerId(); * ``` */ function peerId() { return __awaiter(this, void 0, void 0, function* () { const result = yield IpfsBridge.peerId(); return result; }); } exports.peerId = peerId; /** * Open a new direct connection to a peer using an IPFS multiaddr * ```typescript * Textile.ipfs.connect(multiaddr); * ``` */ function connect(multiaddr) { return __awaiter(this, void 0, void 0, function* () { const result = yield IpfsBridge.connect(multiaddr); return result; }); } exports.connect = connect; /** * Request all Peers to which this node is connected. * ```typescript * Textile.ipfs.peers(verbose, latency, streams, direction); * ``` */ function peers(verbose, latency, streams, direction) { return __awaiter(this, void 0, void 0, function* () { const result = yield IpfsBridge.peers(verbose == true, latency == true, streams == true, direction == true); return model_1.SwarmPeerList.decode(buffer_1.Buffer.from(result, 'base64')); }); } exports.peers = peers; /** * Get raw file data by IPFS path. See `cat` method in IPFS. * ```typescript * Textile.ipfs.dataAtPath(path); * ``` */ function dataAtPath(path) { return __awaiter(this, void 0, void 0, function* () { const { data, mediaType } = yield IpfsBridge.dataAtPath(path); return { data: buffer_1.Buffer.from(data, 'base64'), mediaType }; }); } exports.dataAtPath = dataAtPath; /** * Publishes a message to a given pubsub topic * ```typescript * Textile.ipfs.pubsubPub(topic, data); * ``` */ function pubsubPub(topic, data) { return __awaiter(this, void 0, void 0, function* () { return yield IpfsBridge.pubsubPub(topic, typeof data === 'string' ? data : JSON.stringify(data)); }); } exports.pubsubPub = pubsubPub; /** * Subscribes to messages on a given topic * ```typescript * Textile.ipfs.pubsubSub(topic); * ``` */ function pubsubSub(topic) { return __awaiter(this, void 0, void 0, function* () { const queryId = yield IpfsBridge.pubsubSub(topic); return { queryId, queryHandle: { close: () => { IpfsBridge.cancelPubsubSub(queryId); } } }; }); } exports.pubsubSub = pubsubSub;