diff --git a/README.md b/README.md index 4312eb4..9699f2a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 2066e61..3afb8f5 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/parse.js b/parse.js index 683aa69..24ed329 100644 --- a/parse.js +++ b/parse.js @@ -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])) { diff --git a/test/index.js b/test/index.js index faa1a79..6276f62 100644 --- a/test/index.js +++ b/test/index.js @@ -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')) @@ -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'))