@iso4/sandbox 0.2.0 → 0.2.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.
Files changed (2) hide show
  1. package/dist/index.mjs +68 -9
  2. package/package.json +5 -5
package/dist/index.mjs CHANGED
@@ -696,16 +696,63 @@ function decodeBridgeCallPayload(buf) {
696
696
  };
697
697
  }
698
698
  /**
699
+ * Build a `BridgeErrorPayload` from whatever a host handler threw.
700
+ *
701
+ * Own-enumerable properties beyond `name`/`message`/`stack` travel as `data`;
702
+ * properties that cannot be wire-encoded (functions, symbols, cycles, …) are
703
+ * silently dropped, mirroring the sandbox → host direction (`RunError.data`).
704
+ * @param err
705
+ */
706
+ function bridgeErrorPayloadFromUnknown(err) {
707
+ try {
708
+ if (typeof err !== "object" || err === null) return {
709
+ name: "Error",
710
+ message: String(err)
711
+ };
712
+ const obj = err;
713
+ const name = typeof obj.name === "string" && obj.name.length > 0 ? obj.name : "Error";
714
+ const message = typeof obj.message === "string" ? obj.message : String(err);
715
+ const fields = {};
716
+ let hasFields = false;
717
+ for (const key of Object.keys(obj)) {
718
+ if (key === "name" || key === "message" || key === "stack") continue;
719
+ try {
720
+ const value = obj[key];
721
+ encodeWireValue(value);
722
+ fields[key] = value;
723
+ hasFields = true;
724
+ } catch {
725
+ continue;
726
+ }
727
+ }
728
+ if (!hasFields) return {
729
+ name,
730
+ message
731
+ };
732
+ return {
733
+ name,
734
+ message,
735
+ encodedData: encodeWireValue(fields)
736
+ };
737
+ } catch {
738
+ return {
739
+ name: "Error",
740
+ message: "host handler failed"
741
+ };
742
+ }
743
+ }
744
+ /**
699
745
  * Encode a `BridgeResponsePayload` per `docs/protocol.md` §5.4.
700
746
  *
701
747
  * When `ok = true`, `encodedValue` contains pre-encoded WireValue bytes.
702
- * When `ok = false`, `errorMessage` is the handler rejection message.
748
+ * When `ok = false`, `error` describes the handler rejection. The stack slot
749
+ * is always written as absent — host stacks must not leak into the sandbox.
703
750
  * @param callId
704
751
  * @param ok
705
752
  * @param encodedValue
706
- * @param errorMessage
753
+ * @param error the handler rejection, when `ok = false`
707
754
  */
708
- function encodeBridgeResponsePayload(callId, ok, encodedValue, errorMessage) {
755
+ function encodeBridgeResponsePayload(callId, ok, encodedValue, error) {
709
756
  const w = new PayloadWriter();
710
757
  w.writeU32(callId);
711
758
  if (ok) {
@@ -717,9 +764,13 @@ function encodeBridgeResponsePayload(callId, ok, encodedValue, errorMessage) {
717
764
  } else {
718
765
  w.writeU8(0);
719
766
  w.writeString("ERR_HOST_BRIDGE");
720
- w.writeString("Error");
721
- w.writeString(errorMessage ?? "host handler failed");
767
+ w.writeString(error?.name ?? "Error");
768
+ w.writeString(error?.message ?? "host handler failed");
722
769
  w.writeU8(0);
770
+ if (error?.encodedData !== void 0 && error.encodedData.byteLength > 0) {
771
+ w.writeU8(1);
772
+ w.writeBytes(error.encodedData);
773
+ } else w.writeU8(0);
723
774
  }
724
775
  return w.toBuffer();
725
776
  }
@@ -745,7 +796,9 @@ function formatByte(byte) {
745
796
  * `RunResult`.
746
797
  */
747
798
  var RunAbortedError = class extends Error {
748
- /** The value passed to `AbortController.abort(reason)`, if any. */
799
+ /**
800
+ * The value passed to `AbortController.abort(reason)`, if any.
801
+ */
749
802
  reason;
750
803
  constructor(reason, message = "run was aborted") {
751
804
  super(message);
@@ -923,7 +976,10 @@ var RuntimeIpcClient = class RuntimeIpcClient {
923
976
  case RustToTsMessageTypes.Log: break;
924
977
  case RustToTsMessageTypes.BridgeCall: {
925
978
  const call = decodeBridgeCallPayload(frame.payload);
926
- if (dispatcher === void 0) await this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(call.callId, false, void 0, "no bridge dispatcher configured")));
979
+ if (dispatcher === void 0) await this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(call.callId, false, void 0, {
980
+ name: "Error",
981
+ message: "no bridge dispatcher configured"
982
+ })));
927
983
  else {
928
984
  const { callId } = call;
929
985
  dispatcher({
@@ -934,10 +990,13 @@ var RuntimeIpcClient = class RuntimeIpcClient {
934
990
  try {
935
991
  encoded = encodeWireValue(value);
936
992
  } catch (e) {
937
- return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, e instanceof Error ? e.message : String(e))));
993
+ return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, {
994
+ name: "Error",
995
+ message: e instanceof Error ? e.message : String(e)
996
+ })));
938
997
  }
939
998
  return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, true, encoded)));
940
- }, (err) => this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, err instanceof Error ? err.message : String(err))))).catch(() => {});
999
+ }, (err) => this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, bridgeErrorPayloadFromUnknown(err))))).catch(() => {});
941
1000
  }
942
1001
  break;
943
1002
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/sandbox",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Fast, sandboxed V8 isolate runtime for agent-generated JavaScript. Two-process architecture for crash isolation.",
6
6
  "keywords": [
@@ -47,10 +47,10 @@
47
47
  "vitest": "^4.1.5"
48
48
  },
49
49
  "optionalDependencies": {
50
- "@iso4/v8-darwin-arm64": "^0.2.0",
51
- "@iso4/v8-darwin-x64": "^0.2.0",
52
- "@iso4/v8-linux-arm64-gnu": "^0.2.0",
53
- "@iso4/v8-linux-x64-gnu": "^0.2.0"
50
+ "@iso4/v8-darwin-x64": "^0.2.1",
51
+ "@iso4/v8-darwin-arm64": "^0.2.1",
52
+ "@iso4/v8-linux-arm64-gnu": "^0.2.1",
53
+ "@iso4/v8-linux-x64-gnu": "^0.2.1"
54
54
  },
55
55
  "engines": {
56
56
  "node": ">=24.0.0"