Is it possible to use Uint8Array from within a javascript callout?

vikastiwari2
Participant II

Hi All,

In my js script file one of the functions using following line:

var bytes =new Uint8Array(arrayBuffer);

While executing the call flow, the JS is throwing the error "undefined Uint8Array".

Is there any way to get it work within APIGEE Edge?

*Please note one of my clients using npm package and this line of code is part of npm package code. I have converted node.js code to simple javascript, as we don't have any node application running.

I am not sure if UInt8Array is supported within APIGEE or any other workaround for this issue.

Thanks.

Solved Solved
1 7 1,867
1 ACCEPTED SOLUTION

I have an answer, but it's a bit unsatisfying, I'm afraid.

Apigee Edge uses at least Rhino version 1.7.7.1 (possibly 1.7.10, not sure) to run the JavaScript callouts.

However, not all of the version 1.7 language features including typed arrays (Added in 1.7.6) are available due to how the Rhino context is initialized. See also, this question.

There's an outstanding feature request to turn this capability on inside JavaScript callouts. Reference: b/67167697 .

Contact your Apigee support person if you would like to escalate the priority of the ticket.

I understand what you wrote about the npm module and not running node. If you can describe just what you're trying to do in more detail we may be able to find a workaround for you, in lieu of direct Uint8Array support.

View solution in original post

7 REPLIES 7

I have an answer, but it's a bit unsatisfying, I'm afraid.

Apigee Edge uses at least Rhino version 1.7.7.1 (possibly 1.7.10, not sure) to run the JavaScript callouts.

However, not all of the version 1.7 language features including typed arrays (Added in 1.7.6) are available due to how the Rhino context is initialized. See also, this question.

There's an outstanding feature request to turn this capability on inside JavaScript callouts. Reference: b/67167697 .

Contact your Apigee support person if you would like to escalate the priority of the ticket.

I understand what you wrote about the npm module and not running node. If you can describe just what you're trying to do in more detail we may be able to find a workaround for you, in lieu of direct Uint8Array support.

Hi Dino,

Thanks for your reply, let me describe my case in detail:

We have created APIGEE flow to integrate with one of our partner system. they have authentication API (which authenticate and generates token for all incoming requests), to do this they have implemented custom logic on top of crypto.js.

While they are generating token, at one place they are using below code:

var base64Token = byteArrayToBase64(byteArray);
function byteArrayToBase64 (arrayBuffer) {
  var base64 = '';
  var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  var bytes = new Uint8Array(arrayBuffer);
  var byteLength = bytes.byteLength;
  var byteRemainder = byteLength % 3;
  var mainLength = byteLength - byteRemainder;
  .....

they have published entire code through npm package, but we didn't have any node app running so I converted npm code into javascript and tried to use through javacallout, that's where I am getting this error.

Thanks,

Vikas

I have a couple options for you.

There are multiple base64 codecs available. Some of them are able to run in environments that lack Uint8Array. For example I think this is one example. It should work within your JS callout.

This is a codec I have used in JavaScript callouts. It works with strings as input. You could rework it to make it also work on byte arrays.

finally, if what you want is more crypto capability, I have used the SJCL library within the JS callout successfully.

You haven't described what you *Really* want to do. I guess it's not as simple as "base64 an array". What does the JS callout do?

My Javascript policy executes following steps as different functions, to generate token then service callout will send this token as header to external system and requests will be validated based on correct token.

using CryptoJS to take SHA-256 of cleartext

convert the resulting hex representation of those bytes to UTF-16 characters

get the resulting bytes of our new UTF-16 hex string

encode the resulting byte as Base64

let me try JS script that you have mentioned if that help.

You used the word "token", and the description you provided suggests that you are producing a JWT.

If so, have you tried the builtin GenerateJWT policy in Apigee Edge?

No its not JWT token, the token is just name I used.

My base64 string it token here, which we pass to outside application through service call out for validation.

Thanks for your help, I tried codec.js with little modification and it worked for me.