@edgestore/server 1.0.0-next.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/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 +119 -140
- 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 +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 +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 +13 -12
- 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 +480 -0
- package/src/core/client/index.ts +300 -427
- 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/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,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,164 +1,332 @@
|
|
|
1
|
+
import { planMultipartUpload, createEdgeStoreSdk } from '@edgestore/sdk';
|
|
1
2
|
import { EdgeStoreError } from '@edgestore/shared';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { defineProvider } from '../../core/provider.js';
|
|
5
|
+
import { getEnv } from '../../libs/env.js';
|
|
4
6
|
import EdgeStoreCredentialsError from '../../libs/errors/EdgeStoreCredentialsError.js';
|
|
5
7
|
|
|
6
8
|
const DEFAULT_BASE_URL = 'https://files.edgestore.dev';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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) {
|
|
11
24
|
const baseUrl = getEnv('EDGE_STORE_BASE_URL') ?? DEFAULT_BASE_URL;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
const edgeStoreSdk = initEdgeStoreSdk({
|
|
16
|
-
accessKey,
|
|
17
|
-
secretKey
|
|
18
|
-
});
|
|
19
|
-
return {
|
|
25
|
+
const runtime = createProviderRuntime(options);
|
|
26
|
+
const provider = defineProvider({
|
|
20
27
|
name: 'edgestore',
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
token
|
|
28
|
-
};
|
|
29
|
-
},
|
|
30
|
-
getBaseUrl () {
|
|
31
|
-
return baseUrl;
|
|
28
|
+
baseUrl,
|
|
29
|
+
reference: {
|
|
30
|
+
schema: fileReferenceSchema,
|
|
31
|
+
fromUrl: (url)=>({
|
|
32
|
+
url
|
|
33
|
+
})
|
|
32
34
|
},
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
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
|
+
]))
|
|
36
60
|
});
|
|
37
61
|
return {
|
|
38
|
-
|
|
39
|
-
|
|
62
|
+
token,
|
|
63
|
+
clientInit: {
|
|
64
|
+
path: '/_init',
|
|
65
|
+
headers: {
|
|
66
|
+
'x-edgestore-token': token
|
|
67
|
+
}
|
|
68
|
+
}
|
|
40
69
|
};
|
|
41
70
|
},
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (fileInfo.size > MULTIPART_THRESHOLD) {
|
|
47
|
-
let totalParts = Math.ceil(fileInfo.size / partSize);
|
|
48
|
-
if (totalParts > 1000) {
|
|
49
|
-
// the maximum number of parts is 1000
|
|
50
|
-
totalParts = 1000;
|
|
51
|
-
partSize = Math.ceil(fileInfo.size / totalParts);
|
|
52
|
-
}
|
|
53
|
-
const res = await edgeStoreSdk.requestUpload({
|
|
54
|
-
bucketName,
|
|
55
|
-
bucketType,
|
|
56
|
-
fileInfo,
|
|
57
|
-
autoSignedUrls,
|
|
58
|
-
multipart: {
|
|
59
|
-
parts: Array.from({
|
|
60
|
-
length: totalParts
|
|
61
|
-
}).map((_, index)=>index + 1)
|
|
62
|
-
}
|
|
71
|
+
uploads: {
|
|
72
|
+
async request ({ bucketName, bucketType, fileInfo, autoSignedUrls }) {
|
|
73
|
+
const multipartPlan = planMultipartUpload({
|
|
74
|
+
sizeBytes: fileInfo.size
|
|
63
75
|
});
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
});
|
|
82
99
|
return {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
100
|
+
multipart: {
|
|
101
|
+
uploadId: multipart.uploadId,
|
|
102
|
+
parts: res.parts.map((part)=>({
|
|
103
|
+
partNumber: part.partNumber,
|
|
104
|
+
uploadUrl: part.signedUrl
|
|
105
|
+
}))
|
|
106
|
+
}
|
|
87
107
|
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
108
|
+
},
|
|
109
|
+
complete: async ({ uploadId, parts })=>{
|
|
110
|
+
await runtime.uploads.completeMultipart({
|
|
111
|
+
uploadId,
|
|
112
|
+
parts
|
|
92
113
|
});
|
|
93
114
|
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
+
});
|
|
102
124
|
return {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
125
|
+
file: mapFile(result.file),
|
|
126
|
+
signedReadUrl: result.signedReadUrl ? {
|
|
127
|
+
...result.signedReadUrl,
|
|
128
|
+
expiresAt: new Date(result.signedReadUrl.expiresAt)
|
|
129
|
+
} : undefined
|
|
107
130
|
};
|
|
108
131
|
}
|
|
109
|
-
throw new EdgeStoreError({
|
|
110
|
-
message: 'Could not get upload url',
|
|
111
|
-
code: 'SERVER_ERROR'
|
|
112
|
-
});
|
|
113
|
-
},
|
|
114
|
-
requestUploadParts: async ({ multipart, path })=>{
|
|
115
|
-
const res = await edgeStoreSdk.requestUploadParts({
|
|
116
|
-
multipart,
|
|
117
|
-
key: path
|
|
118
|
-
});
|
|
119
|
-
return {
|
|
120
|
-
multipart: {
|
|
121
|
-
uploadId: res.multipart.uploadId,
|
|
122
|
-
parts: res.multipart.parts.map((part)=>({
|
|
123
|
-
partNumber: part.partNumber,
|
|
124
|
-
uploadUrl: part.signedUrl
|
|
125
|
-
}))
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
},
|
|
129
|
-
getSignedUrls: async (params)=>{
|
|
130
|
-
const res = await edgeStoreSdk.getSignedUrls(params);
|
|
131
|
-
return res.map((item)=>({
|
|
132
|
-
...item,
|
|
133
|
-
expiresAt: new Date(item.expiresAt)
|
|
134
|
-
}));
|
|
135
|
-
},
|
|
136
|
-
completeMultipartUpload: async ({ uploadId, key, parts })=>{
|
|
137
|
-
return await edgeStoreSdk.completeMultipartUpload({
|
|
138
|
-
uploadId,
|
|
139
|
-
key,
|
|
140
|
-
parts
|
|
141
|
-
});
|
|
142
132
|
},
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
|
147
204
|
},
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
|
152
321
|
}
|
|
153
322
|
};
|
|
154
323
|
}
|
|
155
|
-
function
|
|
324
|
+
function mapFile(file) {
|
|
156
325
|
return {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
accessSignedUrlExpiresIn: res.accessSignedUrlExpiresIn
|
|
326
|
+
...file,
|
|
327
|
+
uploadedAt: new Date(file.uploadedAt),
|
|
328
|
+
updatedAt: new Date(file.updatedAt)
|
|
161
329
|
};
|
|
162
330
|
}
|
|
163
331
|
|
|
164
|
-
export {
|
|
332
|
+
export { edgestore };
|