@directus/storage-driver-supabase 1.0.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.
@@ -0,0 +1,34 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import type { Driver, Range } from '@directus/storage';
3
+ import { Readable } from 'node:stream';
4
+ export type DriverSupabaseConfig = {
5
+ bucket: string;
6
+ serviceRole: string;
7
+ projectId?: string;
8
+ endpoint?: string;
9
+ root?: string;
10
+ };
11
+ export declare class DriverSupabase implements Driver {
12
+ private config;
13
+ private client;
14
+ private bucket;
15
+ constructor(config: DriverSupabaseConfig);
16
+ private get endpoint();
17
+ private getClient;
18
+ private getBucket;
19
+ private getFullPath;
20
+ private getAuthenticatedUrl;
21
+ read(filepath: string, range?: Range): Promise<Readable>;
22
+ head(filepath: string): Promise<import("undici").Headers>;
23
+ stat(filepath: string): Promise<{
24
+ size: number;
25
+ modified: Date;
26
+ }>;
27
+ exists(filepath: string): Promise<boolean>;
28
+ move(src: string, dest: string): Promise<void>;
29
+ copy(src: string, dest: string): Promise<void>;
30
+ write(filepath: string, content: Readable, type?: string): Promise<void>;
31
+ delete(filepath: string): Promise<void>;
32
+ list(prefix?: string): AsyncGenerator<string, void, unknown>;
33
+ }
34
+ export default DriverSupabase;
package/dist/index.js ADDED
@@ -0,0 +1,118 @@
1
+ import { normalizePath } from '@directus/utils';
2
+ import { StorageClient } from '@supabase/storage-js';
3
+ import { join } from 'node:path';
4
+ import { Readable } from 'node:stream';
5
+ import { fetch } from 'undici';
6
+ export class DriverSupabase {
7
+ config;
8
+ client;
9
+ bucket;
10
+ constructor(config) {
11
+ this.config = config;
12
+ this.client = this.getClient();
13
+ this.bucket = this.getBucket();
14
+ }
15
+ get endpoint() {
16
+ return this.config.endpoint ?? `https://${this.config.projectId}.supabase.co/storage/v1`;
17
+ }
18
+ getClient() {
19
+ if (!this.config.projectId && !this.config.endpoint) {
20
+ throw new Error('`project_id` or `endpoint` is required');
21
+ }
22
+ if (!this.config.serviceRole) {
23
+ throw new Error('`service_role` is required');
24
+ }
25
+ return new StorageClient(this.endpoint, {
26
+ apikey: this.config.serviceRole,
27
+ Authorization: `Bearer ${this.config.serviceRole}`,
28
+ });
29
+ }
30
+ getBucket() {
31
+ if (!this.config.bucket) {
32
+ throw new Error('`bucket` is required');
33
+ }
34
+ return this.client.from(this.config.bucket);
35
+ }
36
+ getFullPath(filepath) {
37
+ return this.config.root ? normalizePath(join(this.config.root, filepath)) : filepath;
38
+ }
39
+ getAuthenticatedUrl(filepath) {
40
+ return `${this.endpoint}/${join('object/authenticated', this.config.bucket, this.getFullPath(filepath))}`;
41
+ }
42
+ async read(filepath, range) {
43
+ const requestInit = { method: 'GET' };
44
+ requestInit.headers = {
45
+ Authorization: `Bearer ${this.config.serviceRole}`,
46
+ };
47
+ if (range) {
48
+ requestInit.headers['Range'] = `bytes=${range.start ?? ''}-${range.end ?? ''}`;
49
+ }
50
+ const response = await fetch(this.getAuthenticatedUrl(filepath), requestInit);
51
+ if (response.status >= 400 || !response.body) {
52
+ throw new Error(`No stream returned for file "${filepath}"`);
53
+ }
54
+ return Readable.fromWeb(response.body);
55
+ }
56
+ async head(filepath) {
57
+ const response = await fetch(this.getAuthenticatedUrl(filepath), {
58
+ method: 'HEAD',
59
+ headers: {
60
+ Authorization: `Bearer ${this.config.serviceRole}`,
61
+ },
62
+ });
63
+ if (response.status >= 400) {
64
+ throw new Error('File not found');
65
+ }
66
+ return response.headers;
67
+ }
68
+ async stat(filepath) {
69
+ const headers = await this.head(filepath);
70
+ return {
71
+ size: parseInt(headers.get('content-length') || ''),
72
+ modified: new Date(headers.get('last-modified') || ''),
73
+ };
74
+ }
75
+ async exists(filepath) {
76
+ try {
77
+ await this.stat(filepath);
78
+ return true;
79
+ }
80
+ catch {
81
+ return false;
82
+ }
83
+ }
84
+ async move(src, dest) {
85
+ await this.bucket.move(this.getFullPath(src), this.getFullPath(dest));
86
+ }
87
+ async copy(src, dest) {
88
+ await this.bucket.copy(this.getFullPath(src), this.getFullPath(dest));
89
+ }
90
+ async write(filepath, content, type) {
91
+ await this.bucket.upload(this.getFullPath(filepath), content, {
92
+ contentType: type ?? '',
93
+ cacheControl: '3600',
94
+ upsert: true,
95
+ duplex: 'half',
96
+ });
97
+ }
98
+ async delete(filepath) {
99
+ await this.bucket.remove([this.getFullPath(filepath)]);
100
+ }
101
+ async *list(prefix = '') {
102
+ const limit = 1000;
103
+ let offset = 0;
104
+ let itemCount = 0;
105
+ do {
106
+ const { data, error } = await this.bucket.list(this.config.root, { limit, offset, search: prefix });
107
+ if (!data || error) {
108
+ break;
109
+ }
110
+ itemCount = data.length;
111
+ offset += itemCount;
112
+ for (const item of data) {
113
+ yield item.name;
114
+ }
115
+ } while (itemCount === limit);
116
+ }
117
+ }
118
+ export default DriverSupabase;
package/license ADDED
@@ -0,0 +1,107 @@
1
+ Licensor: Monospace, Inc.
2
+
3
+ Licensed Work: Directus
4
+ The Licensed Work is Copyright © 2023 Monospace, Inc.
5
+
6
+ Additional Use Grant: You may use the Licensed Work in production as long as
7
+ your Total Finances do not exceed US $5,000,000 for the
8
+ most recent 12-month period, provided that Monospace, Inc.
9
+ will not be liable to you in any way, including for any
10
+ damages, including general, special, incidental or
11
+ consequential damages, arising out of such use.
12
+
13
+ References to: “Total Finances” mean the largest of your
14
+ aggregate gross revenues, entire budget, and/or funding
15
+ (no matter the source); “you” and “your” include (without
16
+ limitation) any individual or entity agreeing to these
17
+ terms and any affiliates of such individual or entity; and
18
+ “production” mean any use other than (i) development of
19
+ (including evaluation of the Licensed Work), debugging, or
20
+ testing your offerings, or (ii) making the Licensed Work
21
+ available standalone in unmodified object code form.
22
+
23
+ Change Date: Three years from release date
24
+
25
+ Change License: GNU General Public License (GPL) v3
26
+
27
+ For information about alternative licensing arrangements, please visit
28
+ https://directus.io/pricing.
29
+
30
+ --------------------------------------------------------------------------------
31
+
32
+ Business Source License 1.1
33
+
34
+ Terms
35
+
36
+ The Licensor hereby grants you the right to copy, modify, create derivative
37
+ works, redistribute, and make non-production use of the Licensed Work. The
38
+ Licensor may make an Additional Use Grant, above, permitting limited production
39
+ use.
40
+
41
+ Effective on the Change Date, or the fourth anniversary of the first publicly
42
+ available distribution of a specific version of the Licensed Work under this
43
+ License, whichever comes first, the Licensor hereby grants you rights under the
44
+ terms of the Change License, and the rights granted in the paragraph above
45
+ terminate.
46
+
47
+ If your use of the Licensed Work does not comply with the requirements currently
48
+ in effect as described in this License, you must purchase a commercial license
49
+ from the Licensor, its affiliated entities, or authorized resellers, or you must
50
+ refrain from using the Licensed Work.
51
+
52
+ All copies of the original and modified Licensed Work, and derivative works of
53
+ the Licensed Work, are subject to this License. This License applies separately
54
+ for each version of the Licensed Work and the Change Date may vary for each
55
+ version of the Licensed Work released by Licensor.
56
+
57
+ You must conspicuously display this License on each original or modified copy of
58
+ the Licensed Work. If you receive the Licensed Work in original or modified form
59
+ from a third party, the terms and conditions set forth in this License apply to
60
+ your use of that work.
61
+
62
+ Any use of the Licensed Work in violation of this License will automatically
63
+ terminate your rights under this License for the current and all other versions
64
+ of the Licensed Work.
65
+
66
+ This License does not grant you any right in any trademark or logo of Licensor
67
+ or its affiliates (provided that you may use a trademark or logo of Licensor as
68
+ expressly required by this License).
69
+
70
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN
71
+ “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS
72
+ OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY,
73
+ FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE.
74
+
75
+ MariaDB hereby grants you permission to use this License’s text to license your
76
+ works, and to refer to it using the trademark “Business Source License”, as long
77
+ as you comply with the Covenants of Licensor below.
78
+
79
+ Covenants of Licensor
80
+
81
+ In consideration of the right to use this License’s text and the “Business
82
+ Source License” name and trademark, Licensor covenants to MariaDB, and to all
83
+ other recipients of the licensed work to be provided by Licensor:
84
+
85
+ 1. To specify as the Change License the GPL Version 2.0 or any later version,
86
+ or a license that is compatible with GPL Version 2.0 or a later version,
87
+ where “compatible” means that software provided under the Change License can
88
+ be included in a program with software provided under GPL Version 2.0 or a
89
+ later version. Licensor may specify additional Change Licenses without
90
+ limitation.
91
+
92
+ 2. To either: (a) specify an additional grant of rights to use that does not
93
+ impose any additional restriction on the right granted in this License, as
94
+ the Additional Use Grant; or (b) insert the text “None”.
95
+
96
+ 3. To specify a Change Date.
97
+
98
+ 4. Not to modify this License in any other way.
99
+
100
+ Notice
101
+
102
+ The Business Source License (this document, or the "License") is not an Open
103
+ Source license. However, the Licensed Work will eventually be made available
104
+ under an Open Source License, as stated in this License.
105
+
106
+ License text copyright © 2023 MariaDB plc, All Rights Reserved.
107
+ “Business Source License” is a trademark of MariaDB plc.
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@directus/storage-driver-supabase",
3
+ "version": "1.0.0",
4
+ "description": "Supabase file storage abstraction for `@directus/storage`",
5
+ "homepage": "https://directus.io",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/directus/directus.git",
9
+ "directory": "packages/storage-driver-supabase"
10
+ },
11
+ "funding": "https://github.com/directus/directus?sponsor=1",
12
+ "license": "BUSL-1.1",
13
+ "author": "Matthew Rollinson <matt@rolley.io>",
14
+ "type": "module",
15
+ "exports": {
16
+ ".": "./dist/index.js",
17
+ "./package.json": "./package.json"
18
+ },
19
+ "main": "dist/index.js",
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "dependencies": {
24
+ "@supabase/storage-js": "2.5.1",
25
+ "undici": "5.22.1",
26
+ "@directus/storage": "10.0.5",
27
+ "@directus/utils": "10.0.8"
28
+ },
29
+ "devDependencies": {
30
+ "@ngneat/falso": "6.4.0",
31
+ "@vitest/coverage-c8": "0.31.1",
32
+ "typescript": "5.0.4",
33
+ "vitest": "0.31.1",
34
+ "@directus/tsconfig": "1.0.0"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc --project tsconfig.prod.json",
38
+ "dev": "tsc --watch",
39
+ "test": "vitest --watch=false"
40
+ }
41
+ }
package/readme.md ADDED
@@ -0,0 +1,3 @@
1
+ # `@directus/storage-driver-supabase`
2
+
3
+ Supabase file storage driver for `@directus/storage`