@rocicorp/zero 1.4.0-canary.6 → 1.5.0-canary.0
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/out/zero/package.js +1 -1
- package/out/zero/package.js.map +1 -1
- package/out/zero-cache/src/auth/auth.d.ts +1 -1
- package/out/zero-cache/src/auth/auth.d.ts.map +1 -1
- package/out/zero-cache/src/auth/auth.js +1 -1
- package/out/zero-cache/src/auth/auth.js.map +1 -1
- package/out/zero-cache/src/auth/write-authorizer.d.ts +1 -1
- package/out/zero-cache/src/auth/write-authorizer.d.ts.map +1 -1
- package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
- package/out/zero-cache/src/config/normalize.d.ts.map +1 -1
- package/out/zero-cache/src/config/normalize.js +8 -0
- package/out/zero-cache/src/config/normalize.js.map +1 -1
- package/out/zero-cache/src/config/zero-config.d.ts +8 -4
- package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
- package/out/zero-cache/src/config/zero-config.js +28 -6
- package/out/zero-cache/src/config/zero-config.js.map +1 -1
- package/out/zero-cache/src/custom/fetch.d.ts +1 -1
- package/out/zero-cache/src/custom/fetch.d.ts.map +1 -1
- package/out/zero-cache/src/custom/fetch.js +2 -2
- package/out/zero-cache/src/custom/fetch.js.map +1 -1
- package/out/zero-cache/src/custom-queries/transform-query.d.ts +21 -7
- package/out/zero-cache/src/custom-queries/transform-query.d.ts.map +1 -1
- package/out/zero-cache/src/custom-queries/transform-query.js +26 -9
- package/out/zero-cache/src/custom-queries/transform-query.js.map +1 -1
- package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
- package/out/zero-cache/src/server/change-streamer.js +2 -1
- package/out/zero-cache/src/server/change-streamer.js.map +1 -1
- package/out/zero-cache/src/server/runner/run-worker.d.ts.map +1 -1
- package/out/zero-cache/src/server/runner/run-worker.js +5 -2
- package/out/zero-cache/src/server/runner/run-worker.js.map +1 -1
- package/out/zero-cache/src/server/syncer.js +3 -3
- package/out/zero-cache/src/server/syncer.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/change-source.js +24 -20
- package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts +258 -45
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +119 -83
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/init.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/init.js +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/init.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/shard.js +2 -1
- package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts +1 -0
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
- package/out/zero-cache/src/services/http-service.d.ts +1 -0
- package/out/zero-cache/src/services/http-service.d.ts.map +1 -1
- package/out/zero-cache/src/services/http-service.js +5 -4
- package/out/zero-cache/src/services/http-service.js.map +1 -1
- package/out/zero-cache/src/services/life-cycle.d.ts +1 -1
- package/out/zero-cache/src/services/life-cycle.d.ts.map +1 -1
- package/out/zero-cache/src/services/life-cycle.js +1 -2
- package/out/zero-cache/src/services/life-cycle.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.d.ts +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.d.ts.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.js +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.d.ts +4 -3
- package/out/zero-cache/src/services/mutagen/pusher.d.ts.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.js +57 -38
- package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
- package/out/zero-cache/src/services/shadow-sync/shadow-sync-service.js +2 -1
- package/out/zero-cache/src/services/shadow-sync/shadow-sync-service.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/client-handler.js +1 -1
- package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/connection-context-manager.d.ts +39 -27
- package/out/zero-cache/src/services/view-syncer/connection-context-manager.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/connection-context-manager.js +138 -102
- package/out/zero-cache/src/services/view-syncer/connection-context-manager.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr.d.ts +6 -0
- package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr.js +8 -0
- package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +3 -3
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js +115 -86
- package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js +7 -7
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
- package/out/zero-cache/src/workers/syncer.d.ts +1 -1
- package/out/zero-cache/src/workers/syncer.d.ts.map +1 -1
- package/out/zero-cache/src/workers/syncer.js +11 -10
- package/out/zero-cache/src/workers/syncer.js.map +1 -1
- package/out/zero-client/src/client/connection.d.ts +15 -7
- package/out/zero-client/src/client/connection.d.ts.map +1 -1
- package/out/zero-client/src/client/connection.js.map +1 -1
- package/out/zero-client/src/client/crud-impl.d.ts +1 -1
- package/out/zero-client/src/client/crud-impl.d.ts.map +1 -1
- package/out/zero-client/src/client/crud-impl.js +1 -1
- package/out/zero-client/src/client/crud-impl.js.map +1 -1
- package/out/zero-client/src/client/crud.d.ts +1 -1
- package/out/zero-client/src/client/crud.d.ts.map +1 -1
- package/out/zero-client/src/client/crud.js +1 -1
- package/out/zero-client/src/client/crud.js.map +1 -1
- package/out/zero-client/src/client/keys.d.ts +1 -1
- package/out/zero-client/src/client/keys.d.ts.map +1 -1
- package/out/zero-client/src/client/keys.js.map +1 -1
- package/out/zero-client/src/client/make-replicache-mutators.js +1 -1
- package/out/zero-client/src/client/make-replicache-mutators.js.map +1 -1
- package/out/zero-client/src/client/mutation-tracker.d.ts +2 -1
- package/out/zero-client/src/client/mutation-tracker.d.ts.map +1 -1
- package/out/zero-client/src/client/mutation-tracker.js +3 -3
- package/out/zero-client/src/client/mutation-tracker.js.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-client/src/client/zero.d.ts.map +1 -1
- package/out/zero-client/src/client/zero.js +2 -2
- package/out/zero-client/src/client/zero.js.map +1 -1
- package/out/zero-client/src/types/client-state.d.ts +1 -1
- package/out/zero-client/src/types/client-state.d.ts.map +1 -1
- package/out/zero-protocol/src/custom-queries.js +1 -1
- package/out/zero-protocol/src/down.js +1 -1
- package/out/zero-protocol/src/error-kind-enum.d.ts +1 -2
- package/out/zero-protocol/src/error-kind-enum.d.ts.map +1 -1
- package/out/zero-protocol/src/error-kind-enum.js.map +1 -1
- package/out/zero-protocol/src/mutate-server.d.ts +165 -0
- package/out/zero-protocol/src/mutate-server.d.ts.map +1 -0
- package/out/zero-protocol/src/mutate-server.js +24 -0
- package/out/zero-protocol/src/mutate-server.js.map +1 -0
- package/out/zero-protocol/src/mutation.d.ts +229 -0
- package/out/zero-protocol/src/mutation.d.ts.map +1 -0
- package/out/zero-protocol/src/mutation.js +112 -0
- package/out/zero-protocol/src/mutation.js.map +1 -0
- package/out/zero-protocol/src/mutations-patch.js +1 -1
- package/out/zero-protocol/src/mutations-patch.js.map +1 -1
- package/out/zero-protocol/src/push.d.ts +3 -234
- package/out/zero-protocol/src/push.d.ts.map +1 -1
- package/out/zero-protocol/src/push.js +3 -114
- package/out/zero-protocol/src/push.js.map +1 -1
- package/out/zero-protocol/src/query-server.d.ts +150 -0
- package/out/zero-protocol/src/query-server.d.ts.map +1 -0
- package/out/zero-protocol/src/query-server.js +16 -0
- package/out/zero-protocol/src/query-server.js.map +1 -0
- package/out/zero-protocol/src/up.js +1 -1
- package/out/zero-server/src/mod.d.ts +4 -2
- package/out/zero-server/src/mod.d.ts.map +1 -1
- package/out/zero-server/src/process-mutations.d.ts +41 -4
- package/out/zero-server/src/process-mutations.d.ts.map +1 -1
- package/out/zero-server/src/process-mutations.js +52 -35
- package/out/zero-server/src/process-mutations.js.map +1 -1
- package/out/zero-server/src/push-processor.d.ts +3 -3
- package/out/zero-server/src/push-processor.d.ts.map +1 -1
- package/out/zero-server/src/push-processor.js.map +1 -1
- package/out/zero-server/src/queries/process-queries.d.ts +22 -52
- package/out/zero-server/src/queries/process-queries.d.ts.map +1 -1
- package/out/zero-server/src/queries/process-queries.js +50 -49
- package/out/zero-server/src/queries/process-queries.js.map +1 -1
- package/out/zero-server/src/zql-database.js.map +1 -1
- package/out/zero-types/src/default-types.d.ts +1 -0
- package/out/zero-types/src/default-types.d.ts.map +1 -1
- package/out/zql/src/builder/builder.d.ts.map +1 -1
- package/out/zql/src/builder/builder.js +17 -7
- package/out/zql/src/builder/builder.js.map +1 -1
- package/out/zql/src/ivm/cap.d.ts +32 -0
- package/out/zql/src/ivm/cap.d.ts.map +1 -0
- package/out/zql/src/ivm/cap.js +205 -0
- package/out/zql/src/ivm/cap.js.map +1 -0
- package/out/zql/src/ivm/constraint.js +1 -1
- package/out/zql/src/ivm/flipped-join.d.ts.map +1 -1
- package/out/zql/src/ivm/flipped-join.js +61 -15
- package/out/zql/src/ivm/flipped-join.js.map +1 -1
- package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
- package/out/zql/src/ivm/memory-source.js +3 -4
- package/out/zql/src/ivm/memory-source.js.map +1 -1
- package/out/zql/src/ivm/schema.d.ts +8 -0
- package/out/zql/src/ivm/schema.d.ts.map +1 -1
- package/out/zql/src/ivm/take.js +2 -2
- package/out/zql/src/mutate/mutator-registry.js.map +1 -1
- package/out/zql/src/mutate/mutator.d.ts +11 -2
- package/out/zql/src/mutate/mutator.d.ts.map +1 -1
- package/out/zql/src/mutate/mutator.js.map +1 -1
- package/out/zql/src/query/query-registry.d.ts +9 -2
- package/out/zql/src/query/query-registry.d.ts.map +1 -1
- package/out/zql/src/query/query-registry.js.map +1 -1
- package/out/zqlite/src/table-source.d.ts.map +1 -1
- package/out/zqlite/src/table-source.js +4 -1
- package/out/zqlite/src/table-source.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-server.js","names":[],"sources":["../../../../zero-protocol/src/query-server.ts"],"sourcesContent":["import * as v from '../../shared/src/valita.ts';\nimport {\n erroredQuerySchema,\n transformedQuerySchema,\n transformResponseMessageSchema,\n} from './custom-queries.ts';\nimport {transformFailedBodySchema} from './error.ts';\n\nexport const queryResultSchema = v.union(\n transformedQuerySchema,\n erroredQuerySchema,\n);\nexport type QueryResult = v.Infer<typeof queryResultSchema>;\n\nexport const queryResponseBodySchema = v.array(queryResultSchema);\nexport type QueryResponseBody = v.Infer<typeof queryResponseBodySchema>;\n\nexport const querySuccessSchema = v.object({\n kind: v.literal('QueryResponse'),\n userID: v.string().nullable().optional(),\n queries: queryResponseBodySchema,\n});\nexport type QuerySuccess = v.Infer<typeof querySuccessSchema>;\n\nexport const queryResponseSchema = v.union(\n querySuccessSchema,\n transformFailedBodySchema,\n // for backwards compatibility\n transformResponseMessageSchema,\n);\nexport type QueryResponse = v.Infer<typeof queryResponseSchema>;\n"],"mappings":";;;;AAQA,IAAa,oBAAoB,eAAE,MACjC,wBACA,mBACD;AAGD,IAAa,0BAA0B,eAAE,MAAM,kBAAkB;AAGjE,IAAa,qBAAqB,eAAE,OAAO;CACzC,MAAM,eAAE,QAAQ,gBAAgB;CAChC,QAAQ,eAAE,QAAQ,CAAC,UAAU,CAAC,UAAU;CACxC,SAAS;CACV,CAAC;AAGF,IAAa,sBAAsB,eAAE,MACnC,oBACA,2BAEA,+BACD"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { valita_exports } from "../../shared/src/valita.js";
|
|
2
2
|
import { deleteClientsMessageSchema } from "./delete-clients.js";
|
|
3
3
|
import { initConnectionMessageSchema } from "./connect.js";
|
|
4
|
-
import { ackMutationResponsesMessageSchema, pushMessageSchema } from "./push.js";
|
|
5
4
|
import { pullRequestMessageSchema } from "./pull.js";
|
|
5
|
+
import { ackMutationResponsesMessageSchema, pushMessageSchema } from "./push.js";
|
|
6
6
|
import { changeDesiredQueriesMessageSchema } from "./change-desired-queries.js";
|
|
7
7
|
import { closeConnectionMessageSchema } from "./close-connection.js";
|
|
8
8
|
import { inspectUpMessageSchema } from "./inspect-up.js";
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
export { ApplicationError, isApplicationError, type ApplicationErrorOptions, } from '../../zero-protocol/src/application-error.ts';
|
|
2
|
+
export type { MutateResponse } from '../../zero-protocol/src/mutate-server.ts';
|
|
3
|
+
export type { QueryResponse } from '../../zero-protocol/src/query-server.ts';
|
|
2
4
|
export type { ServerColumnSchema, ServerSchema, ServerTableSchema, } from '../../zero-types/src/server-schema.ts';
|
|
3
5
|
export type { AnyTransaction, ClientTransaction, DBConnection, DBTransaction, Location, MutateCRUD, Row, ServerTransaction, Transaction, TransactionBase, TransactionReason, } from '../../zql/src/mutate/custom.ts';
|
|
4
6
|
export { CRUDMutatorFactory, makeSchemaCRUD, type CustomMutatorDefs, } from './custom.ts';
|
|
5
7
|
export { executePostgresQuery } from './pg-query-executor.ts';
|
|
6
|
-
export { getMutation, handleMutateRequest, handleMutationRequest, OutOfOrderMutation, type Database, type ExtractTransactionType, type Params, type Parsed, type TransactFn, type TransactFnCallback, type TransactionProviderHooks, type TransactionProviderInput, } from './process-mutations.ts';
|
|
8
|
+
export { getMutation, handleMutateRequest, handleMutationRequest, OutOfOrderMutation, type Database, type ExtractTransactionType, type MutateRequestOptions, type Params, type Parsed, type TransactFn, type TransactFnCallback, type TransactionProviderHooks, type TransactionProviderInput, } from './process-mutations.ts';
|
|
7
9
|
export { PushProcessor } from './push-processor.ts';
|
|
8
|
-
export { handleGetQueriesRequest, handleQueryRequest, handleTransformRequest, type TransformQueryFunction, } from './queries/process-queries.ts';
|
|
10
|
+
export { handleGetQueriesRequest, handleQueryRequest, handleTransformRequest, type QueryRequestOptions, type TransformQueryFunction, } from './queries/process-queries.ts';
|
|
9
11
|
export { ZQLDatabase } from './zql-database.ts';
|
|
10
12
|
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../../zero-server/src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,KAAK,uBAAuB,GAC7B,MAAM,8CAA8C,CAAC;AACtD,YAAY,EACV,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,GAClB,MAAM,uCAAuC,CAAC;AAC/C,YAAY,EACV,cAAc,EACd,iBAAiB,EACjB,YAAY,EACZ,aAAa,EACb,QAAQ,EACR,UAAU,EACV,GAAG,EACH,iBAAiB,EACjB,WAAW,EACX,eAAe,EACf,iBAAiB,GAClB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,KAAK,iBAAiB,GACvB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,EAClB,KAAK,QAAQ,EACb,KAAK,sBAAsB,EAC3B,KAAK,MAAM,EACX,KAAK,MAAM,EACX,KAAK,UAAU,EACf,KAAK,kBAAkB,EACvB,KAAK,wBAAwB,EAC7B,KAAK,wBAAwB,GAC9B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAC;AAClD,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,sBAAsB,GAC5B,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../../zero-server/src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,KAAK,uBAAuB,GAC7B,MAAM,8CAA8C,CAAC;AACtD,YAAY,EAAC,cAAc,EAAC,MAAM,0CAA0C,CAAC;AAC7E,YAAY,EAAC,aAAa,EAAC,MAAM,yCAAyC,CAAC;AAC3E,YAAY,EACV,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,GAClB,MAAM,uCAAuC,CAAC;AAC/C,YAAY,EACV,cAAc,EACd,iBAAiB,EACjB,YAAY,EACZ,aAAa,EACb,QAAQ,EACR,UAAU,EACV,GAAG,EACH,iBAAiB,EACjB,WAAW,EACX,eAAe,EACf,iBAAiB,GAClB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,KAAK,iBAAiB,GACvB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,EAClB,KAAK,QAAQ,EACb,KAAK,sBAAsB,EAC3B,KAAK,oBAAoB,EACzB,KAAK,MAAM,EACX,KAAK,MAAM,EACX,KAAK,UAAU,EACf,KAAK,kBAAkB,EACvB,KAAK,wBAAwB,EAC7B,KAAK,wBAAwB,GAC9B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAC;AAClD,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,mBAAmB,EACxB,KAAK,sBAAsB,GAC5B,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC"}
|
|
@@ -2,7 +2,9 @@ import type { LogLevel } from '@rocicorp/logger';
|
|
|
2
2
|
import type { ReadonlyJSONValue } from '../../shared/src/json.ts';
|
|
3
3
|
import type { MaybePromise } from '../../shared/src/types.ts';
|
|
4
4
|
import * as v from '../../shared/src/valita.ts';
|
|
5
|
-
import {
|
|
5
|
+
import { mutateParamsSchema, type MutateResponse } from '../../zero-protocol/src/mutate-server.ts';
|
|
6
|
+
import { type CleanupResultsArg, type CustomMutation } from '../../zero-protocol/src/mutation.ts';
|
|
7
|
+
import { type MutationResponse } from '../../zero-protocol/src/push.ts';
|
|
6
8
|
import type { AnyMutatorRegistry } from '../../zql/src/mutate/mutator-registry.ts';
|
|
7
9
|
import type { CustomMutatorDefs, CustomMutatorImpl } from './custom.ts';
|
|
8
10
|
export interface TransactionProviderHooks {
|
|
@@ -26,7 +28,7 @@ export interface Database<T> {
|
|
|
26
28
|
transaction: <R>(callback: (tx: T, transactionHooks: TransactionProviderHooks) => MaybePromise<R>, transactionInput?: TransactionProviderInput) => Promise<R>;
|
|
27
29
|
}
|
|
28
30
|
export type ExtractTransactionType<D> = D extends Database<infer T> ? T : never;
|
|
29
|
-
export type Params = v.Infer<typeof
|
|
31
|
+
export type Params = v.Infer<typeof mutateParamsSchema>;
|
|
30
32
|
export type TransactFn<D extends Database<ExtractTransactionType<D>>> = (cb: TransactFnCallback<D>) => Promise<MutationResponse>;
|
|
31
33
|
export type TransactFnCallback<D extends Database<ExtractTransactionType<D>>> = (tx: ExtractTransactionType<D>, mutatorName: string, mutatorArgs: ReadonlyJSONValue | undefined) => Promise<void>;
|
|
32
34
|
export type Parsed<D extends Database<ExtractTransactionType<D>>> = {
|
|
@@ -37,11 +39,46 @@ export type Parsed<D extends Database<ExtractTransactionType<D>>> = {
|
|
|
37
39
|
* @deprecated Use {@linkcode handleMutateRequest} instead.
|
|
38
40
|
*/
|
|
39
41
|
export declare const handleMutationRequest: typeof handleMutateRequest;
|
|
40
|
-
|
|
41
|
-
export
|
|
42
|
+
type QueryParams = URLSearchParams | Record<string, string>;
|
|
43
|
+
export type MutateRequestOptions = {
|
|
44
|
+
userID?: string | null | undefined;
|
|
45
|
+
logLevel?: LogLevel | undefined;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Process a `/mutate` request from a `Request`.
|
|
49
|
+
*
|
|
50
|
+
* @param dbProvider - Database used to run transactions and store mutation
|
|
51
|
+
* results.
|
|
52
|
+
* @param cb - Runs once per custom mutation. Use `transact` for DB work.
|
|
53
|
+
* Errors before `transact` are saved; errors after commit are logged.
|
|
54
|
+
* @param request - Request to read query params and JSON body from.
|
|
55
|
+
* @param logLevelOrOptions - Either a log level or additional request
|
|
56
|
+
* options.
|
|
57
|
+
* @returns A `MutateResponse`. Success returns `userID: options.userID ?? null`
|
|
58
|
+
* when `options.userID` is provided; invalid requests return the `PushFailed`
|
|
59
|
+
* branch.
|
|
60
|
+
*/
|
|
61
|
+
export declare function handleMutateRequest<D extends Database<ExtractTransactionType<D>>>(dbProvider: D, cb: (transact: TransactFn<D>, mutation: CustomMutation) => Promise<MutationResponse>, request: Request, logLevelOrOptions?: LogLevel | MutateRequestOptions): Promise<MutateResponse>;
|
|
62
|
+
/**
|
|
63
|
+
* Process a `/mutate` request from parsed query params and a parsed JSON body.
|
|
64
|
+
*
|
|
65
|
+
* @param dbProvider - Database used to run transactions and store mutation
|
|
66
|
+
* results.
|
|
67
|
+
* @param cb - Runs once per custom mutation. Use `transact` for DB work.
|
|
68
|
+
* Errors before `transact` are saved; errors after commit are logged.
|
|
69
|
+
* @param queryParams - Parsed query params.
|
|
70
|
+
* @param jsonBody - Parsed JSON body.
|
|
71
|
+
* @param logLevelOrOptions - Either a log level or additional request
|
|
72
|
+
* options.
|
|
73
|
+
* @returns A `MutateResponse`. Success returns `userID: options.userID ?? null`
|
|
74
|
+
* when `options.userID` is provided; invalid requests return the `PushFailed`
|
|
75
|
+
* branch.
|
|
76
|
+
*/
|
|
77
|
+
export declare function handleMutateRequest<D extends Database<ExtractTransactionType<D>>>(dbProvider: D, cb: (transact: TransactFn<D>, mutation: CustomMutation) => Promise<MutationResponse>, queryParams: QueryParams, jsonBody: ReadonlyJSONValue, logLevelOrOptions?: LogLevel | MutateRequestOptions): Promise<MutateResponse>;
|
|
42
78
|
export declare class OutOfOrderMutation extends Error {
|
|
43
79
|
constructor(clientID: string, receivedMutationID: number, lastMutationID: number | bigint);
|
|
44
80
|
}
|
|
45
81
|
/** @deprecated Use getMutator instead */
|
|
46
82
|
export declare function getMutation(mutators: AnyMutatorRegistry | CustomMutatorDefs<any>, name: string): CustomMutatorImpl<any>;
|
|
83
|
+
export {};
|
|
47
84
|
//# sourceMappingURL=process-mutations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process-mutations.d.ts","sourceRoot":"","sources":["../../../../zero-server/src/process-mutations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAa,QAAQ,EAAC,MAAM,kBAAkB,CAAC;AAG3D,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEhE,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,2BAA2B,CAAC;AAC5D,OAAO,KAAK,CAAC,MAAM,4BAA4B,CAAC;AAWhD,OAAO,
|
|
1
|
+
{"version":3,"file":"process-mutations.d.ts","sourceRoot":"","sources":["../../../../zero-server/src/process-mutations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAa,QAAQ,EAAC,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,EACb,EAAE,EAAE,CACF,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC,EACvB,QAAQ,EAAE,cAAc,KACrB,OAAO,CAAC,gBAAgB,CAAC,EAC9B,OAAO,EAAE,OAAO,EAChB,iBAAiB,CAAC,EAAE,QAAQ,GAAG,oBAAoB,GAClD,OAAO,CAAC,cAAc,CAAC,CAAC;AAE3B;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,mBAAmB,CACjC,CAAC,SAAS,QAAQ,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,EAE7C,UAAU,EAAE,CAAC,EACb,EAAE,EAAE,CACF,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC,EACvB,QAAQ,EAAE,cAAc,KACrB,OAAO,CAAC,gBAAgB,CAAC,EAC9B,WAAW,EAAE,WAAW,EACxB,QAAQ,EAAE,iBAAiB,EAC3B,iBAAiB,CAAC,EAAE,QAAQ,GAAG,oBAAoB,GAClD,OAAO,CAAC,cAAc,CAAC,CAAC;AA2a3B,qBAAa,kBAAmB,SAAQ,KAAK;gBAEzC,QAAQ,EAAE,MAAM,EAChB,kBAAkB,EAAE,MAAM,EAC1B,cAAc,EAAE,MAAM,GAAG,MAAM;CAMlC;AAmBD,yCAAyC;AACzC,wBAAgB,WAAW,CAEzB,QAAQ,EAAE,kBAAkB,GAAG,iBAAiB,CAAC,GAAG,CAAC,EACrD,IAAI,EAAE,MAAM,GAEX,iBAAiB,CAAC,GAAG,CAAC,CAaxB"}
|
|
@@ -7,8 +7,10 @@ import { PushFailed } from "../../zero-protocol/src/error-kind-enum.js";
|
|
|
7
7
|
import { Server } from "../../zero-protocol/src/error-origin-enum.js";
|
|
8
8
|
import { Database, Internal, OutOfOrderMutation as OutOfOrderMutation$1, Parse, UnsupportedPushVersion } from "../../zero-protocol/src/error-reason-enum.js";
|
|
9
9
|
import { isMutator } from "../../zql/src/mutate/mutator.js";
|
|
10
|
-
import { cleanupResultsArgSchema
|
|
10
|
+
import { cleanupResultsArgSchema } from "../../zero-protocol/src/mutation.js";
|
|
11
|
+
import { pushBodySchema } from "../../zero-protocol/src/push.js";
|
|
11
12
|
import { MutationAlreadyProcessedError } from "../../zero-cache/src/services/mutagen/error.js";
|
|
13
|
+
import { mutateParamsSchema } from "../../zero-protocol/src/mutate-server.js";
|
|
12
14
|
import { createLogContext } from "./logging.js";
|
|
13
15
|
import { separatorRe } from "./push-processor.js";
|
|
14
16
|
//#region ../zero-server/src/process-mutations.ts
|
|
@@ -24,40 +26,48 @@ var applicationErrorWrapper = async (fn) => {
|
|
|
24
26
|
* @deprecated Use {@linkcode handleMutateRequest} instead.
|
|
25
27
|
*/
|
|
26
28
|
var handleMutationRequest = handleMutateRequest;
|
|
27
|
-
async function handleMutateRequest(dbProvider, cb,
|
|
28
|
-
|
|
29
|
-
let
|
|
30
|
-
let
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
lc = createLogContext(bodyOrLogLevel ?? "info").withContext("PushProcessor");
|
|
36
|
-
queryString = new URL(request.url).searchParams;
|
|
37
|
-
try {
|
|
38
|
-
jsonBody = await request.json();
|
|
39
|
-
} catch (error) {
|
|
40
|
-
lc.error?.("Failed to parse push body", error);
|
|
41
|
-
const message = `Failed to parse push body: ${getErrorMessage(error)}`;
|
|
42
|
-
const details = getErrorDetails(error);
|
|
43
|
-
return {
|
|
44
|
-
kind: PushFailed,
|
|
45
|
-
origin: Server,
|
|
46
|
-
reason: Parse,
|
|
47
|
-
message,
|
|
48
|
-
mutationIDs: [],
|
|
49
|
-
...details ? { details } : {}
|
|
50
|
-
};
|
|
51
|
-
}
|
|
29
|
+
async function handleMutateRequest(dbProvider, cb, requestOrQueryParams, jsonBodyOrLogLevelOrOptions, logLevelOrOptions) {
|
|
30
|
+
let requestOrJsonBody;
|
|
31
|
+
let queryParams;
|
|
32
|
+
let optionsArg;
|
|
33
|
+
if (requestOrQueryParams instanceof Request) {
|
|
34
|
+
requestOrJsonBody = requestOrQueryParams;
|
|
35
|
+
queryParams = new URL(requestOrQueryParams.url).searchParams;
|
|
36
|
+
optionsArg = jsonBodyOrLogLevelOrOptions;
|
|
52
37
|
} else {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
38
|
+
requestOrJsonBody = jsonBodyOrLogLevelOrOptions;
|
|
39
|
+
queryParams = requestOrQueryParams;
|
|
40
|
+
optionsArg = logLevelOrOptions;
|
|
41
|
+
}
|
|
42
|
+
const options = normalizeMutateRequestOptions(optionsArg);
|
|
43
|
+
const normalized = {
|
|
44
|
+
requestOrJsonBody,
|
|
45
|
+
userID: "userID" in options ? options.userID ?? null : void 0,
|
|
46
|
+
queryParams,
|
|
47
|
+
logLevel: options.logLevel ?? "info"
|
|
48
|
+
};
|
|
49
|
+
const lc = createLogContext(normalized.logLevel).withContext("PushProcessor");
|
|
50
|
+
let jsonBody;
|
|
51
|
+
if (normalized.requestOrJsonBody instanceof Request) try {
|
|
52
|
+
jsonBody = await normalized.requestOrJsonBody.json();
|
|
53
|
+
} catch (error) {
|
|
54
|
+
lc.error?.("Failed to parse push body", error);
|
|
55
|
+
const message = `Failed to parse push body: ${getErrorMessage(error)}`;
|
|
56
|
+
const details = getErrorDetails(error);
|
|
57
|
+
return {
|
|
58
|
+
kind: PushFailed,
|
|
59
|
+
origin: Server,
|
|
60
|
+
reason: Parse,
|
|
61
|
+
message,
|
|
62
|
+
mutationIDs: [],
|
|
63
|
+
...details ? { details } : {}
|
|
64
|
+
};
|
|
56
65
|
}
|
|
66
|
+
else jsonBody = normalized.requestOrJsonBody;
|
|
57
67
|
let mutationIDs = [];
|
|
58
68
|
let pushBody;
|
|
59
69
|
try {
|
|
60
|
-
pushBody = parse(jsonBody, pushBodySchema);
|
|
70
|
+
pushBody = parse(jsonBody, pushBodySchema, "passthrough");
|
|
61
71
|
mutationIDs = pushBody.mutations.map((m) => ({
|
|
62
72
|
id: m.id,
|
|
63
73
|
clientID: m.clientID
|
|
@@ -75,9 +85,9 @@ async function handleMutateRequest(dbProvider, cb, queryStringOrRequest, bodyOrL
|
|
|
75
85
|
...details ? { details } : {}
|
|
76
86
|
};
|
|
77
87
|
}
|
|
78
|
-
let
|
|
88
|
+
let parsedQueryParams;
|
|
79
89
|
try {
|
|
80
|
-
|
|
90
|
+
parsedQueryParams = parse(normalized.queryParams instanceof URLSearchParams ? Object.fromEntries(normalized.queryParams) : normalized.queryParams, mutateParamsSchema, "passthrough");
|
|
81
91
|
} catch (error) {
|
|
82
92
|
lc.error?.("Failed to parse push query parameters", error);
|
|
83
93
|
const message = `Failed to parse push query parameters: ${getErrorMessage(error)}`;
|
|
@@ -101,12 +111,12 @@ async function handleMutateRequest(dbProvider, cb, queryStringOrRequest, bodyOrL
|
|
|
101
111
|
const responses = [];
|
|
102
112
|
let processedCount = 0;
|
|
103
113
|
try {
|
|
104
|
-
const transactor = new Transactor(dbProvider, pushBody,
|
|
114
|
+
const transactor = new Transactor(dbProvider, pushBody, parsedQueryParams, lc);
|
|
105
115
|
for (const m of pushBody.mutations) {
|
|
106
116
|
if (m.type === "custom" && m.name === "_zero_cleanupResults") {
|
|
107
117
|
lc.debug?.(`Processing internal mutation '${m.name}' (clientID=${m.clientID})`);
|
|
108
118
|
try {
|
|
109
|
-
await processCleanupResultsMutation(dbProvider, m,
|
|
119
|
+
await processCleanupResultsMutation(dbProvider, m, parsedQueryParams, lc);
|
|
110
120
|
processedCount++;
|
|
111
121
|
} catch (error) {
|
|
112
122
|
lc.warn?.(`Failed to process cleanup mutation for client ${m.clientID}`, error);
|
|
@@ -137,7 +147,11 @@ async function handleMutateRequest(dbProvider, cb, queryStringOrRequest, bodyOrL
|
|
|
137
147
|
processedCount++;
|
|
138
148
|
}
|
|
139
149
|
}
|
|
140
|
-
return {
|
|
150
|
+
return {
|
|
151
|
+
kind: "MutateResponse",
|
|
152
|
+
mutations: responses,
|
|
153
|
+
...typeof normalized.userID !== "undefined" ? { userID: normalized.userID } : {}
|
|
154
|
+
};
|
|
141
155
|
} catch (error) {
|
|
142
156
|
lc.error?.("Failed to process push request", error);
|
|
143
157
|
const unprocessedMutationIDs = mutationIDs.slice(processedCount);
|
|
@@ -153,6 +167,9 @@ async function handleMutateRequest(dbProvider, cb, queryStringOrRequest, bodyOrL
|
|
|
153
167
|
};
|
|
154
168
|
}
|
|
155
169
|
}
|
|
170
|
+
function normalizeMutateRequestOptions(logLevelOrOptions) {
|
|
171
|
+
return typeof logLevelOrOptions === "string" ? { logLevel: logLevelOrOptions } : logLevelOrOptions ?? {};
|
|
172
|
+
}
|
|
156
173
|
var Transactor = class {
|
|
157
174
|
#dbProvider;
|
|
158
175
|
#req;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process-mutations.js","names":["#dbProvider","#req","#params","#lc","#transactImpl","#checkAndIncrementLastMutationID","#getTransactionInput"],"sources":["../../../../zero-server/src/process-mutations.ts"],"sourcesContent":["import type {LogContext, LogLevel} from '@rocicorp/logger';\nimport {assert} from '../../shared/src/asserts.ts';\nimport {getErrorDetails, getErrorMessage} from '../../shared/src/error.ts';\nimport type {ReadonlyJSONValue} from '../../shared/src/json.ts';\nimport {promiseVoid} from '../../shared/src/resolved-promises.ts';\nimport type {MaybePromise} from '../../shared/src/types.ts';\nimport * as v from '../../shared/src/valita.ts';\nimport {MutationAlreadyProcessedError} from '../../zero-cache/src/services/mutagen/error.ts';\nimport type {ApplicationError} from '../../zero-protocol/src/application-error.ts';\nimport {\n isApplicationError,\n wrapWithApplicationError,\n} from '../../zero-protocol/src/application-error.ts';\nimport {ErrorKind} from '../../zero-protocol/src/error-kind.ts';\nimport {ErrorOrigin} from '../../zero-protocol/src/error-origin.ts';\nimport {ErrorReason} from '../../zero-protocol/src/error-reason.ts';\nimport type {PushFailedBody} from '../../zero-protocol/src/error.ts';\nimport {\n CLEANUP_RESULTS_MUTATION_NAME,\n cleanupResultsArgSchema,\n pushBodySchema,\n pushParamsSchema,\n type CleanupResultsArg,\n type CustomMutation,\n type Mutation,\n type MutationID,\n type MutationResponse,\n type PushBody,\n type PushResponse,\n} from '../../zero-protocol/src/push.ts';\nimport type {AnyMutatorRegistry} from '../../zql/src/mutate/mutator-registry.ts';\nimport {isMutator} from '../../zql/src/mutate/mutator.ts';\nimport type {CustomMutatorDefs, CustomMutatorImpl} from './custom.ts';\nimport {createLogContext} from './logging.ts';\nimport {separatorRe} from './push-processor.ts';\n\nexport interface TransactionProviderHooks {\n updateClientMutationID: () => Promise<{lastMutationID: number | bigint}>;\n writeMutationResult: (result: MutationResponse) => Promise<void>;\n deleteMutationResults: (args: CleanupResultsArg) => Promise<void>;\n}\n\nexport interface TransactionProviderInput {\n upstreamSchema: string;\n clientGroupID: string;\n clientID: string;\n mutationID: number;\n}\n\n/**\n * Defines the abstract interface for a database that PushProcessor can execute\n * transactions against.\n */\nexport interface Database<T> {\n transaction: <R>(\n callback: (\n tx: T,\n transactionHooks: TransactionProviderHooks,\n ) => MaybePromise<R>,\n transactionInput?: TransactionProviderInput,\n ) => Promise<R>;\n}\n\nexport type ExtractTransactionType<D> = D extends Database<infer T> ? T : never;\nexport type Params = v.Infer<typeof pushParamsSchema>;\n\nexport type TransactFn<D extends Database<ExtractTransactionType<D>>> = (\n cb: TransactFnCallback<D>,\n) => Promise<MutationResponse>;\n\nexport type TransactFnCallback<D extends Database<ExtractTransactionType<D>>> =\n (\n tx: ExtractTransactionType<D>,\n mutatorName: string,\n mutatorArgs: ReadonlyJSONValue | undefined,\n ) => Promise<void>;\n\nexport type Parsed<D extends Database<ExtractTransactionType<D>>> = {\n transact: TransactFn<D>;\n mutations: CustomMutation[];\n};\n\ntype MutationPhase = 'preTransaction' | 'transactionPending' | 'postCommit';\n\nconst applicationErrorWrapper = async <T>(fn: () => Promise<T>): Promise<T> => {\n try {\n return await fn();\n } catch (error) {\n if (\n error instanceof DatabaseTransactionError ||\n error instanceof OutOfOrderMutation ||\n error instanceof MutationAlreadyProcessedError ||\n isApplicationError(error)\n ) {\n throw error;\n }\n\n throw wrapWithApplicationError(error);\n }\n};\n\n/**\n * @deprecated Use {@linkcode handleMutateRequest} instead.\n */\nexport const handleMutationRequest = handleMutateRequest;\n\nexport function handleMutateRequest<\n D extends Database<ExtractTransactionType<D>>,\n>(\n dbProvider: D,\n cb: (\n transact: TransactFn<D>,\n mutation: CustomMutation,\n ) => Promise<MutationResponse>,\n queryString: URLSearchParams | Record<string, string>,\n body: ReadonlyJSONValue,\n logLevel?: LogLevel,\n): Promise<PushResponse>;\n\nexport function handleMutateRequest<\n D extends Database<ExtractTransactionType<D>>,\n>(\n dbProvider: D,\n cb: (\n transact: TransactFn<D>,\n mutation: CustomMutation,\n ) => Promise<MutationResponse>,\n request: Request,\n logLevel?: LogLevel,\n): Promise<PushResponse>;\n\nexport async function handleMutateRequest<\n D extends Database<ExtractTransactionType<D>>,\n>(\n dbProvider: D,\n cb: (\n transact: TransactFn<D>,\n mutation: CustomMutation,\n ) => Promise<MutationResponse>,\n queryStringOrRequest: Request | URLSearchParams | Record<string, string>,\n bodyOrLogLevel?: ReadonlyJSONValue | LogLevel,\n logLevel?: LogLevel,\n): Promise<PushResponse> {\n // Parse overload arguments\n const isRequestOverload = queryStringOrRequest instanceof Request;\n\n let request: Request | undefined;\n let queryString: URLSearchParams | Record<string, string>;\n let jsonBody: unknown;\n\n let lc: LogContext;\n\n if (isRequestOverload) {\n request = queryStringOrRequest;\n const level = (bodyOrLogLevel as LogLevel | undefined) ?? 'info';\n\n // Create log context early, before extracting JSON from Request\n lc = createLogContext(level).withContext('PushProcessor');\n\n const url = new URL(request.url);\n queryString = url.searchParams;\n\n try {\n jsonBody = await request.json();\n } catch (error) {\n lc.error?.('Failed to parse push body', error);\n const message = `Failed to parse push body: ${getErrorMessage(error)}`;\n const details = getErrorDetails(error);\n return {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason: ErrorReason.Parse,\n message,\n mutationIDs: [],\n ...(details ? {details} : {}),\n } as const satisfies PushFailedBody;\n }\n } else {\n queryString = queryStringOrRequest;\n jsonBody = bodyOrLogLevel;\n const level = logLevel ?? 'info';\n lc = createLogContext(level).withContext('PushProcessor');\n }\n\n let mutationIDs: MutationID[] = [];\n\n let pushBody: PushBody;\n try {\n pushBody = v.parse(jsonBody, pushBodySchema);\n mutationIDs = pushBody.mutations.map(m => ({\n id: m.id,\n clientID: m.clientID,\n }));\n } catch (error) {\n lc.error?.('Failed to parse push body', error);\n const message = `Failed to parse push body: ${getErrorMessage(error)}`;\n const details = getErrorDetails(error);\n return {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason: ErrorReason.Parse,\n message,\n mutationIDs,\n ...(details ? {details} : {}),\n } as const satisfies PushFailedBody;\n }\n\n let queryParams: Params;\n try {\n const queryStringObj =\n queryString instanceof URLSearchParams\n ? Object.fromEntries(queryString)\n : queryString;\n queryParams = v.parse(queryStringObj, pushParamsSchema, 'passthrough');\n } catch (error) {\n lc.error?.('Failed to parse push query parameters', error);\n const message = `Failed to parse push query parameters: ${getErrorMessage(error)}`;\n const details = getErrorDetails(error);\n return {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason: ErrorReason.Parse,\n message,\n mutationIDs,\n ...(details ? {details} : {}),\n } as const satisfies PushFailedBody;\n }\n\n if (pushBody.pushVersion !== 1) {\n const response = {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason: ErrorReason.UnsupportedPushVersion,\n mutationIDs,\n message: `Unsupported push version: ${pushBody.pushVersion}`,\n } as const satisfies PushFailedBody;\n return response;\n }\n\n const responses: MutationResponse[] = [];\n let processedCount = 0;\n\n try {\n const transactor = new Transactor(dbProvider, pushBody, queryParams, lc);\n\n // Each mutation goes through three phases:\n // 1. Pre-transaction: user logic that runs before `transact` is called. If\n // this throws we still advance LMID and persist the failure result.\n // 2. Transaction: the callback passed to `transact`, which can be retried\n // if it fails with an ApplicationError.\n // 3. Post-commit: any logic that runs after `transact` resolves. Failures\n // here are logged but the mutation remains committed.\n for (const m of pushBody.mutations) {\n // Handle internal mutations (like cleanup) directly without user dispatch\n if (m.type === 'custom' && m.name === CLEANUP_RESULTS_MUTATION_NAME) {\n lc.debug?.(\n `Processing internal mutation '${m.name}' (clientID=${m.clientID})`,\n );\n try {\n await processCleanupResultsMutation(dbProvider, m, queryParams, lc);\n // No response added - this is fire-and-forget\n processedCount++;\n } catch (error) {\n lc.warn?.(\n `Failed to process cleanup mutation for client ${m.clientID}`,\n error,\n );\n // Don't fail the whole push for cleanup errors\n processedCount++;\n }\n continue;\n }\n\n assert(m.type === 'custom', 'Expected custom mutation');\n lc.debug?.(\n `Processing mutation '${m.name}' (id=${m.id}, clientID=${m.clientID})`,\n );\n\n let mutationPhase: MutationPhase = 'preTransaction';\n\n const transactProxy: TransactFn<D> = async innerCb => {\n mutationPhase = 'transactionPending';\n const result = await transactor.transact(m, innerCb);\n mutationPhase = 'postCommit';\n return result;\n };\n\n try {\n const res = await applicationErrorWrapper(() => cb(transactProxy, m));\n responses.push(res);\n lc.debug?.(`Mutation '${m.name}' (id=${m.id}) completed successfully`);\n\n processedCount++;\n } catch (error) {\n if (!isApplicationError(error)) {\n throw error;\n }\n\n if (mutationPhase === 'preTransaction') {\n // Pre-transaction\n await transactor.persistPreTransactionFailure(m, error);\n } else if (mutationPhase === 'postCommit') {\n // Post-commit\n lc.error?.(\n `Post-commit mutation handler failed for mutation ${m.id} for client ${m.clientID}`,\n error,\n );\n }\n\n lc.warn?.(\n `Application error processing mutation ${m.id} for client ${m.clientID}`,\n error,\n );\n responses.push(makeAppErrorResponse(m, error));\n\n processedCount++;\n }\n }\n\n return {\n mutations: responses,\n };\n } catch (error) {\n lc.error?.('Failed to process push request', error);\n // only include mutationIDs for mutations that were not processed\n const unprocessedMutationIDs = mutationIDs.slice(processedCount);\n\n const message = getErrorMessage(error);\n const details = getErrorDetails(error);\n\n return {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason:\n error instanceof OutOfOrderMutation\n ? ErrorReason.OutOfOrderMutation\n : error instanceof DatabaseTransactionError\n ? ErrorReason.Database\n : ErrorReason.Internal,\n message,\n mutationIDs: unprocessedMutationIDs,\n ...(details ? {details} : {}),\n };\n }\n}\n\nclass Transactor<D extends Database<ExtractTransactionType<D>>> {\n readonly #dbProvider: D;\n readonly #req: PushBody;\n readonly #params: Params;\n readonly #lc: LogContext;\n\n constructor(dbProvider: D, req: PushBody, params: Params, lc: LogContext) {\n this.#dbProvider = dbProvider;\n this.#req = req;\n this.#params = params;\n this.#lc = lc;\n }\n\n transact = async (\n mutation: CustomMutation,\n cb: TransactFnCallback<D>,\n ): Promise<MutationResponse> => {\n let appError: ApplicationError | undefined = undefined;\n for (;;) {\n try {\n const ret = await this.#transactImpl(mutation, cb, appError);\n if (appError !== undefined) {\n this.#lc.warn?.(\n `Mutation ${mutation.id} for client ${mutation.clientID} was retried after an error`,\n appError,\n );\n return makeAppErrorResponse(mutation, appError);\n }\n\n return ret;\n } catch (error) {\n if (error instanceof OutOfOrderMutation) {\n this.#lc.error?.(error);\n throw error;\n }\n\n if (error instanceof MutationAlreadyProcessedError) {\n this.#lc.warn?.(error);\n return {\n id: {\n clientID: mutation.clientID,\n id: mutation.id,\n },\n result: {\n error: 'alreadyProcessed',\n details: error.message,\n },\n };\n }\n\n if (appError !== undefined) {\n // Retry also failed → internal error, cannot skip mutation\n this.#lc.error?.(\n `Retry also failed for mutation ${mutation.id} for client ${mutation.clientID}`,\n error,\n );\n throw error;\n }\n\n // First attempt failed → store error and retry without mutator\n const originalError =\n error instanceof DatabaseTransactionError\n ? (error.cause ?? error)\n : error;\n appError = wrapWithApplicationError(originalError);\n this.#lc.warn?.(\n `Error processing mutation ${mutation.id} for client ${mutation.clientID}, retrying without mutator`,\n appError,\n );\n continue;\n }\n }\n };\n\n async persistPreTransactionFailure(\n mutation: CustomMutation,\n appError: ApplicationError<ReadonlyJSONValue | undefined>,\n ): Promise<MutationResponse> {\n // User-land code threw before calling `transact`. We still need to bump the\n // LMID for this mutation and persist the error so that the client knows it failed.\n const ret = await this.#transactImpl(\n mutation,\n // noop callback since there's no transaction to execute\n () => promiseVoid,\n appError,\n );\n return ret;\n }\n\n async #transactImpl(\n mutation: CustomMutation,\n cb: TransactFnCallback<D>,\n appError: ApplicationError | undefined,\n ): Promise<MutationResponse> {\n let transactionPhase: DatabaseTransactionPhase = 'open';\n\n try {\n const ret = await this.#dbProvider.transaction(\n async (dbTx, transactionHooks) => {\n // update the transaction phase to 'execute' after the transaction is opened\n transactionPhase = 'execute';\n\n await this.#checkAndIncrementLastMutationID(\n transactionHooks,\n mutation.clientID,\n mutation.id,\n );\n\n if (appError === undefined) {\n this.#lc.debug?.(\n `Executing mutator '${mutation.name}' (id=${mutation.id})`,\n );\n await cb(dbTx, mutation.name, mutation.args[0]);\n } else {\n const mutationResult = makeAppErrorResponse(mutation, appError);\n await transactionHooks.writeMutationResult(mutationResult);\n }\n\n return {\n id: {\n clientID: mutation.clientID,\n id: mutation.id,\n },\n result: {},\n };\n },\n this.#getTransactionInput(mutation),\n );\n\n return ret;\n } catch (error) {\n if (\n isApplicationError(error) ||\n error instanceof OutOfOrderMutation ||\n error instanceof MutationAlreadyProcessedError\n ) {\n throw error;\n }\n\n throw new DatabaseTransactionError(transactionPhase, {cause: error});\n }\n }\n\n #getTransactionInput(mutation: CustomMutation): TransactionProviderInput {\n return {\n upstreamSchema: this.#params.schema,\n clientGroupID: this.#req.clientGroupID,\n clientID: mutation.clientID,\n mutationID: mutation.id,\n };\n }\n\n async #checkAndIncrementLastMutationID(\n transactionHooks: TransactionProviderHooks,\n clientID: string,\n receivedMutationID: number,\n ) {\n const {lastMutationID} = await transactionHooks.updateClientMutationID();\n\n if (receivedMutationID < lastMutationID) {\n throw new MutationAlreadyProcessedError(\n clientID,\n receivedMutationID,\n lastMutationID,\n );\n } else if (receivedMutationID > lastMutationID) {\n throw new OutOfOrderMutation(\n clientID,\n receivedMutationID,\n lastMutationID,\n );\n }\n }\n}\n\nexport class OutOfOrderMutation extends Error {\n constructor(\n clientID: string,\n receivedMutationID: number,\n lastMutationID: number | bigint,\n ) {\n super(\n `Client ${clientID} sent mutation ID ${receivedMutationID} but expected ${lastMutationID}`,\n );\n }\n}\n\nfunction makeAppErrorResponse(\n m: Mutation,\n error: ApplicationError<ReadonlyJSONValue | undefined>,\n): MutationResponse {\n return {\n id: {\n clientID: m.clientID,\n id: m.id,\n },\n result: {\n error: 'app',\n message: error.message,\n ...(error.details ? {details: error.details} : {}),\n },\n };\n}\n\n/** @deprecated Use getMutator instead */\nexport function getMutation(\n // oxlint-disable-next-line no-explicit-any\n mutators: AnyMutatorRegistry | CustomMutatorDefs<any>,\n name: string,\n // oxlint-disable-next-line no-explicit-any\n): CustomMutatorImpl<any> {\n const path = name.split(separatorRe);\n const mutator = getObjectAtPath(mutators, path);\n assert(typeof mutator === 'function', `could not find mutator ${name}`);\n\n if (isMutator(mutator)) {\n // mutator needs to be called with {tx, args, ctx}\n // CustomMutatorImpl is called with (tx, args, ctx)\n return (tx, args, ctx) => mutator.fn({args, ctx, tx});\n }\n\n // oxlint-disable-next-line no-explicit-any\n return mutator as CustomMutatorImpl<any>;\n}\n\nfunction getObjectAtPath(\n obj: Record<string, unknown>,\n path: string[],\n): unknown {\n let current: unknown = obj;\n for (const part of path) {\n if (typeof current !== 'object' || current === null || !(part in current)) {\n return undefined;\n }\n current = (current as Record<string, unknown>)[part];\n }\n return current;\n}\n\n/**\n * Processes internal cleanup mutation that deletes acknowledged mutation results\n * from the upstream database. This runs without LMID tracking since it's an\n * internal operation.\n */\nasync function processCleanupResultsMutation<\n D extends Database<ExtractTransactionType<D>>,\n>(\n dbProvider: D,\n mutation: CustomMutation,\n queryParams: Params,\n lc: LogContext,\n): Promise<void> {\n const parseResult = v.test(mutation.args[0], cleanupResultsArgSchema);\n if (!parseResult.ok) {\n lc.warn?.('Cleanup mutation has invalid args', parseResult.error);\n return;\n }\n const args: CleanupResultsArg = parseResult.value;\n\n // Determine clientID for transaction input based on cleanup type\n // Note: legacy format without type field is treated as single\n const clientID =\n 'type' in args && args.type === 'bulk' ? args.clientIDs[0] : args.clientID;\n\n // Run in a transaction, using the hook for DB-specific operation.\n // Note: only upstreamSchema is used by deleteMutationResults; the other\n // fields are required by the interface but ignored for this operation.\n await dbProvider.transaction(\n async (_, hooks) => {\n await hooks.deleteMutationResults(args);\n },\n {\n upstreamSchema: queryParams.schema,\n clientGroupID: args.clientGroupID,\n clientID,\n mutationID: 0,\n },\n );\n}\n\ntype DatabaseTransactionPhase = 'open' | 'execute';\nclass DatabaseTransactionError extends Error {\n constructor(phase: DatabaseTransactionPhase, options?: ErrorOptions) {\n super(\n phase === 'open'\n ? `Failed to open database transaction: ${getErrorMessage(options?.cause)}`\n : `Database transaction failed after opening: ${getErrorMessage(options?.cause)}`,\n options,\n );\n this.name = 'DatabaseTransactionError';\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAoFA,IAAM,0BAA0B,OAAU,OAAqC;AAC7E,KAAI;AACF,SAAO,MAAM,IAAI;UACV,OAAO;AACd,MACE,iBAAiB,4BACjB,iBAAiB,sBACjB,iBAAiB,iCACjB,mBAAmB,MAAM,CAEzB,OAAM;AAGR,QAAM,yBAAyB,MAAM;;;;;;AAOzC,IAAa,wBAAwB;AA2BrC,eAAsB,oBAGpB,YACA,IAIA,sBACA,gBACA,UACuB;CAEvB,MAAM,oBAAoB,gCAAgC;CAE1D,IAAI;CACJ,IAAI;CACJ,IAAI;CAEJ,IAAI;AAEJ,KAAI,mBAAmB;AACrB,YAAU;AAIV,OAAK,iBAHU,kBAA2C,OAG9B,CAAC,YAAY,gBAAgB;AAGzD,gBADY,IAAI,IAAI,QAAQ,IAAI,CACd;AAElB,MAAI;AACF,cAAW,MAAM,QAAQ,MAAM;WACxB,OAAO;AACd,MAAG,QAAQ,6BAA6B,MAAM;GAC9C,MAAM,UAAU,8BAA8B,gBAAgB,MAAM;GACpE,MAAM,UAAU,gBAAgB,MAAM;AACtC,UAAO;IACL,MAAM;IACN,QAAQ;IACR,QAAQ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Y,0BAA0B,OAAO;AAEvE,KAAI,UAAU,QAAQ,CAGpB,SAAQ,IAAI,MAAM,QAAQ,QAAQ,GAAG;EAAC;EAAM;EAAK;EAAG,CAAC;AAIvD,QAAO;;AAGT,SAAS,gBACP,KACA,MACS;CACT,IAAI,UAAmB;AACvB,MAAK,MAAM,QAAQ,MAAM;AACvB,MAAI,OAAO,YAAY,YAAY,YAAY,QAAQ,EAAE,QAAQ,SAC/D;AAEF,YAAW,QAAoC;;AAEjD,QAAO;;;;;;;AAQT,eAAe,8BAGb,YACA,UACA,aACA,IACe;CACf,MAAM,cAAc,KAAO,SAAS,KAAK,IAAI,wBAAwB;AACrE,KAAI,CAAC,YAAY,IAAI;AACnB,KAAG,OAAO,qCAAqC,YAAY,MAAM;AACjE;;CAEF,MAAM,OAA0B,YAAY;CAI5C,MAAM,WACJ,UAAU,QAAQ,KAAK,SAAS,SAAS,KAAK,UAAU,KAAK,KAAK;AAKpE,OAAM,WAAW,YACf,OAAO,GAAG,UAAU;AAClB,QAAM,MAAM,sBAAsB,KAAK;IAEzC;EACE,gBAAgB,YAAY;EAC5B,eAAe,KAAK;EACpB;EACA,YAAY;EACb,CACF;;AAIH,IAAM,2BAAN,cAAuC,MAAM;CAC3C,YAAY,OAAiC,SAAwB;AACnE,QACE,UAAU,SACN,wCAAwC,gBAAgB,SAAS,MAAM,KACvE,8CAA8C,gBAAgB,SAAS,MAAM,IACjF,QACD;AACD,OAAK,OAAO"}
|
|
1
|
+
{"version":3,"file":"process-mutations.js","names":["#dbProvider","#req","#params","#lc","#transactImpl","#checkAndIncrementLastMutationID","#getTransactionInput"],"sources":["../../../../zero-server/src/process-mutations.ts"],"sourcesContent":["import type {LogContext, LogLevel} from '@rocicorp/logger';\nimport {assert} from '../../shared/src/asserts.ts';\nimport {getErrorDetails, getErrorMessage} from '../../shared/src/error.ts';\nimport type {ReadonlyJSONValue} from '../../shared/src/json.ts';\nimport {promiseVoid} from '../../shared/src/resolved-promises.ts';\nimport type {MaybePromise} from '../../shared/src/types.ts';\nimport * as v from '../../shared/src/valita.ts';\nimport {MutationAlreadyProcessedError} from '../../zero-cache/src/services/mutagen/error.ts';\nimport type {ApplicationError} from '../../zero-protocol/src/application-error.ts';\nimport {\n isApplicationError,\n wrapWithApplicationError,\n} from '../../zero-protocol/src/application-error.ts';\nimport {ErrorKind} from '../../zero-protocol/src/error-kind.ts';\nimport {ErrorOrigin} from '../../zero-protocol/src/error-origin.ts';\nimport {ErrorReason} from '../../zero-protocol/src/error-reason.ts';\nimport type {PushFailedBody} from '../../zero-protocol/src/error.ts';\nimport {\n mutateParamsSchema,\n type MutateResponse,\n} from '../../zero-protocol/src/mutate-server.ts';\nimport type {MutationID} from '../../zero-protocol/src/mutation-id.ts';\nimport {\n CLEANUP_RESULTS_MUTATION_NAME,\n cleanupResultsArgSchema,\n type CleanupResultsArg,\n type CustomMutation,\n type Mutation,\n} from '../../zero-protocol/src/mutation.ts';\nimport {\n pushBodySchema,\n type MutationResponse,\n type PushBody,\n} from '../../zero-protocol/src/push.ts';\nimport type {AnyMutatorRegistry} from '../../zql/src/mutate/mutator-registry.ts';\nimport {isMutator} from '../../zql/src/mutate/mutator.ts';\nimport type {CustomMutatorDefs, CustomMutatorImpl} from './custom.ts';\nimport {createLogContext} from './logging.ts';\nimport {separatorRe} from './push-processor.ts';\n\nexport interface TransactionProviderHooks {\n updateClientMutationID: () => Promise<{lastMutationID: number | bigint}>;\n writeMutationResult: (result: MutationResponse) => Promise<void>;\n deleteMutationResults: (args: CleanupResultsArg) => Promise<void>;\n}\n\nexport interface TransactionProviderInput {\n upstreamSchema: string;\n clientGroupID: string;\n clientID: string;\n mutationID: number;\n}\n\n/**\n * Defines the abstract interface for a database that PushProcessor can execute\n * transactions against.\n */\nexport interface Database<T> {\n transaction: <R>(\n callback: (\n tx: T,\n transactionHooks: TransactionProviderHooks,\n ) => MaybePromise<R>,\n transactionInput?: TransactionProviderInput,\n ) => Promise<R>;\n}\n\nexport type ExtractTransactionType<D> = D extends Database<infer T> ? T : never;\nexport type Params = v.Infer<typeof mutateParamsSchema>;\n\nexport type TransactFn<D extends Database<ExtractTransactionType<D>>> = (\n cb: TransactFnCallback<D>,\n) => Promise<MutationResponse>;\n\nexport type TransactFnCallback<D extends Database<ExtractTransactionType<D>>> =\n (\n tx: ExtractTransactionType<D>,\n mutatorName: string,\n mutatorArgs: ReadonlyJSONValue | undefined,\n ) => Promise<void>;\n\nexport type Parsed<D extends Database<ExtractTransactionType<D>>> = {\n transact: TransactFn<D>;\n mutations: CustomMutation[];\n};\n\ntype MutationPhase = 'preTransaction' | 'transactionPending' | 'postCommit';\n\nconst applicationErrorWrapper = async <T>(fn: () => Promise<T>): Promise<T> => {\n try {\n return await fn();\n } catch (error) {\n if (\n error instanceof DatabaseTransactionError ||\n error instanceof OutOfOrderMutation ||\n error instanceof MutationAlreadyProcessedError ||\n isApplicationError(error)\n ) {\n throw error;\n }\n\n throw wrapWithApplicationError(error);\n }\n};\n\n/**\n * @deprecated Use {@linkcode handleMutateRequest} instead.\n */\nexport const handleMutationRequest = handleMutateRequest;\n\ntype QueryParams = URLSearchParams | Record<string, string>;\n\nexport type MutateRequestOptions = {\n userID?: string | null | undefined;\n logLevel?: LogLevel | undefined;\n};\n\ntype NormalizedMutateRequestArgs = {\n readonly requestOrJsonBody: Request | ReadonlyJSONValue;\n readonly userID: string | null | undefined;\n readonly queryParams: QueryParams;\n readonly logLevel: LogLevel;\n};\n\n/**\n * Process a `/mutate` request from a `Request`.\n *\n * @param dbProvider - Database used to run transactions and store mutation\n * results.\n * @param cb - Runs once per custom mutation. Use `transact` for DB work.\n * Errors before `transact` are saved; errors after commit are logged.\n * @param request - Request to read query params and JSON body from.\n * @param logLevelOrOptions - Either a log level or additional request\n * options.\n * @returns A `MutateResponse`. Success returns `userID: options.userID ?? null`\n * when `options.userID` is provided; invalid requests return the `PushFailed`\n * branch.\n */\nexport function handleMutateRequest<\n D extends Database<ExtractTransactionType<D>>,\n>(\n dbProvider: D,\n cb: (\n transact: TransactFn<D>,\n mutation: CustomMutation,\n ) => Promise<MutationResponse>,\n request: Request,\n logLevelOrOptions?: LogLevel | MutateRequestOptions,\n): Promise<MutateResponse>;\n\n/**\n * Process a `/mutate` request from parsed query params and a parsed JSON body.\n *\n * @param dbProvider - Database used to run transactions and store mutation\n * results.\n * @param cb - Runs once per custom mutation. Use `transact` for DB work.\n * Errors before `transact` are saved; errors after commit are logged.\n * @param queryParams - Parsed query params.\n * @param jsonBody - Parsed JSON body.\n * @param logLevelOrOptions - Either a log level or additional request\n * options.\n * @returns A `MutateResponse`. Success returns `userID: options.userID ?? null`\n * when `options.userID` is provided; invalid requests return the `PushFailed`\n * branch.\n */\nexport function handleMutateRequest<\n D extends Database<ExtractTransactionType<D>>,\n>(\n dbProvider: D,\n cb: (\n transact: TransactFn<D>,\n mutation: CustomMutation,\n ) => Promise<MutationResponse>,\n queryParams: QueryParams,\n jsonBody: ReadonlyJSONValue,\n logLevelOrOptions?: LogLevel | MutateRequestOptions,\n): Promise<MutateResponse>;\n\nexport async function handleMutateRequest<\n D extends Database<ExtractTransactionType<D>>,\n>(\n dbProvider: D,\n cb: (\n transact: TransactFn<D>,\n mutation: CustomMutation,\n ) => Promise<MutationResponse>,\n requestOrQueryParams: Request | QueryParams,\n jsonBodyOrLogLevelOrOptions?:\n | ReadonlyJSONValue\n | LogLevel\n | MutateRequestOptions,\n logLevelOrOptions?: LogLevel | MutateRequestOptions,\n): Promise<MutateResponse> {\n let requestOrJsonBody: Request | ReadonlyJSONValue;\n let queryParams: QueryParams;\n let optionsArg: LogLevel | MutateRequestOptions | undefined;\n\n if (requestOrQueryParams instanceof Request) {\n requestOrJsonBody = requestOrQueryParams;\n queryParams = new URL(requestOrQueryParams.url).searchParams;\n optionsArg = jsonBodyOrLogLevelOrOptions as\n | LogLevel\n | MutateRequestOptions\n | undefined;\n } else {\n requestOrJsonBody = jsonBodyOrLogLevelOrOptions as ReadonlyJSONValue;\n queryParams = requestOrQueryParams;\n optionsArg = logLevelOrOptions;\n }\n\n const options = normalizeMutateRequestOptions(optionsArg);\n const normalized: NormalizedMutateRequestArgs = {\n requestOrJsonBody,\n userID: 'userID' in options ? (options.userID ?? null) : undefined,\n queryParams,\n logLevel: options.logLevel ?? 'info',\n };\n const lc = createLogContext(normalized.logLevel).withContext('PushProcessor');\n let jsonBody: unknown;\n\n if (normalized.requestOrJsonBody instanceof Request) {\n try {\n jsonBody = await normalized.requestOrJsonBody.json();\n } catch (error) {\n lc.error?.('Failed to parse push body', error);\n const message = `Failed to parse push body: ${getErrorMessage(error)}`;\n const details = getErrorDetails(error);\n return {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason: ErrorReason.Parse,\n message,\n mutationIDs: [],\n ...(details ? {details} : {}),\n } as const satisfies PushFailedBody;\n }\n } else {\n jsonBody = normalized.requestOrJsonBody;\n }\n\n let mutationIDs: MutationID[] = [];\n\n let pushBody: PushBody;\n try {\n pushBody = v.parse(jsonBody, pushBodySchema, 'passthrough');\n mutationIDs = pushBody.mutations.map(m => ({\n id: m.id,\n clientID: m.clientID,\n }));\n } catch (error) {\n lc.error?.('Failed to parse push body', error);\n const message = `Failed to parse push body: ${getErrorMessage(error)}`;\n const details = getErrorDetails(error);\n return {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason: ErrorReason.Parse,\n message,\n mutationIDs,\n ...(details ? {details} : {}),\n } as const satisfies PushFailedBody;\n }\n\n let parsedQueryParams: Params;\n try {\n const rawQueryParams =\n normalized.queryParams instanceof URLSearchParams\n ? Object.fromEntries(normalized.queryParams)\n : normalized.queryParams;\n parsedQueryParams = v.parse(\n rawQueryParams,\n mutateParamsSchema,\n 'passthrough',\n );\n } catch (error) {\n lc.error?.('Failed to parse push query parameters', error);\n const message = `Failed to parse push query parameters: ${getErrorMessage(error)}`;\n const details = getErrorDetails(error);\n return {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason: ErrorReason.Parse,\n message,\n mutationIDs,\n ...(details ? {details} : {}),\n } as const satisfies PushFailedBody;\n }\n\n if (pushBody.pushVersion !== 1) {\n const response = {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason: ErrorReason.UnsupportedPushVersion,\n mutationIDs,\n message: `Unsupported push version: ${pushBody.pushVersion}`,\n } as const satisfies PushFailedBody;\n return response;\n }\n\n const responses: MutationResponse[] = [];\n let processedCount = 0;\n\n try {\n const transactor = new Transactor(\n dbProvider,\n pushBody,\n parsedQueryParams,\n lc,\n );\n\n // Each mutation goes through three phases:\n // 1. Pre-transaction: user logic that runs before `transact` is called. If\n // this throws we still advance LMID and persist the failure result.\n // 2. Transaction: the callback passed to `transact`, which can be retried\n // if it fails with an ApplicationError.\n // 3. Post-commit: any logic that runs after `transact` resolves. Failures\n // here are logged but the mutation remains committed.\n for (const m of pushBody.mutations) {\n // Handle internal mutations (like cleanup) directly without user dispatch\n if (m.type === 'custom' && m.name === CLEANUP_RESULTS_MUTATION_NAME) {\n lc.debug?.(\n `Processing internal mutation '${m.name}' (clientID=${m.clientID})`,\n );\n try {\n await processCleanupResultsMutation(\n dbProvider,\n m,\n parsedQueryParams,\n lc,\n );\n // No response added - this is fire-and-forget\n processedCount++;\n } catch (error) {\n lc.warn?.(\n `Failed to process cleanup mutation for client ${m.clientID}`,\n error,\n );\n // Don't fail the whole push for cleanup errors\n processedCount++;\n }\n continue;\n }\n\n assert(m.type === 'custom', 'Expected custom mutation');\n lc.debug?.(\n `Processing mutation '${m.name}' (id=${m.id}, clientID=${m.clientID})`,\n );\n\n let mutationPhase: MutationPhase = 'preTransaction';\n\n const transactProxy: TransactFn<D> = async innerCb => {\n mutationPhase = 'transactionPending';\n const result = await transactor.transact(m, innerCb);\n mutationPhase = 'postCommit';\n return result;\n };\n\n try {\n const res = await applicationErrorWrapper(() => cb(transactProxy, m));\n responses.push(res);\n lc.debug?.(`Mutation '${m.name}' (id=${m.id}) completed successfully`);\n\n processedCount++;\n } catch (error) {\n if (!isApplicationError(error)) {\n throw error;\n }\n\n if (mutationPhase === 'preTransaction') {\n // Pre-transaction\n await transactor.persistPreTransactionFailure(m, error);\n } else if (mutationPhase === 'postCommit') {\n // Post-commit\n lc.error?.(\n `Post-commit mutation handler failed for mutation ${m.id} for client ${m.clientID}`,\n error,\n );\n }\n\n lc.warn?.(\n `Application error processing mutation ${m.id} for client ${m.clientID}`,\n error,\n );\n responses.push(makeAppErrorResponse(m, error));\n\n processedCount++;\n }\n }\n\n return {\n kind: 'MutateResponse',\n mutations: responses,\n ...(typeof normalized.userID !== 'undefined'\n ? {userID: normalized.userID}\n : {}),\n } as const satisfies MutateResponse;\n } catch (error) {\n lc.error?.('Failed to process push request', error);\n // only include mutationIDs for mutations that were not processed\n const unprocessedMutationIDs = mutationIDs.slice(processedCount);\n\n const message = getErrorMessage(error);\n const details = getErrorDetails(error);\n\n return {\n kind: ErrorKind.PushFailed,\n origin: ErrorOrigin.Server,\n reason:\n error instanceof OutOfOrderMutation\n ? ErrorReason.OutOfOrderMutation\n : error instanceof DatabaseTransactionError\n ? ErrorReason.Database\n : ErrorReason.Internal,\n message,\n mutationIDs: unprocessedMutationIDs,\n ...(details ? {details} : {}),\n };\n }\n}\n\nfunction normalizeMutateRequestOptions(\n logLevelOrOptions: LogLevel | MutateRequestOptions | undefined,\n): MutateRequestOptions {\n return typeof logLevelOrOptions === 'string'\n ? {logLevel: logLevelOrOptions}\n : (logLevelOrOptions ?? {});\n}\n\nclass Transactor<D extends Database<ExtractTransactionType<D>>> {\n readonly #dbProvider: D;\n readonly #req: PushBody;\n readonly #params: Params;\n readonly #lc: LogContext;\n\n constructor(dbProvider: D, req: PushBody, params: Params, lc: LogContext) {\n this.#dbProvider = dbProvider;\n this.#req = req;\n this.#params = params;\n this.#lc = lc;\n }\n\n transact = async (\n mutation: CustomMutation,\n cb: TransactFnCallback<D>,\n ): Promise<MutationResponse> => {\n let appError: ApplicationError | undefined = undefined;\n for (;;) {\n try {\n const ret = await this.#transactImpl(mutation, cb, appError);\n if (appError !== undefined) {\n this.#lc.warn?.(\n `Mutation ${mutation.id} for client ${mutation.clientID} was retried after an error`,\n appError,\n );\n return makeAppErrorResponse(mutation, appError);\n }\n\n return ret;\n } catch (error) {\n if (error instanceof OutOfOrderMutation) {\n this.#lc.error?.(error);\n throw error;\n }\n\n if (error instanceof MutationAlreadyProcessedError) {\n this.#lc.warn?.(error);\n return {\n id: {\n clientID: mutation.clientID,\n id: mutation.id,\n },\n result: {\n error: 'alreadyProcessed',\n details: error.message,\n },\n };\n }\n\n if (appError !== undefined) {\n // Retry also failed → internal error, cannot skip mutation\n this.#lc.error?.(\n `Retry also failed for mutation ${mutation.id} for client ${mutation.clientID}`,\n error,\n );\n throw error;\n }\n\n // First attempt failed → store error and retry without mutator\n const originalError =\n error instanceof DatabaseTransactionError\n ? (error.cause ?? error)\n : error;\n appError = wrapWithApplicationError(originalError);\n this.#lc.warn?.(\n `Error processing mutation ${mutation.id} for client ${mutation.clientID}, retrying without mutator`,\n appError,\n );\n continue;\n }\n }\n };\n\n async persistPreTransactionFailure(\n mutation: CustomMutation,\n appError: ApplicationError<ReadonlyJSONValue | undefined>,\n ): Promise<MutationResponse> {\n // User-land code threw before calling `transact`. We still need to bump the\n // LMID for this mutation and persist the error so that the client knows it failed.\n const ret = await this.#transactImpl(\n mutation,\n // noop callback since there's no transaction to execute\n () => promiseVoid,\n appError,\n );\n return ret;\n }\n\n async #transactImpl(\n mutation: CustomMutation,\n cb: TransactFnCallback<D>,\n appError: ApplicationError | undefined,\n ): Promise<MutationResponse> {\n let transactionPhase: DatabaseTransactionPhase = 'open';\n\n try {\n const ret = await this.#dbProvider.transaction(\n async (dbTx, transactionHooks) => {\n // update the transaction phase to 'execute' after the transaction is opened\n transactionPhase = 'execute';\n\n await this.#checkAndIncrementLastMutationID(\n transactionHooks,\n mutation.clientID,\n mutation.id,\n );\n\n if (appError === undefined) {\n this.#lc.debug?.(\n `Executing mutator '${mutation.name}' (id=${mutation.id})`,\n );\n await cb(dbTx, mutation.name, mutation.args[0]);\n } else {\n const mutationResult = makeAppErrorResponse(mutation, appError);\n await transactionHooks.writeMutationResult(mutationResult);\n }\n\n return {\n id: {\n clientID: mutation.clientID,\n id: mutation.id,\n },\n result: {},\n };\n },\n this.#getTransactionInput(mutation),\n );\n\n return ret;\n } catch (error) {\n if (\n isApplicationError(error) ||\n error instanceof OutOfOrderMutation ||\n error instanceof MutationAlreadyProcessedError\n ) {\n throw error;\n }\n\n throw new DatabaseTransactionError(transactionPhase, {cause: error});\n }\n }\n\n #getTransactionInput(mutation: CustomMutation): TransactionProviderInput {\n return {\n upstreamSchema: this.#params.schema,\n clientGroupID: this.#req.clientGroupID,\n clientID: mutation.clientID,\n mutationID: mutation.id,\n };\n }\n\n async #checkAndIncrementLastMutationID(\n transactionHooks: TransactionProviderHooks,\n clientID: string,\n receivedMutationID: number,\n ) {\n const {lastMutationID} = await transactionHooks.updateClientMutationID();\n\n if (receivedMutationID < lastMutationID) {\n throw new MutationAlreadyProcessedError(\n clientID,\n receivedMutationID,\n lastMutationID,\n );\n } else if (receivedMutationID > lastMutationID) {\n throw new OutOfOrderMutation(\n clientID,\n receivedMutationID,\n lastMutationID,\n );\n }\n }\n}\n\nexport class OutOfOrderMutation extends Error {\n constructor(\n clientID: string,\n receivedMutationID: number,\n lastMutationID: number | bigint,\n ) {\n super(\n `Client ${clientID} sent mutation ID ${receivedMutationID} but expected ${lastMutationID}`,\n );\n }\n}\n\nfunction makeAppErrorResponse(\n m: Mutation,\n error: ApplicationError<ReadonlyJSONValue | undefined>,\n): MutationResponse {\n return {\n id: {\n clientID: m.clientID,\n id: m.id,\n },\n result: {\n error: 'app',\n message: error.message,\n ...(error.details ? {details: error.details} : {}),\n },\n };\n}\n\n/** @deprecated Use getMutator instead */\nexport function getMutation(\n // oxlint-disable-next-line no-explicit-any\n mutators: AnyMutatorRegistry | CustomMutatorDefs<any>,\n name: string,\n // oxlint-disable-next-line no-explicit-any\n): CustomMutatorImpl<any> {\n const path = name.split(separatorRe);\n const mutator = getObjectAtPath(mutators, path);\n assert(typeof mutator === 'function', `could not find mutator ${name}`);\n\n if (isMutator(mutator)) {\n // mutator needs to be called with {tx, args, ctx}\n // CustomMutatorImpl is called with (tx, args, ctx)\n return (tx, args, ctx) => mutator.fn({args, ctx, tx});\n }\n\n // oxlint-disable-next-line no-explicit-any\n return mutator as CustomMutatorImpl<any>;\n}\n\nfunction getObjectAtPath(\n obj: Record<string, unknown>,\n path: string[],\n): unknown {\n let current: unknown = obj;\n for (const part of path) {\n if (typeof current !== 'object' || current === null || !(part in current)) {\n return undefined;\n }\n current = (current as Record<string, unknown>)[part];\n }\n return current;\n}\n\n/**\n * Processes internal cleanup mutation that deletes acknowledged mutation results\n * from the upstream database. This runs without LMID tracking since it's an\n * internal operation.\n */\nasync function processCleanupResultsMutation<\n D extends Database<ExtractTransactionType<D>>,\n>(\n dbProvider: D,\n mutation: CustomMutation,\n queryParams: Params,\n lc: LogContext,\n): Promise<void> {\n const parseResult = v.test(mutation.args[0], cleanupResultsArgSchema);\n if (!parseResult.ok) {\n lc.warn?.('Cleanup mutation has invalid args', parseResult.error);\n return;\n }\n const args: CleanupResultsArg = parseResult.value;\n\n // Determine clientID for transaction input based on cleanup type\n // Note: legacy format without type field is treated as single\n const clientID =\n 'type' in args && args.type === 'bulk' ? args.clientIDs[0] : args.clientID;\n\n // Run in a transaction, using the hook for DB-specific operation.\n // Note: only upstreamSchema is used by deleteMutationResults; the other\n // fields are required by the interface but ignored for this operation.\n await dbProvider.transaction(\n async (_, hooks) => {\n await hooks.deleteMutationResults(args);\n },\n {\n upstreamSchema: queryParams.schema,\n clientGroupID: args.clientGroupID,\n clientID,\n mutationID: 0,\n },\n );\n}\n\ntype DatabaseTransactionPhase = 'open' | 'execute';\nclass DatabaseTransactionError extends Error {\n constructor(phase: DatabaseTransactionPhase, options?: ErrorOptions) {\n super(\n phase === 'open'\n ? `Failed to open database transaction: ${getErrorMessage(options?.cause)}`\n : `Database transaction failed after opening: ${getErrorMessage(options?.cause)}`,\n options,\n );\n this.name = 'DatabaseTransactionError';\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAwFA,IAAM,0BAA0B,OAAU,OAAqC;AAC7E,KAAI;AACF,SAAO,MAAM,IAAI;UACV,OAAO;AACd,MACE,iBAAiB,4BACjB,iBAAiB,sBACjB,iBAAiB,iCACjB,mBAAmB,MAAM,CAEzB,OAAM;AAGR,QAAM,yBAAyB,MAAM;;;;;;AAOzC,IAAa,wBAAwB;AAsErC,eAAsB,oBAGpB,YACA,IAIA,sBACA,6BAIA,mBACyB;CACzB,IAAI;CACJ,IAAI;CACJ,IAAI;AAEJ,KAAI,gCAAgC,SAAS;AAC3C,sBAAoB;AACpB,gBAAc,IAAI,IAAI,qBAAqB,IAAI,CAAC;AAChD,eAAa;QAIR;AACL,sBAAoB;AACpB,gBAAc;AACd,eAAa;;CAGf,MAAM,UAAU,8BAA8B,WAAW;CACzD,MAAM,aAA0C;EAC9C;EACA,QAAQ,YAAY,UAAW,QAAQ,UAAU,OAAQ,KAAA;EACzD;EACA,UAAU,QAAQ,YAAY;EAC/B;CACD,MAAM,KAAK,iBAAiB,WAAW,SAAS,CAAC,YAAY,gBAAgB;CAC7E,IAAI;AAEJ,KAAI,WAAW,6BAA6B,QAC1C,KAAI;AACF,aAAW,MAAM,WAAW,kBAAkB,MAAM;UAC7C,OAAO;AACd,KAAG,QAAQ,6BAA6B,MAAM;EAC9C,MAAM,UAAU,8BAA8B,gBAAgB,MAAM;EACpE,MAAM,UAAU,gBAAgB,MAAM;AACtC,SAAO;GACL,MAAM;GACN,QAAQ;GACR,QAAQ;GACR;GACA,aAAa,EAAE;GACf,GAAI,UAAU,EAAC,SAAQ,GAAG,EAAE;GAC7B;;KAGH,YAAW,WAAW;CAGxB,IAAI,cAA4B,EAAE;CAElC,IAAI;AACJ,KAAI;AACF,aAAW,MAAQ,UAAU,gBAAgB,cAAc;AAC3D,gBAAc,SAAS,UAA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QAAQ,QAAQ,GAAG;EAAC;EAAM;EAAK;EAAG,CAAC;AAIvD,QAAO;;AAGT,SAAS,gBACP,KACA,MACS;CACT,IAAI,UAAmB;AACvB,MAAK,MAAM,QAAQ,MAAM;AACvB,MAAI,OAAO,YAAY,YAAY,YAAY,QAAQ,EAAE,QAAQ,SAC/D;AAEF,YAAW,QAAoC;;AAEjD,QAAO;;;;;;;AAQT,eAAe,8BAGb,YACA,UACA,aACA,IACe;CACf,MAAM,cAAc,KAAO,SAAS,KAAK,IAAI,wBAAwB;AACrE,KAAI,CAAC,YAAY,IAAI;AACnB,KAAG,OAAO,qCAAqC,YAAY,MAAM;AACjE;;CAEF,MAAM,OAA0B,YAAY;CAI5C,MAAM,WACJ,UAAU,QAAQ,KAAK,SAAS,SAAS,KAAK,UAAU,KAAK,KAAK;AAKpE,OAAM,WAAW,YACf,OAAO,GAAG,UAAU;AAClB,QAAM,MAAM,sBAAsB,KAAK;IAEzC;EACE,gBAAgB,YAAY;EAC5B,eAAe,KAAK;EACpB;EACA,YAAY;EACb,CACF;;AAIH,IAAM,2BAAN,cAAuC,MAAM;CAC3C,YAAY,OAAiC,SAAwB;AACnE,QACE,UAAU,SACN,wCAAwC,gBAAgB,SAAS,MAAM,KACvE,8CAA8C,gBAAgB,SAAS,MAAM,IACjF,QACD;AACD,OAAK,OAAO"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type LogLevel } from '@rocicorp/logger';
|
|
2
2
|
import type { ReadonlyJSONValue } from '../../shared/src/json.ts';
|
|
3
|
-
import {
|
|
3
|
+
import type { MutateResponse } from '../../zero-protocol/src/mutate-server.ts';
|
|
4
4
|
import { type Database, type ExtractTransactionType } from '../../zero-server/src/process-mutations.ts';
|
|
5
5
|
import type { Schema } from '../../zero-types/src/schema.ts';
|
|
6
6
|
import type { AnyMutatorRegistry } from '../../zql/src/mutate/mutator-registry.ts';
|
|
@@ -20,13 +20,13 @@ export declare class PushProcessor<_S extends Schema, D extends Database<Extract
|
|
|
20
20
|
* @param queryString the query string from the request sent by zero-cache. This will include zero's postgres schema name and appID.
|
|
21
21
|
* @param body the body of the request sent by zero-cache as a JSON object.
|
|
22
22
|
*/
|
|
23
|
-
process(mutators: MD, queryString: URLSearchParams | Record<string, string>, body: ReadonlyJSONValue): Promise<
|
|
23
|
+
process(mutators: MD, queryString: URLSearchParams | Record<string, string>, body: ReadonlyJSONValue): Promise<MutateResponse>;
|
|
24
24
|
/**
|
|
25
25
|
* This override gets the query string and the body from a Request object.
|
|
26
26
|
*
|
|
27
27
|
* @param mutators the custom mutators for the application
|
|
28
28
|
* @param request A `Request` object.
|
|
29
29
|
*/
|
|
30
|
-
process(mutators: MD, request: Request): Promise<
|
|
30
|
+
process(mutators: MD, request: Request): Promise<MutateResponse>;
|
|
31
31
|
}
|
|
32
32
|
//# sourceMappingURL=push-processor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"push-processor.d.ts","sourceRoot":"","sources":["../../../../zero-server/src/push-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,QAAQ,EAAC,MAAM,kBAAkB,CAAC;AAE/C,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAGhE,OAAO,
|
|
1
|
+
{"version":3,"file":"push-processor.d.ts","sourceRoot":"","sources":["../../../../zero-server/src/push-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,QAAQ,EAAC,MAAM,kBAAkB,CAAC;AAE/C,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAGhE,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,0CAA0C,CAAC;AAG7E,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,sBAAsB,EAG5B,MAAM,4CAA4C,CAAC;AACpD,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,gCAAgC,CAAC;AAE3D,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,0CAA0C,CAAC;AAEjF,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,aAAa,CAAC;AAEnD,eAAO,MAAM,WAAW,QAAS,CAAC;AAElC,qBAAa,aAAa,CACxB,EAAE,SAAS,MAAM,EACjB,CAAC,SAAS,QAAQ,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,EAC7C,EAAE,SAAS,kBAAkB,GAAG,iBAAiB,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,EAC5E,CAAC,GAAG,SAAS;;gBAMD,UAAU,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,CAAC,EAAE,QAAQ,GAAE,QAAiB;IAMnE;;;;;;;;;;OAUG;IACH,OAAO,CACL,QAAQ,EAAE,EAAE,EACZ,WAAW,EAAE,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACrD,IAAI,EAAE,iBAAiB,GACtB,OAAO,CAAC,cAAc,CAAC;IAE1B;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,EAAE,EAAE,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC;CAqDjE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"push-processor.js","names":["#dbProvider","#logLevel","#context","#processMutation","#dispatchMutation"],"sources":["../../../../zero-server/src/push-processor.ts"],"sourcesContent":["import {type LogLevel} from '@rocicorp/logger';\nimport {assert} from '../../shared/src/asserts.ts';\nimport type {ReadonlyJSONValue} from '../../shared/src/json.ts';\nimport {must} from '../../shared/src/must.ts';\nimport {getValueAtPath} from '../../shared/src/object-traversal.ts';\nimport {
|
|
1
|
+
{"version":3,"file":"push-processor.js","names":["#dbProvider","#logLevel","#context","#processMutation","#dispatchMutation"],"sources":["../../../../zero-server/src/push-processor.ts"],"sourcesContent":["import {type LogLevel} from '@rocicorp/logger';\nimport {assert} from '../../shared/src/asserts.ts';\nimport type {ReadonlyJSONValue} from '../../shared/src/json.ts';\nimport {must} from '../../shared/src/must.ts';\nimport {getValueAtPath} from '../../shared/src/object-traversal.ts';\nimport type {MutateResponse} from '../../zero-protocol/src/mutate-server.ts';\nimport type {CustomMutation} from '../../zero-protocol/src/mutation.ts';\nimport {type MutationResponse} from '../../zero-protocol/src/push.ts';\nimport {\n type Database,\n type ExtractTransactionType,\n handleMutateRequest,\n type TransactFn,\n} from '../../zero-server/src/process-mutations.ts';\nimport type {Schema} from '../../zero-types/src/schema.ts';\nimport type {Transaction} from '../../zql/src/mutate/custom.ts';\nimport type {AnyMutatorRegistry} from '../../zql/src/mutate/mutator-registry.ts';\nimport {isMutator} from '../../zql/src/mutate/mutator.ts';\nimport type {CustomMutatorDefs} from './custom.ts';\n\nexport const separatorRe = /[.|]/;\n\nexport class PushProcessor<\n _S extends Schema,\n D extends Database<ExtractTransactionType<D>>,\n MD extends AnyMutatorRegistry | CustomMutatorDefs<ExtractTransactionType<D>>,\n C = undefined,\n> {\n readonly #dbProvider: D;\n readonly #logLevel: LogLevel;\n readonly #context: C;\n\n constructor(dbProvider: D, context?: C, logLevel: LogLevel = 'info') {\n this.#dbProvider = dbProvider;\n this.#context = context as C;\n this.#logLevel = logLevel;\n }\n\n /**\n * Processes a push request from zero-cache.\n * This function will parse the request, check the protocol version, and process each mutation in the request.\n * - If a mutation is out of order: processing will stop and an error will be returned. The zero client will retry the mutation.\n * - If a mutation has already been processed: it will be skipped and the processing will continue.\n * - If a mutation receives an application error: it will be skipped, the error will be returned to the client, and processing will continue.\n *\n * @param mutators the custom mutators for the application\n * @param queryString the query string from the request sent by zero-cache. This will include zero's postgres schema name and appID.\n * @param body the body of the request sent by zero-cache as a JSON object.\n */\n process(\n mutators: MD,\n queryString: URLSearchParams | Record<string, string>,\n body: ReadonlyJSONValue,\n ): Promise<MutateResponse>;\n\n /**\n * This override gets the query string and the body from a Request object.\n *\n * @param mutators the custom mutators for the application\n * @param request A `Request` object.\n */\n process(mutators: MD, request: Request): Promise<MutateResponse>;\n process(\n mutators: MD,\n queryOrQueryString: Request | URLSearchParams | Record<string, string>,\n body?: ReadonlyJSONValue,\n ): Promise<MutateResponse> {\n if (queryOrQueryString instanceof Request) {\n return handleMutateRequest(\n this.#dbProvider,\n (transact, mutation) =>\n this.#processMutation(mutators, transact, mutation),\n queryOrQueryString,\n this.#logLevel,\n );\n }\n return handleMutateRequest(\n this.#dbProvider,\n (transact, mutation) =>\n this.#processMutation(mutators, transact, mutation),\n queryOrQueryString,\n must(body, 'body is required when using query params directly'),\n this.#logLevel,\n );\n }\n\n #processMutation(\n mutators: MD,\n transact: TransactFn<D>,\n _mutation: CustomMutation,\n ): Promise<MutationResponse> {\n return transact((tx, name, args) =>\n this.#dispatchMutation(mutators, tx, name, args),\n );\n }\n\n #dispatchMutation(\n mutators: MD,\n dbTx: ExtractTransactionType<D>,\n key: string,\n args: ReadonlyJSONValue | undefined,\n ): Promise<void> {\n // Legacy mutators used | as a separator, new mutators use .\n const mutator = getValueAtPath(mutators, key, separatorRe);\n assert(typeof mutator === 'function', `could not find mutator ${key}`);\n if (isMutator(mutator)) {\n return mutator.fn({\n args,\n ctx: this.#context,\n tx: dbTx as Transaction<Schema, unknown>,\n });\n }\n return mutator(dbTx, args);\n }\n}\n"],"mappings":";;;;;;;;AAoBA,IAAa,cAAc;AAE3B,IAAa,gBAAb,MAKE;CACA;CACA;CACA;CAEA,YAAY,YAAe,SAAa,WAAqB,QAAQ;AACnE,QAAA,aAAmB;AACnB,QAAA,UAAgB;AAChB,QAAA,WAAiB;;CA2BnB,QACE,UACA,oBACA,MACyB;AACzB,MAAI,8BAA8B,QAChC,QAAO,oBACL,MAAA,aACC,UAAU,aACT,MAAA,gBAAsB,UAAU,UAAU,SAAS,EACrD,oBACA,MAAA,SACD;AAEH,SAAO,oBACL,MAAA,aACC,UAAU,aACT,MAAA,gBAAsB,UAAU,UAAU,SAAS,EACrD,oBACA,KAAK,MAAM,oDAAoD,EAC/D,MAAA,SACD;;CAGH,iBACE,UACA,UACA,WAC2B;AAC3B,SAAO,UAAU,IAAI,MAAM,SACzB,MAAA,iBAAuB,UAAU,IAAI,MAAM,KAAK,CACjD;;CAGH,kBACE,UACA,MACA,KACA,MACe;EAEf,MAAM,UAAU,eAAe,UAAU,KAAK,YAAY;AAC1D,SAAO,OAAO,YAAY,YAAY,0BAA0B,MAAM;AACtE,MAAI,UAAU,QAAQ,CACpB,QAAO,QAAQ,GAAG;GAChB;GACA,KAAK,MAAA;GACL,IAAI;GACL,CAAC;AAEJ,SAAO,QAAQ,MAAM,KAAK"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { LogLevel } from '@rocicorp/logger';
|
|
2
2
|
import type { ReadonlyJSONValue } from '../../../shared/src/json.ts';
|
|
3
3
|
import type { MaybePromise } from '../../../shared/src/types.ts';
|
|
4
|
-
import {
|
|
4
|
+
import type { QueryResponse } from '../../../zero-protocol/src/query-server.ts';
|
|
5
5
|
import type { Schema } from '../../../zero-types/src/schema.ts';
|
|
6
6
|
import type { AnyQuery } from '../../../zql/src/query/query.ts';
|
|
7
7
|
/**
|
|
@@ -15,7 +15,7 @@ import type { AnyQuery } from '../../../zql/src/query/query.ts';
|
|
|
15
15
|
*/
|
|
16
16
|
export declare function handleGetQueriesRequest<S extends Schema>(cb: (name: string, args: readonly ReadonlyJSONValue[]) => MaybePromise<{
|
|
17
17
|
query: AnyQuery;
|
|
18
|
-
} | AnyQuery>, schema: S, requestOrJsonBody: Request | ReadonlyJSONValue, logLevel?: LogLevel): Promise<
|
|
18
|
+
} | AnyQuery>, schema: S, requestOrJsonBody: Request | ReadonlyJSONValue, logLevel?: LogLevel): Promise<QueryResponse>;
|
|
19
19
|
/**
|
|
20
20
|
* Invokes the callback `cb` for each query in the request or JSON body.
|
|
21
21
|
* The callback should return a Query or Promise<Query> that is the transformed result.
|
|
@@ -27,59 +27,29 @@ export declare function handleGetQueriesRequest<S extends Schema>(cb: (name: str
|
|
|
27
27
|
*/
|
|
28
28
|
export declare function handleTransformRequest<S extends Schema>(cb: (name: string, args: readonly ReadonlyJSONValue[]) => MaybePromise<{
|
|
29
29
|
query: AnyQuery;
|
|
30
|
-
} | AnyQuery>, schema: S, requestOrJsonBody: Request | ReadonlyJSONValue, logLevel?: LogLevel): Promise<
|
|
30
|
+
} | AnyQuery>, schema: S, requestOrJsonBody: Request | ReadonlyJSONValue, logLevel?: LogLevel): Promise<QueryResponse>;
|
|
31
|
+
export type QueryRequestOptions = {
|
|
32
|
+
userID?: string | null | undefined;
|
|
33
|
+
logLevel?: LogLevel | undefined;
|
|
34
|
+
};
|
|
31
35
|
/**
|
|
32
|
-
*
|
|
33
|
-
* The callback should return a Query that is the transformed result.
|
|
36
|
+
* Process a `/query` request.
|
|
34
37
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* @param
|
|
38
|
-
* @param
|
|
39
|
-
* @param
|
|
40
|
-
*
|
|
41
|
-
* @returns A
|
|
38
|
+
* @param transformQuery - Runs once per requested query with the query name
|
|
39
|
+
* and first JSON argument. Returns a `Query`.
|
|
40
|
+
* @param schema - Schema used when building the returned ASTs.
|
|
41
|
+
* @param request - A Fetch `Request`.
|
|
42
|
+
* @param logLevelOrOptions - Either a log level or additional request
|
|
43
|
+
* options.
|
|
44
|
+
* @returns A `QueryResponse`. Success returns `userID: options.userID ?? null`
|
|
45
|
+
* when `options.userID` is provided. Per-query errors stay in `queries`;
|
|
46
|
+
* malformed requests return `TransformFailed`.
|
|
47
|
+
*/
|
|
48
|
+
export declare function handleQueryRequest<S extends Schema>(transformQuery: TransformQueryFunction, schema: S, request: Request, logLevelOrOptions?: LogLevel | QueryRequestOptions): Promise<QueryResponse>;
|
|
49
|
+
/**
|
|
50
|
+
* Process a `/query` request from a parsed JSON body.
|
|
42
51
|
*/
|
|
43
|
-
export declare function handleQueryRequest<S extends Schema>(transformQuery: TransformQueryFunction, schema: S,
|
|
44
|
-
message?: string | undefined;
|
|
45
|
-
details?: ReadonlyJSONValue | undefined;
|
|
46
|
-
error: "app";
|
|
47
|
-
id: string;
|
|
48
|
-
name: string;
|
|
49
|
-
} | {
|
|
50
|
-
details?: ReadonlyJSONValue | undefined;
|
|
51
|
-
error: "parse";
|
|
52
|
-
id: string;
|
|
53
|
-
name: string;
|
|
54
|
-
message: string;
|
|
55
|
-
} | {
|
|
56
|
-
id: string;
|
|
57
|
-
name: string;
|
|
58
|
-
ast: import("../../../zero-protocol/src/ast.ts").AST;
|
|
59
|
-
})[]] | ["transformFailed", {
|
|
60
|
-
details?: ReadonlyJSONValue | undefined;
|
|
61
|
-
message: string;
|
|
62
|
-
kind: "TransformFailed";
|
|
63
|
-
queryIDs: string[];
|
|
64
|
-
origin: "server";
|
|
65
|
-
reason: "parse" | "database" | "internal";
|
|
66
|
-
} | {
|
|
67
|
-
details?: ReadonlyJSONValue | undefined;
|
|
68
|
-
bodyPreview?: string | undefined;
|
|
69
|
-
message: string;
|
|
70
|
-
kind: "TransformFailed";
|
|
71
|
-
queryIDs: string[];
|
|
72
|
-
origin: "zeroCache";
|
|
73
|
-
reason: "http";
|
|
74
|
-
status: number;
|
|
75
|
-
} | {
|
|
76
|
-
details?: ReadonlyJSONValue | undefined;
|
|
77
|
-
message: string;
|
|
78
|
-
kind: "TransformFailed";
|
|
79
|
-
queryIDs: string[];
|
|
80
|
-
origin: "zeroCache";
|
|
81
|
-
reason: "parse" | "internal" | "timeout";
|
|
82
|
-
}]>;
|
|
52
|
+
export declare function handleQueryRequest<S extends Schema>(transformQuery: TransformQueryFunction, schema: S, jsonBody: ReadonlyJSONValue, logLevelOrOptions?: LogLevel | QueryRequestOptions): Promise<QueryResponse>;
|
|
83
53
|
/**
|
|
84
54
|
* A function that transforms a query by name and arguments into a Query object.
|
|
85
55
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process-queries.d.ts","sourceRoot":"","sources":["../../../../../zero-server/src/queries/process-queries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,kBAAkB,CAAC;AAE/C,OAAO,KA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