Skip to content

Commit

Permalink
feat: add async task (#818)
Browse files Browse the repository at this point in the history
* test: replace await loop with Promise.all (#810)

Co-authored-by: 饶培泽 <peizerao@gmail.com>

* feat: add typescript support (#814)

* feat: add ts

* feat: add cjs

* refactor: checkBucketName

* refactor: common/utils with ts

* refactor: createRequest with ts

* feat: add es2017

Co-authored-by: weiyie <912881342@qq.com>

* chore: add nyc config

* feat: asyncFetch (#817)

Co-authored-by: 冷若霜寒 <912881342@qq.com>
  • Loading branch information
PeterRao and weiyie authored Jun 15, 2020
1 parent ef1e44a commit 8d27f24
Show file tree
Hide file tree
Showing 119 changed files with 1,288 additions and 540 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ test/browser/build
test/benchmark/
test/fixtures/
task/
browser-build.js
browser-build.js
es
cjs
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint max-len: [0] */
module.exports = {
extends: 'airbnb',
extends: ['airbnb', 'eslint-config-ali/typescript'],
parserOptions: {
ecmaFeatures: {
experimentalObjectRestSpread: true,
Expand Down Expand Up @@ -30,5 +30,6 @@ module.exports = {
}],
'no-buffer-constructor': [2],
"comma-dangle": [2, "never"],
'import/prefer-default-export': [0],
}
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ package-lock.json
.DS_Store
/example/node_modules/
/example/public/index.js

es
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- "./lib/common/object/*AsyncFetch.js"
2 changes: 1 addition & 1 deletion lib/browser/bucket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const assert = require('assert');
const _checkBucketName = require('../common/utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../common/utils/checkBucketName');

const proto = exports;

Expand Down
4 changes: 2 additions & 2 deletions lib/browser/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const urllib = require('urllib');
const pkg = require('./version');
const bowser = require('bowser');
const signUtils = require('../common/signUtils');
const _isIP = require('../common/utils/isIP');
const { isIP: _isIP } = require('../common/utils/isIP');
const _initOptions = require('../common/client/initOptions');
const createRequest = require('../common/utils/createRequest');
const { createRequest } = require('../common/utils/createRequest');

const globalHttpAgent = new AgentKeepalive();

Expand Down
5 changes: 3 additions & 2 deletions lib/browser/managed-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const util = require('util');
const path = require('path');
const mime = require('mime');
const copy = require('copy-to');
const isBlob = require('../common/utils/isBlob');
const isFile = require('../common/utils/isFile');
const { isBlob } = require('../common/utils/isBlob');
const { isFile } = require('../common/utils/isFile');

const proto = exports;

Expand Down Expand Up @@ -131,6 +131,7 @@ proto._resumeMultipart = async function _resumeMultipart(checkpoint, options) {
let multipartFinish = false;

let uploadPartJob = function uploadPartJob(self, partNo) {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
try {
if (!self.isCancel()) {
Expand Down
4 changes: 2 additions & 2 deletions lib/browser/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const mime = require('mime');
const callback = require('../common/callback');
const signHelper = require('../common/signUtils');
const merge = require('merge-descriptors');
const isBlob = require('../common/utils/isBlob');
const isFile = require('../common/utils/isFile');
const { isBlob } = require('../common/utils/isBlob');
const { isFile } = require('../common/utils/isFile');

// var assert = require('assert');

Expand Down
4 changes: 2 additions & 2 deletions lib/bucket.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@


const assert = require('assert');
const isArray = require('./common/utils/isArray');
const _checkBucketName = require('../lib/common/utils/checkBucketName');
const { isArray } = require('./common/utils/isArray');
const { checkBucketName: _checkBucketName } = require('../lib/common/utils/checkBucketName');

const proto = exports;

Expand Down
4 changes: 2 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const urllib = require('urllib');
const pkg = require('../package.json');
const bowser = require('bowser');
const signUtils = require('./common/signUtils');
const _isIP = require('./common/utils/isIP');
const { isIP: _isIP } = require('./common/utils/isIP');
const _initOptions = require('./common/client/initOptions');
const createRequest = require('./common/utils/createRequest');
const { createRequest } = require('./common/utils/createRequest');

const globalHttpAgent = new AgentKeepalive();
const globalHttpsAgent = new HttpsAgentKeepalive();
Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/deleteBucketEncryption.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;
// const jstoxml = require('jstoxml');
Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/deleteBucketLifecycle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;

Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/deleteBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/deleteBucketTags.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/deleteBucketWebsite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;

Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/getBucketEncryption.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;
/**
Expand Down
6 changes: 3 additions & 3 deletions lib/common/bucket/getBucketLifecycle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _checkBucketName = require('../utils/checkBucketName');
const isArray = require('../utils/isArray');
const formatObjKey = require('../utils/formatObjKey');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { isArray } = require('../utils/isArray');
const { formatObjKey } = require('../utils/formatObjKey');

const proto = exports;

Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/getBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/getBucketRequestPayment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;
/**
Expand Down
4 changes: 2 additions & 2 deletions lib/common/bucket/getBucketTags.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const proto = exports;
const _checkBucketName = require('../utils/checkBucketName');
const isObject = require('../utils/isObject');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { isObject } = require('../utils/isObject');
/**
* getBucketTags
* @param {String} name - bucket name
Expand Down
2 changes: 1 addition & 1 deletion lib/common/bucket/getBucketVersioning.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;
/**
Expand Down
4 changes: 2 additions & 2 deletions lib/common/bucket/getBucketWebsite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _checkBucketName = require('../utils/checkBucketName');
const isObject = require('../utils/isObject');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { isObject } = require('../utils/isObject');

const proto = exports;

Expand Down
4 changes: 2 additions & 2 deletions lib/common/bucket/putBucket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const proto = exports;
const _checkBucketName = require('../utils/checkBucketName');
const obj2xml = require('../utils/obj2xml');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { obj2xml } = require('../utils/obj2xml');

proto.putBucket = async function putBucket(name, options) {
_checkBucketName(name, true);
Expand Down
4 changes: 2 additions & 2 deletions lib/common/bucket/putBucketEncryption.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const proto = exports;
// const jstoxml = require('jstoxml');
const _checkBucketName = require('../utils/checkBucketName');
const obj2xml = require('../utils/obj2xml');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { obj2xml } = require('../utils/obj2xml');
/**
* putBucketEncryption
* @param {String} bucketName - bucket name
Expand Down
14 changes: 7 additions & 7 deletions lib/common/bucket/putBucketLifecycle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable no-use-before-define */
const _checkBucketName = require('../utils/checkBucketName');
const isArray = require('../utils/isArray');
const deepCopy = require('../utils/deepCopy');
const isObject = require('../utils/isObject');
const obj2xml = require('../utils/obj2xml');
const checkObjectTag = require('../utils/checkObjectTag');
const getStrBytesCount = require('../utils/getStrBytesCount');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { isArray } = require('../utils/isArray');
const { deepCopy } = require('../utils/deepCopy');
const { isObject } = require('../utils/isObject');
const { obj2xml } = require('../utils/obj2xml');
const { checkObjectTag } = require('../utils/checkObjectTag');
const { getStrBytesCount } = require('../utils/getStrBytesCount');

const proto = exports;

Expand Down
6 changes: 3 additions & 3 deletions lib/common/bucket/putBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _checkBucketName = require('../utils/checkBucketName');
const policy2Str = require('../utils/policy2Str');
const isObject = require('../utils/isObject');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { policy2Str } = require('../utils/policy2Str');
const { isObject } = require('../utils/isObject');

const proto = exports;
/**
Expand Down
4 changes: 2 additions & 2 deletions lib/common/bucket/putBucketRequestPayment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _checkBucketName = require('../utils/checkBucketName');
const obj2xml = require('../utils/obj2xml');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { obj2xml } = require('../utils/obj2xml');

const proto = exports;
/**
Expand Down
8 changes: 4 additions & 4 deletions lib/common/bucket/putBucketTags.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _checkBucketName = require('../utils/checkBucketName');
const obj2xml = require('../utils/obj2xml');
const checkTag = require('../utils/checkBucketTag');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { obj2xml } = require('../utils/obj2xml');
const { checkBucketTag } = require('../utils/checkBucketTag');

const proto = exports;
/**
Expand All @@ -12,7 +12,7 @@ const proto = exports;

proto.putBucketTags = async function putBucketTags(name, tag, options = {}) {
_checkBucketName(name);
checkTag(tag);
checkBucketTag(tag);
const params = this._bucketRequestParams('PUT', name, 'tagging', options);
params.successStatuses = [200];
tag = Object.keys(tag).map(key => ({
Expand Down
4 changes: 2 additions & 2 deletions lib/common/bucket/putBucketVersioning.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _checkBucketName = require('../utils/checkBucketName');
const obj2xml = require('../utils/obj2xml');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { obj2xml } = require('../utils/obj2xml');

const proto = exports;
/**
Expand Down
6 changes: 3 additions & 3 deletions lib/common/bucket/putBucketWebsite.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _checkBucketName = require('../utils/checkBucketName');
const obj2xml = require('../utils/obj2xml');
const isArray = require('../utils/isArray');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { obj2xml } = require('../utils/obj2xml');
const { isArray } = require('../utils/isArray');

const proto = exports;
proto.putBucketWebsite = async function putBucketWebsite(name, config = {}, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/common/client/initOptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ms = require('humanize-ms');
const urlutil = require('url');
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

function setEndpoint(endpoint, secure) {
let url = urlutil.parse(endpoint);
Expand Down
2 changes: 1 addition & 1 deletion lib/common/image/processObjectSave.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-use-before-define */
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const querystring = require('querystring');
const { Base64: { encode: str2Base64 } } = require('js-base64');

Expand Down
2 changes: 1 addition & 1 deletion lib/common/multipart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

const copy = require('copy-to');
const callback = require('./callback');
const deepCopy = require('./utils/deepCopy');
const { deepCopy } = require('./utils/deepCopy');

const proto = exports;

Expand Down
4 changes: 2 additions & 2 deletions lib/common/object/calculatePostSignature.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

const policy2Str = require('../utils/policy2Str');
const { policy2Str } = require('../utils/policy2Str');
const signHelper = require('../signUtils');
const isObject = require('../utils/isObject');
const { isObject } = require('../utils/isObject');

const proto = exports;

Expand Down
2 changes: 1 addition & 1 deletion lib/common/object/copyObject.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _checkBucketName = require('../utils/checkBucketName');
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');

const proto = exports;

Expand Down
2 changes: 1 addition & 1 deletion lib/common/object/deleteMulti.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable object-curly-newline */
const utility = require('utility');
const obj2xml = require('../utils/obj2xml');
const { obj2xml } = require('../utils/obj2xml');

const proto = exports;

Expand Down
1 change: 1 addition & 0 deletions lib/common/object/getAsyncFetch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function getAsyncFetch(this: any, taskId: any, options?: any): Promise<object>;
26 changes: 26 additions & 0 deletions lib/common/object/getAsyncFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAsyncFetch = void 0;
const formatObjKey_1 = require("../utils/formatObjKey");
/*
* getAsyncFetch
* @param {String} asyncFetch taskId
* @param {Object} options
*/
async function getAsyncFetch(taskId, options = {}) {
options.subres = Object.assign({ asyncFetch: '' }, options.subres);
options.headers = options.headers || {};
const params = this._objectRequestParams('GET', '', options);
params.headers['x-oss-task-id'] = taskId;
params.successStatuses = [200];
params.xmlResponse = true;
const result = await this.request(params);
const taskInfo = formatObjKey_1.formatObjKey(result.data.TaskInfo, 'firstLowerCase');
return {
res: result.res,
status: result.status,
state: result.data.State,
taskInfo
};
}
exports.getAsyncFetch = getAsyncFetch;
26 changes: 26 additions & 0 deletions lib/common/object/getAsyncFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { formatObjKey } from '../utils/formatObjKey';

/*
* getAsyncFetch
* @param {String} asyncFetch taskId
* @param {Object} options
*/
export async function getAsyncFetch(this: any, taskId, options: any = {}): Promise<object> {
options.subres = Object.assign({ asyncFetch: '' }, options.subres);
options.headers = options.headers || {};

const params = this._objectRequestParams('GET', '', options);
params.headers['x-oss-task-id'] = taskId;
params.successStatuses = [200];
params.xmlResponse = true;

const result = await this.request(params);
const taskInfo = formatObjKey(result.data.TaskInfo, 'firstLowerCase');
return {
res: result.res,
status: result.status,
state: result.data.State,
taskInfo
};
}

4 changes: 2 additions & 2 deletions lib/common/object/getBucketVersions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-use-before-define */
const proto = exports;
const isObject = require('../utils/isObject');
const isArray = require('../utils/isArray');
const { isObject } = require('../utils/isObject');
const { isArray } = require('../utils/isArray');


proto.getBucketVersions = getBucketVersions;
Expand Down
Loading

0 comments on commit 8d27f24

Please sign in to comment.