Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Running the above example will print something like

## API

#### `schema.parse(protobufSchemaBufferOrString)`
#### `schema.parse(protobufSchemaAsStringOrUint8Array)`

Parses a .proto schema into a javascript object

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "No nonsense protocol buffers schema parser written in Javascript",
"main": "index.js",
"devDependencies": {
"standard": "^10.0.3",
"standard": "^14.3.4",
"tape": "^4.8.0"
},
"scripts": {
Expand Down
7 changes: 5 additions & 2 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,11 @@ var onrpc = function (tokens) {
throw new Error('No closing tag for rpc')
}

var parse = function (buf) {
var tokens = tokenize(buf.toString())
var parse = function (input) {
var source = typeof input === 'string'
? input
: new TextDecoder().decode(input)
var tokens = tokenize(source)
// check for isolated strings in tokens by looking for opening quote
for (var i = 0; i < tokens.length; i++) {
if (/^("|')([^'"]*)$/.test(tokens[i])) {
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ var fixture = function (name) {
return fs.readFileSync(path.join(__dirname, 'fixtures', name), 'utf-8')
}

var fixtureBytes = function (name) {
var content = fs.readFileSync(path.join(__dirname, 'fixtures', name))
return new Uint8Array(content.buffer, content.byteOffset, content.byteLength)
}

tape('basic parse', function (t) {
t.same(schema.parse(fixture('basic.proto')), require('./fixtures/basic.json'))
t.end()
})

tape('basic parse (bytes)', function (t) {
t.same(schema.parse(fixtureBytes('basic.proto')), require('./fixtures/basic.json'))
t.end()
})

tape('basic parse + stringify', function (t) {
var syntax = 'syntax = "proto3";\n\n'
t.same(schema.stringify(schema.parse(fixture('basic.proto'))), syntax + fixture('basic.proto'))
Expand All @@ -23,6 +33,11 @@ tape('complex parse', function (t) {
t.end()
})

tape('complex parse (bytes)', function (t) {
t.same(schema.parse(fixtureBytes('complex.proto')), require('./fixtures/complex.json'))
t.end()
})

tape('complex parse + stringify', function (t) {
var syntax = 'syntax = "proto3";\n\n'
t.same(schema.stringify(schema.parse(fixture('complex.proto'))), syntax + fixture('complex.proto'))
Expand Down