-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- preset/server: декомпозиция модуля с утилитами (patch) - preset/node: утилита hideFirstId перенесена в preset/isomorphic (patch)
- Loading branch information
Showing
30 changed files
with
693 additions
and
646 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
24 changes: 24 additions & 0 deletions
24
src/preset/isomorphic/utils/__test__/hide-first-id.test.ts
This file contains 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,24 @@ | ||
import { hideFirstId } from '../hide-first-id'; | ||
|
||
describe('hideFirstId', () => { | ||
const cases = [ | ||
{ | ||
input: '/api/v2/something/123456/some-bff/123456', | ||
output: ['/api/v2/something/{id}/some-bff/123456', 123456], | ||
}, | ||
{ | ||
input: '/api/v2/something/222/some-bff/333', | ||
output: ['/api/v2/something/{id}/some-bff/333', 222], | ||
}, | ||
{ | ||
input: '/api/v2/something/45320/some-bff', | ||
output: ['/api/v2/something/{id}/some-bff', 45320], | ||
}, | ||
]; | ||
|
||
it('should replace first id only', () => { | ||
cases.forEach(({ input, output }) => { | ||
expect(hideFirstId(input)).toEqual(output); | ||
}); | ||
}); | ||
}); |
This file contains 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,14 @@ | ||
/** | ||
* Преобразует строку вида: | ||
* "/api/v2/something/123456/some-bff/123456" | ||
* в строку вида: | ||
* "/api/v2/something/{id}/some-bff/123456" | ||
* и возвращает кортеж с этой строкой и вырезанным числом в случае если оно найдено. | ||
* @param url Url. | ||
* @return Кортеж со строкой и результатом поиска числа. | ||
*/ | ||
export function hideFirstId(url: string): [string, number | undefined] { | ||
const found = /\d{2,}/.exec(url); | ||
|
||
return found ? [url.replace(found[0], '{id}'), Number(found[0])] : [url, undefined]; | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export type { ServerHandler, ServerMiddleware, ServerHandlerContext } from './types'; | ||
export { getClientIp, getForwardedHeaders, getPageResponseFormat, healthCheck } from './utils'; | ||
export { getClientIp } from './utils/get-client-ip'; | ||
export { getForwardedHeaders } from './utils/get-forwarded-headers'; | ||
export { getHealthCheck } from './utils/get-health-check'; | ||
export { getPageResponseFormat } from './utils/get-page-response-format'; |
49 changes: 49 additions & 0 deletions
49
src/preset/server/utils/__test__/apply-server-middleware.test.ts
This file contains 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,49 @@ | ||
import { ServerMiddleware } from '../../types'; | ||
import { applyServerMiddleware } from '../apply-server-middleware'; | ||
|
||
describe('applyServerMiddleware', () => { | ||
it('should compose middleware', async () => { | ||
const log: string[] = []; | ||
|
||
const foo: ServerMiddleware = async (request, next) => { | ||
log.push('<foo>'); | ||
const result = await next(request); | ||
log.push('</foo>'); | ||
return result; | ||
}; | ||
|
||
const bar: ServerMiddleware = async (request, next) => { | ||
log.push('<bar>'); | ||
const result = await next(request); | ||
log.push('</bar>'); | ||
return result; | ||
}; | ||
|
||
const baz: ServerMiddleware = async (request, next) => { | ||
log.push('<baz>'); | ||
const result = await next(request); | ||
log.push('</baz>'); | ||
return result; | ||
}; | ||
|
||
const enhancer = applyServerMiddleware(foo, bar, baz); | ||
|
||
const handler = enhancer(() => { | ||
log.push('<handler />'); | ||
return new Response('Test'); | ||
}); | ||
|
||
await handler(new Request('http://test.com'), { events: new EventTarget() }); | ||
|
||
expect(log).toEqual([ | ||
// expected log | ||
'<foo>', | ||
'<bar>', | ||
'<baz>', | ||
'<handler />', | ||
'</baz>', | ||
'</bar>', | ||
'</foo>', | ||
]); | ||
}); | ||
}); |
This file contains 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,23 @@ | ||
import { getClientIp } from '../get-client-ip'; | ||
|
||
describe('getClientIp', () => { | ||
it('should handle "x-client-ip" header', () => { | ||
const request = new Request('http://test.com', { | ||
headers: { | ||
'x-client-ip': '127.1.2.3', | ||
}, | ||
}); | ||
|
||
expect(getClientIp(request)).toBe('127.1.2.3'); | ||
}); | ||
|
||
it('should handle "x-forwarded-for" header', () => { | ||
const request = new Request('http://test.com', { | ||
headers: { | ||
'x-forwarded-for': '111.222.111.222', | ||
}, | ||
}); | ||
|
||
expect(getClientIp(request)).toBe('111.222.111.222'); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/preset/server/utils/__test__/get-fetch-tracing.test.ts
This file contains 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,41 @@ | ||
import { getFetchTracing } from '../get-fetch-tracing'; | ||
|
||
describe('getFetchTracing', () => { | ||
it('should trace fetch stages', async () => { | ||
const tracer: any = { | ||
startSpan: jest.fn(() => ({ | ||
setAttributes: jest.fn(), | ||
setStatus: jest.fn(), | ||
end: jest.fn(), | ||
})), | ||
}; | ||
const context: any = {}; | ||
const middleware = getFetchTracing(tracer, context); | ||
|
||
const request = new Request('http://test.com/api/v2/product/1005002'); | ||
|
||
expect( | ||
await Promise.resolve( | ||
middleware(request, () => Promise.resolve<Response>(new Response('OK'))), | ||
).catch(() => 'catch!'), | ||
).not.toBe('catch!'); | ||
}); | ||
|
||
it('should handle error', async () => { | ||
const tracer: any = { | ||
startSpan: jest.fn(() => ({ | ||
setAttributes: jest.fn(), | ||
setStatus: jest.fn(), | ||
end: jest.fn(), | ||
})), | ||
}; | ||
const context: any = {}; | ||
const middleware = getFetchTracing(tracer, context); | ||
|
||
const request = new Request('http://test.com/api/v2/product/1005002'); | ||
|
||
expect( | ||
await Promise.resolve(middleware(request, () => Promise.reject('FAKE ERROR'))).catch(e => e), | ||
).toBe('FAKE ERROR'); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
src/preset/server/utils/__test__/get-forwarded-headers.test.ts
This file contains 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,38 @@ | ||
import { getForwardedHeaders } from '../get-forwarded-headers'; | ||
|
||
describe('getForwardedHeaders', () => { | ||
it('should contain headers by convention', () => { | ||
const config = { appName: 'app_name', appVersion: 'version', env: 'env' }; | ||
const request = new Request('http://test.com', { | ||
headers: { | ||
'x-client-ip': '127.0.0.89', | ||
'simaland-foo': 'hello', | ||
'simaland-bar': 'world', | ||
'simaland-params': JSON.stringify({ testParam: 123 }), | ||
}, | ||
}); | ||
const result = getForwardedHeaders(config, request); | ||
|
||
expect(result.get('user-agent')).toBe('simaland-app_name/version'); | ||
expect(result.get('x-client-ip')).toBe('127.0.0.89'); | ||
expect(result.get('simaland-foo')).toBe('hello'); | ||
expect(result.get('simaland-bar')).toBe('world'); | ||
expect(result.get('simaland-params')).toBe(null); | ||
}); | ||
|
||
it('should not contain x-client-ip when client ip is not defined', () => { | ||
const config = { appName: 'app_name', appVersion: 'version', env: 'env' }; | ||
const request = new Request('http://test.com', { | ||
headers: { | ||
'simaland-foo': 'hello', | ||
'simaland-bar': 'world', | ||
}, | ||
}); | ||
const result = getForwardedHeaders(config, request); | ||
|
||
expect(result.get('user-agent')).toBe('simaland-app_name/version'); | ||
expect(result.get('x-client-ip')).toBe(null); | ||
expect(result.get('simaland-foo')).toBe('hello'); | ||
expect(result.get('simaland-bar')).toBe('world'); | ||
}); | ||
}); |
Oops, something went wrong.