Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update dependencies #85

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions bench/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var protobuf = require('../')
var fs = require('fs')
var path = require('path')
var messages = protobuf(fs.readFileSync(path.join(__dirname, 'bench.proto')))
const protobuf = require('../')
const fs = require('fs')
const path = require('path')
const messages = protobuf(fs.readFileSync(path.join(__dirname, 'bench.proto')))

var TIMES = 1000000
const TIMES = 1000000

var then = 0
var diff = 0
let then = 0
let diff = 0

var run = function (name, encode, decode) {
var EXAMPLE = {
const run = function (name, encode, decode) {
const EXAMPLE = {
foo: 'hello',
hello: 42,
payload: Buffer.from('a'),
Expand All @@ -23,8 +23,8 @@ var run = function (name, encode, decode) {
}
}

var EXAMPLE_BUFFER = encode(EXAMPLE)
var i
const EXAMPLE_BUFFER = encode(EXAMPLE)
let i

console.log('Benchmarking %s', name)
console.log(' Running object encoding benchmark...')
Expand Down
18 changes: 9 additions & 9 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env node
var protobuf = require('./')
var fs = require('fs')
const protobuf = require('./')
const fs = require('fs')

var filename = null
var output = null
var watch = false
var encodings = null
let filename = null
let output = null
let watch = false
let encodings = null

// handrolled parser to not introduce minimist as this is used a bunch of prod places
// TODO: if this becomes more complicated / has bugs, move to minimist
for (var i = 2; i < process.argv.length; i++) {
var v = process.argv[i]
var n = v.split('=')[0]
for (let i = 2; i < process.argv.length; i++) {
const v = process.argv[i]
const n = v.split('=')[0]
if (v[0] !== '-') {
filename = v
} else if (n === '--output' || n === '-o' || n === '-wo') {
Expand Down
14 changes: 7 additions & 7 deletions compile-to-js.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var os = require('os')
const os = require('os')

var RESERVED = {
const RESERVED = {
type: true,
message: true,
name: true,
Expand All @@ -16,8 +16,8 @@ function isEncoder (m) {
}

function compile (messages, opts) {
var out = ''
var encodings = opts && opts.encodings
let out = ''
const encodings = opts && opts.encodings

out += '// This file is auto generated by the protocol-buffers compiler' + os.EOL
out += os.EOL
Expand All @@ -43,7 +43,7 @@ function compile (messages, opts) {

if (!safe) return JSON.stringify(map, null, 2).replace(/\n/g, os.EOL) + os.EOL

var out = '{' + os.EOL
let out = '{' + os.EOL

keys.forEach(function (k, i) {
out += spaces + ' ' + k + ': ' + map[k] + (i < keys.length - 1 ? ',' : '') + os.EOL
Expand All @@ -53,12 +53,12 @@ function compile (messages, opts) {
}

function visit (messages, exports, spaces) {
var encoders = Object.keys(messages).filter(function (name) {
const encoders = Object.keys(messages).filter(function (name) {
if (RESERVED[name]) return false
return isEncoder(messages[name])
})

var enums = Object.keys(messages).filter(function (name) {
const enums = Object.keys(messages).filter(function (name) {
if (RESERVED[name]) return false
return !isEncoder(messages[name])
})
Expand Down
108 changes: 54 additions & 54 deletions compile.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/* eslint-disable no-spaced-func */
/* eslint-disable no-unexpected-multiline */
/* eslint-disable func-call-spacing */
/* eslint-disable indent */

var encodings = require('protocol-buffers-encodings')
var varint = require('varint')
var genobj = require('generate-object-property')
var genfun = require('generate-function')
const encodings = require('protocol-buffers-encodings')
const varint = require('varint')
const genobj = require('generate-object-property')
const genfun = require('generate-function')

var flatten = function (values) {
const flatten = function (values) {
if (!values) return null
var result = {}
const result = {}
Object.keys(values).forEach(function (k) {
result[k] = values[k].value
})
return result
}

var defined = function defined (val) {
const defined = function defined (val) {
return val !== null && val !== undefined && (typeof val !== 'number' || !isNaN(val))
}

var isString = function (def) {
const isString = function (def) {
try {
return !!def && typeof JSON.parse(def) === 'string'
} catch (err) {
return false
}
}

var defaultValue = function (f, def) {
const defaultValue = function (f, def) {
if (f.map) return '{}'
if (f.repeated) return '[]'

Expand Down Expand Up @@ -59,33 +59,33 @@ var defaultValue = function (f, def) {
}
}

var unique = function () {
var seen = {}
const unique = function () {
const seen = {}
return function (key) {
if (seen.hasOwnProperty(key)) return false
if (Object.prototype.hasOwnProperty.call(seen, key)) return false
seen[key] = true
return true
}
}

var encName = function (e) {
var name = encodings.name(e)
const encName = function (e) {
let name = encodings.name(e)
if (name) name = 'encodings.' + name
else if (!e.name) name = 'encodings.enum'
else name = e.name
return name
}

module.exports = function (schema, extraEncodings, inlineEnc) {
var messages = {}
var enums = {}
var cache = {}
const messages = {}
const enums = {}
const cache = {}

var encString = function (idx, encs) {
const encString = function (idx, encs) {
return inlineEnc ? encName(encs[idx]) : 'enc[' + idx + ']'
}

var visit = function (schema, prefix) {
const visit = function (schema, prefix) {
if (schema.enums) {
schema.enums.forEach(function (e) {
e.id = prefix + (prefix ? '.' : '') + e.name
Expand All @@ -100,8 +100,8 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
m.fields.forEach(function (f) {
if (!f.map) return

var name = 'Map_' + f.map.from + '_' + f.map.to
var map = {
const name = 'Map_' + f.map.from + '_' + f.map.to
const map = {
name: name,
enums: [],
messages: [],
Expand Down Expand Up @@ -136,16 +136,16 @@ module.exports = function (schema, extraEncodings, inlineEnc) {

visit(schema, '')

var compileEnum = function (e) {
var conditions = Object.keys(e.values)
const compileEnum = function (e) {
let conditions = Object.keys(e.values)
.map(function (k) {
return 'val !== ' + parseInt(e.values[k].value, 10)
})
.join(' && ')

if (!conditions) conditions = 'true'

var encode = genfun()
const encode = genfun()
('function encode (val, buf, offset) {')
('if (%s) throw new Error("Invalid enum value: "+val)', conditions)
('varint.encode(val, buf, offset)')
Expand All @@ -156,7 +156,7 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
varint: varint
})

var decode = genfun()
const decode = genfun()
('function decode (buf, offset) {')
('var val = varint.decode(buf, offset)')
('if (%s) throw new Error("Invalid enum value: "+val)', conditions)
Expand All @@ -170,7 +170,7 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
return encodings.make(0, encode, decode, varint.encodingLength)
}

var compileMessage = function (m, exports) {
const compileMessage = function (m, exports) {
m.messages.forEach(function (nested) {
exports[nested.name] = resolve(nested.name, m.id)
})
Expand All @@ -183,39 +183,39 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
exports.message = true
exports.name = m.name

var oneofs = {}
const oneofs = {}

m.fields.forEach(function (f) {
if (!f.oneof) return
if (!oneofs[f.oneof]) oneofs[f.oneof] = []
oneofs[f.oneof].push(f.name)
})

var enc = m.fields.map(function (f) {
const enc = m.fields.map(function (f) {
return resolve(f.type, m.id)
})

var dedupEnc = enc.filter(function (e, i) {
const dedupEnc = enc.filter(function (e, i) {
return enc.indexOf(e) === i
})

var dedupIndex = enc.map(function (e) {
const dedupIndex = enc.map(function (e) {
return dedupEnc.indexOf(e)
})

var forEach = function (fn) {
for (var i = 0; i < enc.length; i++) fn(enc[i], m.fields[i], genobj('obj', m.fields[i].name), i)
const forEach = function (fn) {
for (let i = 0; i < enc.length; i++) fn(enc[i], m.fields[i], genobj('obj', m.fields[i].name), i)
}

// compile encodingLength

var encodingLength = genfun()
let encodingLength = genfun()
('function encodingLength (obj) {')
('var length = 0')

Object.keys(oneofs).forEach(function (name) {
var msg = JSON.stringify('only one of the properties defined in oneof ' + name + ' can be set')
var cnt = oneofs[name]
const msg = JSON.stringify('only one of the properties defined in oneof ' + name + ' can be set')
const cnt = oneofs[name]
.map(function (prop) {
return '+defined(' + genobj('obj', prop) + ')'
})
Expand All @@ -225,8 +225,8 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
})

forEach(function (e, f, val, i) {
var packed = f.repeated && f.options && f.options.packed && f.options.packed !== 'false'
var hl = varint.encodingLength(f.tag << 3 | e.type)
const packed = f.repeated && f.options && f.options.packed && f.options.packed !== 'false'
const hl = varint.encodingLength(f.tag << 3 | e.type)

if (f.required) encodingLength('if (!defined(%s)) throw new Error(%s)', val, JSON.stringify(f.name + ' is required'))
else encodingLength('if (defined(%s)) {', val)
Expand Down Expand Up @@ -282,15 +282,15 @@ module.exports = function (schema, extraEncodings, inlineEnc) {

// compile encode

var encode = genfun()
let encode = genfun()
('function encode (obj, buf, offset) {')
('if (!offset) offset = 0')
('if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))')
('var oldOffset = offset')

Object.keys(oneofs).forEach(function (name) {
var msg = JSON.stringify('only one of the properties defined in oneof ' + name + ' can be set')
var cnt = oneofs[name]
const msg = JSON.stringify('only one of the properties defined in oneof ' + name + ' can be set')
const cnt = oneofs[name]
.map(function (prop) {
return '+defined(' + genobj('obj', prop) + ')'
})
Expand All @@ -303,10 +303,10 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
if (f.required) encode('if (!defined(%s)) throw new Error(%s)', val, JSON.stringify(f.name + ' is required'))
else encode('if (defined(%s)) {', val)

var packed = f.repeated && f.options && f.options.packed && f.options.packed !== 'false'
var p = varint.encode(f.tag << 3 | 2)
var h = varint.encode(f.tag << 3 | e.type)
var j
const packed = f.repeated && f.options && f.options.packed && f.options.packed !== 'false'
const p = varint.encode(f.tag << 3 | 2)
const h = varint.encode(f.tag << 3 | e.type)
let j

if (f.map) {
encode()
Expand Down Expand Up @@ -367,7 +367,7 @@ module.exports = function (schema, extraEncodings, inlineEnc) {

// compile decode

var invalid = m.fields
const invalid = m.fields
.map(function (f, i) {
return f.required && '!found' + i
})
Expand All @@ -376,14 +376,14 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
})
.join(' || ')

var decode = genfun()
let decode = genfun()

var objectKeys = []
let objectKeys = []

forEach(function (e, f) {
var def = f.options && f.options.default
var resolved = resolve(f.type, m.id, false)
var vals = resolved && resolved.values
let def = f.options && f.options.default
const resolved = resolve(f.type, m.id, false)
const vals = resolved && resolved.values

if (vals) { // is enum
if (f.repeated) {
Expand Down Expand Up @@ -430,7 +430,7 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
('switch (tag) {')

forEach(function (e, f, val, i) {
var packed = f.repeated && f.options && f.options.packed && f.options.packed !== 'false'
const packed = f.repeated && f.options && f.options.packed && f.options.packed !== 'false'

decode('case %d:', f.tag)

Expand Down Expand Up @@ -502,11 +502,11 @@ module.exports = function (schema, extraEncodings, inlineEnc) {
return exports
}

var resolve = function (name, from, compile) {
const resolve = function (name, from, compile) {
if (extraEncodings && extraEncodings[name]) return extraEncodings[name]
if (encodings[name]) return encodings[name]

var m = (from ? from + '.' + name : name).split('.')
const m = (from ? from + '.' + name : name).split('.')
.map(function (part, i, list) {
return list.slice(0, i).concat(name).join('.')
})
Expand Down