@shware/http 0.2.23 → 0.2.26
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/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -1
- package/dist/utils/timing.cjs +74 -0
- package/dist/utils/timing.cjs.map +1 -0
- package/dist/utils/timing.d.cts +13 -0
- package/dist/utils/timing.d.ts +13 -0
- package/dist/utils/timing.mjs +49 -0
- package/dist/utils/timing.mjs.map +1 -0
- package/dist/webhook.cjs +1 -1
- package/dist/webhook.cjs.map +1 -1
- package/dist/webhook.d.cts +2 -2
- package/dist/webhook.d.ts +2 -2
- package/dist/webhook.mjs +1 -1
- package/dist/webhook.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -22,6 +22,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
22
22
|
var index_exports = {};
|
|
23
23
|
__export(index_exports, {
|
|
24
24
|
hasText: () => import_string.hasText,
|
|
25
|
+
timing: () => import_timing.timing,
|
|
25
26
|
verifyStandardWebhook: () => import_webhook.verifyStandardWebhook
|
|
26
27
|
});
|
|
27
28
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -31,10 +32,12 @@ __reExport(index_exports, require("./vaild.cjs"), module.exports);
|
|
|
31
32
|
__reExport(index_exports, require("./snowflake.cjs"), module.exports);
|
|
32
33
|
__reExport(index_exports, require("./response.cjs"), module.exports);
|
|
33
34
|
var import_string = require("./utils/string.cjs");
|
|
35
|
+
var import_timing = require("./utils/timing.cjs");
|
|
34
36
|
var import_webhook = require("./webhook.cjs");
|
|
35
37
|
// Annotate the CommonJS export names for ESM import in node:
|
|
36
38
|
0 && (module.exports = {
|
|
37
39
|
hasText,
|
|
40
|
+
timing,
|
|
38
41
|
verifyStandardWebhook,
|
|
39
42
|
...require("./detail.cjs"),
|
|
40
43
|
...require("./status.cjs"),
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport type {\n NetworkErrorReason,\n StatusErrorReason,\n AuthenticationErrorReason,\n ModerationErrorReason,\n MultipartErrorReason,\n AppErrorReason,\n ErrorReason,\n} from './reason';\n\nexport * from './detail';\nexport * from './status';\nexport * from './vaild';\nexport * from './snowflake';\nexport * from './response';\nexport { hasText } from './utils/string';\nexport { verifyStandardWebhook } from './webhook';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuBA,0BAAc,yBAvBd;AAwBA,0BAAc,yBAxBd;AAyBA,0BAAc,wBAzBd;AA0BA,0BAAc,4BA1Bd;AA2BA,0BAAc,2BA3Bd;AA4BA,oBAAwB;AACxB,qBAAsC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport type {\n NetworkErrorReason,\n StatusErrorReason,\n AuthenticationErrorReason,\n ModerationErrorReason,\n MultipartErrorReason,\n AppErrorReason,\n ErrorReason,\n} from './reason';\n\nexport * from './detail';\nexport * from './status';\nexport * from './vaild';\nexport * from './snowflake';\nexport * from './response';\nexport { hasText } from './utils/string';\nexport { timing } from './utils/timing';\nexport { verifyStandardWebhook } from './webhook';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuBA,0BAAc,yBAvBd;AAwBA,0BAAc,yBAxBd;AAyBA,0BAAc,wBAzBd;AA0BA,0BAAc,4BA1Bd;AA2BA,0BAAc,2BA3Bd;AA4BA,oBAAwB;AACxB,oBAAuB;AACvB,qBAAsC;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -5,5 +5,6 @@ export { Result, valid } from './vaild.cjs';
|
|
|
5
5
|
export { UidGenerator, uid } from './snowflake.cjs';
|
|
6
6
|
export { Cursor, Empty, Entity, EntityId, InitParams, NextParams, PageParams, PagedResponse, ParentPageParams, PrevParams, Response, pageParamsSchema } from './response.cjs';
|
|
7
7
|
export { hasText } from './utils/string.cjs';
|
|
8
|
+
export { timing } from './utils/timing.cjs';
|
|
8
9
|
export { verifyStandardWebhook } from './webhook.cjs';
|
|
9
10
|
import 'zod';
|
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export { Result, valid } from './vaild.js';
|
|
|
5
5
|
export { UidGenerator, uid } from './snowflake.js';
|
|
6
6
|
export { Cursor, Empty, Entity, EntityId, InitParams, NextParams, PageParams, PagedResponse, ParentPageParams, PrevParams, Response, pageParamsSchema } from './response.js';
|
|
7
7
|
export { hasText } from './utils/string.js';
|
|
8
|
+
export { timing } from './utils/timing.js';
|
|
8
9
|
export { verifyStandardWebhook } from './webhook.js';
|
|
9
10
|
import 'zod';
|
package/dist/index.mjs
CHANGED
|
@@ -5,9 +5,11 @@ export * from "./vaild.mjs";
|
|
|
5
5
|
export * from "./snowflake.mjs";
|
|
6
6
|
export * from "./response.mjs";
|
|
7
7
|
import { hasText } from "./utils/string.mjs";
|
|
8
|
+
import { timing } from "./utils/timing.mjs";
|
|
8
9
|
import { verifyStandardWebhook } from "./webhook.mjs";
|
|
9
10
|
export {
|
|
10
11
|
hasText,
|
|
12
|
+
timing,
|
|
11
13
|
verifyStandardWebhook
|
|
12
14
|
};
|
|
13
15
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport type {\n NetworkErrorReason,\n StatusErrorReason,\n AuthenticationErrorReason,\n ModerationErrorReason,\n MultipartErrorReason,\n AppErrorReason,\n ErrorReason,\n} from './reason';\n\nexport * from './detail';\nexport * from './status';\nexport * from './vaild';\nexport * from './snowflake';\nexport * from './response';\nexport { hasText } from './utils/string';\nexport { verifyStandardWebhook } from './webhook';\n"],"mappings":";AAuBA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,SAAS,eAAe;AACxB,SAAS,6BAA6B;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport type {\n NetworkErrorReason,\n StatusErrorReason,\n AuthenticationErrorReason,\n ModerationErrorReason,\n MultipartErrorReason,\n AppErrorReason,\n ErrorReason,\n} from './reason';\n\nexport * from './detail';\nexport * from './status';\nexport * from './vaild';\nexport * from './snowflake';\nexport * from './response';\nexport { hasText } from './utils/string';\nexport { timing } from './utils/timing';\nexport { verifyStandardWebhook } from './webhook';\n"],"mappings":";AAuBA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,SAAS,eAAe;AACxB,SAAS,cAAc;AACvB,SAAS,6BAA6B;","names":[]}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/utils/timing.ts
|
|
21
|
+
var timing_exports = {};
|
|
22
|
+
__export(timing_exports, {
|
|
23
|
+
timing: () => timing
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(timing_exports);
|
|
26
|
+
function timing(options = { enabled: true }) {
|
|
27
|
+
const { enabled = true, crossOrigin } = options;
|
|
28
|
+
let start = performance.now();
|
|
29
|
+
const headers = [];
|
|
30
|
+
const timers = /* @__PURE__ */ new Map();
|
|
31
|
+
return {
|
|
32
|
+
startTime: (name, description) => {
|
|
33
|
+
timers.set(name, { description, start: performance.now() });
|
|
34
|
+
},
|
|
35
|
+
endTime: (name, precision) => {
|
|
36
|
+
const timer = timers.get(name);
|
|
37
|
+
if (!timer) {
|
|
38
|
+
console.warn(`timing: ${name} not found`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const { start: start2, description } = timer;
|
|
42
|
+
const dur = (performance.now() - start2).toFixed(precision ?? 1);
|
|
43
|
+
timers.delete(name);
|
|
44
|
+
const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
|
|
45
|
+
headers.push(metric);
|
|
46
|
+
},
|
|
47
|
+
mark: (name, description, precision) => {
|
|
48
|
+
const dur = (performance.now() - start).toFixed(precision ?? 1);
|
|
49
|
+
const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
|
|
50
|
+
headers.push(metric);
|
|
51
|
+
start = performance.now();
|
|
52
|
+
},
|
|
53
|
+
setMetric: (name, value, description, precision) => {
|
|
54
|
+
if (typeof value === "number") {
|
|
55
|
+
const dur = value.toFixed(precision ?? 1);
|
|
56
|
+
const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
|
|
57
|
+
headers.push(metric);
|
|
58
|
+
} else {
|
|
59
|
+
const metric = value ? `${name};desc="${value}"` : `${name}`;
|
|
60
|
+
headers.push(metric);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
setTiming: (response) => {
|
|
64
|
+
if (!enabled) return;
|
|
65
|
+
response.headers.append("Server-Timing", headers.join(","));
|
|
66
|
+
response.headers.append("Timing-Allow-Origin", crossOrigin ?? "*");
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
timing
|
|
73
|
+
});
|
|
74
|
+
//# sourceMappingURL=timing.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/timing.ts"],"sourcesContent":["interface Timer {\n description?: string;\n start: number;\n}\n\nexport interface Options {\n enabled: boolean;\n crossOrigin?: string;\n}\n\nexport function timing(options: Options = { enabled: true }) {\n const { enabled = true, crossOrigin } = options;\n let start = performance.now();\n const headers: string[] = [];\n const timers = new Map<string, Timer>();\n\n return {\n startTime: (name: Lowercase<string>, description?: string) => {\n timers.set(name, { description, start: performance.now() });\n },\n endTime: (name: Lowercase<string>, precision?: number) => {\n const timer = timers.get(name);\n if (!timer) {\n console.warn(`timing: ${name} not found`);\n return;\n }\n const { start, description } = timer;\n const dur = (performance.now() - start).toFixed(precision ?? 1);\n timers.delete(name);\n const metric = description\n ? `${name};dur=${dur};desc=\"${description}\"`\n : `${name};dur=${dur}`;\n headers.push(metric);\n },\n mark: (name: Lowercase<string>, description?: string, precision?: number) => {\n const dur = (performance.now() - start).toFixed(precision ?? 1);\n const metric = description\n ? `${name};dur=${dur};desc=\"${description}\"`\n : `${name};dur=${dur}`;\n headers.push(metric);\n start = performance.now();\n },\n setMetric: (\n name: Lowercase<string>,\n value: number | string | undefined,\n description?: string,\n precision?: number\n ) => {\n if (typeof value === 'number') {\n const dur = value.toFixed(precision ?? 1);\n const metric = description\n ? `${name};dur=${dur};desc=\"${description}\"`\n : `${name};dur=${dur}`;\n headers.push(metric);\n } else {\n const metric = value ? `${name};desc=\"${value}\"` : `${name}`;\n headers.push(metric);\n }\n },\n setTiming: (response: Response) => {\n if (!enabled) return;\n response.headers.append('Server-Timing', headers.join(','));\n response.headers.append('Timing-Allow-Origin', crossOrigin ?? '*');\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,SAAS,OAAO,UAAmB,EAAE,SAAS,KAAK,GAAG;AAC3D,QAAM,EAAE,UAAU,MAAM,YAAY,IAAI;AACxC,MAAI,QAAQ,YAAY,IAAI;AAC5B,QAAM,UAAoB,CAAC;AAC3B,QAAM,SAAS,oBAAI,IAAmB;AAEtC,SAAO;AAAA,IACL,WAAW,CAAC,MAAyB,gBAAyB;AAC5D,aAAO,IAAI,MAAM,EAAE,aAAa,OAAO,YAAY,IAAI,EAAE,CAAC;AAAA,IAC5D;AAAA,IACA,S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|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface Options {
|
|
2
|
+
enabled: boolean;
|
|
3
|
+
crossOrigin?: string;
|
|
4
|
+
}
|
|
5
|
+
declare function timing(options?: Options): {
|
|
6
|
+
startTime: (name: Lowercase<string>, description?: string) => void;
|
|
7
|
+
endTime: (name: Lowercase<string>, precision?: number) => void;
|
|
8
|
+
mark: (name: Lowercase<string>, description?: string, precision?: number) => void;
|
|
9
|
+
setMetric: (name: Lowercase<string>, value: number | string | undefined, description?: string, precision?: number) => void;
|
|
10
|
+
setTiming: (response: Response) => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { type Options, timing };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface Options {
|
|
2
|
+
enabled: boolean;
|
|
3
|
+
crossOrigin?: string;
|
|
4
|
+
}
|
|
5
|
+
declare function timing(options?: Options): {
|
|
6
|
+
startTime: (name: Lowercase<string>, description?: string) => void;
|
|
7
|
+
endTime: (name: Lowercase<string>, precision?: number) => void;
|
|
8
|
+
mark: (name: Lowercase<string>, description?: string, precision?: number) => void;
|
|
9
|
+
setMetric: (name: Lowercase<string>, value: number | string | undefined, description?: string, precision?: number) => void;
|
|
10
|
+
setTiming: (response: Response) => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { type Options, timing };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/utils/timing.ts
|
|
2
|
+
function timing(options = { enabled: true }) {
|
|
3
|
+
const { enabled = true, crossOrigin } = options;
|
|
4
|
+
let start = performance.now();
|
|
5
|
+
const headers = [];
|
|
6
|
+
const timers = /* @__PURE__ */ new Map();
|
|
7
|
+
return {
|
|
8
|
+
startTime: (name, description) => {
|
|
9
|
+
timers.set(name, { description, start: performance.now() });
|
|
10
|
+
},
|
|
11
|
+
endTime: (name, precision) => {
|
|
12
|
+
const timer = timers.get(name);
|
|
13
|
+
if (!timer) {
|
|
14
|
+
console.warn(`timing: ${name} not found`);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const { start: start2, description } = timer;
|
|
18
|
+
const dur = (performance.now() - start2).toFixed(precision ?? 1);
|
|
19
|
+
timers.delete(name);
|
|
20
|
+
const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
|
|
21
|
+
headers.push(metric);
|
|
22
|
+
},
|
|
23
|
+
mark: (name, description, precision) => {
|
|
24
|
+
const dur = (performance.now() - start).toFixed(precision ?? 1);
|
|
25
|
+
const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
|
|
26
|
+
headers.push(metric);
|
|
27
|
+
start = performance.now();
|
|
28
|
+
},
|
|
29
|
+
setMetric: (name, value, description, precision) => {
|
|
30
|
+
if (typeof value === "number") {
|
|
31
|
+
const dur = value.toFixed(precision ?? 1);
|
|
32
|
+
const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
|
|
33
|
+
headers.push(metric);
|
|
34
|
+
} else {
|
|
35
|
+
const metric = value ? `${name};desc="${value}"` : `${name}`;
|
|
36
|
+
headers.push(metric);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
setTiming: (response) => {
|
|
40
|
+
if (!enabled) return;
|
|
41
|
+
response.headers.append("Server-Timing", headers.join(","));
|
|
42
|
+
response.headers.append("Timing-Allow-Origin", crossOrigin ?? "*");
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
timing
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=timing.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/timing.ts"],"sourcesContent":["interface Timer {\n description?: string;\n start: number;\n}\n\nexport interface Options {\n enabled: boolean;\n crossOrigin?: string;\n}\n\nexport function timing(options: Options = { enabled: true }) {\n const { enabled = true, crossOrigin } = options;\n let start = performance.now();\n const headers: string[] = [];\n const timers = new Map<string, Timer>();\n\n return {\n startTime: (name: Lowercase<string>, description?: string) => {\n timers.set(name, { description, start: performance.now() });\n },\n endTime: (name: Lowercase<string>, precision?: number) => {\n const timer = timers.get(name);\n if (!timer) {\n console.warn(`timing: ${name} not found`);\n return;\n }\n const { start, description } = timer;\n const dur = (performance.now() - start).toFixed(precision ?? 1);\n timers.delete(name);\n const metric = description\n ? `${name};dur=${dur};desc=\"${description}\"`\n : `${name};dur=${dur}`;\n headers.push(metric);\n },\n mark: (name: Lowercase<string>, description?: string, precision?: number) => {\n const dur = (performance.now() - start).toFixed(precision ?? 1);\n const metric = description\n ? `${name};dur=${dur};desc=\"${description}\"`\n : `${name};dur=${dur}`;\n headers.push(metric);\n start = performance.now();\n },\n setMetric: (\n name: Lowercase<string>,\n value: number | string | undefined,\n description?: string,\n precision?: number\n ) => {\n if (typeof value === 'number') {\n const dur = value.toFixed(precision ?? 1);\n const metric = description\n ? `${name};dur=${dur};desc=\"${description}\"`\n : `${name};dur=${dur}`;\n headers.push(metric);\n } else {\n const metric = value ? `${name};desc=\"${value}\"` : `${name}`;\n headers.push(metric);\n }\n },\n setTiming: (response: Response) => {\n if (!enabled) return;\n response.headers.append('Server-Timing', headers.join(','));\n response.headers.append('Timing-Allow-Origin', crossOrigin ?? '*');\n },\n };\n}\n"],"mappings":";AAUO,SAAS,OAAO,UAAmB,EAAE,SAAS,KAAK,GAAG;AAC3D,QAAM,EAAE,UAAU,MAAM,YAAY,IAAI;AACxC,MAAI,QAAQ,YAAY,IAAI;AAC5B,QAAM,UAAoB,CAAC;AAC3B,QAAM,SAAS,oBAAI,IAAmB;AAEtC,SAAO;AAAA,IACL,WAAW,CAAC,MAAyB,gBAAyB;AAC5D,aAAO,IAAI,MAAM,EAAE,aAAa,OAAO,YAAY,IAAI,EAAE,CAAC;AAAA,IAC5D;AAAA,IACA,SAAS,CAAC,MAAyB,cAAuB;AACxD,YAAM,QAAQ,OAAO,IAAI,IAAI;AAC7B,UAAI,CAAC,OAAO;AACV,gBAAQ,KAAK,WAAW,I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42
|
}
|
|
43
|
-
|
|
43
|
+
function verifyStandardWebhook(headers, payload, secret) {
|
|
44
44
|
const webhookId = headers["webhook-id"];
|
|
45
45
|
const webhookTimestamp = headers["webhook-timestamp"];
|
|
46
46
|
const webhookSignature = headers["webhook-signature"];
|
package/dist/webhook.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/webhook.ts"],"sourcesContent":["import { createHmac, timingSafeEqual } from 'crypto';\nimport { Status } from './status';\n\nconst WEBHOOK_TOLERANCE_IN_SECONDS = 5 * 60; // 5 minutes\n\nfunction verifyTimestamp(webhookTimestamp: string) {\n const now = Math.floor(Date.now() / 1000);\n const timestamp = parseInt(webhookTimestamp, 10);\n if (isNaN(timestamp)) {\n throw Status.invalidArgument('invalid webhook timestamp').error();\n }\n if (timestamp < now - WEBHOOK_TOLERANCE_IN_SECONDS) {\n throw Status.invalidArgument('webhook timestamp is too old').error();\n }\n if (timestamp > now + WEBHOOK_TOLERANCE_IN_SECONDS) {\n throw Status.invalidArgument('webhook timestamp is too new').error();\n }\n return timestamp;\n}\n\n/**\n * reference: https://github.com/standard-webhooks/standard-webhooks/tree/main/libraries/javascript\n * hono usage:\n * ```ts\n * const webhook =
|
|
1
|
+
{"version":3,"sources":["../src/webhook.ts"],"sourcesContent":["import { createHmac, timingSafeEqual } from 'crypto';\nimport { Status } from './status';\n\nconst WEBHOOK_TOLERANCE_IN_SECONDS = 5 * 60; // 5 minutes\n\nfunction verifyTimestamp(webhookTimestamp: string) {\n const now = Math.floor(Date.now() / 1000);\n const timestamp = parseInt(webhookTimestamp, 10);\n if (isNaN(timestamp)) {\n throw Status.invalidArgument('invalid webhook timestamp').error();\n }\n if (timestamp < now - WEBHOOK_TOLERANCE_IN_SECONDS) {\n throw Status.invalidArgument('webhook timestamp is too old').error();\n }\n if (timestamp > now + WEBHOOK_TOLERANCE_IN_SECONDS) {\n throw Status.invalidArgument('webhook timestamp is too new').error();\n }\n return timestamp;\n}\n\n/**\n * reference: https://github.com/standard-webhooks/standard-webhooks/tree/main/libraries/javascript\n * hono usage:\n * ```ts\n * const webhook = verifyStandardWebhook(c.req.header(), await c.req.text(), 'secret');\n * ```\n */\nexport function verifyStandardWebhook<T = unknown>(\n headers: Record<string, string>,\n payload: string,\n secret: string\n): T {\n const webhookId = headers['webhook-id'];\n const webhookTimestamp = headers['webhook-timestamp'];\n const webhookSignature = headers['webhook-signature'];\n if (!webhookId || !webhookTimestamp || !webhookSignature) {\n throw Status.invalidArgument('invalid webhook').error();\n }\n const timestamp = verifyTimestamp(webhookTimestamp);\n\n const encoder = new TextEncoder();\n const toSign = encoder.encode(`${webhookId}.${timestamp}.${payload}`);\n const hmac = createHmac('sha256', Buffer.from(secret, 'base64'));\n const digest = hmac.update(toSign).digest();\n\n const computedSignature = `v1,${Buffer.from(digest).toString('base64')}`;\n const expectedSignature = computedSignature.split(',')[1];\n const passedSignatures = webhookSignature.split(' ');\n\n for (const versionedSignature of passedSignatures) {\n const [version, signature] = versionedSignature.split(',');\n if (version !== 'v1') continue;\n if (timingSafeEqual(encoder.encode(signature), encoder.encode(expectedSignature))) {\n try {\n return JSON.parse(payload) as T;\n } catch (_) {\n console.error('invalid payload', payload);\n throw Status.invalidArgument('invalid webhook payload').error();\n }\n }\n }\n console.error('webhook verification failed');\n throw Status.invalidArgument('invalid webhook signature').error();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA4C;AAC5C,oBAAuB;AAEvB,IAAM,+BAA+B,IAAI;AAEzC,SAAS,gBAAgB,kBAA0B;AACjD,QAAM,MAAM,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AACxC,QAAM,YAAY,SAAS,kBAAkB,EAAE;AAC/C,MAAI,MAAM,SAAS,GAAG;AACpB,UAAM,qBAAO,gBAAgB,2BAA2B,EAAE,MAAM;AAAA,EAClE;AACA,MAAI,YAAY,MAAM,8BAA8B;AAClD,UAAM,qBAA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BAAgB,2BAA2B,EAAE,MAAM;AAClE;","names":[]}
|
package/dist/webhook.d.cts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* reference: https://github.com/standard-webhooks/standard-webhooks/tree/main/libraries/javascript
|
|
3
3
|
* hono usage:
|
|
4
4
|
* ```ts
|
|
5
|
-
* const webhook =
|
|
5
|
+
* const webhook = verifyStandardWebhook(c.req.header(), await c.req.text(), 'secret');
|
|
6
6
|
* ```
|
|
7
7
|
*/
|
|
8
|
-
declare function verifyStandardWebhook(headers: Record<string, string>, payload: string, secret: string):
|
|
8
|
+
declare function verifyStandardWebhook<T = unknown>(headers: Record<string, string>, payload: string, secret: string): T;
|
|
9
9
|
|
|
10
10
|
export { verifyStandardWebhook };
|
package/dist/webhook.d.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* reference: https://github.com/standard-webhooks/standard-webhooks/tree/main/libraries/javascript
|
|
3
3
|
* hono usage:
|
|
4
4
|
* ```ts
|
|
5
|
-
* const webhook =
|
|
5
|
+
* const webhook = verifyStandardWebhook(c.req.header(), await c.req.text(), 'secret');
|
|
6
6
|
* ```
|
|
7
7
|
*/
|
|
8
|
-
declare function verifyStandardWebhook(headers: Record<string, string>, payload: string, secret: string):
|
|
8
|
+
declare function verifyStandardWebhook<T = unknown>(headers: Record<string, string>, payload: string, secret: string): T;
|
|
9
9
|
|
|
10
10
|
export { verifyStandardWebhook };
|
package/dist/webhook.mjs
CHANGED
|
@@ -16,7 +16,7 @@ function verifyTimestamp(webhookTimestamp) {
|
|
|
16
16
|
}
|
|
17
17
|
return timestamp;
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
function verifyStandardWebhook(headers, payload, secret) {
|
|
20
20
|
const webhookId = headers["webhook-id"];
|
|
21
21
|
const webhookTimestamp = headers["webhook-timestamp"];
|
|
22
22
|
const webhookSignature = headers["webhook-signature"];
|
package/dist/webhook.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/webhook.ts"],"sourcesContent":["import { createHmac, timingSafeEqual } from 'crypto';\nimport { Status } from './status';\n\nconst WEBHOOK_TOLERANCE_IN_SECONDS = 5 * 60; // 5 minutes\n\nfunction verifyTimestamp(webhookTimestamp: string) {\n const now = Math.floor(Date.now() / 1000);\n const timestamp = parseInt(webhookTimestamp, 10);\n if (isNaN(timestamp)) {\n throw Status.invalidArgument('invalid webhook timestamp').error();\n }\n if (timestamp < now - WEBHOOK_TOLERANCE_IN_SECONDS) {\n throw Status.invalidArgument('webhook timestamp is too old').error();\n }\n if (timestamp > now + WEBHOOK_TOLERANCE_IN_SECONDS) {\n throw Status.invalidArgument('webhook timestamp is too new').error();\n }\n return timestamp;\n}\n\n/**\n * reference: https://github.com/standard-webhooks/standard-webhooks/tree/main/libraries/javascript\n * hono usage:\n * ```ts\n * const webhook =
|
|
1
|
+
{"version":3,"sources":["../src/webhook.ts"],"sourcesContent":["import { createHmac, timingSafeEqual } from 'crypto';\nimport { Status } from './status';\n\nconst WEBHOOK_TOLERANCE_IN_SECONDS = 5 * 60; // 5 minutes\n\nfunction verifyTimestamp(webhookTimestamp: string) {\n const now = Math.floor(Date.now() / 1000);\n const timestamp = parseInt(webhookTimestamp, 10);\n if (isNaN(timestamp)) {\n throw Status.invalidArgument('invalid webhook timestamp').error();\n }\n if (timestamp < now - WEBHOOK_TOLERANCE_IN_SECONDS) {\n throw Status.invalidArgument('webhook timestamp is too old').error();\n }\n if (timestamp > now + WEBHOOK_TOLERANCE_IN_SECONDS) {\n throw Status.invalidArgument('webhook timestamp is too new').error();\n }\n return timestamp;\n}\n\n/**\n * reference: https://github.com/standard-webhooks/standard-webhooks/tree/main/libraries/javascript\n * hono usage:\n * ```ts\n * const webhook = verifyStandardWebhook(c.req.header(), await c.req.text(), 'secret');\n * ```\n */\nexport function verifyStandardWebhook<T = unknown>(\n headers: Record<string, string>,\n payload: string,\n secret: string\n): T {\n const webhookId = headers['webhook-id'];\n const webhookTimestamp = headers['webhook-timestamp'];\n const webhookSignature = headers['webhook-signature'];\n if (!webhookId || !webhookTimestamp || !webhookSignature) {\n throw Status.invalidArgument('invalid webhook').error();\n }\n const timestamp = verifyTimestamp(webhookTimestamp);\n\n const encoder = new TextEncoder();\n const toSign = encoder.encode(`${webhookId}.${timestamp}.${payload}`);\n const hmac = createHmac('sha256', Buffer.from(secret, 'base64'));\n const digest = hmac.update(toSign).digest();\n\n const computedSignature = `v1,${Buffer.from(digest).toString('base64')}`;\n const expectedSignature = computedSignature.split(',')[1];\n const passedSignatures = webhookSignature.split(' ');\n\n for (const versionedSignature of passedSignatures) {\n const [version, signature] = versionedSignature.split(',');\n if (version !== 'v1') continue;\n if (timingSafeEqual(encoder.encode(signature), encoder.encode(expectedSignature))) {\n try {\n return JSON.parse(payload) as T;\n } catch (_) {\n console.error('invalid payload', payload);\n throw Status.invalidArgument('invalid webhook payload').error();\n }\n }\n }\n console.error('webhook verification failed');\n throw Status.invalidArgument('invalid webhook signature').error();\n}\n"],"mappings":";AAAA,SAAS,YAAY,uBAAuB;AAC5C,SAAS,cAAc;AAEvB,IAAM,+BAA+B,IAAI;AAEzC,SAAS,gBAAgB,kBAA0B;AACjD,QAAM,MAAM,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AACxC,QAAM,YAAY,SAAS,kBAAkB,EAAE;AAC/C,MAAI,MAAM,SAAS,GAAG;AACpB,UAAM,OAAO,gBAAgB,2BAA2B,EAAE,MAAM;AAAA,EAClE;AACA,MAAI,YAAY,MAAM,8BAA8B;AAClD,UAAM,OAAO,gBAAgB,8BAA8B,EAAE,MAAM;AAAA,EACrE;AACA,MAAI,YAAY,MAAM,8BAA8B;AAClD,UAAM,OAAO,gBAAgB,8BAA8B,EAAE,MAAM;AAAA,EACrE;AACA,SAAO;AACT;AASO,SAAS,sBACd,SACA,SACA,QACG;AACH,QAAM,YAAY,QAAQ,YAAY;AACtC,QAAM,mBAAmB,QAAQ,mBAAmB;AACpD,QAAM,mBAAmB,Q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