@brickkit/core 0.11.0 → 0.12.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.
@@ -1,6 +1,6 @@
1
1
  import { Kind, TransformKind } from "@sinclair/typebox";
2
2
  import { Check } from "@sinclair/typebox/value";
3
- //#region src/kebab.ts
3
+ // src/kebab.ts
4
4
  /**
5
5
  * The URL segment for a declared name: `orderBilling` becomes `order-billing`,
6
6
  * `importCSV` becomes `import-csv`. Dots and existing dashes stay.
@@ -8,8 +8,7 @@ import { Check } from "@sinclair/typebox/value";
8
8
  function kebabCase(name) {
9
9
  return name.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").toLowerCase();
10
10
  }
11
- //#endregion
12
- //#region src/types.ts
11
+ // src/types.ts
13
12
  var ValidationError = class extends Error {
14
13
  code = "VALIDATION_FAILED";
15
14
  status;
@@ -51,8 +50,7 @@ var ActionExecutionError = class extends Error {
51
50
  };
52
51
  }
53
52
  };
54
- //#endregion
55
- //#region src/socket-schema.ts
53
+ // src/socket-schema.ts
56
54
  const kinds = /* @__PURE__ */ new Set([
57
55
  "Array",
58
56
  "Boolean",
@@ -118,8 +116,7 @@ function compareSocketSeq(a, b) {
118
116
  }
119
117
  const SOCKET_MAX_BYTES = 65536;
120
118
  const SOCKET_BUFFER_BYTES = 1048576;
121
- //#endregion
122
- //#region src/socket-types.ts
119
+ // src/socket-types.ts
123
120
  var BrickSocketError = class extends Error {
124
121
  kind;
125
122
  constructor(kind, message, options) {
@@ -128,8 +125,7 @@ var BrickSocketError = class extends Error {
128
125
  this.name = "BrickSocketError";
129
126
  }
130
127
  };
131
- //#endregion
132
- //#region src/socket-client.ts
128
+ // src/socket-client.ts
133
129
  /** Normal close, policy and auth refusals, protocol errors: retrying would not help. */
134
130
  const STOP_CODES = /* @__PURE__ */ new Set([
135
131
  1e3,
@@ -164,6 +160,7 @@ function connectSocket(baseUrl, contract, options) {
164
160
  const schemas = new Map(Object.entries(contract.output).map(([name, schema]) => [name, restoreSocketSchema(schema)]));
165
161
  const events = /* @__PURE__ */ new Map();
166
162
  const errors = /* @__PURE__ */ new Set();
163
+ const failures = /* @__PURE__ */ new Set();
167
164
  const closes = /* @__PURE__ */ new Set();
168
165
  const leaves = /* @__PURE__ */ new Set();
169
166
  const statuses = /* @__PURE__ */ new Set();
@@ -223,6 +220,7 @@ function connectSocket(baseUrl, contract, options) {
223
220
  events.clear();
224
221
  for (const listeners of [
225
222
  errors,
223
+ failures,
226
224
  closes,
227
225
  leaves,
228
226
  statuses,
@@ -362,6 +360,14 @@ function connectSocket(baseUrl, contract, options) {
362
360
  /** Checks a room acknowledgement or removal; returns the work to run once it passed. */
363
361
  const control = (event, data) => {
364
362
  const fields = data ?? {};
363
+ if (event === "$error") {
364
+ const { event: input, code, message, status, details } = fields;
365
+ if (typeof input !== "string" || typeof code !== "string" || typeof message !== "string" || typeof status !== "number") throw new Error("Invalid control message");
366
+ return () => {
367
+ const failure = Object.assign(new ActionExecutionError(code, message, status, details), { event: input });
368
+ for (const listener of failures) listener(failure);
369
+ };
370
+ }
365
371
  const { room, seq } = fields;
366
372
  if (!isSocketRoom(room) || seq !== void 0 && !isSocketSeq(seq)) throw new Error("Invalid control message");
367
373
  if (event === "$join" && typeof fields.ok === "boolean") {
@@ -481,6 +487,7 @@ function connectSocket(baseUrl, contract, options) {
481
487
  onStatus: (listener) => listen(statuses, listener),
482
488
  onReconnect: (listener) => listen(reconnects, listener),
483
489
  onError: (listener) => listen(errors, listener),
490
+ onHandlerError: (listener) => listen(failures, listener),
484
491
  onClose: (listener) => listen(closes, listener)
485
492
  };
486
493
  if (signal?.aborted) {
@@ -495,8 +502,7 @@ function connectSocket(baseUrl, contract, options) {
495
502
  attempt();
496
503
  return api;
497
504
  }
498
- //#endregion
499
- //#region src/client.ts
505
+ // src/client.ts
500
506
  /**
501
507
  * The request never received a meaningful server answer: DNS / connection
502
508
  * failure, a malformed body, or a 4xx response outside the declared contract
@@ -719,7 +725,6 @@ function createBrickClient(options) {
719
725
  timeoutMs: options.timeoutMs
720
726
  }, [], options.contract);
721
727
  }
722
- //#endregion
723
728
  export { isBrickTransportError as a, SOCKET_MAX_BYTES as c, isSocketRoom as d, isSocketSeq as f, kebabCase as g, ValidationError as h, isBrickServerError as i, compareSocketSeq as l, ActionExecutionError as m, BrickTransportError as n, BrickSocketError as o, serializeSocketSchema as p, createBrickClient as r, SOCKET_BUFFER_BYTES as s, BrickServerError as t, isSocketEnvelope as u };
724
729
 
725
730
  //# sourceMappingURL=client.js.map