Debugging Server Side JS


(Keith Brown) #1

Matrix Version:

5.4.0.1

…are there any gotchas / ‘have you tried this’ type approaches when things don’t work? This is not a criticism - it’s just pretty difficult to debug ( and that is maybe my issue). I have some JS that works when executing in the browser. it doesn’t work with ‘runat=server’ so I copied it to the server environment in a new test.js file and executed with the same binary that squiz is setup to use (spidermonkey js) and it works. Summary - it works in the client - it works as SS js - it doesn’t work when matrix passed it through to the binary!

much confused and any help appreciated!

K


(Bart Banda) #2

Have you got any more info than that to share with us? What is the SSJS you are trying to run? And where?
Do you get any errors in the matrix error logs?
Any replication steps would be handy so that we can try and help in the best way possible.


(Keith Brown) #3

Hi

Ok - so feed coming from tumblr api doesn’t include a ‘feature’ image that could be used as ‘thumbnail’ on the asset listing so… I wanted to extract the first one from the content (excuse the crappy regex - just wanted to get something that worked then I’ll tidy up). Although I’ve using a cut down / mocked up version…

<script runat="server" log="log_prefix"> 
//var contents = '%ds__body%';

var contents = 'lets try this one <img src="http://68.media.tumblr.com/b9f8690bb8f210db14824caf86b56811/tumblr_inline_obc5vsELaB1tux20n_540.jpg" alt="image" data-orig-width="1395" data-orig-height="1536"/>';

var images = contents.match(/<img[^>]+src="?([^"\s]+)"?\salt="?([^"\s]+)"?\sdata-orig-width="?([^"\s]+)"?\sdata-orig-height="?([^"\s]+)"*\/>/g);

print(images);

</script>

…as I say runs fine if you console.log(images) in the client or if you just put the above into a .js file and run it on the server from the command line, The log-prefix part doesn’t return any errors in the matrix system log. No relevant errors in the matrix error log.

…was really just wondering where to go next with the debugging

tia

K

edited to include code block which was hidden without the code tags


(Bart Banda) #4

hmm, interesting.

Can you simplify the code block to narrow down where the issue might be?

For example, does this work?

<script runat="server" log="log_prefix"> 

var contents = 'lets try this one <img src="http://68.media.tumblr.com/b9f8690bb8f210db14824caf86b56811/tumblr_inline_obc5vsELaB1tux20n_540.jpg" alt="image" data-orig-width="1395" data-orig-height="1536"/>';

print(contents);
</script>

(Keith Brown) #5

…yeah that works - as does the original JS above if you run it in the client (or at the command line)

I also tried a different approach using the xpath keyword modifier but that doesn’t work either! The frst bit of HTML in the %ds__body% keyword prints out as:

<div><h2><b>18 things to do while living in Edinburgh</b></h2><p>I have been living in Edinburgh for two years, but there is still so much I have not done and so many places I have not seen. Don’t be like me and make the most out of your time here!</p><h2>1. Watch the sunrise from the top of Arthur’s Seat</h2><figure class="tmblr-embed tmblr-full" data-provider="youtube" data-orig-width="540" data-orig-height="304" data-url="https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DCHboZaW1WBQ"><iframe width="540" height="304" id="youtube_iframe" src="https://www.youtube.com/embed/CHboZaW1WBQ?feature=oembed&amp;enablejsapi=1&amp;origin=https://safe.txmblr.com&amp;wmode=opaque" frameborder="0" allowfullscreen=""></iframe></figure><p>(via <a href="https://www.youtube.com/watch?v=CHboZaW1WBQ" target="_blank">https://www.youtube.com/watch?v=CHboZaW1WBQ</a>) </p><h2>2. Go to the Stockbridge Market  on Sundays</h2><figure data-orig-width="1395" data-orig-height="1536" class="tmblr-full"><img src="http://68.media.tumblr.com/b9f8690bb8f210db14824caf86b56811/tumblr_inline_obc5vsELaB1tux20n_540.jpg" alt="image" data-orig-width="1395" data-orig-height="1536"/></figure>

That should work with:

<ul>
%ds__body^xpath:<li></li>://img/@src%
</ul>

%ds__body^xpathe:count(//img)%  (returns 0 also when there are 10 images in total)

Any ideas?


(Bart Banda) #6

Hmm, I actually tested the first example you printed in latest 5.4 and it worked ok.

What server side JS engine are you using? v8.js or spidermonkey? and what version of that?

This is what we are using:

$ rpm -qa | grep v8
v8-3.14.5.10-9.el6.x86_64
php56-php-pecl-v8js-0.1.3-4.el6.x86_64

(Keith Brown) #7

a fairly old version of spidermonkey

$ js -v
JavaScript-C 1.7.0 2007-10-03

BUT we are upgrading our servers at the moment so the new ones should be more up-to-date . But given that the js works when its in a js file and run against the binary - I’m still confused :S

Any thoughts on the xpath keyword route?

tia

K


(Peter McLeod) #8

Hi

With the xpath method - is the content returned by %ds__body% valid (for the purposes of processing as xml)? eg any unclosed elements?

Could also try using an xsl file and ^xslt modifer - I find these can be quite useful sometimes, and give you more options than just the xpath modifiers.

Thanks
Peter