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

connect-oj

v1.0.4

Published

Connect <-> OJ middleware

Downloads

7

Readme

connect-oj: A unique way to use oj

So, you are working on a project with some rather long function calls? Then you probably have seen something like this:

inst.func(myVar, "some text", true, 20, 20, null);

.... Whaaaaaaat? 0_o

The solution usually is to use better documentation. But, it could be easier:

[inst func:myVar
      withStringData:"some text"
      someBool:true
      height:20
      width:20
      extraData:null
];

No documentation needed. AND you may even have "various definitions":

[inst method:1 andString:"2"];
[inst method:1];

Now, that's what I call code readability. But oj itself lacks a preprocessor, so I put a tiny one to the job. Now, maybe you built your own little foundation of most-used classes and such. Here are two ways to use connect-oj:

myfile.oj:

#include("MyFoundation/file.oj");
@implementation somethingCool: MyBase
+(id)initializeWith:(MyObj)obj {...}
-(BOOL)doSomeWork {
    // Some long and heavy work goes here.
}
@end

// Now, somewhere within your code:
var myModule = {
    someFunction: function() {
        var myVar;
        // Evaluate some data...
        inst = [somethingCool initializeWith:this];
        [inst doSomeWork];
    }
}

In the above example, we created a - rather minimalistic - file, that included a "foundation" file and then used it. But, what if you only wanted readable code for internal stuff, and export simpler stuff to the user - or other scripts already in a site?

otherFile.oj

@implementation Internal
// ...
@end

function myExport(arg) {
    var me;
    me.__inst = [[Internal alloc] init];
    me.myFunc = function() {
        return [this.__inst myFunc];
    }
    me.otherFunc = function(a, b, c, ...) {
        return [this.__inst otherFunc:a and:b andAso:c ...];
    }
}

When you request a .oj file, it will be preprocessed and saved as .js - then served. You can also create a .d file with the same name as an .oj file - or one that might be requested. The .d file is a JSON object:

{
    "files": ["myfile.oj", "myotherfile.oj"]
}

Now, the following scheme might happen:

  • Client requests mysite.oj
  • Server finds mysite.oj and mysite.js does not exist:
    • Preprocesses it
    • Hands preprocessed output to oj
    • oj compiles it
    • output is saved to .js file
  • Server finds mysite.oj and mysite.js exists:
    • Compare the timestamps. If mysite.oj is newer than mysite.js, repeat above condition, but rewrite the .js
  • Server DOES NOT find mysite.oj BUT mysite.d:
    • Open the .d file, parse the JSON inside
    • Concat all the listed input files together
    • Preprocess, compile and save the output
    • If the output - in this case, mysite.js - already exists, see if any of the input files have changed - by timestamp - and if they did, update the output

The .d is taken from compiling native code, and means "dependencies".

Using the preprocessor

You may have a set of include-able files. You can bind them in using the pre-processor. Here is how that could look like:

#include("Helpers.oj");
#include("Defs.oj");

#if(needsBase) {
@implementation MyObject : base
#} else {
@implementation MyObject
#}
+(BOOL)amICool { return false; }
@end

The preprocessor is actually JavaScript. See pp.js to see this mini preprocessor. It basically just uses the integrated vm module to run a preprocessor. This trick makes things very easy, and the preprocessor may also learn new tricks such as include paths and such in the future.

How to use

var coj = require("connect-oj");
app.use(coj({...options...}));

Options

The options object can be a bit cumbersome, since oj and preproc both can receive options. Therefore, its actually split:

var options = {
    oj: {
        // oj options go here
    },
    alwaysSaveOutput: true, // Do we want to save processed scripts?
    usePreprocessor: true // If disabled, you may get a speed-gain.
};

To see oj options, head to the offical OJ repo.