Skip to content

Commit

Permalink
feat: fs package
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Jan 25, 2021
1 parent 5255db9 commit 7d598e0
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 12 deletions.
21 changes: 21 additions & 0 deletions packages/fs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Artur Bier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions packages/fs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@bunt/fs",
"version": "0.1.0",
"keywords": [
"typescript"
],
"author": {
"name": "Artur Bier",
"email": "[email protected]"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/"
],
"description": "FileSystem library",
"repository": "[email protected]:izatop/bunt.git",
"scripts": {
"clean": "yarn build:clean",
"watch": "yarn build:watch",
"build": "yarn build:clean && tsc",
"build:clean": "rimraf dist tsconfig.tsbuildinfo",
"build:watch": "yarn build --watch"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@bunt/unit": "^0.14.8",
"@bunt/util": "^0.14.8",
"@types/minio": "^7.0.7",
"minio": "^7.0.18"
},
"license": "MIT"
}
41 changes: 41 additions & 0 deletions packages/fs/src/Storage/Bucket/FsBucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {FileStorage} from "../FileStorage";

export interface IBucketOptions {
region?: string;
}

export class FsBucket {
public readonly name: string;
public readonly region?: string;
protected ready = false;
readonly #fs: FileStorage;

constructor(fs: FileStorage, name: string, options: IBucketOptions = {}) {
this.#fs = fs;
this.name = name;
this.region = options.region;
}

public setPolicy(policy: string): Promise<void> {
return this.#fs.getDriver().setBucketPolicy(this.name, policy);
}

public getPolicy(): Promise<string> {
return this.#fs.getDriver().getBucketPolicy(this.name);
}

public async write(id: string, file: string, metadata: Record<any, any>): Promise<string> {
if (!this.ready) {
await this.save();
}

const driver = this.#fs.getDriver();
return driver.write(this.name, id, file, metadata);
}

public async save(): Promise<void> {
const driver = this.#fs.getDriver();
await driver.createBucket(this.name, this.region, true);
this.ready = true;
}
}
24 changes: 24 additions & 0 deletions packages/fs/src/Storage/Bucket/FsBucketList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {FileStorage} from "../FileStorage";
import {FsBucket, IBucketOptions} from "./FsBucket";

export class FsBucketList {
readonly #fs: FileStorage;
readonly #buckets = new Map<string, FsBucket>();

constructor(fs: FileStorage) {
this.#fs = fs;
}

public ensure(name: string, options: IBucketOptions = {}): FsBucket {
const bucket = this.#buckets.get(name) || this.factory(name, options);
if (!this.#buckets.has(name)) {
this.#buckets.set(name, bucket);
}

return bucket;
}

protected factory(name: string, options: IBucketOptions = {}): FsBucket {
return new FsBucket(this.#fs, name, options);
}
}
9 changes: 9 additions & 0 deletions packages/fs/src/Storage/Driver/FsDriverAbstract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export abstract class FsDriverAbstract {
public abstract createBucket(name: string, region?: string, checkExists?: boolean): Promise<void>;

public abstract write(bucket: string, name: string, file: string, metadata: Record<any, any>): Promise<string>;

public abstract setBucketPolicy(bucket: string, policy: string): Promise<void>;

public abstract getBucketPolicy(bucket: string): Promise<string>;
}
49 changes: 49 additions & 0 deletions packages/fs/src/Storage/Driver/MinIO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {assert} from "@bunt/util";
import {Client} from "minio";
import {parse} from "url";
import {FsDriverAbstract} from "./FsDriverAbstract";
import {MinIOBucketPolicy} from "./MinIOBucketPolicy";

const DEFAULT_REGION = "default";

export class MinIO extends FsDriverAbstract {
readonly #client: Client;
readonly #policy = new MinIOBucketPolicy();

constructor(dsn: string) {
super();
const {auth, hostname: endPoint, port, protocol} = parse(dsn);
assert(auth, "Authorization is required");
assert(endPoint, "Endpoint is required");
assert(port, "Port is required");

const [accessKey, secretKey] = auth?.split(":");
this.#client = new Client({
useSSL: protocol?.startsWith("https"),
port: parseInt(port),
endPoint,
accessKey,
secretKey,
});
}

public async setBucketPolicy(bucket: string, policy: string): Promise<void> {
await this.#client.setBucketPolicy(bucket, this.#policy.getPolicy(bucket, policy));
}

public getBucketPolicy(bucket: string): Promise<string> {
return this.#client.getBucketPolicy(bucket);
}

public async createBucket(name: string, region?: string, checkExists = true): Promise<void> {
if (checkExists && await this.#client.bucketExists(name)) {
return;
}

await this.#client.makeBucket(name, region ?? DEFAULT_REGION);
}

public write(bucket: string, name: string, file: string, metadata: Record<any, any>): Promise<string> {
return this.#client.fPutObject(bucket, name, file, metadata);
}
}
34 changes: 34 additions & 0 deletions packages/fs/src/Storage/Driver/MinIOBucketPolicy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {assert} from "@bunt/util";

export enum MinIOBucketPolicyEnum {
READONLY = "public-readonly",
}

export class MinIOBucketPolicy {
readonly #policies = new Map<MinIOBucketPolicyEnum, (bucket: string) => string>();

public constructor() {
this.#policies.set(MinIOBucketPolicyEnum.READONLY, this.publicReadOnlyPolicy);
}

public getPolicy(bucket: string, maybe: string): string {
const policy = this.#policies.get(maybe as MinIOBucketPolicyEnum);
assert(policy, `Unknown bucket policy ${maybe}`);
return policy(bucket);
}

public publicReadOnlyPolicy(bucket: string): string {
return JSON.stringify({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
"Resource": [`arn:aws:s3:::${bucket}/*`],
},
],
});
}
}
21 changes: 21 additions & 0 deletions packages/fs/src/Storage/FileStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {FsBucket, IBucketOptions} from "./Bucket/FsBucket";
import {FsBucketList} from "./Bucket/FsBucketList";
import {FsDriverAbstract} from "./Driver/FsDriverAbstract";

export class FileStorage {
readonly #driver: FsDriverAbstract;
readonly #buckets: FsBucketList;

constructor(driver: FsDriverAbstract) {
this.#driver = driver;
this.#buckets = new FsBucketList(this);
}

public getBucket(name: string, options: IBucketOptions = {}): FsBucket {
return this.#buckets.ensure(name, options);
}

public getDriver(): FsDriverAbstract {
return this.#driver;
}
}
16 changes: 16 additions & 0 deletions packages/fs/src/Storage/FileStorageService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Service} from "@bunt/unit";
import {FsDriverAbstract} from "./Driver/FsDriverAbstract";
import {FileStorage} from "./FileStorage";

export class FileStorageService extends Service<FileStorage> {
readonly #driver: FsDriverAbstract;

constructor(driver: FsDriverAbstract) {
super();
this.#driver = driver;
}

public async resolve(): Promise<FileStorage> {
return new FileStorage(this.#driver);
}
}
7 changes: 7 additions & 0 deletions packages/fs/src/Storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from "./FileStorage";
export * from "./FileStorageService";
export * from "./Bucket/FsBucket";
export * from "./Bucket/FsBucketList";
export * from "./Driver/FsDriverAbstract";
export * from "./Driver/MinIOBucketPolicy";
export * from "./Driver/MinIO";
1 change: 1 addition & 0 deletions packages/fs/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Storage";
18 changes: 18 additions & 0 deletions packages/fs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "../../tsconfig.lib.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"references": [
{
"path": "../unit"
},
{
"path": "../util"
}
],
"include": [
"src"
]
}
39 changes: 30 additions & 9 deletions packages/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,35 @@
"files": [],
"include": [],
"references": [
{"path": "./unit"},
{"path": "./app"},
{"path": "./web"},
{"path": "./ws"},
{"path": "./cli"},
{"path": "./util"},
{"path": "./input"},
{"path": "./queue"},
{"path": "./project"}
{
"path": "./unit"
},
{
"path": "./app"
},
{
"path": "./web"
},
{
"path": "./ws"
},
{
"path": "./fs"
},
{
"path": "./cli"
},
{
"path": "./util"
},
{
"path": "./input"
},
{
"path": "./queue"
},
{
"path": "./project"
}
]
}
Loading

0 comments on commit 7d598e0

Please sign in to comment.