Skip to content

Commit

Permalink
chore: change pbf header write (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgwozdz authored Mar 18, 2024
1 parent 1b86974 commit 032d3f1
Show file tree
Hide file tree
Showing 7 changed files with 7,682 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-queens-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@koopjs/featureserver": patch
---

- change the way headers are written for pbf requests
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"lint-staged": "^15.2.2",
"nyc": "^15.1.0",
"prettier": "^3.2.5",
"protobufjs": "^7.2.6",
"shelljs": "^0.8.5",
"simple-git": "^3.23.0",
"snyk": "^1.1285.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ function sendPbf(res, jsonResponse, requestParameters) {

const buffer = getPbfBuffer(jsonResponse, requestParameters);

res.writeHead(200, [
['content-type', 'application/x-protobuf'],
['content-length', buffer.length],
['content-disposition', `inline;filename=${FILENAME}`],
]);
res.set('content-type', 'application/x-protobuf');
res.set('content-length', buffer.length);
res.set('content-disposition', `inline;filename=${FILENAME}`);
res.status(200);

return res.end(buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ const { sendPbf } = proxyquire('./', {
});

const res = {
writeHead: sinon.spy(() => {
set: sinon.spy(() => {
return res;
}),
status: sinon.spy(() => {
return res;
}),
end: sinon.spy(),
Expand All @@ -56,14 +59,12 @@ describe('sendPbf', () => {
esri: 'pbf',
length: 99,
});
res.writeHead.firstCall.args.should.deepEqual([
200,
[
['content-type', 'application/x-protobuf'],
['content-length', 99],
['content-disposition', 'inline;filename=results.pbf'],
],
]);
res.set.firstCall.args.should.deepEqual(['content-type', 'application/x-protobuf']);
res.set.secondCall.args.should.deepEqual(['content-length', 99]);
res.set.thirdCall.args.should.deepEqual(['content-disposition', 'inline;filename=results.pbf']);

res.status.firstCall.args.should.deepEqual([200]);

transformFeaturesForPbfSpy.firstCall.args.should.deepEqual([
{ esri: 'json' },
undefined,
Expand All @@ -87,14 +88,12 @@ describe('sendPbf', () => {
esri: 'pbf',
length: 99,
});
res.writeHead.firstCall.args.should.deepEqual([
200,
[
['content-type', 'application/x-protobuf'],
['content-length', 99],
['content-disposition', 'inline;filename=results.pbf'],
],
]);
res.set.firstCall.args.should.deepEqual(['content-type', 'application/x-protobuf']);
res.set.secondCall.args.should.deepEqual(['content-length', 99]);
res.set.thirdCall.args.should.deepEqual(['content-disposition', 'inline;filename=results.pbf']);

res.status.firstCall.args.should.deepEqual([200]);

transformFeaturesForPbfSpy.called.should.equal(false);

protoSpy.encode.firstCall.args.should.deepEqual([
Expand All @@ -116,14 +115,13 @@ describe('sendPbf', () => {
esri: 'pbf',
length: 99,
});
res.writeHead.firstCall.args.should.deepEqual([
200,
[
['content-type', 'application/x-protobuf'],
['content-length', 99],
['content-disposition', 'inline;filename=results.pbf'],
],
]);

res.set.firstCall.args.should.deepEqual(['content-type', 'application/x-protobuf']);
res.set.secondCall.args.should.deepEqual(['content-length', 99]);
res.set.thirdCall.args.should.deepEqual(['content-disposition', 'inline;filename=results.pbf']);

res.status.firstCall.args.should.deepEqual([200]);

transformFeaturesForPbfSpy.called.should.equal(false);

protoSpy.encode.firstCall.args.should.deepEqual([
Expand Down
130 changes: 130 additions & 0 deletions test/geoservice-pbf.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const Koop = require('@koopjs/koop-core');
const provider = require('@koopjs/provider-file-geojson');
const request = require('supertest');
const FeatureCollection = require('./helpers/FeatureCollection.proto')
.esriPBuffer.FeatureCollectionPBuffer;
const mockLogger = {
debug: () => {},
info: () => {},
silly: () => {},
warn: () => {},
error: () => {},
};

describe('koop', () => {
const koop = new Koop({ logLevel: 'error', logger: mockLogger });
koop.register(provider, { dataDir: './test/provider-data' });

describe('Feature Server', () => {
describe('/query?f=pbf', () => {
test('request as PBF', async () => {
try {
const response = await request(koop.server)
.get(
'/file-geojson/rest/services/points-w-objectid/FeatureServer/0/query?f=pbf',
)
.responseType('blob')
.buffer();
const decodedBuffer = FeatureCollection.decode(response.body);
const pbfJson = FeatureCollection.toObject(decodedBuffer);
expect(response.status).toBe(200);
expect(response.headers['content-disposition']).toBe(
'inline;filename=results.pbf',
);
expect(response.headers['content-type']).toBe(
'application/x-protobuf',
);
expect(response.headers['content-length']).toBe('337');
expect(pbfJson).toEqual({
queryResult: {
featureResult: {
objectIdFieldName: 'OBJECTID',
uniqueIdField: { name: 'OBJECTID', isSystemMaintained: true },
geometryType: 0,
spatialReference: { wkid: 4326 },
transform: {
scale: { xScale: 1e-9, yScale: 1e-9 },
translate: { xTranslate: 0, yTranslate: 0 },
},
fields: [
{ name: 'OBJECTID', fieldType: 6, alias: 'OBJECTID' },
{ name: 'category', fieldType: 4, alias: 'category' },
{ name: 'label', fieldType: 4, alias: 'label' },
{ name: 'timestamp', fieldType: 5, alias: 'timestamp' },
],
features: [
{
attributes: [
{ uintValue: 1 },
{ stringValue: 'pinto' },
{ stringValue: 'White Leg' },
{
sint64Value: {
low: 1811117264,
high: 391,
unsigned: false,
},
},
],
geometry: {
lengths: [2],
coords: [
{ low: 1604378624, high: -19, unsigned: false },
{ low: 769803776, high: -6, unsigned: false },
],
},
},
{
attributes: [
{ uintValue: 2 },
{ stringValue: 'pinto' },
{ stringValue: 'Fireman' },
{
sint64Value: {
low: 1865197776,
high: 369,
unsigned: false,
},
},
],
geometry: {
lengths: [2],
coords: [
{ low: 259084288, high: -28, unsigned: false },
{ low: -2050327040, high: -11, unsigned: false },
],
},
},
{
attributes: [
{ uintValue: 3 },
{ stringValue: 'draft' },
{ stringValue: 'Workhorse' },
{
sint64Value: {
low: -1455179568,
high: 332,
unsigned: false,
},
},
],
geometry: {
lengths: [2],
coords: [
{ low: -1215752192, high: -24, unsigned: false },
{ low: -1345294336, high: -10, unsigned: false },
],
},
},
],
},
},
});
} catch (error) {
console.error(error);
throw error;
}
});
});
});
});
Loading

0 comments on commit 032d3f1

Please sign in to comment.