Skip to content

Commit

Permalink
implementing delta from #261 with small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Radtke authored and Ron Radtke committed Dec 4, 2023
1 parent 8df0134 commit 5dd72a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,9 @@ private void releaseTaskResource() {
*/
private void done(Response resp) {
boolean isBlobResp = isBlobResponse(resp);
WritableMap respmap = getResponseInfo(resp,isBlobResp);
emitStateEvent(respmap.copy());

emitStateEvent(getResponseInfo(resp, isBlobResp));
switch (responseType) {
case KeepInMemory:
Expand All @@ -656,7 +659,7 @@ private void done(Response resp) {
ins.close();
os.flush();
os.close();
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest);
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest, respmap.copy());
}
// response data directly pass to JS context as string.
else {
Expand All @@ -678,11 +681,11 @@ private void done(Response resp) {
invoke_callback("Error from file transformer:" + e.getLocalizedMessage(), null);
return;
}
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath, respmap.copy());
return;
}
if (responseFormat == ResponseFormat.BASE64) {
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP), respmap.copy());
return;
}
try {
Expand All @@ -699,9 +702,9 @@ private void done(Response resp) {
catch (CharacterCodingException ignored) {
if (responseFormat == ResponseFormat.UTF8) {
String utf8 = new String(b);
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8, respmap.copy());
} else {
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP), respmap.copy());
}
}
}
Expand Down Expand Up @@ -746,16 +749,16 @@ private void done(Response resp) {
}

if (ReactNativeBlobUtilFileResp != null && !ReactNativeBlobUtilFileResp.isDownloadComplete()) {
invoke_callback("Download interrupted.", null);
invoke_callback("Download interrupted.", null, respmap.copy());
} else {
this.destPath = this.destPath.replace("?append=true", "");
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath, respmap.copy());
}

break;
default:
try {
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"));
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"), respmap.copy());
} catch (IOException e) {
invoke_callback("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
}
Expand Down
7 changes: 5 additions & 2 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function fetch(...args: any): Promise {
let taskId = getUUID();
let options = this || {};
let subscription, subscriptionUpload, stateEvent, partEvent;
let respInfo = {};
let respInfo = {'uninit': true};
let [method, url, headers, body] = [...args];

// # 241 normalize null or undefined headers, in case nil or null string
Expand Down Expand Up @@ -242,8 +242,9 @@ export function fetch(...args: any): Promise {
* in JS context, and this parameter indicates which one
* dose the response data presents.
* @param data {string} Response data or its reference.
* @param responseInfo {Object.<>}
*/
req(options, taskId, method, url, headers || {}, body, (err, rawType, data) => {
req(options, taskId, method, url, headers || {}, body, (err, rawType, data, responseInfo) => {

// task done, remove event listeners
subscription.remove();
Expand All @@ -269,6 +270,8 @@ export function fetch(...args: any): Promise {
fs.session(options.session).add(data);
}
respInfo.rnfbEncode = rawType;
if (uninit in respInfo && respInfo.uninit) // event didn't fire yet so we override it here
respInfo = responseInfo;
resolve(new FetchBlobResponse(taskId, respInfo, data));
}

Expand Down
7 changes: 6 additions & 1 deletion ios/ReactNativeBlobUtilRequest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,16 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
}
}

NSHTTPURLResponse *response = (NSHTTPURLResponse *) [task response];

callback(@[
errMsg ?: [NSNull null],
rnfbRespType ?: @"",
respStr ?: [NSNull null]
respStr ?: [NSNull null],
@{
@"status": [NSNumber numberWithInteger:[response statusCode]]
}
]);

This comment has been minimized.

Copy link
@hatem-72

hatem-72 Dec 4, 2023

I think there is a merge issue here.

- ]);
+  

This comment has been minimized.

Copy link
@AEP20

AEP20 Dec 4, 2023

yes this here causes syntax error

]);

respData = nil;
Expand Down

0 comments on commit 5dd72a1

Please sign in to comment.