|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * BSD 2-Clause License |
| 19 | + * |
| 20 | + * Copyright (c) 2013-2019, Forrest L Norvell |
| 21 | + * All rights reserved. |
| 22 | + * |
| 23 | + * Redistribution and use in source and binary forms, with or without |
| 24 | + * modification, are permitted provided that the following conditions are met: |
| 25 | + * |
| 26 | + * * Redistributions of source code must retain the above copyright notice, this |
| 27 | + * list of conditions and the following disclaimer. |
| 28 | + * |
| 29 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 30 | + * this list of conditions and the following disclaimer in the documentation |
| 31 | + * and/or other materials provided with the distribution. |
| 32 | + * |
| 33 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 34 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 35 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 36 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 37 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 38 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 39 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 40 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 41 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 42 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 | + */ |
| 44 | + |
| 45 | +/* Modified by OpenTelemetry Authors |
| 46 | + * - converted to TypeScript |
| 47 | + * - aligned with style-guide |
| 48 | + */ |
| 49 | + |
| 50 | +import { ShimWrapped } from './types'; |
| 51 | + |
| 52 | +// Default to complaining loudly when things don't go according to plan. |
| 53 | +// eslint-disable-next-line no-console |
| 54 | +let logger: typeof console.error = console.error.bind(console); |
| 55 | + |
| 56 | +// Sets a property on an object, preserving its enumerability. |
| 57 | +// This function assumes that the property is already writable. |
| 58 | +function defineProperty(obj: object, name: PropertyKey, value: unknown): void { |
| 59 | + const enumerable = |
| 60 | + !!obj[name as keyof typeof obj] && |
| 61 | + Object.prototype.propertyIsEnumerable.call(obj, name); |
| 62 | + |
| 63 | + Object.defineProperty(obj, name, { |
| 64 | + configurable: true, |
| 65 | + enumerable, |
| 66 | + writable: true, |
| 67 | + value, |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +export const wrap = <Nodule extends object, FieldName extends keyof Nodule>( |
| 72 | + nodule: Nodule, |
| 73 | + name: FieldName, |
| 74 | + wrapper: (original: Nodule[FieldName], name: FieldName) => Nodule[FieldName] |
| 75 | +): ShimWrapped | undefined => { |
| 76 | + if (!nodule || !nodule[name]) { |
| 77 | + logger('no original function ' + String(name) + ' to wrap'); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (!wrapper) { |
| 82 | + logger('no wrapper function'); |
| 83 | + logger(new Error().stack); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const original = nodule[name]; |
| 88 | + |
| 89 | + if (typeof original !== 'function' || typeof wrapper !== 'function') { |
| 90 | + logger('original object and wrapper must be functions'); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const wrapped = wrapper(original, name) as object; |
| 95 | + |
| 96 | + defineProperty(wrapped, '__original', original); |
| 97 | + defineProperty(wrapped, '__unwrap', () => { |
| 98 | + if (nodule[name] === wrapped) { |
| 99 | + defineProperty(nodule, name, original); |
| 100 | + } |
| 101 | + }); |
| 102 | + defineProperty(wrapped, '__wrapped', true); |
| 103 | + defineProperty(nodule, name, wrapped); |
| 104 | + return wrapped as ShimWrapped; |
| 105 | +}; |
| 106 | + |
| 107 | +export const massWrap = <Nodule extends object, FieldName extends keyof Nodule>( |
| 108 | + nodules: Nodule[], |
| 109 | + names: FieldName[], |
| 110 | + wrapper: (original: Nodule[FieldName]) => Nodule[FieldName] |
| 111 | +): void => { |
| 112 | + if (!nodules) { |
| 113 | + logger('must provide one or more modules to patch'); |
| 114 | + logger(new Error().stack); |
| 115 | + return; |
| 116 | + } else if (!Array.isArray(nodules)) { |
| 117 | + nodules = [nodules]; |
| 118 | + } |
| 119 | + |
| 120 | + if (!(names && Array.isArray(names))) { |
| 121 | + logger('must provide one or more functions to wrap on modules'); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + nodules.forEach(nodule => { |
| 126 | + names.forEach(name => { |
| 127 | + wrap(nodule, name, wrapper); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}; |
| 131 | + |
| 132 | +export const unwrap = <Nodule extends object>( |
| 133 | + nodule: Nodule, |
| 134 | + name: keyof Nodule |
| 135 | +): void => { |
| 136 | + if (!nodule || !nodule[name]) { |
| 137 | + logger('no function to unwrap.'); |
| 138 | + logger(new Error().stack); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + const wrapped = nodule[name] as unknown as ShimWrapped; |
| 143 | + |
| 144 | + if (!wrapped.__unwrap) { |
| 145 | + logger( |
| 146 | + 'no original to unwrap to -- has ' + |
| 147 | + String(name) + |
| 148 | + ' already been unwrapped?' |
| 149 | + ); |
| 150 | + } else { |
| 151 | + wrapped.__unwrap(); |
| 152 | + return; |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +export const massUnwrap = <Nodule extends object>( |
| 157 | + nodules: Nodule[], |
| 158 | + names: Array<keyof Nodule> |
| 159 | +): void => { |
| 160 | + if (!nodules) { |
| 161 | + logger('must provide one or more modules to patch'); |
| 162 | + logger(new Error().stack); |
| 163 | + return; |
| 164 | + } else if (!Array.isArray(nodules)) { |
| 165 | + nodules = [nodules]; |
| 166 | + } |
| 167 | + |
| 168 | + if (!(names && Array.isArray(names))) { |
| 169 | + logger('must provide one or more functions to unwrap on modules'); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + nodules.forEach(nodule => { |
| 174 | + names.forEach(name => { |
| 175 | + unwrap(nodule, name); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}; |
| 179 | + |
| 180 | +export interface ShimmerOptions { |
| 181 | + logger?: typeof console.error; |
| 182 | +} |
| 183 | + |
| 184 | +export default function shimmer(options: ShimmerOptions): void { |
| 185 | + if (options && options.logger) { |
| 186 | + if (typeof options.logger !== 'function') { |
| 187 | + logger("new logger isn't a function, not replacing"); |
| 188 | + } else { |
| 189 | + logger = options.logger; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +shimmer.wrap = wrap; |
| 195 | +shimmer.massWrap = massWrap; |
| 196 | +shimmer.unwrap = unwrap; |
| 197 | +shimmer.massUnwrap = massUnwrap; |
0 commit comments