Anjanesh

Assignment Statements, Comparisons & Observations
Font: Size: + -

Why do most tutorials tell you to install some package or tool globally via npm ?

Tuesday, November 24, 2020

I was told to install ngrok via npm globally :

npm install -g ngrok

I was doing this on my macOS Mojave

It failed because of permissions error. Then I did :

sudo npm install -g ngrok

I got errors apparently not related to npm. So it still failed.

npm ERR! Failed at the ngrok@3.3.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

I didn't know what to do. Searched everywhere on Google (StackOverflow) etc but still couldn't solve it. So many posts about chown etc.

I then finally did what the documentation said. Download the zip file which just contained a single ngrok executable file.

Ran ./ngrok http 3000 in terminal and all is okay now !

... I mean, wasn't downloading the zip and running the executable the was easiest way ? All this trouble to do only via npm.

DropBox's equivalent of git's .gitignore

Saturday, August 15, 2020

We all know how node_modules is the heaviest object in the universe !
So we have by default a .gitignore file which has an entry /node_modules to prevent it from being uploaded to github or gitlab.
But now I was looking for the similar thing in DropBox to upload my entire workspace folder to my DropBox account without the enourmous node_modules folders.
It seems there is a way to get DropBox to ignore files / folders to be uplaoded.

$ xattr -w com.dropbox.ignored 1 /Users/username/Dropbox/workspace/project-name/node_modules

Set a file or folder to be ignored using the command line : DropBox Help Link.

Content Marketplace = Revenue Sharing ?

Saturday, July 18, 2020

There was a trend in earning money for bloggers / freelance writers a decade ago - advertising (like the infamous adsense code) on famous blog portals like Blogger's blogspot. These are pull page article writers as opposed to micro-blogging like on Twtitter.

Fast-forward to 2020, now there's a different trend. Revenue sharing from online readership by paid readers (as a subscription model) between the platform and the writer. The famous platform right now is Medium which has a ton of useful articles / content. Medium charges $5 / month or $50 / year for readers to view full articles.

Bengaluru's Hashnode offers a free blogging platform for creators, esp tech creators to showcase their code snippets and views on a variety of topics. But they are yet to monetize.

Now, I've come across another Mumbai / Pune based platform called Scrollstack which seems similar to medium but is currently in invite-only / beta mode. Most people aren't ready to pay for viewing original content articles and hence the ads came into existence. So we just have to wait and see how people consume content in the near future.

PHP vs Python vs JavaScript

Friday, April 03, 2020
I was writing code to solve a particular problem on leetcode.com in PHP 7.4 and found that it was taking a lot of time to execute. 10 seconds. Then ported the same code in Python3 thinking it'll be much faster. Nope. 20 seconds. Then I tried JavaScript running not in the browser but in node. Quarter of a second !

Same code in three different languages - three nested loops.

$ time php contiguous-subarray.php
1378

real    0m10.180s
user    0m10.152s
sys 0m0.018s

$ time python3 contiguous-subarray.py
1378

real    0m20.704s
user    0m20.676s
sys 0m0.017s

time node contiguous-subarray.js 
1378

real    0m0.225s
user    0m0.213s
sys 0m0.010s

JavaScript wins.

China Trip

Tuesday, May 28, 2019

I recently went on a trip to China (Shanghai and Beijing) and Hong Kong for about 9 days in May 2019.

The trip was splendid. We were a group of 75 people led by our spiritual leader Swamiji Udit Chaitanya. Most of the devotees are senior citizens from Kerala. The weather was very pleasant. It drizzled for a few minutes one day only.

Language is a major barrier as most people there don't understand English properly - even at airports and train stations. I installed an app on my Android phone called Dear Translate that does translation (text and speech) from English to Chinese but couldn't do the other way around (for the Chinese people to use) as I could not locate an option to type in Chinese language on my phone which ironically is a Chinese make (Xiaomi, but bought in India).

Because most of us were vegetarians, we ended up at large (capacity for 75 people to be seated) Indian restaurants which had excellent North Indian food and one non-veg curry. Only for one particular day we went to a Chinese restaurant for vegetarian lunch that served Chinese cuisine which a lot liked but there were a few persons who still preferred Indian food.

As of now (2019), China's Internet has blocked Google's websites, Facebook, Instagram, Twitter, YouTube and WhatsApp. We need to use a VPN like Express VPN and install it on our phones before entering China. Google Play is also blocked so can't install it after entering into China. These restrictions are not there in Hong Kong though.

Even though China is more populous than India, it's not crowded in China like the way it is in India. The roads are all wide and all neat and clean 4-6 lanes like in the Western countries. Since the country is itself so vast, the cities are spacious.

Shanghai :

Mostly a financial city, we first went on an hour long cruise overlooking the city night life in the financial district which has skyscrapers.

Went on a ride on the Maglev train to and fro. It takes 7 minutes to cover 33 kilometers. It's top speed is 431 km/hour. This was really fantastic.

Traveled to Beijing via bullet train which covered the distance from Mumbai to Delhi in about 5 hours.

Beijing :

Unlike Shanghai, Beijing is a much much larger city which would take 6 hours to travel from one end to the other of the city.

We went for a Kung fu show by Chinese monks which was just amazing, though I can't tell if the performers were Shaolin monks or not.

We walked along the Great Wall of China. The steps are high so many elders couldn't climb. Apparently it would take 200 days to walk / climb the entire length of the Wall. Swamiji, myself and a few others (say about 10 of us) climbed up a few kilometers and returned the same way after sometime.

Hong Kong :

One half of the day (evening) we went sightseeing the night life.

Most spent the next and final whole day at Ocean's park which is like Singapore's Sentosa. But some 12 of us including myself cut short the Ocean Park after lunch and went shopping for the rest of the day.

Things you should buy in Beijing :

Buy as many headphones from Beijing as possible - you'll get wired headphones for 10 Yuan (Rs 50) and wireless bluetooth airpods (not Apple's) for about 100 - 150 Yuan (Rs 1500 or less). The smaller stores may not accept credit cards but I didn't check thoroughly.

Things you should buy in Hong Kong :

Shops in malls close at 6PM.

There is a street called Li Yuen street where there are numerous small shops where you can buy a variety of items. Mongkok has ladies market we were told where you can bargain like anything but we didn't have the time to go there since we were in Hong Kong for about one and a half days only.

Hong Kong has a famous electronic store in central called Fortress. Things are cheaper than in India, but I find that Indian credit cards have some cash back offer when using certain cards. This is not possible in Hong Kong. I saw an iPad Air 1TB version for $15,000 HKD which is about Rs 1.33 lakhs.

Web Hosting Solutions for Web Developers

Saturday, June 29, 2013

There are tons of hosting companies for web developers.
There are many that provide VPS-like solutions.
But there are a few good ones for developers wanting more than the standard LAMP setup and a Fantastico script.
Here we'll look at a handful of the ones that have proven to be good enough.

Webfaction

This is a semi-VPS where you can install your own tools / software as long as it resides in your home directory. Supports Python, Ruby, PHP out-of-the-box.
Plan starts at $9.50 a month for 100GB disk space.

Linode

A fully-fledged unmanaged VPS that has been in the hosting space for quite some time now. Developers normally compared Linode with Slicehost, another great unmanaged VPS before it got acquired by & merged with Rackspace.
Plan starts at $20 a month for 24GB disk space.

DigitalOcean

This is one of the best places to start out for newbies who want to full root-access at a low cost. Its an unmanaged VPS where you can setup your own Linux distro with whatever software you want. Oh - and it may offer the choice of a datacenter in India.
Plan starts at $5 a month for a 20GB SSD disk space.

Amazon AWS

A proven product, if you want scalability & reliability. Some of the major websites / services like Reddit, DropBox, FourSquare, Netflix, Zynga use Amazon AWS. Amazon AWS EC2 is the virtual dedicated hosting service.
Amazon AWS provides a plethora of other services like S3, etc.
You still need to maintain the server on your own - Amazon won't do it for you. Moreover you've got to have sys-admin skills to handle an EC2 instance.

Google App Engine

Google's servers ! That's the best part. Scalability. No worrying of backups, server maintanence, server going down etc etc.
What's the worst part ? It's not the standard setup you get with other providers which provide (S)FTP, SSH etc.
Configuration is done using a file named app.yaml. Everything else is almost custom based.
This is PaaS - Platform as a Service hosting solution and hence you can't go about installing your own software. Instead it provides a wide range of options and custom solutions. Currently Python, Java, Go and PHP are the languages supported.

Others :

Uploading gzipped content to Google Storage via gsutil

Thursday, January 17, 2013

Blogging this, since it doesn't fit on twitter.

It took me sometime to figure out the proper usage of gsutil when uploading files to google storage with the -z option to stream gzipped content. Just make sure to send in the content-type header too.

gsutil -h "Vary:Accept-Encoding" -h "Cache-Control:public,max-age=31536000" -h "Content-Type: text/javascript" cp -z js -a public-read script.min.js gs://[bucket]/script.js

How to upload a web-font to Google Cloud Storage and use it in your CSS

Wednesday, December 26, 2012

Replace [bucket] with your bucket name.

  1. Use fontsquirrel's @font-face Generator to download all the font formats required for various browsers.
  2. Upload the fonts to Google Storage :
    gsutil -h "Vary:Accept-Encoding" -h "Cache-Control:public,max-age=31536000" cp -a public-read font-webfont.eot gs://[bucket]/fonts/font-webfont.eot
    gsutil -h "Vary:Accept-Encoding" -h "Cache-Control:public,max-age=31536000" cp -a public-read font-webfont.woff gs://[bucket]/fonts/font-webfont.woff
    gsutil -h "Vary:Accept-Encoding" -h "Cache-Control:public,max-age=31536000" cp -a public-read font-webfont.ttf gs://[bucket]/fonts/font-webfont.ttf
    gsutil -h "Vary:Accept-Encoding" -h "Cache-Control:public,max-age=31536000" cp -a public-read font-webfont.svg gs://[bucket]/fonts/font-webfont.svg

    Now your font is accessible at http://[bucket].commondatastorage.googleapis.com/fonts/font-webfont.eot or http://[bucket].commondatastorage.googleapis.com/fonts/font-webfont.ttf etc.

  3. Apply the code to your CSS.

    @font-face {
        font-family: 'fontFamilyName';
        src: url('http://[bucket].commondatastorage.googleapis.com/fonts/font-webfont.eot');
        src: url('http://[bucket].commondatastorage.googleapis.com/fonts/font-webfont.eot?#iefix') format('embedded-opentype'),
             url('http://[bucket].commondatastorage.googleapis.com/fonts/font-webfont.woff') format('woff'),
             url('http://[bucket].commondatastorage.googleapis.com/fonts/font-webfont.ttf') format('truetype'),
             url('http://[bucket].commondatastorage.googleapis.com/fonts/font-webfont.svg#aller_displayregular') format('svg');    
        font-weight: normal;
        font-style: normal;
    }
  4. Unfortunately the last step is not enough to display the web-font on your website since the fonts need to be on the same domain as the website.

    In order to allow web-fonts to be hosted elsewhere, the location of the web-fonts must have a Access-Control-Allow-Origin header sent.

    For Google Storage, we do this :

    <?xml version="1.0" encoding="UTF-8"?>
    <CorsConfig>
      <Cors>
        <Origins>
          <Origin>http://mydomain.com</Origin>
        </Origins>
        <Methods>
          <Method>GET</Method>
          <Method>HEAD</Method>      
        </Methods>
        <ResponseHeaders>
          <ResponseHeader>x-goog-meta-foo1</ResponseHeader>
        </ResponseHeaders>
        <MaxAgeSec>1800</MaxAgeSec>
      </Cors>
    </CorsConfig>

    Save this as font.xml.
    Hmmm ... Google says :

    gsutil setcors <cors-xml-file> uri

    But using gsutil, we can set CORS to an entire bucket only and not to an object.

    gsutil setcors font.xml gs://[bucket]

    not uri as mentioned

    Similarily, for getcors :

    gsutil getcors gs://[bucket]
    and not
    gsutil getcors gs://cats/mycats.png

Solving all my permission issues on Ubuntu

Sunday, November 25, 2012

So many permission issues when copying files / folders from an outside source like a remove drive or Dropbox to my hard-disk that I have this mini-snippet to solve them all.

chmod -R 755 directory/
cd directory/
find -type f -exec chmod 644 {} \;
cd ..

Why I bought a Dropbox Pro account ?

Sunday, August 12, 2012

Last weekend was bad. My MacBook Pro's hard-drive crashed and my Ubuntu machine's hard-drive was semi-crashing, booting once in a while and once it booted with the entire file-system as read-only !

While my Ubuntu machine was up and running, I managed to backup all my data to Dropbox. But this time I backed it up in such a way that I don't have to 're-arrange' the folders all over again when my computer crashes.

Here's an example of what I did for my localhost (http://localhost/anjanesh) which, on the physical file system points to /home/anjanesh/www (~/www).

  • I copied all my data from ~/www to ~/Dropbox/www
  • Dropbox synced all the data in ~/Dropbox/www to the server
  • I deleted ~/www and symlinked www to ~/Dropbox/www (ln -s ~/Dropbox/www www)

So now ~/www points to ~/Dropbox/www
Whatever changes I make to ~/www gets stored, indexed and synced to Dropbox's server.

And now on my MacBook, I installed Dropbox and did the same thing with www - except point ~/www to /Users/anjanesh/Dropbox/www
This way, http://localhost/~anjanesh/ physically points to my Dropbox folder.

You can do this with all data folders, like Documents, Pictures and even Desktop. Even /var/lib/mysql to point all the mysql-data to a Dropbox sub-folder itself.