@b12k/porto 0.9.0 → 0.11.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/dist/schemas.d.ts CHANGED
@@ -1,8 +1,14 @@
1
1
  import { z } from 'zod';
2
- import type { PortBlock } from './index.js';
2
+ import type { PortSlot } from './index.js';
3
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>;
4
+ onReleased: z.ZodCustom<(slot: PortSlot, ports: Array<number>) => Promise<void> | void, (slot: PortSlot, ports: Array<number>) => Promise<void> | void>;
5
+ onReserved: z.ZodCustom<(slot: PortSlot, ports: Array<number>) => Promise<void> | void, (slot: PortSlot, ports: Array<number>) => Promise<void> | void>;
6
+ }, z.core.$strip>;
7
+ declare const OUTPUT_OPTIONS_SCHEMA: z.ZodObject<{
8
+ env: z.ZodBoolean;
9
+ envFile: z.ZodString;
10
+ jsonFile: z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodString]>;
11
+ silent: z.ZodBoolean;
6
12
  }, z.core.$strip>;
7
13
  declare const OPTIONS_SCHEMA: z.ZodObject<{
8
14
  host: z.ZodOptional<z.ZodString>;
@@ -11,23 +17,56 @@ declare const OPTIONS_SCHEMA: z.ZodObject<{
11
17
  size: z.ZodInt;
12
18
  start: z.ZodInt;
13
19
  }, z.core.$strip>;
14
- declare const REGISTRY_SCHEMA: z.ZodArray<z.ZodObject<{
20
+ declare const RECORD_SCHEMA: z.ZodObject<{
21
+ env: z.ZodBoolean;
22
+ envFile: z.ZodString;
23
+ jsonFile: z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodString]>;
24
+ silent: z.ZodBoolean;
25
+ expiresAt: z.ZodNullable<z.ZodISODateTime>;
26
+ firstPort: z.ZodInt;
15
27
  host: z.ZodOptional<z.ZodString>;
16
28
  idleTtlMinutes: z.ZodNumber;
17
- size: z.ZodInt;
18
- start: z.ZodInt;
19
- index: z.ZodInt;
20
29
  keep: z.ZodBoolean;
30
+ lastPort: z.ZodInt;
21
31
  lastUsedAt: z.ZodISODateTime;
22
32
  ports: z.ZodArray<z.ZodInt>;
23
- worktree: z.ZodString;
33
+ vars: z.ZodPipe<z.ZodPipe<z.ZodPipe<z.ZodCustom<Record<string, unknown>, Record<string, unknown>>, z.ZodTransform<[string, unknown][], Record<string, unknown>>>, z.ZodArray<z.ZodTuple<[z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>], null>>>, z.ZodTransform<{
34
+ [k: string]: string | number;
35
+ }, [string, string | number][]>>;
24
36
  worktreeId: z.ZodString;
25
- }, z.core.$strip>>;
37
+ worktreePath: z.ZodString;
38
+ }, z.core.$strip>;
39
+ type ReservationRecord = z.output<typeof RECORD_SCHEMA>;
40
+ declare const REGISTRY_SCHEMA: z.ZodObject<{
41
+ portsPerSlot: z.ZodInt;
42
+ slots: z.ZodRecord<z.ZodCoercedNumber<unknown>, z.ZodObject<{
43
+ env: z.ZodBoolean;
44
+ envFile: z.ZodString;
45
+ jsonFile: z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodString]>;
46
+ silent: z.ZodBoolean;
47
+ expiresAt: z.ZodNullable<z.ZodISODateTime>;
48
+ firstPort: z.ZodInt;
49
+ host: z.ZodOptional<z.ZodString>;
50
+ idleTtlMinutes: z.ZodNumber;
51
+ keep: z.ZodBoolean;
52
+ lastPort: z.ZodInt;
53
+ lastUsedAt: z.ZodISODateTime;
54
+ ports: z.ZodArray<z.ZodInt>;
55
+ vars: z.ZodPipe<z.ZodPipe<z.ZodPipe<z.ZodCustom<Record<string, unknown>, Record<string, unknown>>, z.ZodTransform<[string, unknown][], Record<string, unknown>>>, z.ZodArray<z.ZodTuple<[z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>], null>>>, z.ZodTransform<{
56
+ [k: string]: string | number;
57
+ }, [string, string | number][]>>;
58
+ worktreeId: z.ZodString;
59
+ worktreePath: z.ZodString;
60
+ }, z.core.$strip>>;
61
+ startPort: z.ZodInt;
62
+ }, z.core.$strip>;
63
+ type Registry = z.output<typeof REGISTRY_SCHEMA>;
26
64
  declare function parseOptions(options: unknown): {
27
65
  host?: string | undefined;
28
- idleTtlMinutes: number;
66
+ idleTtlMinutes?: number | undefined;
29
67
  keep?: boolean | undefined;
30
- size: number;
31
- start: number;
68
+ size?: number | undefined;
69
+ start?: number | undefined;
32
70
  };
33
- export { HOOKS_SCHEMA, OPTIONS_SCHEMA, parseOptions, REGISTRY_SCHEMA };
71
+ export { HOOKS_SCHEMA, OPTIONS_SCHEMA, OUTPUT_OPTIONS_SCHEMA, parseOptions, REGISTRY_SCHEMA };
72
+ export type { Registry, ReservationRecord };
package/dist/schemas.js CHANGED
@@ -1,63 +1,79 @@
1
1
  import node_path from "node:path";
2
2
  import { z } from "zod";
3
3
  import { PortoError } from "./error.js";
4
- const BLOCK_HOOK_SCHEMA = z.custom((hook)=>'function' == typeof hook);
4
+ import { VAR_VALUES_SCHEMA } from "./vars.js";
5
+ const SLOT_HOOK_SCHEMA = z.custom((hook)=>'function' == typeof hook);
5
6
  const HOOKS_SCHEMA = z.object({
6
- onReleased: BLOCK_HOOK_SCHEMA,
7
- onReserved: BLOCK_HOOK_SCHEMA
7
+ onReleased: SLOT_HOOK_SCHEMA,
8
+ onReserved: SLOT_HOOK_SCHEMA
8
9
  });
10
+ const OUTPUT_OPTIONS_SCHEMA = z.object({
11
+ env: z.boolean(),
12
+ envFile: z.string().min(1),
13
+ jsonFile: z.union([
14
+ z.literal(false),
15
+ z.string().min(1)
16
+ ]),
17
+ silent: z.boolean()
18
+ });
19
+ const PORT_SCHEMA = z.int().min(1).max(65535);
9
20
  const OPTIONS_SCHEMA = z.object({
10
21
  host: z.string().trim().min(1).optional(),
11
22
  idleTtlMinutes: z.number().positive(),
12
23
  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
- ]
24
+ size: PORT_SCHEMA,
25
+ start: PORT_SCHEMA
20
26
  });
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(),
27
+ const INPUT_OPTIONS_SCHEMA = OPTIONS_SCHEMA.partial();
28
+ const RECORD_SCHEMA = z.object({
29
+ ...OUTPUT_OPTIONS_SCHEMA.shape,
30
+ expiresAt: z.iso.datetime().nullable(),
31
+ firstPort: PORT_SCHEMA,
32
+ host: OPTIONS_SCHEMA.shape.host,
33
+ idleTtlMinutes: OPTIONS_SCHEMA.shape.idleTtlMinutes,
29
34
  keep: z.boolean(),
35
+ lastPort: PORT_SCHEMA,
30
36
  lastUsedAt: z.iso.datetime(),
31
- ports: z.array(z.int().min(1).max(65535)).refine((ports)=>new Set(ports).size === ports.length, {
37
+ ports: z.array(z.int()).refine((ports)=>new Set(ports).size === ports.length, {
32
38
  message: 'Assigned ports must be unique.'
33
39
  }),
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.',
40
+ vars: VAR_VALUES_SCHEMA,
41
+ worktreeId: z.string().min(1),
42
+ worktreePath: z.string().refine((worktreePath)=>node_path.isAbsolute(worktreePath))
43
+ }).refine((record)=>record.ports.every((port)=>port >= record.firstPort && port <= record.lastPort), {
44
+ message: 'Assigned ports must belong to the slot.',
38
45
  path: [
39
- 'index'
46
+ 'ports'
40
47
  ]
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.',
48
+ }).refine((record)=>record.keep === (null === record.expiresAt), {
49
+ message: 'Kept slots must have no expiry time.',
43
50
  path: [
44
- 'ports'
51
+ 'expiresAt'
45
52
  ]
46
53
  });
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
- });
54
+ const SLOTS_SCHEMA = z.record(z.coerce.number().int().nonnegative(), RECORD_SCHEMA).refine((slots)=>Object.keys(slots).every((key)=>key === String(Number(key))), {
55
+ message: 'Slot keys must be canonical non-negative integer strings.'
56
+ });
57
+ const REGISTRY_SCHEMA = z.object({
58
+ portsPerSlot: PORT_SCHEMA,
59
+ slots: SLOTS_SCHEMA,
60
+ startPort: PORT_SCHEMA
61
+ }).refine(({ portsPerSlot, slots, startPort })=>{
62
+ const keys = Object.keys(slots).map(Number);
63
+ const usedSlots = 0 === keys.length ? 1 : Math.max(...keys) + 1;
64
+ return startPort + usedSlots * portsPerSlot - 1 <= 65535;
65
+ }, {
66
+ message: 'Port slots must fit in the TCP port range.'
67
+ }).refine(({ slots })=>{
68
+ const records = Object.values(slots);
69
+ return new Set(records.map((record)=>record.worktreeId)).size === records.length;
59
70
  }, {
60
- message: 'Reserved port blocks must not overlap.'
71
+ message: 'Each worktree can have only one slot.'
72
+ }).refine(({ portsPerSlot, slots, startPort })=>Object.entries(slots).every(([key, record])=>{
73
+ const index = Number(key);
74
+ return record.firstPort === startPort + index * portsPerSlot && record.lastPort === record.firstPort + portsPerSlot - 1;
75
+ }), {
76
+ message: 'Slot bounds must match its slot index and the repository range.'
61
77
  });
62
78
  function parseOptions(options) {
63
79
  const result = INPUT_OPTIONS_SCHEMA.safeParse(options);
@@ -69,4 +85,4 @@ function parseOptions(options) {
69
85
  }
70
86
  return result.data;
71
87
  }
72
- export { HOOKS_SCHEMA, OPTIONS_SCHEMA, REGISTRY_SCHEMA, parseOptions };
88
+ export { HOOKS_SCHEMA, OPTIONS_SCHEMA, OUTPUT_OPTIONS_SCHEMA, REGISTRY_SCHEMA, parseOptions };
package/dist/vars.d.ts CHANGED
@@ -1,19 +1,22 @@
1
1
  import { z } from 'zod';
2
- import type { PortBlock } from './index.js';
2
+ import type { PortSlot } from './index.js';
3
3
  type VarEntry = string | VarFactory;
4
4
  type VarEntryResult<Entry extends VarEntry> = Entry extends string ? string extends Entry ? Partial<Record<string, number>> : Record<Entry, number> : Entry extends VarFactory ? ReturnType<Entry> : never;
5
- type VarFactory = (block: number, port: number, index: number) => Record<string, number | string>;
5
+ type VarFactory = (port: number, slot: number) => Record<string, number | string>;
6
6
  type Vars = ReadonlyArray<VarEntry>;
7
7
  type VarsResult<Entries extends Vars> = Entries extends readonly [
8
8
  infer Entry extends VarEntry,
9
9
  ...infer Rest extends Vars
10
10
  ] ? VarEntryResult<Entry> & VarsResult<Rest> : number extends Entries['length'] ? Partial<Record<string, number | string>> : object;
11
+ declare const VAR_VALUES_SCHEMA: z.ZodPipe<z.ZodPipe<z.ZodPipe<z.ZodCustom<Record<string, unknown>, Record<string, unknown>>, z.ZodTransform<[string, unknown][], Record<string, unknown>>>, z.ZodArray<z.ZodTuple<[z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>], null>>>, z.ZodTransform<{
12
+ [k: string]: string | number;
13
+ }, [string, string | number][]>>;
11
14
  declare const VARS_SCHEMA: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodCustom<VarFactory, VarFactory>]>>;
12
15
  declare function assertVarsFit(varsCount: number, size: number): void;
13
- declare function mapBlockToVars(block: PortBlock, entries: Vars): {
16
+ declare function mapSlotToVars(slot: Pick<PortSlot, 'firstPort' | 'index'>, entries: Vars): {
14
17
  [k: string]: string | number;
15
18
  };
16
19
  declare function parseVarEntries(vars: unknown): (string | VarFactory)[];
17
20
  declare function parseVars(source: string): VarEntry[];
18
- export { assertVarsFit, mapBlockToVars, parseVarEntries, parseVars, VARS_SCHEMA };
21
+ export { assertVarsFit, mapSlotToVars, parseVarEntries, parseVars, VAR_VALUES_SCHEMA, VARS_SCHEMA };
19
22
  export type { VarEntry, VarFactory, Vars, VarsResult };
package/dist/vars.js CHANGED
@@ -13,6 +13,7 @@ const VAR_RESULT_ENTRY_SCHEMA = z.tuple([
13
13
  ])
14
14
  ]);
15
15
  const VAR_RESULT_SCHEMA = z.custom(isPlainObject).transform((variables)=>Object.entries(variables)).pipe(z.array(VAR_RESULT_ENTRY_SCHEMA));
16
+ const VAR_VALUES_SCHEMA = VAR_RESULT_SCHEMA.transform((entries)=>Object.fromEntries(entries));
16
17
  const VARS_SCHEMA = z.array(z.union([
17
18
  VAR_NAME_SCHEMA,
18
19
  VAR_FACTORY_SCHEMA
@@ -23,10 +24,10 @@ const VARS_SCHEMA = z.array(z.union([
23
24
  message: 'Variable names must be unique.'
24
25
  });
25
26
  function assertVarsFit(varsCount, size) {
26
- if (varsCount > size) throw new PortoError('mappingInvalid', 'Vars contain more entries than the block contains.');
27
+ if (varsCount > size) throw new PortoError('mappingInvalid', 'Vars contain more entries than the slot contains.');
27
28
  }
28
- function mapVarEntry(entry, block, index) {
29
- const port = block.getPort(index);
29
+ function mapVarEntry(entry, slot, index) {
30
+ const port = slot.firstPort + index;
30
31
  if ('string' == typeof entry) return [
31
32
  [
32
33
  entry,
@@ -34,8 +35,11 @@ function mapVarEntry(entry, block, index) {
34
35
  ]
35
36
  ];
36
37
  try {
37
- const variables = entry(block.index, port, index);
38
- if (types.isPromise(variables)) variables.catch(()=>{});
38
+ const variables = entry(port, slot.index);
39
+ if (types.isPromise(variables)) {
40
+ variables.catch(()=>{});
41
+ throw new PortoError('mappingInvalid', `Cannot create variables for entry ${index + 1}; async factories are not supported, return a plain object.`);
42
+ }
39
43
  return VAR_RESULT_SCHEMA.parse(variables);
40
44
  } catch (error) {
41
45
  throw new PortoError('mappingInvalid', `Cannot create variables for entry ${index + 1}; expected an object with valid names and single-line strings or finite numbers.`, {
@@ -43,21 +47,21 @@ function mapVarEntry(entry, block, index) {
43
47
  });
44
48
  }
45
49
  }
46
- function mapBlockToVars(block, entries) {
50
+ function mapSlotToVars(slot, entries) {
47
51
  const environment = new Map();
48
- for (const [index, entry] of entries.entries())for (const [name, value] of mapVarEntry(entry, block, index)){
52
+ for (const [index, entry] of entries.entries())for (const [name, value] of mapVarEntry(entry, slot, index)){
49
53
  if (environment.has(name)) throw new PortoError('mappingInvalid', `Variable names must be unique. Duplicate: ${name}.`);
50
54
  environment.set(name, value);
51
55
  }
52
56
  return Object.fromEntries(environment);
53
57
  }
54
- function parseVar(entry) {
55
- const separator = entry.indexOf('=');
56
- const name = (-1 === separator ? entry : entry.slice(0, separator)).trim();
58
+ function parseVar(source) {
59
+ const separator = source.indexOf('=');
60
+ const name = (-1 === separator ? source : source.slice(0, separator)).trim();
57
61
  if (-1 === separator) return {
58
62
  name
59
63
  };
60
- const template = entry.slice(separator + 1);
64
+ const template = source.slice(separator + 1);
61
65
  const result = VAR_TEXT_SCHEMA.safeParse(template);
62
66
  if (!result.success) throw new PortoError('mappingInvalid', 'Vars templates must fit on one line.', {
63
67
  cause: result.error
@@ -80,10 +84,10 @@ function parseVars(source) {
80
84
  return groups.map((group)=>{
81
85
  const [firstVar] = group;
82
86
  if (1 === group.length && firstVar && void 0 === firstVar.template) return firstVar.name;
83
- return (block, port)=>Object.fromEntries(group.map(({ name, template })=>[
87
+ return (port, slot)=>Object.fromEntries(group.map(({ name, template })=>[
84
88
  name,
85
- template?.replaceAll('{{block}}', String(block)).replaceAll('{{port}}', String(port)) ?? port
89
+ template?.replaceAll('{{slot}}', String(slot)).replaceAll('{{port}}', String(port)) ?? port
86
90
  ]));
87
91
  });
88
92
  }
89
- export { VARS_SCHEMA, assertVarsFit, mapBlockToVars, parseVarEntries, parseVars };
93
+ export { VARS_SCHEMA, VAR_VALUES_SCHEMA, assertVarsFit, mapSlotToVars, parseVarEntries, parseVars };
package/package.json CHANGED
@@ -1,12 +1,29 @@
1
1
  {
2
2
  "name": "@b12k/porto",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "Port range allocator for git worktrees",
5
5
  "keywords": [
6
6
  "cli",
7
+ "dev-server",
8
+ "dev-tools",
9
+ "dotenv",
10
+ "environment-variables",
11
+ "free-port",
7
12
  "git",
13
+ "git-worktree",
14
+ "local-dev",
15
+ "local-development",
16
+ "localhost",
17
+ "parallel-development",
18
+ "port",
19
+ "port-allocation",
20
+ "port-conflicts",
21
+ "port-range",
22
+ "port-reservation",
8
23
  "ports",
9
- "worktree"
24
+ "tcp",
25
+ "worktree",
26
+ "worktrees"
10
27
  ],
11
28
  "license": "MIT",
12
29
  "author": "Bogdan Kolesnyk",
@@ -37,7 +54,6 @@
37
54
  "cosmiconfig": "10.0.1",
38
55
  "date-fns": "4.4.0",
39
56
  "es-toolkit": "1.53.2",
40
- "jiti": "2.7.0",
41
57
  "proper-lockfile": "4.1.2",
42
58
  "update-notifier": "7.3.1",
43
59
  "zod": "4.6.7"
@@ -1 +0,0 @@
1
- export declare function loadConfigFile(filePath: string): Promise<unknown>;
@@ -1,17 +0,0 @@
1
- import { cosmiconfig } from "cosmiconfig";
2
- import { createJiti } from "jiti";
3
- function loadTypeScriptConfig(filePath) {
4
- return createJiti(import.meta.url).import(filePath, {
5
- default: true
6
- });
7
- }
8
- async function loadConfigFile(filePath) {
9
- const result = await cosmiconfig('porto', {
10
- loaders: {
11
- '.ts': loadTypeScriptConfig
12
- }
13
- }).load(filePath);
14
- const config = result?.config;
15
- return config;
16
- }
17
- export { loadConfigFile };