@edgestore/server 0.0.0-alpha.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.
Files changed (83) hide show
  1. package/README.md +86 -0
  2. package/adapters/next/app/index.d.ts +1 -0
  3. package/adapters/next/app/index.js +1 -0
  4. package/adapters/next/index.d.ts +1 -0
  5. package/adapters/next/index.js +1 -0
  6. package/adapters/next/pages/index.d.ts +1 -0
  7. package/adapters/next/pages/index.js +1 -0
  8. package/core/index.d.ts +1 -0
  9. package/core/index.js +1 -0
  10. package/dist/adapters/imageTypes.d.ts +2 -0
  11. package/dist/adapters/imageTypes.d.ts.map +1 -0
  12. package/dist/adapters/next/app/index.d.ts +14 -0
  13. package/dist/adapters/next/app/index.d.ts.map +1 -0
  14. package/dist/adapters/next/app/index.js +95 -0
  15. package/dist/adapters/next/app/index.mjs +91 -0
  16. package/dist/adapters/next/pages/index.d.ts +15 -0
  17. package/dist/adapters/next/pages/index.d.ts.map +1 -0
  18. package/dist/adapters/next/pages/index.js +69 -0
  19. package/dist/adapters/next/pages/index.mjs +65 -0
  20. package/dist/adapters/shared.d.ts +79 -0
  21. package/dist/adapters/shared.d.ts.map +1 -0
  22. package/dist/core/client/index.d.ts +81 -0
  23. package/dist/core/client/index.d.ts.map +1 -0
  24. package/dist/core/index.d.ts +6 -0
  25. package/dist/core/index.d.ts.map +1 -0
  26. package/dist/core/index.js +96 -0
  27. package/dist/core/index.mjs +91 -0
  28. package/dist/core/internals/bucketBuilder.d.ts +199 -0
  29. package/dist/core/internals/bucketBuilder.d.ts.map +1 -0
  30. package/dist/core/internals/createPathParamProxy.d.ts +21 -0
  31. package/dist/core/internals/createPathParamProxy.d.ts.map +1 -0
  32. package/dist/core/sdk/index.d.ts +200 -0
  33. package/dist/core/sdk/index.d.ts.map +1 -0
  34. package/dist/index-3999aae6.js +187 -0
  35. package/dist/index-3cc4d530.js +183 -0
  36. package/dist/index-ca41982b.mjs +183 -0
  37. package/dist/index.d.ts +2 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +213 -0
  40. package/dist/index.mjs +209 -0
  41. package/dist/libs/errors/EdgeStoreCredentialsError.d.ts +5 -0
  42. package/dist/libs/errors/EdgeStoreCredentialsError.d.ts.map +1 -0
  43. package/dist/libs/errors/EdgeStoreError.d.ts +16 -0
  44. package/dist/libs/errors/EdgeStoreError.d.ts.map +1 -0
  45. package/dist/providers/aws/index.d.ts +9 -0
  46. package/dist/providers/aws/index.d.ts.map +1 -0
  47. package/dist/providers/aws/index.js +81 -0
  48. package/dist/providers/aws/index.mjs +77 -0
  49. package/dist/providers/edgestore/index.d.ts +8 -0
  50. package/dist/providers/edgestore/index.d.ts.map +1 -0
  51. package/dist/providers/edgestore/index.js +121 -0
  52. package/dist/providers/edgestore/index.mjs +117 -0
  53. package/dist/providers/index.d.ts +3 -0
  54. package/dist/providers/index.d.ts.map +1 -0
  55. package/dist/providers/types.d.ts +91 -0
  56. package/dist/providers/types.d.ts.map +1 -0
  57. package/dist/shared-43667670.mjs +232 -0
  58. package/dist/shared-6bef8919.js +227 -0
  59. package/dist/shared-f7607e44.js +239 -0
  60. package/dist/types.d.ts +88 -0
  61. package/dist/types.d.ts.map +1 -0
  62. package/package.json +96 -0
  63. package/providers/aws/index.d.ts +1 -0
  64. package/providers/aws/index.js +1 -0
  65. package/providers/edgestore/index.d.ts +1 -0
  66. package/providers/edgestore/index.js +1 -0
  67. package/src/adapters/imageTypes.ts +10 -0
  68. package/src/adapters/next/app/index.ts +111 -0
  69. package/src/adapters/next/pages/index.ts +84 -0
  70. package/src/adapters/shared.ts +306 -0
  71. package/src/core/client/index.ts +202 -0
  72. package/src/core/index.ts +10 -0
  73. package/src/core/internals/bucketBuilder.ts +462 -0
  74. package/src/core/internals/createPathParamProxy.ts +40 -0
  75. package/src/core/sdk/index.ts +381 -0
  76. package/src/index.ts +1 -0
  77. package/src/libs/errors/EdgeStoreCredentialsError.ts +12 -0
  78. package/src/libs/errors/EdgeStoreError.ts +25 -0
  79. package/src/providers/aws/index.ts +109 -0
  80. package/src/providers/edgestore/index.ts +140 -0
  81. package/src/providers/index.ts +2 -0
  82. package/src/providers/types.ts +107 -0
  83. package/src/types.ts +139 -0
package/dist/index.js ADDED
@@ -0,0 +1,213 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var zod = require('zod');
6
+
7
+ /**
8
+ * Creates a Proxy that prints the path to the property when called.
9
+ *
10
+ * Example:
11
+ *
12
+ * ```ts
13
+ * const pathParamProxy = createPathParamProxy();
14
+ * console.log(pathParamProxy.ctx.user.id());
15
+ * // Logs: "ctx.user.id"
16
+ * console.log(pathParamProxy.input.type());
17
+ * // Logs: "input.type"
18
+ * ```
19
+ */ function createPathParamProxy() {
20
+ const getPath = (target, _prop)=>{
21
+ const proxyFunction = ()=>target;
22
+ return new Proxy(proxyFunction, {
23
+ get: (_target, propChild)=>{
24
+ return getPath(`${target}.${String(propChild)}`);
25
+ }
26
+ });
27
+ };
28
+ return new Proxy(()=>'', {
29
+ get: (_target, prop)=>{
30
+ return getPath(String(prop));
31
+ }
32
+ });
33
+ }
34
+
35
+ const createNewBuilder = (initDef, newDef)=>{
36
+ const mergedDef = {
37
+ ...initDef,
38
+ ...newDef
39
+ };
40
+ return createBuilder({
41
+ type: mergedDef.type
42
+ }, mergedDef);
43
+ };
44
+ function createBuilder(opts, initDef) {
45
+ const _def = {
46
+ type: opts.type,
47
+ input: zod.z.never(),
48
+ path: [],
49
+ ...initDef
50
+ };
51
+ return {
52
+ $config: {
53
+ ctx: undefined
54
+ },
55
+ _def,
56
+ input (input) {
57
+ return createNewBuilder(_def, {
58
+ input
59
+ });
60
+ },
61
+ path (pathResolver) {
62
+ // TODO: Should throw a runtime error in the followin cases:
63
+ // 1. in case of multiple keys in one object
64
+ // 2. in case of duplicate keys
65
+ const pathParamProxy = createPathParamProxy();
66
+ const params = pathResolver(pathParamProxy);
67
+ return createNewBuilder(_def, {
68
+ path: params
69
+ });
70
+ },
71
+ metadata (metadata) {
72
+ return createNewBuilder(_def, {
73
+ metadata
74
+ });
75
+ },
76
+ accessControl (accessControl) {
77
+ return createNewBuilder(_def, {
78
+ accessControl: accessControl
79
+ });
80
+ },
81
+ beforeUpload (beforeUpload) {
82
+ return createNewBuilder(_def, {
83
+ beforeUpload
84
+ });
85
+ },
86
+ beforeDelete (beforeDelete) {
87
+ return createNewBuilder(_def, {
88
+ beforeDelete
89
+ });
90
+ }
91
+ };
92
+ }
93
+ class EdgeStoreBuilder {
94
+ context() {
95
+ return new EdgeStoreBuilder();
96
+ }
97
+ create() {
98
+ return createEdgeStoreInner()();
99
+ }
100
+ }
101
+ function createRouterFactory() {
102
+ return function createRouterInner(buckets) {
103
+ return {
104
+ $config: {
105
+ ctx: undefined
106
+ },
107
+ buckets
108
+ };
109
+ };
110
+ }
111
+ function createEdgeStoreInner() {
112
+ return function initEdgeStoreInner() {
113
+ return {
114
+ /**
115
+ * Builder object for creating an image bucket
116
+ */ imageBucket: createBuilder({
117
+ type: 'IMAGE'
118
+ }),
119
+ /**
120
+ * Builder object for creating a file bucket
121
+ */ fileBucket: createBuilder({
122
+ type: 'FILE'
123
+ }),
124
+ /**
125
+ * Create a router
126
+ */ router: createRouterFactory()
127
+ };
128
+ };
129
+ }
130
+ /**
131
+ * Initialize EdgeStore - be done exactly once per backend
132
+ */ const initEdgeStore = new EdgeStoreBuilder(); // ↓↓↓ TYPE TESTS ↓↓↓
133
+ // type Context = {
134
+ // userId: string;
135
+ // userRole: 'admin' | 'visitor';
136
+ // };
137
+ // const es = initEdgeStore.context<Context>().create();
138
+ // const imagesBucket = es.imageBucket
139
+ // .input(
140
+ // z.object({
141
+ // type: z.enum(['profile', 'post']),
142
+ // extension: z.string().optional(),
143
+ // }),
144
+ // )
145
+ // .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
146
+ // .metadata(({ ctx, input }) => ({
147
+ // extension: input.extension,
148
+ // role: ctx.userRole,
149
+ // }))
150
+ // .beforeUpload(() => {
151
+ // return true;
152
+ // });
153
+ // const a = es.imageBucket
154
+ // .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
155
+ // .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
156
+ // .metadata(({ ctx, input }) => ({
157
+ // role: ctx.userRole,
158
+ // someMeta: input.someMeta,
159
+ // }))
160
+ // .accessControl({
161
+ // OR: [
162
+ // {
163
+ // userId: { path: 'author' }, // this will check if the userId is the same as the author in the path parameter
164
+ // },
165
+ // {
166
+ // userRole: 'admin', // this is the same as { userRole: { eq: "admin" } }
167
+ // },
168
+ // ],
169
+ // })
170
+ // .beforeUpload(({ ctx, input }) => {
171
+ // return true;
172
+ // })
173
+ // .beforeDelete(({ ctx, file }) => {
174
+ // return true;
175
+ // });
176
+ // const b = es.imageBucket.path(({ ctx }) => [{ author: ctx.userId }]);
177
+ // const router = es.router({
178
+ // original: imagesBucket,
179
+ // imageBucket: a,
180
+ // imageBucket2: b,
181
+ // });
182
+ // export { router };
183
+ // type ListFilesResponse<TBucket extends AnyRouter['buckets'][string]> = {
184
+ // data: {
185
+ // // url: string;
186
+ // // size: number;
187
+ // // uploadedAt: Date;
188
+ // // metadata: InferMetadataObject<TBucket>;
189
+ // path: InferBucketPathKeys<TBucket> extends string ? {
190
+ // [key: string]: string;
191
+ // } :{
192
+ // [TKey in InferBucketPathKeys<TBucket>]: string;
193
+ // };
194
+ // }[];
195
+ // pagination: {
196
+ // currentPage: number;
197
+ // totalPages: number;
198
+ // totalCount: number;
199
+ // };
200
+ // };
201
+ // type TPathKeys = 'author' | 'type';
202
+ // type TPathKeys2 = InferBucketPathKeys<AnyBuilder>;
203
+ // type ObjectWithKeys<TKeys extends string> = {
204
+ // [TKey in TKeys]: string;
205
+ // };
206
+ // type Test1 = ObjectWithKeys<TPathKeys>;
207
+ // type Test2 = ObjectWithKeys<TPathKeys2>;
208
+ // type PathKeys = InferBucketPathKeys<typeof router.buckets.imageBucket>;
209
+ // type MetadataKeys = InferMetadataObject<typeof router.buckets.imageBucket>;
210
+ // type MyEdgeStoreRouter = typeof router;
211
+ // type MyAccessControl = AccessControlSchema<Context, AnyDef>;
212
+
213
+ exports.initEdgeStore = initEdgeStore;
package/dist/index.mjs ADDED
@@ -0,0 +1,209 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Creates a Proxy that prints the path to the property when called.
5
+ *
6
+ * Example:
7
+ *
8
+ * ```ts
9
+ * const pathParamProxy = createPathParamProxy();
10
+ * console.log(pathParamProxy.ctx.user.id());
11
+ * // Logs: "ctx.user.id"
12
+ * console.log(pathParamProxy.input.type());
13
+ * // Logs: "input.type"
14
+ * ```
15
+ */ function createPathParamProxy() {
16
+ const getPath = (target, _prop)=>{
17
+ const proxyFunction = ()=>target;
18
+ return new Proxy(proxyFunction, {
19
+ get: (_target, propChild)=>{
20
+ return getPath(`${target}.${String(propChild)}`);
21
+ }
22
+ });
23
+ };
24
+ return new Proxy(()=>'', {
25
+ get: (_target, prop)=>{
26
+ return getPath(String(prop));
27
+ }
28
+ });
29
+ }
30
+
31
+ const createNewBuilder = (initDef, newDef)=>{
32
+ const mergedDef = {
33
+ ...initDef,
34
+ ...newDef
35
+ };
36
+ return createBuilder({
37
+ type: mergedDef.type
38
+ }, mergedDef);
39
+ };
40
+ function createBuilder(opts, initDef) {
41
+ const _def = {
42
+ type: opts.type,
43
+ input: z.never(),
44
+ path: [],
45
+ ...initDef
46
+ };
47
+ return {
48
+ $config: {
49
+ ctx: undefined
50
+ },
51
+ _def,
52
+ input (input) {
53
+ return createNewBuilder(_def, {
54
+ input
55
+ });
56
+ },
57
+ path (pathResolver) {
58
+ // TODO: Should throw a runtime error in the followin cases:
59
+ // 1. in case of multiple keys in one object
60
+ // 2. in case of duplicate keys
61
+ const pathParamProxy = createPathParamProxy();
62
+ const params = pathResolver(pathParamProxy);
63
+ return createNewBuilder(_def, {
64
+ path: params
65
+ });
66
+ },
67
+ metadata (metadata) {
68
+ return createNewBuilder(_def, {
69
+ metadata
70
+ });
71
+ },
72
+ accessControl (accessControl) {
73
+ return createNewBuilder(_def, {
74
+ accessControl: accessControl
75
+ });
76
+ },
77
+ beforeUpload (beforeUpload) {
78
+ return createNewBuilder(_def, {
79
+ beforeUpload
80
+ });
81
+ },
82
+ beforeDelete (beforeDelete) {
83
+ return createNewBuilder(_def, {
84
+ beforeDelete
85
+ });
86
+ }
87
+ };
88
+ }
89
+ class EdgeStoreBuilder {
90
+ context() {
91
+ return new EdgeStoreBuilder();
92
+ }
93
+ create() {
94
+ return createEdgeStoreInner()();
95
+ }
96
+ }
97
+ function createRouterFactory() {
98
+ return function createRouterInner(buckets) {
99
+ return {
100
+ $config: {
101
+ ctx: undefined
102
+ },
103
+ buckets
104
+ };
105
+ };
106
+ }
107
+ function createEdgeStoreInner() {
108
+ return function initEdgeStoreInner() {
109
+ return {
110
+ /**
111
+ * Builder object for creating an image bucket
112
+ */ imageBucket: createBuilder({
113
+ type: 'IMAGE'
114
+ }),
115
+ /**
116
+ * Builder object for creating a file bucket
117
+ */ fileBucket: createBuilder({
118
+ type: 'FILE'
119
+ }),
120
+ /**
121
+ * Create a router
122
+ */ router: createRouterFactory()
123
+ };
124
+ };
125
+ }
126
+ /**
127
+ * Initialize EdgeStore - be done exactly once per backend
128
+ */ const initEdgeStore = new EdgeStoreBuilder(); // ↓↓↓ TYPE TESTS ↓↓↓
129
+ // type Context = {
130
+ // userId: string;
131
+ // userRole: 'admin' | 'visitor';
132
+ // };
133
+ // const es = initEdgeStore.context<Context>().create();
134
+ // const imagesBucket = es.imageBucket
135
+ // .input(
136
+ // z.object({
137
+ // type: z.enum(['profile', 'post']),
138
+ // extension: z.string().optional(),
139
+ // }),
140
+ // )
141
+ // .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
142
+ // .metadata(({ ctx, input }) => ({
143
+ // extension: input.extension,
144
+ // role: ctx.userRole,
145
+ // }))
146
+ // .beforeUpload(() => {
147
+ // return true;
148
+ // });
149
+ // const a = es.imageBucket
150
+ // .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
151
+ // .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
152
+ // .metadata(({ ctx, input }) => ({
153
+ // role: ctx.userRole,
154
+ // someMeta: input.someMeta,
155
+ // }))
156
+ // .accessControl({
157
+ // OR: [
158
+ // {
159
+ // userId: { path: 'author' }, // this will check if the userId is the same as the author in the path parameter
160
+ // },
161
+ // {
162
+ // userRole: 'admin', // this is the same as { userRole: { eq: "admin" } }
163
+ // },
164
+ // ],
165
+ // })
166
+ // .beforeUpload(({ ctx, input }) => {
167
+ // return true;
168
+ // })
169
+ // .beforeDelete(({ ctx, file }) => {
170
+ // return true;
171
+ // });
172
+ // const b = es.imageBucket.path(({ ctx }) => [{ author: ctx.userId }]);
173
+ // const router = es.router({
174
+ // original: imagesBucket,
175
+ // imageBucket: a,
176
+ // imageBucket2: b,
177
+ // });
178
+ // export { router };
179
+ // type ListFilesResponse<TBucket extends AnyRouter['buckets'][string]> = {
180
+ // data: {
181
+ // // url: string;
182
+ // // size: number;
183
+ // // uploadedAt: Date;
184
+ // // metadata: InferMetadataObject<TBucket>;
185
+ // path: InferBucketPathKeys<TBucket> extends string ? {
186
+ // [key: string]: string;
187
+ // } :{
188
+ // [TKey in InferBucketPathKeys<TBucket>]: string;
189
+ // };
190
+ // }[];
191
+ // pagination: {
192
+ // currentPage: number;
193
+ // totalPages: number;
194
+ // totalCount: number;
195
+ // };
196
+ // };
197
+ // type TPathKeys = 'author' | 'type';
198
+ // type TPathKeys2 = InferBucketPathKeys<AnyBuilder>;
199
+ // type ObjectWithKeys<TKeys extends string> = {
200
+ // [TKey in TKeys]: string;
201
+ // };
202
+ // type Test1 = ObjectWithKeys<TPathKeys>;
203
+ // type Test2 = ObjectWithKeys<TPathKeys2>;
204
+ // type PathKeys = InferBucketPathKeys<typeof router.buckets.imageBucket>;
205
+ // type MetadataKeys = InferMetadataObject<typeof router.buckets.imageBucket>;
206
+ // type MyEdgeStoreRouter = typeof router;
207
+ // type MyAccessControl = AccessControlSchema<Context, AnyDef>;
208
+
209
+ export { initEdgeStore };
@@ -0,0 +1,5 @@
1
+ declare class EdgeStoreCredentialsError extends Error {
2
+ constructor(message?: string);
3
+ }
4
+ export default EdgeStoreCredentialsError;
5
+ //# sourceMappingURL=EdgeStoreCredentialsError.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EdgeStoreCredentialsError.d.ts","sourceRoot":"","sources":["../../../src/libs/errors/EdgeStoreCredentialsError.ts"],"names":[],"mappings":"AAIA,cAAM,yBAA0B,SAAQ,KAAK;gBAC/B,OAAO,SAAkB;CAItC;AAED,eAAe,yBAAyB,CAAC"}
@@ -0,0 +1,16 @@
1
+ export declare const EDGE_STORE_ERROR_CODES: {
2
+ readonly BAD_REQUEST: 400;
3
+ readonly UNAUTHORIZED: 401;
4
+ };
5
+ export type EdgeStoreErrorCodeKey = keyof typeof EDGE_STORE_ERROR_CODES;
6
+ declare class EdgeStoreError extends Error {
7
+ readonly cause?: Error;
8
+ readonly code: EdgeStoreErrorCodeKey;
9
+ constructor(opts: {
10
+ message: string;
11
+ code: EdgeStoreErrorCodeKey;
12
+ cause?: Error;
13
+ });
14
+ }
15
+ export default EdgeStoreError;
16
+ //# sourceMappingURL=EdgeStoreError.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EdgeStoreError.d.ts","sourceRoot":"","sources":["../../../src/libs/errors/EdgeStoreError.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,sBAAsB;;;CAGzB,CAAC;AAEX,MAAM,MAAM,qBAAqB,GAAG,MAAM,OAAO,sBAAsB,CAAC;AAExE,cAAM,cAAe,SAAQ,KAAK;IAChC,SAAgB,KAAK,CAAC,EAAE,KAAK,CAAC;IAC9B,SAAgB,IAAI,EAAE,qBAAqB,CAAC;gBAEhC,IAAI,EAAE;QAChB,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,EAAE,qBAAqB,CAAC;QAC5B,KAAK,CAAC,EAAE,KAAK,CAAC;KACf;CAOF;AAED,eAAe,cAAc,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { Provider } from '../types';
2
+ export type AWSProviderOptions = {
3
+ accessKeyId?: string;
4
+ secretAccessKey?: string;
5
+ region?: string;
6
+ bucketName?: string;
7
+ };
8
+ export declare function AWSProvider(options?: AWSProviderOptions): Provider;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/aws/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,QAAQ,CA2FlE"}
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var clientS3 = require('@aws-sdk/client-s3');
6
+ var s3RequestPresigner = require('@aws-sdk/s3-request-presigner');
7
+ var uuid = require('uuid');
8
+
9
+ function AWSProvider(options) {
10
+ const { accessKeyId = process.env.ES_AWS_ACCESS_KEY_ID, secretAccessKey = process.env.ES_AWS_SECRET_ACCESS_KEY, region = process.env.ES_AWS_REGION, bucketName = process.env.ES_AWS_BUCKET_NAME } = options ?? {};
11
+ const credentials = accessKeyId && secretAccessKey ? {
12
+ accessKeyId,
13
+ secretAccessKey
14
+ } : undefined;
15
+ const s3Client = new clientS3.S3Client({
16
+ region,
17
+ credentials
18
+ });
19
+ const baseUrl = process.env.EDGE_STORE_BASE_URL ?? `https://${bucketName}.s3.${region}.amazonaws.com`;
20
+ return {
21
+ async init () {
22
+ return {};
23
+ },
24
+ getBaseUrl () {
25
+ return baseUrl;
26
+ },
27
+ async getFile ({ url }) {
28
+ const path = url.replace(`${baseUrl}/`, '');
29
+ const { ContentLength, LastModified } = await s3Client.send(new clientS3.HeadObjectCommand({
30
+ Bucket: bucketName,
31
+ Key: path
32
+ }));
33
+ if (!ContentLength || !LastModified) {
34
+ throw new Error('File not found');
35
+ }
36
+ return {
37
+ url,
38
+ metadata: {},
39
+ path: {},
40
+ size: ContentLength,
41
+ uploadedAt: LastModified
42
+ };
43
+ },
44
+ async requestUpload ({ bucketName, fileInfo }) {
45
+ const pathPrefix = `${bucketName}${fileInfo.isPublic ? '/_public' : ''}`;
46
+ const nameId = uuid.v4();
47
+ const extension = fileInfo.extension ? `.${fileInfo.extension.replace('.', '')}` : '';
48
+ const fileName = `${nameId}${extension}`;
49
+ const filePath = fileInfo.path.reduce((acc, item)=>{
50
+ return `${acc}/${item.value}`;
51
+ }, '');
52
+ const command = new clientS3.PutObjectCommand({
53
+ Bucket: bucketName,
54
+ Key: `${pathPrefix}/${filePath}/${fileName}`
55
+ });
56
+ const signedUrl = await s3RequestPresigner.getSignedUrl(s3Client, command, {
57
+ expiresIn: 60 * 60
58
+ });
59
+ const url = `${baseUrl}/${fileInfo.path}`;
60
+ return {
61
+ uploadUrl: signedUrl,
62
+ accessUrl: url
63
+ };
64
+ },
65
+ async requestUploadParts () {
66
+ throw new Error('Not implemented');
67
+ },
68
+ async deleteFile ({ url }) {
69
+ const path = url.replace(`${baseUrl}/`, '');
70
+ await s3Client.send(new clientS3.DeleteObjectCommand({
71
+ Bucket: bucketName,
72
+ Key: path
73
+ }));
74
+ return {
75
+ success: true
76
+ };
77
+ }
78
+ };
79
+ }
80
+
81
+ exports.AWSProvider = AWSProvider;
@@ -0,0 +1,77 @@
1
+ import { S3Client, HeadObjectCommand, PutObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
2
+ import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
3
+ import { v4 } from 'uuid';
4
+
5
+ function AWSProvider(options) {
6
+ const { accessKeyId = process.env.ES_AWS_ACCESS_KEY_ID, secretAccessKey = process.env.ES_AWS_SECRET_ACCESS_KEY, region = process.env.ES_AWS_REGION, bucketName = process.env.ES_AWS_BUCKET_NAME } = options ?? {};
7
+ const credentials = accessKeyId && secretAccessKey ? {
8
+ accessKeyId,
9
+ secretAccessKey
10
+ } : undefined;
11
+ const s3Client = new S3Client({
12
+ region,
13
+ credentials
14
+ });
15
+ const baseUrl = process.env.EDGE_STORE_BASE_URL ?? `https://${bucketName}.s3.${region}.amazonaws.com`;
16
+ return {
17
+ async init () {
18
+ return {};
19
+ },
20
+ getBaseUrl () {
21
+ return baseUrl;
22
+ },
23
+ async getFile ({ url }) {
24
+ const path = url.replace(`${baseUrl}/`, '');
25
+ const { ContentLength, LastModified } = await s3Client.send(new HeadObjectCommand({
26
+ Bucket: bucketName,
27
+ Key: path
28
+ }));
29
+ if (!ContentLength || !LastModified) {
30
+ throw new Error('File not found');
31
+ }
32
+ return {
33
+ url,
34
+ metadata: {},
35
+ path: {},
36
+ size: ContentLength,
37
+ uploadedAt: LastModified
38
+ };
39
+ },
40
+ async requestUpload ({ bucketName, fileInfo }) {
41
+ const pathPrefix = `${bucketName}${fileInfo.isPublic ? '/_public' : ''}`;
42
+ const nameId = v4();
43
+ const extension = fileInfo.extension ? `.${fileInfo.extension.replace('.', '')}` : '';
44
+ const fileName = `${nameId}${extension}`;
45
+ const filePath = fileInfo.path.reduce((acc, item)=>{
46
+ return `${acc}/${item.value}`;
47
+ }, '');
48
+ const command = new PutObjectCommand({
49
+ Bucket: bucketName,
50
+ Key: `${pathPrefix}/${filePath}/${fileName}`
51
+ });
52
+ const signedUrl = await getSignedUrl(s3Client, command, {
53
+ expiresIn: 60 * 60
54
+ });
55
+ const url = `${baseUrl}/${fileInfo.path}`;
56
+ return {
57
+ uploadUrl: signedUrl,
58
+ accessUrl: url
59
+ };
60
+ },
61
+ async requestUploadParts () {
62
+ throw new Error('Not implemented');
63
+ },
64
+ async deleteFile ({ url }) {
65
+ const path = url.replace(`${baseUrl}/`, '');
66
+ await s3Client.send(new DeleteObjectCommand({
67
+ Bucket: bucketName,
68
+ Key: path
69
+ }));
70
+ return {
71
+ success: true
72
+ };
73
+ }
74
+ };
75
+ }
76
+
77
+ export { AWSProvider };
@@ -0,0 +1,8 @@
1
+ import { Provider } from '../types';
2
+ export type EdgeStoreProviderOptions = {
3
+ accessKey?: string;
4
+ secretKey?: string;
5
+ baseUrl?: string;
6
+ };
7
+ export declare function EdgeStoreProvider(options?: EdgeStoreProviderOptions): Provider;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/edgestore/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAoB,MAAM,UAAU,CAAC;AAKtD,MAAM,MAAM,wBAAwB,GAAG;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,OAAO,CAAC,EAAE,wBAAwB,GACjC,QAAQ,CA4HV"}