UNPKG

jinaga

Version:

Data management for web and mobile applications.

161 lines 7.6 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.JinagaBrowser = void 0; const authentication_noop_1 = require("./authentication/authentication-noop"); const authentication_offline_1 = require("./authentication/authentication-offline"); const authentication_web_client_1 = require("./authentication/authentication-web-client"); const pass_through_fork_1 = require("./fork/pass-through-fork"); const persistent_fork_1 = require("./fork/persistent-fork"); const transient_fork_1 = require("./fork/transient-fork"); const fetch_1 = require("./http/fetch"); const httpNetwork_1 = require("./http/httpNetwork"); const web_client_1 = require("./http/web-client"); const indexeddb_login_store_1 = require("./indexeddb/indexeddb-login-store"); const indexeddb_queue_1 = require("./indexeddb/indexeddb-queue"); const indexeddb_store_1 = require("./indexeddb/indexeddb-store"); const jinaga_1 = require("./jinaga"); const factManager_1 = require("./managers/factManager"); const NetworkManager_1 = require("./managers/NetworkManager"); const memory_store_1 = require("./memory/memory-store"); const observable_1 = require("./observable/observable"); const purgeConditions_1 = require("./purge/purgeConditions"); const validate_1 = require("./purge/validate"); const wsGraphNetwork_1 = require("./ws/wsGraphNetwork"); class JinagaBrowser { static create(config) { const store = createStore(config); const observableSource = new observable_1.ObservableSource(store); const syncStatusNotifier = new web_client_1.SyncStatusNotifier(); const webClient = createWebClient(config, syncStatusNotifier); const fork = createFork(config, store, webClient); const authentication = createAuthentication(config, webClient); const network = createNetwork(config, webClient, store); const purgeConditions = createPurgeConditions(config); const factManager = new factManager_1.FactManager(fork, observableSource, store, network, purgeConditions, config.feedRefreshIntervalSeconds); // Phase 3.4: Connect observer notification bridge if network supports it if (network && 'setFactsAddedListener' in network && typeof network.setFactsAddedListener === 'function') { network.setFactsAddedListener((envelopes) => __awaiter(this, void 0, void 0, function* () { // Notify FactManager about facts added via WebSocket factManager.factsAdded(envelopes); })); } return new jinaga_1.Jinaga(authentication, factManager, syncStatusNotifier); } } exports.JinagaBrowser = JinagaBrowser; function createStore(config) { if (config.indexedDb) { return new indexeddb_store_1.IndexedDBStore(config.indexedDb); } else { return new memory_store_1.MemoryStore(); } } function createWebClient(config, syncStatusNotifier) { if (config.httpEndpoint) { const provider = config.httpAuthenticationProvider; const getHeaders = provider ? () => provider.getHeaders() : () => Promise.resolve({}); const reauthenticate = provider ? () => provider.reauthenticate() : () => Promise.resolve(false); const httpConnection = new fetch_1.FetchConnection(config.httpEndpoint, getHeaders, reauthenticate); const httpTimeoutSeconds = config.httpTimeoutSeconds || 30; const webClient = new web_client_1.WebClient(httpConnection, syncStatusNotifier, { timeoutSeconds: httpTimeoutSeconds }); return webClient; } else { return null; } } function createFork(config, store, webClient) { if (webClient) { if (config.indexedDb) { const queue = new indexeddb_queue_1.IndexedDBQueue(config.indexedDb); const queueProcessingDelay = config.queueProcessingDelayMs || 100; const fork = new persistent_fork_1.PersistentFork(store, queue, webClient, queueProcessingDelay); fork.initialize(); return fork; } else { const fork = new transient_fork_1.TransientFork(store, webClient); return fork; } } else { const fork = new pass_through_fork_1.PassThroughFork(store); return fork; } } function createAuthentication(config, webClient) { if (webClient) { if (config.indexedDb) { const loginStore = new indexeddb_login_store_1.IndexedDBLoginStore(config.indexedDb); const authentication = new authentication_offline_1.AuthenticationOffline(loginStore, webClient); return authentication; } else { const authentication = new authentication_web_client_1.AuthenticationWebClient(webClient); return authentication; } } else { const authentication = new authentication_noop_1.AuthenticationNoOp(); return authentication; } } function createNetwork(config, webClient, store) { if (webClient) { // Prefer WebSocket graph when endpoint provided and environment supports WebSocket if (config.wsEndpoint && typeof globalThis.WebSocket !== 'undefined') { try { const httpNetwork = new httpNetwork_1.HttpNetwork(webClient); // Derive Authorization header via the HTTP auth provider, convert to token for WS query param const provider = config.httpAuthenticationProvider; const getAuthorizationHeader = provider ? () => __awaiter(this, void 0, void 0, function* () { const headers = yield provider.getHeaders(); return headers["Authorization"] || null; }) : undefined; const network = new wsGraphNetwork_1.WsGraphNetwork(httpNetwork, store, config.wsEndpoint, undefined, getAuthorizationHeader); return network; } catch (_a) { // Fallback to HTTP network on construction error return new httpNetwork_1.HttpNetwork(webClient); } } // Fallback to HTTP streaming return new httpNetwork_1.HttpNetwork(webClient); } else { return new NetworkManager_1.NetworkNoOp(); } } function createPurgeConditions(config) { if (config.purgeConditions) { var specifications = config.purgeConditions(new purgeConditions_1.PurgeConditions([])).specifications; var validationFailures = specifications.map(specification => (0, validate_1.validatePurgeSpecification)(specification)).flat(); if (validationFailures.length > 0) { throw new Error(validationFailures.join("\n")); } return specifications; } else { return []; } } //# sourceMappingURL=jinaga-browser.js.map