@rebasepro/server-core 0.0.1-canary.09e5ec5
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/LICENSE +6 -0
- package/README.md +40 -0
- package/build-errors.txt +52 -0
- package/coverage/clover.xml +3739 -0
- package/coverage/coverage-final.json +31 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +266 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/api/ast-schema-editor.ts.html +952 -0
- package/coverage/lcov-report/src/api/errors.ts.html +472 -0
- package/coverage/lcov-report/src/api/graphql/graphql-schema-generator.ts.html +1069 -0
- package/coverage/lcov-report/src/api/graphql/index.html +116 -0
- package/coverage/lcov-report/src/api/index.html +176 -0
- package/coverage/lcov-report/src/api/openapi-generator.ts.html +565 -0
- package/coverage/lcov-report/src/api/rest/api-generator.ts.html +994 -0
- package/coverage/lcov-report/src/api/rest/index.html +131 -0
- package/coverage/lcov-report/src/api/rest/query-parser.ts.html +550 -0
- package/coverage/lcov-report/src/api/schema-editor-routes.ts.html +202 -0
- package/coverage/lcov-report/src/api/server.ts.html +823 -0
- package/coverage/lcov-report/src/auth/admin-routes.ts.html +973 -0
- package/coverage/lcov-report/src/auth/index.html +176 -0
- package/coverage/lcov-report/src/auth/jwt.ts.html +574 -0
- package/coverage/lcov-report/src/auth/middleware.ts.html +745 -0
- package/coverage/lcov-report/src/auth/password.ts.html +310 -0
- package/coverage/lcov-report/src/auth/services.ts.html +2074 -0
- package/coverage/lcov-report/src/collections/index.html +116 -0
- package/coverage/lcov-report/src/collections/loader.ts.html +232 -0
- package/coverage/lcov-report/src/db/auth-schema.ts.html +523 -0
- package/coverage/lcov-report/src/db/data-transformer.ts.html +1753 -0
- package/coverage/lcov-report/src/db/entityService.ts.html +700 -0
- package/coverage/lcov-report/src/db/index.html +146 -0
- package/coverage/lcov-report/src/db/services/EntityFetchService.ts.html +4048 -0
- package/coverage/lcov-report/src/db/services/EntityPersistService.ts.html +883 -0
- package/coverage/lcov-report/src/db/services/RelationService.ts.html +3121 -0
- package/coverage/lcov-report/src/db/services/entity-helpers.ts.html +442 -0
- package/coverage/lcov-report/src/db/services/index.html +176 -0
- package/coverage/lcov-report/src/db/services/index.ts.html +124 -0
- package/coverage/lcov-report/src/generate-drizzle-schema-logic.ts.html +1960 -0
- package/coverage/lcov-report/src/index.html +116 -0
- package/coverage/lcov-report/src/services/driver-registry.ts.html +631 -0
- package/coverage/lcov-report/src/services/index.html +131 -0
- package/coverage/lcov-report/src/services/postgresDataDriver.ts.html +3025 -0
- package/coverage/lcov-report/src/storage/LocalStorageController.ts.html +1189 -0
- package/coverage/lcov-report/src/storage/S3StorageController.ts.html +970 -0
- package/coverage/lcov-report/src/storage/index.html +161 -0
- package/coverage/lcov-report/src/storage/storage-registry.ts.html +646 -0
- package/coverage/lcov-report/src/storage/types.ts.html +451 -0
- package/coverage/lcov-report/src/utils/drizzle-conditions.ts.html +3082 -0
- package/coverage/lcov-report/src/utils/index.html +116 -0
- package/coverage/lcov.info +7179 -0
- package/dist/common/src/collections/CollectionRegistry.d.ts +56 -0
- package/dist/common/src/collections/index.d.ts +1 -0
- package/dist/common/src/data/buildRebaseData.d.ts +14 -0
- package/dist/common/src/index.d.ts +3 -0
- package/dist/common/src/util/builders.d.ts +57 -0
- package/dist/common/src/util/callbacks.d.ts +6 -0
- package/dist/common/src/util/collections.d.ts +11 -0
- package/dist/common/src/util/common.d.ts +2 -0
- package/dist/common/src/util/conditions.d.ts +26 -0
- package/dist/common/src/util/entities.d.ts +58 -0
- package/dist/common/src/util/enums.d.ts +3 -0
- package/dist/common/src/util/index.d.ts +16 -0
- package/dist/common/src/util/navigation_from_path.d.ts +34 -0
- package/dist/common/src/util/navigation_utils.d.ts +20 -0
- package/dist/common/src/util/parent_references_from_path.d.ts +6 -0
- package/dist/common/src/util/paths.d.ts +14 -0
- package/dist/common/src/util/permissions.d.ts +5 -0
- package/dist/common/src/util/references.d.ts +2 -0
- package/dist/common/src/util/relations.d.ts +22 -0
- package/dist/common/src/util/resolutions.d.ts +72 -0
- package/dist/common/src/util/storage.d.ts +24 -0
- package/dist/index-DXVBFp5V.js +37 -0
- package/dist/index-DXVBFp5V.js.map +1 -0
- package/dist/index.es.js +49934 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +49968 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/server-core/src/api/ast-schema-editor.d.ts +21 -0
- package/dist/server-core/src/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
- package/dist/server-core/src/api/errors.d.ts +35 -0
- package/dist/server-core/src/api/graphql/graphql-schema-generator.d.ts +35 -0
- package/dist/server-core/src/api/graphql/index.d.ts +1 -0
- package/dist/server-core/src/api/index.d.ts +9 -0
- package/dist/server-core/src/api/openapi-generator.d.ts +16 -0
- package/dist/server-core/src/api/rest/api-generator.d.ts +64 -0
- package/dist/server-core/src/api/rest/index.d.ts +1 -0
- package/dist/server-core/src/api/rest/query-parser.d.ts +9 -0
- package/dist/server-core/src/api/schema-editor-routes.d.ts +3 -0
- package/dist/server-core/src/api/server.d.ts +40 -0
- package/dist/server-core/src/api/types.d.ts +90 -0
- package/dist/server-core/src/auth/admin-routes.d.ts +16 -0
- package/dist/server-core/src/auth/apple-oauth.d.ts +30 -0
- package/dist/server-core/src/auth/bitbucket-oauth.d.ts +11 -0
- package/dist/server-core/src/auth/discord-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/facebook-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/github-oauth.d.ts +15 -0
- package/dist/server-core/src/auth/gitlab-oauth.d.ts +13 -0
- package/dist/server-core/src/auth/google-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/index.d.ts +23 -0
- package/dist/server-core/src/auth/interfaces.d.ts +309 -0
- package/dist/server-core/src/auth/jwt.d.ts +43 -0
- package/dist/server-core/src/auth/linkedin-oauth.d.ts +18 -0
- package/dist/server-core/src/auth/microsoft-oauth.d.ts +16 -0
- package/dist/server-core/src/auth/middleware.d.ts +81 -0
- package/dist/server-core/src/auth/password.d.ts +22 -0
- package/dist/server-core/src/auth/rate-limiter.d.ts +31 -0
- package/dist/server-core/src/auth/routes.d.ts +27 -0
- package/dist/server-core/src/auth/slack-oauth.d.ts +12 -0
- package/dist/server-core/src/auth/spotify-oauth.d.ts +12 -0
- package/dist/server-core/src/auth/twitter-oauth.d.ts +18 -0
- package/dist/server-core/src/bootstrappers/index.d.ts +0 -0
- package/dist/server-core/src/collections/BackendCollectionRegistry.d.ts +13 -0
- package/dist/server-core/src/collections/loader.d.ts +5 -0
- package/dist/server-core/src/cron/cron-loader.d.ts +17 -0
- package/dist/server-core/src/cron/cron-routes.d.ts +14 -0
- package/dist/server-core/src/cron/cron-scheduler.d.ts +61 -0
- package/dist/server-core/src/cron/cron-store.d.ts +32 -0
- package/dist/server-core/src/cron/index.d.ts +6 -0
- package/dist/server-core/src/db/interfaces.d.ts +18 -0
- package/dist/server-core/src/email/index.d.ts +6 -0
- package/dist/server-core/src/email/smtp-email-service.d.ts +25 -0
- package/dist/server-core/src/email/templates.d.ts +42 -0
- package/dist/server-core/src/email/types.d.ts +107 -0
- package/dist/server-core/src/functions/function-loader.d.ts +17 -0
- package/dist/server-core/src/functions/function-routes.d.ts +10 -0
- package/dist/server-core/src/functions/index.d.ts +3 -0
- package/dist/server-core/src/history/history-routes.d.ts +23 -0
- package/dist/server-core/src/history/index.d.ts +1 -0
- package/dist/server-core/src/index.d.ts +29 -0
- package/dist/server-core/src/init.d.ts +159 -0
- package/dist/server-core/src/serve-spa.d.ts +30 -0
- package/dist/server-core/src/services/driver-registry.d.ts +78 -0
- package/dist/server-core/src/singleton.d.ts +35 -0
- package/dist/server-core/src/storage/LocalStorageController.d.ts +46 -0
- package/dist/server-core/src/storage/S3StorageController.d.ts +36 -0
- package/dist/server-core/src/storage/index.d.ts +25 -0
- package/dist/server-core/src/storage/routes.d.ts +38 -0
- package/dist/server-core/src/storage/storage-registry.d.ts +78 -0
- package/dist/server-core/src/storage/types.d.ts +103 -0
- package/dist/server-core/src/types/index.d.ts +11 -0
- package/dist/server-core/src/utils/dev-port.d.ts +35 -0
- package/dist/server-core/src/utils/logger.d.ts +31 -0
- package/dist/server-core/src/utils/logging.d.ts +9 -0
- package/dist/server-core/src/utils/request-logger.d.ts +19 -0
- package/dist/server-core/src/utils/sql.d.ts +27 -0
- package/dist/types/src/controllers/analytics_controller.d.ts +7 -0
- package/dist/types/src/controllers/auth.d.ts +119 -0
- package/dist/types/src/controllers/client.d.ts +170 -0
- package/dist/types/src/controllers/collection_registry.d.ts +45 -0
- package/dist/types/src/controllers/customization_controller.d.ts +60 -0
- package/dist/types/src/controllers/data.d.ts +168 -0
- package/dist/types/src/controllers/data_driver.d.ts +160 -0
- package/dist/types/src/controllers/database_admin.d.ts +11 -0
- package/dist/types/src/controllers/dialogs_controller.d.ts +36 -0
- package/dist/types/src/controllers/effective_role.d.ts +4 -0
- package/dist/types/src/controllers/email.d.ts +34 -0
- package/dist/types/src/controllers/index.d.ts +18 -0
- package/dist/types/src/controllers/local_config_persistence.d.ts +20 -0
- package/dist/types/src/controllers/navigation.d.ts +213 -0
- package/dist/types/src/controllers/registry.d.ts +54 -0
- package/dist/types/src/controllers/side_dialogs_controller.d.ts +67 -0
- package/dist/types/src/controllers/side_entity_controller.d.ts +90 -0
- package/dist/types/src/controllers/snackbar.d.ts +24 -0
- package/dist/types/src/controllers/storage.d.ts +171 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/rebase_context.d.ts +105 -0
- package/dist/types/src/types/backend.d.ts +536 -0
- package/dist/types/src/types/builders.d.ts +15 -0
- package/dist/types/src/types/chips.d.ts +5 -0
- package/dist/types/src/types/collections.d.ts +856 -0
- package/dist/types/src/types/cron.d.ts +102 -0
- package/dist/types/src/types/data_source.d.ts +64 -0
- package/dist/types/src/types/entities.d.ts +145 -0
- package/dist/types/src/types/entity_actions.d.ts +98 -0
- package/dist/types/src/types/entity_callbacks.d.ts +173 -0
- package/dist/types/src/types/entity_link_builder.d.ts +7 -0
- package/dist/types/src/types/entity_overrides.d.ts +10 -0
- package/dist/types/src/types/entity_views.d.ts +61 -0
- package/dist/types/src/types/export_import.d.ts +21 -0
- package/dist/types/src/types/index.d.ts +23 -0
- package/dist/types/src/types/locales.d.ts +4 -0
- package/dist/types/src/types/modify_collections.d.ts +5 -0
- package/dist/types/src/types/plugins.d.ts +279 -0
- package/dist/types/src/types/properties.d.ts +1176 -0
- package/dist/types/src/types/property_config.d.ts +70 -0
- package/dist/types/src/types/relations.d.ts +336 -0
- package/dist/types/src/types/slots.d.ts +252 -0
- package/dist/types/src/types/translations.d.ts +870 -0
- package/dist/types/src/types/user_management_delegate.d.ts +121 -0
- package/dist/types/src/types/websockets.d.ts +78 -0
- package/dist/types/src/users/index.d.ts +2 -0
- package/dist/types/src/users/roles.d.ts +22 -0
- package/dist/types/src/users/user.d.ts +46 -0
- package/history_diff.log +385 -0
- package/jest.config.cjs +16 -0
- package/package.json +86 -0
- package/scratch.ts +9 -0
- package/src/api/ast-schema-editor.ts +289 -0
- package/src/api/collections_for_test/callbacks_test_collection.ts +60 -0
- package/src/api/errors.ts +179 -0
- package/src/api/graphql/graphql-schema-generator.ts +336 -0
- package/src/api/graphql/index.ts +2 -0
- package/src/api/index.ts +11 -0
- package/src/api/openapi-generator.ts +715 -0
- package/src/api/rest/api-generator.ts +472 -0
- package/src/api/rest/index.ts +2 -0
- package/src/api/rest/query-parser.ts +155 -0
- package/src/api/schema-editor-routes.ts +41 -0
- package/src/api/server.ts +248 -0
- package/src/api/types.ts +90 -0
- package/src/auth/admin-routes.ts +529 -0
- package/src/auth/apple-oauth.ts +130 -0
- package/src/auth/bitbucket-oauth.ts +82 -0
- package/src/auth/discord-oauth.ts +83 -0
- package/src/auth/facebook-oauth.ts +72 -0
- package/src/auth/github-oauth.ts +110 -0
- package/src/auth/gitlab-oauth.ts +70 -0
- package/src/auth/google-oauth.ts +48 -0
- package/src/auth/index.ts +34 -0
- package/src/auth/interfaces.ts +363 -0
- package/src/auth/jwt.ts +181 -0
- package/src/auth/linkedin-oauth.ts +81 -0
- package/src/auth/microsoft-oauth.ts +88 -0
- package/src/auth/middleware.ts +384 -0
- package/src/auth/password.ts +77 -0
- package/src/auth/rate-limiter.ts +129 -0
- package/src/auth/routes.ts +788 -0
- package/src/auth/slack-oauth.ts +71 -0
- package/src/auth/spotify-oauth.ts +67 -0
- package/src/auth/twitter-oauth.ts +120 -0
- package/src/bootstrappers/index.ts +1 -0
- package/src/collections/BackendCollectionRegistry.ts +20 -0
- package/src/collections/loader.ts +49 -0
- package/src/cron/cron-loader.ts +89 -0
- package/src/cron/cron-routes.test.ts +265 -0
- package/src/cron/cron-routes.ts +85 -0
- package/src/cron/cron-scheduler.test.ts +421 -0
- package/src/cron/cron-scheduler.ts +413 -0
- package/src/cron/cron-store.ts +163 -0
- package/src/cron/index.ts +6 -0
- package/src/db/interfaces.ts +60 -0
- package/src/email/index.ts +18 -0
- package/src/email/smtp-email-service.ts +91 -0
- package/src/email/templates.ts +388 -0
- package/src/email/types.ts +105 -0
- package/src/functions/function-loader.ts +119 -0
- package/src/functions/function-routes.ts +31 -0
- package/src/functions/index.ts +3 -0
- package/src/history/history-routes.ts +129 -0
- package/src/history/index.ts +2 -0
- package/src/index.ts +66 -0
- package/src/init.ts +727 -0
- package/src/serve-spa.ts +81 -0
- package/src/services/driver-registry.ts +182 -0
- package/src/singleton.test.ts +28 -0
- package/src/singleton.ts +70 -0
- package/src/storage/LocalStorageController.ts +365 -0
- package/src/storage/S3StorageController.ts +298 -0
- package/src/storage/index.ts +43 -0
- package/src/storage/routes.ts +264 -0
- package/src/storage/storage-registry.ts +187 -0
- package/src/storage/types.ts +134 -0
- package/src/types/index.ts +27 -0
- package/src/utils/dev-port.ts +176 -0
- package/src/utils/logger.ts +143 -0
- package/src/utils/logging.ts +38 -0
- package/src/utils/request-logger.ts +66 -0
- package/src/utils/sql.ts +38 -0
- package/test/admin-routes.test.ts +640 -0
- package/test/api-generator.test.ts +501 -0
- package/test/ast-schema-editor.test.ts +63 -0
- package/test/auth-middleware-hono.test.ts +556 -0
- package/test/auth-routes.test.ts +1047 -0
- package/test/driver-registry.test.ts +282 -0
- package/test/error-propagation.test.ts +226 -0
- package/test/errors-hono.test.ts +133 -0
- package/test/errors.test.ts +155 -0
- package/test/jwt-security.test.ts +182 -0
- package/test/jwt.test.ts +324 -0
- package/test/middleware.test.ts +300 -0
- package/test/password.test.ts +165 -0
- package/test/query-parser.test.ts +263 -0
- package/test/rate-limiter.test.ts +102 -0
- package/test/safe-compare.test.ts +66 -0
- package/test/singleton.test.ts +59 -0
- package/test/storage-local.test.ts +271 -0
- package/test/storage-registry.test.ts +282 -0
- package/test/storage-routes.test.ts +222 -0
- package/test/storage-s3.test.ts +304 -0
- package/test-ast.ts +28 -0
- package/test.ts +6 -0
- package/test_output.txt +1133 -0
- package/tsconfig.json +49 -0
- package/tsconfig.prod.json +20 -0
- package/vite.config.ts +80 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { mapOperator, parseQueryOptions } from "../src/api/rest/query-parser";
|
|
2
|
+
|
|
3
|
+
// ─────────────────────────────────────────────────────────────
|
|
4
|
+
// mapOperator
|
|
5
|
+
// ─────────────────────────────────────────────────────────────
|
|
6
|
+
describe("mapOperator", () => {
|
|
7
|
+
it("maps PostgREST operators to Rebase operators", () => {
|
|
8
|
+
expect(mapOperator("eq")).toBe("==");
|
|
9
|
+
expect(mapOperator("neq")).toBe("!=");
|
|
10
|
+
expect(mapOperator("gt")).toBe(">");
|
|
11
|
+
expect(mapOperator("gte")).toBe(">=");
|
|
12
|
+
expect(mapOperator("lt")).toBe("<");
|
|
13
|
+
expect(mapOperator("lte")).toBe("<=");
|
|
14
|
+
expect(mapOperator("in")).toBe("in");
|
|
15
|
+
expect(mapOperator("nin")).toBe("not-in");
|
|
16
|
+
expect(mapOperator("cs")).toBe("array-contains");
|
|
17
|
+
expect(mapOperator("csa")).toBe("array-contains-any");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("returns null for unknown operators", () => {
|
|
21
|
+
expect(mapOperator("like")).toBeNull();
|
|
22
|
+
expect(mapOperator("between")).toBeNull();
|
|
23
|
+
expect(mapOperator("")).toBeNull();
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// ─────────────────────────────────────────────────────────────
|
|
28
|
+
// parseQueryOptions — Pagination
|
|
29
|
+
// ─────────────────────────────────────────────────────────────
|
|
30
|
+
describe("parseQueryOptions — pagination", () => {
|
|
31
|
+
it("parses limit", () => {
|
|
32
|
+
const result = parseQueryOptions({ limit: "25" });
|
|
33
|
+
expect(result.limit).toBe(25);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("parses offset", () => {
|
|
37
|
+
const result = parseQueryOptions({ offset: "50" });
|
|
38
|
+
expect(result.offset).toBe(50);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("calculates offset from page number", () => {
|
|
42
|
+
const result = parseQueryOptions({ page: "3",
|
|
43
|
+
limit: "10" });
|
|
44
|
+
expect(result.offset).toBe(20); // (3-1) * 10
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("uses default limit of 20 for page calculation when limit not set", () => {
|
|
48
|
+
const result = parseQueryOptions({ page: "2" });
|
|
49
|
+
expect(result.offset).toBe(20); // (2-1) * 20
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("handles no pagination params", () => {
|
|
53
|
+
const result = parseQueryOptions({});
|
|
54
|
+
expect(result.limit).toBeUndefined();
|
|
55
|
+
expect(result.offset).toBeUndefined();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// ─────────────────────────────────────────────────────────────
|
|
60
|
+
// parseQueryOptions — PostgREST Filters
|
|
61
|
+
// ─────────────────────────────────────────────────────────────
|
|
62
|
+
describe("parseQueryOptions — PostgREST filters", () => {
|
|
63
|
+
it("parses equality filter (implicit eq)", () => {
|
|
64
|
+
const result = parseQueryOptions({ status: "published" });
|
|
65
|
+
expect(result.where?.status).toEqual(["==", "published"]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("parses eq operator explicitly", () => {
|
|
69
|
+
const result = parseQueryOptions({ status: "eq.published" });
|
|
70
|
+
expect(result.where?.status).toEqual(["==", "published"]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("parses gt with number coercion", () => {
|
|
74
|
+
const result = parseQueryOptions({ age: "gt.18" });
|
|
75
|
+
expect(result.where?.age).toEqual([">", 18]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("parses gte operator", () => {
|
|
79
|
+
const result = parseQueryOptions({ price: "gte.9.99" });
|
|
80
|
+
expect(result.where?.price).toEqual([">=", 9.99]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("parses lt operator", () => {
|
|
84
|
+
const result = parseQueryOptions({ count: "lt.100" });
|
|
85
|
+
expect(result.where?.count).toEqual(["<", 100]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("parses lte operator", () => {
|
|
89
|
+
const result = parseQueryOptions({ rating: "lte.5" });
|
|
90
|
+
expect(result.where?.rating).toEqual(["<=", 5]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("parses neq operator", () => {
|
|
94
|
+
const result = parseQueryOptions({ status: "neq.draft" });
|
|
95
|
+
expect(result.where?.status).toEqual(["!=", "draft"]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("parses boolean true", () => {
|
|
99
|
+
const result = parseQueryOptions({ active: "true" });
|
|
100
|
+
expect(result.where?.active).toEqual(["==", true]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("parses boolean false", () => {
|
|
104
|
+
const result = parseQueryOptions({ active: "false" });
|
|
105
|
+
expect(result.where?.active).toEqual(["==", false]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("parses null", () => {
|
|
109
|
+
const result = parseQueryOptions({ deleted_at: "null" });
|
|
110
|
+
expect(result.where?.deleted_at).toEqual(["==", null]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("parses numeric strings as numbers", () => {
|
|
114
|
+
const result = parseQueryOptions({ quantity: "42" });
|
|
115
|
+
expect(result.where?.quantity).toEqual(["==", 42]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("parses in operator with array", () => {
|
|
119
|
+
const result = parseQueryOptions({ role: "in.(admin,editor,viewer)" });
|
|
120
|
+
expect(result.where?.role).toEqual(["in", ["admin", "editor", "viewer"]]);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("parses in operator with numeric array", () => {
|
|
124
|
+
const result = parseQueryOptions({ priority: "in.(1,2,3)" });
|
|
125
|
+
expect(result.where?.priority).toEqual(["in", [1, 2, 3]]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("parses array-contains operator", () => {
|
|
129
|
+
const result = parseQueryOptions({ tags: "cs.javascript" });
|
|
130
|
+
expect(result.where?.tags).toEqual(["array-contains", "javascript"]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("skips reserved query keys", () => {
|
|
134
|
+
const result = parseQueryOptions({
|
|
135
|
+
limit: "10",
|
|
136
|
+
offset: "0",
|
|
137
|
+
orderBy: "name:asc",
|
|
138
|
+
status: "eq.active"
|
|
139
|
+
});
|
|
140
|
+
// Only status should be in where
|
|
141
|
+
expect(result.where?.status).toEqual(["==", "active"]);
|
|
142
|
+
expect(result.where?.limit).toBeUndefined();
|
|
143
|
+
expect(result.where?.offset).toBeUndefined();
|
|
144
|
+
expect(result.where?.orderBy).toBeUndefined();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("handles string values with dots that are not operators (fallback to eq)", () => {
|
|
148
|
+
const result = parseQueryOptions({ email: "user@example.com" });
|
|
149
|
+
// "user@example" is not a valid operator, so fallback to eq
|
|
150
|
+
expect(result.where?.email).toBeDefined();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("removes empty where object", () => {
|
|
154
|
+
const result = parseQueryOptions({ limit: "10" });
|
|
155
|
+
expect(result.where).toBeUndefined();
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// ─────────────────────────────────────────────────────────────
|
|
160
|
+
// parseQueryOptions — Legacy JSON where
|
|
161
|
+
// ─────────────────────────────────────────────────────────────
|
|
162
|
+
describe("parseQueryOptions — legacy JSON where", () => {
|
|
163
|
+
it("parses JSON where string", () => {
|
|
164
|
+
const result = parseQueryOptions({
|
|
165
|
+
where: JSON.stringify({ status: ["==", "published"] })
|
|
166
|
+
});
|
|
167
|
+
expect(result.where?.status).toEqual(["==", "published"]);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("accepts object where directly", () => {
|
|
171
|
+
const result = parseQueryOptions({
|
|
172
|
+
where: { status: ["==", "draft"] }
|
|
173
|
+
});
|
|
174
|
+
expect(result.where?.status).toEqual(["==", "draft"]);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("throws for malformed JSON where", () => {
|
|
178
|
+
expect(() => parseQueryOptions({ where: "not valid json{" })).toThrow("Invalid 'where' filter");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("throws for array where", () => {
|
|
182
|
+
expect(() => parseQueryOptions({ where: JSON.stringify([1, 2]) })).toThrow("Filter must be a JSON object");
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("throws for null where", () => {
|
|
186
|
+
expect(() => parseQueryOptions({ where: JSON.stringify(null) })).toThrow("Filter must be a JSON object");
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// ─────────────────────────────────────────────────────────────
|
|
191
|
+
// parseQueryOptions — Sorting
|
|
192
|
+
// ─────────────────────────────────────────────────────────────
|
|
193
|
+
describe("parseQueryOptions — sorting", () => {
|
|
194
|
+
it("parses JSON orderBy", () => {
|
|
195
|
+
const orderBy = JSON.stringify([{ field: "name",
|
|
196
|
+
direction: "asc" }]);
|
|
197
|
+
const result = parseQueryOptions({ orderBy });
|
|
198
|
+
expect(result.orderBy).toEqual([{ field: "name",
|
|
199
|
+
direction: "asc" }]);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("parses simple field:direction format", () => {
|
|
203
|
+
const result = parseQueryOptions({ orderBy: "created_at:desc" });
|
|
204
|
+
expect(result.orderBy).toEqual([{ field: "created_at",
|
|
205
|
+
direction: "desc" }]);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("defaults direction to asc", () => {
|
|
209
|
+
const result = parseQueryOptions({ orderBy: "name" });
|
|
210
|
+
expect(result.orderBy).toEqual([{ field: "name",
|
|
211
|
+
direction: "asc" }]);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("handles no orderBy", () => {
|
|
215
|
+
const result = parseQueryOptions({});
|
|
216
|
+
expect(result.orderBy).toBeUndefined();
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// ─────────────────────────────────────────────────────────────
|
|
221
|
+
// parseQueryOptions — Relation includes
|
|
222
|
+
// ─────────────────────────────────────────────────────────────
|
|
223
|
+
describe("parseQueryOptions — includes", () => {
|
|
224
|
+
it("parses wildcard include", () => {
|
|
225
|
+
const result = parseQueryOptions({ include: "*" });
|
|
226
|
+
expect(result.include).toEqual(["*"]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("parses comma-separated includes", () => {
|
|
230
|
+
const result = parseQueryOptions({ include: "author,tags,category" });
|
|
231
|
+
expect(result.include).toEqual(["author", "tags", "category"]);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("trims whitespace in includes", () => {
|
|
235
|
+
const result = parseQueryOptions({ include: " author , tags " });
|
|
236
|
+
expect(result.include).toEqual(["author", "tags"]);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("handles no include", () => {
|
|
240
|
+
const result = parseQueryOptions({});
|
|
241
|
+
expect(result.include).toBeUndefined();
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// ─────────────────────────────────────────────────────────────
|
|
246
|
+
// parseQueryOptions — Field selection
|
|
247
|
+
// ─────────────────────────────────────────────────────────────
|
|
248
|
+
describe("parseQueryOptions — fields", () => {
|
|
249
|
+
it("parses comma-separated fields", () => {
|
|
250
|
+
const result = parseQueryOptions({ fields: "id,name,email" });
|
|
251
|
+
expect(result.fields).toEqual(["id", "name", "email"]);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("trims whitespace", () => {
|
|
255
|
+
const result = parseQueryOptions({ fields: " id , name " });
|
|
256
|
+
expect(result.fields).toEqual(["id", "name"]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("handles no fields", () => {
|
|
260
|
+
const result = parseQueryOptions({});
|
|
261
|
+
expect(result.fields).toBeUndefined();
|
|
262
|
+
});
|
|
263
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createRateLimiter } from "../src/auth/rate-limiter";
|
|
2
|
+
import { Hono } from "hono";
|
|
3
|
+
import { HonoEnv } from "../src/api/types";
|
|
4
|
+
|
|
5
|
+
describe("Rate Limiter", () => {
|
|
6
|
+
|
|
7
|
+
function createTestApp(options: { windowMs?: number; limit?: number } = {}) {
|
|
8
|
+
const app = new Hono<HonoEnv>();
|
|
9
|
+
const limiter = createRateLimiter({
|
|
10
|
+
windowMs: options.windowMs ?? 60 * 1000, // 1 minute
|
|
11
|
+
limit: options.limit ?? 3,
|
|
12
|
+
keyGenerator: (c) => c.req.header("x-forwarded-for") || "test-ip"
|
|
13
|
+
});
|
|
14
|
+
app.use("/api/*", limiter);
|
|
15
|
+
app.get("/api/test", (c) => c.json({ ok: true }));
|
|
16
|
+
return app;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
it("allows requests under the limit", async () => {
|
|
20
|
+
const app = createTestApp({ limit: 5 });
|
|
21
|
+
|
|
22
|
+
const res = await app.request("/api/test", {
|
|
23
|
+
headers: { "x-forwarded-for": "1.2.3.4" }
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
expect(res.status).toBe(200);
|
|
27
|
+
expect(res.headers.get("X-RateLimit-Limit")).toBe("5");
|
|
28
|
+
expect(res.headers.get("X-RateLimit-Remaining")).toBe("4");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("returns 429 when limit is exceeded", async () => {
|
|
32
|
+
const app = createTestApp({ limit: 2 });
|
|
33
|
+
|
|
34
|
+
// First two should pass
|
|
35
|
+
await app.request("/api/test", { headers: { "x-forwarded-for": "10.0.0.1" } });
|
|
36
|
+
await app.request("/api/test", { headers: { "x-forwarded-for": "10.0.0.1" } });
|
|
37
|
+
|
|
38
|
+
// Third should be rate limited
|
|
39
|
+
const res = await app.request("/api/test", {
|
|
40
|
+
headers: { "x-forwarded-for": "10.0.0.1" }
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
expect(res.status).toBe(429);
|
|
44
|
+
const body = await res.json() as any;
|
|
45
|
+
expect(body.error.code).toBe("RATE_LIMITED");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("includes Retry-After header when rate limited", async () => {
|
|
49
|
+
const app = createTestApp({ limit: 1 });
|
|
50
|
+
|
|
51
|
+
await app.request("/api/test", { headers: { "x-forwarded-for": "10.0.0.2" } });
|
|
52
|
+
const res = await app.request("/api/test", {
|
|
53
|
+
headers: { "x-forwarded-for": "10.0.0.2" }
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
expect(res.headers.get("Retry-After")).toBeDefined();
|
|
57
|
+
expect(res.headers.get("X-RateLimit-Remaining")).toBe("0");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("tracks different IPs separately", async () => {
|
|
61
|
+
const app = createTestApp({ limit: 1 });
|
|
62
|
+
|
|
63
|
+
const res1 = await app.request("/api/test", {
|
|
64
|
+
headers: { "x-forwarded-for": "ip-a" }
|
|
65
|
+
});
|
|
66
|
+
const res2 = await app.request("/api/test", {
|
|
67
|
+
headers: { "x-forwarded-for": "ip-b" }
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(res1.status).toBe(200);
|
|
71
|
+
expect(res2.status).toBe(200);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("decrements remaining count with each request", async () => {
|
|
75
|
+
const app = createTestApp({ limit: 3 });
|
|
76
|
+
const ip = "counter-ip";
|
|
77
|
+
|
|
78
|
+
const r1 = await app.request("/api/test", { headers: { "x-forwarded-for": ip } });
|
|
79
|
+
expect(r1.headers.get("X-RateLimit-Remaining")).toBe("2");
|
|
80
|
+
|
|
81
|
+
const r2 = await app.request("/api/test", { headers: { "x-forwarded-for": ip } });
|
|
82
|
+
expect(r2.headers.get("X-RateLimit-Remaining")).toBe("1");
|
|
83
|
+
|
|
84
|
+
const r3 = await app.request("/api/test", { headers: { "x-forwarded-for": ip } });
|
|
85
|
+
expect(r3.headers.get("X-RateLimit-Remaining")).toBe("0");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("uses custom message", async () => {
|
|
89
|
+
const app = new Hono<HonoEnv>();
|
|
90
|
+
const limiter = createRateLimiter({
|
|
91
|
+
limit: 0,
|
|
92
|
+
message: "Slow down!",
|
|
93
|
+
keyGenerator: () => "always-same"
|
|
94
|
+
});
|
|
95
|
+
app.use("/api/*", limiter);
|
|
96
|
+
app.get("/api/test", (c) => c.json({ ok: true }));
|
|
97
|
+
|
|
98
|
+
const res = await app.request("/api/test");
|
|
99
|
+
const body = await res.json() as any;
|
|
100
|
+
expect(body.error.message).toBe("Slow down!");
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the safeCompare function used in auth middleware.
|
|
3
|
+
*
|
|
4
|
+
* Since safeCompare is not exported, we test it indirectly through
|
|
5
|
+
* the createAuthMiddleware behavior with service keys. However, for
|
|
6
|
+
* unit-level verification we replicate the logic here.
|
|
7
|
+
*/
|
|
8
|
+
import { timingSafeEqual } from "crypto";
|
|
9
|
+
|
|
10
|
+
// Replicate the safeCompare implementation to test in isolation
|
|
11
|
+
function safeCompare(a: string, b: string): boolean {
|
|
12
|
+
const maxLen = Math.max(a.length, b.length);
|
|
13
|
+
const bufA = Buffer.alloc(maxLen);
|
|
14
|
+
const bufB = Buffer.alloc(maxLen);
|
|
15
|
+
bufA.write(a);
|
|
16
|
+
bufB.write(b);
|
|
17
|
+
try {
|
|
18
|
+
const isEqual = timingSafeEqual(bufA, bufB);
|
|
19
|
+
return isEqual && a.length === b.length;
|
|
20
|
+
} catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("safeCompare", () => {
|
|
26
|
+
it("should return true for identical strings", () => {
|
|
27
|
+
expect(safeCompare("my-secret-key-12345678901234567890", "my-secret-key-12345678901234567890")).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return false for different strings of same length", () => {
|
|
31
|
+
expect(safeCompare("aaaa", "bbbb")).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should return false for different length strings", () => {
|
|
35
|
+
expect(safeCompare("short", "a-much-longer-string")).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should return false when one string is a prefix of the other", () => {
|
|
39
|
+
// This is the critical case: "abc" vs "abc\0\0\0" would match
|
|
40
|
+
// without the length check, since Buffer.alloc zero-fills
|
|
41
|
+
expect(safeCompare("abc", "abc\0\0\0")).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should return false for empty string vs non-empty", () => {
|
|
45
|
+
expect(safeCompare("", "something")).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should return true for two empty strings", () => {
|
|
49
|
+
expect(safeCompare("", "")).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should handle unicode strings", () => {
|
|
53
|
+
expect(safeCompare("café", "café")).toBe(true);
|
|
54
|
+
expect(safeCompare("café", "cafe")).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should not leak length information via early return", () => {
|
|
58
|
+
// Both comparisons should take similar time (constant-time)
|
|
59
|
+
// We can't easily assert timing, but we verify they both
|
|
60
|
+
// go through the same code path
|
|
61
|
+
const result1 = safeCompare("a", "bb");
|
|
62
|
+
const result2 = safeCompare("aa", "bb");
|
|
63
|
+
expect(result1).toBe(false);
|
|
64
|
+
expect(result2).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { rebase, _initRebase, _setRebaseMock, _resetRebaseMock } from "../src/singleton";
|
|
2
|
+
import type { RebaseClient } from "@rebasepro/types";
|
|
3
|
+
|
|
4
|
+
describe("rebase singleton", () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
// Reset after each test to avoid interference
|
|
7
|
+
process.env.NODE_ENV = "test";
|
|
8
|
+
_resetRebaseMock();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should throw when accessing properties before initialization", () => {
|
|
12
|
+
expect(() => rebase.data).toThrowError(/server not initialized yet/);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should allow property access after initialization", () => {
|
|
16
|
+
const mockData = { find: jest.fn() };
|
|
17
|
+
_setRebaseMock({ data: mockData } as Partial<RebaseClient>);
|
|
18
|
+
|
|
19
|
+
expect(rebase.data).toBe(mockData);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should throw on property assignment (set trap)", () => {
|
|
23
|
+
_setRebaseMock({ data: {} } as Partial<RebaseClient>);
|
|
24
|
+
|
|
25
|
+
expect(() => {
|
|
26
|
+
(rebase as Record<string, unknown>).data = "overwritten";
|
|
27
|
+
}).toThrowError(/Cannot set rebase\.data directly/);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should reset properly with _resetRebaseMock", () => {
|
|
31
|
+
_setRebaseMock({ data: {} } as Partial<RebaseClient>);
|
|
32
|
+
expect(() => rebase.data).not.toThrow();
|
|
33
|
+
|
|
34
|
+
_resetRebaseMock();
|
|
35
|
+
expect(() => rebase.data).toThrowError(/server not initialized yet/);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should throw if _setRebaseMock is called outside test env", () => {
|
|
39
|
+
const originalEnv = process.env.NODE_ENV;
|
|
40
|
+
process.env.NODE_ENV = "production";
|
|
41
|
+
|
|
42
|
+
expect(() => _setRebaseMock({} as Partial<RebaseClient>)).toThrowError(
|
|
43
|
+
/can only be called in a test environment/
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
process.env.NODE_ENV = originalEnv;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should throw if _resetRebaseMock is called outside test env", () => {
|
|
50
|
+
const originalEnv = process.env.NODE_ENV;
|
|
51
|
+
process.env.NODE_ENV = "production";
|
|
52
|
+
|
|
53
|
+
expect(() => _resetRebaseMock()).toThrowError(
|
|
54
|
+
/can only be called in a test environment/
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
process.env.NODE_ENV = originalEnv;
|
|
58
|
+
});
|
|
59
|
+
});
|