Skip to content
This repository was archived by the owner on Aug 24, 2018. It is now read-only.

Update code to use a bundler (webpack) and the latest sway #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
166 changes: 94 additions & 72 deletions index.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "sway in a worker",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack.config.js"
},
"repository": {
"type": "git",
Expand All @@ -19,5 +20,10 @@
"bugs": {
"url": "https://github.com/mohsen1/sway-worker/issues"
},
"homepage": "https://github.com/mohsen1/sway-worker#readme"
"homepage": "https://github.com/mohsen1/sway-worker#readme",
"devDependencies": {
"json-loader": "0.5.4",
"sway": "0.6.0",
"webpack": "2.1.0-beta.2"
}
}
46 changes: 46 additions & 0 deletions test.basics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
describe('basics', function(){

it('should return an error for invalid swagger', function(done){

var worker = new Worker('./index.js')

worker.onmessage = function (msg) {
try {
expect(msg.data.errors.length).to.eql(1)
expect(msg.data.errors[0].message).to.match(/Unable to identify the Swagger version/)
done()
} catch(e) {
done(e)
}
}

worker.onerror = done
worker.postMessage({definition: {}})

})

it('should return no errors for valid swagger spec', function(done){

var worker = new Worker('./index.js')

worker.onmessage = function (msg) {
try {
expect(msg.data.errors.length).to.eql(0)
done()
} catch(e) {
done(e)
}
}

worker.onerror = done
worker.postMessage({definition: {
swagger: '2.0',
info: {
title: 'Title',
version: '1.0.0',
},
paths: {}
}})

})
})
22 changes: 22 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sway-Worker Mocha Tests</title>
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>

<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>

<script>mocha.setup('bdd')</script>
<script src="test.basics.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['expect']);
mocha.run();
</script>
</body>
</html>
16 changes: 16 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var webpack = require('webpack');

module.exports = {
entry: './worker.js',
output: {
filename: 'index.js'
},
module: {
loaders: [
{ test: /\.json$/, loader: 'json-loader' }
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin()
]
};
56 changes: 56 additions & 0 deletions worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var Sway = require('sway')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have test it in a worker environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tested it with swagger-editor (does that count?)
webpack has a UMD-like check for Worker environments, something like...

if(typeof window !== 'undefined'){
...
else if (typeof self !== 'undefined') { // Web Worker
...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! If it's bringing in entire dependency tree into this file then it should work. This was something I wanted to do for while but never had time. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, webpack is pretty awesome.. the only hiccup was that I needed to add 'json' as a type.
so that require('something.json') works as expected (from somewhere in sway)
I'm going to add an test.html file that tests some very basic functionality of sway, but via a Web Worker, then perhaps you'd consider a merge 😉

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh if you add karma test runner and two simple test (happy path and errors) it would be golden!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might not have time for karma (tonight)... but we'll get there 😛


// Worker code from here
onmessage = function(message) {

Sway.create(message.data).then(function (api) {

var results = api.validate();

if (results.errors.length) {

postMessage({
specs: api.definitionFullyResolved || api.definition,
errors: sanitizeErrors(results.errors),
warnings: results.warnings
});
return;
}

postMessage({
errors: [],
specs: api.definitionFullyResolved,
warnings: results.warnings
});
})

.catch(function (err) {
postMessage({
specs: message.data,
warnings: [],
errors: [{
message: err.message,
code: err.code ? 'ERROR_THROWN_BY_SWAY_CODE: ' + err.code :
'UNCAUGHT_SWAY_WORKER_ERROR'
}]
});
});
};

// Error object can not get serialized using the structured cloning algorithm,
// therefore we're removing them and appending the error message to our main
// error object.
function sanitizeErrors(errors) {
if (!errors || !errors.length) {
return [];
}

return errors.map(function(error){
if (error.err instanceof Error) {
error.message = error.err.message;
delete error.err;
}

return error;
});
}