@trayio/commons
Advanced tools
Comparing version 4.43.0 to 4.44.0
/// <reference types="node" /> | ||
import * as TE from 'fp-ts/lib/TaskEither'; | ||
import { Dirent } from 'fs'; | ||
import * as O from 'fp-ts/Option'; | ||
import { File, FileStorage, SharedUrl } from './File'; | ||
import { DynamicObject } from '../dynamictype/DynamicType'; | ||
export declare class NodeFsFileStorage implements FileStorage { | ||
private basePath; | ||
constructor(basePath?: string); | ||
write(file: File): TE.TaskEither<Error, File>; | ||
write(file: File, writeOptions?: O.Option<DynamicObject>): TE.TaskEither<Error, File>; | ||
read(key: string, name?: string): TE.TaskEither<Error, File>; | ||
@@ -10,0 +12,0 @@ getSharedUrl(key: string): TE.TaskEither<Error, SharedUrl>; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const stream_1 = require("stream"); | ||
const uuid_1 = require("uuid"); | ||
const O = __importStar(require("fp-ts/Option")); | ||
const E = __importStar(require("fp-ts/Either")); | ||
const FileStorage_abstract_test_1 = require("./FileStorage.abstract.test"); | ||
const NodeFsFileStorage_1 = require("./NodeFsFileStorage"); | ||
describe('NodeFsFileStorage Tests', () => { | ||
(0, FileStorage_abstract_test_1.fileStorageTest)(new NodeFsFileStorage_1.NodeFsFileStorage()); | ||
const fileStorage = new NodeFsFileStorage_1.NodeFsFileStorage(); | ||
(0, FileStorage_abstract_test_1.fileStorageTest)(fileStorage); | ||
it('should write, append, read and delete the file', async () => { | ||
const fileContent = 'This is the initial text.'; | ||
const readableStream = new stream_1.Readable({ | ||
read() { | ||
this.push(Buffer.from(fileContent)); | ||
this.push(null); | ||
}, | ||
}); | ||
const fileName = `test-${(0, uuid_1.v4)()}.txt`; | ||
const file = { | ||
key: fileName, | ||
metadata: { | ||
name: fileName, | ||
}, | ||
content: readableStream, | ||
}; | ||
const writtenFile = await fileStorage.write(file, O.none)(); | ||
expect(writtenFile).toEqual(E.right(file)); | ||
const secondFileContent = ' And we continue to write.'; | ||
const appendReadableStream = new stream_1.Readable({ | ||
read() { | ||
this.push(Buffer.from(secondFileContent)); | ||
this.push(null); | ||
}, | ||
}); | ||
const appendFile = { | ||
key: fileName, | ||
metadata: { | ||
name: fileName, | ||
}, | ||
content: appendReadableStream, | ||
}; | ||
const appendedFile = await fileStorage.write(appendFile, O.some({ flag: 'a' }))(); | ||
expect(appendedFile).toEqual(E.right(appendFile)); | ||
// When reading the full contents. | ||
const read = await fileStorage.read(file.key)(); | ||
const readResponse = E.getOrElse(() => { | ||
throw new Error('Expected right'); | ||
})(read); | ||
let data = ''; | ||
await new Promise((resolve) => { | ||
readResponse.content.on('data', (chunk) => { | ||
data += chunk; | ||
}); | ||
readResponse.content.on('end', () => { | ||
resolve(undefined); | ||
}); | ||
}); | ||
expect(Buffer.from(data).toString()).toStrictEqual(`${fileContent}${secondFileContent}`); | ||
const deleted = await fileStorage.delete(file.key)(); | ||
expect(deleted).toEqual(E.right(undefined)); | ||
}); | ||
}); |
@@ -31,2 +31,3 @@ "use strict"; | ||
const function_1 = require("fp-ts/lib/function"); | ||
const O = __importStar(require("fp-ts/Option")); | ||
const Task_1 = require("../task/Task"); | ||
@@ -38,5 +39,6 @@ class NodeFsFileStorage { | ||
} | ||
write(file) { | ||
write(file, writeOptions = O.none) { | ||
const filePath = `${this.basePath}/${file.key}`; | ||
const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(filePath, file.content)); | ||
const uploadOptions = O.toNullable(writeOptions); | ||
const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(filePath, file.content, uploadOptions?.flag ? { flag: uploadOptions.flag } : {})); | ||
return (0, function_1.pipe)(result, TE.map(() => file)); | ||
@@ -43,0 +45,0 @@ } |
{ | ||
"name": "@trayio/commons", | ||
"version": "4.43.0", | ||
"version": "4.44.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
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
201917
4171