@celljs/ta-host 4.6.1 → 4.8.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.
Files changed (245) hide show
  1. package/README.en.md +70 -0
  2. package/README.md +16 -3
  3. package/cell.schema.json +21 -0
  4. package/cell.yml +7 -0
  5. package/esm/node/auth-server-impl.d.ts +2 -0
  6. package/esm/node/auth-server-impl.js +7 -0
  7. package/esm/node/auth-server-impl.js.map +1 -1
  8. package/esm/node/exec-environment-factory.d.ts +8 -4
  9. package/esm/node/exec-environment-factory.js +28 -6
  10. package/esm/node/exec-environment-factory.js.map +1 -1
  11. package/esm/node/extension-providers.d.ts +22 -0
  12. package/esm/node/extension-providers.js +52 -0
  13. package/esm/node/extension-providers.js.map +1 -0
  14. package/esm/node/index.d.ts +3 -0
  15. package/esm/node/index.js +3 -0
  16. package/esm/node/index.js.map +1 -1
  17. package/esm/node/package-host-extension.d.ts +4 -2
  18. package/esm/node/package-host-extension.js +4 -3
  19. package/esm/node/package-host-extension.js.map +1 -1
  20. package/esm/node/package-hosts.d.ts +6 -2
  21. package/esm/node/package-hosts.js +32 -0
  22. package/esm/node/package-hosts.js.map +1 -1
  23. package/esm/node/remote/index.d.ts +5 -0
  24. package/esm/node/remote/index.js +6 -0
  25. package/esm/node/remote/index.js.map +1 -0
  26. package/esm/node/remote/pi-directories.d.ts +14 -0
  27. package/esm/node/remote/pi-directories.js +64 -0
  28. package/esm/node/remote/pi-directories.js.map +1 -0
  29. package/esm/node/remote/port-forwards.d.ts +35 -0
  30. package/esm/node/remote/port-forwards.js +150 -0
  31. package/esm/node/remote/port-forwards.js.map +1 -0
  32. package/esm/node/remote/remote-hosts.d.ts +40 -0
  33. package/esm/node/remote/remote-hosts.js +170 -0
  34. package/esm/node/remote/remote-hosts.js.map +1 -0
  35. package/esm/node/remote/remote-protocol.d.ts +51 -0
  36. package/esm/node/remote/remote-protocol.js +5 -0
  37. package/esm/node/remote/remote-protocol.js.map +1 -0
  38. package/esm/node/remote/remote-server-impl.d.ts +19 -0
  39. package/esm/node/remote/remote-server-impl.js +66 -0
  40. package/esm/node/remote/remote-server-impl.js.map +1 -0
  41. package/esm/node/remote-ui-context.d.ts +6 -1
  42. package/esm/node/remote-ui-context.js +31 -3
  43. package/esm/node/remote-ui-context.js.map +1 -1
  44. package/esm/node/session-files.d.ts +18 -0
  45. package/esm/node/session-files.js +32 -0
  46. package/esm/node/session-files.js.map +1 -0
  47. package/esm/node/settings-server-impl.d.ts +1 -0
  48. package/esm/node/settings-server-impl.js +8 -0
  49. package/esm/node/settings-server-impl.js.map +1 -1
  50. package/esm/node/settings-store.d.ts +9 -2
  51. package/esm/node/settings-store.js +26 -5
  52. package/esm/node/settings-store.js.map +1 -1
  53. package/esm/node/ta-server-impl.d.ts +2 -0
  54. package/esm/node/ta-server-impl.js +16 -4
  55. package/esm/node/ta-server-impl.js.map +1 -1
  56. package/esm/node/ta-session-factory.d.ts +19 -2
  57. package/esm/node/ta-session-factory.js +54 -11
  58. package/esm/node/ta-session-factory.js.map +1 -1
  59. package/esm/node/ta-session.d.ts +4 -0
  60. package/esm/node/ta-session.js +6 -1
  61. package/esm/node/ta-session.js.map +1 -1
  62. package/esm/node/terminal-service.d.ts +18 -2
  63. package/esm/node/terminal-service.js +46 -10
  64. package/esm/node/terminal-service.js.map +1 -1
  65. package/esm/node/terminal-util.d.ts +14 -0
  66. package/esm/node/terminal-util.js +30 -1
  67. package/esm/node/terminal-util.js.map +1 -1
  68. package/esm/node/web-extension-catalog.d.ts +3 -1
  69. package/esm/node/web-extension-catalog.js +8 -2
  70. package/esm/node/web-extension-catalog.js.map +1 -1
  71. package/esm/node/working-directory.d.ts +12 -1
  72. package/esm/node/working-directory.js +22 -1
  73. package/esm/node/working-directory.js.map +1 -1
  74. package/esm/node/workspace-files.d.ts +1 -1
  75. package/esm/node/workspace-files.js +3 -3
  76. package/esm/node/workspace-files.js.map +1 -1
  77. package/esm/node/workspace-protocol.d.ts +2 -0
  78. package/esm/node/workspace-server-impl.d.ts +2 -1
  79. package/esm/node/workspace-server-impl.js +13 -1
  80. package/esm/node/workspace-server-impl.js.map +1 -1
  81. package/esm/node/workspace-service.d.ts +1 -0
  82. package/esm/node/workspace-service.js +15 -3
  83. package/esm/node/workspace-service.js.map +1 -1
  84. package/lib/node/auth-server-impl.d.ts +2 -0
  85. package/lib/node/auth-server-impl.d.ts.map +1 -1
  86. package/lib/node/auth-server-impl.js +7 -0
  87. package/lib/node/auth-server-impl.js.map +1 -1
  88. package/lib/node/exec-environment-factory.d.ts +8 -4
  89. package/lib/node/exec-environment-factory.d.ts.map +1 -1
  90. package/lib/node/exec-environment-factory.js +28 -6
  91. package/lib/node/exec-environment-factory.js.map +1 -1
  92. package/lib/node/extension-providers.d.ts +23 -0
  93. package/lib/node/extension-providers.d.ts.map +1 -0
  94. package/lib/node/extension-providers.js +55 -0
  95. package/lib/node/extension-providers.js.map +1 -0
  96. package/lib/node/index.d.ts +3 -0
  97. package/lib/node/index.d.ts.map +1 -1
  98. package/lib/node/index.js +3 -0
  99. package/lib/node/index.js.map +1 -1
  100. package/lib/node/package-host-extension.d.ts +4 -2
  101. package/lib/node/package-host-extension.d.ts.map +1 -1
  102. package/lib/node/package-host-extension.js +4 -3
  103. package/lib/node/package-host-extension.js.map +1 -1
  104. package/lib/node/package-hosts.d.ts +6 -2
  105. package/lib/node/package-hosts.d.ts.map +1 -1
  106. package/lib/node/package-hosts.js +32 -0
  107. package/lib/node/package-hosts.js.map +1 -1
  108. package/lib/node/remote/index.d.ts +6 -0
  109. package/lib/node/remote/index.d.ts.map +1 -0
  110. package/lib/node/remote/index.js +22 -0
  111. package/lib/node/remote/index.js.map +1 -0
  112. package/lib/node/remote/pi-directories.d.ts +15 -0
  113. package/lib/node/remote/pi-directories.d.ts.map +1 -0
  114. package/lib/node/remote/pi-directories.js +67 -0
  115. package/lib/node/remote/pi-directories.js.map +1 -0
  116. package/lib/node/remote/port-forwards.d.ts +36 -0
  117. package/lib/node/remote/port-forwards.d.ts.map +1 -0
  118. package/lib/node/remote/port-forwards.js +153 -0
  119. package/lib/node/remote/port-forwards.js.map +1 -0
  120. package/lib/node/remote/remote-hosts.d.ts +41 -0
  121. package/lib/node/remote/remote-hosts.d.ts.map +1 -0
  122. package/lib/node/remote/remote-hosts.js +173 -0
  123. package/lib/node/remote/remote-hosts.js.map +1 -0
  124. package/lib/node/remote/remote-protocol.d.ts +52 -0
  125. package/lib/node/remote/remote-protocol.d.ts.map +1 -0
  126. package/lib/node/remote/remote-protocol.js +8 -0
  127. package/lib/node/remote/remote-protocol.js.map +1 -0
  128. package/lib/node/remote/remote-server-impl.d.ts +20 -0
  129. package/lib/node/remote/remote-server-impl.d.ts.map +1 -0
  130. package/lib/node/remote/remote-server-impl.js +69 -0
  131. package/lib/node/remote/remote-server-impl.js.map +1 -0
  132. package/lib/node/remote/remote.spec.d.ts +2 -0
  133. package/lib/node/remote/remote.spec.d.ts.map +1 -0
  134. package/lib/node/remote/remote.spec.js +130 -0
  135. package/lib/node/remote/remote.spec.js.map +1 -0
  136. package/lib/node/remote-ui-context.d.ts +6 -1
  137. package/lib/node/remote-ui-context.d.ts.map +1 -1
  138. package/lib/node/remote-ui-context.js +31 -3
  139. package/lib/node/remote-ui-context.js.map +1 -1
  140. package/lib/node/remote-ui-context.spec.js +23 -0
  141. package/lib/node/remote-ui-context.spec.js.map +1 -1
  142. package/lib/node/session-files.d.ts +19 -0
  143. package/lib/node/session-files.d.ts.map +1 -0
  144. package/lib/node/session-files.js +36 -0
  145. package/lib/node/session-files.js.map +1 -0
  146. package/lib/node/session-files.spec.d.ts +2 -0
  147. package/lib/node/session-files.spec.d.ts.map +1 -0
  148. package/lib/node/session-files.spec.js +51 -0
  149. package/lib/node/session-files.spec.js.map +1 -0
  150. package/lib/node/settings-server-impl.d.ts +1 -0
  151. package/lib/node/settings-server-impl.d.ts.map +1 -1
  152. package/lib/node/settings-server-impl.js +8 -0
  153. package/lib/node/settings-server-impl.js.map +1 -1
  154. package/lib/node/settings-store.d.ts +9 -2
  155. package/lib/node/settings-store.d.ts.map +1 -1
  156. package/lib/node/settings-store.js +26 -5
  157. package/lib/node/settings-store.js.map +1 -1
  158. package/lib/node/settings-store.spec.js +5 -2
  159. package/lib/node/settings-store.spec.js.map +1 -1
  160. package/lib/node/ta-server-impl.d.ts +2 -0
  161. package/lib/node/ta-server-impl.d.ts.map +1 -1
  162. package/lib/node/ta-server-impl.js +16 -4
  163. package/lib/node/ta-server-impl.js.map +1 -1
  164. package/lib/node/ta-session-factory.d.ts +19 -2
  165. package/lib/node/ta-session-factory.d.ts.map +1 -1
  166. package/lib/node/ta-session-factory.js +53 -10
  167. package/lib/node/ta-session-factory.js.map +1 -1
  168. package/lib/node/ta-session.d.ts +4 -0
  169. package/lib/node/ta-session.d.ts.map +1 -1
  170. package/lib/node/ta-session.js +6 -1
  171. package/lib/node/ta-session.js.map +1 -1
  172. package/lib/node/terminal-service.d.ts +18 -2
  173. package/lib/node/terminal-service.d.ts.map +1 -1
  174. package/lib/node/terminal-service.js +45 -9
  175. package/lib/node/terminal-service.js.map +1 -1
  176. package/lib/node/terminal-service.spec.js +31 -0
  177. package/lib/node/terminal-service.spec.js.map +1 -1
  178. package/lib/node/terminal-util.d.ts +14 -0
  179. package/lib/node/terminal-util.d.ts.map +1 -1
  180. package/lib/node/terminal-util.js +30 -1
  181. package/lib/node/terminal-util.js.map +1 -1
  182. package/lib/node/terminal-util.spec.js +24 -0
  183. package/lib/node/terminal-util.spec.js.map +1 -1
  184. package/lib/node/web-extension-catalog.d.ts +3 -1
  185. package/lib/node/web-extension-catalog.d.ts.map +1 -1
  186. package/lib/node/web-extension-catalog.js +7 -1
  187. package/lib/node/web-extension-catalog.js.map +1 -1
  188. package/lib/node/web-extension-catalog.spec.js +1 -1
  189. package/lib/node/web-extension-catalog.spec.js.map +1 -1
  190. package/lib/node/working-directory.d.ts +12 -1
  191. package/lib/node/working-directory.d.ts.map +1 -1
  192. package/lib/node/working-directory.js +22 -1
  193. package/lib/node/working-directory.js.map +1 -1
  194. package/lib/node/workspace-files.d.ts +1 -1
  195. package/lib/node/workspace-files.d.ts.map +1 -1
  196. package/lib/node/workspace-files.js +3 -3
  197. package/lib/node/workspace-files.js.map +1 -1
  198. package/lib/node/workspace-protocol.d.ts +2 -0
  199. package/lib/node/workspace-protocol.d.ts.map +1 -1
  200. package/lib/node/workspace-server-impl.d.ts +2 -1
  201. package/lib/node/workspace-server-impl.d.ts.map +1 -1
  202. package/lib/node/workspace-server-impl.js +12 -0
  203. package/lib/node/workspace-server-impl.js.map +1 -1
  204. package/lib/node/workspace-service.d.ts +1 -0
  205. package/lib/node/workspace-service.d.ts.map +1 -1
  206. package/lib/node/workspace-service.js +15 -3
  207. package/lib/node/workspace-service.js.map +1 -1
  208. package/lib/node/workspace-service.spec.js +9 -0
  209. package/lib/node/workspace-service.spec.js.map +1 -1
  210. package/package.json +14 -10
  211. package/src/node/auth-server-impl.ts +6 -0
  212. package/src/node/exec-environment-factory.ts +26 -6
  213. package/src/node/extension-providers.ts +49 -0
  214. package/src/node/index.ts +3 -0
  215. package/src/node/package-host-extension.ts +7 -4
  216. package/src/node/package-hosts.ts +32 -2
  217. package/src/node/remote/index.ts +5 -0
  218. package/src/node/remote/pi-directories.ts +49 -0
  219. package/src/node/remote/port-forwards.ts +148 -0
  220. package/src/node/remote/remote-hosts.ts +170 -0
  221. package/src/node/remote/remote-protocol.ts +57 -0
  222. package/src/node/remote/remote-server-impl.ts +60 -0
  223. package/src/node/remote/remote.spec.ts +142 -0
  224. package/src/node/remote-ui-context.spec.ts +25 -0
  225. package/src/node/remote-ui-context.ts +34 -5
  226. package/src/node/session-files.spec.ts +52 -0
  227. package/src/node/session-files.ts +41 -0
  228. package/src/node/settings-server-impl.ts +9 -0
  229. package/src/node/settings-store.spec.ts +5 -2
  230. package/src/node/settings-store.ts +25 -6
  231. package/src/node/ta-server-impl.ts +18 -4
  232. package/src/node/ta-session-factory.ts +56 -10
  233. package/src/node/ta-session.ts +9 -1
  234. package/src/node/terminal-service.spec.ts +35 -0
  235. package/src/node/terminal-service.ts +47 -15
  236. package/src/node/terminal-util.spec.ts +31 -0
  237. package/src/node/terminal-util.ts +32 -1
  238. package/src/node/web-extension-catalog.spec.ts +1 -1
  239. package/src/node/web-extension-catalog.ts +7 -2
  240. package/src/node/working-directory.ts +35 -2
  241. package/src/node/workspace-files.ts +3 -3
  242. package/src/node/workspace-protocol.ts +2 -0
  243. package/src/node/workspace-server-impl.ts +18 -0
  244. package/src/node/workspace-service.spec.ts +11 -1
  245. package/src/node/workspace-service.ts +15 -3
@@ -1,12 +1,13 @@
1
1
  import { rmSync } from 'node:fs';
2
2
  import { join } from 'node:path';
3
3
  import { CODING_AGENT_OPTIONS, type CodingAgentOptions, type PiSdk, PiSdkUtil } from '@celljs/ai-coding-agent';
4
- import { Component, Value } from '@celljs/core';
4
+ import { Autowired, Component, Value } from '@celljs/core';
5
5
  import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
6
6
  import { TaPackages } from '@celljs/ta-harness/lib/node/ta-packages';
7
7
  import type { TaPackageInfo, TaSettingsScope, TaWebExtensionInfo, TaWebExtensionSource } from '@celljs/ta-protocol';
8
8
  import { WebExtensionCatalog } from './host-protocol';
9
9
  import { PackageSwitch } from './package-switch';
10
+ import { PiDirectories } from './remote/remote-protocol';
10
11
  import { TaErrors } from './ta-errors';
11
12
  import { type WebExtensionPackage, WebExtensionUtil } from './web-extension-util';
12
13
 
@@ -31,6 +32,9 @@ export class WebExtensionCatalogImpl implements WebExtensionCatalog {
31
32
  @Value(CODING_AGENT_OPTIONS)
32
33
  protected readonly codingAgentOptions?: CodingAgentOptions;
33
34
 
35
+ @Autowired(PiDirectories)
36
+ protected readonly directories: PiDirectories;
37
+
34
38
  async packages(cwd: string): Promise<TaPackageInfo[]> {
35
39
  const { packages } = await this.load(cwd);
36
40
  return packages.map(pkg => WebExtensionUtil.describePackage(pkg));
@@ -131,7 +135,8 @@ export class WebExtensionCatalogImpl implements WebExtensionCatalog {
131
135
  return manager.resolve(async () => 'skip');
132
136
  }
133
137
 
134
- protected async load(cwd: string): Promise<Loaded> {
138
+ protected async load(location: string): Promise<Loaded> {
139
+ const cwd = this.directories.toPi(location);
135
140
  const sdk = await PiSdkUtil.load();
136
141
  const agentDir = this.codingAgentOptions?.agentDir ?? sdk.getAgentDir();
137
142
  const trusted = new sdk.ProjectTrustStore(agentDir).get(cwd) ?? !sdk.hasTrustRequiringProjectResources(cwd);
@@ -6,12 +6,22 @@ import { TaPackages } from '@celljs/ta-harness/lib/node/ta-packages';
6
6
 
7
7
  export const TaWorkingDirectory = Symbol('TaWorkingDirectory');
8
8
 
9
- /** The working directory the user chose for ta serve (TaServer.setWorkingDirectory), kept across restarts. */
9
+ /**
10
+ * The working directory the user chose for ta serve (TaServer.setWorkingDirectory), and the hosts of the
11
+ * remote directories opened lately (TaRemoteServer), kept across restarts.
12
+ */
10
13
  export interface TaWorkingDirectory {
11
14
  get(): string | undefined;
12
15
  set(cwd: string): void;
16
+ /** The latest first. */
17
+ recentHosts(): string[];
18
+ addRecentHost(host: string): void;
19
+ removeRecentHost(host: string): void;
13
20
  }
14
21
 
22
+ /** Hosts kept in recentHosts. */
23
+ const RECENT_HOSTS = 10;
24
+
15
25
  /** In ta.json of the agent directory, beside Ta's other settings of its own; the file keeps its other keys. */
16
26
  @Component(TaWorkingDirectory)
17
27
  export class TaWorkingDirectoryImpl implements TaWorkingDirectory {
@@ -23,7 +33,30 @@ export class TaWorkingDirectoryImpl implements TaWorkingDirectory {
23
33
  }
24
34
 
25
35
  set(cwd: string): void {
26
- writeFileSync(`${this.file}.tmp`, `${JSON.stringify({ ...this.read(), workingDirectory: cwd }, null, 2)}\n`);
36
+ this.write({ workingDirectory: cwd });
37
+ }
38
+
39
+ recentHosts(): string[] {
40
+ const { remoteHosts } = this.read();
41
+ return Array.isArray(remoteHosts) ? remoteHosts.filter((host): host is string => typeof host === 'string') : [];
42
+ }
43
+
44
+ addRecentHost(host: string): void {
45
+ const hosts = this.recentHosts();
46
+ if (hosts[0] !== host) {
47
+ this.write({ remoteHosts: [host, ...hosts.filter(other => other !== host)].slice(0, RECENT_HOSTS) });
48
+ }
49
+ }
50
+
51
+ removeRecentHost(host: string): void {
52
+ const hosts = this.recentHosts();
53
+ if (hosts.includes(host)) {
54
+ this.write({ remoteHosts: hosts.filter(other => other !== host) });
55
+ }
56
+ }
57
+
58
+ protected write(changes: Record<string, unknown>): void {
59
+ writeFileSync(`${this.file}.tmp`, `${JSON.stringify({ ...this.read(), ...changes }, null, 2)}\n`);
27
60
  renameSync(`${this.file}.tmp`, this.file);
28
61
  }
29
62
 
@@ -159,7 +159,7 @@ export class DefaultWorkspaceFiles implements WorkspaceFiles, Middleware {
159
159
 
160
160
  protected async send(request: IncomingMessage, response: ServerResponse, cwd: string, path: string): Promise<void> {
161
161
  const env = this.environments.create(cwd);
162
- const file = await this.find(env, cwd, path);
162
+ const file = await this.find(env, path);
163
163
  if (!file) {
164
164
  this.fail(response, 404);
165
165
  return;
@@ -207,10 +207,10 @@ export class DefaultWorkspaceFiles implements WorkspaceFiles, Middleware {
207
207
  }
208
208
 
209
209
  /** The file at `path`, or the `index.html` of the directory there. */
210
- protected async find(env: ExecEnvironment, cwd: string, path: string): Promise<{ absolute: string; stat: FileStat } | undefined> {
210
+ protected async find(env: ExecEnvironment, path: string): Promise<{ absolute: string; stat: FileStat } | undefined> {
211
211
  let absolute: string;
212
212
  try {
213
- absolute = WorkspaceUtil.inside(cwd, path).absolute;
213
+ absolute = WorkspaceUtil.inside(env.cwd, path).absolute;
214
214
  } catch {
215
215
  return undefined;
216
216
  }
@@ -12,6 +12,8 @@ export interface TaWorkspace {
12
12
  listDirectory(cwd: string, path: string): Promise<TaDirEntry[]>;
13
13
  readFile(cwd: string, path: string): Promise<TaFileContent>;
14
14
  writeFile(cwd: string, path: string, text: string): Promise<void>;
15
+ /** Bytes to a file, the folders on its way made; with `append`, after what it holds. */
16
+ uploadFile(cwd: string, path: string, data: Uint8Array, append: boolean): Promise<void>;
15
17
  findFiles(cwd: string, query: string, limit?: number): Promise<string[]>;
16
18
  runCommand(cwd: string, command: string, args: string[], options?: TaRunCommandOptions): Promise<TaCommandOutput>;
17
19
  }
@@ -3,6 +3,7 @@ import { Rpc } from '@celljs/rpc';
3
3
  import { RpcContext } from '@celljs/rpc/lib/node';
4
4
  import { ExecEnvironmentFactory } from '@celljs/ta-exec';
5
5
  import {
6
+ TA_UPLOAD_PART_BYTES,
6
7
  type TaCommandOutput,
7
8
  type TaDirEntry,
8
9
  type TaFileContent,
@@ -10,6 +11,7 @@ import {
10
11
  type TaRunCommandOptions,
11
12
  type TaTerminalInfo,
12
13
  TaTerminalServer,
14
+ type TaUploadOptions,
13
15
  TaWorkspaceServer,
14
16
  } from '@celljs/ta-protocol';
15
17
  import { TaErrors } from './ta-errors';
@@ -40,6 +42,22 @@ export class TaWorkspaceServerImpl implements TaWorkspaceServer {
40
42
  return TaErrors.guard(async () => this.workspace.writeFile(await this.cwd(cwd), path, text));
41
43
  }
42
44
 
45
+ uploadFile(path: string, data: string, options: TaUploadOptions = {}): Promise<void> {
46
+ return TaErrors.guard(async () => {
47
+ if (typeof data !== 'string') {
48
+ throw TaErrors.invalidArgument('No bytes to write.', 'Pass the bytes of the file in base64.');
49
+ }
50
+ const bytes = Buffer.from(data, 'base64');
51
+ if (bytes.length > TA_UPLOAD_PART_BYTES) {
52
+ throw TaErrors.invalidArgument(
53
+ 'The part is too large.',
54
+ `Send the file in parts of at most ${TA_UPLOAD_PART_BYTES} bytes, each after the first with append.`,
55
+ );
56
+ }
57
+ await this.workspace.uploadFile(await this.cwd(options.cwd), path, bytes, options.append === true);
58
+ });
59
+ }
60
+
43
61
  fileUrl(path: string, cwd?: string): Promise<string> {
44
62
  return TaErrors.guard(async () => this.files.url(await this.cwd(cwd), path));
45
63
  }
@@ -1,6 +1,6 @@
1
1
  import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
2
2
  import { execSync } from 'node:child_process';
3
- import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
3
+ import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { join } from 'node:path';
6
6
  import { LocalExecEnvironment } from '@celljs/ta-exec';
@@ -77,6 +77,16 @@ describe('DefaultTaWorkspace', () => {
77
77
  await expect(workspace().writeFile(repo, '../secret.txt', 'a')).rejects.toThrow('outside');
78
78
  });
79
79
 
80
+ it('uploads bytes in parts, making the folders on the way', async () => {
81
+ await workspace().uploadFile(root, 'up/load/a.bin', new Uint8Array([0, 1, 2]), false);
82
+ await workspace().uploadFile(root, 'up/load/a.bin', new Uint8Array([3]), true);
83
+ expect([...readFileSync(join(root, 'up/load/a.bin'))]).toEqual([0, 1, 2, 3]);
84
+ await workspace().uploadFile(root, 'up/load/a.bin', new Uint8Array([9]), false);
85
+ expect([...readFileSync(join(root, 'up/load/a.bin'))]).toEqual([9]);
86
+ await expect(workspace().uploadFile(root, 'up', new Uint8Array([1]), false)).rejects.toThrow('is a directory');
87
+ await expect(workspace().uploadFile(root, '../out.bin', new Uint8Array([1]), false)).rejects.toThrow('outside');
88
+ });
89
+
80
90
  it('reads the start of a large file', async () => {
81
91
  write(join(root, 'big/large.txt'), 'x'.repeat(1024 * 1024 + 10));
82
92
  const content = await workspace().readFile(join(root, 'big'), 'large.txt');
@@ -58,7 +58,7 @@ export class DefaultTaWorkspace implements TaWorkspace {
58
58
 
59
59
  async listDirectory(cwd: string, path: string): Promise<TaDirEntry[]> {
60
60
  const env = this.environments.create(cwd);
61
- const target = WorkspaceUtil.inside(cwd, path);
61
+ const target = WorkspaceUtil.inside(env.cwd, path);
62
62
  const stat = await env.stat(target.absolute);
63
63
  if (!stat?.isDirectory) {
64
64
  throw TaErrors.invalidArgument(`${path} is not a directory.`, 'Pass the path of a directory inside the workspace.');
@@ -82,7 +82,7 @@ export class DefaultTaWorkspace implements TaWorkspace {
82
82
 
83
83
  async readFile(cwd: string, path: string): Promise<TaFileContent> {
84
84
  const env = this.environments.create(cwd);
85
- const target = WorkspaceUtil.inside(cwd, path);
85
+ const target = WorkspaceUtil.inside(env.cwd, path);
86
86
  const stat = await env.stat(target.absolute);
87
87
  if (!stat || stat.isDirectory) {
88
88
  throw TaErrors.invalidArgument(`${path} is not a file.`, 'Pass the path of a file inside the workspace.');
@@ -101,7 +101,7 @@ export class DefaultTaWorkspace implements TaWorkspace {
101
101
 
102
102
  async writeFile(cwd: string, path: string, text: string): Promise<void> {
103
103
  const env = this.environments.create(cwd);
104
- const target = WorkspaceUtil.inside(cwd, path);
104
+ const target = WorkspaceUtil.inside(env.cwd, path);
105
105
  const [stat, parent] = await Promise.all([env.stat(target.absolute), env.stat(dirname(target.absolute))]);
106
106
  if (stat?.isDirectory || !parent?.isDirectory) {
107
107
  throw TaErrors.invalidArgument(
@@ -112,6 +112,18 @@ export class DefaultTaWorkspace implements TaWorkspace {
112
112
  await env.writeFile(target.absolute, text);
113
113
  }
114
114
 
115
+ async uploadFile(cwd: string, path: string, data: Uint8Array, append: boolean): Promise<void> {
116
+ const env = this.environments.create(cwd);
117
+ const target = WorkspaceUtil.inside(env.cwd, path);
118
+ if (!target.relative || (await env.stat(target.absolute))?.isDirectory) {
119
+ throw TaErrors.invalidArgument(`${path || '.'} is a directory.`, 'Pass the path of a file inside the workspace.');
120
+ }
121
+ if (!append) {
122
+ await env.mkdir(dirname(target.absolute));
123
+ }
124
+ await env.writeFile(target.absolute, data, { append });
125
+ }
126
+
115
127
  async findFiles(cwd: string, query: string, limit = DEFAULT_FIND_LIMIT): Promise<string[]> {
116
128
  const env = this.environments.create(cwd);
117
129
  return WorkspaceUtil.fuzzyFind(await this.listFiles(env), query, Math.max(1, limit));