@edgestore/server 0.7.0 → 1.0.0-next.0
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/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 +2 -2
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +35 -43
- package/dist/adapters/express/index.d.ts +2 -2
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +34 -41
- package/dist/adapters/fastify/index.d.ts +3 -3
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +33 -42
- package/dist/adapters/hono/index.d.ts +6 -72
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +38 -41
- package/dist/adapters/imageTypes.js +1 -3
- package/dist/adapters/next/app/index.d.ts +2 -2
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +34 -42
- package/dist/adapters/next/pages/index.d.ts +2 -2
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +35 -42
- package/dist/adapters/remix/index.d.ts +2 -2
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +35 -42
- package/dist/adapters/shared.d.ts +8 -0
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +79 -56
- package/dist/adapters/start/index.d.ts +2 -2
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +35 -43
- package/dist/core/client/index.d.ts +112 -69
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +59 -13
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +2 -10
- package/dist/core/sdk/index.d.ts +44 -2
- package/dist/core/sdk/index.d.ts.map +1 -1
- package/dist/core/sdk/index.js +43 -16
- package/dist/index.js +1 -10
- package/dist/libs/errors/EdgeStoreCredentialsError.js +1 -3
- package/dist/libs/logger.js +5 -9
- package/dist/libs/utils.js +1 -3
- package/dist/providers/aws/index.d.ts +1 -1
- package/dist/providers/aws/index.d.ts.map +1 -1
- package/dist/providers/aws/index.js +14 -17
- package/dist/providers/azure/index.d.ts.map +1 -1
- package/dist/providers/azure/index.js +38 -15
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +36 -18
- package/dist/test-utils/edgestoreSmoke.d.ts +32 -0
- package/dist/test-utils/edgestoreSmoke.d.ts.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +4 -0
- package/package.json +59 -49
- package/providers/aws/index.d.ts +1 -1
- package/providers/aws/index.js +1 -1
- package/providers/azure/index.d.ts +1 -1
- package/providers/azure/index.js +1 -1
- package/providers/edgestore/index.d.ts +1 -1
- package/providers/edgestore/index.js +1 -1
- package/src/adapters/astro/index.ts +11 -21
- package/src/adapters/express/index.ts +10 -16
- package/src/adapters/fastify/index.ts +13 -19
- package/src/adapters/hono/index.ts +19 -23
- package/src/adapters/next/app/index.ts +12 -22
- package/src/adapters/next/pages/index.ts +11 -16
- package/src/adapters/remix/index.ts +11 -12
- package/src/adapters/shared.ts +61 -17
- package/src/adapters/start/index.ts +11 -12
- package/src/core/client/index.ts +199 -75
- package/src/core/sdk/index.ts +76 -8
- package/src/providers/aws/index.ts +4 -5
- package/src/providers/azure/index.ts +40 -11
- package/src/providers/edgestore/index.ts +29 -0
- package/src/test-utils/edgestoreSmoke.ts +141 -0
- package/src/version.ts +2 -0
- package/dist/adapters/astro/index.mjs +0 -178
- package/dist/adapters/express/index.mjs +0 -113
- package/dist/adapters/fastify/index.mjs +0 -142
- package/dist/adapters/hono/index.mjs +0 -132
- package/dist/adapters/imageTypes.mjs +0 -12
- package/dist/adapters/next/app/index.mjs +0 -169
- package/dist/adapters/next/pages/index.mjs +0 -117
- package/dist/adapters/remix/index.mjs +0 -153
- package/dist/adapters/shared.mjs +0 -443
- package/dist/adapters/start/index.mjs +0 -187
- package/dist/core/client/index.mjs +0 -173
- package/dist/core/index.mjs +0 -2
- package/dist/core/sdk/index.mjs +0 -223
- 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.mjs +0 -103
- package/dist/providers/azure/index.mjs +0 -59
- package/dist/providers/edgestore/index.mjs +0 -144
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
-
import Logger from '../../libs/logger.mjs';
|
|
3
|
-
import { matchPath } from '../../libs/utils.mjs';
|
|
4
|
-
import { EdgeStoreProvider } from '../../providers/edgestore/index.mjs';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile } from '../shared.mjs';
|
|
6
|
-
|
|
7
|
-
// Helper to safely get cookies from Remix request
|
|
8
|
-
function getCookie(req, name) {
|
|
9
|
-
const cookieHeader = req.headers.get('cookie');
|
|
10
|
-
if (!cookieHeader) return undefined;
|
|
11
|
-
const cookies = cookieHeader.split(';').reduce((acc, cookie)=>{
|
|
12
|
-
const [key, value] = cookie.trim().split('=');
|
|
13
|
-
if (key && value) acc[key] = value;
|
|
14
|
-
return acc;
|
|
15
|
-
}, {});
|
|
16
|
-
return cookies[name];
|
|
17
|
-
}
|
|
18
|
-
function createEdgeStoreRemixHandler(config) {
|
|
19
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
20
|
-
const log = new Logger(config.logLevel);
|
|
21
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
22
|
-
log.debug('Creating EdgeStore Remix handler');
|
|
23
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
24
|
-
return async ({ request: req })=>{
|
|
25
|
-
try {
|
|
26
|
-
const url = new URL(req.url);
|
|
27
|
-
const pathname = url.pathname;
|
|
28
|
-
if (matchPath(pathname, '/health')) {
|
|
29
|
-
return new Response('OK');
|
|
30
|
-
} else if (matchPath(pathname, '/init')) {
|
|
31
|
-
let ctx = {};
|
|
32
|
-
try {
|
|
33
|
-
ctx = 'createContext' in config ? await config.createContext({
|
|
34
|
-
req
|
|
35
|
-
}) : {};
|
|
36
|
-
} catch (err) {
|
|
37
|
-
throw new EdgeStoreError({
|
|
38
|
-
message: 'Error creating context',
|
|
39
|
-
code: 'CREATE_CONTEXT_ERROR',
|
|
40
|
-
cause: err instanceof Error ? err : undefined
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
const { newCookies, token, baseUrl, providerName } = await init({
|
|
44
|
-
ctx,
|
|
45
|
-
provider,
|
|
46
|
-
router: config.router,
|
|
47
|
-
cookieConfig
|
|
48
|
-
});
|
|
49
|
-
// Create response with cookies and token
|
|
50
|
-
const responseHeaders = new Headers();
|
|
51
|
-
if (Array.isArray(newCookies)) {
|
|
52
|
-
for (const cookie of newCookies){
|
|
53
|
-
responseHeaders.append('Set-Cookie', cookie);
|
|
54
|
-
}
|
|
55
|
-
} else if (newCookies) {
|
|
56
|
-
responseHeaders.append('Set-Cookie', newCookies);
|
|
57
|
-
}
|
|
58
|
-
return new Response(JSON.stringify({
|
|
59
|
-
token,
|
|
60
|
-
baseUrl,
|
|
61
|
-
providerName
|
|
62
|
-
}), {
|
|
63
|
-
headers: responseHeaders,
|
|
64
|
-
status: 200
|
|
65
|
-
});
|
|
66
|
-
} else if (matchPath(pathname, '/request-upload')) {
|
|
67
|
-
const body = await req.json();
|
|
68
|
-
return Response.json(await requestUpload({
|
|
69
|
-
provider,
|
|
70
|
-
router: config.router,
|
|
71
|
-
body,
|
|
72
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
73
|
-
}));
|
|
74
|
-
} else if (matchPath(pathname, '/request-upload-parts')) {
|
|
75
|
-
const body = await req.json();
|
|
76
|
-
return Response.json(await requestUploadParts({
|
|
77
|
-
provider,
|
|
78
|
-
router: config.router,
|
|
79
|
-
body,
|
|
80
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
81
|
-
}));
|
|
82
|
-
} else if (matchPath(pathname, '/complete-multipart-upload')) {
|
|
83
|
-
const body = await req.json();
|
|
84
|
-
await completeMultipartUpload({
|
|
85
|
-
provider,
|
|
86
|
-
router: config.router,
|
|
87
|
-
body,
|
|
88
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
89
|
-
});
|
|
90
|
-
return new Response(null, {
|
|
91
|
-
status: 200
|
|
92
|
-
});
|
|
93
|
-
} else if (matchPath(pathname, '/confirm-upload')) {
|
|
94
|
-
const body = await req.json();
|
|
95
|
-
return Response.json(await confirmUpload({
|
|
96
|
-
provider,
|
|
97
|
-
router: config.router,
|
|
98
|
-
body,
|
|
99
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
100
|
-
}));
|
|
101
|
-
} else if (matchPath(pathname, '/delete-file')) {
|
|
102
|
-
const body = await req.json();
|
|
103
|
-
return Response.json(await deleteFile({
|
|
104
|
-
provider,
|
|
105
|
-
router: config.router,
|
|
106
|
-
body,
|
|
107
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
108
|
-
}));
|
|
109
|
-
} else if (matchPath(pathname, '/proxy-file')) {
|
|
110
|
-
const url = new URL(req.url).searchParams.get('url');
|
|
111
|
-
if (typeof url === 'string') {
|
|
112
|
-
const proxyRes = await fetch(url, {
|
|
113
|
-
headers: {
|
|
114
|
-
cookie: req.headers.get('cookie') ?? ''
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
const data = await proxyRes.arrayBuffer();
|
|
118
|
-
const headers = new Headers();
|
|
119
|
-
headers.set('Content-Type', proxyRes.headers.get('Content-Type') ?? 'application/octet-stream');
|
|
120
|
-
return new Response(data, {
|
|
121
|
-
headers
|
|
122
|
-
});
|
|
123
|
-
} else {
|
|
124
|
-
return new Response(null, {
|
|
125
|
-
status: 400
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
} else {
|
|
129
|
-
return new Response(null, {
|
|
130
|
-
status: 404
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
} catch (err) {
|
|
134
|
-
if (err instanceof EdgeStoreError) {
|
|
135
|
-
log[err.level](err.formattedMessage());
|
|
136
|
-
if (err.cause) log[err.level](err.cause);
|
|
137
|
-
return Response.json(err.formattedJson(), {
|
|
138
|
-
status: EDGE_STORE_ERROR_CODES[err.code]
|
|
139
|
-
});
|
|
140
|
-
} else {
|
|
141
|
-
log.error(err);
|
|
142
|
-
return Response.json(new EdgeStoreError({
|
|
143
|
-
message: 'Internal Server Error',
|
|
144
|
-
code: 'SERVER_ERROR'
|
|
145
|
-
}).formattedJson(), {
|
|
146
|
-
status: 500
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export { createEdgeStoreRemixHandler };
|
package/dist/adapters/shared.mjs
DELETED
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
import { EdgeStoreError } from '@edgestore/shared';
|
|
2
|
-
import { hkdf } from '@panva/hkdf';
|
|
3
|
-
import { serialize } from 'cookie';
|
|
4
|
-
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
5
|
-
import { v4 } from 'uuid';
|
|
6
|
-
import { IMAGE_MIME_TYPES } from './imageTypes.mjs';
|
|
7
|
-
|
|
8
|
-
// TODO: change it to 1 hour when we have a way to refresh the token
|
|
9
|
-
const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60; // 30 days
|
|
10
|
-
/**
|
|
11
|
-
* Merges the provided cookie configuration with default values
|
|
12
|
-
*/ function getCookieConfig(cookieConfig) {
|
|
13
|
-
const defaultOptions = {
|
|
14
|
-
path: '/',
|
|
15
|
-
maxAge: DEFAULT_MAX_AGE
|
|
16
|
-
};
|
|
17
|
-
// Helper function to merge options, filtering out undefined values
|
|
18
|
-
const mergeOptions = (configOptions)=>{
|
|
19
|
-
const merged = {
|
|
20
|
-
...defaultOptions
|
|
21
|
-
};
|
|
22
|
-
if (configOptions) {
|
|
23
|
-
Object.keys(configOptions).forEach((key)=>{
|
|
24
|
-
const value = configOptions[key];
|
|
25
|
-
if (value !== undefined) {
|
|
26
|
-
merged[key] = value;
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
return merged;
|
|
31
|
-
};
|
|
32
|
-
return {
|
|
33
|
-
ctx: {
|
|
34
|
-
name: cookieConfig?.ctx?.name ?? 'edgestore-ctx',
|
|
35
|
-
options: mergeOptions(cookieConfig?.ctx?.options)
|
|
36
|
-
},
|
|
37
|
-
token: {
|
|
38
|
-
name: cookieConfig?.token?.name ?? 'edgestore-token',
|
|
39
|
-
options: mergeOptions(cookieConfig?.token?.options)
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
async function init(params) {
|
|
44
|
-
const log = globalThis._EDGE_STORE_LOGGER;
|
|
45
|
-
const { ctx, provider, router, cookieConfig } = params;
|
|
46
|
-
log.debug('Running [init]', {
|
|
47
|
-
ctx
|
|
48
|
-
});
|
|
49
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
50
|
-
const ctxToken = await encryptJWT(ctx);
|
|
51
|
-
const { token } = await provider.init({
|
|
52
|
-
ctx,
|
|
53
|
-
router: router
|
|
54
|
-
});
|
|
55
|
-
const newCookies = [
|
|
56
|
-
serialize(resolvedCookieConfig.ctx.name, ctxToken, resolvedCookieConfig.ctx.options)
|
|
57
|
-
];
|
|
58
|
-
if (token) {
|
|
59
|
-
newCookies.push(serialize(resolvedCookieConfig.token.name, token, resolvedCookieConfig.token.options));
|
|
60
|
-
}
|
|
61
|
-
const baseUrl = await provider.getBaseUrl();
|
|
62
|
-
log.debug('Finished [init]', {
|
|
63
|
-
ctx,
|
|
64
|
-
newCookies,
|
|
65
|
-
token,
|
|
66
|
-
baseUrl,
|
|
67
|
-
providerName: provider.name
|
|
68
|
-
});
|
|
69
|
-
return {
|
|
70
|
-
newCookies,
|
|
71
|
-
token,
|
|
72
|
-
baseUrl,
|
|
73
|
-
providerName: provider.name
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
async function requestUpload(params) {
|
|
77
|
-
const { provider, router, ctxToken, body: { bucketName, input, fileInfo } } = params;
|
|
78
|
-
const log = globalThis._EDGE_STORE_LOGGER;
|
|
79
|
-
log.debug('Running [requestUpload]', {
|
|
80
|
-
bucketName,
|
|
81
|
-
input,
|
|
82
|
-
fileInfo
|
|
83
|
-
});
|
|
84
|
-
if (!ctxToken) {
|
|
85
|
-
throw new EdgeStoreError({
|
|
86
|
-
message: 'Missing edgestore-ctx cookie',
|
|
87
|
-
code: 'UNAUTHORIZED'
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
const ctx = await getContext(ctxToken);
|
|
91
|
-
log.debug('Decrypted Context', {
|
|
92
|
-
ctx
|
|
93
|
-
});
|
|
94
|
-
const bucket = router.buckets[bucketName];
|
|
95
|
-
if (!bucket) {
|
|
96
|
-
throw new EdgeStoreError({
|
|
97
|
-
message: `Bucket ${bucketName} not found`,
|
|
98
|
-
code: 'BAD_REQUEST'
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
if (bucket._def.beforeUpload) {
|
|
102
|
-
log.debug('Running [beforeUpload]');
|
|
103
|
-
const canUpload = await bucket._def.beforeUpload?.({
|
|
104
|
-
ctx,
|
|
105
|
-
input,
|
|
106
|
-
fileInfo: {
|
|
107
|
-
size: fileInfo.size,
|
|
108
|
-
type: fileInfo.type,
|
|
109
|
-
fileName: fileInfo.fileName,
|
|
110
|
-
extension: fileInfo.extension,
|
|
111
|
-
replaceTargetUrl: fileInfo.replaceTargetUrl,
|
|
112
|
-
temporary: fileInfo.temporary
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
log.debug('Finished [beforeUpload]', {
|
|
116
|
-
canUpload
|
|
117
|
-
});
|
|
118
|
-
if (!canUpload) {
|
|
119
|
-
throw new EdgeStoreError({
|
|
120
|
-
message: 'Upload not allowed for the current context',
|
|
121
|
-
code: 'UPLOAD_NOT_ALLOWED'
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (bucket._def.type === 'IMAGE') {
|
|
126
|
-
if (!IMAGE_MIME_TYPES.includes(fileInfo.type)) {
|
|
127
|
-
throw new EdgeStoreError({
|
|
128
|
-
code: 'MIME_TYPE_NOT_ALLOWED',
|
|
129
|
-
message: 'Only images are allowed in this bucket',
|
|
130
|
-
details: {
|
|
131
|
-
allowedMimeTypes: IMAGE_MIME_TYPES,
|
|
132
|
-
mimeType: fileInfo.type
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (bucket._def.bucketConfig?.maxSize) {
|
|
138
|
-
if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
|
|
139
|
-
throw new EdgeStoreError({
|
|
140
|
-
code: 'FILE_TOO_LARGE',
|
|
141
|
-
message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`,
|
|
142
|
-
details: {
|
|
143
|
-
maxFileSize: bucket._def.bucketConfig.maxSize,
|
|
144
|
-
fileSize: fileInfo.size
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (bucket._def.bucketConfig?.accept) {
|
|
150
|
-
const accept = bucket._def.bucketConfig.accept;
|
|
151
|
-
let accepted = false;
|
|
152
|
-
for (const acceptedMimeType of accept){
|
|
153
|
-
if (acceptedMimeType.endsWith('/*')) {
|
|
154
|
-
const mimeType = acceptedMimeType.replace('/*', '');
|
|
155
|
-
if (fileInfo.type.startsWith(mimeType)) {
|
|
156
|
-
accepted = true;
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
} else if (fileInfo.type === acceptedMimeType) {
|
|
160
|
-
accepted = true;
|
|
161
|
-
break;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if (!accepted) {
|
|
165
|
-
throw new EdgeStoreError({
|
|
166
|
-
code: 'MIME_TYPE_NOT_ALLOWED',
|
|
167
|
-
message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
|
|
168
|
-
details: {
|
|
169
|
-
allowedMimeTypes: accept,
|
|
170
|
-
mimeType: fileInfo.type
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const path = buildPath({
|
|
176
|
-
bucket,
|
|
177
|
-
pathAttrs: {
|
|
178
|
-
ctx,
|
|
179
|
-
input
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
const metadata = await bucket._def.metadata?.({
|
|
183
|
-
ctx,
|
|
184
|
-
input
|
|
185
|
-
});
|
|
186
|
-
const isPublic = bucket._def.accessControl === undefined;
|
|
187
|
-
log.debug('upload info', {
|
|
188
|
-
path,
|
|
189
|
-
metadata,
|
|
190
|
-
isPublic,
|
|
191
|
-
bucketType: bucket._def.type
|
|
192
|
-
});
|
|
193
|
-
const requestUploadRes = await provider.requestUpload({
|
|
194
|
-
bucketName,
|
|
195
|
-
bucketType: bucket._def.type,
|
|
196
|
-
fileInfo: {
|
|
197
|
-
...fileInfo,
|
|
198
|
-
path,
|
|
199
|
-
isPublic,
|
|
200
|
-
metadata
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
const { parsedPath, pathOrder } = parsePath(path);
|
|
204
|
-
log.debug('Finished [requestUpload]');
|
|
205
|
-
return {
|
|
206
|
-
...requestUploadRes,
|
|
207
|
-
size: fileInfo.size,
|
|
208
|
-
uploadedAt: new Date().toISOString(),
|
|
209
|
-
path: parsedPath,
|
|
210
|
-
pathOrder,
|
|
211
|
-
metadata
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
async function requestUploadParts(params) {
|
|
215
|
-
const { provider, ctxToken, body: { multipart, path } } = params;
|
|
216
|
-
const log = globalThis._EDGE_STORE_LOGGER;
|
|
217
|
-
log.debug('Running [requestUploadParts]', {
|
|
218
|
-
multipart,
|
|
219
|
-
path
|
|
220
|
-
});
|
|
221
|
-
if (!ctxToken) {
|
|
222
|
-
throw new EdgeStoreError({
|
|
223
|
-
message: 'Missing edgestore-ctx cookie',
|
|
224
|
-
code: 'UNAUTHORIZED'
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
await getContext(ctxToken); // just to check if the token is valid
|
|
228
|
-
const res = await provider.requestUploadParts({
|
|
229
|
-
multipart,
|
|
230
|
-
path
|
|
231
|
-
});
|
|
232
|
-
log.debug('Finished [requestUploadParts]');
|
|
233
|
-
return res;
|
|
234
|
-
}
|
|
235
|
-
async function completeMultipartUpload(params) {
|
|
236
|
-
const { provider, router, ctxToken, body: { bucketName, uploadId, key, parts } } = params;
|
|
237
|
-
const log = globalThis._EDGE_STORE_LOGGER;
|
|
238
|
-
log.debug('Running [completeMultipartUpload]', {
|
|
239
|
-
bucketName,
|
|
240
|
-
uploadId,
|
|
241
|
-
key
|
|
242
|
-
});
|
|
243
|
-
if (!ctxToken) {
|
|
244
|
-
throw new EdgeStoreError({
|
|
245
|
-
message: 'Missing edgestore-ctx cookie',
|
|
246
|
-
code: 'UNAUTHORIZED'
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
await getContext(ctxToken); // just to check if the token is valid
|
|
250
|
-
const bucket = router.buckets[bucketName];
|
|
251
|
-
if (!bucket) {
|
|
252
|
-
throw new EdgeStoreError({
|
|
253
|
-
message: `Bucket ${bucketName} not found`,
|
|
254
|
-
code: 'BAD_REQUEST'
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
const res = await provider.completeMultipartUpload({
|
|
258
|
-
uploadId,
|
|
259
|
-
key,
|
|
260
|
-
parts
|
|
261
|
-
});
|
|
262
|
-
log.debug('Finished [completeMultipartUpload]');
|
|
263
|
-
return res;
|
|
264
|
-
}
|
|
265
|
-
async function confirmUpload(params) {
|
|
266
|
-
const { provider, router, ctxToken, body: { bucketName, url } } = params;
|
|
267
|
-
const log = globalThis._EDGE_STORE_LOGGER;
|
|
268
|
-
log.debug('Running [confirmUpload]', {
|
|
269
|
-
bucketName,
|
|
270
|
-
url
|
|
271
|
-
});
|
|
272
|
-
if (!ctxToken) {
|
|
273
|
-
throw new EdgeStoreError({
|
|
274
|
-
message: 'Missing edgestore-ctx cookie',
|
|
275
|
-
code: 'UNAUTHORIZED'
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
await getContext(ctxToken); // just to check if the token is valid
|
|
279
|
-
const bucket = router.buckets[bucketName];
|
|
280
|
-
if (!bucket) {
|
|
281
|
-
throw new EdgeStoreError({
|
|
282
|
-
message: `Bucket ${bucketName} not found`,
|
|
283
|
-
code: 'BAD_REQUEST'
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
const res = await provider.confirmUpload({
|
|
287
|
-
bucket,
|
|
288
|
-
url: unproxyUrl(url)
|
|
289
|
-
});
|
|
290
|
-
log.debug('Finished [confirmUpload]');
|
|
291
|
-
return res;
|
|
292
|
-
}
|
|
293
|
-
async function deleteFile(params) {
|
|
294
|
-
const { provider, router, ctxToken, body: { bucketName, url } } = params;
|
|
295
|
-
const log = globalThis._EDGE_STORE_LOGGER;
|
|
296
|
-
log.debug('Running [deleteFile]', {
|
|
297
|
-
bucketName,
|
|
298
|
-
url
|
|
299
|
-
});
|
|
300
|
-
if (!ctxToken) {
|
|
301
|
-
throw new EdgeStoreError({
|
|
302
|
-
message: 'Missing edgestore-ctx cookie',
|
|
303
|
-
code: 'UNAUTHORIZED'
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
const ctx = await getContext(ctxToken);
|
|
307
|
-
const bucket = router.buckets[bucketName];
|
|
308
|
-
if (!bucket) {
|
|
309
|
-
throw new EdgeStoreError({
|
|
310
|
-
message: `Bucket ${bucketName} not found`,
|
|
311
|
-
code: 'BAD_REQUEST'
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
if (!bucket._def.beforeDelete) {
|
|
315
|
-
throw new EdgeStoreError({
|
|
316
|
-
message: 'You need to define beforeDelete if you want to delete files directly from the frontend.',
|
|
317
|
-
code: 'SERVER_ERROR'
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
const fileInfo = await provider.getFile({
|
|
321
|
-
url: unproxyUrl(url)
|
|
322
|
-
});
|
|
323
|
-
const canDelete = await bucket._def.beforeDelete({
|
|
324
|
-
ctx,
|
|
325
|
-
fileInfo
|
|
326
|
-
});
|
|
327
|
-
if (!canDelete) {
|
|
328
|
-
throw new EdgeStoreError({
|
|
329
|
-
message: 'Delete not allowed for the current context',
|
|
330
|
-
code: 'DELETE_NOT_ALLOWED'
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
const res = await provider.deleteFile({
|
|
334
|
-
bucket,
|
|
335
|
-
url: unproxyUrl(url)
|
|
336
|
-
});
|
|
337
|
-
log.debug('Finished [deleteFile]');
|
|
338
|
-
return res;
|
|
339
|
-
}
|
|
340
|
-
async function encryptJWT(ctx) {
|
|
341
|
-
const secret = getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
342
|
-
if (!secret) {
|
|
343
|
-
throw new EdgeStoreError({
|
|
344
|
-
message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
|
|
345
|
-
code: 'SERVER_ERROR'
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
const encryptionSecret = await getDerivedEncryptionKey(secret);
|
|
349
|
-
return await new EncryptJWT(ctx).setProtectedHeader({
|
|
350
|
-
alg: 'dir',
|
|
351
|
-
enc: 'A256GCM'
|
|
352
|
-
}).setIssuedAt().setExpirationTime(Date.now() / 1000 + DEFAULT_MAX_AGE).setJti(v4()).encrypt(encryptionSecret);
|
|
353
|
-
}
|
|
354
|
-
async function decryptJWT(token) {
|
|
355
|
-
const secret = getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
356
|
-
if (!secret) {
|
|
357
|
-
throw new EdgeStoreError({
|
|
358
|
-
message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
|
|
359
|
-
code: 'SERVER_ERROR'
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
const encryptionSecret = await getDerivedEncryptionKey(secret);
|
|
363
|
-
const { payload } = await jwtDecrypt(token, encryptionSecret, {
|
|
364
|
-
clockTolerance: 15
|
|
365
|
-
});
|
|
366
|
-
return payload;
|
|
367
|
-
}
|
|
368
|
-
async function getDerivedEncryptionKey(secret) {
|
|
369
|
-
return await hkdf('sha256', secret, '', 'EdgeStore Generated Encryption Key', 32);
|
|
370
|
-
}
|
|
371
|
-
function buildPath(params) {
|
|
372
|
-
const { bucket } = params;
|
|
373
|
-
const pathParams = bucket._def.path;
|
|
374
|
-
const path = pathParams.map((param)=>{
|
|
375
|
-
const paramEntries = Object.entries(param);
|
|
376
|
-
if (paramEntries[0] === undefined) {
|
|
377
|
-
throw new EdgeStoreError({
|
|
378
|
-
message: `Empty path param found in: ${JSON.stringify(pathParams)}`,
|
|
379
|
-
code: 'SERVER_ERROR'
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
const [key, value] = paramEntries[0];
|
|
383
|
-
// this is a string like: "ctx.xxx" or "input.yyy.zzz"
|
|
384
|
-
const currParamVal = value().split('.').reduce((acc2, key)=>{
|
|
385
|
-
if (acc2[key] === undefined) {
|
|
386
|
-
throw new EdgeStoreError({
|
|
387
|
-
message: `Missing key ${key} in ${JSON.stringify(acc2)}`,
|
|
388
|
-
code: 'BAD_REQUEST'
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
return acc2[key];
|
|
392
|
-
}, params.pathAttrs);
|
|
393
|
-
return {
|
|
394
|
-
key,
|
|
395
|
-
value: currParamVal
|
|
396
|
-
};
|
|
397
|
-
});
|
|
398
|
-
return path;
|
|
399
|
-
}
|
|
400
|
-
function parsePath(path) {
|
|
401
|
-
const parsedPath = path.reduce((acc, curr)=>{
|
|
402
|
-
acc[curr.key] = curr.value;
|
|
403
|
-
return acc;
|
|
404
|
-
}, {});
|
|
405
|
-
const pathOrder = path.map((p)=>p.key);
|
|
406
|
-
return {
|
|
407
|
-
parsedPath,
|
|
408
|
-
pathOrder
|
|
409
|
-
};
|
|
410
|
-
}
|
|
411
|
-
async function getContext(token) {
|
|
412
|
-
return await decryptJWT(token);
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* On local development, protected files are proxied to the server,
|
|
416
|
-
* which changes the original URL.
|
|
417
|
-
*
|
|
418
|
-
* This function is used to get the original URL,
|
|
419
|
-
* so that we can delete or confirm the upload.
|
|
420
|
-
*/ function unproxyUrl(url) {
|
|
421
|
-
if (isDev() && url.startsWith('http://')) {
|
|
422
|
-
// get the url param from the query string
|
|
423
|
-
const urlParam = new URL(url).searchParams.get('url');
|
|
424
|
-
if (urlParam) {
|
|
425
|
-
return urlParam;
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
return url;
|
|
429
|
-
}
|
|
430
|
-
function getEnv(key) {
|
|
431
|
-
if (typeof process !== 'undefined' && process.env) {
|
|
432
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
433
|
-
return process.env[key] ?? import.meta.env?.[key];
|
|
434
|
-
}
|
|
435
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
436
|
-
return import.meta.env?.[key];
|
|
437
|
-
}
|
|
438
|
-
function isDev() {
|
|
439
|
-
return process?.env?.NODE_ENV === 'development' || // @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
440
|
-
import.meta.env?.DEV;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
export { buildPath, completeMultipartUpload, confirmUpload, deleteFile, getCookieConfig, getEnv, init, isDev, parsePath, requestUpload, requestUploadParts };
|