From 1ce6a3474c4ebb4735114792aeee1da9df22cda1 Mon Sep 17 00:00:00 2001 From: abudaan Date: Tue, 5 Dec 2023 14:07:35 +0100 Subject: [PATCH] pass all tests --- package.json | 4 ++-- src/AdapterAmazonS3.ts | 7 +------ src/AdapterBackblazeB2.ts | 14 +++++++------- src/AdapterLocal.ts | 1 - tests/config.ts | 8 ++++---- tests/test.ts | 38 +++++++++++++++++++++++++++++++++----- 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 090ba66..ddc011b 100644 --- a/package.json +++ b/package.json @@ -37,12 +37,12 @@ }, "scripts": { "test-jasmine": "ts-node ./node_modules/.bin/jasmine ./tests/test.jasmine.ts", - "test-local": "TYPE='local' BUCKET_NAME='the-buck' LOCAL_DIRECTORY='tests/test_directory' ts-node ./tests/test.ts", + "test-local": "TYPE='local' BUCKET_NAME='the-buck' LOCAL_DIRECTORY='tests/test_directory' ts-node ./node_modules/.bin/jasmine ./tests/test.jasmine.ts", "test-gcs": "TYPE='gcs' ts-node ./node_modules/.bin/jasmine ./tests/test.jasmine.ts", "test-s3": "TYPE='s3' ts-node ./node_modules/.bin/jasmine ./tests/test.jasmine.ts", "test-b2": "TYPE='b2' ts-node ./node_modules/.bin/jasmine ./tests/test.jasmine.ts", "test-azure": "TYPE='azure' ts-node ./node_modules/.bin/jasmine ./tests/test.jasmine.ts", - "test": "ts-node ./tests_local/test.ts", + "test": "ts-node ./tests/test.ts", "test-mode": "ts-node ./tests_local/test-mode.ts", "testB2": "ts-node ./tests_local/testB2.ts", "testS3": "ts-node ./tests_local/testS3.ts", diff --git a/src/AdapterAmazonS3.ts b/src/AdapterAmazonS3.ts index 4569e06..3520eff 100644 --- a/src/AdapterAmazonS3.ts +++ b/src/AdapterAmazonS3.ts @@ -60,15 +60,10 @@ export class AdapterAmazonS3 extends AbstractAdapter { return { error: this.configError, value: null }; } - let range = ""; - - if (typeof options.end !== "undefined") { - range = `bytes=${options.start}-${options.end}`; - } const params = { Bucket: bucketName, Key: fileName, - Range: range, + Range: `bytes=${options.start}-${options.end} || ""`, }; const command = new GetObjectCommand(params); diff --git a/src/AdapterBackblazeB2.ts b/src/AdapterBackblazeB2.ts index c65f4bb..8141ed5 100644 --- a/src/AdapterBackblazeB2.ts +++ b/src/AdapterBackblazeB2.ts @@ -185,7 +185,7 @@ export class AdapterBackblazeB2 extends AbstractAdapter { data: fileData, }) .then((file: BackblazeB2File) => { - console.log(file); + // console.log(file); return { error: null, value: `${this.storage.downloadUrl}/file/${bucketName}/${targetPath}`, @@ -212,10 +212,9 @@ export class AdapterBackblazeB2 extends AbstractAdapter { } const { value: file } = data; - let range = ""; - if (typeof options.end !== "undefined") { - range = `bytes=${options.start}-${options.end}`; - } + delete options.start; + delete options.end; + return this.storage .downloadFileById({ fileId: file.id, @@ -223,8 +222,9 @@ export class AdapterBackblazeB2 extends AbstractAdapter { axios: { headers: { "Content-Type": file.contentType, - Range: range, + Range: `bytes=${options.start}-${options.end} || ""`, }, + ...options, }, }) .then((r: { data: Readable }) => { @@ -342,7 +342,7 @@ export class AdapterBackblazeB2 extends AbstractAdapter { ) ) .then((what) => { - console.log(what); + // console.log("[clearBucket]", what); return { error: null, value: "ok" }; }) .catch((r: BackblazeAxiosResponse) => { diff --git a/src/AdapterLocal.ts b/src/AdapterLocal.ts index 5bf7261..b58c58c 100644 --- a/src/AdapterLocal.ts +++ b/src/AdapterLocal.ts @@ -250,7 +250,6 @@ export class AdapterLocal extends AbstractAdapter { if (this.configError !== null) { return { value: null, error: this.configError }; } - try { const p = path.join(this._config.directory, bucketName, fileName); return fs.promises diff --git a/tests/config.ts b/tests/config.ts index 2f56275..fc49882 100644 --- a/tests/config.ts +++ b/tests/config.ts @@ -2,10 +2,10 @@ import "jasmine"; import dotenv from "dotenv"; import { AdapterConfig, StorageType } from "../src/types"; -export function getConfig(): string | AdapterConfig { +export function getConfig(st: StorageType = StorageType.LOCAL): string | AdapterConfig { dotenv.config(); - const type = process.env.TYPE || StorageType.LOCAL; + const type = process.env.TYPE || st; let config: AdapterConfig | string = ""; if (type === StorageType.LOCAL) { @@ -40,8 +40,8 @@ export function getConfig(): string | AdapterConfig { config = { type, bucketName: process.env.BUCKET_NAME, - storageAccount: process.env.AZURE_STORAGE_ACCOUNT_NAME, - accessKey: process.env.AZURE_STORAGE_ACCOUNT_KEY, + accountName: process.env.AZURE_STORAGE_ACCOUNT_NAME, + accountKey: process.env.AZURE_STORAGE_ACCOUNT_KEY, }; } else { config = process.env.CONFIG_URL || `local://${process.cwd()}/the-buck`; diff --git a/tests/test.ts b/tests/test.ts index c1ecd14..c78479e 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -6,6 +6,7 @@ import uniquid from "uniquid"; import { Storage } from "../src/Storage"; import { StorageType } from "../src/types"; import { getConfig } from "./config"; +import { copyFile } from "./util"; const newBucketName1 = `bucket-${uniquid()}`; const newBucketName2 = `bucket-${uniquid()}`; @@ -18,9 +19,10 @@ function colorLog(s: string): string { } async function init() { - const config = getConfig(); + const config = getConfig(StorageType.B2); storage = new Storage(config); bucketName = storage.config.bucketName || newBucketName1; + console.log(colorLog("init"), storage.config); await fs.promises.stat(path.join(process.cwd(), "tests", "test_directory")).catch(async (e) => { await fs.promises.mkdir(path.join(process.cwd(), "tests", "test_directory")); @@ -54,6 +56,11 @@ async function clearBucket() { console.log(colorLog("clearBucket"), r); } +async function deleteBucket() { + const r = await storage.deleteBucket(newBucketName2); + console.log(colorLog("deleteBucket"), r); +} + async function listFiles() { const r = await storage.listFiles(newBucketName2); console.log(colorLog("listFiles"), r); @@ -89,8 +96,26 @@ async function addFileFromStream() { } async function getFileAsStream() { - const r = await storage.getFileAsStream(bucketName, "image1.jpg"); - console.log(colorLog("getFileAsStream"), r); + const r = await storage.getFileAsStream(newBucketName2, "image1-path.jpg"); + console.log(colorLog("getFileAsStream"), r.error); +} + +async function getFileAsStreamPartial() { + const { value, error } = await storage.getFileAsStream(newBucketName2, "image1-path.jpg", { + start: 0, + end: 2000, + }); + console.log(colorLog("getFileAsStream"), error); + if (value !== null) { + const filePath = path.join( + process.cwd(), + "tests", + "test_directory", + `test-${storage.getType()}-partial.jpg` + ); + const writeStream = fs.createWriteStream(filePath); + await copyFile(value, writeStream); + } } async function run() { @@ -104,8 +129,11 @@ async function run() { await addFileFromStream(); await listFiles(); await getFileAsStream(); - // await clearBucket(); - // await listFiles(); + await getFileAsStreamPartial(); + await clearBucket(); + await listFiles(); + await deleteBucket(); + await listBuckets(); // await cleanup(); }