@fgv/ts-http-storage 5.1.0-1
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/.rush/temp/chunked-rush-logs/ts-http-storage.build.chunks.jsonl +9 -0
- package/.rush/temp/fbfcc9487d290993ba47f1d36e9383196e7b12ac.tar.log +61 -0
- package/.rush/temp/operation/build/all.log +9 -0
- package/.rush/temp/operation/build/log-chunks.jsonl +9 -0
- package/.rush/temp/operation/build/state.json +3 -0
- package/.rush/temp/shrinkwrap-deps.json +647 -0
- package/config/api-extractor.json +36 -0
- package/config/rig.json +10 -0
- package/config/typedoc.json +7 -0
- package/dist/ts-http-storage.d.ts +225 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/docs/README.md +298 -0
- package/docs/classes/FsStorageProvider.createDirectory.md +20 -0
- package/docs/classes/FsStorageProvider.getChildren.md +20 -0
- package/docs/classes/FsStorageProvider.getFile.md +20 -0
- package/docs/classes/FsStorageProvider.getItem.md +20 -0
- package/docs/classes/FsStorageProvider.md +134 -0
- package/docs/classes/FsStorageProvider.saveFile.md +22 -0
- package/docs/classes/FsStorageProvider.sync.md +13 -0
- package/docs/classes/FsStorageProviderFactory.forNamespace.md +20 -0
- package/docs/classes/FsStorageProviderFactory.md +69 -0
- package/docs/classes/HttpStorageService.createDirectory.md +20 -0
- package/docs/classes/HttpStorageService.getChildren.md +20 -0
- package/docs/classes/HttpStorageService.getFile.md +20 -0
- package/docs/classes/HttpStorageService.getItem.md +20 -0
- package/docs/classes/HttpStorageService.md +132 -0
- package/docs/classes/HttpStorageService.saveFile.md +20 -0
- package/docs/classes/HttpStorageService.sync.md +20 -0
- package/docs/functions/createStorageRoutes.md +11 -0
- package/docs/functions/normalizeRequestPath.md +11 -0
- package/docs/functions/sanitizeNamespace.md +11 -0
- package/docs/interfaces/ICreateStorageRoutesOptions.logger.md +9 -0
- package/docs/interfaces/ICreateStorageRoutesOptions.md +61 -0
- package/docs/interfaces/ICreateStorageRoutesOptions.providers.md +9 -0
- package/docs/interfaces/IFsStorageProviderFactoryOptions.md +44 -0
- package/docs/interfaces/IFsStorageProviderFactoryOptions.rootPath.md +9 -0
- package/docs/interfaces/IHttpStorageProvider.createDirectory.md +20 -0
- package/docs/interfaces/IHttpStorageProvider.getChildren.md +20 -0
- package/docs/interfaces/IHttpStorageProvider.getFile.md +20 -0
- package/docs/interfaces/IHttpStorageProvider.getItem.md +20 -0
- package/docs/interfaces/IHttpStorageProvider.md +102 -0
- package/docs/interfaces/IHttpStorageProvider.saveFile.md +22 -0
- package/docs/interfaces/IHttpStorageProvider.sync.md +13 -0
- package/docs/interfaces/IHttpStorageProviderFactory.forNamespace.md +20 -0
- package/docs/interfaces/IHttpStorageProviderFactory.md +36 -0
- package/docs/interfaces/IStorageFileResponse.contentType.md +9 -0
- package/docs/interfaces/IStorageFileResponse.contents.md +9 -0
- package/docs/interfaces/IStorageFileResponse.md +78 -0
- package/docs/interfaces/IStorageFileResponse.path.md +9 -0
- package/docs/interfaces/IStoragePathRequest.md +61 -0
- package/docs/interfaces/IStoragePathRequest.namespace.md +9 -0
- package/docs/interfaces/IStoragePathRequest.path.md +9 -0
- package/docs/interfaces/IStorageSyncRequest.md +44 -0
- package/docs/interfaces/IStorageSyncRequest.namespace.md +9 -0
- package/docs/interfaces/IStorageSyncResponse.md +44 -0
- package/docs/interfaces/IStorageSyncResponse.synced.md +9 -0
- package/docs/interfaces/IStorageTreeChildrenResponse.children.md +9 -0
- package/docs/interfaces/IStorageTreeChildrenResponse.md +61 -0
- package/docs/interfaces/IStorageTreeChildrenResponse.path.md +9 -0
- package/docs/interfaces/IStorageTreeItem.md +78 -0
- package/docs/interfaces/IStorageTreeItem.name.md +9 -0
- package/docs/interfaces/IStorageTreeItem.path.md +9 -0
- package/docs/interfaces/IStorageTreeItem.type.md +9 -0
- package/docs/interfaces/IStorageWriteFileRequest.contentType.md +9 -0
- package/docs/interfaces/IStorageWriteFileRequest.contents.md +9 -0
- package/docs/interfaces/IStorageWriteFileRequest.md +97 -0
- package/docs/type-aliases/StorageItemType.md +11 -0
- package/docs/type-aliases/StorageNamespace.md +11 -0
- package/docs/variables/storageFileResponse.md +9 -0
- package/docs/variables/storagePathRequest.md +9 -0
- package/docs/variables/storageSyncRequest.md +9 -0
- package/docs/variables/storageTreeChildrenResponse.md +9 -0
- package/docs/variables/storageTreeItem.md +9 -0
- package/docs/variables/storageWriteFileRequest.md +9 -0
- package/etc/ts-http-storage.api.md +184 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +43 -0
- package/lib/index.js.map +1 -0
- package/lib/packlets/storage/converters.d.ts +33 -0
- package/lib/packlets/storage/converters.d.ts.map +1 -0
- package/lib/packlets/storage/converters.js +81 -0
- package/lib/packlets/storage/converters.js.map +1 -0
- package/lib/packlets/storage/fsProvider.d.ts +47 -0
- package/lib/packlets/storage/fsProvider.d.ts.map +1 -0
- package/lib/packlets/storage/fsProvider.js +260 -0
- package/lib/packlets/storage/fsProvider.js.map +1 -0
- package/lib/packlets/storage/index.d.ts +11 -0
- package/lib/packlets/storage/index.d.ts.map +1 -0
- package/lib/packlets/storage/index.js +48 -0
- package/lib/packlets/storage/index.js.map +1 -0
- package/lib/packlets/storage/model.d.ts +82 -0
- package/lib/packlets/storage/model.d.ts.map +1 -0
- package/lib/packlets/storage/model.js +24 -0
- package/lib/packlets/storage/model.js.map +1 -0
- package/lib/packlets/storage/provider.d.ts +10 -0
- package/lib/packlets/storage/provider.d.ts.map +1 -0
- package/lib/packlets/storage/provider.js +24 -0
- package/lib/packlets/storage/provider.js.map +1 -0
- package/lib/packlets/storage/routes.d.ts +17 -0
- package/lib/packlets/storage/routes.d.ts.map +1 -0
- package/lib/packlets/storage/routes.js +170 -0
- package/lib/packlets/storage/routes.js.map +1 -0
- package/lib/packlets/storage/service.d.ts +20 -0
- package/lib/packlets/storage/service.d.ts.map +1 -0
- package/lib/packlets/storage/service.js +95 -0
- package/lib/packlets/storage/service.js.map +1 -0
- package/lib/test/unit/storage/fsProvider.test.d.ts +2 -0
- package/lib/test/unit/storage/fsProvider.test.d.ts.map +1 -0
- package/lib/test/unit/storage/fsProvider.test.js +106 -0
- package/lib/test/unit/storage/fsProvider.test.js.map +1 -0
- package/lib/test/unit/storage/routes.test.d.ts +2 -0
- package/lib/test/unit/storage/routes.test.d.ts.map +1 -0
- package/lib/test/unit/storage/routes.test.js +124 -0
- package/lib/test/unit/storage/routes.test.js.map +1 -0
- package/lib/test/unit/storage/service.test.d.ts +2 -0
- package/lib/test/unit/storage/service.test.d.ts.map +1 -0
- package/lib/test/unit/storage/service.test.js +67 -0
- package/lib/test/unit/storage/service.test.js.map +1 -0
- package/package.json +66 -0
- package/rush-logs/ts-http-storage.build.cache.log +3 -0
- package/rush-logs/ts-http-storage.build.log +9 -0
- package/src/index.ts +28 -0
- package/src/packlets/storage/converters.ts +100 -0
- package/src/packlets/storage/fsProvider.ts +263 -0
- package/src/packlets/storage/index.ts +33 -0
- package/src/packlets/storage/model.ts +113 -0
- package/src/packlets/storage/provider.ts +33 -0
- package/src/packlets/storage/routes.ts +215 -0
- package/src/packlets/storage/service.ts +114 -0
- package/src/test/unit/storage/fsProvider.test.ts +129 -0
- package/src/test/unit/storage/routes.test.ts +165 -0
- package/src/test/unit/storage/service.test.ts +95 -0
- package/temp/build/typescript/ts_l9Fw4VUO.json +1 -0
- package/temp/ts-http-storage.api.json +3103 -0
- package/temp/ts-http-storage.api.md +184 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { Result } from '@fgv/ts-utils';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Namespace identifier for scoped storage.
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export type StorageNamespace = string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Storage item type.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export type StorageItemType = 'file' | 'directory';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Storage tree item metadata.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export interface IStorageTreeItem {
|
|
42
|
+
readonly path: string;
|
|
43
|
+
readonly name: string;
|
|
44
|
+
readonly type: StorageItemType;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Response for listing children.
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export interface IStorageTreeChildrenResponse {
|
|
52
|
+
readonly path: string;
|
|
53
|
+
readonly children: ReadonlyArray<IStorageTreeItem>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Response for reading a file.
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
export interface IStorageFileResponse {
|
|
61
|
+
readonly path: string;
|
|
62
|
+
readonly contents: string;
|
|
63
|
+
readonly contentType?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Request for path-based operations.
|
|
68
|
+
* @public
|
|
69
|
+
*/
|
|
70
|
+
export interface IStoragePathRequest {
|
|
71
|
+
readonly path: string;
|
|
72
|
+
readonly namespace?: StorageNamespace;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Request for writing file contents.
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export interface IStorageWriteFileRequest extends IStoragePathRequest {
|
|
80
|
+
readonly contents: string;
|
|
81
|
+
readonly contentType?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Request for sync operation.
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
export interface IStorageSyncRequest {
|
|
89
|
+
readonly namespace?: StorageNamespace;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Sync response metadata.
|
|
94
|
+
* @public
|
|
95
|
+
*/
|
|
96
|
+
export interface IStorageSyncResponse {
|
|
97
|
+
readonly synced: number;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Provider contract for storage backends.
|
|
102
|
+
* All methods are async to support both filesystem and database backends.
|
|
103
|
+
* @public
|
|
104
|
+
*/
|
|
105
|
+
export interface IHttpStorageProvider {
|
|
106
|
+
getItem(path: string): Promise<Result<IStorageTreeItem>>;
|
|
107
|
+
getChildren(path: string): Promise<Result<ReadonlyArray<IStorageTreeItem>>>;
|
|
108
|
+
getFile(path: string): Promise<Result<IStorageFileResponse>>;
|
|
109
|
+
saveFile(path: string, contents: string, contentType?: string): Promise<Result<IStorageFileResponse>>;
|
|
110
|
+
deleteFile(path: string): Promise<Result<boolean>>;
|
|
111
|
+
createDirectory(path: string): Promise<Result<IStorageTreeItem>>;
|
|
112
|
+
sync(): Promise<Result<IStorageSyncResponse>>;
|
|
113
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { Result } from '@fgv/ts-utils';
|
|
24
|
+
|
|
25
|
+
import type { IHttpStorageProvider, StorageNamespace } from './model';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Factory for creating namespace-scoped storage providers.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export interface IHttpStorageProviderFactory {
|
|
32
|
+
forNamespace(namespace?: StorageNamespace): Result<IHttpStorageProvider>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Hono } from 'hono';
|
|
24
|
+
|
|
25
|
+
import type { Logging } from '@fgv/ts-utils';
|
|
26
|
+
|
|
27
|
+
import {
|
|
28
|
+
storagePathRequest,
|
|
29
|
+
storageSyncRequest,
|
|
30
|
+
storageWriteFileRequest,
|
|
31
|
+
storageTreeChildrenResponse,
|
|
32
|
+
storageTreeItem,
|
|
33
|
+
storageFileResponse
|
|
34
|
+
} from './converters';
|
|
35
|
+
import { HttpStorageService } from './service';
|
|
36
|
+
import type { IStoragePathRequest, IStorageSyncRequest } from './model';
|
|
37
|
+
import type { IHttpStorageProviderFactory } from './provider';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Options for creating storage routes.
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
export interface ICreateStorageRoutesOptions {
|
|
44
|
+
readonly providers: IHttpStorageProviderFactory;
|
|
45
|
+
readonly logger?: Logging.ILogger;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function parsePathQuery(path: string | undefined, namespace: string | undefined): IStoragePathRequest {
|
|
49
|
+
return {
|
|
50
|
+
path: path ?? '/',
|
|
51
|
+
namespace
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parseSyncRequest(namespace: string | undefined): IStorageSyncRequest {
|
|
56
|
+
return { namespace };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Builds storage routes for a Hono app.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export function createStorageRoutes(options: ICreateStorageRoutesOptions): Hono {
|
|
64
|
+
const routes = new Hono();
|
|
65
|
+
const service = new HttpStorageService(options.providers);
|
|
66
|
+
|
|
67
|
+
routes.get('/health', async (c) => {
|
|
68
|
+
const namespace = c.req.query('namespace');
|
|
69
|
+
const syncResult = await service.sync(parseSyncRequest(namespace));
|
|
70
|
+
if (syncResult.isFailure()) {
|
|
71
|
+
return c.json({ error: syncResult.message }, 500);
|
|
72
|
+
}
|
|
73
|
+
return c.json({ status: 'ok', namespace: namespace ?? 'default' });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
routes.get('/tree/item', async (c) => {
|
|
77
|
+
const parsed = storagePathRequest.convert(parsePathQuery(c.req.query('path'), c.req.query('namespace')));
|
|
78
|
+
if (parsed.isFailure()) {
|
|
79
|
+
return c.json({ error: `invalid request: ${parsed.message}` }, 400);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const result = await service.getItem(parsed.value);
|
|
83
|
+
if (result.isFailure()) {
|
|
84
|
+
return c.json({ error: result.message }, 404);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const converted = storageTreeItem.convert(result.value);
|
|
88
|
+
if (converted.isFailure()) {
|
|
89
|
+
return c.json({ error: `invalid response: ${converted.message}` }, 500);
|
|
90
|
+
}
|
|
91
|
+
return c.json(converted.value);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
routes.get('/tree/children', async (c) => {
|
|
95
|
+
const parsed = storagePathRequest.convert(parsePathQuery(c.req.query('path'), c.req.query('namespace')));
|
|
96
|
+
if (parsed.isFailure()) {
|
|
97
|
+
return c.json({ error: `invalid request: ${parsed.message}` }, 400);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const result = await service.getChildren(parsed.value);
|
|
101
|
+
if (result.isFailure()) {
|
|
102
|
+
return c.json({ error: result.message }, 404);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const converted = storageTreeChildrenResponse.convert(result.value);
|
|
106
|
+
if (converted.isFailure()) {
|
|
107
|
+
return c.json({ error: `invalid response: ${converted.message}` }, 500);
|
|
108
|
+
}
|
|
109
|
+
return c.json(converted.value);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
routes.get('/file', async (c) => {
|
|
113
|
+
const parsed = storagePathRequest.convert(parsePathQuery(c.req.query('path'), c.req.query('namespace')));
|
|
114
|
+
if (parsed.isFailure()) {
|
|
115
|
+
return c.json({ error: `invalid request: ${parsed.message}` }, 400);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const result = await service.getFile(parsed.value);
|
|
119
|
+
if (result.isFailure()) {
|
|
120
|
+
return c.json({ error: result.message }, 404);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const converted = storageFileResponse.convert(result.value);
|
|
124
|
+
if (converted.isFailure()) {
|
|
125
|
+
return c.json({ error: `invalid response: ${converted.message}` }, 500);
|
|
126
|
+
}
|
|
127
|
+
return c.json(converted.value);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
routes.put('/file', async (c) => {
|
|
131
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
132
|
+
if (raw === undefined) {
|
|
133
|
+
return c.json({ error: 'invalid JSON body' }, 400);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const parsed = storageWriteFileRequest.convert(raw);
|
|
137
|
+
if (parsed.isFailure()) {
|
|
138
|
+
return c.json({ error: `invalid request: ${parsed.message}` }, 400);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const result = await service.saveFile(parsed.value);
|
|
142
|
+
if (result.isFailure()) {
|
|
143
|
+
return c.json({ error: result.message }, 400);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const converted = storageFileResponse.convert(result.value);
|
|
147
|
+
if (converted.isFailure()) {
|
|
148
|
+
return c.json({ error: `invalid response: ${converted.message}` }, 500);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
options.logger?.detail(`storage write: ${parsed.value.namespace ?? 'default'}:${parsed.value.path}`);
|
|
152
|
+
return c.json(converted.value);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
routes.delete('/file', async (c) => {
|
|
156
|
+
const parsed = storagePathRequest.convert(parsePathQuery(c.req.query('path'), c.req.query('namespace')));
|
|
157
|
+
if (parsed.isFailure()) {
|
|
158
|
+
return c.json({ error: `invalid request: ${parsed.message}` }, 400);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const result = await service.deleteFile(parsed.value);
|
|
162
|
+
if (result.isFailure()) {
|
|
163
|
+
return c.json({ error: result.message }, 404);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
options.logger?.detail(`storage delete: ${parsed.value.namespace ?? 'default'}:${parsed.value.path}`);
|
|
167
|
+
return c.json({ deleted: result.value });
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
routes.post('/directories', async (c) => {
|
|
171
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
172
|
+
if (raw === undefined) {
|
|
173
|
+
return c.json({ error: 'invalid JSON body' }, 400);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const parsed = storagePathRequest.convert(raw);
|
|
177
|
+
if (parsed.isFailure()) {
|
|
178
|
+
return c.json({ error: `invalid request: ${parsed.message}` }, 400);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const result = await service.createDirectory(parsed.value);
|
|
182
|
+
if (result.isFailure()) {
|
|
183
|
+
return c.json({ error: result.message }, 400);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const converted = storageTreeItem.convert(result.value);
|
|
187
|
+
if (converted.isFailure()) {
|
|
188
|
+
return c.json({ error: `invalid response: ${converted.message}` }, 500);
|
|
189
|
+
}
|
|
190
|
+
return c.json(converted.value);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
routes.post('/sync', async (c) => {
|
|
194
|
+
const raw = await c.req.json().catch(() => ({}));
|
|
195
|
+
const parsed = storageSyncRequest.convert(raw);
|
|
196
|
+
if (parsed.isFailure()) {
|
|
197
|
+
return c.json({ error: `invalid request: ${parsed.message}` }, 400);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const result = await service.sync(parsed.value);
|
|
201
|
+
if (result.isFailure()) {
|
|
202
|
+
return c.json({ error: result.message }, 500);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return c.json(result.value);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
routes.onError((err, c) => {
|
|
209
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
210
|
+
options.logger?.error(`storage route error: ${message}`);
|
|
211
|
+
return c.json({ error: message }, 500);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return routes;
|
|
215
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { fail, type Result, succeed } from '@fgv/ts-utils';
|
|
24
|
+
|
|
25
|
+
import type {
|
|
26
|
+
IHttpStorageProvider,
|
|
27
|
+
IStorageFileResponse,
|
|
28
|
+
IStoragePathRequest,
|
|
29
|
+
IStorageSyncRequest,
|
|
30
|
+
IStorageSyncResponse,
|
|
31
|
+
IStorageTreeChildrenResponse,
|
|
32
|
+
IStorageTreeItem,
|
|
33
|
+
IStorageWriteFileRequest
|
|
34
|
+
} from './model';
|
|
35
|
+
import type { IHttpStorageProviderFactory } from './provider';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Service layer for storage API operations.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export class HttpStorageService {
|
|
42
|
+
private readonly _providers: IHttpStorageProviderFactory;
|
|
43
|
+
|
|
44
|
+
public constructor(providers: IHttpStorageProviderFactory) {
|
|
45
|
+
this._providers = providers;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async getItem(request: IStoragePathRequest): Promise<Result<IStorageTreeItem>> {
|
|
49
|
+
const providerResult = this._getProvider(request.namespace);
|
|
50
|
+
if (providerResult.isFailure()) {
|
|
51
|
+
return fail(providerResult.message);
|
|
52
|
+
}
|
|
53
|
+
return providerResult.value.getItem(request.path);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public async getChildren(request: IStoragePathRequest): Promise<Result<IStorageTreeChildrenResponse>> {
|
|
57
|
+
const providerResult = this._getProvider(request.namespace);
|
|
58
|
+
if (providerResult.isFailure()) {
|
|
59
|
+
return fail(providerResult.message);
|
|
60
|
+
}
|
|
61
|
+
const childrenResult = await providerResult.value.getChildren(request.path);
|
|
62
|
+
if (childrenResult.isFailure()) {
|
|
63
|
+
return fail(childrenResult.message);
|
|
64
|
+
}
|
|
65
|
+
return succeed({
|
|
66
|
+
path: request.path,
|
|
67
|
+
children: childrenResult.value
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public async getFile(request: IStoragePathRequest): Promise<Result<IStorageFileResponse>> {
|
|
72
|
+
const providerResult = this._getProvider(request.namespace);
|
|
73
|
+
if (providerResult.isFailure()) {
|
|
74
|
+
return fail(providerResult.message);
|
|
75
|
+
}
|
|
76
|
+
return providerResult.value.getFile(request.path);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public async saveFile(request: IStorageWriteFileRequest): Promise<Result<IStorageFileResponse>> {
|
|
80
|
+
const providerResult = this._getProvider(request.namespace);
|
|
81
|
+
if (providerResult.isFailure()) {
|
|
82
|
+
return fail(providerResult.message);
|
|
83
|
+
}
|
|
84
|
+
return providerResult.value.saveFile(request.path, request.contents, request.contentType);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public async deleteFile(request: IStoragePathRequest): Promise<Result<boolean>> {
|
|
88
|
+
const providerResult = this._getProvider(request.namespace);
|
|
89
|
+
if (providerResult.isFailure()) {
|
|
90
|
+
return fail(providerResult.message);
|
|
91
|
+
}
|
|
92
|
+
return providerResult.value.deleteFile(request.path);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public async createDirectory(request: IStoragePathRequest): Promise<Result<IStorageTreeItem>> {
|
|
96
|
+
const providerResult = this._getProvider(request.namespace);
|
|
97
|
+
if (providerResult.isFailure()) {
|
|
98
|
+
return fail(providerResult.message);
|
|
99
|
+
}
|
|
100
|
+
return providerResult.value.createDirectory(request.path);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public async sync(request: IStorageSyncRequest): Promise<Result<IStorageSyncResponse>> {
|
|
104
|
+
const providerResult = this._getProvider(request.namespace);
|
|
105
|
+
if (providerResult.isFailure()) {
|
|
106
|
+
return fail(providerResult.message);
|
|
107
|
+
}
|
|
108
|
+
return providerResult.value.sync();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private _getProvider(namespace?: string): Result<IHttpStorageProvider> {
|
|
112
|
+
return this._providers.forNamespace(namespace).withErrorFormat((msg) => `provider: ${msg}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
import { FsStorageProvider, FsStorageProviderFactory, sanitizeNamespace } from '../../../packlets/storage';
|
|
6
|
+
|
|
7
|
+
describe('sanitizeNamespace', () => {
|
|
8
|
+
test('returns default when namespace is empty', () => {
|
|
9
|
+
expect(sanitizeNamespace(undefined).isSuccess()).toBe(true);
|
|
10
|
+
expect(sanitizeNamespace(' ').isSuccess()).toBe(true);
|
|
11
|
+
expect(sanitizeNamespace(' ').orThrow()).toBe('default');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('accepts valid namespace values', () => {
|
|
15
|
+
const result = sanitizeNamespace('team.alpha_01-dev');
|
|
16
|
+
expect(result.isSuccess()).toBe(true);
|
|
17
|
+
expect(result.orThrow()).toBe('team.alpha_01-dev');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('rejects invalid namespace characters', () => {
|
|
21
|
+
const result = sanitizeNamespace('../escape');
|
|
22
|
+
expect(result.isFailure()).toBe(true);
|
|
23
|
+
expect(result.message).toContain('only [a-zA-Z0-9._-] allowed');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('FsStorageProvider', () => {
|
|
28
|
+
let rootPath: string;
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
rootPath = fs.mkdtempSync(path.join(os.tmpdir(), 'ts-http-storage-test-'));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
afterEach(() => {
|
|
35
|
+
fs.rmSync(rootPath, { recursive: true, force: true });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('saves and loads files', async () => {
|
|
39
|
+
const provider = new FsStorageProvider(rootPath);
|
|
40
|
+
|
|
41
|
+
const saveResult = await provider.saveFile('/data/example.txt', 'hello');
|
|
42
|
+
expect(saveResult.isSuccess()).toBe(true);
|
|
43
|
+
|
|
44
|
+
const fileResult = await provider.getFile('/data/example.txt');
|
|
45
|
+
expect(fileResult.isSuccess()).toBe(true);
|
|
46
|
+
expect(fileResult.orThrow()).toEqual({
|
|
47
|
+
path: '/data/example.txt',
|
|
48
|
+
contents: 'hello'
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('lists children in a directory', async () => {
|
|
53
|
+
const provider = new FsStorageProvider(rootPath);
|
|
54
|
+
await provider.saveFile('/data/one.txt', 'one');
|
|
55
|
+
await provider.saveFile('/data/two.txt', 'two');
|
|
56
|
+
|
|
57
|
+
const childrenResult = await provider.getChildren('/data');
|
|
58
|
+
expect(childrenResult.isSuccess()).toBe(true);
|
|
59
|
+
|
|
60
|
+
const childNames = childrenResult
|
|
61
|
+
.orThrow()
|
|
62
|
+
.map((child) => child.name)
|
|
63
|
+
.sort();
|
|
64
|
+
expect(childNames).toEqual(['one.txt', 'two.txt']);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('normalizes traversal-like request paths under storage root', async () => {
|
|
68
|
+
const provider = new FsStorageProvider(rootPath);
|
|
69
|
+
|
|
70
|
+
const result = await provider.getItem('../../etc/passwd');
|
|
71
|
+
expect(result.isFailure()).toBe(true);
|
|
72
|
+
expect(result.message).toContain('ENOENT');
|
|
73
|
+
expect(result.message).toContain(path.join(rootPath, 'etc/passwd'));
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('deletes an existing file', async () => {
|
|
77
|
+
const provider = new FsStorageProvider(rootPath);
|
|
78
|
+
await provider.saveFile('/data/delete-me.txt', 'temporary');
|
|
79
|
+
|
|
80
|
+
const deleteResult = await provider.deleteFile('/data/delete-me.txt');
|
|
81
|
+
expect(deleteResult.isSuccess()).toBe(true);
|
|
82
|
+
expect(deleteResult.orThrow()).toBe(true);
|
|
83
|
+
|
|
84
|
+
const readResult = await provider.getFile('/data/delete-me.txt');
|
|
85
|
+
expect(readResult.isFailure()).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('FsStorageProviderFactory', () => {
|
|
90
|
+
let rootPath: string;
|
|
91
|
+
|
|
92
|
+
beforeEach(() => {
|
|
93
|
+
rootPath = fs.mkdtempSync(path.join(os.tmpdir(), 'ts-http-storage-factory-'));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
afterEach(() => {
|
|
97
|
+
fs.rmSync(rootPath, { recursive: true, force: true });
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('creates namespace-scoped providers', async () => {
|
|
101
|
+
const factory = new FsStorageProviderFactory({ rootPath });
|
|
102
|
+
|
|
103
|
+
const providerResult = factory.forNamespace('team-a');
|
|
104
|
+
expect(providerResult.isSuccess()).toBe(true);
|
|
105
|
+
|
|
106
|
+
const provider = providerResult.orThrow();
|
|
107
|
+
const saveResult = await provider.saveFile('/notes.txt', 'namespaced');
|
|
108
|
+
expect(saveResult.isSuccess()).toBe(true);
|
|
109
|
+
|
|
110
|
+
const expectedPath = path.join(rootPath, 'team-a', 'notes.txt');
|
|
111
|
+
expect(fs.existsSync(expectedPath)).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('fails for invalid namespace', () => {
|
|
115
|
+
const factory = new FsStorageProviderFactory({ rootPath });
|
|
116
|
+
const result = factory.forNamespace('bad/name');
|
|
117
|
+
|
|
118
|
+
expect(result.isFailure()).toBe(true);
|
|
119
|
+
expect(result.message).toContain('only [a-zA-Z0-9._-] allowed');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('rejects namespace that resolves outside root', () => {
|
|
123
|
+
const factory = new FsStorageProviderFactory({ rootPath });
|
|
124
|
+
const result = factory.forNamespace('..');
|
|
125
|
+
|
|
126
|
+
expect(result.isFailure()).toBe(true);
|
|
127
|
+
expect(result.message).toContain('outside root');
|
|
128
|
+
});
|
|
129
|
+
});
|