@b12k/porto 0.9.0 → 0.10.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.js CHANGED
@@ -1,63 +1,69 @@
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 REGISTRY_SCHEMA = z.object({
55
+ portsPerSlot: PORT_SCHEMA,
56
+ slots: z.array(RECORD_SCHEMA.nullable()),
57
+ startPort: PORT_SCHEMA
58
+ }).refine(({ portsPerSlot, slots, startPort })=>startPort + Math.max(slots.length, 1) * portsPerSlot - 1 <= 65535, {
59
+ message: 'Port slots must fit in the TCP port range.'
60
+ }).refine(({ slots })=>{
61
+ const records = slots.filter((record)=>null !== record);
62
+ return new Set(records.map((record)=>record.worktreeId)).size === records.length;
59
63
  }, {
60
- message: 'Reserved port blocks must not overlap.'
64
+ message: 'Each checkout can have only one reservation.'
65
+ }).refine(({ portsPerSlot, slots, startPort })=>slots.every((record, index)=>null === record || record.firstPort === startPort + index * portsPerSlot && record.lastPort === record.firstPort + portsPerSlot - 1 && record.vars.PORTO_SLOT === index), {
66
+ message: 'Slot bounds and PORTO_SLOT must match its array index and the repository range.'
61
67
  });
62
68
  function parseOptions(options) {
63
69
  const result = INPUT_OPTIONS_SCHEMA.safeParse(options);
@@ -69,4 +75,4 @@ function parseOptions(options) {
69
75
  }
70
76
  return result.data;
71
77
  }
72
- export { HOOKS_SCHEMA, OPTIONS_SCHEMA, REGISTRY_SCHEMA, parseOptions };
78
+ export { HOOKS_SCHEMA, OPTIONS_SCHEMA, OUTPUT_OPTIONS_SCHEMA, REGISTRY_SCHEMA, parseOptions };
package/dist/vars.d.ts CHANGED
@@ -1,19 +1,26 @@
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 = (slotIndex: number, port: number, index: 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.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][]>>, z.ZodTransform<{
14
+ PORTO_SLOT: number;
15
+ }, {
16
+ [k: string]: string | number;
17
+ }>>;
11
18
  declare const VARS_SCHEMA: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodCustom<VarFactory, VarFactory>]>>;
12
19
  declare function assertVarsFit(varsCount: number, size: number): void;
13
- declare function mapBlockToVars(block: PortBlock, entries: Vars): {
20
+ declare function mapSlotToVars(slot: Pick<PortSlot, 'firstPort' | 'index'>, entries: Vars): {
14
21
  [k: string]: string | number;
15
22
  };
16
23
  declare function parseVarEntries(vars: unknown): (string | VarFactory)[];
17
24
  declare function parseVars(source: string): VarEntry[];
18
- export { assertVarsFit, mapBlockToVars, parseVarEntries, parseVars, VARS_SCHEMA };
25
+ export { assertVarsFit, mapSlotToVars, parseVarEntries, parseVars, VAR_VALUES_SCHEMA, VARS_SCHEMA };
19
26
  export type { VarEntry, VarFactory, Vars, VarsResult };
package/dist/vars.js CHANGED
@@ -13,6 +13,18 @@ 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)).refine((variables)=>{
17
+ const slotIndex = variables['PORTO_SLOT'];
18
+ return 'number' == typeof slotIndex && Number.isSafeInteger(slotIndex) && slotIndex >= 0;
19
+ }, {
20
+ message: 'PORTO_SLOT must be a non-negative integer.',
21
+ path: [
22
+ 'PORTO_SLOT'
23
+ ]
24
+ }).transform((variables)=>({
25
+ ...variables,
26
+ PORTO_SLOT: Number(variables['PORTO_SLOT'])
27
+ }));
16
28
  const VARS_SCHEMA = z.array(z.union([
17
29
  VAR_NAME_SCHEMA,
18
30
  VAR_FACTORY_SCHEMA
@@ -23,10 +35,10 @@ const VARS_SCHEMA = z.array(z.union([
23
35
  message: 'Variable names must be unique.'
24
36
  });
25
37
  function assertVarsFit(varsCount, size) {
26
- if (varsCount > size) throw new PortoError('mappingInvalid', 'Vars contain more entries than the block contains.');
38
+ if (varsCount > size) throw new PortoError('mappingInvalid', 'Vars contain more entries than the slot contains.');
27
39
  }
28
- function mapVarEntry(entry, block, index) {
29
- const port = block.getPort(index);
40
+ function mapVarEntry(entry, slot, index) {
41
+ const port = slot.firstPort + index;
30
42
  if ('string' == typeof entry) return [
31
43
  [
32
44
  entry,
@@ -34,7 +46,7 @@ function mapVarEntry(entry, block, index) {
34
46
  ]
35
47
  ];
36
48
  try {
37
- const variables = entry(block.index, port, index);
49
+ const variables = entry(slot.index, port, index);
38
50
  if (types.isPromise(variables)) variables.catch(()=>{});
39
51
  return VAR_RESULT_SCHEMA.parse(variables);
40
52
  } catch (error) {
@@ -43,9 +55,9 @@ function mapVarEntry(entry, block, index) {
43
55
  });
44
56
  }
45
57
  }
46
- function mapBlockToVars(block, entries) {
58
+ function mapSlotToVars(slot, entries) {
47
59
  const environment = new Map();
48
- for (const [index, entry] of entries.entries())for (const [name, value] of mapVarEntry(entry, block, index)){
60
+ for (const [index, entry] of entries.entries())for (const [name, value] of mapVarEntry(entry, slot, index)){
49
61
  if (environment.has(name)) throw new PortoError('mappingInvalid', `Variable names must be unique. Duplicate: ${name}.`);
50
62
  environment.set(name, value);
51
63
  }
@@ -80,10 +92,10 @@ function parseVars(source) {
80
92
  return groups.map((group)=>{
81
93
  const [firstVar] = group;
82
94
  if (1 === group.length && firstVar && void 0 === firstVar.template) return firstVar.name;
83
- return (block, port)=>Object.fromEntries(group.map(({ name, template })=>[
95
+ return (slotIndex, port)=>Object.fromEntries(group.map(({ name, template })=>[
84
96
  name,
85
- template?.replaceAll('{{block}}', String(block)).replaceAll('{{port}}', String(port)) ?? port
97
+ template?.replaceAll('{{slot}}', String(slotIndex)).replaceAll('{{port}}', String(port)) ?? port
86
98
  ]));
87
99
  });
88
100
  }
89
- export { VARS_SCHEMA, assertVarsFit, mapBlockToVars, parseVarEntries, parseVars };
101
+ 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.10.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 };