fortepiano 0.1.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.
Files changed (94) hide show
  1. package/README.md +3 -0
  2. package/lib/Aggregate.d.ts +4 -0
  3. package/lib/Aggregate.js +2 -0
  4. package/lib/Binary.d.ts +5 -0
  5. package/lib/Binary.js +15 -0
  6. package/lib/Buffer.d.ts +5 -0
  7. package/lib/Buffer.js +45 -0
  8. package/lib/Cache.d.ts +18 -0
  9. package/lib/Cache.js +50 -0
  10. package/lib/Console.d.ts +20 -0
  11. package/lib/Console.js +25 -0
  12. package/lib/Crypto.d.ts +5 -0
  13. package/lib/Crypto.js +39 -0
  14. package/lib/Date.d.ts +4 -0
  15. package/lib/Date.js +30 -0
  16. package/lib/Eq.d.ts +4 -0
  17. package/lib/Eq.js +29 -0
  18. package/lib/Error.d.ts +6 -0
  19. package/lib/Error.js +56 -0
  20. package/lib/GeneratorL.d.ts +208 -0
  21. package/lib/GeneratorL.js +501 -0
  22. package/lib/Http.d.ts +60 -0
  23. package/lib/Http.js +131 -0
  24. package/lib/IOAsyncGenerator.d.ts +102 -0
  25. package/lib/IOAsyncGenerator.js +456 -0
  26. package/lib/Log.d.ts +4 -0
  27. package/lib/Log.js +5 -0
  28. package/lib/Magma.d.ts +2 -0
  29. package/lib/Magma.js +26 -0
  30. package/lib/Matrix.d.ts +9 -0
  31. package/lib/Matrix.js +36 -0
  32. package/lib/Mock.d.ts +76 -0
  33. package/lib/Mock.js +113 -0
  34. package/lib/Option.d.ts +2 -0
  35. package/lib/Option.js +26 -0
  36. package/lib/Random.d.ts +7 -0
  37. package/lib/Random.js +7 -0
  38. package/lib/Reader.d.ts +4 -0
  39. package/lib/Reader.js +31 -0
  40. package/lib/ReaderEither.d.ts +9 -0
  41. package/lib/ReaderEither.js +34 -0
  42. package/lib/ReaderTask.d.ts +8 -0
  43. package/lib/ReaderTask.js +35 -0
  44. package/lib/ReaderTaskEither.d.ts +17 -0
  45. package/lib/ReaderTaskEither.js +42 -0
  46. package/lib/ReadonlyArray.d.ts +75 -0
  47. package/lib/ReadonlyArray.js +70 -0
  48. package/lib/ReadonlyRecord.d.ts +4 -0
  49. package/lib/ReadonlyRecord.js +28 -0
  50. package/lib/ReadonlyTuple.d.ts +14 -0
  51. package/lib/ReadonlyTuple.js +34 -0
  52. package/lib/Storage.d.ts +15 -0
  53. package/lib/Storage.js +57 -0
  54. package/lib/Stream.d.ts +5 -0
  55. package/lib/Stream.js +31 -0
  56. package/lib/TaskEither.d.ts +2 -0
  57. package/lib/TaskEither.js +32 -0
  58. package/lib/Type.d.ts +11 -0
  59. package/lib/Type.js +53 -0
  60. package/lib/Validation.d.ts +4 -0
  61. package/lib/Validation.js +30 -0
  62. package/lib/cache/Memcached.d.ts +4 -0
  63. package/lib/cache/Memcached.js +50 -0
  64. package/lib/cache/Memory.d.ts +2 -0
  65. package/lib/cache/Memory.js +66 -0
  66. package/lib/cache/Redis.d.ts +4 -0
  67. package/lib/cache/Redis.js +58 -0
  68. package/lib/cache/Storage.d.ts +4 -0
  69. package/lib/cache/Storage.js +62 -0
  70. package/lib/function.d.ts +52 -0
  71. package/lib/function.js +101 -0
  72. package/lib/http/Axios.d.ts +3 -0
  73. package/lib/http/Axios.js +59 -0
  74. package/lib/http/Fetch.d.ts +2 -0
  75. package/lib/http/Fetch.js +71 -0
  76. package/lib/http/Got.d.ts +3 -0
  77. package/lib/http/Got.js +60 -0
  78. package/lib/http/Mock.d.ts +2 -0
  79. package/lib/http/Mock.js +52 -0
  80. package/lib/index.d.ts +48 -0
  81. package/lib/index.js +86 -0
  82. package/lib/log/Void.d.ts +3 -0
  83. package/lib/log/Void.js +6 -0
  84. package/lib/number.d.ts +3 -0
  85. package/lib/number.js +8 -0
  86. package/lib/storage/Flydrive.d.ts +4 -0
  87. package/lib/storage/Flydrive.js +67 -0
  88. package/lib/storage/Fs.d.ts +3 -0
  89. package/lib/storage/Fs.js +53 -0
  90. package/lib/string.d.ts +5 -0
  91. package/lib/string.js +33 -0
  92. package/lib/struct.d.ts +9 -0
  93. package/lib/struct.js +47 -0
  94. package/package.json +87 -0
package/lib/Storage.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.log = void 0;
23
+ const function_1 = require("fp-ts/function");
24
+ const R = __importStar(require("fp-ts/Random"));
25
+ const TE = __importStar(require("fp-ts/TaskEither"));
26
+ const $L = __importStar(require("./Log"));
27
+ const $R = __importStar(require("./Random"));
28
+ const $Stri = __importStar(require("./string"));
29
+ const _log = ([verb, preposition], path, { fileSystem }, log) => (ma) => $R.salt(TE.MonadIO)(R.randomInt(0, Number.MAX_SAFE_INTEGER), (salt) => {
30
+ const message = `[${salt}] \r${$Stri.capitalize(verb)} file "${path}"${undefined !== fileSystem
31
+ ? ` ${preposition} file system "${fileSystem}"`
32
+ : ''}`;
33
+ return function_1.pipe(log.start(message), TE.fromIO, TE.chain(() => ma), TE.chainFirstIOK(() => log.end(message)), TE.orElseW((error) => function_1.pipe(log.end(message), TE.fromIO, TE.chain(() => TE.left(error)))));
34
+ });
35
+ const log = (logStart, logEnd = $L.void) => (storage) => ({
36
+ getStream: (path, options = {}) => function_1.pipe(storage.getStream(path, options), _log(['getting stream for', 'from'], path, options, {
37
+ start: logStart,
38
+ end: logEnd,
39
+ })),
40
+ getUrl: (path, options = {}) => function_1.pipe(storage.getUrl(path, options), _log(['getting URL for', 'from'], path, options, {
41
+ start: logStart,
42
+ end: logEnd,
43
+ })),
44
+ read: (path, options = {}) => function_1.pipe(storage.read(path, options), _log(['reading', 'from'], path, options, {
45
+ start: logStart,
46
+ end: logEnd,
47
+ })),
48
+ write: (path, options = {}) => function_1.flow(storage.write(path, options), _log(['writing', 'to'], path, options, {
49
+ start: logStart,
50
+ end: logEnd,
51
+ })),
52
+ delete: (path, options = {}) => function_1.pipe(storage.delete(path, options), _log(['deleting', 'from'], path, options, {
53
+ start: logStart,
54
+ end: logEnd,
55
+ })),
56
+ });
57
+ exports.log = log;
@@ -0,0 +1,5 @@
1
+ /// <reference types="node" />
2
+ import * as t from 'io-ts';
3
+ export declare const ReadableStreamC: t.Type<NodeJS.ReadableStream, NodeJS.ReadableStream, unknown>;
4
+ export declare const WritableStreamC: t.Type<NodeJS.WritableStream, NodeJS.WritableStream, unknown>;
5
+ export declare const ReadWriteStreamC: t.Type<NodeJS.ReadWriteStream, NodeJS.ReadWriteStream, unknown>;
package/lib/Stream.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.ReadWriteStreamC = exports.WritableStreamC = exports.ReadableStreamC = void 0;
23
+ const function_1 = require("fp-ts/function");
24
+ const t = __importStar(require("io-ts"));
25
+ const stream_1 = require("stream");
26
+ const isReadable = (u) => u instanceof stream_1.Readable;
27
+ exports.ReadableStreamC = new t.Type('ReadableStream', isReadable, (u, c) => (isReadable(u) ? t.success(u) : t.failure(u, c)), function_1.identity);
28
+ const isWritable = (u) => u instanceof stream_1.Writable;
29
+ exports.WritableStreamC = new t.Type('WritableStream', isWritable, (u, c) => (isWritable(u) ? t.success(u) : t.failure(u, c)), function_1.identity);
30
+ const isReadWrite = (u) => u instanceof stream_1.Duplex;
31
+ exports.ReadWriteStreamC = new t.Type('ReadWriteStream', isReadWrite, (u, c) => (isReadWrite(u) ? t.success(u) : t.failure(u, c)), function_1.identity);
@@ -0,0 +1,2 @@
1
+ import * as TE from 'fp-ts/TaskEither';
2
+ export declare const tryCatch: typeof TE.tryCatch;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.tryCatch = void 0;
23
+ const TE = __importStar(require("fp-ts/TaskEither"));
24
+ const tryCatch = (f, onRejected) => TE.tryCatch(() => {
25
+ try {
26
+ return f();
27
+ }
28
+ catch (error) {
29
+ return Promise.reject(error);
30
+ }
31
+ }, onRejected);
32
+ exports.tryCatch = tryCatch;
package/lib/Type.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import * as t from 'io-ts';
2
+ export declare const numeric: t.Type<number, number, unknown>;
3
+ export declare function literal<V extends number | string>(value: V, name?: string): t.LiteralC<V>;
4
+ export declare function literal(regExp: RegExp, name?: string): t.StringC;
5
+ export declare const struct: t.Type<object, object, unknown>;
6
+ export declare const nullable: <C extends t.Mixed>(codec: C, name?: string | undefined) => t.UnionC<[C, t.UndefinedC]>;
7
+ export declare const alias: <C extends t.Mixed>(name: string, codec: C) => t.Type<t.TypeOf<C>, t.OutputOf<C>, t.InputOf<C>>;
8
+ export declare function literalUnion<A extends number | string, B extends {
9
+ readonly [K in A]: null;
10
+ }>(strings: Readonly<[A, A, ...ReadonlyArray<A>]>, name?: string): t.KeyofC<B>;
11
+ export declare const lax: <P extends t.Props>(props: P, name?: string | undefined) => t.Type<{ [K in keyof P]?: t.TypeOf<P[K]> | undefined; }, { [K_1 in keyof P]?: t.OutputOf<P[K_1]> | undefined; }, unknown>;
package/lib/Type.js ADDED
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.lax = exports.literalUnion = exports.alias = exports.nullable = exports.struct = exports.literal = exports.numeric = void 0;
23
+ const E = __importStar(require("fp-ts/Either"));
24
+ const function_1 = require("fp-ts/function");
25
+ const O = __importStar(require("fp-ts/Option"));
26
+ const RA = __importStar(require("fp-ts/ReadonlyArray"));
27
+ const RR = __importStar(require("fp-ts/ReadonlyRecord"));
28
+ const t = __importStar(require("io-ts"));
29
+ const io_ts_types_1 = require("io-ts-types");
30
+ exports.numeric = new t.Type('Numeric', t.number.is, (u, c) => function_1.pipe(t.number.validate(u, c), E.alt(() => io_ts_types_1.NumberFromString.validate(u, c))), t.number.encode);
31
+ const isLiteral = (r) => (u) => t.string.is(u) && r.test(u);
32
+ function literal(a, name) {
33
+ return a instanceof RegExp
34
+ ? new t.Type(name || 'Literal', isLiteral(a), (u, c) => (isLiteral(a)(u) ? t.success(u) : t.failure(u, c)), function_1.identity)
35
+ : t.literal(a, name);
36
+ }
37
+ exports.literal = literal;
38
+ const isStruct = (u) => 'object' === typeof u && null !== u && !Array.isArray(u);
39
+ exports.struct = new t.Type('struct', isStruct, (u, c) => (isStruct(u) ? t.success(u) : t.failure(u, c)), function_1.identity);
40
+ const nullable = (codec, name) => t.union([codec, t.undefined], name);
41
+ exports.nullable = nullable;
42
+ const alias = (name, codec) => (Object.assign(Object.assign({}, codec), { name }));
43
+ exports.alias = alias;
44
+ const isStringArray = (as) => t.string.is(as[0]);
45
+ function literalUnion(as, name) {
46
+ return isStringArray(as)
47
+ ? t.keyof(as.reduce((result, string) => (Object.assign(Object.assign({}, result), { [string]: null })), {}), name)
48
+ : t.union(as.map((number) => t.literal(number)), name);
49
+ }
50
+ exports.literalUnion = literalUnion;
51
+ const lax = (props, name) => function_1.pipe(t.partial(props, name), (partial) => new t.Type(partial.name, partial.is, function_1.flow(t.UnknownRecord.validate, E.map(RR.toReadonlyArray), E.map(RA.reduce({}, (result, [key, value]) => function_1.pipe(partial.props, RR.lookup(key), O.match(() => result, // ({ ...result, [key]: value }),
52
+ (codec) => function_1.pipe(codec.decode(value), E.match(() => result, (value) => (Object.assign(Object.assign({}, result), { [key]: value })))))))), E.chain(t.success)), partial.encode));
53
+ exports.lax = lax;
@@ -0,0 +1,4 @@
1
+ import * as E from 'fp-ts/Either';
2
+ export declare type Validation<E, A> = E.Either<ReadonlyArray<E>, A>;
3
+ export declare const fromEither: <E, A>(fa: E.Either<E, A>) => Validation<E, A>;
4
+ export declare const getMonoid: <E>() => <A>(M: import("fp-ts/lib/Monoid").Monoid<A>) => import("fp-ts/lib/Monoid").Monoid<E.Either<readonly E[], A>>;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.getMonoid = exports.fromEither = void 0;
23
+ const A = __importStar(require("fp-ts/Applicative"));
24
+ const E = __importStar(require("fp-ts/Either"));
25
+ const function_1 = require("fp-ts/function");
26
+ const RA = __importStar(require("fp-ts/ReadonlyArray"));
27
+ const fromEither = (fa) => function_1.pipe(fa, E.mapLeft(RA.of));
28
+ exports.fromEither = fromEither;
29
+ const getMonoid = () => A.getApplicativeMonoid(E.getApplicativeValidation(RA.getSemigroup()));
30
+ exports.getMonoid = getMonoid;
@@ -0,0 +1,4 @@
1
+ import { Lazy } from 'fp-ts/function';
2
+ import Memcached from 'memcached';
3
+ import * as $C from '../Cache';
4
+ export declare const $memcached: (memcached: Lazy<Memcached>, ttl?: number) => $C.Cache;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.$memcached = void 0;
23
+ const Ei = __importStar(require("fp-ts/Either"));
24
+ const function_1 = require("fp-ts/function");
25
+ const TE = __importStar(require("fp-ts/TaskEither"));
26
+ const $Er = __importStar(require("../Error"));
27
+ const function_2 = require("../function");
28
+ const $TE = __importStar(require("../TaskEither"));
29
+ const $memcached = (memcached, ttl = Infinity) => {
30
+ const _memcached = function_2.memoize(memcached);
31
+ return {
32
+ get: (key, codec) => function_1.pipe($TE.tryCatch(() => new Promise((resolve, reject) => _memcached().get(key, (error, data) => {
33
+ // eslint-disable-next-line eqeqeq
34
+ undefined != error || undefined == data
35
+ ? reject(error)
36
+ : resolve(data);
37
+ })), $Er.fromUnknown(Error(`Cannot find cache item "${key}"`))), TE.chainEitherK(function_1.flow(codec.decode, Ei.mapLeft($Er.fromUnknown(Error(`Cannot decode cache item "${key}" into "${codec.name}"`)))))),
38
+ set: (key, codec, _ttl = ttl) => (value) => function_1.pipe($TE.tryCatch(() => new Promise((resolve, reject) => _memcached().set(key, codec.encode(value), _ttl / 1000, (error) =>
39
+ // eslint-disable-next-line eqeqeq
40
+ undefined != error ? reject(error) : resolve())), $Er.fromUnknown(Error(`Cannot write cache item "${key}"`)))),
41
+ delete: (key) => function_1.pipe($TE.tryCatch(() => new Promise((resolve, reject) => _memcached().del(key, (error) => {
42
+ // eslint-disable-next-line eqeqeq
43
+ undefined != error ? reject(error) : resolve();
44
+ })), $Er.fromUnknown(Error(`Cannot delete cache item "${key}"`)))),
45
+ clear: function_1.pipe($TE.tryCatch(() => new Promise((resolve, reject) => _memcached().flush((error) =>
46
+ // eslint-disable-next-line eqeqeq
47
+ undefined != error ? reject(error) : resolve())), $Er.fromUnknown(Error('Cannot clear cache')))),
48
+ };
49
+ };
50
+ exports.$memcached = $memcached;
@@ -0,0 +1,2 @@
1
+ import * as $C from '../Cache';
2
+ export declare const memory: (ttl?: number) => $C.Cache;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.memory = void 0;
32
+ const Ei = __importStar(require("fp-ts/Either"));
33
+ const function_1 = require("fp-ts/function");
34
+ const memory = (ttl = Infinity) => {
35
+ let cache = {};
36
+ let timeouts = {};
37
+ return {
38
+ get: (key, codec) => () => __awaiter(void 0, void 0, void 0, function* () {
39
+ return function_1.pipe(key in cache
40
+ ? Ei.right(cache[key])
41
+ : Ei.left(Error(`Cannot find cache item "${key}"`)), Ei.filterOrElse(codec.is, () => Error(`Cannot decode cache item "${key}" into "${codec.name}"`)));
42
+ }),
43
+ set: (key, _, _ttl = ttl) => (value) => () => __awaiter(void 0, void 0, void 0, function* () {
44
+ clearTimeout(timeouts[key]);
45
+ cache[key] = value;
46
+ timeouts[key] = setTimeout(() => {
47
+ delete cache[key];
48
+ delete timeouts[key];
49
+ }, Math.min(Math.pow(2, 31) - 1, Math.max(0, _ttl)));
50
+ return Ei.of(undefined);
51
+ }),
52
+ delete: (key) => () => __awaiter(void 0, void 0, void 0, function* () {
53
+ clearTimeout(timeouts[key]);
54
+ delete cache[key];
55
+ delete timeouts[key];
56
+ return Ei.of(undefined);
57
+ }),
58
+ clear: () => __awaiter(void 0, void 0, void 0, function* () {
59
+ Object.values(timeouts).forEach(clearTimeout);
60
+ cache = {};
61
+ timeouts = {};
62
+ return Ei.of(undefined);
63
+ }),
64
+ };
65
+ };
66
+ exports.memory = memory;
@@ -0,0 +1,4 @@
1
+ import { Lazy } from 'fp-ts/function';
2
+ import { RedisClient } from 'redis';
3
+ import * as $C from '../Cache';
4
+ export declare const $redis: (redis: Lazy<RedisClient>, ttl?: number) => $C.Cache;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.$redis = void 0;
23
+ const Ei = __importStar(require("fp-ts/Either"));
24
+ const function_1 = require("fp-ts/function");
25
+ const TE = __importStar(require("fp-ts/TaskEither"));
26
+ const io_ts_types_1 = require("io-ts-types");
27
+ const $Er = __importStar(require("../Error"));
28
+ const function_2 = require("../function");
29
+ const $TE = __importStar(require("../TaskEither"));
30
+ const $redis = (redis, ttl = Infinity) => {
31
+ const _redis = function_2.memoize(() => new Promise((resolve, reject) => {
32
+ const client = redis();
33
+ client.on('error', reject).on('ready', () => resolve(client));
34
+ }));
35
+ return {
36
+ get: (key, codec) => function_1.pipe($TE.tryCatch(() => new Promise((resolve, reject) => _redis()
37
+ .then((client) => client.get(key, (error, result) => {
38
+ null !== error || null === result
39
+ ? reject(error)
40
+ : resolve(result);
41
+ }))
42
+ .catch(reject)), $Er.fromUnknown(Error(`Cannot find cache item "${key}"`))), TE.chainEitherK(function_1.flow(codec.decode, Ei.mapLeft($Er.fromUnknown(Error(`Cannot decode cache item "${key}" into "${codec.name}"`)))))),
43
+ set: (key, codec, _ttl = ttl) => (value) => function_1.pipe($TE.tryCatch(() => new Promise((resolve, reject) => _redis()
44
+ .then((client) => client.set(key, io_ts_types_1.JsonFromString.pipe(codec).encode(value), 'EX', _ttl / 1000, (error) => (null !== error ? reject(error) : resolve())))
45
+ .catch(reject)), $Er.fromUnknown(Error(`Cannot write cache item "${key}"`)))),
46
+ delete: (key) => function_1.pipe($TE.tryCatch(() => new Promise((resolve, reject) => _redis()
47
+ .then((client) => client.del(key, (error, result) => {
48
+ null !== error || null === result
49
+ ? reject(error)
50
+ : resolve();
51
+ }))
52
+ .catch(reject)), $Er.fromUnknown(Error(`Cannot delete cache item "${key}"`)))),
53
+ clear: function_1.pipe($TE.tryCatch(() => new Promise((resolve, reject) => _redis()
54
+ .then((client) => client.flushdb((error) => null !== error ? reject(error) : resolve()))
55
+ .catch(reject)), $Er.fromUnknown(Error('Cannot clear cache')))),
56
+ };
57
+ };
58
+ exports.$redis = $redis;
@@ -0,0 +1,4 @@
1
+ import { Lazy } from 'fp-ts/function';
2
+ import * as $C from '../Cache';
3
+ declare const _storage: (storage: Lazy<Storage>, name?: string | undefined, ttl?: number) => $C.Cache;
4
+ export { _storage as storage };
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.storage = void 0;
32
+ const Ei = __importStar(require("fp-ts/Either"));
33
+ const function_1 = require("fp-ts/function");
34
+ const J = __importStar(require("fp-ts/Json"));
35
+ const t = __importStar(require("io-ts"));
36
+ const io_ts_types_1 = require("io-ts-types");
37
+ const $Er = __importStar(require("../Error"));
38
+ const function_2 = require("../function");
39
+ const $S = __importStar(require("../struct"));
40
+ const CacheItemC = t.type({
41
+ exp: t.number,
42
+ value: io_ts_types_1.Json,
43
+ });
44
+ const _storage = (storage, name, ttl = Infinity) => {
45
+ const _storage = function_2.memoize(storage);
46
+ return {
47
+ get: (key, codec) => () => __awaiter(void 0, void 0, void 0, function* () {
48
+ return function_1.pipe(_storage().getItem(`${undefined !== name ? `${name}_` : ''}${key}`), Ei.fromNullable(Error(`Cannot find cache item "${key}"`)), Ei.chain(function_1.flow(J.parse, Ei.chainW(CacheItemC.decode), Ei.mapLeft($Er.fromUnknown(Error(`Cannot decode cache item "${key}"`))))), Ei.filterOrElse(({ exp }) => Date.now() < exp, () => Error(`Cache item "${key}" is expired`)), Ei.map($S.lookup('value')), Ei.chain(function_1.flow(codec.decode, Ei.mapLeft($Er.fromUnknown(Error(`Cannot decode cache item "${key}" into "${codec.name}"`))))));
49
+ }),
50
+ set: (key, codec, _ttl = ttl) => (value) => () => __awaiter(void 0, void 0, void 0, function* () {
51
+ return function_1.pipe({
52
+ exp: Math.min(Number.MAX_SAFE_INTEGER, Date.now() + Math.max(0, _ttl) + 1),
53
+ value: codec.encode(value),
54
+ }, J.stringify, Ei.bimap($Er.fromUnknown(Error(`Cannot encode cache item "${key}"`)), (item) => _storage().setItem(`${undefined !== name ? `${name}_` : ''}${key}`, item)));
55
+ }),
56
+ delete: (key) => () => __awaiter(void 0, void 0, void 0, function* () {
57
+ return function_1.pipe(_storage().removeItem(`${undefined !== name ? `${name}_` : ''}${key}`), Ei.of);
58
+ }),
59
+ clear: () => __awaiter(void 0, void 0, void 0, function* () { return function_1.pipe(_storage().clear(), Ei.of); }),
60
+ };
61
+ };
62
+ exports.storage = _storage;
@@ -0,0 +1,52 @@
1
+ import { Lazy } from 'fp-ts/function';
2
+ export declare const run: <A>(f: Lazy<A>) => A;
3
+ export declare function curry<A, B, C>(f: (a: A, b: B) => C): (a: A) => (b: B) => C;
4
+ export declare function curry<A, B, C, D>(f: (a: A, b: B, c: C) => D): (a: A) => (b: B) => (c: C) => D;
5
+ export declare function uncurry<A, B, C, D>(f: (a: A) => (b: B) => (c: C) => D): D extends (...args: any) => any ? never : (a: A, b: B, c: C) => D;
6
+ export declare function uncurry<A, B, C>(f: (a: A) => (b: B) => C): C extends (...args: any) => any ? never : (a: A, b: B) => C;
7
+ export declare const memoize: <A extends (...args: any) => any>(f: A) => A;
8
+ /**
9
+ * @example
10
+ * const pi = (max: number) => (): ((n?: number) => number) =>
11
+ * recurse((self, depth) => (n = 1) =>
12
+ * 4 / n + (depth < max ? self(-1 * n + (n < 0 ? 2 : -2)) : 0)
13
+ * )
14
+ * const pi100 = pi(100)()()
15
+ *
16
+ * expect(pi100).toBeGreaterThan(3.15)
17
+ * expect(pi100).toBeLessThan(3.16)
18
+ */
19
+ export declare const recurse: <A extends (...args: any) => any>(f: (self: A, depth: number) => A, cache?: boolean) => A;
20
+ /**
21
+ * @example
22
+ * type _Op<A> = { _tag: A; x: number; y: number }
23
+ * type Add = _Op<'Add'>
24
+ * type Mul = _Op<'Mul'>
25
+ * type Sub = _Op<'Sub'>
26
+ * type Div = _Op<'Div'>
27
+ * type Op = Add | Mul | Sub | Div
28
+ *
29
+ * const calc = match<Op>()({
30
+ * Add: ({ x, y }) => option.some(x + y),
31
+ * Mul: ({ x, y }) => option.some(x * y),
32
+ * Sub: ({ x, y }) => option.some(x - y),
33
+ * Div: ({ x, y }) => (0 === y ? option.none : option.some(x / y)),
34
+ * })
35
+ * const op = (_tag: Op['_tag']) => (y: number) => (x: number) => calc({ _tag, x, y })
36
+ * const add = op('Add')
37
+ * const mul = op('Mul')
38
+ * const sub = op('Sub')
39
+ * const div = op('Div')
40
+ *
41
+ * expect(
42
+ * pipe(
43
+ * option.of(42),
44
+ * option.chain(add(1138)),
45
+ * option.chain(mul(0.1)),
46
+ * option.chain(sub(1337)),
47
+ * option.chain(div(0.1)),
48
+ * option.getOrElse(() => NaN)
49
+ * )
50
+ * ).toBeCloseTo(-12190)
51
+ */
52
+ export declare const match: <A extends { readonly [k in K]: string; }, K extends string = "_tag">() => <T extends A[K], B>(onCases: { readonly [t in T]: (a: Extract<A, { readonly [k_1 in K]: t; }>) => B; }, k?: K) => (a: A) => B;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.match = exports.recurse = exports.memoize = exports.uncurry = exports.curry = exports.run = void 0;
4
+ const run = (f) => f();
5
+ exports.run = run;
6
+ function curry(f) {
7
+ return function curried(...head) {
8
+ return head.length >= f.length
9
+ ? f(...head)
10
+ : (...rest) => curried(...head.concat(rest));
11
+ };
12
+ }
13
+ exports.curry = curry;
14
+ function uncurry(f) {
15
+ return (...args) => args.reduce((_f, arg) => _f(arg), f);
16
+ }
17
+ exports.uncurry = uncurry;
18
+ const memoize = (f) => {
19
+ const cache = {};
20
+ return ((...args) => {
21
+ let key = '';
22
+ try {
23
+ key = JSON.stringify(args);
24
+ }
25
+ catch (error) {
26
+ return f(...args);
27
+ }
28
+ if (!(key in cache)) {
29
+ const result = f(...args);
30
+ cache[key] =
31
+ result instanceof Promise
32
+ ? result.catch((error) => {
33
+ delete cache[key];
34
+ throw error;
35
+ })
36
+ : result;
37
+ }
38
+ return cache[key];
39
+ });
40
+ };
41
+ exports.memoize = memoize;
42
+ /**
43
+ * @example
44
+ * const pi = (max: number) => (): ((n?: number) => number) =>
45
+ * recurse((self, depth) => (n = 1) =>
46
+ * 4 / n + (depth < max ? self(-1 * n + (n < 0 ? 2 : -2)) : 0)
47
+ * )
48
+ * const pi100 = pi(100)()()
49
+ *
50
+ * expect(pi100).toBeGreaterThan(3.15)
51
+ * expect(pi100).toBeLessThan(3.16)
52
+ */
53
+ const recurse = (f, cache = false) => {
54
+ let depth = 0;
55
+ let _cache = null;
56
+ const run = !cache
57
+ ? f
58
+ : (_self, _depth) => {
59
+ if (!_cache) {
60
+ _cache = f(_self, _depth);
61
+ }
62
+ return _cache;
63
+ };
64
+ const self = ((...args) => run(self, depth++)(...args));
65
+ return self;
66
+ };
67
+ exports.recurse = recurse;
68
+ /**
69
+ * @example
70
+ * type _Op<A> = { _tag: A; x: number; y: number }
71
+ * type Add = _Op<'Add'>
72
+ * type Mul = _Op<'Mul'>
73
+ * type Sub = _Op<'Sub'>
74
+ * type Div = _Op<'Div'>
75
+ * type Op = Add | Mul | Sub | Div
76
+ *
77
+ * const calc = match<Op>()({
78
+ * Add: ({ x, y }) => option.some(x + y),
79
+ * Mul: ({ x, y }) => option.some(x * y),
80
+ * Sub: ({ x, y }) => option.some(x - y),
81
+ * Div: ({ x, y }) => (0 === y ? option.none : option.some(x / y)),
82
+ * })
83
+ * const op = (_tag: Op['_tag']) => (y: number) => (x: number) => calc({ _tag, x, y })
84
+ * const add = op('Add')
85
+ * const mul = op('Mul')
86
+ * const sub = op('Sub')
87
+ * const div = op('Div')
88
+ *
89
+ * expect(
90
+ * pipe(
91
+ * option.of(42),
92
+ * option.chain(add(1138)),
93
+ * option.chain(mul(0.1)),
94
+ * option.chain(sub(1337)),
95
+ * option.chain(div(0.1)),
96
+ * option.getOrElse(() => NaN)
97
+ * )
98
+ * ).toBeCloseTo(-12190)
99
+ */
100
+ const match = () => (onCases, k = '_tag') => (a) => onCases[a[k]](a);
101
+ exports.match = match;