capnweb 0.0.0-6e5c562 → 0.0.0-6f790b5

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
@@ -199,7 +199,7 @@ The following types can be passed over RPC (in arguments or return values), and
199
199
  * Arrays
200
200
  * `bigint`
201
201
  * `Date`
202
- * `Uint8Array`
202
+ * `ArrayBuffer`, `DataView`, and typed arrays
203
203
  * `Error` and its well-known subclasses
204
204
  * `Blob`
205
205
  * `ReadableStream` and `WritableStream`, with automatic flow control.
@@ -207,7 +207,6 @@ The following types can be passed over RPC (in arguments or return values), and
207
207
 
208
208
  The following types are not supported as of this writing, but may be added in the future:
209
209
  * `Map` and `Set`
210
- * `ArrayBuffer` and typed arrays other than `Uint8Array`
211
210
  * `RegExp`
212
211
 
213
212
  The following are intentionally NOT supported:
@@ -43,7 +43,19 @@ function typeForRpc(value) {
43
43
  case Array.prototype: return "array";
44
44
  case Date.prototype: return "date";
45
45
  case Uint8Array.prototype:
46
- case BUFFER_PROTOTYPE: return "bytes";
46
+ case BUFFER_PROTOTYPE:
47
+ case ArrayBuffer.prototype:
48
+ case DataView.prototype:
49
+ case Int8Array.prototype:
50
+ case Uint8ClampedArray.prototype:
51
+ case Int16Array.prototype:
52
+ case Uint16Array.prototype:
53
+ case Int32Array.prototype:
54
+ case Uint32Array.prototype:
55
+ case BigInt64Array.prototype:
56
+ case BigUint64Array.prototype:
57
+ case Float32Array.prototype:
58
+ case Float64Array.prototype: return "bytes";
47
59
  case WritableStream.prototype: return "writable";
48
60
  case ReadableStream.prototype: return "readable";
49
61
  case Headers.prototype: return "headers";
@@ -937,6 +949,71 @@ const DEFAULT_LIMITS = {
937
949
  maxDepth: 256,
938
950
  maxMessageSize: 32 * 1024 * 1024
939
951
  };
952
+ const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
953
+ const BYTE_CONTAINER_TYPE_NAMES = [
954
+ "ArrayBuffer",
955
+ "DataView",
956
+ "Int8Array",
957
+ "Uint8Array",
958
+ "Uint8ClampedArray",
959
+ "Int16Array",
960
+ "Uint16Array",
961
+ "Int32Array",
962
+ "Uint32Array",
963
+ "BigInt64Array",
964
+ "BigUint64Array",
965
+ "Float32Array",
966
+ "Float64Array"
967
+ ];
968
+ function isValidByteContainerName(value) {
969
+ return BYTE_CONTAINER_TYPE_NAMES.includes(value);
970
+ }
971
+ const TYPED_ARRAY_ELEMENT_SIZE = {
972
+ ArrayBuffer: void 0,
973
+ DataView: void 0,
974
+ Int8Array: void 0,
975
+ Uint8Array: void 0,
976
+ Uint8ClampedArray: void 0,
977
+ Int16Array: 2,
978
+ Uint16Array: 2,
979
+ Int32Array: 4,
980
+ Uint32Array: 4,
981
+ BigInt64Array: 8,
982
+ BigUint64Array: 8,
983
+ Float32Array: 4,
984
+ Float64Array: 8
985
+ };
986
+ const BYTE_CONTAINER_PROTOTYPES = {
987
+ ArrayBuffer: ArrayBuffer.prototype,
988
+ DataView: DataView.prototype,
989
+ Int8Array: Int8Array.prototype,
990
+ Uint8ClampedArray: Uint8ClampedArray.prototype,
991
+ Int16Array: Int16Array.prototype,
992
+ Uint16Array: Uint16Array.prototype,
993
+ Int32Array: Int32Array.prototype,
994
+ Uint32Array: Uint32Array.prototype,
995
+ BigInt64Array: BigInt64Array.prototype,
996
+ BigUint64Array: BigUint64Array.prototype,
997
+ Float32Array: Float32Array.prototype,
998
+ Float64Array: Float64Array.prototype
999
+ };
1000
+ const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
1001
+ for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
1002
+ function swapByteOrder(bytes, elementSize) {
1003
+ if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
1004
+ let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1005
+ for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
1006
+ case 2:
1007
+ view.setUint16(offset, view.getUint16(offset, false), true);
1008
+ break;
1009
+ case 4:
1010
+ view.setUint32(offset, view.getUint32(offset, false), true);
1011
+ break;
1012
+ case 8:
1013
+ view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1014
+ break;
1015
+ }
1016
+ }
940
1017
  var NullExporter = class {
941
1018
  exportStub(stub) {
942
1019
  throw new Error("Cannot serialize RPC stubs without an RPC session.");
@@ -956,7 +1033,8 @@ async function streamToBlob(stream, type) {
956
1033
  let b = await new Response(stream).blob();
957
1034
  return b.type === type ? b : b.slice(0, b.size, type);
958
1035
  }
959
- const ERROR_TYPES = Object.assign(Object.create(null), {
1036
+ const ERROR_TYPES = {
1037
+ __proto__: null,
960
1038
  Error,
961
1039
  EvalError,
962
1040
  RangeError,
@@ -965,7 +1043,7 @@ const ERROR_TYPES = Object.assign(Object.create(null), {
965
1043
  TypeError,
966
1044
  URIError,
967
1045
  AggregateError
968
- });
1046
+ };
969
1047
  var Devaluator = class Devaluator {
970
1048
  exporter;
971
1049
  source;
@@ -1027,17 +1105,38 @@ var Devaluator = class Devaluator {
1027
1105
  return ["date", Number.isNaN(time) ? null : time];
1028
1106
  }
1029
1107
  case "bytes": {
1030
- let bytes = value;
1031
- if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return ["bytes", bytes];
1032
- if (bytes.toBase64) return ["bytes", bytes.toBase64({ omitPadding: true })];
1108
+ let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
1109
+ let bytes;
1110
+ if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
1111
+ else if (alternateTypeName === void 0) bytes = value;
1112
+ else {
1113
+ let view = value;
1114
+ bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
1115
+ let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
1116
+ if (!NATIVE_LITTLE_ENDIAN && elementSize) {
1117
+ bytes = bytes.slice();
1118
+ swapByteOrder(bytes, elementSize);
1119
+ }
1120
+ }
1121
+ if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
1122
+ "bytes",
1123
+ bytes,
1124
+ alternateTypeName
1125
+ ];
1033
1126
  let b64;
1034
- if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
1127
+ if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
1128
+ else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
1035
1129
  else {
1036
1130
  let binary = "";
1037
1131
  for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
1038
1132
  b64 = btoa(binary);
1039
1133
  }
1040
- return ["bytes", b64.replace(/=+$/, "")];
1134
+ b64 = b64.replace(/=+$/, "");
1135
+ return alternateTypeName === void 0 ? ["bytes", b64] : [
1136
+ "bytes",
1137
+ b64,
1138
+ alternateTypeName
1139
+ ];
1041
1140
  }
1042
1141
  case "headers": return ["headers", [...value]];
1043
1142
  case "request": {
@@ -1290,18 +1389,46 @@ var Evaluator = class Evaluator {
1290
1389
  if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
1291
1390
  if (typeof value[1] == "number") return new Date(value[1]);
1292
1391
  break;
1293
- case "bytes":
1294
- if (value[1] instanceof Uint8Array) return value[1];
1295
- if (typeof value[1] == "string") if (typeof Buffer !== "undefined") return Buffer.from(value[1], "base64");
1296
- else if (Uint8Array.fromBase64) return Uint8Array.fromBase64(value[1]);
1392
+ case "bytes": {
1393
+ let bytes;
1394
+ if (value[1] instanceof Uint8Array) bytes = value[1];
1395
+ else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1396
+ else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1297
1397
  else {
1298
1398
  let bs = atob(value[1]);
1299
1399
  let len = bs.length;
1300
- let bytes = new Uint8Array(len);
1400
+ bytes = new Uint8Array(len);
1301
1401
  for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1302
- return bytes;
1303
1402
  }
1304
- break;
1403
+ else break;
1404
+ if (value.length === 2) return bytes;
1405
+ if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
1406
+ if (!isValidByteContainerName(value[2])) {
1407
+ let marker = value[2].slice(0, 64);
1408
+ throw new TypeError(`Unknown bytes type marker: ${marker}`);
1409
+ }
1410
+ let marker = value[2];
1411
+ let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
1412
+ if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
1413
+ let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
1414
+ if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
1415
+ switch (marker) {
1416
+ case "ArrayBuffer": return buffer;
1417
+ case "DataView": return new DataView(buffer);
1418
+ case "Int8Array": return new Int8Array(buffer);
1419
+ case "Uint8Array": return new Uint8Array(buffer);
1420
+ case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
1421
+ case "Int16Array": return new Int16Array(buffer);
1422
+ case "Uint16Array": return new Uint16Array(buffer);
1423
+ case "Int32Array": return new Int32Array(buffer);
1424
+ case "Uint32Array": return new Uint32Array(buffer);
1425
+ case "BigInt64Array": return new BigInt64Array(buffer);
1426
+ case "BigUint64Array": return new BigUint64Array(buffer);
1427
+ case "Float32Array": return new Float32Array(buffer);
1428
+ case "Float64Array": return new Float64Array(buffer);
1429
+ default:
1430
+ }
1431
+ }
1305
1432
  case "error":
1306
1433
  if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
1307
1434
  let cls = ERROR_TYPES[value[1]] || Error;
@@ -1551,10 +1678,6 @@ var ImportTableEntry = class {
1551
1678
  resolution;
1552
1679
  onBrokenRegistrations;
1553
1680
  resolve(resolution) {
1554
- if (this.resolution) {
1555
- resolution.dispose();
1556
- return;
1557
- }
1558
1681
  if (this.localRefcount == 0) {
1559
1682
  resolution.dispose();
1560
1683
  return;
@@ -2154,6 +2277,8 @@ var RpcSession$1 = class {
2154
2277
 
2155
2278
  //#endregion
2156
2279
  //#region src/websocket.ts
2280
+ /** Close-frame reason max UTF-8 bytes: 125 payload − 2-byte status (RFC 6455 §5.5). */
2281
+ const MAX_CLOSE_REASON_BYTES = 123;
2157
2282
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2158
2283
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
2159
2284
  return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();