-
Notifications
You must be signed in to change notification settings - Fork 7
(refs #469) refactor tile choosing function #470
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
Open
niryuu
wants to merge
1
commit into
geolonia:master
Choose a base branch
from
niryuu:469-refactor-tile-selection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| 'use strict'; | ||
|
|
||
| import assert from 'assert'; | ||
| import { | ||
| transformGeoloniaApiSource, | ||
| transformGeoloniaTileSource, | ||
| transformGeoloniaSprite, | ||
| } from './transform-request'; | ||
|
|
||
| describe('transformGeoloniaApiSource', () => { | ||
| const sourcesUrl = new URL('https://api.geolonia.com/sources?key=test-key&sessionId=abc123'); | ||
|
|
||
| it('should redirect api.geolonia.com URLs to sourcesUrl', () => { | ||
| const result = transformGeoloniaApiSource( | ||
| 'https://api.geolonia.com/some/path', | ||
| sourcesUrl, | ||
| ); | ||
| assert.deepStrictEqual(result, { url: sourcesUrl.toString() }); | ||
| }); | ||
|
|
||
| it('should return null for non-Geolonia URLs', () => { | ||
| const result = transformGeoloniaApiSource( | ||
| 'https://example.com/tiles', | ||
| sourcesUrl, | ||
| ); | ||
| assert.strictEqual(result, null); | ||
| }); | ||
| }); | ||
|
|
||
| describe('transformGeoloniaTileSource', () => { | ||
| const atts = { key: 'test-key', stage: 'v1' }; | ||
| const sessionId = 'session123'; | ||
|
|
||
| it('should transform geolonia:// tile URLs and inject key/sessionId', () => { | ||
| const result = transformGeoloniaTileSource( | ||
| 'geolonia://tiles/myuser/my-tileset', | ||
| atts, | ||
| sessionId, | ||
| ); | ||
| assert.ok(result); | ||
| const url = new URL(result.url); | ||
| assert.strictEqual(url.hostname, 'tileserver.geolonia.com'); | ||
| assert.strictEqual(url.pathname, '/customtiles/my-tileset/tiles.json'); | ||
| assert.strictEqual(url.searchParams.get('key'), 'test-key'); | ||
| assert.strictEqual(url.searchParams.get('sessionId'), 'session123'); | ||
| }); | ||
|
|
||
| it('should inject key/sessionId into Geolonia tiles host URLs', () => { | ||
| const result = transformGeoloniaTileSource( | ||
| 'https://tileserver.geolonia.com/some/path', | ||
| atts, | ||
| sessionId, | ||
| ); | ||
| assert.ok(result); | ||
| const url = new URL(result.url); | ||
| assert.strictEqual(url.searchParams.get('key'), 'test-key'); | ||
| assert.strictEqual(url.searchParams.get('sessionId'), 'session123'); | ||
| }); | ||
|
|
||
| it('should switch to dev hostname when stage is dev', () => { | ||
| const devAtts = { key: 'test-key', stage: 'dev' }; | ||
| const result = transformGeoloniaTileSource( | ||
| 'https://tileserver.geolonia.com/some/path', | ||
| devAtts, | ||
| sessionId, | ||
| ); | ||
| assert.ok(result); | ||
| const url = new URL(result.url); | ||
| assert.strictEqual(url.hostname, 'tileserver-dev.geolonia.com'); | ||
| }); | ||
|
|
||
| it('should return null for non-Geolonia URLs', () => { | ||
| const result = transformGeoloniaTileSource( | ||
| 'https://example.com/tiles', | ||
| atts, | ||
| sessionId, | ||
| ); | ||
| assert.strictEqual(result, null); | ||
| }); | ||
|
|
||
| it('should handle *.tiles.geolonia.com hosts', () => { | ||
| const result = transformGeoloniaTileSource( | ||
| 'https://foo.tiles.geolonia.com/path', | ||
| atts, | ||
| sessionId, | ||
| ); | ||
| assert.ok(result); | ||
| const url = new URL(result.url); | ||
| assert.strictEqual(url.searchParams.get('key'), 'test-key'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('transformGeoloniaSprite', () => { | ||
| const atts = { key: 'test-key', stage: 'v1' }; | ||
|
|
||
| it('should inject key and correct stage into Geolonia sprite URLs', () => { | ||
| const result = transformGeoloniaSprite( | ||
| 'https://api.geolonia.com/dev/sprites/basic-v2/sprite', | ||
| atts, | ||
| ); | ||
| assert.ok(result); | ||
| const url = new URL(result.url); | ||
| assert.strictEqual(url.pathname, '/v1/sprites/basic-v2/sprite'); | ||
| assert.strictEqual(url.searchParams.get('key'), 'test-key'); | ||
| }); | ||
|
|
||
| it('should handle v1 stage in URL', () => { | ||
| const result = transformGeoloniaSprite( | ||
| 'https://api.geolonia.com/v1/sprites/basic-v2/sprite', | ||
| atts, | ||
| ); | ||
| assert.ok(result); | ||
| const url = new URL(result.url); | ||
| assert.strictEqual(url.pathname, '/v1/sprites/basic-v2/sprite'); | ||
| assert.strictEqual(url.searchParams.get('key'), 'test-key'); | ||
| }); | ||
|
|
||
| it('should return null for non-Geolonia sprite URLs', () => { | ||
| const result = transformGeoloniaSprite( | ||
| 'https://example.com/sprites/basic/sprite.json', | ||
| atts, | ||
| ); | ||
| assert.strictEqual(result, null); | ||
| }); | ||
|
|
||
| it('should switch stage from v1 to dev when atts.stage is dev', () => { | ||
| const devAtts = { key: 'test-key', stage: 'dev' }; | ||
| const result = transformGeoloniaSprite( | ||
| 'https://api.geolonia.com/v1/sprites/basic-v2/sprite', | ||
| devAtts, | ||
| ); | ||
| assert.ok(result); | ||
| const url = new URL(result.url); | ||
| assert.strictEqual(url.pathname, '/dev/sprites/basic-v2/sprite'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { isGeoloniaTilesHost } from './util'; | ||
|
|
||
| type RequestParameters = { url: string }; | ||
| type TransformAtts = { key: string; stage: string }; | ||
|
|
||
| /** Redirect Geolonia API source requests (style JSON sources) to the sourcesUrl */ | ||
| export function transformGeoloniaApiSource( | ||
| url: string, | ||
| sourcesUrl: URL, | ||
| ): RequestParameters | null { | ||
| if (url.startsWith('https://api.geolonia.com')) { | ||
| return { url: sourcesUrl.toString() }; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** Transform geolonia:// scheme and inject key/sessionId into Geolonia tile server sources */ | ||
| export function transformGeoloniaTileSource( | ||
| url: string, | ||
| atts: TransformAtts, | ||
| sessionId: string, | ||
| ): RequestParameters | null { | ||
| let transformedUrl = url; | ||
|
|
||
| if (url.startsWith('geolonia://')) { | ||
| const tilesMatch = url.match( | ||
| /^geolonia:\/\/tiles\/(?<username>.+)\/(?<customtileId>.+)/, | ||
| ); | ||
| if (tilesMatch) { | ||
| transformedUrl = `https://tileserver.geolonia.com/customtiles/${tilesMatch.groups.customtileId}/tiles.json`; | ||
| } | ||
| } | ||
|
|
||
| const transformedUrlObj = new URL(transformedUrl); | ||
| if (!isGeoloniaTilesHost(transformedUrlObj)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (atts.stage === 'dev') { | ||
| transformedUrlObj.hostname = 'tileserver-dev.geolonia.com'; | ||
| } | ||
| transformedUrlObj.searchParams.set('sessionId', sessionId); | ||
| transformedUrlObj.searchParams.set('key', atts.key); | ||
| return { url: transformedUrlObj.toString() }; | ||
| } | ||
|
|
||
| /** Inject key and correct stage into Geolonia sprite requests */ | ||
| export function transformGeoloniaSprite( | ||
| url: string, | ||
| atts: TransformAtts, | ||
| ): RequestParameters | null { | ||
| if (!url.match(/^https:\/\/api\.geolonia\.com\/(dev|v1)\/sprites\//)) { | ||
| return null; | ||
| } | ||
| const urlObj = new URL(url); | ||
| const pathParts = urlObj.pathname.split('/'); | ||
| pathParts[1] = String(atts.stage); | ||
| urlObj.pathname = pathParts.join('/'); | ||
| urlObj.searchParams.set('key', atts.key); | ||
| return { url: urlObj.toString() }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
geolonia://URLがパターンにマッチしない場合にランタイムエラーが発生する可能性があります。URLが
geolonia://で始まるが、tilesMatchのパターン(geolonia://tiles/username/customtileId)にマッチしない場合、transformedUrlは変換されずにgeolonia://...のまま残ります。その後、Line 34 でnew URL(transformedUrl)を呼び出すと、geolonia://は有効なURLスキームではないため、TypeErrorがスローされます。🐛 修正案: マッチしない場合は早期リターン
if (url.startsWith('geolonia://')) { const tilesMatch = url.match( /^geolonia:\/\/tiles\/(?<username>.+)\/(?<customtileId>.+)/, ); if (tilesMatch) { transformedUrl = `https://tileserver.geolonia.com/customtiles/${tilesMatch.groups.customtileId}/tiles.json`; + } else { + // geolonia:// URLがパターンにマッチしない場合は変換をスキップ + return null; } }📝 Committable suggestion
🤖 Prompt for AI Agents