@vsaas/loopback-datasource-juggler 13.1.1 → 13.1.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.
@@ -149,18 +149,30 @@ var require_model_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((export
149
149
  const num = Number(val);
150
150
  return !isNaN(num) ? num : val;
151
151
  }
152
- function coerceArray(val) {
152
+ /**
153
+ * The array that `val` stands for: `val` itself, or the values of a plain object
154
+ * with sequential numeric keys. `undefined` for anything else. Never throws:
155
+ * `_coerce` tries it on every where value, and building an error for each
156
+ * scalar cost measurable CPU on every query.
157
+ */
158
+ function arrayLike(val) {
153
159
  if (Array.isArray(val)) return val;
154
- if (!isPlainObject(val)) throw new Error(g.f("Value is not an {{array}} or {{object}} with sequential numeric indices"));
160
+ if (!isPlainObject(val)) return;
155
161
  const props = Object.keys(val);
156
- if (props.length === 0) throw new Error(g.f("Value is an empty {{object}}"));
162
+ if (props.length === 0) return;
157
163
  const arrayVal = Array.from({ length: props.length });
158
164
  for (let i = 0; i < arrayVal.length; ++i) {
159
- if (!val.hasOwnProperty(i)) throw new Error(g.f("Value is not an {{array}} or {{object}} with sequential numeric indices"));
165
+ if (!val.hasOwnProperty(i)) return;
160
166
  arrayVal[i] = val[i];
161
167
  }
162
168
  return arrayVal;
163
169
  }
170
+ function coerceArray(val) {
171
+ const arrayVal = arrayLike(val);
172
+ if (arrayVal) return arrayVal;
173
+ if (isPlainObject(val) && Object.keys(val).length === 0) throw new Error(g.f("Value is an empty {{object}}"));
174
+ throw new Error(g.f("Value is not an {{array}} or {{object}} with sequential numeric indices"));
175
+ }
164
176
  function _normalizeAsArray(result) {
165
177
  if (typeof result === "string") result = [result];
166
178
  if (Array.isArray(result)) return result;
@@ -316,9 +328,7 @@ var require_model_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((export
316
328
  break;
317
329
  }
318
330
  }
319
- try {
320
- val = coerceArray(val);
321
- } catch {}
331
+ val = arrayLike(val) ?? val;
322
332
  const allowExtendedOperators = self._allowExtendedOperators(options);
323
333
  if (Array.isArray(val) && !isNestedModel(DataType)) {
324
334
  for (let i = 0; i < val.length; i++) if (val[i] !== null && val[i] !== void 0) {
package/dist/lib/model.js CHANGED
@@ -79,7 +79,6 @@ var require_model = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, mod
79
79
  const ctor = this.constructor;
80
80
  if (typeof data !== "undefined" && data !== null && data.constructor && typeof data.constructor !== "function") throw new Error(g.f("Property name \"{{constructor}}\" is not allowed in %s data", ctor.modelName));
81
81
  if (data instanceof ctor) data = data.toObject(false);
82
- const properties = Object.assign({}, ctor.definition.properties);
83
82
  data = data || {};
84
83
  if (typeof ctor.applyProperties === "function") ctor.applyProperties(data);
85
84
  options = options || {};
@@ -136,12 +135,21 @@ var require_model = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, mod
136
135
  }
137
136
  enterModelState(this, options.applySetters === "coerce", nestedModelOptions);
138
137
  try {
139
- initPropertyValues(this, data, options, properties, strict, nestedModelOptions);
138
+ initPropertyValues(this, data, options, strict, nestedModelOptions);
140
139
  } finally {
141
140
  exitModelState();
142
141
  }
143
- this.trigger("initialize");
142
+ triggerInitialize(this);
144
143
  };
144
+ function triggerInitialize(instance) {
145
+ if (instance.trigger !== Hookable.prototype.trigger) {
146
+ instance.trigger("initialize");
147
+ return;
148
+ }
149
+ const ctor = instance.constructor;
150
+ const afterHook = ctor.afterInitialize || ctor.postInitialize;
151
+ if (afterHook) afterHook.call(instance, void 0);
152
+ }
145
153
  function getNestedModelOptions(options) {
146
154
  if (options[nestedModelOptionsMarker] === true) return options;
147
155
  const nestedModelOptions = {
@@ -153,8 +161,10 @@ var require_model = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, mod
153
161
  Object.defineProperty(nestedModelOptions, nestedModelOptionsMarker, { value: true });
154
162
  return nestedModelOptions;
155
163
  }
156
- function initPropertyValues(self, data, options, properties, strict, nestedModelOptions) {
164
+ function initPropertyValues(self, data, options, strict, nestedModelOptions) {
157
165
  const ctor = self.constructor;
166
+ let properties = ctor.definition.properties;
167
+ let ownProperties = false;
158
168
  const applySetters = options.applySetters;
159
169
  const applyDefaultValues = options.applyDefaultValues;
160
170
  const applySettersOrCoercion = applySetters === true || applySetters === "coerce";
@@ -179,6 +189,10 @@ var require_model = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, mod
179
189
  const relationType = ctor.relations[p].type;
180
190
  let modelTo;
181
191
  if (!properties[p]) {
192
+ if (!ownProperties) {
193
+ properties = Object.assign({}, properties);
194
+ ownProperties = true;
195
+ }
182
196
  modelTo = ctor.relations[p].modelTo || ModelBaseClass;
183
197
  const multiple = ctor.relations[p].multiple;
184
198
  const typeName = multiple ? "Array" : modelTo.modelName;
@@ -216,7 +230,7 @@ var require_model = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, mod
216
230
  const type = properties[p].type;
217
231
  propVal = applyDefaultValue(self, p, properties[p], applyDefaultValues);
218
232
  if (propVal === void 0 && persistUndefinedAsNull) self.__data[p] = propVal = null;
219
- coercePropertyValue(self, p, type, nestedModelOptions);
233
+ if (self.__data[p] !== void 0) coercePropertyValue(self, p, type, nestedModelOptions);
220
234
  }
221
235
  }
222
236
  /**
package/dist/lib/utils.js CHANGED
@@ -30,7 +30,6 @@ var require_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((exports) =>
30
30
  exports.escapeRegExp = escapeRegExp;
31
31
  exports.applyParentProperty = applyParentProperty;
32
32
  const g = require_lib_globalize();
33
- const traverse = require("neotraverse/legacy");
34
33
  const assert = require("assert");
35
34
  const debug = require("debug")("loopback:juggler:utils");
36
35
  /**
@@ -295,18 +294,31 @@ var require_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((exports) =>
295
294
  const offendingKeys = [];
296
295
  const normalizeUndefinedInQuery = options.normalizeUndefinedInQuery;
297
296
  const maxDepth = options.maxDepth || Number.MAX_SAFE_INTEGER;
298
- const result = traverse(query).forEach(function(x) {
297
+ const ancestors = [];
298
+ const visit = (parent, key, level) => {
299
+ const root = level === 0;
300
+ const original = root ? query : parent[key];
301
+ let node = original;
302
+ const keys = typeof node === "object" && node !== null ? Object.keys(node) : null;
303
+ const update = (value) => {
304
+ if (!root) parent[key] = value;
305
+ node = value;
306
+ };
307
+ const remove = () => {
308
+ if (Array.isArray(parent)) parent.splice(key, 1);
309
+ else delete parent[key];
310
+ };
299
311
  /**
300
312
  * Security risk if the client passes in a very deep where object
301
313
  */
302
- if (this.circular) {
314
+ if (keys && ancestors.includes(original)) {
303
315
  const msg = g.f("The query object is circular");
304
316
  const err = new Error(msg);
305
317
  err.statusCode = 400;
306
318
  err.code = "QUERY_OBJECT_IS_CIRCULAR";
307
319
  throw err;
308
320
  }
309
- if (this.level > maxDepth) {
321
+ if (level > maxDepth) {
310
322
  const msg = g.f("The query object exceeds maximum depth %d", maxDepth);
311
323
  const err = new Error(msg);
312
324
  err.statusCode = 400;
@@ -317,28 +329,36 @@ var require_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((exports) =>
317
329
  * Make sure prohibited keys are removed from the query to prevent
318
330
  * sensitive values from being guessed
319
331
  */
320
- if (isProhibited(this.key, prohibitedKeys)) {
321
- offendingKeys.push(this.key);
322
- this.remove();
323
- return;
324
- }
325
- /**
326
- * Handle undefined values
327
- */
328
- if (x === void 0) switch (normalizeUndefinedInQuery) {
329
- case "nullify":
330
- this.update(null);
331
- break;
332
- case "throw": throw new Error(g.f("Unexpected `undefined` in query"));
333
- default: this.remove();
332
+ if (isProhibited(key, prohibitedKeys)) {
333
+ offendingKeys.push(key);
334
+ remove();
335
+ } else {
336
+ /**
337
+ * Handle undefined values
338
+ */
339
+ if (original === void 0) switch (normalizeUndefinedInQuery) {
340
+ case "nullify":
341
+ update(null);
342
+ break;
343
+ case "throw": throw new Error(g.f("Unexpected `undefined` in query"));
344
+ default: remove();
345
+ }
346
+ if (!Array.isArray(original) && typeof original === "object" && original !== null && original.constructor !== Object) {
347
+ update(original);
348
+ return node;
349
+ }
350
+ if (isRegExpOperator(key) && typeof original === "string") update(escapeRegExp(original));
351
+ else if (original !== void 0) update(original);
334
352
  }
335
- if (!Array.isArray(x) && typeof x === "object" && x !== null && x.constructor !== Object) {
336
- this.update(x, true);
337
- return x;
353
+ if (typeof node === "object" && node !== null) {
354
+ const childKeys = node === original ? keys : Object.keys(node);
355
+ ancestors.push(original);
356
+ for (const childKey of childKeys) visit(node, childKey, level + 1);
357
+ ancestors.pop();
338
358
  }
339
- if (isRegExpOperator(this.key) && typeof x === "string") return escapeRegExp(x);
340
- return x;
341
- });
359
+ return node;
360
+ };
361
+ const result = visit(void 0, void 0, 0);
342
362
  if (offendingKeys.length) console.error(g.f("Potential security alert: hidden/protected properties %j are used in query.", offendingKeys));
343
363
  return result;
344
364
  }
package/dist/package.js CHANGED
@@ -2,7 +2,7 @@
2
2
  var require_package = /* @__PURE__ */ require("./_virtual/_rolldown/runtime.js").__commonJSMin(((exports, module) => {
3
3
  module.exports = {
4
4
  "name": "@vsaas/loopback-datasource-juggler",
5
- "version": "13.1.1",
5
+ "version": "13.1.2",
6
6
  "description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
7
7
  "keywords": [
8
8
  "Connector",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vsaas/loopback-datasource-juggler",
3
- "version": "13.1.1",
3
+ "version": "13.1.2",
4
4
  "description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
5
5
  "keywords": [
6
6
  "Connector",