Comparing version 2.0.5 to 2.0.6
@@ -47,2 +47,3 @@ import { Context } from 'egg'; | ||
private parseState; | ||
private generateId; | ||
} |
@@ -213,2 +213,10 @@ "use strict"; | ||
this.permissionHandler = async (ctx, userId) => { | ||
// 根据 egg 规范,如果存在 tracer,那么将会保持在 ctx.tracer 中 | ||
if (ctx.tracer) { | ||
const tracerId = ctx.tracer.id; | ||
// 某些情况下,可能会出现 spanId 不存在的情况,这时候需要自己设置 spanId | ||
const spanId = ctx.tracer.spanId || this.generateId(8); | ||
kauthApi.setTraceId(tracerId); | ||
kauthApi.setSpanId(spanId); | ||
} | ||
const permissions = await kauthApi.listPermissionsByUserId(userId); | ||
@@ -237,2 +245,20 @@ ctx.permissions = permissions; | ||
} | ||
generateId(bytes) { | ||
const spanBuffer = Buffer.allocUnsafe(bytes); | ||
for (let i = 0; i < bytes / 4; i++) { | ||
// unsigned right shift drops decimal part of the number | ||
// it is required because if a number between 2**32 and 2**32 - 1 is generated, an out of range error is thrown by writeUInt32BE | ||
spanBuffer.writeUInt32BE((Math.random() * 2 ** 32) >>> 0, i * 4); | ||
} | ||
// If buffer is all 0, set the last byte to 1 to guarantee a valid w3c id is generated | ||
for (let i = 0; i < bytes; i++) { | ||
if (spanBuffer[i] > 0) { | ||
break; | ||
} | ||
else if (i === bytes - 1) { | ||
spanBuffer[bytes - 1] = 1; | ||
} | ||
} | ||
return spanBuffer.toString('hex', 0, bytes); | ||
} | ||
} | ||
@@ -239,0 +265,0 @@ exports.KauthProvider = KauthProvider; |
{ | ||
"name": "egg-kauth", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"description": "egg kauth plugin", | ||
@@ -5,0 +5,0 @@ "eggPlugin": { |
24149
532