JSHint not accepting file content as argument - javascript

I am trying to make a server side git pre-receive hook for checking the code quality of php and javascript files. So the repo server will make the git push fail, if the pre-receive hook fails the test. Since the server doesn't have the physical file with the content from the latest commit, I have tried getting the file contents and piping them to the php linting tools. It was successful.
For javascript file, I am using the jshint tool. But the issue with the jshint tool is that it is not accepting the file content as the argument.
Is there any way by which I can make the jshint accept file content instead of the file name ? One solution I find is by writing a temporary file. But that is not an ideal solution.

Jshint could also read contents from the STDIN if you specify - instead of filename. So you can forward your file contents to stdin and you won't need temporary file.
$ jshint -
var a = 2
stdin: line 1, col 10, Missing semicolon.
1 error

Related

How Change GJS App output filename with meson

i have made a graphic software with JavaScript and GJS with GNOME builder. The problem is that whenever I want to run the software, I have to enter the application ID in the terminal (e.g. org.example.app), which is inconvenient.
I changed the output section in src/meson.build file, but when i run it i get this error:
(exampleapp:2411): Gjs-WARNING **: 17:31:11.727: JS ERROR: ImportError: Unable to load file from: resource:///org/example/app/js/main.js (The resource at “/org/example/app/js/main.js” does not exist)
Can anyone help?
This is the expected executable name for a GJS application, per the Package Specification.
If you want to create an alias for it, you should probably just use a symbolic link (e.g. org.foo.Bar => foo-bar).

Tesseract OCR is unable to find custom .traineddata file

I'm trying to use the "node-tesseract-ocr" module with electron to perform some basic image-to-text translations, but I'm having issues that I cannot seem to figure out, for the life of me. I'm using the exact same code as provided in the example (seen here: https://www.npmjs.com/package/node-tesseract-ocr), except I've changed the "lang" parameter for the configuration to the name of my custom .traineddata file. I've installed Tesseract manually alongside this, and have set the PATH variables for Tesseract ("C:\Program Files\Tesseract-OCR" and "C:\Program Files\Tesseract-OCR\tessdata"), and have placed the .traineddata file inside of the \tessdata folder.
Here's the error:
Command failed: tesseract "./screen.png" stdout -l mc --oem 1 --psm 3
Error opening data file C:\Program Files\Tesseract-OCR/tessdata/mc.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.
Failed loading language 'mc'
Tesseract couldn't load any languages!
Could not initialize tesseract.
I have no idea why the slashes swap midway through the path to the .traineddata file; I'm assuming this is the issue, but I have no idea how to fix this, as it seems to be an issue with Tesseract itself.
Did I install an incorrect version or something? (I installed Tesseract using "tesseract-ocr-w64-setup-v5.2.0.20220712.exe" from "https://digi.bib.uni-mannheim.de/tesseract/", as suggested by "https://medium.com/quantrium-tech/installing-and-using-tesseract-4-on-windows-10-4f7930313f82").
Everything works perfectly fine when using "eng" as the language, except the characters are not always read properly (since I'm attempting to read characters from a game, rather than handwriting/a standard English font, which is why I need the custom .traineddata file to work).
Any help is appreciated.
I used the installation from https://github.com/UB-Mannheim/tesseract/wiki and it seems to work now. Not sure why the other installation was bugged, but oh well.

Unable to transform Angular v9's JavaScript error stack trace from its minified form on RollBar in local environment

Just a little background about our app, we have been running our Angular frontend inside the docker container and have a shell script that is responsible to set the environment variables and build the frontend container with the required configuration. While building the frontend we also upload the minified files to our Rollbar server using the command as described in the RollBar docs. So, when we run our app in production we had the following commands to upload them to the Rollbar server. (Note minified_url holds the HTTP path of our app.
curl https://api.rollbar.com/api/1/sourcemap \
-F access_token={$RollBar_Access_Token}\
-F version=$GIT_CURRENT_REVISION\
-F minified_url=https://example.com/$MINIFIED_JS \
-F source_map=#dist/webapp/${MINIFIED_JS}.map;
However now in order to replicate the behaviour in the local development environment, we changed the minified_url to the path inside the container as shown below which points to our minified JS file inside the container(Note $MINIFIED_JS is the variable that holds the name of our minified Js file)
curl https://api.rollbar.com/api/1/sourcemap \
-F access_token={$RollBar_Access_Token}\
-F version=$GIT_CURRENT_REVISION\
-F minified_url=///front/dist/webapp/$MINIFIED_JS \
-F source_map=#dist/webapp/${MINIFIED_JS}.map;
After trying this we were successfully able to upload the main.[hash].js file to the Rollbar Server and thought it is going to work fine until we saw the stack trace of an error after we made the above changes. Here is the stack trace. As we see it was unable to resolve it to the lines pointing to our source code thus making it difficult to debug in the local development environment.
As you see in the above image it says "Source map was not available for some minified file" which made us realize that why it failed to translate. So we went and checked which file it failed to upload and here is the screenshot for the same
So, after many attempts, we thought of just uploading the failed file manually where we attached the source map it wanted, and we put URL as http://localhost/main.[hash].js and the appropriate GIT version of the source code. And yes thing started working fine and it was able to translate the error pointing to our source code.
Now, we want to know is there any way to do it through our shell script as we do in production so that we do not have to go through the manual process every time we work in the local environment with RollBar. Any help or leads appreciated.
Articles referred
https://medium.com/angular-in-depth/debug-angular-apps-in-production-without-revealing-source-maps-ab4a235edd85
https://pusher.com/tutorials/error-handling-angular-part-4
https://www.mattzeunert.com/2016/02/14/how-do-source-maps-work.html
So, we figured out the answer to our question thanks to the RollBar support team as they guided us with the exact bash code needed to successfully transform the minified error to its source code form thus giving us exact line number in the source code where the error has occurred
The problem with our approach was that we were giving the path to the minified file inside our docker container which was not even looked upon by RollBar server, instead, Rollbar always looked for the file as [ //localhost/$MINIFIED_JS] in the local dev environment that's why you see in the second image above //localhost/main.[hash].js in red because RollBar tried to download that file but could not find it, thus leading to the failure. But once we replaced our minified_url path with //localhost/$MINIFIED_JS Rollbar no longer failed to download the file as we had provided the file to it. Here is the exact code we put :
curl https://api.rollbar.com/api/1/sourcemap \
-F access_token={$RollBar_Access_Token}\
-F version=$GIT_CURRENT_REVISION\
-F minified_url=//localhost/$MINIFIED_JS \
-F source_map=#dist/webapp/${MINIFIED_JS}.map;
Remember: $MINIFIED_JS is the variable holding our minified file main.[hash].js. For your case you if you know the minified file name you will have to exactly put that name. Also, we have combined all the minified files into one file named main.[hash].js. If in your case if you have multiple minified files then you will have to upload each of them. Here is the awesome bash code to do upload multiple minified files using bash code( source of the code: https://michalzalecki.com/upload-source-maps-to-rollbar/)
#!/bin/sh
# Save a short git hash, must be run from a git
# repository (or a child directory)
version=$(git rev-parse --short HEAD)
# Use the post_server_time access token, you can
# find one in your project access token settings
post_server_item=$ROLLBAR_POST_SERVER_ITEM
echo "Uploading source maps for version $version!"
# We upload a source map for each resulting JavaScript
# file; the path depends on your build config
for path in $(find dist -name "*.js"); do
# URL of the JavaScript file on the web server
url=https://example.com/${path}
# a path to a corresponding source map file
source_map="#$path.map"
echo "Uploading source map for $url"
curl --silent --show-error https://api.rollbar.com/api/1/sourcemap \
-F access_token=$post_server_item \
-F version=$version \
-F minified_url=$url \
-F source_map=$source_map \
> /dev/null
done

ENAMETOOLONG nodeJs ffmpeg-fluent

I am currently using ffmpeg-fluent to merge video files. (https://github.com/fluent-ffmpeg/node-fluent-ffmpeg)
Unfortunately, my loop in which I put files to merge fail at thousandth file with below exception :
Error ENAMETOOLONG in /nodes_modules/fluent-ffmpeg.
My question is :
how can i bypass this error for writing command with a number of unlimited character?
This is an old question and unfortunately there's no code to point the problem for sure. But getting ENAMETOOLONG from ffmpeg on Windows usually means the command is really too long. And merging thousands of files makes this seem quite natural.
It still happens now in 2020. But one can work this around. We need to put the source filenames (for merger) into a text file and provide this text file as an input to ffmpeg.
A raw ffmpeg call would look like:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.wav
with mylist.txt being like:
file '/path/to/file1.wav'
file '/path/to/file2.wav'
file '/path/to/file3.wav'
With fluent-ffmpeg it's not intuitive but still feasible:
const cmd = ffmpeg();
cmd.input('mylist.txt')
.inputOption(['-f concat', '-safe 0'])
.output('out.wav')
.run();
Note: Be careful with absolute paths of the list file and source files inside the list on Windows. Most ffmpeg versions would add the directory of the list to the source file resulting in similar corrupted path:
c:/ffmpeg/lists/c:/audiofiles/file1.wav
But you still can solve this if you go with url-formatted source files:
file 'file:c:/audiofiles/file1.wav'
file 'file:c:/audiofiles/file2.wav'
file 'file:c:/audiofiles/file3.wav'
I'm sure this will be helpful to someone searching for this ffmpeg error :-)

How can I take a minified javascript stack trace and run it against a source map to get the proper error?

On our production server, I have minified javascript published and I'm not including a map file with it, because I don't want the user to be able to understand what's happening based on the error.
I have a logging service I've written to forward the angular exceptions (caught by $exceptionHandler) to myself via email. However, this stack trace is near unreadable:
n is not defined
at o (http://localhost:9000/build/app.min.js:1:3284)
at new NameController (http://localhost:9000/build/app.min.js:1:3412)
at e (http://localhost:9000/build/bower.min.js:44:193)
at Object.g.instantiate (http://localhost:9000/build/bower.min.js:44:310)
at b.$get (http://localhost:9000/build/bower.min.js:85:313)
at d.compile (http://localhost:9000/build/bower.min.js:321:23333)
at aa (http://localhost:9000/build/bower.min.js:78:90)
at K (http://localhost:9000/build/bower.min.js:67:39)
at g (http://localhost:9000/build/bower.min.js:59:410)
at http://localhost:9000/build/bower.min.js:58:480 <ui-view class="ng-scope">
What I'm wondering is: Is there a program where I can analyze this stack trace against the actual non-minified source code via map file (or not via map file if there's another way)
What you want to do is parse the source maps. This has nothing to do with web browsers. All you need to do is translate the minified reference into the unminified resource.
If you have any experience with NodeJS there is already a package that does this for you.
https://github.com/mozilla/source-map/
To install the library
npm install -g source-map
or
yarn global add source-map
Create a file named "issue.js"
fs = require('fs');
var sourceMap = require('source-map');
var smc = new sourceMap.SourceMapConsumer(fs.readFileSync("./app.min.js.map","utf8"));
console.log(smc.originalPositionFor({line: 1, column: 3284}));
Run the file with node
node issue.js
It should output the location in the original file to the console for first line from the stack trace.
Note: I tell you install source-map globally for ease of use, but you could create a node project that does what you need and installs it locally.
I figured there was no super simple tool for converting a minified stack trace into a readable one using a source map (without having to use a web service), so I created a tool for it:
https://github.com/mifi/stacktracify
Install and use it as follows:
npm install -g stacktracify
Now copy a minified stacktrace to your clipboard - then run:
stacktracify /path/to/js.map
Adding to #Reactgular's answer, the below snippet will work with the latest version of source-map
const rawSourceMap = fs.readFileSync("./app.min.js.map","utf8");
const whatever = sourceMap.SourceMapConsumer.with(rawSourceMap, null, consumer => {
console.log(consumer.originalPositionFor({
line: 1,
column: 3284
}));
});
And to add to the discussion on the thread a simple regex like /\/(\w*[-\.]?\w*).js:\d*:\d*/g
Below is a very simple regex to find all line numbers in a stacktrace.
//regex for patterns like utils.js, utils123.js, utils-name.js, utils.version.js
var patt = /\/(\w*[-\.]?\w*).js:\d*:\d*/g;
// returns matches like ['/app.min.js:1:3284', '/bower.min.js:44:193', ..]
var errorPositions = line.match(patt);
console.log(errorPositions);
if(!errorPositions || errorPositions.length === 0) {
console.log("No error line numbers detected in the file. Ensure your stack trace file is proper");
return;
}
errorPositions.forEach(function(error) {
findInSourceMap(error);
});
});
If you had access to the source map file externally and could get the same file structure you could work it out I guess, but I'm not aware of any tools outside the browser that will help you with that.
The added advantage of having the data in a running browser will allow checking of locals which you won't get even with a source map.
You might want to consider a tool such as rollbar to do error reporting. This will report all the locals in each frame to help debugging. It has support for sourcemaps outside the browser to address your security concerns.
Append comment directive for the JS running in the page.
//# sourceMappingURL=/path/to/your/sourcemap.map
In firefox (not sure about chrome) to tell the Debugger to use source maps if they are available, click the "Debugger settings" button and select "Show original sources" from the list of settings that pops up:

Categories