dean.edwards.name/weblog/2005/04/packer-goes-net/

Packer Goes .NET

I Just received this email from Jesse Hansen:

Dean-
I've managed to port your excellent javascript packer (version 2) to C# using the .Net Framework version 1.1. I have attached the source files along with an application that I used to test it (it's a lot like your packer page and html application). I also took the liberty of adding some rudimentary HTTP handler support to the file so it could be used to pack javascript from an ASP.NET server on request. I haven't quite tested it to death yet, but I believe it's ready for a beta release.

Thanks
Jesse Hansen

Available for download.:-)

Comments (21)

Leave a comment

Now all we need is a brave .NET soul with time on their hands to roll this into an IHttpModule implementation, so that the js files stay unpacked on the server, but are packed on the fly (and cached, optionally) when requested.

Damned if I can get it to work. When opening it I get a “failed to initialize” error. I unzipped it, still no luck.

  • Comment by: Dante
  • Posted:

Dante, you need to install the Microsoft .NET framework version 1.1. Have you done this? It worked fine for me….

  • Comment by: -dean
  • Posted:
using System;
using System.Web;

namespace Dean.Edwards {
	public class PackerModule : IHttpModule {
		public void Init(HttpApplication app) {
			app.BeginRequest += new EventHandler(app_BeginRequest);
		}

		public void Dispose() {
		}

		private void app_BeginRequest(object sender, EventArgs e) {
			HttpApplication app;
			app = (HttpApplication)sender;
			ECMAScriptPacker x = new ECMAScriptPacker();
			x.ProcessRequest(app.Context);
		}
	}
}
  • Comment by: harley
  • Posted:

Awww… I tried packing your cssQuery script using this (just wondering how big a script it can handle) but I got this:

An unhandled exception has occurred in your application. […]

Index was outside the bounds of the array.

Maybe it was too much for the packer to handle?

  • Comment by: Aankhen
  • Posted:

Harley – Blimey that was quick! Only a few hours after a request for an IHttpModule, it gets posted.:-)

Aankhen – the online version of packer can pack cssQuery OK. So there is probably a problem with Jesse’s conversion. I’ll contact him about this.

  • Comment by: -dean
  • Posted:

Nice try harley. Now if only your module wouldn’t pack ALL files, just the ones that need to be packed:)And where’s my caching?:)

Dimtiri – I didn’t bother with any logic to figure out which responses should be packed. Obviously, only Javascript files should be. But a file extension no longer means what it once meant. To be web-standard, an IF statement should surround my logic to sniff for the response’s content-type. But for that to work, your web-server must be setup properly.

So, your two, basic options are:

if (app.Request.Url.AbsolutePath.EndsWith("js"))
	new ECMAScriptPacker().ProcessRequest((HttpApplication)sender.Context);

or

if (app.Response.ContentType == "text/javascript")
	new ECMAScriptPacker().ProcessRequest((HttpApplication)sender.Context);

And, I’m just skipping the caching part:)

  • Comment by: harley
  • Posted:

Aankhen-

I believe I found the error that you ran into already. I’ve emailed an updated version to Dean, but I can send you an updated copy directly if you’d like. Feel free to email me directly with any porting problems at twindagger2k@msn.com

Harley-

I’ve never used the IHttpModule interface before, do you still have to set up IIS to send that particular extension to the ASP.NET executable this way? I was just using my web.config file to make .js extensions use the packer HTTP handler.

  • Comment by: Jesse Hansen
  • Posted:

Jesse – I used the second ZIP file that you sent me. Have you sent me another? I’m not getting the same error as Aankhen anyway.

  • Comment by: -dean
  • Posted:

I can’t duplicate his problem either. Maybe he solved it.

  • Comment by: Jesse Hansen
  • Posted:

My apologies for the greatly delayed reply.

I redownloaded the packer, and this time it seems to work fine when compressing cssQuery. Thanks.:-)

  • Comment by: Aankhen
  • Posted:

This is fantastic work guys, it just keeps getting better and better.

Al.

  • Comment by: Alistair
  • Posted:

Is anone able to port the packer algorighm to perl?

Thanks Rob

  • Comment by: Rob Seiler
  • Posted:

Rob, this hasn’t been done AFAIK. I would be grateful if someone did though. Are you volunteering?;-)

You’ll need the source code of course. ParseMaster is a multi-pattern parser. There is probably something like it already in perl.

  • Comment by: -dean
  • Posted:

Hi All! I pack js-files of FCKEditor2 with Packer .NET and got error in Firefox.

Source:

… String.prototype.replaceNewLineChars=function(replacement){return this.replace(/\n/g,replacement);}FCK_STATUS_NOTLOADED=… …

After Packing:

Firefox error: missing ‘;’ before statement
eChars=function(replacement){return this.replace(/\n/g,replacement)}FCK_STATUS_NOTLOADED=…

Why Packer eat ‘;’?
Thank you!

  • Comment by: alec
  • Posted:

alec – Packer not eat “;”.

See the usage page about terminating all statements with semi-colons. That includes function declarations.

  • Comment by: -dean
  • Posted:

Hello

I’ve tested perl packer. It’s works very well. I need to check if special chars are supported.

I call it from PHP page that compile my js file in one before packaging. It’s very to make javascript licence system…

Maybe author could help me to make a native PHP version ?

  • Comment by: About perl packer ...
  • Posted:

Hey guys,

I’ve run into a max length on the textbox into which I’m pasting my code. It’s only taking 32766 characters. I haven’t compiled it myself or anything, I’ve only run the executable. Just thought I’d mention it.

  • Comment by: nate bates
  • Posted:

Hi guys.

I just started to use packer.net and got one interesting bug. It isn’t reprodused on web page. But you can easily reproduse it by capy and paste my simple test script into packer.net

“if (a) {} else /* not a */ if {}” – comment between else and if

“if (a) {} else // not a *
if {}” – that will do either

as a result of back transformation I got stange “elseif” word.

‘if|a|elseif’.split(‘|’)

  • Comment by: teshca
  • Posted:

Packer2.net —- Have a real simple question. Can somebody recopmpile this thing so that it can handle a file larger than 32,767 bytes???????????

  • Comment by: Walt Mc Whirter
  • Posted:

Comments are closed.