Skip to content

Add DSN functionality to Raven.config #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
RAVEN = ./src/raven.js
BASE64 = ./src/vendor/base64.js
CRYPTO = ./src/vendor/crypto-sha1-hmac.min.js
PARSEURI = ./src/vendor/uri.js
VER = $(shell cat version.txt)
RAVEN_FULL = ./dist/raven-${VER}.js
RAVEN_MIN = ./dist/raven-${VER}.min.js
Expand All @@ -16,7 +17,7 @@ raven:
mkdir -p dist

# Generate the full and compressed distributions
cat ${BASE64} ${CRYPTO} ${RAVEN} | \
cat ${BASE64} ${CRYPTO} ${PARSEURI} ${RAVEN} | \
sed "s/@VERSION/${VER}/" > ${RAVEN_FULL}

cat ${RAVEN_FULL} | ${COMPRESSOR} --type js > ${RAVEN_MIN}
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ open-source projects:

* base64_encode from [php.js][2] (included in the minified distribution)
* crypto-sha1-hmac from [Crypto-JS][3] (included in minified distribution)
* parseUri from [parseUri][5] (included in minified distribution)

The stacktrace generation was inspired by the [javascript-stacktrace][4]
project, and includes heavily modified portions of that project's code.
Expand All @@ -16,6 +17,7 @@ project, and includes heavily modified portions of that project's code.
[2]: http://phpjs.org/
[3]: http://code.google.com/p/crypto-js/
[4]: https://github.com/eriwen/javascript-stacktrace
[5]: http://blog.stevenlevithan.com/archives/parseuri


## Install
Expand All @@ -37,13 +39,17 @@ minified distribution file from the 'dist' directory:

## Configuration

Configure the client like this:
Configure the client by passing the DSN as the first argument:

Raven.config('http://secret:[email protected]/project-id');

Or if you need to specify additional options:

Raven.config({
"secretKey": "77ec8c99a8854256aa68ccb91dd9119d",
"publicKey": "e89652ec30b94d9db6ea6f28580ab499",
"servers": ["http://your.sentryserver.com/api/store/"],
"projectId": 1,
"projectId": "project-id",
"logger": "yoursite.errors.javascript"
});

Expand Down
40 changes: 34 additions & 6 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
// jQuery, Zepto, or Ender owns the `$` variable.
var $ = root.jQuery || root.Zepto || root.ender;

// php.js owns $P, for base64 encoding
var $P = new PHP_JS();
// php.js owns $P, for base64 encoding
var $P = new PHP_JS();

Raven.loaded = false;
Raven.options = {
Expand All @@ -33,23 +33,51 @@
projectId: 1,
logger: 'javascript',
site: undefined,
signatureUrl: undefined,
signatureUrl: undefined,
fetchHeaders: false, // Generates a synchronous request to your server
testMode: false // Disables some things that randomize the signature
};

Raven.funcNameRE = /function\s*([\w\-$]+)?\s*\(/i;

Raven.config = function(config) {
if (typeof(config) === "string") {
config = JSON.parse($P.base64_decode(config));
}
if (typeof(config) === "string") {
if (config.indexOf('http') === 0) {
// new-style DSN configuration
config = Raven.parseDSN(config);
} else {
// assume old base64-style
config = JSON.parse($P.base64_decode(config));
}
}
$.each(config, function(i, option) {
self.options[i] = option;
});

};

Raven.parseDSN = function(dsn) {
var uri = parseUri(dsn);
var path_idx = uri.path.lastIndexOf('/');
var project_id;
var path;

if (path_idx === -1) {
project_id = uri.path.substr(1);
path = '';
} else {
path = uri.path.substr(1, path_idx);
project_id = uri.path.substr(path_idx + 1);
}

return {
servers: [uri.protocol + '://' + uri.host + ':' + uri.port + '/' + path + 'api/store/'],
publicKey: uri.user,
secretKey: uri.password,
projectId: project_id
};
};

Raven.getHeaders = function() {
var headers = {};

Expand Down
32 changes: 32 additions & 0 deletions src/vendor/uri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
var o = parseUri.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14;

while (i--) uri[o.key[i]] = m[i] || "";

uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});

return uri;
}

parseUri.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<!-- Raven -->
<script type="text/javascript" src="../src/vendor/base64.js"></script>
<script type="text/javascript" src="../src/vendor/crypto-sha1-hmac.min.js"></script>
<script type="text/javascript" src="../src/vendor/uri.js"></script>
<script type="text/javascript" src="../src/raven.js"></script>

<!-- Unit tests -->
Expand Down
51 changes: 51 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
$(document).ready(function() {

module("Raven.config");

test("should parse dsn as only argument", function() {
// TODO: this test isnt isolated
var dsn = "http://public:[email protected]:80/project-id";

Raven.config(dsn);
var config = Raven.options;

equal(config['publicKey'], 'public');
equal(config['secretKey'], 'secret');
equal(config['servers'][0], 'http://example.com:80/api/store/');
equal(config['projectId'], 'project-id');

});

module("Raven.parseHeaders");

test("should parse headers into an object", function() {
Expand All @@ -15,4 +31,39 @@ $(document).ready(function() {
equal(headers['Content-Type'], "text/html; charset=utf-8");
});

module("Raven.parseDSN");

test("should parse dsn into an object", function() {
var dsn = "http://public:[email protected]:80/project-id";

var config = Raven.parseDSN(dsn);
equal(config['publicKey'], 'public');
equal(config['secretKey'], 'secret');
equal(config['servers'][0], 'http://example.com:80/api/store/');
equal(config['projectId'], 'project-id');
});


test("should parse dsn with a path", function() {
var dsn = "http://public:[email protected]:80/path/project-id";

var config = Raven.parseDSN(dsn);
equal(config['publicKey'], 'public');
equal(config['secretKey'], 'secret');
equal(config['servers'][0], 'http://example.com:80/path/api/store/');
equal(config['projectId'], 'project-id');
});

test("should parse dsn without a secret key", function() {
var dsn = "http://[email protected]:80/path/project-id";

var config = Raven.parseDSN(dsn);
equal(config['publicKey'], 'public');
equal(config['secretKey'], '');
equal(config['servers'][0], 'http://example.com:80/path/api/store/');
equal(config['projectId'], 'project-id');
});



});