@andpipes/widget-sdk 0.1.0

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.
@@ -0,0 +1,14 @@
1
+ /** Widget Host query tokenの共有wire byte契約。Node/browser双方で使える純粋関数だけを置く。 */
2
+ declare const WIDGET_RENDER_TOKEN_MAX_BYTES: number;
3
+ interface WidgetRenderPayloadSizeInput {
4
+ template: string;
5
+ version: string;
6
+ structuredContent: unknown;
7
+ textFallback?: string;
8
+ issuedAt?: number;
9
+ }
10
+ /** AES-GCM ciphertextはplaintextと同byte長なので、暗号化前に最終token長を決定論的に見積もる。 */
11
+ declare function estimateWidgetRenderTokenBytes(input: WidgetRenderPayloadSizeInput): number;
12
+ declare function isWidgetRenderPayloadWithinTokenLimit(input: WidgetRenderPayloadSizeInput): boolean;
13
+
14
+ export { WIDGET_RENDER_TOKEN_MAX_BYTES as W, type WidgetRenderPayloadSizeInput as a, estimateWidgetRenderTokenBytes as e, isWidgetRenderPayloadWithinTokenLimit as i };
@@ -0,0 +1,14 @@
1
+ /** Widget Host query tokenの共有wire byte契約。Node/browser双方で使える純粋関数だけを置く。 */
2
+ declare const WIDGET_RENDER_TOKEN_MAX_BYTES: number;
3
+ interface WidgetRenderPayloadSizeInput {
4
+ template: string;
5
+ version: string;
6
+ structuredContent: unknown;
7
+ textFallback?: string;
8
+ issuedAt?: number;
9
+ }
10
+ /** AES-GCM ciphertextはplaintextと同byte長なので、暗号化前に最終token長を決定論的に見積もる。 */
11
+ declare function estimateWidgetRenderTokenBytes(input: WidgetRenderPayloadSizeInput): number;
12
+ declare function isWidgetRenderPayloadWithinTokenLimit(input: WidgetRenderPayloadSizeInput): boolean;
13
+
14
+ export { WIDGET_RENDER_TOKEN_MAX_BYTES as W, type WidgetRenderPayloadSizeInput as a, estimateWidgetRenderTokenBytes as e, isWidgetRenderPayloadWithinTokenLimit as i };
@@ -0,0 +1,153 @@
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/render-token.ts
21
+ var render_token_exports = {};
22
+ __export(render_token_exports, {
23
+ WIDGET_RENDER_TOKEN_MAX_BYTES: () => WIDGET_RENDER_TOKEN_MAX_BYTES,
24
+ WIDGET_RENDER_TOKEN_VERSION: () => WIDGET_RENDER_TOKEN_VERSION,
25
+ decodeWidgetRenderPayload: () => decodeWidgetRenderPayload,
26
+ encodeWidgetRenderPayload: () => encodeWidgetRenderPayload
27
+ });
28
+ module.exports = __toCommonJS(render_token_exports);
29
+ var import_node_crypto = require("crypto");
30
+
31
+ // src/render-token-limit.ts
32
+ var WIDGET_RENDER_TOKEN_MAX_BYTES = 64 * 1024;
33
+ var TOKEN_VERSION_BYTES = 2;
34
+ var TOKEN_SEPARATOR_BYTES = 3;
35
+ var IV_BASE64URL_BYTES = 16;
36
+ var TAG_BASE64URL_BYTES = 22;
37
+ var FIXED_TOKEN_BYTES = TOKEN_VERSION_BYTES + TOKEN_SEPARATOR_BYTES + IV_BASE64URL_BYTES + TAG_BASE64URL_BYTES;
38
+ function estimateWidgetRenderTokenBytesFromPlaintextBytes(plaintextBytes) {
39
+ return FIXED_TOKEN_BYTES + Math.ceil(plaintextBytes * 4 / 3);
40
+ }
41
+
42
+ // src/render-token.ts
43
+ var WIDGET_RENDER_TOKEN_VERSION = "v1";
44
+ var AAD = Buffer.from("andpipe-widget-render-payload:v1", "utf8");
45
+ var IV_BYTES = 12;
46
+ var TAG_BYTES = 16;
47
+ function keyFromSecret(secret) {
48
+ const normalized = secret?.trim();
49
+ if (!normalized) {
50
+ return null;
51
+ }
52
+ return (0, import_node_crypto.createHash)("sha256").update(normalized, "utf8").digest();
53
+ }
54
+ function b64url(input) {
55
+ return input.toString("base64url");
56
+ }
57
+ function fromB64url(input) {
58
+ try {
59
+ return Buffer.from(input, "base64url");
60
+ } catch {
61
+ return null;
62
+ }
63
+ }
64
+ function isPayload(input) {
65
+ if (typeof input !== "object" || input === null) {
66
+ return false;
67
+ }
68
+ const value = input;
69
+ return typeof value.template === "string" && typeof value.version === "string" && typeof value.issuedAt === "number" && (value.textFallback === void 0 || typeof value.textFallback === "string") && "structuredContent" in value;
70
+ }
71
+ function sameVersion(input) {
72
+ const a = Buffer.from(input, "utf8");
73
+ const b = Buffer.from(WIDGET_RENDER_TOKEN_VERSION, "utf8");
74
+ return a.length === b.length && (0, import_node_crypto.timingSafeEqual)(a, b);
75
+ }
76
+ function encodeWidgetRenderPayload(input, options) {
77
+ const key = keyFromSecret(options.secret);
78
+ if (key === null) {
79
+ return null;
80
+ }
81
+ const iv = (0, import_node_crypto.randomBytes)(IV_BYTES);
82
+ const cipher = (0, import_node_crypto.createCipheriv)("aes-256-gcm", key, iv, { authTagLength: TAG_BYTES });
83
+ cipher.setAAD(AAD);
84
+ const payload = {
85
+ template: input.template,
86
+ version: input.version,
87
+ structuredContent: input.structuredContent,
88
+ ...input.textFallback ? { textFallback: input.textFallback } : {},
89
+ issuedAt: input.issuedAt ?? Date.now()
90
+ };
91
+ let plaintext;
92
+ try {
93
+ plaintext = Buffer.from(JSON.stringify(payload), "utf8");
94
+ } catch {
95
+ return null;
96
+ }
97
+ if (estimateWidgetRenderTokenBytesFromPlaintextBytes(plaintext.byteLength) > WIDGET_RENDER_TOKEN_MAX_BYTES) {
98
+ return null;
99
+ }
100
+ const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
101
+ const tag = cipher.getAuthTag();
102
+ const token = [WIDGET_RENDER_TOKEN_VERSION, b64url(iv), b64url(tag), b64url(ciphertext)].join(
103
+ "."
104
+ );
105
+ return Buffer.byteLength(token, "utf8") <= WIDGET_RENDER_TOKEN_MAX_BYTES ? token : null;
106
+ }
107
+ function decodeWidgetRenderPayload(token, options) {
108
+ if (typeof token !== "string" || token.length === 0 || Buffer.byteLength(token, "utf8") > WIDGET_RENDER_TOKEN_MAX_BYTES) {
109
+ return null;
110
+ }
111
+ const key = keyFromSecret(options.secret);
112
+ if (key === null) {
113
+ return null;
114
+ }
115
+ const [version, ivPart, tagPart, ciphertextPart, ...extra] = token.split(".");
116
+ if (version === void 0 || ivPart === void 0 || tagPart === void 0 || ciphertextPart === void 0 || extra.length > 0 || !sameVersion(version)) {
117
+ return null;
118
+ }
119
+ const iv = fromB64url(ivPart);
120
+ const tag = fromB64url(tagPart);
121
+ const ciphertext = fromB64url(ciphertextPart);
122
+ if (iv?.length !== IV_BYTES || tag?.length !== TAG_BYTES || ciphertext === null) {
123
+ return null;
124
+ }
125
+ try {
126
+ const decipher = (0, import_node_crypto.createDecipheriv)("aes-256-gcm", key, iv, { authTagLength: TAG_BYTES });
127
+ decipher.setAAD(AAD);
128
+ decipher.setAuthTag(tag);
129
+ const plaintext = Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString(
130
+ "utf8"
131
+ );
132
+ const parsed = JSON.parse(plaintext);
133
+ if (!isPayload(parsed)) {
134
+ return null;
135
+ }
136
+ if (typeof options.maxAgeMs === "number") {
137
+ const ageMs = Date.now() - parsed.issuedAt;
138
+ if (ageMs < -6e4 || ageMs > options.maxAgeMs) {
139
+ return null;
140
+ }
141
+ }
142
+ return parsed;
143
+ } catch {
144
+ return null;
145
+ }
146
+ }
147
+ // Annotate the CommonJS export names for ESM import in node:
148
+ 0 && (module.exports = {
149
+ WIDGET_RENDER_TOKEN_MAX_BYTES,
150
+ WIDGET_RENDER_TOKEN_VERSION,
151
+ decodeWidgetRenderPayload,
152
+ encodeWidgetRenderPayload
153
+ });
@@ -0,0 +1,25 @@
1
+ export { W as WIDGET_RENDER_TOKEN_MAX_BYTES } from './render-token-limit-5gpQxyG-.cjs';
2
+
3
+ declare const WIDGET_RENDER_TOKEN_VERSION: "v1";
4
+ interface WidgetRenderPayload {
5
+ template: string;
6
+ version: string;
7
+ structuredContent: unknown;
8
+ textFallback?: string;
9
+ issuedAt: number;
10
+ }
11
+ interface WidgetRenderTokenOptions {
12
+ secret: string | undefined;
13
+ maxAgeMs?: number;
14
+ }
15
+ interface EncodeWidgetRenderPayloadInput {
16
+ template: string;
17
+ version: string;
18
+ structuredContent: unknown;
19
+ textFallback?: string;
20
+ issuedAt?: number;
21
+ }
22
+ declare function encodeWidgetRenderPayload(input: EncodeWidgetRenderPayloadInput, options: WidgetRenderTokenOptions): string | null;
23
+ declare function decodeWidgetRenderPayload(token: string | string[] | undefined, options: WidgetRenderTokenOptions): WidgetRenderPayload | null;
24
+
25
+ export { type EncodeWidgetRenderPayloadInput, WIDGET_RENDER_TOKEN_VERSION, type WidgetRenderPayload, type WidgetRenderTokenOptions, decodeWidgetRenderPayload, encodeWidgetRenderPayload };
@@ -0,0 +1,25 @@
1
+ export { W as WIDGET_RENDER_TOKEN_MAX_BYTES } from './render-token-limit-5gpQxyG-.js';
2
+
3
+ declare const WIDGET_RENDER_TOKEN_VERSION: "v1";
4
+ interface WidgetRenderPayload {
5
+ template: string;
6
+ version: string;
7
+ structuredContent: unknown;
8
+ textFallback?: string;
9
+ issuedAt: number;
10
+ }
11
+ interface WidgetRenderTokenOptions {
12
+ secret: string | undefined;
13
+ maxAgeMs?: number;
14
+ }
15
+ interface EncodeWidgetRenderPayloadInput {
16
+ template: string;
17
+ version: string;
18
+ structuredContent: unknown;
19
+ textFallback?: string;
20
+ issuedAt?: number;
21
+ }
22
+ declare function encodeWidgetRenderPayload(input: EncodeWidgetRenderPayloadInput, options: WidgetRenderTokenOptions): string | null;
23
+ declare function decodeWidgetRenderPayload(token: string | string[] | undefined, options: WidgetRenderTokenOptions): WidgetRenderPayload | null;
24
+
25
+ export { type EncodeWidgetRenderPayloadInput, WIDGET_RENDER_TOKEN_VERSION, type WidgetRenderPayload, type WidgetRenderTokenOptions, decodeWidgetRenderPayload, encodeWidgetRenderPayload };
@@ -0,0 +1,123 @@
1
+ import {
2
+ WIDGET_RENDER_TOKEN_MAX_BYTES,
3
+ estimateWidgetRenderTokenBytesFromPlaintextBytes
4
+ } from "./chunk-GG2BTLFR.js";
5
+
6
+ // src/render-token.ts
7
+ import {
8
+ createCipheriv,
9
+ createDecipheriv,
10
+ createHash,
11
+ randomBytes,
12
+ timingSafeEqual
13
+ } from "crypto";
14
+ var WIDGET_RENDER_TOKEN_VERSION = "v1";
15
+ var AAD = Buffer.from("andpipe-widget-render-payload:v1", "utf8");
16
+ var IV_BYTES = 12;
17
+ var TAG_BYTES = 16;
18
+ function keyFromSecret(secret) {
19
+ const normalized = secret?.trim();
20
+ if (!normalized) {
21
+ return null;
22
+ }
23
+ return createHash("sha256").update(normalized, "utf8").digest();
24
+ }
25
+ function b64url(input) {
26
+ return input.toString("base64url");
27
+ }
28
+ function fromB64url(input) {
29
+ try {
30
+ return Buffer.from(input, "base64url");
31
+ } catch {
32
+ return null;
33
+ }
34
+ }
35
+ function isPayload(input) {
36
+ if (typeof input !== "object" || input === null) {
37
+ return false;
38
+ }
39
+ const value = input;
40
+ return typeof value.template === "string" && typeof value.version === "string" && typeof value.issuedAt === "number" && (value.textFallback === void 0 || typeof value.textFallback === "string") && "structuredContent" in value;
41
+ }
42
+ function sameVersion(input) {
43
+ const a = Buffer.from(input, "utf8");
44
+ const b = Buffer.from(WIDGET_RENDER_TOKEN_VERSION, "utf8");
45
+ return a.length === b.length && timingSafeEqual(a, b);
46
+ }
47
+ function encodeWidgetRenderPayload(input, options) {
48
+ const key = keyFromSecret(options.secret);
49
+ if (key === null) {
50
+ return null;
51
+ }
52
+ const iv = randomBytes(IV_BYTES);
53
+ const cipher = createCipheriv("aes-256-gcm", key, iv, { authTagLength: TAG_BYTES });
54
+ cipher.setAAD(AAD);
55
+ const payload = {
56
+ template: input.template,
57
+ version: input.version,
58
+ structuredContent: input.structuredContent,
59
+ ...input.textFallback ? { textFallback: input.textFallback } : {},
60
+ issuedAt: input.issuedAt ?? Date.now()
61
+ };
62
+ let plaintext;
63
+ try {
64
+ plaintext = Buffer.from(JSON.stringify(payload), "utf8");
65
+ } catch {
66
+ return null;
67
+ }
68
+ if (estimateWidgetRenderTokenBytesFromPlaintextBytes(plaintext.byteLength) > WIDGET_RENDER_TOKEN_MAX_BYTES) {
69
+ return null;
70
+ }
71
+ const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
72
+ const tag = cipher.getAuthTag();
73
+ const token = [WIDGET_RENDER_TOKEN_VERSION, b64url(iv), b64url(tag), b64url(ciphertext)].join(
74
+ "."
75
+ );
76
+ return Buffer.byteLength(token, "utf8") <= WIDGET_RENDER_TOKEN_MAX_BYTES ? token : null;
77
+ }
78
+ function decodeWidgetRenderPayload(token, options) {
79
+ if (typeof token !== "string" || token.length === 0 || Buffer.byteLength(token, "utf8") > WIDGET_RENDER_TOKEN_MAX_BYTES) {
80
+ return null;
81
+ }
82
+ const key = keyFromSecret(options.secret);
83
+ if (key === null) {
84
+ return null;
85
+ }
86
+ const [version, ivPart, tagPart, ciphertextPart, ...extra] = token.split(".");
87
+ if (version === void 0 || ivPart === void 0 || tagPart === void 0 || ciphertextPart === void 0 || extra.length > 0 || !sameVersion(version)) {
88
+ return null;
89
+ }
90
+ const iv = fromB64url(ivPart);
91
+ const tag = fromB64url(tagPart);
92
+ const ciphertext = fromB64url(ciphertextPart);
93
+ if (iv?.length !== IV_BYTES || tag?.length !== TAG_BYTES || ciphertext === null) {
94
+ return null;
95
+ }
96
+ try {
97
+ const decipher = createDecipheriv("aes-256-gcm", key, iv, { authTagLength: TAG_BYTES });
98
+ decipher.setAAD(AAD);
99
+ decipher.setAuthTag(tag);
100
+ const plaintext = Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString(
101
+ "utf8"
102
+ );
103
+ const parsed = JSON.parse(plaintext);
104
+ if (!isPayload(parsed)) {
105
+ return null;
106
+ }
107
+ if (typeof options.maxAgeMs === "number") {
108
+ const ageMs = Date.now() - parsed.issuedAt;
109
+ if (ageMs < -6e4 || ageMs > options.maxAgeMs) {
110
+ return null;
111
+ }
112
+ }
113
+ return parsed;
114
+ } catch {
115
+ return null;
116
+ }
117
+ }
118
+ export {
119
+ WIDGET_RENDER_TOKEN_MAX_BYTES,
120
+ WIDGET_RENDER_TOKEN_VERSION,
121
+ decodeWidgetRenderPayload,
122
+ encodeWidgetRenderPayload
123
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@andpipes/widget-sdk",
3
+ "version": "0.1.0",
4
+ "license": "Apache-2.0",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ },
14
+ "./render-token": {
15
+ "types": "./dist/render-token.d.ts",
16
+ "import": "./dist/render-token.js",
17
+ "require": "./dist/render-token.cjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "dependencies": {
27
+ "ajv": "^8.20.0",
28
+ "ajv-formats": "^3.0.1",
29
+ "@andpipes/localization": "0.1.0"
30
+ },
31
+ "scripts": {
32
+ "lint": "biome check .",
33
+ "typecheck": "tsc --noEmit",
34
+ "test": "vitest run --passWithNoTests",
35
+ "build": "tsup src/index.ts src/render-token.ts --format esm,cjs --dts --clean"
36
+ }
37
+ }