You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by "mikeal (JIRA)" <ji...@apache.org> on 2010/02/18 22:09:27 UTC

[jira] Created: (COUCHDB-658) Add CommonJS style modules to the view server

Add CommonJS style modules to the view server
---------------------------------------------

                 Key: COUCHDB-658
                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
             Project: CouchDB
          Issue Type: Improvement
          Components: JavaScript View Server
         Environment: CouchDB
            Reporter: mikeal


I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).



-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "mikeal (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

mikeal updated COUCHDB-658:
---------------------------

    Attachment: require.patch

Patch.

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "Chris Anderson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835422#action_12835422 ] 

Chris Anderson commented on COUCHDB-658:
----------------------------------------

@benoit

I don't think we should remove the couchapp macros as they are compatible with all kinds of messy old code, not just commonjs modules.

As far as the JSON macro goes, it is already not strictly necessary outside of map and reduce, as the ddoc is present as 'this' so you can do:

function(req, doc) {
  var template = this.my_templates.some_stuff.the_template;
...
}

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "mikeal (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835405#action_12835405 ] 

mikeal commented on COUCHDB-658:
--------------------------------

Example:

{
   "_id": "_design/test",
   "_rev": "2-0e83a5d55da4b6461524f0996f7d2b17",
   "modules": {
       "name": "exports.testing = 'asdf'"
   },
   "shows": {
       "test": "function(doc,req){return require('name').testing;}"
   }
}

GET _design/test/_show/test/{docid} 

will return a page with 'asdf'

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "mikeal (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835415#action_12835415 ] 

mikeal commented on COUCHDB-658:
--------------------------------

I thought about the semantics for doing "vendor/crypto/sha" and decided against traversing the object so that it was more like the way the attachments work just naming the keys with "/" in them. That will already work, so you can do:

{ ...
  "modules" : {"vendor/crypto/sha":"code"},
}

The thing about importing code from show, list and and other view functions is that they aren't CommonJS compatible, they don't assign their function to exports which means it won't be accessible through a require if we want to comply with the spec.

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "Benoit Chesneau (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835435#action_12835435 ] 

Benoit Chesneau commented on COUCHDB-658:
-----------------------------------------

@jchris oh I ignored this was in place. I missed that.

I'm full for having ddoc as root. It also let more flexibility to developers to choose their namespace.

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "mikeal (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

mikeal updated COUCHDB-658:
---------------------------

    Attachment:     (was: require.patch)

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "mikeal (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835429#action_12835429 ] 

mikeal commented on COUCHDB-658:
--------------------------------

For reference:

http://commonjs.org/specs/modules/1.0.html

In particular 4-6 in the "module identifier section":

# Module identifiers may be "relative" or "top-level". A module identifier is "relative" if the first term is "." or "..".
# Top-level identifiers are resolved off the conceptual module name space root.
# Relative identifiers are resolved relative to the identifier of the module in which "require" is written and called.

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "Chris Anderson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835421#action_12835421 ] 

Chris Anderson commented on COUCHDB-658:
----------------------------------------

That won't do as couchapp will import a filesystem tree in modules as:

{
"modules": {
  "vendor" : {
     "crypto" : {
        "sha" : "code"
    }
  }
}
}

So while you could do the thing you described above it'd be unlikely to be used unless couchapp adopts special behavior for the treatment of the modules directory, which I'd like to avoid.

Adding the tree traversal stuff shouldn't be hard and we should get the ability to stick modules anywhere (the ddoc/vendor/foo/commonjs/bar location will be popular)

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Resolved: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "Chris Anderson (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Chris Anderson resolved COUCHDB-658.
------------------------------------

    Fix Version/s: 0.11
       Resolution: Fixed

this was resolved before 0.11

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>             Fix For: 0.11
>
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Updated: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "mikeal (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

mikeal updated COUCHDB-658:
---------------------------

    Attachment: require.patch

Newer require patch.

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "mikeal (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835516#action_12835516 ] 

mikeal commented on COUCHDB-658:
--------------------------------

While i agree with the assertion that scoping the root to ddoc.modules gives a false sense of security I think that by scoping it to the ddoc we're giving the impression that you can require() show and list functions, which won't work, but at this point I don't really see a way around it.

Any other comments before I go in and mess with the API a bunch?

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "mikeal (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835427#action_12835427 ] 

mikeal commented on COUCHDB-658:
--------------------------------

Traversing the tree isn't the hard part.

If we are saying that filesystem trees map to object tress then we'll also need to map relative require().

so 

require('name')

will always get ddoc.modules.name

but 

require('./name') 

MUST be relative to the module which is doing the importing's place in the tree, not to mention that we'll need to support '../' for working up one space in the tree.

It's all doable, it's just a lot more code.

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "Chris Anderson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835550#action_12835550 ] 

Chris Anderson commented on COUCHDB-658:
----------------------------------------

We still need the necessary error handling for cases where eg the ddoc.modules.foo == ["array", "of", "stuff", 3] or is otherwise not valid, so the case where a user tries to require "shows/blah" doesn't trouble me.


> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "Chris Anderson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835431#action_12835431 ] 

Chris Anderson commented on COUCHDB-658:
----------------------------------------

I'd say the additional code is well worth it.

I hadn't thought about relative require, but I think that will also not be so hard.

And I'd suggest again that "the conceptual module name space root" should be the ddoc, so we can require things outside of ddoc.modules.

If we say that the root is ddoc.modules then we are suddenly giving a weird sense of security guarantees (some crazy person might get the idea that data in eg ddoc.secret can't be accessed from shows and lists, or something...)

I think if we fail hard when someone tries to require("views/foo/reduce") that's just fine, although it'd be nice to give a helpful error message.

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "Chris Anderson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835411#action_12835411 ] 

Chris Anderson commented on COUCHDB-658:
----------------------------------------

Cool!

Glad to see it working.

Is there a way to make it work without keeping everything in the modules subtree?

Eg I might want to require("vendor/crypto/sha") or maybe even require the validation function in a update handler to give users more nicely formatted error messages.

I guess this is gonna pretty much require that people are requiring commonjs modules not arbitrary javascript, so feel free to tell me to shut up. :)

But I still might want to publish some commonjs modules as part of a vendor with other JS (like browser code) so maybe it makes sense to have the require namespace be the full ddoc, and if someone says require("views/foo/map") they can suffer the error that comes from trying to import a raw function as a commonjs module.

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (COUCHDB-658) Add CommonJS style modules to the view server

Posted by "Benoit Chesneau (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/COUCHDB-658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12835419#action_12835419 ] 

Benoit Chesneau commented on COUCHDB-658:
-----------------------------------------

really cool:) Maybe it will help to remove the macros in couchapp script. 

is "/" a requirement of CommonJS ? Couldn't we use dots ? About couchapp, we have !json macros too, any idea how we could have it on server side too ?

> Add CommonJS style modules to the view server
> ---------------------------------------------
>
>                 Key: COUCHDB-658
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-658
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: CouchDB
>            Reporter: mikeal
>         Attachments: require.patch
>
>
> I have a patch that adds CommonJS module support to all ddoc aware view server functions (everything except map/reduce/rereduce).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.