@adviser/cement 0.2.21 → 0.2.23

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,9 +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._sysURL = new URL(urlStr);
870
- this._protocol = this._sysURL.protocol;
871
- this._hasHostpart = ["http:", "https:"].includes(this._protocol);
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
+ }
963
+ try {
964
+ this._sysURL = new URL(hostPartUrl);
965
+ } catch (ie) {
966
+ const e = ie;
967
+ e.message = `${e.message} for URL: ${urlStr}`;
968
+ throw e;
969
+ }
970
+ this._protocol = `${partedURL[0]}:`;
872
971
  if (this._hasHostpart) {
873
972
  this._pathname = this._sysURL.pathname;
874
973
  } else {
@@ -879,15 +978,35 @@ var MutableURL = class _MutableURL extends URL {
879
978
  clone() {
880
979
  return new _MutableURL(this.toString());
881
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
+ }
882
1001
  get hostname() {
883
1002
  if (!this._hasHostpart) {
884
- 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()))}`);
885
1004
  }
886
1005
  return this._sysURL.hostname;
887
1006
  }
888
1007
  set hostname(h) {
889
1008
  if (!this._hasHostpart) {
890
- 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()))}`);
891
1010
  }
892
1011
  this._sysURL.hostname = h;
893
1012
  }
@@ -959,6 +1078,10 @@ var BuildURI = class _BuildURI {
959
1078
  static from(strURLUri, defaultProtocol = "file:") {
960
1079
  return from((url) => new _BuildURI(url), strURLUri, defaultProtocol);
961
1080
  }
1081
+ port(p) {
1082
+ this._url.port = p;
1083
+ return this;
1084
+ }
962
1085
  hostname(h) {
963
1086
  this._url.hostname = h;
964
1087
  return this;
@@ -1020,7 +1143,20 @@ var BuildURI = class _BuildURI {
1020
1143
  return URI.from(this._url);
1021
1144
  }
1022
1145
  };
1146
+ var protocols = /* @__PURE__ */ new Map([
1147
+ ["http", true],
1148
+ ["https", true],
1149
+ ["ws", true],
1150
+ ["wss", true]
1151
+ ]);
1023
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
+ }
1024
1160
  // if no protocol is provided, default to file:
1025
1161
  static merge(into, from2, defaultProtocol = "file:") {
1026
1162
  const intoUrl = BuildURI.from(into, defaultProtocol);
@@ -1042,6 +1178,9 @@ var URI = class _URI {
1042
1178
  static from(strURLUri, defaultProtocol = "file:") {
1043
1179
  return from((url) => new _URI(url), strURLUri, defaultProtocol);
1044
1180
  }
1181
+ static fromResult(strURLUri, defaultProtocol = "file:") {
1182
+ return exception2Result(() => from((url) => new _URI(url), strURLUri, defaultProtocol));
1183
+ }
1045
1184
  constructor(url) {
1046
1185
  this._url = url.clone();
1047
1186
  }
@@ -1054,9 +1193,12 @@ var URI = class _URI {
1054
1193
  // get password(): string {
1055
1194
  // return this._url.password;
1056
1195
  // }
1057
- // get port(): string {
1058
- // return this._url.port;
1059
- // }
1196
+ get port() {
1197
+ return this._url.port;
1198
+ }
1199
+ get host() {
1200
+ return this._url.host;
1201
+ }
1060
1202
  // get username(): string {
1061
1203
  // return this._url.username;
1062
1204
  // }
@@ -1067,7 +1209,7 @@ var URI = class _URI {
1067
1209
  return this._url.protocol;
1068
1210
  }
1069
1211
  get pathname() {
1070
- return this._url.toString().replace(/^.*:\/\//, "").replace(/\?.*$/, "");
1212
+ return this._url.pathname;
1071
1213
  }
1072
1214
  // get hash(): string {
1073
1215
  // return this._url.hash;
@@ -1431,7 +1573,13 @@ var LoggerImpl = class _LoggerImpl {
1431
1573
  }
1432
1574
  Err(err) {
1433
1575
  var _a;
1434
- 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) {
1435
1583
  this._attributes["error"] = logValue(err.message);
1436
1584
  if (this._levelHandler.isStackExposed) {
1437
1585
  this._attributes["stack"] = logValue((_a = err.stack) == null ? void 0 : _a.split("\n").map((s) => s.trim()));
@@ -1785,80 +1933,6 @@ function MockLogger(params) {
1785
1933
  };
1786
1934
  }
1787
1935
 
1788
- // src/result.ts
1789
- var Result = class _Result {
1790
- static Ok(t) {
1791
- return new ResultOK(t);
1792
- }
1793
- static Err(t) {
1794
- if (typeof t === "string") {
1795
- return new ResultError(new Error(t));
1796
- }
1797
- return new ResultError(t);
1798
- }
1799
- static Is(t) {
1800
- if (!t) {
1801
- return false;
1802
- }
1803
- if (t instanceof _Result) {
1804
- return true;
1805
- }
1806
- const rt = t;
1807
- if ([typeof rt.is_ok, typeof rt.is_err, typeof rt.unwrap, typeof rt.unwrap_err].every((x) => x === "function")) {
1808
- return true;
1809
- }
1810
- return false;
1811
- }
1812
- isOk() {
1813
- return this.is_ok();
1814
- }
1815
- isErr() {
1816
- return this.is_err();
1817
- }
1818
- Ok() {
1819
- return this.unwrap();
1820
- }
1821
- Err() {
1822
- return this.unwrap_err();
1823
- }
1824
- };
1825
- var ResultOK = class extends Result {
1826
- constructor(t) {
1827
- super();
1828
- this._t = t;
1829
- }
1830
- is_ok() {
1831
- return true;
1832
- }
1833
- is_err() {
1834
- return false;
1835
- }
1836
- unwrap_err() {
1837
- throw new Error("Result is Ok");
1838
- }
1839
- unwrap() {
1840
- return this._t;
1841
- }
1842
- };
1843
- var ResultError = class extends Result {
1844
- constructor(t) {
1845
- super();
1846
- this._error = t;
1847
- }
1848
- is_ok() {
1849
- return false;
1850
- }
1851
- is_err() {
1852
- return true;
1853
- }
1854
- unwrap() {
1855
- throw new Error(`Result is Err: ${this._error}`);
1856
- }
1857
- unwrap_err() {
1858
- return this._error;
1859
- }
1860
- };
1861
-
1862
1936
  // src/option.ts
1863
1937
  var Option = class _Option {
1864
1938
  static Some(t) {
@@ -2212,8 +2286,10 @@ function uint8array2stream(str) {
2212
2286
  WrapperSysAbstraction,
2213
2287
  asyncLogValue,
2214
2288
  envFactory,
2289
+ exception2Result,
2215
2290
  isURL,
2216
2291
  logValue,
2292
+ protocols,
2217
2293
  removeSelfRef,
2218
2294
  runtimeFn,
2219
2295
  toCryptoRuntime,