@rytass/storages-adapter-gcs 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Rytass Co. Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Rytass Utils - File Storages (Google Cloud Storage)
2
+
3
+ ## Features
4
+
5
+ - [x] Google Cloud Storage bucket
package/index.cjs.js ADDED
@@ -0,0 +1,110 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var storages = require('@rytass/storages');
6
+ var stream = require('stream');
7
+ var uuid = require('uuid');
8
+ var storage = require('@google-cloud/storage');
9
+
10
+ class StorageGCSService extends storages.Storage {
11
+ constructor(options){
12
+ super(options);
13
+ this.storage = new storage.Storage({
14
+ projectId: options.projectId,
15
+ credentials: options.credentials
16
+ });
17
+ this.bucket = this.storage.bucket(options.bucket);
18
+ }
19
+ async url(key, expires = 1000 * 60 * 60 * 24) {
20
+ const file = this.bucket.file(key);
21
+ const [url] = await file.getSignedUrl({
22
+ action: 'read',
23
+ expires
24
+ });
25
+ return url;
26
+ }
27
+ async read(key, options) {
28
+ const file = this.bucket.file(key);
29
+ try {
30
+ if (options?.format === 'buffer') {
31
+ const [buffer] = await file.download();
32
+ return buffer;
33
+ }
34
+ return file.createReadStream();
35
+ } catch (ex) {
36
+ if (/No such object/.test(ex.message)) {
37
+ throw new storages.StorageError(storages.ErrorCode.READ_FILE_ERROR, 'File not found');
38
+ }
39
+ throw ex;
40
+ }
41
+ }
42
+ async writeStreamFile(stream$1, options) {
43
+ const givenFilename = options?.filename;
44
+ if (givenFilename) {
45
+ const file = this.bucket.file(givenFilename);
46
+ const writeStream = file.createWriteStream({
47
+ gzip: 'auto',
48
+ ...options?.contentType ? {
49
+ contentType: options?.contentType
50
+ } : {}
51
+ });
52
+ stream$1.pipe(writeStream);
53
+ return new Promise((resolve)=>{
54
+ writeStream.on('finish', ()=>{
55
+ resolve({
56
+ key: givenFilename
57
+ });
58
+ });
59
+ });
60
+ }
61
+ const tempFilename = uuid.v4();
62
+ const uploadStream = new stream.PassThrough();
63
+ const getFilenamePromise = this.getStreamFilename(stream$1);
64
+ const tempFile = this.bucket.file(tempFilename);
65
+ const writeStream1 = tempFile.createWriteStream({
66
+ gzip: 'auto',
67
+ ...options?.contentType ? {
68
+ contentType: options?.contentType
69
+ } : {}
70
+ });
71
+ stream$1.pipe(uploadStream).pipe(writeStream1);
72
+ const [filename] = await Promise.all([
73
+ getFilenamePromise,
74
+ new Promise((resolve)=>{
75
+ writeStream1.on('finish', resolve);
76
+ }),
77
+ ]);
78
+ await tempFile.move(filename);
79
+ return {
80
+ key: filename
81
+ };
82
+ }
83
+ async writeBufferFile(buffer, options) {
84
+ const filename = options?.filename || await this.getBufferFilename(buffer);
85
+ const file = this.bucket.file(filename);
86
+ await file.save(buffer, {
87
+ gzip: 'auto',
88
+ ...options?.contentType ? {
89
+ contentType: options?.contentType
90
+ } : {}
91
+ });
92
+ return {
93
+ key: filename
94
+ };
95
+ }
96
+ write(file, options) {
97
+ if (file instanceof Buffer) {
98
+ return this.writeBufferFile(file, options);
99
+ }
100
+ return this.writeStreamFile(file, options);
101
+ }
102
+ batchWrite(files) {
103
+ return Promise.all(files.map((file)=>this.write(file)));
104
+ }
105
+ async remove(key) {
106
+ await this.bucket.file(key).delete();
107
+ }
108
+ }
109
+
110
+ exports.StorageGCSService = StorageGCSService;
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './storages-adapter-gcs';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export { StorageGCSService } from './storages-adapter-gcs.js';
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@rytass/storages-adapter-gcs",
3
+ "version": "0.1.1",
4
+ "description": "Google Cloud Storage Adapter for @rytass/storages",
5
+ "keywords": [
6
+ "gcp",
7
+ "cloud",
8
+ "storage",
9
+ "rytass",
10
+ "storage",
11
+ "gcs"
12
+ ],
13
+ "author": "Chia Yu Pai <fantasyatelier@gmail.com>",
14
+ "homepage": "https://github.com/Rytass/Utils#readme",
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/Rytass/Utils.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/Rytass/Utils/issues"
22
+ },
23
+ "dependencies": {
24
+ "@google-cloud/storage": "^6.5.3",
25
+ "@rytass/storages": "^0.1.2",
26
+ "uuid": "^8.3.2"
27
+ },
28
+ "devDependencies": {
29
+ "@types/uuid": "^8.3.4"
30
+ },
31
+ "main": "./index.cjs.js",
32
+ "module": "./index.js",
33
+ "typings": "./index.d.ts"
34
+ }
@@ -0,0 +1,19 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { Storage, ReadBufferFileOptions, ReadStreamFileOptions, InputFile, StorageFile, WriteFileOptions } from '@rytass/storages';
4
+ import { Readable } from 'stream';
5
+ import { GCSOptions } from './typings';
6
+ export declare class StorageGCSService extends Storage<GCSOptions> {
7
+ private readonly storage;
8
+ private readonly bucket;
9
+ constructor(options: GCSOptions);
10
+ url(key: string, expires?: number): Promise<string>;
11
+ read(key: string): Promise<Readable>;
12
+ read(key: string, options: ReadBufferFileOptions): Promise<Buffer>;
13
+ read(key: string, options: ReadStreamFileOptions): Promise<Readable>;
14
+ private writeStreamFile;
15
+ private writeBufferFile;
16
+ write(file: InputFile, options?: WriteFileOptions): Promise<StorageFile>;
17
+ batchWrite(files: InputFile[]): Promise<StorageFile[]>;
18
+ remove(key: string): Promise<void>;
19
+ }
@@ -0,0 +1,106 @@
1
+ import { Storage, StorageError, ErrorCode } from '@rytass/storages';
2
+ import { PassThrough } from 'stream';
3
+ import { v4 } from 'uuid';
4
+ import { Storage as Storage$1 } from '@google-cloud/storage';
5
+
6
+ class StorageGCSService extends Storage {
7
+ constructor(options){
8
+ super(options);
9
+ this.storage = new Storage$1({
10
+ projectId: options.projectId,
11
+ credentials: options.credentials
12
+ });
13
+ this.bucket = this.storage.bucket(options.bucket);
14
+ }
15
+ async url(key, expires = 1000 * 60 * 60 * 24) {
16
+ const file = this.bucket.file(key);
17
+ const [url] = await file.getSignedUrl({
18
+ action: 'read',
19
+ expires
20
+ });
21
+ return url;
22
+ }
23
+ async read(key, options) {
24
+ const file = this.bucket.file(key);
25
+ try {
26
+ if (options?.format === 'buffer') {
27
+ const [buffer] = await file.download();
28
+ return buffer;
29
+ }
30
+ return file.createReadStream();
31
+ } catch (ex) {
32
+ if (/No such object/.test(ex.message)) {
33
+ throw new StorageError(ErrorCode.READ_FILE_ERROR, 'File not found');
34
+ }
35
+ throw ex;
36
+ }
37
+ }
38
+ async writeStreamFile(stream, options) {
39
+ const givenFilename = options?.filename;
40
+ if (givenFilename) {
41
+ const file = this.bucket.file(givenFilename);
42
+ const writeStream = file.createWriteStream({
43
+ gzip: 'auto',
44
+ ...options?.contentType ? {
45
+ contentType: options?.contentType
46
+ } : {}
47
+ });
48
+ stream.pipe(writeStream);
49
+ return new Promise((resolve)=>{
50
+ writeStream.on('finish', ()=>{
51
+ resolve({
52
+ key: givenFilename
53
+ });
54
+ });
55
+ });
56
+ }
57
+ const tempFilename = v4();
58
+ const uploadStream = new PassThrough();
59
+ const getFilenamePromise = this.getStreamFilename(stream);
60
+ const tempFile = this.bucket.file(tempFilename);
61
+ const writeStream1 = tempFile.createWriteStream({
62
+ gzip: 'auto',
63
+ ...options?.contentType ? {
64
+ contentType: options?.contentType
65
+ } : {}
66
+ });
67
+ stream.pipe(uploadStream).pipe(writeStream1);
68
+ const [filename] = await Promise.all([
69
+ getFilenamePromise,
70
+ new Promise((resolve)=>{
71
+ writeStream1.on('finish', resolve);
72
+ }),
73
+ ]);
74
+ await tempFile.move(filename);
75
+ return {
76
+ key: filename
77
+ };
78
+ }
79
+ async writeBufferFile(buffer, options) {
80
+ const filename = options?.filename || await this.getBufferFilename(buffer);
81
+ const file = this.bucket.file(filename);
82
+ await file.save(buffer, {
83
+ gzip: 'auto',
84
+ ...options?.contentType ? {
85
+ contentType: options?.contentType
86
+ } : {}
87
+ });
88
+ return {
89
+ key: filename
90
+ };
91
+ }
92
+ write(file, options) {
93
+ if (file instanceof Buffer) {
94
+ return this.writeBufferFile(file, options);
95
+ }
96
+ return this.writeStreamFile(file, options);
97
+ }
98
+ batchWrite(files) {
99
+ return Promise.all(files.map((file)=>this.write(file)));
100
+ }
101
+ async remove(key) {
102
+ await this.bucket.file(key).delete();
103
+ }
104
+ }
105
+
106
+ export { StorageGCSService };
package/typings.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { StorageOptions } from '@rytass/storages';
2
+ export interface GCSOptions extends StorageOptions {
3
+ bucket: string;
4
+ projectId: string;
5
+ credentials: {
6
+ client_email: string;
7
+ private_key: string;
8
+ };
9
+ }