fsd-oss 0.11.1 → 0.12.0

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/index.d.ts CHANGED
@@ -50,9 +50,23 @@ export interface UploadTokenWithAutoRefresh {
50
50
  }
51
51
 
52
52
  export default class OSSAdpter extends Adapter<OSSAdapterOptions> {
53
- createUploadToken: (videoId: string, meta?: any) => Promise<UploadToken>;
53
+ /**
54
+ * 创建上传凭证
55
+ * @param {string} path 文件路径
56
+ * @param {any} [meta] 文件元信息
57
+ * @param {number} [durationSeconds] 上传凭证有效期,单位秒, 默认 3600
58
+ */
59
+ createUploadToken: (path: string, meta?: any, durationSeconds?: number) => Promise<UploadToken>;
60
+
61
+ /**
62
+ * 创建带自动刷新的上传凭证
63
+ * @param {string} path 文件路径
64
+ * @param {any} [meta] 文件元信息
65
+ * @param {number} [durationSeconds] 上传凭证有效期,单位秒, 默认 3600
66
+ */
54
67
  createUploadTokenWithAutoRefresh: (
55
- videoId: string,
56
- meta?: any
68
+ path: string,
69
+ meta?: any,
70
+ durationSeconds?: number
57
71
  ) => Promise<UploadTokenWithAutoRefresh>;
58
72
  }
package/lib/index.js CHANGED
@@ -47,7 +47,7 @@ class OSSAdapter {
47
47
  apiVersion: '2015-04-01'
48
48
  });
49
49
  }
50
- this.createUploadToken = async (path, meta) => {
50
+ this.createUploadToken = async (path, meta, durationSeconds) => {
51
51
  if (!options.accountId || !options.roleName)
52
52
  throw new Error('Can not create sts token, missing options: accountId and roleName!');
53
53
  path = slash(Path.join(options.root, path)).substring(1);
@@ -64,7 +64,7 @@ class OSSAdapter {
64
64
  }
65
65
  ]
66
66
  }),
67
- DurationSeconds: 3600
67
+ DurationSeconds: durationSeconds || 3600
68
68
  };
69
69
  let result = await this._rpc.request('AssumeRole', params, { method: 'POST' });
70
70
  if (result.Message)
@@ -93,12 +93,12 @@ class OSSAdapter {
93
93
  }
94
94
  return token;
95
95
  };
96
- this.createUploadTokenWithAutoRefresh = async (videoId, meta) => {
97
- let token = await this.createUploadToken(videoId, meta);
96
+ this.createUploadTokenWithAutoRefresh = async (path, meta, durationSeconds) => {
97
+ let token = await this.createUploadToken(path, meta, durationSeconds);
98
98
  let auth = token.auth;
99
99
  auth = Object.assign({}, auth, {
100
100
  refreshSTSToken: async () => {
101
- let t = await this.createUploadToken(videoId, meta);
101
+ let t = await this.createUploadToken(path, meta, durationSeconds);
102
102
  return t.auth;
103
103
  }
104
104
  });
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const qs = require("qs");
4
- const xmljs = require("xml-js");
4
+ const xml2js = require("xml2js");
5
5
  const sha1 = require("crypto-js/hmac-sha1");
6
6
  const md5 = require("crypto-js/md5");
7
7
  const Base64Encoder = require("crypto-js/enc-base64");
@@ -41,7 +41,6 @@ class SimpleOSSClient {
41
41
  options.headers = {};
42
42
  options.headers['x-oss-copy-source'] = `/${this.config.bucket}/${from}`;
43
43
  let res = await this.requestData('PUT', to, null, options);
44
- res = Object.assign({ headers: res.headers }, res.CopyObjectResult);
45
44
  return res;
46
45
  }
47
46
  async head(name, options) {
@@ -74,7 +73,6 @@ class SimpleOSSClient {
74
73
  query['fetch-owner'] = options.fetchOwner;
75
74
  options.query = query;
76
75
  let res = await this.requestData('GET', '', null, options);
77
- res = Object.assign({ headers: res.headers }, res.ListBucketResult);
78
76
  res.KeyCount = parseInt(res.KeyCount);
79
77
  res.MaxKeys = parseInt(res.MaxKeys);
80
78
  res.IsTruncated = res.IsTruncated === 'true';
@@ -103,7 +101,6 @@ class SimpleOSSClient {
103
101
  options.headers = {};
104
102
  options.headers['Content-MD5'] = md5(body).toString(Base64Encoder);
105
103
  let res = await this.requestData('POST', '?delete', body, options);
106
- res = Object.assign({ headers: res.headers }, res.DeleteResult);
107
104
  if (res.Deleted) {
108
105
  if (!Array.isArray(res.Deleted))
109
106
  res.Deleted = [res.Deleted];
@@ -118,8 +115,7 @@ class SimpleOSSClient {
118
115
  options = options || {};
119
116
  if (!options.mime)
120
117
  options.mime = mime.lookup(name) || 'application/octet-stream';
121
- let res = await this.requestData('POST', `${name}?uploads`, '', options);
122
- return Object.assign({ headers: res.headers }, res.InitiateMultipartUploadResult);
118
+ return await this.requestData('POST', `${name}?uploads`, '', options);
123
119
  }
124
120
  uploadPart(name, uploadId, partNumber, body, options) {
125
121
  options = options || {};
@@ -134,7 +130,7 @@ class SimpleOSSClient {
134
130
  .map((part) => `<Part><PartNumber>${part.number}</PartNumber><ETag>${part.etag}</ETag></Part>`)
135
131
  .join('')}</CompleteMultipartUpload>`;
136
132
  let res = await this.requestData('POST', `${name}?uploadId=${uploadId}`, body, options);
137
- return Object.assign({ headers: res.headers }, res.CompleteMultipartUploadResult);
133
+ return res;
138
134
  }
139
135
  request(method, resource, body, options) {
140
136
  options = options || {};
@@ -164,17 +160,16 @@ class SimpleOSSClient {
164
160
  async requestData(method, resource, body, options) {
165
161
  let response = await this.request(method, resource, body, options).response();
166
162
  let xml = await response.text();
167
- if (!xml && response.status === 200) {
163
+ if (!xml && (response.status === 200 || response.status === 204)) {
168
164
  return {
169
165
  headers: response.headers
170
166
  };
171
167
  }
172
- let data = xmljs.xml2js(xml, { compact: true });
173
- if (data.Error) {
174
- throw new Error(data.Error.Message._text);
168
+ let data = await xml2data(xml);
169
+ if (data.Code) {
170
+ throw new Error(data.Message);
175
171
  }
176
- let res = Object.assign({ headers: response.headers }, conventXmlObject(data));
177
- delete res._declaration;
172
+ let res = Object.assign({ headers: response.headers }, data);
178
173
  return res;
179
174
  }
180
175
  signatureUrl(name, options) {
@@ -222,18 +217,14 @@ class SimpleOSSClient {
222
217
  }
223
218
  }
224
219
  exports.default = SimpleOSSClient;
225
- function conventXmlObject(object) {
226
- if (Array.isArray(object))
227
- return object.map(conventXmlObject);
228
- if (object && typeof object === 'object') {
229
- let keys = Object.keys(object);
230
- if (keys.length === 1 && keys[0] === '_text')
231
- return object._text;
232
- let res = {};
233
- for (let key of keys) {
234
- res[key] = conventXmlObject(object[key]);
235
- }
236
- return res;
237
- }
238
- return object;
220
+ function xml2data(xml) {
221
+ return new Promise((resolve, reject) => {
222
+ const opt = { trim: true, explicitArray: false, explicitRoot: false };
223
+ xml2js.parseString(xml, opt, (error, result) => {
224
+ if (error) {
225
+ return reject(new Error('XMLDataError'));
226
+ }
227
+ resolve(result);
228
+ });
229
+ });
239
230
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fsd-oss",
3
- "version": "0.11.1",
3
+ "version": "0.12.0",
4
4
  "description": "Aliyun OSS adapter for fsd",
5
5
  "main": "lib/index.js",
6
6
  "types": "index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "mime-types": "^2.1.35",
21
21
  "minimatch": "^3.1.2",
22
22
  "slash": "^3.0.0",
23
- "xml-js": "^1.6.11"
23
+ "xml2js": "^0.5.0"
24
24
  },
25
- "gitHead": "5f39a56e4f5ae28e6f15c7888413561a924b54de"
25
+ "gitHead": "ad01afd0117496a9b857e566a404f0ececbd723d"
26
26
  }