"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoSerializer=void 0,exports.validateTimeout=validateTimeout,exports.authString=authString,exports.decamelize=decamelize;const bson_1=require("bson"),faas_server_utils_1=require("@alipay/faas-server-utils"),MAX_TIMEOUT=5*60*1e3;function clampTimeout(e,t){return e>MAX_TIMEOUT?(console.log(`${t} is bigger than 5 min, will set it to 5 min`),MAX_TIMEOUT):e}function validateTimeout(e){if(e===void 0)return e;if(!Array.isArray(e))return clampTimeout(e,"timeout");const[t,r]=e;return[clampTimeout(t,"connection timeout"),clampTimeout(r,"response timeout")]}function authString(e,t=""){const r=(0,faas_server_utils_1.getAlipayContext)(),n=Date.now(),s={authorization:"","x-from-app-id":r.APPID,"x-from-env-id":e.fromEnvId,"x-to-env-id":e.toEnvId,"x-from-instance-id":e.functionInstanceId,"x-from-function-name":e.functionName,"x-client-timestamp":`${n}`,"x-trace-id":r.TRACEID},c=["x-from-app-id","x-from-env-id","x-from-instance-id","x-from-function-name","x-client-timestamp","x-to-env-id"],u={path:"",query:"",secretId:e.secretId,secretKey:e.secretKey,method:"POST",headers:s,body:t,timestamp:n,signedHeaders:c,isMySql:!0};s.authorization=(0,faas_server_utils_1.sign)(u);const a=[];for(const i in s){if(i==="authorization")continue;const o=s[i];a.push(`${i}:${o}`)}const p=s.authorization.split(" ");a.push(`SecAlgo:${p[0]}`);for(let i=1;i<p.length;i++){let o=p[i];o.endsWith(",")&&(o=o.substring(0,o.length-1)),a.push(o.replace(/=/g,":").replace(/,/g,";"))}return`!cloudbase:${a.join(",")}`}class MongoSerializer{static serialize(t){return MongoSerializer.filterUndefinedKey(t,[]),bson_1.EJSON.stringify(t)}static deserialize(t){return bson_1.EJSON.deserialize(t)}static filterUndefinedKey(t,r){if(!r.includes(t)){if(r.push(t),!t||typeof t!="object")return t;for(const n in t)t[n]===void 0?delete t[n]:MongoSerializer.filterUndefinedKey(t[n],r)}}}exports.MongoSerializer=MongoSerializer;function decamelize(e){const t="_";if(typeof e!="string")throw new TypeError("The `text` arguments should be of type `string`");if(e.length<2)return e.toLowerCase();const n=`$1${t}$2`;return e.replace(/([\p{Lowercase_Letter}\d])(\p{Uppercase_Letter})/gu,n).replace(/(\p{Uppercase_Letter})(\p{Uppercase_Letter}\p{Lowercase_Letter}+)/gu,n).toLowerCase()}function handlePreserveConsecutiveUppercase(e,t){return e=e.replace(/((?<![\p{Uppercase_Letter}\d])[\p{Uppercase_Letter}\d](?![\p{Uppercase_Letter}\d]))/gu,r=>r.toLowerCase()),e.replace(/(\p{Uppercase_Letter}+)(\p{Uppercase_Letter}\p{Lowercase_Letter}+)/gu,(r,n,s)=>n+t+s.toLowerCase())}