arri 0.36.1 → 0.37.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.
@@ -11,17 +11,17 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
11
11
 
12
12
  const consola__default = /*#__PURE__*/_interopDefaultCompat(consola);
13
13
 
14
- var __defProp$3 = Object.defineProperty;
15
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
16
- var __publicField$3 = (obj, key, value) => {
17
- __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
14
+ var __defProp$4 = Object.defineProperty;
15
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
16
+ var __publicField$4 = (obj, key, value) => {
17
+ __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
18
18
  return value;
19
19
  };
20
20
  class ArriServerError extends Error {
21
21
  constructor(err) {
22
22
  super(err.message);
23
- __publicField$3(this, "code");
24
- __publicField$3(this, "data");
23
+ __publicField$4(this, "code");
24
+ __publicField$4(this, "data");
25
25
  this.code = err.code;
26
26
  this.data = err.data;
27
27
  if (err.stack) {
@@ -267,6 +267,7 @@ function isRpc(input) {
267
267
  return typeof input === "object" && input !== null && "method" in input && arriCodegenUtils.isRpcHttpMethod(input.method) && "handler" in input && typeof input.handler === "function";
268
268
  }
269
269
  function defineRpc(config) {
270
+ config.transport = "http";
270
271
  return config;
271
272
  }
272
273
  function createHttpRpcDefinition(rpcName, httpPath, procedure) {
@@ -478,17 +479,18 @@ async function validateRpcRequestInput(event, httpMethod, schema) {
478
479
  }
479
480
  }
480
481
 
481
- var __defProp$2 = Object.defineProperty;
482
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
483
- var __publicField$2 = (obj, key, value) => {
484
- __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
482
+ var __defProp$3 = Object.defineProperty;
483
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
484
+ var __publicField$3 = (obj, key, value) => {
485
+ __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
485
486
  return value;
486
487
  };
487
488
  function defineEventStreamRpc(config) {
488
489
  return {
489
490
  ...config,
490
491
  method: config.method ?? "get",
491
- isEventStream: true
492
+ isEventStream: true,
493
+ transport: "http"
492
494
  };
493
495
  }
494
496
  function isEventStreamRpc(input) {
@@ -496,14 +498,14 @@ function isEventStreamRpc(input) {
496
498
  }
497
499
  class EventStreamConnection {
498
500
  constructor(event, opts) {
499
- __publicField$2(this, "lastEventId");
500
- __publicField$2(this, "validationErrors");
501
- __publicField$2(this, "validator");
502
- __publicField$2(this, "serializer");
501
+ __publicField$3(this, "lastEventId");
502
+ __publicField$3(this, "validationErrors");
503
+ __publicField$3(this, "validator");
504
+ __publicField$3(this, "serializer");
503
505
  // for some reason Rollup cannot output DTS when this is set to NodeJS.Timeout
504
- __publicField$2(this, "pingInterval");
505
- __publicField$2(this, "pingIntervalMs");
506
- __publicField$2(this, "eventStream");
506
+ __publicField$3(this, "pingInterval");
507
+ __publicField$3(this, "pingIntervalMs");
508
+ __publicField$3(this, "eventStream");
507
509
  this.eventStream = h3Sse.createEventStream(event);
508
510
  this.lastEventId = this.eventStream.lastEventId;
509
511
  this.pingIntervalMs = opts.pingInterval ?? 6e4;
@@ -817,19 +819,24 @@ function handleRoute(router, method, route, opts) {
817
819
  }
818
820
  }
819
821
 
820
- var __defProp$1 = Object.defineProperty;
821
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
822
- var __publicField$1 = (obj, key, value) => {
823
- __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
822
+ var __defProp$2 = Object.defineProperty;
823
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
824
+ var __publicField$2 = (obj, key, value) => {
825
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
824
826
  return value;
825
827
  };
826
828
  class ArriRouter {
827
829
  constructor() {
828
- __publicField$1(this, "procedures", []);
829
- __publicField$1(this, "routes", []);
830
- __publicField$1(this, "models", {});
830
+ __publicField$2(this, "procedures", []);
831
+ __publicField$2(this, "routes", []);
832
+ __publicField$2(this, "models", {});
831
833
  }
832
834
  rpc(procedure) {
835
+ procedure.transport = "http";
836
+ this.procedures.push(procedure);
837
+ }
838
+ wsRpc(procedure) {
839
+ procedure.transport = "ws";
833
840
  this.procedures.push(procedure);
834
841
  }
835
842
  route(route) {
@@ -851,6 +858,145 @@ class ArriRouter {
851
858
  }
852
859
  }
853
860
 
861
+ var __defProp$1 = Object.defineProperty;
862
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
863
+ var __publicField$1 = (obj, key, value) => {
864
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
865
+ return value;
866
+ };
867
+ class WsPeer {
868
+ constructor(peer, opts) {
869
+ __publicField$1(this, "_peer");
870
+ __publicField$1(this, "_validator");
871
+ __publicField$1(this, "context");
872
+ __publicField$1(this, "url");
873
+ this._peer = peer;
874
+ this._validator = opts.validator;
875
+ this.url = peer.url;
876
+ this.context = opts.context;
877
+ }
878
+ send(data) {
879
+ if (!this._validator) {
880
+ return;
881
+ }
882
+ if (!this._validator.validate(data)) {
883
+ return;
884
+ }
885
+ const payload = this._validator.serialize(data);
886
+ return this._peer.send(`type: message
887
+ data: ${payload}`);
888
+ }
889
+ sendError(err) {
890
+ return this._peer.send(`type: error
891
+ data: ${JSON.stringify(err)}`);
892
+ }
893
+ close() {
894
+ throw new Error("close() is not yet implemented on WsPeer");
895
+ }
896
+ subscribe(channel) {
897
+ this._peer.subscribe(channel);
898
+ }
899
+ unsubscribe(channel) {
900
+ this._peer.unsubscribe(channel);
901
+ }
902
+ publish(channel, message) {
903
+ if (!this._validator) {
904
+ return;
905
+ }
906
+ if (!this._validator.validate(message)) {
907
+ const err = {
908
+ code: 500,
909
+ message: `Error serializing message on server. The payload doesn't match the specified schema.`,
910
+ data: {
911
+ payload: message
912
+ }
913
+ };
914
+ this.sendError(err);
915
+ return;
916
+ }
917
+ const payload = this._validator.serialize(message);
918
+ this._peer.publish(channel, payload);
919
+ }
920
+ readyState() {
921
+ return this._peer.readyState;
922
+ }
923
+ }
924
+ function defineWebsocketRpc(def) {
925
+ def.transport = "ws";
926
+ return def;
927
+ }
928
+ function registerWebsocketRpc(router, path, rpc) {
929
+ let responseValidator;
930
+ let paramValidator;
931
+ try {
932
+ responseValidator = rpc.response ? arriValidate.a.compile(rpc.response) : void 0;
933
+ } catch (err) {
934
+ console.error("ERROR COMPILING VALIDATOR", err);
935
+ }
936
+ try {
937
+ paramValidator = rpc.params ? arriValidate.a.compile(rpc.params) : void 0;
938
+ } catch (err) {
939
+ console.error("ERROR COMPILING PARAMS", err);
940
+ }
941
+ const handler = h3.defineWebSocketHandler({
942
+ upgrade(req) {
943
+ },
944
+ open(peer) {
945
+ const urlParts = peer.url.split("?");
946
+ const context = {
947
+ rpcName: rpc.name,
948
+ clientAddress: peer.addr
949
+ };
950
+ if (urlParts.length > 1) {
951
+ urlParts.shift();
952
+ const queryStr = new URLSearchParams(urlParts.join("?"));
953
+ const query = {};
954
+ for (const [key, val] of queryStr.entries()) {
955
+ query[key] = val;
956
+ }
957
+ context.queryParams = query;
958
+ }
959
+ const wsPeer = new WsPeer(peer, {
960
+ validator: responseValidator,
961
+ context
962
+ });
963
+ peer.ctx.__wsPeer = wsPeer;
964
+ rpc.handler.onOpen(peer.ctx.__wsPeer);
965
+ },
966
+ message(peer, message) {
967
+ if (!paramValidator) {
968
+ return;
969
+ }
970
+ const data = paramValidator.safeParse(message.text());
971
+ if (!data.success) {
972
+ const errorResponse = {
973
+ code: 400,
974
+ message: data.error.message,
975
+ data: data.error.errors,
976
+ stack: data.error.stack?.split("\n")
977
+ };
978
+ peer.ctx.__wsPeer.sendError(errorResponse);
979
+ return;
980
+ }
981
+ rpc.handler.onMessage(peer.ctx.__wsPeer, data.value);
982
+ },
983
+ close(peer, details) {
984
+ rpc.handler.onClose(peer.ctx.__wsPeer, details);
985
+ }
986
+ });
987
+ router.use(path, handler);
988
+ }
989
+ function createWsRpcDefinition(rpcName, path, rpc) {
990
+ return {
991
+ transport: "ws",
992
+ path,
993
+ params: getRpcParamName(rpcName, rpc),
994
+ response: getRpcResponseName(rpcName, rpc),
995
+ isDeprecated: rpc.isDeprecated,
996
+ description: rpc.description
997
+ };
998
+ }
999
+
854
1000
  var __defProp = Object.defineProperty;
855
1001
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
856
1002
  var __publicField = (obj, key, value) => {
@@ -928,7 +1074,11 @@ class ArriApp {
928
1074
  this.route(route);
929
1075
  }
930
1076
  for (const rpc of input.getProcedures()) {
931
- this.rpc(rpc);
1077
+ if (rpc.transport === "http") {
1078
+ this.rpc(rpc);
1079
+ } else {
1080
+ this.wsRpc(rpc);
1081
+ }
932
1082
  }
933
1083
  this.registerModels(input.getModels());
934
1084
  return;
@@ -936,26 +1086,24 @@ class ArriApp {
936
1086
  this._middlewares.push(input);
937
1087
  }
938
1088
  rpc(procedure) {
939
- const path = procedure.path ?? getRpcPath(procedure.name, this._rpcRoutePrefix);
940
- this._procedures[procedure.name] = createHttpRpcDefinition(
941
- procedure.name,
942
- path,
943
- procedure
944
- );
945
- if (isRpcParamSchema(procedure.params)) {
946
- const paramName = getRpcParamName(procedure.name, procedure);
1089
+ procedure.transport = "http";
1090
+ const p = procedure;
1091
+ const path = p.path ?? getRpcPath(p.name, this._rpcRoutePrefix);
1092
+ this._procedures[p.name] = createHttpRpcDefinition(p.name, path, p);
1093
+ if (isRpcParamSchema(p.params)) {
1094
+ const paramName = getRpcParamName(p.name, p);
947
1095
  if (paramName) {
948
- this._models[paramName] = procedure.params;
1096
+ this._models[paramName] = p.params;
949
1097
  }
950
1098
  }
951
- if (isRpcParamSchema(procedure.response)) {
952
- const responseName = getRpcResponseName(procedure.name, procedure);
1099
+ if (isRpcParamSchema(p.response)) {
1100
+ const responseName = getRpcResponseName(p.name, p);
953
1101
  if (responseName) {
954
- this._models[responseName] = procedure.response;
1102
+ this._models[responseName] = p.response;
955
1103
  }
956
1104
  }
957
- if (isEventStreamRpc(procedure)) {
958
- registerEventStreamRpc(this.h3Router, path, procedure, {
1105
+ if (isEventStreamRpc(p)) {
1106
+ registerEventStreamRpc(this.h3Router, path, p, {
959
1107
  middleware: this._middlewares,
960
1108
  onRequest: this._onRequest,
961
1109
  onError: this._onError,
@@ -965,7 +1113,7 @@ class ArriApp {
965
1113
  });
966
1114
  return;
967
1115
  }
968
- registerRpc(this.h3Router, path, procedure, {
1116
+ registerRpc(this.h3Router, path, p, {
969
1117
  middleware: this._middlewares,
970
1118
  onRequest: this._onRequest,
971
1119
  onError: this._onError,
@@ -974,6 +1122,29 @@ class ArriApp {
974
1122
  debug: this._debug
975
1123
  });
976
1124
  }
1125
+ wsRpc(procedure) {
1126
+ procedure.transport = "ws";
1127
+ const p = procedure;
1128
+ const path = procedure.path ?? getRpcPath(procedure.name, this._rpcRoutePrefix);
1129
+ this._procedures[procedure.name] = createWsRpcDefinition(
1130
+ procedure.name,
1131
+ path,
1132
+ p
1133
+ );
1134
+ if (isRpcParamSchema(procedure.params)) {
1135
+ const paramName = getRpcParamName(procedure.name, p);
1136
+ if (paramName) {
1137
+ this._models[paramName] = procedure.params;
1138
+ }
1139
+ }
1140
+ if (isRpcParamSchema(procedure.response)) {
1141
+ const responseName = getRpcResponseName(procedure.name, p);
1142
+ if (responseName) {
1143
+ this._models[responseName] = procedure.response;
1144
+ }
1145
+ }
1146
+ registerWebsocketRpc(this.h3Router, path, p);
1147
+ }
977
1148
  route(route) {
978
1149
  registerRoute(this.h3Router, route, {
979
1150
  middleware: this._middlewares,
@@ -1070,14 +1241,17 @@ exports.ArriServerErrorResponse = ArriServerErrorResponse;
1070
1241
  exports.DEV_DEFINITION_ENDPOINT = DEV_DEFINITION_ENDPOINT;
1071
1242
  exports.DEV_ENDPOINT_ROOT = DEV_ENDPOINT_ROOT;
1072
1243
  exports.EventStreamConnection = EventStreamConnection;
1244
+ exports.WsPeer = WsPeer;
1073
1245
  exports.createAppDefinition = createAppDefinition;
1074
1246
  exports.createHttpRpcDefinition = createHttpRpcDefinition;
1247
+ exports.createWsRpcDefinition = createWsRpcDefinition;
1075
1248
  exports.defaultConfig = defaultConfig;
1076
1249
  exports.defineConfig = defineConfig;
1077
1250
  exports.defineError = defineError;
1078
1251
  exports.defineEventStreamRpc = defineEventStreamRpc;
1079
1252
  exports.defineRoute = defineRoute;
1080
1253
  exports.defineRpc = defineRpc;
1254
+ exports.defineWebsocketRpc = defineWebsocketRpc;
1081
1255
  exports.errorResponseFromValidationErrors = errorResponseFromValidationErrors;
1082
1256
  exports.getRpcParamName = getRpcParamName;
1083
1257
  exports.getRpcPath = getRpcPath;
@@ -1092,4 +1266,5 @@ exports.isRpcParamSchema = isRpcParamSchema;
1092
1266
  exports.registerEventStreamRpc = registerEventStreamRpc;
1093
1267
  exports.registerRoute = registerRoute;
1094
1268
  exports.registerRpc = registerRpc;
1269
+ exports.registerWebsocketRpc = registerWebsocketRpc;
1095
1270
  exports.validateRpcRequestInput = validateRpcRequestInput;