@h-ai/storage 0.1.0-alpha9 → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +287 -0
- package/README.ja.md +287 -0
- package/README.md +235 -59
- package/dist/browser.d.ts +491 -102
- package/dist/browser.js +3 -2
- package/dist/{chunk-KHZOR6CR.js → chunk-ANAESRWS.js} +39 -12
- package/dist/chunk-ANAESRWS.js.map +1 -0
- package/dist/chunk-P65WYXJG.js +33 -0
- package/dist/chunk-P65WYXJG.js.map +1 -0
- package/dist/chunk-TLZY4IP6.js +360 -0
- package/dist/chunk-TLZY4IP6.js.map +1 -0
- package/dist/client/index.d.ts +57 -48
- package/dist/client/index.js +1 -1
- package/dist/node.d.ts +15 -11
- package/dist/node.js +597 -180
- package/dist/node.js.map +1 -1
- package/dist/schemas.d.ts +103 -0
- package/dist/schemas.js +3 -0
- package/dist/schemas.js.map +1 -0
- package/package.json +22 -16
- package/dist/api/index.d.ts +0 -167
- package/dist/api/index.js +0 -105
- package/dist/api/index.js.map +0 -1
- package/dist/chunk-KHZOR6CR.js.map +0 -1
- package/dist/chunk-U427BQIQ.js +0 -266
- package/dist/chunk-U427BQIQ.js.map +0 -1
package/dist/browser.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { HaiStorageError, LocalConfigSchema, PresignOptionsSchema, PresignUploadOptionsSchema, S3ConfigSchema, StorageConfigSchema, StorageTypeSchema } from './chunk-
|
|
2
|
-
export { downloadAndSave, downloadWithPresignedUrl, formatFileSize, getFileExtension, getMimeType, uploadWithPresignedUrl } from './chunk-
|
|
1
|
+
export { HaiStorageError, LocalConfigSchema, PresignOptionsSchema, PresignUploadOptionsSchema, S3ConfigSchema, StorageConfigSchema, StorageOperationLogConfigSchema, StorageTypeSchema } from './chunk-ANAESRWS.js';
|
|
2
|
+
export { downloadAndSave, downloadWithPresignedUrl, formatFileSize, getFileExtension, getMimeType, uploadWithPresignedUrl } from './chunk-TLZY4IP6.js';
|
|
3
|
+
export { STORAGE_DELETE_MAX_KEYS, STORAGE_KEY_MAX_BYTES, StorageDeleteKeysSchema, StorageKeySchema, StoragePrefixSchema, StoragePresignExpiresInSchema, getStorageKeyIssue } from './chunk-P65WYXJG.js';
|
|
3
4
|
//# sourceMappingURL=browser.js.map
|
|
4
5
|
//# sourceMappingURL=browser.js.map
|
|
@@ -1,27 +1,51 @@
|
|
|
1
|
-
import { storageM } from './chunk-
|
|
1
|
+
import { storageM } from './chunk-TLZY4IP6.js';
|
|
2
|
+
import { StoragePresignExpiresInSchema } from './chunk-P65WYXJG.js';
|
|
2
3
|
import { z } from 'zod';
|
|
3
4
|
import { core } from '@h-ai/core';
|
|
4
5
|
|
|
5
6
|
var StorageTypeSchema = z.enum(["s3", "local"]);
|
|
7
|
+
var StorageOperationLogConfigSchema = z.object({
|
|
8
|
+
read: z.boolean().default(false),
|
|
9
|
+
write: z.boolean().default(false),
|
|
10
|
+
maxLength: z.number().int().min(0).default(1e3),
|
|
11
|
+
level: z.enum(["info", "debug", "trace"]).default("debug")
|
|
12
|
+
});
|
|
13
|
+
var OperationLogConfigFieldSchema = {
|
|
14
|
+
operationLog: StorageOperationLogConfigSchema.optional()
|
|
15
|
+
};
|
|
6
16
|
var S3ConfigSchema = z.object({
|
|
7
17
|
/** 存储类型 */
|
|
8
18
|
type: z.literal("s3"),
|
|
9
19
|
/** 存储桶名称 */
|
|
10
20
|
bucket: z.string().min(1, storageM("storage_config_bucketRequired")),
|
|
21
|
+
/** 初始化时检查并创建缺失的桶;默认关闭,启用后凭据须有建桶权限。 */
|
|
22
|
+
createBucket: z.boolean().default(false),
|
|
11
23
|
/** 区域 */
|
|
12
24
|
region: z.string().min(1, storageM("storage_config_regionRequired")),
|
|
13
25
|
/** Access Key ID */
|
|
14
26
|
accessKeyId: z.string().min(1, storageM("storage_config_accessKeyIdRequired")),
|
|
15
27
|
/** Secret Access Key */
|
|
16
28
|
secretAccessKey: z.string().min(1, storageM("storage_config_secretAccessKeyRequired")),
|
|
17
|
-
/** 自定义端点(用于 MinIO、阿里云 OSS 等) */
|
|
29
|
+
/** 自定义端点(用于 MinIO、RustFS、阿里云 OSS 等) */
|
|
18
30
|
endpoint: z.string().url().optional(),
|
|
31
|
+
/** 浏览器预签名端点,可含代理目录(如 /delivery/files),不含桶名、凭据或查询;代理须去除目录并保留 Host。 */
|
|
32
|
+
publicEndpoint: z.string().refine((value) => {
|
|
33
|
+
return !/[\u0000-\u0020\u007F\\%]/.test(value) && !/(?:^|\/)\.{1,2}(?:\/|$)/.test(value);
|
|
34
|
+
}, storageM("storage_config_publicEndpointInvalid")).url().refine((value) => {
|
|
35
|
+
try {
|
|
36
|
+
const url = new URL(value);
|
|
37
|
+
return ["http:", "https:"].includes(url.protocol) && !url.username && !url.password && /^\/(?:[\w.-]+(?:\/[\w.-]+)*\/?)?$/.test(url.pathname) && !url.search && !url.hash;
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}, storageM("storage_config_publicEndpointInvalid")).optional(),
|
|
19
42
|
/** 强制使用路径风格 URL(某些 S3 兼容服务需要) */
|
|
20
43
|
forcePathStyle: z.boolean().default(false),
|
|
21
44
|
/** 路径前缀(所有操作都会加上此前缀) */
|
|
22
45
|
prefix: z.string().default(""),
|
|
23
46
|
/** 公开访问基础 URL(用于生成公开 URL) */
|
|
24
|
-
publicUrl: z.string().url().optional()
|
|
47
|
+
publicUrl: z.string().url().optional(),
|
|
48
|
+
...OperationLogConfigFieldSchema
|
|
25
49
|
});
|
|
26
50
|
var LocalConfigSchema = z.object({
|
|
27
51
|
/** 存储类型 */
|
|
@@ -31,7 +55,8 @@ var LocalConfigSchema = z.object({
|
|
|
31
55
|
/** 目录创建权限 */
|
|
32
56
|
directoryMode: z.number().default(493),
|
|
33
57
|
/** 文件创建权限 */
|
|
34
|
-
fileMode: z.number().default(420)
|
|
58
|
+
fileMode: z.number().default(420),
|
|
59
|
+
...OperationLogConfigFieldSchema
|
|
35
60
|
});
|
|
36
61
|
var StorageConfigSchema = z.discriminatedUnion("type", [
|
|
37
62
|
S3ConfigSchema,
|
|
@@ -39,7 +64,7 @@ var StorageConfigSchema = z.discriminatedUnion("type", [
|
|
|
39
64
|
]);
|
|
40
65
|
var PresignOptionsSchema = z.object({
|
|
41
66
|
/** 过期时间(秒),范围 1~604800(7 天),默认 3600(1 小时) */
|
|
42
|
-
expiresIn:
|
|
67
|
+
expiresIn: StoragePresignExpiresInSchema.default(3600),
|
|
43
68
|
// 最长 7 天
|
|
44
69
|
/** 响应内容类型(用于下载时设置 Content-Type) */
|
|
45
70
|
responseContentType: z.string().optional(),
|
|
@@ -48,9 +73,7 @@ var PresignOptionsSchema = z.object({
|
|
|
48
73
|
});
|
|
49
74
|
var PresignUploadOptionsSchema = PresignOptionsSchema.extend({
|
|
50
75
|
/** 上传内容类型(必须与实际上传匹配) */
|
|
51
|
-
contentType: z.string().default("application/octet-stream")
|
|
52
|
-
/** 最大文件大小限制(bytes) */
|
|
53
|
-
maxSize: z.number().optional()
|
|
76
|
+
contentType: z.string().default("application/octet-stream")
|
|
54
77
|
});
|
|
55
78
|
var StorageErrorInfo = {
|
|
56
79
|
CONNECTION_FAILED: "001:500",
|
|
@@ -67,10 +90,14 @@ var StorageErrorInfo = {
|
|
|
67
90
|
CONFIG_ERROR: "012:500",
|
|
68
91
|
PRESIGN_FAILED: "013:500",
|
|
69
92
|
UPLOAD_FAILED: "014:500",
|
|
70
|
-
DOWNLOAD_FAILED: "015:500"
|
|
93
|
+
DOWNLOAD_FAILED: "015:500",
|
|
94
|
+
CHECKSUM_FAILED: "016:500",
|
|
95
|
+
INVALID_CHECKSUM: "017:400",
|
|
96
|
+
UNSUPPORTED_OPERATION: "018:400",
|
|
97
|
+
INVALID_LIST_OPTIONS: "019:400"
|
|
71
98
|
};
|
|
72
99
|
var HaiStorageError = core.error.buildHaiErrorsDef("storage", StorageErrorInfo);
|
|
73
100
|
|
|
74
|
-
export { HaiStorageError, LocalConfigSchema, PresignOptionsSchema, PresignUploadOptionsSchema, S3ConfigSchema, StorageConfigSchema, StorageTypeSchema };
|
|
75
|
-
//# sourceMappingURL=chunk-
|
|
76
|
-
//# sourceMappingURL=chunk-
|
|
101
|
+
export { HaiStorageError, LocalConfigSchema, PresignOptionsSchema, PresignUploadOptionsSchema, S3ConfigSchema, StorageConfigSchema, StorageOperationLogConfigSchema, StorageTypeSchema };
|
|
102
|
+
//# sourceMappingURL=chunk-ANAESRWS.js.map
|
|
103
|
+
//# sourceMappingURL=chunk-ANAESRWS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/storage-config.ts","../src/storage-types.ts"],"names":[],"mappings":";;;;;AAyBO,IAAM,oBAAoB,CAAA,CAAE,IAAA,CAAK,CAAC,IAAA,EAAM,OAAO,CAAC;AAwBhD,IAAM,+BAAA,GAAkC,EAAE,MAAA,CAAO;AAAA,EACtD,IAAA,EAAM,CAAA,CAAE,OAAA,EAAQ,CAAE,QAAQ,KAAK,CAAA;AAAA,EAC/B,KAAA,EAAO,CAAA,CAAE,OAAA,EAAQ,CAAE,QAAQ,KAAK,CAAA;AAAA,EAChC,SAAA,EAAW,CAAA,CAAE,MAAA,EAAO,CAAE,GAAA,GAAM,GAAA,CAAI,CAAC,CAAA,CAAE,OAAA,CAAQ,GAAI,CAAA;AAAA,EAC/C,KAAA,EAAO,CAAA,CAAE,IAAA,CAAK,CAAC,MAAA,EAAQ,SAAS,OAAO,CAAC,CAAA,CAAE,OAAA,CAAQ,OAAO;AAC3D,CAAC;AAcD,IAAM,6BAAA,GAAgC;AAAA,EACpC,YAAA,EAAc,gCAAgC,QAAA;AAChD,CAAA;AAeO,IAAM,cAAA,GAAiB,EAAE,MAAA,CAAO;AAAA;AAAA,EAErC,IAAA,EAAM,CAAA,CAAE,OAAA,CAAQ,IAAI,CAAA;AAAA;AAAA,EAGpB,MAAA,EAAQ,EAAE,MAAA,EAAO,CAAE,IAAI,CAAA,EAAG,QAAA,CAAS,+BAA+B,CAAC,CAAA;AAAA;AAAA,EAGnE,YAAA,EAAc,CAAA,CAAE,OAAA,EAAQ,CAAE,QAAQ,KAAK,CAAA;AAAA;AAAA,EAGvC,MAAA,EAAQ,EAAE,MAAA,EAAO,CAAE,IAAI,CAAA,EAAG,QAAA,CAAS,+BAA+B,CAAC,CAAA;AAAA;AAAA,EAGnE,WAAA,EAAa,EAAE,MAAA,EAAO,CAAE,IAAI,CAAA,EAAG,QAAA,CAAS,oCAAoC,CAAC,CAAA;AAAA;AAAA,EAG7E,eAAA,EAAiB,EAAE,MAAA,EAAO,CAAE,IAAI,CAAA,EAAG,QAAA,CAAS,wCAAwC,CAAC,CAAA;AAAA;AAAA,EAGrF,UAAU,CAAA,CAAE,MAAA,EAAO,CAAE,GAAA,GAAM,QAAA,EAAS;AAAA;AAAA,EAGpC,gBAAgB,CAAA,CAAE,MAAA,EAAO,CAAE,MAAA,CAAO,CAAC,KAAA,KAAU;AAG3C,IAAA,OAAO,CAAC,2BAA2B,IAAA,CAAK,KAAK,KAAK,CAAC,yBAAA,CAA0B,KAAK,KAAK,CAAA;AAAA,EACzF,CAAA,EAAG,SAAS,sCAAsC,CAAC,EAAE,GAAA,EAAI,CAAE,MAAA,CAAO,CAAC,KAAA,KAAU;AAC3E,IAAA,IAAI;AACF,MAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,KAAK,CAAA;AACzB,MAAA,OAAO,CAAC,SAAS,QAAQ,CAAA,CAAE,SAAS,GAAA,CAAI,QAAQ,CAAA,IAAK,CAAC,GAAA,CAAI,QAAA,IAAY,CAAC,GAAA,CAAI,QAAA,IACtE,mCAAA,CAAoC,IAAA,CAAK,GAAA,CAAI,QAAQ,KAAK,CAAC,GAAA,CAAI,MAAA,IAAU,CAAC,GAAA,CAAI,IAAA;AAAA,IACrF,CAAA,CAAA,MACM;AACJ,MAAA,OAAO,KAAA;AAAA,IACT;AAAA,EACF,CAAA,EAAG,QAAA,CAAS,sCAAsC,CAAC,EAAE,QAAA,EAAS;AAAA;AAAA,EAG9D,cAAA,EAAgB,CAAA,CAAE,OAAA,EAAQ,CAAE,QAAQ,KAAK,CAAA;AAAA;AAAA,EAGzC,MAAA,EAAQ,CAAA,CAAE,MAAA,EAAO,CAAE,QAAQ,EAAE,CAAA;AAAA;AAAA,EAG7B,WAAW,CAAA,CAAE,MAAA,EAAO,CAAE,GAAA,GAAM,QAAA,EAAS;AAAA,EAErC,GAAG;AACL,CAAC;AA0BM,IAAM,iBAAA,GAAoB,EAAE,MAAA,CAAO;AAAA;AAAA,EAExC,IAAA,EAAM,CAAA,CAAE,OAAA,CAAQ,OAAO,CAAA;AAAA;AAAA,EAGvB,IAAA,EAAM,EAAE,MAAA,EAAO,CAAE,IAAI,CAAA,EAAG,QAAA,CAAS,6BAA6B,CAAC,CAAA;AAAA;AAAA,EAG/D,aAAA,EAAe,CAAA,CAAE,MAAA,EAAO,CAAE,QAAQ,GAAK,CAAA;AAAA;AAAA,EAGvC,QAAA,EAAU,CAAA,CAAE,MAAA,EAAO,CAAE,QAAQ,GAAK,CAAA;AAAA,EAElC,GAAG;AACL,CAAC;AA0BM,IAAM,mBAAA,GAAsB,CAAA,CAAE,kBAAA,CAAmB,MAAA,EAAQ;AAAA,EAC9D,cAAA;AAAA,EACA;AACF,CAAC;AAwCM,IAAM,oBAAA,GAAuB,EAAE,MAAA,CAAO;AAAA;AAAA,EAE3C,SAAA,EAAW,6BAAA,CAA8B,OAAA,CAAQ,IAAI,CAAA;AAAA;AAAA;AAAA,EAGrD,mBAAA,EAAqB,CAAA,CAAE,MAAA,EAAO,CAAE,QAAA,EAAS;AAAA;AAAA,EAGzC,0BAAA,EAA4B,CAAA,CAAE,MAAA,EAAO,CAAE,QAAA;AACzC,CAAC;AA0BM,IAAM,0BAAA,GAA6B,qBAAqB,MAAA,CAAO;AAAA;AAAA,EAEpE,WAAA,EAAa,CAAA,CAAE,MAAA,EAAO,CAAE,QAAQ,0BAA0B;AAC5D,CAAC;ACnQD,IAAM,gBAAA,GAAmB;AAAA,EACvB,iBAAA,EAAmB,SAAA;AAAA,EACnB,gBAAA,EAAkB,SAAA;AAAA,EAClB,SAAA,EAAW,SAAA;AAAA,EACX,cAAA,EAAgB,SAAA;AAAA,EAChB,iBAAA,EAAmB,SAAA;AAAA,EACnB,cAAA,EAAgB,SAAA;AAAA,EAChB,YAAA,EAAc,SAAA;AAAA,EACd,QAAA,EAAU,SAAA;AAAA,EACV,aAAA,EAAe,SAAA;AAAA,EACf,eAAA,EAAiB,SAAA;AAAA,EACjB,gBAAA,EAAkB,SAAA;AAAA,EAClB,YAAA,EAAc,SAAA;AAAA,EACd,cAAA,EAAgB,SAAA;AAAA,EAChB,aAAA,EAAe,SAAA;AAAA,EACf,eAAA,EAAiB,SAAA;AAAA,EACjB,eAAA,EAAiB,SAAA;AAAA,EACjB,gBAAA,EAAkB,SAAA;AAAA,EAClB,qBAAA,EAAuB,SAAA;AAAA,EACvB,oBAAA,EAAsB;AACxB,CAAA;AAaO,IAAM,eAAA,GAAkB,IAAA,CAAK,KAAA,CAAM,iBAAA,CAAkB,WAAW,gBAAgB","file":"chunk-ANAESRWS.js","sourcesContent":["/**\n * @h-ai/storage — 存储配置 Schema\n * @module storage-config\n */\n\nimport { z } from 'zod'\nimport { storageM } from './storage-i18n.js'\nimport { StoragePresignExpiresInSchema } from './storage-schemas.js'\n\n// ─── 配置 Schema ───\n\n/**\n * 存储类型枚举\n *\n * 支持的存储类型:\n * - `s3` - S3 协议对象存储(兼容 AWS S3、MinIO、RustFS、阿里云 OSS 等)\n * - `local` - 本地文件存储(仅后端)\n * @example\n * ```ts\n * import { StorageTypeSchema } from '@h-ai/storage'\n *\n * const value = StorageTypeSchema.parse('local')\n * void value\n * ```\n */\nexport const StorageTypeSchema = z.enum(['s3', 'local'])\n\n/**\n * 存储类型\n * @example\n * ```ts\n * import type { StorageType } from '@h-ai/storage'\n *\n * const value: StorageType = 's3'\n * void value\n * ```\n */\nexport type StorageType = z.infer<typeof StorageTypeSchema>\n\n/**\n * StorageOperationLogConfigSchema 的数据约束与操作契约。\n * @example\n * ```ts\n * import { StorageOperationLogConfigSchema } from '@h-ai/storage'\n *\n * const value = StorageOperationLogConfigSchema.parse({ read: false, write: true, maxLength: 500, level: 'debug' })\n * void value\n * ```\n */\nexport const StorageOperationLogConfigSchema = z.object({\n read: z.boolean().default(false),\n write: z.boolean().default(false),\n maxLength: z.number().int().min(0).default(1000),\n level: z.enum(['info', 'debug', 'trace']).default('debug'),\n})\n\n/**\n * StorageOperationLogConfig 的数据约束与操作契约。\n * @example\n * ```ts\n * import { StorageOperationLogConfigSchema, type StorageOperationLogConfig } from '@h-ai/storage'\n *\n * const value: StorageOperationLogConfig = StorageOperationLogConfigSchema.parse({ read: false, write: true, maxLength: 500, level: 'debug' })\n * void value\n * ```\n */\nexport type StorageOperationLogConfig = z.infer<typeof StorageOperationLogConfigSchema>\n\nconst OperationLogConfigFieldSchema = {\n operationLog: StorageOperationLogConfigSchema.optional(),\n}\n\n/**\n * S3 存储配置 Schema\n *\n * 支持标准 AWS S3 以及兼容 S3 协议的存储服务(MinIO、RustFS、阿里云 OSS 等)。\n * 必填字段:bucket、region、accessKeyId、secretAccessKey。\n * @example\n * ```ts\n * import { S3ConfigSchema } from '@h-ai/storage'\n *\n * const value = S3ConfigSchema.parse({ type: 's3', bucket: 'uploads', region: 'us-east-1', accessKeyId: process.env.HAI_STORAGE_ACCESS_KEY_ID, secretAccessKey: process.env.HAI_STORAGE_SECRET_ACCESS_KEY })\n * void value\n * ```\n */\nexport const S3ConfigSchema = z.object({\n /** 存储类型 */\n type: z.literal('s3'),\n\n /** 存储桶名称 */\n bucket: z.string().min(1, storageM('storage_config_bucketRequired')),\n\n /** 初始化时检查并创建缺失的桶;默认关闭,启用后凭据须有建桶权限。 */\n createBucket: z.boolean().default(false),\n\n /** 区域 */\n region: z.string().min(1, storageM('storage_config_regionRequired')),\n\n /** Access Key ID */\n accessKeyId: z.string().min(1, storageM('storage_config_accessKeyIdRequired')),\n\n /** Secret Access Key */\n secretAccessKey: z.string().min(1, storageM('storage_config_secretAccessKeyRequired')),\n\n /** 自定义端点(用于 MinIO、RustFS、阿里云 OSS 等) */\n endpoint: z.string().url().optional(),\n\n /** 浏览器预签名端点,可含代理目录(如 /delivery/files),不含桶名、凭据或查询;代理须去除目录并保留 Host。 */\n publicEndpoint: z.string().refine((value) => {\n // 必须先检查原始文本;URL 校验会移除制表符和换行,不能在标准化后再检查。\n // eslint-disable-next-line no-control-regex -- 在入口显式拒绝控制字符,避免校验与代理解释不一致。\n return !/[\\u0000-\\u0020\\u007F\\\\%]/.test(value) && !/(?:^|\\/)\\.{1,2}(?:\\/|$)/.test(value)\n }, storageM('storage_config_publicEndpointInvalid')).url().refine((value) => {\n try {\n const url = new URL(value)\n return ['http:', 'https:'].includes(url.protocol) && !url.username && !url.password\n && /^\\/(?:[\\w.-]+(?:\\/[\\w.-]+)*\\/?)?$/.test(url.pathname) && !url.search && !url.hash\n }\n catch {\n return false\n }\n }, storageM('storage_config_publicEndpointInvalid')).optional(),\n\n /** 强制使用路径风格 URL(某些 S3 兼容服务需要) */\n forcePathStyle: z.boolean().default(false),\n\n /** 路径前缀(所有操作都会加上此前缀) */\n prefix: z.string().default(''),\n\n /** 公开访问基础 URL(用于生成公开 URL) */\n publicUrl: z.string().url().optional(),\n\n ...OperationLogConfigFieldSchema,\n})\n\n/**\n * S3 配置类型(parse 后,带默认值的字段已填充,optional 字段仍可缺省)\n * @example\n * ```ts\n * import { S3ConfigSchema, type S3Config } from '@h-ai/storage'\n *\n * const value: S3Config = S3ConfigSchema.parse({ type: 's3', bucket: 'uploads', region: 'us-east-1', accessKeyId: process.env.HAI_STORAGE_ACCESS_KEY_ID, secretAccessKey: process.env.HAI_STORAGE_SECRET_ACCESS_KEY })\n * void value\n * ```\n */\nexport type S3Config = z.infer<typeof S3ConfigSchema>\n\n/**\n * 本地存储配置 Schema\n *\n * 基于本地文件系统,仅后端可用。必填字段:root。\n * @example\n * ```ts\n * import { LocalConfigSchema } from '@h-ai/storage'\n *\n * const value = LocalConfigSchema.parse({ type: 'local', root: './data/uploads' })\n * void value\n * ```\n */\nexport const LocalConfigSchema = z.object({\n /** 存储类型 */\n type: z.literal('local'),\n\n /** 根目录路径 */\n root: z.string().min(1, storageM('storage_config_rootRequired')),\n\n /** 目录创建权限 */\n directoryMode: z.number().default(0o755),\n\n /** 文件创建权限 */\n fileMode: z.number().default(0o644),\n\n ...OperationLogConfigFieldSchema,\n})\n\n/**\n * 本地配置类型(parse 后,带默认值的字段已填充,optional 字段仍可缺省)\n * @example\n * ```ts\n * import { LocalConfigSchema, type LocalConfig } from '@h-ai/storage'\n *\n * const value: LocalConfig = LocalConfigSchema.parse({ type: 'local', root: './data/uploads' })\n * void value\n * ```\n */\nexport type LocalConfig = z.infer<typeof LocalConfigSchema>\n\n/**\n * 统一存储配置 Schema\n *\n * 支持 S3 和本地文件两种类型的配置验证。\n * @example\n * ```ts\n * import { StorageConfigSchema } from '@h-ai/storage'\n *\n * const value = StorageConfigSchema.parse({ type: 'local', root: './data/uploads' })\n * void value\n * ```\n */\nexport const StorageConfigSchema = z.discriminatedUnion('type', [\n S3ConfigSchema,\n LocalConfigSchema,\n])\n\n/**\n * 存储配置类型(parse 后的完整配置)\n * @example\n * ```ts\n * import { StorageConfigSchema, type StorageConfig } from '@h-ai/storage'\n *\n * const value: StorageConfig = StorageConfigSchema.parse({ type: 'local', root: './data/uploads' })\n * void value\n * ```\n */\nexport type StorageConfig = z.infer<typeof StorageConfigSchema>\n\n/**\n * 存储配置输入类型(parse 前,允许省略带默认值的字段)\n * @example\n * ```ts\n * import type { StorageConfigInput } from '@h-ai/storage'\n *\n * const value: StorageConfigInput = { type: 'local', root: './data/uploads' }\n * void value\n * ```\n */\nexport type StorageConfigInput = z.input<typeof StorageConfigSchema>\n\n// ─── 签名 URL 配置 ───\n\n/**\n * 签名 URL 配置 Schema\n *\n * 用于控制 `storage.presign.getUrl()` 生成的下载签名 URL 参数。\n * @example\n * ```ts\n * import { PresignOptionsSchema } from '@h-ai/storage'\n *\n * const value = PresignOptionsSchema.parse({ expiresIn: 60 })\n * void value\n * ```\n */\nexport const PresignOptionsSchema = z.object({\n /** 过期时间(秒),范围 1~604800(7 天),默认 3600(1 小时) */\n expiresIn: StoragePresignExpiresInSchema.default(3600), // 最长 7 天\n\n /** 响应内容类型(用于下载时设置 Content-Type) */\n responseContentType: z.string().optional(),\n\n /** 响应内容处置(用于设置下载文件名) */\n responseContentDisposition: z.string().optional(),\n})\n\n/**\n * 签名 URL 输入选项,允许省略带默认值的字段。\n * @example\n * ```ts\n * import type { PresignOptions } from '@h-ai/storage'\n *\n * const value: PresignOptions = { expiresIn: 60, responseContentType: 'text/plain' }\n * void value\n * ```\n */\nexport type PresignOptions = z.input<typeof PresignOptionsSchema>\n\n/**\n * 上传签名 URL 配置 Schema\n *\n * 继承 PresignOptionsSchema 并扩展上传专属参数,用于 `storage.presign.putUrl()`。\n * @example\n * ```ts\n * import { PresignUploadOptionsSchema } from '@h-ai/storage'\n *\n * const value = PresignUploadOptionsSchema.parse({ contentType: 'text/plain' })\n * void value\n * ```\n */\nexport const PresignUploadOptionsSchema = PresignOptionsSchema.extend({\n /** 上传内容类型(必须与实际上传匹配) */\n contentType: z.string().default('application/octet-stream'),\n})\n\n/**\n * 上传签名 URL 输入选项,省略 contentType 时使用 application/octet-stream。\n * @example\n * ```ts\n * import type { PresignUploadOptions } from '@h-ai/storage'\n *\n * const value: PresignUploadOptions = { expiresIn: 60, contentType: 'text/plain' }\n * void value\n * ```\n */\nexport type PresignUploadOptions = z.input<typeof PresignUploadOptionsSchema>\n","/**\n * @h-ai/storage — 公共类型定义\n *\n * 包含:\n * - 错误定义(HaiStorageError)\n * - 文件元数据(FileMetadata)\n * - 列表结果(ListResult)\n * - 文件操作接口(FileOperations)\n * - 目录操作接口(DirOperations)\n * - 签名 URL 操作接口(PresignOperations)\n * - 函数接口(StorageFunctions)\n * @module storage-types\n */\n\nimport type { ErrorInfo, HaiError, HaiResult } from '@h-ai/core'\nimport type { Buffer } from 'node:buffer'\nimport type { PresignOptions, PresignUploadOptions, StorageConfig, StorageConfigInput } from './storage-config.js'\nimport { core } from '@h-ai/core'\n\n// ─── 错误定义(照 @h-ai/core 范式) ───\n\nconst StorageErrorInfo = {\n CONNECTION_FAILED: '001:500',\n OPERATION_FAILED: '002:500',\n NOT_FOUND: '003:404',\n ALREADY_EXISTS: '004:409',\n PERMISSION_DENIED: '005:403',\n QUOTA_EXCEEDED: '006:413',\n INVALID_PATH: '007:400',\n IO_ERROR: '008:500',\n NETWORK_ERROR: '009:502',\n NOT_INITIALIZED: '010:500',\n UNSUPPORTED_TYPE: '011:400',\n CONFIG_ERROR: '012:500',\n PRESIGN_FAILED: '013:500',\n UPLOAD_FAILED: '014:500',\n DOWNLOAD_FAILED: '015:500',\n CHECKSUM_FAILED: '016:500',\n INVALID_CHECKSUM: '017:400',\n UNSUPPORTED_OPERATION: '018:400',\n INVALID_LIST_OPTIONS: '019:400',\n} as const satisfies ErrorInfo\n\n/**\n * HaiStorageError 的数据约束与操作契约。\n * @example\n * ```ts\n * import { HaiStorageError } from '@h-ai/storage'\n * import { err } from '@h-ai/core'\n *\n * const result = err(HaiStorageError.NOT_FOUND, 'File missing')\n * if (!result.success) void result.error.code\n * ```\n */\nexport const HaiStorageError = core.error.buildHaiErrorsDef('storage', StorageErrorInfo)\n\n/**\n * 批量删除中未确认完成的一项;key 始终使用调用方视角,不含 Provider prefix。\n * @example\n * ```ts\n * import type { StorageDeleteFailure } from '@h-ai/storage'\n *\n * const value: StorageDeleteFailure = { key: 'private.txt', code: 'AccessDenied', retryable: false, outcome: 'failed' }\n * void value\n * ```\n */\nexport interface StorageDeleteFailure {\n /** 调用方提交的文件键。 */\n key: string\n /** 渠道/框架错误标识,不包含渠道原始 Message。 */\n code: string\n /** 是否属于可重试的瞬时故障;未知结果重试前仍需考虑并发上传/版本语义。 */\n retryable: boolean\n /** failed 为已知失败,unknown 为已发送但结果未知,notAttempted 为尚未发送。 */\n outcome: 'failed' | 'unknown' | 'notAttempted'\n}\n\n/**\n * 批量删除进度;任一失败不能宣称全部删除完成。\n * @example\n * ```ts\n * import type { StorageDeleteSummary } from '@h-ai/storage'\n *\n * const value: StorageDeleteSummary = { deletedKeys: ['a.txt'], failures: [] }\n * void value\n * ```\n */\nexport interface StorageDeleteSummary {\n /** 已确认删除或本来就不存在的唯一文件键。 */\n deletedKeys: string[]\n /** 失败、未知及尚未尝试的唯一文件键。 */\n failures: StorageDeleteFailure[]\n}\n\n/**\n * 批量删除结果;失败仍遵守 HaiResult,部分进度置于 error.ext.deleteMany。\n * @example\n * ```ts\n * import { storage, type StorageDeleteResult } from '@h-ai/storage'\n *\n * async function remove() {\n * const result: StorageDeleteResult = await storage.file.deleteMany(['a.txt', 'b.txt'])\n * return result.success ? result.data : result.error.ext?.deleteMany\n * }\n * ```\n */\nexport type StorageDeleteResult\n = | { success: true, data: StorageDeleteSummary }\n | { success: false, error: HaiError & { ext?: Record<string, unknown> & { deleteMany?: StorageDeleteSummary } } }\n\n// ─── 文件元数据 ───\n\n/**\n * 文件元数据接口\n *\n * 表示存储中单个文件的元信息,由 put/head/copy/list 等操作返回。\n * @example\n * ```ts\n * import type { FileMetadata } from '@h-ai/storage'\n *\n * const value: FileMetadata = { key: 'a.txt', size: 5, contentType: 'text/plain', lastModified: new Date() }\n * void value\n * ```\n */\nexport interface FileMetadata {\n /** 文件键/路径(不含配置 prefix,对用户透明) */\n key: string\n /** 文件大小(字节) */\n size: number\n /** 内容类型(MIME),未指定时根据扩展名自动推断 */\n contentType: string\n /** 最后修改时间 */\n lastModified: Date\n /** ETag(缓存验证标记;本地为 size-mtime,S3 由服务端生成,不等同于内容 checksum) */\n etag?: string\n /** 自定义元数据键值对 */\n metadata?: Record<string, string>\n}\n\n/**\n * 列表选项\n *\n * 用于控制 `storage.dir.list()` 的过滤、分页和目录模拟行为。\n * @example\n * ```ts\n * import type { ListOptions } from '@h-ai/storage'\n *\n * const value: ListOptions = { prefix: 'reports/', delimiter: '/', maxKeys: 100 }\n * void value\n * ```\n */\nexport interface ListOptions {\n /** 前缀过滤(只返回以此前缀开头的文件) */\n prefix?: string\n /**\n * 分页标记(从上次 ListResult.nextContinuationToken 获取)\n *\n * Local 游标绑定根目录、prefix 和 delimiter,可跨实例使用;不得跨查询或 provider 混用。\n */\n continuationToken?: string\n /** 每页逻辑条目上限(1–1000,默认 1000);files 和 commonPrefixes 合计计数。 */\n maxKeys?: number\n /** 分隔符,通常为 '/';从 prefix 之后首次出现的位置折叠 commonPrefixes,空字符串表示不折叠。 */\n delimiter?: string\n}\n\n/**\n * 列表结果\n *\n * 包含当前页的文件列表、目录前缀、分页信息。\n * @example\n * ```ts\n * import type { ListResult } from '@h-ai/storage'\n *\n * const value: ListResult = { files: [], commonPrefixes: ['reports/'], isTruncated: false }\n * void value\n * ```\n */\nexport interface ListResult {\n /** 当前页文件元数据列表 */\n files: FileMetadata[]\n /** 公共前缀:由实际匹配文件按 delimiter 折叠,不包含 Local 空物理目录。 */\n commonPrefixes: string[]\n /** 下一页标记;无后续页时为 undefined */\n nextContinuationToken?: string\n /** 是否被截断(true 表示还有更多数据,可用 nextContinuationToken 继续拉取) */\n isTruncated: boolean\n}\n\n// ─── 上传下载选项 ───\n\n/**\n * 上传选项\n *\n * 用于控制 `storage.file.put()` 的元数据与缓存行为。\n * @example\n * ```ts\n * import type { UploadOptions } from '@h-ai/storage'\n *\n * const value: UploadOptions = { ifNoneMatch: '*', contentType: 'text/plain', signal: new AbortController().signal }\n * void value\n * ```\n */\nexport interface UploadOptions {\n /** 仅当 key 不存在时原子写入;S3 使用标准 If-None-Match,后端不支持时返回错误。 */\n ifNoneMatch?: '*'\n /** 取消上传;失败或取消时清理未完成的临时文件/分片。 */\n signal?: AbortSignal\n /** 内容类型(不指定时根据文件扩展名自动推断) */\n contentType?: string\n /** 自定义元数据键值对(存储后可通过 head 获取) */\n metadata?: Record<string, string>\n /** Cache-Control 响应头(例如 'max-age=3600') */\n cacheControl?: string\n /** Content-Disposition 响应头(例如 'attachment; filename=\"a.txt\"') */\n contentDisposition?: string\n}\n\n/**\n * 下载选项\n *\n * 用于控制 `storage.file.get()` 的范围读取。\n * 两个字段均为字节偏移(含),与 HTTP Range 语义一致。\n * @example\n * ```ts\n * import type { DownloadOptions } from '@h-ai/storage'\n *\n * const value: DownloadOptions = { rangeStart: 0, rangeEnd: 99 }\n * void value\n * ```\n */\nexport interface DownloadOptions {\n /** 取消读取。 */\n signal?: AbortSignal\n /** 范围起始位置(字节,含);不指定时从文件开头读取 */\n rangeStart?: number\n /** 范围结束位置(字节,含);不指定时读取到文件末尾 */\n rangeEnd?: number\n}\n\n/**\n * 复制选项\n *\n * 用于控制 `storage.file.copy()` 的目标文件元数据。\n * 提供任意字段时,S3 Provider 会使用 MetadataDirective='REPLACE' 覆盖源文件元数据。\n * @example\n * ```ts\n * import type { CopyOptions } from '@h-ai/storage'\n *\n * const value: CopyOptions = { contentType: 'text/plain', metadata: { category: 'reports' } }\n * void value\n * ```\n */\nexport interface CopyOptions {\n /** 目标文件的内容类型(不指定时继承源文件) */\n contentType?: string\n /** 目标文件的自定义元数据(不指定时继承源文件) */\n metadata?: Record<string, string>\n}\n\n// ─── 操作接口 ───\n\n/**\n * 文件内容;异步字节流支持 Node Readable 等,不将整个文件载入内存。\n * @example\n * ```ts\n * import type { StorageData } from '@h-ai/storage'\n *\n * const value: StorageData = new TextEncoder().encode('hello')\n * void value\n * ```\n */\nexport type StorageData = Buffer | Uint8Array | string | AsyncIterable<Uint8Array>\n\n/**\n * 常见 checksum 算法;MD5/SHA-1 仅用于兼容性完整性校验,不用于安全认证。\n * @example\n * ```ts\n * import type { ChecksumAlgorithm } from '@h-ai/storage'\n *\n * const value: ChecksumAlgorithm = 'sha256'\n * void value\n * ```\n */\nexport type ChecksumAlgorithm = 'sha256' | 'sha384' | 'sha512' | 'sha1' | 'md5'\n\n/**\n * checksum 计算选项;默认 SHA-256、小写 hex。\n * @example\n * ```ts\n * import type { ChecksumOptions } from '@h-ai/storage'\n *\n * const value: ChecksumOptions = { algorithm: 'sha256', encoding: 'hex' }\n * void value\n * ```\n */\nexport interface ChecksumOptions {\n algorithm?: ChecksumAlgorithm\n encoding?: 'hex' | 'base64'\n signal?: AbortSignal\n}\n\n/**\n * Node 端无状态 checksum 工具,不依赖 storage.init;流输入仅消费一次。\n * @example\n * ```ts\n * import type { ChecksumOperations } from '@h-ai/storage'\n *\n * async function inspect(operations: ChecksumOperations) {\n * const result = await operations.generate('hello')\n * if (!result.success) return result.error.message\n * return result.data\n * }\n * ```\n */\nexport interface ChecksumOperations {\n generate: (data: StorageData, options?: ChecksumOptions) => Promise<HaiResult<string>>\n verify: (data: StorageData, expected: string, options?: ChecksumOptions) => Promise<HaiResult<boolean>>\n}\n\n/**\n * 文件操作接口\n *\n * 提供文件的读写操作。\n * @example\n * ```ts\n * import type { FileOperations } from '@h-ai/storage'\n *\n * async function inspect(operations: FileOperations) {\n * const result = await operations.getStream('reports/a.txt')\n * if (!result.success) return result.error.message\n * for await (const chunk of result.data) void chunk.byteLength\n * }\n * ```\n */\nexport interface FileOperations {\n /**\n * 上传文件\n *\n * @param key - 文件键/路径\n * @param data - 文件内容(Buffer、Uint8Array、字符串或异步字节流)\n * @param options - 上传选项\n * @returns 上传结果包含文件元数据\n * @example\n * ```ts\n * const result = await storage.file.put('uploads/a.txt', 'hello')\n * if (result.success) {\n * result.data.key\n * }\n * ```\n */\n put: (key: string, data: StorageData, options?: UploadOptions) => Promise<HaiResult<FileMetadata>>\n\n /**\n * 下载文件\n *\n * @param key - 文件键/路径\n * @param options - 下载选项\n * @returns 文件内容\n * @example\n * ```ts\n * const result = await storage.file.get('uploads/a.txt')\n * if (result.success) {\n * const buffer = result.data\n * void buffer\n * }\n * ```\n */\n get: (key: string, options?: DownloadOptions) => Promise<HaiResult<Buffer>>\n\n /**\n * 读取字节流,支持 Range 与取消;消费方负责及时迭代或关闭流。\n * @throws 迭代期间的 I/O/取消错误;初始请求失败通过 HaiResult 返回。\n * @example\n * ```ts\n * import { storage } from '@h-ai/storage'\n *\n * const result = await storage.file.getStream('reports/a.txt')\n * if (!result.success) throw new Error(result.error.message)\n * try {\n * for await (const chunk of result.data) void chunk.byteLength\n * } catch (error) {\n * // 初始请求成功后,迭代仍可能因 I/O/取消失败。\n * throw error\n * }\n * ```\n */\n getStream: (key: string, options?: DownloadOptions) => Promise<HaiResult<AsyncIterable<Uint8Array>>>\n\n /**\n * 获取文件元数据\n *\n * @param key - 文件键/路径\n * @returns 文件元数据\n * @example\n * ```ts\n * const result = await storage.file.head('uploads/a.txt')\n * if (result.success) {\n * result.data.size\n * }\n * ```\n */\n head: (key: string) => Promise<HaiResult<FileMetadata>>\n\n /**\n * 检查文件是否存在\n *\n * @param key - 文件键/路径\n * @returns 是否存在\n * @example\n * ```ts\n * const result = await storage.file.exists('uploads/a.txt')\n * if (result.success) {\n * result.data\n * }\n * ```\n */\n exists: (key: string) => Promise<HaiResult<boolean>>\n\n /**\n * 删除文件\n *\n * @param key - 文件键/路径\n * @returns 删除结果\n * @example\n * ```ts\n * import { storage } from '@h-ai/storage'\n *\n * const result = await storage.file.delete('uploads/a.txt')\n * if (!result.success) throw new Error(result.error.message)\n * ```\n */\n delete: (key: string) => Promise<HaiResult<void>>\n\n /**\n * 批量删除文件\n *\n * @param keys - 文件键/路径列表\n * @returns 全部成功时返回进度;任一失败时 success=false,进度在 error.ext.deleteMany\n * @example\n * ```ts\n * const result = await storage.file.deleteMany(['a.txt', 'b.txt'])\n * const progress = result.success ? result.data : result.error.ext?.deleteMany\n * void progress\n * ```\n */\n deleteMany: (keys: string[]) => Promise<StorageDeleteResult>\n\n /**\n * 复制文件\n *\n * @param sourceKey - 源文件键\n * @param destKey - 目标文件键\n * @param options - 复制选项\n * @returns 目标文件元数据\n * @example\n * ```ts\n * import { storage } from '@h-ai/storage'\n *\n * const result = await storage.file.copy('a.txt', 'b.txt')\n * if (!result.success) throw new Error(result.error.message)\n * void result.data.key\n * ```\n */\n copy: (sourceKey: string, destKey: string, options?: CopyOptions) => Promise<HaiResult<FileMetadata>>\n}\n\n/**\n * 目录操作接口\n *\n * 提供目录(前缀)的列表操作。\n * @example\n * ```ts\n * import type { DirOperations } from '@h-ai/storage'\n *\n * async function inspect(operations: DirOperations) {\n * const page = await operations.list({ prefix: 'reports/', maxKeys: 100 })\n * if (!page.success) return page.error.message\n * return page.data.files\n * }\n * ```\n */\nexport interface DirOperations {\n /**\n * 列出目录内容\n *\n * @param options - 列表选项\n * @returns 文件和目录列表\n * @example\n * ```ts\n * const result = await storage.dir.list({ prefix: 'uploads/' })\n * if (result.success) {\n * result.data.files\n * }\n * ```\n */\n list: (options?: ListOptions) => Promise<HaiResult<ListResult>>\n\n /**\n * 删除目录(删除指定前缀下的所有文件)\n *\n * @param prefix - 目录前缀\n * @returns 删除结果\n * @example\n * ```ts\n * import { storage } from '@h-ai/storage'\n *\n * const result = await storage.dir.delete('uploads/tmp/')\n * if (!result.success) throw new Error(result.error.message)\n * ```\n */\n delete: (prefix: string) => Promise<HaiResult<void>>\n}\n\n/**\n * 签名 URL 操作接口\n *\n * S3 provider 提供临时授权 URL,支持前端直接上传下载。\n * local 的 getUrl/putUrl 返回 UNSUPPORTED_OPERATION,publicUrl 返回 null。\n * @example\n * ```ts\n * import type { PresignOperations } from '@h-ai/storage'\n *\n * async function inspect(operations: PresignOperations) {\n * const signed = await operations.putUrl('reports/a.txt', { contentType: 'text/plain', expiresIn: 300 })\n * if (!signed.success) return signed.error.message\n * return signed.data\n * }\n * ```\n */\nexport interface PresignOperations {\n /**\n * 生成 GET 下载签名 URL;使用 publicEndpoint,缺省回退 endpoint/AWS 端点。\n * publicEndpoint 可含代理目录,返回值已带此前缀;代理须去除它并保留 S3 URI/Host/query,调用方不得自行改写。\n *\n * @param key - 文件键/路径\n * @param options - 签名选项\n * @returns 成功时为可访问的 S3 签名 URL;local 返回 UNSUPPORTED_OPERATION。\n * @example\n * ```ts\n * const result = await storage.presign.getUrl('a.txt', { expiresIn: 60 })\n * if (!result.success) throw new Error(result.error.message)\n * const downloadUrl = result.data\n * void downloadUrl\n * ```\n */\n getUrl: (key: string, options?: PresignOptions) => Promise<HaiResult<string>>\n\n /**\n * 生成 PUT 上传签名 URL;publicEndpoint 目录与 GET 相同处理,服务端 CRUD 仍使用内部 endpoint。\n *\n * @param key - 文件键/路径\n * @param options - 上传签名选项\n * @returns 成功时为可访问的 S3 签名 URL;local 返回 UNSUPPORTED_OPERATION。\n * @example\n * ```ts\n * const result = await storage.presign.putUrl('a.txt', { contentType: 'text/plain' })\n * if (!result.success) throw new Error(result.error.message)\n * const uploadUrl = result.data\n * void uploadUrl\n * ```\n */\n putUrl: (key: string, options?: PresignUploadOptions) => Promise<HaiResult<string>>\n\n /**\n * 获取公开访问 URL(如果配置了 publicUrl)\n *\n * @param key - 文件键/路径\n * @returns 公开 URL;S3 未配置 publicUrl 或 local provider 均返回 null。\n * @example\n * ```ts\n * const url = storage.presign.publicUrl('a.txt')\n * void url\n * ```\n */\n publicUrl: (key: string) => string | null\n}\n\n// ─── 函数接口 ───\n\n/**\n * 存储函数接口\n *\n * 统一的存储访问入口:\n * - `storage.init()` — 初始化存储连接\n * - `storage.close()` — 关闭连接并释放资源\n * - `storage.file` — 文件操作(上传/下载/删除/复制等)\n * - `storage.dir` — 目录操作(列表/删除)\n * - `storage.presign` — 签名 URL 操作\n * - `storage.config` — 当前脱敏配置快照(未初始化时为 null)\n * - `storage.isInitialized` — 初始化状态\n * @example\n * ```ts\n * import { storage } from '@h-ai/storage'\n *\n * const initialized = await storage.init({ type: 'local', root: './data/uploads' })\n * if (!initialized.success) throw new Error(initialized.error.message)\n * try {\n * const uploaded = await storage.file.put('reports/a.txt', 'hello', { ifNoneMatch: '*' })\n * if (!uploaded.success) throw new Error(uploaded.error.message)\n * const downloaded = await storage.file.get('reports/a.txt')\n * if (!downloaded.success) throw new Error(downloaded.error.message)\n * void downloaded.data\n * } finally {\n * const closed = await storage.close()\n * if (!closed.success) throw new Error(closed.error.message)\n * }\n * ```\n */\nexport interface StorageFunctions {\n /** 无状态 checksum 生成与校验,Node 专用,无需初始化。 */\n readonly checksum: ChecksumOperations\n /**\n * 初始化存储连接\n *\n * 已有连接时会先 close 再重新初始化。\n *\n * @param config - 存储配置(支持省略带默认值的字段)\n * @returns 成功 ok(undefined);失败返回 err(含 HaiStorageError 定义的错误信息)\n * @example\n * ```ts\n * import { storage } from '@h-ai/storage'\n *\n * const initialized = await storage.init({ type: 'local', root: './data/uploads' })\n * if (!initialized.success) throw new Error(initialized.error.message)\n * const closed = await storage.close()\n * if (!closed.success) throw new Error(closed.error.message)\n * ```\n */\n init: (config: StorageConfigInput) => Promise<HaiResult<void>>\n\n /**\n * 关闭存储连接并释放资源\n *\n * 等待初始化并合并并发关闭;失败时操作不可用,资源保留供 close/init 重试。\n * @returns 所有资源释放成功才返回成功;未初始化时同样成功。\n * @example\n * ```ts\n * import { storage } from '@h-ai/storage'\n *\n * const closed = await storage.close()\n * if (!closed.success) throw new Error(closed.error.message)\n * ```\n */\n close: () => Promise<HaiResult<void>>\n\n /** 当前脱敏后的存储配置快照;未初始化或已关闭时为 null */\n readonly config: StorageConfig | null\n\n /** 是否已完成初始化 */\n readonly isInitialized: boolean\n\n /** 文件操作接口(put / get / head / exists / delete / deleteMany / copy) */\n readonly file: FileOperations\n\n /** 目录操作接口(list / delete) */\n readonly dir: DirOperations\n\n /** 签名 URL 操作接口(getUrl / putUrl / publicUrl) */\n readonly presign: PresignOperations\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// src/storage-schemas.ts
|
|
4
|
+
var STORAGE_KEY_MAX_BYTES = 1024;
|
|
5
|
+
var STORAGE_DELETE_MAX_KEYS = 1e3;
|
|
6
|
+
function getStorageKeyIssue(key, allowEmpty = false) {
|
|
7
|
+
if (allowEmpty && key === "")
|
|
8
|
+
return void 0;
|
|
9
|
+
if (key.trim() === "")
|
|
10
|
+
return "empty";
|
|
11
|
+
if (new TextEncoder().encode(key).byteLength > STORAGE_KEY_MAX_BYTES)
|
|
12
|
+
return "tooLong";
|
|
13
|
+
if (key.startsWith("/") || key.includes("\\") || [...key].some((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127))
|
|
14
|
+
return "characters";
|
|
15
|
+
if (key.split("/").some((segment) => segment === "." || segment === ".."))
|
|
16
|
+
return "segments";
|
|
17
|
+
return void 0;
|
|
18
|
+
}
|
|
19
|
+
function keySchema(allowEmpty) {
|
|
20
|
+
return z.string().superRefine((key, context) => {
|
|
21
|
+
const issue = getStorageKeyIssue(key, allowEmpty);
|
|
22
|
+
if (issue)
|
|
23
|
+
context.addIssue({ code: "custom", params: { storageKeyIssue: issue } });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
var StorageKeySchema = keySchema(false);
|
|
27
|
+
var StoragePrefixSchema = keySchema(true);
|
|
28
|
+
var StorageDeleteKeysSchema = z.array(StorageKeySchema).min(1).max(STORAGE_DELETE_MAX_KEYS);
|
|
29
|
+
var StoragePresignExpiresInSchema = z.number().int().min(1).max(604800);
|
|
30
|
+
|
|
31
|
+
export { STORAGE_DELETE_MAX_KEYS, STORAGE_KEY_MAX_BYTES, StorageDeleteKeysSchema, StorageKeySchema, StoragePrefixSchema, StoragePresignExpiresInSchema, getStorageKeyIssue };
|
|
32
|
+
//# sourceMappingURL=chunk-P65WYXJG.js.map
|
|
33
|
+
//# sourceMappingURL=chunk-P65WYXJG.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/storage-schemas.ts"],"names":[],"mappings":";;;AAgBO,IAAM,qBAAA,GAAwB;AAa9B,IAAM,uBAAA,GAA0B;AA6BhC,SAAS,kBAAA,CAAmB,GAAA,EAAa,UAAA,GAAa,KAAA,EAAoC;AAC/F,EAAA,IAAI,cAAc,GAAA,KAAQ,EAAA;AACxB,IAAA,OAAO,MAAA;AACT,EAAA,IAAI,GAAA,CAAI,MAAK,KAAM,EAAA;AACjB,IAAA,OAAO,OAAA;AACT,EAAA,IAAI,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,EAAE,UAAA,GAAa,qBAAA;AAC7C,IAAA,OAAO,SAAA;AACT,EAAA,IAAI,GAAA,CAAI,WAAW,GAAG,CAAA,IAAK,IAAI,QAAA,CAAS,IAAI,CAAA,IAAK,CAAC,GAAG,GAAG,EAAE,IAAA,CAAK,CAAA,IAAA,KAAQ,IAAA,CAAK,UAAA,CAAW,CAAC,CAAA,GAAI,MAAM,IAAA,CAAK,UAAA,CAAW,CAAC,CAAA,KAAM,GAAG,CAAA;AAC1H,IAAA,OAAO,YAAA;AACT,EAAA,IAAI,GAAA,CAAI,MAAM,GAAG,CAAA,CAAE,KAAK,CAAA,OAAA,KAAW,OAAA,KAAY,GAAA,IAAO,OAAA,KAAY,IAAI,CAAA;AACpE,IAAA,OAAO,UAAA;AACT,EAAA,OAAO,MAAA;AACT;AAGA,SAAS,UAAU,UAAA,EAAqB;AACtC,EAAA,OAAO,EAAE,MAAA,EAAO,CAAE,WAAA,CAAY,CAAC,KAAK,OAAA,KAAY;AAC9C,IAAA,MAAM,KAAA,GAAQ,kBAAA,CAAmB,GAAA,EAAK,UAAU,CAAA;AAChD,IAAA,IAAI,KAAA;AACF,MAAA,OAAA,CAAQ,QAAA,CAAS,EAAE,IAAA,EAAM,QAAA,EAAU,QAAQ,EAAE,eAAA,EAAiB,KAAA,EAAM,EAAG,CAAA;AAAA,EAC3E,CAAC,CAAA;AACH;AAYO,IAAM,gBAAA,GAAmB,UAAU,KAAK;AAYxC,IAAM,mBAAA,GAAsB,UAAU,IAAI;AAY1C,IAAM,uBAAA,GAA0B,EAAE,KAAA,CAAM,gBAAgB,EAAE,GAAA,CAAI,CAAC,CAAA,CAAE,GAAA,CAAI,uBAAuB;AAY5F,IAAM,6BAAA,GAAgC,CAAA,CAAE,MAAA,EAAO,CAAE,GAAA,GAAM,GAAA,CAAI,CAAC,CAAA,CAAE,GAAA,CAAI,MAAM","file":"chunk-P65WYXJG.js","sourcesContent":["/**\n * @h-ai/storage — 跨端输入约束;仅依赖 Zod,不加载 Provider、配置或 Node 模块。\n * @module storage-schemas\n */\nimport { z } from 'zod'\n\n/**\n * 对象键的 UTF-8 字节上限;不是 JavaScript 字符串长度。\n * @example\n * ```ts\n * import { STORAGE_KEY_MAX_BYTES } from '@h-ai/storage/schemas'\n *\n * const fits = new TextEncoder().encode('uploads/报告.pdf').length <= STORAGE_KEY_MAX_BYTES\n * void fits\n * ```\n */\nexport const STORAGE_KEY_MAX_BYTES = 1024\n\n/**\n * 单次公开 HTTP 删除请求的原始键数量上限;Node deleteMany 仍按 Provider 上限分批。\n * @example\n * ```ts\n * import { STORAGE_DELETE_MAX_KEYS } from '@h-ai/storage/schemas'\n *\n * const keys = ['a.txt', 'b.txt']\n * const page = keys.slice(0, STORAGE_DELETE_MAX_KEYS)\n * void page\n * ```\n */\nexport const STORAGE_DELETE_MAX_KEYS = 1000\n\n/**\n * 键校验失败类别;调用层据此生成本地化消息,不解析错误文本。\n * @example\n * ```ts\n * import type { StorageKeyIssue } from '@h-ai/storage'\n *\n * const value: StorageKeyIssue = 'tooLong'\n * void value\n * ```\n */\nexport type StorageKeyIssue = 'empty' | 'tooLong' | 'characters' | 'segments'\n\n/**\n * 检查跨端键边界,不归一化输入,不替代文件系统防逃逸或资源授权。\n *\n * @param key - 调用方视角的对象键,不含 Provider 配置前缀。\n * @param allowEmpty - 是否允许空字符串(用于列表前缀);纯空白仍无效。\n * @returns 首个失败类别;合法时为 undefined。\n * @example\n * ```ts\n * import { getStorageKeyIssue } from '@h-ai/storage/schemas'\n *\n * const unsafe = getStorageKeyIssue('../private.txt') // segments\n * const emptyPrefix = getStorageKeyIssue('', true) // undefined\n * void [unsafe, emptyPrefix]\n * ```\n */\nexport function getStorageKeyIssue(key: string, allowEmpty = false): StorageKeyIssue | undefined {\n if (allowEmpty && key === '')\n return undefined\n if (key.trim() === '')\n return 'empty'\n if (new TextEncoder().encode(key).byteLength > STORAGE_KEY_MAX_BYTES)\n return 'tooLong'\n if (key.startsWith('/') || key.includes('\\\\') || [...key].some(char => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127))\n return 'characters'\n if (key.split('/').some(segment => segment === '.' || segment === '..'))\n return 'segments'\n return undefined\n}\n\n/** 通过稳定 params 保留失败类别;默认 Zod 消息交由请求层按 locale 映射。 */\nfunction keySchema(allowEmpty: boolean) {\n return z.string().superRefine((key, context) => {\n const issue = getStorageKeyIssue(key, allowEmpty)\n if (issue)\n context.addIssue({ code: 'custom', params: { storageKeyIssue: issue } })\n })\n}\n\n/**\n * 文件键 Schema:非空、UTF-8 最多 1024 字节,无绝对路径、反斜线、控制符或点路径段。\n * @example\n * ```ts\n * import { StorageKeySchema } from '@h-ai/storage'\n *\n * const value = StorageKeySchema.parse('users/u1/report.pdf')\n * void value\n * ```\n */\nexport const StorageKeySchema = keySchema(false)\n\n/**\n * 列表前缀 Schema;允许空字符串与部分文件名,不把前缀当物理目录。\n * @example\n * ```ts\n * import { StoragePrefixSchema } from '@h-ai/storage'\n *\n * const value = StoragePrefixSchema.parse('users/u1/rep')\n * void value\n * ```\n */\nexport const StoragePrefixSchema = keySchema(true)\n\n/**\n * HTTP 批量删除键 Schema;按输入数量计数,重复键不能绕过上限。\n * @example\n * ```ts\n * import { StorageDeleteKeysSchema } from '@h-ai/storage'\n *\n * const value = StorageDeleteKeysSchema.parse(['users/u1/a.txt', 'users/u1/b.txt'])\n * void value\n * ```\n */\nexport const StorageDeleteKeysSchema = z.array(StorageKeySchema).min(1).max(STORAGE_DELETE_MAX_KEYS)\n\n/**\n * 预签名有效期,单位秒;整数 1–604800,不在此处填充 Node/HTTP 各自默认值。\n * @example\n * ```ts\n * import { StoragePresignExpiresInSchema } from '@h-ai/storage'\n *\n * const value = StoragePresignExpiresInSchema.parse(900)\n * void value\n * ```\n */\nexport const StoragePresignExpiresInSchema = z.number().int().min(1).max(604800)\n"]}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { core } from '@h-ai/core';
|
|
2
|
+
|
|
3
|
+
// src/storage-i18n.ts
|
|
4
|
+
|
|
5
|
+
// messages/en-US.json
|
|
6
|
+
var en_US_default = {
|
|
7
|
+
$schema: "https://inlang.com/schema/inlang-message-format",
|
|
8
|
+
storage_notInitialized: "Storage not initialized, please call storage.init() first",
|
|
9
|
+
storage_bucketNotExist: "Bucket does not exist",
|
|
10
|
+
storage_networkError: "Network error",
|
|
11
|
+
storage_responseBodyEmpty: "Response body is empty",
|
|
12
|
+
storage_presignUrlFailed: "Failed to generate presigned URL",
|
|
13
|
+
storage_presignUploadUrlFailed: "Failed to generate presigned upload URL",
|
|
14
|
+
storage_localPresignUnsupported: "Local storage does not support presigned URLs; upload or download through an authenticated application file endpoint",
|
|
15
|
+
storage_listOptionsInvalid: "Invalid list options: maxKeys must be an integer between 1 and 1000, and prefix and delimiter must be well-formed Unicode strings",
|
|
16
|
+
storage_listCursorInvalid: "Invalid list cursor or mismatched root, prefix or delimiter",
|
|
17
|
+
storage_s3ConfigTypeError: "Config type error: S3 provider only supports type=s3",
|
|
18
|
+
storage_diskSpaceInsufficient: "Insufficient disk space",
|
|
19
|
+
storage_localConfigTypeError: "Config type error: Local provider only supports type=local",
|
|
20
|
+
storage_operationFailed: "Operation failed: {error}",
|
|
21
|
+
storage_unsupportedType: "Unsupported storage type: {type}",
|
|
22
|
+
storage_config_bucketRequired: "Bucket is required",
|
|
23
|
+
storage_config_publicEndpointInvalid: "Use an HTTP(S) endpoint with an optional proxy path containing letters, numbers, underscores, dots and hyphens. Credentials, query parameters and fragments are not allowed.",
|
|
24
|
+
storage_config_regionRequired: "Region is required",
|
|
25
|
+
storage_config_accessKeyIdRequired: "Access Key ID is required",
|
|
26
|
+
storage_config_secretAccessKeyRequired: "Secret Access Key is required",
|
|
27
|
+
storage_config_rootRequired: "Root path is required",
|
|
28
|
+
storage_fileNotFound: "File not found: {key}",
|
|
29
|
+
storage_permissionDenied: "Permission denied: {key}",
|
|
30
|
+
storage_pathIsDir: "Path is a directory: {key}",
|
|
31
|
+
storage_ioError: "IO error: {error}",
|
|
32
|
+
storage_deleteManyFailed: "Failed to delete {count} files",
|
|
33
|
+
storage_s3ClientNotInitialized: "S3 client not initialized",
|
|
34
|
+
storage_s3ConfigNotInitialized: "S3 config not initialized",
|
|
35
|
+
storage_uploadCanceled: "Upload canceled",
|
|
36
|
+
storage_downloadCanceled: "Download canceled",
|
|
37
|
+
storage_uploadFailedStatus: "Upload failed (status {status}): {statusText}",
|
|
38
|
+
storage_downloadFailedStatus: "Download failed (status {status}): {statusText}",
|
|
39
|
+
storage_unknownError: "Unknown error",
|
|
40
|
+
storage_saveFileFailed: "Failed to save file",
|
|
41
|
+
storage_downloadDefaultFilename: "download",
|
|
42
|
+
storage_pathTraversal: "Path traversal attack detected",
|
|
43
|
+
storage_localNotInitialized: "Local storage not initialized",
|
|
44
|
+
storage_configError: "Storage config validation failed: {error}",
|
|
45
|
+
storage_metaFileAccess: "Direct access to internal files is not allowed: {key}",
|
|
46
|
+
storage_fileAlreadyExists: "File already exists: {key}",
|
|
47
|
+
storage_checksumFailed: "Failed to calculate checksum",
|
|
48
|
+
storage_checksumInvalid: "Invalid checksum or algorithm",
|
|
49
|
+
storage_streamInvalidChunk: "File stream must contain byte chunks",
|
|
50
|
+
storage_sharedBackendRequired: "Production mode requires a shared backend; local files and memory backends are for development or explicit end-to-end tests."
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// messages/ja-JP.json
|
|
54
|
+
var ja_JP_default = {
|
|
55
|
+
$schema: "https://inlang.com/schema/inlang-message-format",
|
|
56
|
+
storage_notInitialized: "\u30B9\u30C8\u30EC\u30FC\u30B8\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093\u3002\u5148\u306Bstorage.init()\u3092\u547C\u3073\u51FA\u3057\u3066\u304F\u3060\u3055\u3044",
|
|
57
|
+
storage_bucketNotExist: "\u30D0\u30B1\u30C3\u30C8\u304C\u5B58\u5728\u3057\u307E\u305B\u3093",
|
|
58
|
+
storage_networkError: "\u30CD\u30C3\u30C8\u30EF\u30FC\u30AF\u30A8\u30E9\u30FC",
|
|
59
|
+
storage_responseBodyEmpty: "\u30EC\u30B9\u30DD\u30F3\u30B9\u672C\u6587\u304C\u7A7A\u3067\u3059",
|
|
60
|
+
storage_presignUrlFailed: "\u7F72\u540D\u4ED8\u304DURL\u306E\u751F\u6210\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
61
|
+
storage_presignUploadUrlFailed: "\u30A2\u30C3\u30D7\u30ED\u30FC\u30C9\u7528\u7F72\u540D\u4ED8\u304DURL\u306E\u751F\u6210\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
62
|
+
storage_localPresignUnsupported: "\u30ED\u30FC\u30AB\u30EB\u30B9\u30C8\u30EC\u30FC\u30B8\u306F\u7F72\u540D\u4ED8\u304D URL \u306B\u5BFE\u5FDC\u3057\u3066\u3044\u307E\u305B\u3093\u3002\u8A8D\u8A3C\u4ED8\u304D\u306E\u30A2\u30D7\u30EA\u30B1\u30FC\u30B7\u30E7\u30F3\u306E\u30D5\u30A1\u30A4\u30EB\u30A8\u30F3\u30C9\u30DD\u30A4\u30F3\u30C8\u3067\u30A2\u30C3\u30D7\u30ED\u30FC\u30C9\u307E\u305F\u306F\u30C0\u30A6\u30F3\u30ED\u30FC\u30C9\u3057\u3066\u304F\u3060\u3055\u3044",
|
|
63
|
+
storage_listOptionsInvalid: "\u4E00\u89A7\u30AA\u30D7\u30B7\u30E7\u30F3\u304C\u7121\u52B9\u3067\u3059\u3002maxKeys \u306F1\u301C1000\u306E\u6574\u6570\u3001prefix \u3068 delimiter \u306F\u6709\u52B9\u306A Unicode \u6587\u5B57\u5217\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044",
|
|
64
|
+
storage_listCursorInvalid: "\u4E00\u89A7\u30AB\u30FC\u30BD\u30EB\u304C\u7121\u52B9\u304B\u3001\u30EB\u30FC\u30C8\u3001\u30D7\u30EC\u30D5\u30A3\u30C3\u30AF\u30B9\u3001\u533A\u5207\u308A\u6587\u5B57\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093",
|
|
65
|
+
storage_s3ConfigTypeError: "\u8A2D\u5B9A\u306E\u578B\u304C\u7121\u52B9\u3067\u3059\u3002S3\u30D7\u30ED\u30D0\u30A4\u30C0\u30FC\u306Ftype=s3\u306E\u307F\u306B\u5BFE\u5FDC\u3057\u3066\u3044\u307E\u3059",
|
|
66
|
+
storage_diskSpaceInsufficient: "\u30C7\u30A3\u30B9\u30AF\u306E\u7A7A\u304D\u5BB9\u91CF\u304C\u4E0D\u8DB3\u3057\u3066\u3044\u307E\u3059",
|
|
67
|
+
storage_localConfigTypeError: "\u8A2D\u5B9A\u306E\u578B\u304C\u7121\u52B9\u3067\u3059\u3002\u30ED\u30FC\u30AB\u30EB\u30D7\u30ED\u30D0\u30A4\u30C0\u30FC\u306Ftype=local\u306E\u307F\u306B\u5BFE\u5FDC\u3057\u3066\u3044\u307E\u3059",
|
|
68
|
+
storage_operationFailed: "\u64CD\u4F5C\u306B\u5931\u6557\u3057\u307E\u3057\u305F: {error}",
|
|
69
|
+
storage_unsupportedType: "\u672A\u5BFE\u5FDC\u306E\u30B9\u30C8\u30EC\u30FC\u30B8\u306E\u7A2E\u985E\u3067\u3059: {type}",
|
|
70
|
+
storage_config_bucketRequired: "\u30D0\u30B1\u30C3\u30C8\u306F\u5FC5\u9808\u3067\u3059",
|
|
71
|
+
storage_config_publicEndpointInvalid: "HTTP(S) \u306E\u30A2\u30AF\u30BB\u30B9\u5148\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044\u3002\u30D7\u30ED\u30AD\u30B7\u306E\u30D1\u30B9\u306B\u306F\u82F1\u6570\u5B57\u3001\u30A2\u30F3\u30C0\u30FC\u30B9\u30B3\u30A2\u3001\u30C9\u30C3\u30C8\u3001\u30CF\u30A4\u30D5\u30F3\u3092\u4F7F\u7528\u3067\u304D\u307E\u3059\u3002\u8A8D\u8A3C\u60C5\u5831\u3001\u30AF\u30A8\u30EA\u3001\u30D5\u30E9\u30B0\u30E1\u30F3\u30C8\u306F\u542B\u3081\u3089\u308C\u307E\u305B\u3093\u3002",
|
|
72
|
+
storage_config_regionRequired: "\u30EA\u30FC\u30B8\u30E7\u30F3\u306F\u5FC5\u9808\u3067\u3059",
|
|
73
|
+
storage_config_accessKeyIdRequired: "Access Key ID\u306F\u5FC5\u9808\u3067\u3059",
|
|
74
|
+
storage_config_secretAccessKeyRequired: "Secret Access Key\u306F\u5FC5\u9808\u3067\u3059",
|
|
75
|
+
storage_config_rootRequired: "\u30EB\u30FC\u30C8\u30D1\u30B9\u306F\u5FC5\u9808\u3067\u3059",
|
|
76
|
+
storage_fileNotFound: "\u30D5\u30A1\u30A4\u30EB\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093: {key}",
|
|
77
|
+
storage_permissionDenied: "\u30A2\u30AF\u30BB\u30B9\u6A29\u9650\u304C\u3042\u308A\u307E\u305B\u3093: {key}",
|
|
78
|
+
storage_pathIsDir: "\u30D1\u30B9\u306F\u30C7\u30A3\u30EC\u30AF\u30C8\u30EA\u3067\u3059: {key}",
|
|
79
|
+
storage_ioError: "\u5165\u51FA\u529B\u30A8\u30E9\u30FC: {error}",
|
|
80
|
+
storage_deleteManyFailed: "{count}\u500B\u306E\u30D5\u30A1\u30A4\u30EB\u306E\u524A\u9664\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
81
|
+
storage_s3ClientNotInitialized: "S3\u30AF\u30E9\u30A4\u30A2\u30F3\u30C8\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093",
|
|
82
|
+
storage_s3ConfigNotInitialized: "S3\u8A2D\u5B9A\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093",
|
|
83
|
+
storage_uploadCanceled: "\u30A2\u30C3\u30D7\u30ED\u30FC\u30C9\u3092\u30AD\u30E3\u30F3\u30BB\u30EB\u3057\u307E\u3057\u305F",
|
|
84
|
+
storage_downloadCanceled: "\u30C0\u30A6\u30F3\u30ED\u30FC\u30C9\u3092\u30AD\u30E3\u30F3\u30BB\u30EB\u3057\u307E\u3057\u305F",
|
|
85
|
+
storage_uploadFailedStatus: "\u30A2\u30C3\u30D7\u30ED\u30FC\u30C9\u306B\u5931\u6557\u3057\u307E\u3057\u305F\uFF08\u30B9\u30C6\u30FC\u30BF\u30B9{status}\uFF09: {statusText}",
|
|
86
|
+
storage_downloadFailedStatus: "\u30C0\u30A6\u30F3\u30ED\u30FC\u30C9\u306B\u5931\u6557\u3057\u307E\u3057\u305F\uFF08\u30B9\u30C6\u30FC\u30BF\u30B9{status}\uFF09: {statusText}",
|
|
87
|
+
storage_unknownError: "\u4E0D\u660E\u306A\u30A8\u30E9\u30FC",
|
|
88
|
+
storage_saveFileFailed: "\u30D5\u30A1\u30A4\u30EB\u306E\u4FDD\u5B58\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
89
|
+
storage_downloadDefaultFilename: "\u30C0\u30A6\u30F3\u30ED\u30FC\u30C9",
|
|
90
|
+
storage_pathTraversal: "\u30D1\u30B9\u30C8\u30E9\u30D0\u30FC\u30B5\u30EB\u653B\u6483\u3092\u691C\u51FA\u3057\u307E\u3057\u305F",
|
|
91
|
+
storage_localNotInitialized: "\u30ED\u30FC\u30AB\u30EB\u30B9\u30C8\u30EC\u30FC\u30B8\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093",
|
|
92
|
+
storage_configError: "\u30B9\u30C8\u30EC\u30FC\u30B8\u8A2D\u5B9A\u306E\u691C\u8A3C\u306B\u5931\u6557\u3057\u307E\u3057\u305F: {error}",
|
|
93
|
+
storage_metaFileAccess: "\u5185\u90E8\u30D5\u30A1\u30A4\u30EB\u3078\u306E\u76F4\u63A5\u30A2\u30AF\u30BB\u30B9\u306F\u8A31\u53EF\u3055\u308C\u3066\u3044\u307E\u305B\u3093: {key}",
|
|
94
|
+
storage_fileAlreadyExists: "\u30D5\u30A1\u30A4\u30EB\u306F\u65E2\u306B\u5B58\u5728\u3057\u307E\u3059: {key}",
|
|
95
|
+
storage_checksumFailed: "\u30C1\u30A7\u30C3\u30AF\u30B5\u30E0\u306E\u8A08\u7B97\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
96
|
+
storage_checksumInvalid: "\u30C1\u30A7\u30C3\u30AF\u30B5\u30E0\u307E\u305F\u306F\u30A2\u30EB\u30B4\u30EA\u30BA\u30E0\u304C\u7121\u52B9\u3067\u3059",
|
|
97
|
+
storage_streamInvalidChunk: "\u30D5\u30A1\u30A4\u30EB\u30B9\u30C8\u30EA\u30FC\u30E0\u306B\u306F\u30D0\u30A4\u30C8\u30C1\u30E3\u30F3\u30AF\u304C\u5FC5\u8981\u3067\u3059",
|
|
98
|
+
storage_sharedBackendRequired: "\u672C\u756A\u74B0\u5883\u306B\u306F\u5171\u6709\u30D0\u30C3\u30AF\u30A8\u30F3\u30C9\u304C\u5FC5\u8981\u3067\u3059\u3002\u30ED\u30FC\u30AB\u30EB\u30D5\u30A1\u30A4\u30EB\u3068\u30E1\u30E2\u30EA\u30FC\u30D0\u30C3\u30AF\u30A8\u30F3\u30C9\u306F\u958B\u767A\u74B0\u5883\u307E\u305F\u306F\u660E\u793A\u7684\u306A E2E \u30C6\u30B9\u30C8\u3067\u306E\u307F\u4F7F\u7528\u3067\u304D\u307E\u3059\u3002"
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// messages/zh-CN.json
|
|
102
|
+
var zh_CN_default = {
|
|
103
|
+
$schema: "https://inlang.com/schema/inlang-message-format",
|
|
104
|
+
storage_notInitialized: "\u5B58\u50A8\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 storage.init()",
|
|
105
|
+
storage_bucketNotExist: "\u5B58\u50A8\u6876\u4E0D\u5B58\u5728",
|
|
106
|
+
storage_networkError: "\u7F51\u7EDC\u9519\u8BEF",
|
|
107
|
+
storage_responseBodyEmpty: "\u54CD\u5E94\u4F53\u4E3A\u7A7A",
|
|
108
|
+
storage_presignUrlFailed: "\u751F\u6210\u7B7E\u540D URL \u5931\u8D25",
|
|
109
|
+
storage_presignUploadUrlFailed: "\u751F\u6210\u4E0A\u4F20\u7B7E\u540D URL \u5931\u8D25",
|
|
110
|
+
storage_localPresignUnsupported: "\u672C\u5730\u5B58\u50A8\u4E0D\u652F\u6301\u9884\u7B7E\u540D URL\uFF0C\u8BF7\u901A\u8FC7\u5E94\u7528\u7684\u9274\u6743\u6587\u4EF6\u63A5\u53E3\u4E0A\u4F20\u6216\u4E0B\u8F7D",
|
|
111
|
+
storage_listOptionsInvalid: "\u5217\u8868\u53C2\u6570\u65E0\u6548\uFF1AmaxKeys \u5FC5\u987B\u4E3A 1 \u5230 1000 \u7684\u6574\u6570\uFF0Cprefix \u548C delimiter \u5FC5\u987B\u4E3A\u6709\u6548\u7684 Unicode \u5B57\u7B26\u4E32",
|
|
112
|
+
storage_listCursorInvalid: "\u5217\u8868\u6E38\u6807\u65E0\u6548\u6216\u4E0E\u5F53\u524D\u6839\u76EE\u5F55\u3001\u524D\u7F00\u3001\u5206\u9694\u7B26\u4E0D\u5339\u914D",
|
|
113
|
+
storage_s3ConfigTypeError: "\u914D\u7F6E\u7C7B\u578B\u9519\u8BEF\uFF1AS3 provider \u4EC5\u652F\u6301 type=s3",
|
|
114
|
+
storage_diskSpaceInsufficient: "\u78C1\u76D8\u7A7A\u95F4\u4E0D\u8DB3",
|
|
115
|
+
storage_localConfigTypeError: "\u914D\u7F6E\u7C7B\u578B\u9519\u8BEF\uFF1ALocal provider \u4EC5\u652F\u6301 type=local",
|
|
116
|
+
storage_operationFailed: "\u64CD\u4F5C\u5931\u8D25: {error}",
|
|
117
|
+
storage_unsupportedType: "\u4E0D\u652F\u6301\u7684\u5B58\u50A8\u7C7B\u578B\uFF1A{type}",
|
|
118
|
+
storage_config_bucketRequired: "\u5B58\u50A8\u6876\u4E0D\u80FD\u4E3A\u7A7A",
|
|
119
|
+
storage_config_publicEndpointInvalid: "\u9884\u7B7E\u540D\u8BBF\u95EE\u5730\u5740\u5FC5\u987B\u662F HTTP(S) \u5730\u5740\uFF0C\u53EF\u5305\u542B\u4EE3\u7406\u76EE\u5F55\uFF1B\u76EE\u5F55\u4EC5\u4F7F\u7528\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u3001\u70B9\u548C\u8FDE\u5B57\u7B26\uFF0C\u4E0D\u80FD\u5305\u542B\u51ED\u636E\u3001\u67E5\u8BE2\u53C2\u6570\u6216\u7247\u6BB5\u3002",
|
|
120
|
+
storage_config_regionRequired: "\u533A\u57DF\u4E0D\u80FD\u4E3A\u7A7A",
|
|
121
|
+
storage_config_accessKeyIdRequired: "Access Key ID \u4E0D\u80FD\u4E3A\u7A7A",
|
|
122
|
+
storage_config_secretAccessKeyRequired: "Secret Access Key \u4E0D\u80FD\u4E3A\u7A7A",
|
|
123
|
+
storage_config_rootRequired: "Root \u4E0D\u80FD\u4E3A\u7A7A",
|
|
124
|
+
storage_fileNotFound: "\u6587\u4EF6\u4E0D\u5B58\u5728: {key}",
|
|
125
|
+
storage_permissionDenied: "\u6743\u9650\u4E0D\u8DB3: {key}",
|
|
126
|
+
storage_pathIsDir: "\u8DEF\u5F84\u662F\u76EE\u5F55: {key}",
|
|
127
|
+
storage_ioError: "IO \u9519\u8BEF: {error}",
|
|
128
|
+
storage_deleteManyFailed: "\u5220\u9664 {count} \u4E2A\u6587\u4EF6\u5931\u8D25",
|
|
129
|
+
storage_s3ClientNotInitialized: "S3 \u5BA2\u6237\u7AEF\u672A\u521D\u59CB\u5316",
|
|
130
|
+
storage_s3ConfigNotInitialized: "S3 \u914D\u7F6E\u672A\u521D\u59CB\u5316",
|
|
131
|
+
storage_uploadCanceled: "\u4E0A\u4F20\u5DF2\u53D6\u6D88",
|
|
132
|
+
storage_downloadCanceled: "\u4E0B\u8F7D\u5DF2\u53D6\u6D88",
|
|
133
|
+
storage_uploadFailedStatus: "\u4E0A\u4F20\u5931\u8D25\uFF08\u72B6\u6001\u7801 {status}\uFF09\uFF1A{statusText}",
|
|
134
|
+
storage_downloadFailedStatus: "\u4E0B\u8F7D\u5931\u8D25\uFF08\u72B6\u6001\u7801 {status}\uFF09\uFF1A{statusText}",
|
|
135
|
+
storage_unknownError: "\u672A\u77E5\u9519\u8BEF",
|
|
136
|
+
storage_saveFileFailed: "\u4FDD\u5B58\u6587\u4EF6\u5931\u8D25",
|
|
137
|
+
storage_downloadDefaultFilename: "\u4E0B\u8F7D",
|
|
138
|
+
storage_pathTraversal: "\u68C0\u6D4B\u5230\u8DEF\u5F84\u7A7F\u8D8A\u653B\u51FB",
|
|
139
|
+
storage_localNotInitialized: "\u672C\u5730\u5B58\u50A8\u672A\u521D\u59CB\u5316",
|
|
140
|
+
storage_configError: "\u5B58\u50A8\u914D\u7F6E\u6821\u9A8C\u5931\u8D25\uFF1A{error}",
|
|
141
|
+
storage_metaFileAccess: "\u4E0D\u5141\u8BB8\u76F4\u63A5\u8BBF\u95EE\u5185\u90E8\u6587\u4EF6\uFF1A{key}",
|
|
142
|
+
storage_fileAlreadyExists: "\u6587\u4EF6\u5DF2\u5B58\u5728\uFF1A{key}",
|
|
143
|
+
storage_checksumFailed: "\u8BA1\u7B97\u6821\u9A8C\u548C\u5931\u8D25",
|
|
144
|
+
storage_checksumInvalid: "\u6821\u9A8C\u548C\u6216\u7B97\u6CD5\u4E0D\u5408\u6CD5",
|
|
145
|
+
storage_streamInvalidChunk: "\u6587\u4EF6\u6D41\u5FC5\u987B\u5305\u542B\u5B57\u8282\u5757",
|
|
146
|
+
storage_sharedBackendRequired: "\u751F\u4EA7\u6A21\u5F0F\u9700\u8981\u8DE8\u8282\u70B9\u5171\u4EAB\u540E\u7AEF\uFF1B\u672C\u673A\u6587\u4EF6\u6216\u5185\u5B58\u540E\u7AEF\u4EC5\u7528\u4E8E\u5F00\u53D1\u548C\u663E\u5F0F\u7AEF\u5230\u7AEF\u6D4B\u8BD5\u3002"
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// src/storage-i18n.ts
|
|
150
|
+
var storageM = core.i18n.createMessageGetter({
|
|
151
|
+
"zh-CN": zh_CN_default,
|
|
152
|
+
"en-US": en_US_default,
|
|
153
|
+
"ja-JP": ja_JP_default
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// src/storage-mime.ts
|
|
157
|
+
var MIME_TYPES = {
|
|
158
|
+
// 图片
|
|
159
|
+
"jpg": "image/jpeg",
|
|
160
|
+
"jpeg": "image/jpeg",
|
|
161
|
+
"png": "image/png",
|
|
162
|
+
"gif": "image/gif",
|
|
163
|
+
"webp": "image/webp",
|
|
164
|
+
"svg": "image/svg+xml",
|
|
165
|
+
"ico": "image/x-icon",
|
|
166
|
+
// 文档
|
|
167
|
+
"pdf": "application/pdf",
|
|
168
|
+
"doc": "application/msword",
|
|
169
|
+
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
170
|
+
"xls": "application/vnd.ms-excel",
|
|
171
|
+
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
172
|
+
"ppt": "application/vnd.ms-powerpoint",
|
|
173
|
+
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
174
|
+
// 文本
|
|
175
|
+
"txt": "text/plain",
|
|
176
|
+
"html": "text/html",
|
|
177
|
+
"css": "text/css",
|
|
178
|
+
"js": "text/javascript",
|
|
179
|
+
"json": "application/json",
|
|
180
|
+
"xml": "application/xml",
|
|
181
|
+
"csv": "text/csv",
|
|
182
|
+
"md": "text/markdown",
|
|
183
|
+
// 音视频
|
|
184
|
+
"mp3": "audio/mpeg",
|
|
185
|
+
"wav": "audio/wav",
|
|
186
|
+
"mp4": "video/mp4",
|
|
187
|
+
"webm": "video/webm",
|
|
188
|
+
"ogg": "audio/ogg",
|
|
189
|
+
// 压缩包
|
|
190
|
+
"zip": "application/zip",
|
|
191
|
+
"rar": "application/x-rar-compressed",
|
|
192
|
+
"7z": "application/x-7z-compressed",
|
|
193
|
+
"tar": "application/x-tar",
|
|
194
|
+
"gz": "application/gzip",
|
|
195
|
+
// 字体
|
|
196
|
+
"woff": "font/woff",
|
|
197
|
+
"woff2": "font/woff2",
|
|
198
|
+
"ttf": "font/ttf",
|
|
199
|
+
"otf": "font/otf"
|
|
200
|
+
};
|
|
201
|
+
var MIME_TYPE_DEFAULT = "application/octet-stream";
|
|
202
|
+
|
|
203
|
+
// src/client/storage-client.ts
|
|
204
|
+
async function uploadWithPresignedUrl(url, data, options = {}) {
|
|
205
|
+
try {
|
|
206
|
+
const signal = options.abortController?.signal;
|
|
207
|
+
if (signal?.aborted)
|
|
208
|
+
return { success: false, error: storageM("storage_uploadCanceled") };
|
|
209
|
+
const body = data;
|
|
210
|
+
if (options.onProgress) {
|
|
211
|
+
const xhr = new XMLHttpRequest();
|
|
212
|
+
return new Promise((resolve) => {
|
|
213
|
+
let settled = false;
|
|
214
|
+
function finish(result) {
|
|
215
|
+
if (settled)
|
|
216
|
+
return;
|
|
217
|
+
settled = true;
|
|
218
|
+
signal?.removeEventListener("abort", onCancel);
|
|
219
|
+
xhr.upload.removeEventListener("progress", onProgress);
|
|
220
|
+
xhr.removeEventListener("load", onLoad);
|
|
221
|
+
xhr.removeEventListener("error", onError);
|
|
222
|
+
xhr.removeEventListener("abort", onAbort);
|
|
223
|
+
xhr.removeEventListener("timeout", onError);
|
|
224
|
+
resolve(result);
|
|
225
|
+
}
|
|
226
|
+
function onAbort() {
|
|
227
|
+
finish({ success: false, error: storageM("storage_uploadCanceled") });
|
|
228
|
+
}
|
|
229
|
+
function onCancel() {
|
|
230
|
+
onAbort();
|
|
231
|
+
xhr.abort();
|
|
232
|
+
}
|
|
233
|
+
function onError() {
|
|
234
|
+
finish({ success: false, error: storageM("storage_networkError") });
|
|
235
|
+
}
|
|
236
|
+
function onLoad() {
|
|
237
|
+
finish(xhr.status >= 200 && xhr.status < 300 ? { success: true } : { success: false, error: storageM("storage_uploadFailedStatus", { params: { status: xhr.status, statusText: xhr.statusText } }) });
|
|
238
|
+
}
|
|
239
|
+
function onProgress(event) {
|
|
240
|
+
if (settled || !event.lengthComputable)
|
|
241
|
+
return;
|
|
242
|
+
try {
|
|
243
|
+
options.onProgress?.({ loaded: event.loaded, total: event.total, percent: Math.round(event.loaded / event.total * 100) });
|
|
244
|
+
} catch (error) {
|
|
245
|
+
finish({ success: false, error: error instanceof Error ? error.message : storageM("storage_unknownError") });
|
|
246
|
+
xhr.abort();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
xhr.upload.addEventListener("progress", onProgress);
|
|
251
|
+
xhr.addEventListener("load", onLoad);
|
|
252
|
+
xhr.addEventListener("error", onError);
|
|
253
|
+
xhr.addEventListener("abort", onAbort);
|
|
254
|
+
xhr.addEventListener("timeout", onError);
|
|
255
|
+
signal?.addEventListener("abort", onCancel, { once: true });
|
|
256
|
+
xhr.open("PUT", url);
|
|
257
|
+
if (settled)
|
|
258
|
+
return;
|
|
259
|
+
if (options.contentType)
|
|
260
|
+
xhr.setRequestHeader("Content-Type", options.contentType);
|
|
261
|
+
if (signal?.aborted) {
|
|
262
|
+
onCancel();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
xhr.send(body);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
finish({ success: false, error: error instanceof Error ? error.message : storageM("storage_unknownError") });
|
|
268
|
+
xhr.abort();
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
const response = await fetch(url, {
|
|
273
|
+
method: "PUT",
|
|
274
|
+
body,
|
|
275
|
+
headers: options.contentType ? { "Content-Type": options.contentType } : void 0,
|
|
276
|
+
signal
|
|
277
|
+
});
|
|
278
|
+
if (!response.ok) {
|
|
279
|
+
return {
|
|
280
|
+
success: false,
|
|
281
|
+
error: storageM("storage_uploadFailedStatus", { params: { status: response.status, statusText: response.statusText } })
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
return { success: true };
|
|
285
|
+
} catch (error) {
|
|
286
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
287
|
+
return { success: false, error: storageM("storage_uploadCanceled") };
|
|
288
|
+
}
|
|
289
|
+
return {
|
|
290
|
+
success: false,
|
|
291
|
+
error: error instanceof Error ? error.message : storageM("storage_unknownError")
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
async function downloadWithPresignedUrl(url, options = {}) {
|
|
296
|
+
try {
|
|
297
|
+
const response = await fetch(url, {
|
|
298
|
+
method: "GET",
|
|
299
|
+
signal: options.abortController?.signal
|
|
300
|
+
});
|
|
301
|
+
if (!response.ok) {
|
|
302
|
+
return {
|
|
303
|
+
success: false,
|
|
304
|
+
error: storageM("storage_downloadFailedStatus", { params: { status: response.status, statusText: response.statusText } })
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
const blob = await response.blob();
|
|
308
|
+
return { success: true, data: blob };
|
|
309
|
+
} catch (error) {
|
|
310
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
311
|
+
return { success: false, error: storageM("storage_downloadCanceled") };
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
success: false,
|
|
315
|
+
error: error instanceof Error ? error.message : storageM("storage_unknownError")
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
async function downloadAndSave(url, options = {}) {
|
|
320
|
+
const result = await downloadWithPresignedUrl(url, options);
|
|
321
|
+
if (!result.success || !result.data) {
|
|
322
|
+
return { success: false, error: result.error };
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
const blobUrl = URL.createObjectURL(result.data);
|
|
326
|
+
const link = document.createElement("a");
|
|
327
|
+
link.href = blobUrl;
|
|
328
|
+
link.download = options.filename || storageM("storage_downloadDefaultFilename");
|
|
329
|
+
document.body.appendChild(link);
|
|
330
|
+
link.click();
|
|
331
|
+
document.body.removeChild(link);
|
|
332
|
+
URL.revokeObjectURL(blobUrl);
|
|
333
|
+
return { success: true };
|
|
334
|
+
} catch (error) {
|
|
335
|
+
return {
|
|
336
|
+
success: false,
|
|
337
|
+
error: error instanceof Error ? error.message : storageM("storage_saveFileFailed")
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
function getFileExtension(file) {
|
|
342
|
+
const name = file.name;
|
|
343
|
+
const lastDot = name.lastIndexOf(".");
|
|
344
|
+
return lastDot > -1 ? name.slice(lastDot + 1).toLowerCase() : "";
|
|
345
|
+
}
|
|
346
|
+
function getMimeType(extension) {
|
|
347
|
+
return MIME_TYPES[extension.toLowerCase()] || MIME_TYPE_DEFAULT;
|
|
348
|
+
}
|
|
349
|
+
function formatFileSize(bytes) {
|
|
350
|
+
if (bytes === 0)
|
|
351
|
+
return "0 B";
|
|
352
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
353
|
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
354
|
+
const size = bytes / 1024 ** i;
|
|
355
|
+
return `${size.toFixed(i > 0 ? 2 : 0)} ${units[i]}`;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export { MIME_TYPES, MIME_TYPE_DEFAULT, downloadAndSave, downloadWithPresignedUrl, formatFileSize, getFileExtension, getMimeType, storageM, uploadWithPresignedUrl };
|
|
359
|
+
//# sourceMappingURL=chunk-TLZY4IP6.js.map
|
|
360
|
+
//# sourceMappingURL=chunk-TLZY4IP6.js.map
|