@filebox/s3-server 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/responses.js DELETED
@@ -1,193 +0,0 @@
1
- const { Builder } = require("xml2js");
2
- const crypto = require("crypto");
3
-
4
- /**
5
- * S3 XML响应生成器
6
- */
7
- class Responses {
8
- constructor() {
9
- this.xmlBuilder = new Builder({
10
- xmldec: {
11
- version: "1.0",
12
- encoding: "utf-8",
13
- },
14
- });
15
- }
16
-
17
- /**
18
- * 发送XML响应
19
- * @param {object} res - Express响应对象
20
- * @param {object} xmlObject - 要转换为XML的对象
21
- * @param {number} status - HTTP状态码
22
- */
23
- async sendXmlResponse(res, xmlObject, status = 200) {
24
- if (res.headersSent) {
25
- return;
26
- }
27
-
28
- const response = this.xmlBuilder.buildObject(xmlObject);
29
-
30
- res.set("Content-Type", "application/xml; charset=utf-8");
31
- res.set("Content-Length", Buffer.byteLength(response).toString());
32
- res.set("Date", new Date().toUTCString());
33
- res.status(status);
34
-
35
- return new Promise((resolve) => {
36
- res.end(response, () => {
37
- resolve();
38
- });
39
- });
40
- }
41
-
42
- /**
43
- * 发送错误响应
44
- * @param {object} res - Express响应对象
45
- * @param {number} status - HTTP状态码
46
- * @param {string} code - 错误代码
47
- * @param {string} message - 错误消息
48
- */
49
- async error(res, status, code, message) {
50
- return this.sendXmlResponse(
51
- res,
52
- {
53
- Error: {
54
- Code: code,
55
- Message: message,
56
- },
57
- },
58
- status,
59
- );
60
- }
61
-
62
- /**
63
- * 列出所有存储桶
64
- * @param {object} res - Express响应对象
65
- * @param {Array} buckets - 存储桶列表
66
- * @param {object} owner - 所有者信息
67
- */
68
- async listBuckets(res, buckets, owner) {
69
- return this.sendXmlResponse(res, {
70
- ListAllMyBucketsResult: {
71
- $: { xmlns: "http://s3.amazonaws.com/doc/2006-03-01/" },
72
- Buckets: {
73
- Bucket: buckets.map((bucket) => ({
74
- CreationDate: new Date(bucket.creationDate).toISOString(),
75
- Name: bucket.name,
76
- })),
77
- },
78
- Owner: {
79
- ID: crypto.createHash("sha256").update(owner.id).digest("hex"),
80
- DisplayName: owner.displayName || "",
81
- },
82
- },
83
- });
84
- }
85
-
86
- /**
87
- * 列出对象(V2)
88
- * @param {object} res - Express响应对象
89
- * @param {string} prefix - 前缀
90
- * @param {Array} objects - 对象列表
91
- * @param {Array} commonPrefixes - 公共前缀
92
- * @param {string} bucket - 存储桶名称
93
- * @param {string} delimiter - 分隔符
94
- */
95
- async listObjectsV2(res, prefix, objects, commonPrefixes, bucket, delimiter) {
96
- return this.sendXmlResponse(res, {
97
- ListBucketResult: {
98
- IsTruncated: false,
99
- Contents: objects.map((object) => ({
100
- Key: object.path,
101
- LastModified: new Date(object.mtimeMs).toISOString(),
102
- Size: object.size.toString(),
103
- ETag: `"${object.uuid || crypto.randomUUID()}"`,
104
- StorageClass: "STANDARD",
105
- })),
106
- CommonPrefixes: commonPrefixes.map((prefix) => ({
107
- Prefix: prefix,
108
- })),
109
- KeyCount: objects.length.toString(),
110
- Prefix: prefix,
111
- Delimiter: delimiter,
112
- MaxKeys: 1000,
113
- Name: bucket,
114
- },
115
- });
116
- }
117
-
118
- /**
119
- * 删除对象响应
120
- * @param {object} res - Express响应对象
121
- * @param {Array} deleted - 已删除的对象
122
- * @param {Array} errors - 错误信息
123
- */
124
- async deleteObjects(res, deleted, errors) {
125
- return this.sendXmlResponse(res, {
126
- DeleteResult: {
127
- Deleted: deleted.map((del) => ({
128
- Key: del.Key,
129
- })),
130
- Error: errors.map((error) => ({
131
- Key: error.Key,
132
- Code: error.Code,
133
- Message: error.Message,
134
- })),
135
- },
136
- });
137
- }
138
-
139
- /**
140
- * 获取存储桶位置
141
- * @param {object} res - Express响应对象
142
- * @param {string} region - 区域
143
- */
144
- async getBucketLocation(res, region) {
145
- return this.sendXmlResponse(res, {
146
- LocationConstraint: region || "",
147
- });
148
- }
149
-
150
- /**
151
- * 成功响应(200 OK)
152
- * @param {object} res - Express响应对象
153
- */
154
- async ok(res) {
155
- if (res.headersSent) {
156
- return;
157
- }
158
-
159
- res.set("Content-Type", "application/xml; charset=utf-8");
160
- res.set("Content-Length", "0");
161
- res.set("Date", new Date().toUTCString());
162
- res.status(200);
163
-
164
- return new Promise((resolve) => {
165
- res.end("", () => {
166
- resolve();
167
- });
168
- });
169
- }
170
-
171
- /**
172
- * 无内容响应(204 No Content)
173
- * @param {object} res - Express响应对象
174
- */
175
- async noContent(res) {
176
- if (res.headersSent) {
177
- return;
178
- }
179
-
180
- res.set("Content-Type", "application/xml; charset=utf-8");
181
- res.set("Content-Length", "0");
182
- res.set("Date", new Date().toUTCString());
183
- res.status(204);
184
-
185
- return new Promise((resolve) => {
186
- res.end("", () => {
187
- resolve();
188
- });
189
- });
190
- }
191
- }
192
-
193
- module.exports = new Responses();