-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BlobStartCopyFromUrlPoller.ts
241 lines (217 loc) · 6.95 KB
/
BlobStartCopyFromUrlPoller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { delay } from "@azure/core-http";
import { PollOperation, PollOperationState, Poller } from "@azure/core-lro";
import { BlobClient, BlobStartCopyFromURLOptions, BlobBeginCopyFromURLResponse } from "../Clients";
/**
* Defines the operations from a {@link BlobClient} that are needed for the poller
* returned by {@link BlobClient.beginCopyFromURL} to work.
*/
export type CopyPollerBlobClient = Pick<BlobClient, "abortCopyFromURL" | "getProperties"> & {
startCopyFromURL(
copySource: string,
options?: BlobStartCopyFromURLOptions
): Promise<BlobBeginCopyFromURLResponse>;
};
/**
* The state used by the poller returned from {@link BlobClient.beginCopyFromURL}.
*
* This state is passed into the user-specified `onProgress` callback
* whenever copy progress is detected.
*/
export interface BlobBeginCopyFromUrlPollState
extends PollOperationState<BlobBeginCopyFromURLResponse> {
/**
* The instance of {@link BlobClient} that was used when calling {@link BlobClient.beginCopyFromURL}.
*/
readonly blobClient: CopyPollerBlobClient;
/**
* The copyId that identifies the in-progress blob copy.
*/
copyId?: string;
/**
* the progress of the blob copy as reported by the service.
*/
copyProgress?: string;
/**
* The source URL provided in {@link BlobClient.beginCopyFromURL}.
*/
copySource: string;
/**
* The options that were passed to the initial {@link BlobClient.beginCopyFromURL} call.
* This is exposed for the poller and should not be modified directly.
*/
readonly startCopyFromURLOptions?: BlobStartCopyFromURLOptions;
}
/**
* The PollOperation responsible for:
* - performing the initial startCopyFromURL
* - checking the copy status via getProperties
* - cancellation via abortCopyFromURL
* @hidden
*/
export interface BlobBeginCopyFromURLPollOperation
extends PollOperation<BlobBeginCopyFromUrlPollState, BlobBeginCopyFromURLResponse> {}
/**
* The set of options used to configure the poller.
* This is an internal interface populated by {@link BlobClient.beginCopyFromURL}.
*
* @hidden
*/
export interface BlobBeginCopyFromUrlPollerOptions {
blobClient: CopyPollerBlobClient;
copySource: string;
intervalInMs?: number;
onProgress?: (state: BlobBeginCopyFromUrlPollState) => void;
resumeFrom?: string;
startCopyFromURLOptions?: BlobStartCopyFromURLOptions;
}
/**
* This is the poller returned by {@link BlobClient.beginCopyFromURL}.
* This can not be instantiated directly outside of this package.
*
* @hidden
*/
export class BlobBeginCopyFromUrlPoller extends Poller<
BlobBeginCopyFromUrlPollState,
BlobBeginCopyFromURLResponse
> {
public intervalInMs: number;
constructor(options: BlobBeginCopyFromUrlPollerOptions) {
const {
blobClient,
copySource,
intervalInMs = 15000,
onProgress,
resumeFrom,
startCopyFromURLOptions
} = options;
let state: BlobBeginCopyFromUrlPollState | undefined;
if (resumeFrom) {
state = JSON.parse(resumeFrom).state;
}
const operation = makeBlobBeginCopyFromURLPollOperation({
...state,
blobClient,
copySource,
startCopyFromURLOptions
});
super(operation);
if (typeof onProgress === "function") {
this.onProgress(onProgress);
}
this.intervalInMs = intervalInMs;
}
public delay(): Promise<void> {
return delay(this.intervalInMs);
}
}
/**
* Note: Intentionally using function expression over arrow function expression
* so that the function can be invoked with a different context.
* This affects what `this` refers to.
* @hidden
*/
const cancel: BlobBeginCopyFromURLPollOperation["cancel"] = async function cancel(
this: BlobBeginCopyFromURLPollOperation,
options = {}
) {
const state = this.state;
const { copyId } = state;
if (state.isCompleted) {
return makeBlobBeginCopyFromURLPollOperation(state);
}
if (!copyId) {
state.isCancelled = true;
return makeBlobBeginCopyFromURLPollOperation(state);
}
// if abortCopyFromURL throws, it will bubble up to user's poller.cancelOperation call
await state.blobClient.abortCopyFromURL(copyId, {
abortSignal: options.abortSignal
});
state.isCancelled = true;
return makeBlobBeginCopyFromURLPollOperation(state);
};
/**
* Note: Intentionally using function expression over arrow function expression
* so that the function can be invoked with a different context.
* This affects what `this` refers to.
* @hidden
*/
const update: BlobBeginCopyFromURLPollOperation["update"] = async function update(
this: BlobBeginCopyFromURLPollOperation,
options = {}
): Promise<BlobBeginCopyFromURLPollOperation> {
const state = this.state;
const { blobClient, copySource, startCopyFromURLOptions } = state;
if (!state.isStarted) {
state.isStarted = true;
const result = await blobClient.startCopyFromURL(copySource, startCopyFromURLOptions);
// copyId is needed to abort
state.copyId = result.copyId;
if (result.copyStatus === "success") {
state.result = result;
state.isCompleted = true;
}
} else if (!state.isCompleted) {
try {
const result = await state.blobClient.getProperties({ abortSignal: options.abortSignal });
const { copyStatus, copyProgress } = result;
const prevCopyProgress = state.copyProgress;
if (copyProgress) {
state.copyProgress = copyProgress;
}
if (
copyStatus === "pending" &&
copyProgress !== prevCopyProgress &&
typeof options.fireProgress === "function"
) {
// trigger in setTimeout, or swallow error?
options.fireProgress(state);
} else if (copyStatus === "success") {
state.result = result;
state.isCompleted = true;
} else if (copyStatus === "failed") {
state.error = new Error(
`Blob copy failed with reason: "${result.copyStatusDescription || "unknown"}"`
);
state.isCompleted = true;
}
} catch (err) {
state.error = err;
state.isCompleted = true;
}
}
return makeBlobBeginCopyFromURLPollOperation(state);
};
/**
* Note: Intentionally using function expression over arrow function expression
* so that the function can be invoked with a different context.
* This affects what `this` refers to.
* @hidden
*/
const toString: BlobBeginCopyFromURLPollOperation["toString"] = function toString(
this: BlobBeginCopyFromURLPollOperation
) {
return JSON.stringify({ state: this.state }, (key, value) => {
// remove blobClient from serialized state since a client can't be hydrated from this info.
if (key === "blobClient") {
return undefined;
}
return value;
});
};
/**
* Creates a poll operation given the provided state.
* @hidden
*/
function makeBlobBeginCopyFromURLPollOperation(
state: BlobBeginCopyFromUrlPollState
): BlobBeginCopyFromURLPollOperation {
return {
state: { ...state },
cancel,
toString,
update
};
}