Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

1.2.5

  • Loading branch information...
commit 77b13ef79138081247230c96039ab34b806df6e5 1 parent 34b7d31
@isaacs authored
Showing with 70 additions and 0 deletions.
  1. +31 −0 cleanse.js
  2. +2 −0  foo.coffee
  3. +8 −0 package.json
  4. +29 −0 test.js
View
31 cleanse.js
@@ -0,0 +1,31 @@
+// cripple the require.extensions hook so that compile-to-js
+// languages can't run without actually being compiled to js
+
+var module = require('module')
+
+var ext = Object.keys(module._extensions).map(function (k) {
+ if (k === '.js' || k === '.json' || k === '.node') {
+ return [k, module._extensions[k]]
+ }
+ delete module._extensions[k]
+ Object.keys(require.cache).forEach(function (m) {
+ if (m.substr(m.lastIndexOf(k)) === k) delete require.cache[m]
+ })
+}).filter(function (k) { return k }).reduce(function (s, k) {
+ return (s[k[0]] = k[1]), s
+}, {})
+
+// Even freezing the
+Object.freeze(ext)
+Object.freeze(module._extensions)
+delete module._extensions
+Object.defineProperty(module, '_extensions',
+ { readable: true
+ , writable: false
+ , configurable: false // you see what you make me do!?
+ , value: ext
+ })
+
+if (module._extensions !== ext) {
+ throw new Error('Cleanse failed')
+}
View
2  foo.coffee
@@ -0,0 +1,2 @@
+console.error 'omg coffee is soooo coooooooeell!!!1'
+module.exports = 'exported coffee'
View
8 package.json
@@ -0,0 +1,8 @@
+{
+ "name": "coffee-cleanse",
+ "version": "1.2.5",
+ "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
+ "devDependencies": { "coffee-script":"" },
+ "main": "cleanse.js",
+ "scripts": { "test": "node test.js" }
+}
View
29 test.js
@@ -0,0 +1,29 @@
+var assert = require('assert')
+, path = require('path')
+
+assert.deepEqual(Object.keys(require.extensions),
+ [ '.js', '.json', '.node' ])
+
+require('coffee-script')
+
+assert.deepEqual(Object.keys(require.extensions),
+ [ '.js', '.json', '.node', '.coffee' ])
+
+console.error(require('./foo.coffee'))
+
+assert(-1 !== Object.keys(require.cache)
+ .indexOf(path.resolve(__dirname, 'foo.coffee')))
+
+assert(require('./cleanse.js'))
+
+assert(-1 === Object.keys(require.cache)
+ .indexOf(path.resolve(__dirname, 'foo.coffee')))
+
+// this should fail!
+assert.throws(function () {
+ require('coffee-script')
+ require('./foo.coffee')
+})
+
+console.log('1..1')
+console.log('ok - All tests passed')
Please sign in to comment.
Something went wrong with that request. Please try again.