Comparing version 0.3.0 to 0.4.0
53
index.js
@@ -1,2 +0,3 @@ | ||
var toml = require('./toml'); | ||
var Stream = require('stream'); | ||
var toml = require('./lib/toml'); | ||
@@ -6,3 +7,53 @@ module.exports = { | ||
return toml.parse(input.toString()); | ||
}, | ||
createStream: function() { | ||
var stream = new Stream(); | ||
var buffers = []; | ||
var bufLen = 0; | ||
stream.readable = true; | ||
stream.writable = true; | ||
var parse = function() { | ||
try { | ||
var str = ""; | ||
if (Buffer.concat) { | ||
str = Buffer.concat(buffers, bufLen); | ||
} else { // Node 0.6 | ||
for(var i = 0; i < buffers.length; i++) { | ||
console.log(buffers[i].toString()) | ||
str += buffers[i].toString(); | ||
} | ||
} | ||
var results = toml.parse(str.toString()); | ||
stream.emit('data', results); | ||
} catch(e) { | ||
stream.emit('error', e); | ||
stream.destroy(); | ||
} | ||
}; | ||
stream.write = function(buffer) { | ||
buffers.push(buffer); | ||
bufLen += buffer.length; | ||
}; | ||
stream.end = function(buffer) { | ||
if (buffer) stream.write(buffer); | ||
this.writable = false; | ||
parse(); | ||
stream.emit('end'); | ||
stream.emit('close'); | ||
stream.readable = false; | ||
stream.writable = false; | ||
}; | ||
stream.destroy = stream.destroySoon = function() { | ||
stream.emit('end'); | ||
stream.emit('close'); | ||
stream.readable = false; | ||
stream.writable = false; | ||
}; | ||
return stream; | ||
} | ||
}; |
{ | ||
"name": "toml", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "TOML parser for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,2 +18,4 @@ TOML Parser for Node.js | ||
### Standalone | ||
Say you have some awesome TOML in a variable called `someTomlString`. Maybe it came from the web; maybe it came from a file; wherever it came from, it came asynchronously! Let's turn that sucker into a JavaScript object. | ||
@@ -25,7 +27,19 @@ | ||
Yet To Come | ||
----------- | ||
### Streaming | ||
* Streaming interface | ||
You can pipe a stream of TOML text into toml-node and it will emit a single `data` event with the parsed results once the stream is complete. | ||
var toml = require('toml'); | ||
var fs = require('fs'); | ||
fs.createReadStream('tomlFile.toml').pipe(toml.createStream()).on('data', function(results) { | ||
// `results` is your parsed TOML | ||
}); | ||
The stream will emit an `error` event in the case of an error while parsing the TOML document. | ||
TOML Spec Support | ||
----------------- | ||
toml-node supports the TOML spec as specified by [mojombo/toml@4a6ed394](https://github.com/mojombo/toml/tree/4a6ed3944183e2a0307ad6022b7daf53fb9e7eb0) | ||
Building & Tests | ||
@@ -37,3 +51,3 @@ ---------------- | ||
npm install -g jison | ||
jison toml.jison # generates toml.js | ||
jison src/toml.jison -o lib/toml.js | ||
npm test | ||
@@ -40,0 +54,0 @@ |
var toml = require('../'); | ||
var fs = require('fs'); | ||
exports.testParsesExample = function(test) { | ||
var expected = { | ||
title: "TOML Example", | ||
owner: { | ||
name: "Tom Preston-Werner", | ||
organization: "GitHub", | ||
bio: "GitHub Cofounder & CEO\n\tLikes \"tater tots\" and beer and backslashes: \\", | ||
dob: new Date("1979-05-27T07:32:00Z") | ||
var exampleExpected = { | ||
title: "TOML Example", | ||
owner: { | ||
name: "Tom Preston-Werner", | ||
organization: "GitHub", | ||
bio: "GitHub Cofounder & CEO\n\tLikes \"tater tots\" and beer and backslashes: \\", | ||
dob: new Date("1979-05-27T07:32:00Z") | ||
}, | ||
database: { | ||
server: "192.168.1.1", | ||
ports: [8001, 8001, 8003], | ||
connection_max: 5000, | ||
connection_min: -2, | ||
max_temp: 87.1, | ||
min_temp: -17.76, | ||
enabled: true | ||
}, | ||
servers: { | ||
alpha: { | ||
ip: "10.0.0.1", | ||
dc: "eqdc10" | ||
}, | ||
database: { | ||
server: "192.168.1.1", | ||
ports: [8001, 8001, 8003], | ||
connection_max: 5000, | ||
connection_min: -2, | ||
max_temp: 87.1, | ||
min_temp: -17.76, | ||
enabled: true | ||
}, | ||
servers: { | ||
alpha: { | ||
ip: "10.0.0.1", | ||
dc: "eqdc10" | ||
}, | ||
beta: { | ||
ip: "10.0.0.2", | ||
dc: "eqdc10" | ||
} | ||
}, | ||
clients: { | ||
data: [ ["gamma", "delta"], [1, 2] ] | ||
beta: { | ||
ip: "10.0.0.2", | ||
dc: "eqdc10" | ||
} | ||
}; | ||
}, | ||
clients: { | ||
data: [ ["gamma", "delta"], [1, 2] ] | ||
} | ||
}; | ||
exports.testParsesExample = function(test) { | ||
fs.readFile(__dirname + "/example.toml", 'utf-8', function(err, str) { | ||
test.ifError(err); | ||
test.deepEqual(toml.parse(str), expected); | ||
test.deepEqual(toml.parse(str), exampleExpected); | ||
test.done(); | ||
@@ -52,2 +52,31 @@ }); | ||
exports.testStreamingInterface = function(test) { | ||
var inStream = fs.createReadStream(__dirname + '/example.toml'); | ||
var outStream = inStream.pipe(toml.createStream()); | ||
var results = null; | ||
var dataCount = 0; | ||
outStream.on('data', function(parsed) { | ||
results = parsed; | ||
dataCount++; | ||
}); | ||
outStream.on('end', function() { | ||
test.deepEqual(exampleExpected, results); | ||
test.equal(dataCount, 1); | ||
test.done(); | ||
}); | ||
}; | ||
exports.testErrorsInStreamingInterface = function(test) { | ||
var inStream = fs.createReadStream(__dirname + '/bad.toml'); | ||
var outStream = inStream.pipe(toml.createStream()); | ||
var results = null; | ||
var dataCount = 0; | ||
outStream.on('data', function() { | ||
throw new Error("Unexpected error event"); | ||
}); | ||
outStream.on('error', function() { | ||
test.done(); | ||
}); | ||
}; | ||
exports.testErrorOnKeyOverride = function(test) { | ||
@@ -54,0 +83,0 @@ test.throws(function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29626
11
617
57
1