Skip to content

Commit

Permalink
Revert code.
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulla-ashurov committed Aug 28, 2023
1 parent 427286c commit 9bf6ebe
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,27 @@ import { RevocationController } from './controllers/revocation.js';
import { CORS_ALLOWED_ORIGINS, CORS_ERROR_MSG, configLogToExpress } from './types/constants.js';
import { LogToWebHook } from './middleware/hook.js';
import { Middleware } from './middleware/middleware.js';
// import { JSONStringify } from './monkey-patch.js';

import * as dotenv from 'dotenv';
dotenv.config();

// monkey patch JSON.stringify to use native-like implementation
// TODO: remove this when @verida/encryption-utils,
// TODO: switches json.sortify to its own implementation
// TODO: e.g. replace JSON.stringify = require('json.sortify')
// TODO: with JSON.sortify = require('json.sortify')
// see: https://github.com/verida/verida-js/blob/c94b95de687c64cc776652602665bb45a327dfb6/packages/encryption-utils/src/index.ts#L10
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// JSON.stringify = function (value, _replacer, _space) {
// return (
// JSONStringify(value) ||
// (function () {
// throw new Error('JSON.stringify failed');
// })()
// );
// };

// Define Swagger file
import swaggerDocument from './static/swagger.json' assert { type: 'json' };
import { handleAuthRoutes, withLogto } from '@logto/express';
Expand Down
117 changes: 117 additions & 0 deletions src/monkey-patch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export const JSONStringify = (obj: Record<string, any> | null) => {
const isArray = (value: Record<string, any> | null) => {
return Array.isArray(value) && typeof value === 'object';
};

const isObject = (value: Record<string, any> | null) => {
return typeof value === 'object' && value !== null && !Array.isArray(value);
};

const isString = (value: Record<string, any> | null) => {
return typeof value === 'string';
};

const isBoolean = (value: Record<string, any> | null) => {
return typeof value === 'boolean';
};

const isNumber = (value: Record<string, any> | null) => {
return typeof value === 'number';
};

const isNull = (value: Record<string, any> | null) => {
return value === null && typeof value === 'object';
};

const isNotNumber = (value: Record<string, any> | null) => {
return typeof value === 'number' && isNaN(value);
};

const isInfinity = (value: Record<string, any> | null) => {
return typeof value === 'number' && !isFinite(value);
};

const isDate = (value: Record<string, any> | null) => {
return typeof value === 'object' && value !== null && typeof value.getMonth === 'function';
};

const isUndefined = (value: Record<string, any> | null) => {
return value === undefined && typeof value === 'undefined';
};

const isFunction = (value: Record<string, any> | null) => {
return typeof value === 'function';
};

const isSymbol = (value: Record<string, any> | null) => {
return typeof value === 'symbol';
};

const restOfDataTypes = (value: Record<string, any> | null) => {
return isNumber(value) || isString(value) || isBoolean(value);
};

const ignoreDataTypes = (value: Record<string, any> | null) => {
return isUndefined(value) || isFunction(value) || isSymbol(value);
};

const nullDataTypes = (value: Record<string, any> | null) => {
return isNotNumber(value) || isInfinity(value) || isNull(value);
};

const arrayValuesNullTypes = (value: Record<string, any>) => {
return isNotNumber(value) || isInfinity(value) || isNull(value) || ignoreDataTypes(value);
};

const removeComma = (str: string) => {
const tempArr = str.split('');
tempArr.pop();
return tempArr.join('');
};

if (ignoreDataTypes(obj)) {
return undefined;
}

if (isDate(obj)) {
return `"${obj?.toISOString()}"`;
}

if (nullDataTypes(obj)) {
return `${null}`;
}

if (isSymbol(obj)) {
return undefined;
}

if (restOfDataTypes(obj)) {
const passQuotes = isString(obj) ? `"` : '';
return `${passQuotes}${obj}${passQuotes}`;
}

if (isArray(obj)) {
let arrStr = '';
obj?.forEach((eachValue: any) => {
arrStr += arrayValuesNullTypes(eachValue) ? JSONStringify(null) : JSONStringify(eachValue);
arrStr += ',';
});

return `[` + removeComma(arrStr) + `]`;
}

if (isObject(obj)) {
let objStr = '';

const objKeys = Object.keys(obj || {});

objKeys.forEach((eachKey) => {
const eachValue = obj?.[eachKey];
objStr += !ignoreDataTypes(eachValue) ? `"${eachKey}":${JSONStringify(eachValue)},` : '';
});
return `{` + removeComma(objStr) + `}`;
}

return undefined;
};

0 comments on commit 9bf6ebe

Please sign in to comment.