brsh 2.5.6 → 2.6.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.
@@ -1859,7 +1859,8 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
1859
1859
  };
1860
1860
  this.sandbox = new r.shell({
1861
1861
  cwd: this.context.fs.cwd,
1862
- ...e
1862
+ ...e,
1863
+ subshell: !0
1863
1864
  }), this.sandbox.context.source(this.context), this.sandbox.on("stdOut", (e) => {
1864
1865
  this.stdOut = e, this.flush();
1865
1866
  }), this.sandbox.on("stdErr", (e) => {
@@ -6261,32 +6262,36 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
6261
6262
  }), t.exports = s;
6262
6263
  })), ar = /* @__PURE__ */ t(((e, t) => {
6263
6264
  i();
6264
- var a = ae(), o = G(), s = K(), c = q(), l = W(), u = ce(), d = be(), f = He(), p = U(), m = C(), h = /\${?([\w\d]+)}?/g, g = p.EventEmitter || p.default || p;
6265
+ var a = ae(), o = G(), s = K(), c = q(), l = W(), u = ce(), d = be(), f = He(), p = U(), m = C(), h = /\$(?:\{([\w\d?#-]+)\}|([\w\d]+|[?#-]))/g, g = p.EventEmitter || p.default || p;
6265
6266
  String.prototype.regexIndexOf = function(e, t) {
6266
6267
  let n = this.substring(t || 0).search(e);
6267
6268
  return n >= 0 ? n + (t || 0) : n;
6268
6269
  }, ir().shim();
6269
6270
  var _ = class e extends g {
6270
- constructor(t) {
6271
+ constructor(e) {
6271
6272
  super();
6272
- let { path: n = "/bin", profile: r, hostname: i = "browser", filesystem: o = {}, permissions: s = {}, cwd: c = "/", onFsChange: l, useRealFilesystem: u = !1 } = t;
6273
- this.context = new a(Object.assign({}, t, {
6273
+ let { path: t = "/bin", profile: n, motd: r, command: i, interactive: o = !0, subshell: s = !1, hostname: c = "browser", filesystem: l = {}, permissions: u = {}, cwd: d = "/", onFsChange: f, useRealFilesystem: p = !1 } = e;
6274
+ if (this.context = new a(Object.assign({}, e, {
6274
6275
  clearFn: this.clear.bind(this),
6275
6276
  destroyFn: this.destroy.bind(this),
6276
- onFsChange: typeof l == "function" ? l : null
6277
+ onFsChange: typeof f == "function" ? f : null
6277
6278
  })), this._readonlyContext = Object.freeze({
6278
6279
  getVar: (e) => this.context.getVar(e),
6279
6280
  getAlias: (e) => this.context.getAlias(e),
6280
6281
  getCommand: (e) => this.context.getCommand(e)
6281
- }), this.context.setVar("PATH", n), this.context.setVar("HOST", i), u ? this.context.setNodeFilesystem(c) : this.context.setFilesystem(o, s), this.context.setCwd(c);
6282
- let d = u || this.context.fs.exists(this.context.getVar("PATH"));
6283
- this._loadDefaultCommands(), this._loadBuiltinCommands(d), this._lastCode = 0, r ? this._loadProfile(r) : setTimeout(() => this.emit("status", e.STATUS_READY), 0);
6282
+ }), this.context.setVar("PATH", t), this.context.setVar("HOST", c), !s) {
6283
+ let e = (o ? "i" : "") + (i ? "c" : "");
6284
+ e && this.context.setVar("-", e);
6285
+ }
6286
+ p ? this.context.setNodeFilesystem(d) : this.context.setFilesystem(l, u), this.context.setCwd(d);
6287
+ let m = p || this.context.fs.exists(this.context.getVar("PATH"));
6288
+ this._loadDefaultCommands(), this._loadBuiltinCommands(m), this._lastCode = 0, this._subshell = s, this._useRealFilesystem = p, this._profile = n, this._motd = r, this._initialCommand = i || null, this._suppressMotd = !!i, s ? this._initializing = !1 : (this._initializing = !0, setTimeout(() => this._init(), 0));
6284
6289
  }
6285
6290
  get path() {
6286
6291
  return this.context.getVar("PATH");
6287
6292
  }
6288
6293
  set lastCode(e) {
6289
- this.context.setVar("?", e), this._lastCode = e;
6294
+ this.context.setVar("?", String(e)), this._lastCode = e;
6290
6295
  }
6291
6296
  get lastCode() {
6292
6297
  return this._lastCode;
@@ -6322,8 +6327,74 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
6322
6327
  this.context.setCommand(e);
6323
6328
  }), e || this.context.fs.setMode(this.context.getVar("PATH"), 365);
6324
6329
  }
6325
- _loadProfile(e) {
6326
- this.onCommand(`source ${e}`);
6330
+ suppressMotd() {
6331
+ this._suppressMotd = !0;
6332
+ let e = this.context.getVar("-");
6333
+ e.includes("c") || this.context.setVar("-", e + "c");
6334
+ }
6335
+ async _init() {
6336
+ try {
6337
+ if (!this._suppressMotd && !this._initialCommand && await this._runMotd(), await this._loadProfile(this._profile), this._initialCommand) {
6338
+ await this.onCommand(this._initialCommand, !1);
6339
+ let e = this.context.getVar("-");
6340
+ e.includes("c") && this.context.setVar("-", e.replace(/c/g, ""));
6341
+ }
6342
+ } finally {
6343
+ this._initializing = !1, this.destroyed || this.emit("status", e.STATUS_READY);
6344
+ }
6345
+ }
6346
+ _hasHushlogin() {
6347
+ if (this.context.fs.exists(".hushlogin")) return !0;
6348
+ let e = this.context.getVar("HOME");
6349
+ return !!(e && this.context.fs.exists(`${e.replace(/\/$/, "")}/.hushlogin`));
6350
+ }
6351
+ async _runMotd() {
6352
+ if (!(this._motd === !1 || this._hasHushlogin())) {
6353
+ if (typeof this._motd == "string") {
6354
+ await this._renderMotdPath(this._motd);
6355
+ return;
6356
+ }
6357
+ this._useRealFilesystem || (this.context.fs.exists("/etc/update-motd.d") && await this._runMotdDir("/etc/update-motd.d"), this.context.fs.exists("/etc/motd") && await this._renderMotdPath("/etc/motd"));
6358
+ }
6359
+ }
6360
+ async _runMotdDir(e) {
6361
+ let t = this.context.fs.getFileByPath(e);
6362
+ if (!t || t.constructor !== Object) return;
6363
+ let n = Object.keys(t).sort(), r = e.endsWith("/") ? e.slice(0, -1) : e;
6364
+ for (let e of n) {
6365
+ let t = `${r}/${e}`, n = this.context.fs.stat(t);
6366
+ !n || n.isDirectory || this.context.fs.isExecutable(t) && await this.onCommand(t, !1);
6367
+ }
6368
+ }
6369
+ async _renderMotdPath(e) {
6370
+ if (!this.context.fs.exists(e)) return;
6371
+ let t = this.context.fs.stat(e);
6372
+ if (!t) return;
6373
+ if (t.isDirectory) {
6374
+ await this._runMotdDir(e);
6375
+ return;
6376
+ }
6377
+ let n = this.context.fs.getFileByPath(e);
6378
+ if (n == null) return;
6379
+ if (typeof n == "function") {
6380
+ this.context.fs.isExecutable(e) && await this.onCommand(e, !1);
6381
+ return;
6382
+ }
6383
+ if (typeof n == "string" && n.startsWith("#!")) {
6384
+ this.context.fs.isExecutable(e) ? await this.onCommand(e, !1) : await this.onCommand(`source ${e}`, !1);
6385
+ return;
6386
+ }
6387
+ let r = n.toString();
6388
+ r.endsWith("\n") && (r = r.slice(0, -1)), r.split(/\r?\n/).forEach((e) => {
6389
+ this.emit("stdOut", e);
6390
+ });
6391
+ }
6392
+ async _loadProfile(e) {
6393
+ if (!this._useRealFilesystem && e !== "/etc/profile" && this.context.fs.exists("/etc/profile")) {
6394
+ let e = this.context.fs.getFileByPath("/etc/profile");
6395
+ typeof e == "string" && e.startsWith("#!") && await this.onCommand("source /etc/profile", !1);
6396
+ }
6397
+ e && await this.onCommand(`source ${e}`, !1);
6327
6398
  }
6328
6399
  onCommand(t, n = !0) {
6329
6400
  let r = this._splitInput(t), i = this._groupPipes(r), a = !0, o = i.reduce((e, { commands: t, op: n }) => e.then(() => {
@@ -6344,7 +6415,7 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
6344
6415
  }
6345
6416
  async _runCommand(t) {
6346
6417
  if (!t || t.length < 1) return;
6347
- this.emit("status", e.STATUS_WORKING);
6418
+ this._initializing || this.emit("status", e.STATUS_WORKING);
6348
6419
  let n;
6349
6420
  try {
6350
6421
  n = await this._parseCommandLine(t);
@@ -6523,8 +6594,8 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
6523
6594
  }
6524
6595
  let i = t.matchAll(h), a = i.next();
6525
6596
  for (; !a.done;) {
6526
- let n = this.context.getVar(a.value[1]);
6527
- t = t.replace(new RegExp(e._escapeRegExp(a.value[0]), "g"), String(n)), a = i.next();
6597
+ let n = a.value[1] || a.value[2], r = this.context.getVar(n);
6598
+ t = t.replace(new RegExp(e._escapeRegExp(a.value[0]), "g"), String(r)), a = i.next();
6528
6599
  }
6529
6600
  return t;
6530
6601
  }
@@ -6541,7 +6612,8 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
6541
6612
  let t = new r.shell({
6542
6613
  cwd: this.context.fs.cwd,
6543
6614
  filesystem: this.context.fs.getRaw(),
6544
- permissions: this.context.fs.getRawPermissions()
6615
+ permissions: this.context.fs.getRawPermissions(),
6616
+ subshell: !0
6545
6617
  });
6546
6618
  t.context.source(this.context);
6547
6619
  let n = [];
@@ -6574,7 +6646,8 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
6574
6646
  let n = new r.shell({
6575
6647
  cwd: this.context.fs.cwd,
6576
6648
  filesystem: this.context.fs.getRaw(),
6577
- permissions: this.context.fs.getRawPermissions()
6649
+ permissions: this.context.fs.getRawPermissions(),
6650
+ subshell: !0
6578
6651
  });
6579
6652
  n.context.source(this.context), t !== void 0 && (n.stdin = t);
6580
6653
  let i = [];
@@ -6970,7 +7043,7 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
6970
7043
  var n = ar(), r = or(), i = r?.default ?? r, a = "/.brsh_history";
6971
7044
  t.exports = class {
6972
7045
  constructor(e) {
6973
- let { el: t, font: r = "Roboto Mono", cursor: a = "none", outputAnimation: o = "none", animateSpeed: s = 1, onExit: c = null, mobileInput: l = "click", lineWrap: u = !1 } = e;
7046
+ let { el: t, font: r = "Roboto Mono", cursor: a = "none", outputAnimation: o = "none", animateSpeed: s = 1, onExit: c = null, mobileInput: l = "click", lineWrap: u = !1, command: d = null } = e;
6974
7047
  if (!t) throw Error("you must set `el` option");
6975
7048
  if (![
6976
7049
  "none",
@@ -6979,12 +7052,12 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
6979
7052
  ].includes(a)) throw Error("cursor value must be one of: 'none', 'block', 'blink'");
6980
7053
  if (!["none", "type"].includes(o)) throw Error("outputAnimation value must be one of: 'none', 'type'");
6981
7054
  if (!Number.isInteger(s) || s < 1 || s > 20) throw Error("animateSpeed value must be between 1 and 20");
6982
- this._exitFn = c, this._cursor = a, this._outputAnimation = o, this._animateSpeed = s, this._lineWrap = u, this._working = !1, this.cli = new n(e), this._buffer = [], this._cursorPos = 0, this._history = [], this._historyPos = null, this._savedInput = null, this._historyLoaded = !1, this._el = t, this._ansi = new i(), this._ansi.use_classes = !0, this._ansi.url_whitelist = {
7055
+ this._exitFn = c, this._cursor = a, this._outputAnimation = o, this._animateSpeed = s, this._lineWrap = u, this._working = !0, this._pendingCommands = [], this.cli = new n(Object.assign({}, e, { command: void 0 })), this._buffer = [], this._cursorPos = 0, this._history = [], this._historyPos = null, this._savedInput = null, this._historyLoaded = !1, this._el = t, this._ansi = new i(), this._ansi.use_classes = !0, this._ansi.url_whitelist = {
6983
7056
  http: !0,
6984
7057
  https: !0,
6985
7058
  tel: !0,
6986
7059
  mailto: !0
6987
- }, this._setupDom(this._el, r), this._attachShellListeners(), l === "click" && this._mobileInput();
7060
+ }, this._setupDom(this._el, r), this._attachShellListeners(), d && this.onCommand(d), l === "click" && this._mobileInput();
6988
7061
  }
6989
7062
  sendKey(e, t = {}) {
6990
7063
  let { alt: n = !1, ctrl: r = !1 } = t, i = this._setCurrentLine.bind(this);
@@ -7008,7 +7081,10 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
7008
7081
  }) : this._keyEvent(i, e);
7009
7082
  }
7010
7083
  onCommand(e) {
7011
- if (!this._currentLine) return setTimeout(this.onCommand.bind(this, e), 10);
7084
+ if (!this._currentLine) {
7085
+ this.cli && this.cli.suppressMotd(), this._pendingCommands.push(e);
7086
+ return;
7087
+ }
7012
7088
  let t = this._setCurrentLine.bind(this);
7013
7089
  this._cursorPos = e.length, t(Array.from(e)), this.cli.onCommand(e);
7014
7090
  }
@@ -7091,7 +7167,14 @@ var ee, P, F, I, L, R, z, te, B, V, ne = e((() => {
7091
7167
  _statusCheck(e) {
7092
7168
  if (e === n.STATUS_READY) {
7093
7169
  if (this._interactive && this._exitInteractiveMode(), this._working = !1, this._historyLoaded ||= (this._loadHistory(), !0), this._animating) return requestAnimationFrame(this._statusCheck.bind(this, e));
7094
- this._newPrompt(this._setCurrentLine.bind(this, this._buffer));
7170
+ if (this._newPrompt(this._setCurrentLine.bind(this, this._buffer)), this._pendingCommands && this._pendingCommands.length > 0) {
7171
+ let e = this._pendingCommands.shift();
7172
+ if (this._pendingCommands.length === 0 && this.cli) {
7173
+ let e = this.cli.context.getVar("-");
7174
+ e.includes("c") && this.cli.context.setVar("-", e.replace(/c/g, ""));
7175
+ }
7176
+ this.onCommand(e);
7177
+ }
7095
7178
  } else this._clearCursor(), this._working = !0;
7096
7179
  }
7097
7180
  _loadHistory() {