@kanun-hq/plugin-file 0.1.3 → 0.1.5

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.
Files changed (2) hide show
  1. package/dist/index.js +22 -9
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -378,6 +378,12 @@ let installed = false;
378
378
  function isRecord(value) {
379
379
  return typeof value === "object" && value !== null && !Array.isArray(value);
380
380
  }
381
+ function isBlobLike(value) {
382
+ return typeof Blob !== "undefined" && value instanceof Blob;
383
+ }
384
+ function isArrayBufferReadable(value) {
385
+ return isRecord(value) && typeof value.arrayBuffer === "function";
386
+ }
381
387
  /**
382
388
  * Check if the given value is a file or an array of files.
383
389
  *
@@ -385,17 +391,21 @@ function isRecord(value) {
385
391
  * @returns
386
392
  */
387
393
  function isFileLike(value) {
394
+ if (isBlobLike(value)) return true;
388
395
  if (!isRecord(value)) return false;
389
396
  return [
390
397
  "buffer",
398
+ "fieldname",
391
399
  "filename",
392
400
  "height",
401
+ "lastModified",
393
402
  "mimetype",
394
403
  "name",
395
404
  "originalname",
396
405
  "path",
397
406
  "size",
398
407
  "type",
408
+ "arrayBuffer",
399
409
  "width"
400
410
  ].some((key) => typeof value[key] !== "undefined");
401
411
  }
@@ -409,6 +419,10 @@ function normalizeFiles(value) {
409
419
  if (Array.isArray(value)) return value.filter(isFileLike);
410
420
  return isFileLike(value) ? [value] : [];
411
421
  }
422
+ function isResolvedFileCandidate(value) {
423
+ if (Array.isArray(value)) return value.length > 0 && value.every(isFileLike);
424
+ return isFileLike(value);
425
+ }
412
426
  /**
413
427
  * Resolve the candidate files for the given attribute and context. T
414
428
  *
@@ -418,7 +432,8 @@ function normalizeFiles(value) {
418
432
  * @returns
419
433
  */
420
434
  async function resolveCandidateFiles(value, attribute, context) {
421
- if (typeof value !== "undefined") return value;
435
+ if (isResolvedFileCandidate(value)) return value;
436
+ let resolvedValue;
422
437
  if (pluginOptions.resolveFiles) {
423
438
  const resolved = await pluginOptions.resolveFiles({
424
439
  attribute,
@@ -426,9 +441,13 @@ async function resolveCandidateFiles(value, attribute, context) {
426
441
  data: context.data,
427
442
  value
428
443
  });
429
- if (typeof resolved !== "undefined") return resolved;
444
+ if (isResolvedFileCandidate(resolved)) return resolved;
445
+ resolvedValue = resolved;
430
446
  }
431
- return deepFind(context.context.requestFiles ?? {}, attribute);
447
+ const requestScopedValue = deepFind(context.context.requestFiles ?? {}, attribute);
448
+ if (typeof requestScopedValue !== "undefined") return requestScopedValue;
449
+ if (typeof resolvedValue !== "undefined") return resolvedValue;
450
+ return value;
432
451
  }
433
452
  async function resolveFiles(value, attribute, context) {
434
453
  const candidates = await resolveCandidateFiles(value, attribute, context);
@@ -437,12 +456,6 @@ async function resolveFiles(value, attribute, context) {
437
456
  files: normalizeFiles(candidates)
438
457
  };
439
458
  }
440
- function isBlobLike(value) {
441
- return typeof Blob !== "undefined" && value instanceof Blob;
442
- }
443
- function isArrayBufferReadable(value) {
444
- return isRecord(value) && typeof value.arrayBuffer === "function";
445
- }
446
459
  async function getBuffer(file) {
447
460
  if (file.buffer instanceof Uint8Array || Buffer.isBuffer(file.buffer)) return file.buffer;
448
461
  if (file.buffer instanceof ArrayBuffer) return Buffer.from(file.buffer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kanun-hq/plugin-file",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "File validation plugin for Kanun.",
5
5
  "type": "module",
6
6
  "publishConfig": {