node-easywechat 3.5.17 → 3.5.19

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 CHANGED
@@ -1,6 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
3
 
4
+ ## v3.5.19 (2023-12-26)
5
+
6
+ - Fix: 工具包对外暴露PublicKey和PrivateKey
7
+ - Fix: 调整微信支付敏感信息加密机默认哈希算法 (#65)
8
+
9
+ ## v3.5.18 (2023-12-26)
10
+
11
+ - Fix: 修复微信支付上传图片时报错的问题 (#64)
12
+
4
13
  ## v3.5.17 (2023-12-20)
5
14
 
6
15
  - Fix: 修复TS问题 (#62)
@@ -1,7 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ /// <reference types="node" />
3
5
  import Crypto from 'crypto';
4
6
  import Stream from 'stream';
7
+ import Fs from 'fs';
5
8
  export declare const createHash: (str: Crypto.BinaryLike, type?: string, target?: Crypto.BinaryToTextEncoding) => any;
6
9
  export declare const createHmac: (str: Crypto.BinaryLike, key: Crypto.BinaryLike, type?: string, target?: Crypto.BinaryToTextEncoding) => any;
7
10
  /**
@@ -102,3 +105,13 @@ export declare const buildXml: (data: Record<string, any>, rootName?: string) =>
102
105
  * @returns
103
106
  */
104
107
  export declare const createUserAgent: (appends?: string[]) => string;
108
+ /**
109
+ * 流转Buffer
110
+ * @param stream 可读流
111
+ */
112
+ export declare const streamToBuffer: (stream: Fs.ReadStream) => Promise<Buffer>;
113
+ /**
114
+ * Buffer转流
115
+ * @param buffer Buffer对象
116
+ */
117
+ export declare const bufferToStream: (buffer: Buffer) => Stream.Duplex;
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.createUserAgent = exports.buildXml = exports.parseXml = exports.singleItem = exports.strSnake = exports.strCamel = exports.strStudly = exports.strLcwords = exports.strUcwords = exports.rtrim = exports.ltrim = exports.trim = exports.applyMixins = exports.inArray = exports.isIp = exports.isIpv6 = exports.isIpv4 = exports.isFunction = exports.isObject = exports.isNumber = exports.isArray = exports.isString = exports.makeSignature = exports.randomString = exports.parseQueryString = exports.buildQueryString = exports.getTimestamp = exports.createFileHash = exports.createHmac = exports.createHash = void 0;
15
+ exports.bufferToStream = exports.streamToBuffer = exports.createUserAgent = exports.buildXml = exports.parseXml = exports.singleItem = exports.strSnake = exports.strCamel = exports.strStudly = exports.strLcwords = exports.strUcwords = exports.rtrim = exports.ltrim = exports.trim = exports.applyMixins = exports.inArray = exports.isIp = exports.isIpv6 = exports.isIpv4 = exports.isFunction = exports.isObject = exports.isNumber = exports.isArray = exports.isString = exports.makeSignature = exports.randomString = exports.parseQueryString = exports.buildQueryString = exports.getTimestamp = exports.createFileHash = exports.createHmac = exports.createHash = void 0;
16
16
  const crypto_1 = __importDefault(require("crypto"));
17
17
  const qs_1 = __importDefault(require("qs"));
18
18
  const xml2js_1 = __importDefault(require("xml2js"));
@@ -337,3 +337,27 @@ const createUserAgent = function (appends = []) {
337
337
  return values.join(' ');
338
338
  };
339
339
  exports.createUserAgent = createUserAgent;
340
+ /**
341
+ * 流转Buffer
342
+ * @param stream 可读流
343
+ */
344
+ const streamToBuffer = function (stream) {
345
+ return new Promise((resolve, reject) => {
346
+ let buffers = [];
347
+ stream.on('error', reject);
348
+ stream.on('data', data => buffers.push(data));
349
+ stream.on('end', () => resolve(Buffer.concat(buffers)));
350
+ });
351
+ };
352
+ exports.streamToBuffer = streamToBuffer;
353
+ /**
354
+ * Buffer转流
355
+ * @param buffer Buffer对象
356
+ */
357
+ const bufferToStream = function (buffer) {
358
+ let stream = new stream_1.default.Duplex();
359
+ stream.push(buffer);
360
+ stream.push(null);
361
+ return stream;
362
+ };
363
+ exports.bufferToStream = bufferToStream;
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import fs from 'fs';
3
4
  import { Method, AxiosRequestConfig, AxiosInstance } from "axios";
4
5
  import HttpClientInterface from "../Core/HttpClient/Contracts/HttpClientInterface";
@@ -22,12 +23,12 @@ declare class Client implements HttpClientInterface {
22
23
  * 文件上传
23
24
  * @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter2_1_1.shtml
24
25
  * @param uri 接口地址
25
- * @param file 文件路径或文件可读流
26
+ * @param file 文件路径、文件Buffer或文件可读流
26
27
  * @param meta 文件元信息,包含 filename 和 sha256 两个字段
27
28
  * @param filename 文件名,必须以 .jpg、.bmp、.png 为后缀
28
29
  * @returns
29
30
  */
30
- uploadMedia(uri: string, file: string | fs.ReadStream, meta?: Record<string, any>, filename?: string): Promise<HttpClientResponse>;
31
+ uploadMedia(uri: string, file: string | fs.ReadStream | Buffer, meta?: Record<string, any>, filename?: string): Promise<HttpClientResponse>;
31
32
  /**
32
33
  * 判断是否是V3请求
33
34
  * @param url 请求地址
@@ -41,7 +42,7 @@ declare class Client implements HttpClientInterface {
41
42
  * @param payload 请求载荷
42
43
  * @returns
43
44
  */
44
- protected createSignature(method: string, url: string, payload: AxiosRequestConfig<any>): string;
45
+ createSignature(method: string, url: string, payload: AxiosRequestConfig<any>): string;
45
46
  /**
46
47
  * 创建签名(V2)
47
48
  * @param body 请求参数
@@ -61,7 +61,7 @@ class Client {
61
61
  if (!payload.headers['user-agent'] && !payload.headers['User-Agent']) {
62
62
  payload.headers['user-agent'] = (0, Utils_1.createUserAgent)();
63
63
  }
64
- if (this.isV3Request(url)) {
64
+ if (this.isV3Request(url) && !payload.headers['authorization']) {
65
65
  payload.headers['authorization'] = this.createSignature(method, url, payload);
66
66
  }
67
67
  else {
@@ -95,7 +95,7 @@ class Client {
95
95
  * 文件上传
96
96
  * @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter2_1_1.shtml
97
97
  * @param uri 接口地址
98
- * @param file 文件路径或文件可读流
98
+ * @param file 文件路径、文件Buffer或文件可读流
99
99
  * @param meta 文件元信息,包含 filename 和 sha256 两个字段
100
100
  * @param filename 文件名,必须以 .jpg、.bmp、.png 为后缀
101
101
  * @returns
@@ -103,17 +103,21 @@ class Client {
103
103
  uploadMedia(uri, file, meta = null, filename = null) {
104
104
  return __awaiter(this, void 0, void 0, function* () {
105
105
  if (typeof file === 'string') {
106
- file = fs_1.default.createReadStream(file);
106
+ file = fs_1.default.readFileSync(file);
107
107
  }
108
+ else if (typeof file !== 'string' && !Buffer.isBuffer(file)) {
109
+ file = yield (0, Utils_1.streamToBuffer)(file);
110
+ }
111
+ filename = filename !== null && filename !== void 0 ? filename : 'file.jpg';
108
112
  if (!meta) {
109
113
  meta = {
110
- filename: filename !== null && filename !== void 0 ? filename : 'file.jpg',
111
- sha256: yield (0, Utils_1.createFileHash)(file, 'sha256'),
114
+ filename: filename,
115
+ sha256: yield (0, Utils_1.createHash)(file, 'sha256'),
112
116
  };
113
117
  }
114
118
  let metaJson = JSON.stringify(meta);
115
119
  let formData = new form_data_1.default();
116
- formData.append('file', file);
120
+ formData.append('file', file, filename);
117
121
  formData.append('meta', metaJson, {
118
122
  contentType: 'application/json',
119
123
  });
@@ -1,6 +1,11 @@
1
+ /// <reference types="node" />
1
2
  import { PrivateKey } from "../Core/Support/PrivateKey";
2
3
  import { PublicKey } from "../Core/Support/PublicKey";
3
4
  import RSA from "../Core/Support/RSA";
5
+ /**
6
+ * 敏感信息加解密
7
+ * @see https://pay.weixin.qq.com/docs/merchant/development/interface-rules/sensitive-data-encryption.html
8
+ */
4
9
  declare class Encryptor extends RSA {
5
10
  protected publicCert: PublicKey;
6
11
  protected privateCert: PrivateKey;
@@ -14,5 +19,23 @@ declare class Encryptor extends RSA {
14
19
  * 获取公钥的序列号
15
20
  */
16
21
  getSerialNo(): string;
22
+ /**
23
+ * 加密
24
+ * @param plaintext 待加密文本
25
+ * @param encoding 编码,默认:'base64'
26
+ * @param hashType 哈希方式,默认:'sha1'
27
+ * @param padding 补位方式,默认:crypto.constants.RSA_PKCS1_OAEP_PADDING
28
+ * @returns
29
+ */
30
+ encrypt(plaintext: string, encoding?: BufferEncoding, hashType?: string, padding?: number): string;
31
+ /**
32
+ * 解密
33
+ * @param ciphertext 待解密文本
34
+ * @param encoding 编码,默认:'base64'
35
+ * @param hashType 哈希方式,默认:'sha1'
36
+ * @param padding 补位方式,默认:crypto.constants.RSA_PKCS1_OAEP_PADDING
37
+ * @returns
38
+ */
39
+ decrypt(ciphertext: string, encoding?: BufferEncoding, hashType?: string, padding?: number): string;
17
40
  }
18
41
  export = Encryptor;
@@ -3,6 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  const RSA_1 = __importDefault(require("../Core/Support/RSA"));
6
+ const crypto_1 = __importDefault(require("crypto"));
7
+ /**
8
+ * 敏感信息加解密
9
+ * @see https://pay.weixin.qq.com/docs/merchant/development/interface-rules/sensitive-data-encryption.html
10
+ */
6
11
  class Encryptor extends RSA_1.default {
7
12
  /**
8
13
  * 设置加密机所需的公私密钥
@@ -21,5 +26,27 @@ class Encryptor extends RSA_1.default {
21
26
  getSerialNo() {
22
27
  return this.publicCert.getSerialNo();
23
28
  }
29
+ /**
30
+ * 加密
31
+ * @param plaintext 待加密文本
32
+ * @param encoding 编码,默认:'base64'
33
+ * @param hashType 哈希方式,默认:'sha1'
34
+ * @param padding 补位方式,默认:crypto.constants.RSA_PKCS1_OAEP_PADDING
35
+ * @returns
36
+ */
37
+ encrypt(plaintext, encoding = 'base64', hashType = 'sha1', padding = crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING) {
38
+ return super.encrypt(plaintext, encoding, hashType, padding);
39
+ }
40
+ /**
41
+ * 解密
42
+ * @param ciphertext 待解密文本
43
+ * @param encoding 编码,默认:'base64'
44
+ * @param hashType 哈希方式,默认:'sha1'
45
+ * @param padding 补位方式,默认:crypto.constants.RSA_PKCS1_OAEP_PADDING
46
+ * @returns
47
+ */
48
+ decrypt(ciphertext, encoding = 'base64', hashType = 'sha1', padding = crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING) {
49
+ return super.decrypt(ciphertext, encoding, hashType, padding);
50
+ }
24
51
  }
25
52
  module.exports = Encryptor;
@@ -12,6 +12,7 @@ declare class Utils {
12
12
  getPlatformCert(): Promise<PublicKey>;
13
13
  /**
14
14
  * 获取敏感信息加密机
15
+ * @see https://pay.weixin.qq.com/docs/merchant/development/interface-rules/sensitive-data-encryption.html
15
16
  * @param platformCert PublicKey封装过的平台证书
16
17
  * @returns
17
18
  */
package/dist/Pay/Utils.js CHANGED
@@ -34,6 +34,7 @@ class Utils {
34
34
  }
35
35
  /**
36
36
  * 获取敏感信息加密机
37
+ * @see https://pay.weixin.qq.com/docs/merchant/development/interface-rules/sensitive-data-encryption.html
37
38
  * @param platformCert PublicKey封装过的平台证书
38
39
  * @returns
39
40
  */
package/dist/index.d.ts CHANGED
@@ -12,7 +12,9 @@ import OfficialAccountMessage from './OfficialAccount/Message';
12
12
  import WorkMessage from './Work/Message';
13
13
  import OpenPlatformMessage from './OpenPlatform/Message';
14
14
  import OpenWorkMessage from './OpenWork/Message';
15
- export { OfficialAccount, OfficialAccountConfig, MiniApp, MiniAppConfig, Pay, PayConfig, OpenPlatform, OpenPlatformConfig, Work, WorkConfig, OpenWork, OpenWorkConfig, CacheInterface, ServerRequest, LogHandler, ServerEventType, ServerHandlerClosure,
15
+ import { PublicKey } from './Core/Support/PublicKey';
16
+ import { PrivateKey } from './Core/Support/PrivateKey';
17
+ export { OfficialAccount, OfficialAccountConfig, MiniApp, MiniAppConfig, Pay, PayConfig, OpenPlatform, OpenPlatformConfig, Work, WorkConfig, OpenWork, OpenWorkConfig, CacheInterface, ServerRequest, LogHandler, ServerEventType, ServerHandlerClosure, PublicKey, PrivateKey,
16
18
  /**
17
19
  * 表单对象
18
20
  * @see https://github.com/axios/axios#formdata
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ 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
- exports.definePaymentScannedHandler = exports.definePaymentRefundedHandler = exports.definePaymentPaidHandler = exports.definePaymentAlertHandler = exports.definePaymentFailHandler = exports.defineHttpClientFailureJudgeClosure = exports.defineOpenWorkServerHandler = exports.defineOpenPlatformServerHandler = exports.defineWorkServerHandler = exports.defineMiniAppServerHandler = exports.defineOfficeAccountServerHandler = exports.defineLogHandler = exports.defineOpenWorkConfig = exports.defineWorkConfig = exports.defineOpenPlatformConfig = exports.definePayConfig = exports.defineMiniAppConfig = exports.defineOfficialAccountConfig = exports.FormData = exports.ServerRequest = exports.CacheInterface = exports.OpenWork = exports.Work = exports.OpenPlatform = exports.Pay = exports.MiniApp = exports.OfficialAccount = void 0;
6
+ exports.definePaymentScannedHandler = exports.definePaymentRefundedHandler = exports.definePaymentPaidHandler = exports.definePaymentAlertHandler = exports.definePaymentFailHandler = exports.defineHttpClientFailureJudgeClosure = exports.defineOpenWorkServerHandler = exports.defineOpenPlatformServerHandler = exports.defineWorkServerHandler = exports.defineMiniAppServerHandler = exports.defineOfficeAccountServerHandler = exports.defineLogHandler = exports.defineOpenWorkConfig = exports.defineWorkConfig = exports.defineOpenPlatformConfig = exports.definePayConfig = exports.defineMiniAppConfig = exports.defineOfficialAccountConfig = exports.FormData = exports.PrivateKey = exports.PublicKey = exports.ServerRequest = exports.CacheInterface = exports.OpenWork = exports.Work = exports.OpenPlatform = exports.Pay = exports.MiniApp = exports.OfficialAccount = void 0;
7
7
  const Application_1 = __importDefault(require("./OfficialAccount/Application"));
8
8
  exports.OfficialAccount = Application_1.default;
9
9
  const Application_2 = __importDefault(require("./MiniApp/Application"));
@@ -22,6 +22,10 @@ const ServerRequest_1 = __importDefault(require("./Core/Http/ServerRequest"));
22
22
  exports.ServerRequest = ServerRequest_1.default;
23
23
  const form_data_1 = __importDefault(require("form-data"));
24
24
  exports.FormData = form_data_1.default;
25
+ const PublicKey_1 = require("./Core/Support/PublicKey");
26
+ Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return PublicKey_1.PublicKey; } });
27
+ const PrivateKey_1 = require("./Core/Support/PrivateKey");
28
+ Object.defineProperty(exports, "PrivateKey", { enumerable: true, get: function () { return PrivateKey_1.PrivateKey; } });
25
29
  /**
26
30
  * 定义公众号配置
27
31
  * @param config
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-easywechat",
3
- "version": "3.5.17",
3
+ "version": "3.5.19",
4
4
  "description": "EasyWechat SDK for Node.js (NOT OFFICIAL)",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {