Skip to content

Commit 9b57f0a

Browse files
bedgerottokbs-oddMentthewdragoscirjanDragos Cirjan
authored
Add onBeforeRequest and onAfterResponse to terminate method (#216)
* #209 Add onBeforeRequest and onAfterResponse to terminate method * Call onAfterResponse even if the request was not successfull * Correct type definition for Upload#terminate (#217) inconsistency between typescript typing and function definition * Allow Promise return type for onBeforeRequest and onAfterResponse. (#212) * Allow Promise return type for onBeforeRequest and onAfterResponse. * Allow Promise return type for onBeforeRequest and onAfterResponse: documentation and test case. * Allow to specify options for Node's request method (#203) * adding nodejs HttpStack request options * fix * fix * fix * visual fix * proposal for request options * disabling browser * fix * Clarify how to import using ESM syntax * Added onShouldRetry callback for controlling retries (#198) * Added onShouldRetry callback for controlling retry Whenever the library is about to retry an upload due to an error, the new optional callback onShouldRetry will be called when defined. Its return value will tell the library whether to actually retry the upload or fail with an error, for example based on status code checks. This makes it possible to customize the behavior like reacting on specific status codes. * Move onShouldRetry example to usage.md * Refactored shouldRetry logic - isOnline is now excluded from the check, so the onShouldRetry callback should manually add an online check if desired - removed inline function and made the conditions more readable - clarified inline comments about status code check * Add test assertion for onShouldRetry arguments * Update usage.md * Update test-common.js * Update api.md Co-authored-by: Marius <[email protected]> * making property 'private', adding small test for 'insecure' request * fixes ? * fix: import * Update js-base64 to a version which does not use eval() anymore Closes #147 * PR comments fixes * fixed exports on node, addex HttpStack export on browser * added new test case - node 14 * test fixes * test fixes * fix tests * Bump lodash from 4.17.14 to 4.17.19 Bumps [lodash](https://github.com/lodash/lodash) from 4.17.14 to 4.17.19. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](lodash/lodash@4.17.14...4.17.19) Signed-off-by: dependabot[bot] <[email protected]> * Clean up request initialization and add proper test Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: Vincent Petry <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Kevin van Zonneveld <[email protected]> * Bump elliptic from 6.3.1 to 6.5.3 (#214) Bumps [elliptic](https://github.com/indutny/elliptic) from 6.3.1 to 6.5.3. - [Release notes](https://github.com/indutny/elliptic/releases) - [Commits](indutny/elliptic@v6.3.1...v6.5.3) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Update dependencies using 'npm audit fix' * Do not pass URL as separate parameter This is not supported in Node.js 8 and 9. * Downgrade tsd to support Node.js 8 and 9 See https://github.com/SamVerschueren/tsd/releases/tag/v0.12.1 * Correct type definition for Upload#terminate (#217) inconsistency between typescript typing and function definition * Minor cleanups Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: Vincent Petry <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Kevin van Zonneveld <[email protected]> Co-authored-by: kabaliserv <[email protected]> * Allow to return Promises in onBeforeRequest for terminate Co-authored-by: kabaliserv <[email protected]> Co-authored-by: Mentthew <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: Vincent Petry <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Kevin van Zonneveld <[email protected]>
1 parent b6907d8 commit 9b57f0a

File tree

3 files changed

+64
-65
lines changed

3 files changed

+64
-65
lines changed

lib/upload.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ class BaseUpload {
115115
}
116116

117117
const req = openRequest("DELETE", url, options);
118-
const promise = req.send();
119118

120-
return promise.then((res) => {
119+
return sendRequest(req, null, options).then((res) => {
121120
// A 204 response indicates a successfull request
122121
if (res.getStatus() === 204) {
123122
return;
@@ -828,26 +827,12 @@ class BaseUpload {
828827
}
829828

830829
/**
831-
* Send a request with the provided body while invoking the onBeforeRequest
832-
* and onAfterResponse callbacks.
830+
* Send a request with the provided body.
833831
*
834832
* @api private
835833
*/
836834
_sendRequest(req, body = null) {
837-
const onBeforeRequestPromise = (typeof this.options.onBeforeRequest === "function")
838-
? Promise.resolve(this.options.onBeforeRequest(req))
839-
: Promise.resolve();
840-
841-
return onBeforeRequestPromise.then(() => {
842-
return req.send(body)
843-
.then((res) => {
844-
const onAfterResponsePromise = (typeof this.options.onAfterResponse === "function")
845-
? Promise.resolve(this.options.onAfterResponse(req, res))
846-
: Promise.resolve();
847-
848-
return onAfterResponsePromise.then(() => res);
849-
});
850-
});
835+
return sendRequest(req, body, this.options);
851836
}
852837
}
853838

@@ -896,6 +881,29 @@ function openRequest(method, url, options) {
896881
return req;
897882
}
898883

884+
/**
885+
* Send a request with the provided body while invoking the onBeforeRequest
886+
* and onAfterResponse callbacks.
887+
*
888+
* @api private
889+
*/
890+
function sendRequest(req, body, options) {
891+
const onBeforeRequestPromise = (typeof options.onBeforeRequest === "function")
892+
? Promise.resolve(options.onBeforeRequest(req))
893+
: Promise.resolve();
894+
895+
return onBeforeRequestPromise.then(() => {
896+
return req.send(body)
897+
.then((res) => {
898+
const onAfterResponsePromise = (typeof options.onAfterResponse === "function")
899+
? Promise.resolve(options.onAfterResponse(req, res))
900+
: Promise.resolve();
901+
902+
return onAfterResponsePromise.then(() => res);
903+
});
904+
});
905+
}
906+
899907
/**
900908
* Checks whether the browser running this code has internet access.
901909
* This function will always return true in the node.js environment

test/spec/test-common.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,52 +1248,5 @@ describe("tus", function () {
12481248
expect(options.onError).not.toHaveBeenCalled();
12491249
expect(options.onSuccess).toHaveBeenCalled();
12501250
});
1251-
1252-
it("should invoke the request and response Promises", async function () {
1253-
const testStack = new TestHttpStack();
1254-
var file = getBlob("hello world");
1255-
var options = {
1256-
httpStack: testStack,
1257-
uploadUrl: "http://tus.io/uploads/foo",
1258-
onBeforeRequest: function (req) {
1259-
return new Promise(resolve => {
1260-
expect(req.getURL()).toBe("http://tus.io/uploads/foo");
1261-
expect(req.getMethod()).toBe("HEAD");
1262-
resolve();
1263-
});
1264-
},
1265-
onAfterResponse: function (req, res) {
1266-
return new Promise(resolve => {
1267-
expect(req.getURL()).toBe("http://tus.io/uploads/foo");
1268-
expect(req.getMethod()).toBe("HEAD");
1269-
expect(res.getStatus()).toBe(204);
1270-
expect(res.getHeader("Upload-Offset")).toBe(11);
1271-
resolve();
1272-
});
1273-
},
1274-
onSuccess: waitableFunction("onSuccess")
1275-
};
1276-
spyOn(options, "onBeforeRequest");
1277-
spyOn(options, "onAfterResponse");
1278-
1279-
var upload = new tus.Upload(file, options);
1280-
upload.start();
1281-
1282-
var req = await testStack.nextRequest();
1283-
expect(req.url).toBe("http://tus.io/uploads/foo");
1284-
expect(req.method).toBe("HEAD");
1285-
1286-
req.respondWith({
1287-
status: 204,
1288-
responseHeaders: {
1289-
"Upload-Offset": 11,
1290-
"Upload-Length": 11
1291-
}
1292-
});
1293-
1294-
await options.onSuccess.toBeCalled;
1295-
expect(options.onBeforeRequest).toHaveBeenCalled();
1296-
expect(options.onAfterResponse).toHaveBeenCalled();
1297-
});
12981251
});
12991252
});

test/spec/test-terminate.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,43 @@ describe("tus", function () {
152152

153153
await expectAsync(terminatePromise).toBeRejectedWithError(/tus: unexpected response while terminating upload/);
154154
});
155+
156+
it("should invoke the request and response Promises", async function () {
157+
const testStack = new TestHttpStack();
158+
var options = {
159+
httpStack: testStack,
160+
onBeforeRequest: function (req) {
161+
return new Promise(resolve => {
162+
expect(req.getURL()).toBe("http://tus.io/uploads/foo");
163+
expect(req.getMethod()).toBe("DELETE");
164+
resolve();
165+
});
166+
},
167+
onAfterResponse: function (req, res) {
168+
return new Promise(resolve => {
169+
expect(req.getURL()).toBe("http://tus.io/uploads/foo");
170+
expect(req.getMethod()).toBe("DELETE");
171+
expect(res.getStatus()).toBe(204);
172+
resolve();
173+
});
174+
}
175+
};
176+
spyOn(options, "onBeforeRequest");
177+
spyOn(options, "onAfterResponse");
178+
179+
const terminatePromise = tus.Upload.terminate("http://tus.io/uploads/foo", options);
180+
181+
let req = await testStack.nextRequest();
182+
expect(req.url).toBe("http://tus.io/uploads/foo");
183+
expect(req.method).toBe("DELETE");
184+
185+
req.respondWith({
186+
status: 204
187+
});
188+
189+
await expectAsync(terminatePromise).toBeResolved();
190+
expect(options.onBeforeRequest).toHaveBeenCalled();
191+
expect(options.onAfterResponse).toHaveBeenCalled();
192+
});
155193
});
156194
});

0 commit comments

Comments
 (0)