Skip to content

Commit 1dbb77a

Browse files
committed
Add DSN functionality to Raven.config
1 parent f23a501 commit 1dbb77a

File tree

6 files changed

+128
-9
lines changed

6 files changed

+128
-9
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
RAVEN = ./src/raven.js
22
BASE64 = ./src/vendor/base64.js
33
CRYPTO = ./src/vendor/crypto-sha1-hmac.min.js
4+
PARSEURI = ./src/vendor/uri.js
45
VER = $(shell cat version.txt)
56
RAVEN_FULL = ./dist/raven-${VER}.js
67
RAVEN_MIN = ./dist/raven-${VER}.min.js
@@ -16,7 +17,7 @@ raven:
1617
mkdir -p dist
1718

1819
# Generate the full and compressed distributions
19-
cat ${BASE64} ${CRYPTO} ${RAVEN} | \
20+
cat ${BASE64} ${CRYPTO} ${PARSEURI} ${RAVEN} | \
2021
sed "s/@VERSION/${VER}/" > ${RAVEN_FULL}
2122

2223
cat ${RAVEN_FULL} | ${COMPRESSOR} --type js > ${RAVEN_MIN}

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open-source projects:
88

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

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

2022

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

3840
## Configuration
3941

40-
Configure the client like this:
42+
Configure the client by passing the DSN as the first argument:
43+
44+
Raven.config('http://secret:[email protected]/project-id');
45+
46+
Or if you need to specify additional options:
4147

4248
Raven.config({
4349
"secretKey": "77ec8c99a8854256aa68ccb91dd9119d",
4450
"publicKey": "e89652ec30b94d9db6ea6f28580ab499",
4551
"servers": ["http://your.sentryserver.com/api/store/"],
46-
"projectId": 1,
52+
"projectId": "project-id",
4753
"logger": "yoursite.errors.javascript"
4854
});
4955

src/raven.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
// jQuery, Zepto, or Ender owns the `$` variable.
2323
var $ = root.jQuery || root.Zepto || root.ender;
2424

25-
// php.js owns $P, for base64 encoding
26-
var $P = new PHP_JS();
25+
// php.js owns $P, for base64 encoding
26+
var $P = new PHP_JS();
2727

2828
Raven.loaded = false;
2929
Raven.options = {
@@ -33,23 +33,51 @@
3333
projectId: 1,
3434
logger: 'javascript',
3535
site: undefined,
36-
signatureUrl: undefined,
36+
signatureUrl: undefined,
3737
fetchHeaders: false, // Generates a synchronous request to your server
3838
testMode: false // Disables some things that randomize the signature
3939
};
4040

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

4343
Raven.config = function(config) {
44-
if (typeof(config) === "string") {
45-
config = JSON.parse($P.base64_decode(config));
46-
}
44+
if (typeof(config) === "string") {
45+
if (config.indexOf('http') === 0) {
46+
// new-style DSN configuration
47+
config = Raven.parseDSN(config);
48+
} else {
49+
// assume old base64-style
50+
config = JSON.parse($P.base64_decode(config));
51+
}
52+
}
4753
$.each(config, function(i, option) {
4854
self.options[i] = option;
4955
});
5056

5157
};
5258

59+
Raven.parseDSN = function(dsn) {
60+
var uri = parseUri(dsn);
61+
var path_idx = uri.path.lastIndexOf('/');
62+
var project_id;
63+
var path;
64+
65+
if (path_idx === -1) {
66+
project_id = uri.path.substr(1);
67+
path = '';
68+
} else {
69+
path = uri.path.substr(1, path_idx);
70+
project_id = uri.path.substr(path_idx + 1);
71+
}
72+
73+
return {
74+
servers: [uri.protocol + '://' + uri.host + ':' + uri.port + '/' + path + 'api/store/'],
75+
publicKey: uri.user,
76+
secretKey: uri.password,
77+
projectId: project_id
78+
};
79+
};
80+
5381
Raven.getHeaders = function() {
5482
var headers = {};
5583

src/vendor/uri.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// parseUri 1.2.2
2+
// (c) Steven Levithan <stevenlevithan.com>
3+
// MIT License
4+
5+
function parseUri (str) {
6+
var o = parseUri.options,
7+
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
8+
uri = {},
9+
i = 14;
10+
11+
while (i--) uri[o.key[i]] = m[i] || "";
12+
13+
uri[o.q.name] = {};
14+
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
15+
if ($1) uri[o.q.name][$1] = $2;
16+
});
17+
18+
return uri;
19+
}
20+
21+
parseUri.options = {
22+
strictMode: false,
23+
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
24+
q: {
25+
name: "queryKey",
26+
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
27+
},
28+
parser: {
29+
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
30+
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
31+
}
32+
};

test/test.html

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<!-- Raven -->
2020
<script type="text/javascript" src="../src/vendor/base64.js"></script>
2121
<script type="text/javascript" src="../src/vendor/crypto-sha1-hmac.min.js"></script>
22+
<script type="text/javascript" src="../src/vendor/uri.js"></script>
2223
<script type="text/javascript" src="../src/raven.js"></script>
2324

2425
<!-- Unit tests -->

test/utils.js

+51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
$(document).ready(function() {
22

3+
module("Raven.config");
4+
5+
test("should parse dsn as only argument", function() {
6+
// TODO: this test isnt isolated
7+
var dsn = "http://public:[email protected]:80/project-id";
8+
9+
Raven.config(dsn);
10+
var config = Raven.options;
11+
12+
equal(config['publicKey'], 'public');
13+
equal(config['secretKey'], 'secret');
14+
equal(config['servers'][0], 'http://example.com:80/api/store/');
15+
equal(config['projectId'], 'project-id');
16+
17+
});
18+
319
module("Raven.parseHeaders");
420

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

34+
module("Raven.parseDSN");
35+
36+
test("should parse dsn into an object", function() {
37+
var dsn = "http://public:[email protected]:80/project-id";
38+
39+
var config = Raven.parseDSN(dsn);
40+
equal(config['publicKey'], 'public');
41+
equal(config['secretKey'], 'secret');
42+
equal(config['servers'][0], 'http://example.com:80/api/store/');
43+
equal(config['projectId'], 'project-id');
44+
});
45+
46+
47+
test("should parse dsn with a path", function() {
48+
var dsn = "http://public:[email protected]:80/path/project-id";
49+
50+
var config = Raven.parseDSN(dsn);
51+
equal(config['publicKey'], 'public');
52+
equal(config['secretKey'], 'secret');
53+
equal(config['servers'][0], 'http://example.com:80/path/api/store/');
54+
equal(config['projectId'], 'project-id');
55+
});
56+
57+
test("should parse dsn without a secret key", function() {
58+
var dsn = "http://[email protected]:80/path/project-id";
59+
60+
var config = Raven.parseDSN(dsn);
61+
equal(config['publicKey'], 'public');
62+
equal(config['secretKey'], '');
63+
equal(config['servers'][0], 'http://example.com:80/path/api/store/');
64+
equal(config['projectId'], 'project-id');
65+
});
66+
67+
68+
1869
});

0 commit comments

Comments
 (0)