Initial WIP commit with streaming parser

This commit is contained in:
Andrew Sliwinski 2016-03-15 10:31:35 -04:00
parent 41119c5869
commit 4642c66e08
10 changed files with 260 additions and 1 deletions

16
.eslintrc Normal file
View file

@ -0,0 +1,16 @@
{
"rules": {
"curly": [2, "multi-line"],
"eol-last": [2],
"indent": [2, 4],
"quotes": [2, "single"],
"linebreak-style": [2, "unix"],
"max-len": [2, 80, 4],
"semi": [2, "always"],
"strict": [2, "never"]
},
"env": {
"node": true
},
"extends": "eslint:recommended"
}

10
.gitignore vendored Normal file
View file

@ -0,0 +1,10 @@
# Mac OS
.DS_Store
# NPM
/node_modules
npm-*
# Testing
/.nyc_output
/coverage

9
.travis.yml Normal file
View file

@ -0,0 +1,9 @@
language: node_js
node_js:
- "0.12"
- "4"
- "stable"
sudo: false
cache:
directories:
- node_modules

22
Makefile Normal file
View file

@ -0,0 +1,22 @@
ESLINT=./node_modules/.bin/eslint
NODE=node
TAP=./node_modules/.bin/tap
# ------------------------------------------------------------------------------
lint:
$(ESLINT) ./test/**/*.js
test:
@make lint
$(TAP) ./test/{unit,integration}/*.js
coverage:
$(TAP) ./test/{unit,integration}/*.js --coverage --coverage-report=lcov
benchmark:
$(NODE) ./test/benchmark/performance.js
# ------------------------------------------------------------------------------
.PHONY: lint test coverage benchmark

View file

@ -1,2 +1,39 @@
## scratch-parser
#### Validation and parsing for Scratch projects
#### Streaming parser for Scratch projects
## Installation
```bash
npm install scratch-parser
```
## Interface
#### HTTP Stream
```js
var parser = require('scratch-parser');
parser({
method: 'GET',
url: 'https://scratch.mit.edu/path/to/project.json'
}, function (err, callback) {
if (err) // handle the error
// do something interesting
});
```
#### File Stream
```js
var fs = require('fs');
var parser = require('scratch-parser');
var stream = fs.createReadStream('/path/to/project.json');
parser(stream, function (err, callback) {
if (err) // handle the error
// do something interesting
});
```
## Testing
```bash
npm test
```

13
index.js Normal file
View file

@ -0,0 +1,13 @@
var validate = require('./lib/validate');
var parse = require('./lib/parse');
module.exports = function (input, callback) {
parse(input, function (err, project) {
if (err) return callback(err);
validate(project, function (err) {
if (err) return callback(err);
callback(null, project);
});
});
};

63
lib/parse.js Normal file
View file

@ -0,0 +1,63 @@
var oboe = require('oboe');
var once = require('once');
module.exports = function (input, callback) {
// Ensure callback is only called once
callback = once(callback);
// Metadata storage object
var _meta = {
assets: [],
blocks: [],
variables: []
};
// Node handlers
function _infoHandler (obj) {
return obj;
}
function _costumeHandler (obj) {
return obj;
}
function _listHandler (obj) {
return obj;
}
function _soundHandler (obj) {
return obj;
}
function _variableHandler (obj) {
return obj;
}
// Start parser
oboe(input)
// Meta
.node('info', _infoHandler)
// Stage
.node('costumes.*', _costumeHandler)
.node('lists.*', _listHandler)
.node('sounds.*', _soundHandler)
.node('variables.*', _variableHandler)
// Sprites
.node('children.*.costumes.*', _costumeHandler)
.node('children.*.lists.*', _listHandler)
.node('children.*.sounds.*', _soundHandler)
.node('children.*.variables.*', _variableHandler)
// Error handler
.fail(function (err) {
callback(err);
})
// Return
.done(function () {
var result = this.root();
callback(null, result);
});
};

52
lib/schema.json Normal file
View file

@ -0,0 +1,52 @@
{
"id": "https://scratch.mit.edu",
"description": "Scratch project schema",
"type": "object",
"properties": {
"objName": {
"type": "string"
},
"sounds": {
"type": "array"
},
"costumes": {
"type": "array"
},
"currentCostumeIndex": {
"type": "number",
"minimum": 0
},
"penLayerMD5": {
"type": "string"
},
"penLayerID": {
"type": "number",
"minimum": -1
},
"tempoBPM": {
"type": "number"
},
"videoAlpha": {
"type": "number",
"minimum": 0
},
"children": {
"type": "array"
},
"info": {
"type": "object"
}
},
"required": [
"objName",
"sounds",
"costumes",
"currentCostumeIndex",
"penLayerMD5",
"penLayerID",
"tempoBPM",
"videoAlpha",
"children",
"info"
]
}

10
lib/validate.js Normal file
View file

@ -0,0 +1,10 @@
var ajv = require('ajv')();
var schema = require('./schema.json');
module.exports = function (input, callback) {
var validate = ajv.compile(schema);
var valid = validate(input);
if (!valid) return callback(validate.errors);
callback();
};

27
package.json Normal file
View file

@ -0,0 +1,27 @@
{
"name": "scratch-parser",
"version": "1.0.0",
"description": "Streaming parser for Scratch projects",
"author": "MIT Media Lab",
"license": "BSD-3-Clause",
"homepage": "https://github.com/LLK/scratch-parser#readme",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/LLK/scratch-parser.git"
},
"scripts": {
"test": "make test"
},
"dependencies": {
"ajv": "3.4.0",
"oboe": "2.1.2",
"once": "1.3.3"
},
"devDependencies": {
"benchmark": "2.0.0",
"eslint": "^1.10.3",
"glob": "^6.0.4",
"tap": "^5.1.1"
}
}