-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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(http): better binary support & XHR support #7707
feat(http): better binary support & XHR support #7707
Conversation
Thanks @charsleysa , really appreciate this. Few things:
FWIW I'm willing to test and write the docs |
Hi @rynop Regarding your first point, you are correct that binary download support is not currently available through angular due to XHR not having binary response support. I will add in arraybuffer responseType support to XHR and include it in this PR. As for your second point, any angular tutorial for arraybuffer should work. Here's the http snippet from our app when we upload a user profile image: this.http
.request(
new HttpRequest('PUT', uploadUrl, data, {
headers: new HttpHeaders({
'Content-Type': fileType,
'Content-Disposition': `attachment; filename="${fileName}"`
})
})
) If you're able to write tests and docs that'd be a great help! |
@rynop I've implemented full support (within limitations) for Blob and ArrayBuffer in both upload and download for XHR, updated the Fetch API polyfill. I've also added some tests but this PR will probably need a bit of testing against a few apps to make sure nothing is horribly broken. |
@charsleysa I'm having trouble testing your fork. I'm following the NS dev guidelines without luck. I'm:
I get TONS of errors, here is one:
When I look at the Any idea what I'm doing wrong? I've tried |
@rynop did you solve your problem and get this running? |
@charsleysa nope, I think there is a |
The problem with linking is that Angular/TS project tend to grab the linked TS files in core modules and try to compile them without the references needed. I've only managed to run it by compiling the core modules with outDir and linking that (.d.ts, package.json and other files, except TS ones should be copied over too). Maybe the team can consider moving to an outDir compilation. :) Another way would be dev-webpack to forbid Angular/ts-loader for traversing node_modules. |
@charsleysa It was a long (30+) day journey but I finally was able to verify the changes in the PR work great when using binary protobuf payloads. Can't tell you how much I appreciate this PR, I really think it is a big step forward for NS and gives a competitive advantage over React Native (which last time I checked has problems passing binary data over their JS bridge). Now the details. Workaround so I could test: What I tested In my test, I ran against a Twirp RPC server. For those curious, I used https://github.com/rynop/abp-sam-twirp almost verbatim. I then created an Angular NS project, and an Angular service with the method below, which invokes the code in this PR.
I successfully tested both green and error (500/404/400 etc) paths. I also verified binary in the request body AND binary in the response body. There is quite a bit more to the protobuf aspect, but I don't want to bloat this PR. I'll blog and/or post more details for others wanting this use case in #7632 @bundyo the only issue I see is the potentially breaking change ( |
@manoldonev Any chance this hitting 6.2? |
We really need this as well! Simply trying to upload a raw image to S3.. |
@bundyo most probably will not ship with 6.2 but this PR is definitely on our radar and we will do our best to process it as soon as possible (and release it officially with 6.3) |
I should have noted that my test above uses For completeness sake, I just tested public createTwirpRPCAdapter(servicePathPrefix: string, headerOverrides?: HttpHeaders): $protobuf.RPCImpl {
return (serviceMethodName: $protobuf.Method, requestData: Uint8Array, callback: Function) => {
fetch(servicePathPrefix + TwirpService.getServiceMethodName(serviceMethodName),
{
method: 'POST',
headers: {
'Accept': 'application/protobuf',
'Content-Type': 'application/protobuf'
},
body: requestData.slice().buffer,
})
.then(response => response.arrayBuffer())
.then(body => callback(null, new Uint8Array(body)))
.catch(error => {
callback(TwirpError.isTwirpError(error) ? new TwirpError(error) : error, null);
});
};
} Sending binary in the request body and getting binary in the response body works in my tests via |
Hello! |
@manoldonev since this did not make it into v6.3 as hoped, can you confirm it is slated to go into v6.4? |
Is the PR stuck for some reason? |
From my perspective it is ready to go. I tested multiple scenarios on multiple devices/emulators/simulators. I'm guessing the team wants to get more testing (wise). |
tns-core-modules-widgets/android/widgets/src/main/java/org/nativescript/widgets/Async.java
Outdated
Show resolved
Hide resolved
…into origin-master
Wahooo!!! Thanks for everyone's hard work on this! Especially you @charsleysa |
I'm trying to upload image in binary mode using Angular's |
PR Checklist
What is the current behavior?
HTTP module does not support uploading binary information, only text based information.
XHR doesn't follow spec.
What is the new behavior?
HTTP module now supports uploading binary information through the use of ArrayBuffer.
XHR now better follows spec, now has support for Blob and ArrayBuffer.
Implements #570.
Closes #7632.
Closes NativeScript/nativescript-angular#1202.
BREAKING CHANGES:
Possibly a breaking change if anyone is using the
org.nativescript.widgets2.Async.Http
Java class directly asRequestOptions.content
has changed from a typeString
to typejava.nio.ByteBuffer
, though I'm not sure if this is considered to be an internal API.Also, XHR is now more compliant with the spec which means some code that may have previously worked (or failed silently) will now cause an error.
Migration steps:
The content string must be converted to a
java.nio.ByteBuffer
instead of setting it directly to the request options. Here's an example of converting a content string: