@arcblock/did-connect-js 1.29.13 → 1.29.14

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,41 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ let cors = require("cors");
3
+ cors = require_rolldown_runtime.__toESM(cors);
4
+
5
+ //#region src/adapters/express.ts
6
+ /**
7
+ * Register DID Connect routes on an Express-compatible app.
8
+ *
9
+ * Routes registered:
10
+ * - GET/POST `{prefix}/{action}/token` — generate session
11
+ * - GET `{prefix}/{action}/status` — check session status
12
+ * - GET `{prefix}/{action}/timeout` — expire session
13
+ * - GET `{prefix}/{action}/auth` — wallet fetches auth request
14
+ * - POST `{prefix}/{action}/auth` — wallet submits auth response
15
+ * - GET `{prefix}/{action}/auth/submit` — web wallet submit
16
+ */
17
+ function attachExpress({ app, prefix, action, handlers }) {
18
+ const pathname = `${prefix}/${action}/auth`;
19
+ const { generateSession, checkSession, expireSession, onAuthRequest, onAuthResponse, ensureContext, ensureSignedJson } = handlers;
20
+ app.use(`${prefix}/${action}`, (0, cors.default)({
21
+ origin: "*",
22
+ optionsSuccessStatus: 204
23
+ }));
24
+ app.get(`${prefix}/${action}/token`, generateSession);
25
+ app.post(`${prefix}/${action}/token`, generateSession);
26
+ app.get(`${prefix}/${action}/status`, ensureContext, checkSession);
27
+ app.get(`${prefix}/${action}/timeout`, ensureContext, expireSession);
28
+ app.get(pathname, ensureContext, ensureSignedJson, onAuthRequest);
29
+ app.post(pathname, ensureContext, ensureSignedJson, onAuthResponse);
30
+ app.get(`${pathname}/submit`, ensureContext, ensureSignedJson, onAuthResponse);
31
+ return {
32
+ generateSession,
33
+ expireSession,
34
+ checkSession,
35
+ onAuthRequest,
36
+ onAuthResponse
37
+ };
38
+ }
39
+
40
+ //#endregion
41
+ exports.attachExpress = attachExpress;
@@ -0,0 +1,48 @@
1
+ import { ConnectRequest, ConnectResponse, NextFunction } from "../types.cjs";
2
+
3
+ //#region src/adapters/express.d.ts
4
+ interface ExpressLikeApp {
5
+ use(path: string, ...handlers: any[]): void;
6
+ get(path: string, ...handlers: any[]): void;
7
+ post(path: string, ...handlers: any[]): void;
8
+ }
9
+ interface HandlerFunctions {
10
+ generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
11
+ checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
12
+ expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
13
+ onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
14
+ onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
15
+ ensureContext: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => Promise<void>;
16
+ ensureSignedJson: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => void;
17
+ }
18
+ interface AttachExpressOptions {
19
+ app: ExpressLikeApp;
20
+ prefix: string;
21
+ action: string;
22
+ handlers: HandlerFunctions;
23
+ }
24
+ /**
25
+ * Register DID Connect routes on an Express-compatible app.
26
+ *
27
+ * Routes registered:
28
+ * - GET/POST `{prefix}/{action}/token` — generate session
29
+ * - GET `{prefix}/{action}/status` — check session status
30
+ * - GET `{prefix}/{action}/timeout` — expire session
31
+ * - GET `{prefix}/{action}/auth` — wallet fetches auth request
32
+ * - POST `{prefix}/{action}/auth` — wallet submits auth response
33
+ * - GET `{prefix}/{action}/auth/submit` — web wallet submit
34
+ */
35
+ declare function attachExpress({
36
+ app,
37
+ prefix,
38
+ action,
39
+ handlers
40
+ }: AttachExpressOptions): {
41
+ generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
42
+ expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
43
+ checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
44
+ onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
45
+ onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
46
+ };
47
+ //#endregion
48
+ export { AttachExpressOptions, ExpressLikeApp, HandlerFunctions, attachExpress };
@@ -0,0 +1,167 @@
1
+
2
+ //#region src/adapters/hono.ts
3
+ /**
4
+ * Parse a raw Cookie header string into a key-value map.
5
+ */
6
+ function parseCookieHeader(header) {
7
+ const cookies = {};
8
+ if (!header) return cookies;
9
+ for (const pair of header.split(";")) {
10
+ const idx = pair.indexOf("=");
11
+ if (idx > 0) {
12
+ const key = pair.slice(0, idx).trim();
13
+ const val = pair.slice(idx + 1).trim();
14
+ cookies[key] = decodeURIComponent(val);
15
+ }
16
+ }
17
+ return cookies;
18
+ }
19
+ /**
20
+ * Simple Accept-Language negotiation matching Express's req.acceptsLanguages().
21
+ * Only needs to handle the two languages used by DID Connect: 'en-US' and 'zh-CN'.
22
+ */
23
+ function negotiateLanguage(acceptHeader, ...langs) {
24
+ if (!acceptHeader || langs.length === 0) return langs[0] || false;
25
+ const lower = acceptHeader.toLowerCase();
26
+ for (const lang of langs) {
27
+ const prefix = lang.toLowerCase().split("-")[0];
28
+ if (lower.includes(lang.toLowerCase()) || lower.includes(prefix)) return lang;
29
+ }
30
+ return langs[0] || false;
31
+ }
32
+ /**
33
+ * Create a ConnectRequest from a Hono-like Context.
34
+ *
35
+ * @param c - Hono Context object
36
+ * @param bodyCache - Pre-parsed request body (since c.req.json() is async)
37
+ */
38
+ function createHonoRequest(c, bodyCache = {}) {
39
+ const url = new URL(c.req.url);
40
+ const rawHeaders = {};
41
+ if (c.req.raw?.headers) c.req.raw.headers.forEach((value, key) => {
42
+ rawHeaders[key.toLowerCase()] = value;
43
+ });
44
+ return {
45
+ body: bodyCache,
46
+ query: Object.fromEntries(url.searchParams.entries()),
47
+ params: typeof c.req.param === "function" ? c.req.param() : {},
48
+ headers: rawHeaders,
49
+ cookies: parseCookieHeader(rawHeaders.cookie || ""),
50
+ protocol: url.protocol.replace(":", ""),
51
+ originalUrl: url.pathname + url.search,
52
+ get(name) {
53
+ return rawHeaders[name.toLowerCase()];
54
+ },
55
+ acceptsLanguages(...langs) {
56
+ return negotiateLanguage(rawHeaders["accept-language"] || "", ...langs);
57
+ },
58
+ raw: c
59
+ };
60
+ }
61
+ /**
62
+ * Create a ConnectResponse that buffers the handler's response.
63
+ * The Hono route wrapper reads the buffer via _getResult() to return c.json().
64
+ */
65
+ function createHonoResponse() {
66
+ let result = null;
67
+ return {
68
+ jsonp(data) {
69
+ result = {
70
+ statusCode: 200,
71
+ body: data
72
+ };
73
+ },
74
+ json(data) {
75
+ result = {
76
+ statusCode: 200,
77
+ body: data
78
+ };
79
+ },
80
+ status(code) {
81
+ return { json(data) {
82
+ result = {
83
+ statusCode: code,
84
+ body: data
85
+ };
86
+ } };
87
+ },
88
+ _getResult() {
89
+ return result;
90
+ }
91
+ };
92
+ }
93
+ /**
94
+ * Wrap a middleware chain + handler into a single Hono route handler.
95
+ * Runs middlewares sequentially with manual next() control,
96
+ * then runs the handler, then returns the buffered response.
97
+ */
98
+ function wrapHandler(middlewares, handler) {
99
+ return async (c) => {
100
+ let body = {};
101
+ try {
102
+ const text = await c.req.text();
103
+ if (text) {
104
+ const contentType = c.req.header("content-type") || "";
105
+ if (contentType.includes("json")) body = JSON.parse(text);
106
+ else if (contentType.includes("urlencoded")) body = Object.fromEntries(new URLSearchParams(text).entries());
107
+ }
108
+ } catch {}
109
+ const req = createHonoRequest(c, body);
110
+ const res = createHonoResponse();
111
+ for (const mw of middlewares) {
112
+ let nextCalled = false;
113
+ await mw(req, res, () => {
114
+ nextCalled = true;
115
+ });
116
+ if (!nextCalled) {
117
+ const r$1 = res._getResult();
118
+ if (r$1) return c.json(r$1.body, r$1.statusCode);
119
+ return c.json({ error: "Unknown error" }, 500);
120
+ }
121
+ }
122
+ await handler(req, res);
123
+ const r = res._getResult();
124
+ return c.json(r?.body ?? {}, r?.statusCode ?? 200);
125
+ };
126
+ }
127
+ /**
128
+ * Register DID Connect routes on a Hono-compatible app.
129
+ *
130
+ * Routes registered are the same as attachExpress:
131
+ * - GET/POST `{prefix}/{action}/token`
132
+ * - GET `{prefix}/{action}/status`
133
+ * - GET `{prefix}/{action}/timeout`
134
+ * - GET `{prefix}/{action}/auth`
135
+ * - POST `{prefix}/{action}/auth`
136
+ * - GET `{prefix}/{action}/auth/submit`
137
+ */
138
+ function attachHono({ app, prefix, action, handlers }) {
139
+ const pathname = `${prefix}/${action}/auth`;
140
+ const { generateSession, checkSession, expireSession, onAuthRequest, onAuthResponse, ensureContext, ensureSignedJson } = handlers;
141
+ app.use(`${prefix}/${action}/*`, async (c, next) => {
142
+ c.header("Access-Control-Allow-Origin", "*");
143
+ c.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
144
+ c.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
145
+ if (c.req.method === "OPTIONS") return c.text("", 204);
146
+ await next();
147
+ });
148
+ app.get(`${prefix}/${action}/token`, wrapHandler([], generateSession));
149
+ app.post(`${prefix}/${action}/token`, wrapHandler([], generateSession));
150
+ app.get(`${prefix}/${action}/status`, wrapHandler([ensureContext], checkSession));
151
+ app.get(`${prefix}/${action}/timeout`, wrapHandler([ensureContext], expireSession));
152
+ app.get(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthRequest));
153
+ app.post(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
154
+ app.get(`${pathname}/submit`, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
155
+ return {
156
+ generateSession,
157
+ expireSession,
158
+ checkSession,
159
+ onAuthRequest,
160
+ onAuthResponse
161
+ };
162
+ }
163
+
164
+ //#endregion
165
+ exports.attachHono = attachHono;
166
+ exports.createHonoRequest = createHonoRequest;
167
+ exports.createHonoResponse = createHonoResponse;
@@ -0,0 +1,57 @@
1
+ import { ConnectRequest, ConnectResponse } from "../types.cjs";
2
+ import { HandlerFunctions } from "./express.cjs";
3
+
4
+ //#region src/adapters/hono.d.ts
5
+ interface HonoLikeApp {
6
+ get(path: string, handler: (c: any) => any): void;
7
+ post(path: string, handler: (c: any) => any): void;
8
+ use(path: string, handler: any): void;
9
+ }
10
+ interface AttachHonoOptions {
11
+ app: HonoLikeApp;
12
+ prefix: string;
13
+ action: string;
14
+ handlers: HandlerFunctions;
15
+ }
16
+ /**
17
+ * Create a ConnectRequest from a Hono-like Context.
18
+ *
19
+ * @param c - Hono Context object
20
+ * @param bodyCache - Pre-parsed request body (since c.req.json() is async)
21
+ */
22
+ declare function createHonoRequest(c: any, bodyCache?: any): ConnectRequest;
23
+ /**
24
+ * Create a ConnectResponse that buffers the handler's response.
25
+ * The Hono route wrapper reads the buffer via _getResult() to return c.json().
26
+ */
27
+ declare function createHonoResponse(): ConnectResponse & {
28
+ _getResult(): {
29
+ statusCode: number;
30
+ body: any;
31
+ } | null;
32
+ };
33
+ /**
34
+ * Register DID Connect routes on a Hono-compatible app.
35
+ *
36
+ * Routes registered are the same as attachExpress:
37
+ * - GET/POST `{prefix}/{action}/token`
38
+ * - GET `{prefix}/{action}/status`
39
+ * - GET `{prefix}/{action}/timeout`
40
+ * - GET `{prefix}/{action}/auth`
41
+ * - POST `{prefix}/{action}/auth`
42
+ * - GET `{prefix}/{action}/auth/submit`
43
+ */
44
+ declare function attachHono({
45
+ app,
46
+ prefix,
47
+ action,
48
+ handlers
49
+ }: AttachHonoOptions): {
50
+ generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
51
+ expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
52
+ checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
53
+ onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
54
+ onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
55
+ };
56
+ //#endregion
57
+ export { AttachHonoOptions, HonoLikeApp, attachHono, createHonoRequest, createHonoResponse };
@@ -1,7 +1,9 @@
1
+ import { ConnectRequest, ConnectResponse, NextFunction } from "../types.cjs";
2
+
1
3
  //#region src/handlers/util.d.ts
2
4
  declare const errors: Record<string, Record<string, string>>;
3
- declare const preparePathname: (path: string, req: any) => string;
4
- declare const prepareBaseUrl: (req: any, params?: Record<string, any>) => string;
5
+ declare const preparePathname: (path: string, req: ConnectRequest) => string;
6
+ declare const prepareBaseUrl: (req: ConnectRequest, params?: Record<string, any>) => string;
5
7
  declare const getStepChallenge: () => any;
6
8
  declare const parseWalletUA: (userAgent: string) => {
7
9
  os: string;
@@ -26,8 +28,8 @@ interface CreateHandlersOptions {
26
28
  authenticator: any;
27
29
  authPrincipal: any;
28
30
  persistentDynamicClaims?: boolean;
29
- getSignParams?: (req: any) => any;
30
- getPathName?: (pathname: string, req: any) => string;
31
+ getSignParams?: (req: ConnectRequest) => any;
32
+ getPathName?: (pathname: string, req: ConnectRequest) => string;
31
33
  options: {
32
34
  tokenKey: string;
33
35
  encKey: string;
@@ -55,13 +57,13 @@ declare function createHandlers({
55
57
  getPathName,
56
58
  options
57
59
  }: CreateHandlersOptions): {
58
- generateSession: (req: any, res: any) => Promise<void>;
59
- expireSession: (req: any, res: any) => Promise<void>;
60
- checkSession: (req: any, res: any) => Promise<void>;
61
- onAuthRequest: (req: any, res: any) => Promise<any>;
62
- onAuthResponse: (req: any, res: any) => Promise<any>;
63
- ensureContext: (req: any, _res: any, next: () => void) => Promise<void>;
64
- ensureSignedJson: (req: any, res: any, next: () => void) => any;
60
+ generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
61
+ expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
62
+ checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
63
+ onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
64
+ onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
65
+ ensureContext: (req: ConnectRequest, _res: ConnectResponse, next: NextFunction) => Promise<void>;
66
+ ensureSignedJson: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => void;
65
67
  createExtraParams: (locale: string, params: any, extra?: any) => any;
66
68
  };
67
69
  //#endregion
@@ -1,10 +1,10 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
- const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
2
  const require_package$1 = require('../package.cjs');
3
+ const require_adapters_detect = require('../adapters/detect.cjs');
4
+ const require_adapters_express = require('../adapters/express.cjs');
5
+ const require_adapters_hono = require('../adapters/hono.cjs');
4
6
  const require_handlers_base = require('./base.cjs');
5
7
  const require_handlers_util = require('./util.cjs');
6
- let cors = require("cors");
7
- cors = require_rolldown_runtime.__toESM(cors);
8
8
 
9
9
  //#region src/handlers/wallet.ts
10
10
  const debug = require("debug")(`${require_package$1.default.name}:handlers:wallet`);
@@ -110,24 +110,27 @@ var WalletHandlers = class extends require_handlers_base.default {
110
110
  tokenStorage: this.tokenStorage,
111
111
  authenticator: this.authenticator
112
112
  });
113
- app.use(`${prefix}/${action}`, (0, cors.default)({
114
- origin: "*",
115
- optionsSuccessStatus: 204
116
- }));
117
- app.get(`${prefix}/${action}/token`, generateSession);
118
- app.post(`${prefix}/${action}/token`, generateSession);
119
- app.get(`${prefix}/${action}/status`, ensureContext, checkSession);
120
- app.get(`${prefix}/${action}/timeout`, ensureContext, expireSession);
121
- app.get(pathname, ensureContext, ensureSignedJson, onAuthRequest);
122
- app.post(pathname, ensureContext, ensureSignedJson, onAuthResponse);
123
- app.get(`${pathname}/submit`, ensureContext, ensureSignedJson, onAuthResponse);
124
- return {
113
+ const handlerFns = {
125
114
  generateSession,
126
- expireSession,
127
115
  checkSession,
116
+ expireSession,
128
117
  onAuthRequest,
129
- onAuthResponse
118
+ onAuthResponse,
119
+ ensureContext,
120
+ ensureSignedJson
130
121
  };
122
+ if (require_adapters_detect.isHonoApp(app)) return require_adapters_hono.attachHono({
123
+ app,
124
+ prefix,
125
+ action,
126
+ handlers: handlerFns
127
+ });
128
+ return require_adapters_express.attachExpress({
129
+ app,
130
+ prefix,
131
+ action,
132
+ handlers: handlerFns
133
+ });
131
134
  }
132
135
  };
133
136
  var wallet_default = WalletHandlers;
@@ -1,3 +1,4 @@
1
+ import { ConnectRequest, ConnectResponse } from "../types.cjs";
1
2
  import BaseHandler from "./base.cjs";
2
3
 
3
4
  //#region src/handlers/wallet.d.ts
@@ -107,11 +108,11 @@ declare class WalletHandlers extends BaseHandler {
107
108
  authPrincipal?: boolean | string | any;
108
109
  persistentDynamicClaims?: boolean;
109
110
  }): {
110
- generateSession: (req: any, res: any) => Promise<void>;
111
- expireSession: (req: any, res: any) => Promise<void>;
112
- checkSession: (req: any, res: any) => Promise<void>;
113
- onAuthRequest: (req: any, res: any) => Promise<any>;
114
- onAuthResponse: (req: any, res: any) => Promise<any>;
111
+ generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
112
+ expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
113
+ checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
114
+ onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
115
+ onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
115
116
  };
116
117
  }
117
118
  //#endregion
package/lib/index.cjs CHANGED
@@ -1,5 +1,16 @@
1
1
  const require_authenticator_wallet = require('./authenticator/wallet.cjs');
2
+ const require_adapters_detect = require('./adapters/detect.cjs');
3
+ const require_adapters_express = require('./adapters/express.cjs');
4
+ const require_adapters_hono = require('./adapters/hono.cjs');
2
5
  const require_handlers_wallet = require('./handlers/wallet.cjs');
6
+ const require_storage_kv = require('./storage/kv.cjs');
3
7
 
8
+ exports.CloudflareKVStorage = require_storage_kv.default;
4
9
  exports.WalletAuthenticator = require_authenticator_wallet.default;
5
- exports.WalletHandlers = require_handlers_wallet.default;
10
+ exports.WalletHandlers = require_handlers_wallet.default;
11
+ exports.attachExpress = require_adapters_express.attachExpress;
12
+ exports.attachHono = require_adapters_hono.attachHono;
13
+ exports.createHonoRequest = require_adapters_hono.createHonoRequest;
14
+ exports.createHonoResponse = require_adapters_hono.createHonoResponse;
15
+ exports.isExpressApp = require_adapters_detect.isExpressApp;
16
+ exports.isHonoApp = require_adapters_detect.isHonoApp;
package/lib/index.d.cts CHANGED
@@ -1,3 +1,8 @@
1
+ import { isExpressApp, isHonoApp } from "./adapters/detect.cjs";
2
+ import { ConnectRequest, ConnectResponse, NextFunction } from "./types.cjs";
3
+ import { attachExpress } from "./adapters/express.cjs";
4
+ import { attachHono, createHonoRequest, createHonoResponse } from "./adapters/hono.cjs";
1
5
  import WalletAuthenticator from "./authenticator/wallet.cjs";
6
+ import CloudflareKVStorage from "./storage/kv.cjs";
2
7
  import WalletHandlers from "./handlers/wallet.cjs";
3
- export { WalletAuthenticator, WalletHandlers };
8
+ export { CloudflareKVStorage, type ConnectRequest, type ConnectResponse, type NextFunction, WalletAuthenticator, WalletHandlers, attachExpress, attachHono, createHonoRequest, createHonoResponse, isExpressApp, isHonoApp };
package/lib/package.cjs CHANGED
@@ -46,6 +46,7 @@ var require_package = /* @__PURE__ */ require_rolldown_runtime.__commonJSMin(((e
46
46
  "axios": "^1.13.4",
47
47
  "remark-cli": "^10.0.1",
48
48
  "remark-preset-github": "^4.0.4",
49
+ "hono": "^4.7.0",
49
50
  "tsdown": "^0.18.4",
50
51
  "tweetnacl": "^1.0.3"
51
52
  },
@@ -0,0 +1,57 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
+ let events = require("events");
4
+
5
+ //#region src/storage/kv.ts
6
+ var CloudflareKVStorage = class extends events.EventEmitter {
7
+ constructor(kv, options = {}) {
8
+ super();
9
+ this.kv = kv;
10
+ this.ttl = options.ttl ?? 300;
11
+ this.prefix = options.prefix ?? "";
12
+ }
13
+ key(token) {
14
+ return `${this.prefix}${token}`;
15
+ }
16
+ async create(token, status = "created") {
17
+ const record = {
18
+ token,
19
+ status
20
+ };
21
+ await this.kv.put(this.key(token), JSON.stringify(record), { expirationTtl: this.ttl });
22
+ this.emit("create", record);
23
+ return record;
24
+ }
25
+ async read(token) {
26
+ const raw = await this.kv.get(this.key(token));
27
+ if (!raw) return null;
28
+ return JSON.parse(raw);
29
+ }
30
+ async update(token, updates) {
31
+ const existing = await this.read(token);
32
+ if (!existing) return null;
33
+ delete updates.token;
34
+ const merged = {
35
+ ...existing,
36
+ ...updates
37
+ };
38
+ await this.kv.put(this.key(token), JSON.stringify(merged), { expirationTtl: this.ttl });
39
+ this.emit("update", merged);
40
+ return merged;
41
+ }
42
+ async delete(token) {
43
+ const existing = await this.read(token);
44
+ if (existing) this.emit("destroy", existing);
45
+ await this.kv.delete(this.key(token));
46
+ }
47
+ async exist(token, did) {
48
+ const record = await this.read(token);
49
+ if (!record) return false;
50
+ if (did) return record.did === did;
51
+ return true;
52
+ }
53
+ };
54
+ var kv_default = CloudflareKVStorage;
55
+
56
+ //#endregion
57
+ exports.default = kv_default;
@@ -0,0 +1,33 @@
1
+ import { EventEmitter } from "events";
2
+
3
+ //#region src/storage/kv.d.ts
4
+ interface KVNamespace {
5
+ get(key: string): Promise<string | null>;
6
+ put(key: string, value: string, options?: {
7
+ expirationTtl?: number;
8
+ }): Promise<void>;
9
+ delete(key: string): Promise<void>;
10
+ }
11
+ interface CloudflareKVStorageOptions {
12
+ /** TTL in seconds for KV entries. Default: 300 (5 minutes) */
13
+ ttl?: number;
14
+ /** Key prefix for KV entries. Default: '' */
15
+ prefix?: string;
16
+ }
17
+ declare class CloudflareKVStorage extends EventEmitter {
18
+ private kv;
19
+ private ttl;
20
+ private prefix;
21
+ constructor(kv: KVNamespace, options?: CloudflareKVStorageOptions);
22
+ private key;
23
+ create(token: string, status?: string): Promise<{
24
+ token: string;
25
+ status: string;
26
+ }>;
27
+ read(token: string): Promise<any>;
28
+ update(token: string, updates: Record<string, any>): Promise<any>;
29
+ delete(token: string): Promise<void>;
30
+ exist(token: string, did?: string): Promise<boolean>;
31
+ }
32
+ //#endregion
33
+ export { CloudflareKVStorage as default };
package/lib/types.cjs ADDED
File without changes
@@ -0,0 +1,55 @@
1
+ //#region src/types.d.ts
2
+ /**
3
+ * Framework-agnostic request interface for DID Connect handlers.
4
+ *
5
+ * Designed to match Express's `req` shape so that Express requests
6
+ * satisfy it natively (zero-wrapping). Hono adapter creates a wrapper
7
+ * that maps Hono Context to this interface.
8
+ */
9
+ interface ConnectRequest {
10
+ /** Parsed request body (from body-parser or Hono) */
11
+ body: Record<string, any>;
12
+ /** Parsed query string parameters */
13
+ query: Record<string, any>;
14
+ /** URL path parameters (e.g. :action) */
15
+ params: Record<string, any>;
16
+ /** Raw request headers */
17
+ headers: Record<string, string | string[] | undefined>;
18
+ /** Parsed cookies (from cookie-parser or manual parsing) */
19
+ cookies: Record<string, string>;
20
+ /** Request protocol ('http' or 'https') */
21
+ protocol: string;
22
+ /** Full original URL including query string */
23
+ originalUrl: string;
24
+ /** Session context, populated by ensureContext middleware */
25
+ context?: any;
26
+ /** Flag to prevent double monkey-patching in ensureSignedJson */
27
+ ensureSignedJson?: boolean;
28
+ /** Case-insensitive header getter (matches Express req.get) */
29
+ get(name: string): string | undefined;
30
+ /** Content negotiation for locale (matches Express req.acceptsLanguages) */
31
+ acceptsLanguages(...languages: string[]): string | false;
32
+ /** Original framework-specific request object (Express req or Hono Context) */
33
+ raw?: any;
34
+ }
35
+ /**
36
+ * Framework-agnostic response interface for DID Connect handlers.
37
+ *
38
+ * Express: maps directly to res.jsonp/res.json/res.status().json().
39
+ * Hono: uses a buffer pattern — calls are captured, then the outer
40
+ * Hono handler reads the buffer to return c.json().
41
+ */
42
+ interface ConnectResponse {
43
+ /** Send JSON response (Express JSONP, Hono json) */
44
+ jsonp(data: any): void;
45
+ /** Send JSON response */
46
+ json(data: any): void;
47
+ /** Set status code and return object with json() method */
48
+ status(code: number): {
49
+ json(data: any): void;
50
+ };
51
+ }
52
+ /** Middleware next() callback */
53
+ type NextFunction = () => void;
54
+ //#endregion
55
+ export { ConnectRequest, ConnectResponse, NextFunction };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@arcblock/did-connect-js",
3
3
  "description": "Helper function to setup DID Connect support on a node.js web server",
4
4
  "type": "module",
5
- "version": "1.29.13",
5
+ "version": "1.29.14",
6
6
  "author": {
7
7
  "name": "wangshijun",
8
8
  "email": "shijun@arcblock.io",
@@ -19,13 +19,13 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@arcblock/did": "1.29.13",
23
- "@arcblock/jwt": "1.29.13",
24
- "@arcblock/validator": "1.29.13",
25
- "@ocap/client": "1.29.13",
26
- "@ocap/mcrypto": "1.29.13",
27
- "@ocap/util": "1.29.13",
28
- "@ocap/wallet": "1.29.13",
22
+ "@arcblock/did": "1.29.14",
23
+ "@arcblock/jwt": "1.29.14",
24
+ "@arcblock/validator": "1.29.14",
25
+ "@ocap/client": "1.29.14",
26
+ "@ocap/mcrypto": "1.29.14",
27
+ "@ocap/util": "1.29.14",
28
+ "@ocap/wallet": "1.29.14",
29
29
  "cors": "^2.8.5",
30
30
  "debug": "^4.4.3",
31
31
  "json-stable-stringify": "^1.0.1",
@@ -36,7 +36,7 @@
36
36
  "devDependencies": {
37
37
  "@arcblock/did-agent-storage-memory": "^1.8.0",
38
38
  "@arcblock/did-connect-storage-memory": "^1.8.0",
39
- "@ocap/e2e-test": "1.29.13",
39
+ "@ocap/e2e-test": "1.29.14",
40
40
  "@types/cors": "^2.8.17",
41
41
  "@types/debug": "^4.1.12",
42
42
  "@types/json-stable-stringify": "^1.0.36",
@@ -46,6 +46,7 @@
46
46
  "axios": "^1.13.4",
47
47
  "remark-cli": "^10.0.1",
48
48
  "remark-preset-github": "^4.0.4",
49
+ "hono": "^4.7.0",
49
50
  "tsdown": "^0.18.4",
50
51
  "tweetnacl": "^1.0.3"
51
52
  },