Files
desa-darmasaba/find-port.ts
nico 171d7f7947 fix(biome-lint): resolve critical and medium priority lint issues
CRITICAL FIXES:
- Fix noAsyncPromiseExecutor in xcoba.ts and xcoba2.ts
  * Removed async promise executor pattern
  * Refactored to proper promise chain with .then()/.catch()
  * Added proper error handling for unhandled rejections

- Fix useIterableCallbackReturn in seed_berita.ts
  * Replaced forEach with for...of loop to avoid returning values in callbacks

MEDIUM FIXES:
- Fix useNodejsImportProtocol (728 files auto-fixed)
  * Updated Node.js builtin imports to use node: protocol
  * Files: eslint.config.mjs, vitest.config.ts, zgen/image.ts, and 725+ more

- Fix useOptionalChain in xcoba.ts (auto-fixed)
  * Changed 'resOut && resOut.body' to 'resOut?.body'

- Fix noImportantStyles in dark-mode-table.css
  * Added biome-ignore suppression comments with justification
  * Required to override Mantine UI library styles

- Fix noUselessContinue in find-port.ts (auto-fixed)
  * Removed unnecessary continue statement

- Fix useLiteralKeys (700+ files auto-fixed)
  * Simplified computed expressions to use literal keys
  * Example: obj['create'] -> obj.create

RESULTS:
- Errors reduced: 4,516 → 3,521 (-22%)
- Warnings reduced: 3,861 → 2,083 (-46%)
- Total issues reduced: 8,991 → 6,115 (-32%)
- 735 files auto-fixed by biome lint --fix

Remaining issues (~6,115):
- Mostly noExplicitAny warnings requiring manual refactoring
- Will be addressed in gradual code quality improvements

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-04-09 17:27:17 +08:00

68 lines
2.5 KiB
TypeScript

import getPort, { portNumbers } from 'get-port';
/**
* Mencari port yang tersedia dalam rentang tertentu.
* @param params - Parameter opsional untuk mencari port.
* @param params.count - Jumlah port yang dibutuhkan (default: 1).
* @param params.portStart - Awal rentang port (default: 3000).
* @param params.portEnd - Akhir rentang port (default: 6000).
* @param params.exclude - Daftar port yang harus dikecualikan.
* @returns Array port yang tersedia atau null jika tidak ada port yang cukup.
*/
async function findPort(params?: { count?: number, portStart?: number, portEnd?: number, exclude?: number[] }) {
const { count = 1, portStart = 3000, portEnd = 6000, exclude = [] } = params || {};
// Gabungkan port yang dikecualikan
const listPort = [...exclude]; // Hapus .flat() karena tidak diperlukan
const usedPorts = Array.from(new Set(listPort)) as number[];
// Validasi input
if (count <= 0) {
throw new Error('Count harus lebih besar dari 0');
}
if (count > (portEnd - portStart + 1)) {
throw new Error(`Count tidak boleh lebih besar dari range port (${portEnd - portStart + 1})`);
}
if (portStart >= portEnd) {
throw new Error('portStart harus lebih kecil dari portEnd');
}
if (portStart < 0 || portEnd > 65535) {
throw new Error('Port harus berada dalam rentang 0-65535');
}
// Optimasi pencarian port
const availablePorts = new Set<number>();
const portRange = portNumbers(portStart, portEnd);
const usedPortsSet = new Set(usedPorts);
for (const port of portRange) {
if (availablePorts.size >= count) break;
// Skip jika port sudah digunakan
if (usedPortsSet.has(port)) continue;
try {
const availablePort = await getPort({
port,
exclude: [...usedPorts, ...Array.from(availablePorts)],
});
// Pastikan port yang diperiksa berada dalam rentang yang ditentukan
if (availablePort === port && availablePort >= portStart && availablePort <= portEnd) {
availablePorts.add(port);
}
} catch (error) {
console.warn(`Gagal memeriksa port ${port}:`, error);
}
}
// Jika tidak cukup port yang tersedia, lempar error
if (availablePorts.size < count) {
throw new Error('Tidak cukup port yang tersedia dalam rentang yang diberikan');
}
return Array.from(availablePorts);
}
export default findPort;