@wener/common 2.0.5 → 2.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ai/qwen3vl/index.js +1 -1
- package/lib/ai/qwen3vl/utils.js +15 -15
- package/lib/ai/vision/DocLayoutElementTypeSchema.js +22 -22
- package/lib/ai/vision/ImageAnnotationSchema.js +63 -47
- package/lib/ai/vision/index.js +2 -2
- package/lib/ai/vision/resolveImageAnnotation.js +81 -95
- package/lib/cn/ChineseResidentIdNo.js +55 -41
- package/lib/cn/ChineseResidentIdNo.mod.js +6 -1
- package/lib/cn/ChineseResidentIdNo.test.js +22 -21
- package/lib/cn/DivisionCode.js +220 -235
- package/lib/cn/DivisionCode.mod.js +6 -1
- package/lib/cn/DivisionCode.test.js +92 -121
- package/lib/cn/Mod11.js +18 -37
- package/lib/cn/Mod31.js +23 -41
- package/lib/cn/UnifiedSocialCreditCode.js +143 -137
- package/lib/cn/UnifiedSocialCreditCode.mod.js +6 -1
- package/lib/cn/UnifiedSocialCreditCode.test.js +21 -15
- package/lib/cn/formatChineseAmount.js +46 -71
- package/lib/cn/index.js +6 -6
- package/lib/cn/mod.js +5 -3
- package/lib/cn/parseChineseNumber.js +81 -85
- package/lib/cn/parseChineseNumber.test.js +183 -261
- package/lib/cn/pinyin/cartesianProduct.js +19 -19
- package/lib/cn/pinyin/cartesianProduct.test.js +78 -178
- package/lib/cn/pinyin/loader.js +13 -11
- package/lib/cn/pinyin/preload.js +2 -1
- package/lib/cn/pinyin/toPinyin.test.js +149 -161
- package/lib/cn/pinyin/toPinyinPure.js +28 -23
- package/lib/cn/pinyin/transform.js +11 -11
- package/lib/cn/types.d.js +2 -2
- package/lib/consola/createStandardConsolaReporter.js +14 -15
- package/lib/consola/formatLogObject.js +149 -133
- package/lib/consola/formatLogObject.test.js +167 -178
- package/lib/consola/index.js +2 -2
- package/lib/data/formatSort.js +14 -12
- package/lib/data/formatSort.test.js +33 -33
- package/lib/data/index.js +3 -3
- package/lib/data/maybeNumber.js +23 -23
- package/lib/data/parseSort.js +75 -68
- package/lib/data/parseSort.test.js +196 -187
- package/lib/data/resolvePagination.js +38 -39
- package/lib/data/resolvePagination.test.js +228 -218
- package/lib/data/types.d.js +2 -2
- package/lib/dayjs/dayjs.js +20 -20
- package/lib/dayjs/formatDuration.js +56 -56
- package/lib/dayjs/formatDuration.test.js +63 -77
- package/lib/dayjs/index.js +4 -4
- package/lib/dayjs/parseDuration.js +21 -26
- package/lib/dayjs/parseRelativeTime.js +65 -66
- package/lib/dayjs/parseRelativeTime.test.js +227 -243
- package/lib/dayjs/resolveRelativeTime.js +73 -72
- package/lib/dayjs/resolveRelativeTime.test.js +296 -307
- package/lib/decimal/index.js +1 -1
- package/lib/decimal/parseDecimal.js +12 -12
- package/lib/drain3/Drain.js +303 -338
- package/lib/drain3/LogCluster.js +25 -25
- package/lib/drain3/Node.js +24 -24
- package/lib/drain3/TemplateMiner.js +197 -196
- package/lib/drain3/index.js +5 -5
- package/lib/drain3/persistence/FilePersistence.js +19 -19
- package/lib/drain3/persistence/MemoryPersistence.js +8 -8
- package/lib/drain3/persistence/PersistenceHandler.js +2 -2
- package/lib/drain3/types.js +2 -2
- package/lib/emittery/emitter.js +7 -7
- package/lib/emittery/index.js +1 -1
- package/lib/foundation/schema/SexType.js +15 -12
- package/lib/foundation/schema/index.js +1 -1
- package/lib/foundation/schema/parseSexType.js +15 -16
- package/lib/foundation/schema/types.js +8 -6
- package/lib/fs/FileSystemError.js +18 -18
- package/lib/fs/IFileSystem.d.js +2 -2
- package/lib/fs/MemoryFileSystem.test.js +172 -181
- package/lib/fs/createBrowserFileSystem.js +222 -235
- package/lib/fs/createMemoryFileSystem.js +472 -510
- package/lib/fs/createSandboxFileSystem.js +102 -101
- package/lib/fs/createWebDavFileSystem.js +162 -149
- package/lib/fs/createWebFileSystem.js +197 -220
- package/lib/fs/findMimeType.js +14 -14
- package/lib/fs/index.js +7 -7
- package/lib/fs/minio/createMinioFileSystem.js +959 -956
- package/lib/fs/minio/index.js +1 -1
- package/lib/fs/orpc/FileSystemContract.js +57 -57
- package/lib/fs/orpc/createContractClientFileSystem.js +88 -88
- package/lib/fs/orpc/index.js +2 -2
- package/lib/fs/orpc/server/createFileSystemContractImpl.js +62 -60
- package/lib/fs/orpc/server/index.js +1 -1
- package/lib/fs/s3/createS3MiniFileSystem.js +756 -737
- package/lib/fs/s3/index.js +1 -1
- package/lib/fs/s3/s3mini.test.js +524 -553
- package/lib/fs/scandir.js +56 -56
- package/lib/fs/server/createDatabaseFileSystem.js +834 -741
- package/lib/fs/server/createNodeFileSystem.js +407 -405
- package/lib/fs/server/dbfs.test.js +201 -214
- package/lib/fs/server/index.js +1 -1
- package/lib/fs/server/loadTestDatabase.js +40 -43
- package/lib/fs/tests/runFileSystemTest.js +352 -316
- package/lib/fs/types.js +17 -20
- package/lib/fs/utils/getFileUrl.js +24 -30
- package/lib/fs/utils.js +17 -17
- package/lib/fs/webdav/index.js +1 -1
- package/lib/index.js +2 -2
- package/lib/jsonschema/JsonSchema.js +216 -155
- package/lib/jsonschema/JsonSchema.test.js +123 -124
- package/lib/jsonschema/forEachJsonSchema.js +41 -41
- package/lib/jsonschema/index.js +2 -2
- package/lib/jsonschema/types.d.js +2 -2
- package/lib/meta/defineFileType.js +32 -38
- package/lib/meta/defineInit.js +39 -35
- package/lib/meta/defineMetadata.js +37 -34
- package/lib/meta/defineMetadata.test.js +13 -12
- package/lib/meta/index.js +3 -3
- package/lib/orpc/createOpenApiContractClient.js +26 -24
- package/lib/orpc/createRpcContractClient.js +37 -31
- package/lib/orpc/index.js +2 -2
- package/lib/orpc/resolveLinkPlugins.js +25 -25
- package/lib/password/PHC.js +187 -189
- package/lib/password/PHC.test.js +517 -535
- package/lib/password/Password.js +85 -80
- package/lib/password/Password.test.js +330 -364
- package/lib/password/createArgon2PasswordAlgorithm.js +50 -51
- package/lib/password/createBase64PasswordAlgorithm.js +11 -11
- package/lib/password/createBcryptPasswordAlgorithm.js +20 -18
- package/lib/password/createPBKDF2PasswordAlgorithm.js +65 -52
- package/lib/password/createScryptPasswordAlgorithm.js +74 -63
- package/lib/password/index.js +5 -5
- package/lib/password/server/index.js +1 -1
- package/lib/resource/Identifiable.js +2 -2
- package/lib/resource/ListQuery.js +42 -42
- package/lib/resource/getTitleOfResource.js +5 -5
- package/lib/resource/index.js +2 -2
- package/lib/resource/schema/AnyResourceSchema.js +91 -89
- package/lib/resource/schema/BaseResourceSchema.js +26 -26
- package/lib/resource/schema/ResourceActionType.js +117 -115
- package/lib/resource/schema/ResourceStatus.js +94 -92
- package/lib/resource/schema/ResourceType.js +25 -23
- package/lib/resource/schema/index.js +5 -5
- package/lib/resource/schema/types.js +86 -55
- package/lib/resource/schema/types.test.js +16 -13
- package/lib/s3/formatS3Url.js +60 -60
- package/lib/s3/formatS3Url.test.js +238 -261
- package/lib/s3/index.js +2 -2
- package/lib/s3/parseS3Url.js +61 -60
- package/lib/s3/parseS3Url.test.js +270 -269
- package/lib/schema/SchemaRegistry.js +41 -42
- package/lib/schema/SchemaRegistry.mod.js +1 -1
- package/lib/schema/TypeSchema.d.js +2 -2
- package/lib/schema/createSchemaData.js +113 -67
- package/lib/schema/findJsonSchemaByPath.js +28 -23
- package/lib/schema/formatZodError.js +112 -131
- package/lib/schema/formatZodError.test.js +192 -195
- package/lib/schema/getSchemaCache.js +7 -7
- package/lib/schema/getSchemaOptions.js +17 -16
- package/lib/schema/index.js +6 -6
- package/lib/schema/toJsonSchema.js +195 -189
- package/lib/schema/toJsonSchema.test.js +34 -26
- package/lib/schema/validate.js +105 -96
- package/lib/tools/generateSchema.js +40 -40
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js +74 -74
- package/lib/utils/buildBaseUrl.js +8 -8
- package/lib/utils/buildRedactorFormSchema.js +54 -53
- package/lib/utils/getEstimateProcessTime.js +24 -19
- package/lib/utils/index.js +3 -3
- package/lib/utils/resolveFeatureOptions.js +9 -9
- package/package.json +14 -14
- package/src/ai/vision/index.ts +2 -2
- package/src/cn/index.ts +1 -2
- package/src/consola/index.ts +1 -1
- package/src/data/index.ts +3 -4
- package/src/data/resolvePagination.ts +2 -2
- package/src/dayjs/formatDuration.ts +8 -9
- package/src/dayjs/index.ts +1 -1
- package/src/dayjs/parseRelativeTime.ts +1 -1
- package/src/dayjs/resolveRelativeTime.ts +1 -1
- package/src/drain3/Drain.test.ts +2 -2
- package/src/drain3/index.ts +2 -4
- package/src/fs/createWebDavFileSystem.ts +2 -7
- package/src/fs/createWebFileSystem.ts +1 -1
- package/src/fs/index.ts +4 -4
- package/src/fs/minio/createMinioFileSystem.ts +2 -2
- package/src/fs/minio/index.ts +1 -1
- package/src/fs/s3/createS3MiniFileSystem.ts +1 -1
- package/src/fs/server/createDatabaseFileSystem.ts +84 -120
- package/src/fs/server/dbfs.test.ts +14 -10
- package/src/fs/server/index.ts +1 -0
- package/src/fs/server/loadTestDatabase.ts +8 -119
- package/src/jsonschema/index.ts +1 -1
- package/src/meta/index.ts +2 -3
- package/src/orm/createSqliteDialect.ts +17 -0
- package/src/orm/index.ts +1 -0
- package/src/orpc/createOpenApiContractClient.ts +1 -1
- package/src/orpc/index.ts +1 -1
- package/src/password/createArgon2PasswordAlgorithm.ts +1 -1
- package/src/password/index.ts +2 -2
- package/src/resource/index.ts +3 -3
- package/src/resource/schema/index.ts +4 -4
- package/src/s3/index.ts +1 -1
- package/src/schema/SchemaRegistry.ts +1 -1
- package/src/schema/createSchemaData.ts +1 -1
- package/src/schema/findJsonSchemaByPath.ts +1 -1
- package/src/schema/index.ts +5 -5
- package/src/schema/validate.ts +1 -1
- package/src/utils/buildRedactorFormSchema.ts +1 -1
- package/src/utils/formatNumber.ts +18 -0
- package/src/utils/formatPercent.ts +17 -0
- package/src/utils/index.ts +3 -3
- package/src/utils/resolveFeatureOptions.ts +1 -1
|
@@ -1,108 +1,109 @@
|
|
|
1
|
-
import { join, normalize, relative, sep } from
|
|
2
|
-
import { getPath } from
|
|
1
|
+
import { join, normalize, relative, sep } from 'pathe';
|
|
2
|
+
import { getPath } from './utils';
|
|
3
|
+
|
|
3
4
|
let SandboxSecurityError = class SandboxSecurityError extends Error {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'SandboxSecurityError';
|
|
8
|
+
}
|
|
8
9
|
};
|
|
9
10
|
export function createSandboxFileSystem(fs, basePath) {
|
|
10
|
-
|
|
11
|
+
return new SandboxFS(fs, basePath);
|
|
11
12
|
}
|
|
12
13
|
let SandboxFS = class SandboxFS {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
14
|
+
fs;
|
|
15
|
+
basePath;
|
|
16
|
+
constructor(fs, basePath) {
|
|
17
|
+
this.fs = fs;
|
|
18
|
+
this.basePath = normalize(basePath);
|
|
19
|
+
}
|
|
20
|
+
_resolvePath(userPath) {
|
|
21
|
+
const fullPath = join(this.basePath, userPath);
|
|
22
|
+
const normalizedFullPath = normalize(fullPath);
|
|
23
|
+
const rel = relative(this.basePath, normalizedFullPath);
|
|
24
|
+
if (rel.startsWith('..') || rel === '..') {
|
|
25
|
+
throw new SandboxSecurityError(`Path traversal attempt detected: ${userPath}`);
|
|
26
|
+
}
|
|
27
|
+
return normalizedFullPath;
|
|
28
|
+
}
|
|
29
|
+
_stripPath(fullPath) {
|
|
30
|
+
const relPath = relative(this.basePath, fullPath);
|
|
31
|
+
// 保证返回的是一个以 '/' 开头的绝对路径(在沙箱内)
|
|
32
|
+
return sep + relPath.split(sep).join('/');
|
|
33
|
+
}
|
|
34
|
+
_processStat(stat) {
|
|
35
|
+
stat.path = this._stripPath(stat.path);
|
|
36
|
+
stat.directory = this._stripPath(stat.directory);
|
|
37
|
+
return stat;
|
|
38
|
+
}
|
|
39
|
+
async stat(path, options) {
|
|
40
|
+
const fullPath = this._resolvePath(path);
|
|
41
|
+
const result = await this.fs.stat(fullPath, options);
|
|
42
|
+
return this._processStat(result);
|
|
43
|
+
}
|
|
44
|
+
async readdir(dir, options) {
|
|
45
|
+
const fullPath = this._resolvePath(dir);
|
|
46
|
+
const results = await this.fs.readdir(fullPath, options);
|
|
47
|
+
return results.map((stat) => this._processStat(stat));
|
|
48
|
+
}
|
|
49
|
+
async exists(path) {
|
|
50
|
+
try {
|
|
51
|
+
const fullPath = this._resolvePath(path);
|
|
52
|
+
return await this.fs.exists(fullPath);
|
|
53
|
+
} catch (e) {
|
|
54
|
+
if (e instanceof SandboxSecurityError) {
|
|
55
|
+
return false; // 越界访问视为不存在
|
|
56
|
+
}
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
mkdir(path, options) {
|
|
61
|
+
const fullPath = this._resolvePath(path);
|
|
62
|
+
return this.fs.mkdir(fullPath, options);
|
|
63
|
+
}
|
|
64
|
+
readFile(path, options) {
|
|
65
|
+
const fullPath = this._resolvePath(path);
|
|
66
|
+
return this.fs.readFile(fullPath, options);
|
|
67
|
+
}
|
|
68
|
+
writeFile(path, data, options) {
|
|
69
|
+
const fullPath = this._resolvePath(path);
|
|
70
|
+
return this.fs.writeFile(fullPath, data, options);
|
|
71
|
+
}
|
|
72
|
+
rm(path, options) {
|
|
73
|
+
const fullPath = this._resolvePath(path);
|
|
74
|
+
return this.fs.rm(fullPath, options);
|
|
75
|
+
}
|
|
76
|
+
rename(oldPath, newPath, options) {
|
|
77
|
+
const fullOldPath = this._resolvePath(oldPath);
|
|
78
|
+
const fullNewPath = this._resolvePath(newPath);
|
|
79
|
+
return this.fs.rename(fullOldPath, fullNewPath, options);
|
|
80
|
+
}
|
|
81
|
+
copy(src, dest, options) {
|
|
82
|
+
const fullSrc = this._resolvePath(src);
|
|
83
|
+
const fullDest = this._resolvePath(dest);
|
|
84
|
+
return this.fs.copy(fullSrc, fullDest, options);
|
|
85
|
+
}
|
|
86
|
+
createReadStream(path, options) {
|
|
87
|
+
const fullPath = this._resolvePath(path);
|
|
88
|
+
if (!this.fs.createReadStream) {
|
|
89
|
+
throw new Error('Underlying filesystem does not support createReadStream');
|
|
90
|
+
}
|
|
91
|
+
return this.fs.createReadStream(fullPath, options);
|
|
92
|
+
}
|
|
93
|
+
createWriteStream(path, options) {
|
|
94
|
+
const fullPath = this._resolvePath(path);
|
|
95
|
+
if (!this.fs.createWriteStream) {
|
|
96
|
+
throw new Error('Underlying filesystem does not support createWriteStream');
|
|
97
|
+
}
|
|
98
|
+
return this.fs.createWriteStream(fullPath, options);
|
|
99
|
+
}
|
|
100
|
+
getUrl(file, options) {
|
|
101
|
+
let path = this._resolvePath(getPath(file.path));
|
|
102
|
+
if (this.fs.getUrl) {
|
|
103
|
+
return this.fs.getUrl(path, options);
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
106
107
|
};
|
|
107
108
|
|
|
108
|
-
//# sourceMappingURL=createSandboxFileSystem.js.map
|
|
109
|
+
//# sourceMappingURL=createSandboxFileSystem.js.map
|
|
@@ -1,154 +1,167 @@
|
|
|
1
|
-
import { maybeFunction } from
|
|
1
|
+
import { maybeFunction } from '@wener/utils';
|
|
2
2
|
export function createWebDavFileSystem({ client }) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
let fs = new WebdavFS({
|
|
4
|
+
client,
|
|
5
|
+
});
|
|
6
|
+
return fs;
|
|
7
7
|
}
|
|
8
8
|
let WebdavFS = class WebdavFS {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
9
|
+
_client;
|
|
10
|
+
constructor({
|
|
11
|
+
client = () => {
|
|
12
|
+
throw new Error('WebdavFS client not initialized');
|
|
13
|
+
},
|
|
14
|
+
}) {
|
|
15
|
+
this._client = client;
|
|
16
|
+
}
|
|
17
|
+
set client(client) {
|
|
18
|
+
this._client = client;
|
|
19
|
+
}
|
|
20
|
+
get client() {
|
|
21
|
+
return maybeFunction(this._client);
|
|
22
|
+
}
|
|
23
|
+
toEntry(input) {
|
|
24
|
+
const { filename: path, basename, lastmod, type: kind, etag, size, mime } = input;
|
|
25
|
+
let meta = {};
|
|
26
|
+
if (etag) {
|
|
27
|
+
meta.etag = etag;
|
|
28
|
+
}
|
|
29
|
+
if (mime) {
|
|
30
|
+
meta.mime = mime;
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
directory: path.substring(0, path.lastIndexOf('/')) || '/',
|
|
34
|
+
path,
|
|
35
|
+
name: basename,
|
|
36
|
+
mtime: +new Date(lastmod),
|
|
37
|
+
kind,
|
|
38
|
+
meta,
|
|
39
|
+
size,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
getData(input) {
|
|
43
|
+
if (
|
|
44
|
+
input &&
|
|
45
|
+
typeof input === 'object' &&
|
|
46
|
+
'data' in input && // 'headers' in input &&
|
|
47
|
+
'status' in input &&
|
|
48
|
+
typeof input.status === 'number'
|
|
49
|
+
) {
|
|
50
|
+
return input.data;
|
|
51
|
+
}
|
|
52
|
+
return input;
|
|
53
|
+
}
|
|
54
|
+
async readdir(path, { glob, recursive, depth, kind, hidden, signal } = {}) {
|
|
55
|
+
// webdav depth 只支持 0,1
|
|
56
|
+
let o = {};
|
|
57
|
+
if (recursive) {
|
|
58
|
+
o.deep = true;
|
|
59
|
+
}
|
|
60
|
+
let res = await this.client.getDirectoryContents(path, {
|
|
61
|
+
deep: recursive,
|
|
62
|
+
signal,
|
|
63
|
+
});
|
|
64
|
+
let out = this.getData(res);
|
|
65
|
+
if (!recursive && typeof depth === 'number' && depth >= 2) {
|
|
66
|
+
let l = depth;
|
|
67
|
+
let cur = out;
|
|
68
|
+
while (l-- > 1) {
|
|
69
|
+
let sub = (
|
|
70
|
+
await Promise.all(
|
|
71
|
+
cur.map(async (v) => {
|
|
72
|
+
if (v.type === 'directory') {
|
|
73
|
+
return this.getData(
|
|
74
|
+
await this.client.getDirectoryContents(v.filename, {
|
|
75
|
+
signal,
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return [];
|
|
80
|
+
}),
|
|
81
|
+
)
|
|
82
|
+
).flat();
|
|
83
|
+
out = out.concat(...sub);
|
|
84
|
+
cur = sub;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (glob) {
|
|
88
|
+
const { default: def, matcher = def.matcher } = await import('micromatch');
|
|
89
|
+
const match = matcher(glob);
|
|
90
|
+
out = out.filter((v) => match(v.filename));
|
|
91
|
+
}
|
|
92
|
+
if (kind) {
|
|
93
|
+
out = out.filter((v) => v.type === kind);
|
|
94
|
+
}
|
|
95
|
+
if (!hidden) {
|
|
96
|
+
out = out.filter((v) => !v.basename.startsWith('.'));
|
|
97
|
+
}
|
|
98
|
+
return out.map((stat) => this.toEntry(stat));
|
|
99
|
+
}
|
|
100
|
+
async stat(path, { signal } = {}) {
|
|
101
|
+
const res = await this.client.stat(path, {
|
|
102
|
+
details: true,
|
|
103
|
+
signal,
|
|
104
|
+
});
|
|
105
|
+
return this.toEntry(this.getData(res));
|
|
106
|
+
}
|
|
107
|
+
async mkdir(path, { recursive, signal } = {}) {
|
|
108
|
+
await this.client.createDirectory(path, {
|
|
109
|
+
recursive,
|
|
110
|
+
signal,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async readFile(path, options = {}) {
|
|
114
|
+
const format = options.encoding === 'text' ? 'text' : 'binary';
|
|
115
|
+
const res = await this.client.getFileContents(path, {
|
|
116
|
+
format,
|
|
117
|
+
...options,
|
|
118
|
+
});
|
|
119
|
+
return this.getData(res);
|
|
120
|
+
}
|
|
121
|
+
async writeFile(path, data, options = {}) {
|
|
122
|
+
// Convert web ReadableStream to something WebDAV client can handle
|
|
123
|
+
let webdavData = data;
|
|
124
|
+
if (data instanceof ReadableStream) {
|
|
125
|
+
// Convert web ReadableStream to Buffer
|
|
126
|
+
const reader = data.getReader();
|
|
127
|
+
const chunks = [];
|
|
128
|
+
while (true) {
|
|
129
|
+
const { done, value } = await reader.read();
|
|
130
|
+
if (done) break;
|
|
131
|
+
if (value) chunks.push(value);
|
|
132
|
+
}
|
|
133
|
+
webdavData = Buffer.concat(chunks);
|
|
134
|
+
} else if (ArrayBuffer.isView(data) && !(data instanceof Buffer)) {
|
|
135
|
+
// Convert ArrayBufferView to Buffer
|
|
136
|
+
webdavData = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
137
|
+
}
|
|
138
|
+
await this.client.putFileContents(path, webdavData, options);
|
|
139
|
+
}
|
|
140
|
+
async rm(path, { signal: _signal, force, recursive: _recursive } = {}) {
|
|
141
|
+
try {
|
|
142
|
+
await this.client.deleteFile(path);
|
|
143
|
+
} catch (e) {
|
|
144
|
+
if (force && e.status === 404) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
throw e;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async rename(oldPath, newPath, options = {}) {
|
|
151
|
+
await this.client.moveFile(oldPath, newPath, options);
|
|
152
|
+
}
|
|
153
|
+
async exists(path) {
|
|
154
|
+
return await this.client.exists(path);
|
|
155
|
+
}
|
|
156
|
+
async copy(src, dest, options = {}) {
|
|
157
|
+
await this.client.copyFile(src, dest, options);
|
|
158
|
+
}
|
|
159
|
+
createReadStream(path, options = {}) {
|
|
160
|
+
return this.client.createReadStream(path, options);
|
|
161
|
+
}
|
|
162
|
+
createWriteStream(path, options = {}) {
|
|
163
|
+
return this.client.createWriteStream(path, options);
|
|
164
|
+
}
|
|
152
165
|
};
|
|
153
166
|
|
|
154
|
-
//# sourceMappingURL=createWebDavFileSystem.js.map
|
|
167
|
+
//# sourceMappingURL=createWebDavFileSystem.js.map
|