@stamhoofd/redirecter 2.118.1 → 2.120.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -7
- package/src/boot.ts +10 -6
- package/src/classes/Geolocator.test.ts +34 -0
- package/src/classes/Geolocator.ts +175 -37
- package/src/data/belgium.csv +1316 -225
- package/src/data/netherlands.csv +8202 -979
- package/src/endpoints/RedirectEndpoint.ts +6 -18
- package/{index.ts → src/index.ts} +1 -1
- package/stamhoofd.d.ts +2 -0
- package/tests/vitest.global.setup.ts +20 -0
- package/tests/vitest.setup.ts +30 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.json +9 -33
- package/tsconfig.test.json +17 -0
- package/vitest.config.js +13 -0
- package/eslint.config.mjs +0 -5
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DecodedRequest,
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import type { DecodedRequest, Request} from '@simonbackx/simple-endpoints';
|
|
2
|
+
import { Endpoint, Response } from '@simonbackx/simple-endpoints';
|
|
3
|
+
import { Country } from '@stamhoofd/types/Country';
|
|
4
4
|
import { Geolocator } from '../classes/Geolocator.js';
|
|
5
5
|
|
|
6
6
|
type Params = Record<string, never>;
|
|
@@ -8,25 +8,13 @@ type Body = undefined;
|
|
|
8
8
|
type Query = undefined;
|
|
9
9
|
type ResponseBody = string;
|
|
10
10
|
|
|
11
|
-
function getRequestIP(request: Request): string {
|
|
12
|
-
let ipAddress = request.request?.socket.remoteAddress;
|
|
13
|
-
if (request.headers['x-real-ip'] && typeof request.headers['x-real-ip'] === 'string' && (ipAddress === '127.0.0.1' || ipAddress === '0.0.0.0')) {
|
|
14
|
-
ipAddress = request.headers['x-real-ip'];
|
|
15
|
-
}
|
|
16
|
-
if (!ipAddress) {
|
|
17
|
-
ipAddress = '?';
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return ipAddress.split(':', 2)[0];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
11
|
export class RedirectEndpoint extends Endpoint<Params, Query, Body, ResponseBody> {
|
|
24
12
|
protected doesMatch(_request: Request): [true, Params] | [false] {
|
|
25
13
|
return [true, {}];
|
|
26
14
|
}
|
|
27
15
|
|
|
28
16
|
async handle(request: DecodedRequest<Params, Query, Body>) {
|
|
29
|
-
const ip =
|
|
17
|
+
const ip = request.request.getIP()
|
|
30
18
|
|
|
31
19
|
const country = Geolocator.shared.getCountry(ip);
|
|
32
20
|
|
|
@@ -34,7 +22,7 @@ export class RedirectEndpoint extends Endpoint<Params, Query, Body, ResponseBody
|
|
|
34
22
|
|
|
35
23
|
if (country === Country.Belgium) {
|
|
36
24
|
const url = 'https://www.stamhoofd.be' + path;
|
|
37
|
-
const response = new Response($t(`
|
|
25
|
+
const response = new Response($t(`%G3`) + ' ' + url);
|
|
38
26
|
response.status = 302;
|
|
39
27
|
response.headers['Location'] = url;
|
|
40
28
|
|
|
@@ -44,7 +32,7 @@ export class RedirectEndpoint extends Endpoint<Params, Query, Body, ResponseBody
|
|
|
44
32
|
}
|
|
45
33
|
|
|
46
34
|
const url = 'https://www.stamhoofd.nl' + path;
|
|
47
|
-
const response = new Response($t(`
|
|
35
|
+
const response = new Response($t(`%G3`) + ' ' + url);
|
|
48
36
|
response.status = 302;
|
|
49
37
|
response.headers['Location'] = url;
|
|
50
38
|
|
|
@@ -5,7 +5,7 @@ backendEnv.load({ service: 'redirecter' }).catch((error) => {
|
|
|
5
5
|
console.error('Failed to load environment:', error);
|
|
6
6
|
process.exit(1);
|
|
7
7
|
}).then(async () => {
|
|
8
|
-
await import('./
|
|
8
|
+
await import('./boot.js');
|
|
9
9
|
}).catch((error) => {
|
|
10
10
|
console.error('Failed to start the API:', error);
|
|
11
11
|
process.exit(1);
|
package/stamhoofd.d.ts
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// first import nock
|
|
2
|
+
import nock from 'nock';
|
|
3
|
+
|
|
4
|
+
// prevent nock import from being removed on save
|
|
5
|
+
console.log('Imported nock: ', !!nock);
|
|
6
|
+
|
|
7
|
+
import { TestUtils } from '@stamhoofd/test-utils';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// Set timezone!
|
|
11
|
+
process.env.TZ = 'UTC';
|
|
12
|
+
|
|
13
|
+
// Quick check
|
|
14
|
+
if (new Date().getTimezoneOffset() !== 0) {
|
|
15
|
+
throw new Error('Process should always run in UTC timezone');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function setup() {
|
|
19
|
+
TestUtils.globalSetup();
|
|
20
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// first import nock
|
|
2
|
+
|
|
3
|
+
import { Column } from '@simonbackx/simple-database';
|
|
4
|
+
import { Request } from '@simonbackx/simple-endpoints';
|
|
5
|
+
import { Version } from '@stamhoofd/structures';
|
|
6
|
+
import { TestUtils } from '@stamhoofd/test-utils';
|
|
7
|
+
|
|
8
|
+
Error.stackTraceLimit = 100;
|
|
9
|
+
|
|
10
|
+
// Set version of saved structures
|
|
11
|
+
Column.setJSONVersion(Version);
|
|
12
|
+
|
|
13
|
+
// Automatically set endpoint default version to latest one (only in tests!)
|
|
14
|
+
Request.defaultVersion = Version;
|
|
15
|
+
|
|
16
|
+
// Set timezone!
|
|
17
|
+
process.env.TZ = 'UTC';
|
|
18
|
+
|
|
19
|
+
// Quick check
|
|
20
|
+
if (new Date().getTimezoneOffset() !== 0) {
|
|
21
|
+
throw new Error('Process should always run in UTC timezone');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
console.log = () => {};
|
|
25
|
+
|
|
26
|
+
beforeAll(async () => {
|
|
27
|
+
// Override default $t handlers
|
|
28
|
+
TestUtils.loadEnvironment();
|
|
29
|
+
});
|
|
30
|
+
TestUtils.setup();
|
package/tsconfig.json
CHANGED
|
@@ -1,36 +1,12 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"sourceMap": true,
|
|
12
|
-
"strictNullChecks": true,
|
|
13
|
-
"declaration": true,
|
|
14
|
-
"declarationMap": true,
|
|
15
|
-
"outDir": "dist",
|
|
16
|
-
"lib": [
|
|
17
|
-
"es2022"
|
|
18
|
-
],
|
|
19
|
-
"types": [
|
|
20
|
-
"node",
|
|
21
|
-
"jest",
|
|
22
|
-
"@stamhoofd/backend-i18n",
|
|
23
|
-
]
|
|
24
|
-
},
|
|
25
|
-
"include": [
|
|
26
|
-
"src/**/*.ts",
|
|
27
|
-
"tests/**/*.ts",
|
|
28
|
-
"index.ts",
|
|
29
|
-
"migrations.ts",
|
|
30
|
-
"../../../*.d.ts",
|
|
31
|
-
"*.d.ts"
|
|
32
|
-
],
|
|
33
|
-
"exclude": [
|
|
34
|
-
"node_modules"
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"files": [],
|
|
4
|
+
"references": [
|
|
5
|
+
{
|
|
6
|
+
"path": "./tsconfig.build.json"
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"path": "./tsconfig.test.json"
|
|
10
|
+
}
|
|
35
11
|
]
|
|
36
12
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.test.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": ".",
|
|
5
|
+
"outDir": "dist"
|
|
6
|
+
},
|
|
7
|
+
"references": [
|
|
8
|
+
{ "path": "./tsconfig.build.json" }
|
|
9
|
+
],
|
|
10
|
+
"include": [
|
|
11
|
+
"./stamhoofd.d.ts",
|
|
12
|
+
"../../../jest-extended.d.ts",
|
|
13
|
+
"./src/**/*.spec.ts",
|
|
14
|
+
"./src/**/*.test.ts",
|
|
15
|
+
"./tests"
|
|
16
|
+
]
|
|
17
|
+
}
|
package/vitest.config.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globalSetup: './tests/vitest.global.setup.ts',
|
|
6
|
+
setupFiles: ['./tests/vitest.setup.ts'],
|
|
7
|
+
watch: false,
|
|
8
|
+
globals: true,
|
|
9
|
+
root: import.meta.dirname,
|
|
10
|
+
isolate: true,
|
|
11
|
+
maxWorkers: 1, // For now we can't run parallel because all test files use the same database
|
|
12
|
+
},
|
|
13
|
+
});
|