@zjazj/server 0.0.3 → 0.0.5
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 +4 -0
- package/dist/{chunk-SVZCJQNB.js → chunk-PIZAN32W.js} +1 -1
- package/dist/{chunk-6IYKARAJ.js → chunk-UWDDQH25.js} +4 -0
- package/dist/{db-CWKXPFWA.d.ts → db-Dp7BB3Kc.d.ts} +24 -5
- package/dist/db.d.ts +1 -1
- package/dist/db.js +6 -5
- package/dist/index.d.ts +2 -2
- package/dist/index.js +24 -3
- package/dist/testing.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,4 +37,8 @@ Routes go on one of four routers:
|
|
|
37
37
|
|
|
38
38
|
`callPlatform(c, path)` is the calling side of `app.delegated`: it asks the control plane with the token of the request's user and returns the answer.
|
|
39
39
|
|
|
40
|
+
Everything under `/internal` answers only to a caller that reaches the app directly. A request that a proxy passed on, which the headers the proxy adds give away, gets a 404.
|
|
41
|
+
|
|
42
|
+
`tenantTable` and `globalTable` take the table's further indexes and constraints as a fourth argument, written as for Drizzle's `pgTable`. `drizzle-orm` and `postgres` are optional: an app without a database does not install them.
|
|
43
|
+
|
|
40
44
|
Part of [platform-cli](https://github.com/zjazj/platform-cli). All `@zjazj/*` packages share one version. Install them at the same version.
|
|
@@ -93,6 +93,9 @@ function isValidAppName(value) {
|
|
|
93
93
|
function isValidAppId(value) {
|
|
94
94
|
return typeof value === "string" && APP_ID_PATTERN.test(value);
|
|
95
95
|
}
|
|
96
|
+
function appBasePath(name) {
|
|
97
|
+
return `/apps/${name}/`;
|
|
98
|
+
}
|
|
96
99
|
function assertValidAppId(value) {
|
|
97
100
|
if (!isValidAppId(value)) {
|
|
98
101
|
throw new Error(
|
|
@@ -136,6 +139,7 @@ export {
|
|
|
136
139
|
isUuid,
|
|
137
140
|
isValidAppName,
|
|
138
141
|
isValidAppId,
|
|
142
|
+
appBasePath,
|
|
139
143
|
assertValidAppId,
|
|
140
144
|
generateAppId,
|
|
141
145
|
uuidv7
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as drizzle_orm from 'drizzle-orm';
|
|
2
|
-
import { BuildColumns } from 'drizzle-orm';
|
|
2
|
+
import { BuildColumns, BuildExtraConfigColumns } from 'drizzle-orm';
|
|
3
3
|
import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
|
|
4
|
-
import { PgDatabase, PgColumnBuilderBase, PgTableWithColumns, PgSchema } from 'drizzle-orm/pg-core';
|
|
4
|
+
import { PgDatabase, PgColumnBuilderBase, PgTableWithColumns, PgTableExtraConfigValue, PgSchema } from 'drizzle-orm/pg-core';
|
|
5
5
|
import { Context, MiddlewareHandler } from 'hono';
|
|
6
6
|
|
|
7
7
|
/** `name` in package.json: the URL segment and the app code in the registry. */
|
|
@@ -12,6 +12,13 @@ declare const APP_ID_PATTERN: RegExp;
|
|
|
12
12
|
declare function isUuid(value: unknown): value is string;
|
|
13
13
|
declare function isValidAppName(value: unknown): value is string;
|
|
14
14
|
declare function isValidAppId(value: unknown): value is string;
|
|
15
|
+
/**
|
|
16
|
+
* Where the app of this name is served on the platform's address, with a trailing slash:
|
|
17
|
+
* `/apps/<name>/`. A deployed app uses it unless `BASE_URL` says otherwise. The control
|
|
18
|
+
* plane uses it to route an app and to tell the build which path to build for. An app
|
|
19
|
+
* itself never needs it: links and API calls already carry the base path.
|
|
20
|
+
*/
|
|
21
|
+
declare function appBasePath(name: string): string;
|
|
15
22
|
/** Generate a new app id. Called once, by `platform create`. */
|
|
16
23
|
declare function generateAppId(): string;
|
|
17
24
|
/** A time-ordered UUID (version 7). Use it for primary keys. */
|
|
@@ -104,9 +111,20 @@ declare function tenantColumns(): {
|
|
|
104
111
|
/**
|
|
105
112
|
* Define a table whose rows belong to a tenant. It gets `id`, `tenant_id`, `created_at`
|
|
106
113
|
* and `updated_at`, an index on `tenant_id`, and row-level security once migrated.
|
|
114
|
+
*
|
|
115
|
+
* `extras` declares further indexes and constraints, as the third argument of Drizzle's
|
|
116
|
+
* `pgTable` does. It gets the tenant columns too, so a value that must be unique inside a
|
|
117
|
+
* tenant is `uniqueIndex('items_tenant_code_key').on(table.tenantId, table.code)`. A unique
|
|
118
|
+
* index without `tenantId` is unique across all tenants, and tells one tenant what another
|
|
119
|
+
* one has.
|
|
107
120
|
*/
|
|
108
|
-
declare function tenantTable<TName extends string, TColumns extends Record<string, PgColumnBuilderBase>>(schema: PgSchema<string>, name: TName, columns: TColumns): TenantTable<TName, TColumns>;
|
|
121
|
+
declare function tenantTable<TName extends string, TColumns extends Record<string, PgColumnBuilderBase>>(schema: PgSchema<string>, name: TName, columns: TColumns, extras?: TableExtras<TName, TenantColumnBuilders & TColumns>): TenantTable<TName, TColumns>;
|
|
109
122
|
type TenantColumnBuilders = ReturnType<typeof tenantColumns>;
|
|
123
|
+
/**
|
|
124
|
+
* Further indexes and constraints of a table: a function that gets the table's columns and
|
|
125
|
+
* returns a list, as in Drizzle's `pgTable`.
|
|
126
|
+
*/
|
|
127
|
+
type TableExtras<TName extends string, TColumns extends Record<string, PgColumnBuilderBase>> = (table: BuildExtraConfigColumns<TName, TColumns, 'pg'>) => PgTableExtraConfigValue[];
|
|
110
128
|
/** A table created by `tenantTable`: the given columns plus the tenant columns. */
|
|
111
129
|
type TenantTable<TName extends string, TColumns extends Record<string, PgColumnBuilderBase>> = PgTableWithColumns<{
|
|
112
130
|
name: TName;
|
|
@@ -124,7 +142,8 @@ type GlobalTable<TName extends string, TColumns extends Record<string, PgColumnB
|
|
|
124
142
|
/**
|
|
125
143
|
* Define a table shared by all tenants, such as a lookup list. It has no `tenant_id` and
|
|
126
144
|
* no row-level security. Using this helper states that the table is global on purpose.
|
|
145
|
+
* `extras` declares indexes and constraints, as for `tenantTable`.
|
|
127
146
|
*/
|
|
128
|
-
declare function globalTable<TName extends string, TColumns extends Record<string, PgColumnBuilderBase>>(schema: PgSchema<string>, name: TName, columns: TColumns): GlobalTable<TName, TColumns>;
|
|
147
|
+
declare function globalTable<TName extends string, TColumns extends Record<string, PgColumnBuilderBase>>(schema: PgSchema<string>, name: TName, columns: TColumns, extras?: TableExtras<TName, TColumns>): GlobalTable<TName, TColumns>;
|
|
129
148
|
|
|
130
|
-
export { APP_ID_PATTERN as A, type CreateDatabaseOptions as C, type Database as D, type GlobalTable as G, type MigrateOptions as M, type
|
|
149
|
+
export { APP_ID_PATTERN as A, type CreateDatabaseOptions as C, type Database as D, type GlobalTable as G, type MigrateOptions as M, type TableExtras as T, APP_NAME_PATTERN as a, appBasePath as b, isValidAppId as c, isValidAppName as d, type Db as e, type TenantTable as f, generateAppId as g, appSchema as h, isUuid as i, applyTenantPolicies as j, createDatabase as k, db as l, globalTable as m, migrate as n, tenantDatabase as o, tenantTable as p, withTenant as q, tenantColumns as t, uuidv7 as u, withAllTenants as w };
|
package/dist/db.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import 'drizzle-orm';
|
|
2
2
|
import 'drizzle-orm/pg-core';
|
|
3
3
|
import 'hono';
|
|
4
|
-
export { C as CreateDatabaseOptions, D as Database,
|
|
4
|
+
export { C as CreateDatabaseOptions, D as Database, e as Db, G as GlobalTable, M as MigrateOptions, T as TableExtras, f as TenantTable, h as appSchema, j as applyTenantPolicies, k as createDatabase, l as db, m as globalTable, n as migrate, t as tenantColumns, o as tenantDatabase, p as tenantTable, u as uuidv7, w as withAllTenants, q as withTenant } from './db-Dp7BB3Kc.js';
|
package/dist/db.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
UUID_PATTERN,
|
|
4
4
|
assertValidAppId,
|
|
5
5
|
uuidv7
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-UWDDQH25.js";
|
|
7
7
|
import "./chunk-ERGI2X5S.js";
|
|
8
8
|
|
|
9
9
|
// src/db.ts
|
|
@@ -250,14 +250,15 @@ function tenantColumns() {
|
|
|
250
250
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow().$onUpdate(() => /* @__PURE__ */ new Date())
|
|
251
251
|
};
|
|
252
252
|
}
|
|
253
|
-
function tenantTable(schema, name, columns) {
|
|
253
|
+
function tenantTable(schema, name, columns, extras) {
|
|
254
254
|
return schema.table(name, { ...tenantColumns(), ...columns }, (table) => [
|
|
255
255
|
// biome-ignore lint/suspicious/noExplicitAny: the generic spread hides the column type
|
|
256
|
-
index(`${name}_tenant_id_idx`).on(table.tenantId)
|
|
256
|
+
index(`${name}_tenant_id_idx`).on(table.tenantId),
|
|
257
|
+
...extras?.(table) ?? []
|
|
257
258
|
]);
|
|
258
259
|
}
|
|
259
|
-
function globalTable(schema, name, columns) {
|
|
260
|
-
return schema.table(name, columns);
|
|
260
|
+
function globalTable(schema, name, columns, extras) {
|
|
261
|
+
return schema.table(name, columns, extras);
|
|
261
262
|
}
|
|
262
263
|
export {
|
|
263
264
|
appSchema,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Hono, Context, ErrorHandler, NotFoundHandler, MiddlewareHandler } from 'hono';
|
|
2
2
|
import { V as Verifier, A as AuthUser } from './auth-DqrXYPCf.js';
|
|
3
3
|
export { a as AuthenticateOptions, O as ORGANIZATION_AUDIENCE_PREFIX, b as VerifierOptions, c as authenticate, d as bearerToken, e as createVerifier, g as getUser, r as requirePermission, f as requireRole, u as userFromClaims, v as verifierFromEnv } from './auth-DqrXYPCf.js';
|
|
4
|
-
import { D as Database } from './db-
|
|
5
|
-
export { A as APP_ID_PATTERN, a as APP_NAME_PATTERN, g as generateAppId, i as isUuid,
|
|
4
|
+
import { D as Database } from './db-Dp7BB3Kc.js';
|
|
5
|
+
export { A as APP_ID_PATTERN, a as APP_NAME_PATTERN, b as appBasePath, g as generateAppId, i as isUuid, c as isValidAppId, d as isValidAppName, u as uuidv7 } from './db-Dp7BB3Kc.js';
|
|
6
6
|
import 'jose';
|
|
7
7
|
import 'drizzle-orm';
|
|
8
8
|
import 'drizzle-orm/pg-core';
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
requireRole,
|
|
17
17
|
userFromClaims,
|
|
18
18
|
verifierFromEnv
|
|
19
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-PIZAN32W.js";
|
|
20
20
|
import {
|
|
21
21
|
APP_ID_PATTERN,
|
|
22
22
|
APP_NAME_PATTERN,
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
NotFoundError,
|
|
27
27
|
UnauthorizedError,
|
|
28
28
|
ValidationError,
|
|
29
|
+
appBasePath,
|
|
29
30
|
assertValidAppId,
|
|
30
31
|
errorHandler,
|
|
31
32
|
generateAppId,
|
|
@@ -35,7 +36,7 @@ import {
|
|
|
35
36
|
notFoundHandler,
|
|
36
37
|
toErrorResponse,
|
|
37
38
|
uuidv7
|
|
38
|
-
} from "./chunk-
|
|
39
|
+
} from "./chunk-UWDDQH25.js";
|
|
39
40
|
import {
|
|
40
41
|
isDevIdentityEnabled
|
|
41
42
|
} from "./chunk-NMKEVYGN.js";
|
|
@@ -123,6 +124,24 @@ function databaseCache() {
|
|
|
123
124
|
holder[DATABASES] ??= /* @__PURE__ */ new Map();
|
|
124
125
|
return holder[DATABASES];
|
|
125
126
|
}
|
|
127
|
+
var PROXY_HEADERS = [
|
|
128
|
+
"forwarded",
|
|
129
|
+
"via",
|
|
130
|
+
"x-forwarded-for",
|
|
131
|
+
"x-forwarded-host",
|
|
132
|
+
"x-forwarded-port",
|
|
133
|
+
"x-forwarded-proto",
|
|
134
|
+
"x-forwarded-server",
|
|
135
|
+
"x-real-ip"
|
|
136
|
+
];
|
|
137
|
+
var internalCallersOnly = async (c, next) => {
|
|
138
|
+
const header = PROXY_HEADERS.find((name) => c.req.header(name) !== void 0);
|
|
139
|
+
if (header) {
|
|
140
|
+
log.warn("Refused an internal route to a request that came through a proxy", { header });
|
|
141
|
+
return c.notFound();
|
|
142
|
+
}
|
|
143
|
+
await next();
|
|
144
|
+
};
|
|
126
145
|
function normalizeBasePath(value) {
|
|
127
146
|
const trimmed = (value ?? "/").trim().replace(/\/+$/, "");
|
|
128
147
|
if (trimmed === "") return "";
|
|
@@ -136,7 +155,7 @@ function createApp(options) {
|
|
|
136
155
|
}
|
|
137
156
|
assertValidAppId(manifest.id);
|
|
138
157
|
const basePath = normalizeBasePath(
|
|
139
|
-
env.BASE_URL || (env.NODE_ENV === "production" ?
|
|
158
|
+
env.BASE_URL || (env.NODE_ENV === "production" ? appBasePath(manifest.name) : "/")
|
|
140
159
|
);
|
|
141
160
|
configureLog({ app: manifest.name, env: env.ENV || env.NODE_ENV || "development" });
|
|
142
161
|
const databaseOptions = typeof options.database === "object" ? options.database : options.database ? {} : void 0;
|
|
@@ -186,6 +205,7 @@ function createApp(options) {
|
|
|
186
205
|
root.onError(errorHandler((error) => log.error("Unhandled error", { err: error })));
|
|
187
206
|
root.notFound(notFoundHandler());
|
|
188
207
|
root.get("/healthz", (c) => c.json({ status: "ok", app: manifest.name }));
|
|
208
|
+
root.use("/internal/*", internalCallersOnly);
|
|
189
209
|
root.route("/internal/delegated", delegated);
|
|
190
210
|
root.all("/internal/delegated/*", (c) => c.notFound());
|
|
191
211
|
root.route("/internal", internal);
|
|
@@ -479,6 +499,7 @@ export {
|
|
|
479
499
|
UnauthorizedError,
|
|
480
500
|
UnsafeUrlError,
|
|
481
501
|
ValidationError,
|
|
502
|
+
appBasePath,
|
|
482
503
|
assertPublicUrl,
|
|
483
504
|
authenticate,
|
|
484
505
|
bearerToken,
|
package/dist/testing.js
CHANGED
package/package.json
CHANGED