@edgestore/server 1.0.0-next.5 → 1.0.0-rc.6
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/agent-docs/README.md +1 -1
- package/agent-docs/astro.md +1 -1
- package/agent-docs/backend-client.md +5 -8
- package/agent-docs/express.md +1 -1
- package/agent-docs/fastify.md +1 -1
- package/agent-docs/hono.md +1 -1
- package/agent-docs/next.md +1 -1
- package/agent-docs/provider-azure-blob.md +150 -57
- package/agent-docs/provider-custom.md +17 -3
- package/agent-docs/provider-s3.md +4 -3
- package/agent-docs/remix.md +1 -1
- package/agent-docs/tanstack-start.md +1 -1
- package/dist/adapters/shared.d.ts +12 -102
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +63 -108
- package/dist/core/client/index.d.ts +0 -4
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/provider.d.ts +0 -1
- package/dist/core/provider.d.ts.map +1 -1
- package/dist/core/provider.js +1 -4
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/providers/azure-blob/blocks.d.ts +48 -0
- package/dist/providers/azure-blob/blocks.d.ts.map +1 -0
- package/dist/providers/azure-blob/blocks.js +126 -0
- package/dist/providers/azure-blob/index.d.ts +76 -51
- package/dist/providers/azure-blob/index.d.ts.map +1 -1
- package/dist/providers/azure-blob/index.js +189 -107
- package/dist/providers/edgestore/index.d.ts +6 -22
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +19 -18
- package/dist/providers/s3/backendUpload.d.ts +2 -5
- package/dist/providers/s3/backendUpload.d.ts.map +1 -1
- package/dist/providers/s3/backendUpload.js +19 -57
- package/dist/providers/s3/index.d.ts +3 -12
- package/dist/providers/s3/index.d.ts.map +1 -1
- package/dist/providers/s3/index.js +23 -44
- package/dist/providers/s3/multipart.d.ts +3 -5
- package/dist/providers/s3/multipart.d.ts.map +1 -1
- package/dist/providers/s3/multipart.js +26 -62
- package/dist/providers/s3/options.d.ts +10 -32
- package/dist/providers/s3/options.d.ts.map +1 -1
- package/dist/providers/storage/multipartSession.d.ts +51 -0
- package/dist/providers/storage/multipartSession.d.ts.map +1 -0
- package/dist/providers/storage/multipartSession.js +83 -0
- package/dist/providers/storage/objectKeys.d.ts +41 -0
- package/dist/providers/storage/objectKeys.d.ts.map +1 -0
- package/dist/providers/{s3/keys.js → storage/objectKeys.js} +31 -4
- package/dist/providers/storage/transfer.d.ts +18 -0
- package/dist/providers/storage/transfer.d.ts.map +1 -0
- package/dist/providers/storage/transfer.js +69 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/package.json +4 -11
- package/src/adapters/shared.ts +81 -127
- package/src/core/client/index.ts +0 -4
- package/src/core/provider.ts +0 -8
- package/src/index.ts +3 -8
- package/src/providers/azure-blob/blocks.ts +201 -0
- package/src/providers/azure-blob/index.ts +289 -171
- package/src/providers/edgestore/index.ts +19 -22
- package/src/providers/s3/backendUpload.ts +20 -55
- package/src/providers/s3/index.ts +31 -58
- package/src/providers/s3/multipart.ts +36 -94
- package/src/providers/s3/options.ts +10 -33
- package/src/providers/storage/multipartSession.ts +145 -0
- package/src/providers/storage/objectKeys.ts +106 -0
- package/src/providers/storage/transfer.ts +105 -0
- package/src/version.ts +1 -1
- package/core/index.d.ts +0 -1
- package/core/index.js +0 -1
- package/dist/core/index.d.ts +0 -4
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -3
- package/dist/providers/s3/keys.d.ts +0 -10
- package/dist/providers/s3/keys.d.ts.map +0 -1
- package/src/core/index.ts +0 -9
- package/src/providers/s3/keys.ts +0 -59
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { EdgeStoreError } from '@edgestore/shared';
|
|
2
|
+
import { jwtVerify, SignJWT } from 'jose';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { INITIAL_MULTIPART_PART_URLS } from '../../core/provider.js';
|
|
5
|
+
import { getEnv } from '../../libs/env.js';
|
|
6
|
+
|
|
7
|
+
const sessionSchema = z.object({
|
|
8
|
+
key: z.string(),
|
|
9
|
+
/** Storage upload ID (S3) or block ID prefix (Azure). */ id: z.string(),
|
|
10
|
+
size: z.number().int().nonnegative(),
|
|
11
|
+
partSize: z.number().int().positive(),
|
|
12
|
+
totalParts: z.number().int().min(1).max(10000),
|
|
13
|
+
/** Provider-specific settings applied when the upload completes. */ data: z.record(z.string(), z.unknown()).optional()
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Browser multipart sessions are HS256 JWTs. The token is the `uploadId` the
|
|
17
|
+
* browser sends back, and acts as a bearer capability for one object key.
|
|
18
|
+
*/ function createMultipartSessions({ providerName, audience, secret, expiresIn }) {
|
|
19
|
+
if (!Number.isSafeInteger(expiresIn) || expiresIn < 1) {
|
|
20
|
+
throw new RangeError(`${providerName} multipart session lifetime must be a positive integer number of seconds.`);
|
|
21
|
+
}
|
|
22
|
+
function signingKey() {
|
|
23
|
+
const value = secret ?? getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
24
|
+
if (!value) throw new Error(`${providerName} multipart uploads require jwtSecret or EDGE_STORE_JWT_SECRET.`);
|
|
25
|
+
return new TextEncoder().encode(value);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
/** Fails before storage resources are allocated when no secret is set. */ assertConfigured () {
|
|
29
|
+
signingKey();
|
|
30
|
+
},
|
|
31
|
+
sign (session) {
|
|
32
|
+
return new SignJWT(session).setProtectedHeader({
|
|
33
|
+
alg: 'HS256'
|
|
34
|
+
}).setAudience(audience).setIssuedAt().setExpirationTime(`${expiresIn}s`).sign(signingKey());
|
|
35
|
+
},
|
|
36
|
+
async read (token, key) {
|
|
37
|
+
try {
|
|
38
|
+
const { payload } = await jwtVerify(token, signingKey(), {
|
|
39
|
+
algorithms: [
|
|
40
|
+
'HS256'
|
|
41
|
+
],
|
|
42
|
+
audience
|
|
43
|
+
});
|
|
44
|
+
const session = sessionSchema.parse(payload);
|
|
45
|
+
if (session.key !== key) throw new Error('Wrong object key');
|
|
46
|
+
return session;
|
|
47
|
+
} catch {
|
|
48
|
+
throw new EdgeStoreError({
|
|
49
|
+
code: 'BAD_REQUEST',
|
|
50
|
+
message: `Invalid or expired ${providerName} multipart upload session.`
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** Builds a session from an upload plan. */ function sessionFromPlan({ key, id, size, plan, data }) {
|
|
57
|
+
return {
|
|
58
|
+
key,
|
|
59
|
+
id,
|
|
60
|
+
size,
|
|
61
|
+
partSize: plan.partSizeBytes,
|
|
62
|
+
totalParts: plan.totalParts,
|
|
63
|
+
...data ? {
|
|
64
|
+
data
|
|
65
|
+
} : {}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** Part numbers whose URLs are signed when the upload starts. */ function initialPartNumbers(plan) {
|
|
69
|
+
return plan.partNumbers.slice(0, INITIAL_MULTIPART_PART_URLS);
|
|
70
|
+
}
|
|
71
|
+
/** Rejects empty, duplicate, or out-of-range part lists. */ function assertValidParts(session, parts, { complete = false } = {}) {
|
|
72
|
+
if (!parts.length || new Set(parts).size !== parts.length || parts.some((part)=>!Number.isInteger(part) || part < 1 || part > session.totalParts) || complete && parts.length !== session.totalParts) {
|
|
73
|
+
throw new EdgeStoreError({
|
|
74
|
+
code: 'BAD_REQUEST',
|
|
75
|
+
message: 'Invalid multipart part numbers.'
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Byte length of one part; the last part may be shorter. */ function partLength(session, partNumber) {
|
|
80
|
+
return Math.min(session.partSize, session.size - (partNumber - 1) * session.partSize);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { assertValidParts, createMultipartSessions, initialPartNumbers, partLength, sessionFromPlan };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { MaybePromise, RequestUploadParams } from '@edgestore/shared';
|
|
2
|
+
export type ObjectPathFnArgs = {
|
|
3
|
+
/** Logical EdgeStore router bucket name. */
|
|
4
|
+
edgestoreBucketName: string;
|
|
5
|
+
/** File info after EdgeStore path and metadata generation. */
|
|
6
|
+
fileInfo: RequestUploadParams['fileInfo'];
|
|
7
|
+
/** Default object path relative to the logical bucket prefix. */
|
|
8
|
+
defaultPath: string;
|
|
9
|
+
};
|
|
10
|
+
/** Returns an object path relative to the logical bucket prefix. */
|
|
11
|
+
export type ObjectPathFn = (args: ObjectPathFnArgs) => MaybePromise<string>;
|
|
12
|
+
export declare function createObjectKeys(providerName: string, baseUrl: string): {
|
|
13
|
+
toUrl: (objectKey: string) => string;
|
|
14
|
+
fromReference: (bucketName: string, file: {
|
|
15
|
+
url: string;
|
|
16
|
+
} | {
|
|
17
|
+
key: string;
|
|
18
|
+
}) => string;
|
|
19
|
+
forUpload: (edgestoreBucketName: string, fileInfo: RequestUploadParams["fileInfo"], path?: ObjectPathFn) => Promise<{
|
|
20
|
+
key: string;
|
|
21
|
+
pathArgs: {
|
|
22
|
+
edgestoreBucketName: string;
|
|
23
|
+
fileInfo: {
|
|
24
|
+
type?: string;
|
|
25
|
+
size: number;
|
|
26
|
+
extension: string;
|
|
27
|
+
isPublic: boolean;
|
|
28
|
+
fileName?: string;
|
|
29
|
+
path: {
|
|
30
|
+
key: string;
|
|
31
|
+
value: string;
|
|
32
|
+
}[];
|
|
33
|
+
metadata: import("@edgestore/shared").AnyMetadata;
|
|
34
|
+
replaceTargetUrl?: string;
|
|
35
|
+
temporary: boolean;
|
|
36
|
+
};
|
|
37
|
+
defaultPath: string;
|
|
38
|
+
};
|
|
39
|
+
}>;
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=objectKeys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objectKeys.d.ts","sourceRoot":"","sources":["../../../src/providers/storage/objectKeys.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAE3E,MAAM,MAAM,gBAAgB,GAAG;IAC7B,4CAA4C;IAC5C,mBAAmB,EAAE,MAAM,CAAC;IAC5B,8DAA8D;IAC9D,QAAQ,EAAE,mBAAmB,CAAC,UAAU,CAAC,CAAC;IAC1C,iEAAiE;IACjE,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,oEAAoE;AACpE,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,gBAAgB,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC;AAE5E,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;uBAGjC,MAAM;gCA2C3B,MAAM,QACZ;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE;qCAoBlB,MAAM,YACjB,mBAAmB,CAAC,UAAU,CAAC,SAClC,YAAY;;;;;;;;;;;;;;;;;;;;;EAsBtB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function createObjectKeys(baseUrl) {
|
|
1
|
+
function createObjectKeys(providerName, baseUrl) {
|
|
2
2
|
const normalizedBaseUrl = baseUrl.replace(/\/+$/, '');
|
|
3
3
|
function objectKeyToUrl(objectKey) {
|
|
4
4
|
const encodedKey = objectKey.split('/').map((segment)=>encodeURIComponent(segment)).join('/');
|
|
@@ -13,13 +13,13 @@ function createObjectKeys(baseUrl) {
|
|
|
13
13
|
}
|
|
14
14
|
return decodeURIComponent(fileUrl.pathname.slice(providerUrl.pathname.length));
|
|
15
15
|
} catch {
|
|
16
|
-
throw new Error(
|
|
16
|
+
throw new Error(`File URL does not belong to this ${providerName} provider.`);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
function normalizeRelativePath(value) {
|
|
20
20
|
const path = value.replace(/^\/+|\/+$/g, '');
|
|
21
21
|
if (path.length === 0 || path.split('/').some((segment)=>segment === '.' || segment === '..')) {
|
|
22
|
-
throw new Error(
|
|
22
|
+
throw new Error(`${providerName} paths must stay within the EdgeStore bucket prefix.`);
|
|
23
23
|
}
|
|
24
24
|
return path;
|
|
25
25
|
}
|
|
@@ -30,10 +30,37 @@ function createObjectKeys(baseUrl) {
|
|
|
30
30
|
}
|
|
31
31
|
return key;
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Resolves the object key for an upload. The logical bucket is always the
|
|
35
|
+
* first key segment, so router authorization for one bucket cannot reach
|
|
36
|
+
* objects from another.
|
|
37
|
+
*/ async function forUpload(edgestoreBucketName, fileInfo, path) {
|
|
38
|
+
const extension = fileInfo.extension ? `.${fileInfo.extension.replace(/^\./, '')}` : '';
|
|
39
|
+
const defaultPath = [
|
|
40
|
+
...fileInfo.isPublic ? [
|
|
41
|
+
'_public'
|
|
42
|
+
] : [],
|
|
43
|
+
...fileInfo.path.map((part)=>part.value),
|
|
44
|
+
fileInfo.fileName ?? `${crypto.randomUUID()}${extension}`
|
|
45
|
+
].join('/');
|
|
46
|
+
const relativePath = normalizeRelativePath(path ? await path({
|
|
47
|
+
edgestoreBucketName,
|
|
48
|
+
fileInfo,
|
|
49
|
+
defaultPath
|
|
50
|
+
}) : defaultPath);
|
|
51
|
+
return {
|
|
52
|
+
key: `${edgestoreBucketName}/${relativePath}`,
|
|
53
|
+
pathArgs: {
|
|
54
|
+
edgestoreBucketName,
|
|
55
|
+
fileInfo,
|
|
56
|
+
defaultPath
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
33
60
|
return {
|
|
34
61
|
toUrl: objectKeyToUrl,
|
|
35
62
|
fromReference,
|
|
36
|
-
|
|
63
|
+
forUpload
|
|
37
64
|
};
|
|
38
65
|
}
|
|
39
66
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AnyContext, BackendUploadParams, EdgeStoreRouter } from '@edgestore/shared';
|
|
2
|
+
/** Validates a signed URL lifetime against the storage service's maximum. */
|
|
3
|
+
export declare function signedUrlLifetime(providerName: string, value: number, maxSeconds?: number): number;
|
|
4
|
+
/**
|
|
5
|
+
* Direct-storage providers authorize private reads with backend-issued signed
|
|
6
|
+
* URLs. Cookie-based rules would silently not be enforced, so reject them.
|
|
7
|
+
*/
|
|
8
|
+
export declare function assertSignedUrlAccessControl<TCtx extends AnyContext>(providerName: string, router: EdgeStoreRouter<TCtx>): void;
|
|
9
|
+
/** Reports cumulative backend upload progress. */
|
|
10
|
+
export declare function createProgress(totalBytes: number, onProgress: BackendUploadParams['onProgress']): (bytes: number) => void;
|
|
11
|
+
/**
|
|
12
|
+
* Runs tasks a few at a time to bound memory use. The first failure, or
|
|
13
|
+
* the caller's signal, stops the remaining parts before the error is thrown.
|
|
14
|
+
*/
|
|
15
|
+
export declare function runConcurrently<TItem, TResult>(items: TItem[], signal: AbortSignal | undefined, task: (item: TItem, signal: AbortSignal) => Promise<TResult>): Promise<TResult[]>;
|
|
16
|
+
/** Reads one part of the source into memory. */
|
|
17
|
+
export declare function readPart(source: Blob, partNumber: number, partSizeBytes: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
18
|
+
//# sourceMappingURL=transfer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transfer.d.ts","sourceRoot":"","sources":["../../../src/providers/storage/transfer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,UAAU,EACV,mBAAmB,EACnB,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAE3B,6EAA6E;AAC7E,wBAAgB,iBAAiB,CAC/B,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,MAAM,EACb,UAAU,SAAS,UAQpB;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,SAAS,UAAU,EAClE,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,eAAe,CAAC,IAAI,CAAC,QAU9B;AAED,kDAAkD;AAClD,wBAAgB,cAAc,CAC5B,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,mBAAmB,CAAC,YAAY,CAAC,WAGtB,MAAM,UAW9B;AAED;;;GAGG;AACH,wBAAsB,eAAe,CAAC,KAAK,EAAE,OAAO,EAClD,KAAK,EAAE,KAAK,EAAE,EACd,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,OAAO,CAAC,sBA2B7D;AAED,gDAAgD;AAChD,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,IAAI,EACZ,UAAU,EAAE,MAAM,EAClB,aAAa,EAAE,MAAM,oCAMtB"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DEFAULT_MULTIPART_CONCURRENCY } from '@edgestore/sdk';
|
|
2
|
+
|
|
3
|
+
/** Validates a signed URL lifetime against the storage service's maximum. */ function signedUrlLifetime(providerName, value, maxSeconds = 604800) {
|
|
4
|
+
if (!Number.isInteger(value) || value < 1 || value > maxSeconds) {
|
|
5
|
+
throw new RangeError(`${providerName} signed URL expiration must be an integer between 1 and ${maxSeconds} seconds.`);
|
|
6
|
+
}
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Direct-storage providers authorize private reads with backend-issued signed
|
|
11
|
+
* URLs. Cookie-based rules would silently not be enforced, so reject them.
|
|
12
|
+
*/ function assertSignedUrlAccessControl(providerName, router) {
|
|
13
|
+
for (const bucket of Object.values(router.buckets)){
|
|
14
|
+
const { accessControl } = bucket._def;
|
|
15
|
+
if (accessControl !== undefined && accessControl !== 'private') {
|
|
16
|
+
throw new Error(`${providerName} supports accessControl("private") with backend signed URLs, not cookie-based access-control rules.`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Reports cumulative backend upload progress. */ function createProgress(totalBytes, onProgress) {
|
|
21
|
+
let transferredBytes = 0;
|
|
22
|
+
const report = (bytes)=>{
|
|
23
|
+
transferredBytes += bytes;
|
|
24
|
+
onProgress?.({
|
|
25
|
+
transferredBytes,
|
|
26
|
+
totalBytes,
|
|
27
|
+
percentage: totalBytes ? transferredBytes / totalBytes * 100 : 100,
|
|
28
|
+
phase: 'uploading'
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
report(0);
|
|
32
|
+
return report;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Runs tasks a few at a time to bound memory use. The first failure, or
|
|
36
|
+
* the caller's signal, stops the remaining parts before the error is thrown.
|
|
37
|
+
*/ async function runConcurrently(items, signal, task) {
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
const partSignal = signal ? AbortSignal.any([
|
|
40
|
+
signal,
|
|
41
|
+
controller.signal
|
|
42
|
+
]) : controller.signal;
|
|
43
|
+
const results = new Array(items.length);
|
|
44
|
+
let next = 0;
|
|
45
|
+
const workers = Array.from({
|
|
46
|
+
length: Math.min(DEFAULT_MULTIPART_CONCURRENCY, items.length)
|
|
47
|
+
}, async ()=>{
|
|
48
|
+
while(next < items.length){
|
|
49
|
+
partSignal.throwIfAborted();
|
|
50
|
+
const index = next++;
|
|
51
|
+
results[index] = await task(items[index], partSignal);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
try {
|
|
55
|
+
await Promise.all(workers);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
controller.abort(error);
|
|
58
|
+
await Promise.allSettled(workers);
|
|
59
|
+
throw signal?.aborted ? signal.reason : error;
|
|
60
|
+
}
|
|
61
|
+
signal?.throwIfAborted();
|
|
62
|
+
return results;
|
|
63
|
+
}
|
|
64
|
+
/** Reads one part of the source into memory. */ async function readPart(source, partNumber, partSizeBytes) {
|
|
65
|
+
const start = (partNumber - 1) * partSizeBytes;
|
|
66
|
+
return new Uint8Array(await source.slice(start, start + partSizeBytes).arrayBuffer());
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { assertSignedUrlAccessControl, createProgress, readPart, runConcurrently, signedUrlLifetime };
|
package/dist/version.d.ts
CHANGED
package/dist/version.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAC3D,eAAO,MAAM,0BAA0B,
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAC3D,eAAO,MAAM,0BAA0B,eAAe,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edgestore/server",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-rc.6",
|
|
4
4
|
"description": "Upload files with ease from React/Next.js",
|
|
5
5
|
"homepage": "https://edgestore.dev",
|
|
6
6
|
"license": "MIT",
|
|
@@ -78,11 +78,6 @@
|
|
|
78
78
|
"import": "./dist/adapters/start/index.js",
|
|
79
79
|
"default": "./dist/adapters/start/index.js"
|
|
80
80
|
},
|
|
81
|
-
"./core": {
|
|
82
|
-
"types": "./dist/core/index.d.ts",
|
|
83
|
-
"import": "./dist/core/index.js",
|
|
84
|
-
"default": "./dist/core/index.js"
|
|
85
|
-
},
|
|
86
81
|
"./providers/azure-blob": {
|
|
87
82
|
"types": "./dist/providers/azure-blob/index.d.ts",
|
|
88
83
|
"import": "./dist/providers/azure-blob/index.js",
|
|
@@ -107,19 +102,17 @@
|
|
|
107
102
|
"agent-docs",
|
|
108
103
|
"package.json",
|
|
109
104
|
"adapters",
|
|
110
|
-
"core",
|
|
111
105
|
"providers",
|
|
112
106
|
"!**/*.test.*",
|
|
113
107
|
"!**/__tests__"
|
|
114
108
|
],
|
|
115
109
|
"dependencies": {
|
|
116
|
-
"@edgestore/sdk": "1.0.0-
|
|
117
|
-
"@edgestore/shared": "1.0.0-
|
|
110
|
+
"@edgestore/sdk": "1.0.0-rc.6",
|
|
111
|
+
"@edgestore/shared": "1.0.0-rc.6",
|
|
118
112
|
"@standard-schema/spec": "^1.1.0",
|
|
119
|
-
"@panva/hkdf": "^1.2.1",
|
|
120
113
|
"cookie": "^2.0.1",
|
|
121
114
|
"jose": "^6.2.3",
|
|
122
|
-
"zod": "3.
|
|
115
|
+
"zod": "4.3.5"
|
|
123
116
|
},
|
|
124
117
|
"peerDependencies": {
|
|
125
118
|
"@aws-sdk/client-s3": ">=3.0.0",
|
package/src/adapters/shared.ts
CHANGED
|
@@ -10,13 +10,11 @@ import {
|
|
|
10
10
|
type SharedRequestUploadPartsRes,
|
|
11
11
|
type SharedRequestUploadRes,
|
|
12
12
|
} from '@edgestore/shared';
|
|
13
|
-
import { hkdf } from '@panva/hkdf';
|
|
14
13
|
import { stringifySetCookie } from 'cookie';
|
|
15
14
|
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
16
15
|
import { z } from 'zod';
|
|
17
16
|
import {
|
|
18
17
|
assertSupportedUploadOptions,
|
|
19
|
-
getProviderBaseUrl,
|
|
20
18
|
referenceFromUrl,
|
|
21
19
|
} from '../core/provider';
|
|
22
20
|
import { buildPath, parseBucketInput, parsePath } from '../core/routerRules';
|
|
@@ -90,31 +88,17 @@ type ResolvedCookieConfig = {
|
|
|
90
88
|
export function getCookieConfig(
|
|
91
89
|
cookieConfig?: CookieConfig,
|
|
92
90
|
): ResolvedCookieConfig {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const mergeOptions = (configOptions?: CookieOptions): CookieOptions => {
|
|
100
|
-
const merged = { ...defaultOptions };
|
|
101
|
-
|
|
102
|
-
if (configOptions) {
|
|
103
|
-
Object.keys(configOptions).forEach((key) => {
|
|
104
|
-
const value = configOptions[key as keyof CookieOptions];
|
|
105
|
-
if (value !== undefined) {
|
|
106
|
-
(merged as any)[key] = value;
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return merged;
|
|
112
|
-
};
|
|
91
|
+
// Explicit `undefined` options keep their defaults.
|
|
92
|
+
const configured = Object.fromEntries(
|
|
93
|
+
Object.entries(cookieConfig?.ctx?.options ?? {}).filter(
|
|
94
|
+
([, value]) => value !== undefined,
|
|
95
|
+
),
|
|
96
|
+
);
|
|
113
97
|
|
|
114
98
|
return {
|
|
115
99
|
ctx: {
|
|
116
100
|
name: cookieConfig?.ctx?.name ?? 'edgestore-ctx',
|
|
117
|
-
options:
|
|
101
|
+
options: { path: '/', maxAge: DEFAULT_MAX_AGE, ...configured },
|
|
118
102
|
},
|
|
119
103
|
};
|
|
120
104
|
}
|
|
@@ -132,7 +116,7 @@ export async function init<TCtx extends AnyContext>(params: {
|
|
|
132
116
|
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
133
117
|
|
|
134
118
|
const ctxToken = await encryptJWT(ctx);
|
|
135
|
-
const
|
|
119
|
+
const { clientInit } = await provider.init({ ctx, router });
|
|
136
120
|
const newCookies = [
|
|
137
121
|
stringifySetCookie({
|
|
138
122
|
name: resolvedCookieConfig.ctx.name,
|
|
@@ -140,22 +124,10 @@ export async function init<TCtx extends AnyContext>(params: {
|
|
|
140
124
|
...resolvedCookieConfig.ctx.options,
|
|
141
125
|
}),
|
|
142
126
|
];
|
|
143
|
-
const baseUrl = initRes.baseUrl ?? (await getProviderBaseUrl(provider));
|
|
144
|
-
|
|
145
|
-
logger.debug('Finished [init]', {
|
|
146
|
-
ctx,
|
|
147
|
-
newCookies,
|
|
148
|
-
baseUrl,
|
|
149
|
-
providerName: provider.name,
|
|
150
|
-
clientInit: initRes.clientInit,
|
|
151
|
-
});
|
|
152
127
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
providerName: provider.name,
|
|
157
|
-
clientInit: initRes.clientInit,
|
|
158
|
-
};
|
|
128
|
+
logger.debug('Finished [init]', { ctx, newCookies, clientInit });
|
|
129
|
+
|
|
130
|
+
return { newCookies, clientInit };
|
|
159
131
|
}
|
|
160
132
|
|
|
161
133
|
const nonEmptyStringSchema = z.string().min(1);
|
|
@@ -164,7 +136,7 @@ export const requestUploadBodySchema = z.object({
|
|
|
164
136
|
bucketName: nonEmptyStringSchema,
|
|
165
137
|
input: z.unknown(),
|
|
166
138
|
fileInfo: z.object({
|
|
167
|
-
size: z.number().
|
|
139
|
+
size: z.number().int().nonnegative(),
|
|
168
140
|
type: z.string(),
|
|
169
141
|
extension: z.string(),
|
|
170
142
|
fileName: z.string().optional(),
|
|
@@ -191,23 +163,9 @@ export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
191
163
|
} = params;
|
|
192
164
|
logger.debug('Running [requestUpload]', { bucketName, input, fileInfo });
|
|
193
165
|
|
|
194
|
-
if (!ctxToken) {
|
|
195
|
-
throw new EdgeStoreError({
|
|
196
|
-
message: 'Missing edgestore-ctx cookie',
|
|
197
|
-
code: 'UNAUTHORIZED',
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
166
|
const ctx = await getContext(ctxToken);
|
|
201
|
-
|
|
202
167
|
logger.debug('Decrypted Context', { ctx });
|
|
203
|
-
|
|
204
|
-
const bucket = router.buckets[bucketName];
|
|
205
|
-
if (!bucket) {
|
|
206
|
-
throw new EdgeStoreError({
|
|
207
|
-
message: `Bucket ${bucketName} not found`,
|
|
208
|
-
code: 'BAD_REQUEST',
|
|
209
|
-
});
|
|
210
|
-
}
|
|
168
|
+
const bucket = getBucket(router, bucketName);
|
|
211
169
|
assertSupportedUploadOptions(provider, fileInfo);
|
|
212
170
|
const parsedInput = await parseBucketInput(bucket, input);
|
|
213
171
|
if (bucket._def.beforeUpload) {
|
|
@@ -272,7 +230,6 @@ export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
272
230
|
return {
|
|
273
231
|
...requestUploadRes,
|
|
274
232
|
size: fileInfo.size,
|
|
275
|
-
uploadedAt: new Date().toISOString(), // TODO: maybe delete this field since it's not the actual upload time
|
|
276
233
|
path: parsedPath,
|
|
277
234
|
pathOrder,
|
|
278
235
|
metadata,
|
|
@@ -300,7 +257,7 @@ export const completeMultipartUploadBodySchema =
|
|
|
300
257
|
parts: z.array(
|
|
301
258
|
z.object({
|
|
302
259
|
partNumber: z.number().int().positive(),
|
|
303
|
-
eTag: nonEmptyStringSchema,
|
|
260
|
+
eTag: nonEmptyStringSchema.optional(),
|
|
304
261
|
}),
|
|
305
262
|
),
|
|
306
263
|
});
|
|
@@ -332,19 +289,8 @@ async function getMultipartUploads<TCtx extends AnyContext>({
|
|
|
332
289
|
ctxToken,
|
|
333
290
|
body: { bucketName },
|
|
334
291
|
}: MultipartRequest<TCtx, { bucketName: string }>) {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
message: 'Missing edgestore-ctx cookie',
|
|
338
|
-
code: 'UNAUTHORIZED',
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
await getContext(ctxToken); // just to check if the token is valid
|
|
342
|
-
if (!router.buckets[bucketName]) {
|
|
343
|
-
throw new EdgeStoreError({
|
|
344
|
-
message: `Bucket ${bucketName} not found`,
|
|
345
|
-
code: 'BAD_REQUEST',
|
|
346
|
-
});
|
|
347
|
-
}
|
|
292
|
+
await getContext(ctxToken);
|
|
293
|
+
getBucket(router, bucketName);
|
|
348
294
|
const multipartUploads = provider.uploads.multipart;
|
|
349
295
|
if (!multipartUploads) {
|
|
350
296
|
throw new EdgeStoreError({
|
|
@@ -423,20 +369,8 @@ export async function confirmUploads<TCtx extends AnyContext>(params: {
|
|
|
423
369
|
|
|
424
370
|
logger.debug('Running [confirmUploads]', { bucketName, urls });
|
|
425
371
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
message: 'Missing edgestore-ctx cookie',
|
|
429
|
-
code: 'UNAUTHORIZED',
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
await getContext(ctxToken); // just to check if the token is valid
|
|
433
|
-
const bucket = router.buckets[bucketName];
|
|
434
|
-
if (!bucket) {
|
|
435
|
-
throw new EdgeStoreError({
|
|
436
|
-
message: `Bucket ${bucketName} not found`,
|
|
437
|
-
code: 'BAD_REQUEST',
|
|
438
|
-
});
|
|
439
|
-
}
|
|
372
|
+
await getContext(ctxToken);
|
|
373
|
+
getBucket(router, bucketName);
|
|
440
374
|
|
|
441
375
|
if (!provider.files.confirm) {
|
|
442
376
|
throw new EdgeStoreError({
|
|
@@ -480,20 +414,8 @@ export async function deleteFiles<TCtx extends AnyContext>(params: {
|
|
|
480
414
|
|
|
481
415
|
logger.debug('Running [deleteFiles]', { bucketName, urls });
|
|
482
416
|
|
|
483
|
-
if (!ctxToken) {
|
|
484
|
-
throw new EdgeStoreError({
|
|
485
|
-
message: 'Missing edgestore-ctx cookie',
|
|
486
|
-
code: 'UNAUTHORIZED',
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
417
|
const ctx = await getContext(ctxToken);
|
|
490
|
-
const bucket = router
|
|
491
|
-
if (!bucket) {
|
|
492
|
-
throw new EdgeStoreError({
|
|
493
|
-
message: `Bucket ${bucketName} not found`,
|
|
494
|
-
code: 'BAD_REQUEST',
|
|
495
|
-
});
|
|
496
|
-
}
|
|
418
|
+
const bucket = getBucket(router, bucketName);
|
|
497
419
|
|
|
498
420
|
if (!bucket._def.beforeDelete) {
|
|
499
421
|
throw new EdgeStoreError({
|
|
@@ -580,38 +502,53 @@ function mapFrontendMutationResult(
|
|
|
580
502
|
return { succeeded, failed };
|
|
581
503
|
}
|
|
582
504
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
505
|
+
function getBucket<TCtx extends AnyContext>(
|
|
506
|
+
router: EdgeStoreRouter<TCtx>,
|
|
507
|
+
bucketName: string,
|
|
508
|
+
) {
|
|
509
|
+
const bucket = router.buckets[bucketName];
|
|
510
|
+
if (!bucket) {
|
|
587
511
|
throw new EdgeStoreError({
|
|
588
|
-
message:
|
|
589
|
-
code: '
|
|
512
|
+
message: `Bucket ${bucketName} not found`,
|
|
513
|
+
code: 'BAD_REQUEST',
|
|
590
514
|
});
|
|
591
515
|
}
|
|
592
|
-
|
|
516
|
+
return bucket;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
async function encryptJWT(ctx: AnyContext) {
|
|
593
520
|
return await new EncryptJWT({ ctx })
|
|
594
521
|
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
|
|
595
522
|
.setIssuedAt()
|
|
596
523
|
.setExpirationTime(Date.now() / 1000 + DEFAULT_MAX_AGE)
|
|
597
524
|
.setJti(crypto.randomUUID())
|
|
598
|
-
.encrypt(
|
|
525
|
+
.encrypt(await getEncryptionKey());
|
|
599
526
|
}
|
|
600
527
|
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
528
|
+
const contextPayloadSchema = z.object({
|
|
529
|
+
ctx: z.record(z.string(), z.string()),
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
/** Decrypts and validates the `edgestore-ctx` cookie set by `/init`. */
|
|
533
|
+
async function getContext(token: string | undefined) {
|
|
534
|
+
if (!token) {
|
|
605
535
|
throw new EdgeStoreError({
|
|
606
|
-
message: '
|
|
607
|
-
code: '
|
|
536
|
+
message: 'Missing edgestore-ctx cookie',
|
|
537
|
+
code: 'UNAUTHORIZED',
|
|
608
538
|
});
|
|
609
539
|
}
|
|
610
|
-
const
|
|
611
|
-
const
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
540
|
+
const key = await getEncryptionKey();
|
|
541
|
+
const payload = await jwtDecrypt(token, key, { clockTolerance: 15 }).then(
|
|
542
|
+
(result) => result.payload,
|
|
543
|
+
(error: unknown) => {
|
|
544
|
+
throw new EdgeStoreError({
|
|
545
|
+
message: 'Invalid edgestore-ctx cookie',
|
|
546
|
+
code: 'UNAUTHORIZED',
|
|
547
|
+
cause: error instanceof Error ? error : undefined,
|
|
548
|
+
});
|
|
549
|
+
},
|
|
550
|
+
);
|
|
551
|
+
const result = contextPayloadSchema.safeParse(payload);
|
|
615
552
|
if (!result.success) {
|
|
616
553
|
throw new EdgeStoreError({
|
|
617
554
|
message: 'Invalid edgestore-ctx cookie',
|
|
@@ -622,16 +559,33 @@ async function decryptJWT(token: string) {
|
|
|
622
559
|
return result.data.ctx;
|
|
623
560
|
}
|
|
624
561
|
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
562
|
+
/** Derives the context-cookie encryption key from the configured secret. */
|
|
563
|
+
async function getEncryptionKey() {
|
|
564
|
+
const secret =
|
|
565
|
+
getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
566
|
+
if (!secret) {
|
|
567
|
+
throw new EdgeStoreError({
|
|
568
|
+
message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
|
|
569
|
+
code: 'SERVER_ERROR',
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
const encoder = new TextEncoder();
|
|
573
|
+
const material = await crypto.subtle.importKey(
|
|
574
|
+
'raw',
|
|
575
|
+
encoder.encode(secret),
|
|
576
|
+
'HKDF',
|
|
577
|
+
false,
|
|
578
|
+
['deriveBits'],
|
|
632
579
|
);
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
580
|
+
const bits = await crypto.subtle.deriveBits(
|
|
581
|
+
{
|
|
582
|
+
name: 'HKDF',
|
|
583
|
+
hash: 'SHA-256',
|
|
584
|
+
salt: new Uint8Array(),
|
|
585
|
+
info: encoder.encode('EdgeStore Generated Encryption Key'),
|
|
586
|
+
},
|
|
587
|
+
material,
|
|
588
|
+
256,
|
|
589
|
+
);
|
|
590
|
+
return new Uint8Array(bits);
|
|
637
591
|
}
|
package/src/core/client/index.ts
CHANGED
|
@@ -427,7 +427,3 @@ export type InferClientOutputs<TRouter extends { readonly client: object }> = {
|
|
|
427
427
|
>;
|
|
428
428
|
};
|
|
429
429
|
};
|
|
430
|
-
|
|
431
|
-
/** @deprecated Use {@link InferClientOutputs} instead. */
|
|
432
|
-
export type InferClientResponse<TRouter extends { readonly client: object }> =
|
|
433
|
-
InferClientOutputs<TRouter>;
|