UNPKG

4.76 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const Utils_1 = require("../system/Utils");
16const EventBus_1 = __importDefault(require("./EventBus"));
17const Instrumentor_1 = require("./Instrumentor");
18const Nexus_1 = __importDefault(require("./Nexus"));
19const Types_1 = require("./Types");
20let initialized = false;
21let protectedMode = false;
22const seal = Symbol();
23function lock(obj, key) {
24 const guard = Object.freeze({ key });
25 function unlock(suppliedKey) {
26 const keyRef = suppliedKey;
27 if (typeof keyRef === 'symbol' && guard.key === keyRef) {
28 return obj;
29 }
30 throw new Error('illegal access to a protected object');
31 }
32 Object.defineProperty(unlock, 'name', { value: Symbol() });
33 Object.defineProperty(unlock, 'prototype', { value: null });
34 Object.defineProperty(unlock, '__proto__', { value: null });
35 return Object.freeze(unlock);
36}
37function sealModule(module) {
38 return lock(module, seal);
39}
40function init(settings) {
41 return __awaiter(this, void 0, void 0, function* () {
42 if (initialized && protectedMode) {
43 return;
44 }
45 const { logger, mask } = settings.logging;
46 const debugLoggingEnabled = !mask.includes('debug');
47 function log(level, message) {
48 switch (level) {
49 case 'debug': {
50 if (debugLoggingEnabled) {
51 logger[level](message);
52 }
53 break;
54 }
55 default: {
56 logger[level](message);
57 break;
58 }
59 }
60 }
61 log('debug', 'beginning to initialize the internal bf-lib system');
62 Instrumentor_1.SetInstrumentorLogger(log);
63 const httpHeaders = {};
64 const impersonateHeaders = settings.impersonate || {};
65 const libModuleMap = new Map();
66 const nexus = yield Nexus_1.default(system, settings.nexus, settings.auth);
67 const eventBus = EventBus_1.default();
68 protectedMode = settings.protected || false;
69 function getEventBus() {
70 return eventBus;
71 }
72 function getHttpHeaders() {
73 return Object.assign({}, httpHeaders);
74 }
75 function getImpersonationHeaders() {
76 return Object.assign({}, impersonateHeaders);
77 }
78 function isProtected() {
79 return protectedMode;
80 }
81 function setHttpHeader(key, value) {
82 httpHeaders[key] = value;
83 }
84 function getLibModule(type) {
85 const libModule = libModuleMap.get(type);
86 if (!libModule) {
87 throw new Error('An attempt was mad to access an instance for a missing Lib Module.');
88 }
89 return libModule(seal);
90 }
91 log('debug', 'beginning to initialize all system lib modules');
92 libModuleMap.set(Types_1.LibModule.AUTH, require('../auth/Auth').default);
93 libModuleMap.set(Types_1.LibModule.API, require('../api/Api').default);
94 libModuleMap.set(Types_1.LibModule.MODULE, require('../module/Module').default);
95 libModuleMap.set(Types_1.LibModule.MULTITOOL, require('../multitool/Multitool').default);
96 log('debug', 'finished initializing all system lib modules');
97 const instanceMethods = {
98 isProtected,
99 getEventBus,
100 getHttpHeaders,
101 getImpersonationHeaders,
102 setHttpHeader,
103 getLibModule,
104 nexus,
105 };
106 Object.assign(instance, instanceMethods);
107 if (settings.auth.afterConnect) {
108 settings.auth.afterConnect(instance);
109 }
110 initialized = true;
111 log('debug', 'finished initializing the internal bf-lib system');
112 });
113}
114const [instance, system] = Utils_1.proxyWrap({}, { init, sealModule });
115exports.default = system;