@trayio/commons 1.10.0 → 1.11.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.
@@ -1 +1 @@
1
- {"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/error/Error.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,KAQvC,CAAC"}
1
+ {"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/error/Error.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,KASvC,CAAC"}
@@ -1,13 +1,14 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'stream';
1
3
  import { TaskEither } from 'fp-ts/TaskEither';
2
4
  import * as t from 'io-ts';
3
5
  export type FileMetadata = {
4
6
  key: string;
5
- size: number;
6
7
  };
7
8
  export declare const fileMetadataDescriptor: t.Type<FileMetadata, unknown>;
8
9
  export type File = {
9
10
  metadata: FileMetadata;
10
- content: ArrayBuffer;
11
+ content: Readable;
11
12
  };
12
13
  export interface FileStorage {
13
14
  write(file: File): TaskEither<Error, undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"File.d.ts","sourceRoot":"","sources":["../../src/file/File.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,MAAM,MAAM,YAAY,GAAG;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,eAAO,MAAM,sBAAsB,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAMhE,CAAC;AAEF,MAAM,MAAM,IAAI,GAAG;IAClB,QAAQ,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,WAAW,CAAC;CACrB,CAAC;AAEF,MAAM,WAAW,WAAW;IAC3B,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAChD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC3C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;CAClD"}
1
+ {"version":3,"file":"File.d.ts","sourceRoot":"","sources":["../../src/file/File.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,MAAM,MAAM,YAAY,GAAG;IAC1B,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,eAAO,MAAM,sBAAsB,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAKhE,CAAC;AAEF,MAAM,MAAM,IAAI,GAAG;IAClB,QAAQ,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,QAAQ,CAAC;CAClB,CAAC;AAEF,MAAM,WAAW,WAAW;IAC3B,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAChD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC3C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;CAClD"}
package/dist/file/File.js CHANGED
@@ -27,5 +27,4 @@ exports.fileMetadataDescriptor = void 0;
27
27
  const t = __importStar(require("io-ts"));
28
28
  exports.fileMetadataDescriptor = t.type({
29
29
  key: t.string,
30
- size: t.number,
31
30
  }, 'FileMetadata');
@@ -1 +1 @@
1
- {"version":3,"file":"FileStorage.abstract.test.d.ts","sourceRoot":"","sources":["../../src/file/FileStorage.abstract.test.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAQ,MAAM,QAAQ,CAAC;AAE3C,eAAO,MAAM,eAAe,gBAAiB,WAAW,SAiDvD,CAAC"}
1
+ {"version":3,"file":"FileStorage.abstract.test.d.ts","sourceRoot":"","sources":["../../src/file/FileStorage.abstract.test.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAQ,MAAM,QAAQ,CAAC;AAE3C,eAAO,MAAM,eAAe,gBAAiB,WAAW,SAyEvD,CAAC"}
@@ -35,6 +35,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.fileStorageTest = void 0;
36
36
  const E = __importStar(require("fp-ts/Either"));
37
37
  const uuid_1 = require("uuid");
38
+ const stream_1 = require("stream");
38
39
  const JsonSerialization_1 = require("../serialization/JsonSerialization");
39
40
  const fileStorageTest = (fileStorage) => {
40
41
  describe('FileStorage Tests', () => {
@@ -42,13 +43,18 @@ const fileStorageTest = (fileStorage) => {
42
43
  describe("when it's successful", () => {
43
44
  it('should write a file, read the file then delete the file', () => __awaiter(void 0, void 0, void 0, function* () {
44
45
  const serialization = new JsonSerialization_1.JsonSerialization();
45
- const fileContent = Buffer.from(serialization.serialize({ test: 'someValue' }));
46
+ const fileContent = { test: 'someValue' };
47
+ const readableStream = new stream_1.Readable({
48
+ read() {
49
+ this.push(Buffer.from(serialization.serialize(fileContent)));
50
+ this.push(null);
51
+ },
52
+ });
46
53
  const file = {
47
54
  metadata: {
48
55
  key: `test-${(0, uuid_1.v4)()}.json`,
49
- size: fileContent.byteLength,
50
56
  },
51
- content: fileContent,
57
+ content: readableStream,
52
58
  };
53
59
  const writtenFile = yield fileStorage.write(file)();
54
60
  expect(writtenFile).toEqual(E.right(undefined));
@@ -56,7 +62,20 @@ const fileStorageTest = (fileStorage) => {
56
62
  const readResponse = E.getOrElse(() => {
57
63
  throw new Error('Expected right');
58
64
  })(read);
59
- expect(fileContent).toEqual(readResponse.content);
65
+ let data = '';
66
+ yield new Promise((resolve) => {
67
+ readResponse.content.on('data', (chunk) => {
68
+ data += chunk;
69
+ });
70
+ readResponse.content.on('end', () => {
71
+ resolve(undefined);
72
+ });
73
+ });
74
+ const deserializedData = serialization.deserialize(Buffer.from(data));
75
+ if (E.isLeft(deserializedData)) {
76
+ throw new Error('failed to deserialise stream contents');
77
+ }
78
+ expect(deserializedData.right).toStrictEqual(fileContent);
60
79
  const deleted = yield fileStorage.delete(file.metadata.key)();
61
80
  expect(deleted).toEqual(E.right(undefined));
62
81
  }));
@@ -1 +1 @@
1
- {"version":3,"file":"NodeFsFileStorage.d.ts","sourceRoot":"","sources":["../../src/file/NodeFsFileStorage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG3C,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAG3C,qBAAa,iBAAkB,YAAW,WAAW;IACxC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,GAAE,MAAe;IAE7C,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAalD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAkB7C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;CAWpD"}
1
+ {"version":3,"file":"NodeFsFileStorage.d.ts","sourceRoot":"","sources":["../../src/file/NodeFsFileStorage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG3C,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAG3C,qBAAa,iBAAkB,YAAW,WAAW;IACxC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,GAAE,MAAe;IAE7C,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAalD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAgB7C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;CAWpD"}
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const FileStorage_abstract_test_1 = require("@trayio/commons/src/file/FileStorage.abstract.test");
3
+ const FileStorage_abstract_test_1 = require("./FileStorage.abstract.test");
4
4
  const NodeFsFileStorage_1 = require("./NodeFsFileStorage");
5
5
  (0, FileStorage_abstract_test_1.fileStorageTest)(new NodeFsFileStorage_1.NodeFsFileStorage());
@@ -22,6 +22,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
25
34
  Object.defineProperty(exports, "__esModule", { value: true });
26
35
  exports.NodeFsFileStorage = void 0;
27
36
  const TE = __importStar(require("fp-ts/lib/TaskEither"));
@@ -34,19 +43,19 @@ class NodeFsFileStorage {
34
43
  }
35
44
  write(file) {
36
45
  const path = `${this.basePath}/${file.metadata.key}`;
37
- const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(path, Buffer.from(file.content)));
46
+ const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(path, file.content));
38
47
  return (0, function_1.pipe)(result, TE.map(() => undefined));
39
48
  }
40
49
  read(key) {
41
50
  const filePath = `${this.basePath}/${key}`;
42
- const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.readFile(filePath));
43
- return (0, function_1.pipe)(result, TE.map((content) => ({
44
- metadata: {
51
+ return TE.tryCatch(() => __awaiter(this, void 0, void 0, function* () {
52
+ const fileHandle = yield fs.open(filePath, 'r');
53
+ const stream = fileHandle.createReadStream();
54
+ const metadata = {
45
55
  key,
46
- size: content.byteLength,
47
- },
48
- content,
49
- })));
56
+ };
57
+ return { metadata, content: stream };
58
+ }), (reason) => new Error(String(reason)));
50
59
  }
51
60
  delete(key) {
52
61
  const filePath = `${this.basePath}/${key}`;
@@ -1 +1 @@
1
- {"version":3,"file":"HttpClient.abstract.test.d.ts","sourceRoot":"","sources":["../../src/http/HttpClient.abstract.test.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAK1C,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,QAgdpD"}
1
+ {"version":3,"file":"HttpClient.abstract.test.d.ts","sourceRoot":"","sources":["../../src/http/HttpClient.abstract.test.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAK1C,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,QAwcpD"}
@@ -37,6 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
38
  exports.httpClientTest = void 0;
39
39
  const E = __importStar(require("fp-ts/Either"));
40
+ const stream_1 = require("stream");
40
41
  const function_1 = require("fp-ts/function");
41
42
  const t = __importStar(require("io-ts"));
42
43
  const http_1 = require("http");
@@ -62,7 +63,6 @@ function httpClientTest(httpClient) {
62
63
  fields: t.record(t.string, t.string),
63
64
  files: t.record(t.string, t.type({
64
65
  key: t.string,
65
- size: t.number,
66
66
  })),
67
67
  }));
68
68
  const testInputCodec = TypeCodec_1.TypeCodec.fromDescriptor(t.type({
@@ -137,7 +137,6 @@ function httpClientTest(httpClient) {
137
137
  var _a;
138
138
  return ({
139
139
  key: (_a = file.originalFilename) !== null && _a !== void 0 ? _a : '',
140
- size: file.size,
141
140
  });
142
141
  };
143
142
  const requestListener = (req, res) => __awaiter(this, void 0, void 0, function* () {
@@ -236,16 +235,14 @@ function httpClientTest(httpClient) {
236
235
  const file1 = yield fileStorage.write({
237
236
  metadata: {
238
237
  key: 'file1',
239
- size: 123,
240
238
  },
241
- content: Buffer.from('file 1 content'),
239
+ content: stream_1.Readable.from('file 1 content'),
242
240
  })();
243
241
  const file2 = yield fileStorage.write({
244
242
  metadata: {
245
243
  key: 'file2',
246
- size: 456,
247
244
  },
248
- content: Buffer.from('file 2 content'),
245
+ content: stream_1.Readable.from('file 2 content'),
249
246
  })();
250
247
  if (file1._tag === 'Left') {
251
248
  throw file1.left;
@@ -261,11 +258,9 @@ function httpClientTest(httpClient) {
261
258
  files: {
262
259
  file1: {
263
260
  key: 'file1',
264
- size: 123,
265
261
  },
266
262
  file2: {
267
263
  key: 'file2',
268
- size: 456,
269
264
  },
270
265
  },
271
266
  };
@@ -284,11 +279,9 @@ function httpClientTest(httpClient) {
284
279
  files: expect.objectContaining({
285
280
  file1: expect.objectContaining({
286
281
  key: 'file1',
287
- size: expect.any(Number),
288
282
  }),
289
283
  file2: expect.objectContaining({
290
284
  key: 'file2',
291
- size: expect.any(Number),
292
285
  }),
293
286
  }),
294
287
  })));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "1.10.0",
3
+ "version": "1.11.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"