Skip to content

Commit df2f77a

Browse files
committed
feat: add idle-time fop support and type declaration
1 parent 05677a2 commit df2f77a

File tree

5 files changed

+264
-93
lines changed

5 files changed

+264
-93
lines changed

index.d.ts

+40-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
StatObjectResult
2121
} from "./StorageResponseInterface";
2222

23-
export declare type callback = (e?: Error, respBody?: any, respInfo?: any) => void;
23+
export declare type callback<T = any> = (e?: Error, respBody?: T, respInfo?: any) => void;
2424

2525
export declare namespace auth {
2626
namespace digest {
@@ -1050,6 +1050,11 @@ export declare namespace fop {
10501050
* 结果是否强制覆盖已有的同名文件
10511051
*/
10521052
force?: boolean;
1053+
1054+
/**
1055+
* 为 `1` 时开启闲时任务
1056+
*/
1057+
type?: number;
10531058
}
10541059
class OperationManager {
10551060
mac: auth.digest.Mac;
@@ -1066,14 +1071,44 @@ export declare namespace fop {
10661071
* @param options
10671072
* @param callback
10681073
*/
1069-
pfop(bucket: string, key: string, fops: string[], pipeline: string, options: PfopOptions | null, callback: callback): void;
1074+
pfop(
1075+
bucket: string,
1076+
key: string,
1077+
fops: string[],
1078+
pipeline: string,
1079+
options: PfopOptions | null,
1080+
callback: callback<{
1081+
persistentId: string
1082+
}>
1083+
): void;
10701084

10711085
/**
10721086
* 查询持久化数据处理进度
1073-
* @param persistentId pfop操作返回的持久化处理ID
1087+
* @param persistentId pfop 操作返回的持久化处理ID
10741088
* @param callback
10751089
*/
1076-
prefop(persistentId: string, callback: callback): void;
1090+
prefop(
1091+
persistentId: string,
1092+
callback: callback<{
1093+
id: string,
1094+
pipeline: string,
1095+
code: number,
1096+
desc: string,
1097+
reqid: string,
1098+
inputBucket: string,
1099+
inputKey: string,
1100+
creationDate: string,
1101+
type: number,
1102+
items: {
1103+
cmd: string,
1104+
code: number,
1105+
desc: string,
1106+
returnOld: number,
1107+
error?: string,
1108+
hash?: string,
1109+
}[]
1110+
}>
1111+
): void;
10771112
}
10781113
}
10791114

@@ -1744,6 +1779,7 @@ export declare namespace rs {
17441779
persistentOps?: string;
17451780
persistentNotifyUrl?: string;
17461781
persistentPipeline?: string;
1782+
persistentType?: string;
17471783

17481784
fsizeLimit?: number;
17491785
fsizeMin?: number;

qiniu/fop.js

+52-26
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,42 @@ function OperationManager (mac, config) {
1111
this.config = config || new conf.Config();
1212
}
1313

14-
// 发送持久化数据处理请求
15-
// @param bucket - 空间名称
16-
// @param key - 文件名称
17-
// @param fops - 处理指令集合
18-
// @param pipeline - 处理队列名称
19-
// @param options - 可选参数
20-
// notifyURL 回调业务服务器,通知处理结果
21-
// force 结果是否强制覆盖已有的同名文件
22-
// @param callbackFunc(err, respBody, respInfo) - 回调函数
23-
OperationManager.prototype.pfop = function (bucket, key, fops, pipeline,
24-
options, callbackFunc) {
14+
/**
15+
* @typedef {function(Error, any, IncomingMessage)} OperationCallback
16+
*/
17+
18+
/**
19+
* @param {string} bucket 空间名称
20+
* @param {string} key 文件名称
21+
* @param {string[]} fops 处理指令
22+
* @param {string} pipeline 队列名称
23+
* @param {object} options 可选参数
24+
* @param {string} [options.notifyURL] 回调业务服务器,通知处理结果
25+
* @param {boolean} [options.force] 是否强制覆盖已有的同名文件
26+
* @param {string} [options.type] 为 `1` 时,开启闲时任务
27+
* @param {OperationCallback} callbackFunc 回调函数
28+
*/
29+
OperationManager.prototype.pfop = function (
30+
bucket,
31+
key,
32+
fops,
33+
pipeline,
34+
options,
35+
callbackFunc
36+
) {
2537
options = options || {};
2638
// 必须参数
27-
var reqParams = {
39+
const reqParams = {
2840
bucket: bucket,
2941
key: key,
30-
pipeline: pipeline,
3142
fops: fops.join(';')
3243
};
3344

45+
// pipeline
46+
if (!pipeline) {
47+
delete reqParams.pipeline;
48+
}
49+
3450
// notifyURL
3551
if (options.notifyURL) {
3652
reqParams.notifyURL = options.notifyURL;
@@ -41,6 +57,11 @@ OperationManager.prototype.pfop = function (bucket, key, fops, pipeline,
4157
reqParams.force = 1;
4258
}
4359

60+
const persistentType = parseInt(options.type, 10);
61+
if (!isNaN(persistentType)) {
62+
reqParams.type = options.type;
63+
}
64+
4465
util.prepareZone(this, this.mac.accessKey, bucket, function (err, ctx) {
4566
if (err) {
4667
callbackFunc(err, null, null);
@@ -51,27 +72,32 @@ OperationManager.prototype.pfop = function (bucket, key, fops, pipeline,
5172
};
5273

5374
function pfopReq (mac, config, reqParams, callbackFunc) {
54-
var scheme = config.useHttpsDomain ? 'https://' : 'http://';
55-
var requestURI = scheme + config.zone.apiHost + '/pfop/';
56-
var reqBody = querystring.stringify(reqParams);
57-
var auth = util.generateAccessToken(mac, requestURI, reqBody);
75+
const scheme = config.useHttpsDomain ? 'https://' : 'http://';
76+
const requestURI = scheme + config.zone.apiHost + '/pfop/';
77+
const reqBody = querystring.stringify(reqParams);
78+
const auth = util.generateAccessToken(mac, requestURI, reqBody);
5879
rpc.postWithForm(requestURI, reqBody, auth, callbackFunc);
5980
}
6081

61-
// 查询持久化数据处理进度
62-
// @param persistentId
63-
// @callbackFunc(err, respBody, respInfo) - 回调函数
64-
OperationManager.prototype.prefop = function (persistentId, callbackFunc) {
65-
var apiHost = 'api.qiniu.com';
82+
/**
83+
* 查询持久化数据处理进度
84+
* @param {string} persistentId
85+
* @param {OperationCallback} callbackFunc 回调函数
86+
*/
87+
OperationManager.prototype.prefop = function (
88+
persistentId,
89+
callbackFunc
90+
) {
91+
let apiHost = 'api.qiniu.com';
6692
if (this.config.zone) {
6793
apiHost = this.config.zone.apiHost;
6894
}
6995

70-
var scheme = this.config.useHttpsDomain ? 'https://' : 'http://';
71-
var requestURI = scheme + apiHost + '/status/get/prefop';
72-
var reqParams = {
96+
const scheme = this.config.useHttpsDomain ? 'https://' : 'http://';
97+
const requestURI = scheme + apiHost + '/status/get/prefop';
98+
const reqParams = {
7399
id: persistentId
74100
};
75-
var reqBody = querystring.stringify(reqParams);
101+
const reqBody = querystring.stringify(reqParams);
76102
rpc.postWithForm(requestURI, reqBody, null, callbackFunc);
77103
};

qiniu/storage/form.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function putReq (
222222
* @param {string | null} key
223223
* @param {any} body
224224
* @param {PutExtra | null} putExtra
225-
* @param {reqCallback} callbackFunc
225+
* @param {reqCallback} [callbackFunc]
226226
* @returns {Promise<UploadResult>}
227227
*/
228228
FormUploader.prototype.put = function (

qiniu/storage/rs.js

+1
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,7 @@ function _putPolicyBuildInKeys () {
16951695
* @property {string} [persistentOps]
16961696
* @property {string} [persistentNotifyUrl]
16971697
* @property {string} [persistentPipeline]
1698+
* @property {string} [persistentType]
16981699
* @property {number} [fsizeLimit]
16991700
* @property {number} [fsizeMin]
17001701
* @property {string} [mimeLimit]

0 commit comments

Comments
 (0)