auth

Main authentication module for my projects
git clone git://git.finwo.net/app/auth
Log | Files | Refs

params.js (1254B)


      1 // src/lib/db/params.js - bind value normalization.
      2 //
      3 // node:sqlite rejects booleans, undefined and Date objects. Timestamps are
      4 // epoch milliseconds, so Date values are flattened here.
      5 
      6 export function normalizeValue(v, key) {
      7   if (v === undefined || v === null) return null;
      8   if (typeof v === 'boolean') return v ? 1 : 0;
      9   if (v instanceof Date) {
     10     if (Number.isNaN(v.getTime())) {
     11       throw new Error(`bind parameter ${key ? `{${key}} ` : ''}is an invalid Date`);
     12     }
     13     return v.getTime();
     14   }
     15   if (typeof v === 'bigint') {
     16     if (v > BigInt(Number.MAX_SAFE_INTEGER) || v < BigInt(Number.MIN_SAFE_INTEGER)) {
     17       throw new Error(`bind parameter ${key ? `{${key}} ` : ''}exceeds the safe integer range: ${v}`);
     18     }
     19     return Number(v);
     20   }
     21   if (typeof v === 'number' || typeof v === 'string') return v;
     22   if (Buffer.isBuffer(v) || v instanceof Uint8Array) return v;
     23 
     24   // Objects and arrays are almost always a forgotten JSON.stringify, or a
     25   // dotted key that should have addressed a leaf. Both are worth catching.
     26   throw new Error(
     27     `bind parameter ${key ? `{${key}} ` : ''}has unsupported type ${
     28       Array.isArray(v) ? 'array' : typeof v
     29     }; serialize it first, or address a nested value with a dotted key`
     30   );
     31 }