UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const lodash_1 = require("lodash");
7const function_arguments_1 = __importDefault(require("function-arguments"));
8const APIConfig_1 = require("./APIConfig");
9const crypto_1 = __importDefault(require("crypto"));
10const slugify_1 = __importDefault(require("slugify"));
11const shortid_1 = __importDefault(require("shortid"));
12const nanoid_1 = require("nanoid");
13const safe_base64_1 = require("safe-base64");
14slugify_1.default.extend({ "/": ":" });
15class APIUtils {
16 static getRawTypeName(obj) {
17 return Object.prototype.toString.call(obj).slice(8, -1);
18 }
19 static generateShortID() {
20 return shortid_1.default.generate();
21 }
22 static generateLongID(length) {
23 return nanoid_1.nanoid(length);
24 }
25 static slugify(text) {
26 return slugify_1.default(text);
27 }
28 static convertToType(value, convertToType) {
29 if (lodash_1.isNil(convertToType)) {
30 return value;
31 }
32 let convertedValue;
33 let rawValueType = APIUtils.getRawTypeName(value);
34 // No conversion needed
35 if (rawValueType === convertToType) {
36 return value;
37 }
38 // Objects and Arrays can only be converted to JSON strings
39 if (rawValueType === "Object" || rawValueType === "Array") {
40 if (convertToType === "String") {
41 try {
42 return JSON.stringify(value);
43 }
44 catch (e) {
45 }
46 }
47 return undefined;
48 }
49 switch (convertToType) {
50 case "String": {
51 convertedValue = lodash_1.toString(value);
52 break;
53 }
54 case "Boolean": {
55 convertedValue = APIUtils.toBoolean(value);
56 break;
57 }
58 case "Number": {
59 convertedValue = lodash_1.toNumber(value);
60 break;
61 }
62 case "Array": {
63 convertedValue = lodash_1.castArray(value);
64 break;
65 }
66 case "Object": {
67 try {
68 convertedValue = JSON.parse(value);
69 }
70 catch (e) {
71 convertedValue = value;
72 }
73 }
74 }
75 return convertedValue;
76 }
77 static toBoolean(input) {
78 if (lodash_1.isBoolean(input)) {
79 return input;
80 }
81 return (/(1|true|yes)/i).test(input);
82 }
83 static coalesce(...inputArgs) {
84 for (let inputArg of inputArgs) {
85 // Consider an empty string as a null value
86 if (lodash_1.isNil(inputArg) || (lodash_1.isString(inputArg) && inputArg === "")) {
87 continue;
88 }
89 return inputArg;
90 }
91 return null;
92 }
93 static getFunctionParamNames(fn) {
94 return function_arguments_1.default(fn);
95 }
96 static bufferToString(buffer, encoding) {
97 switch (encoding) {
98 case "urlsafe":
99 {
100 return safe_base64_1.encode(buffer);
101 }
102 default:
103 {
104 return buffer.toString(encoding);
105 }
106 }
107 }
108 static stringToBuffer(theString, encoding) {
109 switch (encoding) {
110 case "urlsafe":
111 {
112 return safe_base64_1.decode(theString);
113 }
114 default:
115 {
116 return Buffer.from(theString, encoding);
117 }
118 }
119 }
120 static encrypt(text, password = APIConfig_1.APIConfig.ENCRYPTION_SECRET, encoding = 'base64') {
121 password = lodash_1.padEnd(password, 32, "0");
122 let iv = crypto_1.default.randomBytes(this._IV_LENGTH);
123 let cipher = crypto_1.default.createCipheriv(this._CRYPTO_ALG, Buffer.from(password), iv);
124 let encrypted = cipher.update(text);
125 const separator = encoding === "urlsafe" ? "%3A" : ":";
126 encrypted = Buffer.concat([encrypted, cipher.final()]);
127 return APIUtils.bufferToString(iv, encoding) + separator + APIUtils.bufferToString(encrypted, encoding);
128 }
129 static decrypt(text, password = APIConfig_1.APIConfig.ENCRYPTION_SECRET, encoding = 'base64') {
130 const separator = encoding === "urlsafe" ? "%3A" : ":";
131 let textParts = text.split(separator);
132 let iv = APIUtils.stringToBuffer(textParts.shift(), encoding);
133 let encryptedText = APIUtils.stringToBuffer(textParts.join(separator), encoding);
134 let decipher = crypto_1.default.createDecipheriv(this._CRYPTO_ALG, Buffer.from(password), iv);
135 let decrypted = decipher.update(encryptedText);
136 decrypted = Buffer.concat([decrypted, decipher.final()]);
137 return decrypted.toString();
138 }
139 static hashString(text, encoding = 'base64') {
140 let shasum = crypto_1.default.createHash(this._HASH_ALG);
141 shasum.update(text);
142 return APIUtils.bufferToString(shasum.digest(), encoding);
143 }
144 static hashMD5(text, encoding = 'base64') {
145 let md5 = crypto_1.default.createHash('md5');
146 md5.update(text);
147 return APIUtils.bufferToString(md5.digest(), encoding);
148 }
149 /**
150 * Creates an expiration date in seconds since UNIX epoch from now.
151 * @param expirationInSeconds
152 */
153 static createExpirationInSeconds(expirationInSeconds) {
154 return Math.floor(Date.now() / 1000) + expirationInSeconds;
155 }
156}
157exports.APIUtils = APIUtils;
158APIUtils._IV_LENGTH = 16;
159APIUtils._CRYPTO_ALG = 'aes-256-cbc';
160APIUtils._HASH_ALG = 'sha256';
161//# sourceMappingURL=APIUtils.js.map
\No newline at end of file