@trayio/commons 4.42.0 → 4.44.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.
- package/dist/file/NodeFsFileStorage.d.ts +3 -1
- package/dist/file/NodeFsFileStorage.d.ts.map +1 -1
- package/dist/file/NodeFsFileStorage.integration.test.js +81 -1
- package/dist/file/NodeFsFileStorage.js +4 -2
- package/dist/logging/Logging.d.ts +12 -0
- package/dist/logging/Logging.d.ts.map +1 -1
- package/dist/logging/Logging.js +23 -0
- package/package.json +1 -1
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import * as TE from 'fp-ts/lib/TaskEither';
|
|
3
3
|
import { Dirent } from 'fs';
|
|
4
|
+
import * as O from 'fp-ts/Option';
|
|
4
5
|
import { File, FileStorage, SharedUrl } from './File';
|
|
6
|
+
import { DynamicObject } from '../dynamictype/DynamicType';
|
|
5
7
|
export declare class NodeFsFileStorage implements FileStorage {
|
|
6
8
|
private basePath;
|
|
7
9
|
constructor(basePath?: string);
|
|
8
|
-
write(file: File): TE.TaskEither<Error, File>;
|
|
10
|
+
write(file: File, writeOptions?: O.Option<DynamicObject>): TE.TaskEither<Error, File>;
|
|
9
11
|
read(key: string, name?: string): TE.TaskEither<Error, File>;
|
|
10
12
|
getSharedUrl(key: string): TE.TaskEither<Error, SharedUrl>;
|
|
11
13
|
delete(key: string): TE.TaskEither<Error, undefined>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeFsFileStorage.d.ts","sourceRoot":"","sources":["../../src/file/NodeFsFileStorage.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG3C,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,WAAW,EAAgB,SAAS,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"NodeFsFileStorage.d.ts","sourceRoot":"","sources":["../../src/file/NodeFsFileStorage.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG3C,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAgB,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEpE,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,qBAAa,iBAAkB,YAAW,WAAW;IACxC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,GAAE,MAAe;IAE7C,KAAK,CACJ,IAAI,EAAE,IAAI,EACV,YAAY,GAAE,CAAC,CAAC,MAAM,CAAC,aAAa,CAAU,GAC5C,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAkB7B,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,MAAY,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAkBjE,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAW1D,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAYpD,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAY7D,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;CAW1D"}
|
|
@@ -1,7 +1,87 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const stream_1 = require("stream");
|
|
27
|
+
const uuid_1 = require("uuid");
|
|
28
|
+
const O = __importStar(require("fp-ts/Option"));
|
|
29
|
+
const E = __importStar(require("fp-ts/Either"));
|
|
3
30
|
const FileStorage_abstract_test_1 = require("./FileStorage.abstract.test");
|
|
4
31
|
const NodeFsFileStorage_1 = require("./NodeFsFileStorage");
|
|
5
32
|
describe('NodeFsFileStorage Tests', () => {
|
|
6
|
-
|
|
33
|
+
const fileStorage = new NodeFsFileStorage_1.NodeFsFileStorage();
|
|
34
|
+
(0, FileStorage_abstract_test_1.fileStorageTest)(fileStorage);
|
|
35
|
+
it('should write, append, read and delete the file', async () => {
|
|
36
|
+
const fileContent = 'This is the initial text.';
|
|
37
|
+
const readableStream = new stream_1.Readable({
|
|
38
|
+
read() {
|
|
39
|
+
this.push(Buffer.from(fileContent));
|
|
40
|
+
this.push(null);
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
const fileName = `test-${(0, uuid_1.v4)()}.txt`;
|
|
44
|
+
const file = {
|
|
45
|
+
key: fileName,
|
|
46
|
+
metadata: {
|
|
47
|
+
name: fileName,
|
|
48
|
+
},
|
|
49
|
+
content: readableStream,
|
|
50
|
+
};
|
|
51
|
+
const writtenFile = await fileStorage.write(file, O.none)();
|
|
52
|
+
expect(writtenFile).toEqual(E.right(file));
|
|
53
|
+
const secondFileContent = ' And we continue to write.';
|
|
54
|
+
const appendReadableStream = new stream_1.Readable({
|
|
55
|
+
read() {
|
|
56
|
+
this.push(Buffer.from(secondFileContent));
|
|
57
|
+
this.push(null);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
const appendFile = {
|
|
61
|
+
key: fileName,
|
|
62
|
+
metadata: {
|
|
63
|
+
name: fileName,
|
|
64
|
+
},
|
|
65
|
+
content: appendReadableStream,
|
|
66
|
+
};
|
|
67
|
+
const appendedFile = await fileStorage.write(appendFile, O.some({ flag: 'a' }))();
|
|
68
|
+
expect(appendedFile).toEqual(E.right(appendFile));
|
|
69
|
+
// When reading the full contents.
|
|
70
|
+
const read = await fileStorage.read(file.key)();
|
|
71
|
+
const readResponse = E.getOrElse(() => {
|
|
72
|
+
throw new Error('Expected right');
|
|
73
|
+
})(read);
|
|
74
|
+
let data = '';
|
|
75
|
+
await new Promise((resolve) => {
|
|
76
|
+
readResponse.content.on('data', (chunk) => {
|
|
77
|
+
data += chunk;
|
|
78
|
+
});
|
|
79
|
+
readResponse.content.on('end', () => {
|
|
80
|
+
resolve(undefined);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
expect(Buffer.from(data).toString()).toStrictEqual(`${fileContent}${secondFileContent}`);
|
|
84
|
+
const deleted = await fileStorage.delete(file.key)();
|
|
85
|
+
expect(deleted).toEqual(E.right(undefined));
|
|
86
|
+
});
|
|
7
87
|
});
|
|
@@ -28,15 +28,17 @@ const fs = __importStar(require("fs/promises"));
|
|
|
28
28
|
const path = __importStar(require("path"));
|
|
29
29
|
const TE = __importStar(require("fp-ts/lib/TaskEither"));
|
|
30
30
|
const function_1 = require("fp-ts/lib/function");
|
|
31
|
+
const O = __importStar(require("fp-ts/Option"));
|
|
31
32
|
const Task_1 = require("../task/Task");
|
|
32
33
|
class NodeFsFileStorage {
|
|
33
34
|
basePath;
|
|
34
35
|
constructor(basePath = '/tmp') {
|
|
35
36
|
this.basePath = basePath;
|
|
36
37
|
}
|
|
37
|
-
write(file) {
|
|
38
|
+
write(file, writeOptions = O.none) {
|
|
38
39
|
const filePath = `${this.basePath}/${file.key}`;
|
|
39
|
-
const
|
|
40
|
+
const uploadOptions = O.toNullable(writeOptions);
|
|
41
|
+
const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(filePath, file.content, uploadOptions?.flag ? { flag: uploadOptions.flag } : {}));
|
|
40
42
|
return (0, function_1.pipe)(result, TE.map(() => file));
|
|
41
43
|
}
|
|
42
44
|
read(key, name = key) {
|
|
@@ -7,4 +7,16 @@ export interface Logging {
|
|
|
7
7
|
export interface ObservabilityContext {
|
|
8
8
|
[key: string]: string | number | Record<string, string>;
|
|
9
9
|
}
|
|
10
|
+
export declare class ConsoleLogger implements Logging {
|
|
11
|
+
info(message: string, octx: ObservabilityContext): void;
|
|
12
|
+
debug(message: string, octx: ObservabilityContext): void;
|
|
13
|
+
error(message: string, octx: ObservabilityContext): void;
|
|
14
|
+
warning(message: string, octx: ObservabilityContext): void;
|
|
15
|
+
}
|
|
16
|
+
export declare class NoOpLogger implements Logging {
|
|
17
|
+
info(message: string, octx: ObservabilityContext): void;
|
|
18
|
+
debug(message: string, octx: ObservabilityContext): void;
|
|
19
|
+
error(message: string, octx: ObservabilityContext): void;
|
|
20
|
+
warning(message: string, octx: ObservabilityContext): void;
|
|
21
|
+
}
|
|
10
22
|
//# sourceMappingURL=Logging.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Logging.d.ts","sourceRoot":"","sources":["../../src/logging/Logging.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,OAAO;IACvB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAAC;IAExD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAAC;IAEzD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAAC;IAEzD,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAAC;CAC3D;AAED,MAAM,WAAW,oBAAoB;IACpC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACxD"}
|
|
1
|
+
{"version":3,"file":"Logging.d.ts","sourceRoot":"","sources":["../../src/logging/Logging.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,OAAO;IACvB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAAC;IAExD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAAC;IAEzD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAAC;IAEzD,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAAC;CAC3D;AAED,MAAM,WAAW,oBAAoB;IACpC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACxD;AAED,qBAAa,aAAc,YAAW,OAAO;IAC5C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI;IAIvD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI;IAIxD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI;IAIxD,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI;CAG1D;AAED,qBAAa,UAAW,YAAW,OAAO;IACzC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI;IAEvD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI;IAExD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI;IAExD,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,IAAI;CAC1D"}
|
package/dist/logging/Logging.js
CHANGED
|
@@ -1,2 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NoOpLogger = exports.ConsoleLogger = void 0;
|
|
4
|
+
class ConsoleLogger {
|
|
5
|
+
info(message, octx) {
|
|
6
|
+
console.info(message, octx);
|
|
7
|
+
}
|
|
8
|
+
debug(message, octx) {
|
|
9
|
+
console.debug(message, octx);
|
|
10
|
+
}
|
|
11
|
+
error(message, octx) {
|
|
12
|
+
console.error(message, octx);
|
|
13
|
+
}
|
|
14
|
+
warning(message, octx) {
|
|
15
|
+
console.warn(message, octx);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.ConsoleLogger = ConsoleLogger;
|
|
19
|
+
class NoOpLogger {
|
|
20
|
+
info(message, octx) { }
|
|
21
|
+
debug(message, octx) { }
|
|
22
|
+
error(message, octx) { }
|
|
23
|
+
warning(message, octx) { }
|
|
24
|
+
}
|
|
25
|
+
exports.NoOpLogger = NoOpLogger;
|