aveazul 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,16 +4,14 @@ const { promisify } = require("./promisify");
4
4
  const {
5
5
  isIdentifier,
6
6
  isClass,
7
- isConstructor,
8
7
  isPromisified,
9
- getObjectKeys,
10
- isExcludedPrototype,
8
+ getObjectDataKeys,
11
9
  } = require("./util");
12
10
 
13
11
  const defaultSuffix = "Async";
14
12
 
15
13
  const defaultFilter = function (name) {
16
- return isIdentifier(name) && name.charAt(0) !== "_" && name !== "constructor";
14
+ return isIdentifier(name) && name.charAt(0) !== "_" && name !== "constructor" && !name.endsWith("Sync");
17
15
  };
18
16
 
19
17
  const defaultPromisifier = (fn, _defaultPromisifier, options) => {
@@ -23,33 +21,9 @@ const defaultPromisifier = (fn, _defaultPromisifier, options) => {
23
21
  });
24
22
  };
25
23
 
26
- const excludedClasses = [Array, Object, Function];
27
-
28
- // Helper function to determine if a class extends from any excluded class
29
- function isExcludedClass(obj) {
30
- if (excludedClasses.includes(obj)) {
31
- return true;
32
- }
33
-
34
- // Check if obj extends from any excluded class using instanceof
35
- if (typeof obj === "function" && obj.prototype) {
36
- // Check if prototype is instance of any excluded class
37
- for (const excludedClass of excludedClasses) {
38
- if (obj.prototype instanceof excludedClass) {
39
- return true;
40
- }
41
- }
42
- }
43
-
44
- return false;
45
- }
46
24
 
47
25
  function promisifyAll2(obj, options) {
48
- if (isExcludedClass(obj)) {
49
- return;
50
- }
51
-
52
- const allKeys = getObjectKeys(obj);
26
+ const allKeys = getObjectDataKeys(obj);
53
27
 
54
28
  for (const key of allKeys) {
55
29
  const value = obj[key];
@@ -57,7 +31,6 @@ function promisifyAll2(obj, options) {
57
31
  const passesDefaultFilter =
58
32
  options.filter === defaultFilter ? true : defaultFilter(key, value, obj);
59
33
  if (
60
- isConstructor(value) ||
61
34
  typeof value !== "function" ||
62
35
  isPromisified(value) ||
63
36
  obj[promisifiedKey] ||
@@ -78,17 +51,14 @@ function promisifyAll2(obj, options) {
78
51
  obj[promisifiedKey] = options.promisifier(value, defaultPromisifier, {
79
52
  // context: obj, // promisified function should get the binded object using this
80
53
  copyProps: false,
81
- multiArgs: options.multiArgs,
82
- Promise: options.Promise,
54
+ ...options
83
55
  });
84
56
  }
85
57
  }
86
58
 
87
59
  function promisifyAll(target, _options) {
88
60
  if (typeof target !== "function" && typeof target !== "object") {
89
- throw new TypeError(
90
- "the target of promisifyAll must be an object or a function"
91
- );
61
+ throw new TypeError("the target of promisifyAll must be an object or a function");
92
62
  }
93
63
 
94
64
  const options = {
@@ -107,21 +77,12 @@ function promisifyAll(target, _options) {
107
77
  );
108
78
  }
109
79
 
110
- const allKeys = getObjectKeys(target);
80
+ const allKeys = getObjectDataKeys(target);
111
81
 
112
82
  for (const key of allKeys) {
113
83
  const value = target[key];
114
- if (
115
- value &&
116
- key !== "constructor" &&
117
- !key.startsWith("_") &&
118
- isClass(value)
119
- ) {
120
- const proto = Object.getPrototypeOf(value);
121
- if (!isExcludedPrototype(proto)) {
122
- promisifyAll2(proto, options);
123
- }
124
-
84
+ if (value && key !== "constructor" && !key.startsWith("_") && isClass(value)) {
85
+ promisifyAll2(value.prototype, options);
125
86
  promisifyAll2(value, options);
126
87
  }
127
88
  }
package/lib/util.js CHANGED
@@ -11,32 +11,25 @@
11
11
  */
12
12
  const thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
13
13
  function isClass(fn) {
14
- try {
15
- if (typeof fn === "function") {
16
- const keys = Object.getOwnPropertyNames(fn.prototype);
17
-
18
- const fnStr = fn.toString();
19
- const es6Class = fnStr.startsWith("class ") || /^class\s+/.test(fnStr);
20
- const hasMethods = keys.length > 1;
21
- const hasMethodsOtherThanConstructor =
22
- keys.length > 0 && !(keys.length === 1 && keys[0] === "constructor");
23
- const hasThisAssignmentAndStaticMethods =
24
- thisAssignmentPattern.test(fnStr) &&
25
- Object.getOwnPropertyNames(fn).length > 0;
26
-
27
- if (
28
- es6Class ||
29
- hasMethods ||
30
- hasMethodsOtherThanConstructor ||
31
- hasThisAssignmentAndStaticMethods
32
- ) {
33
- return true;
34
- }
14
+ try {
15
+ if (typeof fn === "function") {
16
+ const keys = Object.getOwnPropertyNames(fn.prototype);
17
+
18
+ const hasMethods = keys.length > 1;
19
+ const hasMethodsOtherThanConstructor = keys.length > 0 &&
20
+ !(keys.length === 1 && keys[0] === "constructor");
21
+ const hasThisAssignmentAndStaticMethods =
22
+ thisAssignmentPattern.test(fn + "") && Object.getOwnPropertyNames (fn).length > 0;
23
+
24
+ if (hasMethods || hasMethodsOtherThanConstructor ||
25
+ hasThisAssignmentAndStaticMethods) {
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ } catch (e) {
31
+ return false;
35
32
  }
36
- return false;
37
- } catch (e) {
38
- return false;
39
- }
40
33
  }
41
34
 
42
35
  const rident = /^[a-z$_][a-z$_0-9]*$/i;
@@ -44,18 +37,6 @@ function isIdentifier(str) {
44
37
  return rident.test(str);
45
38
  }
46
39
 
47
- function isConstructor(func) {
48
- if (!func) {
49
- return false;
50
- }
51
- const proto = func.prototype;
52
- return (
53
- !!proto &&
54
- !!proto.constructor &&
55
- !!proto.constructor.name &&
56
- proto.constructor.name === func.name
57
- );
58
- }
59
40
 
60
41
  /**
61
42
  * Prop filtering code copied from bluebird/js/release
@@ -81,11 +62,7 @@ function copyOwnProperties(source, target, filter = propsFilter) {
81
62
 
82
63
  for (const name of names) {
83
64
  if (filter(name)) {
84
- Object.defineProperty(
85
- target,
86
- name,
87
- Object.getOwnPropertyDescriptor(source, name)
88
- );
65
+ Object.defineProperty(target, name, Object.getOwnPropertyDescriptor(source, name));
89
66
  }
90
67
  }
91
68
  }
@@ -118,52 +95,66 @@ function isPromise(obj) {
118
95
  );
119
96
  }
120
97
 
121
- // istanbul ignore next
122
- const emptyFatArrow = () => {};
123
- // istanbul ignore next
124
- const emptyFunction = function () {};
125
-
126
- const defaultExcluded = [
127
- Object.getPrototypeOf(Array), // Array.prototype
128
- Object.getPrototypeOf(Object), // Object.prototype
129
- Object.getPrototypeOf(Function), // Function.prototype
130
- Object.getPrototypeOf([]),
131
- Object.getPrototypeOf({}),
132
- Object.getPrototypeOf(emptyFatArrow),
133
- Object.getPrototypeOf(emptyFunction),
134
- ];
135
-
136
- function isExcludedPrototype(proto) {
137
- return defaultExcluded.includes(proto);
138
- }
139
-
140
98
  /**
141
99
  * Gets all property keys from an object and its prototype chain, excluding standard
142
100
  * prototypes like Object.prototype, Array.prototype, and Function.prototype
143
101
  *
144
- * @param {Object} target - The target object to get keys from
102
+ * @param {Object} obj - The target object to get keys from
145
103
  * @param {Array} [excludedPrototypes=[]] - An array of prototype objects to exclude keys from
146
104
  * @returns {Array<string>} - Array of property keys
147
105
  */
148
- function getObjectKeys(target, excludedPrototypes = []) {
149
- const excluded =
150
- excludedPrototypes.length > 0 ? excludedPrototypes : defaultExcluded;
151
-
152
- // Get own properties
153
- const ownKeys = Object.getOwnPropertyNames(target);
154
-
155
- // Get prototype properties, excluding those from excluded prototypes
156
- let protoKeys = [];
157
- let currentProto = Object.getPrototypeOf(target);
106
+ function getObjectDataKeys(obj, excludedProtos = []) {
107
+ const excludedPrototypes = [
108
+ Array.prototype,
109
+ Object.prototype,
110
+ Function.prototype,
111
+ ...excludedProtos,
112
+ ];
113
+
114
+ const isExcludedProto = function (val) {
115
+ for (const protoVal of excludedPrototypes) {
116
+ if (protoVal === val) {
117
+ return true;
118
+ }
119
+ }
120
+ return false;
121
+ };
122
+
123
+ const ret = [];
124
+ const visitedKeys = Object.create(null);
125
+
126
+ /* copied from bluebird/js/release/util.js and modified */
127
+ while (obj && !isExcludedProto(obj)) {
128
+ let keys;
129
+ try {
130
+ keys = Object.getOwnPropertyNames(obj);
131
+ } catch (e) {
132
+ /* istanbul ignore next */
133
+ return ret;
134
+ }
158
135
 
159
- // Walk up the prototype chain until we hit null or an excluded prototype
160
- while (currentProto && !excluded.includes(currentProto)) {
161
- protoKeys = [...protoKeys, ...Object.getOwnPropertyNames(currentProto)];
162
- currentProto = Object.getPrototypeOf(currentProto);
136
+ for (const key of keys) {
137
+ /* istanbul ignore if */
138
+ if (visitedKeys[key]) {
139
+ /* istanbul ignore next */
140
+ continue;
141
+ }
142
+ visitedKeys[key] = true;
143
+ const desc = Object.getOwnPropertyDescriptor(obj, key);
144
+ /**
145
+ * When desc.get && desc.set are falsy, it means the property is a data
146
+ * property that holds an actual value, rather than being computed dynamically
147
+ * through getter/setter functions.
148
+ */
149
+ /* istanbul ignore next */
150
+ if (desc && !desc.get && !desc.set) {
151
+ ret.push(key);
152
+ }
153
+ }
154
+ obj = Object.getPrototypeOf(obj);
163
155
  }
164
156
 
165
- // Combine own properties and prototype properties
166
- return [...protoKeys, ...ownKeys];
157
+ return ret;
167
158
  }
168
159
 
169
160
  /**
@@ -191,9 +182,7 @@ function toArray(args) {
191
182
  // to detect if too many errors occurred and completion is impossible.
192
183
  args = Array.from(args);
193
184
  } else {
194
- throw new TypeError(
195
- "expecting an array or an iterable object but got " + args
196
- );
185
+ throw new TypeError("expecting an array or an iterable object but got " + args);
197
186
  }
198
187
  }
199
188
 
@@ -203,10 +192,8 @@ function toArray(args) {
203
192
  module.exports.copyOwnProperties = copyOwnProperties;
204
193
  module.exports.isClass = isClass;
205
194
  module.exports.isIdentifier = isIdentifier;
206
- module.exports.isConstructor = isConstructor;
207
195
  module.exports.isPromisified = isPromisified;
208
196
  module.exports.isPromise = isPromise;
209
197
  module.exports.triggerUncaughtException = triggerUncaughtException;
210
- module.exports.getObjectKeys = getObjectKeys;
211
- module.exports.isExcludedPrototype = isExcludedPrototype;
198
+ module.exports.getObjectDataKeys = getObjectDataKeys;
212
199
  module.exports.toArray = toArray;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aveazul",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Bluebird drop-in replacement built on native Promise",
5
5
  "main": "lib/aveazul.js",
6
6
  "homepage": "https://github.com/jchip/aveazul",