Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Design Doc: Javascript Embedding

Jeff Kaufman edited this page Jan 5, 2017 · 1 revision

Javascript Embedding

Atul Vasu, November 2011

Introduction

Traditionally PSA rewriters were simple, and only modified urls in HTML or CSS etc, to provide new processed resources. But as rewriters get more complex, even simple filters like computing the load time, needs a non trivial amount of javascript to be served or embedded through PSA.

Proposed Solution

Step 1: Write javascript in a new file.

Pros:

  • Provides better syntax highlighting, and works better with existing tools.
  • Ensures JS readability within google3, and hence JS will be better readable.
  • Can write js_test, and unit tests easily.
  • Can compile JS at compile time.

Cons:

  • Makes it an overkill for small scripts, but an exception can be made for those.

Step 2: Convert this javascript into a C++ file using a tool. Make it a part of Makefile syntax.

Pros:

  • No need of a new data file while serving; this can be linked in PSA library
  • Can be used either to serve directly as a path or as a embedded inlined script. (Even though if purpose is to inline, the original script must be "inline safe" escaped)

Cons:

  • Makes C++ binary a bit bulkier
  • This is done using a new tool, and not a well used mechanism.
  • The line width must be restricted to 75 or so so that generated c++ file follows style guidelines.

Detailed Design

Input:

file.js

doSomething = function() {
    callSomeOtherFunction();

}

Output

file.cc

/** Copyright .. .
*/
// Author: ...
const char* kDosomethingJs =
    "doSomething = function() {\n"
     “    callSomeOtherFunction();\n”
    "}\n";

The string becomes correctly escaped, the variable declaration file header etc. are provides as flags to the converting code.

Clone this wiki locally