Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
echo "registry=https://registry.npmjs.org/" > .npmrc
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" >> .npmrc

- run: npm publish --scope=@internxt --access public
- run: npm publish --scope=@internxt --access public
5 changes: 3 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

yarn lint && yarn format && yarn test
yarn lint
yarn lint-staged
yarn test
8 changes: 4 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default [
...eslintConfigInternxt,
{
rules: {
"@typescript-eslint/no-explicit-any": "warn",
}
}
];
'@typescript-eslint/no-explicit-any': 'warn',
},
},
];
30 changes: 17 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internxt/lib",
"version": "1.4.1",
"version": "1.4.2",
"description": "Common logic shared between different projects of Internxt ",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand All @@ -12,7 +12,7 @@
"test:cov": "vitest run --coverage",
"build": "tsc",
"lint": "eslint src/**/*.ts",
"format": "prettier src/**/*.ts",
"format": "prettier --write **/*.{js,jsx,tsx,ts,cjs,mjs,mts,json}",
"prepare": "husky"
},
"repository": {
Expand All @@ -26,17 +26,21 @@
},
"homepage": "https://github.com/internxt/lib#readme",
"devDependencies": {
"@internxt/eslint-config-internxt": "2.1.0",
"@internxt/prettier-config": "internxt/prettier-config#v1.0.2",
"@types/node": "25.5.0",
"@vitest/coverage-istanbul": "4.1.0",
"eslint": "10.1.0",
"husky": "9.1.7",
"prettier": "3.8.1",
"typescript": "5.9.3",
"vitest": "4.1.0"
"@internxt/eslint-config-internxt": "^2.1.0",
"@internxt/prettier-config": "^2.0.1",
"@types/node": "^25.6.0",
"@vitest/coverage-istanbul": "^4.1.5",
"eslint": "^10.3.0",
"husky": "^9.1.7",
"lint-staged": "^17.0.2",
"prettier": "^3.8.3",
"typescript": "^6.0.3",
"vitest": "^4.1.5"
},
"dependencies": {
"uuid": "^14.0.0"
"dependencies": {},
"lint-staged": {
"*.{js,jsx,tsx,ts,cjs,mjs,mts,json}": [
"prettier --write"
]
}
}
2 changes: 1 addition & 1 deletion src/auth/isValidEmail.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RFC 5322 Official Standard
export const emailRegexPattern =
// eslint-disable-next-line no-control-regex, max-len
// eslint-disable-next-line max-len
/^((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*"))@((?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))$/;

/**
Expand Down
7 changes: 5 additions & 2 deletions src/items/renameIfNeeded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ export default function renameIfNeeded(
incrementIndex,
};
})
.filter((item) => item.cleanName.toLowerCase() === cleanFilename.toLowerCase()
&& (item.type?.toLowerCase() ?? '') === (type?.toLowerCase() ?? ''))
.filter(
(item) =>
item.cleanName.toLowerCase() === cleanFilename.toLowerCase() &&
(item.type?.toLowerCase() ?? '') === (type?.toLowerCase() ?? ''),
)
.sort((a, b) => b.incrementIndex - a.incrementIndex);

const filenameExists = !!infoFilenames.length;
Expand Down
32 changes: 21 additions & 11 deletions src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { randomBytes } from 'crypto';
import { Buffer } from 'buffer';
import { validate as validateUuidv4 } from 'uuid';

/**
* Checks if a string is a valid UUID v4
* @param uuid - The string to check
* @returns True if the string is a valid UUID v4, false otherwise
*/
const validateV4Uuid = (uuid: string): boolean => {
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return UUID_REGEX.test(uuid);
};

/**
* Converts a standard Base64 string into a URL-safe Base64 variant:
Expand Down Expand Up @@ -52,14 +61,12 @@ const fromBase64UrlSafe = (urlSafe: string): string => {
* @returns A URL-safe string exactly `size` characters long
*/
const generateRandomStringUrlSafe = (size: number): string => {
if (size <= 0) {
if (!Number.isInteger(size) || size <= 0) {
throw new Error('Size must be a positive integer');
}
// Base64 yields 4 chars per 3 bytes, so it computes the minimum bytes required
const numBytes = Math.ceil((size * 3) / 4);
const buf = randomBytes(numBytes).toString('base64');

return toBase64UrlSafe(buf).substring(0, size);
const charsUrlSafe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
const random = randomBytes(size);
return Array.from(random, (byte) => charsUrlSafe[byte % charsUrlSafe.length]).join('');
};

/**
Expand All @@ -70,8 +77,10 @@ const generateRandomStringUrlSafe = (size: number): string => {
function base64UrlSafetoUUID(base64UrlSafe: string): string {
const hex = Buffer.from(fromBase64UrlSafe(base64UrlSafe), 'base64').toString('hex');

return `${hex.substring(0, 8)}-${hex.substring(8, 12)}-${hex.substring(12, 16)}` +
`-${hex.substring(16, 20)}-${hex.substring(20)}`;
return (
`${hex.substring(0, 8)}-${hex.substring(8, 12)}-${hex.substring(12, 16)}` +
`-${hex.substring(16, 20)}-${hex.substring(20)}`
);
}

/**
Expand All @@ -82,7 +91,7 @@ function base64UrlSafetoUUID(base64UrlSafe: string): string {
* @returns The decoded v4 UUID string.
*/
const decodeV4Uuid = (value: string) => {
if (!validateUuidv4(value)) {
if (!validateV4Uuid(value)) {
try {
return base64UrlSafetoUUID(value);
} catch {
Expand All @@ -101,7 +110,7 @@ const decodeV4Uuid = (value: string) => {
* @throws {Error} If the provided UUIDv4 is not valid.
*/
const encodeV4Uuid = (v4Uuid: string) => {
if (!validateUuidv4(v4Uuid)) {
if (!validateV4Uuid(v4Uuid)) {
throw new Error('The provided UUIDv4 is not valid');
}
const removedUuidDecoration = v4Uuid.replace(/-/g, '');
Expand All @@ -117,4 +126,5 @@ export default {
base64UrlSafetoUUID,
decodeV4Uuid,
encodeV4Uuid,
validateV4Uuid,
};
24 changes: 23 additions & 1 deletion test/utils/stringUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, expect, it } from 'vitest';
import { stringUtils } from '../../src';

const { base64UrlSafetoUUID, fromBase64UrlSafe, generateRandomStringUrlSafe, toBase64UrlSafe } = stringUtils;
const { base64UrlSafetoUUID, fromBase64UrlSafe, generateRandomStringUrlSafe, toBase64UrlSafe, validateV4Uuid } =
stringUtils;

describe('stringUtils', () => {
describe('toBase64UrlSafe', () => {
Expand Down Expand Up @@ -69,4 +70,25 @@ describe('stringUtils', () => {
expect(uuid).toBe('f32a91da-c799-4e13-aa17-8c4d9e0323c9');
});
});

describe('validateV4Uuid', () => {
it('should return true for a valid UUIDv4', () => {
expect(validateV4Uuid('f32a91da-c799-4e13-aa17-8c4d9e0323c9')).toBe(true);
});

it('should return false for an invalid UUIDv4', () => {
expect(validateV4Uuid('invalid-uuid')).toBe(false);
expect(validateV4Uuid('f32a91da-c799-4e13-aa17-8c4d9e0323c99')).toBe(false);
expect(validateV4Uuid('00000000-0000-0000-0000-000000000000')).toBe(false);
expect(validateV4Uuid('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')).toBe(false);
});

it('should return false for an empty string', () => {
expect(validateV4Uuid('')).toBe(false);
});

it('should return false for a string with incorrect length', () => {
expect(validateV4Uuid('12345678-1234-1234-1234-123456789012')).toBe(false);
});
});
});
77 changes: 9 additions & 68 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,73 +1,14 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
"target": "esnext",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"rootDir": "./",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["test", "dist", "node_modules"]
}
14 changes: 3 additions & 11 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@ import { coverageConfigDefaults, defineConfig } from 'vitest/config';
export default defineConfig({
test: {
exclude: ['node_modules', 'dist'],
include: [
'src/**/*.test.{ts,tsx,js,jsx}',
'test/**/*.test.{ts,tsx,js,jsx}',
],
include: ['src/**/*.test.{ts,tsx,js,jsx}', 'test/**/*.test.{ts,tsx,js,jsx}'],
coverage: {
provider: 'istanbul',
reporter: ['text', 'lcov'],
include: [
'src/**/*.{js,ts,jsx,tsx}',
'test/**/*.{js,ts,jsx,tsx}'
],
exclude: [
...coverageConfigDefaults.exclude
]
include: ['src/**/*.{js,ts,jsx,tsx}', 'test/**/*.{js,ts,jsx,tsx}'],
exclude: [...coverageConfigDefaults.exclude],
},
},
});
Loading
Loading