mbkbucket 4.0.0 → 4.0.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.
@@ -1,86 +1,86 @@
1
- const TRUE_VALUES = new Set(['true', 't', '1', 'yes', 'y', 'on']);
2
- const FALSE_VALUES = new Set(['false', 'f', '0', 'no', 'n', 'off']);
3
- const REQUIRED_S3_FIELDS = ['BUCKET_NAME', 'ACCESS_KEY_ID', 'SECRET_ACCESS_KEY', 'ENDPOINT'];
4
-
5
- export function normalizeBooleanLike(value) {
6
- if (typeof value === 'boolean') return value;
7
- if (typeof value === 'string') {
8
- const norm = value.trim().toLowerCase();
9
- if (TRUE_VALUES.has(norm)) return true;
10
- if (FALSE_VALUES.has(norm)) return false;
11
- }
12
- return value;
13
- }
14
-
15
- export function parseAndValidateMbkbucketVar(rawValue) {
16
- let parsed;
17
- try {
18
- parsed = rawValue ? JSON.parse(rawValue) : {};
19
- } catch {
20
- throw new Error('Invalid JSON in process.env.mbkbucketVar');
21
- }
22
-
23
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
24
- throw new Error('mbkbucketVar must be a valid object');
25
- }
26
-
27
- const defaults = { p_view_inline: true, publiView_enabled: false };
28
- for (const [key, defaultVal] of Object.entries(defaults)) {
29
- const val = parsed[key];
30
- if (val === undefined || (typeof val === 'string' && val.trim() === '')) {
31
- parsed[key] = defaultVal;
32
- }
33
- }
34
-
35
- parsed.p_view_inline = normalizeBooleanLike(parsed.p_view_inline);
36
- parsed.publiView_enabled = normalizeBooleanLike(parsed.publiView_enabled);
37
-
38
- const errors = [];
39
- if (typeof parsed.p_view_inline !== 'boolean') errors.push('mbkbucketVar.p_view_inline must be a boolean if provided');
40
- if (typeof parsed.publiView_enabled !== 'boolean') errors.push('mbkbucketVar.publiView_enabled must be a boolean if provided');
41
- if (errors.length) throw new Error(errors.join(' | '));
42
-
43
- return parsed;
44
- }
45
-
46
- export function parseAndValidateBucketConnection(rawValue) {
47
- if (!rawValue) return null;
48
-
49
- let parsed;
50
- try {
51
- parsed = JSON.parse(rawValue);
52
- } catch {
53
- throw new Error('BucketConnection is not valid JSON');
54
- }
55
-
56
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed) || !Object.keys(parsed).length) {
57
- throw new Error('BucketConnection must be a non-empty object');
58
- }
59
-
60
- for (const [bucketName, config] of Object.entries(parsed)) {
61
- if (!config || typeof config !== 'object' || Array.isArray(config)) {
62
- throw new Error(`Bucket '${bucketName}' configuration must be an object, not a string. Remove quotes around the inner JSON object.`);
63
- }
64
-
65
- const providerType = String(config.type || config.provider || '').toLowerCase();
66
-
67
- if (providerType === 'gdrive' || providerType === 'google-drive' || providerType === 'googledrive') {
68
- const hasOAuth = Boolean(config.client_id && config.client_secret && (config.refresh_token || config.access_token || config.code));
69
- const hasServiceAccount = Boolean((config.client_email || config.service_account_email) && (config.private_key || config.service_account_key));
70
- const hasAccessTokenOnly = Boolean(config.access_token);
71
- const hasServiceAccountKeyObj = Boolean(config.service_account && typeof config.service_account === 'object');
72
-
73
- if (!hasOAuth && !hasServiceAccount && !hasAccessTokenOnly && !hasServiceAccountKeyObj) {
74
- throw new Error(`Google Drive connection '${bucketName}' requires OAuth credentials (client_id, client_secret, refresh_token) or Service Account credentials (client_email, private_key).`);
75
- }
76
- } else {
77
- // Default is S3-compatible provider
78
- const missingFields = REQUIRED_S3_FIELDS.filter(field => !config[field]);
79
- if (missingFields.length) {
80
- throw new Error(`Bucket '${bucketName}' is missing required fields: ${missingFields.join(', ')}`);
81
- }
82
- }
83
- }
84
-
85
- return parsed;
86
- }
1
+ const TRUE_VALUES = new Set(['true', 't', '1', 'yes', 'y', 'on']);
2
+ const FALSE_VALUES = new Set(['false', 'f', '0', 'no', 'n', 'off']);
3
+ const REQUIRED_S3_FIELDS = ['BUCKET_NAME', 'ACCESS_KEY_ID', 'SECRET_ACCESS_KEY', 'ENDPOINT'];
4
+
5
+ export function normalizeBooleanLike(value) {
6
+ if (typeof value === 'boolean') return value;
7
+ if (typeof value === 'string') {
8
+ const norm = value.trim().toLowerCase();
9
+ if (TRUE_VALUES.has(norm)) return true;
10
+ if (FALSE_VALUES.has(norm)) return false;
11
+ }
12
+ return value;
13
+ }
14
+
15
+ export function parseAndValidateMbkbucketVar(rawValue) {
16
+ let parsed;
17
+ try {
18
+ parsed = rawValue ? JSON.parse(rawValue) : {};
19
+ } catch {
20
+ throw new Error('Invalid JSON in process.env.mbkbucketVar');
21
+ }
22
+
23
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
24
+ throw new Error('mbkbucketVar must be a valid object');
25
+ }
26
+
27
+ const defaults = { p_view_inline: true, publiView_enabled: false };
28
+ for (const [key, defaultVal] of Object.entries(defaults)) {
29
+ const val = parsed[key];
30
+ if (val === undefined || (typeof val === 'string' && val.trim() === '')) {
31
+ parsed[key] = defaultVal;
32
+ }
33
+ }
34
+
35
+ parsed.p_view_inline = normalizeBooleanLike(parsed.p_view_inline);
36
+ parsed.publiView_enabled = normalizeBooleanLike(parsed.publiView_enabled);
37
+
38
+ const errors = [];
39
+ if (typeof parsed.p_view_inline !== 'boolean') errors.push('mbkbucketVar.p_view_inline must be a boolean if provided');
40
+ if (typeof parsed.publiView_enabled !== 'boolean') errors.push('mbkbucketVar.publiView_enabled must be a boolean if provided');
41
+ if (errors.length) throw new Error(errors.join(' | '));
42
+
43
+ return parsed;
44
+ }
45
+
46
+ export function parseAndValidateBucketConnection(rawValue) {
47
+ if (!rawValue) return null;
48
+
49
+ let parsed;
50
+ try {
51
+ parsed = JSON.parse(rawValue);
52
+ } catch {
53
+ throw new Error('BucketConnection is not valid JSON');
54
+ }
55
+
56
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed) || !Object.keys(parsed).length) {
57
+ throw new Error('BucketConnection must be a non-empty object');
58
+ }
59
+
60
+ for (const [bucketName, config] of Object.entries(parsed)) {
61
+ if (!config || typeof config !== 'object' || Array.isArray(config)) {
62
+ throw new Error(`Bucket '${bucketName}' configuration must be an object, not a string. Remove quotes around the inner JSON object.`);
63
+ }
64
+
65
+ const providerType = String(config.type || config.provider || '').toLowerCase();
66
+
67
+ if (providerType === 'gdrive' || providerType === 'google-drive' || providerType === 'googledrive') {
68
+ const hasOAuth = Boolean(config.client_id && config.client_secret && (config.refresh_token || config.access_token || config.code));
69
+ const hasServiceAccount = Boolean((config.client_email || config.service_account_email) && (config.private_key || config.service_account_key));
70
+ const hasAccessTokenOnly = Boolean(config.access_token);
71
+ const hasServiceAccountKeyObj = Boolean(config.service_account && typeof config.service_account === 'object');
72
+
73
+ if (!hasOAuth && !hasServiceAccount && !hasAccessTokenOnly && !hasServiceAccountKeyObj) {
74
+ throw new Error(`Google Drive connection '${bucketName}' requires OAuth credentials (client_id, client_secret, refresh_token) or Service Account credentials (client_email, private_key).`);
75
+ }
76
+ } else {
77
+ // Default is S3-compatible provider
78
+ const missingFields = REQUIRED_S3_FIELDS.filter(field => !config[field]);
79
+ if (missingFields.length) {
80
+ throw new Error(`Bucket '${bucketName}' is missing required fields: ${missingFields.join(', ')}`);
81
+ }
82
+ }
83
+ }
84
+
85
+ return parsed;
86
+ }