@edgestore/server 1.0.0 → 1.0.1

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 (57) hide show
  1. package/README.md +2 -0
  2. package/agent-docs/README.md +1 -1
  3. package/agent-docs/configuration.md +3 -2
  4. package/agent-docs/provider-custom.md +5 -0
  5. package/dist/adapters/astro/index.d.ts.map +1 -1
  6. package/dist/adapters/astro/index.js +1 -0
  7. package/dist/adapters/dispatcher.d.ts +1 -0
  8. package/dist/adapters/dispatcher.d.ts.map +1 -1
  9. package/dist/adapters/dispatcher.js +11 -1
  10. package/dist/adapters/express/index.d.ts.map +1 -1
  11. package/dist/adapters/express/index.js +1 -0
  12. package/dist/adapters/fastify/index.d.ts.map +1 -1
  13. package/dist/adapters/fastify/index.js +1 -0
  14. package/dist/adapters/hono/index.d.ts.map +1 -1
  15. package/dist/adapters/hono/index.js +1 -0
  16. package/dist/adapters/next/app/index.d.ts.map +1 -1
  17. package/dist/adapters/next/app/index.js +1 -0
  18. package/dist/adapters/next/pages/index.d.ts.map +1 -1
  19. package/dist/adapters/next/pages/index.js +1 -0
  20. package/dist/adapters/remix/index.d.ts.map +1 -1
  21. package/dist/adapters/remix/index.js +1 -0
  22. package/dist/adapters/shared.d.ts +11 -4
  23. package/dist/adapters/shared.d.ts.map +1 -1
  24. package/dist/adapters/shared.js +75 -27
  25. package/dist/adapters/start/index.d.ts.map +1 -1
  26. package/dist/adapters/start/index.js +1 -0
  27. package/dist/libs/env.d.ts +1 -0
  28. package/dist/libs/env.d.ts.map +1 -1
  29. package/dist/libs/env.js +4 -1
  30. package/dist/providers/azure-blob/index.d.ts.map +1 -1
  31. package/dist/providers/azure-blob/index.js +27 -12
  32. package/dist/providers/edgestore/index.d.ts +1 -1
  33. package/dist/providers/edgestore/index.d.ts.map +1 -1
  34. package/dist/providers/edgestore/index.js +37 -14
  35. package/dist/providers/s3/index.d.ts.map +1 -1
  36. package/dist/providers/s3/index.js +34 -16
  37. package/dist/providers/storage/objectKeys.d.ts +8 -0
  38. package/dist/providers/storage/objectKeys.d.ts.map +1 -1
  39. package/dist/providers/storage/objectKeys.js +18 -1
  40. package/dist/version.d.ts +1 -1
  41. package/package.json +4 -4
  42. package/src/adapters/astro/index.ts +1 -0
  43. package/src/adapters/dispatcher.ts +12 -0
  44. package/src/adapters/express/index.ts +1 -0
  45. package/src/adapters/fastify/index.ts +1 -0
  46. package/src/adapters/hono/index.ts +1 -0
  47. package/src/adapters/next/app/index.ts +1 -0
  48. package/src/adapters/next/pages/index.ts +1 -0
  49. package/src/adapters/remix/index.ts +1 -0
  50. package/src/adapters/shared.ts +125 -34
  51. package/src/adapters/start/index.ts +1 -0
  52. package/src/libs/env.ts +4 -0
  53. package/src/providers/azure-blob/index.ts +32 -11
  54. package/src/providers/edgestore/index.ts +42 -20
  55. package/src/providers/s3/index.ts +41 -14
  56. package/src/providers/storage/objectKeys.ts +21 -0
  57. package/src/version.ts +1 -1
@@ -3,6 +3,7 @@ import {
3
3
  BlobSASPermissions,
4
4
  BlobServiceClient,
5
5
  generateBlobSASQueryParameters,
6
+ RestError,
6
7
  SASProtocol,
7
8
  StorageSharedKeyCredential,
8
9
  } from '@azure/storage-blob';
@@ -18,6 +19,7 @@ import { getEnv } from '../../libs/env';
18
19
  import { createMultipartSessions } from '../storage/multipartSession';
19
20
  import {
20
21
  createObjectKeys,
22
+ pathFromDefaultKey,
21
23
  type ObjectPathFn,
22
24
  type ObjectPathFnArgs,
23
25
  } from '../storage/objectKeys';
@@ -204,6 +206,34 @@ export function azureBlob(options: AzureBlobProviderOptions = {}) {
204
206
  };
205
207
  }
206
208
 
209
+ async function describeBlob(key: string) {
210
+ const { contentLength, lastModified } = await blob(key).getProperties();
211
+ if (contentLength === undefined || !lastModified)
212
+ throw new Error('File not found');
213
+ return {
214
+ key,
215
+ url: keys.toUrl(key),
216
+ sizeBytes: contentLength,
217
+ uploadedAt: lastModified,
218
+ updatedAt: lastModified,
219
+ };
220
+ }
221
+
222
+ /** Authorizes overwriting the blob at `key`, if one exists. */
223
+ async function authorizeOverwrite(key: string, params: RequestUploadParams) {
224
+ if (!params.authorizeOverwrite) return;
225
+ const existing = await describeBlob(key).catch((error: unknown) => {
226
+ if (error instanceof RestError && error.statusCode === 404)
227
+ return undefined;
228
+ throw error;
229
+ });
230
+ if (!existing) return;
231
+ await params.authorizeOverwrite({
232
+ ...existing,
233
+ path: pathFromDefaultKey(params.fileInfo, options.path),
234
+ });
235
+ }
236
+
207
237
  function readForUpload(key: string, params: RequestUploadParams) {
208
238
  if (params.fileInfo.isPublic || !params.autoSignedUrls) return undefined;
209
239
  return signedRead(key, params.autoSignedUrls.expiresIn);
@@ -262,6 +292,7 @@ export function azureBlob(options: AzureBlobProviderOptions = {}) {
262
292
  supportedOptions: { temporary: false, replaceTargetUrl: false },
263
293
  async request(params): Promise<RequestUploadRes> {
264
294
  const { key, commit, plan } = await prepare(params);
295
+ await authorizeOverwrite(key, params);
265
296
  const signedReadUrl = readForUpload(key, params);
266
297
  const access = {
267
298
  key,
@@ -320,17 +351,7 @@ export function azureBlob(options: AzureBlobProviderOptions = {}) {
320
351
  },
321
352
  files: {
322
353
  async get({ bucketName, file }) {
323
- const key = keys.fromReference(bucketName, file);
324
- const { contentLength, lastModified } = await blob(key).getProperties();
325
- if (contentLength === undefined || !lastModified)
326
- throw new Error('File not found');
327
- return {
328
- key,
329
- url: keys.toUrl(key),
330
- sizeBytes: contentLength,
331
- uploadedAt: lastModified,
332
- updatedAt: lastModified,
333
- };
354
+ return describeBlob(keys.fromReference(bucketName, file));
334
355
  },
335
356
  async getSignedUrls({ bucketName, files, expiresIn }) {
336
357
  return files.map((file) =>
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  createEdgeStoreSdk,
3
+ EdgeStoreApiError,
3
4
  planMultipartUpload,
4
5
  type ProjectRuntimeClient,
5
6
  } from '@edgestore/sdk';
@@ -131,33 +132,44 @@ export function edgestore(options?: EdgeStoreProviderOptions) {
131
132
  bucketType,
132
133
  fileInfo,
133
134
  autoSignedUrls,
135
+ authorizeOverwrite,
134
136
  }): Promise<RequestUploadRes> {
135
137
  const multipartPlan = planMultipartUpload({
136
138
  sizeBytes: fileInfo.size,
137
139
  });
138
- if (multipartPlan) {
139
- return mapUploadResponse(
140
- await runtime.uploads.request({
141
- bucket: bucketName,
142
- ...mapRawUploadRequest(bucketType, fileInfo, autoSignedUrls),
143
- multipart: {
144
- partNumbers: multipartPlan.partNumbers.slice(
145
- 0,
146
- INITIAL_MULTIPART_PART_URLS,
147
- ),
148
- },
149
- }),
150
- {
151
- partSize: multipartPlan.partSizeBytes,
152
- totalParts: multipartPlan.totalParts,
140
+ const res = await runtime.uploads.request({
141
+ bucket: bucketName,
142
+ ...mapRawUploadRequest(bucketType, fileInfo, autoSignedUrls),
143
+ ...(multipartPlan && {
144
+ multipart: {
145
+ partNumbers: multipartPlan.partNumbers.slice(
146
+ 0,
147
+ INITIAL_MULTIPART_PART_URLS,
148
+ ),
153
149
  },
154
- );
150
+ }),
151
+ });
152
+ if (authorizeOverwrite) {
153
+ // The upload supersedes an uploaded file at the same key once it
154
+ // completes, so cancel it unless the requester may delete that file.
155
+ try {
156
+ const existing = await findFile(bucketName, res.file.key);
157
+ if (existing) await authorizeOverwrite(mapFile(existing));
158
+ } catch (error) {
159
+ await runtime.uploads
160
+ .cancel({ uploadId: res.file.id })
161
+ .catch(() => undefined);
162
+ throw error;
163
+ }
155
164
  }
156
165
  return mapUploadResponse(
157
- await runtime.uploads.request({
158
- bucket: bucketName,
159
- ...mapRawUploadRequest(bucketType, fileInfo, autoSignedUrls),
160
- }),
166
+ res,
167
+ multipartPlan
168
+ ? {
169
+ partSize: multipartPlan.partSizeBytes,
170
+ totalParts: multipartPlan.totalParts,
171
+ }
172
+ : undefined,
161
173
  );
162
174
  },
163
175
  multipart: {
@@ -287,6 +299,16 @@ export function edgestore(options?: EdgeStoreProviderOptions) {
287
299
  },
288
300
  });
289
301
 
302
+ async function findFile(bucketName: string, key: string) {
303
+ try {
304
+ return (await runtime.files.lookup({ bucketName, file: { key } })).file;
305
+ } catch (error) {
306
+ if (error instanceof EdgeStoreApiError && error.code === 'file_not_found')
307
+ return undefined;
308
+ throw error;
309
+ }
310
+ }
311
+
290
312
  return provider;
291
313
  }
292
314
 
@@ -4,6 +4,7 @@ import {
4
4
  HeadObjectCommand,
5
5
  PutObjectCommand,
6
6
  S3Client,
7
+ S3ServiceException,
7
8
  type PutObjectCommandInput,
8
9
  } from '@aws-sdk/client-s3';
9
10
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
@@ -16,7 +17,7 @@ import { z } from 'zod';
16
17
  import { defineProvider } from '../../core/provider';
17
18
  import { getEnv } from '../../libs/env';
18
19
  import { createMultipartSessions } from '../storage/multipartSession';
19
- import { createObjectKeys } from '../storage/objectKeys';
20
+ import { createObjectKeys, pathFromDefaultKey } from '../storage/objectKeys';
20
21
  import {
21
22
  assertSignedUrlAccessControl,
22
23
  signedUrlLifetime,
@@ -169,6 +170,42 @@ export function s3(options: S3ProviderOptions = {}) {
169
170
  })());
170
171
  }
171
172
 
173
+ async function describeObject(key: string) {
174
+ const { ContentLength, LastModified } = await client.send(
175
+ new HeadObjectCommand({ Bucket: bucket(), Key: key }),
176
+ );
177
+ if (ContentLength === undefined || !LastModified)
178
+ throw new Error('File not found');
179
+ return {
180
+ key,
181
+ url: (await keys()).toUrl(key),
182
+ sizeBytes: ContentLength,
183
+ uploadedAt: LastModified,
184
+ updatedAt: LastModified,
185
+ };
186
+ }
187
+
188
+ /** Authorizes overwriting the object at `key`, if one exists. */
189
+ async function authorizeOverwrite(key: string, params: RequestUploadParams) {
190
+ if (!params.authorizeOverwrite) return;
191
+ const existing = await describeObject(key).catch((error: unknown) => {
192
+ // Without s3:ListBucket, S3 reports a missing key as 403, not 404.
193
+ // Credentials without s3:GetObject can't detect overwrites; the
194
+ // documented policy grants it.
195
+ const status =
196
+ error instanceof S3ServiceException
197
+ ? error.$metadata.httpStatusCode
198
+ : undefined;
199
+ if (status === 403 || status === 404) return undefined;
200
+ throw error;
201
+ });
202
+ if (!existing) return;
203
+ await params.authorizeOverwrite({
204
+ ...existing,
205
+ path: pathFromDefaultKey(params.fileInfo, options.path),
206
+ });
207
+ }
208
+
172
209
  async function readForUpload(key: string, params: RequestUploadParams) {
173
210
  if (params.fileInfo.isPublic || !params.autoSignedUrls) return undefined;
174
211
  return signedRead(key, params.autoSignedUrls.expiresIn);
@@ -193,6 +230,7 @@ export function s3(options: S3ProviderOptions = {}) {
193
230
  async request(params): Promise<RequestUploadRes> {
194
231
  await assertPresignableClient();
195
232
  const prepared = await prepare(params);
233
+ await authorizeOverwrite(prepared.key, params);
196
234
  const read = await readForUpload(prepared.key, params);
197
235
  const access = {
198
236
  key: prepared.key,
@@ -268,20 +306,9 @@ export function s3(options: S3ProviderOptions = {}) {
268
306
  },
269
307
  files: {
270
308
  async get({ bucketName: logicalBucket, file }) {
271
- const objectKeys = await keys();
272
- const key = objectKeys.fromReference(logicalBucket, file);
273
- const { ContentLength, LastModified } = await client.send(
274
- new HeadObjectCommand({ Bucket: bucket(), Key: key }),
309
+ return describeObject(
310
+ (await keys()).fromReference(logicalBucket, file),
275
311
  );
276
- if (ContentLength === undefined || !LastModified)
277
- throw new Error('File not found');
278
- return {
279
- key,
280
- url: objectKeys.toUrl(key),
281
- sizeBytes: ContentLength,
282
- uploadedAt: LastModified,
283
- updatedAt: LastModified,
284
- };
285
312
  },
286
313
  async getSignedUrls({ bucketName: logicalBucket, files, expiresIn }) {
287
314
  const objectKeys = await keys();
@@ -104,3 +104,24 @@ export function createObjectKeys(providerName: string, baseUrl: string) {
104
104
 
105
105
  return { toUrl: objectKeyToUrl, fromReference, forUpload };
106
106
  }
107
+
108
+ /**
109
+ * Path values of an existing object at an upload's key, when the key proves
110
+ * them. Only the default layout encodes the path, and a `/` inside a path
111
+ * value or the file name makes its segment boundaries ambiguous.
112
+ */
113
+ export function pathFromDefaultKey(
114
+ fileInfo: RequestUploadParams['fileInfo'],
115
+ customPath: ObjectPathFn | undefined,
116
+ ) {
117
+ const segments = [
118
+ ...fileInfo.path.map(({ value }) => value),
119
+ fileInfo.fileName ?? '',
120
+ ];
121
+ if (customPath || segments.some((segment) => segment.includes('/'))) {
122
+ return undefined;
123
+ }
124
+ return Object.fromEntries(
125
+ fileInfo.path.map(({ key, value }) => [key, value]),
126
+ );
127
+ }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export const EDGE_STORE_PACKAGE_NAME = '@edgestore/server';
2
- export const EDGE_STORE_PACKAGE_VERSION = '1.0.0';
2
+ export const EDGE_STORE_PACKAGE_VERSION = '1.0.1';