@glodon-aiot/workspace-sandbox 0.2.0-alpha.5 → 0.2.0-alpha.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glodon-aiot/workspace-sandbox",
3
- "version": "0.2.0-alpha.5",
3
+ "version": "0.2.0-alpha.6",
4
4
  "description": "供端侧宿主调用:在工程目录内读写文件,并在 Microsoft Execution Containers 里执行命令和脚本。",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/listen.mjs CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  import { sandboxError } from './policy.mjs';
19
19
  import { runExec } from './exec.mjs';
20
20
  import { installSkillZip, pruneSkills, readSkillFile, skillStatus, SKILL_ZIP_LIMIT } from './skills.mjs';
21
- import { cacheBase, rootHash } from './view.mjs';
21
+ import { dataCacheRoot, rootHash } from './view.mjs';
22
22
 
23
23
  export const LISTEN_PROTOCOL = 1;
24
24
  const MAX_TEXT = 256 * 1024;
@@ -82,7 +82,7 @@ function hostAllowed(host, port) {
82
82
  }
83
83
 
84
84
  async function cleanupPartials(rootReal) {
85
- const file = join(cacheBase(), rootHash(rootReal), 'partials.json');
85
+ const file = join(dataCacheRoot(), rootHash(rootReal), 'partials.json');
86
86
  let paths = [];
87
87
  try {
88
88
  paths = JSON.parse(await readFileText(file));
@@ -98,7 +98,7 @@ function readFileText(file) {
98
98
  }
99
99
 
100
100
  async function rememberPartial(rootReal, path) {
101
- const dir = join(cacheBase(), rootHash(rootReal));
101
+ const dir = join(dataCacheRoot(), rootHash(rootReal));
102
102
  await mkdir(dir, { recursive: true });
103
103
  const file = join(dir, 'partials.json');
104
104
  let paths = [];
@@ -472,12 +472,24 @@ export function runListen({
472
472
  await sendJson(socket, { protocol: LISTEN_PROTOCOL, id: message.id, ...failure(error) });
473
473
  return;
474
474
  }
475
- if (message.createDirectories === true) await mkdir(dirname(real), { recursive: true });
476
- fileOps += 1;
477
- writeActive = true;
478
- let settled = false;
475
+ let counted = false;
476
+ let forget;
479
477
  const temporary = `${real}.${randomBytes(6).toString('hex')}.partial`;
480
- const forget = await rememberPartial(rootReal, temporary);
478
+ try {
479
+ if (message.createDirectories === true) await mkdir(dirname(real), { recursive: true });
480
+ fileOps += 1;
481
+ writeActive = true;
482
+ counted = true;
483
+ forget = await rememberPartial(rootReal, temporary);
484
+ } catch (error) {
485
+ if (counted) {
486
+ fileOps -= 1;
487
+ writeActive = false;
488
+ }
489
+ await sendJson(socket, { protocol: LISTEN_PROTOCOL, id: message.id, ...failure(error) });
490
+ return;
491
+ }
492
+ let settled = false;
481
493
  let received = 0;
482
494
  let timer;
483
495
  const fail = async (error) => {
package/src/python.mjs CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  truncateOutput,
14
14
  } from './policy.mjs';
15
15
  import { startPypiProxy } from './pypiProxy.mjs';
16
- import { cacheBase, rootHash } from './view.mjs';
16
+ import { dataCacheRoot, rootHash } from './view.mjs';
17
17
 
18
18
  const INSTALL_TIMEOUT_MS = 90_000;
19
19
  const CERT_DIRECTORIES = ['/etc/ssl', '/private/etc/ssl', '/opt/homebrew/etc/openssl@3', '/opt/homebrew/etc/ca-certificates'];
@@ -211,7 +211,7 @@ export async function preparePython({ containment, rootReal, python, tmp }) {
211
211
  if (packages.length === 0) {
212
212
  return { python: pythonReal, readonlyPaths: [prefix, ...interpreterLibs], note: '' };
213
213
  }
214
- const cacheHome = join(cacheBase(), rootHash(rootReal, pythonReal));
214
+ const cacheHome = join(dataCacheRoot(), rootHash(rootReal, pythonReal));
215
215
  const venvDir = join(cacheHome, 'venv');
216
216
  const venvPython =
217
217
  process.platform === 'win32' ? join(venvDir, 'Scripts', 'python.exe') : join(venvDir, 'bin', 'python');
package/src/skills.mjs CHANGED
@@ -5,6 +5,7 @@ import { homedir, userInfo } from 'node:os';
5
5
  import { isAbsolute, join, relative, resolve, sep } from 'node:path';
6
6
  import { inflateRawSync } from 'node:zlib';
7
7
  import { isSafeRelative, sandboxError } from './policy.mjs';
8
+ import { windowsDataCache } from './view.mjs';
8
9
 
9
10
  export const SKILL_ZIP_LIMIT = 20 * 1024 * 1024;
10
11
  export const SKILL_ENTRY_LIMIT = 300;
@@ -13,10 +14,7 @@ const ID_PATTERN = /^[A-Za-z0-9._-]+$/;
13
14
 
14
15
  /** 技能缓存根。与工程视图缓存分开,不因盘符或空格改到临时目录。 */
15
16
  export function skillCacheRoot() {
16
- if (process.platform === 'win32') {
17
- const base = process.env.LOCALAPPDATA || join(homedir(), 'AppData', 'Local');
18
- return join(base, 'glodon-workspace-sandbox');
19
- }
17
+ if (process.platform === 'win32') return windowsDataCache();
20
18
  if (process.platform === 'darwin') return join(homedir(), 'Library', 'Caches', 'glodon-workspace-sandbox');
21
19
  const xdg = process.env.XDG_CACHE_HOME;
22
20
  if (typeof xdg === 'string' && isAbsolute(xdg)) return join(xdg, 'glodon-workspace-sandbox');
package/src/view.mjs CHANGED
@@ -32,6 +32,23 @@ export function cacheBase() {
32
32
  throw sandboxError('NotAllowedError', '缓存目录路径含有特殊字符,无法建立沙箱视图。');
33
33
  }
34
34
 
35
+ /**
36
+ * Windows 上的数据缓存。盘符、反斜杠和空格都保留。
37
+ * 只给写入记账和 venv 用,不拼进 shell。技能缓存使用同一目录。
38
+ */
39
+ export function windowsDataCache(env = process.env, home = homedir()) {
40
+ const base = env.LOCALAPPDATA || join(home, 'AppData', 'Local');
41
+ return join(base, 'glodon-workspace-sandbox');
42
+ }
43
+
44
+ /** 写入记账和 venv 的目录。macOS、Linux 仍用可拼进命令的视图缓存。 */
45
+ export function dataCacheRoot() {
46
+ if (process.platform !== 'win32') return cacheBase();
47
+ const dir = windowsDataCache();
48
+ mkdirSync(dir, { recursive: true });
49
+ return realpathSync(dir);
50
+ }
51
+
35
52
  export function rootHash(...parts) {
36
53
  const hash = createHash('sha256');
37
54
  parts.forEach((part, index) => {