@xnestjs/storage 1.12.2 → 1.13.0

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.
Files changed (31) hide show
  1. package/package.json +12 -25
  2. package/{esm/providers → providers}/s3-storage-connection.js +3 -1
  3. package/{esm/services → services}/storage-bucket.js +2 -0
  4. package/{esm/storage-core.module.js → storage-core.module.js} +8 -8
  5. package/cjs/constants.js +0 -5
  6. package/cjs/get-storage-config.js +0 -35
  7. package/cjs/index.js +0 -9
  8. package/cjs/package.json +0 -3
  9. package/cjs/providers/s3-storage-connection.js +0 -97
  10. package/cjs/services/storage-bucket.js +0 -28
  11. package/cjs/services/storage-connection.js +0 -10
  12. package/cjs/storage-core.module.js +0 -88
  13. package/cjs/storage.module.js +0 -25
  14. package/cjs/types.js +0 -2
  15. package/esm/package.json +0 -3
  16. package/types/index.d.cts +0 -6
  17. /package/{types/constants.d.ts → constants.d.ts} +0 -0
  18. /package/{esm/constants.js → constants.js} +0 -0
  19. /package/{types/get-storage-config.d.ts → get-storage-config.d.ts} +0 -0
  20. /package/{esm/get-storage-config.js → get-storage-config.js} +0 -0
  21. /package/{types/index.d.ts → index.d.ts} +0 -0
  22. /package/{esm/index.js → index.js} +0 -0
  23. /package/{types/providers → providers}/s3-storage-connection.d.ts +0 -0
  24. /package/{types/services → services}/storage-bucket.d.ts +0 -0
  25. /package/{types/services → services}/storage-connection.d.ts +0 -0
  26. /package/{esm/services → services}/storage-connection.js +0 -0
  27. /package/{types/storage-core.module.d.ts → storage-core.module.d.ts} +0 -0
  28. /package/{types/storage.module.d.ts → storage.module.d.ts} +0 -0
  29. /package/{esm/storage.module.js → storage.module.js} +0 -0
  30. /package/{types/types.d.ts → types.d.ts} +0 -0
  31. /package/{esm/types.js → types.js} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xnestjs/storage",
3
- "version": "1.12.2",
3
+ "version": "1.13.0",
4
4
  "description": "NestJS extension library for Storage solutions (S3,GS)",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -17,41 +17,28 @@
17
17
  "minio": "^8.0.6"
18
18
  },
19
19
  "type": "module",
20
+ "module": "./index.js",
21
+ "types": "./index.d.ts",
20
22
  "exports": {
21
23
  ".": {
22
- "import": {
23
- "types": "./types/index.d.ts",
24
- "default": "./esm/index.js"
25
- },
26
- "require": {
27
- "types": "./types/index.d.cts",
28
- "default": "./cjs/index.js"
29
- },
30
- "default": "./esm/index.js"
24
+ "types": "./index.d.ts",
25
+ "default": "./index.js"
31
26
  },
32
27
  "./package.json": "./package.json"
33
28
  },
34
- "main": "./cjs/index.js",
35
- "module": "./esm/index.js",
36
- "types": "./types/index.d.ts",
29
+ "engines": {
30
+ "node": ">=20.0"
31
+ },
37
32
  "repository": {
38
33
  "registry": "https://github.com/panates/xnestjs.git",
39
34
  "directory": "packages/storage"
40
35
  },
41
- "engines": {
42
- "node": ">=16.0",
43
- "npm": ">=7.0.0"
44
- },
45
- "files": [
46
- "cjs/",
47
- "esm/",
48
- "types/",
49
- "LICENSE",
50
- "README.md"
51
- ],
52
36
  "keywords": [
53
37
  "nestjs",
54
38
  "storage",
55
39
  "s3"
56
- ]
40
+ ],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ }
57
44
  }
@@ -1,9 +1,11 @@
1
1
  import * as Minio from 'minio';
2
2
  import { StorageConnection } from '../services/storage-connection.js';
3
3
  export class S3StorageConnection extends StorageConnection {
4
+ provider = 's3';
5
+ config;
6
+ _client;
4
7
  constructor(config) {
5
8
  super();
6
- this.provider = 's3';
7
9
  this.config = config;
8
10
  this._client = new Minio.Client(config);
9
11
  this._client.setRequestOptions({
@@ -1,4 +1,6 @@
1
1
  export class StorageBucket {
2
+ _connection;
3
+ _bucketName;
2
4
  constructor(_connection, _bucketName) {
3
5
  this._connection = _connection;
4
6
  this._bucketName = _bucketName;
@@ -44,6 +44,7 @@ export class StorageCoreModule {
44
44
  static _createDynamicModule(opts, metadata) {
45
45
  const token = opts.token ?? StorageConnection;
46
46
  const providers = [
47
+ ...(metadata.providers ?? []),
47
48
  {
48
49
  provide: token,
49
50
  inject: [STORAGE_OPTIONS],
@@ -65,18 +66,17 @@ export class StorageCoreModule {
65
66
  ? new Logger(opts.logger)
66
67
  : opts.logger,
67
68
  },
69
+ {
70
+ provide: STORAGE_MODULE_ID,
71
+ useValue: crypto.randomUUID(),
72
+ },
68
73
  ];
69
74
  return {
75
+ global: opts.global,
70
76
  module: StorageCoreModule,
77
+ imports: opts.imports,
71
78
  ...metadata,
72
- providers: [
73
- ...(metadata.providers ?? []),
74
- ...providers,
75
- {
76
- provide: STORAGE_MODULE_ID,
77
- useValue: crypto.randomUUID(),
78
- },
79
- ],
79
+ providers,
80
80
  exports: [STORAGE_OPTIONS, token, ...(metadata.exports ?? [])],
81
81
  };
82
82
  }
package/cjs/constants.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.STORAGE_MODULE_ID = exports.STORAGE_OPTIONS = void 0;
4
- exports.STORAGE_OPTIONS = Symbol('STORAGE_OPTIONS');
5
- exports.STORAGE_MODULE_ID = Symbol('STORAGE_MODULE_ID');
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStorageConfig = getStorageConfig;
4
- const tslib_1 = require("tslib");
5
- const node_process_1 = tslib_1.__importDefault(require("node:process"));
6
- const objects_1 = require("@jsopen/objects");
7
- const putil_varhelpers_1 = require("putil-varhelpers");
8
- function getStorageConfig(moduleOptions, prefix = 'STORAGE_') {
9
- const options = (0, objects_1.clone)(moduleOptions);
10
- const env = node_process_1.default.env;
11
- options.provider = options.provider || env[prefix + 'PROVIDER'];
12
- if (!options.provider)
13
- throw new Error(`You must provide a Storage provider or "${prefix + 'PROVIDER'}" env variable`);
14
- if (options.provider === 's3') {
15
- options.s3 = options.s3 || {};
16
- options.s3.endPoint = options.s3.endPoint ?? env[prefix + 'S3_ENDPOINT'];
17
- options.s3.accessKey =
18
- options.s3.accessKey ?? env[prefix + 'S3_ACCESS_KEY'];
19
- options.s3.secretKey =
20
- options.s3.secretKey ?? env[prefix + 'S3_SECRET_KEY'];
21
- options.s3.useSSL = options.s3.useSSL ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'S3_SSL']);
22
- options.s3.port = options.s3.port ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'S3_PORT']);
23
- options.s3.sessionToken =
24
- options.s3.sessionToken ?? env[prefix + 'S3_SESSION_TOKEN'];
25
- options.s3.partSize =
26
- options.s3.partSize ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'S3_PART_SIZE']);
27
- options.s3.pathStyle =
28
- options.s3.pathStyle ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'S3_PATH_STYLE']);
29
- options.s3.s3AccelerateEndpoint =
30
- options.s3.s3AccelerateEndpoint ?? env[prefix + 'S3_ACC_ENDPOINT'];
31
- }
32
- else
33
- throw new Error(`Unknown Storage provider (${options.provider})`);
34
- return options;
35
- }
package/cjs/index.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./constants.js"), exports);
5
- tslib_1.__exportStar(require("./get-storage-config.js"), exports);
6
- tslib_1.__exportStar(require("./services/storage-bucket.js"), exports);
7
- tslib_1.__exportStar(require("./services/storage-connection.js"), exports);
8
- tslib_1.__exportStar(require("./storage.module.js"), exports);
9
- tslib_1.__exportStar(require("./types.js"), exports);
package/cjs/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
@@ -1,97 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.S3StorageConnection = void 0;
4
- const tslib_1 = require("tslib");
5
- const Minio = tslib_1.__importStar(require("minio"));
6
- const storage_connection_js_1 = require("../services/storage-connection.js");
7
- class S3StorageConnection extends storage_connection_js_1.StorageConnection {
8
- constructor(config) {
9
- super();
10
- this.provider = 's3';
11
- this.config = config;
12
- this._client = new Minio.Client(config);
13
- this._client.setRequestOptions({
14
- rejectUnauthorized: config.rejectUnauthorized,
15
- });
16
- }
17
- async putObject(bucketName, objectName, source, options) {
18
- const meta = options ? updateMetadata(options?.metadata, options) : {};
19
- if (typeof source === 'string')
20
- await this._client.fPutObject(bucketName, objectName, source, meta);
21
- else
22
- await this._client.putObject(bucketName, objectName, source, undefined, meta);
23
- }
24
- async getObjectInfo(bucketName, objectName) {
25
- const o = await this._client.statObject(bucketName, objectName);
26
- const inf = {
27
- size: o.size,
28
- etag: o.etag,
29
- lastModified: o.lastModified,
30
- metadata: o.metaData,
31
- };
32
- if (inf.metadata) {
33
- const meta = inf.metadata;
34
- removeKey(meta, 'content-length');
35
- removeKey(meta, 'last-modified');
36
- if (meta['Content-Type']) {
37
- inf.contentLanguage = meta['Content-Type'];
38
- removeKey(meta, 'content-type');
39
- }
40
- if (meta['Content-Language']) {
41
- inf.contentLanguage = meta['Content-Language'];
42
- removeKey(meta, 'content-language');
43
- }
44
- if (meta['Content-Encoding']) {
45
- inf.contentEncoding = meta['Content-Encoding'];
46
- removeKey(meta, 'content-encoding');
47
- }
48
- if (meta['Content-Name']) {
49
- inf.originalName = meta['Content-Name'];
50
- removeKey(meta, 'content-name');
51
- }
52
- }
53
- return inf;
54
- }
55
- async removeObject(bucketName, objectName) {
56
- await this._client.removeObject(bucketName, objectName);
57
- }
58
- async getFile(bucketName, objectName, filePath) {
59
- await this._client.fGetObject(bucketName, objectName, filePath);
60
- }
61
- getObject(bucketName, objectName) {
62
- return this._client.getObject(bucketName, objectName);
63
- }
64
- presignedGetObject(bucketName, objectName, options) {
65
- const expires = options?.expires || 5 * 60 * 60; // 5 minutes
66
- return this._client.presignedGetObject(bucketName, objectName, expires);
67
- }
68
- }
69
- exports.S3StorageConnection = S3StorageConnection;
70
- function updateMetadata(meta, options) {
71
- if (!options)
72
- return meta;
73
- const newMeta = { ...meta };
74
- if (options.contentType) {
75
- removeKey(newMeta, 'content-type');
76
- newMeta['Content-Type'] = options.contentType;
77
- }
78
- if (options.contentLanguage) {
79
- removeKey(newMeta, 'content-language');
80
- newMeta['Content-Language'] = options.contentLanguage;
81
- }
82
- if (options.contentEncoding) {
83
- removeKey(newMeta, 'content-encoding');
84
- newMeta['Content-Encoding'] = options.contentLanguage;
85
- }
86
- if (options.originalName) {
87
- removeKey(newMeta, 'content-name');
88
- newMeta['Content-Name'] = options.originalName;
89
- }
90
- return newMeta;
91
- }
92
- function removeKey(obj, key) {
93
- key = key.toLowerCase();
94
- const k = Object.keys(obj).find(x => x.toLowerCase() === key);
95
- if (k)
96
- delete obj[k];
97
- }
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StorageBucket = void 0;
4
- class StorageBucket {
5
- constructor(_connection, _bucketName) {
6
- this._connection = _connection;
7
- this._bucketName = _bucketName;
8
- }
9
- putObject(objectName, buffer, options) {
10
- return this._connection.putObject(this._bucketName, objectName, buffer, options);
11
- }
12
- getObjectInfo(objectName) {
13
- return this._connection.getObjectInfo(this._bucketName, objectName);
14
- }
15
- removeObject(objectName) {
16
- return this._connection.removeObject(this._bucketName, objectName);
17
- }
18
- getFile(objectName, filePath) {
19
- return this._connection.getFile(this._bucketName, objectName, filePath);
20
- }
21
- getObject(objectName) {
22
- return this._connection.getObject(this._bucketName, objectName);
23
- }
24
- presignedGetObject(objectName, options) {
25
- return this._connection.presignedGetObject(this._bucketName, objectName, options);
26
- }
27
- }
28
- exports.StorageBucket = StorageBucket;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StorageConnection = void 0;
4
- const storage_bucket_js_1 = require("./storage-bucket.js");
5
- class StorageConnection {
6
- getBucket(bucketName) {
7
- return new storage_bucket_js_1.StorageBucket(this, bucketName);
8
- }
9
- }
10
- exports.StorageConnection = StorageConnection;
@@ -1,88 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StorageCoreModule = void 0;
4
- const tslib_1 = require("tslib");
5
- const node_assert_1 = tslib_1.__importDefault(require("node:assert"));
6
- const common_1 = require("@nestjs/common");
7
- const crypto_1 = tslib_1.__importDefault(require("crypto"));
8
- const constants_js_1 = require("./constants.js");
9
- const get_storage_config_js_1 = require("./get-storage-config.js");
10
- const s3_storage_connection_js_1 = require("./providers/s3-storage-connection.js");
11
- const storage_connection_js_1 = require("./services/storage-connection.js");
12
- const CLIENT_TOKEN = Symbol('CLIENT_TOKEN');
13
- class StorageCoreModule {
14
- /**
15
- * Configures and returns a dynamic module
16
- */
17
- static forRoot(moduleOptions) {
18
- const connectionOptions = (0, get_storage_config_js_1.getStorageConfig)(moduleOptions.useValue || {}, moduleOptions.envPrefix);
19
- return this._createDynamicModule(moduleOptions, {
20
- global: moduleOptions.global,
21
- providers: [
22
- {
23
- provide: constants_js_1.STORAGE_OPTIONS,
24
- useValue: connectionOptions,
25
- },
26
- ],
27
- });
28
- }
29
- /**
30
- * Configures and returns an async dynamic module
31
- */
32
- static forRootAsync(asyncOptions) {
33
- node_assert_1.default.ok(asyncOptions.useFactory, 'useFactory is required');
34
- return this._createDynamicModule(asyncOptions, {
35
- global: asyncOptions.global,
36
- providers: [
37
- {
38
- provide: constants_js_1.STORAGE_OPTIONS,
39
- inject: asyncOptions.inject,
40
- useFactory: async (...args) => {
41
- const opts = await asyncOptions.useFactory(...args);
42
- return (0, get_storage_config_js_1.getStorageConfig)(opts, asyncOptions.envPrefix);
43
- },
44
- },
45
- ],
46
- });
47
- }
48
- static _createDynamicModule(opts, metadata) {
49
- const token = opts.token ?? storage_connection_js_1.StorageConnection;
50
- const providers = [
51
- {
52
- provide: token,
53
- inject: [constants_js_1.STORAGE_OPTIONS],
54
- useFactory: async (storageOptions) => {
55
- if (storageOptions.provider === 's3') {
56
- return new s3_storage_connection_js_1.S3StorageConnection(storageOptions.s3);
57
- }
58
- /** istanbul ignore next */
59
- throw new Error(`Unknown Storage provider (${storageOptions.provider})`);
60
- },
61
- },
62
- {
63
- provide: CLIENT_TOKEN,
64
- useExisting: token,
65
- },
66
- {
67
- provide: common_1.Logger,
68
- useValue: typeof opts.logger === 'string'
69
- ? new common_1.Logger(opts.logger)
70
- : opts.logger,
71
- },
72
- ];
73
- return {
74
- module: StorageCoreModule,
75
- ...metadata,
76
- providers: [
77
- ...(metadata.providers ?? []),
78
- ...providers,
79
- {
80
- provide: constants_js_1.STORAGE_MODULE_ID,
81
- useValue: crypto_1.default.randomUUID(),
82
- },
83
- ],
84
- exports: [constants_js_1.STORAGE_OPTIONS, token, ...(metadata.exports ?? [])],
85
- };
86
- }
87
- }
88
- exports.StorageCoreModule = StorageCoreModule;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- var StorageModule_1;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.StorageModule = void 0;
5
- const tslib_1 = require("tslib");
6
- const common_1 = require("@nestjs/common");
7
- const storage_core_module_js_1 = require("./storage-core.module.js");
8
- let StorageModule = StorageModule_1 = class StorageModule {
9
- static forRoot(options) {
10
- return {
11
- module: StorageModule_1,
12
- imports: [storage_core_module_js_1.StorageCoreModule.forRoot(options || {})],
13
- };
14
- }
15
- static forRootAsync(options) {
16
- return {
17
- module: StorageModule_1,
18
- imports: [storage_core_module_js_1.StorageCoreModule.forRootAsync(options)],
19
- };
20
- }
21
- };
22
- exports.StorageModule = StorageModule;
23
- exports.StorageModule = StorageModule = StorageModule_1 = tslib_1.__decorate([
24
- (0, common_1.Module)({})
25
- ], StorageModule);
package/cjs/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/esm/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
package/types/index.d.cts DELETED
@@ -1,6 +0,0 @@
1
- export * from './constants.js';
2
- export * from './get-storage-config.js';
3
- export * from './services/storage-bucket.js';
4
- export * from './services/storage-connection.js';
5
- export * from './storage.module.js';
6
- export * from './types.js';
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes