@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.d.cts CHANGED
@@ -4,8 +4,8 @@ export { i as utils } from './index-_GuS2IT4.cjs';
4
4
  import 'vitest';
5
5
 
6
6
  declare abstract class Result<T, E = Error> {
7
- static Ok<T>(t: T): Result<T, Error>;
8
- static Err<T extends Error = Error>(t: T | string): Result<never, T>;
7
+ static Ok<T = void>(t: T): Result<T, Error>;
8
+ static Err<T extends Error = Error>(t: T | string): Result<void, T>;
9
9
  static Is<T>(t: unknown): t is Result<T>;
10
10
  isOk(): boolean;
11
11
  isErr(): boolean;
@@ -33,6 +33,8 @@ declare class ResultError<T extends Error> extends Result<never, T> {
33
33
  unwrap_err(): T;
34
34
  }
35
35
  type WithoutResult<T> = T extends Result<infer U> ? U : T;
36
+ type WithResult<T> = T extends Promise<infer U> ? Promise<Result<U>> : Result<T>;
37
+ declare function exception2Result<FN extends () => Promise<T> | T, T>(fn: FN): WithResult<ReturnType<FN>>;
36
38
 
37
39
  type NullOrUndef = null | undefined;
38
40
  declare function isURL(value: unknown): value is URL;
@@ -44,6 +46,9 @@ declare class MutableURL extends URL {
44
46
  readonly hash: string;
45
47
  constructor(urlStr: string);
46
48
  clone(): MutableURL;
49
+ get host(): string;
50
+ get port(): string;
51
+ set port(p: string);
47
52
  get hostname(): string;
48
53
  set hostname(h: string);
49
54
  set pathname(p: string);
@@ -58,6 +63,7 @@ declare class BuildURI {
58
63
  private constructor();
59
64
  static is(value: unknown): value is BuildURI;
60
65
  static from(strURLUri?: CoerceURI, defaultProtocol?: string): BuildURI;
66
+ port(p: string): this;
61
67
  hostname(h: string): this;
62
68
  protocol(p: string): this;
63
69
  pathname(p: string): this;
@@ -69,14 +75,19 @@ declare class BuildURI {
69
75
  URI(): URI;
70
76
  }
71
77
  type CoerceURI = string | URI | MutableURL | URL | BuildURI | NullOrUndef;
78
+ declare const protocols: Map<string, boolean>;
72
79
  declare class URI {
80
+ static protocolHasHostpart(protocol: string): () => void;
73
81
  static merge(into: CoerceURI, from: CoerceURI, defaultProtocol?: string): URI;
74
82
  static is(value: unknown): value is URI;
75
83
  static from(strURLUri?: CoerceURI, defaultProtocol?: string): URI;
84
+ static fromResult(strURLUri?: CoerceURI, defaultProtocol?: string): Result<URI>;
76
85
  readonly _url: MutableURL;
77
86
  private constructor();
78
87
  build(): BuildURI;
79
88
  get hostname(): string;
89
+ get port(): string;
90
+ get host(): string;
80
91
  get protocol(): string;
81
92
  get pathname(): string;
82
93
  get getParams(): Iterable<[string, string]>;
@@ -133,7 +144,7 @@ interface LoggerInterface<R> {
133
144
  Debug(): R;
134
145
  Log(): R;
135
146
  WithLevel(level: Level): R;
136
- Err(err: unknown): R;
147
+ Err(err: unknown | Result<unknown> | Error): R;
137
148
  Info(): R;
138
149
  Timestamp(): R;
139
150
  Any(key: string, value: unknown): R;
@@ -209,7 +220,7 @@ declare class LoggerImpl implements Logger {
209
220
  Debug(): Logger;
210
221
  Error(): Logger;
211
222
  Info(): Logger;
212
- Err(err: unknown): Logger;
223
+ Err(err: unknown | Result<unknown> | Error): Logger;
213
224
  WithLevel(l: Level): Logger;
214
225
  Ref(key: string, action: {
215
226
  toString: () => string;
@@ -424,4 +435,4 @@ interface CryptoRuntime {
424
435
  }
425
436
  declare function toCryptoRuntime(cryptoOpts?: Partial<CryptoRuntime>): CryptoRuntime;
426
437
 
427
- export { type AsError, BuildURI, type CTAesKeyAlgorithm, type CTAlgorithm, type CTAlgorithmIdentifier, type CTArrayBufferView, type CTBufferSource, type CTCryptoKey, type CTEcKeyImportParams, type CTHmacImportParams, type CTJsonWebKey, type CTKeyFormat, type CTKeyType, type CTKeyUsage, type CTNamedCurve, type CTRsaHashedImportParams, type CoerceURI, type CryptoRuntime, type FnSerialized, Future, IsLogger, Keyed, KeyedResolvOnce, KeyedResolvSeq, type Lengthed, Level, type LevelHandler, LevelHandlerImpl, LogCollector, type LogSerializable, LogValue, LogWriteStream, LogWriterStream, type Logger, LoggerImpl, type LoggerImplParams, type LoggerInterface, MockLogger, type MockLoggerReturn, MutableURL, None, Option, ResolveOnce, ResolveSeq, Result, ResultError, ResultOK, type Runtime, type Serialized, type SizeOrLength, type Sized, Some, SysAbstraction, URI, type WithLogger, type WithoutOption, type WithoutResult, asyncLogValue, isURL, logValue, removeSelfRef, runtimeFn, toCryptoRuntime };
438
+ export { type AsError, BuildURI, type CTAesKeyAlgorithm, type CTAlgorithm, type CTAlgorithmIdentifier, type CTArrayBufferView, type CTBufferSource, type CTCryptoKey, type CTEcKeyImportParams, type CTHmacImportParams, type CTJsonWebKey, type CTKeyFormat, type CTKeyType, type CTKeyUsage, type CTNamedCurve, type CTRsaHashedImportParams, type CoerceURI, type CryptoRuntime, type FnSerialized, Future, IsLogger, Keyed, KeyedResolvOnce, KeyedResolvSeq, type Lengthed, Level, type LevelHandler, LevelHandlerImpl, LogCollector, type LogSerializable, LogValue, LogWriteStream, LogWriterStream, type Logger, LoggerImpl, type LoggerImplParams, type LoggerInterface, MockLogger, type MockLoggerReturn, MutableURL, None, Option, ResolveOnce, ResolveSeq, Result, ResultError, ResultOK, type Runtime, type Serialized, type SizeOrLength, type Sized, Some, SysAbstraction, URI, type WithLogger, type WithoutOption, type WithoutResult, asyncLogValue, exception2Result, isURL, logValue, protocols, removeSelfRef, runtimeFn, toCryptoRuntime };
package/index.d.ts CHANGED
@@ -4,8 +4,8 @@ export { i as utils } from './index-_GuS2IT4.js';
4
4
  import 'vitest';
5
5
 
6
6
  declare abstract class Result<T, E = Error> {
7
- static Ok<T>(t: T): Result<T, Error>;
8
- static Err<T extends Error = Error>(t: T | string): Result<never, T>;
7
+ static Ok<T = void>(t: T): Result<T, Error>;
8
+ static Err<T extends Error = Error>(t: T | string): Result<void, T>;
9
9
  static Is<T>(t: unknown): t is Result<T>;
10
10
  isOk(): boolean;
11
11
  isErr(): boolean;
@@ -33,6 +33,8 @@ declare class ResultError<T extends Error> extends Result<never, T> {
33
33
  unwrap_err(): T;
34
34
  }
35
35
  type WithoutResult<T> = T extends Result<infer U> ? U : T;
36
+ type WithResult<T> = T extends Promise<infer U> ? Promise<Result<U>> : Result<T>;
37
+ declare function exception2Result<FN extends () => Promise<T> | T, T>(fn: FN): WithResult<ReturnType<FN>>;
36
38
 
37
39
  type NullOrUndef = null | undefined;
38
40
  declare function isURL(value: unknown): value is URL;
@@ -44,6 +46,9 @@ declare class MutableURL extends URL {
44
46
  readonly hash: string;
45
47
  constructor(urlStr: string);
46
48
  clone(): MutableURL;
49
+ get host(): string;
50
+ get port(): string;
51
+ set port(p: string);
47
52
  get hostname(): string;
48
53
  set hostname(h: string);
49
54
  set pathname(p: string);
@@ -58,6 +63,7 @@ declare class BuildURI {
58
63
  private constructor();
59
64
  static is(value: unknown): value is BuildURI;
60
65
  static from(strURLUri?: CoerceURI, defaultProtocol?: string): BuildURI;
66
+ port(p: string): this;
61
67
  hostname(h: string): this;
62
68
  protocol(p: string): this;
63
69
  pathname(p: string): this;
@@ -69,14 +75,19 @@ declare class BuildURI {
69
75
  URI(): URI;
70
76
  }
71
77
  type CoerceURI = string | URI | MutableURL | URL | BuildURI | NullOrUndef;
78
+ declare const protocols: Map<string, boolean>;
72
79
  declare class URI {
80
+ static protocolHasHostpart(protocol: string): () => void;
73
81
  static merge(into: CoerceURI, from: CoerceURI, defaultProtocol?: string): URI;
74
82
  static is(value: unknown): value is URI;
75
83
  static from(strURLUri?: CoerceURI, defaultProtocol?: string): URI;
84
+ static fromResult(strURLUri?: CoerceURI, defaultProtocol?: string): Result<URI>;
76
85
  readonly _url: MutableURL;
77
86
  private constructor();
78
87
  build(): BuildURI;
79
88
  get hostname(): string;
89
+ get port(): string;
90
+ get host(): string;
80
91
  get protocol(): string;
81
92
  get pathname(): string;
82
93
  get getParams(): Iterable<[string, string]>;
@@ -133,7 +144,7 @@ interface LoggerInterface<R> {
133
144
  Debug(): R;
134
145
  Log(): R;
135
146
  WithLevel(level: Level): R;
136
- Err(err: unknown): R;
147
+ Err(err: unknown | Result<unknown> | Error): R;
137
148
  Info(): R;
138
149
  Timestamp(): R;
139
150
  Any(key: string, value: unknown): R;
@@ -209,7 +220,7 @@ declare class LoggerImpl implements Logger {
209
220
  Debug(): Logger;
210
221
  Error(): Logger;
211
222
  Info(): Logger;
212
- Err(err: unknown): Logger;
223
+ Err(err: unknown | Result<unknown> | Error): Logger;
213
224
  WithLevel(l: Level): Logger;
214
225
  Ref(key: string, action: {
215
226
  toString: () => string;
@@ -424,4 +435,4 @@ interface CryptoRuntime {
424
435
  }
425
436
  declare function toCryptoRuntime(cryptoOpts?: Partial<CryptoRuntime>): CryptoRuntime;
426
437
 
427
- export { type AsError, BuildURI, type CTAesKeyAlgorithm, type CTAlgorithm, type CTAlgorithmIdentifier, type CTArrayBufferView, type CTBufferSource, type CTCryptoKey, type CTEcKeyImportParams, type CTHmacImportParams, type CTJsonWebKey, type CTKeyFormat, type CTKeyType, type CTKeyUsage, type CTNamedCurve, type CTRsaHashedImportParams, type CoerceURI, type CryptoRuntime, type FnSerialized, Future, IsLogger, Keyed, KeyedResolvOnce, KeyedResolvSeq, type Lengthed, Level, type LevelHandler, LevelHandlerImpl, LogCollector, type LogSerializable, LogValue, LogWriteStream, LogWriterStream, type Logger, LoggerImpl, type LoggerImplParams, type LoggerInterface, MockLogger, type MockLoggerReturn, MutableURL, None, Option, ResolveOnce, ResolveSeq, Result, ResultError, ResultOK, type Runtime, type Serialized, type SizeOrLength, type Sized, Some, SysAbstraction, URI, type WithLogger, type WithoutOption, type WithoutResult, asyncLogValue, isURL, logValue, removeSelfRef, runtimeFn, toCryptoRuntime };
438
+ export { type AsError, BuildURI, type CTAesKeyAlgorithm, type CTAlgorithm, type CTAlgorithmIdentifier, type CTArrayBufferView, type CTBufferSource, type CTCryptoKey, type CTEcKeyImportParams, type CTHmacImportParams, type CTJsonWebKey, type CTKeyFormat, type CTKeyType, type CTKeyUsage, type CTNamedCurve, type CTRsaHashedImportParams, type CoerceURI, type CryptoRuntime, type FnSerialized, Future, IsLogger, Keyed, KeyedResolvOnce, KeyedResolvSeq, type Lengthed, Level, type LevelHandler, LevelHandlerImpl, LogCollector, type LogSerializable, LogValue, LogWriteStream, LogWriterStream, type Logger, LoggerImpl, type LoggerImplParams, type LoggerInterface, MockLogger, type MockLoggerReturn, MutableURL, None, Option, ResolveOnce, ResolveSeq, Result, ResultError, ResultOK, type Runtime, type Serialized, type SizeOrLength, type Sized, Some, SysAbstraction, URI, type WithLogger, type WithoutOption, type WithoutResult, asyncLogValue, exception2Result, isURL, logValue, protocols, removeSelfRef, runtimeFn, toCryptoRuntime };
package/index.js CHANGED
@@ -117,6 +117,91 @@ function IsLogger(obj) {
117
117
  ].map((fn) => typeof obj[fn] === "function").reduce((a, b) => a && b, true);
118
118
  }
119
119
 
120
+ // src/result.ts
121
+ var Result = class _Result {
122
+ static Ok(t) {
123
+ return new ResultOK(t);
124
+ }
125
+ static Err(t) {
126
+ if (typeof t === "string") {
127
+ return new ResultError(new Error(t));
128
+ }
129
+ return new ResultError(t);
130
+ }
131
+ static Is(t) {
132
+ if (!t) {
133
+ return false;
134
+ }
135
+ if (t instanceof _Result) {
136
+ return true;
137
+ }
138
+ const rt = t;
139
+ if ([typeof rt.is_ok, typeof rt.is_err, typeof rt.unwrap, typeof rt.unwrap_err].every((x) => x === "function")) {
140
+ return true;
141
+ }
142
+ return false;
143
+ }
144
+ isOk() {
145
+ return this.is_ok();
146
+ }
147
+ isErr() {
148
+ return this.is_err();
149
+ }
150
+ Ok() {
151
+ return this.unwrap();
152
+ }
153
+ Err() {
154
+ return this.unwrap_err();
155
+ }
156
+ };
157
+ var ResultOK = class extends Result {
158
+ constructor(t) {
159
+ super();
160
+ this._t = t;
161
+ }
162
+ is_ok() {
163
+ return true;
164
+ }
165
+ is_err() {
166
+ return false;
167
+ }
168
+ unwrap_err() {
169
+ throw new Error("Result is Ok");
170
+ }
171
+ unwrap() {
172
+ return this._t;
173
+ }
174
+ };
175
+ var ResultError = class extends Result {
176
+ constructor(t) {
177
+ super();
178
+ this._error = t;
179
+ }
180
+ is_ok() {
181
+ return false;
182
+ }
183
+ is_err() {
184
+ return true;
185
+ }
186
+ unwrap() {
187
+ throw new Error(`Result is Err: ${this._error}`);
188
+ }
189
+ unwrap_err() {
190
+ return this._error;
191
+ }
192
+ };
193
+ function exception2Result(fn) {
194
+ try {
195
+ const res = fn();
196
+ if (res instanceof Promise) {
197
+ return res.then((value) => Result.Ok(value)).catch((e) => Result.Err(e));
198
+ }
199
+ return Result.Ok(res);
200
+ } catch (e) {
201
+ return Result.Err(e);
202
+ }
203
+ }
204
+
120
205
  // src/uri.ts
121
206
  function falsy2undef(value) {
122
207
  return value === void 0 || value === null ? void 0 : value;
@@ -141,9 +226,21 @@ function isURL(value) {
141
226
  var MutableURL = class _MutableURL extends URL {
142
227
  constructor(urlStr) {
143
228
  super("defect://does.not.exist");
144
- this._sysURL = new URL(urlStr);
145
- this._protocol = this._sysURL.protocol;
146
- this._hasHostpart = ["http:", "https:"].includes(this._protocol);
229
+ const partedURL = urlStr.split(":");
230
+ this._hasHostpart = protocols.has(partedURL[0]);
231
+ let hostPartUrl = ["http", ...partedURL.slice(1)].join(":");
232
+ if (!this._hasHostpart) {
233
+ const pathname = hostPartUrl.replace(/http:\/\/[/]*/, "").replace(/[#?].*$/, "");
234
+ hostPartUrl = hostPartUrl.replace(/http:\/\//, `http://localhost/${pathname}`);
235
+ }
236
+ try {
237
+ this._sysURL = new URL(hostPartUrl);
238
+ } catch (ie) {
239
+ const e = ie;
240
+ e.message = `${e.message} for URL: ${urlStr}`;
241
+ throw e;
242
+ }
243
+ this._protocol = `${partedURL[0]}:`;
147
244
  if (this._hasHostpart) {
148
245
  this._pathname = this._sysURL.pathname;
149
246
  } else {
@@ -154,15 +251,35 @@ var MutableURL = class _MutableURL extends URL {
154
251
  clone() {
155
252
  return new _MutableURL(this.toString());
156
253
  }
254
+ get host() {
255
+ if (!this._hasHostpart) {
256
+ throw new Error(
257
+ `you can use hostname only if protocol is ${this.toString()} ${JSON.stringify(Array.from(protocols.keys()))}`
258
+ );
259
+ }
260
+ return this._sysURL.host;
261
+ }
262
+ get port() {
263
+ if (!this._hasHostpart) {
264
+ throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(protocols.keys()))}`);
265
+ }
266
+ return this._sysURL.port;
267
+ }
268
+ set port(p) {
269
+ if (!this._hasHostpart) {
270
+ throw new Error(`you can use port only if protocol is ${JSON.stringify(Array.from(protocols.keys()))}`);
271
+ }
272
+ this._sysURL.port = p;
273
+ }
157
274
  get hostname() {
158
275
  if (!this._hasHostpart) {
159
- throw new Error("you can use hostname only if protocol is http or https");
276
+ throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(protocols.keys()))}`);
160
277
  }
161
278
  return this._sysURL.hostname;
162
279
  }
163
280
  set hostname(h) {
164
281
  if (!this._hasHostpart) {
165
- throw new Error("you can use hostname only if protocol is http or https");
282
+ throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(protocols.keys()))}`);
166
283
  }
167
284
  this._sysURL.hostname = h;
168
285
  }
@@ -234,6 +351,10 @@ var BuildURI = class _BuildURI {
234
351
  static from(strURLUri, defaultProtocol = "file:") {
235
352
  return from((url) => new _BuildURI(url), strURLUri, defaultProtocol);
236
353
  }
354
+ port(p) {
355
+ this._url.port = p;
356
+ return this;
357
+ }
237
358
  hostname(h) {
238
359
  this._url.hostname = h;
239
360
  return this;
@@ -295,7 +416,20 @@ var BuildURI = class _BuildURI {
295
416
  return URI.from(this._url);
296
417
  }
297
418
  };
419
+ var protocols = /* @__PURE__ */ new Map([
420
+ ["http", true],
421
+ ["https", true],
422
+ ["ws", true],
423
+ ["wss", true]
424
+ ]);
298
425
  var URI = class _URI {
426
+ static protocolHasHostpart(protocol) {
427
+ protocol = protocol.replace(/:$/, "");
428
+ protocols.set(protocol, true);
429
+ return () => {
430
+ protocols.delete(protocol);
431
+ };
432
+ }
299
433
  // if no protocol is provided, default to file:
300
434
  static merge(into, from2, defaultProtocol = "file:") {
301
435
  const intoUrl = BuildURI.from(into, defaultProtocol);
@@ -317,6 +451,9 @@ var URI = class _URI {
317
451
  static from(strURLUri, defaultProtocol = "file:") {
318
452
  return from((url) => new _URI(url), strURLUri, defaultProtocol);
319
453
  }
454
+ static fromResult(strURLUri, defaultProtocol = "file:") {
455
+ return exception2Result(() => from((url) => new _URI(url), strURLUri, defaultProtocol));
456
+ }
320
457
  constructor(url) {
321
458
  this._url = url.clone();
322
459
  }
@@ -329,9 +466,12 @@ var URI = class _URI {
329
466
  // get password(): string {
330
467
  // return this._url.password;
331
468
  // }
332
- // get port(): string {
333
- // return this._url.port;
334
- // }
469
+ get port() {
470
+ return this._url.port;
471
+ }
472
+ get host() {
473
+ return this._url.host;
474
+ }
335
475
  // get username(): string {
336
476
  // return this._url.username;
337
477
  // }
@@ -342,7 +482,7 @@ var URI = class _URI {
342
482
  return this._url.protocol;
343
483
  }
344
484
  get pathname() {
345
- return this._url.toString().replace(/^.*:\/\//, "").replace(/\?.*$/, "");
485
+ return this._url.pathname;
346
486
  }
347
487
  // get hash(): string {
348
488
  // return this._url.hash;
@@ -642,7 +782,13 @@ var LoggerImpl = class _LoggerImpl {
642
782
  }
643
783
  Err(err) {
644
784
  var _a;
645
- if (err instanceof Error) {
785
+ if (Result.Is(err)) {
786
+ if (err.isOk()) {
787
+ this.Result("noerror", err);
788
+ } else {
789
+ this.Result("error", err);
790
+ }
791
+ } else if (err instanceof Error) {
646
792
  this._attributes["error"] = logValue(err.message);
647
793
  if (this._levelHandler.isStackExposed) {
648
794
  this._attributes["stack"] = logValue((_a = err.stack) == null ? void 0 : _a.split("\n").map((s) => s.trim()));
@@ -970,80 +1116,6 @@ function MockLogger(params) {
970
1116
  };
971
1117
  }
972
1118
 
973
- // src/result.ts
974
- var Result = class _Result {
975
- static Ok(t) {
976
- return new ResultOK(t);
977
- }
978
- static Err(t) {
979
- if (typeof t === "string") {
980
- return new ResultError(new Error(t));
981
- }
982
- return new ResultError(t);
983
- }
984
- static Is(t) {
985
- if (!t) {
986
- return false;
987
- }
988
- if (t instanceof _Result) {
989
- return true;
990
- }
991
- const rt = t;
992
- if ([typeof rt.is_ok, typeof rt.is_err, typeof rt.unwrap, typeof rt.unwrap_err].every((x) => x === "function")) {
993
- return true;
994
- }
995
- return false;
996
- }
997
- isOk() {
998
- return this.is_ok();
999
- }
1000
- isErr() {
1001
- return this.is_err();
1002
- }
1003
- Ok() {
1004
- return this.unwrap();
1005
- }
1006
- Err() {
1007
- return this.unwrap_err();
1008
- }
1009
- };
1010
- var ResultOK = class extends Result {
1011
- constructor(t) {
1012
- super();
1013
- this._t = t;
1014
- }
1015
- is_ok() {
1016
- return true;
1017
- }
1018
- is_err() {
1019
- return false;
1020
- }
1021
- unwrap_err() {
1022
- throw new Error("Result is Ok");
1023
- }
1024
- unwrap() {
1025
- return this._t;
1026
- }
1027
- };
1028
- var ResultError = class extends Result {
1029
- constructor(t) {
1030
- super();
1031
- this._error = t;
1032
- }
1033
- is_ok() {
1034
- return false;
1035
- }
1036
- is_err() {
1037
- return true;
1038
- }
1039
- unwrap() {
1040
- throw new Error(`Result is Err: ${this._error}`);
1041
- }
1042
- unwrap_err() {
1043
- return this._error;
1044
- }
1045
- };
1046
-
1047
1119
  // src/option.ts
1048
1120
  var Option = class _Option {
1049
1121
  static Some(t) {
@@ -1156,8 +1228,10 @@ export {
1156
1228
  WrapperSysAbstraction,
1157
1229
  asyncLogValue,
1158
1230
  envFactory,
1231
+ exception2Result,
1159
1232
  isURL,
1160
1233
  logValue,
1234
+ protocols,
1161
1235
  removeSelfRef,
1162
1236
  runtimeFn,
1163
1237
  toCryptoRuntime,