Skip to content

Commit 03ef46d

Browse files
committed
Make a good deal of @solana/rpc-transformers private
1 parent 64d702a commit 03ef46d

15 files changed

+129
-118
lines changed

.changeset/cyan-rules-swim.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@solana/rpc-transformers': patch
3+
---
4+
5+
Removed `OPTIONS_OBJECT_POSITION_BY_METHOD`, `downcastNodeToNumberIfBigint()`, `applyDefaultCommitment()`, `getIntegerOverflowNodeVisitor()`, `getBigIntUpcastVisitor()`, and `getTreeWalker()` from the exports of `@solana/rpc-transformer`

packages/rpc-transformers/src/__tests__/request-transformer-bigint-downcast-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { downcastNodeToNumberIfBigint } from '../request-transformer-bigint-downcast';
1+
import { downcastNodeToNumberIfBigint } from '../request-transformer-bigint-downcast-internal';
22

33
describe('bigint downcast visitor', () => {
44
it.each([10, '10', null, undefined, Symbol()])('returns the value `%p` as-is', value => {

packages/rpc-transformers/src/__tests__/request-transformer-default-commitment-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Commitment } from '@solana/rpc-types';
22

3-
import { applyDefaultCommitment } from '../request-transformer-default-commitment';
3+
import { applyDefaultCommitment } from '../request-transformer-default-commitment-internal';
44

55
const MOCK_COMMITMENT_PROPERTY_NAME = 'commitmentProperty';
66

packages/rpc-transformers/src/__tests__/request-transformer-integer-overflow-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getIntegerOverflowNodeVisitor } from '../request-transformer-integer-overflow';
1+
import { getIntegerOverflowNodeVisitor } from '../request-transformer-integer-overflow-internal';
22
import { TraversalState } from '../tree-traversal';
33

44
const MOCK_TRAVERSAL_STATE = {

packages/rpc-transformers/src/__tests__/response-transformer-bigint-upcast-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getBigIntUpcastVisitor } from '../response-transformer-bigint-upcast';
1+
import { getBigIntUpcastVisitor } from '../response-transformer-bigint-upcast-internal';
22
import { TraversalState } from '../tree-traversal';
33

44
const MOCK_TRAVERSAL_STATE = {

packages/rpc-transformers/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export * from './request-transformer';
22
export * from './request-transformer-bigint-downcast';
33
export * from './request-transformer-default-commitment';
44
export * from './request-transformer-integer-overflow';
5-
export * from './request-transformer-options-object-position-config';
65
export * from './response-transformer';
76
export * from './response-transformer-allowed-numeric-values';
87
export * from './response-transformer-bigint-upcast';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function downcastNodeToNumberIfBigint(value: bigint): number;
2+
export function downcastNodeToNumberIfBigint<T>(value: T): T;
3+
export function downcastNodeToNumberIfBigint(value: unknown): unknown {
4+
return typeof value === 'bigint'
5+
? // FIXME(solana-labs/solana/issues/30341) Create a data type to represent u64 in the Solana
6+
// JSON RPC implementation so that we can throw away this entire patcher instead of unsafely
7+
// downcasting `bigints` to `numbers`.
8+
Number(value)
9+
: value;
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1+
import { downcastNodeToNumberIfBigint } from './request-transformer-bigint-downcast-internal';
12
import { getTreeWalkerRequestTransformer } from './tree-traversal';
23

34
export function getBigIntDowncastRequestTransformer() {
45
return getTreeWalkerRequestTransformer([downcastNodeToNumberIfBigint], { keyPath: [] });
56
}
6-
7-
export function downcastNodeToNumberIfBigint(value: bigint): number;
8-
export function downcastNodeToNumberIfBigint<T>(value: T): T;
9-
export function downcastNodeToNumberIfBigint(value: unknown): unknown {
10-
return typeof value === 'bigint'
11-
? // FIXME(solana-labs/solana/issues/30341) Create a data type to represent u64 in the Solana
12-
// JSON RPC implementation so that we can throw away this entire patcher instead of unsafely
13-
// downcasting `bigints` to `numbers`.
14-
Number(value)
15-
: value;
16-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Commitment } from '@solana/rpc-types';
2+
3+
export function applyDefaultCommitment({
4+
commitmentPropertyName,
5+
params,
6+
optionsObjectPositionInParams,
7+
overrideCommitment,
8+
}: Readonly<{
9+
commitmentPropertyName: string;
10+
optionsObjectPositionInParams: number;
11+
overrideCommitment?: Commitment;
12+
params: unknown[];
13+
}>) {
14+
const paramInTargetPosition = params[optionsObjectPositionInParams];
15+
if (
16+
// There's no config.
17+
paramInTargetPosition === undefined ||
18+
// There is a config object.
19+
(paramInTargetPosition && typeof paramInTargetPosition === 'object' && !Array.isArray(paramInTargetPosition))
20+
) {
21+
if (
22+
// The config object already has a commitment set.
23+
paramInTargetPosition &&
24+
commitmentPropertyName in paramInTargetPosition
25+
) {
26+
if (
27+
!paramInTargetPosition[commitmentPropertyName as keyof typeof paramInTargetPosition] ||
28+
paramInTargetPosition[commitmentPropertyName as keyof typeof paramInTargetPosition] === 'finalized'
29+
) {
30+
// Delete the commitment property; `finalized` is already the server default.
31+
const nextParams = [...params];
32+
const {
33+
[commitmentPropertyName as keyof typeof paramInTargetPosition]: _, // eslint-disable-line @typescript-eslint/no-unused-vars
34+
...rest
35+
} = paramInTargetPosition;
36+
if (Object.keys(rest).length > 0) {
37+
nextParams[optionsObjectPositionInParams] = rest;
38+
} else {
39+
if (optionsObjectPositionInParams === nextParams.length - 1) {
40+
nextParams.length--;
41+
} else {
42+
nextParams[optionsObjectPositionInParams] = undefined;
43+
}
44+
}
45+
return nextParams;
46+
}
47+
} else if (overrideCommitment !== 'finalized') {
48+
// Apply the default commitment.
49+
const nextParams = [...params];
50+
nextParams[optionsObjectPositionInParams] = {
51+
...paramInTargetPosition,
52+
[commitmentPropertyName]: overrideCommitment,
53+
};
54+
return nextParams;
55+
}
56+
}
57+
return params;
58+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { RpcRequest, RpcRequestTransformer } from '@solana/rpc-spec-types';
22
import type { Commitment } from '@solana/rpc-types';
33

4+
import { applyDefaultCommitment } from './request-transformer-default-commitment-internal';
5+
46
export function getDefaultCommitmentRequestTransformer({
57
defaultCommitment,
68
optionsObjectPositionByMethod,
@@ -33,60 +35,3 @@ export function getDefaultCommitmentRequestTransformer({
3335
});
3436
};
3537
}
36-
37-
export function applyDefaultCommitment({
38-
commitmentPropertyName,
39-
params,
40-
optionsObjectPositionInParams,
41-
overrideCommitment,
42-
}: Readonly<{
43-
commitmentPropertyName: string;
44-
optionsObjectPositionInParams: number;
45-
overrideCommitment?: Commitment;
46-
params: unknown[];
47-
}>) {
48-
const paramInTargetPosition = params[optionsObjectPositionInParams];
49-
if (
50-
// There's no config.
51-
paramInTargetPosition === undefined ||
52-
// There is a config object.
53-
(paramInTargetPosition && typeof paramInTargetPosition === 'object' && !Array.isArray(paramInTargetPosition))
54-
) {
55-
if (
56-
// The config object already has a commitment set.
57-
paramInTargetPosition &&
58-
commitmentPropertyName in paramInTargetPosition
59-
) {
60-
if (
61-
!paramInTargetPosition[commitmentPropertyName as keyof typeof paramInTargetPosition] ||
62-
paramInTargetPosition[commitmentPropertyName as keyof typeof paramInTargetPosition] === 'finalized'
63-
) {
64-
// Delete the commitment property; `finalized` is already the server default.
65-
const nextParams = [...params];
66-
const {
67-
[commitmentPropertyName as keyof typeof paramInTargetPosition]: _, // eslint-disable-line @typescript-eslint/no-unused-vars
68-
...rest
69-
} = paramInTargetPosition;
70-
if (Object.keys(rest).length > 0) {
71-
nextParams[optionsObjectPositionInParams] = rest;
72-
} else {
73-
if (optionsObjectPositionInParams === nextParams.length - 1) {
74-
nextParams.length--;
75-
} else {
76-
nextParams[optionsObjectPositionInParams] = undefined;
77-
}
78-
}
79-
return nextParams;
80-
}
81-
} else if (overrideCommitment !== 'finalized') {
82-
// Apply the default commitment.
83-
const nextParams = [...params];
84-
nextParams[optionsObjectPositionInParams] = {
85-
...paramInTargetPosition,
86-
[commitmentPropertyName]: overrideCommitment,
87-
};
88-
return nextParams;
89-
}
90-
}
91-
return params;
92-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { KeyPath, TraversalState } from './tree-traversal';
2+
3+
export function getIntegerOverflowNodeVisitor(onIntegerOverflow: (keyPath: KeyPath, value: bigint) => void) {
4+
return <T>(value: T, { keyPath }: TraversalState): T => {
5+
if (typeof value === 'bigint') {
6+
if (onIntegerOverflow && (value > Number.MAX_SAFE_INTEGER || value < -Number.MAX_SAFE_INTEGER)) {
7+
onIntegerOverflow(keyPath as (number | string)[], value);
8+
}
9+
}
10+
return value;
11+
};
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { RpcRequest } from '@solana/rpc-spec-types';
22

3-
import { getTreeWalkerRequestTransformer, KeyPath, TraversalState } from './tree-traversal';
3+
import { getIntegerOverflowNodeVisitor } from './request-transformer-integer-overflow-internal';
4+
import { getTreeWalkerRequestTransformer, KeyPath } from './tree-traversal';
45

56
export type IntegerOverflowHandler = (request: RpcRequest, keyPath: KeyPath, value: bigint) => void;
67

@@ -13,14 +14,3 @@ export function getIntegerOverflowRequestTransformer(onIntegerOverflow: IntegerO
1314
return transformer(request);
1415
};
1516
}
16-
17-
export function getIntegerOverflowNodeVisitor(onIntegerOverflow: (keyPath: KeyPath, value: bigint) => void) {
18-
return <T>(value: T, { keyPath }: TraversalState): T => {
19-
if (typeof value === 'bigint') {
20-
if (onIntegerOverflow && (value > Number.MAX_SAFE_INTEGER || value < -Number.MAX_SAFE_INTEGER)) {
21-
onIntegerOverflow(keyPath as (number | string)[], value);
22-
}
23-
}
24-
return value;
25-
};
26-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { KeyPath, KEYPATH_WILDCARD, TraversalState } from './tree-traversal';
2+
3+
export function getBigIntUpcastVisitor(allowedNumericKeyPaths: readonly KeyPath[]) {
4+
return function upcastNodeToBigIntIfNumber(value: unknown, { keyPath }: TraversalState) {
5+
const isInteger = (typeof value === 'number' && Number.isInteger(value)) || typeof value === 'bigint';
6+
if (!isInteger) return value;
7+
if (keyPathIsAllowedToBeNumeric(keyPath, allowedNumericKeyPaths)) {
8+
return Number(value);
9+
} else {
10+
return BigInt(value);
11+
}
12+
};
13+
}
14+
15+
function keyPathIsAllowedToBeNumeric(keyPath: KeyPath, allowedNumericKeyPaths: readonly KeyPath[]) {
16+
return allowedNumericKeyPaths.some(prohibitedKeyPath => {
17+
if (prohibitedKeyPath.length !== keyPath.length) {
18+
return false;
19+
}
20+
for (let ii = keyPath.length - 1; ii >= 0; ii--) {
21+
const keyPathPart = keyPath[ii];
22+
const prohibitedKeyPathPart = prohibitedKeyPath[ii];
23+
if (
24+
prohibitedKeyPathPart !== keyPathPart &&
25+
(prohibitedKeyPathPart !== KEYPATH_WILDCARD || typeof keyPathPart !== 'number')
26+
) {
27+
return false;
28+
}
29+
}
30+
return true;
31+
});
32+
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,6 @@
1-
import { getTreeWalkerResponseTransformer, KeyPath, KEYPATH_WILDCARD, TraversalState } from './tree-traversal';
1+
import { getBigIntUpcastVisitor } from './response-transformer-bigint-upcast-internal';
2+
import { getTreeWalkerResponseTransformer, KeyPath } from './tree-traversal';
23

34
export function getBigIntUpcastResponseTransformer(allowedNumericKeyPaths: readonly KeyPath[]) {
45
return getTreeWalkerResponseTransformer([getBigIntUpcastVisitor(allowedNumericKeyPaths)], { keyPath: [] });
56
}
6-
7-
export function getBigIntUpcastVisitor(allowedNumericKeyPaths: readonly KeyPath[]) {
8-
return function upcastNodeToBigIntIfNumber(value: unknown, { keyPath }: TraversalState) {
9-
const isInteger = (typeof value === 'number' && Number.isInteger(value)) || typeof value === 'bigint';
10-
if (!isInteger) return value;
11-
if (keyPathIsAllowedToBeNumeric(keyPath, allowedNumericKeyPaths)) {
12-
return Number(value);
13-
} else {
14-
return BigInt(value);
15-
}
16-
};
17-
}
18-
19-
function keyPathIsAllowedToBeNumeric(keyPath: KeyPath, allowedNumericKeyPaths: readonly KeyPath[]) {
20-
return allowedNumericKeyPaths.some(prohibitedKeyPath => {
21-
if (prohibitedKeyPath.length !== keyPath.length) {
22-
return false;
23-
}
24-
for (let ii = keyPath.length - 1; ii >= 0; ii--) {
25-
const keyPathPart = keyPath[ii];
26-
const prohibitedKeyPathPart = prohibitedKeyPath[ii];
27-
if (
28-
prohibitedKeyPathPart !== keyPathPart &&
29-
(prohibitedKeyPathPart !== KEYPATH_WILDCARD || typeof keyPathPart !== 'number')
30-
) {
31-
return false;
32-
}
33-
}
34-
return true;
35-
});
36-
}

packages/rpc-transformers/src/tree-traversal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type TraversalState = Readonly<{
1010
keyPath: KeyPath;
1111
}>;
1212

13-
export function getTreeWalker(visitors: NodeVisitor[]) {
13+
function getTreeWalker(visitors: NodeVisitor[]) {
1414
return function traverse<TState extends TraversalState>(node: unknown, state: TState): unknown {
1515
if (Array.isArray(node)) {
1616
return node.map((element, ii) => {

0 commit comments

Comments
 (0)