Skip to content

Commit 79756a4

Browse files
committed
Loading node environment & functions from a file.
This has many problems which are discussed in TryGhost#448.
1 parent f8cd018 commit 79756a4

File tree

3 files changed

+77
-34
lines changed

3 files changed

+77
-34
lines changed

src/database.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,7 @@ NAN_METHOD(Database::RegisterFunction) {
367367

368368
REQUIRE_ARGUMENTS(2);
369369
REQUIRE_ARGUMENT_STRING(0, functionName);
370-
REQUIRE_ARGUMENT_FUNCTION(1, callback);
371-
372-
std::string str = "(" + std::string(*String::Utf8Value(callback->ToString())) + ")";
370+
REQUIRE_ARGUMENT_STRING(1, module);
373371

374372
Isolate *isolate = v8::Isolate::New();
375373
isolate->Enter();
@@ -379,14 +377,16 @@ NAN_METHOD(Database::RegisterFunction) {
379377
HandleScope handle_scope(isolate);
380378
Local<Context> context = Context::New(isolate);
381379
Context::Scope context_scope(context);
380+
Environment *env = CreateEnvironment(isolate, uv_default_loop(), context,
381+
2, (const char *[]){ "node", *module },
382+
0, (const char *[]){});
383+
LoadEnvironment(env);
382384

383385
Local<Object> global = NanGetCurrentContext()->Global();
384-
Local<Function> eval = Local<Function>::Cast(global->Get(NanNew<String>("eval")));
385-
386-
// Local<String> str = String::Concat(String::Concat(NanNew<String>("("), callback->ToString()), NanNew<String>(")"));
387-
Local<Value> argv[] = { NanNew<String>(str.c_str(), str.length()) };
388-
// Local<Function> function = Local<Function>::Cast(TRY_CATCH_CALL(global, eval, 1, argv));
389-
Local<Function> function = Local<Function>::Cast(eval->Call(global, 1, argv));
386+
Local<Object> process = Local<Object>::Cast(global->Get(NanNew<String>("process")));
387+
Local<Object> mainModule = Local<Object>::Cast(process->Get(NanNew<String>("mainModule")));
388+
Local<Object> exports = Local<Object>::Cast(mainModule->Get(NanNew<String>("exports")));
389+
Local<Function> function = Local<Function>::Cast(exports->Get(NanNew<String>(*functionName)));
390390

391391
FunctionEnvironment *fn = new FunctionEnvironment(isolate, *functionName, function);
392392
sqlite3_create_function(
@@ -461,7 +461,7 @@ void Database::FunctionExecute(FunctionEnvironment *fn, sqlite3_context *context
461461
}
462462

463463
TryCatch trycatch;
464-
Local<Value> result = cb->Call(NanGetCurrentContext()->Global(), argc, argv.data());
464+
Local<Value> result = cb->Call(NanNew(NanUndefined()), argc, argv.data());
465465

466466
// process the result
467467
if (trycatch.HasCaught()) {

test/support/user_functions.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
exports.MY_UPPERCASE = function(value) {
2+
return value.toUpperCase();
3+
};
4+
5+
exports.MY_STRING_JOIN = function(value1, value2) {
6+
return [value1, value2].join(' ');
7+
};
8+
9+
exports.MY_Add = function(value1, value2) {
10+
return value1 + value2;
11+
};
12+
13+
exports.MY_REGEX = function(regex, value) {
14+
return !!value.match(new RegExp(regex));
15+
};
16+
17+
exports.MY_REGEX_VALUE = function(regex, value) {
18+
return /match things/i;
19+
};
20+
21+
exports.MY_ERROR = function(value) {
22+
throw new Error('This function always throws');
23+
};
24+
25+
exports.MY_UNHANDLED_TYPE = function(value) {
26+
return {};
27+
};
28+
29+
exports.MY_NOTHING = function(value) {
30+
31+
};
32+
33+
exports.MY_INVALID_SCOPING = function(value) {
34+
return db; // not accessible
35+
};
36+
37+
exports.MY_REQUIRE = function(value) {
38+
require('./helper');
39+
};

test/user_functions.test.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
11
var sqlite3 = require('..');
22
var assert = require('assert');
3+
var path = require('path');
34

45
describe('user functions', function() {
56
var db;
67
before(function(done) { db = new sqlite3.Database(':memory:', done); });
78

89
it('should allow registration of user functions', function() {
9-
db.registerFunction('MY_UPPERCASE', function(value) {
10-
return value.toUpperCase();
11-
});
12-
db.registerFunction('MY_STRING_JOIN', function(value1, value2) {
13-
return [value1, value2].join(' ');
14-
});
15-
db.registerFunction('MY_Add', function(value1, value2) {
16-
return value1 + value2;
17-
});
18-
db.registerFunction('MY_REGEX', function(regex, value) {
19-
return !!value.match(new RegExp(regex));
20-
});
21-
db.registerFunction('MY_REGEX_VALUE', function(regex, value) {
22-
return /match things/i;
23-
});
24-
db.registerFunction('MY_ERROR', function(value) {
25-
throw new Error('This function always throws');
26-
});
27-
db.registerFunction('MY_UNHANDLED_TYPE', function(value) {
28-
return {};
29-
});
30-
db.registerFunction('MY_NOTHING', function(value) {
31-
32-
});
10+
db.registerFunction('MY_UPPERCASE', path.join(__dirname, 'support/user_functions.js'));
11+
db.registerFunction('MY_STRING_JOIN', path.join(__dirname, 'support/user_functions.js'));
12+
db.registerFunction('MY_Add', path.join(__dirname, 'support/user_functions.js'));
13+
db.registerFunction('MY_REGEX', path.join(__dirname, 'support/user_functions.js'));
14+
db.registerFunction('MY_REGEX_VALUE', path.join(__dirname, 'support/user_functions.js'));
15+
db.registerFunction('MY_ERROR', path.join(__dirname, 'support/user_functions.js'));
16+
db.registerFunction('MY_UNHANDLED_TYPE', path.join(__dirname, 'support/user_functions.js'));
17+
db.registerFunction('MY_NOTHING', path.join(__dirname, 'support/user_functions.js'));
18+
db.registerFunction('MY_INVALID_SCOPING', path.join(__dirname, 'support/user_functions.js'));
19+
db.registerFunction('MY_REQUIRE', path.join(__dirname, 'support/user_functions.js'));
3320
});
3421

3522
it('should process user functions with one arg', function(done) {
@@ -103,5 +90,22 @@ describe('user functions', function() {
10390
});
10491
});
10592

93+
it('does not allow access to external scope', function(done) {
94+
db.all('SELECT MY_INVALID_SCOPING() AS val', function(err, rows) {
95+
assert.equal(err.message, 'SQLITE_ERROR: Uncaught ReferenceError: db is not defined');
96+
assert.equal(rows, undefined);
97+
done();
98+
});
99+
});
100+
101+
it('allows use of require', function(done) {
102+
db.all('SELECT MY_REQUIRE() AS val', function(err, rows) {
103+
if (err) throw err;
104+
assert.equal(rows.length, 1);
105+
assert.equal(rows[0].val, undefined);
106+
done();
107+
});
108+
});
109+
106110
after(function(done) { db.close(done); });
107111
});

0 commit comments

Comments
 (0)