bunsane 0.1.4 → 0.2.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/.claude/settings.local.json +47 -0
- package/.claude/skills/update-memory.md +74 -0
- package/.prettierrc +4 -0
- package/.serena/memories/architectural-decision-no-dependency-injection.md +76 -0
- package/.serena/memories/architecture.md +154 -0
- package/.serena/memories/cache-interface-refactoring-2026-01-24.md +165 -0
- package/.serena/memories/code_style_and_conventions.md +76 -0
- package/.serena/memories/project_overview.md +43 -0
- package/.serena/memories/schema-dsl-plan.md +107 -0
- package/.serena/memories/suggested_commands.md +80 -0
- package/.serena/memories/typescript-compilation-status.md +54 -0
- package/.serena/project.yml +114 -0
- package/TODO.md +1 -7
- package/bun.lock +150 -4
- package/bunfig.toml +10 -0
- package/config/cache.config.ts +77 -0
- package/config/upload.config.ts +4 -5
- package/core/App.ts +870 -123
- package/core/ArcheType.ts +2268 -377
- package/core/BatchLoader.ts +181 -71
- package/core/Config.ts +153 -0
- package/core/Decorators.ts +4 -1
- package/core/Entity.ts +621 -92
- package/core/EntityHookManager.ts +1 -1
- package/core/EntityInterface.ts +3 -1
- package/core/EntityManager.ts +1 -13
- package/core/ErrorHandler.ts +8 -2
- package/core/Logger.ts +9 -0
- package/core/Middleware.ts +34 -0
- package/core/RequestContext.ts +5 -1
- package/core/RequestLoaders.ts +227 -93
- package/core/SchedulerManager.ts +193 -52
- package/core/cache/CacheAnalytics.ts +399 -0
- package/core/cache/CacheFactory.ts +145 -0
- package/core/cache/CacheManager.ts +520 -0
- package/core/cache/CacheProvider.ts +34 -0
- package/core/cache/CacheWarmer.ts +157 -0
- package/core/cache/CompressionUtils.ts +110 -0
- package/core/cache/MemoryCache.ts +251 -0
- package/core/cache/MultiLevelCache.ts +180 -0
- package/core/cache/NoOpCache.ts +53 -0
- package/core/cache/RedisCache.ts +464 -0
- package/core/cache/TTLStrategy.ts +254 -0
- package/core/cache/index.ts +6 -0
- package/core/components/BaseComponent.ts +120 -0
- package/core/{ComponentRegistry.ts → components/ComponentRegistry.ts} +148 -54
- package/core/components/Decorators.ts +88 -0
- package/core/components/Interfaces.ts +7 -0
- package/core/components/index.ts +5 -0
- package/core/decorators/EntityHooks.ts +0 -3
- package/core/decorators/IndexedField.ts +26 -0
- package/core/decorators/ScheduledTask.ts +0 -47
- package/core/events/EntityLifecycleEvents.ts +1 -1
- package/core/health.ts +112 -0
- package/core/metadata/definitions/ArcheType.ts +14 -0
- package/core/metadata/definitions/Component.ts +9 -0
- package/core/metadata/definitions/gqlObject.ts +1 -1
- package/core/metadata/index.ts +42 -1
- package/core/metadata/metadata-storage.ts +28 -2
- package/core/middleware/AccessLog.ts +59 -0
- package/core/middleware/RequestId.ts +38 -0
- package/core/middleware/SecurityHeaders.ts +62 -0
- package/core/middleware/index.ts +3 -0
- package/core/scheduler/DistributedLock.ts +266 -0
- package/core/scheduler/index.ts +15 -0
- package/core/validateEnv.ts +92 -0
- package/database/DatabaseHelper.ts +416 -40
- package/database/IndexingStrategy.ts +342 -0
- package/database/PreparedStatementCache.ts +226 -0
- package/database/index.ts +32 -7
- package/database/sqlHelpers.ts +14 -2
- package/endpoints/archetypes.ts +362 -0
- package/endpoints/components.ts +58 -0
- package/endpoints/entity.ts +80 -0
- package/endpoints/index.ts +27 -0
- package/endpoints/query.ts +93 -0
- package/endpoints/stats.ts +76 -0
- package/endpoints/tables.ts +212 -0
- package/endpoints/types.ts +155 -0
- package/gql/ArchetypeOperations.ts +32 -86
- package/gql/Generator.ts +27 -315
- package/gql/GeneratorV2.ts +37 -0
- package/gql/builders/InputTypeBuilder.ts +99 -0
- package/gql/builders/ResolverBuilder.ts +234 -0
- package/gql/builders/TypeDefBuilder.ts +105 -0
- package/gql/builders/index.ts +3 -0
- package/gql/decorators/Upload.ts +1 -1
- package/gql/depthLimit.ts +85 -0
- package/gql/graph/GraphNode.ts +224 -0
- package/gql/graph/SchemaGraph.ts +278 -0
- package/gql/helpers.ts +8 -2
- package/gql/index.ts +56 -4
- package/gql/middleware.ts +79 -0
- package/gql/orchestration/GraphQLSchemaOrchestrator.ts +241 -0
- package/gql/orchestration/index.ts +1 -0
- package/gql/scanner/ServiceScanner.ts +347 -0
- package/gql/schema/index.ts +458 -0
- package/gql/strategies/TypeGenerationStrategy.ts +329 -0
- package/gql/types.ts +1 -0
- package/gql/utils/TypeSignature.ts +220 -0
- package/gql/utils/index.ts +1 -0
- package/gql/visitors/ArchetypePreprocessorVisitor.ts +80 -0
- package/gql/visitors/DeduplicationVisitor.ts +82 -0
- package/gql/visitors/GraphVisitor.ts +78 -0
- package/gql/visitors/ResolverGeneratorVisitor.ts +122 -0
- package/gql/visitors/SchemaGeneratorVisitor.ts +851 -0
- package/gql/visitors/TypeCollectorVisitor.ts +79 -0
- package/gql/visitors/VisitorComposer.ts +96 -0
- package/gql/visitors/index.ts +7 -0
- package/package.json +59 -37
- package/plugins/index.ts +2 -2
- package/query/CTENode.ts +97 -0
- package/query/ComponentInclusionNode.ts +689 -0
- package/query/FilterBuilder.ts +127 -0
- package/query/FilterBuilderRegistry.ts +202 -0
- package/query/OrNode.ts +517 -0
- package/query/OrQuery.ts +42 -0
- package/query/Query.ts +1022 -0
- package/query/QueryContext.ts +170 -0
- package/query/QueryDAG.ts +122 -0
- package/query/QueryNode.ts +65 -0
- package/query/SourceNode.ts +53 -0
- package/query/builders/FullTextSearchBuilder.ts +236 -0
- package/query/index.ts +21 -0
- package/scheduler/index.ts +40 -8
- package/service/Service.ts +2 -1
- package/service/ServiceRegistry.ts +6 -5
- package/{core/storage → storage}/LocalStorageProvider.ts +2 -2
- package/storage/S3StorageProvider.ts +316 -0
- package/{core/storage → storage}/StorageProvider.ts +7 -3
- package/studio/bun.lock +482 -0
- package/studio/index.html +13 -0
- package/studio/package.json +39 -0
- package/studio/postcss.config.js +6 -0
- package/studio/src/components/DataTable.tsx +211 -0
- package/studio/src/components/Layout.tsx +13 -0
- package/studio/src/components/PageContainer.tsx +9 -0
- package/studio/src/components/PageHeader.tsx +13 -0
- package/studio/src/components/SearchBar.tsx +57 -0
- package/studio/src/components/Sidebar.tsx +294 -0
- package/studio/src/components/ui/button.tsx +56 -0
- package/studio/src/components/ui/checkbox.tsx +26 -0
- package/studio/src/components/ui/input.tsx +25 -0
- package/studio/src/hooks/useDataTable.ts +131 -0
- package/studio/src/index.css +36 -0
- package/studio/src/lib/api.ts +186 -0
- package/studio/src/lib/utils.ts +13 -0
- package/studio/src/main.tsx +17 -0
- package/studio/src/pages/ArcheType.tsx +239 -0
- package/studio/src/pages/Components.tsx +124 -0
- package/studio/src/pages/EntityInspector.tsx +302 -0
- package/studio/src/pages/QueryRunner.tsx +246 -0
- package/studio/src/pages/Table.tsx +94 -0
- package/studio/src/pages/Welcome.tsx +241 -0
- package/studio/src/routes.tsx +45 -0
- package/studio/src/store/archeTypeSettings.ts +30 -0
- package/studio/src/store/studio.ts +65 -0
- package/studio/src/utils/columnHelpers.tsx +114 -0
- package/studio/studio-instructions.md +81 -0
- package/studio/tailwind.config.js +77 -0
- package/studio/tsconfig.json +24 -0
- package/studio/utils.ts +54 -0
- package/studio/vite.config.js +19 -0
- package/swagger/generator.ts +1 -1
- package/tests/e2e/http.test.ts +126 -0
- package/tests/fixtures/archetypes/TestUserArchetype.ts +21 -0
- package/tests/fixtures/components/TestOrder.ts +23 -0
- package/tests/fixtures/components/TestProduct.ts +23 -0
- package/tests/fixtures/components/TestUser.ts +20 -0
- package/tests/fixtures/components/index.ts +6 -0
- package/tests/graphql/SchemaGeneration.test.ts +90 -0
- package/tests/graphql/builders/ResolverBuilder.test.ts +223 -0
- package/tests/graphql/builders/TypeDefBuilder.test.ts +153 -0
- package/tests/integration/archetype/ArcheType.persistence.test.ts +241 -0
- package/tests/integration/cache/CacheInvalidation.test.ts +259 -0
- package/tests/integration/entity/Entity.persistence.test.ts +333 -0
- package/tests/integration/query/Query.exec.test.ts +523 -0
- package/tests/pglite-setup.ts +61 -0
- package/tests/setup.ts +164 -0
- package/tests/stress/BenchmarkRunner.ts +203 -0
- package/tests/stress/DataSeeder.ts +190 -0
- package/tests/stress/StressTestReporter.ts +229 -0
- package/tests/stress/cursor-perf-test.ts +171 -0
- package/tests/stress/fixtures/StressTestComponents.ts +58 -0
- package/tests/stress/index.ts +7 -0
- package/tests/stress/scenarios/query-benchmarks.test.ts +285 -0
- package/tests/unit/BatchLoader.test.ts +82 -0
- package/tests/unit/archetype/ArcheType.test.ts +107 -0
- package/tests/unit/cache/CacheManager.test.ts +347 -0
- package/tests/unit/cache/MemoryCache.test.ts +260 -0
- package/tests/unit/cache/RedisCache.test.ts +411 -0
- package/tests/unit/entity/Entity.components.test.ts +244 -0
- package/tests/unit/entity/Entity.test.ts +345 -0
- package/tests/unit/gql/depthLimit.test.ts +203 -0
- package/tests/unit/gql/operationMiddleware.test.ts +293 -0
- package/tests/unit/health/Health.test.ts +129 -0
- package/tests/unit/middleware/AccessLog.test.ts +37 -0
- package/tests/unit/middleware/Middleware.test.ts +98 -0
- package/tests/unit/middleware/RequestId.test.ts +54 -0
- package/tests/unit/middleware/SecurityHeaders.test.ts +66 -0
- package/tests/unit/query/FilterBuilder.test.ts +111 -0
- package/tests/unit/query/Query.test.ts +308 -0
- package/tests/unit/scheduler/DistributedLock.test.ts +274 -0
- package/tests/unit/schema/schema-integration.test.ts +426 -0
- package/tests/unit/schema/schema.test.ts +580 -0
- package/tests/unit/storage/S3StorageProvider.test.ts +571 -0
- package/tests/unit/upload/RestUpload.test.ts +267 -0
- package/tests/unit/validateEnv.test.ts +82 -0
- package/tests/utils/entity-tracker.ts +57 -0
- package/tests/utils/index.ts +13 -0
- package/tests/utils/test-context.ts +149 -0
- package/tsconfig.json +5 -1
- package/types/archetype.types.ts +6 -0
- package/types/hooks.types.ts +1 -1
- package/types/query.types.ts +110 -0
- package/types/scheduler.types.ts +68 -7
- package/types/upload.types.ts +1 -0
- package/{core → upload}/FileValidator.ts +10 -1
- package/upload/RestUpload.ts +130 -0
- package/{core/components → upload}/UploadComponent.ts +11 -11
- package/{core → upload}/UploadManager.ts +3 -3
- package/upload/index.ts +23 -7
- package/utils/UploadHelper.ts +27 -6
- package/utils/cronParser.ts +16 -6
- package/.github/workflows/deploy-docs.yml +0 -57
- package/core/Components.ts +0 -202
- package/core/EntityCache.ts +0 -15
- package/core/Query.ts +0 -880
- package/docs/README.md +0 -149
- package/docs/_coverpage.md +0 -36
- package/docs/_sidebar.md +0 -23
- package/docs/api/core.md +0 -568
- package/docs/api/hooks.md +0 -554
- package/docs/api/index.md +0 -222
- package/docs/api/query.md +0 -678
- package/docs/api/service.md +0 -744
- package/docs/core-concepts/archetypes.md +0 -512
- package/docs/core-concepts/components.md +0 -498
- package/docs/core-concepts/entity.md +0 -314
- package/docs/core-concepts/hooks.md +0 -683
- package/docs/core-concepts/query.md +0 -588
- package/docs/core-concepts/services.md +0 -647
- package/docs/examples/code-examples.md +0 -425
- package/docs/getting-started.md +0 -337
- package/docs/index.html +0 -97
- package/tests/bench/insert.bench.ts +0 -60
- package/tests/bench/relations.bench.ts +0 -270
- package/tests/bench/sorting.bench.ts +0 -416
- package/tests/component-hooks-simple.test.ts +0 -117
- package/tests/component-hooks.test.ts +0 -1461
- package/tests/component.test.ts +0 -339
- package/tests/errorHandling.test.ts +0 -155
- package/tests/hooks.test.ts +0 -667
- package/tests/query-sorting.test.ts +0 -101
- package/tests/query.test.ts +0 -81
- package/tests/relations.test.ts +0 -170
- package/tests/scheduler.test.ts +0 -724
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, mock } from "bun:test";
|
|
2
|
+
import { S3StorageProvider } from "../../../storage/S3StorageProvider";
|
|
3
|
+
import type { FileMetadata, UploadConfiguration } from "../../../types/upload.types";
|
|
4
|
+
|
|
5
|
+
function createMockS3Client(overrides: Record<string, any> = {}) {
|
|
6
|
+
return {
|
|
7
|
+
list: mock(async () => ({
|
|
8
|
+
contents: [],
|
|
9
|
+
isTruncated: false,
|
|
10
|
+
keyCount: 0,
|
|
11
|
+
maxKeys: 1000,
|
|
12
|
+
name: "test-bucket",
|
|
13
|
+
})),
|
|
14
|
+
write: mock(async () => {}),
|
|
15
|
+
delete: mock(async () => {}),
|
|
16
|
+
exists: mock(async () => true),
|
|
17
|
+
stat: mock(async () => ({
|
|
18
|
+
size: 1024,
|
|
19
|
+
lastModified: new Date("2026-01-01"),
|
|
20
|
+
etag: '"abc123"',
|
|
21
|
+
type: "image/png",
|
|
22
|
+
})),
|
|
23
|
+
file: mock((key: string) => ({
|
|
24
|
+
stream: () => new ReadableStream(),
|
|
25
|
+
arrayBuffer: async () => new ArrayBuffer(1024),
|
|
26
|
+
})),
|
|
27
|
+
presign: mock((key: string, opts?: any) => `https://s3.example.com/${key}?signed=true`),
|
|
28
|
+
...overrides,
|
|
29
|
+
} as any;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function createTestMetadata(overrides: Partial<FileMetadata> = {}): FileMetadata {
|
|
33
|
+
return {
|
|
34
|
+
uploadId: "test-upload-id",
|
|
35
|
+
fileName: "test-image.png",
|
|
36
|
+
originalFileName: "my-photo.png",
|
|
37
|
+
mimeType: "image/png",
|
|
38
|
+
size: 1024,
|
|
39
|
+
extension: ".png",
|
|
40
|
+
uploadedAt: new Date().toISOString(),
|
|
41
|
+
...overrides,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function createTestConfig(overrides: Partial<UploadConfiguration> = {}): UploadConfiguration {
|
|
46
|
+
return {
|
|
47
|
+
maxFileSize: 10 * 1024 * 1024,
|
|
48
|
+
allowedMimeTypes: ["image/png", "image/jpeg"],
|
|
49
|
+
allowedExtensions: [".png", ".jpg"],
|
|
50
|
+
validateFileSignature: true,
|
|
51
|
+
sanitizeFileName: true,
|
|
52
|
+
preserveOriginalName: false,
|
|
53
|
+
generateThumbnails: false,
|
|
54
|
+
uploadPath: "uploads",
|
|
55
|
+
namingStrategy: "uuid",
|
|
56
|
+
...overrides,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe("S3StorageProvider", () => {
|
|
61
|
+
describe("constructor", () => {
|
|
62
|
+
it("throws if bucket is missing", () => {
|
|
63
|
+
const client = createMockS3Client();
|
|
64
|
+
expect(
|
|
65
|
+
() => new S3StorageProvider({ bucket: "" }, client),
|
|
66
|
+
).toThrow("bucket is required");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("creates with required config", () => {
|
|
70
|
+
const client = createMockS3Client();
|
|
71
|
+
const provider = new S3StorageProvider(
|
|
72
|
+
{ bucket: "my-bucket" },
|
|
73
|
+
client,
|
|
74
|
+
);
|
|
75
|
+
expect(provider.getName()).toBe("s3");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("applies default config values", () => {
|
|
79
|
+
const client = createMockS3Client();
|
|
80
|
+
const provider = new S3StorageProvider(
|
|
81
|
+
{ bucket: "my-bucket" },
|
|
82
|
+
client,
|
|
83
|
+
);
|
|
84
|
+
expect(provider).toBeDefined();
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("initialize", () => {
|
|
89
|
+
it("verifies S3 connectivity via list", async () => {
|
|
90
|
+
const client = createMockS3Client();
|
|
91
|
+
const provider = new S3StorageProvider(
|
|
92
|
+
{ bucket: "my-bucket" },
|
|
93
|
+
client,
|
|
94
|
+
);
|
|
95
|
+
await provider.initialize();
|
|
96
|
+
expect(client.list).toHaveBeenCalledWith({ maxKeys: 1 });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("throws on connectivity failure", async () => {
|
|
100
|
+
const client = createMockS3Client({
|
|
101
|
+
list: mock(async () => {
|
|
102
|
+
throw new Error("Access Denied");
|
|
103
|
+
}),
|
|
104
|
+
});
|
|
105
|
+
const provider = new S3StorageProvider(
|
|
106
|
+
{ bucket: "my-bucket" },
|
|
107
|
+
client,
|
|
108
|
+
);
|
|
109
|
+
await expect(provider.initialize()).rejects.toThrow(
|
|
110
|
+
"S3 initialization failed: Access Denied",
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("store", () => {
|
|
116
|
+
it("writes file with correct key and options", async () => {
|
|
117
|
+
const client = createMockS3Client();
|
|
118
|
+
const provider = new S3StorageProvider(
|
|
119
|
+
{ bucket: "my-bucket", keyPrefix: "app/" },
|
|
120
|
+
client,
|
|
121
|
+
);
|
|
122
|
+
const metadata = createTestMetadata();
|
|
123
|
+
const config = createTestConfig();
|
|
124
|
+
const file = new File(["data"], "test.png", { type: "image/png" });
|
|
125
|
+
|
|
126
|
+
const result = await provider.store(file, metadata, config);
|
|
127
|
+
|
|
128
|
+
expect(client.write).toHaveBeenCalled();
|
|
129
|
+
const [key, body, opts] = client.write.mock.calls[0];
|
|
130
|
+
expect(key).toBe("app/uploads/test-image.png");
|
|
131
|
+
expect(body).toBeInstanceOf(File);
|
|
132
|
+
expect(opts.type).toBe("image/png");
|
|
133
|
+
expect(opts.acl).toBe("private");
|
|
134
|
+
expect(result.path).toBe("app/uploads/test-image.png");
|
|
135
|
+
expect(result.url).toBeTypeOf("string");
|
|
136
|
+
expect(result.metadata?.bucket).toBe("my-bucket");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("uses configured acl", async () => {
|
|
140
|
+
const client = createMockS3Client();
|
|
141
|
+
const provider = new S3StorageProvider(
|
|
142
|
+
{ bucket: "my-bucket", acl: "public-read" },
|
|
143
|
+
client,
|
|
144
|
+
);
|
|
145
|
+
const file = new File(["data"], "test.png", { type: "image/png" });
|
|
146
|
+
|
|
147
|
+
await provider.store(file, createTestMetadata(), createTestConfig());
|
|
148
|
+
|
|
149
|
+
const [, , opts] = client.write.mock.calls[0];
|
|
150
|
+
expect(opts.acl).toBe("public-read");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("throws on write failure", async () => {
|
|
154
|
+
const client = createMockS3Client({
|
|
155
|
+
write: mock(async () => {
|
|
156
|
+
throw new Error("Write failed");
|
|
157
|
+
}),
|
|
158
|
+
});
|
|
159
|
+
const provider = new S3StorageProvider(
|
|
160
|
+
{ bucket: "my-bucket" },
|
|
161
|
+
client,
|
|
162
|
+
);
|
|
163
|
+
const file = new File(["data"], "test.png", { type: "image/png" });
|
|
164
|
+
|
|
165
|
+
await expect(
|
|
166
|
+
provider.store(file, createTestMetadata(), createTestConfig()),
|
|
167
|
+
).rejects.toThrow("Failed to store file to S3");
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe("delete", () => {
|
|
172
|
+
it("deletes and returns true on success", async () => {
|
|
173
|
+
const client = createMockS3Client();
|
|
174
|
+
const provider = new S3StorageProvider(
|
|
175
|
+
{ bucket: "my-bucket" },
|
|
176
|
+
client,
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
const result = await provider.delete("uploads/file.png");
|
|
180
|
+
expect(result).toBe(true);
|
|
181
|
+
expect(client.delete).toHaveBeenCalledWith("uploads/file.png");
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("returns false on failure", async () => {
|
|
185
|
+
const client = createMockS3Client({
|
|
186
|
+
delete: mock(async () => {
|
|
187
|
+
throw new Error("Not found");
|
|
188
|
+
}),
|
|
189
|
+
});
|
|
190
|
+
const provider = new S3StorageProvider(
|
|
191
|
+
{ bucket: "my-bucket" },
|
|
192
|
+
client,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const result = await provider.delete("uploads/file.png");
|
|
196
|
+
expect(result).toBe(false);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("sanitizes path traversal attempts", async () => {
|
|
200
|
+
const client = createMockS3Client();
|
|
201
|
+
const provider = new S3StorageProvider(
|
|
202
|
+
{ bucket: "my-bucket" },
|
|
203
|
+
client,
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
await provider.delete("../../../etc/passwd");
|
|
207
|
+
const [key] = client.delete.mock.calls[0];
|
|
208
|
+
expect(key).not.toContain("..");
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("getUrl", () => {
|
|
213
|
+
it("returns presigned URL", async () => {
|
|
214
|
+
const client = createMockS3Client();
|
|
215
|
+
const provider = new S3StorageProvider(
|
|
216
|
+
{ bucket: "my-bucket" },
|
|
217
|
+
client,
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
const url = await provider.getUrl("uploads/file.png");
|
|
221
|
+
expect(url).toContain("uploads/file.png");
|
|
222
|
+
expect(client.presign).toHaveBeenCalled();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("exists", () => {
|
|
227
|
+
it("delegates to client.exists", async () => {
|
|
228
|
+
const client = createMockS3Client();
|
|
229
|
+
const provider = new S3StorageProvider(
|
|
230
|
+
{ bucket: "my-bucket" },
|
|
231
|
+
client,
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const result = await provider.exists("uploads/file.png");
|
|
235
|
+
expect(result).toBe(true);
|
|
236
|
+
expect(client.exists).toHaveBeenCalledWith("uploads/file.png");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("returns false on error", async () => {
|
|
240
|
+
const client = createMockS3Client({
|
|
241
|
+
exists: mock(async () => {
|
|
242
|
+
throw new Error("Network error");
|
|
243
|
+
}),
|
|
244
|
+
});
|
|
245
|
+
const provider = new S3StorageProvider(
|
|
246
|
+
{ bucket: "my-bucket" },
|
|
247
|
+
client,
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const result = await provider.exists("uploads/file.png");
|
|
251
|
+
expect(result).toBe(false);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
describe("getMetadata", () => {
|
|
256
|
+
it("returns metadata from stat", async () => {
|
|
257
|
+
const client = createMockS3Client();
|
|
258
|
+
const provider = new S3StorageProvider(
|
|
259
|
+
{ bucket: "my-bucket" },
|
|
260
|
+
client,
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const meta = await provider.getMetadata("uploads/photo.png");
|
|
264
|
+
expect(meta).not.toBeNull();
|
|
265
|
+
expect(meta!.size).toBe(1024);
|
|
266
|
+
expect(meta!.mimeType).toBe("image/png");
|
|
267
|
+
expect(meta!.fileName).toBe("photo.png");
|
|
268
|
+
expect(meta!.extension).toBe(".png");
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("returns null on error", async () => {
|
|
272
|
+
const client = createMockS3Client({
|
|
273
|
+
stat: mock(async () => {
|
|
274
|
+
throw new Error("Not found");
|
|
275
|
+
}),
|
|
276
|
+
});
|
|
277
|
+
const provider = new S3StorageProvider(
|
|
278
|
+
{ bucket: "my-bucket" },
|
|
279
|
+
client,
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
const meta = await provider.getMetadata("uploads/missing.png");
|
|
283
|
+
expect(meta).toBeNull();
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
describe("list", () => {
|
|
288
|
+
it("returns keys from paginated list", async () => {
|
|
289
|
+
let callCount = 0;
|
|
290
|
+
const client = createMockS3Client({
|
|
291
|
+
list: mock(async () => {
|
|
292
|
+
callCount++;
|
|
293
|
+
if (callCount === 1) {
|
|
294
|
+
return {
|
|
295
|
+
contents: [
|
|
296
|
+
{ key: "uploads/a.png", size: 100 },
|
|
297
|
+
{ key: "uploads/b.png", size: 200 },
|
|
298
|
+
],
|
|
299
|
+
isTruncated: true,
|
|
300
|
+
nextContinuationToken: "page2",
|
|
301
|
+
keyCount: 2,
|
|
302
|
+
maxKeys: 1000,
|
|
303
|
+
name: "test-bucket",
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
contents: [{ key: "uploads/c.png", size: 300 }],
|
|
308
|
+
isTruncated: false,
|
|
309
|
+
keyCount: 1,
|
|
310
|
+
maxKeys: 1000,
|
|
311
|
+
name: "test-bucket",
|
|
312
|
+
};
|
|
313
|
+
}),
|
|
314
|
+
});
|
|
315
|
+
const provider = new S3StorageProvider(
|
|
316
|
+
{ bucket: "my-bucket" },
|
|
317
|
+
client,
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
const keys = await provider.list("uploads");
|
|
321
|
+
expect(keys).toEqual([
|
|
322
|
+
"uploads/a.png",
|
|
323
|
+
"uploads/b.png",
|
|
324
|
+
"uploads/c.png",
|
|
325
|
+
]);
|
|
326
|
+
expect(client.list).toHaveBeenCalledTimes(2);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("returns empty array on error", async () => {
|
|
330
|
+
const client = createMockS3Client({
|
|
331
|
+
list: mock(async () => {
|
|
332
|
+
throw new Error("Forbidden");
|
|
333
|
+
}),
|
|
334
|
+
});
|
|
335
|
+
const provider = new S3StorageProvider(
|
|
336
|
+
{ bucket: "my-bucket" },
|
|
337
|
+
client,
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
const keys = await provider.list("uploads");
|
|
341
|
+
expect(keys).toEqual([]);
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
describe("getStream", () => {
|
|
346
|
+
it("returns a ReadableStream from S3File", async () => {
|
|
347
|
+
const client = createMockS3Client();
|
|
348
|
+
const provider = new S3StorageProvider(
|
|
349
|
+
{ bucket: "my-bucket" },
|
|
350
|
+
client,
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
const stream = await provider.getStream("uploads/file.png");
|
|
354
|
+
expect(stream).toBeInstanceOf(ReadableStream);
|
|
355
|
+
expect(client.file).toHaveBeenCalledWith("uploads/file.png");
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
describe("copy", () => {
|
|
360
|
+
it("reads source and writes to destination", async () => {
|
|
361
|
+
const client = createMockS3Client();
|
|
362
|
+
const provider = new S3StorageProvider(
|
|
363
|
+
{ bucket: "my-bucket" },
|
|
364
|
+
client,
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const result = await provider.copy(
|
|
368
|
+
"uploads/src.png",
|
|
369
|
+
"uploads/dst.png",
|
|
370
|
+
);
|
|
371
|
+
expect(result).toBe(true);
|
|
372
|
+
expect(client.file).toHaveBeenCalledWith("uploads/src.png");
|
|
373
|
+
expect(client.stat).toHaveBeenCalledWith("uploads/src.png");
|
|
374
|
+
expect(client.write).toHaveBeenCalled();
|
|
375
|
+
const [destKey] = client.write.mock.calls[0];
|
|
376
|
+
expect(destKey).toBe("uploads/dst.png");
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("returns false on failure", async () => {
|
|
380
|
+
const client = createMockS3Client({
|
|
381
|
+
file: mock(() => ({
|
|
382
|
+
arrayBuffer: async () => {
|
|
383
|
+
throw new Error("Read failed");
|
|
384
|
+
},
|
|
385
|
+
})),
|
|
386
|
+
});
|
|
387
|
+
const provider = new S3StorageProvider(
|
|
388
|
+
{ bucket: "my-bucket" },
|
|
389
|
+
client,
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
const result = await provider.copy("src.png", "dst.png");
|
|
393
|
+
expect(result).toBe(false);
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
describe("move", () => {
|
|
398
|
+
it("copies then deletes source", async () => {
|
|
399
|
+
const client = createMockS3Client();
|
|
400
|
+
const provider = new S3StorageProvider(
|
|
401
|
+
{ bucket: "my-bucket" },
|
|
402
|
+
client,
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
const result = await provider.move(
|
|
406
|
+
"uploads/src.png",
|
|
407
|
+
"uploads/dst.png",
|
|
408
|
+
);
|
|
409
|
+
expect(result).toBe(true);
|
|
410
|
+
expect(client.write).toHaveBeenCalled();
|
|
411
|
+
expect(client.delete).toHaveBeenCalledWith("uploads/src.png");
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("returns false if copy fails", async () => {
|
|
415
|
+
const client = createMockS3Client({
|
|
416
|
+
file: mock(() => ({
|
|
417
|
+
arrayBuffer: async () => {
|
|
418
|
+
throw new Error("Read failed");
|
|
419
|
+
},
|
|
420
|
+
})),
|
|
421
|
+
});
|
|
422
|
+
const provider = new S3StorageProvider(
|
|
423
|
+
{ bucket: "my-bucket" },
|
|
424
|
+
client,
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
const result = await provider.move("src.png", "dst.png");
|
|
428
|
+
expect(result).toBe(false);
|
|
429
|
+
expect(client.delete).not.toHaveBeenCalled();
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
describe("getStats", () => {
|
|
434
|
+
it("sums files and sizes from list", async () => {
|
|
435
|
+
const client = createMockS3Client({
|
|
436
|
+
list: mock(async () => ({
|
|
437
|
+
contents: [
|
|
438
|
+
{ key: "a.png", size: 100 },
|
|
439
|
+
{ key: "b.png", size: 200 },
|
|
440
|
+
],
|
|
441
|
+
isTruncated: false,
|
|
442
|
+
keyCount: 2,
|
|
443
|
+
maxKeys: 1000,
|
|
444
|
+
name: "test-bucket",
|
|
445
|
+
})),
|
|
446
|
+
});
|
|
447
|
+
const provider = new S3StorageProvider(
|
|
448
|
+
{ bucket: "my-bucket" },
|
|
449
|
+
client,
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
const stats = await provider.getStats();
|
|
453
|
+
expect(stats.totalFiles).toBe(2);
|
|
454
|
+
expect(stats.totalSize).toBe(300);
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
describe("cleanup", () => {
|
|
459
|
+
it("is a no-op that resolves", async () => {
|
|
460
|
+
const client = createMockS3Client();
|
|
461
|
+
const provider = new S3StorageProvider(
|
|
462
|
+
{ bucket: "my-bucket" },
|
|
463
|
+
client,
|
|
464
|
+
);
|
|
465
|
+
await expect(provider.cleanup()).resolves.toBeUndefined();
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
describe("resolveKey with keyPrefix", () => {
|
|
470
|
+
it("prepends keyPrefix when missing from path", async () => {
|
|
471
|
+
const client = createMockS3Client();
|
|
472
|
+
const provider = new S3StorageProvider(
|
|
473
|
+
{ bucket: "my-bucket", keyPrefix: "app/" },
|
|
474
|
+
client,
|
|
475
|
+
);
|
|
476
|
+
|
|
477
|
+
await provider.delete("uploads/file.png");
|
|
478
|
+
const [key] = client.delete.mock.calls[0];
|
|
479
|
+
expect(key).toBe("app/uploads/file.png");
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it("does not double-prefix when path already has prefix", async () => {
|
|
483
|
+
const client = createMockS3Client();
|
|
484
|
+
const provider = new S3StorageProvider(
|
|
485
|
+
{ bucket: "my-bucket", keyPrefix: "app/" },
|
|
486
|
+
client,
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
await provider.delete("app/uploads/file.png");
|
|
490
|
+
const [key] = client.delete.mock.calls[0];
|
|
491
|
+
expect(key).toBe("app/uploads/file.png");
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("normalizes keyPrefix to add trailing slash", () => {
|
|
495
|
+
const client = createMockS3Client();
|
|
496
|
+
const provider = new S3StorageProvider(
|
|
497
|
+
{ bucket: "my-bucket", keyPrefix: "app" },
|
|
498
|
+
client,
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
// Verify via a store call that the key is correctly prefixed
|
|
502
|
+
const file = new File(["data"], "test.png", { type: "image/png" });
|
|
503
|
+
provider.store(file, createTestMetadata(), createTestConfig());
|
|
504
|
+
const [key] = client.write.mock.calls[0];
|
|
505
|
+
expect(key).toStartWith("app/");
|
|
506
|
+
});
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
describe("config validation", () => {
|
|
510
|
+
it("throws if presignExpiry is zero or negative", () => {
|
|
511
|
+
const client = createMockS3Client();
|
|
512
|
+
expect(
|
|
513
|
+
() => new S3StorageProvider({ bucket: "my-bucket", presignExpiry: 0 }, client),
|
|
514
|
+
).toThrow("presignExpiry must be positive");
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it("throws if publicPresignExpiry is zero or negative", () => {
|
|
518
|
+
const client = createMockS3Client();
|
|
519
|
+
expect(
|
|
520
|
+
() => new S3StorageProvider({ bucket: "my-bucket", publicPresignExpiry: -1 }, client),
|
|
521
|
+
).toThrow("publicPresignExpiry must be positive");
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
describe("sanitizePath security", () => {
|
|
526
|
+
it("prevents nested path traversal bypass (....//)", async () => {
|
|
527
|
+
const client = createMockS3Client();
|
|
528
|
+
const provider = new S3StorageProvider(
|
|
529
|
+
{ bucket: "my-bucket" },
|
|
530
|
+
client,
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
await provider.delete("....//....//etc/passwd");
|
|
534
|
+
const [key] = client.delete.mock.calls[0];
|
|
535
|
+
// After iterative sanitization, no ".." sequences should remain
|
|
536
|
+
expect(key).not.toContain("..");
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
it("handles deeply nested traversal patterns", async () => {
|
|
540
|
+
const client = createMockS3Client();
|
|
541
|
+
const provider = new S3StorageProvider(
|
|
542
|
+
{ bucket: "my-bucket" },
|
|
543
|
+
client,
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
await provider.delete("......///secret/file.txt");
|
|
547
|
+
const [key] = client.delete.mock.calls[0];
|
|
548
|
+
expect(key).not.toContain("..");
|
|
549
|
+
});
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
describe("store error cleanup", () => {
|
|
553
|
+
it("attempts to delete key on write failure", async () => {
|
|
554
|
+
const client = createMockS3Client({
|
|
555
|
+
write: mock(async () => {
|
|
556
|
+
throw new Error("Write failed");
|
|
557
|
+
}),
|
|
558
|
+
});
|
|
559
|
+
const provider = new S3StorageProvider(
|
|
560
|
+
{ bucket: "my-bucket" },
|
|
561
|
+
client,
|
|
562
|
+
);
|
|
563
|
+
const file = new File(["data"], "test.png", { type: "image/png" });
|
|
564
|
+
|
|
565
|
+
await expect(
|
|
566
|
+
provider.store(file, createTestMetadata(), createTestConfig()),
|
|
567
|
+
).rejects.toThrow("Failed to store file to S3");
|
|
568
|
+
expect(client.delete).toHaveBeenCalled();
|
|
569
|
+
});
|
|
570
|
+
});
|
|
571
|
+
});
|