@wener/common 2.0.2 → 2.0.5
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 +2 -0
- package/lib/ai/qwen3vl/index.js.map +1 -0
- package/lib/ai/qwen3vl/utils.js +31 -0
- package/lib/ai/qwen3vl/utils.js.map +1 -0
- package/lib/ai/vision/DocLayoutElementTypeSchema.js +28 -0
- package/lib/ai/vision/DocLayoutElementTypeSchema.js.map +1 -0
- package/lib/ai/vision/ImageAnnotationSchema.js +50 -0
- package/lib/ai/vision/ImageAnnotationSchema.js.map +1 -0
- package/lib/ai/vision/index.js +3 -0
- package/lib/ai/vision/index.js.map +1 -0
- package/lib/ai/vision/resolveImageAnnotation.js +105 -0
- package/lib/ai/vision/resolveImageAnnotation.js.map +1 -0
- package/lib/cn/ChineseResidentIdNo.js +22 -15
- package/lib/cn/ChineseResidentIdNo.js.map +1 -0
- package/lib/cn/ChineseResidentIdNo.test.js +1 -1
- package/lib/cn/DivisionCode.js +30 -25
- package/lib/cn/DivisionCode.js.map +1 -0
- package/lib/cn/DivisionCode.test.js +1 -1
- package/lib/cn/Mod11.js +38 -81
- package/lib/cn/Mod11.js.map +1 -0
- package/lib/cn/Mod31.js +41 -90
- package/lib/cn/Mod31.js.map +1 -0
- package/lib/cn/UnifiedSocialCreditCode.js +43 -34
- package/lib/cn/UnifiedSocialCreditCode.js.map +1 -0
- package/lib/cn/UnifiedSocialCreditCode.test.js +1 -1
- package/lib/cn/formatChineseAmount.js +77 -0
- package/lib/cn/formatChineseAmount.js.map +1 -0
- package/lib/cn/index.js +7 -1
- package/lib/cn/index.js.map +1 -0
- package/lib/cn/parseChineseNumber.js +94 -0
- package/lib/cn/parseChineseNumber.js.map +1 -0
- package/lib/cn/parseChineseNumber.test.js +278 -0
- package/lib/cn/pinyin/cartesianProduct.js +22 -0
- package/lib/cn/pinyin/cartesianProduct.js.map +1 -0
- package/lib/cn/pinyin/cartesianProduct.test.js +179 -0
- package/lib/cn/pinyin/data.json +23573 -0
- package/lib/cn/pinyin/loader.js +14 -0
- package/lib/cn/pinyin/loader.js.map +1 -0
- package/lib/cn/pinyin/preload.js +3 -0
- package/lib/cn/pinyin/preload.js.map +1 -0
- package/lib/cn/pinyin/toPinyin.test.js +167 -0
- package/lib/cn/pinyin/toPinyinPure.js +33 -0
- package/lib/cn/pinyin/toPinyinPure.js.map +1 -0
- package/lib/cn/pinyin/transform.js +14 -0
- package/lib/cn/pinyin/transform.js.map +1 -0
- package/lib/cn/types.d.js +2 -0
- package/lib/cn/types.d.js.map +1 -0
- package/lib/consola/createStandardConsolaReporter.js +6 -6
- package/lib/consola/createStandardConsolaReporter.js.map +1 -0
- package/lib/consola/formatLogObject.js +66 -146
- package/lib/consola/formatLogObject.js.map +1 -0
- package/lib/consola/formatLogObject.test.js +184 -0
- package/lib/consola/index.js +1 -0
- package/lib/consola/index.js.map +1 -0
- package/lib/data/formatSort.js +6 -5
- package/lib/data/formatSort.js.map +1 -0
- package/lib/data/index.js +1 -0
- package/lib/data/index.js.map +1 -0
- package/lib/data/maybeNumber.js +6 -8
- package/lib/data/maybeNumber.js.map +1 -0
- package/lib/data/parseSort.js +22 -28
- package/lib/data/parseSort.js.map +1 -0
- package/lib/data/resolvePagination.js +13 -17
- package/lib/data/resolvePagination.js.map +1 -0
- package/lib/data/types.d.js +2 -0
- package/lib/data/types.d.js.map +1 -0
- package/lib/dayjs/dayjs.js +21 -19
- package/lib/dayjs/dayjs.js.map +1 -0
- package/lib/dayjs/formatDuration.js +15 -14
- package/lib/dayjs/formatDuration.js.map +1 -0
- package/lib/dayjs/index.js +2 -0
- package/lib/dayjs/index.js.map +1 -0
- package/lib/dayjs/parseDuration.js +5 -8
- package/lib/dayjs/parseDuration.js.map +1 -0
- package/lib/dayjs/parseRelativeTime.js +90 -0
- package/lib/dayjs/parseRelativeTime.js.map +1 -0
- package/lib/dayjs/parseRelativeTime.test.js +247 -0
- package/lib/dayjs/resolveRelativeTime.js +87 -0
- package/lib/dayjs/resolveRelativeTime.js.map +1 -0
- package/lib/dayjs/resolveRelativeTime.test.js +310 -0
- package/lib/decimal/index.js +1 -0
- package/lib/decimal/index.js.map +1 -0
- package/lib/decimal/parseDecimal.js +3 -1
- package/lib/decimal/parseDecimal.js.map +1 -0
- package/lib/drain3/Drain.js +356 -0
- package/lib/drain3/Drain.js.map +1 -0
- package/lib/drain3/LogCluster.js +38 -0
- package/lib/drain3/LogCluster.js.map +1 -0
- package/lib/drain3/Node.js +39 -0
- package/lib/drain3/Node.js.map +1 -0
- package/lib/drain3/TemplateMiner.js +204 -0
- package/lib/drain3/TemplateMiner.js.map +1 -0
- package/lib/drain3/index.js +31 -0
- package/lib/drain3/index.js.map +1 -0
- package/lib/drain3/persistence/FilePersistence.js +24 -0
- package/lib/drain3/persistence/FilePersistence.js.map +1 -0
- package/lib/drain3/persistence/MemoryPersistence.js +18 -0
- package/lib/drain3/persistence/MemoryPersistence.js.map +1 -0
- package/lib/drain3/persistence/PersistenceHandler.js +5 -0
- package/lib/drain3/persistence/PersistenceHandler.js.map +1 -0
- package/lib/drain3/types.js +7 -0
- package/lib/drain3/types.js.map +1 -0
- package/lib/emittery/emitter.js +10 -0
- package/lib/emittery/emitter.js.map +1 -0
- package/lib/emittery/index.js +2 -0
- package/lib/emittery/index.js.map +1 -0
- package/lib/foundation/schema/SexType.js +5 -3
- package/lib/foundation/schema/SexType.js.map +1 -0
- package/lib/foundation/schema/index.js +1 -0
- package/lib/foundation/schema/index.js.map +1 -0
- package/lib/foundation/schema/parseSexType.js +1 -0
- package/lib/foundation/schema/parseSexType.js.map +1 -0
- package/lib/foundation/schema/types.js +4 -2
- package/lib/foundation/schema/types.js.map +1 -0
- package/lib/fs/FileSystemError.js +23 -0
- package/lib/fs/FileSystemError.js.map +1 -0
- package/lib/fs/IFileSystem.d.js +3 -0
- package/lib/fs/IFileSystem.d.js.map +1 -0
- package/lib/fs/MemoryFileSystem.test.js +188 -0
- package/lib/fs/createBrowserFileSystem.js +250 -0
- package/lib/fs/createBrowserFileSystem.js.map +1 -0
- package/lib/fs/createMemoryFileSystem.js +517 -0
- package/lib/fs/createMemoryFileSystem.js.map +1 -0
- package/lib/fs/createSandboxFileSystem.js +108 -0
- package/lib/fs/createSandboxFileSystem.js.map +1 -0
- package/lib/fs/createWebDavFileSystem.js +154 -0
- package/lib/fs/createWebDavFileSystem.js.map +1 -0
- package/lib/fs/createWebFileSystem.js +225 -0
- package/lib/fs/createWebFileSystem.js.map +1 -0
- package/lib/fs/findMimeType.js +17 -0
- package/lib/fs/findMimeType.js.map +1 -0
- package/lib/fs/index.js +8 -0
- package/lib/fs/index.js.map +1 -0
- package/lib/fs/minio/createMinioFileSystem.js +974 -0
- package/lib/fs/minio/createMinioFileSystem.js.map +1 -0
- package/lib/fs/minio/index.js +2 -0
- package/lib/fs/minio/index.js.map +1 -0
- package/lib/fs/orpc/FileSystemContract.js +93 -0
- package/lib/fs/orpc/FileSystemContract.js.map +1 -0
- package/lib/fs/orpc/createContractClientFileSystem.js +93 -0
- package/lib/fs/orpc/createContractClientFileSystem.js.map +1 -0
- package/lib/fs/orpc/index.js +3 -0
- package/lib/fs/orpc/index.js.map +1 -0
- package/lib/fs/orpc/server/createFileSystemContractImpl.js +63 -0
- package/lib/fs/orpc/server/createFileSystemContractImpl.js.map +1 -0
- package/lib/fs/orpc/server/index.js +2 -0
- package/lib/fs/orpc/server/index.js.map +1 -0
- package/lib/fs/s3/createS3MiniFileSystem.js +753 -0
- package/lib/fs/s3/createS3MiniFileSystem.js.map +1 -0
- package/lib/fs/s3/index.js +2 -0
- package/lib/fs/s3/index.js.map +1 -0
- package/lib/fs/s3/s3mini.test.js +584 -0
- package/lib/fs/scandir.js +59 -0
- package/lib/fs/scandir.js.map +1 -0
- package/lib/fs/server/createDatabaseFileSystem.js +750 -0
- package/lib/fs/server/createDatabaseFileSystem.js.map +1 -0
- package/lib/fs/server/createNodeFileSystem.js +426 -0
- package/lib/fs/server/createNodeFileSystem.js.map +1 -0
- package/lib/fs/server/dbfs.test.js +221 -0
- package/lib/fs/server/index.js +2 -0
- package/lib/fs/server/index.js.map +1 -0
- package/lib/fs/server/loadTestDatabase.js +127 -0
- package/lib/fs/server/loadTestDatabase.js.map +1 -0
- package/lib/fs/tests/runFileSystemTest.js +319 -0
- package/lib/fs/tests/runFileSystemTest.js.map +1 -0
- package/lib/fs/types.js +27 -0
- package/lib/fs/types.js.map +1 -0
- package/lib/fs/utils/getFileUrl.js +35 -0
- package/lib/fs/utils/getFileUrl.js.map +1 -0
- package/lib/fs/utils.js +22 -0
- package/lib/fs/utils.js.map +1 -0
- package/lib/fs/webdav/index.js +2 -0
- package/lib/fs/webdav/index.js.map +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/lib/jsonschema/JsonSchema.js +146 -172
- package/lib/jsonschema/JsonSchema.js.map +1 -0
- package/lib/jsonschema/forEachJsonSchema.js +44 -0
- package/lib/jsonschema/forEachJsonSchema.js.map +1 -0
- package/lib/jsonschema/index.js +2 -0
- package/lib/jsonschema/index.js.map +1 -0
- package/lib/jsonschema/types.d.js +2 -0
- package/lib/jsonschema/types.d.js.map +1 -0
- package/lib/meta/defineFileType.js +20 -103
- package/lib/meta/defineFileType.js.map +1 -0
- package/lib/meta/defineInit.js +31 -250
- package/lib/meta/defineInit.js.map +1 -0
- package/lib/meta/defineMetadata.js +24 -140
- package/lib/meta/defineMetadata.js.map +1 -0
- package/lib/meta/index.js +1 -0
- package/lib/meta/index.js.map +1 -0
- package/lib/orpc/createOpenApiContractClient.js +27 -0
- package/lib/orpc/createOpenApiContractClient.js.map +1 -0
- package/lib/orpc/createRpcContractClient.js +34 -0
- package/lib/orpc/createRpcContractClient.js.map +1 -0
- package/lib/orpc/index.js +3 -0
- package/lib/orpc/index.js.map +1 -0
- package/lib/orpc/resolveLinkPlugins.js +28 -0
- package/lib/orpc/resolveLinkPlugins.js.map +1 -0
- package/lib/password/PHC.js +63 -87
- package/lib/password/PHC.js.map +1 -0
- package/lib/password/PHC.test.js +11 -3
- package/lib/password/Password.js +29 -294
- package/lib/password/Password.js.map +1 -0
- package/lib/password/Password.test.js +35 -22
- package/lib/password/createArgon2PasswordAlgorithm.js +35 -191
- package/lib/password/createArgon2PasswordAlgorithm.js.map +1 -0
- package/lib/password/createBase64PasswordAlgorithm.js +8 -141
- package/lib/password/createBase64PasswordAlgorithm.js.map +1 -0
- package/lib/password/createBcryptPasswordAlgorithm.js +13 -168
- package/lib/password/createBcryptPasswordAlgorithm.js.map +1 -0
- package/lib/password/createPBKDF2PasswordAlgorithm.js +46 -228
- package/lib/password/createPBKDF2PasswordAlgorithm.js.map +1 -0
- package/lib/password/createScryptPasswordAlgorithm.js +55 -211
- package/lib/password/createScryptPasswordAlgorithm.js.map +1 -0
- package/lib/password/index.js +1 -0
- package/lib/password/index.js.map +1 -0
- package/lib/password/server/index.js +1 -0
- package/lib/password/server/index.js.map +1 -0
- package/lib/resource/Identifiable.js +2 -0
- package/lib/resource/Identifiable.js.map +1 -0
- package/lib/resource/ListQuery.js +21 -93
- package/lib/resource/ListQuery.js.map +1 -0
- package/lib/resource/getTitleOfResource.js +3 -5
- package/lib/resource/getTitleOfResource.js.map +1 -0
- package/lib/resource/index.js +1 -0
- package/lib/resource/index.js.map +1 -0
- package/lib/resource/schema/AnyResourceSchema.js +2 -1
- package/lib/resource/schema/AnyResourceSchema.js.map +1 -0
- package/lib/resource/schema/BaseResourceSchema.js +2 -1
- package/lib/resource/schema/BaseResourceSchema.js.map +1 -0
- package/lib/resource/schema/ResourceActionType.js +6 -4
- package/lib/resource/schema/ResourceActionType.js.map +1 -0
- package/lib/resource/schema/ResourceStatus.js +5 -3
- package/lib/resource/schema/ResourceStatus.js.map +1 -0
- package/lib/resource/schema/ResourceType.js +5 -3
- package/lib/resource/schema/ResourceType.js.map +1 -0
- package/lib/resource/schema/index.js +1 -0
- package/lib/resource/schema/index.js.map +1 -0
- package/lib/resource/schema/types.js +16 -20
- package/lib/resource/schema/types.js.map +1 -0
- package/lib/s3/formatS3Url.js +65 -0
- package/lib/s3/formatS3Url.js.map +1 -0
- package/lib/s3/formatS3Url.test.js +262 -0
- package/lib/s3/index.js +3 -0
- package/lib/s3/index.js.map +1 -0
- package/lib/s3/parseS3Url.js +65 -0
- package/lib/s3/parseS3Url.js.map +1 -0
- package/lib/s3/parseS3Url.test.js +270 -0
- package/lib/schema/SchemaRegistry.js +38 -38
- package/lib/schema/SchemaRegistry.js.map +1 -0
- package/lib/schema/TypeSchema.d.js +2 -0
- package/lib/schema/TypeSchema.d.js.map +1 -0
- package/lib/schema/createSchemaData.js +26 -125
- package/lib/schema/createSchemaData.js.map +1 -0
- package/lib/schema/findJsonSchemaByPath.js +13 -36
- package/lib/schema/findJsonSchemaByPath.js.map +1 -0
- package/lib/schema/formatZodError.js +138 -0
- package/lib/schema/formatZodError.js.map +1 -0
- package/lib/schema/formatZodError.test.js +196 -0
- package/lib/schema/getSchemaCache.js +5 -5
- package/lib/schema/getSchemaCache.js.map +1 -0
- package/lib/schema/getSchemaOptions.js +8 -11
- package/lib/schema/getSchemaOptions.js.map +1 -0
- package/lib/schema/index.js +2 -1
- package/lib/schema/index.js.map +1 -0
- package/lib/schema/toJsonSchema.js +50 -293
- package/lib/schema/toJsonSchema.js.map +1 -0
- package/lib/schema/validate.js +34 -46
- package/lib/schema/validate.js.map +1 -0
- package/lib/tools/generateSchema.js +39 -197
- package/lib/tools/generateSchema.js.map +1 -0
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js +55 -143
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +1 -0
- package/lib/utils/buildBaseUrl.js +13 -0
- package/lib/utils/buildBaseUrl.js.map +1 -0
- package/lib/utils/buildRedactorFormSchema.js +59 -0
- package/lib/utils/buildRedactorFormSchema.js.map +1 -0
- package/lib/utils/getEstimateProcessTime.js +12 -11
- package/lib/utils/getEstimateProcessTime.js.map +1 -0
- package/lib/utils/index.js +3 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/resolveFeatureOptions.js +12 -0
- package/lib/utils/resolveFeatureOptions.js.map +1 -0
- package/package.json +80 -13
- package/src/ai/qwen3vl/index.ts +1 -0
- package/src/ai/qwen3vl/utils.ts +36 -0
- package/src/ai/vision/DocLayoutElementTypeSchema.ts +30 -0
- package/src/ai/vision/ImageAnnotationSchema.ts +60 -0
- package/src/ai/vision/index.ts +2 -0
- package/src/ai/vision/resolveImageAnnotation.ts +135 -0
- package/src/cn/ChineseResidentIdNo.test.ts +1 -1
- package/src/cn/ChineseResidentIdNo.ts +9 -1
- package/src/cn/DivisionCode.test.ts +1 -1
- package/src/cn/DivisionCode.ts +8 -0
- package/src/cn/Mod11.ts +1 -1
- package/src/cn/UnifiedSocialCreditCode.test.ts +1 -1
- package/src/cn/UnifiedSocialCreditCode.ts +15 -0
- package/src/cn/__snapshots__/ChineseResidentIdNo.test.ts.snap +1 -1
- package/src/cn/formatChineseAmount.ts +61 -0
- package/src/cn/index.ts +7 -1
- package/src/cn/parseChineseNumber.test.ts +159 -0
- package/src/cn/parseChineseNumber.ts +97 -0
- package/src/cn/pinyin/cartesianProduct.test.ts +64 -0
- package/src/cn/pinyin/cartesianProduct.ts +24 -0
- package/src/cn/pinyin/data.json +23573 -0
- package/src/cn/pinyin/loader.ts +12 -0
- package/src/cn/pinyin/preload.ts +3 -0
- package/src/cn/pinyin/toPinyin.test.ts +12 -0
- package/src/cn/pinyin/toPinyinPure.ts +43 -0
- package/src/cn/pinyin/transform.ts +12 -0
- package/src/consola/formatLogObject.test.ts +27 -0
- package/src/consola/formatLogObject.ts +40 -12
- package/src/data/maybeNumber.ts +1 -1
- package/src/data/parseSort.test.ts +0 -1
- package/src/data/types.d.ts +2 -2
- package/src/dayjs/dayjs.ts +18 -18
- package/src/dayjs/formatDuration.ts +2 -2
- package/src/dayjs/index.ts +3 -1
- package/src/dayjs/parseRelativeTime.test.ts +185 -0
- package/src/dayjs/parseRelativeTime.ts +115 -0
- package/src/dayjs/resolveRelativeTime.test.ts +357 -0
- package/src/dayjs/resolveRelativeTime.ts +164 -0
- package/src/drain3/Drain.test.ts +378 -0
- package/src/drain3/Drain.ts +394 -0
- package/src/drain3/LogCluster.ts +46 -0
- package/src/drain3/Node.ts +53 -0
- package/src/drain3/TemplateMiner.ts +246 -0
- package/src/drain3/index.ts +36 -0
- package/src/drain3/persistence/FilePersistence.ts +24 -0
- package/src/drain3/persistence/MemoryPersistence.ts +23 -0
- package/src/drain3/persistence/PersistenceHandler.ts +19 -0
- package/src/drain3/types.ts +75 -0
- package/src/emittery/emitter.ts +9 -0
- package/src/emittery/index.ts +1 -0
- package/src/fs/FileSystemError.ts +26 -0
- package/src/fs/IFileSystem.d.ts +101 -0
- package/src/fs/MemoryFileSystem.test.ts +37 -0
- package/src/fs/createBrowserFileSystem.ts +293 -0
- package/src/fs/createMemoryFileSystem.ts +600 -0
- package/src/fs/createSandboxFileSystem.ts +136 -0
- package/src/fs/createWebDavFileSystem.ts +190 -0
- package/src/fs/createWebFileSystem.ts +242 -0
- package/src/fs/findMimeType.ts +20 -0
- package/src/fs/index.ts +8 -0
- package/src/fs/minio/createMinioFileSystem.ts +1148 -0
- package/src/fs/minio/index.ts +1 -0
- package/src/fs/orpc/FileSystemContract.ts +92 -0
- package/src/fs/orpc/createContractClientFileSystem.ts +115 -0
- package/src/fs/orpc/index.ts +2 -0
- package/src/fs/orpc/server/createFileSystemContractImpl.ts +64 -0
- package/src/fs/orpc/server/index.ts +1 -0
- package/src/fs/s3/createS3MiniFileSystem.ts +871 -0
- package/src/fs/s3/index.ts +1 -0
- package/src/fs/s3/s3fs.test.ts +441 -0
- package/src/fs/s3/s3mini.test.ts +264 -0
- package/src/fs/scandir.ts +75 -0
- package/src/fs/server/createDatabaseFileSystem.ts +668 -0
- package/src/fs/server/createNodeFileSystem.ts +518 -0
- package/src/fs/server/dbfs.test.ts +48 -0
- package/src/fs/server/index.ts +1 -0
- package/src/fs/server/loadTestDatabase.ts +131 -0
- package/src/fs/tests/runFileSystemTest.ts +289 -0
- package/src/fs/types.ts +29 -0
- package/src/fs/utils/getFileUrl.ts +44 -0
- package/src/fs/utils.ts +23 -0
- package/src/fs/webdav/index.ts +1 -0
- package/src/jsonschema/JsonSchema.ts +118 -110
- package/src/jsonschema/forEachJsonSchema.ts +50 -0
- package/src/jsonschema/index.ts +1 -0
- package/src/jsonschema/types.d.ts +1 -1
- package/src/meta/defineMetadata.ts +1 -1
- package/src/orpc/createOpenApiContractClient.ts +52 -0
- package/src/orpc/createRpcContractClient.ts +50 -0
- package/src/orpc/index.ts +2 -0
- package/src/orpc/resolveLinkPlugins.ts +29 -0
- package/src/password/PHC.ts +6 -6
- package/src/password/Password.test.ts +1 -1
- package/src/password/createArgon2PasswordAlgorithm.ts +1 -1
- package/src/password/createBase64PasswordAlgorithm.ts +2 -2
- package/src/password/createBcryptPasswordAlgorithm.ts +4 -2
- package/src/password/createPBKDF2PasswordAlgorithm.ts +4 -4
- package/src/password/createScryptPasswordAlgorithm.ts +4 -4
- package/src/resource/ListQuery.ts +4 -1
- package/src/resource/index.ts +2 -2
- package/src/resource/schema/AnyResourceSchema.ts +16 -2
- package/src/s3/formatS3Url.test.ts +254 -0
- package/src/s3/formatS3Url.ts +84 -0
- package/src/s3/index.ts +2 -0
- package/src/s3/parseS3Url.test.ts +258 -0
- package/src/s3/parseS3Url.ts +88 -0
- package/src/schema/SchemaRegistry.ts +35 -33
- package/src/schema/TypeSchema.d.ts +6 -6
- package/src/schema/createSchemaData.ts +4 -4
- package/src/schema/findJsonSchemaByPath.ts +4 -4
- package/src/schema/formatZodError.test.ts +197 -0
- package/src/schema/formatZodError.ts +139 -0
- package/src/schema/getSchemaOptions.ts +2 -2
- package/src/schema/index.ts +1 -1
- package/src/schema/toJsonSchema.ts +6 -6
- package/src/schema/validate.ts +1 -1
- package/src/utils/buildBaseUrl.ts +12 -0
- package/src/utils/buildRedactorFormSchema.ts +85 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/resolveFeatureOptions.ts +14 -0
- package/src/cn/ChineseResidentIdNo.mod.ts +0 -7
- package/src/cn/DivisionCode.mod.ts +0 -7
- package/src/cn/UnifiedSocialCreditCode.mod.ts +0 -7
- package/src/cn/mod.ts +0 -3
- package/src/schema/SchemaRegistry.mod.ts +0 -1
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { join, normalize, relative, sep } from 'pathe';
|
|
2
|
+
import type { IFileStat, IServerFileSystem, ReadFileOptions } from './IFileSystem';
|
|
3
|
+
import { getPath } from './utils';
|
|
4
|
+
|
|
5
|
+
class SandboxSecurityError extends Error {
|
|
6
|
+
constructor(message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'SandboxSecurityError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function createSandboxFileSystem(
|
|
13
|
+
fs: IServerFileSystem,
|
|
14
|
+
basePath: string,
|
|
15
|
+
): IServerFileSystem & {
|
|
16
|
+
fs: IServerFileSystem;
|
|
17
|
+
basePath: string;
|
|
18
|
+
} {
|
|
19
|
+
return new SandboxFS(fs, basePath);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class SandboxFS implements IServerFileSystem {
|
|
23
|
+
fs: IServerFileSystem;
|
|
24
|
+
basePath: string;
|
|
25
|
+
|
|
26
|
+
constructor(fs: IServerFileSystem, basePath: string) {
|
|
27
|
+
this.fs = fs;
|
|
28
|
+
this.basePath = normalize(basePath);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private _resolvePath(userPath: string): string {
|
|
32
|
+
const fullPath = join(this.basePath, userPath);
|
|
33
|
+
|
|
34
|
+
const normalizedFullPath = normalize(fullPath);
|
|
35
|
+
|
|
36
|
+
const rel = relative(this.basePath, normalizedFullPath);
|
|
37
|
+
if (rel.startsWith('..') || rel === '..') {
|
|
38
|
+
throw new SandboxSecurityError(`Path traversal attempt detected: ${userPath}`);
|
|
39
|
+
}
|
|
40
|
+
return normalizedFullPath;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private _stripPath(fullPath: string): string {
|
|
44
|
+
const relPath = relative(this.basePath, fullPath);
|
|
45
|
+
// 保证返回的是一个以 '/' 开头的绝对路径(在沙箱内)
|
|
46
|
+
return sep + relPath.split(sep).join('/');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private _processStat(stat: IFileStat): IFileStat {
|
|
50
|
+
stat.path = this._stripPath(stat.path);
|
|
51
|
+
stat.directory = this._stripPath(stat.directory);
|
|
52
|
+
return stat;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async stat(path: string, options?: any): Promise<IFileStat> {
|
|
56
|
+
const fullPath = this._resolvePath(path);
|
|
57
|
+
const result = await this.fs.stat(fullPath, options);
|
|
58
|
+
return this._processStat(result);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async readdir(dir: string, options?: any): Promise<IFileStat[]> {
|
|
62
|
+
const fullPath = this._resolvePath(dir);
|
|
63
|
+
const results = await this.fs.readdir(fullPath, options);
|
|
64
|
+
return results.map((stat) => this._processStat(stat));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async exists(path: string): Promise<boolean> {
|
|
68
|
+
try {
|
|
69
|
+
const fullPath = this._resolvePath(path);
|
|
70
|
+
return await this.fs.exists(fullPath);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (e instanceof SandboxSecurityError) {
|
|
73
|
+
return false; // 越界访问视为不存在
|
|
74
|
+
}
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
mkdir(path: string, options?: any): Promise<void> {
|
|
80
|
+
const fullPath = this._resolvePath(path);
|
|
81
|
+
return this.fs.mkdir(fullPath, options);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
readFile(path: string, options?: ReadFileOptions & { encoding: 'text' }): Promise<string>;
|
|
85
|
+
readFile(path: string, options?: ReadFileOptions): Promise<Uint8Array>;
|
|
86
|
+
readFile(path: string, options?: ReadFileOptions): Promise<string | Uint8Array> {
|
|
87
|
+
const fullPath = this._resolvePath(path);
|
|
88
|
+
return this.fs.readFile(fullPath, options);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
writeFile(path: string, data: any, options?: any): Promise<void> {
|
|
92
|
+
const fullPath = this._resolvePath(path);
|
|
93
|
+
return this.fs.writeFile(fullPath, data, options);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
rm(path: string, options?: any): Promise<void> {
|
|
97
|
+
const fullPath = this._resolvePath(path);
|
|
98
|
+
return this.fs.rm(fullPath, options);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
rename(oldPath: string, newPath: string, options?: any): Promise<void> {
|
|
102
|
+
const fullOldPath = this._resolvePath(oldPath);
|
|
103
|
+
const fullNewPath = this._resolvePath(newPath);
|
|
104
|
+
return this.fs.rename(fullOldPath, fullNewPath, options);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
copy(src: string, dest: string, options?: any): Promise<void> {
|
|
108
|
+
const fullSrc = this._resolvePath(src);
|
|
109
|
+
const fullDest = this._resolvePath(dest);
|
|
110
|
+
return this.fs.copy(fullSrc, fullDest, options);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
createReadStream(path: string, options?: any): any {
|
|
114
|
+
const fullPath = this._resolvePath(path);
|
|
115
|
+
if (!this.fs.createReadStream) {
|
|
116
|
+
throw new Error('Underlying filesystem does not support createReadStream');
|
|
117
|
+
}
|
|
118
|
+
return this.fs.createReadStream(fullPath, options);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
createWriteStream(path: string, options?: any): any {
|
|
122
|
+
const fullPath = this._resolvePath(path);
|
|
123
|
+
if (!this.fs.createWriteStream) {
|
|
124
|
+
throw new Error('Underlying filesystem does not support createWriteStream');
|
|
125
|
+
}
|
|
126
|
+
return this.fs.createWriteStream(fullPath, options);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
getUrl(file: IFileStat, options?: any): string | undefined {
|
|
130
|
+
let path = this._resolvePath(getPath(file.path));
|
|
131
|
+
if (this.fs.getUrl) {
|
|
132
|
+
return this.fs.getUrl(path, options);
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import type { Readable, Writable } from 'node:stream';
|
|
2
|
+
import { maybeFunction, type MaybeFunction } from '@wener/utils';
|
|
3
|
+
import type { FileStat, GetDirectoryContentsOptions, ResponseDataDetailed, WebDAVClient } from 'webdav';
|
|
4
|
+
import type {
|
|
5
|
+
IFileStat,
|
|
6
|
+
IFileSystem,
|
|
7
|
+
MkdirOptions,
|
|
8
|
+
ReaddirOptions,
|
|
9
|
+
ReadFileOptions,
|
|
10
|
+
RmOptions,
|
|
11
|
+
StatOptions,
|
|
12
|
+
WritableData,
|
|
13
|
+
WriteFileOptions,
|
|
14
|
+
} from './IFileSystem';
|
|
15
|
+
|
|
16
|
+
export function createWebDavFileSystem({ client }: { client: MaybeFunction<WebDAVClient> }): IFileSystem {
|
|
17
|
+
let fs = new WebdavFS({ client });
|
|
18
|
+
return fs;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class WebdavFS implements IFileSystem {
|
|
22
|
+
_client: MaybeFunction<WebDAVClient>;
|
|
23
|
+
|
|
24
|
+
constructor({
|
|
25
|
+
client = () => {
|
|
26
|
+
throw new Error('WebdavFS client not initialized');
|
|
27
|
+
},
|
|
28
|
+
}: {
|
|
29
|
+
client?: MaybeFunction<WebDAVClient>;
|
|
30
|
+
}) {
|
|
31
|
+
this._client = client;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
set client(client: MaybeFunction<WebDAVClient>) {
|
|
35
|
+
this._client = client;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get client(): WebDAVClient {
|
|
39
|
+
return maybeFunction(this._client);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private toEntry(input: FileStat): IFileStat {
|
|
43
|
+
const { filename: path, basename, lastmod, type: kind, etag, size, mime } = input;
|
|
44
|
+
let meta: Record<string, any> = {};
|
|
45
|
+
if (etag) {
|
|
46
|
+
meta.etag = etag;
|
|
47
|
+
}
|
|
48
|
+
if (mime) {
|
|
49
|
+
meta.mime = mime;
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
directory: path.substring(0, path.lastIndexOf('/')) || '/',
|
|
53
|
+
path,
|
|
54
|
+
name: basename,
|
|
55
|
+
mtime: +new Date(lastmod),
|
|
56
|
+
kind,
|
|
57
|
+
meta,
|
|
58
|
+
size,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private getData<T>(input: ResponseDataDetailed<T> | T): T {
|
|
63
|
+
if (
|
|
64
|
+
input &&
|
|
65
|
+
typeof input === 'object' &&
|
|
66
|
+
'data' in input &&
|
|
67
|
+
// 'headers' in input &&
|
|
68
|
+
'status' in input &&
|
|
69
|
+
typeof input.status === 'number'
|
|
70
|
+
) {
|
|
71
|
+
return input.data;
|
|
72
|
+
}
|
|
73
|
+
return input as T;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async readdir(
|
|
77
|
+
path: string,
|
|
78
|
+
{ glob, recursive, depth, kind, hidden, signal }: ReaddirOptions = {},
|
|
79
|
+
): Promise<IFileStat[]> {
|
|
80
|
+
// webdav depth 只支持 0,1
|
|
81
|
+
let o: GetDirectoryContentsOptions = {};
|
|
82
|
+
if (recursive) {
|
|
83
|
+
o.deep = true;
|
|
84
|
+
}
|
|
85
|
+
let res = await this.client.getDirectoryContents(path, {
|
|
86
|
+
deep: recursive,
|
|
87
|
+
signal,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
let out: FileStat[] = this.getData(res);
|
|
91
|
+
|
|
92
|
+
if (!recursive && typeof depth === 'number' && depth >= 2) {
|
|
93
|
+
let l = depth;
|
|
94
|
+
let cur = out;
|
|
95
|
+
while (l-- > 1) {
|
|
96
|
+
let sub = (
|
|
97
|
+
await Promise.all(
|
|
98
|
+
cur.map(async (v) => {
|
|
99
|
+
if (v.type === 'directory') {
|
|
100
|
+
return this.getData(await this.client.getDirectoryContents(v.filename, { signal }));
|
|
101
|
+
}
|
|
102
|
+
return [];
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
105
|
+
).flat();
|
|
106
|
+
out = out.concat(...sub);
|
|
107
|
+
cur = sub;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (glob) {
|
|
112
|
+
const { default: def, matcher = def.matcher } = await import('micromatch');
|
|
113
|
+
const match = matcher(glob);
|
|
114
|
+
out = out.filter((v) => match(v.filename));
|
|
115
|
+
}
|
|
116
|
+
if (kind) {
|
|
117
|
+
out = out.filter((v) => v.type === kind);
|
|
118
|
+
}
|
|
119
|
+
if (!hidden) {
|
|
120
|
+
out = out.filter((v) => !v.basename.startsWith('.'));
|
|
121
|
+
}
|
|
122
|
+
return out.map((stat) => this.toEntry(stat));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async stat(path: string, { signal }: StatOptions = {}): Promise<IFileStat> {
|
|
126
|
+
const res = await this.client.stat(path, { details: true, signal });
|
|
127
|
+
return this.toEntry(this.getData(res));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async mkdir(path: string, { recursive, signal }: MkdirOptions = {}): Promise<void> {
|
|
131
|
+
await this.client.createDirectory(path, { recursive, signal });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async readFile(path: string, options: ReadFileOptions = {}): Promise<any> {
|
|
135
|
+
const format = options.encoding === 'text' ? 'text' : 'binary';
|
|
136
|
+
const res = await this.client.getFileContents(path, { format, ...options });
|
|
137
|
+
return this.getData(res);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async writeFile(path: string, data: WritableData, options: WriteFileOptions = {}): Promise<void> {
|
|
141
|
+
// Convert web ReadableStream to something WebDAV client can handle
|
|
142
|
+
let webdavData: string | Buffer | ArrayBuffer | Readable = data as string | Buffer | ArrayBuffer | Readable;
|
|
143
|
+
if (data instanceof ReadableStream) {
|
|
144
|
+
// Convert web ReadableStream to Buffer
|
|
145
|
+
const reader = data.getReader();
|
|
146
|
+
const chunks: Uint8Array[] = [];
|
|
147
|
+
while (true) {
|
|
148
|
+
const { done, value } = await reader.read();
|
|
149
|
+
if (done) break;
|
|
150
|
+
if (value) chunks.push(value);
|
|
151
|
+
}
|
|
152
|
+
webdavData = Buffer.concat(chunks);
|
|
153
|
+
} else if (ArrayBuffer.isView(data) && !(data instanceof Buffer)) {
|
|
154
|
+
// Convert ArrayBufferView to Buffer
|
|
155
|
+
webdavData = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
156
|
+
}
|
|
157
|
+
await this.client.putFileContents(path, webdavData, options);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async rm(path: string, { signal: _signal, force, recursive: _recursive }: RmOptions = {}): Promise<void> {
|
|
161
|
+
try {
|
|
162
|
+
await this.client.deleteFile(path);
|
|
163
|
+
} catch (e: any) {
|
|
164
|
+
if (force && e.status === 404) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
throw e;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async rename(oldPath: string, newPath: string, options = {}): Promise<void> {
|
|
172
|
+
await this.client.moveFile(oldPath, newPath, options);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async exists(path: string): Promise<boolean> {
|
|
176
|
+
return await this.client.exists(path);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async copy(src: string, dest: string, options = {}): Promise<void> {
|
|
180
|
+
await this.client.copyFile(src, dest, options);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
createReadStream(path: string, options = {}): Readable {
|
|
184
|
+
return this.client.createReadStream(path, options);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
createWriteStream(path: string, options = {}): Writable {
|
|
188
|
+
return this.client.createWriteStream(path, options);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { basename, dirname, join, normalize } from 'pathe';
|
|
2
|
+
import { FileSystemError } from './FileSystemError';
|
|
3
|
+
import type {
|
|
4
|
+
CopyOptions,
|
|
5
|
+
IFileStat,
|
|
6
|
+
IFileSystem,
|
|
7
|
+
MkdirOptions,
|
|
8
|
+
ReadFileOptions,
|
|
9
|
+
ReaddirOptions,
|
|
10
|
+
RenameOptions,
|
|
11
|
+
RmOptions,
|
|
12
|
+
StatOptions,
|
|
13
|
+
WriteFileOptions,
|
|
14
|
+
} from './IFileSystem';
|
|
15
|
+
|
|
16
|
+
export function createWebFileSystem(options: { root: FileSystemDirectoryHandle }): IFileSystem {
|
|
17
|
+
return new WebFileSystem(options);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class WebFileSystem implements IFileSystem {
|
|
21
|
+
private readonly root: FileSystemDirectoryHandle;
|
|
22
|
+
|
|
23
|
+
constructor({ root }: { root: FileSystemDirectoryHandle }) {
|
|
24
|
+
this.root = root;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private async _getHandle(path: string): Promise<FileSystemHandle> {
|
|
28
|
+
const parts = normalize(path).split('/').filter(Boolean);
|
|
29
|
+
if (parts.length === 0) return this.root;
|
|
30
|
+
|
|
31
|
+
let current: FileSystemDirectoryHandle = this.root;
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < parts.length; i++) {
|
|
34
|
+
const part = parts[i];
|
|
35
|
+
const isLast = i === parts.length - 1;
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
// Try to get as directory first
|
|
39
|
+
current = await current.getDirectoryHandle(part);
|
|
40
|
+
} catch (e: any) {
|
|
41
|
+
if (e.name === 'TypeMismatchError' || e.name === 'NotFoundError') {
|
|
42
|
+
if (isLast) {
|
|
43
|
+
// Might be a file
|
|
44
|
+
try {
|
|
45
|
+
return await current.getFileHandle(part);
|
|
46
|
+
} catch (_e2) {
|
|
47
|
+
throw new FileSystemError(`File not found: ${path}`, 'ENOENT');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (e.name === 'NotFoundError') {
|
|
52
|
+
throw new FileSystemError(`Path not found: ${path}`, 'ENOENT');
|
|
53
|
+
}
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return current;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private async _getParent(path: string): Promise<{ parent: FileSystemDirectoryHandle; name: string }> {
|
|
61
|
+
const normalized = normalize(path);
|
|
62
|
+
const parentPath = dirname(normalized);
|
|
63
|
+
const name = basename(normalized);
|
|
64
|
+
|
|
65
|
+
if (!name) throw new FileSystemError('Invalid path', 'EINVAL');
|
|
66
|
+
|
|
67
|
+
const parentHandle = await this._getHandle(parentPath);
|
|
68
|
+
if (parentHandle.kind !== 'directory') {
|
|
69
|
+
throw new FileSystemError(`Parent is not a directory: ${parentPath}`, 'ENOTDIR');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { parent: parentHandle as FileSystemDirectoryHandle, name };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async stat(path: string, options?: StatOptions): Promise<IFileStat> {
|
|
76
|
+
if (options?.signal?.aborted) throw new FileSystemError('Operation aborted', 'ABORT_ERR');
|
|
77
|
+
|
|
78
|
+
const handle = await this._getHandle(path);
|
|
79
|
+
return this._handleToStat(handle, path);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private async _handleToStat(handle: FileSystemHandle, path: string): Promise<IFileStat> {
|
|
83
|
+
let size = 0;
|
|
84
|
+
let mtime = 0;
|
|
85
|
+
|
|
86
|
+
if (handle.kind === 'file') {
|
|
87
|
+
const file = await (handle as FileSystemFileHandle).getFile();
|
|
88
|
+
size = file.size;
|
|
89
|
+
mtime = file.lastModified;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
path,
|
|
94
|
+
directory: dirname(path),
|
|
95
|
+
name: handle.name,
|
|
96
|
+
kind: handle.kind,
|
|
97
|
+
size,
|
|
98
|
+
mtime,
|
|
99
|
+
meta: {},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async exists(path: string): Promise<boolean> {
|
|
104
|
+
try {
|
|
105
|
+
await this._getHandle(path);
|
|
106
|
+
return true;
|
|
107
|
+
} catch {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async readdir(path: string, options?: ReaddirOptions): Promise<IFileStat[]> {
|
|
113
|
+
if (options?.signal?.aborted) throw new FileSystemError('Operation aborted', 'ABORT_ERR');
|
|
114
|
+
|
|
115
|
+
const handle = await this._getHandle(path);
|
|
116
|
+
if (handle.kind !== 'directory') {
|
|
117
|
+
throw new FileSystemError(`Not a directory: ${path}`, 'ENOTDIR');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const entries: IFileStat[] = [];
|
|
121
|
+
// @ts-expect-error - FileSystemDirectoryHandle is async iterable in modern browsers
|
|
122
|
+
for await (const entry of (handle as FileSystemDirectoryHandle).values()) {
|
|
123
|
+
entries.push(await this._handleToStat(entry, join(path, entry.name)));
|
|
124
|
+
}
|
|
125
|
+
return entries;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async mkdir(path: string, options?: MkdirOptions): Promise<void> {
|
|
129
|
+
if (options?.signal?.aborted) throw new FileSystemError('Operation aborted', 'ABORT_ERR');
|
|
130
|
+
|
|
131
|
+
const normalized = normalize(path);
|
|
132
|
+
const parts = normalized.split('/').filter(Boolean);
|
|
133
|
+
let current = this.root;
|
|
134
|
+
|
|
135
|
+
if (options?.recursive) {
|
|
136
|
+
for (const part of parts) {
|
|
137
|
+
current = await current.getDirectoryHandle(part, { create: true });
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
const parentPath = dirname(normalized);
|
|
141
|
+
const name = basename(normalized);
|
|
142
|
+
try {
|
|
143
|
+
const parent = await this._getHandle(parentPath);
|
|
144
|
+
if (parent.kind !== 'directory') throw new FileSystemError(`Parent not a directory: ${parentPath}`, 'ENOTDIR');
|
|
145
|
+
await (parent as FileSystemDirectoryHandle).getDirectoryHandle(name, { create: true });
|
|
146
|
+
} catch (e: any) {
|
|
147
|
+
if (e.code === 'ENOENT' || e.name === 'NotFoundError') {
|
|
148
|
+
throw new FileSystemError(`Parent does not exist: ${parentPath}`, 'ENOENT');
|
|
149
|
+
}
|
|
150
|
+
throw e;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
readFile(path: string, options?: ReadFileOptions & { encoding: 'text' }): Promise<string>;
|
|
156
|
+
readFile(path: string, options?: ReadFileOptions): Promise<Uint8Array>;
|
|
157
|
+
async readFile(path: string, options?: ReadFileOptions): Promise<string | Uint8Array> {
|
|
158
|
+
if (options?.signal?.aborted) throw new FileSystemError('Operation aborted', 'ABORT_ERR');
|
|
159
|
+
|
|
160
|
+
const handle = await this._getHandle(path);
|
|
161
|
+
if (handle.kind !== 'file') throw new FileSystemError(`Is a directory: ${path}`, 'EISDIR');
|
|
162
|
+
|
|
163
|
+
const file = await (handle as FileSystemFileHandle).getFile();
|
|
164
|
+
|
|
165
|
+
if (options?.encoding === 'text') {
|
|
166
|
+
return await file.text();
|
|
167
|
+
} else {
|
|
168
|
+
const buffer = await file.arrayBuffer();
|
|
169
|
+
return new Uint8Array(buffer);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async writeFile(
|
|
174
|
+
path: string,
|
|
175
|
+
data: string | ArrayBuffer | ArrayBufferView<ArrayBufferLike> | ReadableStream,
|
|
176
|
+
options?: WriteFileOptions,
|
|
177
|
+
): Promise<void> {
|
|
178
|
+
if (options?.signal?.aborted) throw new FileSystemError('Operation aborted', 'ABORT_ERR');
|
|
179
|
+
|
|
180
|
+
if (options?.overwrite === false) {
|
|
181
|
+
if (await this.exists(path)) {
|
|
182
|
+
throw new FileSystemError(`File already exists: ${path}`, 'EEXIST');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const { parent, name } = await this._getParent(path);
|
|
187
|
+
const handle = await parent.getFileHandle(name, { create: true });
|
|
188
|
+
|
|
189
|
+
const writable = await (handle as any).createWritable();
|
|
190
|
+
await writable.write(data);
|
|
191
|
+
await writable.close();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async rm(path: string, options?: RmOptions): Promise<void> {
|
|
195
|
+
if (options?.signal?.aborted) throw new FileSystemError('Operation aborted', 'ABORT_ERR');
|
|
196
|
+
|
|
197
|
+
const { parent, name } = await this._getParent(path);
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
await (parent as any).removeEntry(name, { recursive: options?.recursive });
|
|
201
|
+
} catch (e: any) {
|
|
202
|
+
if (e.name === 'NotFoundError') {
|
|
203
|
+
if (!options?.force) throw new FileSystemError(`File not found: ${path}`, 'ENOENT');
|
|
204
|
+
} else if (e.name === 'InvalidModificationError') {
|
|
205
|
+
throw new FileSystemError(`Directory not empty: ${path}`, 'ENOTEMPTY');
|
|
206
|
+
} else {
|
|
207
|
+
throw e;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async rename(oldPath: string, newPath: string, options?: RenameOptions): Promise<void> {
|
|
213
|
+
if (options?.signal?.aborted) throw new FileSystemError('Operation aborted', 'ABORT_ERR');
|
|
214
|
+
|
|
215
|
+
// File System Access API doesn't support move/rename directly
|
|
216
|
+
// Fallback to copy + delete
|
|
217
|
+
await this.copy(oldPath, newPath, { overwrite: options?.overwrite });
|
|
218
|
+
await this.rm(oldPath, { recursive: true });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async copy(src: string, dest: string, options?: CopyOptions): Promise<void> {
|
|
222
|
+
if (options?.signal?.aborted) throw new FileSystemError('Operation aborted', 'ABORT_ERR');
|
|
223
|
+
|
|
224
|
+
const srcHandle = await this._getHandle(src);
|
|
225
|
+
|
|
226
|
+
if (srcHandle.kind === 'file') {
|
|
227
|
+
const file = await (srcHandle as FileSystemFileHandle).getFile();
|
|
228
|
+
await this.writeFile(dest, await file.arrayBuffer(), { overwrite: options?.overwrite });
|
|
229
|
+
} else {
|
|
230
|
+
// Directory copy
|
|
231
|
+
await this.mkdir(dest);
|
|
232
|
+
const entries = await this.readdir(src);
|
|
233
|
+
for (const entry of entries) {
|
|
234
|
+
await this.copy(entry.path, join(dest, entry.name), options);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
getUrl(_file: IFileStat | string): string | undefined {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { types } from 'mime-types';
|
|
2
|
+
import pathe from 'pathe';
|
|
3
|
+
|
|
4
|
+
export function findMimeType(path: string | undefined | null) {
|
|
5
|
+
// fix extname error
|
|
6
|
+
// https://github.com/jshttp/mime-types/issues/111
|
|
7
|
+
|
|
8
|
+
if (!path || typeof path !== 'string') {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// get the extension ("ext" or ".ext" or full path)
|
|
13
|
+
const extension = pathe.extname(`x.${path}`).toLowerCase().slice(1);
|
|
14
|
+
|
|
15
|
+
if (!extension) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return types[extension] || false;
|
|
20
|
+
}
|
package/src/fs/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type * from './IFileSystem';
|
|
2
|
+
export { createSandboxFileSystem } from './createSandboxFileSystem';
|
|
3
|
+
export { createMemoryFileSystem } from './createMemoryFileSystem';
|
|
4
|
+
export { createBrowserFileSystem } from './createBrowserFileSystem';
|
|
5
|
+
export { createWebFileSystem } from './createWebFileSystem';
|
|
6
|
+
export { findMimeType } from './findMimeType';
|
|
7
|
+
export { type FileUrlOptions, FileUrlOptionsSchema } from './types';
|
|
8
|
+
export { FileSystemError, FileSystemErrorCode } from './FileSystemError';
|