Skip to content

Support mapbox content type #1500

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

Merged
merged 5 commits into from
Aug 2, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/transport.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ class MyTransport extends Transport {
}
----

==== Supported content types

- `application/json`, in this case the transport will return a plain JavaScript object
- `text/plain`, in this case the transport will return a plain string
- `application/vnd.mapbox-vector-tile`, in this case the transport will return a Buffer
- `application/vnd.elasticsearch+json`, in this case the transport will return a plain JavaScript object

4 changes: 2 additions & 2 deletions lib/Transport.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ export default class Transport {
_nextSniff: number;
_isSniffing: boolean;
constructor(opts: TransportOptions);
request(params: TransportRequestParams, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse>;
request(params: TransportRequestParams, options?: TransportRequestOptions, callback?: (err: ApiError, result: ApiResponse) => void): TransportRequestCallback;
request<TResponse = Record<string, any>, TContext = Context>(params: TransportRequestParams, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
request<TResponse = Record<string, any>, TContext = Context>(params: TransportRequestParams, options?: TransportRequestOptions, callback?: (err: ApiError, result: ApiResponse<TResponse, TContext>) => void): TransportRequestCallback;
getConnection(opts: TransportGetConnectionOptions): Connection | null;
sniff(opts?: TransportSniffOptions, callback?: (...args: any[]) => void): void;
}
14 changes: 9 additions & 5 deletions lib/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class Transport {

const contentEncoding = (result.headers['content-encoding'] || '').toLowerCase()
const isCompressed = contentEncoding.indexOf('gzip') > -1 || contentEncoding.indexOf('deflate') > -1
const isVectorTile = (result.headers['content-type'] || '').indexOf('application/vnd.mapbox-vector-tile') > -1

/* istanbul ignore else */
if (result.headers['content-length'] !== undefined) {
Expand All @@ -255,8 +256,9 @@ class Transport {
}
// if the response is compressed, we must handle it
// as buffer for allowing decompression later
let payload = isCompressed ? [] : ''
const onData = isCompressed
// while if it's a vector tile, we should return it as buffer
let payload = isCompressed || isVectorTile ? [] : ''
const onData = isCompressed || isVectorTile
? chunk => { payload.push(chunk) }
: chunk => { payload += chunk }
const onEnd = err => {
Expand All @@ -272,7 +274,7 @@ class Transport {
if (isCompressed) {
unzip(Buffer.concat(payload), onBody)
} else {
onBody(null, payload)
onBody(null, isVectorTile ? Buffer.concat(payload) : payload)
}
}

Expand All @@ -281,7 +283,7 @@ class Transport {
onEnd(new Error('Response aborted while reading the body'))
}

if (!isCompressed) {
if (!isCompressed && !isVectorTile) {
response.setEncoding('utf8')
}

Expand All @@ -297,7 +299,9 @@ class Transport {
this.emit('response', err, result)
return callback(err, result)
}
if (Buffer.isBuffer(payload)) {

const isVectorTile = (result.headers['content-type'] || '').indexOf('application/vnd.mapbox-vector-tile') > -1
if (Buffer.isBuffer(payload) && !isVectorTile) {
payload = payload.toString()
}
const isHead = params.method === 'HEAD'
Expand Down
73 changes: 73 additions & 0 deletions test/unit/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2689,3 +2689,76 @@ test('The callback with a sync error should be called in the next tick - ndjson'
t.type(transportReturn.catch, 'function')
t.type(transportReturn.abort, 'function')
})

test('Support mapbox vector tile', t => {
t.plan(2)
const mvtContent = 'GoMCCgRtZXRhEikSFAAAAQACAQMBBAAFAgYDBwAIBAkAGAMiDwkAgEAagEAAAP8//z8ADxoOX3NoYXJkcy5mYWlsZWQaD19zaGFyZHMuc2tpcHBlZBoSX3NoYXJkcy5zdWNjZXNzZnVsGg1fc2hhcmRzLnRvdGFsGhlhZ2dyZWdhdGlvbnMuX2NvdW50LmNvdW50GhdhZ2dyZWdhdGlvbnMuX2NvdW50LnN1bRoTaGl0cy50b3RhbC5yZWxhdGlvbhoQaGl0cy50b3RhbC52YWx1ZRoJdGltZWRfb3V0GgR0b29rIgIwACICMAIiCRkAAAAAAAAAACIECgJlcSICOAAogCB4Ag=='

function handler (req, res) {
res.setHeader('Content-Type', 'application/vnd.mapbox-vector-tile')
res.end(Buffer.from(mvtContent, 'base64'))
}

buildServer(handler, ({ port }, server) => {
const pool = new ConnectionPool({ Connection })
pool.addConnection(`http://localhost:${port}`)

const transport = new Transport({
emit: () => {},
connectionPool: pool,
serializer: new Serializer(),
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false
})
skipProductCheck(transport)

transport.request({
method: 'GET',
path: '/hello'
}, (err, { body }) => {
t.error(err)
t.same(body.toString('base64'), Buffer.from(mvtContent, 'base64').toString('base64'))
server.stop()
})
})
})

test('Compressed mapbox vector tile', t => {
t.plan(2)
const mvtContent = 'GoMCCgRtZXRhEikSFAAAAQACAQMBBAAFAgYDBwAIBAkAGAMiDwkAgEAagEAAAP8//z8ADxoOX3NoYXJkcy5mYWlsZWQaD19zaGFyZHMuc2tpcHBlZBoSX3NoYXJkcy5zdWNjZXNzZnVsGg1fc2hhcmRzLnRvdGFsGhlhZ2dyZWdhdGlvbnMuX2NvdW50LmNvdW50GhdhZ2dyZWdhdGlvbnMuX2NvdW50LnN1bRoTaGl0cy50b3RhbC5yZWxhdGlvbhoQaGl0cy50b3RhbC52YWx1ZRoJdGltZWRfb3V0GgR0b29rIgIwACICMAIiCRkAAAAAAAAAACIECgJlcSICOAAogCB4Ag=='

function handler (req, res) {
const body = gzipSync(Buffer.from(mvtContent, 'base64'))
res.setHeader('Content-Type', 'application/vnd.mapbox-vector-tile')
res.setHeader('Content-Encoding', 'gzip')
res.setHeader('Content-Length', Buffer.byteLength(body))
res.end(body)
}

buildServer(handler, ({ port }, server) => {
const pool = new ConnectionPool({ Connection })
pool.addConnection(`http://localhost:${port}`)

const transport = new Transport({
emit: () => {},
connectionPool: pool,
serializer: new Serializer(),
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false
})
skipProductCheck(transport)

transport.request({
method: 'GET',
path: '/hello'
}, (err, { body }) => {
t.error(err)
t.same(body.toString('base64'), Buffer.from(mvtContent, 'base64').toString('base64'))
server.stop()
})
})
})