@particle-academy/fancy-term-host 0.1.0 → 0.1.1

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.
@@ -1,8 +1,28 @@
1
+ import fs, { existsSync, statSync } from 'fs';
2
+ import os2 from 'os';
1
3
  import path from 'path';
2
- import os from 'os';
3
- import fs from 'fs';
4
4
  import crypto from 'crypto';
5
5
 
6
+ // src/cwd.ts
7
+ function toNativeCwd(p) {
8
+ if (process.platform !== "win32" || !p) return p;
9
+ const m = /^\/([A-Za-z])\/(.*)$/.exec(p);
10
+ if (m) return `${m[1].toUpperCase()}:\\${m[2].replace(/\//g, "\\")}`;
11
+ const root = /^\/([A-Za-z])\/?$/.exec(p);
12
+ if (root) return `${root[1].toUpperCase()}:\\`;
13
+ return p;
14
+ }
15
+ function resolveSpawnCwd(requested) {
16
+ if (requested) {
17
+ const native = toNativeCwd(requested);
18
+ try {
19
+ if (existsSync(native) && statSync(native).isDirectory()) return native;
20
+ } catch {
21
+ }
22
+ }
23
+ return os2.homedir();
24
+ }
25
+
6
26
  // src/host-protocol.ts
7
27
  var PROTOCOL_VERSION = 1;
8
28
  var LENGTH_BYTES = 4;
@@ -47,7 +67,7 @@ var _FrameDecoder = class _FrameDecoder {
47
67
  _FrameDecoder.MAX_FRAME = 16 * 1024 * 1024;
48
68
  var FrameDecoder = _FrameDecoder;
49
69
  function userHash() {
50
- const seed = `${os.userInfo().username}|${os.hostname()}`;
70
+ const seed = `${os2.userInfo().username}|${os2.hostname()}`;
51
71
  return crypto.createHash("sha1").update(seed).digest("hex").slice(0, 12);
52
72
  }
53
73
  function socketPathFor(userDataDir) {
@@ -56,7 +76,7 @@ function socketPathFor(userDataDir) {
56
76
  }
57
77
  const candidate = path.join(userDataDir, "ptyhost.sock");
58
78
  if (candidate.length < 100) return candidate;
59
- return path.join(os.tmpdir(), `genie-ptyhost-${userHash()}.sock`);
79
+ return path.join(os2.tmpdir(), `genie-ptyhost-${userHash()}.sock`);
60
80
  }
61
81
  function pidfilePath(userDataDir) {
62
82
  return path.join(userDataDir, "ptyhost.json");
@@ -122,6 +142,6 @@ function resolveHostScript(dirname) {
122
142
  return null;
123
143
  }
124
144
 
125
- export { FrameDecoder, PROTOCOL_VERSION, deletePidfile, encodeFrame, isPidAlive, pidfilePath, pidfileUsable, readPidfile, resolveHostScript, socketPathFor, userHash, writePidfile };
126
- //# sourceMappingURL=chunk-2DQJKTG5.js.map
127
- //# sourceMappingURL=chunk-2DQJKTG5.js.map
145
+ export { FrameDecoder, PROTOCOL_VERSION, deletePidfile, encodeFrame, isPidAlive, pidfilePath, pidfileUsable, readPidfile, resolveHostScript, resolveSpawnCwd, socketPathFor, toNativeCwd, userHash, writePidfile };
146
+ //# sourceMappingURL=chunk-WMP4YLM5.js.map
147
+ //# sourceMappingURL=chunk-WMP4YLM5.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/cwd.ts","../src/host-protocol.ts","../src/host-locate.ts"],"names":["os"],"mappings":";;;;;;AA8BO,SAAS,YAAY,CAAA,EAAmB;AAC3C,EAAA,IAAI,OAAA,CAAQ,QAAA,KAAa,OAAA,IAAW,CAAC,GAAG,OAAO,CAAA;AAC/C,EAAA,MAAM,CAAA,GAAI,sBAAA,CAAuB,IAAA,CAAK,CAAC,CAAA;AACvC,EAAA,IAAI,CAAA,EAAG,OAAO,CAAA,EAAG,CAAA,CAAE,CAAC,CAAA,CAAE,WAAA,EAAa,CAAA,GAAA,EAAM,EAAE,CAAC,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,IAAI,CAAC,CAAA,CAAA;AAClE,EAAA,MAAM,IAAA,GAAO,mBAAA,CAAoB,IAAA,CAAK,CAAC,CAAA;AACvC,EAAA,IAAI,MAAM,OAAO,CAAA,EAAG,KAAK,CAAC,CAAA,CAAE,aAAa,CAAA,GAAA,CAAA;AACzC,EAAA,OAAO,CAAA;AACX;AAOO,SAAS,gBAAgB,SAAA,EAA8C;AAC1E,EAAA,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AW,KAAK,UAAA,EAAY;AACxB,IAAA,IAAI;AACA,MAAA,IAAI,EAAA,CAAG,UAAA,CAAW,CAAC,CAAA,EAAG,OAAO,CAAA;AAAA,IACjC,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACJ;AACA,EAAA,OAAO,IAAA;AACX","file":"chunk-WMP4YLM5.js","sourcesContent":["/**\n * Spawn-cwd normalization (Tier 1.5 companion to osc7.ts).\n *\n * Git Bash / MSYS reports `$PWD` in MSYS form (`/c/Users/me`), not native\n * Windows (`C:\\Users\\me`). The OSC-7 hook emits that raw, and `parseFileUrl`\n * only converts the drive-colon form (`/C:/...`), so an MSYS path flows through\n * unchanged. Handing `/c/Users/me` to node-pty as a working dir makes Windows\n * fail with ERROR_DIRECTORY (error code 267) — terminal creation crashes.\n *\n * Two small, OS-agnostic helpers fix this at the source:\n * - `toNativeCwd` converts an MSYS path to native Windows form (no-op on\n * POSIX, or when already native).\n * - `resolveSpawnCwd` native-converts the requested dir AND validates it,\n * falling back to the home directory so a stale/foreign/deleted cwd can\n * never crash spawn.\n *\n * Used at both spawn sites (manager.ts, pty-host.ts) and at the OSC-7 capture\n * so the persisted `live_cwd` is already a valid native path.\n */\n\nimport { existsSync, statSync } from 'node:fs';\nimport os from 'node:os';\n\n/**\n * Convert an MSYS/Git-Bash cwd to a native Windows path.\n * /c/Users/me -> C:\\Users\\me\n * /d/work -> D:\\work\n * /c -> C:\\ (bare drive root)\n * No-op on non-win32, on an empty string, or on an already-native path.\n */\nexport function toNativeCwd(p: string): string {\n if (process.platform !== 'win32' || !p) return p;\n const m = /^\\/([A-Za-z])\\/(.*)$/.exec(p);\n if (m) return `${m[1].toUpperCase()}:\\\\${m[2].replace(/\\//g, '\\\\')}`;\n const root = /^\\/([A-Za-z])\\/?$/.exec(p);\n if (root) return `${root[1].toUpperCase()}:\\\\`;\n return p;\n}\n\n/**\n * Resolve the directory a pty should actually spawn in. Prefer the requested\n * dir (native-converted); if it isn't an existing directory, fall back to the\n * home directory — so a stale, foreign, or deleted cwd can't crash spawn.\n */\nexport function resolveSpawnCwd(requested: string | undefined | null): string {\n if (requested) {\n const native = toNativeCwd(requested);\n try {\n if (existsSync(native) && statSync(native).isDirectory()) return native;\n } catch {\n /* fall through to home */\n }\n }\n return os.homedir();\n}\n","/**\n * Pty-host wire protocol (Tier 3).\n *\n * The detached pty-host (main/terminal/pty-host.ts) and the in-app HostClient\n * (main/terminal/host-client.ts) talk over a local IPC transport — a named pipe\n * on Windows, a unix domain socket on POSIX — using a tiny length-prefixed JSON\n * framing so there's no heavy dependency. This module is PURE (no electron, no\n * node-pty, no net): just the message shapes + the encode/decode for the framing,\n * so it can be imported by both ends AND unit-tested in isolation.\n *\n * Framing: each message is `[4-byte big-endian uint32 length][utf8 JSON body]`.\n * The length prefix is the byte length of the JSON body. A FrameDecoder buffers\n * partial reads and yields whole messages as they complete — TCP/pipe streams\n * don't preserve message boundaries, so we can't assume one `data` event == one\n * message.\n */\n\n/**\n * Protocol version. Bumped whenever the message shapes change in a way that\n * makes an old host incompatible with a new client (or vice-versa). The client\n * refuses to attach to a host whose pidfile reports a different version and\n * spawns a fresh host instead — see host-client.ts connect-or-spawn.\n */\nexport const PROTOCOL_VERSION = 1;\n\n/** Requests the client sends to the host. `seq` correlates a reply. */\nexport type ClientMessage =\n | { kind: 'hello'; seq: number; protocolVersion: number }\n | {\n kind: 'create';\n seq: number;\n opts: {\n id: string;\n cwd: string;\n shell?: string;\n args?: string[];\n cols?: number;\n rows?: number;\n env?: Record<string, string>;\n };\n }\n | { kind: 'write'; id: string; data: string }\n | { kind: 'resize'; id: string; cols: number; rows: number }\n | { kind: 'kill'; id: string }\n | { kind: 'list'; seq: number }\n | { kind: 'set-retained'; id: string; retained: boolean }\n | { kind: 'get-scrollback'; seq: number; id: string }\n | { kind: 'ping'; seq: number };\n\n/** Pushes + replies the host sends to the client. */\nexport type HostMessage =\n | { kind: 'hello-ok'; seq: number; protocolVersion: number; pid: number }\n | {\n kind: 'created';\n seq: number;\n result: {\n id: string;\n pid: number;\n shell: string;\n existing: boolean;\n scrollback: string;\n };\n }\n | {\n kind: 'list-result';\n seq: number;\n terminals: Array<{ id: string; pid: number; shell: string }>;\n }\n | { kind: 'scrollback-result'; seq: number; scrollback: string | null }\n | { kind: 'pong'; seq: number }\n | { kind: 'data'; id: string; data: string }\n | { kind: 'exit'; id: string; exitCode: number; signal?: number };\n\nexport type Frame = ClientMessage | HostMessage;\n\nconst LENGTH_BYTES = 4;\n\n/** Encode a message as a length-prefixed JSON frame ready for the socket. */\nexport function encodeFrame(msg: Frame): Buffer {\n const body = Buffer.from(JSON.stringify(msg), 'utf8');\n const header = Buffer.allocUnsafe(LENGTH_BYTES);\n header.writeUInt32BE(body.length, 0);\n return Buffer.concat([header, body]);\n}\n\n/**\n * Streaming frame decoder. Feed it raw socket chunks via `push`; it returns the\n * complete messages that became available (zero or more), buffering any partial\n * tail until the rest arrives. One decoder per socket.\n *\n * Resilient by design: a malformed JSON body is skipped (the frame is consumed\n * but yields nothing) rather than throwing — a corrupt frame must not wedge the\n * whole stream. An absurd length prefix (> MAX_FRAME) is treated as a desync and\n * the buffer is reset; the caller can decide whether to drop the connection.\n */\nexport class FrameDecoder {\n private buffer: Buffer = Buffer.alloc(0);\n\n /** Hard cap on a single frame (16 MB). Guards against a runaway/garbage\n * length prefix allocating unbounded memory. node-pty data chunks are tiny;\n * a serialized scrollback is bounded well under this. */\n static readonly MAX_FRAME = 16 * 1024 * 1024;\n\n /** True when the last push hit an oversized/desynced frame. The caller\n * should drop the connection — the stream can't be trusted to realign. */\n desynced = false;\n\n push(chunk: Buffer): Frame[] {\n this.buffer = this.buffer.length ? Buffer.concat([this.buffer, chunk]) : chunk;\n const out: Frame[] = [];\n for (;;) {\n if (this.buffer.length < LENGTH_BYTES) break;\n const len = this.buffer.readUInt32BE(0);\n if (len > FrameDecoder.MAX_FRAME) {\n // Desync / garbage. Reset and flag — realigning a length-prefixed\n // stream after a bad prefix isn't possible without a sentinel.\n this.desynced = true;\n this.buffer = Buffer.alloc(0);\n break;\n }\n if (this.buffer.length < LENGTH_BYTES + len) break; // wait for more\n const body = this.buffer.subarray(LENGTH_BYTES, LENGTH_BYTES + len);\n this.buffer = this.buffer.subarray(LENGTH_BYTES + len);\n try {\n out.push(JSON.parse(body.toString('utf8')) as Frame);\n } catch {\n /* skip a corrupt frame; the framing itself is still aligned */\n }\n }\n return out;\n }\n}\n","import path from 'node:path';\nimport os from 'node:os';\nimport fs from 'node:fs';\nimport crypto from 'node:crypto';\nimport { PROTOCOL_VERSION } from './host-protocol';\n\n/**\n * Path + pidfile resolution for the detached pty-host (Tier 3).\n *\n * Kept ELECTRON-FREE on the resolution side that the host itself uses (the host\n * is a plain node process — no `app`), so the userData path is passed IN. The\n * in-app side (host-client lifecycle) imports `app` separately and feeds it here.\n */\n\nexport interface Pidfile {\n pid: number;\n socketPath: string;\n protocolVersion: number;\n startedAt: number;\n}\n\n/** Short, stable per-user hash so two OS users don't collide on the Windows\n * pipe name (the pipe namespace is machine-global). */\nexport function userHash(): string {\n const seed = `${os.userInfo().username}|${os.hostname()}`;\n return crypto.createHash('sha1').update(seed).digest('hex').slice(0, 12);\n}\n\n/**\n * The local IPC transport address.\n * • Windows: a named pipe `\\\\.\\pipe\\genie-ptyhost-<userhash>`. The default\n * Windows pipe ACL is per-logon-session, so another user on the same machine\n * can't open it — that's our ACL. (Documented; we don't tighten further.)\n * • POSIX: a unix domain socket under userData (preferred — survives /tmp\n * cleaners and is per-user by directory perms) named `ptyhost.sock`.\n */\nexport function socketPathFor(userDataDir: string): string {\n if (process.platform === 'win32') {\n return `\\\\\\\\.\\\\pipe\\\\genie-ptyhost-${userHash()}`;\n }\n // Keep the path short — unix socket paths have a ~104-char limit. userData is\n // typically well under that; fall back to os.tmpdir() if it's pathologically\n // long.\n const candidate = path.join(userDataDir, 'ptyhost.sock');\n if (candidate.length < 100) return candidate;\n return path.join(os.tmpdir(), `genie-ptyhost-${userHash()}.sock`);\n}\n\nexport function pidfilePath(userDataDir: string): string {\n return path.join(userDataDir, 'ptyhost.json');\n}\n\nexport function writePidfile(userDataDir: string, pf: Pidfile): void {\n const target = pidfilePath(userDataDir);\n const tmp = `${target}.tmp`;\n fs.writeFileSync(tmp, JSON.stringify(pf));\n fs.renameSync(tmp, target);\n}\n\nexport function readPidfile(userDataDir: string): Pidfile | null {\n try {\n const raw = fs.readFileSync(pidfilePath(userDataDir), 'utf8');\n const pf = JSON.parse(raw) as Pidfile;\n if (\n typeof pf.pid !== 'number' ||\n typeof pf.socketPath !== 'string' ||\n typeof pf.protocolVersion !== 'number'\n ) {\n return null;\n }\n return pf;\n } catch {\n return null;\n }\n}\n\nexport function deletePidfile(userDataDir: string): void {\n try {\n fs.rmSync(pidfilePath(userDataDir), { force: true });\n } catch {\n /* ignore */\n }\n}\n\n/** True when a process with `pid` is alive (signal 0 probes without killing). */\nexport function isPidAlive(pid: number): boolean {\n if (!pid || pid <= 0) return false;\n try {\n process.kill(pid, 0);\n return true;\n } catch (err) {\n // EPERM = exists but not ours (still \"alive\"); ESRCH = gone.\n return (err as NodeJS.ErrnoException).code === 'EPERM';\n }\n}\n\n/**\n * Decide whether an existing pidfile points at a usable host.\n * Usable = pid alive AND protocol versions match. A stale/dead/mismatched\n * pidfile means we must spawn a fresh host.\n */\nexport function pidfileUsable(pf: Pidfile | null): boolean {\n if (!pf) return false;\n if (pf.protocolVersion !== PROTOCOL_VERSION) return false;\n if (!isPidAlive(pf.pid)) return false;\n return true;\n}\n\n/**\n * Resolve the compiled pty-host script on disk, trying multiple candidate paths\n * so it works in BOTH `npm run dev` (script at app/pty-host.js next to\n * background.js) AND a packaged asar build. node-pty's native binding can't load\n * from inside an asar, so the host (which requires node-pty) must run UNPACKED —\n * `app.asar.unpacked/...`. We try the unpacked path first, then the in-asar path,\n * then a dev-relative path. Returns the first that exists, or null.\n *\n * `dirname` is main/background's __dirname (the directory the compiled main\n * bundle lives in). The host script is emitted alongside it as `pty-host.js`.\n */\nexport function resolveHostScript(dirname: string): string | null {\n const candidates = [\n // Packaged: node-pty must be unpacked, so run the host from the unpacked\n // tree too (its require('node-pty') resolves to the unpacked .node).\n dirname.includes(`app.asar${path.sep}`) || dirname.includes('app.asar/')\n ? dirname.replace(\n /app\\.asar([\\\\/])/,\n `app.asar.unpacked$1`,\n ) + path.sep + 'pty-host.js'\n : '',\n // Same dir as the compiled main bundle (dev: app/pty-host.js).\n path.join(dirname, 'pty-host.js'),\n // Defensive: a sibling unpacked dir computed from the asar path.\n path.join(dirname.replace('app.asar', 'app.asar.unpacked'), 'pty-host.js'),\n ].filter(Boolean);\n\n for (const c of candidates) {\n try {\n if (fs.existsSync(c)) return c;\n } catch {\n /* keep trying */\n }\n }\n return null;\n}\n"]}
package/dist/index.cjs CHANGED
@@ -3,8 +3,8 @@
3
3
  var nodePty = require('node-pty');
4
4
  var events = require('events');
5
5
  var fs2 = require('fs');
6
+ var os2 = require('os');
6
7
  var path = require('path');
7
- var os = require('os');
8
8
  var crypto = require('crypto');
9
9
  var zlib = require('zlib');
10
10
  var net = require('net');
@@ -14,8 +14,8 @@ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentS
14
14
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
15
 
16
16
  var fs2__default = /*#__PURE__*/_interopDefault(fs2);
17
+ var os2__default = /*#__PURE__*/_interopDefault(os2);
17
18
  var path__default = /*#__PURE__*/_interopDefault(path);
18
- var os__default = /*#__PURE__*/_interopDefault(os);
19
19
  var crypto__default = /*#__PURE__*/_interopDefault(crypto);
20
20
  var zlib__default = /*#__PURE__*/_interopDefault(zlib);
21
21
  var net__default = /*#__PURE__*/_interopDefault(net);
@@ -53,6 +53,24 @@ function scanOsc7Cwd(chunk) {
53
53
  }
54
54
  return last;
55
55
  }
56
+ function toNativeCwd(p) {
57
+ if (process.platform !== "win32" || !p) return p;
58
+ const m = /^\/([A-Za-z])\/(.*)$/.exec(p);
59
+ if (m) return `${m[1].toUpperCase()}:\\${m[2].replace(/\//g, "\\")}`;
60
+ const root = /^\/([A-Za-z])\/?$/.exec(p);
61
+ if (root) return `${root[1].toUpperCase()}:\\`;
62
+ return p;
63
+ }
64
+ function resolveSpawnCwd(requested) {
65
+ if (requested) {
66
+ const native = toNativeCwd(requested);
67
+ try {
68
+ if (fs2.existsSync(native) && fs2.statSync(native).isDirectory()) return native;
69
+ } catch {
70
+ }
71
+ }
72
+ return os2__default.default.homedir();
73
+ }
56
74
  function firstExisting(paths) {
57
75
  for (const p of paths) {
58
76
  try {
@@ -183,9 +201,9 @@ function shellKind(command) {
183
201
  return "other";
184
202
  }
185
203
  function hookDir() {
186
- const seed = `${os__default.default.userInfo().username}|${os__default.default.hostname()}`;
204
+ const seed = `${os2__default.default.userInfo().username}|${os2__default.default.hostname()}`;
187
205
  const hash = crypto__default.default.createHash("sha1").update(seed).digest("hex").slice(0, 12);
188
- const dir = path__default.default.join(os__default.default.tmpdir(), "fancy-term-host", hash);
206
+ const dir = path__default.default.join(os2__default.default.tmpdir(), "fancy-term-host", hash);
189
207
  fs2__default.default.mkdirSync(dir, { recursive: true });
190
208
  return dir;
191
209
  }
@@ -201,14 +219,14 @@ function cwdHookSpawn(command, settings) {
201
219
  const empty = { env: {}, args: [] };
202
220
  if (settings.get("track_cwd") === "off") return empty;
203
221
  const kind = shellKind(command);
204
- const host = os__default.default.hostname();
222
+ const host = os2__default.default.hostname();
205
223
  if (kind === "bash") {
206
224
  const emit = `printf '\\033]7;file://${host}%s\\033\\\\' "$PWD"`;
207
225
  const prev = process.env.PROMPT_COMMAND ? "; " + process.env.PROMPT_COMMAND : "";
208
226
  return { env: { PROMPT_COMMAND: `${emit}${prev}` }, args: [] };
209
227
  }
210
228
  if (kind === "zsh") {
211
- const orig = process.env.ZDOTDIR || os__default.default.homedir();
229
+ const orig = process.env.ZDOTDIR || os2__default.default.homedir();
212
230
  const dir = hookDir();
213
231
  const okEnv = writeShim(
214
232
  path__default.default.join(dir, ".zshenv"),
@@ -354,7 +372,10 @@ var InProcessBackend = class extends events.EventEmitter {
354
372
  env.TERM = env.TERM || "xterm-256color";
355
373
  const pty = nodePty.spawn(shell, args, {
356
374
  name: "xterm-color",
357
- cwd: opts.cwd,
375
+ // Native-convert + validate the requested dir; a stale/foreign/MSYS
376
+ // cwd (e.g. Git Bash's /c/Users/me) would otherwise crash spawn with
377
+ // Windows ERROR_DIRECTORY (267). Falls back to home if unusable.
378
+ cwd: resolveSpawnCwd(opts.cwd),
358
379
  cols: opts.cols ?? 80,
359
380
  rows: opts.rows ?? 24,
360
381
  env
@@ -369,7 +390,8 @@ var InProcessBackend = class extends events.EventEmitter {
369
390
  opts.id,
370
391
  next.length > SCROLLBACK_MAX ? next.slice(-SCROLLBACK_MAX) : next
371
392
  );
372
- const cwd = scanOsc7Cwd(data);
393
+ const raw = scanOsc7Cwd(data);
394
+ const cwd = raw ? toNativeCwd(raw) : null;
373
395
  if (cwd && cwd !== this.liveCwd.get(opts.id)) {
374
396
  this.liveCwd.set(opts.id, cwd);
375
397
  this.scheduleCwdPersist(opts.id, cwd);
@@ -959,7 +981,7 @@ var HostClient = class _HostClient extends events.EventEmitter {
959
981
  }
960
982
  };
961
983
  function userHash() {
962
- const seed = `${os__default.default.userInfo().username}|${os__default.default.hostname()}`;
984
+ const seed = `${os2__default.default.userInfo().username}|${os2__default.default.hostname()}`;
963
985
  return crypto__default.default.createHash("sha1").update(seed).digest("hex").slice(0, 12);
964
986
  }
965
987
  function socketPathFor(userDataDir) {
@@ -968,7 +990,7 @@ function socketPathFor(userDataDir) {
968
990
  }
969
991
  const candidate = path__default.default.join(userDataDir, "ptyhost.sock");
970
992
  if (candidate.length < 100) return candidate;
971
- return path__default.default.join(os__default.default.tmpdir(), `genie-ptyhost-${userHash()}.sock`);
993
+ return path__default.default.join(os2__default.default.tmpdir(), `genie-ptyhost-${userHash()}.sock`);
972
994
  }
973
995
  function pidfilePath(userDataDir) {
974
996
  return path__default.default.join(userDataDir, "ptyhost.json");
@@ -1170,12 +1192,14 @@ exports.ptyHostScriptPath = ptyHostScriptPath;
1170
1192
  exports.readPidfile = readPidfile;
1171
1193
  exports.resolveDefaultShell = resolveDefaultShell;
1172
1194
  exports.resolveHostScript = resolveHostScript;
1195
+ exports.resolveSpawnCwd = resolveSpawnCwd;
1173
1196
  exports.scanOsc7Cwd = scanOsc7Cwd;
1174
1197
  exports.setActiveBackend = setActiveBackend;
1175
1198
  exports.shellKind = shellKind;
1176
1199
  exports.socketPathFor = socketPathFor;
1177
1200
  exports.subscribeBackendEvents = subscribeBackendEvents;
1178
1201
  exports.terminalManager = terminalManager;
1202
+ exports.toNativeCwd = toNativeCwd;
1179
1203
  exports.userHash = userHash;
1180
1204
  exports.writePidfile = writePidfile;
1181
1205
  //# sourceMappingURL=index.cjs.map