cosmemilton-acbr-node 0.1.2 → 0.2.1
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/LICENSE +502 -502
- package/NOTICE +8 -8
- package/README.md +40 -11
- package/acbr.config.json +16 -16
- package/acbr.lock.json +0 -0
- package/dist/cli.js +375 -176
- package/dist/cli.js.map +4 -4
- package/dist/contract.json +0 -0
- package/dist/index.cjs +36 -11
- package/dist/index.cjs.map +2 -2
- package/dist/index.js +36 -11
- package/dist/index.js.map +2 -2
- package/dist/runtime.cjs +36 -11
- package/dist/runtime.cjs.map +2 -2
- package/dist/runtime.js +36 -11
- package/dist/runtime.js.map +2 -2
- package/dist/types/generated/acbr/index.d.ts +1 -0
- package/dist/types/generated/acbr/models.d.ts +0 -0
- package/dist/types/src/cli.d.ts +0 -0
- package/dist/types/src/config.d.ts +0 -0
- package/dist/types/src/contract-help.d.ts +0 -0
- package/dist/types/src/diagnostics.d.ts +2 -0
- package/dist/types/src/generator.d.ts +0 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/native-build.d.ts +1 -0
- package/dist/types/src/offline-check.d.ts +0 -0
- package/dist/types/src/offline-fixture.d.ts +0 -0
- package/dist/types/src/runtime.d.ts +1 -0
- package/dist/types/src/source.d.ts +0 -0
- package/dist/types/src/types.d.ts +4 -2
- package/docs/API.md +116 -90
- package/docs/ARQUITETURA.md +19 -15
- package/docs/CI.md +60 -60
- package/docs/CLI.md +1 -1
- package/docs/DEPENDENCIAS-NATIVAS.md +103 -93
- package/docs/VALIDACAO.md +67 -67
- package/docs/examples/recuperar-xml.mjs +54 -0
- package/docs/licenses/common/Apache-2.0.txt +202 -0
- package/docs/licenses/common/Artistic.txt +131 -0
- package/docs/licenses/common/GFDL-1.2.txt +397 -0
- package/docs/licenses/common/GPL-1.txt +251 -0
- package/docs/licenses/common/GPL-2.txt +339 -0
- package/docs/licenses/common/GPL-3.txt +674 -0
- package/docs/licenses/common/GPL.txt +674 -0
- package/docs/licenses/common/LGPL-2.1.txt +502 -0
- package/docs/licenses/common/LGPL-2.txt +481 -0
- package/docs/licenses/common/LGPL-3.txt +165 -0
- package/docs/licenses/common/LGPL.txt +165 -0
- package/docs/licenses/common/ORIGIN.json +208 -0
- package/docs/licenses/fpc/COPYING.FPC +25 -25
- package/docs/licenses/fpc/COPYING.GPL.txt +340 -340
- package/docs/licenses/fpc/COPYING.LGPL.txt +504 -504
- package/docs/licenses/fpc/ORIGIN.json +21 -21
- package/docs/licenses/lazarus/COPYING.LGPL.txt +481 -481
- package/docs/licenses/lazarus/COPYING.modifiedLGPL.txt +26 -26
- package/docs/licenses/lazarus/COPYING.txt +46 -46
- package/docs/licenses/lazarus/ORIGIN.json +1 -1
- package/native/NativeMotor.pas +55 -3
- package/native/NativeProtocol.pas +3 -2
- package/native/NativeRecovery.pas +274 -0
- package/native/generated/AcbrModels.pas +0 -0
- package/package.json +6 -4
- package/scripts/build-native-deps.mjs +237 -237
- package/scripts/runtime-license-inventory.d.mts +5 -0
- package/scripts/runtime-license-inventory.mjs +161 -0
- package/tools/extract-model.lpr +119 -119
package/dist/index.js
CHANGED
|
@@ -9,6 +9,12 @@ import { TextDecoder } from "node:util";
|
|
|
9
9
|
// src/diagnostics.ts
|
|
10
10
|
var ETAPAS = /* @__PURE__ */ new Set(["configuracao", "documento", "runtime", "geracao", "validacao", "assinatura", "transmissao", "consulta", "cancelamento", "carta_correcao", "inutilizacao", "operacao", "limpeza"]);
|
|
11
11
|
var CAMPOS = /* @__PURE__ */ new Set(["cnpj", "uf", "modelo", "ambiente", "contrato", "certificado", "certificado.senha", "certificado.pfx", "certificado.arquivo", "csc", "csc.id", "csc.valor", "timeoutMs", "limiteFila", "limiteBytes", "runtimeDirectory", "documento", "xml", "consulta", "chave", "recibo", "cancelamento", "protocolo", "justificativa", "cartaCorrecao", "correcao", "sequenciaEvento", "inutilizacao", "ano", "serie", "numeroInicial", "numeroFinal", "operacao", "signal"]);
|
|
12
|
+
var MAX_RECOVERED_XML_BYTES = 4 * 1024 * 1024;
|
|
13
|
+
function base64XmlRecuperadoValido(value) {
|
|
14
|
+
if (typeof value !== "string" || value.length === 0 || value.length > Math.ceil(MAX_RECOVERED_XML_BYTES / 3) * 4) return false;
|
|
15
|
+
const bytes = Buffer.from(value, "base64");
|
|
16
|
+
return bytes.length > 0 && bytes.length <= MAX_RECOVERED_XML_BYTES && bytes.toString("base64") === value;
|
|
17
|
+
}
|
|
12
18
|
var CATALOGO = {
|
|
13
19
|
CONFIGURACAO_INVALIDA: ["configuracao", "A configura\xE7\xE3o cont\xE9m um campo inv\xE1lido.", "Confira o campo indicado e seu tipo antes de continuar."],
|
|
14
20
|
CONTRATO_INVALIDO: ["configuracao", "O contrato do cliente \xE9 inv\xE1lido.", "Use o contrato gerado junto com o cliente."],
|
|
@@ -24,6 +30,7 @@ var CATALOGO = {
|
|
|
24
30
|
XML_NAO_ASSINADO: ["assinatura", "A opera\xE7\xE3o exige XML previamente assinado.", "Conclua a assinatura e a valida\xE7\xE3o antes de transmitir."],
|
|
25
31
|
XML_ALTERADO: ["assinatura", "O XML assinado foi alterado.", "Use exatamente o XML assinado e persistido; n\xE3o edite seu conte\xFAdo."],
|
|
26
32
|
RETORNO_INVALIDO: ["transmissao", "A identidade ou o protocolo do retorno fiscal \xE9 inv\xE1lido.", "Consulte a situa\xE7\xE3o fiscal antes de decidir repetir a opera\xE7\xE3o."],
|
|
33
|
+
RECUPERACAO_NAO_COMPROVADA: ["consulta", "A consulta n\xE3o comprovou a recupera\xE7\xE3o do XML autorizado.", "Preserve o XML assinado original e confira a situa\xE7\xE3o fiscal. N\xE3o transmita a nota para tentar recuperar o protocolo."],
|
|
27
34
|
OPERACAO_FISCAL_FALHOU: ["operacao", "O motor n\xE3o conseguiu concluir a etapa fiscal.", "Confira a configura\xE7\xE3o da etapa, os dados do XML e as depend\xEAncias do runtime."],
|
|
28
35
|
TLS_SERVIDOR_INVALIDO: ["transmissao", "A verifica\xE7\xE3o TLS do servi\xE7o fiscal falhou.", "Confira o servi\xE7o e a cadeia de confian\xE7a; mantenha a verifica\xE7\xE3o TLS habilitada."],
|
|
29
36
|
CSC_INVALIDO: ["configuracao", "A configura\xE7\xE3o do CSC \xE9 inv\xE1lida.", "Confira id e valor do CSC sem registrar seu conte\xFAdo."],
|
|
@@ -32,6 +39,7 @@ var CATALOGO = {
|
|
|
32
39
|
RUNTIME_INVALIDO: ["runtime", "O runtime fiscal \xE9 inv\xE1lido.", "Use um runtime distribu\xEDdo e compat\xEDvel com a plataforma."],
|
|
33
40
|
RUNTIME_INCOMPLETO: ["runtime", "Falta um arquivo do runtime fiscal.", "Reinstale o runtime da plataforma."],
|
|
34
41
|
RUNTIME_ALTERADO: ["runtime", "A integridade do runtime fiscal n\xE3o foi confirmada.", "Reinstale o runtime; n\xE3o desative a verifica\xE7\xE3o dos hashes."],
|
|
42
|
+
RUNTIME_SEM_CAPACIDADE: ["runtime", "O runtime instalado n\xE3o oferece esta opera\xE7\xE3o.", "Atualize o pacote principal e o runtime da plataforma para vers\xF5es compat\xEDveis com a opera\xE7\xE3o solicitada."],
|
|
35
43
|
CONTRATO_DIVERGENTE: ["runtime", "Cliente e motor usam contratos diferentes.", "Use vers\xF5es compat\xEDveis ou regenere e recompile ambos."],
|
|
36
44
|
DOCUMENTO_INVALIDO: ["documento", "O documento cont\xE9m dados incompat\xEDveis com o modelo.", "Valide os campos e tipos do documento antes de gerar o XML."],
|
|
37
45
|
OPERACAO_INVALIDA: ["operacao", "Os par\xE2metros da opera\xE7\xE3o s\xE3o inv\xE1lidos.", "Informe um objeto com os campos exigidos pela opera\xE7\xE3o."],
|
|
@@ -58,7 +66,7 @@ var CATALOGO = {
|
|
|
58
66
|
FALHA_LOCAL: ["operacao", "N\xE3o foi poss\xEDvel concluir a opera\xE7\xE3o local.", "Confira a etapa e os tipos dos dados; n\xE3o registre o erro bruto."]
|
|
59
67
|
};
|
|
60
68
|
function etapaDaOperacao(comando) {
|
|
61
|
-
return { gerarXml: "geracao", validar: "validacao", assinar: "assinatura", transmitir: "transmissao", consultar: "consulta", cancelar: "cancelamento", cartaCorrecao: "carta_correcao", inutilizar: "inutilizacao" }[comando] ?? "operacao";
|
|
69
|
+
return { gerarXml: "geracao", validar: "validacao", assinar: "assinatura", transmitir: "transmissao", consultar: "consulta", recuperarXml: "consulta", cancelar: "cancelamento", cartaCorrecao: "carta_correcao", inutilizar: "inutilizacao" }[comando] ?? "operacao";
|
|
62
70
|
}
|
|
63
71
|
function criarDiagnosticoErro(codigo, desconhecido = false, detalhes = {}) {
|
|
64
72
|
const safeCode = Object.hasOwn(CATALOGO, codigo) ? codigo : "FALHA_LOCAL";
|
|
@@ -98,18 +106,30 @@ function diagnosticarResultado(resultado, comando) {
|
|
|
98
106
|
const protocolo = typeof resultado.protocolo === "string" && /^\d{15}$/.test(resultado.protocolo);
|
|
99
107
|
const chave = typeof resultado.chave === "string" && /^\d{44}$/.test(resultado.chave);
|
|
100
108
|
const autorizacao = (comando === "transmitir" || comando === "consultar") && resultado.estado === "AUTORIZADO" && [100, 150].includes(resultado.cStat) && protocolo && chave && (comando === "consultar" || resultado.envioIniciado !== false);
|
|
109
|
+
const xmlRecuperado = comando === "recuperarXml" && base64XmlRecuperadoValido(resultado.xmlBase64);
|
|
110
|
+
const recuperacao = comando === "recuperarXml" && resultado.estado === "XML_RECUPERADO" && resultado.cStat === 100 && protocolo && chave && semEvento && xmlRecuperado;
|
|
101
111
|
const evento = comando === "cancelar" && resultado.estado === "CANCELADO" && [135, 136].includes(resultado.cStat) && chave || comando === "cartaCorrecao" && resultado.estado === "EVENTO_REGISTRADO" && resultado.cStat === 135 && chave || comando === "inutilizar" && resultado.estado === "INUTILIZADO" && resultado.cStat === 102;
|
|
102
112
|
if (local && resultado.sucesso === true && semEvento) {
|
|
103
113
|
statusEnvio = "nao_enviado";
|
|
104
114
|
codigo = "ETAPA_LOCAL_CONCLUIDA";
|
|
105
115
|
mensagem = "A etapa local foi conclu\xEDda; esta opera\xE7\xE3o n\xE3o transmitiu a nota.";
|
|
106
116
|
acao = "Confira o resultado antes de seguir para a pr\xF3xima etapa.";
|
|
117
|
+
} else if (resultado.sucesso === true && recuperacao) {
|
|
118
|
+
statusEnvio = "autorizado";
|
|
119
|
+
codigo = "XML_RECUPERADO";
|
|
120
|
+
mensagem = "O XML autorizado foi recuperado por consulta; esta opera\xE7\xE3o n\xE3o transmitiu a nota.";
|
|
121
|
+
acao = "Guarde o XML recuperado com o protocolo de autoriza\xE7\xE3o.";
|
|
122
|
+
} else if (comando === "recuperarXml" && resultado.sucesso === false && resultado.codigo === "RECUPERACAO_NAO_COMPROVADA" && ["CONSULTADO", "ERRO"].includes(resultado.estado) && semEvento) {
|
|
123
|
+
statusEnvio = "nao_enviado";
|
|
124
|
+
codigo = "RECUPERACAO_NAO_COMPROVADA";
|
|
125
|
+
mensagem = CATALOGO.RECUPERACAO_NAO_COMPROVADA[1];
|
|
126
|
+
acao = CATALOGO.RECUPERACAO_NAO_COMPROVADA[2];
|
|
107
127
|
} else if (resultado.sucesso === true && (autorizacao || evento && protocolo && resultado.envioIniciado !== false)) {
|
|
108
128
|
statusEnvio = "autorizado";
|
|
109
129
|
codigo = "OPERACAO_CONFIRMADA";
|
|
110
130
|
mensagem = { AUTORIZADO: "A nota foi autorizada.", CANCELADO: "O cancelamento foi confirmado.", EVENTO_REGISTRADO: "O evento foi registrado.", INUTILIZADO: "A faixa foi inutilizada." }[resultado.estado];
|
|
111
131
|
acao = "Guarde o resultado fiscal. N\xE3o repita esta opera\xE7\xE3o.";
|
|
112
|
-
} else if (resultado.sucesso === false && (comando === "consultar" || resultado.envioIniciado !== false) && ["transmitir", "consultar", "cancelar", "cartaCorrecao", "inutilizar"].includes(comando) && Number.isInteger(resultado.cStat) && Number(resultado.cStat) > 0 && Number(resultado.cStat) <= 9999 && (resultado.estado === "DENEGADO" && [110, 301, 302].includes(resultado.cStat) || resultado.estado === "REJEITADO" && ![100, 150, 110, 301, 302, 101, 151, 155, 103, 104, 105, 108, 109, 204, 539, 573, 135, 136, 102, 217].includes(resultado.cStat))) {
|
|
132
|
+
} else if (resultado.sucesso === false && (comando === "consultar" || comando === "recuperarXml" || resultado.envioIniciado !== false) && ["transmitir", "consultar", "recuperarXml", "cancelar", "cartaCorrecao", "inutilizar"].includes(comando) && Number.isInteger(resultado.cStat) && Number(resultado.cStat) > 0 && Number(resultado.cStat) <= 9999 && (resultado.estado === "DENEGADO" && [110, 301, 302].includes(resultado.cStat) || resultado.estado === "REJEITADO" && ![100, 150, 110, 301, 302, 101, 151, 155, 103, 104, 105, 108, 109, 204, 539, 573, 135, 136, 102, 217].includes(resultado.cStat))) {
|
|
113
133
|
statusEnvio = "rejeitado";
|
|
114
134
|
codigo = resultado.estado === "DENEGADO" ? "NOTA_DENEGADA" : "OPERACAO_REJEITADA";
|
|
115
135
|
mensagem = resultado.estado === "DENEGADO" ? "A nota foi denegada." : "A opera\xE7\xE3o foi rejeitada.";
|
|
@@ -152,7 +172,7 @@ var ErroAcbrNode = class extends Error {
|
|
|
152
172
|
};
|
|
153
173
|
|
|
154
174
|
// src/runtime.ts
|
|
155
|
-
var STATES = /* @__PURE__ */ new Set(["DENEGADO", "XML_GERADO", "VALIDADO", "ASSINADO", "AUTORIZADO", "REJEITADO", "PENDENTE", "INDETERMINADO", "CONSULTADO", "CANCELADO", "EVENTO_REGISTRADO", "INUTILIZADO", "ERRO"]);
|
|
175
|
+
var STATES = /* @__PURE__ */ new Set(["DENEGADO", "XML_GERADO", "VALIDADO", "ASSINADO", "XML_RECUPERADO", "AUTORIZADO", "REJEITADO", "PENDENTE", "INDETERMINADO", "CONSULTADO", "CANCELADO", "EVENTO_REGISTRADO", "INUTILIZADO", "ERRO"]);
|
|
156
176
|
var UFS = new Set("AC AL AP AM BA CE DF ES GO MA MT MS MG PA PB PR PE PI RJ RN RS RO RR SC SP SE TO".split(" "));
|
|
157
177
|
var MAX_BYTES = 8 * 1024 * 1024;
|
|
158
178
|
var MAX_TEARDOWN_MS = 1e3;
|
|
@@ -184,6 +204,7 @@ async function resolveRuntime(directory, contract) {
|
|
|
184
204
|
throw fail("RUNTIME_AUSENTE", "Manifesto do runtime n\xE3o encontrado.");
|
|
185
205
|
}
|
|
186
206
|
if (!validContract(m) || m.formatVersion !== 1 || !plain(m.files) || typeof m.executable !== "string" || m.platform !== process.platform || m.arch !== process.arch) throw fail("RUNTIME_INVALIDO", "Manifesto ou plataforma do runtime inv\xE1lidos.");
|
|
207
|
+
if (m.capabilities !== void 0 && (!Array.isArray(m.capabilities) || !m.capabilities.every((value) => typeof value === "string" && /^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(value)))) throw fail("RUNTIME_INVALIDO", "Capacidades do runtime inv\xE1lidas.");
|
|
187
208
|
if (m.sourceRevision !== contract.sourceRevision || m.contractHash !== contract.contractHash) throw fail("CONTRATO_DIVERGENTE", "Cliente e motor foram gerados de contratos diferentes; regenere e recompile.");
|
|
188
209
|
for (const [name, hash] of Object.entries(m.files)) {
|
|
189
210
|
if (typeof hash !== "string" || !/^[a-f0-9]{64}$/.test(hash)) throw fail("RUNTIME_INVALIDO", "Hash inv\xE1lido no runtime.");
|
|
@@ -198,7 +219,7 @@ async function resolveRuntime(directory, contract) {
|
|
|
198
219
|
if (createHash("sha256").update(data).digest("hex") !== hash) throw fail("RUNTIME_ALTERADO", "O runtime n\xE3o corresponde ao manifesto distribu\xEDdo.");
|
|
199
220
|
}
|
|
200
221
|
if (!m.files[m.executable]) throw fail("RUNTIME_INVALIDO", "O execut\xE1vel n\xE3o consta no invent\xE1rio.");
|
|
201
|
-
return { directory: root, executable: confined(root, m.executable) };
|
|
222
|
+
return { directory: root, executable: confined(root, m.executable), capabilities: new Set(m.capabilities ?? []) };
|
|
202
223
|
}
|
|
203
224
|
function criarEmissor(options) {
|
|
204
225
|
const configFail = (campo) => fail("CONFIGURACAO_INVALIDA", "Informe campos e tipos v\xE1lidos na configura\xE7\xE3o.", void 0, false, { etapa: "configuracao", campo });
|
|
@@ -215,7 +236,7 @@ function criarEmissor(options) {
|
|
|
215
236
|
if (options.validaDocumento !== void 0 && typeof options.validaDocumento !== "function" || options.criaProcesso !== void 0 && typeof options.criaProcesso !== "function") throw configFail();
|
|
216
237
|
const cfg = { cnpj: options.cnpj, uf: options.uf, modelo: options.modelo, ambiente: options.ambiente, csc: options.csc ? { ...options.csc } : void 0 };
|
|
217
238
|
const certificate = cert ? { ...cert, pfx: cert.pfx ? Buffer.from(cert.pfx) : void 0 } : void 0;
|
|
218
|
-
const contract = {
|
|
239
|
+
const contract = { sourceRevision: options.contrato.sourceRevision, contractHash: options.contrato.contractHash };
|
|
219
240
|
const timeout = options.timeoutMs ?? 12e4, capacity = options.limiteFila ?? 32, bytesLimit = options.limiteBytes ?? MAX_BYTES;
|
|
220
241
|
if (!Number.isInteger(timeout) || timeout < 1 || timeout > 12e4) throw configFail("timeoutMs");
|
|
221
242
|
if (!Number.isInteger(capacity) || capacity < 0 || capacity > 1e3) throw configFail("limiteFila");
|
|
@@ -238,6 +259,7 @@ function criarEmissor(options) {
|
|
|
238
259
|
async function execute(item) {
|
|
239
260
|
if (closed || item.signal?.aborted) throw fail("OPERACAO_CANCELADA", "Opera\xE7\xE3o cancelada antes da execu\xE7\xE3o.");
|
|
240
261
|
const runtime = await (runtimePromise ??= resolveRuntime(options.runtimeDirectory, contract));
|
|
262
|
+
if (item.req.comando === "recuperarXml" && !runtime.capabilities.has("recuperarXml")) throw fail("RUNTIME_SEM_CAPACIDADE", "O runtime n\xE3o oferece recupera\xE7\xE3o de XML; atualize o pacote e o runtime compat\xEDvel.", item.req, false, { etapa: "runtime" });
|
|
241
263
|
if (closed || item.signal?.aborted) throw fail("OPERACAO_CANCELADA", "Opera\xE7\xE3o cancelada antes da execu\xE7\xE3o.");
|
|
242
264
|
return await new Promise((done, rejectExecution) => {
|
|
243
265
|
let stopError;
|
|
@@ -360,9 +382,11 @@ function criarEmissor(options) {
|
|
|
360
382
|
continue;
|
|
361
383
|
}
|
|
362
384
|
if (result || frame.versao !== 1 || frame.sourceRevision !== contract.sourceRevision || frame.contractHash !== contract.contractHash || typeof frame.sucesso !== "boolean" || !STATES.has(String(frame.estado))) throw Error();
|
|
363
|
-
if (frame.xmlBase64 !== void 0 && (typeof frame.xmlBase64 !== "string" || !frame.xmlBase64.length || Buffer.from(frame.xmlBase64, "base64").toString("base64") !== frame.xmlBase64)) throw Error();
|
|
385
|
+
if (frame.xmlBase64 !== void 0 && (item.req.comando === "recuperarXml" ? !base64XmlRecuperadoValido(frame.xmlBase64) : typeof frame.xmlBase64 !== "string" || !frame.xmlBase64.length || Buffer.from(frame.xmlBase64, "base64").toString("base64") !== frame.xmlBase64)) throw Error();
|
|
364
386
|
if (frame.cStat !== void 0 && (!Number.isInteger(frame.cStat) || Number(frame.cStat) < 1 || Number(frame.cStat) > 9999)) throw Error();
|
|
365
387
|
if (frame.envioIniciado !== void 0 && typeof frame.envioIniciado !== "boolean" || sending && frame.envioIniciado === false) throw Error();
|
|
388
|
+
if (frame.estado === "XML_RECUPERADO" && item.req.comando !== "recuperarXml") throw Error();
|
|
389
|
+
if (item.req.comando === "recuperarXml" && (frame.sucesso === true || frame.estado === "XML_RECUPERADO") && (frame.sucesso !== true || frame.estado !== "XML_RECUPERADO" || frame.cStat !== 100 || typeof frame.chave !== "string" || !/^\d{44}$/.test(frame.chave) || typeof frame.protocolo !== "string" || !/^\d{15}$/.test(frame.protocolo) || typeof frame.xmlBase64 !== "string" || frame.envioIniciado === true || sending)) throw Error();
|
|
366
390
|
const fiscal = frame;
|
|
367
391
|
result = { ...fiscal, diagnostico: diagnosticarResultado(sending ? { ...fiscal, envioIniciado: true } : fiscal, item.req.comando) };
|
|
368
392
|
} catch {
|
|
@@ -412,7 +436,7 @@ function criarEmissor(options) {
|
|
|
412
436
|
if (signal !== void 0 && (!plain(signal) || typeof signal.aborted !== "boolean" || typeof signal.addEventListener !== "function" || typeof signal.removeEventListener !== "function")) throw fail("OPERACAO_INVALIDA", "Informe op\xE7\xF5es v\xE1lidas para a opera\xE7\xE3o.", void 0, false, { etapa: etapaDaOperacao(command), campo: "signal" });
|
|
413
437
|
if (closed) throw closedError();
|
|
414
438
|
if (operation.signal?.aborted) throw fail("OPERACAO_CANCELADA", "Opera\xE7\xE3o cancelada antes da execu\xE7\xE3o.");
|
|
415
|
-
if (["assinar", "transmitir", "consultar", "cancelar", "cartaCorrecao", "inutilizar"].includes(command) && !certificate) throw fail("CERTIFICADO_AUSENTE", "A opera\xE7\xE3o exige um certificado A1.");
|
|
439
|
+
if (["assinar", "transmitir", "consultar", "recuperarXml", "cancelar", "cartaCorrecao", "inutilizar"].includes(command) && !certificate) throw fail("CERTIFICADO_AUSENTE", "A opera\xE7\xE3o exige um certificado A1.", void 0, false, { etapa: etapaDaOperacao(command) });
|
|
416
440
|
if (command === "gerarXml") options.validaDocumento?.(data.documento);
|
|
417
441
|
const config = { ...cfg };
|
|
418
442
|
if (certificate) {
|
|
@@ -427,7 +451,7 @@ function criarEmissor(options) {
|
|
|
427
451
|
}
|
|
428
452
|
let bytes, req;
|
|
429
453
|
try {
|
|
430
|
-
bytes = Buffer.from(JSON.stringify({ versao: 1, id: randomUUID(),
|
|
454
|
+
bytes = Buffer.from(JSON.stringify({ ...data, ...contract, versao: 1, id: randomUUID(), comando: command, config }) + "\n", "utf8");
|
|
431
455
|
if (bytes.length > bytesLimit) throw Error();
|
|
432
456
|
req = JSON.parse(bytes.toString("utf8"));
|
|
433
457
|
} catch {
|
|
@@ -478,25 +502,26 @@ function criarEmissor(options) {
|
|
|
478
502
|
validar: (xml, op) => request("validar", xmlData(xml), op),
|
|
479
503
|
assinar: (xml, op) => request("assinar", xmlData(xml), op),
|
|
480
504
|
transmitir: (xml, op) => request("transmitir", xmlData(xml), op),
|
|
505
|
+
recuperarXml: (xmlAssinado, opcoes) => request("recuperarXml", xmlData(xmlAssinado), opcoes),
|
|
481
506
|
consultar: (consulta, op) => {
|
|
482
507
|
checkObject(consulta, "consulta");
|
|
483
508
|
checkKey(consulta.chave);
|
|
484
509
|
if (consulta.recibo !== void 0 && (typeof consulta.recibo !== "string" || !/^\d{15}$/.test(consulta.recibo))) throw fail("RECIBO_INVALIDO", "Recibo inv\xE1lido.", void 0, false, { campo: "recibo" });
|
|
485
|
-
return request("consultar", consulta, op);
|
|
510
|
+
return request("consultar", { chave: consulta.chave, ...consulta.recibo !== void 0 ? { recibo: consulta.recibo } : {} }, op);
|
|
486
511
|
},
|
|
487
512
|
cancelar: (data, op) => {
|
|
488
513
|
checkObject(data, "cancelamento");
|
|
489
514
|
checkKey(data.chave);
|
|
490
515
|
reason(data.justificativa);
|
|
491
516
|
if (typeof data.protocolo !== "string" || !/^\d{15}$/.test(data.protocolo)) throw fail("PROTOCOLO_INVALIDO", "Protocolo inv\xE1lido.", void 0, false, { campo: "protocolo" });
|
|
492
|
-
return request("cancelar", data, op);
|
|
517
|
+
return request("cancelar", { chave: data.chave, protocolo: data.protocolo, justificativa: data.justificativa }, op);
|
|
493
518
|
},
|
|
494
519
|
cartaCorrecao: (data, op) => {
|
|
495
520
|
checkObject(data, "cartaCorrecao");
|
|
496
521
|
if (cfg.modelo !== 55) throw fail("OPERACAO_NAO_SUPORTADA", "Carta de corre\xE7\xE3o exige NF-e modelo 55.");
|
|
497
522
|
checkKey(data.chave);
|
|
498
523
|
if (typeof data.correcao !== "string" || data.correcao.length < 15 || data.correcao.length > 1e3 || !Number.isInteger(data.sequenciaEvento ?? 1) || (data.sequenciaEvento ?? 1) < 1 || (data.sequenciaEvento ?? 1) > 20) throw fail("CORRECAO_INVALIDA", "Dados da carta de corre\xE7\xE3o inv\xE1lidos.", void 0, false, { campo: "cartaCorrecao" });
|
|
499
|
-
return request("cartaCorrecao", {
|
|
524
|
+
return request("cartaCorrecao", { chave: data.chave, correcao: data.correcao, sequenciaEvento: data.sequenciaEvento ?? 1 }, op);
|
|
500
525
|
},
|
|
501
526
|
inutilizar: (data, op) => {
|
|
502
527
|
checkObject(data, "inutilizacao");
|