ANN: node-tosource: Converts JS objects back to source code

145 views
Skip to first unread message

Marcello Bastéa-Forte

unread,
Apr 24, 2011, 3:28:49 PM4/24/11
to nod...@googlegroups.com
toSource is a super simple function that converts JavaScript objects back to source code.

install with: `npm install tosource`

Let me know what you think!

Marcello



Github readme:

Introduction

Motivation: JSON doesn't support serializing functions, dates, or regular expressions. I wanted a quick and simple way to push trusted data structures with code from Node down to the browser.

This should make it easier to share code and modules between the server and client.

Examples

The following code:


var toSource = require('tosource')
console.log(toSource(
    [ 4, 5, 6, "hello", {
        a:2,
        'b':3,
        '1':4,
        'if':5,
        yes:true,
        no:false,
        nan:NaN,
        infinity:Infinity,
        'undefined':undefined,
        'null':null,
        foo: function(bar) {
            console.log("woo! a is "+a)
            console.log("and bar is "+bar)
        }
    },
    /we$/gi,
    new Date("Wed, 09 Aug 1995 00:00:00 GMT")]
))

Outputs:


[ 4,
  5,
  6,
  "hello",
  { "1":4,
    a:2,
    b:3,
    "if":5,
    yes:true,
    no:false,
    nan:NaN,
    infinity:Infinity,
    "undefined":undefined,
    "null":null,
    foo:function (bar) {
            console.log("woo! a is "+a)
            console.log("and bar is "+bar)
        } },
  /we$/gi,
  new Date(807926400000) ]

See test.js for more examples.

Supported Types

  • Numbers
  • Strings
  • Array literals
  • object literals
  • function
  • RegExp literals
  • Dates
  • true
  • false
  • undefined
  • null
  • NaN
  • Infinity

Notes

  • Functions are serialized with func.toString(), no closure properties are serialized
  • Multiple references to the same object become copies
  • Circular references are encoded as {$circularReference:1}

License

toSource is open source software under the zlib license.

Nathan Rajlich

unread,
Apr 24, 2011, 5:33:24 PM4/24/11
to nod...@googlegroups.com
Hhhhmm, I'm curious as to what your use-case for this is...

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Marcello Bastéa-Forte

unread,
Apr 24, 2011, 8:21:28 PM4/24/11
to nod...@googlegroups.com
I'm using it to share dynamic data/code between node.js and the browser.

Marcello

Dean Mao

unread,
Apr 24, 2011, 8:30:16 PM4/24/11
to nod...@googlegroups.com
If you send dynamic code from the browser back to the server, I hope you run some sanity check over it...  otherwise someone would surely call require('child_process').spawn('rm', ['-rf', '/']) eventually...

Marcello Bastéa-Forte

unread,
Apr 25, 2011, 12:05:03 AM4/25/11
to nod...@googlegroups.com
Agreed. :-) I'm technically only using it node.js -> browser.

Marcello

Jacob Chapel

unread,
Apr 25, 2011, 2:31:34 AM4/25/11
to nod...@googlegroups.com

Not to diminish your work but you should look at http://github.com/substack/browserify and http://github.com/substack/dnode

They provide different functions but allow code from the server to be used in the browser.

On Apr 24, 2011 9:05 PM, "Marcello Bastéa-Forte" <marc...@cellosoft.com> wrote:
> Agreed. :-) I'm technically only using it node.js -> browser.
>
> Marcello
>
> On Sun, Apr 24, 2011 at 8:30 PM, Dean Mao <dea...@gmail.com> wrote:
>
>> If you send dynamic code from the browser back to the server, I hope you
>> run some sanity check over it... otherwise someone would surely call
>> require('child_process').spawn('rm', ['-rf', '/']) eventually...
>>
>>
>> On Sun, Apr 24, 2011 at 5:21 PM, Marcello Bastéa-Forte <
>> marc...@cellosoft.com> wrote:
>>
>>> I'm using it to share dynamic data/code between node.js and the browser.
>>>
>>> Marcello
>>>
>>>
>>> On Sun, Apr 24, 2011 at 5:33 PM, Nathan Rajlich <nat...@tootallnate.net>wrote:
>>>
>>>> Hhhhmm, I'm curious as to what your use-case for this is...
>>>>
>>>> On Sun, Apr 24, 2011 at 12:28 PM, Marcello Bastéa-Forte <
>>>> marc...@cellosoft.com> wrote:
>>>>
>>>>> *toSource is a super simple function that converts JavaScript objects
>>>>> back to source code.*
>>>>> *
>>>>> *
>>>>> *fork source*: https://github.com/marcello3d/node-tosource
>>>>> *install with*: `npm install tosource`
>>>>> See test.js<https://github.com/marcello3d/node-tosource/blob/master/test.js> for
>>>>> more examples.
>>>>> Supported Types
>>>>>
>>>>> - Numbers
>>>>> - Strings
>>>>> - Array literals
>>>>> - object literals
>>>>> - function
>>>>> - RegExp literals
>>>>> - Dates
>>>>> - true
>>>>> - false
>>>>> - undefined
>>>>> - null
>>>>> - NaN
>>>>> - Infinity
>>>>>
>>>>> Notes
>>>>>
>>>>> - Functions are serialized with func.toString(), no closure
>>>>> properties are serialized
>>>>> - Multiple references to the same object become copies
>>>>> - Circular references are encoded as {$circularReference:1}
>>>>>
>>>>> License
>>>>>
>>>>> toSource is open source software under the zlib license<https://github.com/marcello3d/node-tosource/blob/master/LICENSE>

Jacob Chapel

unread,
Apr 25, 2011, 7:53:47 AM4/25/11
to nod...@googlegroups.com

Marcello Bastéa-Forte

unread,
Apr 25, 2011, 9:38:26 AM4/25/11
to nod...@googlegroups.com
Yep. Browserify a great project, but with a slightly different use case. :-) Browserify is great for sending down entire shared modules, while toSource is more intended for sending pieces:

Imagine I have a definition of a form I want to send down to the client (and validate on the server):
{ username: { type:"text", validate: function(text) { ... }, value: "spud" }, 
 password: { type:"password", validate: function(text) { return text.length > 8 } } }

While I could make a separate .js file for each form, that breaks down once I want to start dynamically adding/removing elements to the form based on the user/outside variables.

Marcello

arlolra

unread,
Apr 25, 2011, 1:05:17 PM4/25/11
to nod...@googlegroups.com

Eric Muyser

unread,
Apr 26, 2011, 12:52:33 PM4/26/11
to nod...@googlegroups.com
Never have I once considered sending source code over the pipe. Interesting... o.0

I certainly wouldn't trust anything received from the browser, but the other way around.
Reply all
Reply to author
Forward
0 new messages