diff --git a/package.json b/package.json index 7654b32c..275c115d 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,8 @@ "typecheck:src": "tsc --noEmit -p ./tsconfig.prod.json", "typecheck:examples": "tsc --noEmit -p ./examples/tsconfig.json", "typecheck": "npm run typecheck:src && npm run typecheck:examples", - "cloudapi:update": "git submodule update --remote" + "cloudapi:update": "git submodule update --remote", + "discover_endpoints": "ts-node scripts/discover_endpoints" }, "engines": { "node": ">=18.0.0" diff --git a/scripts/detect_services/index.ts b/scripts/detect_services/index.ts index 28be5aed..3bac315f 100644 --- a/scripts/detect_services/index.ts +++ b/scripts/detect_services/index.ts @@ -138,11 +138,11 @@ export const writeToFile = (serviceMap: ServiceMapType) => { }); }; +export type ClientsMapType = Record; export const readFile = () => { const data = fs.readFileSync(FILE_PATH, 'utf8'); const jsonData = JSON.parse(data); - - return jsonData as Record; + return jsonData as ClientsMapType; }; const main = async () => { diff --git a/scripts/discover_endpoints/index.ts b/scripts/discover_endpoints/index.ts new file mode 100644 index 00000000..dc7c2596 --- /dev/null +++ b/scripts/discover_endpoints/index.ts @@ -0,0 +1,71 @@ +import { readFile } from '../detect_services'; + +type EndpointType = { id: string; address: string }; + +const buildEndpointsMap = (endpoints: EndpointType[]) => { + return new Map(endpoints.map(({ id, address }) => [id.replaceAll('-', ''), { address, id }])); +}; + +const subsequenceLenghth = (target: string, subsequenceStr: string) => { + let maxCount = 0; + + for (let i = 0; i !== target.length; i++) { + if (subsequenceStr[0] === target[i]) { + let count = 0; + for (let j = 0, k = i; j !== subsequenceStr.length && k !== target.length; j++, k++) { + if (subsequenceStr[j] === target[k]) count++; + else break; + } + + if (count > maxCount) maxCount = count; + } + } + + return maxCount; +}; + +const main = async () => { + const resp = await fetch('https://api.cloud.yandex.net/endpoints', { + method: 'get', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + }).then((response) => response.json()); + + const endpointsMap = buildEndpointsMap(resp.endpoints); + + const serviceMap = readFile(); + + Object.keys(serviceMap).forEach((serviceName) => { + let maxCount = 0; + let res: string[] = []; + let resId: string[] = []; + + endpointsMap.forEach(({ address, id }, endpointId) => { + const count = subsequenceLenghth(serviceName.split('/').join(''), endpointId); + + if (count === 0) return; + + if (count > maxCount) { + maxCount = count; + res = [address]; + resId = [id]; + return; + } + + if (count === maxCount) { + res.push(address); + resId.push(id); + } + }); + + console.log(`\n ${serviceName} ${maxCount}`); + console.log(res, resId); + console.log(`\n`); + }); +}; + +if (require.main === module) { + main(); +} diff --git a/tsconfig.json b/tsconfig.json index f0d98e3c..4be54a2a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,22 @@ "lib": ["es6"], "disableSizeLimit": true, "moduleResolution": "nodenext" + }, + "include": ["./src"], + "ts-node": { + "compilerOptions": { + "target": "ES2021", + "module": "NodeNext", + "allowJs": true, + "declaration": true, + "outDir": "./dist", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "resolveJsonModule": true, + "lib": ["ES2021", "DOM"], + "disableSizeLimit": true + } } }