-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathindex.node.ts
More file actions
166 lines (148 loc) · 5.58 KB
/
index.node.ts
File metadata and controls
166 lines (148 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright 2016-2017, 2019-2024 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getLogger, setErrorHandler, getErrorHandler, LogLevel, setLogHandler, setLogLevel } from './modules/logging';
import Optimizely from './optimizely';
import * as enums from './utils/enums';
import * as loggerPlugin from './plugins/logger';
import configValidator from './utils/config_validator';
import defaultErrorHandler from './plugins/error_handler';
import defaultEventDispatcher from './event_processor/event_dispatcher/default_dispatcher.node';
import { createNotificationCenter } from './notification_center';
import { OptimizelyDecideOption, Client, Config } from './shared_types';
import { NodeOdpManager } from './odp/odp_manager.node';
import * as commonExports from './common_exports';
import { createPollingProjectConfigManager } from './project_config/config_manager_factory.node';
import { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor/event_processor_factory.node';
const logger = getLogger();
setLogLevel(LogLevel.ERROR);
const DEFAULT_EVENT_BATCH_SIZE = 10;
const DEFAULT_EVENT_FLUSH_INTERVAL = 30000; // Unit is ms, default is 30s
const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000;
/**
* Creates an instance of the Optimizely class
* @param {Config} config
* @return {Client|null} the Optimizely client object
* null on error
*/
const createInstance = function(config: Config): Client | null {
try {
let hasLogger = false;
let isValidInstance = false;
// TODO warn about setting per instance errorHandler / logger / logLevel
if (config.errorHandler) {
setErrorHandler(config.errorHandler);
}
if (config.logger) {
// only set a logger in node if one is provided, by not setting we are noop-ing
hasLogger = true;
setLogHandler(config.logger);
// respect the logger's shouldLog functionality
setLogLevel(LogLevel.NOTSET);
}
if (config.logLevel !== undefined) {
setLogLevel(config.logLevel);
}
try {
configValidator.validate(config);
isValidInstance = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (ex) {
if (hasLogger) {
logger.error(ex);
} else {
console.error(ex.message);
}
}
// let eventBatchSize = config.eventBatchSize;
// let eventFlushInterval = config.eventFlushInterval;
// if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
// logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
// eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
// }
// if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
// logger.warn(
// 'Invalid eventFlushInterval %s, defaulting to %s',
// config.eventFlushInterval,
// DEFAULT_EVENT_FLUSH_INTERVAL
// );
// eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
// }
const errorHandler = getErrorHandler();
const notificationCenter = createNotificationCenter({ logger: logger, errorHandler: errorHandler });
// const eventProcessorConfig = {
// dispatcher: config.eventDispatcher || defaultEventDispatcher,
// flushInterval: eventFlushInterval,
// batchSize: eventBatchSize,
// maxQueueSize: config.eventMaxQueueSize || DEFAULT_EVENT_MAX_QUEUE_SIZE,
// notificationCenter,
// };
// const eventProcessor = createEventProcessor(eventProcessorConfig);
// const eventProcessor = config.eventProcessor;
const odpExplicitlyOff = config.odpOptions?.disabled === true;
if (odpExplicitlyOff) {
logger.info(enums.LOG_MESSAGES.ODP_DISABLED);
}
const { clientEngine, clientVersion } = config;
const optimizelyOptions = {
clientEngine: enums.NODE_CLIENT_ENGINE,
...config,
// eventProcessor,
logger,
errorHandler,
notificationCenter,
isValidInstance,
odpManager: odpExplicitlyOff ? undefined
: NodeOdpManager.createInstance({ logger, odpOptions: config.odpOptions, clientEngine, clientVersion }),
};
return new Optimizely(optimizelyOptions);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e) {
logger.error(e);
return null;
}
};
/**
* Entry point into the Optimizely Node testing SDK
*/
export {
loggerPlugin as logging,
defaultErrorHandler as errorHandler,
defaultEventDispatcher as eventDispatcher,
enums,
setLogHandler as setLogger,
setLogLevel,
createInstance,
OptimizelyDecideOption,
createPollingProjectConfigManager,
createForwardingEventProcessor,
createBatchEventProcessor,
};
export * from './common_exports';
export default {
...commonExports,
logging: loggerPlugin,
errorHandler: defaultErrorHandler,
eventDispatcher: defaultEventDispatcher,
enums,
setLogger: setLogHandler,
setLogLevel,
createInstance,
OptimizelyDecideOption,
createPollingProjectConfigManager,
createForwardingEventProcessor,
createBatchEventProcessor,
};
export * from './export_types';