node-sped-nfe 1.0.2 → 1.0.4

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/README.md CHANGED
@@ -44,6 +44,40 @@ Para garantir o funcionamento correto da biblioteca, certifique-se de que os seg
44
44
 
45
45
  ---
46
46
 
47
+ ## 🧪 Estados Testados
48
+
49
+ A biblioteca já foi testada com sucesso nos seguintes estados brasileiros:
50
+
51
+ - ✅ MT — Mato Grosso
52
+ - ⬜ AC — Acre
53
+ - ⬜ AL — Alagoas
54
+ - ⬜ AM — Amazonas
55
+ - ⬜ AP — Amapá
56
+ - ⬜ BA — Bahia
57
+ - ⬜ CE — Ceará
58
+ - ⬜ DF — Distrito Federal
59
+ - ⬜ ES — Espírito Santo
60
+ - ⬜ GO — Goiás
61
+ - ⬜ MA — Maranhão
62
+ - ⬜ MG — Minas Gerais
63
+ - ⬜ MS — Mato Grosso do Sul
64
+ - ⬜ PA — Pará
65
+ - ⬜ PB — Paraíba
66
+ - ⬜ PE — Pernambuco
67
+ - ⬜ PI — Piauí
68
+ - ⬜ PR — Paraná
69
+ - ⬜ RJ — Rio de Janeiro
70
+ - ⬜ RN — Rio Grande do Norte
71
+ - ⬜ RO — Rondônia
72
+ - ⬜ RR — Roraima
73
+ - ⬜ RS — Rio Grande do Sul
74
+ - ⬜ SC — Santa Catarina
75
+ - ⬜ SE — Sergipe
76
+ - ⬜ SP — São Paulo
77
+ - ⬜ TO — Tocantins
78
+
79
+ Contribua com a comunidade! Testou com sucesso em algum estado? Envie um PR ou abra uma issue!
80
+
47
81
  ## 🚀 Começando
48
82
 
49
83
  ### 1. Instalação
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { Tools } from "./utils/tools";
2
- import { Make } from "./utils/make";
1
+ import { Tools } from "./utils/tools.js";
2
+ import { Make } from "./utils/make.js";
3
3
  export { Tools, Make };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { Tools } from "./utils/tools";
2
- import { Make } from "./utils/make";
1
+ import { Tools } from "./utils/tools.js";
2
+ import { Make } from "./utils/make.js";
3
3
  export { Tools, Make };
@@ -0,0 +1 @@
1
+ {"root":["../src/index.ts","../src/utils/eventos.ts","../src/utils/make.ts","../src/utils/tools.ts"],"version":"5.8.3"}
@@ -9,12 +9,9 @@ declare class Tools {
9
9
  pfx: string;
10
10
  senha: string;
11
11
  });
12
- sefazEnviaLote(xml: string, data?: {
13
- idLote: number;
14
- indSinc: number;
15
- compactar: boolean;
16
- }): Promise<unknown>;
17
- xmlSign(xmlJSON: string, data: any): Promise<unknown>;
12
+ teste(): Promise<object>;
13
+ sefazEnviaLote(xml: string, data?: any): Promise<string>;
14
+ xmlSign(xmlJSON: string, data?: any): Promise<string>;
18
15
  xml2json(xml: string): Promise<object>;
19
16
  json2xml(obj: object): Promise<string>;
20
17
  getCertificado(): Promise<unknown>;
@@ -18,6 +18,10 @@ import tmp from "tmp";
18
18
  import crypto from "crypto";
19
19
  import { urlServicos } from "./eventos.js";
20
20
  import fs from "fs";
21
+ import path from 'path';
22
+ import { fileURLToPath } from 'url';
23
+ const __filename = fileURLToPath(import.meta.url);
24
+ const __dirname = path.dirname(__filename);
21
25
  class Tools {
22
26
  constructor(config = { mod: "", xmllint: 'xmllint', cUF: '51', tpAmb: 2 }, certificado = { pfx: "", senha: "" }) {
23
27
  _Tools_instances.add(this);
@@ -26,7 +30,11 @@ class Tools {
26
30
  XMLBuilder: {},
27
31
  XMLParser: {}
28
32
  });
29
- _Tools_pem.set(this, {});
33
+ _Tools_pem.set(this, {
34
+ key: "", // A chave privada extraída do PKCS#12, em formato PEM
35
+ cert: "", // O certificado extraído, em formato PEM
36
+ ca: [] // Uma lista de certificados da cadeia (se houver), ou null
37
+ });
30
38
  _Tools_config.set(this, void 0);
31
39
  //Configurar certificado
32
40
  __classPrivateFieldSet(this, _Tools_config, config, "f");
@@ -41,6 +49,9 @@ class Tools {
41
49
  parseTagValue: false, // Evita conversão automática de valores
42
50
  });
43
51
  }
52
+ async teste() {
53
+ return await __classPrivateFieldGet(this, _Tools_instances, "m", _Tools_certTools).call(this);
54
+ }
44
55
  sefazEnviaLote(xml, data = { idLote: 1, indSinc: 0, compactar: false }) {
45
56
  return new Promise(async (resvol, reject) => {
46
57
  if (typeof data.idLote == "undefined")
@@ -104,13 +115,13 @@ class Tools {
104
115
  }
105
116
  });
106
117
  }
107
- async xmlSign(xmlJSON, data) {
118
+ async xmlSign(xmlJSON, data = { tag: "infNFe" }) {
108
119
  return new Promise(async (resvol, reject) => {
109
120
  if (data.tag === undefined)
110
121
  data.tag = "infNFe";
111
122
  //Obter a tag que ira ser assinada
112
123
  let xml = await this.xml2json(xmlJSON);
113
- let pem = await this.getCertificado();
124
+ let tempPem = await __classPrivateFieldGet(this, _Tools_instances, "m", _Tools_certTools).call(this);
114
125
  const sign = crypto.createSign('RSA-SHA1'); // Correção: Alterado para RSA-SHA1
115
126
  let signedInfo = {
116
127
  "SignedInfo": {
@@ -145,10 +156,10 @@ class Tools {
145
156
  xml.NFe.Signature = {
146
157
  ...signedInfo,
147
158
  ...{
148
- "SignatureValue": sign.sign(pem.key, 'base64'),
159
+ "SignatureValue": sign.sign(tempPem.key, 'base64'),
149
160
  "KeyInfo": {
150
161
  "X509Data": {
151
- "X509Certificate": pem.cert.replace(/-----BEGIN CERTIFICATE-----/g, '').replace(/-----END CERTIFICATE-----/g, '').replace(/\r\n/g, '')
162
+ "X509Certificate": tempPem.cert.replace(/-----BEGIN CERTIFICATE-----/g, '').replace(/-----END CERTIFICATE-----/g, '').replace(/\r\n/g, '')
152
163
  }
153
164
  },
154
165
  "@xmlns": "http://www.w3.org/2000/09/xmldsig#"
@@ -246,7 +257,8 @@ _Tools_cert = new WeakMap(), _Tools_xmlTools = new WeakMap(), _Tools_pem = new W
246
257
  async function _Tools_xmlValido(xml) {
247
258
  const xmlFile = tmp.fileSync({ mode: 0o644, prefix: 'xml-', postfix: '.xml' });
248
259
  fs.writeFileSync(xmlFile.name, xml, { encoding: 'utf8' });
249
- const verif = spawnSync(__classPrivateFieldGet(this, _Tools_config, "f").xmllint, ['--noout', '--schema', './utils/schemas/nfe_v4.00.xsd', xmlFile.name], { encoding: 'utf8' });
260
+ const schemaPath = path.resolve(__dirname, '../../schemas/nfe_v4.00.xsd');
261
+ const verif = spawnSync(__classPrivateFieldGet(this, _Tools_config, "f").xmllint, ['--noout', '--schema', schemaPath, xmlFile.name], { encoding: 'utf8' });
250
262
  xmlFile.removeCallback();
251
263
  // Aqui, usamos o operador de encadeamento opcional (?.)
252
264
  if (verif.error) {
@@ -258,11 +270,13 @@ async function _Tools_xmlValido(xml) {
258
270
  return 1;
259
271
  }, _Tools_certTools = function _Tools_certTools() {
260
272
  return new Promise(async (resvol, reject) => {
261
- if (__classPrivateFieldGet(this, _Tools_pem, "f") != null)
273
+ if (__classPrivateFieldGet(this, _Tools_pem, "f").key != "")
262
274
  resvol(__classPrivateFieldGet(this, _Tools_pem, "f"));
263
275
  pem.readPkcs12(__classPrivateFieldGet(this, _Tools_cert, "f").pfx, { p12Password: __classPrivateFieldGet(this, _Tools_cert, "f").senha }, (err, myPem) => {
276
+ if (err)
277
+ return reject(err); // <-- importante!
264
278
  __classPrivateFieldSet(this, _Tools_pem, myPem, "f");
265
- resvol(myPem);
279
+ resvol(__classPrivateFieldGet(this, _Tools_pem, "f"));
266
280
  });
267
281
  });
268
282
  };
package/package.json CHANGED
@@ -1,10 +1,21 @@
1
1
  {
2
2
  "name": "node-sped-nfe",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "author": "Kalmon V. Tavares",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
+ "keywords": [
9
+ "nota fiscal",
10
+ "xml",
11
+ "sefaz",
12
+ "nfe",
13
+ "sped",
14
+ "documento fiscal",
15
+ "danfe",
16
+ "nfce",
17
+ "pdf"
18
+ ],
8
19
  "exports": {
9
20
  ".": {
10
21
  "import": "./dist/index.js",
@@ -13,11 +24,13 @@
13
24
  }
14
25
  },
15
26
  "scripts": {
16
- "build": "tsc"
27
+ "build": "tsc > saida.txt",
28
+ "test:nfe": "node testes/nfe.js"
17
29
  },
18
30
  "devDependencies": {
19
31
  "@types/axios": "^0.9.36",
20
32
  "@types/node": "^22.14.0",
33
+ "@types/node-forge": "^1.3.11",
21
34
  "@types/pem": "^1.14.4",
22
35
  "@types/tmp": "^0.2.6",
23
36
  "typescript": "^5.0.0"
@@ -25,6 +38,7 @@
25
38
  "dependencies": {
26
39
  "crypto": "^1.0.1",
27
40
  "fast-xml-parser": "^4.5.1",
41
+ "node-forge": "^1.3.1",
28
42
  "pem": "^1.14.8",
29
43
  "tmp": "^0.2.3",
30
44
  "xml-crypto": "^6.0.0"
@@ -33,4 +47,4 @@
33
47
  "type": "git",
34
48
  "url": "https://github.com/kalmonv/node-sped-nfe.git"
35
49
  }
36
- }
50
+ }
package/saida.txt CHANGED
@@ -1,4 +0,0 @@
1
-
2
- > node-sped-nfe@1.0.0 build
3
- > tsc
4
-
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns="http://www.portalfiscal.inf.br/nfe" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.portalfiscal.inf.br/nfe" elementFormDefault="qualified" attributeFormDefault="unqualified">
3
+ <xs:include schemaLocation="leiauteNFe_v4.00.xsd"/>
4
+ <xs:element name="consReciNFe" type="TConsReciNFe">
5
+ <xs:annotation>
6
+ <xs:documentation>Schema XML de validação do Pedido de Consulta do Recido do Lote de Notas Fiscais Eletrônicas</xs:documentation>
7
+ </xs:annotation>
8
+ </xs:element>
9
+ </xs:schema>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.portalfiscal.inf.br/nfe" targetNamespace="http://www.portalfiscal.inf.br/nfe" elementFormDefault="qualified" attributeFormDefault="unqualified">
3
+ <xs:include schemaLocation="leiauteConsSitNFe_v4.00.xsd"/>
4
+ <xs:element name="consSitNFe" type="TConsSitNFe">
5
+ <xs:annotation>
6
+ <xs:documentation>Schema de validação XML dp Pedido de Consulta da Situação Atual da Nota Fiscal Eletrônica</xs:documentation>
7
+ </xs:annotation>
8
+ </xs:element>
9
+ </xs:schema>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns="http://www.portalfiscal.inf.br/nfe" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.portalfiscal.inf.br/nfe" elementFormDefault="qualified" attributeFormDefault="unqualified">
3
+ <xs:include schemaLocation="leiauteConsStatServ_v4.00.xsd"/>
4
+ <xs:element name="consStatServ" type="TConsStatServ">
5
+ <xs:annotation>
6
+ <xs:documentation>Schema XML de validação do Pedido de Consulta do Status do Serviço</xs:documentation>
7
+ </xs:annotation>
8
+ </xs:element>
9
+ </xs:schema>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.portalfiscal.inf.br/nfe" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" targetNamespace="http://www.portalfiscal.inf.br/nfe" elementFormDefault="qualified" attributeFormDefault="unqualified">
3
+ <xs:include schemaLocation="leiauteNFe_v4.00.xsd"/>
4
+ <xs:element name="enviNFe" type="TEnviNFe">
5
+ <xs:annotation>
6
+ <xs:documentation>Schema XML de validação do Pedido de Concessão de Autorização da Nota Fiscal Eletrônica</xs:documentation>
7
+ </xs:annotation>
8
+ </xs:element>
9
+ </xs:schema>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.portalfiscal.inf.br/nfe" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" targetNamespace="http://www.portalfiscal.inf.br/nfe" elementFormDefault="qualified" attributeFormDefault="unqualified">
3
+ <xs:include schemaLocation="leiauteInutNFe_v4.00.xsd"/>
4
+ <xs:element name="inutNFe" type="TInutNFe">
5
+ <xs:annotation>
6
+ <xs:documentation>Schema XML de validação do Pedido de Inutilização de Numeração da Nota Fiscal Eletrônica</xs:documentation>
7
+ </xs:annotation>
8
+ </xs:element>
9
+ </xs:schema>