|
| 1 | +import { Collector } from './api/Collector'; |
| 2 | +import { HttpBreadCrumbOptions, Options, StackOptions, UrlFilter } from './api/Options'; |
| 3 | + |
| 4 | +export function defaultOptions(): ParsedOptions { |
| 5 | + return { |
| 6 | + breadcrumbs: { |
| 7 | + maxBreadcrumbs: 50, |
| 8 | + evaluations: true, |
| 9 | + flagChange: true, |
| 10 | + click: true, |
| 11 | + keyboardInput: true, |
| 12 | + http: { |
| 13 | + instrumentFetch: true, |
| 14 | + instrumentXhr: true, |
| 15 | + }, |
| 16 | + }, |
| 17 | + stack: { |
| 18 | + source: { |
| 19 | + beforeLines: 3, |
| 20 | + afterLines: 3, |
| 21 | + maxLineLength: 280, |
| 22 | + }, |
| 23 | + }, |
| 24 | + maxPendingEvents: 100, |
| 25 | + collectors: [], |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +function itemOrDefault<T>(item: T | undefined, defaultValue: T): T { |
| 30 | + if (item !== undefined && item !== null) { |
| 31 | + return item; |
| 32 | + } |
| 33 | + return defaultValue; |
| 34 | +} |
| 35 | + |
| 36 | +function parseHttp( |
| 37 | + options: HttpBreadCrumbOptions | false | undefined, |
| 38 | + defaults: ParsedHttpOptions, |
| 39 | +): ParsedHttpOptions { |
| 40 | + if (options === false) { |
| 41 | + return { |
| 42 | + instrumentFetch: false, |
| 43 | + instrumentXhr: false, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + // Make sure that the custom filter is at least a function. |
| 48 | + const customUrlFilter = |
| 49 | + options?.customUrlFilter && typeof options?.customUrlFilter === 'function' |
| 50 | + ? options.customUrlFilter |
| 51 | + : undefined; |
| 52 | + |
| 53 | + // TODO: Logging for incorrect types. |
| 54 | + |
| 55 | + return { |
| 56 | + instrumentFetch: itemOrDefault(options?.instrumentFetch, defaults.instrumentFetch), |
| 57 | + instrumentXhr: itemOrDefault(options?.instrumentFetch, defaults.instrumentXhr), |
| 58 | + customUrlFilter, |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +function parseStack( |
| 63 | + options: StackOptions | undefined, |
| 64 | + defaults: ParsedStackOptions, |
| 65 | +): ParsedStackOptions { |
| 66 | + return { |
| 67 | + source: { |
| 68 | + beforeLines: itemOrDefault(options?.source?.beforeLines, defaults.source.beforeLines), |
| 69 | + afterLines: itemOrDefault(options?.source?.afterLines, defaults.source.afterLines), |
| 70 | + maxLineLength: itemOrDefault(options?.source?.maxLineLength, defaults.source.maxLineLength), |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +export default function parse(options: Options): ParsedOptions { |
| 76 | + const defaults = defaultOptions(); |
| 77 | + return { |
| 78 | + breadcrumbs: { |
| 79 | + maxBreadcrumbs: itemOrDefault( |
| 80 | + options.breadcrumbs?.maxBreadcrumbs, |
| 81 | + defaults.breadcrumbs.maxBreadcrumbs, |
| 82 | + ), |
| 83 | + evaluations: itemOrDefault( |
| 84 | + options.breadcrumbs?.evaluations, |
| 85 | + defaults.breadcrumbs.evaluations, |
| 86 | + ), |
| 87 | + flagChange: itemOrDefault(options.breadcrumbs?.flagChange, defaults.breadcrumbs.flagChange), |
| 88 | + click: itemOrDefault(options.breadcrumbs?.click, defaults.breadcrumbs.click), |
| 89 | + keyboardInput: itemOrDefault( |
| 90 | + options.breadcrumbs?.keyboardInput, |
| 91 | + defaults.breadcrumbs.keyboardInput, |
| 92 | + ), |
| 93 | + http: parseHttp(options.breadcrumbs?.http, defaults.breadcrumbs.http), |
| 94 | + }, |
| 95 | + stack: parseStack(options.stack, defaults.stack), |
| 96 | + maxPendingEvents: itemOrDefault(options.maxPendingEvents, defaults.maxPendingEvents), |
| 97 | + collectors: [...itemOrDefault(options.collectors, defaults.collectors)], |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +export interface ParsedHttpOptions { |
| 102 | + /** |
| 103 | + * True to instrument fetch and enable fetch breadcrumbs. |
| 104 | + */ |
| 105 | + instrumentFetch: boolean; |
| 106 | + |
| 107 | + /** |
| 108 | + * True to instrument XMLHttpRequests and enable XMLHttpRequests breadcrumbs. |
| 109 | + */ |
| 110 | + instrumentXhr: boolean; |
| 111 | + |
| 112 | + /** |
| 113 | + * Optional custom URL filter. |
| 114 | + */ |
| 115 | + customUrlFilter?: UrlFilter; |
| 116 | +} |
| 117 | + |
| 118 | +export interface ParsedStackOptions { |
| 119 | + source: { |
| 120 | + /** |
| 121 | + * The number of lines captured before the originating line. |
| 122 | + */ |
| 123 | + beforeLines: number; |
| 124 | + |
| 125 | + /** |
| 126 | + * The number of lines captured after the originating line. |
| 127 | + */ |
| 128 | + afterLines: number; |
| 129 | + |
| 130 | + /** |
| 131 | + * The maximum length of source line to include. Lines longer than this will be |
| 132 | + * trimmed. |
| 133 | + */ |
| 134 | + maxLineLength: number; |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +export interface ParsedOptions { |
| 139 | + /** |
| 140 | + * The maximum number of pending events. Events may be captured before the LaunchDarkly |
| 141 | + * SDK is initialized and these are stored until they can be sent. This only affects the |
| 142 | + * events captured during initialization. |
| 143 | + */ |
| 144 | + maxPendingEvents: number; |
| 145 | + /** |
| 146 | + * Properties related to automatic breadcrumb collection. |
| 147 | + */ |
| 148 | + breadcrumbs: { |
| 149 | + /** |
| 150 | + * Set the maximum number of breadcrumbs. Defaults to 50. |
| 151 | + */ |
| 152 | + maxBreadcrumbs: number; |
| 153 | + |
| 154 | + /** |
| 155 | + * True to enable automatic evaluation breadcrumbs. Defaults to true. |
| 156 | + */ |
| 157 | + evaluations: boolean; |
| 158 | + |
| 159 | + /** |
| 160 | + * True to enable flag change breadcrumbs. Defaults to true. |
| 161 | + */ |
| 162 | + flagChange: boolean; |
| 163 | + |
| 164 | + /** |
| 165 | + * True to enable click breadcrumbs. Defaults to true. |
| 166 | + */ |
| 167 | + click: boolean; |
| 168 | + |
| 169 | + /** |
| 170 | + * True to enable input breadcrumbs for keypresses. Defaults to true. |
| 171 | + */ |
| 172 | + keyboardInput?: boolean; |
| 173 | + |
| 174 | + /** |
| 175 | + * Settings for http instrumentation and breadcrumbs. |
| 176 | + */ |
| 177 | + http: ParsedHttpOptions; |
| 178 | + }; |
| 179 | + |
| 180 | + /** |
| 181 | + * Settings which affect call stack capture. |
| 182 | + */ |
| 183 | + stack: ParsedStackOptions; |
| 184 | + |
| 185 | + /** |
| 186 | + * Additional, or custom, collectors. |
| 187 | + */ |
| 188 | + collectors: Collector[]; |
| 189 | +} |
0 commit comments