Skip to content

Commit

Permalink
Switch to new (undocumented) Bing Streetside bubble metadata URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed Feb 9, 2024
1 parent 6cebded commit 2a8ce99
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
54 changes: 23 additions & 31 deletions modules/services/StreetsideService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { utilArrayUnion, utilQsString, utilUniqueString } from '@rapid-sdk/util'
import RBush from 'rbush';

import { AbstractSystem } from '../core/AbstractSystem.js';
import { jsonpRequest } from '../util/jsonp_request.js';
import { utilFetchResponse } from '../util/index.js';

const PANNELLUM_JS = 'https://cdn.jsdelivr.net/npm/pannellum@2/build/pannellum.min.js';
Expand Down Expand Up @@ -267,12 +266,7 @@ export class StreetsideService extends AbstractSystem {
if (this._cache.loaded.has(tileID) || this._cache.inflight.has(tileID)) continue;

// Promise.all([this._fetchMetadataAsync(tile), this._loadTileAsync(tile)])
this._loadTileAsync(tile)
.then(results => this._processResults(results))
.catch(err => {
if (err.name === 'AbortError') return; // ok
if (err instanceof Error) console.error(err); // eslint-disable-line no-console
});
this._loadTileAsync(tile);
}
}

Expand Down Expand Up @@ -717,11 +711,11 @@ const streetsideImagesApi = 'http://ecn.t0.tiles.virtualearth.net/tiles/';


/**
* _processResults
* _loadedBubbleData
* Processes the results of the tile data fetch.
* @param {Array} results
*/
_processResults(results) {
_loadedBubbleData(results) {
// const metadata = results[0];
// this._cache.loaded.add(results[1].tile.id);
// const bubbles = results[1].data;
Expand Down Expand Up @@ -920,38 +914,36 @@ const streetsideImagesApi = 'http://ecn.t0.tiles.virtualearth.net/tiles/';
/**
* _loadTileAsync
* bubbles: undocumented / unsupported API?
* see Rapid#1305, iD#10100
*/
_loadTileAsync(tile) {
const inflight = this._cache.inflight.get(tile.id);
if (inflight) return inflight.promise;

const [w, s, e, n] = tile.wgs84Extent.rectangle();
const MAXRESULTS = 2000;

const bubbleURLBase = 'https://dev.virtualearth.net/mapcontrol/HumanScaleServices/GetBubbles.ashx?';
const bubbleURLBase = 'https://t.ssl.ak.tiles.virtualearth.net/tiles/cmd/StreetSideBubbleMetaData?';
const bubbleKey = 'AuftgJsO0Xs8Ts4M1xZUQJQXJNsvmh3IV8DkNieCiy3tCwCUMq76-WpkrBtNAuEm';
const bubbleURL = bubbleURLBase + utilQsString({ n: n, s: s, e: e, w: w, c: MAXRESULTS, appkey: bubbleKey, jsCallback: '{callback}' });
const bubbleURL = bubbleURLBase + utilQsString({ north: n, south: s, east: e, west: w, count: MAXRESULTS, key: bubbleKey });

const inflight = this._cache.inflight.get(tile.id);
if (inflight) return inflight.promise;

// Wrap JSONP request in an abortable Promise
const controller = new AbortController();
const promise = new Promise((resolve, reject) => {
let onAbort;
const request = jsonpRequest(bubbleURL, data => {
if (onAbort) controller.signal.removeEventListener('abort', onAbort);
resolve({ data: data, tile: tile });
const promise = fetch(bubbleURL, { signal: controller.signal })
.then(utilFetchResponse)
.then(data => {
this._loadedBubbleData({
data: JSON.parse(data), // Content-Type is 'text/plain' for some reason
tile: tile
});
})
.catch(err => {
if (err.name === 'AbortError') return; // ok
if (err instanceof Error) console.error(err); // eslint-disable-line no-console
})
.finally(() => {
this._cache.inflight.delete(tile.id);
});

onAbort = () => {
controller.signal.removeEventListener('abort', onAbort);
request.abort();
reject(new DOMException('Aborted', 'AbortError'));
};
controller.signal.addEventListener('abort', onAbort);
})
.finally(() => {
this._cache.inflight.delete(tile.id);
});

this._cache.inflight.set(tile.id, { promise: promise, controller: controller });

return promise;
Expand Down
27 changes: 18 additions & 9 deletions test/browser/services/StreetsideService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ describe('StreetsideService', () => {
});


afterEach(() => {
window.JSONP_FIX = undefined;
});


describe('#initAsync', () => {
it('initializes cache', () => {
const cache = _streetside._cache;
Expand All @@ -64,8 +59,8 @@ describe('StreetsideService', () => {
const spy = sinon.spy();
_streetside.on('loadedData', spy);

window.JSONP_DELAY = 0;
window.JSONP_FIX = [{
const data = [
{
elapsed: 0.001
}, {
id: 1, la: 0, lo: 10.001, al: 0, ro: 0, pi: 0, he: 0, bl: '',
Expand All @@ -82,6 +77,12 @@ describe('StreetsideService', () => {
}
];

fetchMock.mock(/StreetSideBubbleMetaData/, {
body: JSON.stringify(data),
status: 200,
headers: { 'Content-Type': 'text/plain' }
});

_streetside.loadTiles();

window.setTimeout(() => {
Expand All @@ -90,14 +91,15 @@ describe('StreetsideService', () => {
}, 20);
});


it('does not load tiles around Null Island', done => {
_streetside.context.projection.translate([0, 0]); // move map to Null Island

const spy = sinon.spy();
_streetside.on('loadedData', spy);

window.JSONP_DELAY = 0;
window.JSONP_FIX = [{
const data = [
{
elapsed: 0.001
}, {
id: 1, la: 0, lo: 0, al: 0, ro: 0, pi: 0, he: 0, bl: '',
Expand All @@ -114,10 +116,17 @@ describe('StreetsideService', () => {
}
];

fetchMock.mock(/StreetSideBubbleMetaData/, {
body: JSON.stringify(data),
status: 200,
headers: { 'Content-Type': 'text/plain' }
});

_streetside.loadTiles();

window.setTimeout(() => {
expect(spy.notCalled).to.be.ok;
expect(fetchMock.calls().length).to.eql(0); // no tile requests of any kind
done();
}, 20);
});
Expand Down

0 comments on commit 2a8ce99

Please sign in to comment.