string GetJavaScript(string scriptPath) { if (scriptPath.EndsWith(".coffee", StringComparison.OrdinalIgnoreCase)) { return(coffeeScriptCompiler.CompileFile(scriptPath)); } else // assume it's a regular ".js" file { return(loadSourceFromFile(scriptPath)); } }
void ProcessCoffeeRequest(HttpContextBase context) { // Request PathInfo contains the coffeescript file. // (After we remove the "/coffee" prefix.) var appRelativePath = "~" + context.Request.PathInfo.Substring("/coffee".Length) + ".coffee"; var path = context.Server.MapPath(appRelativePath); if (!File.Exists(path)) { NotFound(context); return; } var lastModified = File.GetLastWriteTimeUtc(path); DateTime clientLastModified; if (DateTime.TryParse(context.Request.Headers["If-Modified-Since"], out clientLastModified)) { // If-Modified-Since header is only to the nearest second. // So must ignore any fractional difference from file write time. // Thus the cast to int. if ((int)(clientLastModified.Subtract(lastModified)).TotalSeconds >= 0) { NotModified(context); return; } } string javaScript; try { javaScript = coffeeScriptCompiler.CompileFile(path); } catch (CompileException ex) { // Since we treat this type of request as "debug" only, // return javascript that "alerts" the compilation error. // I think it's good to "fail loud"! javaScript = JavaScriptErrorAlert(ex); } context.Response.ContentType = "text/javascript"; context.Response.Cache.SetLastModified(lastModified); context.Response.Write(javaScript); }