@trayio/commons
Advanced tools
Comparing version 1.10.0 to 1.11.0
@@ -0,1 +1,3 @@ | ||
/// <reference types="node" /> | ||
import { Readable } from 'stream'; | ||
import { TaskEither } from 'fp-ts/TaskEither'; | ||
@@ -5,3 +7,2 @@ import * as t from 'io-ts'; | ||
key: string; | ||
size: number; | ||
}; | ||
@@ -11,3 +12,3 @@ export declare const fileMetadataDescriptor: t.Type<FileMetadata, unknown>; | ||
metadata: FileMetadata; | ||
content: ArrayBuffer; | ||
content: Readable; | ||
}; | ||
@@ -14,0 +15,0 @@ export interface FileStorage { |
@@ -30,3 +30,2 @@ "use strict"; | ||
key: t.string, | ||
size: t.number, | ||
}, 'FileMetadata'); |
@@ -38,2 +38,3 @@ "use strict"; | ||
const uuid_1 = require("uuid"); | ||
const stream_1 = require("stream"); | ||
const JsonSerialization_1 = require("../serialization/JsonSerialization"); | ||
@@ -46,9 +47,14 @@ const fileStorageTest = (fileStorage) => { | ||
const serialization = new JsonSerialization_1.JsonSerialization(); | ||
const fileContent = Buffer.from(serialization.serialize({ test: 'someValue' })); | ||
const fileContent = { test: 'someValue' }; | ||
const readableStream = new stream_1.Readable({ | ||
read() { | ||
this.push(Buffer.from(serialization.serialize(fileContent))); | ||
this.push(null); | ||
}, | ||
}); | ||
const file = { | ||
metadata: { | ||
key: `test-${(0, uuid_1.v4)()}.json`, | ||
size: fileContent.byteLength, | ||
}, | ||
content: fileContent, | ||
content: readableStream, | ||
}; | ||
@@ -61,3 +67,16 @@ const writtenFile = yield fileStorage.write(file)(); | ||
})(read); | ||
expect(fileContent).toEqual(readResponse.content); | ||
let data = ''; | ||
yield new Promise((resolve) => { | ||
readResponse.content.on('data', (chunk) => { | ||
data += chunk; | ||
}); | ||
readResponse.content.on('end', () => { | ||
resolve(undefined); | ||
}); | ||
}); | ||
const deserializedData = serialization.deserialize(Buffer.from(data)); | ||
if (E.isLeft(deserializedData)) { | ||
throw new Error('failed to deserialise stream contents'); | ||
} | ||
expect(deserializedData.right).toStrictEqual(fileContent); | ||
const deleted = yield fileStorage.delete(file.metadata.key)(); | ||
@@ -64,0 +83,0 @@ expect(deleted).toEqual(E.right(undefined)); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const FileStorage_abstract_test_1 = require("@trayio/commons/src/file/FileStorage.abstract.test"); | ||
const FileStorage_abstract_test_1 = require("./FileStorage.abstract.test"); | ||
const NodeFsFileStorage_1 = require("./NodeFsFileStorage"); | ||
(0, FileStorage_abstract_test_1.fileStorageTest)(new NodeFsFileStorage_1.NodeFsFileStorage()); |
@@ -25,2 +25,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -38,3 +47,3 @@ exports.NodeFsFileStorage = void 0; | ||
const path = `${this.basePath}/${file.metadata.key}`; | ||
const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(path, Buffer.from(file.content))); | ||
const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(path, file.content)); | ||
return (0, function_1.pipe)(result, TE.map(() => undefined)); | ||
@@ -44,10 +53,10 @@ } | ||
const filePath = `${this.basePath}/${key}`; | ||
const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.readFile(filePath)); | ||
return (0, function_1.pipe)(result, TE.map((content) => ({ | ||
metadata: { | ||
return TE.tryCatch(() => __awaiter(this, void 0, void 0, function* () { | ||
const fileHandle = yield fs.open(filePath, 'r'); | ||
const stream = fileHandle.createReadStream(); | ||
const metadata = { | ||
key, | ||
size: content.byteLength, | ||
}, | ||
content, | ||
}))); | ||
}; | ||
return { metadata, content: stream }; | ||
}), (reason) => new Error(String(reason))); | ||
} | ||
@@ -54,0 +63,0 @@ delete(key) { |
@@ -40,2 +40,3 @@ "use strict"; | ||
const E = __importStar(require("fp-ts/Either")); | ||
const stream_1 = require("stream"); | ||
const function_1 = require("fp-ts/function"); | ||
@@ -66,3 +67,2 @@ const t = __importStar(require("io-ts")); | ||
key: t.string, | ||
size: t.number, | ||
})), | ||
@@ -142,3 +142,2 @@ })); | ||
key: (_a = file.originalFilename) !== null && _a !== void 0 ? _a : '', | ||
size: file.size, | ||
}); | ||
@@ -242,5 +241,4 @@ }; | ||
key: 'file1', | ||
size: 123, | ||
}, | ||
content: Buffer.from('file 1 content'), | ||
content: stream_1.Readable.from('file 1 content'), | ||
})(); | ||
@@ -250,5 +248,4 @@ const file2 = yield fileStorage.write({ | ||
key: 'file2', | ||
size: 456, | ||
}, | ||
content: Buffer.from('file 2 content'), | ||
content: stream_1.Readable.from('file 2 content'), | ||
})(); | ||
@@ -269,7 +266,5 @@ if (file1._tag === 'Left') { | ||
key: 'file1', | ||
size: 123, | ||
}, | ||
file2: { | ||
key: 'file2', | ||
size: 456, | ||
}, | ||
@@ -293,7 +288,5 @@ }, | ||
key: 'file1', | ||
size: expect.any(Number), | ||
}), | ||
file2: expect.objectContaining({ | ||
key: 'file2', | ||
size: expect.any(Number), | ||
}), | ||
@@ -300,0 +293,0 @@ }), |
{ | ||
"name": "@trayio/commons", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"description": "Extensions to the standard/core libraries and basic features", | ||
@@ -5,0 +5,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100796
1834