@edgestore/server 1.0.0-next.0 → 1.0.0-next.2
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.
- package/README.md +11 -5
- package/dist/adapters/astro/index.d.ts +7 -12
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +14 -161
- package/dist/adapters/dispatcher.d.ts +30 -0
- package/dist/adapters/dispatcher.d.ts.map +1 -0
- package/dist/adapters/dispatcher.js +172 -0
- package/dist/adapters/express/index.d.ts +7 -12
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +24 -94
- package/dist/adapters/fastify/index.d.ts +7 -12
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +23 -121
- package/dist/adapters/hono/index.d.ts +7 -12
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +14 -121
- package/dist/adapters/next/app/index.d.ts +7 -12
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +21 -148
- package/dist/adapters/next/pages/index.d.ts +7 -12
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +34 -95
- package/dist/adapters/remix/index.d.ts +7 -12
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +14 -136
- package/dist/adapters/shared.d.ts +140 -55
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +188 -179
- package/dist/adapters/start/index.d.ts +7 -12
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +14 -169
- package/dist/core/client/bucketClient.d.ts +7 -0
- package/dist/core/client/bucketClient.d.ts.map +1 -0
- package/dist/core/client/bucketClient.js +312 -0
- package/dist/core/client/index.d.ts +120 -142
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +13 -218
- package/dist/core/index.d.ts +20 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +14 -2
- package/dist/core/provider.d.ts +15 -0
- package/dist/core/provider.d.ts.map +1 -0
- package/dist/core/provider.js +25 -0
- package/dist/core/routerRules.d.ts +20 -0
- package/dist/core/routerRules.d.ts.map +1 -0
- package/dist/core/routerRules.js +42 -0
- package/dist/core/validateFile.d.ts +9 -0
- package/dist/core/validateFile.d.ts.map +1 -0
- package/dist/core/validateFile.js +43 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/libs/env.d.ts +3 -0
- package/dist/libs/env.d.ts.map +1 -0
- package/dist/libs/env.js +14 -0
- package/dist/libs/logger.d.ts +2 -1
- package/dist/libs/logger.d.ts.map +1 -1
- package/dist/libs/logger.js +1 -1
- package/dist/providers/azure-blob/index.d.ts +119 -0
- package/dist/providers/azure-blob/index.d.ts.map +1 -0
- package/dist/providers/azure-blob/index.js +166 -0
- package/dist/providers/edgestore/index.d.ts +171 -3
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +304 -136
- package/dist/providers/{aws → s3}/index.d.ts +68 -11
- package/dist/providers/s3/index.d.ts.map +1 -0
- package/dist/providers/s3/index.js +146 -0
- package/dist/test-utils/edgestoreSmoke.d.ts +0 -2
- package/dist/test-utils/edgestoreSmoke.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/package.json +17 -20
- package/providers/azure-blob/index.d.ts +1 -0
- package/providers/azure-blob/index.js +1 -0
- package/providers/s3/index.d.ts +1 -0
- package/providers/s3/index.js +1 -0
- package/src/adapters/astro/index.ts +27 -211
- package/src/adapters/dispatcher.ts +261 -0
- package/src/adapters/express/index.ts +43 -149
- package/src/adapters/fastify/index.ts +43 -191
- package/src/adapters/hono/index.ts +28 -183
- package/src/adapters/next/app/index.ts +37 -201
- package/src/adapters/next/pages/index.ts +55 -156
- package/src/adapters/remix/index.ts +27 -190
- package/src/adapters/shared.ts +237 -261
- package/src/adapters/start/index.ts +28 -200
- package/src/core/client/bucketClient.ts +476 -0
- package/src/core/client/index.ts +301 -428
- package/src/core/index.ts +42 -1
- package/src/core/provider.ts +80 -0
- package/src/core/routerRules.ts +63 -0
- package/src/core/validateFile.ts +50 -0
- package/src/index.ts +10 -0
- package/src/libs/env.ts +16 -0
- package/src/libs/logger.ts +4 -2
- package/src/providers/azure-blob/index.ts +265 -0
- package/src/providers/edgestore/index.ts +426 -154
- package/src/providers/s3/index.ts +289 -0
- package/src/test-utils/edgestoreSmoke.ts +0 -11
- package/src/version.ts +1 -1
- package/dist/core/sdk/index.d.ts +0 -282
- package/dist/core/sdk/index.d.ts.map +0 -1
- package/dist/core/sdk/index.js +0 -253
- package/dist/providers/aws/index.d.ts.map +0 -1
- package/dist/providers/aws/index.js +0 -102
- package/dist/providers/azure/index.d.ts +0 -39
- package/dist/providers/azure/index.d.ts.map +0 -1
- package/dist/providers/azure/index.js +0 -84
- package/dist/version.js +0 -4
- package/providers/aws/index.d.ts +0 -1
- package/providers/aws/index.js +0 -1
- package/providers/azure/index.d.ts +0 -1
- package/providers/azure/index.js +0 -1
- package/src/core/sdk/index.ts +0 -542
- package/src/providers/aws/index.ts +0 -239
- package/src/providers/azure/index.ts +0 -141
|
@@ -1,15 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createEdgeStoreSdk,
|
|
3
|
+
planMultipartUpload,
|
|
4
|
+
type ProjectRuntimeClient,
|
|
5
|
+
} from '@edgestore/sdk';
|
|
1
6
|
import {
|
|
2
7
|
EdgeStoreError,
|
|
3
|
-
type
|
|
8
|
+
type ProviderFile,
|
|
9
|
+
type ProviderFileMutationResult,
|
|
10
|
+
type RequestUploadParams,
|
|
4
11
|
type RequestUploadRes,
|
|
5
12
|
} from '@edgestore/shared';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
13
|
+
import { z } from 'zod';
|
|
14
|
+
import { defineProvider } from '../../core/provider';
|
|
15
|
+
import { getEnv } from '../../libs/env';
|
|
8
16
|
import EdgeStoreCredentialsError from '../../libs/errors/EdgeStoreCredentialsError';
|
|
9
17
|
|
|
10
18
|
const DEFAULT_BASE_URL = 'https://files.edgestore.dev';
|
|
11
19
|
|
|
12
|
-
|
|
20
|
+
const fileReferenceSchema = z
|
|
21
|
+
.union([
|
|
22
|
+
z.string(),
|
|
23
|
+
z.object({ id: z.string() }),
|
|
24
|
+
z.object({ key: z.string() }),
|
|
25
|
+
z.object({ url: z.string() }),
|
|
26
|
+
])
|
|
27
|
+
.transform((reference) =>
|
|
28
|
+
typeof reference === 'string' ? { url: reference } : reference,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
type EdgeStoreProviderCommonOptions = {
|
|
32
|
+
/** Override the complete API v2 URL. */
|
|
33
|
+
apiUrl?: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type EdgeStoreProjectProviderOptions = {
|
|
13
37
|
/**
|
|
14
38
|
* Access key for your EdgeStore project.
|
|
15
39
|
* Can be found in the EdgeStore dashboard.
|
|
@@ -24,186 +48,434 @@ export type EdgeStoreProviderOptions = {
|
|
|
24
48
|
* This can be omitted if the `EDGE_STORE_SECRET_KEY` environment variable is set.
|
|
25
49
|
*/
|
|
26
50
|
secretKey?: string;
|
|
51
|
+
token?: never;
|
|
52
|
+
project?: never;
|
|
27
53
|
};
|
|
28
54
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
38
|
-
import.meta.env?.EDGE_STORE_SECRET_KEY,
|
|
39
|
-
} = options ?? {};
|
|
55
|
+
type EdgeStoreBearerProviderOptions = {
|
|
56
|
+
/** Bearer token used to authenticate API v2 requests. */
|
|
57
|
+
token: string;
|
|
58
|
+
/** Project ID or slug used by runtime operations. */
|
|
59
|
+
project: string;
|
|
60
|
+
accessKey?: never;
|
|
61
|
+
secretKey?: never;
|
|
62
|
+
};
|
|
40
63
|
|
|
41
|
-
|
|
64
|
+
export type EdgeStoreProviderOptions = EdgeStoreProviderCommonOptions &
|
|
65
|
+
(EdgeStoreProjectProviderOptions | EdgeStoreBearerProviderOptions);
|
|
42
66
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
67
|
+
export function edgestore(options?: EdgeStoreProviderOptions) {
|
|
68
|
+
const baseUrl = getEnv('EDGE_STORE_BASE_URL') ?? DEFAULT_BASE_URL;
|
|
69
|
+
const runtime = createProviderRuntime(options);
|
|
46
70
|
|
|
47
|
-
const
|
|
48
|
-
accessKey,
|
|
49
|
-
secretKey,
|
|
50
|
-
});
|
|
51
|
-
return {
|
|
71
|
+
const provider = defineProvider({
|
|
52
72
|
name: 'edgestore',
|
|
73
|
+
baseUrl,
|
|
74
|
+
reference: {
|
|
75
|
+
schema: fileReferenceSchema,
|
|
76
|
+
fromUrl: (url) => ({ url }),
|
|
77
|
+
},
|
|
53
78
|
init: async ({ ctx, router }) => {
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
79
|
+
const requiresFileAccessCookie = Object.values(router.buckets).some(
|
|
80
|
+
(bucket) =>
|
|
81
|
+
bucket._def.accessControl !== undefined &&
|
|
82
|
+
bucket._def.accessControl !== 'private',
|
|
83
|
+
);
|
|
84
|
+
if (!requiresFileAccessCookie) return {};
|
|
85
|
+
|
|
86
|
+
const { token } = await runtime.accessTokens.create({
|
|
87
|
+
context: Object.fromEntries(
|
|
88
|
+
Object.entries(ctx).filter(
|
|
89
|
+
(entry): entry is [string, string] => entry[1] !== undefined,
|
|
90
|
+
),
|
|
91
|
+
),
|
|
92
|
+
buckets: Object.fromEntries(
|
|
93
|
+
Object.entries(router.buckets).map(([bucketName, bucket]) => [
|
|
94
|
+
bucketName,
|
|
95
|
+
{
|
|
96
|
+
path: bucket._def.path.map((part) => {
|
|
97
|
+
const entry = Object.entries(part)[0];
|
|
98
|
+
if (!entry) {
|
|
99
|
+
throw new EdgeStoreError({
|
|
100
|
+
message: 'Bucket paths cannot contain empty segments.',
|
|
101
|
+
code: 'SERVER_ERROR',
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const [key, value] = entry;
|
|
105
|
+
return { key, value: value() };
|
|
106
|
+
}),
|
|
107
|
+
accessControl: bucket._def.accessControl,
|
|
108
|
+
},
|
|
109
|
+
]),
|
|
110
|
+
),
|
|
57
111
|
});
|
|
58
112
|
return {
|
|
59
113
|
token,
|
|
114
|
+
clientInit: {
|
|
115
|
+
path: '/_init',
|
|
116
|
+
headers: {
|
|
117
|
+
'x-edgestore-token': token,
|
|
118
|
+
},
|
|
119
|
+
},
|
|
60
120
|
};
|
|
61
121
|
},
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
...rest,
|
|
72
|
-
};
|
|
73
|
-
},
|
|
74
|
-
async requestUpload({
|
|
75
|
-
bucketName,
|
|
76
|
-
bucketType,
|
|
77
|
-
fileInfo,
|
|
78
|
-
autoSignedUrls,
|
|
79
|
-
}): Promise<RequestUploadRes> {
|
|
80
|
-
// multipart upload if file is bigger than a certain size
|
|
81
|
-
const MULTIPART_THRESHOLD = 10 * 1024 * 1024; // 10MB
|
|
82
|
-
let partSize = 5 * 1024 * 1024; // 5MB
|
|
83
|
-
if (fileInfo.size > MULTIPART_THRESHOLD) {
|
|
84
|
-
let totalParts = Math.ceil(fileInfo.size / partSize);
|
|
85
|
-
if (totalParts > 1000) {
|
|
86
|
-
// the maximum number of parts is 1000
|
|
87
|
-
totalParts = 1000;
|
|
88
|
-
partSize = Math.ceil(fileInfo.size / totalParts);
|
|
89
|
-
}
|
|
90
|
-
const res = await edgeStoreSdk.requestUpload({
|
|
91
|
-
bucketName,
|
|
92
|
-
bucketType,
|
|
93
|
-
fileInfo,
|
|
94
|
-
autoSignedUrls,
|
|
95
|
-
multipart: {
|
|
96
|
-
parts: Array.from({ length: totalParts }).map(
|
|
97
|
-
(_, index) => index + 1,
|
|
98
|
-
),
|
|
99
|
-
},
|
|
122
|
+
uploads: {
|
|
123
|
+
async request({
|
|
124
|
+
bucketName,
|
|
125
|
+
bucketType,
|
|
126
|
+
fileInfo,
|
|
127
|
+
autoSignedUrls,
|
|
128
|
+
}): Promise<RequestUploadRes> {
|
|
129
|
+
const multipartPlan = planMultipartUpload({
|
|
130
|
+
sizeBytes: fileInfo.size,
|
|
100
131
|
});
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
132
|
+
if (multipartPlan) {
|
|
133
|
+
return mapUploadResponse(
|
|
134
|
+
await runtime.uploads.request({
|
|
135
|
+
bucket: bucketName,
|
|
136
|
+
...mapRawUploadRequest(bucketType, fileInfo, autoSignedUrls),
|
|
137
|
+
multipart: {
|
|
138
|
+
partNumbers: multipartPlan.partNumbers,
|
|
139
|
+
},
|
|
140
|
+
}),
|
|
141
|
+
{
|
|
142
|
+
partSize: multipartPlan.partSizeBytes,
|
|
143
|
+
totalParts: multipartPlan.totalParts,
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return mapUploadResponse(
|
|
148
|
+
await runtime.uploads.request({
|
|
149
|
+
bucket: bucketName,
|
|
150
|
+
...mapRawUploadRequest(bucketType, fileInfo, autoSignedUrls),
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
},
|
|
154
|
+
multipart: {
|
|
155
|
+
requestParts: async ({ multipart }) => {
|
|
156
|
+
const res = await runtime.uploads.createParts({
|
|
157
|
+
uploadId: multipart.uploadId,
|
|
158
|
+
partNumbers: multipart.parts,
|
|
159
|
+
});
|
|
160
|
+
return {
|
|
161
|
+
multipart: {
|
|
162
|
+
uploadId: multipart.uploadId,
|
|
163
|
+
parts: res.parts.map((part) => ({
|
|
106
164
|
partNumber: part.partNumber,
|
|
107
165
|
uploadUrl: part.signedUrl,
|
|
108
166
|
})),
|
|
109
|
-
|
|
110
|
-
totalParts,
|
|
111
|
-
}
|
|
112
|
-
: undefined;
|
|
113
|
-
if (multipart) {
|
|
114
|
-
return {
|
|
115
|
-
accessUrl: res.accessUrl,
|
|
116
|
-
thumbnailUrl: res.thumbnailUrl,
|
|
117
|
-
...mapSignedUploadAccess(res),
|
|
118
|
-
multipart,
|
|
167
|
+
},
|
|
119
168
|
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
...mapSignedUploadAccess(res),
|
|
126
|
-
};
|
|
127
|
-
} else {
|
|
128
|
-
throw new EdgeStoreError({
|
|
129
|
-
message: 'Could not get upload url',
|
|
130
|
-
code: 'SERVER_ERROR',
|
|
169
|
+
},
|
|
170
|
+
complete: async ({ uploadId, parts }) => {
|
|
171
|
+
await runtime.uploads.completeMultipart({
|
|
172
|
+
uploadId,
|
|
173
|
+
parts,
|
|
131
174
|
});
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
upload: async ({
|
|
135
178
|
bucketName,
|
|
136
|
-
bucketType,
|
|
137
179
|
fileInfo,
|
|
138
180
|
autoSignedUrls,
|
|
139
|
-
|
|
140
|
-
|
|
181
|
+
source,
|
|
182
|
+
signal,
|
|
183
|
+
onProgress,
|
|
184
|
+
}) => {
|
|
185
|
+
const result = await runtime.uploads.upload({
|
|
186
|
+
bucket: bucketName,
|
|
187
|
+
source,
|
|
188
|
+
...mapHighLevelUploadOptions(fileInfo, autoSignedUrls),
|
|
189
|
+
signal,
|
|
190
|
+
onProgress,
|
|
191
|
+
});
|
|
141
192
|
return {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
193
|
+
file: mapFile(result.file),
|
|
194
|
+
signedReadUrl: result.signedReadUrl
|
|
195
|
+
? {
|
|
196
|
+
...result.signedReadUrl,
|
|
197
|
+
expiresAt: new Date(result.signedReadUrl.expiresAt),
|
|
198
|
+
}
|
|
199
|
+
: undefined,
|
|
146
200
|
};
|
|
147
|
-
}
|
|
148
|
-
throw new EdgeStoreError({
|
|
149
|
-
message: 'Could not get upload url',
|
|
150
|
-
code: 'SERVER_ERROR',
|
|
151
|
-
});
|
|
152
|
-
},
|
|
153
|
-
requestUploadParts: async ({ multipart, path }) => {
|
|
154
|
-
const res = await edgeStoreSdk.requestUploadParts({
|
|
155
|
-
multipart,
|
|
156
|
-
key: path,
|
|
157
|
-
});
|
|
158
|
-
return {
|
|
159
|
-
multipart: {
|
|
160
|
-
uploadId: res.multipart.uploadId,
|
|
161
|
-
parts: res.multipart.parts.map((part) => ({
|
|
162
|
-
partNumber: part.partNumber,
|
|
163
|
-
uploadUrl: part.signedUrl,
|
|
164
|
-
})),
|
|
165
|
-
},
|
|
166
|
-
};
|
|
167
|
-
},
|
|
168
|
-
getSignedUrls: async (params) => {
|
|
169
|
-
const res = await edgeStoreSdk.getSignedUrls(params);
|
|
170
|
-
return res.map((item) => ({
|
|
171
|
-
...item,
|
|
172
|
-
expiresAt: new Date(item.expiresAt),
|
|
173
|
-
}));
|
|
201
|
+
},
|
|
174
202
|
},
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
203
|
+
files: {
|
|
204
|
+
cursorSchema: z.string(),
|
|
205
|
+
get: async ({ bucketName, file: fileRef }) => {
|
|
206
|
+
const { file } = await runtime.files.lookup({
|
|
207
|
+
bucketName,
|
|
208
|
+
file: fileRef,
|
|
209
|
+
});
|
|
210
|
+
return mapFile(file);
|
|
211
|
+
},
|
|
212
|
+
list: async ({ bucketName, filter, cursor, limit }) => {
|
|
213
|
+
const { files, pagination } = await runtime.files.search({
|
|
214
|
+
bucket: bucketName,
|
|
215
|
+
filter,
|
|
216
|
+
pagination: { cursor, limit },
|
|
217
|
+
});
|
|
218
|
+
return {
|
|
219
|
+
items: files.map(mapFile),
|
|
220
|
+
...pagination,
|
|
221
|
+
};
|
|
222
|
+
},
|
|
223
|
+
confirm: async ({ bucketName, files }) =>
|
|
224
|
+
mapMutationResult(
|
|
225
|
+
await runtime.files.confirmMany({ bucketName, files }),
|
|
226
|
+
),
|
|
227
|
+
delete: async ({ bucketName, files }) =>
|
|
228
|
+
mapMutationResult(
|
|
229
|
+
await runtime.files.deleteMany({ bucketName, files }),
|
|
230
|
+
),
|
|
231
|
+
restore: async ({ bucketName, files }) =>
|
|
232
|
+
mapMutationResult(
|
|
233
|
+
await runtime.files.restoreMany({ bucketName, files }),
|
|
234
|
+
),
|
|
235
|
+
getSignedUrls: async (params) => {
|
|
236
|
+
const urls = await Promise.all(
|
|
237
|
+
params.files.map(async (fileRef) =>
|
|
238
|
+
'url' in fileRef
|
|
239
|
+
? fileRef.url
|
|
240
|
+
: (
|
|
241
|
+
await runtime.files.lookup({
|
|
242
|
+
bucketName: params.bucketName,
|
|
243
|
+
file: fileRef,
|
|
244
|
+
})
|
|
245
|
+
).file.url,
|
|
246
|
+
),
|
|
247
|
+
);
|
|
248
|
+
const { signedUrls } = await runtime.files.generateSignedReadUrls({
|
|
249
|
+
bucket: params.bucketName,
|
|
250
|
+
urls,
|
|
251
|
+
expiresIn: params.expiresIn,
|
|
252
|
+
includeThumbnails: params.includeThumbnails,
|
|
253
|
+
});
|
|
254
|
+
return signedUrls.map((item) => ({
|
|
255
|
+
...item,
|
|
256
|
+
expiresAt: new Date(item.expiresAt),
|
|
257
|
+
}));
|
|
258
|
+
},
|
|
186
259
|
},
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
return provider;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export type EdgeStoreBackendProvider = ReturnType<typeof edgestore>;
|
|
266
|
+
|
|
267
|
+
type ResolvedProviderAuthentication =
|
|
268
|
+
| {
|
|
269
|
+
kind: 'project';
|
|
270
|
+
accessKey: string;
|
|
271
|
+
secretKey: string;
|
|
272
|
+
}
|
|
273
|
+
| {
|
|
274
|
+
kind: 'bearer';
|
|
275
|
+
token: string;
|
|
276
|
+
project: string;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
function createProviderRuntime(
|
|
280
|
+
options?: EdgeStoreProviderOptions,
|
|
281
|
+
): ProjectRuntimeClient {
|
|
282
|
+
const authentication = resolveProviderAuthentication(options);
|
|
283
|
+
const apiUrl = options?.apiUrl ?? getApiUrl();
|
|
284
|
+
|
|
285
|
+
if (authentication.kind === 'bearer') {
|
|
286
|
+
const sdk = createEdgeStoreSdk({
|
|
287
|
+
credentials: { token: authentication.token },
|
|
288
|
+
apiUrl,
|
|
289
|
+
});
|
|
290
|
+
return sdk.runtime.forProject(authentication.project);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const sdk = createEdgeStoreSdk({
|
|
294
|
+
credentials: {
|
|
295
|
+
accessKey: authentication.accessKey,
|
|
296
|
+
secretKey: authentication.secretKey,
|
|
191
297
|
},
|
|
298
|
+
apiUrl,
|
|
299
|
+
});
|
|
300
|
+
return sdk.runtime;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function resolveProviderAuthentication(
|
|
304
|
+
options?: EdgeStoreProviderOptions,
|
|
305
|
+
): ResolvedProviderAuthentication {
|
|
306
|
+
const values = (options ?? {}) as Record<string, unknown>;
|
|
307
|
+
const token = values.token;
|
|
308
|
+
const project = values.project;
|
|
309
|
+
const configuredAccessKey = values.accessKey;
|
|
310
|
+
const configuredSecretKey = values.secretKey;
|
|
311
|
+
|
|
312
|
+
if (token !== undefined) {
|
|
313
|
+
if (
|
|
314
|
+
configuredAccessKey !== undefined ||
|
|
315
|
+
configuredSecretKey !== undefined
|
|
316
|
+
) {
|
|
317
|
+
throw new TypeError(
|
|
318
|
+
'EdgeStore provider credentials cannot contain both a Bearer token and project keys.',
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
assertNonEmptyProviderOption(token, 'token');
|
|
322
|
+
assertNonEmptyProviderOption(project, 'project');
|
|
323
|
+
return { kind: 'bearer', token, project };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (project !== undefined) {
|
|
327
|
+
throw new TypeError(
|
|
328
|
+
'EdgeStore provider option `project` requires a Bearer token.',
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const accessKey =
|
|
333
|
+
configuredAccessKey ??
|
|
334
|
+
getEnv('EDGE_STORE_ACCESS_KEY') ??
|
|
335
|
+
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
336
|
+
import.meta.env?.EDGE_STORE_ACCESS_KEY;
|
|
337
|
+
const secretKey =
|
|
338
|
+
configuredSecretKey ??
|
|
339
|
+
getEnv('EDGE_STORE_SECRET_KEY') ??
|
|
340
|
+
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
341
|
+
import.meta.env?.EDGE_STORE_SECRET_KEY;
|
|
342
|
+
|
|
343
|
+
if (
|
|
344
|
+
typeof accessKey !== 'string' ||
|
|
345
|
+
!accessKey.trim() ||
|
|
346
|
+
typeof secretKey !== 'string' ||
|
|
347
|
+
!secretKey.trim()
|
|
348
|
+
) {
|
|
349
|
+
throw new EdgeStoreCredentialsError();
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return { kind: 'project', accessKey, secretKey };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function assertNonEmptyProviderOption(
|
|
356
|
+
value: unknown,
|
|
357
|
+
name: string,
|
|
358
|
+
): asserts value is string {
|
|
359
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
360
|
+
throw new TypeError(
|
|
361
|
+
`EdgeStore provider option \`${name}\` must not be empty.`,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function mapMutationResult<TErrorCode extends string>(result: {
|
|
367
|
+
results: (
|
|
368
|
+
| { success: true }
|
|
369
|
+
| {
|
|
370
|
+
success: false;
|
|
371
|
+
error: {
|
|
372
|
+
code: TErrorCode;
|
|
373
|
+
message: string;
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
)[];
|
|
377
|
+
}): ProviderFileMutationResult<TErrorCode> {
|
|
378
|
+
return {
|
|
379
|
+
results: result.results.map((item) =>
|
|
380
|
+
item.success
|
|
381
|
+
? { success: true as const }
|
|
382
|
+
: { success: false as const, error: item.error },
|
|
383
|
+
),
|
|
192
384
|
};
|
|
193
385
|
}
|
|
194
386
|
|
|
195
|
-
function
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
387
|
+
function getApiUrl() {
|
|
388
|
+
const configured = getEnv('EDGE_STORE_API_ENDPOINT');
|
|
389
|
+
if (!configured) return undefined;
|
|
390
|
+
const base = configured.replace(/\/+$/, '');
|
|
391
|
+
return base.endsWith('/v2') ? base : `${base}/v2`;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function normalizeMetadata(
|
|
395
|
+
metadata: Record<string, string | null | undefined>,
|
|
396
|
+
) {
|
|
397
|
+
return Object.fromEntries(
|
|
398
|
+
Object.entries(metadata).filter(
|
|
399
|
+
(entry): entry is [string, string] => entry[1] != null,
|
|
400
|
+
),
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function mapHighLevelUploadOptions(
|
|
405
|
+
fileInfo: RequestUploadParams['fileInfo'],
|
|
406
|
+
signedReadUrl: RequestUploadParams['autoSignedUrls'],
|
|
407
|
+
) {
|
|
201
408
|
return {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
409
|
+
fileName: fileInfo.fileName,
|
|
410
|
+
mimeType: fileInfo.type,
|
|
411
|
+
temporary: fileInfo.temporary,
|
|
412
|
+
path: fileInfo.path,
|
|
413
|
+
extension: fileInfo.extension,
|
|
414
|
+
metadata: normalizeMetadata(fileInfo.metadata),
|
|
415
|
+
replaceTarget: fileInfo.replaceTargetUrl
|
|
416
|
+
? { url: fileInfo.replaceTargetUrl }
|
|
206
417
|
: undefined,
|
|
207
|
-
|
|
418
|
+
signedReadUrl,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function mapRawUploadRequest(
|
|
423
|
+
bucketType: string,
|
|
424
|
+
fileInfo: RequestUploadParams['fileInfo'],
|
|
425
|
+
signedReadUrl: RequestUploadParams['autoSignedUrls'],
|
|
426
|
+
) {
|
|
427
|
+
return {
|
|
428
|
+
bucketType: bucketType.toLowerCase() as 'file' | 'image',
|
|
429
|
+
visibility: fileInfo.isPublic
|
|
430
|
+
? ('public' as const)
|
|
431
|
+
: ('protected' as const),
|
|
432
|
+
sizeBytes: fileInfo.size,
|
|
433
|
+
...mapHighLevelUploadOptions(fileInfo, signedReadUrl),
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function mapUploadResponse(
|
|
438
|
+
res: Awaited<ReturnType<ProjectRuntimeClient['uploads']['request']>>,
|
|
439
|
+
multipartConfig?: { partSize: number; totalParts: number },
|
|
440
|
+
): RequestUploadRes {
|
|
441
|
+
const signed = res.signedReadUrl;
|
|
442
|
+
const access = {
|
|
443
|
+
accessUrl: res.file.url,
|
|
444
|
+
thumbnailUrl: res.file.thumbnailUrl,
|
|
445
|
+
accessSignedUrl: signed?.signedUrl,
|
|
446
|
+
accessSignedThumbnailUrl: signed?.signedThumbnailUrl,
|
|
447
|
+
accessSignedUrlExpiresAt: signed?.expiresAt,
|
|
448
|
+
accessSignedUrlExpiresIn: signed?.expiresIn,
|
|
449
|
+
};
|
|
450
|
+
if (res.upload.kind === 'single') {
|
|
451
|
+
return { ...access, uploadUrl: res.upload.signedUrl };
|
|
452
|
+
}
|
|
453
|
+
if (!multipartConfig) {
|
|
454
|
+
throw new EdgeStoreError({
|
|
455
|
+
message: 'Missing multipart configuration.',
|
|
456
|
+
code: 'SERVER_ERROR',
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
return {
|
|
460
|
+
...access,
|
|
461
|
+
multipart: {
|
|
462
|
+
key: res.file.key,
|
|
463
|
+
uploadId: res.upload.id,
|
|
464
|
+
parts: res.upload.parts.map((part) => ({
|
|
465
|
+
partNumber: part.partNumber,
|
|
466
|
+
uploadUrl: part.signedUrl,
|
|
467
|
+
})),
|
|
468
|
+
...multipartConfig,
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function mapFile(
|
|
474
|
+
file: Awaited<ReturnType<ProjectRuntimeClient['files']['lookup']>>['file'],
|
|
475
|
+
): ProviderFile {
|
|
476
|
+
return {
|
|
477
|
+
...file,
|
|
478
|
+
uploadedAt: new Date(file.uploadedAt),
|
|
479
|
+
updatedAt: new Date(file.updatedAt),
|
|
208
480
|
};
|
|
209
481
|
}
|