@hop-top/kit 0.5.0-alpha.12 → 0.5.0-alpha.13

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.
Files changed (93) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +5 -5
  3. package/dist/bus-D2bAOPYz.d.ts +55 -0
  4. package/dist/bus.d.ts +1 -0
  5. package/dist/bus.js +13 -0
  6. package/dist/chunk-74E6RNOI.js +82 -0
  7. package/dist/chunk-B7XTFDBB.js +17 -0
  8. package/dist/chunk-CAONBSPO.js +497 -0
  9. package/dist/chunk-F7RSL4N4.js +88 -0
  10. package/dist/chunk-I6XDPSXQ.js +75 -0
  11. package/dist/chunk-IDM6YZXN.js +204 -0
  12. package/dist/chunk-VKQCOVKI.js +880 -0
  13. package/dist/cli.js +3 -3
  14. package/dist/index-BqZ7WGQf.d.ts +731 -0
  15. package/dist/index.d.ts +4 -1
  16. package/dist/index.js +27 -13
  17. package/dist/upgrade/drivers/configfile.d.ts +45 -0
  18. package/dist/upgrade/drivers/configfile.js +78 -0
  19. package/dist/upgrade/drivers/fsdir.d.ts +61 -0
  20. package/dist/upgrade/drivers/fsdir.js +170 -0
  21. package/dist/upgrade/drivers/postgres.d.ts +121 -0
  22. package/dist/upgrade/drivers/postgres.js +195 -0
  23. package/dist/upgrade/drivers/sqlite.d.ts +101 -0
  24. package/dist/upgrade/drivers/sqlite.js +241 -0
  25. package/dist/upgrade/index.d.ts +2 -0
  26. package/dist/upgrade/index.js +73 -0
  27. package/dist/uri-IJ_AoY3d.d.ts +144 -0
  28. package/dist/uri.d.ts +1 -0
  29. package/dist/uri.js +37 -0
  30. package/dist/version-DEJ3pbiO.d.ts +10 -0
  31. package/dist/wizard-Bf_qHc31.d.ts +159 -0
  32. package/dist/wizard.d.ts +1 -0
  33. package/dist/wizard.js +39 -0
  34. package/package.json +74 -15
  35. package/skills/hop-top-kit-ts/.claude-plugin/plugin.json +19 -0
  36. package/skills/hop-top-kit-ts/SKILL.md +117 -0
  37. package/skills/hop-top-kit-ts/assets/snippets/mcp/mcp.expected +2 -0
  38. package/skills/hop-top-kit-ts/assets/snippets/mcp/mcp.ts +20 -0
  39. package/skills/hop-top-kit-ts/assets/snippets/quickstart/quickstart.expected +13 -0
  40. package/skills/hop-top-kit-ts/assets/snippets/quickstart/quickstart.ts +26 -0
  41. package/skills/hop-top-kit-ts/gym.json +5 -0
  42. package/skills/hop-top-kit-ts/references/ai-and-agents.md +45 -0
  43. package/skills/hop-top-kit-ts/references/cli-and-console.md +63 -0
  44. package/skills/hop-top-kit-ts/references/coverage.md +56 -0
  45. package/skills/hop-top-kit-ts/references/guardrails-and-trust.md +60 -0
  46. package/skills/hop-top-kit-ts/references/other-ports.md +31 -0
  47. package/skills/hop-top-kit-ts/references/serving-and-transports.md +49 -0
  48. package/skills/hop-top-kit-ts/scripts/detect-port.sh +168 -0
  49. package/src/index.test.ts +48 -2
  50. package/src/index.ts +6 -1
  51. package/src/published-types.test.ts +8 -0
  52. package/src/serve.stopped-order.test.ts +156 -0
  53. package/src/upgrade/README.md +42 -0
  54. package/src/upgrade/backup.ts +174 -0
  55. package/src/upgrade/conformance.test.ts +310 -0
  56. package/src/upgrade/document.ts +229 -0
  57. package/src/upgrade/driver.ts +66 -0
  58. package/src/upgrade/drivers/README.md +47 -0
  59. package/src/upgrade/drivers/configfile.test.ts +112 -0
  60. package/src/upgrade/drivers/configfile.ts +107 -0
  61. package/src/upgrade/drivers/fsdir.test.ts +188 -0
  62. package/src/upgrade/drivers/fsdir.ts +216 -0
  63. package/src/upgrade/drivers/isolation.test.ts +18 -0
  64. package/src/upgrade/drivers/postgres.integration.test.ts +395 -0
  65. package/src/upgrade/drivers/postgres.test.ts +272 -0
  66. package/src/upgrade/drivers/postgres.ts +284 -0
  67. package/src/upgrade/drivers/sqlite.integration.test.ts +492 -0
  68. package/src/upgrade/drivers/sqlite.ts +300 -0
  69. package/src/upgrade/drivers/version.ts +21 -0
  70. package/src/upgrade/errors.ts +117 -0
  71. package/src/upgrade/events.ts +117 -0
  72. package/src/{upgrade.test.ts → upgrade/index.test.ts} +1 -1
  73. package/src/{upgrade.ts → upgrade/index.ts} +20 -0
  74. package/src/upgrade/migration.ts +89 -0
  75. package/src/upgrade/migrator.test.ts +391 -0
  76. package/src/upgrade/migrator.ts +527 -0
  77. package/src/upgrade/options.ts +59 -0
  78. package/src/upgrade/semver.ts +90 -0
  79. package/test/skill-snippets.test.ts +60 -0
  80. package/test/upgrade/conformance-migrator.ts +439 -0
  81. package/test/upgrade/conformance-support.ts +44 -0
  82. package/test/upgrade/driver-support.ts +44 -0
  83. package/test/upgrade/pg-container.ts +64 -0
  84. package/test/upgrade/pg-race-worker.ts +48 -0
  85. package/test/upgrade/race-helper.ts +36 -0
  86. package/test/upgrade/sqlite-support.ts +41 -0
  87. package/test/upgrade/testdata/go-fsdir-mydata.tar.gz +0 -0
  88. package/vitest.config.ts +26 -0
  89. package/dist/chunk-X4E5QGEB.js +0 -171
  90. package/dist/upgrade-BJ2z6Oqf.d.ts +0 -156
  91. package/dist/upgrade.d.ts +0 -1
  92. package/dist/upgrade.js +0 -9
  93. package/dist/{chunk-MFVUENZR.js → chunk-KV7FYEAF.js} +2 -2
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { a as alias } from './alias-D9_qFlyP.js';
3
3
  export { l as anthropic } from './llm-anthropic-Cd_I2xXB.js';
4
4
  export { a as api } from './api-DAexsu2H.js';
5
5
  export { a as auth } from './auth-2-jfgiHn.js';
6
+ export { b as bus } from './bus-D2bAOPYz.js';
6
7
  export { c as cli } from './cli-RSRk1M36.js';
7
8
  export { c as completion } from './completion-csVun6SP.js';
8
9
  export { c as completionValues } from './completion-values-By-mnjNt.js';
@@ -27,7 +28,9 @@ export { s as stream } from './stream-CrjeCp0M.js';
27
28
  export { i as telemetry } from './index-C3Zw4wDJ.js';
28
29
  export { t as toolspec } from './toolspec-DcPwwoAZ.js';
29
30
  export { i as tui } from './index-CGE5yLBj.js';
30
- export { u as upgrade } from './upgrade-BJ2z6Oqf.js';
31
+ export { i as upgrade } from './index-BqZ7WGQf.js';
32
+ export { u as uri } from './uri-IJ_AoY3d.js';
33
+ export { w as wizard } from './wizard-Bf_qHc31.js';
31
34
  export { x as xdg } from './xdg-DB7Shoib.js';
32
35
  import 'commander';
33
36
  import './parity-CvMuYcVI.js';
package/dist/index.js CHANGED
@@ -1,25 +1,36 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkRYKLVOVKjs = require('./chunk-RYKLVOVK.js');
3
+ var _chunkIVQHLI56js = require('./chunk-IVQHLI56.js');
4
4
 
5
5
 
6
- var _chunkBRNEKPWXjs = require('./chunk-BRNEKPWX.js');
6
+ var _chunkYOBAUCVNjs = require('./chunk-YOBAUCVN.js');
7
7
 
8
8
 
9
- var _chunkEE4M2UOFjs = require('./chunk-EE4M2UOF.js');
9
+ var _chunkRYKLVOVKjs = require('./chunk-RYKLVOVK.js');
10
10
 
11
11
 
12
- var _chunkX4E5QGEBjs = require('./chunk-X4E5QGEB.js');
12
+ var _chunkVKQCOVKIjs = require('./chunk-VKQCOVKI.js');
13
+ require('./chunk-IDM6YZXN.js');
14
+ require('./chunk-I6XDPSXQ.js');
13
15
 
14
16
 
15
- var _chunkP3Y46PYFjs = require('./chunk-P3Y46PYF.js');
17
+ var _chunkMQXYDYRDjs = require('./chunk-MQXYDYRD.js');
16
18
 
17
19
 
18
- var _chunkIVQHLI56js = require('./chunk-IVQHLI56.js');
20
+ var _chunkBRNEKPWXjs = require('./chunk-BRNEKPWX.js');
21
+
22
+
23
+ var _chunkEE4M2UOFjs = require('./chunk-EE4M2UOF.js');
19
24
  require('./chunk-SNVGDYX7.js');
20
25
 
21
26
 
22
- var _chunkYOBAUCVNjs = require('./chunk-YOBAUCVN.js');
27
+ var _chunkF7RSL4N4js = require('./chunk-F7RSL4N4.js');
28
+
29
+
30
+ var _chunkCAONBSPOjs = require('./chunk-CAONBSPO.js');
31
+
32
+
33
+ var _chunkP3Y46PYFjs = require('./chunk-P3Y46PYF.js');
23
34
 
24
35
 
25
36
  var _chunkBDNNPRX6js = require('./chunk-BDNNPRX6.js');
@@ -40,7 +51,7 @@ var _chunk6HFAEZ52js = require('./chunk-6HFAEZ52.js');
40
51
  var _chunkWBVO43HUjs = require('./chunk-WBVO43HU.js');
41
52
 
42
53
 
43
- var _chunkMQXYDYRDjs = require('./chunk-MQXYDYRD.js');
54
+ var _chunkXJTRY453js = require('./chunk-XJTRY453.js');
44
55
 
45
56
 
46
57
  var _chunkQQQYCWSCjs = require('./chunk-QQQYCWSC.js');
@@ -74,16 +85,19 @@ var _chunkCSJWB6D3js = require('./chunk-CSJWB6D3.js');
74
85
  var _chunkUKZIAQA7js = require('./chunk-UKZIAQA7.js');
75
86
 
76
87
 
77
- var _chunkMFVUENZRjs = require('./chunk-MFVUENZR.js');
88
+ var _chunk74E6RNOIjs = require('./chunk-74E6RNOI.js');
89
+
90
+
91
+ var _chunkKV7FYEAFjs = require('./chunk-KV7FYEAF.js');
78
92
  require('./chunk-CXBZTONY.js');
79
93
 
80
94
 
81
95
  var _chunkGFRAYYRDjs = require('./chunk-GFRAYYRD.js');
82
96
  require('./chunk-POR5L2DG.js');
83
- require('./chunk-T6OZMPPV.js');
84
97
 
85
98
 
86
99
  var _chunkKVG7YCTSjs = require('./chunk-KVG7YCTS.js');
100
+ require('./chunk-T6OZMPPV.js');
87
101
 
88
102
 
89
103
  var _chunk4UE5TCMQjs = require('./chunk-4UE5TCMQ.js');
@@ -93,10 +107,10 @@ var _chunk65VPWMPVjs = require('./chunk-65VPWMPV.js');
93
107
 
94
108
 
95
109
  var _chunkJKMQSAJPjs = require('./chunk-JKMQSAJP.js');
110
+ require('./chunk-R5U7XKVJ.js');
111
+
96
112
 
97
113
 
98
- var _chunkXJTRY453js = require('./chunk-XJTRY453.js');
99
- require('./chunk-R5U7XKVJ.js');
100
114
 
101
115
 
102
116
 
@@ -129,4 +143,4 @@ require('./chunk-R5U7XKVJ.js');
129
143
 
130
144
 
131
145
 
132
- exports.aim = _chunkQJVSKKRQjs.aim_exports; exports.alias = _chunkML7D5DWYjs.alias_exports; exports.anthropic = _chunk4FMALWYBjs.llm_anthropic_exports; exports.api = _chunkCSJWB6D3js.api_exports; exports.auth = _chunkUKZIAQA7js.auth_exports; exports.cli = _chunkMFVUENZRjs.cli_exports; exports.completion = _chunk65VPWMPVjs.completion_exports; exports.completionValues = _chunkJKMQSAJPjs.completion_values_exports; exports.compliance = _chunkXJTRY453js.compliance_exports; exports.config = _chunkQQQYCWSCjs.config_exports; exports.errcorrect = _chunk3FIKGFKIjs.errcorrect_exports; exports.id = _chunkP3Y46PYFjs.id_exports; exports.llm = _chunkK7E34Q3Ajs.llm_exports; exports.log = _chunkIZ6Q4SE3js.log_exports; exports.mcp = _chunkIVQHLI56js.mcp_exports; exports.netpolicy = _chunkKVG7YCTSjs.netpolicy_exports; exports.openai = _chunkVWGP4NVJjs.llm_openai_exports; exports.output = _chunkGFRAYYRDjs.output_exports; exports.progress = _chunkBDNNPRX6js.progress_exports; exports.provenance = _chunk6SVOHNICjs.provenance_exports; exports.routellm = _chunkVGOA5Y7Sjs.routellm_exports; exports.rpc = _chunkWUOAQVOAjs.rpc_exports; exports.safety = _chunk6HFAEZ52js.safety_exports; exports.scope = _chunkWBVO43HUjs.scope_exports; exports.serve = _chunkMQXYDYRDjs.serve_exports; exports.stream = _chunkBRNEKPWXjs.stream_exports; exports.telemetry = _chunkYOBAUCVNjs.telemetry_exports; exports.toolspec = _chunkEE4M2UOFjs.toolspec_exports; exports.tui = _chunkRYKLVOVKjs.tui_exports; exports.upgrade = _chunkX4E5QGEBjs.upgrade_exports; exports.xdg = _chunk4UE5TCMQjs.xdg_exports;
146
+ exports.aim = _chunkQJVSKKRQjs.aim_exports; exports.alias = _chunkML7D5DWYjs.alias_exports; exports.anthropic = _chunk4FMALWYBjs.llm_anthropic_exports; exports.api = _chunkCSJWB6D3js.api_exports; exports.auth = _chunkUKZIAQA7js.auth_exports; exports.bus = _chunk74E6RNOIjs.bus_exports; exports.cli = _chunkKV7FYEAFjs.cli_exports; exports.completion = _chunk65VPWMPVjs.completion_exports; exports.completionValues = _chunkJKMQSAJPjs.completion_values_exports; exports.compliance = _chunkXJTRY453js.compliance_exports; exports.config = _chunkQQQYCWSCjs.config_exports; exports.errcorrect = _chunk3FIKGFKIjs.errcorrect_exports; exports.id = _chunkP3Y46PYFjs.id_exports; exports.llm = _chunkK7E34Q3Ajs.llm_exports; exports.log = _chunkIZ6Q4SE3js.log_exports; exports.mcp = _chunkIVQHLI56js.mcp_exports; exports.netpolicy = _chunkKVG7YCTSjs.netpolicy_exports; exports.openai = _chunkVWGP4NVJjs.llm_openai_exports; exports.output = _chunkGFRAYYRDjs.output_exports; exports.progress = _chunkBDNNPRX6js.progress_exports; exports.provenance = _chunk6SVOHNICjs.provenance_exports; exports.routellm = _chunkVGOA5Y7Sjs.routellm_exports; exports.rpc = _chunkWUOAQVOAjs.rpc_exports; exports.safety = _chunk6HFAEZ52js.safety_exports; exports.scope = _chunkWBVO43HUjs.scope_exports; exports.serve = _chunkMQXYDYRDjs.serve_exports; exports.stream = _chunkBRNEKPWXjs.stream_exports; exports.telemetry = _chunkYOBAUCVNjs.telemetry_exports; exports.toolspec = _chunkEE4M2UOFjs.toolspec_exports; exports.tui = _chunkRYKLVOVKjs.tui_exports; exports.upgrade = _chunkVKQCOVKIjs.upgrade_exports; exports.uri = _chunkF7RSL4N4js.uri_exports; exports.wizard = _chunkCAONBSPOjs.wizard_exports; exports.xdg = _chunk4UE5TCMQjs.xdg_exports;
@@ -0,0 +1,45 @@
1
+ import { D as DriverOptions } from '../../version-DEJ3pbiO.js';
2
+
3
+ /**
4
+ * A {@link SchemaDriver} for configuration files, ported from Go
5
+ * `core/upgrade/driver/configfile`: backup copies each managed file into
6
+ * the backup directory, restore copies them back. The version lives in
7
+ * the Migrator's version file.
8
+ *
9
+ * @packageDocumentation
10
+ */
11
+
12
+ /** Options of a {@link ConfigFileDriver}. */
13
+ interface ConfigFileOptions extends DriverOptions {
14
+ /** The schema name (default `"config"`). */
15
+ name?: string;
16
+ }
17
+ /**
18
+ * The backup file name of a managed path: the cleaned path with every
19
+ * separator turned into `_` (`/etc/foo/config.yaml` becomes
20
+ * `_etc_foo_config.yaml`), so two files named alike never collide.
21
+ */
22
+ declare function backupFileName(p: string): string;
23
+ /**
24
+ * Backs up and restores one or more configuration files.
25
+ *
26
+ * A `SchemaDriver`; not declared with `implements` so the bundled declarations
27
+ * keep `SchemaDriver` out of a chunk shared with the root barrel (the tests
28
+ * check the fit).
29
+ */
30
+ declare class ConfigFileDriver {
31
+ private readonly schema;
32
+ private readonly paths;
33
+ private readonly opts;
34
+ constructor(paths: readonly string[], options?: ConfigFileOptions);
35
+ name(): string;
36
+ version(): Promise<string>;
37
+ /** The Migrator writes the version file; nothing to do here. */
38
+ setVersion(): void;
39
+ /** Copies each managed file into `dest`; a file that does not exist is skipped. */
40
+ backup(dest: string): Promise<void>;
41
+ /** Copies each backed-up file back to its place; a file absent from `src` is skipped. */
42
+ restore(src: string): Promise<void>;
43
+ }
44
+
45
+ export { ConfigFileDriver, type ConfigFileOptions, backupFileName };
@@ -0,0 +1,78 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
+
3
+ var _chunkB7XTFDBBjs = require('../../chunk-B7XTFDBB.js');
4
+ require('../../chunk-IDM6YZXN.js');
5
+ require('../../chunk-I6XDPSXQ.js');
6
+ require('../../chunk-4UE5TCMQ.js');
7
+ require('../../chunk-R5U7XKVJ.js');
8
+
9
+ // src/upgrade/drivers/configfile.ts
10
+ var _fs = require('fs');
11
+ var _path = require('path'); var path = _interopRequireWildcard(_path);
12
+ var isMissing = (e) => e.code === "ENOENT";
13
+ var msg = (e) => e instanceof Error ? e.message : String(e);
14
+ function backupFileName(p) {
15
+ let clean = path.normalize(p);
16
+ if (clean.length > 1 && clean.endsWith(path.sep)) clean = clean.slice(0, -1);
17
+ return clean.split(path.sep).join("_");
18
+ }
19
+ var ConfigFileDriver = class {
20
+ constructor(paths, options = {}) {
21
+ this.paths = [...paths];
22
+ this.schema = _nullishCoalesce(options.name, () => ( "config"));
23
+ this.opts = { dataDir: options.dataDir, tool: options.tool };
24
+ }
25
+ name() {
26
+ return this.schema;
27
+ }
28
+ version() {
29
+ return _chunkB7XTFDBBjs.versionOf.call(void 0, this.opts, this.schema);
30
+ }
31
+ /** The Migrator writes the version file; nothing to do here. */
32
+ setVersion() {
33
+ }
34
+ /** Copies each managed file into `dest`; a file that does not exist is skipped. */
35
+ async backup(dest) {
36
+ try {
37
+ await _fs.promises.mkdir(dest, { recursive: true, mode: 488 });
38
+ } catch (e) {
39
+ throw new Error(`create backup dir: ${msg(e)}`, { cause: e });
40
+ }
41
+ for (const p of this.paths) {
42
+ const name = backupFileName(p);
43
+ try {
44
+ await copyFile(p, path.join(dest, name));
45
+ } catch (e) {
46
+ if (isMissing(e)) continue;
47
+ throw new Error(`backup ${name}: ${msg(e)}`, { cause: e });
48
+ }
49
+ }
50
+ }
51
+ /** Copies each backed-up file back to its place; a file absent from `src` is skipped. */
52
+ async restore(src) {
53
+ for (const p of this.paths) {
54
+ const name = backupFileName(p);
55
+ const file = path.join(src, name);
56
+ try {
57
+ await _fs.promises.stat(file);
58
+ } catch (e) {
59
+ if (isMissing(e)) continue;
60
+ throw new Error(`restore ${name}: ${msg(e)}`, { cause: e });
61
+ }
62
+ try {
63
+ await copyFile(file, p);
64
+ } catch (e) {
65
+ throw new Error(`restore ${name}: ${msg(e)}`, { cause: e });
66
+ }
67
+ }
68
+ }
69
+ };
70
+ async function copyFile(src, dst) {
71
+ const data = await _fs.promises.readFile(src);
72
+ await _fs.promises.mkdir(path.dirname(dst), { recursive: true, mode: 488 });
73
+ await _fs.promises.writeFile(dst, data);
74
+ }
75
+
76
+
77
+
78
+ exports.ConfigFileDriver = ConfigFileDriver; exports.backupFileName = backupFileName;
@@ -0,0 +1,61 @@
1
+ import { D as DriverOptions } from '../../version-DEJ3pbiO.js';
2
+
3
+ /**
4
+ * A {@link SchemaDriver} for a filesystem directory, ported from Go
5
+ * `core/upgrade/driver/fsdir`: backup writes `<name>.tar.gz` into the
6
+ * backup directory, restore replaces the directory with its contents.
7
+ * The archive layout is Go's (entries relative to the directory, `.` for
8
+ * the directory itself, gzip), so either port restores the other's.
9
+ *
10
+ * Imports the `tar` package: import this module only where it is used
11
+ * (`@hop-top/kit/upgrade/drivers/fsdir`); the main entry never loads it.
12
+ *
13
+ * @packageDocumentation
14
+ */
15
+
16
+ /** Options of an {@link FsDirDriver}. */
17
+ type FsDirOptions = DriverOptions;
18
+ /**
19
+ * Why a member name must not be extracted under the target directory, or
20
+ * `undefined` when it is a plain relative path inside it. Rejects absolute
21
+ * names (POSIX, backslash and drive-letter forms), any `..` segment and
22
+ * NUL bytes, on top of what the `tar` package does by default (it strips
23
+ * a leading `/` and skips `..` members with a warning instead of failing).
24
+ */
25
+ declare function unsafeName(name: string): string | undefined;
26
+ /**
27
+ * Backs up and restores one directory as a tar.gz archive.
28
+ *
29
+ * A `SchemaDriver`; not declared with `implements` so the bundled declarations
30
+ * keep `SchemaDriver` out of a chunk shared with the root barrel (the tests
31
+ * check the fit).
32
+ */
33
+ declare class FsDirDriver {
34
+ private readonly schema;
35
+ private readonly dirPath;
36
+ private readonly opts;
37
+ constructor(schema: string, dirPath: string, options?: FsDirOptions);
38
+ name(): string;
39
+ version(): Promise<string>;
40
+ /** The Migrator writes the version file; nothing to do here. */
41
+ setVersion(): void;
42
+ private archiveName;
43
+ /**
44
+ * Writes `<dest>/<name>.tar.gz` of the directory; a missing directory is
45
+ * "nothing to back up" (no archive, no error). Symbolic links and special
46
+ * files are not archived: they make the backup fail rather than be lost.
47
+ */
48
+ backup(dest: string): Promise<void>;
49
+ /**
50
+ * Replaces the directory with the archive in `src`. Every member is
51
+ * checked first (see {@link unsafeName}; links, devices and other
52
+ * special members are refused: `path traversal blocked: <name>`,
53
+ * `special member blocked: <name>`); the
54
+ * archive is then extracted into a temporary directory beside the target
55
+ * and swapped in, so a refused or failed restore leaves the directory
56
+ * as it was.
57
+ */
58
+ restore(src: string): Promise<void>;
59
+ }
60
+
61
+ export { FsDirDriver, type FsDirOptions, unsafeName };
@@ -0,0 +1,170 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }
2
+
3
+ var _chunkB7XTFDBBjs = require('../../chunk-B7XTFDBB.js');
4
+ require('../../chunk-IDM6YZXN.js');
5
+ require('../../chunk-I6XDPSXQ.js');
6
+ require('../../chunk-4UE5TCMQ.js');
7
+ require('../../chunk-R5U7XKVJ.js');
8
+
9
+ // src/upgrade/drivers/fsdir.ts
10
+ var _fs = require('fs');
11
+ var _path = require('path'); var path = _interopRequireWildcard(_path);
12
+ var _crypto = require('crypto');
13
+ var _tar = require('tar'); var tar = _interopRequireWildcard(_tar);
14
+ var isMissing = (e) => e.code === "ENOENT";
15
+ var msg = (e) => e instanceof Error ? e.message : String(e);
16
+ var PLAIN = /* @__PURE__ */ new Set(["File", "OldFile", "ContiguousFile", "Directory"]);
17
+ function unsafeName(name) {
18
+ if (name.includes("\0")) return "NUL in name";
19
+ if (name.startsWith("/") || name.startsWith("\\") || /^[A-Za-z]:/.test(name)) return "absolute path";
20
+ if (name.split(/[\\/]/).includes("..")) return "parent directory reference";
21
+ return void 0;
22
+ }
23
+ var FsDirDriver = class {
24
+ constructor(schema, dirPath, options = {}) {
25
+ this.schema = schema;
26
+ this.dirPath = dirPath;
27
+ this.opts = { dataDir: options.dataDir, tool: options.tool };
28
+ }
29
+ name() {
30
+ return this.schema;
31
+ }
32
+ version() {
33
+ return _chunkB7XTFDBBjs.versionOf.call(void 0, this.opts, this.schema);
34
+ }
35
+ /** The Migrator writes the version file; nothing to do here. */
36
+ setVersion() {
37
+ }
38
+ archiveName() {
39
+ return this.schema + ".tar.gz";
40
+ }
41
+ /**
42
+ * Writes `<dest>/<name>.tar.gz` of the directory; a missing directory is
43
+ * "nothing to back up" (no archive, no error). Symbolic links and special
44
+ * files are not archived: they make the backup fail rather than be lost.
45
+ */
46
+ async backup(dest) {
47
+ try {
48
+ await _fs.promises.mkdir(dest, { recursive: true, mode: 488 });
49
+ } catch (e) {
50
+ throw new Error(`create backup dir: ${msg(e)}`, { cause: e });
51
+ }
52
+ try {
53
+ await _fs.promises.stat(this.dirPath);
54
+ } catch (e) {
55
+ if (isMissing(e)) return;
56
+ throw new Error(`stat dir: ${msg(e)}`, { cause: e });
57
+ }
58
+ await requirePlain(this.dirPath);
59
+ const archive = path.join(dest, this.archiveName());
60
+ const tmp = archive + ".tmp";
61
+ try {
62
+ await tar.c(
63
+ {
64
+ file: tmp,
65
+ cwd: this.dirPath,
66
+ gzip: true,
67
+ // Go's names: relative to the directory, `.` for the directory.
68
+ onWriteEntry: (entry) => {
69
+ entry.path = entry.path.replace(/^\.\//, "") || ".";
70
+ }
71
+ },
72
+ ["."]
73
+ );
74
+ await _fs.promises.rename(tmp, archive);
75
+ } catch (e) {
76
+ await _fs.promises.rm(tmp, { force: true });
77
+ throw new Error(`create archive: ${msg(e)}`, { cause: e });
78
+ }
79
+ }
80
+ /**
81
+ * Replaces the directory with the archive in `src`. Every member is
82
+ * checked first (see {@link unsafeName}; links, devices and other
83
+ * special members are refused: `path traversal blocked: <name>`,
84
+ * `special member blocked: <name>`); the
85
+ * archive is then extracted into a temporary directory beside the target
86
+ * and swapped in, so a refused or failed restore leaves the directory
87
+ * as it was.
88
+ */
89
+ async restore(src) {
90
+ const archive = path.join(src, this.archiveName());
91
+ try {
92
+ await _fs.promises.stat(archive);
93
+ } catch (e) {
94
+ if (isMissing(e)) throw new Error(`backup archive not found: ${archive}`, { cause: e });
95
+ throw new Error(`open archive: ${msg(e)}`, { cause: e });
96
+ }
97
+ const hasRoot = await checkMembers(archive);
98
+ const target = path.resolve(this.dirPath);
99
+ const parent = path.dirname(target);
100
+ const base = path.basename(target);
101
+ await _fs.promises.mkdir(parent, { recursive: true, mode: 488 });
102
+ const tmp = await _fs.promises.mkdtemp(path.join(parent, `.${base}.restore-`));
103
+ let old;
104
+ try {
105
+ try {
106
+ await tar.x({
107
+ file: archive,
108
+ cwd: tmp,
109
+ strict: true,
110
+ preservePaths: false,
111
+ preserveOwner: false,
112
+ filter: (_p, entry) => PLAIN.has(entry.type)
113
+ });
114
+ } catch (e) {
115
+ throw new Error(`tar read: ${msg(e)}`, { cause: e });
116
+ }
117
+ if (!hasRoot) await _fs.promises.chmod(tmp, 488);
118
+ try {
119
+ old = path.join(parent, `.${base}.old-${_crypto.randomBytes.call(void 0, 6).toString("hex")}`);
120
+ await _fs.promises.rename(target, old);
121
+ } catch (e) {
122
+ if (!isMissing(e)) throw new Error(`remove dir: ${msg(e)}`, { cause: e });
123
+ old = void 0;
124
+ }
125
+ try {
126
+ await _fs.promises.rename(tmp, target);
127
+ } catch (e) {
128
+ if (old !== void 0) await _fs.promises.rename(old, target).catch(() => void 0);
129
+ throw new Error(`recreate dir: ${msg(e)}`, { cause: e });
130
+ }
131
+ } finally {
132
+ await _fs.promises.rm(tmp, { recursive: true, force: true });
133
+ }
134
+ if (old !== void 0) await _fs.promises.rm(old, { recursive: true, force: true });
135
+ }
136
+ };
137
+ async function requirePlain(dir) {
138
+ const entries = await _fs.promises.readdir(dir, { withFileTypes: true, recursive: true });
139
+ for (const e of entries) {
140
+ if (e.isFile() || e.isDirectory()) continue;
141
+ const rel = path.relative(dir, path.join(e.parentPath, e.name));
142
+ throw new Error(`create archive: ${rel}: only files and directories can be backed up`);
143
+ }
144
+ }
145
+ async function checkMembers(archive) {
146
+ let problem;
147
+ let hasRoot = false;
148
+ try {
149
+ await tar.t({
150
+ file: archive,
151
+ strict: true,
152
+ onReadEntry: (entry) => {
153
+ entry.resume();
154
+ if (problem !== void 0) return;
155
+ const bad = unsafeName(entry.path);
156
+ if (bad !== void 0) problem = `path traversal blocked: ${entry.path}`;
157
+ else if (!PLAIN.has(entry.type)) problem = `special member blocked: ${entry.path} (${entry.type})`;
158
+ else if (path.posix.normalize(entry.path) === "./" || path.posix.normalize(entry.path) === ".") hasRoot = true;
159
+ }
160
+ });
161
+ } catch (e) {
162
+ throw new Error(`tar read: ${msg(e)}`, { cause: e });
163
+ }
164
+ if (problem !== void 0) throw new Error(problem);
165
+ return hasRoot;
166
+ }
167
+
168
+
169
+
170
+ exports.FsDirDriver = FsDirDriver; exports.unsafeName = unsafeName;
@@ -0,0 +1,121 @@
1
+ /**
2
+ * A {@link SchemaDriver} for PostgreSQL over a caller-provided
3
+ * node-postgres style handle, ported from Go
4
+ * `core/upgrade/driver/postgres`. Kit has no runtime Postgres dependency:
5
+ * the caller opens the `pg` Pool (or any object with the same `query`
6
+ * shape) and passes it in, plus a function that returns a dedicated client
7
+ * for the lock.
8
+ *
9
+ * SQL, the table-name rule and the lock key text are the Go driver's,
10
+ * character for character, so a Go and a TypeScript Migrator on one table
11
+ * and schema name exclude each other.
12
+ *
13
+ * Import from `@hop-top/kit/upgrade/drivers/postgres`. The class is a
14
+ * structural `SchemaDriver` (checked in `postgres.test.ts`); it imports no
15
+ * type from `../driver` so the published declarations stay one chunk per
16
+ * entry.
17
+ */
18
+ /** What a query resolves with: the rows, as objects keyed by column. */
19
+ interface PgResult {
20
+ rows: Array<Record<string, any>>;
21
+ }
22
+ /** The node-postgres `query(text, params)` shape (a `Pool` or `Client`). */
23
+ interface PgQueryable {
24
+ query(text: string, params?: any[]): Promise<PgResult>;
25
+ /** A node-postgres `Pool` carries its options; `max: 1` is refused at lock time. */
26
+ readonly options?: {
27
+ max?: number;
28
+ };
29
+ }
30
+ /**
31
+ * A dedicated connection for the lock: a client from `pool.connect()` (has
32
+ * `release`) or a standalone connected `Client` (has `end`). The driver
33
+ * ends the physical connection through whichever exists.
34
+ */
35
+ interface PgLockClient {
36
+ query(text: string, params?: any[]): Promise<PgResult>;
37
+ /** Pooled client: `release()` returns it, `release(true)` destroys it. */
38
+ release?(destroy?: boolean | Error): void;
39
+ /** Standalone client: closes the connection. */
40
+ end?(): void | Promise<void>;
41
+ }
42
+ interface PostgresDriverOptions {
43
+ /** Runs the version queries. Never closed by the driver. */
44
+ db: PgQueryable;
45
+ /**
46
+ * Returns a dedicated connected client, e.g. `() => pool.connect()`.
47
+ * It carries the lock for the whole run, so it must not be `db` itself
48
+ * and a pool needs at least two connections.
49
+ */
50
+ connect: () => Promise<PgLockClient>;
51
+ /** Version table, `^[a-zA-Z_][a-zA-Z0-9_]{0,63}$`, interpolated unquoted. */
52
+ table: string;
53
+ /** Schema name, matched against `Migration.schema`. Default `postgres`. */
54
+ name?: string;
55
+ }
56
+ /** SQL shared verbatim by every port (the server hashes the key text). */
57
+ declare const SQL_LOCK = "SELECT pg_advisory_lock(hashtextextended($1, 0))";
58
+ declare const SQL_UNLOCK = "SELECT pg_advisory_unlock(hashtextextended($1, 0))";
59
+ declare const SQL_XACT_LOCK = "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))";
60
+ /**
61
+ * The non-blocking form of {@link SQL_LOCK}: same key, same session-level
62
+ * lock, so it excludes (and is excluded by) a blocking `pg_advisory_lock`
63
+ * on the key. Polled so an aborted wait holds nothing.
64
+ */
65
+ declare const SQL_TRY_LOCK = "SELECT pg_try_advisory_lock(hashtextextended($1, 0))";
66
+ /** Text hashed into the advisory lock key for a run. */
67
+ declare const lockKey: (table: string, name: string) => string;
68
+ /** Text hashed into the key that serialises table creation. */
69
+ declare const ddlKey: (table: string) => string;
70
+ /** The DDL for the version table. */
71
+ declare const createTableSql: (table: string) => string;
72
+ declare const versionSql: (table: string) => string;
73
+ declare const upsertSql: (table: string) => string;
74
+ /** Throws `invalid table name: "<name>"` unless `name` is a legal table name. */
75
+ declare function validateTable(name: string): void;
76
+ /**
77
+ * Tracks a schema version in a PostgreSQL table and serialises concurrent
78
+ * migrators with a session-level advisory lock. Create it with
79
+ * {@link openPostgresDriver}.
80
+ */
81
+ declare class PostgresDriver {
82
+ private readonly o;
83
+ readonly storesVersion = true;
84
+ /** @internal use {@link openPostgresDriver}. */
85
+ constructor(o: Required<PostgresDriverOptions>);
86
+ name(): string;
87
+ /** The stored version; no row means `""`. */
88
+ version(): Promise<string>;
89
+ /** Upserts the version row for this schema. */
90
+ setVersion(version: string): Promise<void>;
91
+ /** Kit does not back up a server database: the run goes on without one. */
92
+ backup(_dest: string): Promise<void>;
93
+ /** Nothing is restored: a rollback that needs it ends "not restored". */
94
+ restore(_src: string): Promise<void>;
95
+ /**
96
+ * Takes the session-level advisory lock on one dedicated connection and
97
+ * holds that connection until the returned unlock runs. Waits by polling
98
+ * `pg_try_advisory_lock` (about every 50 ms) until acquired or `signal`
99
+ * aborts; an aborted wait rejects with `abortError(signal)` and the
100
+ * connection is destroyed, so nothing is held. Unlock runs
101
+ * `pg_advisory_unlock` on that same connection regardless of `signal`;
102
+ * when it fails or answers `false` the connection is destroyed, which
103
+ * ends the session and the lock.
104
+ *
105
+ * A pool with `options.max === 1` is refused (the lock holds its only
106
+ * connection and the run's queries would wait for ever).
107
+ */
108
+ lock(signal?: AbortSignal): Promise<() => Promise<void>>;
109
+ private unlocker;
110
+ private release;
111
+ }
112
+ /**
113
+ * Ensures the version table exists, then returns a driver for it. The
114
+ * table is created in a transaction on a dedicated client that first takes
115
+ * `pg_advisory_xact_lock`: two sessions running `CREATE TABLE IF NOT
116
+ * EXISTS` at once can still fail with a catalog unique violation. The
117
+ * driver never closes `db`.
118
+ */
119
+ declare function openPostgresDriver(opts: PostgresDriverOptions): Promise<PostgresDriver>;
120
+
121
+ export { type PgLockClient, type PgQueryable, type PgResult, PostgresDriver, type PostgresDriverOptions, SQL_LOCK, SQL_TRY_LOCK, SQL_UNLOCK, SQL_XACT_LOCK, createTableSql, ddlKey, lockKey, openPostgresDriver, upsertSql, validateTable, versionSql };