Skip to content

Commit e44ca6c

Browse files
authored
Merge pull request #219 from luoyeshu0507/master
fix some bugs and release v1.0.19
2 parents 219e0a6 + 912428c commit e44ca6c

File tree

15 files changed

+472
-25
lines changed

15 files changed

+472
-25
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
node_modules
33
bower_components
44
demo/config.js
5-
demo/deploy.sh
5+
deploy.sh

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Qiniu-JavaScript-SDK 的示例 Demo 中的服务器端部分是基于[ Node.js
9191
- 在使用 JS-SDK 之前,您必须先注册一个七牛帐号,并登录控制台获取一对有效的 AccessKey 和 SecretKey,您可以阅读[ 快速入门 ](https://developer.qiniu.com/kodo/manual/console-quickstart)[ 安全机制 ](https://developer.qiniu.com/kodo/manual/security#security) 以进一步了解如何正确使用和管理密钥 。
9292

9393
- JS-SDK 依赖服务端颁发 uptoken,可以通过以下二种方式实现:
94-
- 利用[七牛服务端 SDK ](https://developer.qiniu.com/sdk#sdk)构建后端服务
94+
- 利用[七牛服务端 SDK ](https://developer.qiniu.com/sdk#sdk)构建后端服务
9595
- 利用七牛底层 API 构建服务,详见七牛[上传策略](https://developer.qiniu.com/kodo/manual/put-policy)[上传凭证](https://developer.qiniu.com/kodo/manual/upload-token)
9696

9797
后端服务应提供一个 URL 地址,供 JS-SDK 初始化使用,前端通过 Ajax 请求该地址后获得 uptoken。Ajax 请求成功后,服务端应返回如下格式的 json:
@@ -197,7 +197,7 @@ Qiniu-JavaScript-SDK 的示例 Demo 中的服务器端部分是基于[ Node.js
197197
// downtoken_url: '/downtoken',
198198
// Ajax请求downToken的Url,私有空间时使用,JS-SDK 将向该地址POST文件的key和domain,服务端返回的JSON必须包含`url`字段,`url`值为该文件的下载地址
199199
// unique_names: true, // 默认 false,key 为文件名。若开启该选项,JS-SDK 会为每个文件自动生成key(文件名)
200-
// save_key: true, // 默认 false。若在服务端生成 uptoken 的上传策略中指定了 `sava_key`,则开启,SDK在前端将不对key进行任何处理
200+
// save_key: true, // 默认 false。若在服务端生成 uptoken 的上传策略中指定了 `save_key`,则开启,SDK在前端将不对key进行任何处理
201201
domain: '<Your bucket domain>', // bucket 域名,下载资源时用到,如:'http://xxx.bkt.clouddn.com/' **必需**
202202
container: 'container', // 上传区域 DOM ID,默认是 browser_button 的父元素,
203203
max_file_size: '100mb', // 最大文件体积限制
@@ -789,7 +789,7 @@ $(function() {
789789

790790
<a class="btn btn-default btn-lg " id="up_load" style="width:160px" href="#" >
791791
<span>确认上传</span>
792-
</a>
792+
</a>
793793

794794
<a class="btn btn-default btn-lg " id="stop_load" style="width:160px" href="#" >
795795
<span>暂停上传</span>
@@ -804,7 +804,7 @@ $(function() {
804804

805805
<a class="btn btn-default btn-lg " id="up_load2" style="width:160px" href="#" >
806806
<span>确认上传</span>
807-
</a>
807+
</a>
808808

809809
<a class="btn btn-default btn-lg " id="stop_load2" style="width:160px" href="#" >
810810
<span>暂停上传</span>
@@ -830,8 +830,6 @@ $(function() {
830830
<a id="license"></a>
831831
### 许可证
832832
833-
> Copyright (c) 2014 qiniu.com
834-
835-
### 基于 GPL V2 协议发布:
833+
> Copyright (c) 2017 qiniu.com
836834
837-
> [www.gnu.org/licenses/gpl-2.0.html](http://www.gnu.org/licenses/gpl-2.0.html)
835+
### 基于 MIT 协议发布

bower.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "qiniu",
33
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage API",
4-
"version": "v1.0.14-beta",
4+
"version": "v1.0.19",
55
"main": "dist/qiniu.js",
6-
"moduleType": ["globals"],
7-
"license": "GPL V2",
6+
"moduleType": [
7+
"globals"
8+
],
9+
"license": "MIT",
810
"ignore": [
911
"**/.*",
1012
"node_modules",
@@ -21,6 +23,10 @@
2123
],
2224
"authors": "[email protected]",
2325
"contributors": [
26+
{
27+
"name": "luoyeshu0507",
28+
"email": "[email protected]"
29+
},
2430
{
2531
"name": "codedogfish",
2632
"email": "[email protected]"
@@ -42,7 +48,8 @@
4248
"bootstrap": "~3.3.6",
4349
"highlight": "~8.9.1",
4450
"jquery": "~1.9.1",
45-
"respond": "~1.4.2"
51+
"respond": "~1.4.2",
52+
"vue": "^2.2.1"
4653
},
4754
"private": false
4855
}

demo/scripts/http-performance.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license httpPerformance v1.0.0
3+
* License: MIT
4+
* author: luoyeshu0507
5+
*
6+
* @description
7+
*
8+
* This object provides a utility for detailing the http request performance
9+
*
10+
*/
11+
;(function(window, undefined){
12+
'use strict';
13+
var p = window.performance;
14+
var httpPerformance = {
15+
clear: function() {
16+
p.clearMarks();
17+
p.clearMeasures();
18+
p.clearResourceTimings();
19+
},
20+
getAll: function() {
21+
var performanceArr = [];
22+
p.getEntries().forEach(function(item) {
23+
performanceArr.push(httpPerformance.formatPerformance(item));
24+
});
25+
return performanceArr;
26+
},
27+
getByName: function(name) {
28+
var performanceArr = [];
29+
p.getEntriesByName(name).forEach(function(item) {
30+
performanceArr.push(httpPerformance.formatPerformance(item));
31+
});
32+
return performanceArr;
33+
},
34+
formatPerformance: function(prt) { // PerformanceResourceTiming
35+
return {
36+
redirect: prt.redirectEnd - prt.redirectStart,
37+
domainLookup: prt.domainLookupEnd - prt.domainLookupStart,
38+
connect: prt.connectEnd - prt.connectStart,
39+
request: prt.responseStart - prt.requestStart,
40+
response: prt.responseEnd - prt.responseStart,
41+
42+
entryType: prt.entryType,
43+
initiatorType: prt.initiatorType,
44+
name: prt.name,
45+
duration: prt.duration,
46+
};
47+
}
48+
};
49+
50+
// support AMD and CMD
51+
if (typeof module !== 'undefined' && module.exports) {
52+
module.exports = httpPerformance;
53+
} else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
54+
define('httpPerformance', [], function () {
55+
return httpPerformance;
56+
});
57+
} else {
58+
window.httpPerformance = httpPerformance;
59+
}
60+
})(window);

demo/scripts/performance.js

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
Vue.component('zone-list', {
2+
props: ['hostMap', 'currentZone', 'switchZone', 'selectedHost'],
3+
template: '<div class="uphosts-list"><ul><li v-for="item in hostMap" :class="{on: item.zone == currentZone}" @click="switchZone(item.zone)">{{item.zoneZh}}</li></ul><ul><li v-for="item in hostMap" :class="{on: item.zone == currentZone}"><label v-for="uphost in item.uphosts"><input type="radio" v-model="selectedHost.host" :value="uphost"/>{{uphost}}</label></li></ul></div>'
4+
});
5+
Vue.component('upload-performance', {
6+
props: ['per'],
7+
template: '<div class="up-performance"><div class="per-title">上传耗时:</div>' +
8+
'<table>' +
9+
'<tr><th>类型</th><th>耗时 / ms</th></tr>' +
10+
'<tr><td>重定向:</td><td>{{per.redirect | tofixed(2)}}</td></tr>' +
11+
'<tr><td>DNS 查询:</td><td>{{per.domainLookup | tofixed(2)}}</td></tr>' +
12+
'<tr><td>建立连接:</td><td>{{per.connect | tofixed(2)}}</td></tr>' +
13+
'<tr><td>发送数据到开始响应:</td><td>{{per.request | tofixed(2)}}</td></tr>' +
14+
'<tr><td>接收响应:</td><td>{{per.response | tofixed(2)}}</td></tr>' +
15+
'<tr><td>总耗时:</td><td>{{per.duration | tofixed(2)}}</td></tr></table>' +
16+
'</div>'
17+
});
18+
Vue.component('up-headers', {
19+
props: ['headers'],
20+
template: '<div class="up-headers"><div class="per-title">响应头:</div><table><tr><th>类型</th><th>值</th></tr><tr v-for="header in headers"><td>{{header.key}}</td><td>{{header.val}}</td></tr></table></div>'
21+
});
22+
Vue.filter('tofixed', function (val, size) {
23+
return val.toFixed(size);
24+
});
25+
26+
var app = new Vue({
27+
el: '#app',
28+
data: {
29+
currentZone: 'z0',
30+
selectedHost: {
31+
host: ''
32+
},
33+
currentToken: '',
34+
loadMessage: '',
35+
hostMap: [
36+
{
37+
zone: 'z0',
38+
zoneZh: '华东',
39+
token: 'xozWSPMxkMjIVoHg2JyXq4-7-oJaEADLOKHVR0vU:hzD116IsDOpb4gjzyPg7ngg4Qjs=:eyJzY29wZSI6Impzc2RrOmEuanBnIiwiZGVhZGxpbmUiOjIxMTQzODA4MDAwMDAsImZzaXplTGltaXQiOjEwMDAwMDB9',
40+
uphosts: [
41+
'http://up.qiniu.com',
42+
'http://upload.qiniu.com',
43+
'https://up.qbox.me',
44+
'https://upload.qbox.me'
45+
]
46+
},
47+
{
48+
zone: 'z1',
49+
zoneZh: '华北',
50+
token: 'xozWSPMxkMjIVoHg2JyXq4-7-oJaEADLOKHVR0vU:9RFuA13c6pp-O2aIOe1UfTI3rlo=:eyJzY29wZSI6Impzc2RrLXoxOmEuanBnIiwiZGVhZGxpbmUiOjIxMTQzODA4MDAwMDAsImZzaXplTGltaXQiOjEwMDAwMDB9',
51+
uphosts: [
52+
'http://up-z1.qiniu.com',
53+
'http://upload-z1.qiniu.com',
54+
'https://up-z1.qbox.me',
55+
'https://upload-z1.qbox.me'
56+
]
57+
},
58+
{
59+
zone: 'z2',
60+
zoneZh: '华南',
61+
token: 'xozWSPMxkMjIVoHg2JyXq4-7-oJaEADLOKHVR0vU:qmDBxiYviu0tg2hZzCFHjCZM3-w=:eyJzY29wZSI6Impzc2RrLXoyOmEuanBnIiwiZGVhZGxpbmUiOjIxMTQzODA4MDAwMDAsImZzaXplTGltaXQiOjEwMDAwMDB9',
62+
uphosts: [
63+
'http://up-z2.qiniu.com',
64+
'http://upload-z2.qiniu.com',
65+
'https://up-z2.qbox.me',
66+
'https://upload-z2.qbox.me'
67+
]
68+
},
69+
{
70+
zone: 'na0',
71+
zoneZh: '北美',
72+
token: 'xozWSPMxkMjIVoHg2JyXq4-7-oJaEADLOKHVR0vU:DMPzTGGhX3HY0ph99YfAK_y-0XM=:eyJzY29wZSI6Impzc2RrLW5hMDphLmpwZyIsImRlYWRsaW5lIjoyMTE0MzgwODAwMDAwLCJmc2l6ZUxpbWl0IjoxMDAwMDAwfQ==',
73+
uphosts: [
74+
'http://up-na0.qiniu.com',
75+
'http://upload-na0.qiniu.com',
76+
'https://up-na0.qbox.me',
77+
'https://upload-na0.qbox.me'
78+
]
79+
},
80+
],
81+
isPerformanceSupported: true,
82+
performance: null,
83+
headers: null
84+
},
85+
methods: {
86+
renderHtml: function() {},
87+
post: function(opt) {
88+
var xmlHttp = new XMLHttpRequest();
89+
xmlHttp.open('POST', opt.url, true);
90+
xmlHttp.setRequestHeader('X-Qiniu-Performance', 'true');
91+
xmlHttp.onreadystatechange = function () {
92+
if (xmlHttp.readyState == 4) {
93+
if (xmlHttp.status == 200) {
94+
opt.success(xmlHttp);
95+
} else {
96+
opt.error(xmlHttp.responseText);
97+
}
98+
}
99+
};
100+
xmlHttp.upload.onprogress = opt.progress;
101+
xmlHttp.send(opt.data);
102+
},
103+
uploadTest: function() {
104+
if(!this.selectedHost.host) return;
105+
this.resetResult();
106+
var self = this;
107+
this.post({
108+
url: this.selectedHost.host,
109+
data: this.mockDate(),
110+
progress: function(e) {
111+
if (e.lengthComputable) {
112+
var percent = e.loaded/e.total*100;
113+
self.loadMessage = '模拟数据上传:' + e.loaded + " / " + e.total+" bytes 完成:" + percent.toFixed(2) + "%";
114+
}
115+
},
116+
success: function(xhr) {
117+
self.formateHeader(xhr.getAllResponseHeaders());
118+
self.getPerformance();
119+
},
120+
error: function(res) {
121+
self.loadMessage = '上传失败:' + res;
122+
}
123+
});
124+
},
125+
mockDate: function(size) {
126+
var f = new FormData(document.getElementById("testform"));
127+
f.append('file', this.dataURLtoBlob(this.randomBase64(size)));
128+
f.append('token', this.currentToken);
129+
return f;
130+
},
131+
randomBase64: function() {
132+
var dataurl = 'data:image/jpeg;base64,';
133+
var len = parseInt(Math.random()*(1330000-1100000+1)+1100000, 10);
134+
while (dataurl.length < len) {
135+
dataurl += Math.random > 0.5 ? '/9j/4AAQSkZJRgABAQAASABIAAD/4Q' : '/9j/4AAQSkZJRgABAQAASABIAAD/4W';
136+
}
137+
return dataurl;
138+
},
139+
dataURLtoBlob: function(dataurl) {
140+
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
141+
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
142+
while(n--){
143+
u8arr[n] = bstr.charCodeAt(n);
144+
}
145+
return new Blob([u8arr], {type:mime});
146+
},
147+
resetResult: function() {
148+
for (var i = 0; i < this.hostMap.length; i++) {
149+
if (this.hostMap[i].zone === this.currentZone) {
150+
this.currentToken = this.hostMap[i].token;
151+
break;
152+
}
153+
}
154+
this.performance = null;
155+
this.headers = null;
156+
},
157+
switchZone: function(zone) {
158+
this.currentZone = zone;
159+
this.selectedHost.host = '';
160+
},
161+
getPerformance: function() {
162+
var per = httpPerformance.getByName(this.selectedHost.host + '/');
163+
if(per.length == 2) {
164+
per[1].redirect = per[0].redirect;
165+
per[1].domainLookup = per[0].domainLookup;
166+
per[1].connect = per[0].connect;
167+
}
168+
this.performance = per[1] || per[0]; // 跨域有时候会先发送一个 option 请求,并不是真的上传请求。
169+
},
170+
formateHeader: function(headers) {
171+
var list = [];
172+
headers.match(/.+/mg).map(function(item) {
173+
var index = item.indexOf(':');
174+
var o = {
175+
key: item.substr(0, index),
176+
val: item.substr(index + 1)
177+
};
178+
list.push(o);
179+
});
180+
this.headers = list;
181+
}
182+
},
183+
created: function() {
184+
this.isPerformanceSupported = !!(window.performance && window.performance.getEntries);
185+
}
186+
});
187+
188+

demo/server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ app.get('/formdata', function(req, res) {
8383
});
8484
});
8585

86+
app.get('/performance', function(req, res) {
87+
var token = uptoken.token();
88+
res.render('performance.html', {
89+
uptoken: token
90+
});
91+
});
92+
8693
qiniu.conf.ACCESS_KEY = config.ACCESS_KEY;
8794
qiniu.conf.SECRET_KEY = config.SECRET_KEY;
8895

@@ -95,5 +102,6 @@ app.listen(config.Port, function() {
95102
console.log(' ▹▹▹▹▹▹▹▹▹▹▹▹▹▹▹▹ Upload: http://127.0.0.1:%d ◁ ◁ ◁ ◁ ◁ ◁ ◁', config.Port);
96103
console.log(' ▹▹▹▹▹▹▹ Multiple upload: http://127.0.0.1:%d/multiple ◁ ◁ ◁', config.Port);
97104
console.log(' ▹▹▹▹▹▹▹ Formdata upload: http://127.0.0.1:%d/formdata ◁ ◁ ◁', config.Port);
105+
console.log(' ▹▹▹▹▹▹▹ Up Performance: http://127.0.0.1:%d/performance ◁ ◁', config.Port);
98106
console.log('△ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △ △\n');
99107
});

0 commit comments

Comments
 (0)