Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add typescript support #814

Merged
merged 13 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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
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
2 changes: 1 addition & 1 deletion lib/common/object/getObjectTagging.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const proto = exports;
const isObject = require('../utils/isObject');
const { isObject } = require('../utils/isObject');
/**
* getObjectTagging
* @param {String} name - object name
Expand Down
6 changes: 3 additions & 3 deletions lib/common/object/putObjectTagging.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const obj2xml = require('../utils/obj2xml');
const checkTag = require('../utils/checkObjectTag');
const { obj2xml } = require('../utils/obj2xml');
const { checkObjectTag } = require('../utils/checkObjectTag');

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

proto.putObjectTagging = async function putObjectTagging(name, tag, options = {}) {
checkTag(tag);
checkObjectTag(tag);

options.subres = Object.assign({ tagging: '' }, options.subres);
if (options.versionId) {
Expand Down
1 change: 1 addition & 0 deletions lib/common/utils/checkBucketName.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const checkBucketName: (name: string, createBucket: boolean) => void;
17 changes: 8 additions & 9 deletions lib/common/utils/checkBucketName.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* check Bucket Name
*/

module.exports = function (name, createBucket) {
const bucketRegex = createBucket ? /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/ : /^[a-z0-9_][a-z0-9-_]{1,61}[a-z0-9_]$/;
if (!bucketRegex.test(name)) {
throw new Error('The bucket must be conform to the specifications');
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkBucketName = void 0;
exports.checkBucketName = (name, createBucket) => {
const bucketRegex = createBucket ? /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/ : /^[a-z0-9_][a-z0-9-_]{1,61}[a-z0-9_]$/;
if (!bucketRegex.test(name)) {
throw new Error('The bucket must be conform to the specifications');
}
};
7 changes: 7 additions & 0 deletions lib/common/utils/checkBucketName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const checkBucketName = (name: string, createBucket: boolean): void => {
const bucketRegex = createBucket ? /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/ : /^[a-z0-9_][a-z0-9-_]{1,61}[a-z0-9_]$/;
if (!bucketRegex.test(name)) {
throw new Error('The bucket must be conform to the specifications');
}
};

1 change: 1 addition & 0 deletions lib/common/utils/checkBucketNameTest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const checkBucketNameTest: (name: string, createBucket: boolean) => void;
Loading