@20syldev/api 3.3.8 → 3.4.0
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.
- package/.github/FUNDING.yml +1 -1
- package/.github/workflows/publish.yml +27 -27
- package/LICENSE +27 -27
- package/README.md +110 -60
- package/app.js +817 -1174
- package/modules/v3/algorithms.js +218 -0
- package/modules/v3/captcha.js +55 -0
- package/modules/v3/chat.js +109 -0
- package/modules/v3/color.js +41 -0
- package/modules/v3/convert.js +39 -0
- package/modules/v3/domain.js +39 -0
- package/modules/v3/hash.js +16 -0
- package/modules/v3/hyperplanning.js +51 -0
- package/modules/v3/levenshtein.js +46 -0
- package/modules/v3/personal.js +127 -0
- package/modules/v3/qrcode.js +20 -0
- package/modules/v3/tic_tac_toe.js +167 -0
- package/modules/v3/time.js +87 -0
- package/modules/v3/token.js +48 -0
- package/modules/v3/username.js +32 -0
- package/modules/v3/utils.js +66 -0
- package/modules/v3.js +15 -0
- package/package.json +53 -49
- package/robots.txt +73 -73
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { random, randomNumber } from './utils.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generate random personal information
|
|
5
|
+
*
|
|
6
|
+
* @returns {Object} Comprehensive personal profile with contact info, financial data, and more
|
|
7
|
+
*/
|
|
8
|
+
export default function personal() {
|
|
9
|
+
const people = [
|
|
10
|
+
{ name: 'John Doe', social: 'john_doe', email: 'john@example.com', country: 'US' },
|
|
11
|
+
{ name: 'Jane Martin', social: 'jane_martin', email: 'jane@example.com', country: 'FR' },
|
|
12
|
+
{ name: 'Michael Johnson', social: 'mike_johnson', email: 'michael@example.com', country: 'UK' },
|
|
13
|
+
{ name: 'Emily Davis', social: 'emily_davis', email: 'emily@example.com', country: 'ES' },
|
|
14
|
+
{ name: 'Alexis Barbos', social: 'alexis_barbos', email: 'alexis@example.com', country: 'DE' },
|
|
15
|
+
{ name: 'Sarah Williams', social: 'sarah_williams', email: 'sarah@example.com', country: 'IT' },
|
|
16
|
+
{ name: 'Daniel Brown', social: 'daniel_brown', email: 'daniel@example.com', country: 'JP' },
|
|
17
|
+
{ name: 'Sophia Wilson', social: 'sophia_wilson', email: 'sophia@example.com', country: 'BR' },
|
|
18
|
+
{ name: 'James Taylor', social: 'james_taylor', email: 'james@example.com', country: 'CA' },
|
|
19
|
+
{ name: 'Olivia Thomas', social: 'olivia_thomas', email: 'olivia@example.com', country: 'AU' }
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const countries = {
|
|
23
|
+
US: { tel: '123-456-7890', code: '1', lang: 'English' },
|
|
24
|
+
FR: { tel: '06 78 90 12 34', code: '33', lang: 'French' },
|
|
25
|
+
UK: { tel: '7911 123456', code: '44', lang: 'English' },
|
|
26
|
+
ES: { tel: '678 901 234', code: '34', lang: 'Spanish' },
|
|
27
|
+
DE: { tel: '163 555 1584', code: '49', lang: 'German' },
|
|
28
|
+
IT: { tel: '345 678 9012', code: '39', lang: 'Italian' },
|
|
29
|
+
JP: { tel: '080-1234-5678', code: '81', lang: 'Japanese' },
|
|
30
|
+
BR: { tel: '(11) 98765-4321', code: '55', lang: 'Portuguese' },
|
|
31
|
+
CA: { tel: '416-123-4567', code: '1', lang: 'English' },
|
|
32
|
+
AU: { tel: '0412 345 678', code: '61', lang: 'English' }
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const jobs = ['Writer', 'Artist', 'Musician', 'Explorer', 'Scientist', 'Engineer', 'Athlete', 'Doctor'];
|
|
36
|
+
const hobbies = ['Reading', 'Traveling', 'Gaming', 'Cooking', 'Fitness', 'Music', 'Photography', 'Writing'];
|
|
37
|
+
const cities = ['New York', 'Paris', 'London', 'Madrid', 'Berlin', 'Rome', 'Tokyo', 'Los Angeles', 'Sydney', 'São Paulo', 'Toronto'];
|
|
38
|
+
const streets = ['Main St', '2nd Ave', 'Broadway', 'Park Lane', 'Elm St', 'Sunset Blvd', 'Maple St', 'Highland Rd'];
|
|
39
|
+
|
|
40
|
+
const card = Array.from({ length: 4 }, () => randomNumber(1000, 9999)).join(' ');
|
|
41
|
+
const cvc = randomNumber(100, 999);
|
|
42
|
+
const expiration = `${String(randomNumber(1, 12)).padStart(2, '0')}/${(new Date().getFullYear() + randomNumber(0, 3)).toString().slice(-2)}`;
|
|
43
|
+
|
|
44
|
+
const person = random(people);
|
|
45
|
+
const social = person.social;
|
|
46
|
+
const country = person.country;
|
|
47
|
+
const phone = countries[country].tel;
|
|
48
|
+
const lang = countries[country].lang;
|
|
49
|
+
|
|
50
|
+
const age = randomNumber(18, 67);
|
|
51
|
+
const birthday = new Date(Date.now() - randomNumber(18, 67) * 365.25 * 24 * 60 * 60 * 1000).toISOString();
|
|
52
|
+
|
|
53
|
+
let emergencyContacts = [];
|
|
54
|
+
let yearIncome = randomNumber(20000, 100000);
|
|
55
|
+
let subscriptions = [];
|
|
56
|
+
let pets = [];
|
|
57
|
+
let vehicles = [];
|
|
58
|
+
let civilStatus = 'Single';
|
|
59
|
+
let children = 0;
|
|
60
|
+
|
|
61
|
+
if (age >= 21 && Math.random() > 0.7) civilStatus = 'Married';
|
|
62
|
+
|
|
63
|
+
if (civilStatus === 'Married' && age >= 25) children = randomNumber(0, 3);
|
|
64
|
+
|
|
65
|
+
while (emergencyContacts.length < randomNumber(1, 3)) {
|
|
66
|
+
let emergencyContact = random(people);
|
|
67
|
+
while (
|
|
68
|
+
emergencyContact.email === person.email ||
|
|
69
|
+
emergencyContacts.some(e => e.email === emergencyContact.email)
|
|
70
|
+
) {
|
|
71
|
+
emergencyContact = random(people);
|
|
72
|
+
}
|
|
73
|
+
const emergencyPhone = `${randomNumber(100, 999)}-${randomNumber(100, 999)}-${randomNumber(1000, 9999)}`;
|
|
74
|
+
emergencyContacts.push({
|
|
75
|
+
name: emergencyContact.name,
|
|
76
|
+
relationship: random(['Spouse', 'Parent', 'Sibling', 'Friend']),
|
|
77
|
+
phone: `+${countries[country].code} ${emergencyPhone}`
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
while (subscriptions.length < randomNumber(1, 3)) {
|
|
82
|
+
let subscription = random(['Netflix', 'Spotify', 'Amazon Prime', 'Disney+', 'Hulu']);
|
|
83
|
+
if (!subscriptions.includes(subscription)) subscriptions.push(subscription);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
while (pets.length < randomNumber(1, 3)) {
|
|
87
|
+
let pet = random(['Dog', 'Cat', 'Fish', 'Bird', 'None']);
|
|
88
|
+
if (!pets.includes(pet)) pets.push(pet);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
while (vehicles.length < randomNumber(1, 3)) {
|
|
92
|
+
let vehicle = random(['Car', 'Bike', 'Motorcycle', 'Bus', 'None']);
|
|
93
|
+
if (!vehicles.includes(vehicle)) vehicles.push(vehicle);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
name: person.name,
|
|
98
|
+
email: person.email,
|
|
99
|
+
localisation: country,
|
|
100
|
+
phone: `+${countries[country].code} ${phone}`,
|
|
101
|
+
job: random(jobs),
|
|
102
|
+
hobbies: random(hobbies),
|
|
103
|
+
language: lang,
|
|
104
|
+
card,
|
|
105
|
+
cvc,
|
|
106
|
+
expiration,
|
|
107
|
+
address: `${randomNumber(1, 9999)} ${random(streets)}, ${random(cities)}`,
|
|
108
|
+
birthday,
|
|
109
|
+
civil_status: civilStatus,
|
|
110
|
+
children,
|
|
111
|
+
vehicle: vehicles,
|
|
112
|
+
social_profiles: {
|
|
113
|
+
twitter: `@${social}`,
|
|
114
|
+
facebook: `facebook.com/${social}`,
|
|
115
|
+
linkedin: `linkedin.com/in/${social}`,
|
|
116
|
+
instagram: `instagram.com/${social}`
|
|
117
|
+
},
|
|
118
|
+
year_income: `${yearIncome} USD/year`,
|
|
119
|
+
month_income: `${(yearIncome / 12).toFixed(2)} USD/month`,
|
|
120
|
+
education: random(['High School', 'Bachelor\'s', 'Master\'s', 'PhD']),
|
|
121
|
+
work_experience: `${randomNumber(0, 20)} years`,
|
|
122
|
+
health_status: random(['Healthy', 'Minor Issues', 'Chronic Conditions']),
|
|
123
|
+
emergency_contacts: emergencyContacts,
|
|
124
|
+
subscriptions,
|
|
125
|
+
pets,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { toDataURL } from 'qrcode';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generate a QR code for the provided URL.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} url - The URL to encode in the QR code
|
|
7
|
+
* @returns {Promise<string>} - Base64 encoded QR code image
|
|
8
|
+
* @throws {Error} - If URL is invalid or QR code generation fails
|
|
9
|
+
*/
|
|
10
|
+
export default async function qrcode(url) {
|
|
11
|
+
// Input validation
|
|
12
|
+
if (!url || typeof url !== 'string') throw new Error('Please provide a valid URL');
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
// Generate QR code
|
|
16
|
+
return await toDataURL(url);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
throw new Error(`Failed to generate QR code: ${error.message}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { randomBytes } from 'crypto';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Manages a Tic-Tac-Toe game session
|
|
5
|
+
*
|
|
6
|
+
* @param {string} action - The action to perform ('play' or 'fetch')
|
|
7
|
+
* @param {Object} params - The parameters for the action
|
|
8
|
+
* @returns {Object} - The result of the action
|
|
9
|
+
* @throws {Error} - If parameters are invalid
|
|
10
|
+
*/
|
|
11
|
+
export default function tic_tac_toe(action, params = {}) {
|
|
12
|
+
// In-memory storage
|
|
13
|
+
const storage = params.storage || {};
|
|
14
|
+
|
|
15
|
+
// Initialize storage
|
|
16
|
+
storage.games ??= {};
|
|
17
|
+
storage.sessions ??= {};
|
|
18
|
+
storage.rateLimits ??= {};
|
|
19
|
+
|
|
20
|
+
// Reference the storage properties
|
|
21
|
+
const { games, sessions, rateLimits } = storage;
|
|
22
|
+
|
|
23
|
+
// Input validation for common parameters
|
|
24
|
+
if (!params.username) throw new Error('Please provide a username');
|
|
25
|
+
|
|
26
|
+
const u = params.username.toLowerCase();
|
|
27
|
+
const now = Date.now();
|
|
28
|
+
|
|
29
|
+
// Rate limiting
|
|
30
|
+
rateLimits[u] = (rateLimits[u] || []).filter(ts => now - ts < 10000);
|
|
31
|
+
if (rateLimits[u].length > 50) {
|
|
32
|
+
const remainingTime = Math.ceil((rateLimits[u][0] + 10000 - now) / 1000);
|
|
33
|
+
throw new Error(`Rate limit exceeded. Try again in ${remainingTime} seconds.`);
|
|
34
|
+
}
|
|
35
|
+
rateLimits[u].push(now);
|
|
36
|
+
|
|
37
|
+
// Handle different actions
|
|
38
|
+
if (action === 'play') {
|
|
39
|
+
return playMove(params, games, sessions, u, now);
|
|
40
|
+
} else if (action === 'fetch') {
|
|
41
|
+
return fetchGame(params, games, u);
|
|
42
|
+
} else {
|
|
43
|
+
throw new Error('Invalid action. Use "play" or "fetch"');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Process a player's move
|
|
49
|
+
*/
|
|
50
|
+
function playMove(params, games, sessions, u, now) {
|
|
51
|
+
const { move, session, game } = params;
|
|
52
|
+
const validMoves = ['1-1', '1-2', '1-3', '2-1', '2-2', '2-3', '3-1', '3-2', '3-3'];
|
|
53
|
+
|
|
54
|
+
// Input validation for play action
|
|
55
|
+
if (!move) throw new Error('Please provide a valid move');
|
|
56
|
+
if (!session) throw new Error('Please provide a valid session ID');
|
|
57
|
+
if (!game) throw new Error('Please provide a valid game ID');
|
|
58
|
+
if (!validMoves.includes(move)) throw new Error('Invalid move. Please provide a valid move (e.g., 1-1, 2-2, 3-3).');
|
|
59
|
+
|
|
60
|
+
// Session validation
|
|
61
|
+
if (sessions[u] && sessions[u].user !== session) {
|
|
62
|
+
throw new Error('Session ID mismatch');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Initialize game if needed
|
|
66
|
+
games[game] = games[game] || [];
|
|
67
|
+
|
|
68
|
+
// Check if game is full
|
|
69
|
+
const players = [...new Set(games[game].map(play => play.username))];
|
|
70
|
+
if (players.length >= 2 && !players.includes(params.username)) {
|
|
71
|
+
throw new Error('Game is full, you can only watch.');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Check if it's player's turn
|
|
75
|
+
if (games[game].length > 0 && games[game][games[game].length - 1].username === params.username) {
|
|
76
|
+
throw new Error('Please wait for the other player to make a move.');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Check if move is already made
|
|
80
|
+
if (games[game].some(play => play.move === move)) {
|
|
81
|
+
throw new Error('Move already made. Please choose a different move.');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Add move to game
|
|
85
|
+
const play = { username: params.username, move, session };
|
|
86
|
+
games[game].push(play);
|
|
87
|
+
|
|
88
|
+
// Check game result
|
|
89
|
+
const result = checkGame(games[game]);
|
|
90
|
+
if (result.winner || result.tie) {
|
|
91
|
+
setTimeout(() => delete games[game], 600000);
|
|
92
|
+
return {
|
|
93
|
+
message: `Move sent successfully. ${result.winner ? result.winner + ' wins. ' + result.loser + ' loses.' : 'It\'s a tie.'}`,
|
|
94
|
+
...result
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Set cleanup timeout
|
|
99
|
+
setTimeout(() => delete games[game], 3600000);
|
|
100
|
+
|
|
101
|
+
// Update session
|
|
102
|
+
sessions[u] = sessions[u] || { user: session, last: now };
|
|
103
|
+
sessions[u].last = now;
|
|
104
|
+
setTimeout(() => { if (now - sessions[u].last >= 3600000) delete sessions[u]; }, 3600000);
|
|
105
|
+
|
|
106
|
+
return { message: 'Move sent successfully' };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Fetch a game state
|
|
111
|
+
*/
|
|
112
|
+
function fetchGame(params, games, u) {
|
|
113
|
+
const ID = params.game || generateGameId();
|
|
114
|
+
|
|
115
|
+
// Initialize game if needed
|
|
116
|
+
if (!games[ID]) games[ID] = [];
|
|
117
|
+
|
|
118
|
+
const data = games[ID];
|
|
119
|
+
const last = data.length ? data[data.length - 1].username : null;
|
|
120
|
+
const players = [...new Set(data.map(p => p.username))];
|
|
121
|
+
const turn = players.find(p => p !== last);
|
|
122
|
+
const result = data.length ? checkGame(data) : {};
|
|
123
|
+
|
|
124
|
+
return { game: data, turn, ID, ...result };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Generate a random game ID
|
|
129
|
+
*/
|
|
130
|
+
function generateGameId() {
|
|
131
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
132
|
+
return Array.from(randomBytes(5)).map(b => chars[b % chars.length]).join('');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Check the game result
|
|
137
|
+
*/
|
|
138
|
+
function checkGame(moves) {
|
|
139
|
+
let board = Array(3).fill().map(() => Array(3).fill(null));
|
|
140
|
+
let playerSymbols = {};
|
|
141
|
+
let playersOrder = [];
|
|
142
|
+
|
|
143
|
+
moves.forEach(({ username, move }) => {
|
|
144
|
+
if (!playerSymbols[username]) {
|
|
145
|
+
playersOrder.push(username);
|
|
146
|
+
playerSymbols[username] = playersOrder.length === 1 ? 'X' : 'O';
|
|
147
|
+
}
|
|
148
|
+
let [row, col] = move.split('-').map(Number);
|
|
149
|
+
board[row - 1][col - 1] = playerSymbols[username];
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const checkWinner = (symbol) => {
|
|
153
|
+
for (let i = 0; i < 3; i++) {
|
|
154
|
+
if (board[i][0] === symbol && board[i][1] === symbol && board[i][2] === symbol) return true;
|
|
155
|
+
if (board[0][i] === symbol && board[1][i] === symbol && board[2][i] === symbol) return true;
|
|
156
|
+
}
|
|
157
|
+
if (board[0][0] === symbol && board[1][1] === symbol && board[2][2] === symbol) return true;
|
|
158
|
+
if (board[0][2] === symbol && board[1][1] === symbol && board[2][0] === symbol) return true;
|
|
159
|
+
return false;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
let winner = Object.keys(playerSymbols).find(player => checkWinner(playerSymbols[player]));
|
|
163
|
+
let isTie = !winner && moves.length === 9;
|
|
164
|
+
let loser = winner && playersOrder.length === 2 ? playersOrder.find(player => player !== winner) : null;
|
|
165
|
+
|
|
166
|
+
return { winner, loser, tie: isTie };
|
|
167
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate time information based on specified parameters.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} type - The type of time information ('live' or 'random')
|
|
5
|
+
* @param {string} start - Start date for random generation (ISO format)
|
|
6
|
+
* @param {string} end - End date for random generation (ISO format)
|
|
7
|
+
* @param {string} format - Specific time format to return
|
|
8
|
+
* @param {string} timezone - Timezone to use
|
|
9
|
+
* @returns {object} - Time information in various formats
|
|
10
|
+
* @throws {Error} - If inputs are invalid
|
|
11
|
+
*/
|
|
12
|
+
export default function time(type = 'live', start, end, format, timezone) {
|
|
13
|
+
const validFormats = [
|
|
14
|
+
'iso', 'utc', 'timestamp', 'locale', 'date', 'time',
|
|
15
|
+
'year', 'month', 'day', 'hour', 'minute', 'second',
|
|
16
|
+
'ms', 'dayOfWeek', 'dayOfYear', 'weekNumber',
|
|
17
|
+
'timezone', 'timezoneOffset'
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const validTimezones = [
|
|
21
|
+
'UTC', 'America/New_York', 'Europe/Paris',
|
|
22
|
+
'Asia/Tokyo', 'Australia/Sydney'
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
// Input validation
|
|
26
|
+
if (type !== 'live' && type !== 'random') {
|
|
27
|
+
throw new Error('Please provide a valid type (live or random)');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (start && !Date.parse(start)) {
|
|
31
|
+
throw new Error('Please provide a valid start date (YYYY-MM-DD)');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (end && !Date.parse(end)) {
|
|
35
|
+
throw new Error('Please provide a valid end date (YYYY-MM-DD)');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (format && !validFormats.includes(format)) {
|
|
39
|
+
throw new Error(`Please provide a valid format. Options: ${validFormats.join(', ')}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (timezone && !validTimezones.includes(timezone)) {
|
|
43
|
+
throw new Error(`Please provide a valid timezone. Options: ${validTimezones.join(', ')}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Helper function to get all time formats for a date
|
|
47
|
+
const getTimeFormats = (date, tz) => {
|
|
48
|
+
return {
|
|
49
|
+
iso: date.toISOString(),
|
|
50
|
+
utc: date.toUTCString(),
|
|
51
|
+
timestamp: date.getTime(),
|
|
52
|
+
locale: date.toLocaleString('en-US', { timeZone: tz, timeZoneName: 'long' }),
|
|
53
|
+
date: date.toLocaleDateString('en-US', { timeZone: tz }),
|
|
54
|
+
time: date.toLocaleTimeString('en-US', { timeZone: tz }),
|
|
55
|
+
year: date.getFullYear(),
|
|
56
|
+
month: date.getMonth() + 1,
|
|
57
|
+
day: date.getDate(),
|
|
58
|
+
hour: date.getHours(),
|
|
59
|
+
minute: date.getMinutes(),
|
|
60
|
+
second: date.getSeconds(),
|
|
61
|
+
ms: date.getMilliseconds(),
|
|
62
|
+
dayOfWeek: date.getDay(),
|
|
63
|
+
dayOfYear: Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 86400000),
|
|
64
|
+
weekNumber: Math.ceil((((date - new Date(date.getFullYear(), 0, 0)) / 86400000) + 1) / 7),
|
|
65
|
+
timezone: tz,
|
|
66
|
+
timezoneOffset: date.getTimezoneOffset()
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// Generate time information based on type
|
|
71
|
+
if (type === 'random') {
|
|
72
|
+
const startDate = start ? new Date(start).getTime() : new Date('1900-01-01').getTime();
|
|
73
|
+
const endDate = end ? new Date(end).getTime() : new Date('2100-12-31').getTime();
|
|
74
|
+
const randomDate = new Date(startDate + Math.random() * (endDate - startDate));
|
|
75
|
+
const tz = timezone || validTimezones[Math.floor(Math.random() * validTimezones.length)];
|
|
76
|
+
const formats = getTimeFormats(randomDate, tz);
|
|
77
|
+
|
|
78
|
+
return format ? { date: formats[format] } : formats;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Live time information
|
|
82
|
+
const now = new Date();
|
|
83
|
+
const tz = timezone || 'UTC';
|
|
84
|
+
const formats = getTimeFormats(now, tz);
|
|
85
|
+
|
|
86
|
+
return format ? { date: formats[format] } : formats;
|
|
87
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { randomBytes } from 'crypto';
|
|
2
|
+
import { v4 } from 'uuid';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generate a random token of specified length and type.
|
|
6
|
+
*
|
|
7
|
+
* @param {number} len - Length of the token
|
|
8
|
+
* @param {string} type - Type of token to generate
|
|
9
|
+
* @returns {string} - The generated token
|
|
10
|
+
* @throws {Error} - If inputs are invalid
|
|
11
|
+
*/
|
|
12
|
+
export default function token(len, type = 'alphanum') {
|
|
13
|
+
// Input validation
|
|
14
|
+
if (isNaN(len) || len < 12) {
|
|
15
|
+
throw new Error('Length must be a number greater than or equal to 12');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (len > 4096) {
|
|
19
|
+
throw new Error('Length cannot exceed 4096');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Helper function to generate token from character set
|
|
23
|
+
const genToken = (chars, length) => {
|
|
24
|
+
return Array.from({ length }, () => {
|
|
25
|
+
return chars[Math.floor(Math.random() * chars.length)];
|
|
26
|
+
}).join('');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Token type definitions
|
|
30
|
+
const tokenTypes = {
|
|
31
|
+
alpha: () => genToken('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', len),
|
|
32
|
+
alphanum: () => genToken('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', len),
|
|
33
|
+
base64: () => randomBytes(Math.ceil(len * 0.75)).toString('base64').slice(0, len),
|
|
34
|
+
hex: () => randomBytes(Math.ceil(len * 0.5)).toString('hex').slice(0, len),
|
|
35
|
+
num: () => genToken('0123456789', len),
|
|
36
|
+
punct: () => genToken('!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', len),
|
|
37
|
+
urlsafe: () => genToken('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_', len),
|
|
38
|
+
uuid: () => v4().replace(/-/g, '').slice(0, len)
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Generate token based on type or default to alphanum
|
|
42
|
+
if (!tokenTypes[type.toLowerCase()]) {
|
|
43
|
+
throw new Error(`Invalid token type. Valid types: ${Object.keys(tokenTypes).join(', ')}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Generate token based on type or default to alphanum
|
|
47
|
+
return (tokenTypes[type.toLowerCase()] || tokenTypes.alphanum)();
|
|
48
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { random, randomNumber } from './utils.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generate a random username with related information
|
|
5
|
+
*
|
|
6
|
+
* @returns {Object} Object containing username and component parts
|
|
7
|
+
*/
|
|
8
|
+
export default function username() {
|
|
9
|
+
const adj = ['Happy', 'Silly', 'Clever', 'Creative', 'Brave', 'Gentle', 'Kind', 'Funny', 'Wise', 'Charming', 'Sincere', 'Resourceful', 'Patient', 'Energetic', 'Adventurous', 'Ambitious', 'Courageous', 'Courteous', 'Determined'];
|
|
10
|
+
const ani = ['Cat', 'Dog', 'Tiger', 'Elephant', 'Monkey', 'Penguin', 'Dolphin', 'Lion', 'Bear', 'Fox', 'Owl', 'Giraffe', 'Zebra', 'Koala', 'Rabbit', 'Squirrel', 'Panda', 'Horse', 'Wolf', 'Eagle'];
|
|
11
|
+
const job = ['Writer', 'Artist', 'Musician', 'Explorer', 'Scientist', 'Engineer', 'Athlete', 'Chef', 'Doctor', 'Teacher', 'Lawyer', 'Entrepreneur', 'Actor', 'Dancer', 'Photographer', 'Architect', 'Pilot', 'Designer', 'Journalist', 'Veterinarian'];
|
|
12
|
+
|
|
13
|
+
const nombre = randomNumber(0, 99);
|
|
14
|
+
const choix = {
|
|
15
|
+
adj_num: () => random(adj) + nombre,
|
|
16
|
+
ani_num: () => random(ani) + nombre,
|
|
17
|
+
pro_num: () => random(job) + nombre,
|
|
18
|
+
adj_ani: () => random(adj) + random(ani),
|
|
19
|
+
adj_ani_num: () => random(adj) + random(ani) + nombre,
|
|
20
|
+
adj_pro: () => random(adj) + random(job),
|
|
21
|
+
pro_ani: () => random(job) + random(ani),
|
|
22
|
+
pro_ani_num: () => random(job) + random(ani) + nombre
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
username: choix[random(Object.keys(choix))](),
|
|
27
|
+
number: nombre,
|
|
28
|
+
adjective: random(adj),
|
|
29
|
+
animal: random(ani),
|
|
30
|
+
job: random(job)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utility functions for the API modules
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Return a random element from an array.
|
|
7
|
+
*
|
|
8
|
+
* @param {Array} arr - The array to choose from.
|
|
9
|
+
* @returns {*} - A random element from the array.
|
|
10
|
+
*/
|
|
11
|
+
export function random(arr) {
|
|
12
|
+
return arr[Math.floor(Math.random() * arr.length)];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Return a random number between min and max (inclusive).
|
|
17
|
+
*
|
|
18
|
+
* @param {number} min - The minimum number.
|
|
19
|
+
* @param {number} max - The maximum number.
|
|
20
|
+
* @returns {number} - A random number between min and max.
|
|
21
|
+
*/
|
|
22
|
+
export function randomNumber(min, max) {
|
|
23
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Generate a random IP address.
|
|
28
|
+
*
|
|
29
|
+
* @returns {string} - A random IP address in the format "X.X.X.X".
|
|
30
|
+
*/
|
|
31
|
+
export function genIP() {
|
|
32
|
+
return `${randomNumber(0, 255)}.${randomNumber(0, 255)}.${randomNumber(0, 255)}.${randomNumber(0, 255)}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Format a JavaScript Date object to ISO format without timezone.
|
|
37
|
+
*
|
|
38
|
+
* @param {Date} date - Date object to format
|
|
39
|
+
* @returns {string} - Formatted date string
|
|
40
|
+
*/
|
|
41
|
+
export function formatDate(date) {
|
|
42
|
+
return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().replace('Z', '');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Manage rate limiting for users
|
|
47
|
+
*
|
|
48
|
+
* @param {Object} rateLimits - Rate limits object
|
|
49
|
+
* @param {string} userId - User ID
|
|
50
|
+
* @param {number} timestamp - Current timestamp
|
|
51
|
+
* @param {number} window - Time window in milliseconds
|
|
52
|
+
* @param {number} limit - Maximum requests in window
|
|
53
|
+
* @returns {boolean} - True if rate limit is exceeded
|
|
54
|
+
* @throws {Error} - If rate limit is exceeded
|
|
55
|
+
*/
|
|
56
|
+
export function checkRateLimit(rateLimits, userId, timestamp, window = 10000, limit = 50) {
|
|
57
|
+
rateLimits[userId] = (rateLimits[userId] || []).filter(ts => timestamp - ts < window);
|
|
58
|
+
|
|
59
|
+
if (rateLimits[userId].length > limit) {
|
|
60
|
+
const remainingTime = Math.ceil((rateLimits[userId][0] + window - timestamp) / 1000);
|
|
61
|
+
throw new Error(`Rate limit exceeded. Try again in ${remainingTime} seconds.`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
rateLimits[userId].push(timestamp);
|
|
65
|
+
return false;
|
|
66
|
+
}
|
package/modules/v3.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * as algorithms from './v3/algorithms.js';
|
|
2
|
+
export { default as chat } from './v3/chat.js';
|
|
3
|
+
export { default as captcha } from './v3/captcha.js';
|
|
4
|
+
export { default as color } from './v3/color.js';
|
|
5
|
+
export { default as convert } from './v3/convert.js';
|
|
6
|
+
export { default as domain } from './v3/domain.js';
|
|
7
|
+
export { default as hash } from './v3/hash.js';
|
|
8
|
+
export { default as hyperplanning } from './v3/hyperplanning.js';
|
|
9
|
+
export { default as levenshtein } from './v3/levenshtein.js';
|
|
10
|
+
export { default as personal } from './v3/personal.js';
|
|
11
|
+
export { default as qrcode } from './v3/qrcode.js';
|
|
12
|
+
export { default as tic_tac_toe } from './v3/tic_tac_toe.js';
|
|
13
|
+
export { default as time } from './v3/time.js';
|
|
14
|
+
export { default as token } from './v3/token.js';
|
|
15
|
+
export { default as username } from './v3/username.js';
|
package/package.json
CHANGED
|
@@ -1,49 +1,53 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "3.
|
|
3
|
-
"name": "@20syldev/api",
|
|
4
|
-
"description": "Node.js API with multiple features. Check the documentation at https://docs.sylvain.pro",
|
|
5
|
-
"main": "app.js",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"start": "node app.js",
|
|
9
|
-
"dev": "nodemon app.js",
|
|
10
|
-
"build": "npm install && node app.js",
|
|
11
|
-
"lint": "prettier --write .",
|
|
12
|
-
"upgrade:minor": "npm upgrade",
|
|
13
|
-
"upgrade:major": "npx npm-check-updates -u && npm install",
|
|
14
|
-
"upgrade:build": "npm upgrade && npm install && node app.js"
|
|
15
|
-
},
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
},
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
|
|
1
|
+
{
|
|
2
|
+
"version": "3.4.0",
|
|
3
|
+
"name": "@20syldev/api",
|
|
4
|
+
"description": "Node.js API with multiple features. Check the documentation at https://docs.sylvain.pro",
|
|
5
|
+
"main": "app.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node app.js",
|
|
9
|
+
"dev": "nodemon app.js",
|
|
10
|
+
"build": "npm install && node app.js",
|
|
11
|
+
"lint": "prettier --write .",
|
|
12
|
+
"upgrade:minor": "npm upgrade",
|
|
13
|
+
"upgrade:major": "npx npm-check-updates -u && npm install",
|
|
14
|
+
"upgrade:build": "npm upgrade && npm install && node app.js"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
"./v1": "./modules/v1.js",
|
|
18
|
+
"./v2": "./modules/v2.js",
|
|
19
|
+
"./v3": "./modules/v3.js"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"canvas": "latest",
|
|
23
|
+
"cors": "latest",
|
|
24
|
+
"dotenv": "latest",
|
|
25
|
+
"express": "latest",
|
|
26
|
+
"ical.js": "latest",
|
|
27
|
+
"node-fetch": "latest",
|
|
28
|
+
"qrcode": "latest",
|
|
29
|
+
"random": "latest",
|
|
30
|
+
"uuid": "latest"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"nodemon": "latest",
|
|
34
|
+
"npm-check-updates": "latest",
|
|
35
|
+
"prettier": "latest"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/20syldev/api.git"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"api",
|
|
43
|
+
"express",
|
|
44
|
+
"utility",
|
|
45
|
+
"math"
|
|
46
|
+
],
|
|
47
|
+
"author": "Sylvain L.",
|
|
48
|
+
"license": "BSD 3-Clause",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/20syldev/api/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://api.sylvain.pro"
|
|
53
|
+
}
|