Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

qiniu

Package Overview
Dependencies
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

qiniu - npm Package Compare versions

Comparing version 6.1.13 to 7.0.1

examples/bucket_image_unimage.js

25

CHANGELOG.md
## CHANGE LOG
### v7.0.1
- 重构文件表单上传和分片上传代码
- 重构CDN操作相关的代码
- 重构资源管理相关的代码
- 重构数据处理相关的代码
- 重构上传策略的相关代码
### v6.1.14
2017-01-16
- 增加CDN刷新、预取
- 增加获取带宽、流量数据
- 增加获取日志列表
- 增加时间戳防盗链签名
### v6.1.13

@@ -36,6 +53,6 @@

- Make secure base url
- policy add fsizeMin
- 修正 getEncodedEntryUri(bucket, key)
- 文档修正
- Make secure base url
- policy add fsizeMin
- 修正 getEncodedEntryUri(bucket, key)
- 文档修正

@@ -42,0 +59,0 @@ ### v6.1.8

22

index.js

@@ -0,16 +1,16 @@

var libPath = process.env.QINIU_COV ? './lib-cov' : './qiniu';
var libpath = process.env.QINIU_COV ? './lib-cov' : './qiniu';
module.exports = {
auth: {
digest: require(libpath + '/auth' + '/digest.js')
digest: require(libPath + '/auth' + '/digest.js')
},
io: require(libpath + '/io.js'),
rs: require(libpath + '/rs.js'),
rsf: require(libpath + '/rsf.js'),
fop: require(libpath + '/fop.js'),
conf: require(libpath + '/conf.js'),
rpc: require(libpath + '/rpc.js'),
util: require(libpath + '/util.js'),
zone: require(libpath + '/zone.js')
cdn: require(libPath + "/cdn.js"),
form_up: require(libPath + '/storage/form.js'),
resume_up: require(libPath + '/storage/resume.js'),
rs: require(libPath + '/storage/rs.js'),
fop: require(libPath + '/fop.js'),
conf: require(libPath + '/conf.js'),
rpc: require(libPath + '/rpc.js'),
util: require(libPath + '/util.js'),
zone: require(libPath + '/zone.js')
};
{
"name": "qiniu",
"version": "6.1.13",
"version": "7.0.1",
"description": "Node wrapper for Qiniu Resource (Cloud) Storage API",

@@ -58,7 +58,8 @@ "main": "index.js",

"dependencies": {
"mime": "1.2.9",
"formstream": "0.0.7",
"mime": "1.3.6",
"formstream": "1.1.0",
"crc32": "0.2.2",
"urllib": "0.5.1",
"sync-request":"3.0.1"
"urllib": "2.22.0",
"agentkeepalive": "3.3.0",
"urlencode": "1.1.0"
},

@@ -65,0 +66,0 @@ "devDependencies": {

@@ -1,2 +0,1 @@

var url = require('url');

@@ -12,29 +11,1 @@ var conf = require('../conf');

}
//Mac.prototype._sign = function(data) {
// return util.hmacSha1(data, this.secretKey);
//}
//
//Mac.prototype.sign = function(data) {
// return this.accessKey + ':' + this._sign(data);
//}
//
//Mac.prototype.signWithData = function(b) {
// var data = util.urlsafeBase64Encode(b);
// var sign = this._sign(data);
// return this.accessKey + ':' + sign + ':' + data;
//}
//
//Mac.prototype.sign_request = function(path, body, content_type) {
// var u = url.parse(path);
// var path = u.path;
// var data = path + '\n';
//
// if (body) {
// data += body;
// }
//
// return this.access + ':' + this._sign(data);
//}
//

@@ -1,4 +0,5 @@

var fs = require('fs');
var path = require('path');
var os = require('os');
const fs = require('fs');
const path = require('path');
const os = require('os');
const pkg = require('../package.json');

@@ -8,18 +9,67 @@ exports.ACCESS_KEY = '<PLEASE APPLY YOUR ACCESS KEY>';

var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'package.json')));
var ua = function() {
return 'QiniuNodejs/' + pkg.version + ' (' + os.type() + '; ' + os.platform() + '; ' + os.arch() + '; )';
var defaultUserAgent = function() {
return 'QiniuNodejs/' + pkg.version + ' (' + os.type() + '; ' + os.platform() +
'; ' + os.arch() + '; )';
}
exports.USER_AGENT = ua();
exports.UP_HOST = 'http://upload.qiniu.com';
exports.RS_HOST = 'http://rs.qbox.me';
exports.RSF_HOST = 'http://rsf.qbox.me';
exports.API_HOST = 'http://api.qiniu.com';
exports.RPC_TIMEOUT = 3600000; // default rpc timeout: one hour
exports.UC_HOST = 'http://uc.qbox.me';
exports.UP_HTTPS_HOST = 'https://up-z1.qbox.me';
exports.SCHEME = 'http'; // 上传使用的协议
exports.AUTOZONE = true; // 自动获取不同机房空间的上传域名
exports.BUCKET = null ; // 上传空间
exports.EXPIRE = 0;
exports.USER_AGENT = defaultUserAgent();
exports.BLOCK_SIZE = 4 * 1024 * 1024; //4MB, never change
//define api form mime type
exports.FormMimeUrl = "application/x-www-form-urlencoded";
exports.FormMimeJson = "application/json";
exports.FormMimeRaw = "application/octet-stream";
exports.RS_HOST = "http://rs.qiniu.com";
exports.RPC_TIMEOUT = 60000; //60s
exports.Config = function Config(options) {
options = options || {};
//use http or https protocol
this.useHttpsDomain = options.useHttpsDomain || false;
//use cdn accerlated domains
this.useCdnDomain = options.useCdnDomain || true;
//zone of the bucket
//z0 huadong, z1 huabei, z2 huanan, na0 beimei
this.zone = options.zone || null;
this.zoneExpire = options.zoneExpire || -1;
}
exports.Zone = function(srcUpHosts, cdnUpHosts, ioHost, rsHost, rsfHost,
apiHost) {
this.srcUpHosts = srcUpHosts || {};
this.cdnUpHosts = cdnUpHosts || {};
this.ioHost = ioHost || "";
this.rsHost = rsHost || "rs.qiniu.com";
this.rsfHost = rsfHost || "rsf.qiniu.com";
this.apiHost = apiHost || "api.qiniu.com";
var dotIndex = this.ioHost.indexOf(".");
if (dotIndex != -1) {
var ioTag = this.ioHost.substring(0, dotIndex);
var zoneSepIndex = ioTag.indexOf("-");
if (zoneSepIndex != -1) {
var zoneTag = ioTag.substring(zoneSepIndex + 1);
switch (zoneTag) {
case "z1":
this.rsHost = "rs-z1.qiniu.com";
this.rsfHost = "rsf-z1.qiniu.com";
this.apiHost = "api-z1.qiniu.com";
break;
case "z2":
this.rsHost = "rs-z2.qiniu.com";
this.rsfHost = "rsf-z2.qiniu.com";
this.apiHost = "api-z2.qiniu.com";
break;
case "na0":
this.rsHost = "rs-na0.qiniu.com";
this.rsfHost = "rsf-na0.qiniu.com";
this.apiHost = "api-na0.qiniu.com";
break;
default:
this.rsHost = "rs.qiniu.com";
this.rsfHost = "rsf.qiniu.com";
this.apiHost = "api.qiniu.com";
break;
}
}
}
}

@@ -1,80 +0,117 @@

var util = require('./util');
var rpc = require('./rpc');
var conf = require('./conf');
const util = require('./util');
const rpc = require('./rpc');
const conf = require('./conf');
const digest = require('./auth/digest');
const zone = require('./zone.js');
const querystring = require('querystring');
var querystring = require('querystring');
exports.OperationManager = OperationManager;
exports.ImageView = ImageView;
exports.ImageInfo = ImageInfo;
exports.Exif = Exif;
exports.pfop = pfop;
function ImageView(mode, width, height, quality, format) {
this.mode = mode || 1;
this.width = width || 0;
this.height = height || 0;
this.quality = quality || 0;
this.format = format || null;
function OperationManager(mac, config) {
this.mac = mac || new digest.Mac();
this.config = config || new conf.Config();
}
ImageView.prototype.makeRequest = function(url) {
url += '?imageView2/' + this.mode;
// 发送持久化数据处理请求
// @param bucket - 空间名称
// @param key - 文件名称
// @param fops - 处理指令集合
// @param pipeline - 处理队列名称
// @param options - 可选参数
// notifyURL 回调业务服务器,通知处理结果
// force 结果是否强制覆盖已有的同名文件
// @param callbackFunc(err, respBody, respInfo) - 回调函数
OperationManager.prototype.pfop = function(bucket, key, fops, pipeline,
options, callbackFunc) {
options = options || {};
var that = this;
//必须参数
var reqParams = {
bucket: bucket,
key: key,
pipeline: pipeline,
fops: fops.join(";"),
};
if (this.width > 0) {
url += '/w/' + this.width;
//notifyURL
if (options.notifyURL) {
reqParams.notifyURL = options.notifyURL;
}
if (this.height > 0) {
url += '/h/' + this.height;
//force
if (options.force) {
reqParams.force = 1;
}
if (this.quality > 0) {
url += '/q/' + this.quality;
var useCache = false;
if (this.config.zone) {
if (this.config.zoneExpire == -1) {
useCache = true;
} else {
if (!util.isTimestampExpired(this.config.zoneExpire)) {
useCache = true;
}
}
}
if (this.format) {
url += '/format/' + this.format;
if (useCache) {
pfopReq(this.mac, this.config, reqParams, callbackFunc);
} else {
zone.getZoneInfo(this.mac.accessKey, bucket, function(err, cZoneInfo,
cZoneExpire) {
if (err) {
callbackFunc(err, null, null);
return;
}
//update object
that.config.zone = cZoneInfo;
that.config.zoneExpire = cZoneExpire;
//pfopReq
pfopReq(that.mac, that.config, reqParams, callbackFunc);
});
}
return url;
}
function ImageInfo() {}
ImageInfo.prototype.makeRequest = function(url) {
return url + '?imageInfo'
function pfopReq(mac, config, reqParams, callbackFunc) {
var scheme = config.useHttpsDomain ? "https://" : "http://";
var requestURI = scheme + config.zone.apiHost + '/pfop/';
var reqBody = querystring.stringify(reqParams);
var auth = util.generateAccessToken(mac, requestURI, reqBody);
rpc.postWithForm(requestURI, reqBody, auth, callbackFunc);
}
function Exif() {}
Exif.prototype.makeRequest = function(url) {
return url + '?exif'
}
function pfop(bucket, key, fops, opts, onret) {
opts = opts || {};
param = {
bucket: bucket,
key: key,
fops: fops
};
if (opts.notifyURL) {
param.notifyURL = opts.notifyURL;
} else {
param.notifyURL = 'www.test.com';
// 查询持久化数据处理进度
// @param persistentId
// @callbackFunc(err, respBody, respInfo) - 回调函数
OperationManager.prototype.prefop = function(persistentId, callbackFunc) {
var apiHost = "http://api.qiniu.com";
var zoneIndex = persistentId.indexOf(".");
if (zoneIndex == -1) {
callbackFunc("invalid persistentId", null, null);
return;
}
if (opts.force) {
param.force = 1;
var zoneTag = persistentId.substring(0, zoneIndex);
switch (zoneTag) {
case "z1":
apiHost = "api-z1.qiniu.com";
break;
case "z2":
apiHost = "api-z2.qiniu.com";
break;
case "na0":
apiHost = "api-na0.qiniu.com";
break;
default:
apiHost = "api.qiniu.com";
break;
}
if (opts.pipeline) {
param.pipeline = opts.pipeline;
}
var uri = conf.API_HOST + '/pfop/';
var body = querystring.stringify(param);
var auth = util.generateAccessToken(uri, body);
rpc.postWithForm(uri, body, auth, onret);
var scheme = this.config.useHttpsDomain ? "https://" : "http://";
var requestURI = scheme + apiHost + "/status/get/prefop";
var reqParams = {
id: persistentId
};
var reqBody = querystring.stringify(reqParams);
rpc.postWithForm(requestURI, reqBody, null, callbackFunc);
}

@@ -5,2 +5,3 @@ var urllib = require('urllib');

exports.post = post;
exports.postMultipart = postMultipart;

@@ -10,7 +11,7 @@ exports.postWithForm = postWithForm;

function postMultipart(uri, form, onret) {
return post(uri, form, form.headers(), onret);
function postMultipart(requestURI, requestForm, callbackFunc) {
return post(requestURI, requestForm, requestForm.headers(), callbackFunc);
}
function postWithForm(uri, form, token, onret) {
function postWithForm(requestURI, requestForm, token, callbackFunc) {
var headers = {

@@ -22,6 +23,6 @@ 'Content-Type': 'application/x-www-form-urlencoded'

}
return post(uri, form, headers, onret);
return post(requestURI, requestForm, headers, callbackFunc);
}
function postWithoutForm(uri, token, onret) {
function postWithoutForm(requestURI, token, callbackFunc) {
var headers = {

@@ -33,8 +34,10 @@ 'Content-Type': 'application/x-www-form-urlencoded',

}
return post(uri, null, headers, onret);
return post(requestURI, null, headers, callbackFunc);
}
function post(uri, form, headers, onresp) {
function post(requestURI, requestForm, headers, callbackFunc) {
//var start = parseInt(Date.now() / 1000);
headers = headers || {};
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
headers['Connection'] = 'keep-alive';

@@ -46,8 +49,10 @@ var data = {

timeout: conf.RPC_TIMEOUT,
gzip: true,
// timing: true,
};
if (Buffer.isBuffer(form) || typeof form === 'string') {
data.content = form;
} else if (form) {
data.stream = form;
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
data.content = requestForm;
} else if (requestForm) {
data.stream = requestForm;
} else {

@@ -57,8 +62,14 @@ data.headers['Content-Length'] = 0;

var req = urllib.request(uri, data, function(err, result, res) {
var rerr = null;
if (err || Math.floor(res.statusCode/100) !== 2) {
rerr = {code: res&&res.statusCode||-1, error: err||result&&result.error||''};
}
onresp(rerr, result, res);
var req = urllib.request(requestURI, data, function(respErr, respBody,
respInfo) {
//var end = parseInt(Date.now() / 1000);
// console.log((end - start) + " seconds");
// console.log("queuing:\t" + respInfo.timing.queuing);
// console.log("dnslookup:\t" + respInfo.timing.dnslookup);
// console.log("connected:\t" + respInfo.timing.connected);
// console.log("requestSent:\t" + respInfo.timing.requestSent);
// console.log("waiting:\t" + respInfo.timing.waiting);
// console.log("contentDownload:\t" + respInfo.timing.contentDownload);
callbackFunc(respErr, respBody, respInfo);
});

@@ -68,2 +79,1 @@

}

@@ -1,12 +0,46 @@

var fs = require('fs');
var url = require('url');
var path = require('path');
var crypto = require('crypto');
var conf = require('./conf');
exports.isQiniuCallback = isQiniuCallback;
// Check Timestamp Expired or not
exports.isTimestampExpired = function(timestamp) {
return timestamp < parseInt(Date.now() / 1000);
}
// ------------------------------------------------------------------------------------------
// func encode
// Encoded Entry
exports.encodedEntry = function(bucket, key) {
return exports.urlsafeBase64Encode(bucket + (key ? ':' + key : ''));
}
// Get accessKey from uptoken
exports.getAKFromUptoken = function(uploadToken) {
var sepIndex = uploadToken.indexOf(":");
return uploadToken.substring(0, sepIndex);
}
// Get bucket from uptoken
exports.getBucketFromUptoken = function(uploadToken) {
var sepIndex = uploadToken.lastIndexOf(":");
var encodedPutPolicy = uploadToken.substring(sepIndex + 1);
var putPolicy = exports.urlSafeBase64Decode(encodedPutPolicy);
var putPolicyObj = JSON.parse(putPolicy);
var scope = putPolicyObj.scope;
var scopeSepIndex = scope.indexOf(":");
if (scopeSepIndex == -1) {
return scope;
} else {
return scope.substring(0, scopeSepIndex);
}
}
exports.base64ToUrlSafe = function(v) {
return v.replace(/\//g, '_').replace(/\+/g, '-');
}
exports.urlSafeToBase64 = function(v) {
return v.replace(/\_/g, '/').replace(/\-/g, '+');
}
// UrlSafe Base64 Decode
exports.urlsafeBase64Encode = function(jsonFlags) {

@@ -17,10 +51,12 @@ var encoded = new Buffer(jsonFlags).toString('base64');

exports.base64ToUrlSafe = function(v) {
return v.replace(/\//g, '_').replace(/\+/g, '-');
// UrlSafe Base64 Decode
exports.urlSafeBase64Decode = function(fromStr) {
return new Buffer(exports.urlSafeToBase64(fromStr), 'base64').toString();
}
// Hmac-sha1 Crypt
exports.hmacSha1 = function(encodedFlags, secretKey) {
/*
*return value already encoded with base64
* */
* */
var hmac = crypto.createHmac('sha1', secretKey);

@@ -31,22 +67,30 @@ hmac.update(encodedFlags);

// ------------------------------------------------------------------------------------------
// func generateAccessToken
exports.generateAccessToken = function(uri, body) {
var u = url.parse(uri);
// 创建AccessToken凭证
// @param mac AK&SK对象
// @param requestURI 回调的URL中的requestURI
// @param reqBody 请求Body,仅当请求的ContentType为
// application/x-www-form-urlencoded时才需要传入该参数
exports.generateAccessToken = function(mac, requestURI, reqBody) {
var u = url.parse(requestURI);
var path = u.path;
var access = path + '\n';
if (body) {
access += body;
if (reqBody) {
access += reqBody;
}
var digest = exports.hmacSha1(access, conf.SECRET_KEY);
var digest = exports.hmacSha1(access, mac.secretKey);
var safeDigest = exports.base64ToUrlSafe(digest);
return 'QBox ' + conf.ACCESS_KEY + ':' + safeDigest;
return 'QBox ' + mac.accessKey + ':' + safeDigest;
}
function isQiniuCallback(path, body, callbackAuth) {
var auth = exports.generateAccessToken(path, body);
// 校验七牛上传回调的Authorization
// @param mac AK&SK对象
// @param requestURI 回调的URL中的requestURI
// @param reqBody 请求Body,仅当请求的ContentType为
// application/x-www-form-urlencoded时才需要传入该参数
// @param callbackAuth 回调时请求的Authorization头部值
exports.isQiniuCallback = function(mac, requestURI, reqBody, callbackAuth) {
var auth = exports.generateAccessToken(mac, requestURI, reqBody);
return auth === callbackAuth;
}

@@ -1,61 +0,105 @@

var request = require('sync-request');
const urllib = require('urllib')
const util = require('util')
const conf = require('./conf');
//conf 为全局变量
exports.up_host = function (uptoken, conf){
//huadong
exports.Zone_z0 = new conf.Zone([
'up.qiniup.com',
'up-nb.qiniup.com',
'up-xs.qiniup.com',
], [
'upload.qiniup.com',
'upload-nb.qiniup.com',
'upload-xs.qiniup.com',
], 'iovip.qbox.me',
'rs.qiniu.com',
'rsf.qiniu.com',
'api.qiniu.com');
var version = process.versions;
var num = version.node.split(".")[0];
//huabei
exports.Zone_z1 = new conf.Zone([
'up-z1.qiniup.com',
], [
'upload-z1.qiniup.com',
], 'iovip-z1.qbox.me',
'rs-z1.qiniu.com',
'rsf-z1.qiniu.com',
'api-z1.qiniu.com');
// node 版本号低于 1.0.0, 使用默认域名上传,可以在conf中指定上传域名
if(num < 1 ){
conf.AUTOZONE = false;
}
//huanan
exports.Zone_z2 = new conf.Zone([
'up-z2.qiniup.com',
'up-gz.qiniup.com',
'up-fs.qiniup.com'
], [
'upload-z2.qiniup.com',
'upload-gz.qiniup.com',
'upload-fs.qiniup.com',
], 'iovip-z2.qbox.me',
'rs-z2.qiniu.com',
'rsf-z2.qiniu.com',
'api-z2.qiniu.com');
if(!conf.AUTOZONE){
return;
}
var ak = uptoken.toString().split(":")[0];
var tokenPolicy = uptoken.toString().split(":")[2];
var tokenPolicyStr = new Buffer(tokenPolicy, 'base64').toString();
var json_tokenPolicyStr = JSON.parse(tokenPolicyStr);
//beimei
exports.Zone_na0 = new conf.Zone([
'up-na0.qiniup.com',
], [
'upload-na0.qiniup.com',
], 'iovip-na0.qbox.me',
'rs-na0.qiniu.com',
'rsf-na0.qiniu.com',
'api-na0.qiniu.com')
var scope = json_tokenPolicyStr.scope;
var bucket = scope.toString().split(":")[0];
// bucket 相同,上传域名仍在过期时间内
if((new Date().getTime() < conf.EXPIRE) && bucket == conf.BUCKET){
return;
exports.getZoneInfo = function(accessKey, bucket, callbackFunc) {
var apiAddr = util.format('http://uc.qbox.me/v2/query?ak=%s&bucket=%s',
accessKey, bucket);
urllib.request(apiAddr, function(respErr, respData, respInfo) {
if (respErr) {
callback(respErr, null, null);
return;
}
//记录bucket名
conf.BUCKET = bucket;
var res = request('GET', 'http://uc.qbox.me/v1/query?ak=' + ak + '&bucket=' + bucket, {
'headers': {
'Content-Type': 'application/json'
}
});
if (respInfo.statusCode != 200) {
//not ok
respErr = new Error(respInfo.statusCode + "\n" + respData);
callbackFunc(respErr, null, null);
return;
}
if(res.statusCode == 200){
var json_str = JSON.parse(res.body.toString());
var zoneData = JSON.parse(respData);
var srcUpHosts = [];
var cdnUpHosts = [];
var zoneExpire = 0;
//判断设置使用的协议, 默认使用http
if(conf.SCHEME == 'http'){
conf.UP_HOST = json_str.http.up[1];
}else{
conf.UP_HOST = json_str.https.up[0];
}
try {
zoneExpire = zoneData.ttl;
//read src hosts
zoneData.up.src.main.forEach(function(host) {
srcUpHosts.push(host);
});
if (zoneData.up.src.backup) {
zoneData.up.src.backup.forEach(function(host) {
srcUpHosts.push(host);
});
}
conf.EXPIRE = 86400 + new Date().getTime();
//read acc hosts
zoneData.up.acc.main.forEach(function(host) {
cdnUpHosts.push(host);
});
if (zoneData.up.acc.backup) {
zoneData.up.acc.backup.forEach(function(host) {
cdnUpHosts.push(host);
});
}
}else{
var err = new Error('Server responded with status code ' + res.statusCode + ':\n' + res.body.toString());
err.statusCode = res.statusCode;
err.headers = res.headers;
err.body = res.body;
throw err;
var ioHost = zoneData.io.src.main[0];
var zoneInfo = new conf.Zone(srcUpHosts, cdnUpHosts, ioHost)
callbackFunc(null, zoneInfo, zoneExpire);
} catch (e) {
callbackFunc(e, null, null);
}
});
}

@@ -31,3 +31,3 @@ # Qiniu Resource Storage SDK for Node.js

参考文档:[七牛云存储 Node.js SDK 使用指南](http://developer.qiniu.com/docs/v6/sdk/nodejs-sdk.html)
参考文档:[七牛云存储 Node.js SDK 使用指南](http://developer.qiniu.com/kodo/sdk/nodejs)

@@ -34,0 +34,0 @@ ## 贡献代码

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc