UNPKG

@authup/kit

Version:

A Package containing general (context independent) utilities.

334 lines (316 loc) 10 kB
import { customAlphabet } from 'nanoid'; import { destr } from 'destr'; /* * Copyright (c) 2025. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ var DecisionStrategy = /*#__PURE__*/ function(DecisionStrategy) { /** * One or more positive */ DecisionStrategy["AFFIRMATIVE"] = "affirmative"; /** * All positive */ DecisionStrategy["UNANIMOUS"] = "unanimous"; /** * More positive than negative */ DecisionStrategy["CONSENSUS"] = "consensus"; return DecisionStrategy; }({}); var EnvironmentName = /*#__PURE__*/ function(EnvironmentName) { EnvironmentName["PRODUCTION"] = "production"; EnvironmentName["TEST"] = "test"; EnvironmentName["DEVELOPMENT"] = "development"; return EnvironmentName; }({}); /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function toArray(input) { if (!input) { return []; } return Array.isArray(input) ? input : [ input ]; } function toStringArray(input) { return toArray(input).filter((el)=>typeof el === 'string'); } function toArrayElement(input) { return Array.isArray(input) ? input[0] : input; } /* * Copyright (c) 2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function arrayBufferToBase64(buffer) { return btoa(String.fromCharCode(...new Uint8Array(buffer))); } /* * Copyright (c) 2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function base64ToArrayBuffer(base64) { const bin = atob(base64); const len = bin.length; const bytes = new Uint8Array(len); for(let i = 0; i < len; i++){ bytes[i] = bin.charCodeAt(i); } return bytes.buffer; } /** * @see https://thewoods.blog/base64url/ * * @param input */ function base64URLEncode(input) { return btoa(input).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); } /** * @see https://thewoods.blog/base64url/ * * @param value */ function base64URLDecode(value) { const m = value.length % 4; return atob(value.replace(/-/g, '+').replace(/_/g, '/').padEnd(value.length + (m === 0 ? 0 : 4 - m), '=')); } /* * Copyright (c) 2026. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ /** * Check if an input string might be a bcrypt hash. * * @see https://stackoverflow.com/questions/5393803/can-someone-explain-how-bcrypt-verifies-a-hash * @param input */ function isBCryptHash(input) { return [ '$2a$', '$2x$', '$2y$', '$2b$' ].includes(input.substring(0, 4)); } /* * Copyright (c) 2021-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function hasOwnProperty(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); } function isPropertySet(obj, prop) { return hasOwnProperty(obj, prop); } /* * Copyright (c) 2025. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function isSimpleMatch(value, pattern) { if (Array.isArray(pattern)) { for (const element of pattern){ if (isSimpleMatch(value, element)) { return true; } } return false; } if (value === pattern) { return true; } const maxLength = Math.max(value.length, pattern.length); for(let i = 0; i < maxLength; i++){ if (value[i] === pattern[i]) { continue; } if (pattern[i] === '*') { if (pattern[i + 1] === '*') { return true; } let sI = -1; for(let j = i; j < value.length; j++){ if (value[j] === '/') { sI = j + 1; break; } } if (sI === -1) { return true; } continue; } return !value[i] && pattern[i] === '/' && pattern[i + 1] === '*'; } return false; } function createNanoID(alphabetOrLen, len) { if (typeof alphabetOrLen === 'string') { return customAlphabet(alphabetOrLen, len || 21)(); } if (typeof alphabetOrLen === 'number') { return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', alphabetOrLen)(); } return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', len || 21)(); } /* * Copyright (c) 2023-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function isObject(input) { return !!input && typeof input === 'object' && !Array.isArray(input); } function extendObject(target, source) { const keys = Object.keys(source); for (const key of keys){ target[key] = source[key]; } return target; } function flattenObject(input) { const output = {}; const keys = Object.keys(input); for (const key of keys){ const value = input[key]; if (isObject(value)) { const childAttributes = flattenObject(value); const childAttributeKeys = Object.keys(childAttributes); for (const childAttributeKey of childAttributeKeys){ output[`${key}.${childAttributeKey}`] = childAttributes[childAttributeKey]; } continue; } output[key] = input[key]; } return output; } function removeObjectProperty(input, key) { delete input[key]; } function omitObjectProperties(input, excludeKeys) { const output = {}; const keys = Object.keys(input); for (const key of keys){ if (excludeKeys.includes(key)) { continue; } output[key] = input[key]; } return output; } /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function pickRecord(data, keys) { const output = {}; for (const key of keys){ output[key] = data[key]; } return output; } function omitRecord(data, keys) { const dataKeys = Object.keys(data); let index; const output = {}; for (const dataKey of dataKeys){ index = keys.indexOf(dataKey); if (index !== -1) { continue; } output[dataKey] = data[dataKey]; } return output; } /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function isScalar(value) { return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || value === null || value === undefined; } function serialize(input) { if (typeof input === 'boolean') { return input ? 'true' : 'false'; } if (typeof input === 'undefined') { return 'undefined'; } if (input === null) { return 'null'; } if (typeof input === 'number') { return `${input}`; } if (typeof input === 'string') { return input; } return JSON.stringify(input, (key, value)=>{ if (value instanceof RegExp) { return value.toString(); } if (typeof value === 'bigint') { return value.toString(); } return value; }); } function deserialize(input) { return destr(input); } /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function template(str, data, regex = /\{\{(.+?)\}\}/g) { return Array.from(str.matchAll(regex)).reduce((acc, match)=>{ const key = match[1]; if (key && Object.prototype.hasOwnProperty.call(data, key) && typeof data[key] !== 'undefined') { return acc.replace(match[0], data[key]); } return acc; }, str); } /* * Copyright (c) 2023-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ const regexp = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i; function isUUID(input) { return regexp.test(input); } /* * Copyright (c) 2022-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function makeURLPublicAccessible(url) { return url.replace('0.0.0.0', '127.0.0.1'); } /* * Copyright (c) 2025. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ /** * Wait for x ms. * * @param ms */ async function wait(ms) { return new Promise((resolve)=>{ setTimeout(resolve, ms); }); } export { DecisionStrategy, EnvironmentName, arrayBufferToBase64, base64ToArrayBuffer, base64URLDecode, base64URLEncode, createNanoID, deserialize, extendObject, flattenObject, hasOwnProperty, isBCryptHash, isObject, isPropertySet, isScalar, isSimpleMatch, isUUID, makeURLPublicAccessible, omitObjectProperties, omitRecord, pickRecord, removeObjectProperty, serialize, template, toArray, toArrayElement, toStringArray, wait }; //# sourceMappingURL=index.mjs.map