@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/dist/index.js CHANGED
@@ -301,7 +301,7 @@ ${cmd}`;
301
301
  try {
302
302
  r = o.onStdout || o.onStderr ? await this.execStream(cmd, o) : await this.exec(cmd, o);
303
303
  } finally {
304
- if (staged && !r) await this.exec(`rm -f -- ${shellQuote(staged)}`, { noHistory: true }).catch(() => {
304
+ if (staged) await this.exec(`rm -f -- ${shellQuote(staged)}`, { noHistory: true }).catch(() => {
305
305
  });
306
306
  }
307
307
  if (r.exitCode !== 0) throw new CommandExitError(r);
@@ -1275,24 +1275,44 @@ function unsafe(name) {
1275
1275
  code: "invalid_response"
1276
1276
  });
1277
1277
  }
1278
- function components(name) {
1278
+ var RESERVED = /* @__PURE__ */ new Set([
1279
+ "CON",
1280
+ "PRN",
1281
+ "AUX",
1282
+ "NUL",
1283
+ "CONIN$",
1284
+ "CONOUT$",
1285
+ ...["COM", "LPT"].flatMap((d) => [1, 2, 3, 4, 5, 6, 7, 8, 9, "\xB9", "\xB2", "\xB3"].map((i) => `${d}${i}`))
1286
+ ]);
1287
+ var WINDOWS = globalThis.process?.platform === "win32";
1288
+ function components(name, windows = WINDOWS) {
1279
1289
  if (name.startsWith("/")) throw unsafe(name);
1280
1290
  const parts = [];
1281
1291
  for (const p of name.split("/")) {
1282
1292
  if (p === "" || p === ".") continue;
1283
1293
  if (p === ".." || p.includes("\\") || p.includes("\0")) throw unsafe(name);
1294
+ if (windows && (p.includes(":") || p.endsWith(".") || p.endsWith(" ") || RESERVED.has(p.split(".")[0].replace(/ +$/, "").toUpperCase()))) {
1295
+ throw unsafe(name);
1296
+ }
1284
1297
  parts.push(p);
1285
1298
  }
1286
1299
  return parts;
1287
1300
  }
1288
- async function unpackLocal(n, src, dest) {
1301
+ async function unpackLocal(n, src, dest, windows = WINDOWS) {
1289
1302
  await n.fsp.mkdir(dest, { recursive: true });
1290
1303
  const root = await n.fsp.realpath(dest);
1291
1304
  const gunzip = n.zlib.createGunzip();
1292
1305
  n.stream.pipeline(n.stream.Readable.from(src, { objectMode: false }), gunzip, () => {
1293
1306
  });
1294
- const dirModes = [];
1307
+ const dirModes = /* @__PURE__ */ new Map();
1295
1308
  let files = 0;
1309
+ const writable = async (abs, st) => {
1310
+ const mode = st.mode & 4095;
1311
+ if ((mode & 192) === 192 || dirModes.has(abs)) return;
1312
+ dirModes.set(abs, mode);
1313
+ await n.fsp.chmod(abs, mode | 192);
1314
+ };
1315
+ await writable(root, await n.fsp.stat(root));
1296
1316
  const ensureDir = async (parts, name) => {
1297
1317
  let cur = root;
1298
1318
  for (const p of parts) {
@@ -1305,6 +1325,7 @@ async function unpackLocal(n, src, dest) {
1305
1325
  }
1306
1326
  if (!st) await n.fsp.mkdir(cur, { mode: 493 });
1307
1327
  else if (!st.isDirectory()) throw unsafe(name);
1328
+ else await writable(cur, st);
1308
1329
  }
1309
1330
  return cur;
1310
1331
  };
@@ -1322,39 +1343,49 @@ async function unpackLocal(n, src, dest) {
1322
1343
  }
1323
1344
  await n.fsp.unlink(abs);
1324
1345
  };
1325
- for await (const e of untarStream(gunzip)) {
1326
- const parts = components(e.name);
1327
- if (parts.length === 0) {
1328
- for await (const _ of e.data) void _;
1329
- continue;
1346
+ let ok = false;
1347
+ try {
1348
+ for await (const e of untarStream(gunzip)) {
1349
+ const parts = components(e.name, windows);
1350
+ if (parts.length === 0) {
1351
+ for await (const _ of e.data) void _;
1352
+ continue;
1353
+ }
1354
+ const parent = await ensureDir(parts.slice(0, -1), e.name);
1355
+ const abs = n.path.join(parent, parts[parts.length - 1]);
1356
+ if (e.type === "dir") {
1357
+ await ensureDir(parts, e.name);
1358
+ dirModes.set(abs, e.mode);
1359
+ } else if (e.type === "file") {
1360
+ await clear(abs, e.name);
1361
+ await n.sp.pipeline(
1362
+ n.stream.Readable.from(e.data, { objectMode: false }),
1363
+ n.fs.createWriteStream(abs, { flags: "wx", mode: e.mode | 128 })
1364
+ );
1365
+ await n.fsp.chmod(abs, e.mode);
1366
+ files++;
1367
+ } else if (e.type === "symlink") {
1368
+ await clear(abs, e.name);
1369
+ await n.fsp.symlink(e.linkname, abs);
1370
+ } else if (e.type === "link") {
1371
+ const tparts = components(e.linkname, windows);
1372
+ const target = n.path.join(await ensureDir(tparts.slice(0, -1), e.linkname), tparts[tparts.length - 1] ?? "");
1373
+ const st = await n.fsp.lstat(target).catch(() => void 0);
1374
+ if (!st?.isFile()) throw unsafe(e.linkname);
1375
+ await clear(abs, e.name);
1376
+ await n.fsp.copyFile(target, abs);
1377
+ files++;
1378
+ }
1330
1379
  }
1331
- const parent = await ensureDir(parts.slice(0, -1), e.name);
1332
- const abs = n.path.join(parent, parts[parts.length - 1]);
1333
- if (e.type === "dir") {
1334
- await ensureDir(parts, e.name);
1335
- dirModes.push([abs, e.mode]);
1336
- } else if (e.type === "file") {
1337
- await clear(abs, e.name);
1338
- await n.sp.pipeline(
1339
- n.stream.Readable.from(e.data, { objectMode: false }),
1340
- n.fs.createWriteStream(abs, { flags: "wx", mode: e.mode | 128 })
1341
- );
1342
- await n.fsp.chmod(abs, e.mode);
1343
- files++;
1344
- } else if (e.type === "symlink") {
1345
- await clear(abs, e.name);
1346
- await n.fsp.symlink(e.linkname, abs);
1347
- } else if (e.type === "link") {
1348
- const tparts = components(e.linkname);
1349
- const target = n.path.join(await ensureDir(tparts.slice(0, -1), e.linkname), tparts[tparts.length - 1] ?? "");
1350
- const st = await n.fsp.lstat(target).catch(() => void 0);
1351
- if (!st?.isFile()) throw unsafe(e.linkname);
1352
- await clear(abs, e.name);
1353
- await n.fsp.copyFile(target, abs);
1354
- files++;
1355
- }
1356
- }
1357
- for (const [abs, mode] of dirModes.reverse()) await n.fsp.chmod(abs, mode);
1380
+ ok = true;
1381
+ } finally {
1382
+ const depth = (p) => p.split(n.path.sep).length;
1383
+ for (const [abs, mode] of [...dirModes].sort((a, b) => depth(b[0]) - depth(a[0]))) {
1384
+ await n.fsp.chmod(abs, mode).catch((err) => {
1385
+ if (ok) throw err;
1386
+ });
1387
+ }
1388
+ }
1358
1389
  return files;
1359
1390
  }
1360
1391