@seayoo-io/combo-sdk-node 0.1.0 → 0.1.3
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 +134 -12
- package/dist/const.d.ts +1 -1
- package/dist/index.cjs +44 -13
- package/dist/index.d.ts +1 -1
- package/dist/index.js +43 -14
- package/dist/notify/index.d.ts +14 -6
- package/dist/utils/signer.d.ts +1 -1
- package/package.json +12 -7
- package/vitest.config.ts +9 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Combo SDK for
|
|
1
|
+
# Combo SDK for Node.js
|
|
2
2
|
|
|
3
3
|
`@seayoo-io/combo-sdk-node` 是世游核心系统 (Combo) 为 Node.js 提供的 SDK。
|
|
4
4
|
|
|
@@ -18,8 +18,22 @@ npm install @seayoo-io/combo-sdk-node
|
|
|
18
18
|
pnpm add @seayoo-io/combo-sdk-node
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
+
## 导入方式
|
|
22
|
+
|
|
23
|
+
请按照工程实际情况选择导入方式,后续示例采用 ES 方式导入。
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
// ES
|
|
27
|
+
import { ApiClient } from "@seayoo-io/combo-sdk-node"
|
|
28
|
+
|
|
29
|
+
// CommonJS
|
|
30
|
+
const { ApiClient } = require("@seayoo-io/combo-sdk-node")
|
|
31
|
+
```
|
|
32
|
+
|
|
21
33
|
## ApiClient
|
|
22
34
|
|
|
35
|
+
### 初始化
|
|
36
|
+
|
|
23
37
|
```js
|
|
24
38
|
import { ApiClient, Endpoint } from "@seayoo-io/combo-sdk-node"
|
|
25
39
|
|
|
@@ -33,7 +47,11 @@ const client = new ApiClient({
|
|
|
33
47
|
logger: function(log) {} // 请求日志函数,log 类型参见源码类型定义
|
|
34
48
|
timeout: 5000, // 超时等待时长,单位 ms,默认 5000
|
|
35
49
|
})
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 创建订单 CreateOrder
|
|
36
53
|
|
|
54
|
+
```js
|
|
37
55
|
// 创建订单
|
|
38
56
|
const createOrderResult = await client.createOrder({
|
|
39
57
|
/** 用于标识创建订单请求的唯一 ID */
|
|
@@ -54,7 +72,7 @@ const createOrderResult = await client.createOrder({
|
|
|
54
72
|
/** [可选]订单上下文,在发货通知中透传回游戏 */
|
|
55
73
|
context: "",
|
|
56
74
|
/**
|
|
57
|
-
*
|
|
75
|
+
* 订单的元数据,详细定义参看 OrderMetaData
|
|
58
76
|
*
|
|
59
77
|
* 大部分元数据用于数据分析与查询,游戏侧应当尽量提供
|
|
60
78
|
* 某些元数据在特定的支付场景下是必须的,例如微信小游戏的 iOS 支付场景
|
|
@@ -62,20 +80,50 @@ const createOrderResult = await client.createOrder({
|
|
|
62
80
|
meta: { ... }
|
|
63
81
|
})
|
|
64
82
|
|
|
83
|
+
interface OrderMetaData {
|
|
84
|
+
/** 游戏大区 ID */
|
|
85
|
+
zone_id?: string
|
|
86
|
+
/** 游戏服务器 ID */
|
|
87
|
+
server_id?: string
|
|
88
|
+
/** 游戏角色 ID */
|
|
89
|
+
role_id?: string
|
|
90
|
+
/** 游戏角色名 */
|
|
91
|
+
role_name?: string
|
|
92
|
+
/** 游戏角色的等级 */
|
|
93
|
+
role_level?: number
|
|
94
|
+
/** 微信小游戏的 App ID, 微信小游戏的 iOS 支付场景必须传入 */
|
|
95
|
+
weixin_appid?: string
|
|
96
|
+
/** 微信小游戏的玩家 OpenID, 微信小游戏的 iOS 支付场景必须传入 */
|
|
97
|
+
weixin_openid?: string
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface CreateOrderResponse {
|
|
101
|
+
/** 世游服务端创建的,标识订单的唯一 ID。 */
|
|
102
|
+
order_id: string
|
|
103
|
+
/** 世游服务端创建的订单 token,用于后续支付流程。 */
|
|
104
|
+
order_token: string
|
|
105
|
+
/** 订单失效时间。Unix timestamp in seconds。 */
|
|
106
|
+
expires_at: number
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 进入/离开游戏
|
|
111
|
+
|
|
112
|
+
```js
|
|
65
113
|
// 进入游戏
|
|
66
114
|
await client.enterGame("<ComboID>", "<SessionID>");
|
|
67
115
|
|
|
68
116
|
// 离开游戏
|
|
69
117
|
await client.leaveGame("<ComboID>", "<SessionID>");
|
|
70
|
-
|
|
71
118
|
```
|
|
72
119
|
|
|
73
120
|
## Notify
|
|
74
121
|
|
|
122
|
+
### Step 1 准备参数
|
|
123
|
+
|
|
75
124
|
```js
|
|
76
125
|
import { NotificationType, Endpoint } from "@seayoo-io/combo-sdk-node"
|
|
77
126
|
|
|
78
|
-
// Step 1 准备参数
|
|
79
127
|
// 1.1 定义消息处理函数,类型定义参见源码 NotificationHandler
|
|
80
128
|
function notificationHandler(type, payload) {
|
|
81
129
|
switch(type) {
|
|
@@ -84,34 +132,90 @@ function notificationHandler(type, payload) {
|
|
|
84
132
|
break;
|
|
85
133
|
}
|
|
86
134
|
}
|
|
135
|
+
|
|
87
136
|
// 1.2 创建配置,类型定义参见源码 SDKBaseConfig
|
|
88
137
|
const config = {
|
|
89
138
|
game: "<GameId>",
|
|
90
139
|
secret: "<SecretKey>",
|
|
91
140
|
endpoint: Endpoint.China
|
|
92
141
|
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Step 2 使用 RequestHandler 处理通知
|
|
93
145
|
|
|
94
|
-
|
|
95
|
-
//
|
|
146
|
+
```js
|
|
147
|
+
// 使用 http 模块的处理函数
|
|
96
148
|
import { getNotificationHandler } from "@seayoo-io/combo-sdk-node"
|
|
97
149
|
const notifyHandler = getNotificationHandler(config, notificationHandler)
|
|
98
150
|
http.createServer(async function(req, res){
|
|
99
|
-
if(req.
|
|
151
|
+
if(req.path === "<YourNotifyUrl>" && req.method === "POST") {
|
|
100
152
|
await notifyHandler(req, res)
|
|
101
153
|
}
|
|
102
154
|
})
|
|
103
|
-
|
|
155
|
+
|
|
156
|
+
// 或,使用 Express 处理函数
|
|
104
157
|
import { getNotificationHandlerForExpress } from "@seayoo-io/combo-sdk-node"
|
|
105
158
|
const expressHandler = getNotificationHandlerForExpress(config, notificationHandler)
|
|
106
159
|
app.post("/path/to/your/notify/url", expressHandler)
|
|
107
|
-
|
|
160
|
+
|
|
161
|
+
// 或,使用 Koa 处理函数
|
|
108
162
|
import { getNotificationHandlerForKoa } from "@seayoo-io/combo-sdk-node"
|
|
109
163
|
const expressHandler = getNotificationHandlerForKoa(config, notificationHandler)
|
|
110
164
|
app.post("/path/to/your/notify/url", expressHandler)
|
|
111
165
|
```
|
|
112
166
|
|
|
167
|
+
> ⚠️ 注意事项
|
|
168
|
+
>
|
|
169
|
+
> 由于 http request 的 body 只能被消费(读取)一次,如果您使用了 express 或 koa 框架并优先加载了 bodyParse 类似的插件,那么 NotificationHandler 将无法再次读取 request 内容用于签名计算,此时需要提供请求的原始 rawBody 才可以继续。
|
|
170
|
+
>
|
|
171
|
+
> 网上有一些方法可以在 express 或 koa 框架下获取 rawBody 内容,但都具有一定的破坏性风险。
|
|
172
|
+
>
|
|
173
|
+
> 推荐以下解决方案:
|
|
174
|
+
|
|
175
|
+
### Step 2 使用 RequestMiddleware 处理通知
|
|
176
|
+
|
|
177
|
+
针对 express 或 koa 框架,可以创建一个高优先级的插件来处理回调通知,其原理如下
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
// 插件需要先于 bodyParse 类似的插件执行,以确保可以获取原始请求的内容
|
|
181
|
+
app.use(function(req, res, next){
|
|
182
|
+
// 在其他插件处理之前,检查是否为通知请求的 url,方法是否为 POST
|
|
183
|
+
if(req.method === "POST" && req.path === "/path/to/your/notify/url") {
|
|
184
|
+
// 如果是,则交由插件处理后续响应
|
|
185
|
+
notifyMiddleware(req, res)
|
|
186
|
+
return
|
|
187
|
+
}
|
|
188
|
+
// 否则其他一切正常
|
|
189
|
+
next()
|
|
190
|
+
})
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
SDK 提供了两个插件来处理:
|
|
194
|
+
|
|
195
|
+
```js
|
|
196
|
+
// express 插件
|
|
197
|
+
import { getNotificationMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
|
|
198
|
+
const notifyMiddleware = getNotificationMiddlewareForExpress(
|
|
199
|
+
"/path/to/your/notify/url",
|
|
200
|
+
config,
|
|
201
|
+
handler
|
|
202
|
+
);
|
|
203
|
+
app.use(notifyMiddleware);
|
|
204
|
+
|
|
205
|
+
// koa 插件
|
|
206
|
+
import { getNotificationMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
|
|
207
|
+
const notifyMiddleware = getNotificationMiddlewareForKoa(
|
|
208
|
+
"/path/to/your/notify/url",
|
|
209
|
+
config,
|
|
210
|
+
handler
|
|
211
|
+
);
|
|
212
|
+
app.use(notifyMiddleware);
|
|
213
|
+
```
|
|
214
|
+
|
|
113
215
|
## Verify
|
|
114
216
|
|
|
217
|
+
### 初始化
|
|
218
|
+
|
|
115
219
|
```js
|
|
116
220
|
import { TokenVerifier, Endpoint } from "@seayoo-io/combo-sdk-node"
|
|
117
221
|
|
|
@@ -120,7 +224,12 @@ const verifier = new TokenVerifier({
|
|
|
120
224
|
secret: "<SecretKey>",
|
|
121
225
|
endpoint: Endpoint.China
|
|
122
226
|
})
|
|
227
|
+
```
|
|
123
228
|
|
|
229
|
+
### IdentityToken 验证
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
import { IdP } from "@seayoo-io/combo-sdk-node"
|
|
124
233
|
// verifyIdentityToken 对 IdentityToken 进行验证
|
|
125
234
|
// 返回 IdentityPayload 数据,如果解析出错则返回 Error 对象
|
|
126
235
|
const result = verifier.verifyIdentityToken(token)
|
|
@@ -128,15 +237,28 @@ if(result instanceof Error) {
|
|
|
128
237
|
console.error(result.message)
|
|
129
238
|
} else {
|
|
130
239
|
// got Id info: { combo_id, idp, external_id, external_name, weixin_unionid }
|
|
240
|
+
// 微信登录判断
|
|
241
|
+
if(result.idp === IdP.Weixin) {
|
|
242
|
+
// 微信登录会提供 weixin_unionid
|
|
243
|
+
// do something
|
|
244
|
+
}
|
|
245
|
+
// 游客登录判断
|
|
246
|
+
if(result.idp === IdP.Device) {
|
|
247
|
+
// do something
|
|
248
|
+
}
|
|
249
|
+
// 更多 IdP 枚举可以查看源码定义
|
|
131
250
|
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### AdToken 验证
|
|
132
254
|
|
|
255
|
+
```js
|
|
133
256
|
// verifyAdToken 对 AdToken 进行验证
|
|
134
257
|
// 返回 AdPayload 数据,如果解析出错则返回 Error 对象
|
|
135
258
|
const adInfo = vierfier.verifyAdToken(token)
|
|
136
259
|
if(result instanceof Error) {
|
|
137
260
|
console.error(result.message)
|
|
138
261
|
} else {
|
|
139
|
-
// got ad info: { combo_id,
|
|
262
|
+
// got ad info: { combo_id, placement_id, impression_id }
|
|
140
263
|
}
|
|
141
|
-
```
|
|
142
|
-
|
|
264
|
+
```
|
package/dist/const.d.ts
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -28,7 +28,7 @@ var https__namespace = /*#__PURE__*/_interopNamespace(https);
|
|
|
28
28
|
/** SDK 名称 */
|
|
29
29
|
const SDKName = "combo-sdk-node";
|
|
30
30
|
/** SDK 版本,在编译前会被动态替换 */
|
|
31
|
-
const SDKVersion = "0.1.
|
|
31
|
+
const SDKVersion = "0.1.3";
|
|
32
32
|
/**
|
|
33
33
|
* 世游支持的服务端点列表
|
|
34
34
|
*/
|
|
@@ -187,7 +187,6 @@ function verifyConfig(config) {
|
|
|
187
187
|
|
|
188
188
|
const AuthorizationField = "Authorization";
|
|
189
189
|
const MaxTimeDiff = 5 * 60 * 1000;
|
|
190
|
-
const EmptyStringSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
191
190
|
const timestampReg = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/;
|
|
192
191
|
// 签名方法定义,给未来扩展新签名方法留出空间
|
|
193
192
|
const SigningDefinitions = {
|
|
@@ -196,15 +195,17 @@ const SigningDefinitions = {
|
|
|
196
195
|
sign({
|
|
197
196
|
game,
|
|
198
197
|
secret,
|
|
199
|
-
endpoint,
|
|
200
198
|
method,
|
|
201
199
|
url,
|
|
202
200
|
data,
|
|
203
201
|
timestamp
|
|
204
202
|
}, onlySignature = false) {
|
|
205
|
-
|
|
203
|
+
if (timestamp && !timestampReg.test(timestamp)) {
|
|
204
|
+
return "ErrorTimestamp";
|
|
205
|
+
}
|
|
206
|
+
const payloadHash = sha256(convertBodyData(data || ""));
|
|
206
207
|
const ts = timestamp || getTimestamp();
|
|
207
|
-
const fullURL = url
|
|
208
|
+
const fullURL = isFullURL(url) ? new URL(url) : new URL(url, "http://x.com");
|
|
208
209
|
const uri = fullURL.pathname + fullURL.search;
|
|
209
210
|
const signature = hS256(secret, [this.prefix, method.toUpperCase(), uri, ts, payloadHash].join("\n"));
|
|
210
211
|
return onlySignature ? signature : `${this.prefix} Game=${game},Timestamp=${ts},Signature=${signature}`;
|
|
@@ -221,7 +222,8 @@ const SigningDefinitions = {
|
|
|
221
222
|
return false;
|
|
222
223
|
}
|
|
223
224
|
const ts = parseTimestamp(timestamp);
|
|
224
|
-
|
|
225
|
+
// 时间戳精度只到秒,需要忽略毫秒值
|
|
226
|
+
const now = Math.floor(Date.now() / 1000) * 1000;
|
|
225
227
|
if (!ts || ts < now - MaxTimeDiff || ts > now + MaxTimeDiff) {
|
|
226
228
|
return false;
|
|
227
229
|
}
|
|
@@ -295,8 +297,7 @@ function convertBodyData(body) {
|
|
|
295
297
|
if (typeof body === "string") {
|
|
296
298
|
return body;
|
|
297
299
|
}
|
|
298
|
-
|
|
299
|
-
return body.toString();
|
|
300
|
+
return JSON.stringify(body);
|
|
300
301
|
}
|
|
301
302
|
function parseRawAuthHeader(authString, prefix) {
|
|
302
303
|
if (!authString || !authString.startsWith(prefix + " ")) {
|
|
@@ -317,7 +318,7 @@ function parseRawAuthHeader(authString, prefix) {
|
|
|
317
318
|
}
|
|
318
319
|
function getValue(value) {
|
|
319
320
|
const v = value.trim();
|
|
320
|
-
return /^".+"$/.test(v) ?
|
|
321
|
+
return /^".+"$/.test(v) ? v.replace(/^"|"$/g, "") : v;
|
|
321
322
|
}
|
|
322
323
|
|
|
323
324
|
async function readBody(req, backup) {
|
|
@@ -1237,16 +1238,28 @@ function getNotificationHandlerForExpress(config, handler) {
|
|
|
1237
1238
|
await func(req, res, "rawBody" in req ? `${req.rawBody}` : "");
|
|
1238
1239
|
};
|
|
1239
1240
|
}
|
|
1241
|
+
/**
|
|
1242
|
+
* 获取基于 express 的中间件用于接收 Seayoo Server 推送过来的消息通知
|
|
1243
|
+
*
|
|
1244
|
+
* 需要先于 body-parse 等类似插件加载
|
|
1245
|
+
*/
|
|
1246
|
+
function getNotificationMiddlewareForExpress(paths, config, handler) {
|
|
1247
|
+
const func = getNotificationHandler(config, handler);
|
|
1248
|
+
const matchPaths = Array.isArray(paths) ? paths : [paths];
|
|
1249
|
+
return async function (req, res, next) {
|
|
1250
|
+
if (req.method.toUpperCase() === "POST" && matchPaths.includes(req.path)) {
|
|
1251
|
+
await func(req, res);
|
|
1252
|
+
return;
|
|
1253
|
+
}
|
|
1254
|
+
next();
|
|
1255
|
+
};
|
|
1256
|
+
}
|
|
1240
1257
|
/**
|
|
1241
1258
|
* 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
|
|
1242
1259
|
*
|
|
1243
1260
|
* 如需自定义处理,可以使用 getNotificationHandler 方法
|
|
1244
1261
|
*
|
|
1245
1262
|
* 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
|
|
1246
|
-
*
|
|
1247
|
-
* 处理方法可以参考
|
|
1248
|
-
*
|
|
1249
|
-
*
|
|
1250
1263
|
*/
|
|
1251
1264
|
function getNotificationHandlerForKoa(config, handler) {
|
|
1252
1265
|
const func = getNotificationHandler(config, handler);
|
|
@@ -1254,6 +1267,22 @@ function getNotificationHandlerForKoa(config, handler) {
|
|
|
1254
1267
|
await func(ctx.req, ctx.res, "rawBody" in ctx ? `${ctx.rawBody}` : "");
|
|
1255
1268
|
};
|
|
1256
1269
|
}
|
|
1270
|
+
/**
|
|
1271
|
+
* 获取基于 koa2 的中间件用于接收 Seayoo Server 推送过来的消息通知
|
|
1272
|
+
*
|
|
1273
|
+
* 需要先于 koa-body 等类似插件加载
|
|
1274
|
+
*/
|
|
1275
|
+
function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
1276
|
+
const func = getNotificationHandler(config, handler);
|
|
1277
|
+
const matchPaths = Array.isArray(paths) ? paths : [paths];
|
|
1278
|
+
return async function (ctx, next) {
|
|
1279
|
+
if (ctx.method.toUpperCase() === "POST" && matchPaths.includes(ctx.path)) {
|
|
1280
|
+
await func(ctx.req, ctx.res);
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
next();
|
|
1284
|
+
};
|
|
1285
|
+
}
|
|
1257
1286
|
|
|
1258
1287
|
exports.ApiClient = ApiClient;
|
|
1259
1288
|
exports.TokenVerifier = TokenVerifier;
|
|
@@ -1261,4 +1290,6 @@ exports.calcAuthorizationHeader = calcAuthorizationHeader;
|
|
|
1261
1290
|
exports.getNotificationHandler = getNotificationHandler;
|
|
1262
1291
|
exports.getNotificationHandlerForExpress = getNotificationHandlerForExpress;
|
|
1263
1292
|
exports.getNotificationHandlerForKoa = getNotificationHandlerForKoa;
|
|
1293
|
+
exports.getNotificationMiddlewareForExpress = getNotificationMiddlewareForExpress;
|
|
1294
|
+
exports.getNotificationMiddlewareForKoa = getNotificationMiddlewareForKoa;
|
|
1264
1295
|
exports.parseAuthorizationHeader = parseAuthorizationHeader;
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type { CreateOrderOption, OrderMetaData, CreateOrderResponse } from "./ap
|
|
|
4
4
|
export { TokenVerifier } from "./verify";
|
|
5
5
|
export type { IdentityPayload } from "./verify/id";
|
|
6
6
|
export type { AdPayload } from "./verify/ads";
|
|
7
|
-
export { getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa } from "./notify";
|
|
7
|
+
export { getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, } from "./notify";
|
|
8
8
|
export { NotificationType } from "./notify/types";
|
|
9
9
|
export type { NotificationHandler } from "./notify/types";
|
|
10
10
|
export type { SDKBaseConfig } from "./utils";
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { verify } from 'jsonwebtoken';
|
|
|
7
7
|
/** SDK 名称 */
|
|
8
8
|
const SDKName = "combo-sdk-node";
|
|
9
9
|
/** SDK 版本,在编译前会被动态替换 */
|
|
10
|
-
const SDKVersion = "0.1.
|
|
10
|
+
const SDKVersion = "0.1.3";
|
|
11
11
|
/**
|
|
12
12
|
* 世游支持的服务端点列表
|
|
13
13
|
*/
|
|
@@ -166,7 +166,6 @@ function verifyConfig(config) {
|
|
|
166
166
|
|
|
167
167
|
const AuthorizationField = "Authorization";
|
|
168
168
|
const MaxTimeDiff = 5 * 60 * 1000;
|
|
169
|
-
const EmptyStringSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
170
169
|
const timestampReg = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/;
|
|
171
170
|
// 签名方法定义,给未来扩展新签名方法留出空间
|
|
172
171
|
const SigningDefinitions = {
|
|
@@ -175,15 +174,17 @@ const SigningDefinitions = {
|
|
|
175
174
|
sign({
|
|
176
175
|
game,
|
|
177
176
|
secret,
|
|
178
|
-
endpoint,
|
|
179
177
|
method,
|
|
180
178
|
url,
|
|
181
179
|
data,
|
|
182
180
|
timestamp
|
|
183
181
|
}, onlySignature = false) {
|
|
184
|
-
|
|
182
|
+
if (timestamp && !timestampReg.test(timestamp)) {
|
|
183
|
+
return "ErrorTimestamp";
|
|
184
|
+
}
|
|
185
|
+
const payloadHash = sha256(convertBodyData(data || ""));
|
|
185
186
|
const ts = timestamp || getTimestamp();
|
|
186
|
-
const fullURL = url
|
|
187
|
+
const fullURL = isFullURL(url) ? new URL(url) : new URL(url, "http://x.com");
|
|
187
188
|
const uri = fullURL.pathname + fullURL.search;
|
|
188
189
|
const signature = hS256(secret, [this.prefix, method.toUpperCase(), uri, ts, payloadHash].join("\n"));
|
|
189
190
|
return onlySignature ? signature : `${this.prefix} Game=${game},Timestamp=${ts},Signature=${signature}`;
|
|
@@ -200,7 +201,8 @@ const SigningDefinitions = {
|
|
|
200
201
|
return false;
|
|
201
202
|
}
|
|
202
203
|
const ts = parseTimestamp(timestamp);
|
|
203
|
-
|
|
204
|
+
// 时间戳精度只到秒,需要忽略毫秒值
|
|
205
|
+
const now = Math.floor(Date.now() / 1000) * 1000;
|
|
204
206
|
if (!ts || ts < now - MaxTimeDiff || ts > now + MaxTimeDiff) {
|
|
205
207
|
return false;
|
|
206
208
|
}
|
|
@@ -274,8 +276,7 @@ function convertBodyData(body) {
|
|
|
274
276
|
if (typeof body === "string") {
|
|
275
277
|
return body;
|
|
276
278
|
}
|
|
277
|
-
|
|
278
|
-
return body.toString();
|
|
279
|
+
return JSON.stringify(body);
|
|
279
280
|
}
|
|
280
281
|
function parseRawAuthHeader(authString, prefix) {
|
|
281
282
|
if (!authString || !authString.startsWith(prefix + " ")) {
|
|
@@ -296,7 +297,7 @@ function parseRawAuthHeader(authString, prefix) {
|
|
|
296
297
|
}
|
|
297
298
|
function getValue(value) {
|
|
298
299
|
const v = value.trim();
|
|
299
|
-
return /^".+"$/.test(v) ?
|
|
300
|
+
return /^".+"$/.test(v) ? v.replace(/^"|"$/g, "") : v;
|
|
300
301
|
}
|
|
301
302
|
|
|
302
303
|
async function readBody(req, backup) {
|
|
@@ -1216,16 +1217,28 @@ function getNotificationHandlerForExpress(config, handler) {
|
|
|
1216
1217
|
await func(req, res, "rawBody" in req ? `${req.rawBody}` : "");
|
|
1217
1218
|
};
|
|
1218
1219
|
}
|
|
1220
|
+
/**
|
|
1221
|
+
* 获取基于 express 的中间件用于接收 Seayoo Server 推送过来的消息通知
|
|
1222
|
+
*
|
|
1223
|
+
* 需要先于 body-parse 等类似插件加载
|
|
1224
|
+
*/
|
|
1225
|
+
function getNotificationMiddlewareForExpress(paths, config, handler) {
|
|
1226
|
+
const func = getNotificationHandler(config, handler);
|
|
1227
|
+
const matchPaths = Array.isArray(paths) ? paths : [paths];
|
|
1228
|
+
return async function (req, res, next) {
|
|
1229
|
+
if (req.method.toUpperCase() === "POST" && matchPaths.includes(req.path)) {
|
|
1230
|
+
await func(req, res);
|
|
1231
|
+
return;
|
|
1232
|
+
}
|
|
1233
|
+
next();
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1219
1236
|
/**
|
|
1220
1237
|
* 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
|
|
1221
1238
|
*
|
|
1222
1239
|
* 如需自定义处理,可以使用 getNotificationHandler 方法
|
|
1223
1240
|
*
|
|
1224
1241
|
* 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
|
|
1225
|
-
*
|
|
1226
|
-
* 处理方法可以参考
|
|
1227
|
-
*
|
|
1228
|
-
*
|
|
1229
1242
|
*/
|
|
1230
1243
|
function getNotificationHandlerForKoa(config, handler) {
|
|
1231
1244
|
const func = getNotificationHandler(config, handler);
|
|
@@ -1233,5 +1246,21 @@ function getNotificationHandlerForKoa(config, handler) {
|
|
|
1233
1246
|
await func(ctx.req, ctx.res, "rawBody" in ctx ? `${ctx.rawBody}` : "");
|
|
1234
1247
|
};
|
|
1235
1248
|
}
|
|
1249
|
+
/**
|
|
1250
|
+
* 获取基于 koa2 的中间件用于接收 Seayoo Server 推送过来的消息通知
|
|
1251
|
+
*
|
|
1252
|
+
* 需要先于 koa-body 等类似插件加载
|
|
1253
|
+
*/
|
|
1254
|
+
function getNotificationMiddlewareForKoa(paths, config, handler) {
|
|
1255
|
+
const func = getNotificationHandler(config, handler);
|
|
1256
|
+
const matchPaths = Array.isArray(paths) ? paths : [paths];
|
|
1257
|
+
return async function (ctx, next) {
|
|
1258
|
+
if (ctx.method.toUpperCase() === "POST" && matchPaths.includes(ctx.path)) {
|
|
1259
|
+
await func(ctx.req, ctx.res);
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
next();
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1236
1265
|
|
|
1237
|
-
export { ApiClient, Endpoint, IdP, Platform, TokenVerifier, calcAuthorizationHeader, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, parseAuthorizationHeader };
|
|
1266
|
+
export { ApiClient, Endpoint, IdP, Platform, TokenVerifier, calcAuthorizationHeader, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
|
package/dist/notify/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { type SDKBaseConfig } from "../utils";
|
|
3
3
|
import { type NotificationHandler } from "./types";
|
|
4
|
-
import type { ParameterizedContext } from "koa";
|
|
4
|
+
import type { ParameterizedContext, Next } from "koa";
|
|
5
5
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
6
|
-
import type { Request as ExpressRequest, Response as ExpressResponse } from "express";
|
|
6
|
+
import type { Request as ExpressRequest, Response as ExpressResponse, NextFunction as ExpressNextFunction } from "express";
|
|
7
7
|
/**
|
|
8
8
|
* 获取一个处理函数用于接收 Seayoo Server 推送过来的消息通知
|
|
9
9
|
*
|
|
@@ -24,15 +24,23 @@ export declare function getNotificationHandler(config: SDKBaseConfig, handler: N
|
|
|
24
24
|
* https://cloud.tencent.com/developer/ask/sof/110062928
|
|
25
25
|
*/
|
|
26
26
|
export declare function getNotificationHandlerForExpress(config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest, res: ExpressResponse) => Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* 获取基于 express 的中间件用于接收 Seayoo Server 推送过来的消息通知
|
|
29
|
+
*
|
|
30
|
+
* 需要先于 body-parse 等类似插件加载
|
|
31
|
+
*/
|
|
32
|
+
export declare function getNotificationMiddlewareForExpress(paths: string | string[], config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest, res: ExpressResponse, next: ExpressNextFunction) => Promise<void>;
|
|
27
33
|
/**
|
|
28
34
|
* 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
|
|
29
35
|
*
|
|
30
36
|
* 如需自定义处理,可以使用 getNotificationHandler 方法
|
|
31
37
|
*
|
|
32
38
|
* 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
|
|
33
|
-
*
|
|
34
|
-
* 处理方法可以参考
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
39
|
*/
|
|
38
40
|
export declare function getNotificationHandlerForKoa(config: SDKBaseConfig, handler: NotificationHandler): (ctx: ParameterizedContext) => Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* 获取基于 koa2 的中间件用于接收 Seayoo Server 推送过来的消息通知
|
|
43
|
+
*
|
|
44
|
+
* 需要先于 koa-body 等类似插件加载
|
|
45
|
+
*/
|
|
46
|
+
export declare function getNotificationMiddlewareForKoa(paths: string | string[], config: SDKBaseConfig, handler: NotificationHandler): (ctx: ParameterizedContext, next: Next) => Promise<void>;
|
package/dist/utils/signer.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ type SignDataOptionForVerify = Omit<SignDataOption, "game" | "timestamp"> & {
|
|
|
15
15
|
declare const SigningDefinitions: {
|
|
16
16
|
readonly HS256: {
|
|
17
17
|
readonly prefix: "SEAYOO-HMAC-SHA256";
|
|
18
|
-
readonly sign: ({ game, secret,
|
|
18
|
+
readonly sign: ({ game, secret, method, url, data, timestamp }: SignDataOption, onlySignature?: boolean) => string;
|
|
19
19
|
readonly verify: (authString: string, option: SignDataOptionForVerify) => boolean;
|
|
20
20
|
};
|
|
21
21
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seayoo-io/combo-sdk-node",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Combo SDK for Node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -23,7 +23,11 @@
|
|
|
23
23
|
"type-check": "tsc --noEmit",
|
|
24
24
|
"lint": "eslint ./src/**/*.ts",
|
|
25
25
|
"lint:fix": "eslint ./src/**/*.{ts,js} --fix",
|
|
26
|
-
"
|
|
26
|
+
"test": "vitest",
|
|
27
|
+
"pub": "pnpm type-check && pnpm lint:fix && vitest run && pnpm build && npm publish"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=14.0.0"
|
|
27
31
|
},
|
|
28
32
|
"simple-git-hooks": {
|
|
29
33
|
"pre-commit": "pnpm lint && pnpm type-check"
|
|
@@ -40,9 +44,9 @@
|
|
|
40
44
|
"@types/express": "^4.17.21",
|
|
41
45
|
"@types/jsonwebtoken": "^9.0.5",
|
|
42
46
|
"@types/koa": "^2.13.12",
|
|
43
|
-
"@types/node": "^20.10.
|
|
44
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
45
|
-
"@typescript-eslint/parser": "^6.
|
|
47
|
+
"@types/node": "^20.10.6",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^6.17.0",
|
|
49
|
+
"@typescript-eslint/parser": "^6.17.0",
|
|
46
50
|
"eslint": "^8.56.0",
|
|
47
51
|
"eslint-config-prettier": "^9.1.0",
|
|
48
52
|
"eslint-plugin-prettier": "^5.1.2",
|
|
@@ -50,8 +54,9 @@
|
|
|
50
54
|
"microbundle": "^0.15.1",
|
|
51
55
|
"rimraf": "^5.0.5",
|
|
52
56
|
"simple-git-hooks": "^2.9.0",
|
|
53
|
-
"
|
|
54
|
-
"
|
|
57
|
+
"typescript": "^5.3.3",
|
|
58
|
+
"vite": "^5.0.10",
|
|
59
|
+
"vitest": "^1.1.1"
|
|
55
60
|
},
|
|
56
61
|
"dependencies": {
|
|
57
62
|
"jsonwebtoken": "^9.0.2"
|