Skip to content

Commit

Permalink
pass all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abudaan committed Dec 5, 2023
1 parent 730e378 commit 1ce6a34
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 1 addition & 6 deletions src/AdapterAmazonS3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/AdapterBackblazeB2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand All @@ -212,19 +212,19 @@ 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,
responseType: "stream",
axios: {
headers: {
"Content-Type": file.contentType,
Range: range,
Range: `bytes=${options.start}-${options.end} || ""`,
},
...options,
},
})
.then((r: { data: Readable }) => {
Expand Down Expand Up @@ -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) => {
Expand Down
1 change: 0 additions & 1 deletion src/AdapterLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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`;
Expand Down
38 changes: 33 additions & 5 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`;
Expand All @@ -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"));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -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();
}
Expand Down

0 comments on commit 1ce6a34

Please sign in to comment.