@insforge/instabox-sdk 0.7.0 → 0.7.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.
package/README.md CHANGED
@@ -170,7 +170,7 @@ const { stdout } = await sandbox.commands.run('sha256sum', { stdin: new Uint8Arr
170
170
  - What `download` brings: everything under `remoteDir`, dot files and empty directories included.
171
171
  - Symlinks are copied as symlinks (their target text as is), never followed. Sockets, FIFOs and devices are skipped. File and directory permission bits are kept; owners are not (files belong to the user that unpacks them).
172
172
  - The destination and its parents are created. Existing files there are replaced; files the archive does not name are kept (a merge, not a mirror).
173
- - `download` refuses an archive entry that would land outside `localDir` (an absolute path, `..`, or a path through a symlink), with `invalid_response`.
173
+ - `download` refuses an archive entry that would land outside `localDir` (an absolute path, `..`, or a path through a symlink), with `invalid_response` On Windows it also refuses names Windows would not keep as they are: a `:` anywhere, a device name (`CON`, `NUL.txt`, `COM1`...) or a trailing dot or space.
174
174
  - Both return a summary: `upload` resolves to `{ path, files, bytes }` (the resolved sandbox directory, regular files sent, archive size); `download` to `{ path, remotePath, files, bytes }`.
175
175
  - Errors: a missing local directory throws `local_not_found`, a local error `local_io_error`; a missing sandbox directory `not_found` (404); a file where a directory was expected `not_a_directory`; a failure of `tar` in the sandbox `transfer_failed` (`permission_denied`, 403, when it was refused access). Every error is an `InstaboxError`.
176
176
  - They need Node.js (or Bun, Deno) for the local file system; in a browser or a Worker they throw `unsupported`. `read`, `readStream`, `write` and everything else work there too.
package/dist/index.cjs CHANGED
@@ -355,7 +355,7 @@ ${cmd}`;
355
355
  try {
356
356
  r = o.onStdout || o.onStderr ? await this.execStream(cmd, o) : await this.exec(cmd, o);
357
357
  } finally {
358
- if (staged && !r) await this.exec(`rm -f -- ${shellQuote(staged)}`, { noHistory: true }).catch(() => {
358
+ if (staged) await this.exec(`rm -f -- ${shellQuote(staged)}`, { noHistory: true }).catch(() => {
359
359
  });
360
360
  }
361
361
  if (r.exitCode !== 0) throw new CommandExitError(r);
@@ -1329,24 +1329,44 @@ function unsafe(name) {
1329
1329
  code: "invalid_response"
1330
1330
  });
1331
1331
  }
1332
- function components(name) {
1332
+ var RESERVED = /* @__PURE__ */ new Set([
1333
+ "CON",
1334
+ "PRN",
1335
+ "AUX",
1336
+ "NUL",
1337
+ "CONIN$",
1338
+ "CONOUT$",
1339
+ ...["COM", "LPT"].flatMap((d) => [1, 2, 3, 4, 5, 6, 7, 8, 9, "\xB9", "\xB2", "\xB3"].map((i) => `${d}${i}`))
1340
+ ]);
1341
+ var WINDOWS = globalThis.process?.platform === "win32";
1342
+ function components(name, windows = WINDOWS) {
1333
1343
  if (name.startsWith("/")) throw unsafe(name);
1334
1344
  const parts = [];
1335
1345
  for (const p of name.split("/")) {
1336
1346
  if (p === "" || p === ".") continue;
1337
1347
  if (p === ".." || p.includes("\\") || p.includes("\0")) throw unsafe(name);
1348
+ if (windows && (p.includes(":") || p.endsWith(".") || p.endsWith(" ") || RESERVED.has(p.split(".")[0].replace(/ +$/, "").toUpperCase()))) {
1349
+ throw unsafe(name);
1350
+ }
1338
1351
  parts.push(p);
1339
1352
  }
1340
1353
  return parts;
1341
1354
  }
1342
- async function unpackLocal(n, src, dest) {
1355
+ async function unpackLocal(n, src, dest, windows = WINDOWS) {
1343
1356
  await n.fsp.mkdir(dest, { recursive: true });
1344
1357
  const root = await n.fsp.realpath(dest);
1345
1358
  const gunzip = n.zlib.createGunzip();
1346
1359
  n.stream.pipeline(n.stream.Readable.from(src, { objectMode: false }), gunzip, () => {
1347
1360
  });
1348
- const dirModes = [];
1361
+ const dirModes = /* @__PURE__ */ new Map();
1349
1362
  let files = 0;
1363
+ const writable = async (abs, st) => {
1364
+ const mode = st.mode & 4095;
1365
+ if ((mode & 192) === 192 || dirModes.has(abs)) return;
1366
+ dirModes.set(abs, mode);
1367
+ await n.fsp.chmod(abs, mode | 192);
1368
+ };
1369
+ await writable(root, await n.fsp.stat(root));
1350
1370
  const ensureDir = async (parts, name) => {
1351
1371
  let cur = root;
1352
1372
  for (const p of parts) {
@@ -1359,6 +1379,7 @@ async function unpackLocal(n, src, dest) {
1359
1379
  }
1360
1380
  if (!st) await n.fsp.mkdir(cur, { mode: 493 });
1361
1381
  else if (!st.isDirectory()) throw unsafe(name);
1382
+ else await writable(cur, st);
1362
1383
  }
1363
1384
  return cur;
1364
1385
  };
@@ -1376,39 +1397,49 @@ async function unpackLocal(n, src, dest) {
1376
1397
  }
1377
1398
  await n.fsp.unlink(abs);
1378
1399
  };
1379
- for await (const e of untarStream(gunzip)) {
1380
- const parts = components(e.name);
1381
- if (parts.length === 0) {
1382
- for await (const _ of e.data) void _;
1383
- continue;
1400
+ let ok = false;
1401
+ try {
1402
+ for await (const e of untarStream(gunzip)) {
1403
+ const parts = components(e.name, windows);
1404
+ if (parts.length === 0) {
1405
+ for await (const _ of e.data) void _;
1406
+ continue;
1407
+ }
1408
+ const parent = await ensureDir(parts.slice(0, -1), e.name);
1409
+ const abs = n.path.join(parent, parts[parts.length - 1]);
1410
+ if (e.type === "dir") {
1411
+ await ensureDir(parts, e.name);
1412
+ dirModes.set(abs, e.mode);
1413
+ } else if (e.type === "file") {
1414
+ await clear(abs, e.name);
1415
+ await n.sp.pipeline(
1416
+ n.stream.Readable.from(e.data, { objectMode: false }),
1417
+ n.fs.createWriteStream(abs, { flags: "wx", mode: e.mode | 128 })
1418
+ );
1419
+ await n.fsp.chmod(abs, e.mode);
1420
+ files++;
1421
+ } else if (e.type === "symlink") {
1422
+ await clear(abs, e.name);
1423
+ await n.fsp.symlink(e.linkname, abs);
1424
+ } else if (e.type === "link") {
1425
+ const tparts = components(e.linkname, windows);
1426
+ const target = n.path.join(await ensureDir(tparts.slice(0, -1), e.linkname), tparts[tparts.length - 1] ?? "");
1427
+ const st = await n.fsp.lstat(target).catch(() => void 0);
1428
+ if (!st?.isFile()) throw unsafe(e.linkname);
1429
+ await clear(abs, e.name);
1430
+ await n.fsp.copyFile(target, abs);
1431
+ files++;
1432
+ }
1384
1433
  }
1385
- const parent = await ensureDir(parts.slice(0, -1), e.name);
1386
- const abs = n.path.join(parent, parts[parts.length - 1]);
1387
- if (e.type === "dir") {
1388
- await ensureDir(parts, e.name);
1389
- dirModes.push([abs, e.mode]);
1390
- } else if (e.type === "file") {
1391
- await clear(abs, e.name);
1392
- await n.sp.pipeline(
1393
- n.stream.Readable.from(e.data, { objectMode: false }),
1394
- n.fs.createWriteStream(abs, { flags: "wx", mode: e.mode | 128 })
1395
- );
1396
- await n.fsp.chmod(abs, e.mode);
1397
- files++;
1398
- } else if (e.type === "symlink") {
1399
- await clear(abs, e.name);
1400
- await n.fsp.symlink(e.linkname, abs);
1401
- } else if (e.type === "link") {
1402
- const tparts = components(e.linkname);
1403
- const target = n.path.join(await ensureDir(tparts.slice(0, -1), e.linkname), tparts[tparts.length - 1] ?? "");
1404
- const st = await n.fsp.lstat(target).catch(() => void 0);
1405
- if (!st?.isFile()) throw unsafe(e.linkname);
1406
- await clear(abs, e.name);
1407
- await n.fsp.copyFile(target, abs);
1408
- files++;
1409
- }
1410
- }
1411
- for (const [abs, mode] of dirModes.reverse()) await n.fsp.chmod(abs, mode);
1434
+ ok = true;
1435
+ } finally {
1436
+ const depth = (p) => p.split(n.path.sep).length;
1437
+ for (const [abs, mode] of [...dirModes].sort((a, b) => depth(b[0]) - depth(a[0]))) {
1438
+ await n.fsp.chmod(abs, mode).catch((err) => {
1439
+ if (ok) throw err;
1440
+ });
1441
+ }
1442
+ }
1412
1443
  return files;
1413
1444
  }
1414
1445