Skip to content

Commit

Permalink
wip configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
abudaan committed Nov 26, 2023
1 parent bc1df5b commit f0fbe38
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 225 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- `addFile` is added; you can use this method whenever you use `addFileFromPath`, `addFileFromBuffer` or `addFileFromReadable`
- `getConfig()` and `getType()` are implemented as getter as well, resp.: `storage.config` and `storage.type`
- The configuration object are no longer extensible; if you want to provide extra parameters you can use the `options` object, for instance:
- removed adapter config types

```typescript
const conf: ConfigAmazonS3 = {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"test": "ts-node ./tests/test.ts",
"test-mode": "ts-node ./tests/test-mode.ts",
"testB2": "ts-node ./tests/testB2.ts",
"testS3": "ts-node ./tests/testS3.ts",
"testGCS": "ts-node ./tests/testGCS.ts",
"testLocal": "ts-node ./tests/testLocal.ts",
"ts": "ts-node",
"tsc": "node_modules/.bin/tsc",
Expand Down
134 changes: 44 additions & 90 deletions src/AdapterAmazonS3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import {
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import {
ConfigAmazonS3,
AdapterConfig,
StorageType,
S3Compatible,
ResultObjectStream,
ResultObject,
ResultObjectBuckets,
Expand All @@ -37,98 +35,54 @@ import { parseUrl } from "./util";

export class AdapterAmazonS3 extends AbstractAdapter {
protected _type = StorageType.S3;
protected _config: ConfigAmazonS3;
protected _config: AdapterConfig;
private configError: string | null = null;
private storage: S3Client;
private s3Compatible: S3Compatible = S3Compatible.Amazon;

constructor(config: string | AdapterConfig) {
constructor(config?: string | AdapterConfig) {
super();
this._config = this.parseConfig(config as ConfigAmazonS3);

// handle small differences in supported S3 compatible storages
if (typeof (this._config as ConfigAmazonS3).region === "undefined") {
if (this.s3Compatible === S3Compatible.R2) {
this._config.region = "auto";
} else if (this.s3Compatible === S3Compatible.Backblaze) {
let ep = this._config.endpoint;
ep = ep.substring(ep.indexOf("s3.") + 3);
this._config.region = ep.substring(0, ep.indexOf("."));
}
}
if (typeof this._config.endpoint === "undefined") {
this.storage = new S3Client({ region: this._config.region });
} else {
this.storage = new S3Client({
region: this._config.region,
endpoint: this._config.endpoint,
credentials: {
accessKeyId: this._config.accessKeyId,
secretAccessKey: this._config.secretAccessKey,
},
});
}
}

private parseConfig(config: string | ConfigAmazonS3): ConfigAmazonS3 | null {
let cfg: ConfigAmazonS3;
if (typeof config === "string") {
const { value, error } = parseUrl(config);
if (error) {
this.configError = error;
return null;
}
const {
type,
part1: accessKeyId,
part2: secretAccessKey,
part3: region,
bucketName,
queryString: options,
} = value;
cfg = {
type,
accessKeyId,
secretAccessKey,
region,
bucketName,
...options,
};
this._config = this.parseConfig(config);
} else {
if (typeof config.options !== "undefined") {
cfg = { ...config, ...config.options };
delete cfg.options;
} else {
cfg = { ...config };
}
this._config = config;
}

if (cfg.skipCheck === true) {
return cfg;
if (this._config === null) {
return;
}

if (!cfg.accessKeyId || !cfg.secretAccessKey) {
this.configError =
"You must specify a value for both 'applicationKeyId' and 'applicationKey' for storage type 's3'";
return null;
}
this.storage = new S3Client(this.config);
console.log(this.storage.config);
}

if (typeof cfg.endpoint !== "undefined") {
if (cfg.endpoint.indexOf("r2.cloudflarestorage.com") !== -1) {
this.s3Compatible = S3Compatible.R2;
} else if (cfg.endpoint.indexOf("backblazeb2.com") !== -1) {
this.s3Compatible = S3Compatible.Backblaze;
}
}
if (!cfg.region && this.s3Compatible === S3Compatible.Amazon) {
this.configError = "You must specify a default region for storage type 's3'";
private parseConfig(config: string): AdapterConfig | null {
const { value, error } = parseUrl(config);
if (error) {
this.configError = error;
return null;
}

return cfg;
const {
type,
part1: accessKeyId,
part2: secretAccessKey,
part3: region,
bucketName,
queryString: options,
} = value;

return {
type,
accessKeyId,
secretAccessKey,
region,
bucketName,
...options,
};
}

async getFileAsStream(
// Public API

public async getFileAsStream(
bucketName: string,
fileName: string,
options: { start?: number; end?: number } = { start: 0 }
Expand All @@ -152,7 +106,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
}
}

async removeFile(bucketName: string, fileName: string): Promise<ResultObject> {
public async removeFile(bucketName: string, fileName: string): Promise<ResultObject> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand All @@ -170,7 +124,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
}
}

async createBucket(name: string, options: object = {}): Promise<ResultObject> {
public async createBucket(name: string, options: object = {}): Promise<ResultObject> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand All @@ -195,7 +149,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
...options,
};
// see issue: https://github.com/aws/aws-sdk-js/issues/3647
if (typeof this._config.region !== "undefined" && this._config.region !== "us-east-1") {
if (typeof this._config.region === "string" && this._config.region !== "us-east-1") {
input.CreateBucketConfiguration = {
LocationConstraint: BucketLocationConstraint[this._config.region.replace("-", "_")],
};
Expand All @@ -217,7 +171,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
}
}

async clearBucket(name: string): Promise<ResultObject> {
public async clearBucket(name: string): Promise<ResultObject> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand Down Expand Up @@ -256,7 +210,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
}
}

async deleteBucket(name: string): Promise<ResultObject> {
public async deleteBucket(name: string): Promise<ResultObject> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand All @@ -277,7 +231,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
}
}

async listBuckets(): Promise<ResultObjectBuckets> {
public async listBuckets(): Promise<ResultObjectBuckets> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand Down Expand Up @@ -335,7 +289,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
}
}

async getFileAsURL(bucketName: string, fileName: string): Promise<ResultObject> {
public async getFileAsURL(bucketName: string, fileName: string): Promise<ResultObject> {
return getSignedUrl(
this.storage,
new GetObjectCommand({
Expand All @@ -352,7 +306,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
});
}

async listFiles(bucketName: string, maxFiles: number = 1000): Promise<ResultObjectFiles> {
public async listFiles(bucketName: string, maxFiles: number = 1000): Promise<ResultObjectFiles> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand All @@ -373,7 +327,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
}
}

async sizeOf(bucketName: string, fileName: string): Promise<ResultObjectNumber> {
public async sizeOf(bucketName: string, fileName: string): Promise<ResultObjectNumber> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand All @@ -391,7 +345,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
}
}

async bucketExists(bucketName: string): Promise<ResultObjectBoolean> {
public async bucketExists(bucketName: string): Promise<ResultObjectBoolean> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand All @@ -410,7 +364,7 @@ export class AdapterAmazonS3 extends AbstractAdapter {
});
}

async fileExists(bucketName: string, fileName: string): Promise<ResultObjectBoolean> {
public async fileExists(bucketName: string, fileName: string): Promise<ResultObjectBoolean> {
if (this.configError !== null) {
return { value: null, error: this.configError };
}
Expand Down
12 changes: 4 additions & 8 deletions src/AdapterAzureStorageBlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class AdapterAzureStorageBlob extends AbstractAdapter {
);
this.storage = new BlobServiceClient(
`https://${(this._config as ConfigAzureStorageBlob).storageAccount}.blob.core.windows.net`,
this.sharedKeyCredential
this.sharedKeyCredential,
this._config.options
);
}

Expand All @@ -65,15 +66,10 @@ export class AdapterAzureStorageBlob extends AbstractAdapter {
storageAccount,
accessKey,
bucketName,
...options,
options,
};
} else {
if (typeof config.options !== "undefined") {
cfg = { ...config, ...config.options };
delete cfg.options;
} else {
cfg = { ...config };
}
cfg = { ...config };
}

if (cfg.skipCheck === true) {
Expand Down
22 changes: 10 additions & 12 deletions src/AdapterBackblazeB2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Readable } from "stream";
import { AbstractAdapter } from "./AbstractAdapter";
import {
StorageType,
ConfigBackblazeB2,
BackblazeB2File,
ResultObjectBoolean,
ResultObject,
Expand All @@ -21,24 +20,28 @@ import {
ResultObjectNumber,
BackblazeAxiosResponse,
BackblazeBucketOptions,
AdapterConfig,
} from "./types";
import { parseUrl, validateName } from "./util";

require("@gideo-llc/backblaze-b2-upload-any").install(B2);

export class AdapterBackblazeB2 extends AbstractAdapter {
protected _type = StorageType.B2;
protected _config: ConfigBackblazeB2;
protected _config: AdapterConfig;
private storage: B2;
private authorized: boolean = false;
private configError: string | null = null;

constructor(config: string | ConfigBackblazeB2) {
constructor(config?: string | AdapterConfig) {
super();
this._config = this.parseConfig(config);
if (this._config !== null) {
try {
this.storage = new B2(this._config);
const c = { ...this._config, ...(this._config.options as object) };
delete c.options;
this.storage = new B2(c);
console.log(this.storage.config);
} catch (e) {
this.configError = e.message;
}
Expand All @@ -47,8 +50,8 @@ export class AdapterBackblazeB2 extends AbstractAdapter {

// util members

private parseConfig(config: string | ConfigBackblazeB2): ConfigBackblazeB2 | null {
let cfg: ConfigBackblazeB2;
private parseConfig(config: string | AdapterConfig): AdapterConfig | null {
let cfg: AdapterConfig;
if (typeof config === "string") {
const { error, value } = parseUrl(config);
if (error !== null) {
Expand All @@ -70,12 +73,7 @@ export class AdapterBackblazeB2 extends AbstractAdapter {
...options,
};
} else {
if (typeof config.options !== "undefined") {
cfg = { ...config, ...config.options };
delete cfg.options;
} else {
cfg = { ...config };
}
cfg = { ...config };
}

if (cfg.skipCheck === true) {
Expand Down
Loading

0 comments on commit f0fbe38

Please sign in to comment.