@zintrust/core 0.4.30 → 0.4.32
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 +15 -0
- package/package.json +5 -1
- package/src/boot.d.ts +3 -0
- package/src/boot.d.ts.map +1 -0
- package/src/boot.js +1 -0
- package/src/cli/commands/D1ProxyCommand.d.ts.map +1 -1
- package/src/cli/commands/D1ProxyCommand.js +24 -159
- package/src/cli/commands/KvProxyCommand.d.ts.map +1 -1
- package/src/cli/commands/KvProxyCommand.js +23 -155
- package/src/cli/commands/ProxyScaffoldUtils.d.ts +31 -0
- package/src/cli/commands/ProxyScaffoldUtils.d.ts.map +1 -0
- package/src/cli/commands/ProxyScaffoldUtils.js +120 -0
- package/src/cli/commands/WranglerProxyCommandUtils.d.ts +26 -0
- package/src/cli/commands/WranglerProxyCommandUtils.d.ts.map +1 -0
- package/src/cli/commands/WranglerProxyCommandUtils.js +63 -0
- package/src/cli/scaffolding/GovernanceScaffolder.d.ts.map +1 -1
- package/src/cli/scaffolding/GovernanceScaffolder.js +3 -40
- package/src/cli/scaffolding/ProjectScaffolder.d.ts.map +1 -1
- package/src/cli/scaffolding/ProjectScaffolder.js +1 -34
- package/src/cli/scaffolding/ScaffoldingVersionUtils.d.ts +6 -0
- package/src/cli/scaffolding/ScaffoldingVersionUtils.d.ts.map +1 -0
- package/src/cli/scaffolding/ScaffoldingVersionUtils.js +34 -0
- package/src/index.js +3 -3
- package/src/proxy/d1/ZintrustD1Proxy.d.ts +46 -1
- package/src/proxy/d1/ZintrustD1Proxy.d.ts.map +1 -1
- package/src/proxy/d1/ZintrustD1Proxy.js +356 -31
- package/src/proxy/kv/ZintrustKvProxy.d.ts +42 -1
- package/src/proxy/kv/ZintrustKvProxy.d.ts.map +1 -1
- package/src/proxy/kv/ZintrustKvProxy.js +297 -34
- package/src/templates/project/basic/src/boot/bootstrap.ts.tpl +3 -0
|
@@ -1,38 +1,301 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
1
|
+
import { isObject, isString } from '../../helper/index.js';
|
|
2
|
+
import { ErrorHandler } from '../ErrorHandler.js';
|
|
3
|
+
import { RequestValidator } from '../RequestValidator.js';
|
|
4
|
+
import { SigningService } from '../SigningService.js';
|
|
5
|
+
const DEFAULT_SIGNING_WINDOW_MS = 60_000;
|
|
6
|
+
const DEFAULT_MAX_BODY_BYTES = 128 * 1024;
|
|
7
|
+
const DEFAULT_LIST_LIMIT = 100;
|
|
8
|
+
const json = (status, body) => {
|
|
9
|
+
return new Response(JSON.stringify(body), {
|
|
10
|
+
status,
|
|
11
|
+
headers: {
|
|
12
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
13
|
+
'Cache-Control': 'no-store',
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
const toErrorResponse = (status, code, message) => {
|
|
18
|
+
const error = ErrorHandler.toProxyError(status, code, message);
|
|
19
|
+
return json(error.status, error.body);
|
|
20
|
+
};
|
|
21
|
+
const getEnvInt = (env, name, fallback) => {
|
|
22
|
+
const raw = env[name];
|
|
23
|
+
if (!isString(raw))
|
|
24
|
+
return fallback;
|
|
25
|
+
const parsed = Number.parseInt(raw, 10);
|
|
26
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
27
|
+
};
|
|
28
|
+
const normalizeBindingName = (value) => {
|
|
29
|
+
if (!isString(value))
|
|
30
|
+
return null;
|
|
31
|
+
const trimmed = value.trim();
|
|
32
|
+
return trimmed === '' ? null : trimmed;
|
|
33
|
+
};
|
|
34
|
+
const readBodyBytes = async (request, maxBytes) => {
|
|
35
|
+
const buf = await request.arrayBuffer();
|
|
36
|
+
if (buf.byteLength > maxBytes) {
|
|
37
|
+
return {
|
|
38
|
+
ok: false,
|
|
39
|
+
response: toErrorResponse(413, 'PAYLOAD_TOO_LARGE', 'Body too large'),
|
|
35
40
|
};
|
|
41
|
+
}
|
|
42
|
+
const bytes = new Uint8Array(buf);
|
|
43
|
+
const text = new TextDecoder().decode(bytes);
|
|
44
|
+
return { ok: true, bytes, text };
|
|
45
|
+
};
|
|
46
|
+
const parseOptionalJson = (text) => {
|
|
47
|
+
if (text.trim() === '')
|
|
48
|
+
return { ok: true, payload: null };
|
|
49
|
+
const parsed = RequestValidator.parseJson(text);
|
|
50
|
+
if (!parsed.ok) {
|
|
51
|
+
let message = parsed.error.message;
|
|
52
|
+
if (parsed.error.code === 'INVALID_JSON') {
|
|
53
|
+
message = 'Invalid JSON body';
|
|
54
|
+
}
|
|
55
|
+
else if (parsed.error.code === 'VALIDATION_ERROR') {
|
|
56
|
+
message = 'Invalid body';
|
|
57
|
+
}
|
|
58
|
+
return { ok: false, response: toErrorResponse(400, parsed.error.code, message) };
|
|
59
|
+
}
|
|
60
|
+
return { ok: true, payload: parsed.value };
|
|
61
|
+
};
|
|
62
|
+
const loadSigningSecret = (env) => {
|
|
63
|
+
const direct = isString(env.KV_REMOTE_SECRET) ? env.KV_REMOTE_SECRET.trim() : '';
|
|
64
|
+
if (direct !== '')
|
|
65
|
+
return direct;
|
|
66
|
+
const fallback = isString(env.APP_KEY) ? env.APP_KEY.trim() : '';
|
|
67
|
+
if (fallback !== '')
|
|
68
|
+
return fallback;
|
|
69
|
+
return null;
|
|
70
|
+
};
|
|
71
|
+
const verifyNonceKv = async (kv, keyId, nonce, ttlMs) => {
|
|
72
|
+
const ttlSeconds = Math.max(1, Math.ceil(ttlMs / 1000));
|
|
73
|
+
const storageKey = `nonce:${keyId}:${nonce}`;
|
|
74
|
+
const existing = await kv.get(storageKey);
|
|
75
|
+
if (existing !== null)
|
|
76
|
+
return false;
|
|
77
|
+
await kv.put(storageKey, '1', { expirationTtl: ttlSeconds });
|
|
78
|
+
return true;
|
|
79
|
+
};
|
|
80
|
+
const verifySignedRequest = async (request, env, bodyBytes) => {
|
|
81
|
+
const secret = loadSigningSecret(env);
|
|
82
|
+
if (secret === null) {
|
|
83
|
+
return toErrorResponse(500, 'CONFIG_ERROR', 'Missing signing secret (KV_REMOTE_SECRET or APP_KEY)');
|
|
84
|
+
}
|
|
85
|
+
const windowMs = getEnvInt(env, 'ZT_PROXY_SIGNING_WINDOW_MS', DEFAULT_SIGNING_WINDOW_MS);
|
|
86
|
+
const verifyResult = await SigningService.verifyWithKeyProvider({
|
|
87
|
+
method: request.method,
|
|
88
|
+
url: request.url,
|
|
89
|
+
body: bodyBytes,
|
|
90
|
+
headers: request.headers,
|
|
91
|
+
windowMs,
|
|
92
|
+
getSecretForKeyId: (_keyId) => secret,
|
|
93
|
+
verifyNonce: env.ZT_NONCES === undefined
|
|
94
|
+
? undefined
|
|
95
|
+
: async (keyId, nonce, ttlMs) => verifyNonceKv(env.ZT_NONCES, keyId, nonce, ttlMs),
|
|
96
|
+
});
|
|
97
|
+
if (!verifyResult.ok) {
|
|
98
|
+
return toErrorResponse(verifyResult.status, verifyResult.code, verifyResult.message);
|
|
99
|
+
}
|
|
100
|
+
return { ok: true };
|
|
101
|
+
};
|
|
102
|
+
const requireCache = (env) => {
|
|
103
|
+
if (env.CACHE !== undefined && env.CACHE !== null)
|
|
104
|
+
return env.CACHE;
|
|
105
|
+
const bindingName = normalizeBindingName(env.KV_NAMESPACE);
|
|
106
|
+
if (bindingName !== null) {
|
|
107
|
+
const record = env;
|
|
108
|
+
const kv = record[bindingName];
|
|
109
|
+
if (kv !== undefined && kv !== null)
|
|
110
|
+
return kv;
|
|
111
|
+
}
|
|
112
|
+
return toErrorResponse(500, 'CONFIG_ERROR', 'Missing KV binding (CACHE)');
|
|
113
|
+
};
|
|
114
|
+
const normalizeNamespace = (value) => {
|
|
115
|
+
if (!isString(value))
|
|
116
|
+
return undefined;
|
|
117
|
+
const trimmed = value.trim();
|
|
118
|
+
return trimmed === '' ? undefined : trimmed;
|
|
119
|
+
};
|
|
120
|
+
const buildStorageKey = (env, params) => {
|
|
121
|
+
const prefix = isString(env.ZT_KV_PREFIX) ? env.ZT_KV_PREFIX : '';
|
|
122
|
+
const namespace = normalizeNamespace(params.namespace);
|
|
123
|
+
const parts = [];
|
|
124
|
+
if (prefix.trim() !== '')
|
|
125
|
+
parts.push(prefix.trim());
|
|
126
|
+
if (namespace !== undefined)
|
|
127
|
+
parts.push(namespace);
|
|
128
|
+
parts.push(params.key);
|
|
129
|
+
return parts.join(':');
|
|
130
|
+
};
|
|
131
|
+
const readAndVerifyJson = async (request, env) => {
|
|
132
|
+
const maxBodyBytes = getEnvInt(env, 'ZT_MAX_BODY_BYTES', DEFAULT_MAX_BODY_BYTES);
|
|
133
|
+
const bodyResult = await readBodyBytes(request, maxBodyBytes);
|
|
134
|
+
if (!bodyResult.ok)
|
|
135
|
+
return { ok: false, response: bodyResult.response };
|
|
136
|
+
const auth = await verifySignedRequest(request, env, bodyResult.bytes);
|
|
137
|
+
if (auth instanceof Response)
|
|
138
|
+
return { ok: false, response: auth };
|
|
139
|
+
const parsed = parseOptionalJson(bodyResult.text);
|
|
140
|
+
if (!parsed.ok)
|
|
141
|
+
return { ok: false, response: parsed.response };
|
|
142
|
+
return { ok: true, payload: parsed.payload, bodyBytes: bodyResult.bytes };
|
|
143
|
+
};
|
|
144
|
+
const parseGetPayload = (payload) => {
|
|
145
|
+
if (!isObject(payload)) {
|
|
146
|
+
return { ok: false, response: toErrorResponse(400, 'VALIDATION_ERROR', 'Invalid body') };
|
|
147
|
+
}
|
|
148
|
+
const key = payload['key'];
|
|
149
|
+
const type = payload['type'];
|
|
150
|
+
if (!isString(key) || key.trim() === '') {
|
|
151
|
+
return { ok: false, response: toErrorResponse(400, 'VALIDATION_ERROR', 'key is required') };
|
|
152
|
+
}
|
|
153
|
+
const typeValue = type === 'text' || type === 'arrayBuffer' || type === 'json' ? type : 'text';
|
|
154
|
+
return { ok: true, namespace: normalizeNamespace(payload['namespace']), key, type: typeValue };
|
|
155
|
+
};
|
|
156
|
+
const handleGet = async (request, env) => {
|
|
157
|
+
const check = await readAndVerifyJson(request, env);
|
|
158
|
+
if (!check.ok)
|
|
159
|
+
return check.response;
|
|
160
|
+
const cache = requireCache(env);
|
|
161
|
+
if (cache instanceof Response)
|
|
162
|
+
return cache;
|
|
163
|
+
const parsed = parseGetPayload(check.payload);
|
|
164
|
+
if (!parsed.ok)
|
|
165
|
+
return parsed.response;
|
|
166
|
+
const storageKey = buildStorageKey(env, { namespace: parsed.namespace, key: parsed.key });
|
|
167
|
+
if (parsed.type === 'json') {
|
|
168
|
+
const value = await cache.get(storageKey, 'json');
|
|
169
|
+
return json(200, { value: value ?? null });
|
|
170
|
+
}
|
|
171
|
+
if (parsed.type === 'arrayBuffer') {
|
|
172
|
+
const value = await cache.get(storageKey, 'arrayBuffer');
|
|
173
|
+
return json(200, { value: value ?? null });
|
|
174
|
+
}
|
|
175
|
+
const value = await cache.get(storageKey);
|
|
176
|
+
return json(200, { value: value ?? null });
|
|
177
|
+
};
|
|
178
|
+
const parsePutPayload = (payload) => {
|
|
179
|
+
if (!isObject(payload)) {
|
|
180
|
+
return { ok: false, response: toErrorResponse(400, 'VALIDATION_ERROR', 'Invalid body') };
|
|
181
|
+
}
|
|
182
|
+
const key = payload['key'];
|
|
183
|
+
if (!isString(key) || key.trim() === '') {
|
|
184
|
+
return { ok: false, response: toErrorResponse(400, 'VALIDATION_ERROR', 'key is required') };
|
|
185
|
+
}
|
|
186
|
+
const ttlSeconds = payload['ttlSeconds'];
|
|
187
|
+
const ttl = typeof ttlSeconds === 'number' && Number.isFinite(ttlSeconds) && ttlSeconds > 0
|
|
188
|
+
? ttlSeconds
|
|
189
|
+
: undefined;
|
|
190
|
+
return {
|
|
191
|
+
ok: true,
|
|
192
|
+
namespace: normalizeNamespace(payload['namespace']),
|
|
193
|
+
key,
|
|
194
|
+
value: payload['value'],
|
|
195
|
+
ttlSeconds: ttl,
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
const handlePut = async (request, env) => {
|
|
199
|
+
const check = await readAndVerifyJson(request, env);
|
|
200
|
+
if (!check.ok)
|
|
201
|
+
return check.response;
|
|
202
|
+
const cache = requireCache(env);
|
|
203
|
+
if (cache instanceof Response)
|
|
204
|
+
return cache;
|
|
205
|
+
const parsed = parsePutPayload(check.payload);
|
|
206
|
+
if (!parsed.ok)
|
|
207
|
+
return parsed.response;
|
|
208
|
+
const storageKey = buildStorageKey(env, { namespace: parsed.namespace, key: parsed.key });
|
|
209
|
+
const value = JSON.stringify(parsed.value);
|
|
210
|
+
const options = {};
|
|
211
|
+
if (parsed.ttlSeconds !== undefined) {
|
|
212
|
+
options.expirationTtl = Math.floor(parsed.ttlSeconds);
|
|
213
|
+
}
|
|
214
|
+
await cache.put(storageKey, value, options);
|
|
215
|
+
return json(200, { ok: true });
|
|
216
|
+
};
|
|
217
|
+
const parseDeletePayload = (payload) => {
|
|
218
|
+
if (!isObject(payload)) {
|
|
219
|
+
return { ok: false, response: toErrorResponse(400, 'VALIDATION_ERROR', 'Invalid body') };
|
|
220
|
+
}
|
|
221
|
+
const key = payload['key'];
|
|
222
|
+
if (!isString(key) || key.trim() === '') {
|
|
223
|
+
return { ok: false, response: toErrorResponse(400, 'VALIDATION_ERROR', 'key is required') };
|
|
224
|
+
}
|
|
225
|
+
return { ok: true, namespace: normalizeNamespace(payload['namespace']), key };
|
|
226
|
+
};
|
|
227
|
+
const handleDelete = async (request, env) => {
|
|
228
|
+
const check = await readAndVerifyJson(request, env);
|
|
229
|
+
if (!check.ok)
|
|
230
|
+
return check.response;
|
|
231
|
+
const cache = requireCache(env);
|
|
232
|
+
if (cache instanceof Response)
|
|
233
|
+
return cache;
|
|
234
|
+
const parsed = parseDeletePayload(check.payload);
|
|
235
|
+
if (!parsed.ok)
|
|
236
|
+
return parsed.response;
|
|
237
|
+
const storageKey = buildStorageKey(env, { namespace: parsed.namespace, key: parsed.key });
|
|
238
|
+
await cache.delete(storageKey);
|
|
239
|
+
return json(200, { ok: true });
|
|
240
|
+
};
|
|
241
|
+
const parseListPayload = (payload) => {
|
|
242
|
+
if (payload === null)
|
|
243
|
+
return { ok: true, params: {} };
|
|
244
|
+
if (!isObject(payload)) {
|
|
245
|
+
return { ok: false, response: toErrorResponse(400, 'VALIDATION_ERROR', 'Invalid body') };
|
|
246
|
+
}
|
|
247
|
+
const namespace = normalizeNamespace(payload['namespace']);
|
|
248
|
+
const prefix = isString(payload['prefix']) ? payload['prefix'] : undefined;
|
|
249
|
+
const cursor = isString(payload['cursor']) ? payload['cursor'] : undefined;
|
|
250
|
+
const limitRaw = payload['limit'];
|
|
251
|
+
const limitParsed = typeof limitRaw === 'number' && Number.isFinite(limitRaw) ? Math.floor(limitRaw) : undefined;
|
|
252
|
+
return { ok: true, params: { namespace, prefix, cursor, limit: limitParsed } };
|
|
253
|
+
};
|
|
254
|
+
const handleList = async (request, env) => {
|
|
255
|
+
const check = await readAndVerifyJson(request, env);
|
|
256
|
+
if (!check.ok)
|
|
257
|
+
return check.response;
|
|
258
|
+
const cache = requireCache(env);
|
|
259
|
+
if (cache instanceof Response)
|
|
260
|
+
return cache;
|
|
261
|
+
const parsed = parseListPayload(check.payload);
|
|
262
|
+
if (!parsed.ok)
|
|
263
|
+
return parsed.response;
|
|
264
|
+
const envLimit = getEnvInt(env, 'ZT_KV_LIST_LIMIT', DEFAULT_LIST_LIMIT);
|
|
265
|
+
const requested = parsed.params.limit ?? envLimit;
|
|
266
|
+
const limit = Math.max(1, Math.min(requested, envLimit));
|
|
267
|
+
const prefixKey = parsed.params.prefix;
|
|
268
|
+
const namespacePrefix = normalizeNamespace(parsed.params.namespace);
|
|
269
|
+
const basePrefix = buildStorageKey(env, { namespace: namespacePrefix, key: '' });
|
|
270
|
+
const fullPrefix = prefixKey === undefined ? basePrefix : `${basePrefix}${prefixKey}`;
|
|
271
|
+
const out = await cache.list({ prefix: fullPrefix, limit, cursor: parsed.params.cursor });
|
|
272
|
+
return json(200, {
|
|
273
|
+
keys: out.keys.map((key) => key.name),
|
|
274
|
+
cursor: out.cursor,
|
|
275
|
+
listComplete: out.list_complete,
|
|
276
|
+
});
|
|
277
|
+
};
|
|
278
|
+
export const ZintrustKvProxy = Object.freeze({
|
|
279
|
+
_ZINTRUST_CLOUDFLARE_KV_PROXY_VERSION: '0.1.15',
|
|
280
|
+
_ZINTRUST_CLOUDFLARE_KV_PROXY_BUILD_DATE: '__BUILD_DATE__',
|
|
281
|
+
async fetch(request, env) {
|
|
282
|
+
const url = new URL(request.url);
|
|
283
|
+
const methodError = RequestValidator.requirePost(request.method);
|
|
284
|
+
if (methodError !== null) {
|
|
285
|
+
return toErrorResponse(405, methodError.code, 'Method not allowed');
|
|
286
|
+
}
|
|
287
|
+
switch (url.pathname) {
|
|
288
|
+
case '/zin/kv/get':
|
|
289
|
+
return handleGet(request, env);
|
|
290
|
+
case '/zin/kv/put':
|
|
291
|
+
return handlePut(request, env);
|
|
292
|
+
case '/zin/kv/delete':
|
|
293
|
+
return handleDelete(request, env);
|
|
294
|
+
case '/zin/kv/list':
|
|
295
|
+
return handleList(request, env);
|
|
296
|
+
default:
|
|
297
|
+
return toErrorResponse(404, 'NOT_FOUND', 'Not found');
|
|
298
|
+
}
|
|
36
299
|
},
|
|
37
300
|
});
|
|
38
301
|
export default ZintrustKvProxy;
|