This repository was archived by the owner on Aug 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Update code to use a bundler (webpack) and the latest sway #3
Open
ponelat
wants to merge
2
commits into
master
Choose a base branch
from
use-a-bundler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} | ||
}}) | ||
|
||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
] | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var Sway = require('sway') | ||
|
||
// 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; | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 insway
)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 😉
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 😛