strapi-plugin-magic-sessionmanager 4.7.2 → 4.8.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/CHANGELOG.md +20 -0
- package/README.md +17 -38
- package/dist/admin/Analytics-CaRlu5ad.js +332 -0
- package/dist/admin/Analytics-THqDbNX0.mjs +330 -0
- package/dist/admin/App-AbZwLHtF.js +1263 -0
- package/dist/admin/App-hSh7guii.mjs +1261 -0
- package/dist/admin/FormField-C8kIT2NE.mjs +59 -0
- package/dist/admin/FormField-b5KBvkrj.js +60 -0
- package/dist/admin/License-BXBVwUVu.mjs +299 -0
- package/dist/admin/License-CABdPwnd.js +301 -0
- package/dist/admin/OnlineUsersWidget-BnWFj9IM.mjs +230 -0
- package/dist/admin/OnlineUsersWidget-CPFAACSb.js +230 -0
- package/dist/admin/PluginPageLayout-BPFMcgrx.mjs +42 -0
- package/dist/admin/PluginPageLayout-DmNzMdqu.js +43 -0
- package/dist/admin/Settings-B4DNdeKx.js +2152 -0
- package/dist/admin/Settings-C4RsJosh.mjs +2150 -0
- package/dist/admin/{de-JBE-iylO.mjs → de-AwIFTd9i.mjs} +154 -20
- package/dist/admin/{de-BnqGCokx.js → de-D23nmqnl.js} +154 -20
- package/dist/admin/{en-BRmVwd8-.js → en-C5iYQ0mC.js} +155 -21
- package/dist/admin/{en-kQWtdEai.mjs → en-D6QEGeRh.mjs} +155 -21
- package/dist/admin/{es-CH691xcL.js → es-CIXnsG-p.js} +154 -20
- package/dist/admin/{es-DOQvBxih.mjs → es-CnFfVdKw.mjs} +154 -20
- package/dist/admin/{fr-CLExO0w_.js → fr-BglXE-W4.js} +154 -20
- package/dist/admin/{fr-CmfCpepX.mjs → fr-DefoR7uH.mjs} +154 -20
- package/dist/admin/index-BFVITQ-o.mjs +624 -0
- package/dist/admin/{index-Duc7cR_d.js → index-DlMm-NyK.js} +261 -188
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.mjs +1 -1
- package/dist/admin/maskSensitiveValue-1uGWmrfG.mjs +20 -0
- package/dist/admin/maskSensitiveValue-B9vhxPad.js +19 -0
- package/dist/admin/{pt-BJ_F0yNi.js → pt-J5sf7jiY.js} +154 -20
- package/dist/admin/{pt-BSzZdyAH.mjs → pt-TODwvNQA.mjs} +154 -20
- package/dist/server/index.js +4396 -7820
- package/dist/server/index.mjs +4396 -7820
- package/package.json +21 -19
- package/scripts/geoip-updater.cjs +234 -15
- package/scripts/update-geoip-db.mjs +3 -0
- package/dist/admin/Analytics-CsrpxJwQ.mjs +0 -497
- package/dist/admin/Analytics-D03ZNNMR.js +0 -499
- package/dist/admin/App-B2VCS3cz.js +0 -1481
- package/dist/admin/App-qxKnK7G2.mjs +0 -1479
- package/dist/admin/License-ClW7sZLR.mjs +0 -318
- package/dist/admin/License-fixnQ7wF.js +0 -320
- package/dist/admin/OnlineUsersWidget-mR_OMiwi.mjs +0 -185
- package/dist/admin/OnlineUsersWidget-qb9utPBy.js +0 -185
- package/dist/admin/Settings-U3V4eGMo.mjs +0 -2038
- package/dist/admin/Settings-gv2BF54w.js +0 -2040
- package/dist/admin/StyledButtons-Cz8oYhmc.mjs +0 -418
- package/dist/admin/StyledButtons-DDuxnYz8.js +0 -419
- package/dist/admin/index-B_lFjUAg.mjs +0 -551
- package/dist/admin/sessionStatus-D_fWEzgj.js +0 -11
- package/dist/admin/sessionStatus-kwggjEMw.mjs +0 -12
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "4.
|
|
2
|
+
"version": "4.8.0",
|
|
3
3
|
"keywords": [
|
|
4
4
|
"strapi",
|
|
5
5
|
"plugin",
|
|
@@ -41,12 +41,14 @@
|
|
|
41
41
|
"pics"
|
|
42
42
|
],
|
|
43
43
|
"engines": {
|
|
44
|
-
"node": "
|
|
44
|
+
"node": "^22.22.2 || >=24.15.0",
|
|
45
45
|
"npm": ">=6.0.0"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
49
49
|
"test": "node --test tests/*.test.js tests/*.test.mjs",
|
|
50
|
+
"test:ts:front": "tsc --noEmit --allowJs --jsx react-jsx --moduleResolution bundler --module esnext -p admin/jsconfig.json",
|
|
51
|
+
"test:ts:back": "tsc --noEmit --allowJs --moduleResolution node --module commonjs -p server/jsconfig.json",
|
|
50
52
|
"build": "npm run clean && strapi-plugin build && node scripts/build-server-bundle.mjs",
|
|
51
53
|
"watch": "strapi-plugin watch",
|
|
52
54
|
"watch:link": "strapi-plugin watch:link",
|
|
@@ -56,28 +58,28 @@
|
|
|
56
58
|
"prepublishOnly": "npm test && npm run build && npm run verify && npm run verify:runtime"
|
|
57
59
|
},
|
|
58
60
|
"dependencies": {
|
|
59
|
-
"@maxmind/geoip2-node": "^
|
|
60
|
-
"sanitize-html": "^2.
|
|
61
|
+
"@maxmind/geoip2-node": "^7.1.0",
|
|
62
|
+
"sanitize-html": "^2.18.0"
|
|
61
63
|
},
|
|
62
64
|
"devDependencies": {
|
|
63
|
-
"@semantic-release/changelog": "^
|
|
64
|
-
"@semantic-release/commit-analyzer": "^13.0.
|
|
65
|
-
"@semantic-release/git": "^
|
|
66
|
-
"@semantic-release/github": "^12.0.
|
|
67
|
-
"@semantic-release/npm": "^13.
|
|
65
|
+
"@semantic-release/changelog": "^7.0.0",
|
|
66
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
67
|
+
"@semantic-release/git": "^11.0.1",
|
|
68
|
+
"@semantic-release/github": "^12.0.10",
|
|
69
|
+
"@semantic-release/npm": "^13.2.0",
|
|
68
70
|
"@semantic-release/release-notes-generator": "^14.1.1",
|
|
69
|
-
"@strapi/design-system": "^2.2.
|
|
70
|
-
"@strapi/icons": "^2.2.
|
|
71
|
+
"@strapi/design-system": "^2.2.4",
|
|
72
|
+
"@strapi/icons": "^2.2.4",
|
|
71
73
|
"@strapi/sdk-plugin": "^6.1.1",
|
|
72
|
-
"@strapi/strapi": "
|
|
73
|
-
"esbuild": "^0.28.
|
|
74
|
-
"prettier": "^3.9.
|
|
74
|
+
"@strapi/strapi": "5.61.0",
|
|
75
|
+
"esbuild": "^0.28.2",
|
|
76
|
+
"prettier": "^3.9.10",
|
|
75
77
|
"react": "^18.3.1",
|
|
76
78
|
"react-dom": "^18.3.1",
|
|
77
|
-
"react-intl": "^
|
|
78
|
-
"react-router-dom": "^6.30.
|
|
79
|
-
"semantic-release": "^25.0.
|
|
80
|
-
"styled-components": "^6.
|
|
79
|
+
"react-intl": "^6.8.9",
|
|
80
|
+
"react-router-dom": "^6.30.6",
|
|
81
|
+
"semantic-release": "^25.0.9",
|
|
82
|
+
"styled-components": "^6.6.0"
|
|
81
83
|
},
|
|
82
84
|
"peerDependencies": {
|
|
83
85
|
"@strapi/design-system": "^2.0.0",
|
|
@@ -86,7 +88,7 @@
|
|
|
86
88
|
"@strapi/utils": "^5.0.0",
|
|
87
89
|
"react": "^18.3.1",
|
|
88
90
|
"react-dom": "^18.3.1",
|
|
89
|
-
"react-intl": "^
|
|
91
|
+
"react-intl": "^6.0.0",
|
|
90
92
|
"react-router-dom": "^6.0.0",
|
|
91
93
|
"styled-components": "^6.0.0"
|
|
92
94
|
},
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const fs = require('node:fs/promises');
|
|
4
|
+
const crypto = require('node:crypto');
|
|
4
5
|
const path = require('node:path');
|
|
5
6
|
const process = require('node:process');
|
|
6
7
|
const zlib = require('node:zlib');
|
|
@@ -10,18 +11,113 @@ const gunzip = promisify(zlib.gunzip);
|
|
|
10
11
|
|
|
11
12
|
const DEFAULT_EDITION_ID = 'GeoLite2-City';
|
|
12
13
|
const DEFAULT_MMDB_NAME = `${DEFAULT_EDITION_ID}.mmdb`;
|
|
13
|
-
const
|
|
14
|
+
const DEFAULT_GEOIP_DIRECTORY = path.resolve(process.cwd(), 'data', 'magic-sessionmanager', 'geoip');
|
|
15
|
+
const DEFAULT_OUTPUT_PATH = path.join(DEFAULT_GEOIP_DIRECTORY, DEFAULT_MMDB_NAME);
|
|
14
16
|
const SUPPORTED_EDITION_IDS = new Set([
|
|
15
17
|
'GeoLite2-City',
|
|
16
18
|
'GeoLite2-Country',
|
|
17
19
|
'GeoIP2-City',
|
|
18
20
|
'GeoIP2-Country',
|
|
19
21
|
]);
|
|
22
|
+
const SUPPORTED_MMDB_NAMES = new Set([...SUPPORTED_EDITION_IDS].map((editionId) => `${editionId}.mmdb`));
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Resolves a supported MMDB filename into the plugin's dedicated database
|
|
26
|
+
* directory. Absolute paths are accepted only when they already target that
|
|
27
|
+
* exact directory; nested paths, traversal and other directories are rejected.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} [value] Filename or canonical path to an MMDB file
|
|
30
|
+
* @param {string} [directory] Dedicated database directory
|
|
31
|
+
* @returns {string} Lexically resolved MMDB destination
|
|
32
|
+
* @throws {Error} When the destination escapes the dedicated directory
|
|
33
|
+
*/
|
|
34
|
+
function resolveDatabaseTarget(value, directory = DEFAULT_GEOIP_DIRECTORY) {
|
|
35
|
+
const root = path.resolve(directory);
|
|
36
|
+
if (value !== undefined && typeof value !== 'string') {
|
|
37
|
+
throw new Error('GeoIP database path must be a string');
|
|
38
|
+
}
|
|
39
|
+
const requested = typeof value === 'string' && value.trim() ? value.trim() : DEFAULT_MMDB_NAME;
|
|
40
|
+
let filename;
|
|
41
|
+
|
|
42
|
+
if (path.isAbsolute(requested)) {
|
|
43
|
+
const absolute = path.resolve(requested);
|
|
44
|
+
if (path.dirname(absolute) !== root) {
|
|
45
|
+
throw new Error('GeoIP database path must stay inside the dedicated GeoIP directory');
|
|
46
|
+
}
|
|
47
|
+
filename = path.basename(absolute);
|
|
48
|
+
} else {
|
|
49
|
+
if (path.basename(requested) !== requested || requested === '.' || requested === '..') {
|
|
50
|
+
throw new Error('GeoIP database path must stay inside the dedicated GeoIP directory');
|
|
51
|
+
}
|
|
52
|
+
filename = requested;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!SUPPORTED_MMDB_NAMES.has(filename)) {
|
|
56
|
+
throw new Error('GeoIP database path must use a supported MMDB filename');
|
|
57
|
+
}
|
|
20
58
|
|
|
59
|
+
return path.join(root, filename);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Creates and canonicalizes the dedicated GeoIP directory, then rejects
|
|
64
|
+
* symlinked directories, database files or metadata sidecars before a provider
|
|
65
|
+
* request occurs.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} [value] Filename or canonical path to an MMDB file
|
|
68
|
+
* @param {string} [directory] Dedicated database directory
|
|
69
|
+
* @returns {Promise<string>} Canonical MMDB destination
|
|
70
|
+
* @throws {Error} When the directory, database or sidecar is unsafe
|
|
71
|
+
*/
|
|
72
|
+
async function canonicalizeDatabaseTarget(value, directory = DEFAULT_GEOIP_DIRECTORY) {
|
|
73
|
+
const root = path.resolve(directory);
|
|
74
|
+
const target = resolveDatabaseTarget(value, root);
|
|
75
|
+
await fs.mkdir(root, { recursive: true, mode: 0o700 });
|
|
76
|
+
|
|
77
|
+
const rootStat = await fs.lstat(root);
|
|
78
|
+
const canonicalRoot = await fs.realpath(root);
|
|
79
|
+
if (rootStat.isSymbolicLink()) {
|
|
80
|
+
throw new Error('GeoIP database directory must not be a symlink');
|
|
81
|
+
}
|
|
82
|
+
const canonicalTarget = path.join(canonicalRoot, path.basename(target));
|
|
83
|
+
|
|
84
|
+
for (const candidate of [canonicalTarget, `${canonicalTarget}.metadata.json`]) {
|
|
85
|
+
try {
|
|
86
|
+
const stat = await fs.lstat(candidate);
|
|
87
|
+
if (stat.isSymbolicLink()) {
|
|
88
|
+
throw new Error('GeoIP database destination must not be a symlink');
|
|
89
|
+
}
|
|
90
|
+
if (!stat.isFile()) {
|
|
91
|
+
throw new Error('GeoIP database destination must be a regular file');
|
|
92
|
+
}
|
|
93
|
+
if (path.dirname(await fs.realpath(candidate)) !== canonicalRoot) {
|
|
94
|
+
throw new Error('GeoIP database destination is outside the canonical directory');
|
|
95
|
+
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (error.code !== 'ENOENT') throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return canonicalTarget;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Selects a supported MaxMind edition or uses the configured safe default.
|
|
106
|
+
*
|
|
107
|
+
* @param {string} value Requested edition identifier
|
|
108
|
+
* @param {string} [fallback=DEFAULT_EDITION_ID] Fallback edition
|
|
109
|
+
* @returns {string} Supported edition identifier
|
|
110
|
+
*/
|
|
21
111
|
function normalizeEditionId(value, fallback = DEFAULT_EDITION_ID) {
|
|
22
112
|
return SUPPORTED_EDITION_IDS.has(value) ? value : fallback;
|
|
23
113
|
}
|
|
24
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Infers the MaxMind edition from a database filename.
|
|
117
|
+
*
|
|
118
|
+
* @param {string} filePath Candidate MMDB path
|
|
119
|
+
* @returns {string} Supported edition identifier, or an empty string
|
|
120
|
+
*/
|
|
25
121
|
function inferEditionIdFromPath(filePath) {
|
|
26
122
|
if (typeof filePath !== 'string' || filePath.trim() === '') return '';
|
|
27
123
|
|
|
@@ -36,14 +132,36 @@ function inferEditionIdFromPath(filePath) {
|
|
|
36
132
|
return '';
|
|
37
133
|
}
|
|
38
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Builds the fixed MaxMind download endpoint for a supported edition.
|
|
137
|
+
*
|
|
138
|
+
* @param {string} [editionId=DEFAULT_EDITION_ID] MaxMind edition
|
|
139
|
+
* @returns {string} MaxMind archive URL
|
|
140
|
+
*/
|
|
39
141
|
function buildDownloadUrl(editionId = DEFAULT_EDITION_ID) {
|
|
40
142
|
return `https://download.maxmind.com/geoip/databases/${encodeURIComponent(editionId)}/download?suffix=tar.gz`;
|
|
41
143
|
}
|
|
42
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Encodes MaxMind credentials for the updater request header.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} accountId MaxMind account identifier
|
|
149
|
+
* @param {string} licenseKey MaxMind license key
|
|
150
|
+
* @returns {string} Basic authorization header value
|
|
151
|
+
*/
|
|
43
152
|
function buildBasicAuthHeader(accountId, licenseKey) {
|
|
44
153
|
return `Basic ${Buffer.from(`${accountId}:${licenseKey}`).toString('base64')}`;
|
|
45
154
|
}
|
|
46
155
|
|
|
156
|
+
/**
|
|
157
|
+
* Builds updater configuration and confines its output to the GeoIP directory.
|
|
158
|
+
*
|
|
159
|
+
* @param {NodeJS.ProcessEnv} [env=process.env] Environment values
|
|
160
|
+
* @param {string[]} [argv=process.argv.slice(2)] CLI arguments
|
|
161
|
+
* @param {object} [overrides] Explicit test or caller overrides
|
|
162
|
+
* @returns {object} Validated updater configuration
|
|
163
|
+
* @throws {Error} When a requested output path escapes its dedicated directory
|
|
164
|
+
*/
|
|
47
165
|
function resolveConfig(env = process.env, argv = process.argv.slice(2), overrides = {}) {
|
|
48
166
|
const force = argv.includes('--force') || overrides.force === true;
|
|
49
167
|
const configuredOutputPath =
|
|
@@ -57,7 +175,11 @@ function resolveConfig(env = process.env, argv = process.argv.slice(2), override
|
|
|
57
175
|
inferEditionIdFromPath(configuredOutputPath) ||
|
|
58
176
|
DEFAULT_EDITION_ID
|
|
59
177
|
);
|
|
60
|
-
const
|
|
178
|
+
const databaseDirectory = overrides.databaseDirectory || DEFAULT_GEOIP_DIRECTORY;
|
|
179
|
+
const outputPath = resolveDatabaseTarget(
|
|
180
|
+
configuredOutputPath || `${editionId}.mmdb`,
|
|
181
|
+
databaseDirectory
|
|
182
|
+
);
|
|
61
183
|
|
|
62
184
|
return {
|
|
63
185
|
accountId: overrides.accountId || env.MAXMIND_ACCOUNT_ID || env.MAXMIND_USER_ID || '',
|
|
@@ -69,6 +191,12 @@ function resolveConfig(env = process.env, argv = process.argv.slice(2), override
|
|
|
69
191
|
};
|
|
70
192
|
}
|
|
71
193
|
|
|
194
|
+
/**
|
|
195
|
+
* Decides whether the local database differs from the remote edition.
|
|
196
|
+
*
|
|
197
|
+
* @param {object} options Comparison inputs and force flag
|
|
198
|
+
* @returns {boolean} Whether the updater should fetch a fresh archive
|
|
199
|
+
*/
|
|
72
200
|
function needsDownload({
|
|
73
201
|
force,
|
|
74
202
|
localExists,
|
|
@@ -81,6 +209,14 @@ function needsDownload({
|
|
|
81
209
|
return localMetadata?.lastModified !== remoteLastModified;
|
|
82
210
|
}
|
|
83
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Extracts only the requested regular MMDB file from a MaxMind tar.gz archive.
|
|
214
|
+
*
|
|
215
|
+
* @param {Buffer} archiveBuffer Compressed archive bytes
|
|
216
|
+
* @param {string} [preferredFileName=DEFAULT_MMDB_NAME] Expected database file
|
|
217
|
+
* @returns {Promise<{name: string, data: Buffer}>} Archive entry and contents
|
|
218
|
+
* @throws {Error} When the expected regular file is absent
|
|
219
|
+
*/
|
|
84
220
|
async function extractMmdbFromTarGz(archiveBuffer, preferredFileName = DEFAULT_MMDB_NAME) {
|
|
85
221
|
const tarBuffer = await gunzip(archiveBuffer);
|
|
86
222
|
let offset = 0;
|
|
@@ -120,12 +256,26 @@ async function extractMmdbFromTarGz(archiveBuffer, preferredFileName = DEFAULT_M
|
|
|
120
256
|
throw new Error(`No ${preferredFileName} file found in MaxMind archive`);
|
|
121
257
|
}
|
|
122
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Reads a null-terminated UTF-8 value from a fixed-width tar header field.
|
|
261
|
+
*
|
|
262
|
+
* @param {Buffer} buffer Tar header bytes
|
|
263
|
+
* @param {number} start Field offset
|
|
264
|
+
* @param {number} length Field width
|
|
265
|
+
* @returns {string} Decoded header field
|
|
266
|
+
*/
|
|
123
267
|
function readTarString(buffer, start, length) {
|
|
124
268
|
const slice = buffer.subarray(start, start + length);
|
|
125
269
|
const nullIndex = slice.indexOf(0);
|
|
126
270
|
return slice.subarray(0, nullIndex === -1 ? slice.length : nullIndex).toString('utf8');
|
|
127
271
|
}
|
|
128
272
|
|
|
273
|
+
/**
|
|
274
|
+
* Reads updater metadata, returning null for missing or invalid JSON files.
|
|
275
|
+
*
|
|
276
|
+
* @param {string} metadataPath Canonical sidecar file path
|
|
277
|
+
* @returns {Promise<object|null>} Parsed metadata or null
|
|
278
|
+
*/
|
|
129
279
|
async function readMetadata(metadataPath) {
|
|
130
280
|
try {
|
|
131
281
|
return JSON.parse(await fs.readFile(metadataPath, 'utf8'));
|
|
@@ -134,6 +284,12 @@ async function readMetadata(metadataPath) {
|
|
|
134
284
|
}
|
|
135
285
|
}
|
|
136
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Checks whether a database file can be accessed.
|
|
289
|
+
*
|
|
290
|
+
* @param {string} filePath Canonical database file path
|
|
291
|
+
* @returns {Promise<boolean>} Whether the file exists and is accessible
|
|
292
|
+
*/
|
|
137
293
|
async function fileExists(filePath) {
|
|
138
294
|
try {
|
|
139
295
|
await fs.access(filePath);
|
|
@@ -143,6 +299,14 @@ async function fileExists(filePath) {
|
|
|
143
299
|
}
|
|
144
300
|
}
|
|
145
301
|
|
|
302
|
+
/**
|
|
303
|
+
* Requests remote edition metadata from the fixed MaxMind provider.
|
|
304
|
+
*
|
|
305
|
+
* @param {string} url MaxMind edition URL
|
|
306
|
+
* @param {string} authHeader Authorization value
|
|
307
|
+
* @returns {Promise<{lastModified: string, contentDisposition: string}>}
|
|
308
|
+
* @sideeffect Sends an authenticated HEAD request to MaxMind
|
|
309
|
+
*/
|
|
146
310
|
async function fetchHead(url, authHeader) {
|
|
147
311
|
const response = await fetch(url, {
|
|
148
312
|
method: 'HEAD',
|
|
@@ -163,6 +327,14 @@ async function fetchHead(url, authHeader) {
|
|
|
163
327
|
};
|
|
164
328
|
}
|
|
165
329
|
|
|
330
|
+
/**
|
|
331
|
+
* Downloads a MaxMind archive for the selected edition.
|
|
332
|
+
*
|
|
333
|
+
* @param {string} url MaxMind edition URL
|
|
334
|
+
* @param {string} authHeader Authorization value
|
|
335
|
+
* @returns {Promise<Buffer>} Archive bytes
|
|
336
|
+
* @sideeffect Sends an authenticated GET request to MaxMind
|
|
337
|
+
*/
|
|
166
338
|
async function fetchArchive(url, authHeader) {
|
|
167
339
|
const response = await fetch(url, {
|
|
168
340
|
method: 'GET',
|
|
@@ -180,28 +352,72 @@ async function fetchArchive(url, authHeader) {
|
|
|
180
352
|
return Buffer.from(await response.arrayBuffer());
|
|
181
353
|
}
|
|
182
354
|
|
|
355
|
+
/**
|
|
356
|
+
* Writes a new file through a unique, exclusive temporary file and atomic rename.
|
|
357
|
+
*
|
|
358
|
+
* @param {string} outputPath Canonical destination inside the GeoIP directory
|
|
359
|
+
* @param {Buffer|string} data Database or metadata contents
|
|
360
|
+
* @returns {Promise<void>}
|
|
361
|
+
* @sideeffect Creates and renames files in the destination directory
|
|
362
|
+
*/
|
|
183
363
|
async function writeAtomic(outputPath, data) {
|
|
184
364
|
const directory = path.dirname(outputPath);
|
|
185
|
-
|
|
365
|
+
const tempPath = path.join(directory, `.${path.basename(outputPath)}.${crypto.randomUUID()}.tmp`);
|
|
366
|
+
let handle;
|
|
367
|
+
try {
|
|
368
|
+
handle = await fs.open(tempPath, 'wx', 0o600);
|
|
369
|
+
await handle.writeFile(data);
|
|
370
|
+
await handle.sync();
|
|
371
|
+
} catch (error) {
|
|
372
|
+
await handle?.close().catch(() => {});
|
|
373
|
+
await fs.rm(tempPath, { force: true }).catch(() => {});
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
try {
|
|
378
|
+
await handle.close();
|
|
379
|
+
} catch (error) {
|
|
380
|
+
await fs.rm(tempPath, { force: true }).catch(() => {});
|
|
381
|
+
throw error;
|
|
382
|
+
}
|
|
186
383
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
384
|
+
try {
|
|
385
|
+
await fs.rename(tempPath, outputPath);
|
|
386
|
+
} catch (error) {
|
|
387
|
+
await fs.rm(tempPath, { force: true }).catch(() => {});
|
|
388
|
+
throw error;
|
|
389
|
+
}
|
|
190
390
|
}
|
|
191
391
|
|
|
392
|
+
/**
|
|
393
|
+
* Downloads, validates and atomically installs a MaxMind MMDB edition.
|
|
394
|
+
* Destination validation happens before the first provider request.
|
|
395
|
+
*
|
|
396
|
+
* @param {object} [config=resolveConfig()] Credentials, edition and destination
|
|
397
|
+
* @returns {Promise<object>} Safe update result with the installed file path
|
|
398
|
+
* @sideeffect Contacts MaxMind and writes the MMDB plus metadata sidecar
|
|
399
|
+
* @throws {Error} When credentials, destination, archive or provider fail
|
|
400
|
+
*/
|
|
192
401
|
async function updateGeoIpDatabase(config = resolveConfig()) {
|
|
193
402
|
if (!config.accountId || !config.licenseKey) {
|
|
194
403
|
throw new Error('MAXMIND_ACCOUNT_ID and MAXMIND_LICENSE_KEY are required');
|
|
195
404
|
}
|
|
196
405
|
|
|
197
|
-
const
|
|
406
|
+
const editionId = normalizeEditionId(config.editionId);
|
|
407
|
+
const outputPath = await canonicalizeDatabaseTarget(config.outputPath);
|
|
408
|
+
const mmdbFileName = `${editionId}.mmdb`;
|
|
409
|
+
if (path.basename(outputPath) !== mmdbFileName) {
|
|
410
|
+
throw new Error('GeoIP database filename must match the selected edition');
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const url = buildDownloadUrl(editionId);
|
|
198
414
|
const authHeader = buildBasicAuthHeader(config.accountId, config.licenseKey);
|
|
199
|
-
const metadataPath = `${
|
|
415
|
+
const metadataPath = `${outputPath}.metadata.json`;
|
|
200
416
|
|
|
201
417
|
const [head, localMetadata, localExists] = await Promise.all([
|
|
202
418
|
fetchHead(url, authHeader),
|
|
203
419
|
readMetadata(metadataPath),
|
|
204
|
-
fileExists(
|
|
420
|
+
fileExists(outputPath),
|
|
205
421
|
]);
|
|
206
422
|
|
|
207
423
|
if (!needsDownload({
|
|
@@ -212,18 +428,18 @@ async function updateGeoIpDatabase(config = resolveConfig()) {
|
|
|
212
428
|
})) {
|
|
213
429
|
return {
|
|
214
430
|
changed: false,
|
|
215
|
-
outputPath
|
|
431
|
+
outputPath,
|
|
216
432
|
lastModified: head.lastModified,
|
|
217
433
|
message: 'GeoIP database is already current',
|
|
218
434
|
};
|
|
219
435
|
}
|
|
220
436
|
|
|
221
437
|
const archive = await fetchArchive(url, authHeader);
|
|
222
|
-
const extracted = await extractMmdbFromTarGz(archive,
|
|
438
|
+
const extracted = await extractMmdbFromTarGz(archive, mmdbFileName);
|
|
223
439
|
|
|
224
|
-
await writeAtomic(
|
|
225
|
-
await
|
|
226
|
-
editionId
|
|
440
|
+
await writeAtomic(outputPath, extracted.data);
|
|
441
|
+
await writeAtomic(metadataPath, JSON.stringify({
|
|
442
|
+
editionId,
|
|
227
443
|
sourceFile: extracted.name,
|
|
228
444
|
lastModified: head.lastModified,
|
|
229
445
|
downloadedAt: new Date().toISOString(),
|
|
@@ -232,7 +448,7 @@ async function updateGeoIpDatabase(config = resolveConfig()) {
|
|
|
232
448
|
|
|
233
449
|
return {
|
|
234
450
|
changed: true,
|
|
235
|
-
outputPath
|
|
451
|
+
outputPath,
|
|
236
452
|
sourceFile: extracted.name,
|
|
237
453
|
lastModified: head.lastModified,
|
|
238
454
|
message: 'GeoIP database updated',
|
|
@@ -241,15 +457,18 @@ async function updateGeoIpDatabase(config = resolveConfig()) {
|
|
|
241
457
|
|
|
242
458
|
module.exports = {
|
|
243
459
|
DEFAULT_EDITION_ID,
|
|
460
|
+
DEFAULT_GEOIP_DIRECTORY,
|
|
244
461
|
DEFAULT_MMDB_NAME,
|
|
245
462
|
DEFAULT_OUTPUT_PATH,
|
|
246
463
|
buildBasicAuthHeader,
|
|
247
464
|
buildDownloadUrl,
|
|
465
|
+
canonicalizeDatabaseTarget,
|
|
248
466
|
extractMmdbFromTarGz,
|
|
249
467
|
fileExists,
|
|
250
468
|
inferEditionIdFromPath,
|
|
251
469
|
needsDownload,
|
|
252
470
|
readMetadata,
|
|
471
|
+
resolveDatabaseTarget,
|
|
253
472
|
resolveConfig,
|
|
254
473
|
updateGeoIpDatabase,
|
|
255
474
|
};
|
|
@@ -7,15 +7,18 @@ const require = createRequire(import.meta.url);
|
|
|
7
7
|
const updater = require('./geoip-updater.cjs');
|
|
8
8
|
|
|
9
9
|
export const DEFAULT_EDITION_ID = updater.DEFAULT_EDITION_ID;
|
|
10
|
+
export const DEFAULT_GEOIP_DIRECTORY = updater.DEFAULT_GEOIP_DIRECTORY;
|
|
10
11
|
export const DEFAULT_MMDB_NAME = updater.DEFAULT_MMDB_NAME;
|
|
11
12
|
export const DEFAULT_OUTPUT_PATH = updater.DEFAULT_OUTPUT_PATH;
|
|
12
13
|
export const buildBasicAuthHeader = updater.buildBasicAuthHeader;
|
|
13
14
|
export const buildDownloadUrl = updater.buildDownloadUrl;
|
|
15
|
+
export const canonicalizeDatabaseTarget = updater.canonicalizeDatabaseTarget;
|
|
14
16
|
export const extractMmdbFromTarGz = updater.extractMmdbFromTarGz;
|
|
15
17
|
export const fileExists = updater.fileExists;
|
|
16
18
|
export const inferEditionIdFromPath = updater.inferEditionIdFromPath;
|
|
17
19
|
export const needsDownload = updater.needsDownload;
|
|
18
20
|
export const readMetadata = updater.readMetadata;
|
|
21
|
+
export const resolveDatabaseTarget = updater.resolveDatabaseTarget;
|
|
19
22
|
export const resolveConfig = updater.resolveConfig;
|
|
20
23
|
export const updateGeoIpDatabase = updater.updateGeoIpDatabase;
|
|
21
24
|
|