Repository navigation
Commit 5535a24
authored
fix(contract, server): match prototype chain first in
Closes #2480
## What
`ProcedureContract`, `Procedure`, `Lazy` and the error factory classes
now check the prototype chain first in `Symbol.hasInstance`, then fall
back to their structural check:
```ts
static [Symbol.hasInstance](instance: unknown): boolean {
if (Function.prototype[Symbol.hasInstance].call(this, instance)) {
return true
}
if (this !== ProcedureContract) {
return false
}
return /* structural check, unchanged */
}
```
`Procedure`'s structural check also reads `~orpc` with `?.`. An object
with `ProcedureContract.prototype` but no `~orpc` can now pass
`instanceof ProcedureContract`, so the check returns `false` for it
instead of throwing.
## Why
Babel's `@babel/plugin-transform-classes`, which React Native's Metro
adds by default, starts each compiled constructor with
`_classCallCheck(this, Class)`. That is a `this instanceof Class` check,
and it runs **before** the constructor body or `super()` gives the
instance its shape. The old checks only matched the immediate
constructor (`getConstructor(instance) === X`) or the shape, so a
subclass instance in the middle of construction was rejected:
- `ContractBuilder` → `ProcedureContract`: importing `@orpc/contract`
threw `TypeError: Cannot call a class as a function`, because `oc` is
created at module load.
- Error factory classes: even `new RateLimitedError()` threw, because
`code` isn't set until `super()` runs.
- `DecoratedProcedure` / `ImplementedProcedure` → `Procedure`, and
subclassed `Lazy`: same failure.
`ORPCError` in `@orpc/client` already walks the prototype chain and was
not affected.
## Notes for reviewers
- Behavior for constructed objects is unchanged: plain objects with the
right shape still match the base classes, and still don't match
subclasses.
- The regression tests use `Object.create(Subclass.prototype)`, which is
exactly the half-built instance Babel's check sees, so Babel isn't
needed as a dev dependency. They fail on `main` and pass with this
change. I also ran the issue's repro through the real
`@babel/plugin-transform-classes`: it throws on the old logic and passes
on the new.
- For genuine instances, the error-factory check now returns early on
the prototype match, skipping data validation.
- `getConstructor` from `@orpc/shared` no longer has callers outside its
own test. I kept it because it's a public export.
## Performance
A microbenchmark of `instanceof Procedure` (a copy of the check in plain
JS on Node, 10M iterations per case, median of 7 runs):
| Case | `main` | This PR |
| --- | --- | --- |
| Real procedure (`DecoratedProcedure`, from `os.handler()`) | 21.1 ns |
8.6 ns |
| Router object, not a procedure | 20.7 ns | 19.8 ns |
| Plain object with procedure shape | 26.2 ns | 23.6 ns |
Most procedures are subclass instances. The prototype match now returns
right away instead of falling through to the structural check. Other
inputs cost about the same as before.Symbol.hasInstance checks (#2481)1 parent bfae145 commit 5535a24
8 files changed
Lines changed: 43 additions & 23 deletions
File tree
- packages
- contract/src
- server/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
107 | 111 | | |
108 | 112 | | |
109 | 113 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
100 | 104 | | |
101 | 105 | | |
102 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
53 | 59 | | |
54 | 60 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
42 | | - | |
| 40 | + | |
| 41 | + | |
43 | 42 | | |
44 | 43 | | |
45 | | - | |
46 | | - | |
47 | | - | |
| 44 | + | |
| 45 | + | |
48 | 46 | | |
49 | 47 | | |
50 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
40 | 46 | | |
41 | 47 | | |
42 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
| 21 | + | |
| 22 | + | |
24 | 23 | | |
25 | 24 | | |
26 | | - | |
27 | | - | |
28 | | - | |
| 25 | + | |
| 26 | + | |
29 | 27 | | |
30 | 28 | | |
31 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
72 | 79 | | |
73 | 80 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
127 | 126 | | |
128 | 127 | | |
129 | 128 | | |
130 | | - | |
131 | | - | |
132 | | - | |
| 129 | + | |
| 130 | + | |
133 | 131 | | |
134 | 132 | | |
135 | | - | |
136 | | - | |
137 | | - | |
| 133 | + | |
| 134 | + | |
138 | 135 | | |
139 | 136 | | |
140 | 137 | | |
141 | 138 | | |
142 | | - | |
143 | | - | |
| 139 | + | |
| 140 | + | |
144 | 141 | | |
145 | 142 | | |
146 | 143 | | |
| |||
0 commit comments