brasil-ceps-offline 2.0.0 → 2.0.2
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/dist/scripts/download-db.js +25 -29
- package/package.json +1 -1
|
@@ -3,18 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const https_1 = __importDefault(require("https"));
|
|
7
6
|
const fs_1 = __importDefault(require("fs"));
|
|
8
7
|
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
//
|
|
8
|
+
const promises_1 = require("stream/promises");
|
|
9
|
+
const stream_1 = require("stream");
|
|
10
|
+
const zlib_1 = __importDefault(require("zlib")); // <-- O import que estava faltando!
|
|
11
|
+
// ⚠️ Coloque a URL EXATA do arquivo .gz do seu release da v2.0.0
|
|
12
|
+
const URL_DO_BANCO = 'https://github.com/kaique-oliveira/brasil-ceps-offline/releases/download/v2.0.0/brasil-ceps.sqlite.gz';
|
|
13
13
|
const DB_DIR = path_1.default.join(__dirname, '..', '..', '.db');
|
|
14
|
-
// Ajustamos o nome para casar exatamente com o que a lib está procurando
|
|
15
14
|
const DB_PATH = path_1.default.join(DB_DIR, 'ceps.sqlite');
|
|
16
|
-
function downloadDatabase() {
|
|
17
|
-
// Kill switch para pipelines de CI/CD (GitHub Actions, Vercel, etc)
|
|
15
|
+
async function downloadDatabase() {
|
|
18
16
|
if (process.env.SKIP_POSTINSTALL_DB === 'true') {
|
|
19
17
|
console.log('⏩ CI/CD detectado: Pulando o download automático do banco de dados.');
|
|
20
18
|
return;
|
|
@@ -22,33 +20,31 @@ function downloadDatabase() {
|
|
|
22
20
|
if (!fs_1.default.existsSync(DB_DIR)) {
|
|
23
21
|
fs_1.default.mkdirSync(DB_DIR, { recursive: true });
|
|
24
22
|
}
|
|
25
|
-
// Se o banco já existe (ex: o dev tá só rodando npm install de novo), não baixa de novo
|
|
26
23
|
if (fs_1.default.existsSync(DB_PATH)) {
|
|
27
24
|
console.log('✅ Banco de CEPs já está instalado e pronto para uso.');
|
|
28
25
|
return;
|
|
29
26
|
}
|
|
30
|
-
console.log('📦 Baixando banco de dados de CEPs do Brasil offline
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (response.
|
|
34
|
-
|
|
35
|
-
https_1.default.get(response.headers.location, processDownload);
|
|
27
|
+
console.log('📦 Baixando banco de dados de CEPs do Brasil offline...');
|
|
28
|
+
try {
|
|
29
|
+
const response = await fetch(URL_DO_BANCO);
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
throw new Error(`HTTP Error: ${response.status} - ${response.statusText}`);
|
|
36
32
|
}
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
if (!response.body) {
|
|
34
|
+
throw new Error('O corpo da resposta da URL está vazio.');
|
|
39
35
|
}
|
|
40
|
-
|
|
41
|
-
.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
function processDownload(response) {
|
|
46
|
-
const unzip = zlib_1.default.createGunzip(); // Descompactador nativo do Node
|
|
47
|
-
const file = fs_1.default.createWriteStream(DB_PATH);
|
|
48
|
-
response.pipe(unzip).pipe(file);
|
|
49
|
-
file.on('finish', () => {
|
|
50
|
-
file.close();
|
|
36
|
+
const nodeStream = stream_1.Readable.fromWeb(response.body);
|
|
37
|
+
const fileStream = fs_1.default.createWriteStream(DB_PATH);
|
|
38
|
+
// Como o arquivo é .gz, usamos o zlib.createGunzip() no meio do caminho para extrair
|
|
39
|
+
await (0, promises_1.pipeline)(nodeStream, zlib_1.default.createGunzip(), fileStream);
|
|
51
40
|
console.log('🚀 Banco de dados baixado e descompactado com sucesso! Zero-latência ativada.');
|
|
52
|
-
}
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error('❌ Erro crítico ao baixar o banco de CEPs:', error.message);
|
|
44
|
+
if (fs_1.default.existsSync(DB_PATH)) {
|
|
45
|
+
fs_1.default.unlinkSync(DB_PATH);
|
|
46
|
+
}
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
53
49
|
}
|
|
54
50
|
downloadDatabase();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brasil-ceps-offline",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Motor de Inteligência Geográfica Offline para CEPs brasileiros — dados do Censo IBGE 2022 com IBGE, DDD, fuso horário e coordenadas",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|