forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropwrap.js
147 lines (139 loc) · 5.16 KB
/
propwrap.js
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
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
// Utilities to wrap properties of an object.
//
// This is similar to "./instrumentation/shimmer.js". However, it uses a
// different technique to support wrapping properties that are only available
// via a getter (i.e. their property descriptor is `.writable === false`).
/*
* This block is derived from esbuild's bundling support.
* https://github.com/evanw/esbuild/blob/v0.14.42/internal/runtime/runtime.go#L22
*
* License:
* MIT License
*
* Copyright (c) 2020 Evan Wallace
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __copyProps = (to, from, except, desc) => {
if ((from && typeof from === 'object') || typeof from === 'function') {
for (const key of __getOwnPropNames(from)) {
if (!__hasOwnProp.call(to, key) && key !== except) {
__defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
});
}
}
}
return to;
};
/**
* Return a new object that is a copy of `obj`, with its `subpath` property
* replaced with the return value of `wrapper(original)`.
*
* For example:
* var os = wrap(require('os'), 'platform', (orig) => {
* return function wrappedPlatform () {
* return orig().toUpperCase()
* }
* })
* console.log(os.platform()) // => DARWIN
*
* The subpath can indicate a nested property. Each property in that subpath,
* except the last, must identify an *Object*.
*
* Limitations:
* - This doesn't handle possible Symbol properties on the copied object(s).
* - This cannot wrap a property of a function, because we cannot create a
* copy of the function.
*
* @param {Object} obj
* @param {String} subpath - The property subpath on `obj` to wrap. This may
* point to a nested property by using a '.' to separate levels. For example:
* var fs = wrap(fs, 'promises.sync', (orig) => { ... })
* @param {Function} wrapper - A function of the form `function (orig)`, where
* `orig` is the original property value. This must synchronously return the
* new property value.
* @returns {Object} A new object with the wrapped property.
* @throws {TypeError} if the subpath points to a non-existant property, or if
* any but the last subpath part points to a non-Object.
*/
function wrap(obj, subpath, wrapper) {
const parts = subpath.split('.');
const namespaces = [obj];
let namespace = obj;
let key;
let val;
// 1. Traverse the subpath parts to sanity check and get references to the
// Objects that will will be copying.
for (let i = 0; i < parts.length; i++) {
key = parts[i];
val = namespace[key];
if (!val) {
throw new TypeError(
`cannot wrap "${subpath}": "<obj>.${parts
.slice(0, i)
.join('.')}" is ${typeof val}`,
);
} else if (i < parts.length - 1) {
if (typeof val !== 'object') {
throw new TypeError(
`cannot wrap "${subpath}": "<obj>.${parts
.slice(0, i)
.join('.')}" is not an Object`,
);
}
namespace = val;
namespaces.push(namespace);
}
}
// 2. Now work backwards, wrapping each namespace with a new object that has a
// copy of all the properties, except the one that we've wrapped.
for (let i = parts.length - 1; i >= 0; i--) {
key = parts[i];
namespace = namespaces[i];
if (i === parts.length - 1) {
const orig = namespace[key];
val = wrapper(orig);
} else {
val = namespaces[i + 1];
}
const desc = __getOwnPropDesc(namespace, key);
const wrappedNamespace = __defProp({}, key, {
value: val,
enumerable: !desc || desc.enumerable,
});
__copyProps(wrappedNamespace, namespace, key);
namespaces[i] = wrappedNamespace;
}
return namespaces[0];
}
module.exports = {
wrap,
};