Skip to content

Commit 5535a24

Browse files
authored
fix(contract, server): match prototype chain first in Symbol.hasInstance checks (#2481)
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.
1 parent bfae145 commit 5535a24

8 files changed

Lines changed: 43 additions & 23 deletions

File tree

‎packages/contract/src/error-factory.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ describe('error factory', () => {
104104
expect(new SimpleError()).toBeInstanceOf(SimpleError)
105105
})
106106

107+
it('matches by prototype chain before the constructor sets code and data (Babel class transform)', () => {
108+
expect(Object.create(TestError.prototype)).toBeInstanceOf(TestError)
109+
})
110+
107111
it('matches plain ORPCError instances with the same code and valid data', () => {
108112
expect(new ORPCError('TEST', { data: { value: 1 } })).toBeInstanceOf(TestError)
109113
})

‎packages/contract/src/error-factory.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export function error<TCode extends ORPCErrorCode, TData = unknown>(
9797
}
9898

9999
static override [Symbol.hasInstance](instance: unknown): boolean {
100+
if (Function.prototype[Symbol.hasInstance].call(this, instance)) {
101+
return true
102+
}
103+
100104
if (!(instance instanceof ORPCError)) {
101105
return false
102106
}

‎packages/contract/src/procedure.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,11 @@ describe('procedureContract', () => {
5050
ExtendedProcedureContract,
5151
)
5252
})
53+
54+
it('match by prototype chain before the constructor body runs (Babel class transform)', () => {
55+
class ExtendedProcedureContract extends ProcedureContract<any, any, any> {}
56+
57+
expect(Object.create(ExtendedProcedureContract.prototype)).toBeInstanceOf(ProcedureContract)
58+
})
5359
})
5460
})

‎packages/contract/src/procedure.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getConstructor, isNonPrimitive } from '@orpc/shared'
1+
import { isNonPrimitive } from '@orpc/shared'
22

33
import type { ErrorMap } from './error'
44
import type { AnyMetaPlugin, Meta } from './meta'
@@ -37,14 +37,12 @@ export class ProcedureContract<
3737
* Checks if the given instance satisfies the {@link ProcedureContract} class/interface.
3838
*/
3939
static [Symbol.hasInstance](instance: unknown): boolean {
40-
if (this !== ProcedureContract) {
41-
// fallback to default instanceof check if this is extended class
42-
return Function.prototype[Symbol.hasInstance].call(this, instance)
40+
if (Function.prototype[Symbol.hasInstance].call(this, instance)) {
41+
return true
4342
}
4443

45-
const constructor = getConstructor(instance)
46-
if (constructor === ProcedureContract) {
47-
return true
44+
if (this !== ProcedureContract) {
45+
return false
4846
}
4947

5048
return (

‎packages/server/src/lazy.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ describe('lazy', () => {
3737
expect({ '~orpc': extendedLazy['~orpc'] }).toBeInstanceOf(Lazy)
3838
expect({ '~orpc': extendedLazy['~orpc'] }).not.toBeInstanceOf(ExtendedLazy)
3939
})
40+
41+
it('match by prototype chain before the constructor body runs (Babel class transform)', () => {
42+
class ExtendedLazy extends Lazy<any> {}
43+
44+
expect(Object.create(ExtendedLazy.prototype)).toBeInstanceOf(Lazy)
45+
})
4046
})
4147
})
4248

‎packages/server/src/lazy.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AnyMetaPlugin, Meta } from '@orpc/contract'
2-
import { getConstructor, isNonPrimitive } from '@orpc/shared'
2+
import { isNonPrimitive } from '@orpc/shared'
33

44
export interface LazyDefinition<T> {
55
meta: Meta
@@ -18,14 +18,12 @@ export class Lazy<T> {
1818
* Checks if the given instance satisfies the {@link Lazy} class/interface.
1919
*/
2020
static [Symbol.hasInstance](instance: unknown): boolean {
21-
if (this !== Lazy) {
22-
// fallback to default instanceof check if this is extended class
23-
return Function.prototype[Symbol.hasInstance].call(this, instance)
21+
if (Function.prototype[Symbol.hasInstance].call(this, instance)) {
22+
return true
2423
}
2524

26-
const constructor = getConstructor(instance)
27-
if (constructor === Lazy) {
28-
return true
25+
if (this !== Lazy) {
26+
return false
2927
}
3028

3129
return (

‎packages/server/src/procedure.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,12 @@ describe('procedure', () => {
6969
expect({ '~orpc': new ExtendedProcedure()['~orpc'] }).toBeInstanceOf(Procedure)
7070
expect({ '~orpc': new ExtendedProcedure()['~orpc'] }).not.toBeInstanceOf(ExtendedProcedure)
7171
})
72+
73+
it('match by prototype chain before the constructor body runs (Babel class transform)', () => {
74+
class ExtendedProcedure extends Procedure<any, any, any, any, any> {}
75+
76+
expect(Object.create(ExtendedProcedure.prototype)).toBeInstanceOf(Procedure)
77+
expect(Object.create(ProcedureContract.prototype)).not.toBeInstanceOf(Procedure)
78+
})
7279
})
7380
})

‎packages/server/src/procedure.ts‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type {
66
} from '@orpc/contract'
77
import { ProcedureContract } from '@orpc/contract'
88
import type { Promisable } from '@orpc/shared'
9-
import { getConstructor } from '@orpc/shared'
109

1110
import type { Context } from './context'
1211
import type { AnyMiddleware } from './middleware'
@@ -127,20 +126,18 @@ export class Procedure<
127126
* Checks if the given instance satisfies the {@link Procedure} class/interface.
128127
*/
129128
static [Symbol.hasInstance](instance: unknown): boolean {
130-
if (this !== Procedure) {
131-
// fallback to default instanceof check if this is extended class
132-
return Function.prototype[Symbol.hasInstance].call(this, instance)
129+
if (Function.prototype[Symbol.hasInstance].call(this, instance)) {
130+
return true
133131
}
134132

135-
const constructor = getConstructor(instance)
136-
if (constructor === Procedure) {
137-
return true
133+
if (this !== Procedure) {
134+
return false
138135
}
139136

140137
return (
141138
instance instanceof ProcedureContract &&
142-
Array.isArray((instance['~orpc'] as any).orderedMiddlewares) &&
143-
typeof (instance['~orpc'] as any).handler === 'function'
139+
Array.isArray((instance['~orpc'] as any)?.orderedMiddlewares) &&
140+
typeof (instance['~orpc'] as any)?.handler === 'function'
144141
)
145142
}
146143
}

0 commit comments

Comments
 (0)