@quatrain/storage-supabase 1.1.11 → 1.1.13

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.
@@ -3,16 +3,17 @@
3
3
  /// <reference types="node" />
4
4
  import { AbstractStorageAdapter, FileType, FileResponseLinkType, StorageParameters, DownloadFileMetaType } from '@quatrain/storage';
5
5
  import { Readable, Stream } from 'stream';
6
- import { SupabaseClient } from '@supabase/supabase-js';
6
+ import { StorageClient } from '@supabase/storage-js';
7
7
  export declare class SupabaseStorageAdapter extends AbstractStorageAdapter {
8
- protected _client: SupabaseClient;
8
+ protected _client: StorageClient;
9
9
  constructor(params: StorageParameters);
10
- getDriver(): SupabaseClient<any, "public", any>;
10
+ getDriver(): StorageClient;
11
11
  getMetaData(file: FileType): Promise<FileType>;
12
+ test(): Promise<boolean>;
12
13
  streamToBuffer(stream: Stream): Promise<Buffer>;
13
- create(file: FileType, stream: Readable): Promise<FileType>;
14
+ create(file: FileType, stream: Readable | string): Promise<FileType>;
14
15
  copy(file: FileType, destFile: FileType): Promise<void>;
15
- move(file: FileType, destFile: FileType): Promise<string | FileType>;
16
+ move(file: FileType, destFile: FileType): Promise<FileType>;
16
17
  getUrl(file: FileType, expiresIn?: number): Promise<{
17
18
  url: string;
18
19
  expiresIn: number;
@@ -8,18 +8,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.SupabaseStorageAdapter = void 0;
13
16
  const storage_1 = require("@quatrain/storage");
14
17
  const stream_1 = require("stream");
15
- const supabase_js_1 = require("@supabase/supabase-js");
18
+ const storage_js_1 = require("@supabase/storage-js");
16
19
  const os_1 = require("os");
17
20
  const path_1 = require("path");
21
+ const node_fs_1 = __importDefault(require("node:fs"));
18
22
  class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
19
23
  constructor(params) {
20
24
  super(params);
21
- this._client = (0, supabase_js_1.createClient)(this._params.config.supabaseUrl, this._params.config.supabaseKey);
22
- storage_1.Storage.log(`[ASA] Supabase Storage Adapter initialized`);
25
+ this._client = new storage_js_1.StorageClient(params.config.endpoint, {
26
+ apikey: params.config.secret,
27
+ Authorization: `Bearer ${params.config.secret}`,
28
+ });
29
+ storage_1.Storage.log(`[SSA] Supabase Storage Adapter initialized`);
23
30
  }
24
31
  getDriver() {
25
32
  return this._client;
@@ -27,6 +34,23 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
27
34
  getMetaData(file) {
28
35
  return new Promise(() => file);
29
36
  }
37
+ test() {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ try {
40
+ const { data, error } = yield this._client.listBuckets();
41
+ if (error !== null) {
42
+ storage_1.Storage.error(`Unable to get buckets list`);
43
+ throw new Error(`Unable to get buckets list: ${error.message}`);
44
+ }
45
+ // Storage.info(`S3 Buckets: ${JSON.stringify(data)}`)
46
+ return true;
47
+ }
48
+ catch (err) {
49
+ storage_1.Storage.error(`Failed to connect to storage: ${err.message}`);
50
+ return false;
51
+ }
52
+ });
53
+ }
30
54
  streamToBuffer(stream) {
31
55
  return __awaiter(this, void 0, void 0, function* () {
32
56
  return new Promise((resolve, reject) => {
@@ -39,21 +63,28 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
39
63
  }
40
64
  create(file, stream) {
41
65
  return __awaiter(this, void 0, void 0, function* () {
42
- storage_1.Storage.log(`Uploading ${file.ref} to ${file.bucket}`);
43
- const { data, error } = yield this._client.storage
44
- .from(file.bucket)
45
- .upload(file.ref, yield this.streamToBuffer(stream), {
46
- cacheControl: '3600',
47
- upsert: false,
48
- });
66
+ console.log(stream);
67
+ storage_1.Storage.info(`Uploading ${file.ref} to ${file.bucket}`);
68
+ if (typeof stream === 'string') {
69
+ const { data, error } = yield this._client
70
+ .from(file.bucket)
71
+ .upload(file.ref, new Blob([node_fs_1.default.readFileSync(stream)]), {
72
+ // cacheControl: '3600',
73
+ upsert: false,
74
+ contentType: file.contentType,
75
+ });
76
+ if (error !== null) {
77
+ storage_1.Storage.error(`Unable to upload ${file.ref} to ${file.bucket}`);
78
+ throw new Error(`Unable to upload ${file.ref} to ${file.bucket}`);
79
+ }
80
+ }
49
81
  return file;
50
82
  });
51
83
  }
52
84
  copy(file, destFile) {
53
85
  return __awaiter(this, void 0, void 0, function* () {
54
- storage_1.Storage.log(`Copying file ${file.ref} to ${destFile.ref} in bucket ${file.bucket}`);
55
86
  try {
56
- const response = yield this._client.storage
87
+ const response = yield this._client
57
88
  .from(file.bucket)
58
89
  .copy(file.ref, destFile.ref, {
59
90
  destinationBucket: destFile.bucket,
@@ -67,21 +98,23 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
67
98
  }
68
99
  move(file, destFile) {
69
100
  return __awaiter(this, void 0, void 0, function* () {
70
- try {
71
- yield this.copy(file, destFile);
72
- yield this.delete(file);
73
- return destFile;
74
- }
75
- catch (err) {
76
- return err.message;
101
+ storage_1.Storage.info(`Moving file ${file.ref} to ${destFile.ref} in same bucket ${file.bucket}`);
102
+ const { data, error } = yield this._client
103
+ .from(file.bucket)
104
+ .move(file.ref, destFile.ref);
105
+ if (error !== null) {
106
+ storage_1.Storage.error(`Unable to move ${file.ref} to ${destFile.ref}: ${error.message}`);
107
+ throw new Error(`Unable to move ${file.ref} to ${destFile.ref}`);
77
108
  }
109
+ return destFile;
78
110
  });
79
111
  }
80
112
  getUrl(file, expiresIn = 3600) {
81
113
  return __awaiter(this, void 0, void 0, function* () {
82
- const { data, error } = yield this._client.storage
114
+ storage_1.Storage.info(`Getting signed url for file ${file.ref} in bucket ${file.bucket}`);
115
+ const { data, error } = yield this._client
83
116
  .from(file.bucket)
84
- .createSignedUrl(file.path, expiresIn);
117
+ .createSignedUrl(file.ref, expiresIn, { download: true });
85
118
  if (error !== null) {
86
119
  throw new Error(`Unable to get signed url: ${error}`);
87
120
  }
@@ -90,13 +123,12 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
90
123
  }
91
124
  delete(file) {
92
125
  return __awaiter(this, void 0, void 0, function* () {
93
- const { error } = yield this._client.storage
94
- .from(file.bucket)
95
- .remove([file.ref]);
126
+ storage_1.Storage.info(`Deleting file ${file.ref} in bucket ${file.bucket}`);
127
+ const { error } = yield this._client.from(file.bucket).remove([file.ref]);
96
128
  if (error !== null) {
97
129
  throw new Error(`Unable to delete ${file.ref}`);
98
130
  }
99
- storage_1.Storage.log('Object successfully deleted');
131
+ storage_1.Storage.info(`Object ${file.ref} successfully deleted`);
100
132
  return true;
101
133
  });
102
134
  }
@@ -127,7 +159,7 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
127
159
  }
128
160
  download(file, meta) {
129
161
  return __awaiter(this, void 0, void 0, function* () {
130
- const { data, error } = yield this._client.storage
162
+ const { data, error } = yield this._client
131
163
  .from(file.bucket)
132
164
  .download(meta.path);
133
165
  if (error !== null) {
@@ -140,14 +172,15 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
140
172
  return meta.path;
141
173
  });
142
174
  }
143
- getUploadUrl(file, _expiresIn = 3600) {
175
+ getUploadUrl(file, _expiresIn = 7200) {
144
176
  return __awaiter(this, void 0, void 0, function* () {
145
- const { data, error } = yield this._client.storage
177
+ const { data, error } = yield this._client
146
178
  .from(file.bucket)
147
- .createSignedUploadUrl(file.ref);
179
+ .createSignedUploadUrl(file.ref, { upsert: true });
148
180
  if (error !== null) {
149
181
  throw new Error(`Unable to get signed upload url: ${error}`);
150
182
  }
183
+ storage_1.Storage.info(`Upload URL for ${file.ref} in bucket ${file.bucket} is ${data === null || data === void 0 ? void 0 : data.signedUrl}`);
151
184
  return {
152
185
  url: data === null || data === void 0 ? void 0 : data.signedUrl,
153
186
  method: 'PUT',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/storage-supabase",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "Storage adapter for Supabase Storage",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -22,16 +22,20 @@
22
22
  "typescript": "^5.1.5"
23
23
  },
24
24
  "dependencies": {
25
- "@quatrain/core": "^1.1.15",
26
- "@quatrain/storage": "^1.1.13",
25
+ "@quatrain/core": "^1.1.19",
26
+ "@quatrain/storage": "^1.1.16",
27
+ "@supabase/storage-js": "^2.7.2",
27
28
  "@supabase/supabase-js": "^2.45.1"
28
29
  },
29
30
  "scripts": {
30
- "test": "tsc && jest --verbose",
31
+ "test-ci": "jest --runInBand",
31
32
  "build": "tsc",
32
33
  "wbuild": "tsc --watch",
33
34
  "bump-to": "yarn version",
34
- "patch": "yarn version --patch --no-git-tag-version",
35
- "publish": "yarn build && yarn npm publish --access public"
35
+ "hash": "node ../../bin/hashFolder.js",
36
+ "hash:persist": "yarn hash > .hash_latest.txt",
37
+ "hash:compare": "yarn hash > .hash_newest.txt && cmp -s .hash_latest.txt .hash_newest.txt",
38
+ "publish": "yarn hash:compare || yarn publish:process",
39
+ "publish:process": "yarn version patch && yarn build && yarn npm publish --access public && yarn hash:persist"
36
40
  }
37
41
  }