@zenstackhq/cli 3.9.0 → 3.9.2
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 +17 -198
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +4 -154
- package/dist/index.mjs.map +1 -1
- package/dist/proxy-ByxBTLnf.cjs +251 -0
- package/dist/proxy-ByxBTLnf.cjs.map +1 -0
- package/dist/proxy-DV8jo5y1.mjs +163 -0
- package/dist/proxy-DV8jo5y1.mjs.map +1 -0
- package/dist/proxy.cjs +7 -0
- package/dist/proxy.d.cts +33 -0
- package/dist/proxy.d.mts +33 -0
- package/dist/proxy.mjs +2 -0
- package/package.json +27 -14
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { n as createProxyApp, o as checkNewVersion, s as getVersion } from "./proxy-DV8jo5y1.mjs";
|
|
1
2
|
import { createRequire } from "node:module";
|
|
2
3
|
import { ZModelCodeGenerator, ZModelLanguageMetaData, formatDocument, loadDocument } from "@zenstackhq/language";
|
|
3
4
|
import colors from "colors";
|
|
@@ -8,9 +9,9 @@ import path from "node:path";
|
|
|
8
9
|
import { invariant, lowerCaseFirst, singleDebounce } from "@zenstackhq/common-helpers";
|
|
9
10
|
import { PrismaSchemaGenerator, TsSchemaGenerator, detectImportFileExtension } from "@zenstackhq/sdk";
|
|
10
11
|
import { createJiti } from "jiti";
|
|
11
|
-
import crypto, { createHash, randomUUID
|
|
12
|
+
import crypto, { createHash, randomUUID } from "node:crypto";
|
|
12
13
|
import fs from "node:fs";
|
|
13
|
-
import {
|
|
14
|
+
import { pathToFileURL } from "node:url";
|
|
14
15
|
import terminalLink from "terminal-link";
|
|
15
16
|
import { z } from "zod";
|
|
16
17
|
import ora from "ora";
|
|
@@ -23,16 +24,12 @@ import { watch } from "chokidar";
|
|
|
23
24
|
import semver from "semver";
|
|
24
25
|
import { detect, resolveCommand } from "package-manager-detector";
|
|
25
26
|
import { execaCommand } from "execa";
|
|
27
|
+
import { serve } from "@hono/node-server";
|
|
26
28
|
import { ZenStackClient } from "@zenstackhq/orm";
|
|
27
29
|
import { MysqlDialect } from "@zenstackhq/orm/dialects/mysql";
|
|
28
30
|
import { PostgresDialect } from "@zenstackhq/orm/dialects/postgres";
|
|
29
31
|
import { SqliteDialect } from "@zenstackhq/orm/dialects/sqlite";
|
|
30
32
|
import { PolicyPlugin } from "@zenstackhq/plugin-policy";
|
|
31
|
-
import { RPCApiHandler } from "@zenstackhq/server/api";
|
|
32
|
-
import { createHonoHandler } from "@zenstackhq/server/hono";
|
|
33
|
-
import { serve } from "@hono/node-server";
|
|
34
|
-
import { Hono } from "hono";
|
|
35
|
-
import { cors } from "hono/cors";
|
|
36
33
|
import "mixpanel";
|
|
37
34
|
import * as os$1 from "os";
|
|
38
35
|
import process$1, { env } from "node:process";
|
|
@@ -3066,67 +3063,7 @@ function handleSubProcessError(err) {
|
|
|
3066
3063
|
else process.exit(1);
|
|
3067
3064
|
}
|
|
3068
3065
|
//#endregion
|
|
3069
|
-
//#region src/utils/version-utils.ts
|
|
3070
|
-
const CHECK_VERSION_TIMEOUT = 2e3;
|
|
3071
|
-
const VERSION_CHECK_TAG = "latest";
|
|
3072
|
-
function getVersion() {
|
|
3073
|
-
try {
|
|
3074
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname : path.dirname(fileURLToPath(import.meta.url));
|
|
3075
|
-
return JSON.parse(fs.readFileSync(path.join(_dirname, "../package.json"), "utf8")).version;
|
|
3076
|
-
} catch {
|
|
3077
|
-
return;
|
|
3078
|
-
}
|
|
3079
|
-
}
|
|
3080
|
-
async function checkNewVersion() {
|
|
3081
|
-
const currVersion = getVersion();
|
|
3082
|
-
let latestVersion;
|
|
3083
|
-
let timer;
|
|
3084
|
-
try {
|
|
3085
|
-
const fetchPromise = getLatestVersion();
|
|
3086
|
-
fetchPromise.catch(() => {});
|
|
3087
|
-
latestVersion = await Promise.race([fetchPromise, new Promise((_, reject) => {
|
|
3088
|
-
timer = setTimeout(() => reject(/* @__PURE__ */ new Error("version check timed out")), CHECK_VERSION_TIMEOUT + 1e3);
|
|
3089
|
-
})]);
|
|
3090
|
-
} catch {
|
|
3091
|
-
return;
|
|
3092
|
-
} finally {
|
|
3093
|
-
clearTimeout(timer);
|
|
3094
|
-
}
|
|
3095
|
-
if (latestVersion && currVersion && semver.gt(latestVersion, currVersion)) console.log(`A newer version ${colors.cyan(latestVersion)} is available.`);
|
|
3096
|
-
}
|
|
3097
|
-
async function getLatestVersion() {
|
|
3098
|
-
const fetchResult = await fetch(`https://registry.npmjs.org/@zenstackhq/cli/${VERSION_CHECK_TAG}`, {
|
|
3099
|
-
headers: { accept: "application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*" },
|
|
3100
|
-
signal: AbortSignal.timeout(CHECK_VERSION_TIMEOUT)
|
|
3101
|
-
});
|
|
3102
|
-
if (fetchResult.ok) {
|
|
3103
|
-
const latestVersion = (await fetchResult.json())?.version;
|
|
3104
|
-
if (typeof latestVersion === "string" && semver.valid(latestVersion)) return latestVersion;
|
|
3105
|
-
}
|
|
3106
|
-
throw new Error("invalid npm registry response");
|
|
3107
|
-
}
|
|
3108
|
-
//#endregion
|
|
3109
3066
|
//#region src/actions/proxy.ts
|
|
3110
|
-
const ProxyAuthError = {
|
|
3111
|
-
MISSING_SIGNATURE_HEADER: "Missing x-zenstack-signature header",
|
|
3112
|
-
INVALID_TIMESTAMP: "Request timestamp is expired or invalid",
|
|
3113
|
-
INVALID_SIGNATURE_FORMAT: "Invalid x-zenstack-signature format"
|
|
3114
|
-
};
|
|
3115
|
-
function rejectAuth(c, code) {
|
|
3116
|
-
return c.json({
|
|
3117
|
-
code,
|
|
3118
|
-
message: ProxyAuthError[code]
|
|
3119
|
-
}, 401);
|
|
3120
|
-
}
|
|
3121
|
-
const UserClaimSchema = z.discriminatedUnion("type", [z.object({ type: z.literal("superUser") }), z.object({
|
|
3122
|
-
type: z.literal("user"),
|
|
3123
|
-
data: z.record(z.string(), z.unknown())
|
|
3124
|
-
})]);
|
|
3125
|
-
function normalizePublicKey(key) {
|
|
3126
|
-
key = key.trim();
|
|
3127
|
-
if (key.startsWith("-----BEGIN PUBLIC KEY-----")) return key;
|
|
3128
|
-
return `-----BEGIN PUBLIC KEY-----\n${key.replace(/-/g, "+").replace(/_/g, "/")}\n-----END PUBLIC KEY-----`;
|
|
3129
|
-
}
|
|
3130
3067
|
async function run(options) {
|
|
3131
3068
|
options = {
|
|
3132
3069
|
...options,
|
|
@@ -3224,93 +3161,6 @@ async function createDialect(provider, databaseUrl, schemaFilePath) {
|
|
|
3224
3161
|
default: throw new CliError(`Unsupported database provider: ${provider}`);
|
|
3225
3162
|
}
|
|
3226
3163
|
}
|
|
3227
|
-
function createProxyApp(optionsOrClient, schema, authDb, auth) {
|
|
3228
|
-
let options;
|
|
3229
|
-
if ("client" in optionsOrClient && "schema" in optionsOrClient) options = optionsOrClient;
|
|
3230
|
-
else options = {
|
|
3231
|
-
client: optionsOrClient,
|
|
3232
|
-
schema,
|
|
3233
|
-
authDb,
|
|
3234
|
-
auth
|
|
3235
|
-
};
|
|
3236
|
-
const app = new Hono();
|
|
3237
|
-
app.use("*", cors(options.cors));
|
|
3238
|
-
if (options.auth?.studioAuthKey) {
|
|
3239
|
-
const toleranceSecs = options.auth.signatureToleranceSecs;
|
|
3240
|
-
const sigMiddleware = createSignatureMiddleware(normalizePublicKey(options.auth.studioAuthKey), toleranceSecs);
|
|
3241
|
-
app.use("/api/model/*", sigMiddleware);
|
|
3242
|
-
app.use("/api/schema", sigMiddleware);
|
|
3243
|
-
}
|
|
3244
|
-
app.use("/api/model/*", createHonoHandler({
|
|
3245
|
-
apiHandler: new RPCApiHandler({ schema: options.schema }),
|
|
3246
|
-
getClient: (c) => resolveClient(options.client, options.authDb ?? options.client, c, !!options.auth?.studioAuthKey)
|
|
3247
|
-
}));
|
|
3248
|
-
app.get("/api/schema", (c) => {
|
|
3249
|
-
return c.json({
|
|
3250
|
-
...options.schema,
|
|
3251
|
-
zenstackVersion: getVersion()
|
|
3252
|
-
});
|
|
3253
|
-
});
|
|
3254
|
-
return app;
|
|
3255
|
-
}
|
|
3256
|
-
function createSignatureMiddleware(publicKey, toleranceSeconds) {
|
|
3257
|
-
let lastInvalidSigWarnAt = 0;
|
|
3258
|
-
const WARN_THROTTLE_SECS = 60;
|
|
3259
|
-
function warnInvalidSignature() {
|
|
3260
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
3261
|
-
if (now - lastInvalidSigWarnAt >= WARN_THROTTLE_SECS) {
|
|
3262
|
-
lastInvalidSigWarnAt = now;
|
|
3263
|
-
console.warn(colors.yellow("Warning: Received a request with an invalid signature. Please double-check whether you have the correct public API key configured."));
|
|
3264
|
-
}
|
|
3265
|
-
}
|
|
3266
|
-
return async (c, next) => {
|
|
3267
|
-
const signatureHeader = c.req.header("x-zenstack-signature");
|
|
3268
|
-
if (!signatureHeader) return rejectAuth(c, "MISSING_SIGNATURE_HEADER");
|
|
3269
|
-
const parts = signatureHeader.split(",");
|
|
3270
|
-
const timestampPart = parts.find((p) => p.startsWith("t="));
|
|
3271
|
-
const sigPart = parts.find((p) => p.startsWith("v1="));
|
|
3272
|
-
if (!timestampPart || !sigPart) return rejectAuth(c, "INVALID_SIGNATURE_FORMAT");
|
|
3273
|
-
const timestamp = timestampPart.substring(2);
|
|
3274
|
-
const sig = sigPart.substring(3);
|
|
3275
|
-
const requestTime = parseInt(timestamp, 10);
|
|
3276
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
3277
|
-
if (isNaN(requestTime) || Math.abs(now - requestTime) > toleranceSeconds) return rejectAuth(c, "INVALID_TIMESTAMP");
|
|
3278
|
-
let payload;
|
|
3279
|
-
if (c.req.method === "GET" || c.req.method === "DELETE") {
|
|
3280
|
-
const rawUrl = c.req.url;
|
|
3281
|
-
const qMark = rawUrl.indexOf("?");
|
|
3282
|
-
payload = qMark >= 0 ? rawUrl.substring(qMark + 1) : "";
|
|
3283
|
-
} else payload = await c.req.text();
|
|
3284
|
-
const authHeader = c.req.header("authorization");
|
|
3285
|
-
const authorizationToken = authHeader && authHeader.startsWith("Bearer ") ? authHeader.substring(7) : void 0;
|
|
3286
|
-
const message = authorizationToken ? `${payload}${timestamp}${authorizationToken}` : `${payload}${timestamp}`;
|
|
3287
|
-
try {
|
|
3288
|
-
if (!verify(null, Buffer.from(message, "utf8"), publicKey, Buffer.from(sig, "base64url"))) {
|
|
3289
|
-
warnInvalidSignature();
|
|
3290
|
-
return rejectAuth(c, "INVALID_SIGNATURE_FORMAT");
|
|
3291
|
-
}
|
|
3292
|
-
} catch {
|
|
3293
|
-
warnInvalidSignature();
|
|
3294
|
-
return rejectAuth(c, "INVALID_SIGNATURE_FORMAT");
|
|
3295
|
-
}
|
|
3296
|
-
return next();
|
|
3297
|
-
};
|
|
3298
|
-
}
|
|
3299
|
-
function resolveClient(client, authDb, c, isAuthKeyEnabled) {
|
|
3300
|
-
const authHeader = c.req.header("authorization");
|
|
3301
|
-
if (!isAuthKeyEnabled && !authHeader) return client;
|
|
3302
|
-
if (!authHeader?.startsWith("Bearer ")) return authDb;
|
|
3303
|
-
const token = authHeader.substring(7);
|
|
3304
|
-
let claim;
|
|
3305
|
-
try {
|
|
3306
|
-
claim = UserClaimSchema.parse(JSON.parse(Buffer.from(token, "base64").toString("utf8")));
|
|
3307
|
-
} catch (err) {
|
|
3308
|
-
console.error(colors.red(`Failed to parse user claim from token: ${err instanceof Error ? err.message : String(err)}`));
|
|
3309
|
-
return authDb;
|
|
3310
|
-
}
|
|
3311
|
-
if (claim.type === "superUser") return client;
|
|
3312
|
-
else return authDb.$setAuth(claim.data);
|
|
3313
|
-
}
|
|
3314
3164
|
function startServer(client, schema, options, authDb) {
|
|
3315
3165
|
const server = serve({
|
|
3316
3166
|
fetch: createProxyApp({
|