@trayio/commons 1.10.0 → 1.12.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.
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'stream';
3
+ import * as TE from 'fp-ts/TaskEither';
4
+ export interface BufferExtensionsInterface {
5
+ readableToArrayBuffer: (stream: Readable) => TE.TaskEither<Error, ArrayBuffer>;
6
+ arrayBufferToReadable: (arrayBuffer: ArrayBuffer) => Readable;
7
+ }
8
+ export declare const BufferExtensions: BufferExtensionsInterface;
9
+ //# sourceMappingURL=BufferExtensions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BufferExtensions.d.ts","sourceRoot":"","sources":["../../src/buffer/BufferExtensions.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEvC,MAAM,WAAW,yBAAyB;IACzC,qBAAqB,EAAE,CACtB,MAAM,EAAE,QAAQ,KACZ,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;IACvC,qBAAqB,EAAE,CAAC,WAAW,EAAE,WAAW,KAAK,QAAQ,CAAC;CAC9D;AAED,eAAO,MAAM,gBAAgB,EAAE,yBAiB9B,CAAC"}
@@ -0,0 +1,68 @@
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
+ };
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
+ };
34
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
35
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
36
+ var m = o[Symbol.asyncIterator], i;
37
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
38
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
39
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.BufferExtensions = void 0;
43
+ const stream_1 = require("stream");
44
+ const TE = __importStar(require("fp-ts/TaskEither"));
45
+ exports.BufferExtensions = {
46
+ readableToArrayBuffer: (stream) => TE.tryCatch(() => __awaiter(void 0, void 0, void 0, function* () {
47
+ var _a, e_1, _b, _c;
48
+ const chunks = [];
49
+ try {
50
+ for (var _d = true, stream_2 = __asyncValues(stream), stream_2_1; stream_2_1 = yield stream_2.next(), _a = stream_2_1.done, !_a; _d = true) {
51
+ _c = stream_2_1.value;
52
+ _d = false;
53
+ const chunk = _c;
54
+ chunks.push(chunk);
55
+ }
56
+ }
57
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
58
+ finally {
59
+ try {
60
+ if (!_d && !_a && (_b = stream_2.return)) yield _b.call(stream_2);
61
+ }
62
+ finally { if (e_1) throw e_1.error; }
63
+ }
64
+ const buffer = Buffer.concat(chunks);
65
+ return Uint8Array.from(buffer).buffer;
66
+ }), (error) => error),
67
+ arrayBufferToReadable: (arrayBuffer) => stream_1.Readable.from(Buffer.from(arrayBuffer)),
68
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=BufferExtensions.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BufferExtensions.unit.test.d.ts","sourceRoot":"","sources":["../../src/buffer/BufferExtensions.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,47 @@
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
+ };
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
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ const E = __importStar(require("fp-ts/Either"));
36
+ const util_1 = require("util");
37
+ const BufferExtensions_1 = require("./BufferExtensions");
38
+ describe('Buffer Tests', () => {
39
+ it('should convert a buffer to a stream, then stream back to a buffer', () => __awaiter(void 0, void 0, void 0, function* () {
40
+ const readableStream = BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new util_1.TextEncoder().encode('test').buffer);
41
+ const bufferResult = yield BufferExtensions_1.BufferExtensions.readableToArrayBuffer(readableStream)();
42
+ const buffer = E.getOrElse((error) => {
43
+ throw new Error(error.message);
44
+ })(bufferResult);
45
+ expect(buffer).toEqual(new util_1.TextEncoder().encode('test').buffer);
46
+ }));
47
+ });
@@ -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,7 @@
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
- (0, FileStorage_abstract_test_1.fileStorageTest)(new NodeFsFileStorage_1.NodeFsFileStorage());
5
+ describe('NodeFsFileStorage Tests', () => {
6
+ (0, FileStorage_abstract_test_1.fileStorageTest)(new NodeFsFileStorage_1.NodeFsFileStorage());
7
+ });
@@ -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,4 +1,6 @@
1
+ /// <reference types="node" />
1
2
  import * as O from 'fp-ts/lib/Option';
3
+ import { Readable } from 'stream';
2
4
  import { FileMetadata } from '../file/File';
3
5
  import { SerializationFormat } from '../serialization/Serialization';
4
6
  export type HttpHeaderValue = string | string[];
@@ -6,7 +8,7 @@ export type HttpHeaders = Record<string, HttpHeaderValue | HttpContentType>;
6
8
  export type HttpQueryStringSingleValue = number | string | boolean;
7
9
  export type HttpQueryStringValue = HttpQueryStringSingleValue | HttpQueryStringSingleValue[];
8
10
  export type HttpQueryString = Record<string, HttpQueryStringValue>;
9
- export type RegularHttpRequestBody = ArrayBuffer;
11
+ export type RegularHttpRequestBody = Readable;
10
12
  export type MultipartHttpRequestBody = {
11
13
  fields: Record<string, string>;
12
14
  files: Record<string, FileMetadata>;
@@ -1 +1 @@
1
- {"version":3,"file":"Http.d.ts","sourceRoot":"","sources":["../../src/http/Http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,kBAAkB,CAAC;AAEtC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAOrE,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;AAEhD,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CAAC,CAAC;AAE5E,MAAM,MAAM,0BAA0B,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAEnE,MAAM,MAA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AAM,CAAC,CAAC;IACnC,WAAW,EAAE,eAAe,CAAC;IAC7B,IAAI,EAAE,eAAe,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,WAAW,CAAC;IACrB,IAAI,EAAE,WAAW,CAAC;CAClB,CAAC;AAEF,oBAAY,UAAU;IACrB,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,KAAK,UAAU;IACf,MAAM,WAAW;CACjB;AAED,oBAAY,eAAe;IAC1B,IAAI,qBAAqB;IACzB,WAAW,6BAA6B;IACxC,oBAAoB,wBAAwB;CAC5C;AAED,oBAAY,UAAU;IACrB,aAAa,kBAAkB;IAC/B,WAAW,iBAAiB;IAC5B,aAAa,6BAA6B;IAC1C,SAAS,eAAe;CACxB;AAED,MAAM,WAAW,oBAAoB;IACpC,GAAG,EAAE,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;IACvE,kBAAkB,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;CAC/D;AAED,eAAO,MAAM,WAAW,EAAE,oBAYzB,CAAC;AAEF,eAAO,MAAM,oCAAoC,EAAE,MAAM,CACxD,mBAAmB,EACnB,eAAe,CAGf,CAAC;AAEF,eAAO,MAAM,eAAe,SACrB,eAAe,qCAEkC,CAAC"}
@@ -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;AAM1C,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,QAycpD"}
@@ -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");
@@ -48,6 +49,7 @@ const JsonSerialization_1 = require("../serialization/JsonSerialization");
48
49
  const TypeCodec_1 = require("../codec/TypeCodec");
49
50
  const Http_1 = require("./Http");
50
51
  const NodeFsFileStorage_1 = require("../file/NodeFsFileStorage");
52
+ const BufferExtensions_1 = require("../buffer/BufferExtensions");
51
53
  function httpClientTest(httpClient) {
52
54
  const serialization = new JsonSerialization_1.JsonSerialization();
53
55
  const fileStorage = new NodeFsFileStorage_1.NodeFsFileStorage();
@@ -62,7 +64,6 @@ function httpClientTest(httpClient) {
62
64
  fields: t.record(t.string, t.string),
63
65
  files: t.record(t.string, t.type({
64
66
  key: t.string,
65
- size: t.number,
66
67
  })),
67
68
  }));
68
69
  const testInputCodec = TypeCodec_1.TypeCodec.fromDescriptor(t.type({
@@ -137,7 +138,6 @@ function httpClientTest(httpClient) {
137
138
  var _a;
138
139
  return ({
139
140
  key: (_a = file.originalFilename) !== null && _a !== void 0 ? _a : '',
140
- size: file.size,
141
141
  });
142
142
  };
143
143
  const requestListener = (req, res) => __awaiter(this, void 0, void 0, function* () {
@@ -202,7 +202,7 @@ function httpClientTest(httpClient) {
202
202
  headers: { 'x-custom-header': 'customVal' },
203
203
  pathParams: {},
204
204
  queryString: {},
205
- body: new ArrayBuffer(0),
205
+ body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)),
206
206
  })();
207
207
  const testValue = {
208
208
  strField: 'customVal',
@@ -221,7 +221,7 @@ function httpClientTest(httpClient) {
221
221
  },
222
222
  pathParams: {},
223
223
  queryString: {},
224
- body: serializedInput,
224
+ body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(serializedInput),
225
225
  })))();
226
226
  const testValue = {
227
227
  strField: 'customVal',
@@ -236,16 +236,14 @@ function httpClientTest(httpClient) {
236
236
  const file1 = yield fileStorage.write({
237
237
  metadata: {
238
238
  key: 'file1',
239
- size: 123,
240
239
  },
241
- content: Buffer.from('file 1 content'),
240
+ content: stream_1.Readable.from('file 1 content'),
242
241
  })();
243
242
  const file2 = yield fileStorage.write({
244
243
  metadata: {
245
244
  key: 'file2',
246
- size: 456,
247
245
  },
248
- content: Buffer.from('file 2 content'),
246
+ content: stream_1.Readable.from('file 2 content'),
249
247
  })();
250
248
  if (file1._tag === 'Left') {
251
249
  throw file1.left;
@@ -261,11 +259,9 @@ function httpClientTest(httpClient) {
261
259
  files: {
262
260
  file1: {
263
261
  key: 'file1',
264
- size: 123,
265
262
  },
266
263
  file2: {
267
264
  key: 'file2',
268
- size: 456,
269
265
  },
270
266
  },
271
267
  };
@@ -284,11 +280,9 @@ function httpClientTest(httpClient) {
284
280
  files: expect.objectContaining({
285
281
  file1: expect.objectContaining({
286
282
  key: 'file1',
287
- size: expect.any(Number),
288
283
  }),
289
284
  file2: expect.objectContaining({
290
285
  key: 'file2',
291
- size: expect.any(Number),
292
286
  }),
293
287
  }),
294
288
  })));
@@ -298,7 +292,7 @@ function httpClientTest(httpClient) {
298
292
  headers: { error: '500' },
299
293
  pathParams: {},
300
294
  queryString: {},
301
- body: new ArrayBuffer(0),
295
+ body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)),
302
296
  })();
303
297
  const testErrorResponse = {
304
298
  message: 'some error message 500',
@@ -315,7 +309,7 @@ function httpClientTest(httpClient) {
315
309
  headers: { error: '404' },
316
310
  pathParams: {},
317
311
  queryString: {},
318
- body: new ArrayBuffer(0),
312
+ body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)),
319
313
  })();
320
314
  const testErrorResponse = {
321
315
  message: 'some error message 404',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "1.10.0",
3
+ "version": "1.12.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -1,5 +0,0 @@
1
- /// <reference types="node" />
2
- import { ReadableStream } from 'node:stream/web';
3
- export declare const streamToBuffer: (stream: ReadableStream) => Promise<Buffer>;
4
- export declare const bufferToStream: (buffer: Buffer) => ReadableStream;
5
- //# sourceMappingURL=Buffer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Buffer.d.ts","sourceRoot":"","sources":["../../src/buffer/Buffer.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,eAAO,MAAM,cAAc,WAClB,cAAc,KACpB,QAAQ,MAAM,CAOhB,CAAC;AAEF,eAAO,MAAM,cAAc,WAAY,MAAM,KAAG,cAQ/C,CAAC"}
@@ -1,52 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __asyncValues = (this && this.__asyncValues) || function (o) {
12
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
- var m = o[Symbol.asyncIterator], i;
14
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
- };
18
- Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.bufferToStream = exports.streamToBuffer = void 0;
20
- const web_1 = require("node:stream/web");
21
- const streamToBuffer = (stream) => { var _a, stream_1, stream_1_1; return __awaiter(void 0, void 0, void 0, function* () {
22
- var _b, e_1, _c, _d;
23
- const chunks = [];
24
- try {
25
- // eslint-disable-next-line no-restricted-syntax
26
- for (_a = true, stream_1 = __asyncValues(stream); stream_1_1 = yield stream_1.next(), _b = stream_1_1.done, !_b; _a = true) {
27
- _d = stream_1_1.value;
28
- _a = false;
29
- const chunk = _d;
30
- chunks.push(chunk);
31
- }
32
- }
33
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
34
- finally {
35
- try {
36
- if (!_a && !_b && (_c = stream_1.return)) yield _c.call(stream_1);
37
- }
38
- finally { if (e_1) throw e_1.error; }
39
- }
40
- return Buffer.concat(chunks);
41
- }); };
42
- exports.streamToBuffer = streamToBuffer;
43
- const bufferToStream = (buffer) => {
44
- const readable = new web_1.ReadableStream({
45
- start(controller) {
46
- controller.enqueue(buffer);
47
- controller.close();
48
- },
49
- });
50
- return readable;
51
- };
52
- exports.bufferToStream = bufferToStream;
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=Buffer.unit.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Buffer.unit.test.d.ts","sourceRoot":"","sources":["../../src/buffer/Buffer.unit.test.ts"],"names":[],"mappings":""}
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const Buffer_1 = require("./Buffer");
13
- describe('Buffer Tests', () => {
14
- it('should convert a buffer to a stream, then stream back to a buffer', () => __awaiter(void 0, void 0, void 0, function* () {
15
- const readableStream = (0, Buffer_1.bufferToStream)(Buffer.from('test'));
16
- const buffer = yield (0, Buffer_1.streamToBuffer)(readableStream);
17
- expect(buffer).toEqual(Buffer.from('test'));
18
- }));
19
- });