Skip to content

Commit a069003

Browse files
authored
chore(runtime): replace traverse dependency (#2160)
1 parent 7ed36bb commit a069003

File tree

6 files changed

+74
-346
lines changed

6 files changed

+74
-346
lines changed

packages/runtime/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
"safe-json-stringify": "^1.2.0",
108108
"semver": "^7.5.2",
109109
"superjson": "^1.13.0",
110-
"traverse": "^0.6.10",
111110
"ts-pattern": "^4.3.0",
112111
"tslib": "^2.4.1",
113112
"uuid": "^9.0.0",
@@ -127,7 +126,6 @@
127126
"@types/pluralize": "^0.0.29",
128127
"@types/safe-json-stringify": "^1.1.5",
129128
"@types/semver": "^7.3.13",
130-
"@types/traverse": "^0.6.37",
131129
"@types/uuid": "^8.3.4",
132130
"decimal.js-light": "^2.5.1",
133131
"superjson": "^1.13.0",

packages/runtime/src/enhancements/node/delegate.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import deepmerge, { type ArrayMergeOptions } from 'deepmerge';
3-
import traverse from 'traverse';
43
import { DELEGATE_AUX_RELATION_PREFIX } from '../../constants';
54
import {
65
FieldInfo,
@@ -14,7 +13,7 @@ import {
1413
isDelegateModel,
1514
resolveField,
1615
} from '../../cross';
17-
import { isPlainObject, lowerCaseFirst } from '../../local-helpers';
16+
import { isPlainObject, simpleTraverse, lowerCaseFirst } from '../../local-helpers';
1817
import type { CrudContract, DbClientContract, EnhancementContext } from '../../types';
1918
import type { InternalEnhancementOptions } from './create-enhancement';
2019
import { Logger } from './logger';
@@ -487,12 +486,12 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
487486

488487
const prisma = this.prisma;
489488
const prismaModule = this.options.prismaModule;
490-
traverse(data).forEach(function () {
491-
if (this.key?.startsWith(DELEGATE_AUX_RELATION_PREFIX)) {
489+
simpleTraverse(data, ({ key }) => {
490+
if (key?.startsWith(DELEGATE_AUX_RELATION_PREFIX)) {
492491
throw prismaClientValidationError(
493492
prisma,
494493
prismaModule,
495-
`Auxiliary relation field "${this.key}" cannot be set directly`
494+
`Auxiliary relation field "${key}" cannot be set directly`
496495
);
497496
}
498497
});

packages/runtime/src/enhancements/node/policy/policy-utils.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

33
import deepmerge from 'deepmerge';
4-
import traverse from 'traverse';
54
import { z, type ZodError, type ZodObject, type ZodSchema } from 'zod';
65
import { fromZodError } from 'zod-validation-error';
76
import { CrudFailureReason, PrismaErrorCode } from '../../../constants';
@@ -15,7 +14,7 @@ import {
1514
type FieldInfo,
1615
type ModelMeta,
1716
} from '../../../cross';
18-
import { isPlainObject, lowerCaseFirst, upperCaseFirst } from '../../../local-helpers';
17+
import { isPlainObject, simpleTraverse, lowerCaseFirst, upperCaseFirst } from '../../../local-helpers';
1918
import {
2019
AuthUser,
2120
CrudContract,
@@ -691,27 +690,24 @@ export class PolicyUtil extends QueryUtils {
691690
// here we prefix the constraint variables coming from delegated checkers
692691
// with the relation field name to avoid conflicts
693692
const prefixConstraintVariables = (constraint: unknown, prefix: string) => {
694-
return traverse(constraint).map(function (value) {
693+
return simpleTraverse(constraint, ({ value, update }) => {
695694
if (isVariableConstraint(value)) {
696-
this.update(
695+
update(
697696
{
698697
...value,
699698
name: `${prefix}${value.name}`,
700-
},
701-
true
699+
}
702700
);
703701
}
704702
});
705703
};
706704

707-
// eslint-disable-next-line @typescript-eslint/no-this-alias
708-
const that = this;
709-
result = traverse(result).forEach(function (value) {
705+
result = simpleTraverse(result, ({ value, update }) => {
710706
if (isDelegateConstraint(value)) {
711707
const { model: delegateModel, relation, operation: delegateOp } = value;
712-
let newValue = that.getCheckerConstraint(delegateModel, delegateOp ?? operation);
708+
let newValue = this.getCheckerConstraint(delegateModel, delegateOp ?? operation);
713709
newValue = prefixConstraintVariables(newValue, `${relation}.`);
714-
this.update(newValue, true);
710+
update(newValue);
715711
}
716712
});
717713

packages/runtime/src/local-helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './simple-traverse';
12
export * from './sleep';
23
export * from './is-plain-object';
34
export * from './lower-case-first';
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
3+
type Cb = (
4+
data: {
5+
path: readonly string[];
6+
key: string | undefined;
7+
value: any;
8+
update: (nextValue: any) => void;
9+
}
10+
) => void;
11+
12+
export function simpleTraverse<T>(root: T, cb: Cb) {
13+
const path: string[] = [];
14+
const parents: any[] = [];
15+
16+
function walker(node: any) {
17+
const isObject = typeof node === 'object' && node !== null;
18+
const isCircular = isObject && parents.some((p) => p === node);
19+
20+
let keepGoing = true;
21+
22+
function update(nextValue: any) {
23+
if (path.length) {
24+
const parent = parents[parents.length - 1];
25+
const key = path[path.length - 1];
26+
parent[key] = nextValue;
27+
}
28+
29+
node = nextValue;
30+
31+
keepGoing = false;
32+
}
33+
34+
cb({
35+
path: [...path],
36+
key: path[path.length - 1],
37+
value: node,
38+
update,
39+
});
40+
41+
if (!keepGoing) return node;
42+
43+
if (isObject && !isCircular) {
44+
parents.push(node);
45+
46+
Object.keys(node).forEach((key) => {
47+
path.push(key);
48+
49+
walker(node[key]);
50+
51+
path.pop();
52+
});
53+
54+
parents.pop();
55+
}
56+
57+
return node;
58+
}
59+
60+
return walker(root);
61+
}

0 commit comments

Comments
 (0)