projects
/
squeep-indie-auther
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
fix changelog
[squeep-indie-auther]
/
src
/
common.js
diff --git
a/src/common.js
b/src/common.js
index 7b9f7adc659bb6d01a2c0ece8a7e791b6e7e363b..0855720ee2a1766e46e92dad2a853edce22288a9 100644
(file)
--- a/
src/common.js
+++ b/
src/common.js
@@
-2,15
+2,15
@@
const { common } = require('@squeep/api-dingus');
const { common } = require('@squeep/api-dingus');
-const { randomBytes } = require('crypto');
-const { promisify } = require('util');
+const { randomBytes } = require('
node:
crypto');
+const { promisify } = require('
node:
util');
const randomBytesAsync = promisify(randomBytes);
/**
* Limit length of string to keep logs sane
const randomBytesAsync = promisify(randomBytes);
/**
* Limit length of string to keep logs sane
- * @param {
String}
str
- * @param {
Number}
len
- * @returns {
String}
+ * @param {
string} str
str
+ * @param {
number} len
len
+ * @returns {
string} str
*/
const logTruncate = (str, len) => {
if (typeof str !== 'string' || str.toString().length <= len) {
*/
const logTruncate = (str, len) => {
if (typeof str !== 'string' || str.toString().length <= len) {
@@
-21,9
+21,9
@@
const logTruncate = (str, len) => {
/**
* Turn a snake into a camel.
/**
* Turn a snake into a camel.
- * @param {
String} snakeC
ase
- * @param {
String|RegExp}
delimiter
- * @returns {
String}
+ * @param {
string} snakeCase snake c
ase
+ * @param {
string | RegExp} delimiter
delimiter
+ * @returns {
string} camel case
*/
const camelfy = (snakeCase, delimiter = '_') => {
if (!snakeCase || typeof snakeCase.split !== 'function') {
*/
const camelfy = (snakeCase, delimiter = '_') => {
if (!snakeCase || typeof snakeCase.split !== 'function') {
@@
-38,7
+38,8
@@
const camelfy = (snakeCase, delimiter = '_') => {
/**
* Return an array containing x if x is not an array.
/**
* Return an array containing x if x is not an array.
- * @param {*} x
+ * @param {*} x x
+ * @returns {any[]} x[]
*/
const ensureArray = (x) => {
if (x === undefined) {
*/
const ensureArray = (x) => {
if (x === undefined) {
@@
-52,13
+53,13
@@
const ensureArray = (x) => {
/**
* Recursively freeze an object.
/**
* Recursively freeze an object.
- * @param {
Object} o
- * @returns {
Object}
+ * @param {
object} o obj
+ * @returns {
object} frozen obj
*/
const freezeDeep = (o) => {
Object.freeze(o);
Object.getOwnPropertyNames(o).forEach((prop) => {
*/
const freezeDeep = (o) => {
Object.freeze(o);
Object.getOwnPropertyNames(o).forEach((prop) => {
- if (Object.hasOwn
Property.call
(o, prop)
+ if (Object.hasOwn(o, prop)
&& ['object', 'function'].includes(typeof o[prop]) // eslint-disable-line security/detect-object-injection
&& !Object.isFrozen(o[prop])) { // eslint-disable-line security/detect-object-injection
return freezeDeep(o[prop]); // eslint-disable-line security/detect-object-injection
&& ['object', 'function'].includes(typeof o[prop]) // eslint-disable-line security/detect-object-injection
&& !Object.isFrozen(o[prop])) { // eslint-disable-line security/detect-object-injection
return freezeDeep(o[prop]); // eslint-disable-line security/detect-object-injection
@@
-68,9
+69,13
@@
const freezeDeep = (o) => {
};
};
-/** Oauth2.1 §3.2.3.1
+/**
+ * Oauth2.1 §3.2.3.1
* %x20-21 / %x23-5B / %x5D-7E
* %x20-21 / %x23-5B / %x5D-7E
- * @param {String} char
+ * ' '-'!' / '#'-'[' / ']'-'~'
+ * not allowed: control characters, '"', '\'
+ * @param {string} char character
+ * @returns {boolean} is valid
*/
const validErrorChar = (char) => {
const value = char.charCodeAt(0);
*/
const validErrorChar = (char) => {
const value = char.charCodeAt(0);
@@
-82,8
+87,8
@@
const validErrorChar = (char) => {
/**
* Determine if an OAuth error message is valid.
/**
* Determine if an OAuth error message is valid.
- * @param {
String}
error
- * @returns {
Boolean}
+ * @param {
string} error
error
+ * @returns {
boolean} is valid
*/
const validError = (error) => {
return error && error.split('').filter((c) => !validErrorChar(c)).length === 0 || false;
*/
const validError = (error) => {
return error && error.split('').filter((c) => !validErrorChar(c)).length === 0 || false;
@@
-93,7
+98,8
@@
const validError = (error) => {
/**
* OAuth2.1 §3.2.2.1
* scope-token = 1*( %x21 / %x23-5B / %x5D-7E )
/**
* OAuth2.1 §3.2.2.1
* scope-token = 1*( %x21 / %x23-5B / %x5D-7E )
- * @param {String} char
+ * @param {string} char char
+ * @returns {boolean} is valid
*/
const validScopeChar = (char) => {
const value = char.charCodeAt(0);
*/
const validScopeChar = (char) => {
const value = char.charCodeAt(0);
@@
-105,8
+111,8
@@
const validScopeChar = (char) => {
/**
* Determine if a scope has a valid name.
/**
* Determine if a scope has a valid name.
- * @param {
String}
scope
- * @returns {
Boolean}
+ * @param {
string} scope
scope
+ * @returns {
boolean} is valid
*/
const validScope = (scope) => {
return scope && scope.split('').filter((c) => !validScopeChar(c)).length === 0 || false;
*/
const validScope = (scope) => {
return scope && scope.split('').filter((c) => !validScopeChar(c)).length === 0 || false;
@@
-115,7
+121,8
@@
const validScope = (scope) => {
/**
*
/**
*
- * @param {Number} bytes
+ * @param {number} bytes bytes
+ * @returns {string} base64 random string
*/
const newSecret = async (bytes = 64) => {
return (await randomBytesAsync(bytes * 3 / 4)).toString('base64');
*/
const newSecret = async (bytes = 64) => {
return (await randomBytesAsync(bytes * 3 / 4)).toString('base64');
@@
-124,8
+131,8
@@
const newSecret = async (bytes = 64) => {
/**
* Convert a Date object to epoch seconds.
/**
* Convert a Date object to epoch seconds.
- * @param {Date=} date
- * @returns {
Number}
+ * @param {Date=} date
date
+ * @returns {
number} epoch
*/
const dateToEpoch = (date) => {
const dateMs = date ? date.getTime() : Date.now();
*/
const dateToEpoch = (date) => {
const dateMs = date ? date.getTime() : Date.now();
@@
-134,14
+141,20
@@
const dateToEpoch = (date) => {
const omit = (o, props) => {
const omit = (o, props) => {
- return Object.fromEntries(Object.entries(o).filter(([k]) => !props.includes(k)))
+ return Object.fromEntries(Object.entries(o).filter(([k]) => !props.includes(k)))
;
};
};
+/**
+ * @typedef {object} ConsoleLike
+ * @property {Function} debug log debug
+ */
+
/**
* Log Mystery Box statistics events.
/**
* Log Mystery Box statistics events.
- * @param {Console} logger
- * @param {String} scope
+ * @param {ConsoleLike} logger logger instance
+ * @param {string} scope scope
+ * @returns {Function} stat logger
*/
const mysteryBoxLogger = (logger, scope) => {
return (s) => {
*/
const mysteryBoxLogger = (logger, scope) => {
return (s) => {