@edgestore/server 0.8.0 → 1.0.0-next.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.
- package/README.md +10 -4
- package/adapters/astro/index.d.ts +1 -1
- package/adapters/astro/index.js +1 -1
- package/adapters/express/index.d.ts +1 -1
- package/adapters/express/index.js +1 -1
- package/adapters/fastify/index.d.ts +1 -1
- package/adapters/fastify/index.js +1 -1
- package/adapters/hono/index.d.ts +1 -1
- package/adapters/hono/index.js +1 -1
- package/adapters/next/app/index.d.ts +1 -1
- package/adapters/next/app/index.js +1 -1
- package/adapters/next/pages/index.d.ts +1 -1
- package/adapters/next/pages/index.js +1 -1
- package/adapters/remix/index.d.ts +1 -1
- package/adapters/remix/index.js +1 -1
- package/adapters/start/index.d.ts +1 -1
- package/adapters/start/index.js +1 -1
- package/core/index.d.ts +1 -1
- package/core/index.js +1 -1
- package/dist/adapters/astro/index.d.ts +8 -13
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +17 -166
- 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 +8 -13
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +27 -99
- package/dist/adapters/fastify/index.d.ts +8 -13
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +26 -126
- package/dist/adapters/hono/index.d.ts +8 -13
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +17 -126
- package/dist/adapters/imageTypes.js +1 -3
- package/dist/adapters/next/app/index.d.ts +8 -13
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +24 -153
- package/dist/adapters/next/pages/index.d.ts +8 -13
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +37 -100
- package/dist/adapters/remix/index.d.ts +8 -13
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +17 -141
- package/dist/adapters/shared.d.ts +140 -55
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +218 -215
- package/dist/adapters/start/index.d.ts +8 -13
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +17 -174
- 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 +119 -140
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +13 -220
- package/dist/core/index.d.ts +21 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +14 -10
- 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 +21 -0
- package/dist/core/routerRules.d.ts.map +1 -0
- package/dist/core/routerRules.js +54 -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 +4 -10
- 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/errors/EdgeStoreCredentialsError.js +1 -3
- package/dist/libs/logger.d.ts +2 -1
- package/dist/libs/logger.d.ts.map +1 -1
- package/dist/libs/logger.js +5 -9
- package/dist/libs/utils.js +1 -3
- 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 +307 -141
- 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/dist/version.d.ts.map +1 -1
- package/package.json +58 -56
- package/providers/azure-blob/index.d.ts +1 -0
- package/providers/azure-blob/index.js +1 -0
- package/providers/edgestore/index.d.ts +1 -1
- package/providers/edgestore/index.js +1 -1
- 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 +249 -274
- package/src/adapters/start/index.ts +28 -200
- package/src/core/client/bucketClient.ts +480 -0
- package/src/core/client/index.ts +300 -428
- package/src/core/index.ts +42 -1
- package/src/core/provider.ts +80 -0
- package/src/core/routerRules.ts +75 -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/adapters/astro/index.mjs +0 -172
- package/dist/adapters/express/index.mjs +0 -108
- package/dist/adapters/fastify/index.mjs +0 -135
- package/dist/adapters/hono/index.mjs +0 -131
- package/dist/adapters/imageTypes.mjs +0 -12
- package/dist/adapters/next/app/index.mjs +0 -163
- package/dist/adapters/next/pages/index.mjs +0 -112
- package/dist/adapters/remix/index.mjs +0 -148
- package/dist/adapters/shared.mjs +0 -471
- package/dist/adapters/start/index.mjs +0 -181
- package/dist/core/client/index.mjs +0 -221
- package/dist/core/index.mjs +0 -2
- 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 -256
- package/dist/core/sdk/index.mjs +0 -253
- package/dist/index.mjs +0 -1
- package/dist/libs/errors/EdgeStoreCredentialsError.mjs +0 -10
- package/dist/libs/logger.mjs +0 -53
- package/dist/libs/utils.mjs +0 -10
- package/dist/providers/aws/index.d.ts.map +0 -1
- package/dist/providers/aws/index.js +0 -105
- package/dist/providers/aws/index.mjs +0 -103
- 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 -87
- package/dist/providers/azure/index.mjs +0 -85
- package/dist/providers/edgestore/index.mjs +0 -164
- package/dist/version.js +0 -7
- package/dist/version.mjs +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 -549
- package/src/providers/aws/index.ts +0 -240
- package/src/providers/azure/index.ts +0 -142
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
1
|
+
import { type ProviderFile, type ProviderFileMutationResult, type RequestUploadParams, type RequestUploadRes } from '@edgestore/shared';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
type EdgeStoreProviderCommonOptions = {
|
|
4
|
+
/** Override the complete API v2 URL. */
|
|
5
|
+
apiUrl?: string;
|
|
6
|
+
};
|
|
7
|
+
type EdgeStoreProjectProviderOptions = {
|
|
3
8
|
/**
|
|
4
9
|
* Access key for your EdgeStore project.
|
|
5
10
|
* Can be found in the EdgeStore dashboard.
|
|
@@ -14,6 +19,169 @@ export type EdgeStoreProviderOptions = {
|
|
|
14
19
|
* This can be omitted if the `EDGE_STORE_SECRET_KEY` environment variable is set.
|
|
15
20
|
*/
|
|
16
21
|
secretKey?: string;
|
|
22
|
+
token?: never;
|
|
23
|
+
project?: never;
|
|
24
|
+
};
|
|
25
|
+
type EdgeStoreBearerProviderOptions = {
|
|
26
|
+
/** Bearer token used to authenticate API v2 requests. */
|
|
27
|
+
token: string;
|
|
28
|
+
/** Project ID or slug used by runtime operations. */
|
|
29
|
+
project: string;
|
|
30
|
+
accessKey?: never;
|
|
31
|
+
secretKey?: never;
|
|
32
|
+
};
|
|
33
|
+
export type EdgeStoreProviderOptions = EdgeStoreProviderCommonOptions & (EdgeStoreProjectProviderOptions | EdgeStoreBearerProviderOptions);
|
|
34
|
+
export declare function edgestore(options?: EdgeStoreProviderOptions): {
|
|
35
|
+
readonly name: "edgestore";
|
|
36
|
+
readonly baseUrl: string;
|
|
37
|
+
readonly reference: {
|
|
38
|
+
readonly schema: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodObject<{
|
|
39
|
+
id: z.ZodString;
|
|
40
|
+
}, "strip", z.ZodTypeAny, {
|
|
41
|
+
id: string;
|
|
42
|
+
}, {
|
|
43
|
+
id: string;
|
|
44
|
+
}>, z.ZodObject<{
|
|
45
|
+
key: z.ZodString;
|
|
46
|
+
}, "strip", z.ZodTypeAny, {
|
|
47
|
+
key: string;
|
|
48
|
+
}, {
|
|
49
|
+
key: string;
|
|
50
|
+
}>, z.ZodObject<{
|
|
51
|
+
url: z.ZodString;
|
|
52
|
+
}, "strip", z.ZodTypeAny, {
|
|
53
|
+
url: string;
|
|
54
|
+
}, {
|
|
55
|
+
url: string;
|
|
56
|
+
}>]>, {
|
|
57
|
+
id: string;
|
|
58
|
+
} | {
|
|
59
|
+
key: string;
|
|
60
|
+
} | {
|
|
61
|
+
url: string;
|
|
62
|
+
}, string | {
|
|
63
|
+
id: string;
|
|
64
|
+
} | {
|
|
65
|
+
key: string;
|
|
66
|
+
} | {
|
|
67
|
+
url: string;
|
|
68
|
+
}>;
|
|
69
|
+
readonly fromUrl: (url: string) => {
|
|
70
|
+
url: string;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
readonly init: <TCtx extends import("@edgestore/shared").AnyContext>({ ctx, router }: import("@edgestore/shared").InitParams<TCtx>) => Promise<{
|
|
74
|
+
token?: undefined;
|
|
75
|
+
clientInit?: undefined;
|
|
76
|
+
} | {
|
|
77
|
+
token: string;
|
|
78
|
+
clientInit: {
|
|
79
|
+
path: string;
|
|
80
|
+
headers: {
|
|
81
|
+
'x-edgestore-token': string;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
}>;
|
|
85
|
+
readonly uploads: {
|
|
86
|
+
readonly request: ({ bucketName, bucketType, fileInfo, autoSignedUrls, }: RequestUploadParams) => Promise<RequestUploadRes>;
|
|
87
|
+
readonly multipart: {
|
|
88
|
+
readonly requestParts: ({ multipart }: import("@edgestore/shared").RequestUploadPartsParams) => Promise<{
|
|
89
|
+
multipart: {
|
|
90
|
+
uploadId: string;
|
|
91
|
+
parts: {
|
|
92
|
+
partNumber: number;
|
|
93
|
+
uploadUrl: string;
|
|
94
|
+
}[];
|
|
95
|
+
};
|
|
96
|
+
}>;
|
|
97
|
+
readonly complete: ({ uploadId, parts }: import("@edgestore/shared").CompleteMultipartUploadParams) => Promise<void>;
|
|
98
|
+
};
|
|
99
|
+
readonly upload: ({ bucketName, fileInfo, autoSignedUrls, source, signal, onProgress, }: import("@edgestore/shared").BackendUploadParams) => Promise<{
|
|
100
|
+
file: ProviderFile;
|
|
101
|
+
signedReadUrl: {
|
|
102
|
+
expiresAt: Date;
|
|
103
|
+
url: string;
|
|
104
|
+
signedUrl: string;
|
|
105
|
+
expiresIn: number;
|
|
106
|
+
thumbnailUrl?: string | null;
|
|
107
|
+
signedThumbnailUrl?: string | null;
|
|
108
|
+
} | undefined;
|
|
109
|
+
}>;
|
|
110
|
+
};
|
|
111
|
+
readonly files: {
|
|
112
|
+
readonly cursorSchema: z.ZodString;
|
|
113
|
+
readonly get: ({ bucketName, file: fileRef }: {
|
|
114
|
+
bucketName: string;
|
|
115
|
+
file: {
|
|
116
|
+
id: string;
|
|
117
|
+
} | {
|
|
118
|
+
key: string;
|
|
119
|
+
} | {
|
|
120
|
+
url: string;
|
|
121
|
+
};
|
|
122
|
+
}) => Promise<ProviderFile>;
|
|
123
|
+
readonly list: ({ bucketName, filter, cursor, limit }: {
|
|
124
|
+
bucketName: string;
|
|
125
|
+
filter?: import("@edgestore/shared").ListFilesFilter;
|
|
126
|
+
cursor?: string | undefined;
|
|
127
|
+
limit?: number;
|
|
128
|
+
}) => Promise<{
|
|
129
|
+
limit: number;
|
|
130
|
+
nextCursor: string | null;
|
|
131
|
+
hasMore: boolean;
|
|
132
|
+
items: ProviderFile[];
|
|
133
|
+
}>;
|
|
134
|
+
readonly confirm: ({ bucketName, files }: {
|
|
135
|
+
bucketName: string;
|
|
136
|
+
files: ({
|
|
137
|
+
id: string;
|
|
138
|
+
} | {
|
|
139
|
+
key: string;
|
|
140
|
+
} | {
|
|
141
|
+
url: string;
|
|
142
|
+
})[];
|
|
143
|
+
}) => Promise<ProviderFileMutationResult<"FILE_NOT_CONFIRMABLE" | "FILE_NOT_DELETABLE" | "FILE_NOT_RESTORABLE" | "INVALID_FILE_REF">>;
|
|
144
|
+
readonly delete: ({ bucketName, files }: {
|
|
145
|
+
bucketName: string;
|
|
146
|
+
files: ({
|
|
147
|
+
id: string;
|
|
148
|
+
} | {
|
|
149
|
+
key: string;
|
|
150
|
+
} | {
|
|
151
|
+
url: string;
|
|
152
|
+
})[];
|
|
153
|
+
}) => Promise<ProviderFileMutationResult<"FILE_NOT_CONFIRMABLE" | "FILE_NOT_DELETABLE" | "FILE_NOT_RESTORABLE" | "INVALID_FILE_REF">>;
|
|
154
|
+
readonly restore: ({ bucketName, files }: {
|
|
155
|
+
bucketName: string;
|
|
156
|
+
files: ({
|
|
157
|
+
id: string;
|
|
158
|
+
} | {
|
|
159
|
+
key: string;
|
|
160
|
+
} | {
|
|
161
|
+
url: string;
|
|
162
|
+
})[];
|
|
163
|
+
}) => Promise<ProviderFileMutationResult<"FILE_NOT_CONFIRMABLE" | "FILE_NOT_DELETABLE" | "FILE_NOT_RESTORABLE" | "INVALID_FILE_REF">>;
|
|
164
|
+
readonly getSignedUrls: (params: {
|
|
165
|
+
bucketName: string;
|
|
166
|
+
files: ({
|
|
167
|
+
id: string;
|
|
168
|
+
} | {
|
|
169
|
+
key: string;
|
|
170
|
+
} | {
|
|
171
|
+
url: string;
|
|
172
|
+
})[];
|
|
173
|
+
expiresIn?: number;
|
|
174
|
+
includeThumbnails?: boolean;
|
|
175
|
+
}) => Promise<{
|
|
176
|
+
expiresAt: Date;
|
|
177
|
+
url: string;
|
|
178
|
+
signedUrl: string;
|
|
179
|
+
expiresIn: number;
|
|
180
|
+
thumbnailUrl?: string | null;
|
|
181
|
+
signedThumbnailUrl?: string | null;
|
|
182
|
+
}[]>;
|
|
183
|
+
};
|
|
17
184
|
};
|
|
18
|
-
export
|
|
185
|
+
export type EdgeStoreBackendProvider = ReturnType<typeof edgestore>;
|
|
186
|
+
export {};
|
|
19
187
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/edgestore/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/edgestore/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACtB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAkBxB,KAAK,8BAA8B,GAAG;IACpC,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,KAAK,+BAA+B,GAAG;IACrC;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,OAAO,CAAC,EAAE,KAAK,CAAC;CACjB,CAAC;AAEF,KAAK,8BAA8B,GAAG;IACpC,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,KAAK,CAAC;IAClB,SAAS,CAAC,EAAE,KAAK,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG,8BAA8B,GACnE,CAAC,+BAA+B,GAAG,8BAA8B,CAAC,CAAC;AAErE,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0GA6DlD,OAAO,CAAC,gBAAgB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuIlC;AAED,MAAM,MAAM,wBAAwB,GAAG,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC"}
|
|
@@ -1,166 +1,332 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { planMultipartUpload, createEdgeStoreSdk } from '@edgestore/sdk';
|
|
2
|
+
import { EdgeStoreError } from '@edgestore/shared';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { defineProvider } from '../../core/provider.js';
|
|
5
|
+
import { getEnv } from '../../libs/env.js';
|
|
6
|
+
import EdgeStoreCredentialsError from '../../libs/errors/EdgeStoreCredentialsError.js';
|
|
7
7
|
|
|
8
8
|
const DEFAULT_BASE_URL = 'https://files.edgestore.dev';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
9
|
+
const fileReferenceSchema = z.union([
|
|
10
|
+
z.string(),
|
|
11
|
+
z.object({
|
|
12
|
+
id: z.string()
|
|
13
|
+
}),
|
|
14
|
+
z.object({
|
|
15
|
+
key: z.string()
|
|
16
|
+
}),
|
|
17
|
+
z.object({
|
|
18
|
+
url: z.string()
|
|
19
|
+
})
|
|
20
|
+
]).transform((reference)=>typeof reference === 'string' ? {
|
|
21
|
+
url: reference
|
|
22
|
+
} : reference);
|
|
23
|
+
function edgestore(options) {
|
|
24
|
+
const baseUrl = getEnv('EDGE_STORE_BASE_URL') ?? DEFAULT_BASE_URL;
|
|
25
|
+
const runtime = createProviderRuntime(options);
|
|
26
|
+
const provider = defineProvider({
|
|
22
27
|
name: 'edgestore',
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
token
|
|
30
|
-
};
|
|
28
|
+
baseUrl,
|
|
29
|
+
reference: {
|
|
30
|
+
schema: fileReferenceSchema,
|
|
31
|
+
fromUrl: (url)=>({
|
|
32
|
+
url
|
|
33
|
+
})
|
|
31
34
|
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
init: async ({ ctx, router })=>{
|
|
36
|
+
const requiresFileAccessCookie = Object.values(router.buckets).some((bucket)=>bucket._def.accessControl !== undefined && bucket._def.accessControl !== 'private');
|
|
37
|
+
if (!requiresFileAccessCookie) return {};
|
|
38
|
+
const { token } = await runtime.accessTokens.create({
|
|
39
|
+
context: Object.fromEntries(Object.entries(ctx).filter((entry)=>entry[1] !== undefined)),
|
|
40
|
+
buckets: Object.fromEntries(Object.entries(router.buckets).map(([bucketName, bucket])=>[
|
|
41
|
+
bucketName,
|
|
42
|
+
{
|
|
43
|
+
path: bucket._def.path.map((part)=>{
|
|
44
|
+
const entry = Object.entries(part)[0];
|
|
45
|
+
if (!entry) {
|
|
46
|
+
throw new EdgeStoreError({
|
|
47
|
+
message: 'Bucket paths cannot contain empty segments.',
|
|
48
|
+
code: 'SERVER_ERROR'
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const [key, value] = entry;
|
|
52
|
+
return {
|
|
53
|
+
key,
|
|
54
|
+
value: value()
|
|
55
|
+
};
|
|
56
|
+
}),
|
|
57
|
+
accessControl: bucket._def.accessControl
|
|
58
|
+
}
|
|
59
|
+
]))
|
|
38
60
|
});
|
|
39
61
|
return {
|
|
40
|
-
|
|
41
|
-
|
|
62
|
+
token,
|
|
63
|
+
clientInit: {
|
|
64
|
+
path: '/_init',
|
|
65
|
+
headers: {
|
|
66
|
+
'x-edgestore-token': token
|
|
67
|
+
}
|
|
68
|
+
}
|
|
42
69
|
};
|
|
43
70
|
},
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (fileInfo.size > MULTIPART_THRESHOLD) {
|
|
49
|
-
let totalParts = Math.ceil(fileInfo.size / partSize);
|
|
50
|
-
if (totalParts > 1000) {
|
|
51
|
-
// the maximum number of parts is 1000
|
|
52
|
-
totalParts = 1000;
|
|
53
|
-
partSize = Math.ceil(fileInfo.size / totalParts);
|
|
54
|
-
}
|
|
55
|
-
const res = await edgeStoreSdk.requestUpload({
|
|
56
|
-
bucketName,
|
|
57
|
-
bucketType,
|
|
58
|
-
fileInfo,
|
|
59
|
-
autoSignedUrls,
|
|
60
|
-
multipart: {
|
|
61
|
-
parts: Array.from({
|
|
62
|
-
length: totalParts
|
|
63
|
-
}).map((_, index)=>index + 1)
|
|
64
|
-
}
|
|
71
|
+
uploads: {
|
|
72
|
+
async request ({ bucketName, bucketType, fileInfo, autoSignedUrls }) {
|
|
73
|
+
const multipartPlan = planMultipartUpload({
|
|
74
|
+
sizeBytes: fileInfo.size
|
|
65
75
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
76
|
+
if (multipartPlan) {
|
|
77
|
+
return mapUploadResponse(await runtime.uploads.request({
|
|
78
|
+
bucket: bucketName,
|
|
79
|
+
...mapRawUploadRequest(bucketType, fileInfo, autoSignedUrls),
|
|
80
|
+
multipart: {
|
|
81
|
+
partNumbers: multipartPlan.partNumbers
|
|
82
|
+
}
|
|
83
|
+
}), {
|
|
84
|
+
partSize: multipartPlan.partSizeBytes,
|
|
85
|
+
totalParts: multipartPlan.totalParts
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return mapUploadResponse(await runtime.uploads.request({
|
|
89
|
+
bucket: bucketName,
|
|
90
|
+
...mapRawUploadRequest(bucketType, fileInfo, autoSignedUrls)
|
|
91
|
+
}));
|
|
92
|
+
},
|
|
93
|
+
multipart: {
|
|
94
|
+
requestParts: async ({ multipart })=>{
|
|
95
|
+
const res = await runtime.uploads.createParts({
|
|
96
|
+
uploadId: multipart.uploadId,
|
|
97
|
+
partNumbers: multipart.parts
|
|
98
|
+
});
|
|
84
99
|
return {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
100
|
+
multipart: {
|
|
101
|
+
uploadId: multipart.uploadId,
|
|
102
|
+
parts: res.parts.map((part)=>({
|
|
103
|
+
partNumber: part.partNumber,
|
|
104
|
+
uploadUrl: part.signedUrl
|
|
105
|
+
}))
|
|
106
|
+
}
|
|
89
107
|
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
108
|
+
},
|
|
109
|
+
complete: async ({ uploadId, parts })=>{
|
|
110
|
+
await runtime.uploads.completeMultipart({
|
|
111
|
+
uploadId,
|
|
112
|
+
parts
|
|
94
113
|
});
|
|
95
114
|
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
115
|
+
},
|
|
116
|
+
upload: async ({ bucketName, fileInfo, autoSignedUrls, source, signal, onProgress })=>{
|
|
117
|
+
const result = await runtime.uploads.upload({
|
|
118
|
+
bucket: bucketName,
|
|
119
|
+
source,
|
|
120
|
+
...mapHighLevelUploadOptions(fileInfo, autoSignedUrls),
|
|
121
|
+
signal,
|
|
122
|
+
onProgress
|
|
123
|
+
});
|
|
104
124
|
return {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
125
|
+
file: mapFile(result.file),
|
|
126
|
+
signedReadUrl: result.signedReadUrl ? {
|
|
127
|
+
...result.signedReadUrl,
|
|
128
|
+
expiresAt: new Date(result.signedReadUrl.expiresAt)
|
|
129
|
+
} : undefined
|
|
109
130
|
};
|
|
110
131
|
}
|
|
111
|
-
throw new shared$1.EdgeStoreError({
|
|
112
|
-
message: 'Could not get upload url',
|
|
113
|
-
code: 'SERVER_ERROR'
|
|
114
|
-
});
|
|
115
132
|
},
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
133
|
+
files: {
|
|
134
|
+
cursorSchema: z.string(),
|
|
135
|
+
get: async ({ bucketName, file: fileRef })=>{
|
|
136
|
+
const { file } = await runtime.files.lookup({
|
|
137
|
+
bucketName,
|
|
138
|
+
file: fileRef
|
|
139
|
+
});
|
|
140
|
+
return mapFile(file);
|
|
141
|
+
},
|
|
142
|
+
list: async ({ bucketName, filter, cursor, limit })=>{
|
|
143
|
+
const { files, pagination } = await runtime.files.search({
|
|
144
|
+
bucket: bucketName,
|
|
145
|
+
filter,
|
|
146
|
+
pagination: {
|
|
147
|
+
cursor,
|
|
148
|
+
limit
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
return {
|
|
152
|
+
items: files.map(mapFile),
|
|
153
|
+
...pagination
|
|
154
|
+
};
|
|
155
|
+
},
|
|
156
|
+
confirm: async ({ bucketName, files })=>mapMutationResult(await runtime.files.confirmMany({
|
|
157
|
+
bucketName,
|
|
158
|
+
files
|
|
159
|
+
})),
|
|
160
|
+
delete: async ({ bucketName, files })=>mapMutationResult(await runtime.files.deleteMany({
|
|
161
|
+
bucketName,
|
|
162
|
+
files
|
|
163
|
+
})),
|
|
164
|
+
restore: async ({ bucketName, files })=>mapMutationResult(await runtime.files.restoreMany({
|
|
165
|
+
bucketName,
|
|
166
|
+
files
|
|
167
|
+
})),
|
|
168
|
+
getSignedUrls: async (params)=>{
|
|
169
|
+
const urls = await Promise.all(params.files.map(async (fileRef)=>'url' in fileRef ? fileRef.url : (await runtime.files.lookup({
|
|
170
|
+
bucketName: params.bucketName,
|
|
171
|
+
file: fileRef
|
|
172
|
+
})).file.url));
|
|
173
|
+
const { signedUrls } = await runtime.files.generateSignedReadUrls({
|
|
174
|
+
bucket: params.bucketName,
|
|
175
|
+
urls,
|
|
176
|
+
expiresIn: params.expiresIn,
|
|
177
|
+
includeThumbnails: params.includeThumbnails
|
|
178
|
+
});
|
|
179
|
+
return signedUrls.map((item)=>({
|
|
180
|
+
...item,
|
|
181
|
+
expiresAt: new Date(item.expiresAt)
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
return provider;
|
|
187
|
+
}
|
|
188
|
+
function createProviderRuntime(options) {
|
|
189
|
+
const authentication = resolveProviderAuthentication(options);
|
|
190
|
+
const apiUrl = options?.apiUrl ?? getApiUrl();
|
|
191
|
+
if (authentication.kind === 'bearer') {
|
|
192
|
+
const sdk = createEdgeStoreSdk({
|
|
193
|
+
credentials: {
|
|
194
|
+
token: authentication.token
|
|
195
|
+
},
|
|
196
|
+
apiUrl
|
|
197
|
+
});
|
|
198
|
+
return sdk.runtime.forProject(authentication.project);
|
|
199
|
+
}
|
|
200
|
+
const sdk = createEdgeStoreSdk({
|
|
201
|
+
credentials: {
|
|
202
|
+
accessKey: authentication.accessKey,
|
|
203
|
+
secretKey: authentication.secretKey
|
|
149
204
|
},
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
205
|
+
apiUrl
|
|
206
|
+
});
|
|
207
|
+
return sdk.runtime;
|
|
208
|
+
}
|
|
209
|
+
function resolveProviderAuthentication(options) {
|
|
210
|
+
const values = options ?? {};
|
|
211
|
+
const token = values.token;
|
|
212
|
+
const project = values.project;
|
|
213
|
+
const configuredAccessKey = values.accessKey;
|
|
214
|
+
const configuredSecretKey = values.secretKey;
|
|
215
|
+
if (token !== undefined) {
|
|
216
|
+
if (configuredAccessKey !== undefined || configuredSecretKey !== undefined) {
|
|
217
|
+
throw new TypeError('EdgeStore provider credentials cannot contain both a Bearer token and project keys.');
|
|
218
|
+
}
|
|
219
|
+
assertNonEmptyProviderOption(token, 'token');
|
|
220
|
+
assertNonEmptyProviderOption(project, 'project');
|
|
221
|
+
return {
|
|
222
|
+
kind: 'bearer',
|
|
223
|
+
token,
|
|
224
|
+
project
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
if (project !== undefined) {
|
|
228
|
+
throw new TypeError('EdgeStore provider option `project` requires a Bearer token.');
|
|
229
|
+
}
|
|
230
|
+
const accessKey = configuredAccessKey ?? getEnv('EDGE_STORE_ACCESS_KEY') ?? // @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
231
|
+
import.meta.env?.EDGE_STORE_ACCESS_KEY;
|
|
232
|
+
const secretKey = configuredSecretKey ?? getEnv('EDGE_STORE_SECRET_KEY') ?? // @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
233
|
+
import.meta.env?.EDGE_STORE_SECRET_KEY;
|
|
234
|
+
if (typeof accessKey !== 'string' || !accessKey.trim() || typeof secretKey !== 'string' || !secretKey.trim()) {
|
|
235
|
+
throw new EdgeStoreCredentialsError();
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
kind: 'project',
|
|
239
|
+
accessKey,
|
|
240
|
+
secretKey
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function assertNonEmptyProviderOption(value, name) {
|
|
244
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
245
|
+
throw new TypeError(`EdgeStore provider option \`${name}\` must not be empty.`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function mapMutationResult(result) {
|
|
249
|
+
return {
|
|
250
|
+
results: result.results.map((item)=>item.success ? {
|
|
251
|
+
success: true
|
|
252
|
+
} : {
|
|
253
|
+
success: false,
|
|
254
|
+
error: item.error
|
|
255
|
+
})
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
function getApiUrl() {
|
|
259
|
+
const configured = getEnv('EDGE_STORE_API_ENDPOINT');
|
|
260
|
+
if (!configured) return undefined;
|
|
261
|
+
const base = configured.replace(/\/+$/, '');
|
|
262
|
+
return base.endsWith('/v2') ? base : `${base}/v2`;
|
|
263
|
+
}
|
|
264
|
+
function normalizeMetadata(metadata) {
|
|
265
|
+
return Object.fromEntries(Object.entries(metadata).filter((entry)=>entry[1] != null));
|
|
266
|
+
}
|
|
267
|
+
function mapHighLevelUploadOptions(fileInfo, signedReadUrl) {
|
|
268
|
+
return {
|
|
269
|
+
fileName: fileInfo.fileName,
|
|
270
|
+
mimeType: fileInfo.type,
|
|
271
|
+
temporary: fileInfo.temporary,
|
|
272
|
+
path: fileInfo.path,
|
|
273
|
+
extension: fileInfo.extension,
|
|
274
|
+
metadata: normalizeMetadata(fileInfo.metadata),
|
|
275
|
+
replaceTarget: fileInfo.replaceTargetUrl ? {
|
|
276
|
+
url: fileInfo.replaceTargetUrl
|
|
277
|
+
} : undefined,
|
|
278
|
+
signedReadUrl
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
function mapRawUploadRequest(bucketType, fileInfo, signedReadUrl) {
|
|
282
|
+
return {
|
|
283
|
+
bucketType: bucketType.toLowerCase(),
|
|
284
|
+
visibility: fileInfo.isPublic ? 'public' : 'protected',
|
|
285
|
+
sizeBytes: fileInfo.size,
|
|
286
|
+
...mapHighLevelUploadOptions(fileInfo, signedReadUrl)
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
function mapUploadResponse(res, multipartConfig) {
|
|
290
|
+
const signed = res.signedReadUrl;
|
|
291
|
+
const access = {
|
|
292
|
+
accessUrl: res.file.url,
|
|
293
|
+
thumbnailUrl: res.file.thumbnailUrl,
|
|
294
|
+
accessSignedUrl: signed?.signedUrl,
|
|
295
|
+
accessSignedThumbnailUrl: signed?.signedThumbnailUrl,
|
|
296
|
+
accessSignedUrlExpiresAt: signed?.expiresAt,
|
|
297
|
+
accessSignedUrlExpiresIn: signed?.expiresIn
|
|
298
|
+
};
|
|
299
|
+
if (res.upload.kind === 'single') {
|
|
300
|
+
return {
|
|
301
|
+
...access,
|
|
302
|
+
uploadUrl: res.upload.signedUrl
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
if (!multipartConfig) {
|
|
306
|
+
throw new EdgeStoreError({
|
|
307
|
+
message: 'Missing multipart configuration.',
|
|
308
|
+
code: 'SERVER_ERROR'
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
...access,
|
|
313
|
+
multipart: {
|
|
314
|
+
key: res.file.key,
|
|
315
|
+
uploadId: res.upload.id,
|
|
316
|
+
parts: res.upload.parts.map((part)=>({
|
|
317
|
+
partNumber: part.partNumber,
|
|
318
|
+
uploadUrl: part.signedUrl
|
|
319
|
+
})),
|
|
320
|
+
...multipartConfig
|
|
154
321
|
}
|
|
155
322
|
};
|
|
156
323
|
}
|
|
157
|
-
function
|
|
324
|
+
function mapFile(file) {
|
|
158
325
|
return {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
accessSignedUrlExpiresIn: res.accessSignedUrlExpiresIn
|
|
326
|
+
...file,
|
|
327
|
+
uploadedAt: new Date(file.uploadedAt),
|
|
328
|
+
updatedAt: new Date(file.updatedAt)
|
|
163
329
|
};
|
|
164
330
|
}
|
|
165
331
|
|
|
166
|
-
|
|
332
|
+
export { edgestore };
|