Skip to content

Commit

Permalink
modernize (ESM, simplify codegen)
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jul 4, 2024
1 parent f9fe337 commit de82bf0
Show file tree
Hide file tree
Showing 12 changed files with 1,123 additions and 1,212 deletions.
33 changes: 16 additions & 17 deletions bench/bench-tiles.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict';

var runStats = require('tile-stats-runner');
var Tile = require('./vector_tile').Tile;
var Pbf = require('../');
import runStats from 'tile-stats-runner';
import {readTile, writeTile} from './vector_tile.js';
import Pbf from '../index.js';

var ids = 'mapbox.mapbox-streets-v7';
var token = 'pk.eyJ1IjoicmVkdWNlciIsImEiOiJrS3k2czVJIn0.CjwU0V9fO4FAf3ukyV4eqQ';
var url = 'https://b.tiles.mapbox.com/v4/' + ids + '/{z}/{x}/{y}.vector.pbf?access_token=' + token;
const ids = 'mapbox.mapbox-streets-v7';
const token = 'pk.eyJ1IjoicmVkdWNlciIsImEiOiJrS3k2czVJIn0.CjwU0V9fO4FAf3ukyV4eqQ';
const url = `https://b.tiles.mapbox.com/v4/${ids}/{z}/{x}/{y}.vector.pbf?access_token=${ token}`;

var readTime = 0;
var writeTime = 0;
var size = 0;
var numTiles = 0;
let readTime = 0;
let writeTime = 0;
let size = 0;
let numTiles = 0;

runStats(url, processTile, showStats, {
width: 2880,
Expand All @@ -25,14 +24,14 @@ function processTile(body) {
size += body.length;
numTiles++;

var now = clock();
var tile = Tile.read(new Pbf(body));
let now = clock();
const tile = readTile(new Pbf(body));
readTime += clock(now);

now = clock();
var pbf = new Pbf();
Tile.write(tile, pbf);
var buf = pbf.finish();
const pbf = new Pbf();
writeTile(tile, pbf);
const buf = pbf.finish();
writeTime += clock(now);

console.assert(buf);
Expand All @@ -50,7 +49,7 @@ function speed(time, size) {

function clock(start) {
if (!start) return process.hrtime();
var t = process.hrtime(start);
const t = process.hrtime(start);
return t[0] * 1e3 + t[1] * 1e-6;
}

32 changes: 15 additions & 17 deletions bench/bench.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
'use strict';

var Benchmark = require('benchmark'),
fs = require('fs'),
path = require('path'),
protocolBuffers = require('protocol-buffers'),
protobufjs = require('protobufjs'),
vt = require('./vector_tile'),
Pbf = require('../');
import Benchmark from 'benchmark';
import fs from 'fs';
import {fileURLToPath} from 'node:url';
import protocolBuffers from 'protocol-buffers';
import protobufjs from 'protobufjs';

var pbfReadTile = vt.Tile.read,
pbfWriteTile = vt.Tile.write,
data = fs.readFileSync(path.resolve(__dirname, '../test/fixtures/12665.vector.pbf')),
import {readTile, writeTile} from './vector_tile.js';
import Pbf from '../index.js';

var data = fs.readFileSync(new URL('../test/fixtures/12665.vector.pbf', import.meta.url)),
suite = new Benchmark.Suite(),
ProtocolBuffersTile = protocolBuffers(fs.readFileSync(path.resolve(__dirname, 'vector_tile.proto'))).Tile,
ProtobufjsTile = protobufjs.loadSync(path.resolve(__dirname, 'vector_tile.proto'))
.lookup('vector_tile.Tile');
vtProtoUrl = new URL('vector_tile.proto', import.meta.url),
ProtocolBuffersTile = protocolBuffers(fs.readFileSync(vtProtoUrl)).Tile,
ProtobufjsTile = protobufjs.loadSync(fileURLToPath(vtProtoUrl)).lookup('vector_tile.Tile');

var pbfTile = pbfReadTile(new Pbf(data)),
var pbfTile = readTile(new Pbf(data)),
tileJSON = JSON.stringify(pbfTile),
protocolBuffersTile = ProtocolBuffersTile.decode(data),
protobufjsTile = ProtobufjsTile.decode(data);

suite
.add('decode vector tile with pbf', function() {
pbfReadTile(new Pbf(data));
readTile(new Pbf(data));
})
.add('encode vector tile with pbf', function() {
var pbf = new Pbf();
pbfWriteTile(pbfTile, pbf);
writeTile(pbfTile, pbf);
pbf.finish();
})
.add('decode vector tile with protocol-buffers', function() {
Expand Down
1 change: 0 additions & 1 deletion bench/data.js

This file was deleted.

94 changes: 39 additions & 55 deletions bench/vector_tile.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,76 @@
'use strict'; // code generated by pbf v3.2.1
// code generated by pbf v3.3.0

// Tile ========================================
export function readTile(pbf, end) {
return pbf.readFields(readTileTag, {layers: []}, end);
}
function readTileTag(tag, obj, pbf) {
if (tag === 3) obj.layers.push(readTileLayer(pbf, pbf.readEnd()));
}
export function writeTile(obj, pbf) {
if (obj.layers) for (const item of obj.layers) pbf.writeMessage(3, writeTileLayer, item);
}

var Tile = exports.Tile = {};

Tile.read = function (pbf, end) {
return pbf.readFields(Tile._readField, {layers: []}, end);
};
Tile._readField = function (tag, obj, pbf) {
if (tag === 3) obj.layers.push(Tile.Layer.read(pbf, pbf.readVarint() + pbf.pos));
};
Tile.write = function (obj, pbf) {
if (obj.layers) for (var i = 0; i < obj.layers.length; i++) pbf.writeMessage(3, Tile.Layer.write, obj.layers[i]);
};

Tile.GeomType = {
export const TileGeomType = {
"UNKNOWN": 0,
"POINT": 1,
"LINESTRING": 2,
"POLYGON": 3
};

// Tile.Value ========================================

Tile.Value = {};

Tile.Value.read = function (pbf, end) {
return pbf.readFields(Tile.Value._readField, {string_value: "", float_value: 0, double_value: 0, int_value: 0, uint_value: 0, sint_value: 0, bool_value: false}, end);
};
Tile.Value._readField = function (tag, obj, pbf) {
export function readTileValue(pbf, end) {
return pbf.readFields(readTileValueTag, {string_value: "", float_value: 0, double_value: 0, int_value: 0, uint_value: 0, sint_value: 0, bool_value: false}, end);
}
function readTileValueTag(tag, obj, pbf) {
if (tag === 1) obj.string_value = pbf.readString();
else if (tag === 2) obj.float_value = pbf.readFloat();
else if (tag === 3) obj.double_value = pbf.readDouble();
else if (tag === 4) obj.int_value = pbf.readVarint(true);
else if (tag === 5) obj.uint_value = pbf.readVarint();
else if (tag === 6) obj.sint_value = pbf.readSVarint();
else if (tag === 7) obj.bool_value = pbf.readBoolean();
};
Tile.Value.write = function (obj, pbf) {
}
export function writeTileValue(obj, pbf) {
if (obj.string_value) pbf.writeStringField(1, obj.string_value);
if (obj.float_value) pbf.writeFloatField(2, obj.float_value);
if (obj.double_value) pbf.writeDoubleField(3, obj.double_value);
if (obj.int_value) pbf.writeVarintField(4, obj.int_value);
if (obj.uint_value) pbf.writeVarintField(5, obj.uint_value);
if (obj.sint_value) pbf.writeSVarintField(6, obj.sint_value);
if (obj.bool_value) pbf.writeBooleanField(7, obj.bool_value);
};
}

// Tile.Feature ========================================

Tile.Feature = {};

Tile.Feature.read = function (pbf, end) {
return pbf.readFields(Tile.Feature._readField, {id: 0, tags: [], type: 0, geometry: []}, end);
};
Tile.Feature._readField = function (tag, obj, pbf) {
export function readTileFeature(pbf, end) {
return pbf.readFields(readTileFeatureTag, {id: 0, tags: [], type: 0, geometry: []}, end);
}
function readTileFeatureTag(tag, obj, pbf) {
if (tag === 1) obj.id = pbf.readVarint();
else if (tag === 2) pbf.readPackedVarint(obj.tags);
else if (tag === 3) obj.type = pbf.readVarint();
else if (tag === 4) pbf.readPackedVarint(obj.geometry);
};
Tile.Feature.write = function (obj, pbf) {
}
export function writeTileFeature(obj, pbf) {
if (obj.id) pbf.writeVarintField(1, obj.id);
if (obj.tags) pbf.writePackedVarint(2, obj.tags);
if (obj.type) pbf.writeVarintField(3, obj.type);
if (obj.geometry) pbf.writePackedVarint(4, obj.geometry);
};
}

// Tile.Layer ========================================

Tile.Layer = {};

Tile.Layer.read = function (pbf, end) {
return pbf.readFields(Tile.Layer._readField, {version: 1, name: "", features: [], keys: [], values: [], extent: 4096}, end);
};
Tile.Layer._readField = function (tag, obj, pbf) {
export function readTileLayer(pbf, end) {
return pbf.readFields(readTileLayerTag, {version: 1, name: "", features: [], keys: [], values: [], extent: 4096}, end);
}
function readTileLayerTag(tag, obj, pbf) {
if (tag === 15) obj.version = pbf.readVarint();
else if (tag === 1) obj.name = pbf.readString();
else if (tag === 2) obj.features.push(Tile.Feature.read(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 2) obj.features.push(readTileFeature(pbf, pbf.readEnd()));
else if (tag === 3) obj.keys.push(pbf.readString());
else if (tag === 4) obj.values.push(Tile.Value.read(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 4) obj.values.push(readTileValue(pbf, pbf.readEnd()));
else if (tag === 5) obj.extent = pbf.readVarint();
};
Tile.Layer.write = function (obj, pbf) {
if (obj.version != undefined && obj.version !== 1) pbf.writeVarintField(15, obj.version);
}
export function writeTileLayer(obj, pbf) {
if (obj.version != null && obj.version !== 1) pbf.writeVarintField(15, obj.version);
if (obj.name) pbf.writeStringField(1, obj.name);
if (obj.features) for (var i = 0; i < obj.features.length; i++) pbf.writeMessage(2, Tile.Feature.write, obj.features[i]);
if (obj.keys) for (i = 0; i < obj.keys.length; i++) pbf.writeStringField(3, obj.keys[i]);
if (obj.values) for (i = 0; i < obj.values.length; i++) pbf.writeMessage(4, Tile.Value.write, obj.values[i]);
if (obj.extent != undefined && obj.extent !== 4096) pbf.writeVarintField(5, obj.extent);
};
if (obj.features) for (const item of obj.features) pbf.writeMessage(2, writeTileFeature, item);
if (obj.keys) for (const item of obj.keys) pbf.writeStringField(3, item);
if (obj.values) for (const item of obj.values) pbf.writeMessage(4, writeTileValue, item);
if (obj.extent != null && obj.extent !== 4096) pbf.writeVarintField(5, obj.extent);
}
11 changes: 4 additions & 7 deletions bin/pbf
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#!/usr/bin/env node

'use strict';

var resolve = require('resolve-protobuf-schema');
var compile = require('../compile');
import resolve from 'resolve-protobuf-schema';
import {compileRaw} from '../compile.js';

if (process.argv.length < 3) {
console.error('Usage: pbf [file.proto] [--browser] [--no-read] [--no-write]');
return;
process.exit(0);
}

var code = compile.raw(resolve.sync(process.argv[2]), {
exports: process.argv.indexOf('--browser') >= 0 ? 'self' : 'exports',
const code = compileRaw(resolve.sync(process.argv[2]), {
noRead: process.argv.indexOf('--no-read') >= 0,
noWrite: process.argv.indexOf('--no-write') >= 0
});
Expand Down
Loading

0 comments on commit de82bf0

Please sign in to comment.