Skip to content

Refactor raven-js to use CommonJS modules, class instances [WIP] #421

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 17 commits into from
Dec 28, 2015
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
4 changes: 2 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"globalstrict": true,
"browser": true,
"predef": [
"TraceKit",
"console",
"_slice"
"module",
"require"
]
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.0.0

* CHANGE: Default HTTP transport changed from `Image` GET to `XMLHttpRequest` POST (w/ CORS)

## 1.3.0
* CHANGE: `console` plugin will now send all arguments as an `extra` value. See: https://github.com/getsentry/raven-js/pull/398
* CHANGE: Bump to v7 of the Sentry API spec. This now requires a Sentry 7.7.0+ https://github.com/getsentry/raven-js/pull/403
Expand Down
79 changes: 57 additions & 22 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
var proxyquire = require('proxyquireify');

module.exports = function(grunt) {
"use strict";

var _ = require('lodash');
var path = require('path');

var coreFiles = [
'template/_header.js',
'vendor/**/*.js',
'src/**/*.js',
'template/_footer.js'
];
var through = require('through2');

var excludedPlugins = [
'react-native'
Expand All @@ -26,6 +22,21 @@ module.exports = function(grunt) {
return path;
});

// custom browserify transformer to re-write plugins to
// self-register with Raven via addPlugin
function AddPluginBrowserifyTransformer() {
return function (file) {
return through(function (buf, enc, next) {
buf = buf.toString('utf8');
if (/plugins/.test(file)) {
buf += "\nrequire('../src/singleton').addPlugin(module.exports);";
}
this.push(buf);
next();
});
};
}

// Taken from http://dzone.com/snippets/calculate-all-combinations
var combine = function (a) {
var fn = function (n, src, got, all) {
Expand Down Expand Up @@ -65,7 +76,7 @@ module.exports = function(grunt) {
key.sort();

var dest = path.join('build/', key.join(','), '/raven.js');
dict[dest] = coreFiles.concat(comb);
dict[dest] = ['src/singleton.js'].concat(comb);

return dict;
}, {});
Expand All @@ -75,18 +86,35 @@ module.exports = function(grunt) {
aws: grunt.file.exists('aws.json') ? grunt.file.readJSON('aws.json'): {},

clean: ['build'],
concat: {

browserify: {
options: {
separator: '\n',
banner: grunt.file.read('template/_copyright.js'),
process: true
browserifyOptions: {
banner: grunt.file.read('template/_copyright.js'),
standalone: 'Raven' // umd
}
},
core: {
src: coreFiles.concat(plugins),
src: 'src/singleton.js',
dest: 'build/raven.js'
},
all: {
files: pluginConcatFiles
plugins: {
files: pluginConcatFiles,
options: {
transform: [
[ new AddPluginBrowserifyTransformer() ]
]
}
},
test: {
src: 'test/**/*.test.js',
dest: 'build/raven.test.js',
options: {
browserifyOptions: {
debug: true // source maps
},
plugin: [proxyquire.plugin]
}
}
},

Expand All @@ -100,7 +128,13 @@ module.exports = function(grunt) {
sourceMappingURL: function (dest) {
return path.basename(dest, '.js') + '.map';
},
preserveComments: 'some'
preserveComments: 'some',
compress: {
dead_code: true,
global_defs: {
"TEST": false
}
}
},
dist: {
src: ['build/**/*.js'],
Expand Down Expand Up @@ -251,13 +285,13 @@ module.exports = function(grunt) {

// Grunt contrib tasks
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');

// 3rd party Grunt tasks
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-mocha');
grunt.loadNpmTasks('grunt-release');
grunt.loadNpmTasks('grunt-s3');
Expand All @@ -266,15 +300,16 @@ module.exports = function(grunt) {

// Build tasks
grunt.registerTask('_prep', ['clean', 'gitinfo', 'version']);
grunt.registerTask('concat.core', ['_prep', 'concat:core']);
grunt.registerTask('concat.all', ['_prep', 'concat:all']);
grunt.registerTask('build.core', ['concat.core', 'uglify', 'fixSourceMaps', 'sri:dist']);
grunt.registerTask('build.all', ['concat.all', 'uglify', 'fixSourceMaps', 'sri:dist', 'sri:build']);
grunt.registerTask('browserify.core', ['_prep', 'browserify:core']);
grunt.registerTask('browserify.plugins', ['_prep', 'browserify:plugins']);
grunt.registerTask('build.test', ['_prep', 'browserify:test']);
grunt.registerTask('build.core', ['browserify.core', 'uglify', 'fixSourceMaps', 'sri:dist']);
grunt.registerTask('build.all', ['browserify.plugins', 'uglify', 'fixSourceMaps', 'sri:dist', 'sri:build']);
grunt.registerTask('build', ['build.all']);
grunt.registerTask('dist', ['build.core', 'copy:dist']);

// Test task
grunt.registerTask('test', ['jshint', 'mocha']);
grunt.registerTask('test', ['jshint', 'browserify.core', 'browserify:test', 'mocha']);

// Webserver tasks
grunt.registerTask('run:test', ['connect:test']);
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "raven-js",
"version": "1.3.0",
"version": "2.0.0-rc1",
"dependencies": {},
"main": "dist/raven.js",
"ignore": [
Expand Down
52 changes: 48 additions & 4 deletions dist/raven.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Raven.js 1.3.0 (768fdca) | github.com/getsentry/raven-js */
/*! Raven.js 2.0.0-rc1 (4de0c94) | github.com/getsentry/raven-js */

/*
* Includes TraceKit
Expand Down Expand Up @@ -1067,6 +1067,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
return computeStackTrace;
}());

/*global XDomainRequest:false*/
'use strict';

// First, check for JSON support
Expand Down Expand Up @@ -1110,7 +1111,7 @@ for (var method in originalConsole) {
* @this {Raven}
*/
var Raven = {
VERSION: '1.3.0',
VERSION: '2.0.0-rc1',

debug: false,

Expand Down Expand Up @@ -1174,7 +1175,9 @@ var Raven = {
(uri.port ? ':' + uri.port : '') +
'/' + path + 'api/' + globalProject + '/store/';

if (uri.protocol) {
// can safely use protocol relative (//) if target host is
// app.getsentry.com; otherwise use protocol from DSN
if (uri.protocol && uri.host !== 'app.getsentry.com') {
globalServer = uri.protocol + ':' + globalServer;
}

Expand Down Expand Up @@ -1909,7 +1912,7 @@ function send(data) {
});
}

function makeRequest(opts) {
function makeImageRequest(opts) {
// Tack on sentry_data to auth options, which get urlencoded
opts.auth.sentry_data = JSON.stringify(opts.data);

Expand All @@ -1925,6 +1928,47 @@ function makeRequest(opts) {
img.src = src;
}

function makeXhrRequest(opts) {
var request;

function handler() {
if (request.status === 200) {
if (opts.onSuccess) {
opts.onSuccess();
}
} else if (opts.onError) {
opts.onError();
}
}

request = new XMLHttpRequest();
if ('withCredentials' in request) {
request.onreadystatechange = function () {
if (request.readyState !== 4) {
return;
}
handler();
};
} else {
request = new XDomainRequest();
// onreadystatechange not supported by XDomainRequest
request.onload = handler;
}

// NOTE: auth is intentionally sent as part of query string (NOT as custom
// HTTP header) so as to avoid preflight CORS requests
request.open('POST', opts.url + '?' + urlencode(opts.auth));
request.send(JSON.stringify(opts.data));
}

function makeRequest(opts) {
var hasCORS =
'withCredentials' in new XMLHttpRequest() ||
typeof XDomainRequest !== 'undefined';

return (hasCORS ? makeXhrRequest : makeImageRequest)(opts);
}

// Note: this is shitty, but I can't figure out how to get
// sinon to stub document.createElement without breaking everything
// so this wrapper is just so I can stub it for tests.
Expand Down
Loading