Skip to content

Commit

Permalink
support upload retry with multiple regions and add abstract design ab…
Browse files Browse the repository at this point in the history
…out region
  • Loading branch information
lihsai0 committed Aug 21, 2023
1 parent 84d6f5b commit 802c431
Show file tree
Hide file tree
Showing 14 changed files with 2,436 additions and 354 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ module.exports = {
httpc: {
middleware: require('./qiniu/httpc/middleware'),
HttpClient: require('./qiniu/httpc/client').HttpClient,
ResponseWrapper: require('./qiniu/httpc/responseWrapper').ResponseWrapper
ResponseWrapper: require('./qiniu/httpc/responseWrapper').ResponseWrapper,
Endpoint: require('./qiniu/httpc/endpoint').Endpoint,
StaticEndpointsProvider: require('./qiniu/httpc/endpointsProvider').StaticEndpointsProvider,
SERVICE_NAME: require('./qiniu/httpc/region').SERVICE_NAME,
Region: require('./qiniu/httpc/region').Region,
StaticRegionsProvider: require('./qiniu/httpc/regionsProvider').StaticRegionsProvider,
CachedRegionsProvider: require('./qiniu/httpc/regionsProvider').CachedRegionsProvider,
ChainedRegionsProvider: require('./qiniu/httpc/regionsProvider').ChainedRegionsProvider
},
rpc: require('./qiniu/rpc.js'),
util: require('./qiniu/util.js'),
Expand All @@ -20,6 +27,14 @@ module.exports = {
room: require('./qiniu/rtc/room.js'),
Credentials: require('./qiniu/rtc/credentials.js'),
sms: {
message: require('./qiniu/sms/message.js'),
message: require('./qiniu/sms/message.js')
}
};

// TODO: is this warning message ok in here?
console.warn(
'WARNING:\n' +
'qiniu SDK will migrate API to Promise style gradually.\n' +
'The callback style will not be removed for now,\n' +
'but you should catch your error in your callback function itself'
);
3 changes: 2 additions & 1 deletion qiniu/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ exports.Config = function Config (options) {
options = options || {};
// use http or https protocol
this.useHttpsDomain = !!(options.useHttpsDomain || false);
// use cdn accerlated domains
// use cdn accerlated domains, this is not work with auto query region
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;
this.regionsProvider = options.regionsProvider || null;
};

exports.Zone = function (
Expand Down
7 changes: 0 additions & 7 deletions qiniu/httpc/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ HttpClient.prototype._handleRequest = function (req) {
* @property {urllib.RequestOptions} urllibOptions
*/

/**
* Wrapped result of request
* @typedef {Object} RespWrapper
* @property {*} data
* @property {http.IncomingMessage} resp
*/

/**
*
* @param {ReqOpts} requestOptions
Expand Down
51 changes: 51 additions & 0 deletions qiniu/httpc/endpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function Endpoint (host, options) {
options = options || {};

this.host = host;
this.defaultScheme = options.defaultScheme || 'https';
}

/**
* @typedef EndpointPersistInfo
* @property {string} host
* @property {string} defaultScheme
*/

/**
* @param {EndpointPersistInfo} persistInfo
* @returns {Endpoint}
*/
Endpoint.fromPersistInfo = function (persistInfo) {
return new Endpoint(persistInfo.host, {
defaultScheme: persistInfo.defaultScheme
});
};

/**
* @param {Object} [options]
* @param {string} [options.scheme]
*/
Endpoint.prototype.getValue = function (options) {
options = options || {};

const scheme = options.scheme || this.defaultScheme;
const host = this.host;

return scheme + '://' + host;
};

Object.defineProperty(Endpoint.prototype, 'persistInfo', {
/**
* @returns {EndpointPersistInfo}
*/
get: function () {
return {
defaultScheme: this.defaultScheme,
host: this.host
};
},
enumerable: false,
configurable: true
});

exports.Endpoint = Endpoint;
68 changes: 68 additions & 0 deletions qiniu/httpc/endpointsProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @interface EndpointsProvider
*/

/**
* @function
* @name EndpointsProvider#setEndpoints
* @param {endpoints: Endpoint[]} endpoints
* @returns {Promise<void>}
*/

/**
* @function
* @name EndpointsProvider#getEndpoints
* @returns {Promise<Endpoint[]>}
*/

// --- could split to files if migrate to typescript --- //

/**
* @class
* @implements EndpointsProvider
* @property {Endpoint[]} endpoints
* @constructor
* @param {Endpoint[]} endpoints
*/
function StaticEndpointsProvider (endpoints) {
this.endpoints = endpoints;
}

/**
* @param {Region} region
* @param {string} serviceName
*/
StaticEndpointsProvider.fromRegion = function (region, serviceName) {
return new StaticEndpointsProvider(region.services[serviceName]);
};

/**
* @returns {Promise<Endpoint[]>}
*/
StaticEndpointsProvider.prototype.getEndpoints = function () {
return Promise.resolve(this.endpoints);
};

/**
* @param {Endpoint[]} endpoints
* @returns {Promise<void>}
*/
StaticEndpointsProvider.prototype.setEndpoints = function (endpoints) {
this.endpoints = endpoints;
return Promise.resolve();
};

/**
* @param {RegionsProvider} regionsProvider
* @param {string} serviceName
* @returns {Promise<EndpointsProvider[]>}
*/
function getEndpointsProvidersFromRegionsProvider (regionsProvider, serviceName) {
return regionsProvider.getRegions()
.then(regions => {
return regions.map(r => StaticEndpointsProvider.fromRegion(r, serviceName));
});
}

exports.StaticEndpointsProvider = StaticEndpointsProvider;
exports.getEndpointsProvidersFromRegionsProvider = getEndpointsProvidersFromRegionsProvider;
2 changes: 2 additions & 0 deletions qiniu/httpc/middleware/retryDomains.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const middleware = require('./base');

const URL = require('url').URL;

/**
* @class
* @extends middleware.Middleware
Expand Down
Loading

0 comments on commit 802c431

Please sign in to comment.