@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
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AnyEdgeStoreProvider, EdgeStoreProvider, ProviderCursor, ProviderReference } from '@edgestore/shared';
|
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
+
export declare function defineProvider<const TReferenceSchema extends StandardSchemaV1, const TCursorSchema extends StandardSchemaV1 = StandardSchemaV1<unknown, string>, const TProvider extends EdgeStoreProvider<TReferenceSchema, StandardSchemaV1.InferOutput<TCursorSchema>> = EdgeStoreProvider<TReferenceSchema, StandardSchemaV1.InferOutput<TCursorSchema>>>(provider: TProvider & {
|
|
4
|
+
reference: {
|
|
5
|
+
schema: TReferenceSchema;
|
|
6
|
+
};
|
|
7
|
+
files: {
|
|
8
|
+
cursorSchema?: TCursorSchema;
|
|
9
|
+
};
|
|
10
|
+
}): TProvider;
|
|
11
|
+
export declare function getProviderBaseUrl(provider: AnyEdgeStoreProvider): Promise<string>;
|
|
12
|
+
export declare function referenceFromUrl<TProvider extends AnyEdgeStoreProvider>(provider: TProvider, url: string): Promise<ProviderReference<TProvider>>;
|
|
13
|
+
export declare function validateProviderReference<TProvider extends AnyEdgeStoreProvider>(provider: TProvider, reference: unknown): Promise<ProviderReference<TProvider>>;
|
|
14
|
+
export declare function validateProviderCursor<TProvider extends AnyEdgeStoreProvider>(provider: TProvider, cursor: unknown): Promise<ProviderCursor<TProvider>>;
|
|
15
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/core/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,wBAAgB,cAAc,CAC5B,KAAK,CAAC,gBAAgB,SAAS,gBAAgB,EAC/C,KAAK,CAAC,aAAa,SAAS,gBAAgB,GAAG,gBAAgB,CAC7D,OAAO,EACP,MAAM,CACP,EACD,KAAK,CAAC,SAAS,SAAS,iBAAiB,CACvC,gBAAgB,EAChB,gBAAgB,CAAC,WAAW,CAAC,aAAa,CAAC,CAC5C,GAAG,iBAAiB,CACnB,gBAAgB,EAChB,gBAAgB,CAAC,WAAW,CAAC,aAAa,CAAC,CAC5C,EAED,QAAQ,EAAE,SAAS,GAAG;IACpB,SAAS,EAAE;QAAE,MAAM,EAAE,gBAAgB,CAAA;KAAE,CAAC;IACxC,KAAK,EAAE;QAAE,YAAY,CAAC,EAAE,aAAa,CAAA;KAAE,CAAC;CACzC,GACA,SAAS,CAEX;AAED,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,oBAAoB,GAC7B,OAAO,CAAC,MAAM,CAAC,CAIjB;AAED,wBAAsB,gBAAgB,CAAC,SAAS,SAAS,oBAAoB,EAC3E,QAAQ,EAAE,SAAS,EACnB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,CAMvC;AAED,wBAAsB,yBAAyB,CAC7C,SAAS,SAAS,oBAAoB,EAEtC,QAAQ,EAAE,SAAS,EACnB,SAAS,EAAE,OAAO,GACjB,OAAO,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,CAMvC;AAED,wBAAsB,sBAAsB,CAC1C,SAAS,SAAS,oBAAoB,EACtC,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC,CAI1E"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
function defineProvider(provider) {
|
|
2
|
+
return provider;
|
|
3
|
+
}
|
|
4
|
+
async function getProviderBaseUrl(provider) {
|
|
5
|
+
return typeof provider.baseUrl === 'function' ? await provider.baseUrl() : provider.baseUrl;
|
|
6
|
+
}
|
|
7
|
+
async function referenceFromUrl(provider, url) {
|
|
8
|
+
return await validateProviderValue(provider.reference.schema, await provider.reference.fromUrl(url), 'file reference');
|
|
9
|
+
}
|
|
10
|
+
async function validateProviderReference(provider, reference) {
|
|
11
|
+
return await validateProviderValue(provider.reference.schema, reference, 'file reference');
|
|
12
|
+
}
|
|
13
|
+
async function validateProviderCursor(provider, cursor) {
|
|
14
|
+
const schema = provider.files.cursorSchema;
|
|
15
|
+
if (!schema) return cursor;
|
|
16
|
+
return await validateProviderValue(schema, cursor, 'list cursor');
|
|
17
|
+
}
|
|
18
|
+
async function validateProviderValue(schema, value, label) {
|
|
19
|
+
const result = await schema['~standard'].validate(value);
|
|
20
|
+
if (!result.issues) return result.value;
|
|
21
|
+
const detail = result.issues.map((issue)=>issue.message).join('; ');
|
|
22
|
+
throw new TypeError(`Invalid provider ${label}: ${detail}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { defineProvider, getProviderBaseUrl, referenceFromUrl, validateProviderCursor, validateProviderReference };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type AnyBuilder, type InferBucketPathObject, type InferBucketPathOrder } from '@edgestore/shared';
|
|
2
|
+
export declare function parseBucketInput<TBucket extends AnyBuilder>(bucket: TBucket, input: unknown): Promise<Record<string, never> | Record<string, unknown>>;
|
|
3
|
+
export declare function buildPath(params: {
|
|
4
|
+
bucket: AnyBuilder;
|
|
5
|
+
pathAttrs: {
|
|
6
|
+
ctx: unknown;
|
|
7
|
+
input: unknown;
|
|
8
|
+
};
|
|
9
|
+
}): {
|
|
10
|
+
key: string;
|
|
11
|
+
value: string;
|
|
12
|
+
}[];
|
|
13
|
+
export declare function parsePath<TBucket extends AnyBuilder>(path: {
|
|
14
|
+
key: string;
|
|
15
|
+
value: string;
|
|
16
|
+
}[]): {
|
|
17
|
+
parsedPath: InferBucketPathObject<TBucket>;
|
|
18
|
+
pathOrder: InferBucketPathOrder<TBucket>;
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=routerRules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routerRules.d.ts","sourceRoot":"","sources":["../../src/core/routerRules.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,UAAU,EACf,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EAC1B,MAAM,mBAAmB,CAAC;AAE3B,wBAAsB,gBAAgB,CAAC,OAAO,SAAS,UAAU,EAC/D,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,OAAO,4DAGf;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE;IAChC,MAAM,EAAE,UAAU,CAAC;IACnB,SAAS,EAAE;QACT,GAAG,EAAE,OAAO,CAAC;QACb,KAAK,EAAE,OAAO,CAAC;KAChB,CAAC;CACH;;WA2BqC,MAAM;IAE3C;AAED,wBAAgB,SAAS,CAAC,OAAO,SAAS,UAAU,EAClD,IAAI,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,GAKjC;IACH,UAAU,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC;IAC3C,SAAS,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;CAC1C,CACF"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { parseBucketInput as parseBucketInput$1, EdgeStoreError } from '@edgestore/shared';
|
|
2
|
+
|
|
3
|
+
async function parseBucketInput(bucket, input) {
|
|
4
|
+
return parseBucketInput$1(bucket._def.input, input);
|
|
5
|
+
}
|
|
6
|
+
function buildPath(params) {
|
|
7
|
+
const pathParams = params.bucket._def.path;
|
|
8
|
+
return pathParams.map((param)=>{
|
|
9
|
+
const entry = Object.entries(param)[0];
|
|
10
|
+
if (!entry) {
|
|
11
|
+
throw new EdgeStoreError({
|
|
12
|
+
message: `Empty path param found in: ${JSON.stringify(pathParams)}`,
|
|
13
|
+
code: 'SERVER_ERROR'
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
const [key, value] = entry;
|
|
17
|
+
const pathValue = value().split('.').reduce((current, segment)=>{
|
|
18
|
+
if (typeof current !== 'object' || current === null || !(segment in current)) {
|
|
19
|
+
throw new EdgeStoreError({
|
|
20
|
+
message: `Missing key ${segment} in ${JSON.stringify(current)}`,
|
|
21
|
+
code: 'BAD_REQUEST'
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return current[segment];
|
|
25
|
+
}, params.pathAttrs);
|
|
26
|
+
return {
|
|
27
|
+
key,
|
|
28
|
+
value: pathValue
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function parsePath(path) {
|
|
33
|
+
return {
|
|
34
|
+
parsedPath: Object.fromEntries(path.map(({ key, value })=>[
|
|
35
|
+
key,
|
|
36
|
+
value
|
|
37
|
+
])),
|
|
38
|
+
pathOrder: path.map(({ key })=>key)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { buildPath, parseBucketInput, parsePath };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateFile.d.ts","sourceRoot":"","sources":["../../src/core/validateFile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGpE,wBAAgB,qBAAqB,CAAC,MAAM,EAAE;IAC5C,MAAM,EAAE,UAAU,CAAC;IACnB,QAAQ,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CAC1C,QAqCA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { EdgeStoreError } from '@edgestore/shared';
|
|
2
|
+
import { IMAGE_MIME_TYPES } from '../adapters/imageTypes.js';
|
|
3
|
+
|
|
4
|
+
function validateFileForBucket(params) {
|
|
5
|
+
const { bucket, fileInfo } = params;
|
|
6
|
+
if (bucket._def.type === 'IMAGE' && !IMAGE_MIME_TYPES.includes(fileInfo.type)) {
|
|
7
|
+
throw new EdgeStoreError({
|
|
8
|
+
code: 'MIME_TYPE_NOT_ALLOWED',
|
|
9
|
+
message: 'Only images are allowed in this bucket',
|
|
10
|
+
details: {
|
|
11
|
+
allowedMimeTypes: IMAGE_MIME_TYPES,
|
|
12
|
+
mimeType: fileInfo.type
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
const maxSize = bucket._def.bucketConfig?.maxSize;
|
|
17
|
+
if (maxSize !== undefined && fileInfo.size > maxSize) {
|
|
18
|
+
throw new EdgeStoreError({
|
|
19
|
+
code: 'FILE_TOO_LARGE',
|
|
20
|
+
message: `File size is too big. Max size is ${maxSize}`,
|
|
21
|
+
details: {
|
|
22
|
+
maxFileSize: maxSize,
|
|
23
|
+
fileSize: fileInfo.size
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
const accept = bucket._def.bucketConfig?.accept;
|
|
28
|
+
if (accept && !accept.some((value)=>matchesMimeType(fileInfo.type, value))) {
|
|
29
|
+
throw new EdgeStoreError({
|
|
30
|
+
code: 'MIME_TYPE_NOT_ALLOWED',
|
|
31
|
+
message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
|
|
32
|
+
details: {
|
|
33
|
+
allowedMimeTypes: accept,
|
|
34
|
+
mimeType: fileInfo.type
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function matchesMimeType(mimeType, accepted) {
|
|
40
|
+
return accepted.endsWith('/*') ? mimeType.startsWith(accepted.slice(0, -1)) : mimeType === accepted;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { validateFileForBucket };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { initEdgeStore } from '@edgestore/shared';
|
|
2
|
+
export { createEdgeStore, defineProvider, type ConfiguredEdgeStore, type EdgeStoreClient, type InferClientInputs, type InferClientOutputs, type InferClientResponse, EdgeStoreFileMutationError, } from './core/index.js';
|
|
2
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EACL,eAAe,EACf,cAAc,EACd,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,EACxB,0BAA0B,GAC3B,MAAM,QAAQ,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/libs/env.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAOtD;AAED,wBAAgB,KAAK,IAAI,OAAO,CAM/B"}
|
package/dist/libs/env.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function getEnv(key) {
|
|
2
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
3
|
+
// @ts-expect-error - In Vite/Astro, env variables are available on import.meta.
|
|
4
|
+
return process.env[key] ?? import.meta.env?.[key];
|
|
5
|
+
}
|
|
6
|
+
// @ts-expect-error - In Vite/Astro, env variables are available on import.meta.
|
|
7
|
+
return import.meta.env?.[key];
|
|
8
|
+
}
|
|
9
|
+
function isDev() {
|
|
10
|
+
return process?.env?.NODE_ENV === 'development' || // @ts-expect-error - In Vite/Astro, env variables are available on import.meta.
|
|
11
|
+
import.meta.env?.DEV;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { getEnv, isDev };
|
package/dist/libs/logger.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
declare const logLevel: readonly ["debug", "info", "warn", "error", "none"];
|
|
2
2
|
export type LogLevel = (typeof logLevel)[number];
|
|
3
|
-
|
|
3
|
+
export type LoggerLike = Pick<Console, 'debug' | 'info' | 'warn' | 'error'>;
|
|
4
|
+
declare class Logger implements LoggerLike {
|
|
4
5
|
private logLevel;
|
|
5
6
|
constructor(logLevel?: LogLevel);
|
|
6
7
|
private shouldLog;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/libs/logger.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,QAAQ,qDAAsD,CAAC;AAErE,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjD,
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/libs/logger.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,QAAQ,qDAAsD,CAAC;AAErE,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjD,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;AAE5E,cAAM,MAAO,YAAW,UAAU;IAChC,OAAO,CAAC,QAAQ,CAAW;gBAEf,QAAQ,CAAC,EAAE,QAAQ;IAI/B,OAAO,CAAC,SAAS;IAIjB,KAAK,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,EAAE,GAAG,IAAI;IAMpD,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,EAAE,GAAG,IAAI;IAMnD,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,EAAE,GAAG,IAAI;IAMnD,KAAK,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,EAAE,GAAG,IAAI;CAKrD;AAED,eAAe,MAAM,CAAC"}
|
package/dist/libs/logger.js
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { type RequestUploadParams } from '@edgestore/shared';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the Azure provider. Compatible with Azure Blob Storage and Azurite.
|
|
5
|
+
* Use Azure Storage Explorer for local development with Azurite.
|
|
6
|
+
* @see https://azure.microsoft.com/de-de/products/storage/storage-explorer
|
|
7
|
+
* @category Providers
|
|
8
|
+
* @example
|
|
9
|
+
* azureBlob({
|
|
10
|
+
* storageAccountName: 'devstoreaccount1',
|
|
11
|
+
* storageAccountKey: 'some-account-key',
|
|
12
|
+
* containerName: 'some-container-name',
|
|
13
|
+
* customBaseUrl: 'http://localhost:10000/devstoreaccount1',
|
|
14
|
+
* })
|
|
15
|
+
*/
|
|
16
|
+
export type AzureBlobProviderOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* The storage account name for Azure Blob Storage
|
|
19
|
+
* Can also be set via the `ES_AZURE_ACCOUNT_NAME` environment variable.
|
|
20
|
+
*/
|
|
21
|
+
storageAccountName?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Account key used to authenticate server-side operations and sign
|
|
24
|
+
* short-lived, blob-scoped URLs.
|
|
25
|
+
* Can also be set via the `ES_AZURE_ACCOUNT_KEY` environment variable.
|
|
26
|
+
*/
|
|
27
|
+
storageAccountKey?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Azure Blob Storage container name
|
|
30
|
+
* Can also be set via the `ES_AZURE_CONTAINER_NAME` environment variable.
|
|
31
|
+
*/
|
|
32
|
+
containerName?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Optional base URL for the Azure Blob Storage.
|
|
35
|
+
* Useful for local development with Azurite. For example: `http://localhost:10000/devstoreaccount1`
|
|
36
|
+
* Can also be set via the `ES_AZURE_BASE_URL` environment variable.
|
|
37
|
+
*/
|
|
38
|
+
customBaseUrl?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Lifetime of generated upload URLs in seconds.
|
|
41
|
+
* @default 3600
|
|
42
|
+
*/
|
|
43
|
+
uploadUrlExpiresIn?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Default lifetime of generated private read URLs in seconds.
|
|
46
|
+
* @default 3600
|
|
47
|
+
*/
|
|
48
|
+
signedUrlExpiresIn?: number;
|
|
49
|
+
};
|
|
50
|
+
export declare function azureBlob(options?: AzureBlobProviderOptions): {
|
|
51
|
+
readonly name: "azure-blob";
|
|
52
|
+
readonly baseUrl: string;
|
|
53
|
+
readonly reference: {
|
|
54
|
+
readonly schema: z.ZodObject<{
|
|
55
|
+
url: z.ZodString;
|
|
56
|
+
}, "strip", z.ZodTypeAny, {
|
|
57
|
+
url: string;
|
|
58
|
+
}, {
|
|
59
|
+
url: string;
|
|
60
|
+
}>;
|
|
61
|
+
readonly fromUrl: (url: string) => {
|
|
62
|
+
url: string;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
readonly init: () => Promise<{}>;
|
|
66
|
+
readonly uploads: {
|
|
67
|
+
readonly request: (params: RequestUploadParams) => Promise<{
|
|
68
|
+
uploadUrl: string;
|
|
69
|
+
accessUrl: string;
|
|
70
|
+
accessSignedUrl: string | undefined;
|
|
71
|
+
accessSignedUrlExpiresAt: Date | undefined;
|
|
72
|
+
accessSignedUrlExpiresIn: number | undefined;
|
|
73
|
+
}>;
|
|
74
|
+
};
|
|
75
|
+
readonly files: {
|
|
76
|
+
readonly get: ({ bucketName, file }: {
|
|
77
|
+
bucketName: string;
|
|
78
|
+
file: {
|
|
79
|
+
url: string;
|
|
80
|
+
};
|
|
81
|
+
}) => Promise<{
|
|
82
|
+
url: string;
|
|
83
|
+
sizeBytes: number;
|
|
84
|
+
uploadedAt: Date;
|
|
85
|
+
updatedAt: Date;
|
|
86
|
+
}>;
|
|
87
|
+
readonly getSignedUrls: ({ bucketName, files, expiresIn }: {
|
|
88
|
+
bucketName: string;
|
|
89
|
+
files: {
|
|
90
|
+
url: string;
|
|
91
|
+
}[];
|
|
92
|
+
expiresIn?: number;
|
|
93
|
+
includeThumbnails?: boolean;
|
|
94
|
+
}) => Promise<{
|
|
95
|
+
url: string;
|
|
96
|
+
signedUrl: string;
|
|
97
|
+
expiresAt: Date;
|
|
98
|
+
expiresIn: number;
|
|
99
|
+
}[]>;
|
|
100
|
+
readonly delete: ({ bucketName, files }: {
|
|
101
|
+
bucketName: string;
|
|
102
|
+
files: {
|
|
103
|
+
url: string;
|
|
104
|
+
}[];
|
|
105
|
+
}) => Promise<{
|
|
106
|
+
results: ({
|
|
107
|
+
success: true;
|
|
108
|
+
error?: undefined;
|
|
109
|
+
} | {
|
|
110
|
+
success: false;
|
|
111
|
+
error: {
|
|
112
|
+
code: "DELETE_FAILED";
|
|
113
|
+
message: string;
|
|
114
|
+
};
|
|
115
|
+
})[];
|
|
116
|
+
}>;
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/azure-blob/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA2M3D"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { StorageSharedKeyCredential, BlobServiceClient, generateBlobSASQueryParameters, SASProtocol, BlobSASPermissions } from '@azure/storage-blob';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { defineProvider } from '../../core/provider.js';
|
|
4
|
+
import { getEnv } from '../../libs/env.js';
|
|
5
|
+
|
|
6
|
+
function azureBlob(options) {
|
|
7
|
+
const { storageAccountName = getEnv('ES_AZURE_ACCOUNT_NAME'), storageAccountKey = getEnv('ES_AZURE_ACCOUNT_KEY'), containerName = getEnv('ES_AZURE_CONTAINER_NAME'), customBaseUrl = getEnv('ES_AZURE_BASE_URL'), uploadUrlExpiresIn = 60 * 60, signedUrlExpiresIn = 60 * 60 } = options ?? {};
|
|
8
|
+
if (!storageAccountName) {
|
|
9
|
+
throw new Error('Azure storageAccountName is not configured in AzureBlobProviderOptions.');
|
|
10
|
+
}
|
|
11
|
+
if (!storageAccountKey) {
|
|
12
|
+
throw new Error('Azure storageAccountKey is not configured in AzureBlobProviderOptions.');
|
|
13
|
+
}
|
|
14
|
+
if (!containerName) {
|
|
15
|
+
throw new Error('Azure containerName is not configured in AzureBlobProviderOptions.');
|
|
16
|
+
}
|
|
17
|
+
const resolvedContainerName = containerName;
|
|
18
|
+
const baseUrl = customBaseUrl ?? `https://${storageAccountName}.blob.core.windows.net`;
|
|
19
|
+
const sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
|
|
20
|
+
const blobServiceClient = new BlobServiceClient(baseUrl, sharedKeyCredential);
|
|
21
|
+
const containerClient = blobServiceClient.getContainerClient(resolvedContainerName);
|
|
22
|
+
function createSignedBlobUrl(params) {
|
|
23
|
+
const expiresAt = new Date(Date.now() + params.expiresIn * 1000);
|
|
24
|
+
const sas = generateBlobSASQueryParameters({
|
|
25
|
+
containerName: resolvedContainerName,
|
|
26
|
+
blobName: params.blobName,
|
|
27
|
+
permissions: BlobSASPermissions.parse(params.permissions),
|
|
28
|
+
protocol: baseUrl.startsWith('https:') ? SASProtocol.Https : SASProtocol.HttpsAndHttp,
|
|
29
|
+
startsOn: new Date(Date.now() - 5 * 60 * 1000),
|
|
30
|
+
expiresOn: expiresAt
|
|
31
|
+
}, sharedKeyCredential).toString();
|
|
32
|
+
const blobClient = containerClient.getBlobClient(params.blobName);
|
|
33
|
+
return {
|
|
34
|
+
url: blobClient.url.split('?')[0],
|
|
35
|
+
signedUrl: `${blobClient.url.split('?')[0]}?${sas}`,
|
|
36
|
+
expiresAt,
|
|
37
|
+
expiresIn: params.expiresIn
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function getBlobNameFromUrl(url) {
|
|
41
|
+
try {
|
|
42
|
+
const blobUrl = new URL(url);
|
|
43
|
+
const containerUrl = new URL(containerClient.url);
|
|
44
|
+
const containerPath = containerUrl.pathname.replace(/\/$/, '');
|
|
45
|
+
if (blobUrl.origin !== containerUrl.origin) {
|
|
46
|
+
throw new Error();
|
|
47
|
+
}
|
|
48
|
+
if (!blobUrl.pathname.startsWith(`${containerPath}/`)) {
|
|
49
|
+
throw new Error();
|
|
50
|
+
}
|
|
51
|
+
return decodeURIComponent(blobUrl.pathname.slice(containerPath.length + 1));
|
|
52
|
+
} catch {
|
|
53
|
+
throw new Error('File URL does not belong to this Azure Blob provider.');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function getBucketBlobName(edgestoreBucketName, url) {
|
|
57
|
+
const blobName = getBlobNameFromUrl(url);
|
|
58
|
+
if (!blobName.startsWith(`${edgestoreBucketName}/`)) {
|
|
59
|
+
throw new Error(`File does not belong to EdgeStore bucket "${edgestoreBucketName}".`);
|
|
60
|
+
}
|
|
61
|
+
return blobName;
|
|
62
|
+
}
|
|
63
|
+
function getBlobName(params) {
|
|
64
|
+
const { bucketName: esBucketName, fileInfo } = params;
|
|
65
|
+
const extension = fileInfo.extension ? `.${fileInfo.extension.replace('.', '')}` : '';
|
|
66
|
+
const fileName = fileInfo.fileName ?? `${crypto.randomUUID()}${extension}`;
|
|
67
|
+
return [
|
|
68
|
+
esBucketName,
|
|
69
|
+
...fileInfo.isPublic ? [
|
|
70
|
+
'_public'
|
|
71
|
+
] : [],
|
|
72
|
+
...fileInfo.path.map((item)=>item.value),
|
|
73
|
+
fileName
|
|
74
|
+
].join('/');
|
|
75
|
+
}
|
|
76
|
+
return defineProvider({
|
|
77
|
+
name: 'azure-blob',
|
|
78
|
+
baseUrl,
|
|
79
|
+
reference: {
|
|
80
|
+
schema: z.object({
|
|
81
|
+
url: z.string()
|
|
82
|
+
}),
|
|
83
|
+
fromUrl: (url)=>({
|
|
84
|
+
url
|
|
85
|
+
})
|
|
86
|
+
},
|
|
87
|
+
async init () {
|
|
88
|
+
return {};
|
|
89
|
+
},
|
|
90
|
+
uploads: {
|
|
91
|
+
async request (params) {
|
|
92
|
+
const blobName = getBlobName(params);
|
|
93
|
+
const uploadAccess = createSignedBlobUrl({
|
|
94
|
+
blobName,
|
|
95
|
+
permissions: 'cw',
|
|
96
|
+
expiresIn: uploadUrlExpiresIn
|
|
97
|
+
});
|
|
98
|
+
const readAccess = params.fileInfo.isPublic ? undefined : createSignedBlobUrl({
|
|
99
|
+
blobName,
|
|
100
|
+
permissions: 'r',
|
|
101
|
+
expiresIn: signedUrlExpiresIn
|
|
102
|
+
});
|
|
103
|
+
return {
|
|
104
|
+
uploadUrl: uploadAccess.signedUrl,
|
|
105
|
+
accessUrl: uploadAccess.url,
|
|
106
|
+
accessSignedUrl: readAccess?.signedUrl,
|
|
107
|
+
accessSignedUrlExpiresAt: readAccess?.expiresAt,
|
|
108
|
+
accessSignedUrlExpiresIn: readAccess?.expiresIn
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
files: {
|
|
113
|
+
async get ({ bucketName, file }) {
|
|
114
|
+
const blobClient = containerClient.getBlobClient(getBucketBlobName(bucketName, file.url));
|
|
115
|
+
const { contentLength, lastModified } = await blobClient.getProperties();
|
|
116
|
+
const timestamp = lastModified ?? new Date();
|
|
117
|
+
return {
|
|
118
|
+
url: blobClient.url.split('?')[0],
|
|
119
|
+
sizeBytes: contentLength ?? 0,
|
|
120
|
+
uploadedAt: timestamp,
|
|
121
|
+
updatedAt: timestamp
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
async getSignedUrls ({ bucketName, files, expiresIn }) {
|
|
125
|
+
return files.map((file)=>{
|
|
126
|
+
const access = createSignedBlobUrl({
|
|
127
|
+
blobName: getBucketBlobName(bucketName, file.url),
|
|
128
|
+
permissions: 'r',
|
|
129
|
+
expiresIn: expiresIn ?? signedUrlExpiresIn
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
url: access.url,
|
|
133
|
+
signedUrl: access.signedUrl,
|
|
134
|
+
expiresAt: access.expiresAt,
|
|
135
|
+
expiresIn: access.expiresIn
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
async delete ({ bucketName, files }) {
|
|
140
|
+
const blobNames = files.map((file)=>getBucketBlobName(bucketName, file.url));
|
|
141
|
+
const results = await Promise.all(blobNames.map(async (blobName)=>{
|
|
142
|
+
try {
|
|
143
|
+
const blobClient = containerClient.getBlobClient(blobName);
|
|
144
|
+
await blobClient.delete();
|
|
145
|
+
return {
|
|
146
|
+
success: true
|
|
147
|
+
};
|
|
148
|
+
} catch (error) {
|
|
149
|
+
return {
|
|
150
|
+
success: false,
|
|
151
|
+
error: {
|
|
152
|
+
code: 'DELETE_FAILED',
|
|
153
|
+
message: error instanceof Error ? error.message : 'Delete failed'
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}));
|
|
158
|
+
return {
|
|
159
|
+
results
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { azureBlob };
|