@bridgerb/port-from-name 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # @bridgerb/port-from-name
2
+
3
+ Give every project its own dev-server port, automatically. The port comes from your project's name, so it's the same every time you run it — no more "port 5173 is already in use" when you've got a couple of apps going at once.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install -D @bridgerb/port-from-name
9
+ ```
10
+
11
+ ## Use it
12
+
13
+ Add it to `vite.config.ts` and you're done:
14
+
15
+ ```ts
16
+ import { sveltekit } from '@sveltejs/kit/vite';
17
+ import { autoPort } from '@bridgerb/port-from-name';
18
+ import { defineConfig } from 'vite';
19
+
20
+ export default defineConfig({
21
+ plugins: [sveltekit(), autoPort()],
22
+ });
23
+ ```
24
+
25
+ It picks a stable port for both `npm run dev` and `npm run preview`. If two apps ever happen to pick the same one, Vite just bumps to the next free port instead of crashing.
26
+
27
+ ## What ports it uses
28
+
29
+ By default it picks from **3100–4899**. That range is chosen to stay out of the way:
30
+
31
+ - Above the low ports your OS reserves (anything under 1024).
32
+ - Past the usual suspects everything else grabs — 3000 (Node/Next/Rails), 5173 (Vite's default), 5432 (Postgres), and 5000/7000 (AirPlay on Mac).
33
+ - Well below the high ports your machine hands out for its own connections, so you're never fighting it for one.
34
+
35
+ It's also wide enough that running a handful of apps at once almost never picks the same port twice. Want a different range? Set `base` and `range` (see below).
36
+
37
+ ## Just want the number?
38
+
39
+ ```ts
40
+ import { portFromName } from '@bridgerb/port-from-name';
41
+
42
+ const port = portFromName(); // from this project's name
43
+ const port2 = portFromName({ name: 'my-app' });
44
+ ```
45
+
46
+ ## Options
47
+
48
+ ```ts
49
+ autoPort({
50
+ name, // use a custom name instead of package.json's
51
+ base, // lowest port to pick (default 3100)
52
+ range, // how wide the range is (default 1800, so 3100–4899)
53
+ strictPort, // crash instead of bumping on a clash (default false)
54
+ });
55
+ ```
56
+
57
+ ## License
58
+
59
+ [Unlicense](./LICENSE) — public domain.
@@ -0,0 +1,37 @@
1
+ import type { Plugin } from 'vite';
2
+ export interface PortOptions {
3
+ /** Override the project name. Defaults to the "name" field of the nearest package.json. */
4
+ name?: string;
5
+ /** Directory to look for package.json in. Defaults to process.cwd(). */
6
+ cwd?: string;
7
+ /** Lowest port to assign. Default 3100. */
8
+ base?: number;
9
+ /** Width of the port band. Default 1800, so ports land in 3100–4899. */
10
+ range?: number;
11
+ /** Fail instead of incrementing when the port is already taken. Default false. */
12
+ strictPort?: boolean;
13
+ }
14
+ /**
15
+ * Lowest port assigned by default. Sits above the privileged range (<1024) and
16
+ * steps off 3000, the single most contested dev port (Node, Rails, Next, CRA).
17
+ */
18
+ export declare const DEFAULT_BASE = 3100;
19
+ /**
20
+ * Default width of the port band. 3100–4899 clears privileged ports, ends before
21
+ * AirPlay (5000), Vite (5173) and Postgres (5432), stays far below the ephemeral
22
+ * range (Linux 32768+, macOS 49152+), and is wide enough that the birthday-paradox
23
+ * collision odds stay low for the handful of projects you run at once.
24
+ */
25
+ export declare const DEFAULT_RANGE = 1800;
26
+ /**
27
+ * Deterministic port derived from a project name.
28
+ * Same name -> same port, every time, on every machine.
29
+ */
30
+ export declare function portFromName(options?: PortOptions): number;
31
+ /**
32
+ * Vite plugin that pins the dev server and preview ports to portFromName().
33
+ * Drop it in plugins[] and forget about it.
34
+ */
35
+ export declare function autoPort(options?: PortOptions): Plugin;
36
+ export default autoPort;
37
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,MAAM,WAAW,WAAW;IAC1B,2FAA2F;IAC3F,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wEAAwE;IACxE,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wEAAwE;IACxE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,kFAAkF;IAClF,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,aAAa,OAAO,CAAC;AA4BlC;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,WAAgB,GAAG,MAAM,CAI9D;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,OAAO,GAAE,WAAgB,GAAG,MAAM,CAY1D;AAED,eAAe,QAAQ,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { resolve } from 'node:path';
3
+ /**
4
+ * Lowest port assigned by default. Sits above the privileged range (<1024) and
5
+ * steps off 3000, the single most contested dev port (Node, Rails, Next, CRA).
6
+ */
7
+ export const DEFAULT_BASE = 3100;
8
+ /**
9
+ * Default width of the port band. 3100–4899 clears privileged ports, ends before
10
+ * AirPlay (5000), Vite (5173) and Postgres (5432), stays far below the ephemeral
11
+ * range (Linux 32768+, macOS 49152+), and is wide enough that the birthday-paradox
12
+ * collision odds stay low for the handful of projects you run at once.
13
+ */
14
+ export const DEFAULT_RANGE = 1800;
15
+ /** FNV-1a 32-bit hash. Stable across machines, well-distributed, zero deps. */
16
+ function fnv1a(str) {
17
+ let h = 0x811c9dc5; // 2166136261 offset basis
18
+ for (let i = 0; i < str.length; i++) {
19
+ h ^= str.charCodeAt(i);
20
+ h = Math.imul(h, 0x01000193); // 16777619 prime; Math.imul = correct 32-bit multiply
21
+ }
22
+ return h >>> 0;
23
+ }
24
+ function resolveName(options) {
25
+ if (options.name)
26
+ return options.name;
27
+ const pkgPath = resolve(options.cwd ?? process.cwd(), 'package.json');
28
+ let raw;
29
+ try {
30
+ raw = readFileSync(pkgPath, 'utf8');
31
+ }
32
+ catch {
33
+ throw new Error(`[port-from-name] could not read ${pkgPath} — pass { name } explicitly.`);
34
+ }
35
+ const name = JSON.parse(raw).name;
36
+ if (typeof name !== 'string' || name.length === 0) {
37
+ throw new Error(`[port-from-name] no "name" field in ${pkgPath} — pass { name } explicitly.`);
38
+ }
39
+ return name;
40
+ }
41
+ /**
42
+ * Deterministic port derived from a project name.
43
+ * Same name -> same port, every time, on every machine.
44
+ */
45
+ export function portFromName(options = {}) {
46
+ const base = options.base ?? DEFAULT_BASE;
47
+ const range = options.range ?? DEFAULT_RANGE;
48
+ return base + (fnv1a(resolveName(options)) % range);
49
+ }
50
+ /**
51
+ * Vite plugin that pins the dev server and preview ports to portFromName().
52
+ * Drop it in plugins[] and forget about it.
53
+ */
54
+ export function autoPort(options = {}) {
55
+ return {
56
+ name: 'port-from-name',
57
+ config() {
58
+ const port = portFromName(options);
59
+ const strictPort = options.strictPort ?? false;
60
+ return {
61
+ server: { port, strictPort },
62
+ preview: { port, strictPort },
63
+ };
64
+ },
65
+ };
66
+ }
67
+ export default autoPort;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@bridgerb/port-from-name",
3
+ "version": "1.0.0",
4
+ "description": "Deterministic Vite/SvelteKit dev-server port derived from your project name",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": ["dist"],
15
+ "scripts": {
16
+ "build": "tsc",
17
+ "prepublishOnly": "npm run build"
18
+ },
19
+ "keywords": ["vite", "vite-plugin", "sveltekit", "port", "dev-server", "deterministic"],
20
+ "author": "BridgerB",
21
+ "license": "Unlicense",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/BridgerB/port-from-name.git"
25
+ },
26
+ "engines": {
27
+ "node": ">=22"
28
+ },
29
+ "peerDependencies": {
30
+ "vite": ">=5"
31
+ },
32
+ "peerDependenciesMeta": {
33
+ "vite": {
34
+ "optional": true
35
+ }
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^22.0.0",
39
+ "typescript": "^5.9.3",
40
+ "vite": "^7.0.0"
41
+ }
42
+ }