Author Archives: Śpiechu

Serving throttled static files in NodeJS Express

Curren­tly I’m not only PHP deve­lo­per, but rather <put some random technology> deve­lo­per. I need to have at least basic under­stan­ding about what’s going on in Node Express, Java Spring and Angu­lar JS. That’s why I’ll talk about NodeJS a bit today.

Serving static files on Express is rather trivial. Under the hood express.static uses serve-static module and that’s it. There are plenty of confi­gu­ra­tion options. Usually it’s enough to have file serving up’n’running smoothly. But I wanted to take control over file stre­aming even further.

Imagine user who did not pay on time for your service and you need to punish him a little by setting him a down­load limit to 300 KB/s max. ;-) I know I could use limit_rate in Nginx. But it’s pretty static. There are Lua scrip­ting possi­bi­li­ties to coun­ter that, but c’mon, I’m too old for that stuff ;-)

We’re going to use advan­ced throt­tle npm library. Basi­cally it utili­zes Node stream Trans­form with extra passth­ro­ugh magic. Code is pretty simple, but it took me a while to make it working with serve-static.

Ecma­Script 6 consts are well suppor­ted in Node 6 and 7. If you’re using Node < 6 then replace consts to vars.

The nice thing is we have no bloc­king here. Promise ;-)

Symfony2 compiler pass with tags and custom attributes

It passed about half year since I scrat­ched Symfony2 Frame­work surface. Basics are behind me, now it’s time for middle-level stuff. I’m exci­ted to start a whole new Symfony rela­ted blog post series. I didn’t wanted to copy typi­cal tuto­rial topics. It would be waste of your time and my work. I thought it would be nice to show some code rela­ted to Symfony Depen­dency Injec­tion Conta­iner.

Tuto­rials usually show typi­cal conta­iner servi­ces defi­ni­tion. Boring stuff. I’m going to show you dyna­mic collec­tion of servi­ces passed into proces­sor service. The idea is that collec­tion doesn’t know its elements, so every bundle can define its own servi­ces to be added to collec­tion. The secret is tags. To add some spice, tagged service can be defi­ned with prio­rity. And we can specify that this proces­sor should be run at the begin­ning, and that at the end.

We’ll start with defi­ning common proces­sor inter­face and some basic imple­men­ta­tions.

Now it’s time for a service that proces­ses given argu­ment with all the proces­sors one by one.

We could define typi­cal conta­iner defi­ni­tions and pass proces­sors right into proces­sing service, but remem­ber about requ­ire­ment that exter­nal bundles can define their own proces­sors. We’ll use tags.

Now the key part: compi­ler pass defi­ni­tion.

Usage:

At the first sight you may think that it’s a lot of logic to run to define stupid proces­sor. What about perfor­mance? No worries. Remem­ber, the compi­la­tion outcome is dumped into cache file on prod envi­ron­ment. All these fore­ach loops and prio­rity queues are being run only once.

Solu­tion is pretty gene­ric, you can easily adapt to your own needs. Of course code is on MIT license, so grab it and use in your own (even commer­cial) projects!

Happy PHP conta­iner compiling :-)

New Composer version constraints

Topic is not 0 day news. First commits adding new version constra­ints were added on decem­ber 2014. I’ll only mention what’s new:

  • when defi­ning logi­cal AND you can use space just like you use comma, so >1.0 <=1.5 is the same as >1.0,<=1.5
  • when defi­ning logi­cal OR you should (must?) use double pipe || instead of single pipe |
  • I’m sure you know tilde opera­tor ~1.5 (which means you’re inte­re­sted in versions >=1.5,<2), new caret opera­tor ^ was intro­du­ced, ^1.2.3 defi­ni­tion means >=1.2.3,<2, so the change is you can more preci­sely define star­ting version; if you would like to define the same constra­int without caret opera­tor you would need to write ~1.2,>=1.2.3, so new opera­tor is more concise
  • the last one, hyphen opera­tor - is inclu­sive set of versions, it’s a kind of myste­rious to me, 1.0 - 2.0 is equiva­lent to >=1.0.0 <2.1 and 1.0.0 - 2.1.0 is equiva­lent to >=1.0.0 <=2.1.0 (take a look at compo­ser doc and then matcher source code)