@b12k/porto 0.0.0-stage → 0.2.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,33 @@
1
+ import { z } from 'zod';
2
+ import type { PortBlock } from './index.js';
3
+ declare const HOOKS_SCHEMA: z.ZodObject<{
4
+ onReleased: z.ZodCustom<(block: PortBlock, ports: Array<number>) => Promise<void> | void, (block: PortBlock, ports: Array<number>) => Promise<void> | void>;
5
+ onReserved: z.ZodCustom<(block: PortBlock, ports: Array<number>) => Promise<void> | void, (block: PortBlock, ports: Array<number>) => Promise<void> | void>;
6
+ }, z.core.$strip>;
7
+ declare const OPTIONS_SCHEMA: z.ZodObject<{
8
+ host: z.ZodOptional<z.ZodString>;
9
+ idleTtlMinutes: z.ZodNumber;
10
+ keep: z.ZodOptional<z.ZodBoolean>;
11
+ size: z.ZodInt;
12
+ start: z.ZodInt;
13
+ }, z.core.$strip>;
14
+ declare const REGISTRY_SCHEMA: z.ZodArray<z.ZodObject<{
15
+ host: z.ZodOptional<z.ZodString>;
16
+ idleTtlMinutes: z.ZodNumber;
17
+ size: z.ZodInt;
18
+ start: z.ZodInt;
19
+ index: z.ZodInt;
20
+ keep: z.ZodBoolean;
21
+ lastUsedAt: z.ZodISODateTime;
22
+ ports: z.ZodArray<z.ZodInt>;
23
+ worktree: z.ZodString;
24
+ worktreeId: z.ZodString;
25
+ }, z.core.$strip>>;
26
+ declare function parseOptions(options: unknown): {
27
+ host?: string | undefined;
28
+ idleTtlMinutes: number;
29
+ keep?: boolean | undefined;
30
+ size: number;
31
+ start: number;
32
+ };
33
+ export { HOOKS_SCHEMA, OPTIONS_SCHEMA, parseOptions, REGISTRY_SCHEMA };
@@ -0,0 +1,72 @@
1
+ import node_path from "node:path";
2
+ import { z } from "zod";
3
+ import { PortoError } from "./error.js";
4
+ const BLOCK_HOOK_SCHEMA = z.custom((hook)=>'function' == typeof hook);
5
+ const HOOKS_SCHEMA = z.object({
6
+ onReleased: BLOCK_HOOK_SCHEMA,
7
+ onReserved: BLOCK_HOOK_SCHEMA
8
+ });
9
+ const OPTIONS_SCHEMA = z.object({
10
+ host: z.string().trim().min(1).optional(),
11
+ idleTtlMinutes: z.number().positive(),
12
+ keep: z.boolean().optional(),
13
+ size: z.int().min(1).max(65535),
14
+ start: z.int().min(1).max(65535)
15
+ }).refine((options)=>options.start + options.size - 1 <= 65535, {
16
+ message: 'size must fit in the TCP port range.',
17
+ path: [
18
+ 'size'
19
+ ]
20
+ });
21
+ const INPUT_OPTIONS_SCHEMA = z.object({
22
+ ...OPTIONS_SCHEMA.shape,
23
+ idleTtlMinutes: OPTIONS_SCHEMA.shape.idleTtlMinutes.default(60),
24
+ size: OPTIONS_SCHEMA.shape.size.default(10),
25
+ start: OPTIONS_SCHEMA.shape.start.default(10000)
26
+ }).pipe(OPTIONS_SCHEMA);
27
+ const RECORD_SCHEMA = OPTIONS_SCHEMA.safeExtend({
28
+ index: z.int().nonnegative(),
29
+ keep: z.boolean(),
30
+ lastUsedAt: z.iso.datetime(),
31
+ ports: z.array(z.int().min(1).max(65535)).refine((ports)=>new Set(ports).size === ports.length, {
32
+ message: 'Assigned ports must be unique.'
33
+ }),
34
+ worktree: z.string().refine((worktree)=>node_path.isAbsolute(worktree)),
35
+ worktreeId: z.string().min(1)
36
+ }).refine((record)=>record.start + (record.index + 1) * record.size - 1 <= 65535, {
37
+ message: 'Block must fit in the TCP port range.',
38
+ path: [
39
+ 'index'
40
+ ]
41
+ }).refine((record)=>record.ports.every((port)=>port >= record.start + record.index * record.size && port < record.start + (record.index + 1) * record.size), {
42
+ message: 'Assigned ports must belong to the block.',
43
+ path: [
44
+ 'ports'
45
+ ]
46
+ });
47
+ const REGISTRY_SCHEMA = z.array(RECORD_SCHEMA).refine((records)=>new Set(records.map((record)=>JSON.stringify([
48
+ record.worktreeId,
49
+ record.start,
50
+ record.size
51
+ ]))).size === records.length, {
52
+ message: 'Each checkout can have only one reservation per range.'
53
+ }).refine((records)=>{
54
+ const sortedRecords = records.toSorted((first, second)=>first.start + first.index * first.size - second.start - second.index * second.size);
55
+ return sortedRecords.every((record, index)=>{
56
+ const previousRecord = sortedRecords[index - 1];
57
+ return !previousRecord || previousRecord.start + (previousRecord.index + 1) * previousRecord.size <= record.start + record.index * record.size;
58
+ });
59
+ }, {
60
+ message: 'Reserved port blocks must not overlap.'
61
+ });
62
+ function parseOptions(options) {
63
+ const result = INPUT_OPTIONS_SCHEMA.safeParse(options);
64
+ if (!result.success) {
65
+ const details = result.error.issues.map((issue)=>`${issue.path.join('.')}: ${issue.message}`).join('; ');
66
+ throw new PortoError('optionsInvalid', details, {
67
+ cause: result.error
68
+ });
69
+ }
70
+ return result.data;
71
+ }
72
+ export { HOOKS_SCHEMA, OPTIONS_SCHEMA, REGISTRY_SCHEMA, parseOptions };
package/dist/vars.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { z } from 'zod';
2
+ import type { PortBlock } from './index.js';
3
+ type VarEntry = string | VarFactory;
4
+ type VarFactory = (block: number, port: number, index: number) => Record<string, number | string>;
5
+ type Vars = Array<VarEntry>;
6
+ declare const VARS_SCHEMA: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodCustom<VarFactory, VarFactory>]>>;
7
+ declare function assertVarsFit(varsCount: number, size: number): void;
8
+ declare function mapBlockToVars(block: PortBlock, entries: Vars): {
9
+ [k: string]: string | number;
10
+ };
11
+ declare function parseVarEntries(vars: unknown): (string | VarFactory)[];
12
+ declare function parseVars(source: string): VarEntry[];
13
+ export { assertVarsFit, mapBlockToVars, parseVarEntries, parseVars, VARS_SCHEMA };
14
+ export type { VarEntry, VarFactory, Vars };
package/dist/vars.js ADDED
@@ -0,0 +1,87 @@
1
+ import { isPlainObject } from "es-toolkit";
2
+ import { types } from "node:util";
3
+ import { z } from "zod";
4
+ import { PortoError } from "./error.js";
5
+ const VAR_NAME_SCHEMA = z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/u);
6
+ const VAR_TEXT_SCHEMA = z.string().regex(/^[^\r\n\0]*$/u);
7
+ const VAR_FACTORY_SCHEMA = z.custom((entry)=>'function' == typeof entry);
8
+ const VAR_RESULT_ENTRY_SCHEMA = z.tuple([
9
+ VAR_NAME_SCHEMA,
10
+ z.union([
11
+ VAR_TEXT_SCHEMA,
12
+ z.number()
13
+ ])
14
+ ]);
15
+ const VAR_RESULT_SCHEMA = z.custom(isPlainObject).transform((variables)=>Object.entries(variables)).pipe(z.array(VAR_RESULT_ENTRY_SCHEMA));
16
+ const VARS_SCHEMA = z.array(z.union([
17
+ VAR_NAME_SCHEMA,
18
+ VAR_FACTORY_SCHEMA
19
+ ])).refine((entries)=>{
20
+ const names = entries.filter((entry)=>'string' == typeof entry);
21
+ return new Set(names).size === names.length;
22
+ }, {
23
+ message: 'Variable names must be unique.'
24
+ });
25
+ function assertVarsFit(varsCount, size) {
26
+ if (varsCount > size) throw new PortoError('mappingInvalid', 'Vars contain more entries than the block contains.');
27
+ }
28
+ function mapVarEntry(entry, block, index) {
29
+ const port = block.getPort(index);
30
+ if ('string' == typeof entry) return [
31
+ [
32
+ entry,
33
+ port
34
+ ]
35
+ ];
36
+ try {
37
+ const variables = entry(block.index, port, index);
38
+ if (types.isPromise(variables)) variables.catch(()=>{});
39
+ return VAR_RESULT_SCHEMA.parse(variables);
40
+ } catch (error) {
41
+ throw new PortoError('mappingInvalid', `Cannot create variables for entry ${index + 1}; expected an object with valid names and single-line strings or finite numbers.`, {
42
+ cause: error
43
+ });
44
+ }
45
+ }
46
+ function mapBlockToVars(block, entries) {
47
+ const environment = new Map();
48
+ for (const [index, entry] of entries.entries())for (const [name, value] of mapVarEntry(entry, block, index)){
49
+ if (environment.has(name)) throw new PortoError('mappingInvalid', `Variable names must be unique. Duplicate: ${name}.`);
50
+ environment.set(name, value);
51
+ }
52
+ return Object.fromEntries(environment);
53
+ }
54
+ function parseVar(entry) {
55
+ const separator = entry.indexOf(':');
56
+ const name = (-1 === separator ? entry : entry.slice(0, separator)).trim();
57
+ if (-1 === separator) return {
58
+ name
59
+ };
60
+ const template = entry.slice(separator + 1);
61
+ const result = VAR_TEXT_SCHEMA.safeParse(template);
62
+ if (!result.success) throw new PortoError('mappingInvalid', 'Vars templates must fit on one line.', {
63
+ cause: result.error
64
+ });
65
+ return {
66
+ name,
67
+ template
68
+ };
69
+ }
70
+ function parseVarEntries(vars) {
71
+ const result = VARS_SCHEMA.safeParse(vars);
72
+ if (!result.success) throw new PortoError('mappingInvalid', result.error.issues.map((issue)=>issue.message).join('; '), {
73
+ cause: result.error
74
+ });
75
+ return result.data;
76
+ }
77
+ function parseVars(source) {
78
+ const entries = source.split(';').map((entry)=>parseVar(entry));
79
+ parseVarEntries(entries.map(({ name })=>name));
80
+ return entries.map(({ name, template })=>{
81
+ if (void 0 === template) return name;
82
+ return (block, port)=>({
83
+ [name]: template.replaceAll('{{block}}', String(block)).replaceAll('{{port}}', String(port))
84
+ });
85
+ });
86
+ }
87
+ export { VARS_SCHEMA, assertVarsFit, mapBlockToVars, parseVarEntries, parseVars };
package/package.json CHANGED
@@ -1,6 +1,96 @@
1
1
  {
2
2
  "name": "@b12k/porto",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Port range allocator for git worktrees",
5
+ "keywords": [
6
+ "cli",
7
+ "git",
8
+ "ports",
9
+ "worktree"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "Bogdan Kolesnyk",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/b12k/porto.git"
16
+ },
17
+ "bin": {
18
+ "porto": "dist/cli.js"
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "type": "module",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js",
28
+ "require": "./dist/index.js"
29
+ }
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "build": "rslib",
36
+ "check": "run-p -l check:*",
37
+ "check:format": "oxfmt . --check",
38
+ "check:scripts": "oxlint",
39
+ "check:types": "tsc --noEmit",
40
+ "check:tests": "rstest run",
41
+ "deps:check": "ncu -e 2 -c 1",
42
+ "deps:update": "ncu -du --doctorInstall \"pnpm deps:update:install\" --doctorTest \"pnpm deps:update:doctor\" -c 1",
43
+ "deps:update:doctor": "run-p -l check:types check:scripts check:tests build",
44
+ "deps:update:install": "pnpm i --no-frozen-lockfile",
45
+ "dev": "rslib --watch",
46
+ "fix": "run-s -l fix:*",
47
+ "fix:format": "oxfmt . --write",
48
+ "fix:scripts": "oxlint --fix",
49
+ "ga:notify": "tsx .github/notify.ts",
50
+ "husky:commit-msg": "commitlint",
51
+ "husky:pre-commit": "lint-staged",
52
+ "husky:pre-push": "branch-name-lint",
53
+ "prepack": "run-p check build",
54
+ "prepare": "husky",
55
+ "release": "release-it",
56
+ "test": "rstest run"
57
+ },
58
+ "dependencies": {
59
+ "boxen": "9.0.0",
60
+ "commander": "15.1.0",
61
+ "cosmiconfig": "10.0.1",
62
+ "date-fns": "4.4.0",
63
+ "es-toolkit": "1.53.2",
64
+ "jiti": "2.7.0",
65
+ "proper-lockfile": "4.1.2",
66
+ "update-notifier": "7.3.1",
67
+ "zod": "4.6.7"
68
+ },
69
+ "devDependencies": {
70
+ "@b12k/branch-name-lint": "1.3.0",
71
+ "@commitlint/config-conventional": "21.2.4",
72
+ "@release-it/conventional-changelog": "12.0.2",
73
+ "@rslib/core": "1.0.7",
74
+ "@rstest/core": "0.12.6",
75
+ "@types/node": "26.6.6",
76
+ "@types/proper-lockfile": "4.1.4",
77
+ "@types/update-notifier": "6.0.9",
78
+ "commitlint": "21.2.4",
79
+ "eslint-plugin-perfectionist": "5.13.0",
80
+ "husky": "9.1.7",
81
+ "lint-staged": "17.6.0",
82
+ "npm-check-updates": "23.1.1",
83
+ "npm-run-all2": "9.0.4",
84
+ "oxfmt": "0.78.0",
85
+ "oxlint": "1.93.0",
86
+ "oxlint-tsgolint": "7.0.2007",
87
+ "release-it": "21.1.2",
88
+ "tsx": "4.23.15",
89
+ "type-fest": "5.10.0",
90
+ "typescript": "7.0.2"
91
+ },
92
+ "engines": {
93
+ "node": ">=24"
94
+ },
95
+ "packageManager": "pnpm@12.18.0"
96
+ }