UNPKG

@actyx/sdk

Version:
84 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Actyx = void 0; const internal_common_1 = require("./internal_common"); const log_1 = require("./internal_common/log"); const node_info_1 = require("./node-info"); const snapshotStore_1 = require("./snapshotStore"); const types_1 = require("./types"); const v1_1 = require("./v1"); const v2_1 = require("./v2"); const blobSnapshotStore_1 = require("./v2/blobSnapshotStore"); const utils_1 = require("./v2/utils"); const createV2 = async (manifest, opts, nodeId) => { const token = await (0, utils_1.getToken)(opts, manifest); const [ws, tok] = await (0, v2_1.makeWsMultiplexerV2)(opts, token, manifest); const eventStore = new v2_1.WebsocketEventStoreV2(ws, types_1.AppId.of(manifest.appId), () => tok[1]); const snapshotStore = new blobSnapshotStore_1.BlobSnapshotStore('http://' + (0, utils_1.getApiLocation)(opts.actyxHost, opts.actyxPort), () => tok[0], () => tok[1], 100000000); const fns = (0, internal_common_1.EventFnsFromEventStoreV2)(nodeId, eventStore, snapshotStore, () => tok[1]); const waitForSync = async () => (0, utils_1.v2WaitForSwarmSync)(opts, token, fns.offsets); const getNodeInfo = (0, node_info_1.getInfo)(opts); return { ...fns, snapshotStore, nodeId, dispose: () => ws.close(), waitForSync, nodeInfo: (maxAgeMillis) => getNodeInfo(tok[0], maxAgeMillis), }; }; const createV1 = async (opts) => { const { eventStore, sourceId, close, snapshotStore } = await (0, v1_1.mkV1eventStore)(opts); const fns = (0, internal_common_1.EventFnsFromEventStoreV2)(sourceId, eventStore, snapshotStore, () => '1.0.0'); return { ...fns, snapshotStore, nodeId: sourceId, dispose: () => close(), waitForSync: () => Promise.resolve(), nodeInfo: () => Promise.resolve(node_info_1.invalidNodeInfo), }; }; /** Function for creating `Actyx` instances. * @public */ exports.Actyx = { /** Create an `Actyx` instance that talks to a running `Actyx` system. * @public */ of: async (manifest, opts = {}) => { const nodeId = await (0, v2_1.v2getNodeId)(opts); log_1.log.actyx.debug('NodeId call returned:', nodeId); if (!nodeId) { // Try connecting to v1 if we failed to retrieve a v2 node id // (Note that if the port is completely unreachable, v2getNodeId will throw an exception and we don’t get here.) log_1.log.actyx.debug('NodeId was null, trying to reach V1 backend...'); return createV1(opts); } log_1.log.actyx.debug('Detected V2 is running, trying to authorize with manifest', JSON.stringify(manifest)); return createV2(manifest, opts, nodeId); }, /** * Create an `Actyx` instance that mocks all APIs within TypeScript. Useful for unit-tests. * Will not talk to other nodes, but rather that can be simulated via `directlyPushEvents`. * @public */ test: (opts = {}) => { const store = internal_common_1.EventStoreV2.test(opts.nodeId, opts.timeInjector); const snaps = snapshotStore_1.SnapshotStore.inMem(); const nodeId = opts.nodeId || types_1.NodeId.of('TESTNODEID'); const fns = (0, internal_common_1.EventFnsFromEventStoreV2)(nodeId, store, snaps, () => '2.0.0'); return { ...fns, snapshotStore: snaps, nodeId, directlyPushEvents: store.directlyPushEvents, dispose: () => { store.close(); }, waitForSync: async () => { /* noop */ }, // snapshotStore: snaps, }; }, }; //# sourceMappingURL=actyx.js.map