@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.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, E extends Error = Error>(t: E | string): Result<T, E>;
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, E extends Error = Error>(t: E | string): Result<T, E>;
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,16 +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._hasHostpart = ["http", "https"].includes(urlStr.split(":")[0]);
145
- const prefix = this._hasHostpart ? "" : "cement-";
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
+ }
146
236
  try {
147
- this._sysURL = new URL(prefix + urlStr);
237
+ this._sysURL = new URL(hostPartUrl);
148
238
  } catch (ie) {
149
239
  const e = ie;
150
240
  e.message = `${e.message} for URL: ${urlStr}`;
151
241
  throw e;
152
242
  }
153
- this._protocol = this._sysURL.protocol.replace(new RegExp("^cement-"), "");
243
+ this._protocol = `${partedURL[0]}:`;
154
244
  if (this._hasHostpart) {
155
245
  this._pathname = this._sysURL.pathname;
156
246
  } else {
@@ -161,15 +251,35 @@ var MutableURL = class _MutableURL extends URL {
161
251
  clone() {
162
252
  return new _MutableURL(this.toString());
163
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
+ }
164
274
  get hostname() {
165
275
  if (!this._hasHostpart) {
166
- 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()))}`);
167
277
  }
168
278
  return this._sysURL.hostname;
169
279
  }
170
280
  set hostname(h) {
171
281
  if (!this._hasHostpart) {
172
- 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()))}`);
173
283
  }
174
284
  this._sysURL.hostname = h;
175
285
  }
@@ -241,6 +351,10 @@ var BuildURI = class _BuildURI {
241
351
  static from(strURLUri, defaultProtocol = "file:") {
242
352
  return from((url) => new _BuildURI(url), strURLUri, defaultProtocol);
243
353
  }
354
+ port(p) {
355
+ this._url.port = p;
356
+ return this;
357
+ }
244
358
  hostname(h) {
245
359
  this._url.hostname = h;
246
360
  return this;
@@ -302,7 +416,20 @@ var BuildURI = class _BuildURI {
302
416
  return URI.from(this._url);
303
417
  }
304
418
  };
419
+ var protocols = /* @__PURE__ */ new Map([
420
+ ["http", true],
421
+ ["https", true],
422
+ ["ws", true],
423
+ ["wss", true]
424
+ ]);
305
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
+ }
306
433
  // if no protocol is provided, default to file:
307
434
  static merge(into, from2, defaultProtocol = "file:") {
308
435
  const intoUrl = BuildURI.from(into, defaultProtocol);
@@ -324,6 +451,9 @@ var URI = class _URI {
324
451
  static from(strURLUri, defaultProtocol = "file:") {
325
452
  return from((url) => new _URI(url), strURLUri, defaultProtocol);
326
453
  }
454
+ static fromResult(strURLUri, defaultProtocol = "file:") {
455
+ return exception2Result(() => from((url) => new _URI(url), strURLUri, defaultProtocol));
456
+ }
327
457
  constructor(url) {
328
458
  this._url = url.clone();
329
459
  }
@@ -336,9 +466,12 @@ var URI = class _URI {
336
466
  // get password(): string {
337
467
  // return this._url.password;
338
468
  // }
339
- // get port(): string {
340
- // return this._url.port;
341
- // }
469
+ get port() {
470
+ return this._url.port;
471
+ }
472
+ get host() {
473
+ return this._url.host;
474
+ }
342
475
  // get username(): string {
343
476
  // return this._url.username;
344
477
  // }
@@ -349,7 +482,7 @@ var URI = class _URI {
349
482
  return this._url.protocol;
350
483
  }
351
484
  get pathname() {
352
- return this._url.toString().replace(/^.*:\/\//, "").replace(/\?.*$/, "");
485
+ return this._url.pathname;
353
486
  }
354
487
  // get hash(): string {
355
488
  // return this._url.hash;
@@ -649,7 +782,13 @@ var LoggerImpl = class _LoggerImpl {
649
782
  }
650
783
  Err(err) {
651
784
  var _a;
652
- 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) {
653
792
  this._attributes["error"] = logValue(err.message);
654
793
  if (this._levelHandler.isStackExposed) {
655
794
  this._attributes["stack"] = logValue((_a = err.stack) == null ? void 0 : _a.split("\n").map((s) => s.trim()));
@@ -977,80 +1116,6 @@ function MockLogger(params) {
977
1116
  };
978
1117
  }
979
1118
 
980
- // src/result.ts
981
- var Result = class _Result {
982
- static Ok(t) {
983
- return new ResultOK(t);
984
- }
985
- static Err(t) {
986
- if (typeof t === "string") {
987
- return new ResultError(new Error(t));
988
- }
989
- return new ResultError(t);
990
- }
991
- static Is(t) {
992
- if (!t) {
993
- return false;
994
- }
995
- if (t instanceof _Result) {
996
- return true;
997
- }
998
- const rt = t;
999
- if ([typeof rt.is_ok, typeof rt.is_err, typeof rt.unwrap, typeof rt.unwrap_err].every((x) => x === "function")) {
1000
- return true;
1001
- }
1002
- return false;
1003
- }
1004
- isOk() {
1005
- return this.is_ok();
1006
- }
1007
- isErr() {
1008
- return this.is_err();
1009
- }
1010
- Ok() {
1011
- return this.unwrap();
1012
- }
1013
- Err() {
1014
- return this.unwrap_err();
1015
- }
1016
- };
1017
- var ResultOK = class extends Result {
1018
- constructor(t) {
1019
- super();
1020
- this._t = t;
1021
- }
1022
- is_ok() {
1023
- return true;
1024
- }
1025
- is_err() {
1026
- return false;
1027
- }
1028
- unwrap_err() {
1029
- throw new Error("Result is Ok");
1030
- }
1031
- unwrap() {
1032
- return this._t;
1033
- }
1034
- };
1035
- var ResultError = class extends Result {
1036
- constructor(t) {
1037
- super();
1038
- this._error = t;
1039
- }
1040
- is_ok() {
1041
- return false;
1042
- }
1043
- is_err() {
1044
- return true;
1045
- }
1046
- unwrap() {
1047
- throw new Error(`Result is Err: ${this._error}`);
1048
- }
1049
- unwrap_err() {
1050
- return this._error;
1051
- }
1052
- };
1053
-
1054
1119
  // src/option.ts
1055
1120
  var Option = class _Option {
1056
1121
  static Some(t) {
@@ -1163,8 +1228,10 @@ export {
1163
1228
  WrapperSysAbstraction,
1164
1229
  asyncLogValue,
1165
1230
  envFactory,
1231
+ exception2Result,
1166
1232
  isURL,
1167
1233
  logValue,
1234
+ protocols,
1168
1235
  removeSelfRef,
1169
1236
  runtimeFn,
1170
1237
  toCryptoRuntime,