Is it possible to read a portion of a blob without copying it through blob.splice()? - binary

I'm trying to pass an image stored as binary to the client through a web socket and display it. However, the first byte of the binary payload is an integer denoting a request id.
Is this the correct way to read that byte and convert to an unsigned integer?
Is this inefficient since the rest of the blob, excluding the first byte, is copied over using evt.data.splice(1)? The MDN document on blob.splice() seems to indicate that it returns a new blob. Does this mean that this method will require twice as much memory since it makes a new copy of the image data less the first byte?
Is there a more efficient method that perhaps just reads from evt.data starting at the first byte rather than copying it first?
Thank you.
WEBS.socks[name].onmessage = function(evt) {
if (evt.data instanceof Blob) {
evt.data.slice(0,1).arrayBuffer()
.then( (b) => {
let v = new DataView(b);
if ( v.getUint8() === 123 ) {
let objectURL = URL.createObjectURL( evt.data.slice(1) );
imgscan.onload = () => { URL.revokeObjectURL( objectURL ); };
imgscan.src = objectURL;
}
});
}
};

Related

IPFS js - is it possible to know what peers are holding what data?

I have this snippet here from https://js.ipfs.io/:
const node = await IPFS.create()
const stream = node.cat('QmPChd2hVbrJ6bfo3WBcTW4iZnpHm8TEzWkLHmLpXhF68A')
let data = ''
for await (const chunk of stream) {
// chunks of data are returned as a Buffer, convert it back to a string
data += chunk.toString()
}
console.log(data)
It prints fine and I can the data.
My questions:
Where exactly is this data corresponding to the hash stored? Is it possible to know what peers are storing this data?
What happens if I want to change the data? Do I have to generate a new hash every time?
If your node has a copy of the data, it'll be stored in your local repo.
If it does not, it'll use bitswap to try to fetch it from connected peers.
You can find out which peers have the data by querying the DHT using ipfs.dht.findProvs.
The 'QmPCh..' string is a content identifier so if the data changes, so will the hash.

Typescript JSON How do I read JSON using hierarchical key like "appInsights:instrumentationKey"

Given the below JSON, I should be able to read the JSON using hierarchical key path such as "appInsights:instrumentationKey". How can i do this in an angular project?
{
"appInsights": {
"instrumentationKey": "<dev-guid-here>"
}
},
Is this what you are looking for ?
const obj = JSON.parse('your json');
const val1 = obj.appInsights.instrumentationKey;
const val2 = obj['appInsights'].instrumentationKey;
UPDATE: Will has fair point. If your JSON data doesnt contain user input e.g won't contain anything harmful that will execute in browser then
const val = eval('obj.' + keyPath.replace(/:/g, '.'));
or if you use Lodash: get
Something along these lines perhaps:
getJsonProperty(jsonObj, keyPath: string) {
const keys: string[] = keyPath.split(':');
return jsonObj[keys[0]][keys[1]];
}
If you don't know how many levels the object can have, you could write a switch statement with cases for each accepted length of the keys Array. Obviously that means hard-coding each case (and a fixed maximum number of possible levels), unless there's some other way of using string literals I'm forgetting.

How to extract value from JSON object in React?

I have a case where my JSON object needs remapping first e.g. from
{ name: goals, value: 65 } to { goals: 65}. I believe I achieved that with reduce in getStats function (see screenshot from the console)
However I want to wrap it in a function i.e. getData(name){}
where I will be passing name = 'goals' and get the specific value 65
How to achieve that and simplify my code?
Create simple getData function which returns value for key.
getData(key, defaultValue = '') {
// I suggest to cache result of next call (invalidate cache when needed)
const data = this.getStats(playerStats);
return data[key] || defaultValue;
}

How to get the values from the Json using Jquery

Hi I'm new to Jquery and i don't know how to use json data. I have a Json which is coming as a response. My success function is
success: function(resp)
{
var x = JSON.stringify(resp);
alert(x);
}
alert is having following JSON
{"xyz":{"abc":[{"action":"fail","result":"xxx"},{"action":"pass","resut":"yyy"}]}}
I want action value alone. How can i get this value from x variable and how can i utilize this value in HTML. Thanks in advance.
When you use JSON.stringify() you are turning it into a string, you want it as an object to access the data. Try this:
success: function(resp) {
alert(resp.xyz.abc[0].action);
var x = resp.xyz.abc[0].action // to use the value in html later on
}
If it is returned as a string (I can't tell at this point), you can turn it into an object (as long as it is valid JSON) by using $.parseJSON()
success: function(resp)
{
var x = $.parseJSON(resp);
var xyz = x.xyz;
var pass = x.xyz.abc[1].action;
}
or you can loop though each of the array by $.each
success: function(resp)
{
var x = $.parseJSON(resp);
$.each(x.xyz.abc, function(index, element){
alert('action:' + element.action + ', result:' + element.resut)
});
}
i think, and don't take it personally,that your JSON object is not well organized as an object to get and to have. Action,from my perspective is either fail or success, and reading it, as you saw in the above good answer, will give you exactly what you want.
What's the point in getting a JSON with data structured like you did, with 2 possible answers encoded in it, when there is only one available (either success or fail).

problems reading int from bytearray

This is my first question, so do not judge strictly.
I have an object that I'm getting from php server to as3(flash) client. That object is AMF encoded, so I write server response to ByteArray:
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes( rawData );
and than I'm reading object from ByteArray:
ba.position = 0;
var response:Object = ba.readObject();
Part of object contains such data:
{
'money' : 900
}
And when reading object from ByteArray, I get a seven-digit number ~ 1824344 instead of 900. But when I get form server String '900' or int value equals 100 - data reads correctly.
Has someone had such a problem?
You have to read the same way you wrote. If you write something using writeUTFBytes(), you have to read it using readUTFBytes().
In this case you should use writeObject() and readObject() because you are writing pure Object but not String.