@zjazj/server 0.0.4 → 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/{db-BmTPWKyI.d.ts → db-Dp7BB3Kc.d.ts} +17 -5
- package/dist/db.d.ts +1 -1
- package/dist/db.js +5 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +19 -0
- 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.
|
|
@@ -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. */
|
|
@@ -111,9 +111,20 @@ declare function tenantColumns(): {
|
|
|
111
111
|
/**
|
|
112
112
|
* Define a table whose rows belong to a tenant. It gets `id`, `tenant_id`, `created_at`
|
|
113
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.
|
|
114
120
|
*/
|
|
115
|
-
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>;
|
|
116
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[];
|
|
117
128
|
/** A table created by `tenantTable`: the given columns plus the tenant columns. */
|
|
118
129
|
type TenantTable<TName extends string, TColumns extends Record<string, PgColumnBuilderBase>> = PgTableWithColumns<{
|
|
119
130
|
name: TName;
|
|
@@ -131,7 +142,8 @@ type GlobalTable<TName extends string, TColumns extends Record<string, PgColumnB
|
|
|
131
142
|
/**
|
|
132
143
|
* Define a table shared by all tenants, such as a lookup list. It has no `tenant_id` and
|
|
133
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`.
|
|
134
146
|
*/
|
|
135
|
-
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>;
|
|
136
148
|
|
|
137
|
-
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, e as Db, G as GlobalTable, M as MigrateOptions, T as
|
|
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
|
@@ -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, b as appBasePath, g as generateAppId, i as isUuid, c as isValidAppId, d as isValidAppName, u as uuidv7 } from './db-
|
|
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
|
@@ -124,6 +124,24 @@ function databaseCache() {
|
|
|
124
124
|
holder[DATABASES] ??= /* @__PURE__ */ new Map();
|
|
125
125
|
return holder[DATABASES];
|
|
126
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
|
+
};
|
|
127
145
|
function normalizeBasePath(value) {
|
|
128
146
|
const trimmed = (value ?? "/").trim().replace(/\/+$/, "");
|
|
129
147
|
if (trimmed === "") return "";
|
|
@@ -187,6 +205,7 @@ function createApp(options) {
|
|
|
187
205
|
root.onError(errorHandler((error) => log.error("Unhandled error", { err: error })));
|
|
188
206
|
root.notFound(notFoundHandler());
|
|
189
207
|
root.get("/healthz", (c) => c.json({ status: "ok", app: manifest.name }));
|
|
208
|
+
root.use("/internal/*", internalCallersOnly);
|
|
190
209
|
root.route("/internal/delegated", delegated);
|
|
191
210
|
root.all("/internal/delegated/*", (c) => c.notFound());
|
|
192
211
|
root.route("/internal", internal);
|
package/package.json
CHANGED