@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,180 +1,25 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
1
|
import Logger from '../../libs/logger.js';
|
|
3
|
-
import {
|
|
4
|
-
import { EdgeStoreProvider } from '../../providers/edgestore/index.js';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../shared.js';
|
|
2
|
+
import { dispatchEdgeStoreRequest, resolveContext } from '../dispatcher.js';
|
|
6
3
|
|
|
7
|
-
// Helper to extract a cookie from the request's cookie header
|
|
8
|
-
function getCookie(req, cookieName) {
|
|
9
|
-
const cookieHeader = req.headers.get('cookie');
|
|
10
|
-
if (!cookieHeader) return undefined;
|
|
11
|
-
return cookieHeader.split(';').map((cookieStr)=>cookieStr.trim()).reduce((acc, cookieStr)=>{
|
|
12
|
-
const [name, ...rest] = cookieStr.split('=');
|
|
13
|
-
if (name && rest.length > 0) {
|
|
14
|
-
acc[name] = rest.join('=');
|
|
15
|
-
}
|
|
16
|
-
return acc;
|
|
17
|
-
}, {})[cookieName];
|
|
18
|
-
}
|
|
19
4
|
function createEdgeStoreStartHandler(config) {
|
|
20
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
21
5
|
const log = new Logger(config.logLevel);
|
|
22
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
23
6
|
log.debug('Creating EdgeStore TanStack Start handler');
|
|
24
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
25
7
|
return async ({ request })=>{
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
8
|
+
const url = new URL(request.url);
|
|
9
|
+
return await dispatchEdgeStoreRequest({
|
|
10
|
+
edgestore: config.edgestore,
|
|
11
|
+
logger: log,
|
|
12
|
+
cookieConfig: config.cookieConfig,
|
|
13
|
+
request: {
|
|
14
|
+
pathname: url.pathname,
|
|
15
|
+
readJson: ()=>request.json(),
|
|
16
|
+
getQuery: (name)=>url.searchParams.get(name) ?? undefined,
|
|
17
|
+
cookieHeader: request.headers.get('cookie') ?? undefined,
|
|
18
|
+
createContext: ()=>resolveContext(config, {
|
|
36
19
|
req: request
|
|
37
|
-
})
|
|
38
|
-
} catch (err) {
|
|
39
|
-
throw new EdgeStoreError({
|
|
40
|
-
message: 'Error creating context',
|
|
41
|
-
code: 'CREATE_CONTEXT_ERROR',
|
|
42
|
-
cause: err instanceof Error ? err : undefined
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
const { newCookies, ...body } = await init({
|
|
46
|
-
ctx,
|
|
47
|
-
provider,
|
|
48
|
-
router: config.router,
|
|
49
|
-
cookieConfig
|
|
50
|
-
});
|
|
51
|
-
const headers = new Headers();
|
|
52
|
-
newCookies.forEach((cookie)=>{
|
|
53
|
-
headers.append('Set-Cookie', cookie);
|
|
54
|
-
});
|
|
55
|
-
headers.set('Content-Type', 'application/json');
|
|
56
|
-
return new Response(JSON.stringify(body), {
|
|
57
|
-
status: 200,
|
|
58
|
-
headers
|
|
59
|
-
});
|
|
60
|
-
} else if (matchPath(pathname, '/request-upload')) {
|
|
61
|
-
const body = await request.json();
|
|
62
|
-
const ctxToken = getCookie(request, resolvedCookieConfig.ctx.name);
|
|
63
|
-
const result = await requestUpload({
|
|
64
|
-
provider,
|
|
65
|
-
router: config.router,
|
|
66
|
-
body: body,
|
|
67
|
-
ctxToken
|
|
68
|
-
});
|
|
69
|
-
return new Response(JSON.stringify(result), {
|
|
70
|
-
status: 200,
|
|
71
|
-
headers: {
|
|
72
|
-
'Content-Type': 'application/json'
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
} else if (matchPath(pathname, '/request-upload-parts')) {
|
|
76
|
-
const body = await request.json();
|
|
77
|
-
const ctxToken = getCookie(request, resolvedCookieConfig.ctx.name);
|
|
78
|
-
const result = await requestUploadParts({
|
|
79
|
-
provider,
|
|
80
|
-
router: config.router,
|
|
81
|
-
body: body,
|
|
82
|
-
ctxToken
|
|
83
|
-
});
|
|
84
|
-
return new Response(JSON.stringify(result), {
|
|
85
|
-
status: 200,
|
|
86
|
-
headers: {
|
|
87
|
-
'Content-Type': 'application/json'
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
} else if (matchPath(pathname, '/complete-multipart-upload')) {
|
|
91
|
-
const body = await request.json();
|
|
92
|
-
const ctxToken = getCookie(request, resolvedCookieConfig.ctx.name);
|
|
93
|
-
await completeMultipartUpload({
|
|
94
|
-
provider,
|
|
95
|
-
router: config.router,
|
|
96
|
-
body: body,
|
|
97
|
-
ctxToken
|
|
98
|
-
});
|
|
99
|
-
return new Response(null, {
|
|
100
|
-
status: 200
|
|
101
|
-
});
|
|
102
|
-
} else if (matchPath(pathname, '/confirm-upload')) {
|
|
103
|
-
const body = await request.json();
|
|
104
|
-
const ctxToken = getCookie(request, resolvedCookieConfig.ctx.name);
|
|
105
|
-
const result = await confirmUpload({
|
|
106
|
-
provider,
|
|
107
|
-
router: config.router,
|
|
108
|
-
body: body,
|
|
109
|
-
ctxToken
|
|
110
|
-
});
|
|
111
|
-
return new Response(JSON.stringify(result), {
|
|
112
|
-
status: 200,
|
|
113
|
-
headers: {
|
|
114
|
-
'Content-Type': 'application/json'
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
} else if (matchPath(pathname, '/delete-file')) {
|
|
118
|
-
const body = await request.json();
|
|
119
|
-
const ctxToken = getCookie(request, resolvedCookieConfig.ctx.name);
|
|
120
|
-
const result = await deleteFile({
|
|
121
|
-
provider,
|
|
122
|
-
router: config.router,
|
|
123
|
-
body: body,
|
|
124
|
-
ctxToken
|
|
125
|
-
});
|
|
126
|
-
return new Response(JSON.stringify(result), {
|
|
127
|
-
status: 200,
|
|
128
|
-
headers: {
|
|
129
|
-
'Content-Type': 'application/json'
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
} else if (matchPath(pathname, '/proxy-file')) {
|
|
133
|
-
const urlParam = new URL(request.url).searchParams.get('url');
|
|
134
|
-
if (typeof urlParam === 'string') {
|
|
135
|
-
const proxyRes = await fetchProxyFile({
|
|
136
|
-
cookieHeader: request.headers.get('cookie') ?? undefined,
|
|
137
|
-
url: urlParam
|
|
138
|
-
});
|
|
139
|
-
const headers = new Headers();
|
|
140
|
-
headers.set('Content-Type', proxyRes.contentType);
|
|
141
|
-
return new Response(proxyRes.body, {
|
|
142
|
-
headers,
|
|
143
|
-
status: proxyRes.status
|
|
144
|
-
});
|
|
145
|
-
} else {
|
|
146
|
-
return new Response(null, {
|
|
147
|
-
status: 400
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
} else {
|
|
151
|
-
return new Response(null, {
|
|
152
|
-
status: 404
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
} catch (err) {
|
|
156
|
-
if (err instanceof EdgeStoreError) {
|
|
157
|
-
log[err.level](err.formattedMessage());
|
|
158
|
-
if (err.cause) log[err.level](err.cause);
|
|
159
|
-
const status = EDGE_STORE_ERROR_CODES[err.code] || 500;
|
|
160
|
-
return new Response(JSON.stringify(err.formattedJson()), {
|
|
161
|
-
status,
|
|
162
|
-
headers: {
|
|
163
|
-
'Content-Type': 'application/json'
|
|
164
|
-
}
|
|
165
|
-
});
|
|
20
|
+
})
|
|
166
21
|
}
|
|
167
|
-
|
|
168
|
-
return new Response(JSON.stringify(new EdgeStoreError({
|
|
169
|
-
message: 'Internal server error',
|
|
170
|
-
code: 'SERVER_ERROR'
|
|
171
|
-
}).formattedJson()), {
|
|
172
|
-
status: 500,
|
|
173
|
-
headers: {
|
|
174
|
-
'Content-Type': 'application/json'
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
22
|
+
});
|
|
178
23
|
};
|
|
179
24
|
}
|
|
180
25
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type AnyEdgeStoreProvider, type AnyRouter } from '@edgestore/shared';
|
|
2
|
+
import type { BucketClient } from '.';
|
|
3
|
+
export declare function createBucketClient<TRouter extends AnyRouter, TName extends keyof TRouter['buckets'] & string, TProvider extends AnyEdgeStoreProvider>(router: TRouter, bucketName: TName, options: {
|
|
4
|
+
provider: TProvider;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
}): BucketClient<TRouter['buckets'][TName], TProvider>;
|
|
7
|
+
//# sourceMappingURL=bucketClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bucketClient.d.ts","sourceRoot":"","sources":["../../../src/core/client/bucketClient.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,oBAAoB,EACzB,KAAK,SAAS,EAWf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EACV,YAAY,EAQb,MAAM,GAAG,CAAC;AAsBX,wBAAgB,kBAAkB,CAChC,OAAO,SAAS,SAAS,EACzB,KAAK,SAAS,MAAM,OAAO,CAAC,SAAS,CAAC,GAAG,MAAM,EAC/C,SAAS,SAAS,oBAAoB,EAEtC,MAAM,EAAE,OAAO,EACf,UAAU,EAAE,KAAK,EACjB,OAAO,EAAE;IACP,QAAQ,EAAE,SAAS,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GACA,YAAY,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,CA0BpD"}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { EdgeStoreFileMutationError } from '@edgestore/sdk';
|
|
2
|
+
import { isDev } from '../../libs/env.js';
|
|
3
|
+
import { validateProviderReference, validateProviderCursor } from '../provider.js';
|
|
4
|
+
import { parseBucketInput, buildPath, parsePath } from '../routerRules.js';
|
|
5
|
+
import { validateFileForBucket } from '../validateFile.js';
|
|
6
|
+
|
|
7
|
+
function createBucketClient(router, bucketName, options) {
|
|
8
|
+
const context = {
|
|
9
|
+
bucket: router.buckets[bucketName],
|
|
10
|
+
bucketName,
|
|
11
|
+
...options
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
...createUploadMethods(context),
|
|
15
|
+
...createGetMethods(context),
|
|
16
|
+
...createMutationMethods(context, context.provider.files.confirm, {
|
|
17
|
+
single: 'confirm',
|
|
18
|
+
many: 'confirmMany'
|
|
19
|
+
}),
|
|
20
|
+
...createMutationMethods(context, context.provider.files.delete, {
|
|
21
|
+
single: 'delete',
|
|
22
|
+
many: 'deleteMany'
|
|
23
|
+
}),
|
|
24
|
+
...createMutationMethods(context, context.provider.files.restore, {
|
|
25
|
+
single: 'restore',
|
|
26
|
+
many: 'restoreMany'
|
|
27
|
+
}),
|
|
28
|
+
...createListMethods(context),
|
|
29
|
+
...createSignedUrlMethods(context)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function createUploadMethods(context) {
|
|
33
|
+
const upload = context.provider.uploads.upload;
|
|
34
|
+
if (!upload) return {};
|
|
35
|
+
return {
|
|
36
|
+
upload: async (params)=>{
|
|
37
|
+
const { content, ctx = {}, input } = params;
|
|
38
|
+
let { blob, extension } = await resolveUploadContent(content, params.signal);
|
|
39
|
+
if (params.options?.transform) {
|
|
40
|
+
const transformed = await params.options.transform({
|
|
41
|
+
blob,
|
|
42
|
+
extension,
|
|
43
|
+
type: blob.type
|
|
44
|
+
});
|
|
45
|
+
blob = transformed.blob;
|
|
46
|
+
extension = transformed.extension;
|
|
47
|
+
}
|
|
48
|
+
const parsedInput = await parseBucketInput(context.bucket, input);
|
|
49
|
+
validateFileForBucket({
|
|
50
|
+
bucket: context.bucket,
|
|
51
|
+
fileInfo: {
|
|
52
|
+
size: blob.size,
|
|
53
|
+
type: blob.type
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
const path = buildPath({
|
|
57
|
+
bucket: context.bucket,
|
|
58
|
+
pathAttrs: {
|
|
59
|
+
ctx,
|
|
60
|
+
input: parsedInput
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
const metadata = await context.bucket._def.metadata?.({
|
|
64
|
+
ctx,
|
|
65
|
+
input: parsedInput
|
|
66
|
+
}) ?? {};
|
|
67
|
+
const uploadResult = await upload({
|
|
68
|
+
bucketName: context.bucketName,
|
|
69
|
+
bucketType: context.bucket._def.type,
|
|
70
|
+
autoSignedUrls: context.bucket._def.autoSignedUrls,
|
|
71
|
+
source: blob,
|
|
72
|
+
signal: params.signal,
|
|
73
|
+
onProgress: params.onProgress,
|
|
74
|
+
fileInfo: {
|
|
75
|
+
fileName: params.options?.manualFileName,
|
|
76
|
+
replaceTargetUrl: params.options?.replaceTargetUrl,
|
|
77
|
+
type: blob.type,
|
|
78
|
+
size: blob.size,
|
|
79
|
+
extension,
|
|
80
|
+
isPublic: context.bucket._def.accessControl === undefined,
|
|
81
|
+
temporary: params.options?.temporary ?? false,
|
|
82
|
+
path,
|
|
83
|
+
metadata: normalizeMetadata(metadata)
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
const { parsedPath, pathOrder } = parsePath(path);
|
|
87
|
+
return {
|
|
88
|
+
...mapFileRecord(uploadResult.file, context.baseUrl),
|
|
89
|
+
...mapSignedReadAccess(uploadResult.signedReadUrl),
|
|
90
|
+
metadata,
|
|
91
|
+
path: parsedPath,
|
|
92
|
+
pathOrder
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function createGetMethods(context) {
|
|
98
|
+
const getFile = context.provider.files.get;
|
|
99
|
+
if (!getFile) return {};
|
|
100
|
+
return {
|
|
101
|
+
get: async (ref)=>{
|
|
102
|
+
const file = await getFile({
|
|
103
|
+
bucketName: context.bucketName,
|
|
104
|
+
file: await validateProviderReference(context.provider, ref)
|
|
105
|
+
});
|
|
106
|
+
return mapBucketFileRecord(file, context.baseUrl);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function createMutationMethods(context, operation, names) {
|
|
111
|
+
if (!operation) return {};
|
|
112
|
+
return {
|
|
113
|
+
[names.single]: async (ref)=>{
|
|
114
|
+
const file = await validateProviderReference(context.provider, ref);
|
|
115
|
+
return requireMutationSuccess(file, await operation({
|
|
116
|
+
bucketName: context.bucketName,
|
|
117
|
+
files: [
|
|
118
|
+
file
|
|
119
|
+
]
|
|
120
|
+
}));
|
|
121
|
+
},
|
|
122
|
+
[names.many]: async ({ refs })=>{
|
|
123
|
+
const files = await Promise.all(refs.map((ref)=>validateProviderReference(context.provider, ref)));
|
|
124
|
+
return mapMutationResult(files, await operation({
|
|
125
|
+
bucketName: context.bucketName,
|
|
126
|
+
files
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function createListMethods(context) {
|
|
132
|
+
const listFiles = context.provider.files.list;
|
|
133
|
+
if (!listFiles) return {};
|
|
134
|
+
const listBucketFiles = async (params)=>{
|
|
135
|
+
const result = await listFiles({
|
|
136
|
+
bucketName: context.bucketName,
|
|
137
|
+
filter: serializeFilter(params?.filter),
|
|
138
|
+
cursor: params?.cursor === undefined ? undefined : await validateProviderCursor(context.provider, params.cursor),
|
|
139
|
+
limit: params?.limit
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
...result,
|
|
143
|
+
items: result.items.map((file)=>mapBucketFileRecord(file, context.baseUrl))
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
return {
|
|
147
|
+
list: listBucketFiles
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function createSignedUrlMethods(context) {
|
|
151
|
+
const getSignedUrls = context.provider.files.getSignedUrls;
|
|
152
|
+
if (!getSignedUrls || context.bucket._def.accessControl === undefined) {
|
|
153
|
+
return {};
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
createSignedUrl: async (params)=>{
|
|
157
|
+
const file = await validateProviderReference(context.provider, params.url);
|
|
158
|
+
const [signedUrl] = await getSignedUrls({
|
|
159
|
+
bucketName: context.bucketName,
|
|
160
|
+
files: [
|
|
161
|
+
file
|
|
162
|
+
],
|
|
163
|
+
expiresIn: params.expiresIn
|
|
164
|
+
});
|
|
165
|
+
if (!signedUrl) {
|
|
166
|
+
throw new Error('Missing signed URL response');
|
|
167
|
+
}
|
|
168
|
+
return mapSignedUrl(signedUrl);
|
|
169
|
+
},
|
|
170
|
+
createSignedUrls: async (params)=>{
|
|
171
|
+
const files = await Promise.all(params.urls.map((url)=>validateProviderReference(context.provider, url)));
|
|
172
|
+
const signedUrls = await getSignedUrls({
|
|
173
|
+
bucketName: context.bucketName,
|
|
174
|
+
files,
|
|
175
|
+
expiresIn: params.expiresIn,
|
|
176
|
+
includeThumbnails: params.includeThumbnails
|
|
177
|
+
});
|
|
178
|
+
return signedUrls.map(mapSignedUrl);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async function resolveUploadContent(content, signal) {
|
|
183
|
+
if (typeof content === 'string') {
|
|
184
|
+
return {
|
|
185
|
+
blob: new Blob([
|
|
186
|
+
content
|
|
187
|
+
], {
|
|
188
|
+
type: 'text/plain'
|
|
189
|
+
}),
|
|
190
|
+
extension: 'txt'
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
if ('blob' in content) {
|
|
194
|
+
return {
|
|
195
|
+
blob: content.blob,
|
|
196
|
+
extension: content.extension
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
blob: await getBlobFromUrl(content.url, signal),
|
|
201
|
+
extension: content.extension
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
async function getBlobFromUrl(url, signal) {
|
|
205
|
+
const response = await fetch(url, {
|
|
206
|
+
signal
|
|
207
|
+
});
|
|
208
|
+
if (!response.ok) {
|
|
209
|
+
throw new Error(`Could not fetch upload source: HTTP ${response.status}`);
|
|
210
|
+
}
|
|
211
|
+
return await response.blob();
|
|
212
|
+
}
|
|
213
|
+
function normalizeMetadata(metadata) {
|
|
214
|
+
return Object.fromEntries(Object.entries(metadata).filter((entry)=>entry[1] != null));
|
|
215
|
+
}
|
|
216
|
+
function serializeFilter(filter) {
|
|
217
|
+
if (!filter) return undefined;
|
|
218
|
+
return {
|
|
219
|
+
uploadedAt: serializeComparison(filter.uploadedAt),
|
|
220
|
+
path: serializeStringComparisons(filter.path),
|
|
221
|
+
metadata: serializeStringComparisons(filter.metadata),
|
|
222
|
+
AND: filter.AND?.map((item)=>serializeFilter(item)),
|
|
223
|
+
OR: filter.OR?.map((item)=>serializeFilter(item))
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function serializeComparison(value) {
|
|
227
|
+
if (value instanceof Date) return value.toISOString();
|
|
228
|
+
if (!value || typeof value !== 'object') return value;
|
|
229
|
+
return Object.fromEntries(Object.entries(value).map(([key, item])=>[
|
|
230
|
+
key,
|
|
231
|
+
Array.isArray(item) ? item.map((date)=>date.toISOString()) : item?.toISOString()
|
|
232
|
+
]));
|
|
233
|
+
}
|
|
234
|
+
function serializeStringComparisons(value) {
|
|
235
|
+
if (!value) return undefined;
|
|
236
|
+
return Object.fromEntries(Object.entries(value).filter((entry)=>entry[1] !== undefined));
|
|
237
|
+
}
|
|
238
|
+
function mapSignedReadAccess(signedReadUrl) {
|
|
239
|
+
if (!signedReadUrl) return {};
|
|
240
|
+
return mapSignedUrl(signedReadUrl);
|
|
241
|
+
}
|
|
242
|
+
function mapSignedUrl(signedUrl) {
|
|
243
|
+
return {
|
|
244
|
+
...signedUrl,
|
|
245
|
+
expiresAt: new Date(signedUrl.expiresAt)
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function mapFileRecord(file, baseUrl) {
|
|
249
|
+
return {
|
|
250
|
+
...file,
|
|
251
|
+
url: getUrl(file.url, baseUrl),
|
|
252
|
+
uploadedAt: new Date(file.uploadedAt),
|
|
253
|
+
updatedAt: new Date(file.updatedAt)
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function mapBucketFileRecord(file, baseUrl) {
|
|
257
|
+
return mapFileRecord(file, baseUrl);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Protected files need third-party cookies to work.
|
|
261
|
+
* Since third party cookies don't work on localhost,
|
|
262
|
+
* we need to proxy the file through the server.
|
|
263
|
+
*/ function getUrl(url, baseUrl) {
|
|
264
|
+
if (isDev() && !url.includes('/_public/')) {
|
|
265
|
+
if (!baseUrl) {
|
|
266
|
+
throw new Error('Missing baseUrl. Pass the baseUrl to `createEdgeStore` to get protected files in development.');
|
|
267
|
+
}
|
|
268
|
+
const proxyUrl = new URL(baseUrl);
|
|
269
|
+
proxyUrl.pathname = `${proxyUrl.pathname}/proxy-file`;
|
|
270
|
+
proxyUrl.search = new URLSearchParams({
|
|
271
|
+
url
|
|
272
|
+
}).toString();
|
|
273
|
+
return proxyUrl.toString();
|
|
274
|
+
}
|
|
275
|
+
return url;
|
|
276
|
+
}
|
|
277
|
+
function mapMutationResult(files, result) {
|
|
278
|
+
assertMutationResultCount(files, result);
|
|
279
|
+
const succeeded = [];
|
|
280
|
+
const failed = [];
|
|
281
|
+
result.results.forEach((item, index)=>{
|
|
282
|
+
const file = files[index];
|
|
283
|
+
if (item.success) succeeded.push(file);
|
|
284
|
+
else failed.push({
|
|
285
|
+
ref: file,
|
|
286
|
+
error: item.error
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
return {
|
|
290
|
+
succeeded,
|
|
291
|
+
failed
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function requireMutationSuccess(file, result) {
|
|
295
|
+
assertMutationResultCount([
|
|
296
|
+
file
|
|
297
|
+
], result);
|
|
298
|
+
const item = result.results[0];
|
|
299
|
+
if (!item.success) {
|
|
300
|
+
throw new EdgeStoreFileMutationError(item.error.code, item.error.message, file);
|
|
301
|
+
}
|
|
302
|
+
return {
|
|
303
|
+
ref: file
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function assertMutationResultCount(files, result) {
|
|
307
|
+
if (result.results.length !== files.length) {
|
|
308
|
+
throw new Error(`The provider returned ${result.results.length} mutation results for ${files.length} files.`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export { createBucketClient };
|