@be-link/cos 1.12.3 → 1.12.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.
Files changed (2) hide show
  1. package/bin/upload.js +23 -72
  2. package/package.json +1 -1
package/bin/upload.js CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
4
  const fs = require('fs');
5
5
  const path = require('path');
6
- const https = require('https');
7
6
  const COS = require('cos-nodejs-sdk-v5');
8
7
  const { getBucketConfig, getRegion } = require('../dist/index.cjs.js');
9
8
 
@@ -55,80 +54,32 @@ let cos = null;
55
54
  /**
56
55
  * 通过云函数获取临时密钥(参照浏览器端实现)
57
56
  */
58
- function getTempCredentials(mode) {
59
- return new Promise((resolve, reject) => {
60
- console.log('🔐 环境模式:', mode);
61
- const url =
62
- mode === 'production'
63
- ? 'https://shield-60660-10-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret'
64
- : 'https://shield-74680-5-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret';
65
-
66
- const urlObj = new URL(url);
67
- const postData = JSON.stringify({});
68
-
69
- const options = {
70
- hostname: urlObj.hostname,
71
- path: urlObj.pathname,
72
- method: 'POST',
73
- headers: {
74
- 'Content-Type': 'application/json',
75
- 'Content-Length': Buffer.byteLength(postData),
76
- },
77
- };
78
-
79
- console.log('🔐 请求 URL:', url);
80
- console.log('🔐 请求选项:', options);
81
-
82
- const req = https.request(options, (res) => {
83
- let data = '';
84
-
85
- console.log('🔐 响应状态码:', res.statusCode);
86
- console.log('🔐 响应头:', res.headers);
87
-
88
- res.on('data', (chunk) => {
89
- data += chunk;
90
- });
91
-
92
- res.on('end', () => {
93
- console.log('🔐 原始响应数据:', data);
94
-
95
- try {
96
- const result = JSON.parse(data);
97
- console.log('🔐 解析后的响应:', JSON.stringify(result, null, 2));
98
-
99
- const credentials = result?.data?.credentials;
57
+ async function getTempCredentials(mode) {
58
+ const url =
59
+ mode === 'production'
60
+ ? 'https://shield-60660-10-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret'
61
+ : 'https://shield-74680-5-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret';
100
62
 
101
- if (!credentials) {
102
- console.error('❌ 返回数据结构:', result);
103
- reject(new Error('获取临时密钥失败:返回数据格式错误'));
104
- return;
105
- }
106
-
107
- console.log('✅ 密钥信息获取成功');
108
- resolve({
109
- TmpSecretId: credentials.tmpSecretId,
110
- TmpSecretKey: credentials.tmpSecretKey,
111
- SecurityToken: credentials.sessionToken,
112
- StartTime: result.data.startTime,
113
- ExpiredTime: result.data.expiredTime,
114
- });
115
- } catch (err) {
116
- console.error('❌ JSON 解析失败:', err.message);
117
- console.error('❌ 原始数据:', data);
118
- reject(new Error('解析临时密钥响应失败:' + err.message));
119
- }
120
- });
121
- });
63
+ try {
64
+ const response = await fetch(url, { method: 'POST' });
65
+ const result = await response.json();
66
+ const data = result?.data || {};
67
+ const credentials = data?.credentials;
122
68
 
123
- req.on('error', (err) => {
124
- console.error('❌ 请求错误:', err);
125
- reject(new Error('请求临时密钥失败:' + err.message));
126
- });
69
+ if (!credentials) {
70
+ throw new Error('获取临时密钥失败:返回数据格式错误');
71
+ }
127
72
 
128
- // 发送请求体
129
- req.write(postData);
130
- req.end();
131
- });
73
+ return {
74
+ TmpSecretId: credentials.tmpSecretId,
75
+ TmpSecretKey: credentials.tmpSecretKey,
76
+ SecurityToken: credentials.sessionToken,
77
+ StartTime: data.startTime,
78
+ ExpiredTime: data.expiredTime,
79
+ };
80
+ } catch (error) {
81
+ throw new Error('获取临时密钥失败:' + error.message);
82
+ }
132
83
  }
133
84
 
134
85
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@be-link/cos",
3
- "version": "1.12.3",
3
+ "version": "1.12.5",
4
4
  "description": "前端项目产物上传cos",
5
5
  "homepage": "https://github.com/snowmountain-top/be-link#readme",
6
6
  "author": "zhuiyi",