@quatrain/storage-supabase 1.1.7 → 1.1.12

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,15 +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
+ getMetaData(file: FileType): Promise<FileType>;
12
+ test(): Promise<boolean>;
11
13
  streamToBuffer(stream: Stream): Promise<Buffer>;
12
- create(file: FileType, stream: Readable): Promise<FileType>;
14
+ create(file: FileType, stream: Readable | string): Promise<FileType>;
13
15
  copy(file: FileType, destFile: FileType): Promise<void>;
14
- move(file: FileType, destFile: FileType): Promise<string | FileType>;
16
+ move(file: FileType, destFile: FileType): Promise<FileType>;
15
17
  getUrl(file: FileType, expiresIn?: number): Promise<{
16
18
  url: string;
17
19
  expiresIn: number;
@@ -8,22 +8,49 @@ 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;
26
33
  }
34
+ getMetaData(file) {
35
+ return new Promise(() => file);
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
+ }
27
54
  streamToBuffer(stream) {
28
55
  return __awaiter(this, void 0, void 0, function* () {
29
56
  return new Promise((resolve, reject) => {
@@ -36,21 +63,28 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
36
63
  }
37
64
  create(file, stream) {
38
65
  return __awaiter(this, void 0, void 0, function* () {
39
- storage_1.Storage.log(`Uploading ${file.ref} to ${file.bucket}`);
40
- const { data, error } = yield this._client.storage
41
- .from(file.bucket)
42
- .upload(file.ref, yield this.streamToBuffer(stream), {
43
- cacheControl: '3600',
44
- upsert: false,
45
- });
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
+ }
46
81
  return file;
47
82
  });
48
83
  }
49
84
  copy(file, destFile) {
50
85
  return __awaiter(this, void 0, void 0, function* () {
51
- storage_1.Storage.log(`Copying file ${file.ref} to ${destFile.ref} in bucket ${file.bucket}`);
52
86
  try {
53
- const response = yield this._client.storage
87
+ const response = yield this._client
54
88
  .from(file.bucket)
55
89
  .copy(file.ref, destFile.ref, {
56
90
  destinationBucket: destFile.bucket,
@@ -64,21 +98,23 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
64
98
  }
65
99
  move(file, destFile) {
66
100
  return __awaiter(this, void 0, void 0, function* () {
67
- try {
68
- yield this.copy(file, destFile);
69
- yield this.delete(file);
70
- return destFile;
71
- }
72
- catch (err) {
73
- 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}`);
74
108
  }
109
+ return destFile;
75
110
  });
76
111
  }
77
112
  getUrl(file, expiresIn = 3600) {
78
113
  return __awaiter(this, void 0, void 0, function* () {
79
- 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
80
116
  .from(file.bucket)
81
- .createSignedUrl(file.path, expiresIn);
117
+ .createSignedUrl(file.ref, expiresIn, { download: true });
82
118
  if (error !== null) {
83
119
  throw new Error(`Unable to get signed url: ${error}`);
84
120
  }
@@ -87,13 +123,12 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
87
123
  }
88
124
  delete(file) {
89
125
  return __awaiter(this, void 0, void 0, function* () {
90
- const { error } = yield this._client.storage
91
- .from(file.bucket)
92
- .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]);
93
128
  if (error !== null) {
94
129
  throw new Error(`Unable to delete ${file.ref}`);
95
130
  }
96
- storage_1.Storage.log('Object successfully deleted');
131
+ storage_1.Storage.info(`Object ${file.ref} successfully deleted`);
97
132
  return true;
98
133
  });
99
134
  }
@@ -124,7 +159,7 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
124
159
  }
125
160
  download(file, meta) {
126
161
  return __awaiter(this, void 0, void 0, function* () {
127
- const { data, error } = yield this._client.storage
162
+ const { data, error } = yield this._client
128
163
  .from(file.bucket)
129
164
  .download(meta.path);
130
165
  if (error !== null) {
@@ -139,15 +174,15 @@ class SupabaseStorageAdapter extends storage_1.AbstractStorageAdapter {
139
174
  }
140
175
  getUploadUrl(file, _expiresIn = 3600) {
141
176
  return __awaiter(this, void 0, void 0, function* () {
142
- const { data, error } = yield this._client.storage
177
+ const { data, error } = yield this._client
143
178
  .from(file.bucket)
144
179
  .createSignedUploadUrl(file.ref);
145
180
  if (error !== null) {
146
181
  throw new Error(`Unable to get signed upload url: ${error}`);
147
182
  }
148
183
  return {
149
- href: data === null || data === void 0 ? void 0 : data.signedUrl,
150
- type: 'PUT',
184
+ url: data === null || data === void 0 ? void 0 : data.signedUrl,
185
+ method: 'PUT',
151
186
  accept: file.contentType || 'application/octet-stream',
152
187
  expiresIn: 7200, // fixed value in Supabase
153
188
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/storage-supabase",
3
- "version": "1.1.7",
3
+ "version": "1.1.12",
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.7",
26
- "@quatrain/storage": "^1.1.7",
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
  }