1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getSafeProperty = getSafeProperty;
|
7 | exports.setSafeProperty = setSafeProperty;
|
8 | exports.isSafeProperty = isSafeProperty;
|
9 | exports.validateSafeMethod = validateSafeMethod;
|
10 | exports.isSafeMethod = isSafeMethod;
|
11 | exports.isPlainObject = isPlainObject;
|
12 |
|
13 | var _object = require("./object.js");
|
14 |
|
15 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function getSafeProperty(object, prop) {
|
26 |
|
27 | if (isPlainObject(object) && isSafeProperty(object, prop)) {
|
28 | return object[prop];
|
29 | }
|
30 |
|
31 | if (typeof object[prop] === 'function' && isSafeMethod(object, prop)) {
|
32 | throw new Error('Cannot access method "' + prop + '" as a property');
|
33 | }
|
34 |
|
35 | throw new Error('No access to property "' + prop + '"');
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function setSafeProperty(object, prop, value) {
|
50 |
|
51 | if (isPlainObject(object) && isSafeProperty(object, prop)) {
|
52 | object[prop] = value;
|
53 | return value;
|
54 | }
|
55 |
|
56 | throw new Error('No access to property "' + prop + '"');
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function isSafeProperty(object, prop) {
|
67 | if (!object || _typeof(object) !== 'object') {
|
68 | return false;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | if ((0, _object.hasOwnProperty)(safeNativeProperties, prop)) {
|
74 | return true;
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | if (prop in Object.prototype) {
|
80 |
|
81 |
|
82 |
|
83 | return false;
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 | if (prop in Function.prototype) {
|
89 |
|
90 |
|
91 |
|
92 | return false;
|
93 | }
|
94 |
|
95 | return true;
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function validateSafeMethod(object, method) {
|
107 | if (!isSafeMethod(object, method)) {
|
108 | throw new Error('No access to method "' + method + '"');
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | function isSafeMethod(object, method) {
|
121 | if (object === null || object === undefined || typeof object[method] !== 'function') {
|
122 | return false;
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | if ((0, _object.hasOwnProperty)(object, method) && Object.getPrototypeOf && method in Object.getPrototypeOf(object)) {
|
129 | return false;
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 | if ((0, _object.hasOwnProperty)(safeNativeMethods, method)) {
|
135 | return true;
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 | if (method in Object.prototype) {
|
141 |
|
142 |
|
143 |
|
144 | return false;
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 | if (method in Function.prototype) {
|
150 |
|
151 |
|
152 |
|
153 | return false;
|
154 | }
|
155 |
|
156 | return true;
|
157 | }
|
158 |
|
159 | function isPlainObject(object) {
|
160 | return _typeof(object) === 'object' && object && object.constructor === Object;
|
161 | }
|
162 |
|
163 | var safeNativeProperties = {
|
164 | length: true,
|
165 | name: true
|
166 | };
|
167 | var safeNativeMethods = {
|
168 | toString: true,
|
169 | valueOf: true,
|
170 | toLocaleString: true
|
171 | }; |
\ | No newline at end of file |