npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prepare

v0.0.6

Published

PHP for today's web: simple server-side JavaScript execution

Downloads

297

Readme

Prepare.js

An easy method for executing JavaScript on the server in a browser-like environment, or more fancily, a basic framework for isomorphic JavaScript applications.

Prepare can preprocess HTML files with the extension ''.js.html'', executing JS snippets along the way. To specify the content of a script tag should be executed by Prepare, give it a ''type'' attribute of (or similar to) "server", such as:

<script type="server">console.log('Hi!');</script>
<script type="on/server">
    var db = require('../db');
    $('post').text(db.get(request.split('/').pop()));
</script>

These are then executed in a browser-like environment. In addition to all standard node-functionality, the following is currently also available:

  • DOM implementation courtesy of jsdom
  • A request- and response-object
  • A few other useful small things, see the source for specifics

After parsing the whole document, Prepare removes all server-side code, adds a few useful definitions for the client side, and returns the resulting HTML. Client side script tags (lacking the "server" attribute) will not be executed by Prepare. To specify a script should be executed on both client and server, use a type of "client/server". Inside of these scripts, you can distinguish between the two via the onServer and onClient attributes. Note that all of the code inside these scripts will still be transmitted to the client, so make sure that it doesn't contain any sensitive information.

Another way to construct your pages is by using ''.html.js'' files. These are JavaScript files that are executed in a similar environment, but let you start with a blank slate. ''window.document'' is an empty document, so you can build your document in anyway you want. Here's an example of a script that serves a .js.html file indirectly:

response.hold();
require('fs').readFile('someFile.js.html', 'utf8', function(err, html) {
    document.write(html);
    response.release();
});

This is (apart from having a different request/response object and being slower) equivalent to visiting the .js.html file directly. By default, at the end of the script, the current document is sent to the client. You also have full access to the response object though, so you can decide to send something else entirely and close the connection yourself using response.end(). If you need it to wait before sending the response, you can use the hold() function to keep it from sending until you've called release() (you can hold/release more than once). Note that all code runs inside a fiber using ''fibrous'', so you could also rewrite the script above as:

var fs = require('fs'),
    html = fs.sync.readFile('someFile.js.html', 'utf8');
document.write(html);

If you only care about not blocking the event loop for other requests, this lets you keep your code structure simple while still being able to serve multiple requests simultaneously.

Planned additions:

  • An easy way to use client/server modules
  • Express-like additions to the request object (maybe even further integration?)
  • Ease of use (installation and basic usage should be as simple as possible, to make it worth using :P)

Known bugs:

  • Client-side script tags arenot executed, but if they have a (remote) src attibute, the file is still loaded (this slows down response time considerably).