@adviser/cement 0.2.22 → 0.2.24

Sign up to get free protection for your applications and to get access to all the features.
package/index.cjs CHANGED
@@ -116,8 +116,10 @@ __export(src_exports, {
116
116
  WrapperSysAbstraction: () => WrapperSysAbstraction,
117
117
  asyncLogValue: () => asyncLogValue,
118
118
  envFactory: () => envFactory,
119
+ exception2Result: () => exception2Result,
119
120
  isURL: () => isURL,
120
121
  logValue: () => logValue,
122
+ protocols: () => protocols,
121
123
  removeSelfRef: () => removeSelfRef,
122
124
  runtimeFn: () => runtimeFn,
123
125
  toCryptoRuntime: () => toCryptoRuntime,
@@ -842,6 +844,91 @@ function WebSysAbstraction(param) {
842
844
  return new WrapperSysAbstraction(my, param);
843
845
  }
844
846
 
847
+ // src/result.ts
848
+ var Result = class _Result {
849
+ static Ok(t) {
850
+ return new ResultOK(t);
851
+ }
852
+ static Err(t) {
853
+ if (typeof t === "string") {
854
+ return new ResultError(new Error(t));
855
+ }
856
+ return new ResultError(t);
857
+ }
858
+ static Is(t) {
859
+ if (!t) {
860
+ return false;
861
+ }
862
+ if (t instanceof _Result) {
863
+ return true;
864
+ }
865
+ const rt = t;
866
+ if ([typeof rt.is_ok, typeof rt.is_err, typeof rt.unwrap, typeof rt.unwrap_err].every((x) => x === "function")) {
867
+ return true;
868
+ }
869
+ return false;
870
+ }
871
+ isOk() {
872
+ return this.is_ok();
873
+ }
874
+ isErr() {
875
+ return this.is_err();
876
+ }
877
+ Ok() {
878
+ return this.unwrap();
879
+ }
880
+ Err() {
881
+ return this.unwrap_err();
882
+ }
883
+ };
884
+ var ResultOK = class extends Result {
885
+ constructor(t) {
886
+ super();
887
+ this._t = t;
888
+ }
889
+ is_ok() {
890
+ return true;
891
+ }
892
+ is_err() {
893
+ return false;
894
+ }
895
+ unwrap_err() {
896
+ throw new Error("Result is Ok");
897
+ }
898
+ unwrap() {
899
+ return this._t;
900
+ }
901
+ };
902
+ var ResultError = class extends Result {
903
+ constructor(t) {
904
+ super();
905
+ this._error = t;
906
+ }
907
+ is_ok() {
908
+ return false;
909
+ }
910
+ is_err() {
911
+ return true;
912
+ }
913
+ unwrap() {
914
+ throw new Error(`Result is Err: ${this._error}`);
915
+ }
916
+ unwrap_err() {
917
+ return this._error;
918
+ }
919
+ };
920
+ function exception2Result(fn) {
921
+ try {
922
+ const res = fn();
923
+ if (res instanceof Promise) {
924
+ return res.then((value) => Result.Ok(value)).catch((e) => Result.Err(e));
925
+ }
926
+ return Result.Ok(res);
927
+ } catch (e) {
928
+ return Result.Err(e);
929
+ }
930
+ }
931
+
845
932
  // src/uri.ts
846
933
  function falsy2undef(value) {
847
934
  return value === void 0 || value === null ? void 0 : value;
@@ -866,16 +953,21 @@ function isURL(value) {
866
953
  var MutableURL = class _MutableURL extends URL {
867
954
  constructor(urlStr) {
868
955
  super("defect://does.not.exist");
869
- this._hasHostpart = ["http", "https"].includes(urlStr.split(":")[0]);
870
- const prefix = this._hasHostpart ? "" : "cement-";
956
+ const partedURL = urlStr.split(":");
957
+ this._hasHostpart = protocols.has(partedURL[0]);
958
+ let hostPartUrl = ["http", ...partedURL.slice(1)].join(":");
959
+ if (!this._hasHostpart) {
960
+ const pathname = hostPartUrl.replace(/http:\/\/[/]*/, "").replace(/[#?].*$/, "");
961
+ hostPartUrl = hostPartUrl.replace(/http:\/\//, `http://localhost/${pathname}`);
962
+ }
871
963
  try {
872
- this._sysURL = new URL(prefix + urlStr);
964
+ this._sysURL = new URL(hostPartUrl);
873
965
  } catch (ie) {
874
966
  const e = ie;
875
967
  e.message = `${e.message} for URL: ${urlStr}`;
876
968
  throw e;
877
969
  }
878
- this._protocol = this._sysURL.protocol.replace(new RegExp("^cement-"), "");
970
+ this._protocol = `${partedURL[0]}:`;
879
971
  if (this._hasHostpart) {
880
972
  this._pathname = this._sysURL.pathname;
881
973
  } else {
@@ -886,15 +978,35 @@ var MutableURL = class _MutableURL extends URL {
886
978
  clone() {
887
979
  return new _MutableURL(this.toString());
888
980
  }
981
+ get host() {
982
+ if (!this._hasHostpart) {
983
+ throw new Error(
984
+ `you can use hostname only if protocol is ${this.toString()} ${JSON.stringify(Array.from(protocols.keys()))}`
985
+ );
986
+ }
987
+ return this._sysURL.host;
988
+ }
989
+ get port() {
990
+ if (!this._hasHostpart) {
991
+ throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(protocols.keys()))}`);
992
+ }
993
+ return this._sysURL.port;
994
+ }
995
+ set port(p) {
996
+ if (!this._hasHostpart) {
997
+ throw new Error(`you can use port only if protocol is ${JSON.stringify(Array.from(protocols.keys()))}`);
998
+ }
999
+ this._sysURL.port = p;
1000
+ }
889
1001
  get hostname() {
890
1002
  if (!this._hasHostpart) {
891
- throw new Error("you can use hostname only if protocol is http or https");
1003
+ throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(protocols.keys()))}`);
892
1004
  }
893
1005
  return this._sysURL.hostname;
894
1006
  }
895
1007
  set hostname(h) {
896
1008
  if (!this._hasHostpart) {
897
- throw new Error("you can use hostname only if protocol is http or https");
1009
+ throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(protocols.keys()))}`);
898
1010
  }
899
1011
  this._sysURL.hostname = h;
900
1012
  }
@@ -966,6 +1078,10 @@ var BuildURI = class _BuildURI {
966
1078
  static from(strURLUri, defaultProtocol = "file:") {
967
1079
  return from((url) => new _BuildURI(url), strURLUri, defaultProtocol);
968
1080
  }
1081
+ port(p) {
1082
+ this._url.port = p;
1083
+ return this;
1084
+ }
969
1085
  hostname(h) {
970
1086
  this._url.hostname = h;
971
1087
  return this;
@@ -1027,7 +1143,20 @@ var BuildURI = class _BuildURI {
1027
1143
  return URI.from(this._url);
1028
1144
  }
1029
1145
  };
1146
+ var protocols = /* @__PURE__ */ new Map([
1147
+ ["http", true],
1148
+ ["https", true],
1149
+ ["ws", true],
1150
+ ["wss", true]
1151
+ ]);
1030
1152
  var URI = class _URI {
1153
+ static protocolHasHostpart(protocol) {
1154
+ protocol = protocol.replace(/:$/, "");
1155
+ protocols.set(protocol, true);
1156
+ return () => {
1157
+ protocols.delete(protocol);
1158
+ };
1159
+ }
1031
1160
  // if no protocol is provided, default to file:
1032
1161
  static merge(into, from2, defaultProtocol = "file:") {
1033
1162
  const intoUrl = BuildURI.from(into, defaultProtocol);
@@ -1049,6 +1178,9 @@ var URI = class _URI {
1049
1178
  static from(strURLUri, defaultProtocol = "file:") {
1050
1179
  return from((url) => new _URI(url), strURLUri, defaultProtocol);
1051
1180
  }
1181
+ static fromResult(strURLUri, defaultProtocol = "file:") {
1182
+ return exception2Result(() => from((url) => new _URI(url), strURLUri, defaultProtocol));
1183
+ }
1052
1184
  constructor(url) {
1053
1185
  this._url = url.clone();
1054
1186
  }
@@ -1061,9 +1193,12 @@ var URI = class _URI {
1061
1193
  // get password(): string {
1062
1194
  // return this._url.password;
1063
1195
  // }
1064
- // get port(): string {
1065
- // return this._url.port;
1066
- // }
1196
+ get port() {
1197
+ return this._url.port;
1198
+ }
1199
+ get host() {
1200
+ return this._url.host;
1201
+ }
1067
1202
  // get username(): string {
1068
1203
  // return this._url.username;
1069
1204
  // }
@@ -1074,7 +1209,7 @@ var URI = class _URI {
1074
1209
  return this._url.protocol;
1075
1210
  }
1076
1211
  get pathname() {
1077
- return this._url.toString().replace(/^.*:\/\//, "").replace(/\?.*$/, "");
1212
+ return this._url.pathname;
1078
1213
  }
1079
1214
  // get hash(): string {
1080
1215
  // return this._url.hash;
@@ -1438,7 +1573,13 @@ var LoggerImpl = class _LoggerImpl {
1438
1573
  }
1439
1574
  Err(err) {
1440
1575
  var _a;
1441
- if (err instanceof Error) {
1576
+ if (Result.Is(err)) {
1577
+ if (err.isOk()) {
1578
+ this.Result("noerror", err);
1579
+ } else {
1580
+ this.Result("error", err);
1581
+ }
1582
+ } else if (err instanceof Error) {
1442
1583
  this._attributes["error"] = logValue(err.message);
1443
1584
  if (this._levelHandler.isStackExposed) {
1444
1585
  this._attributes["stack"] = logValue((_a = err.stack) == null ? void 0 : _a.split("\n").map((s) => s.trim()));
@@ -1792,80 +1933,6 @@ function MockLogger(params) {
1792
1933
  };
1793
1934
  }
1794
1935
 
1795
- // src/result.ts
1796
- var Result = class _Result {
1797
- static Ok(t) {
1798
- return new ResultOK(t);
1799
- }
1800
- static Err(t) {
1801
- if (typeof t === "string") {
1802
- return new ResultError(new Error(t));
1803
- }
1804
- return new ResultError(t);
1805
- }
1806
- static Is(t) {
1807
- if (!t) {
1808
- return false;
1809
- }
1810
- if (t instanceof _Result) {
1811
- return true;
1812
- }
1813
- const rt = t;
1814
- if ([typeof rt.is_ok, typeof rt.is_err, typeof rt.unwrap, typeof rt.unwrap_err].every((x) => x === "function")) {
1815
- return true;
1816
- }
1817
- return false;
1818
- }
1819
- isOk() {
1820
- return this.is_ok();
1821
- }
1822
- isErr() {
1823
- return this.is_err();
1824
- }
1825
- Ok() {
1826
- return this.unwrap();
1827
- }
1828
- Err() {
1829
- return this.unwrap_err();
1830
- }
1831
- };
1832
- var ResultOK = class extends Result {
1833
- constructor(t) {
1834
- super();
1835
- this._t = t;
1836
- }
1837
- is_ok() {
1838
- return true;
1839
- }
1840
- is_err() {
1841
- return false;
1842
- }
1843
- unwrap_err() {
1844
- throw new Error("Result is Ok");
1845
- }
1846
- unwrap() {
1847
- return this._t;
1848
- }
1849
- };
1850
- var ResultError = class extends Result {
1851
- constructor(t) {
1852
- super();
1853
- this._error = t;
1854
- }
1855
- is_ok() {
1856
- return false;
1857
- }
1858
- is_err() {
1859
- return true;
1860
- }
1861
- unwrap() {
1862
- throw new Error(`Result is Err: ${this._error}`);
1863
- }
1864
- unwrap_err() {
1865
- return this._error;
1866
- }
1867
- };
1868
-
1869
1936
  // src/option.ts
1870
1937
  var Option = class _Option {
1871
1938
  static Some(t) {
@@ -2219,8 +2286,10 @@ function uint8array2stream(str) {
2219
2286
  WrapperSysAbstraction,
2220
2287
  asyncLogValue,
2221
2288
  envFactory,
2289
+ exception2Result,
2222
2290
  isURL,
2223
2291
  logValue,
2292
+ protocols,
2224
2293
  removeSelfRef,
2225
2294
  runtimeFn,
2226
2295
  toCryptoRuntime,