@amodalai/amodal 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Amodal Labs, Inc.
4
+ * SPDX-License-Identifier: MIT
5
+ */
6
+ import { createServer } from 'node:net';
7
+ /**
8
+ * Find a free TCP port, starting from `preferred`. If `preferred` is taken,
9
+ * tries incrementally higher ports up to `preferred + maxAttempts`.
10
+ *
11
+ * Uses the "bind then close" technique: creates a TCP server on the candidate
12
+ * port, reads the actual assigned port, then closes the server. This avoids
13
+ * TOCTOU races better than simply checking if a port is open — the OS reserves
14
+ * the port for the brief lifetime of the server.
15
+ */
16
+ export async function findFreePort(preferred, maxAttempts = 10) {
17
+ for (let offset = 0; offset < maxAttempts; offset++) {
18
+ const candidate = preferred + offset;
19
+ const port = await tryPort(candidate);
20
+ if (port !== null)
21
+ return port;
22
+ }
23
+ // Fallback: let the OS pick any available port
24
+ const port = await tryPort(0);
25
+ if (port !== null)
26
+ return port;
27
+ throw new PortAllocationError(preferred, maxAttempts);
28
+ }
29
+ /**
30
+ * Attempt to bind a TCP server to the given port. Returns the port number on
31
+ * success, or null if the port is in use.
32
+ */
33
+ function tryPort(port) {
34
+ return new Promise((resolve) => {
35
+ const server = createServer();
36
+ server.once('error', () => {
37
+ resolve(null);
38
+ });
39
+ server.listen(port, '127.0.0.1', () => {
40
+ const addr = server.address();
41
+ const assignedPort = typeof addr === 'object' && addr !== null ? addr.port : null;
42
+ server.close(() => {
43
+ resolve(assignedPort);
44
+ });
45
+ });
46
+ });
47
+ }
48
+ // ---------------------------------------------------------------------------
49
+ // Error
50
+ // ---------------------------------------------------------------------------
51
+ export class PortAllocationError extends Error {
52
+ preferred;
53
+ maxAttempts;
54
+ constructor(preferred, maxAttempts) {
55
+ super(`Failed to find a free port starting from ${String(preferred)} ` +
56
+ `after ${String(maxAttempts)} attempts`);
57
+ this.name = 'PortAllocationError';
58
+ this.preferred = preferred;
59
+ this.maxAttempts = maxAttempts;
60
+ }
61
+ }
62
+ //# sourceMappingURL=find-free-port.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find-free-port.js","sourceRoot":"","sources":["../../../src/shared/find-free-port.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAC,YAAY,EAAC,MAAM,UAAU,CAAC;AAEtC;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,SAAiB,EAAE,WAAW,GAAG,EAAE;IACpE,KAAK,IAAI,MAAM,GAAG,CAAC,EAAE,MAAM,GAAG,WAAW,EAAE,MAAM,EAAE,EAAE,CAAC;QACpD,MAAM,SAAS,GAAG,SAAS,GAAG,MAAM,CAAC;QACrC,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC,CAAC;QACtC,IAAI,IAAI,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC;IACjC,CAAC;IACD,+CAA+C;IAC/C,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;IAC9B,IAAI,IAAI,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAC/B,MAAM,IAAI,mBAAmB,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;AACxD,CAAC;AAED;;;GAGG;AACH,SAAS,OAAO,CAAC,IAAY;IAC3B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,MAAM,GAAG,YAAY,EAAE,CAAC;QAC9B,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;YACxB,OAAO,CAAC,IAAI,CAAC,CAAC;QAChB,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE;YACpC,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;YAC9B,MAAM,YAAY,GAAG,OAA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