Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Typescript conversion of send-metadata.js #23738

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from

Conversation

NiranjanaBinoy
Copy link
Contributor

@NiranjanaBinoy NiranjanaBinoy commented Mar 26, 2024

Part of #23014
Fixes #23472

Converting the level 6 dependency file app/scripts/lib/rpc-method-middleware/handlers/send-metadata.js to typescript for contributing to metamask-controller.js.

Description

Open in GitHub Codespaces

Related issues

Fixes:

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Collaborator

Builds ready [47ae366]
Page Load Metrics (836 ± 470 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint863601627536
domContentLoaded17114422813
load682344836979470
domInteractive17114412813
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@NiranjanaBinoy NiranjanaBinoy changed the base branch from develop to extract-wrapper-type March 27, 2024 16:14
Copy link
Contributor

github-actions bot commented Jun 8, 2024

This PR has been automatically marked as stale because it has not had recent activity in the last 60 days. It will be closed in 14 days. Thank you for your contributions.

@github-actions github-actions bot added the stale issues and PRs marked as stale label Jun 8, 2024
Copy link
Contributor

This PR was closed because there has been no follow up activity in the last 14 days. Thank you for your contributions.

@github-actions github-actions bot closed this Jun 22, 2024
@github-actions github-actions bot removed the stale issues and PRs marked as stale label Jun 28, 2024
Copy link

socket-security bot commented Jul 19, 2024

No dependency changes detected. Learn more about Socket for GitHub ↗︎

👍 No dependency changes detected in pull request

@NiranjanaBinoy NiranjanaBinoy changed the base branch from extract-wrapper-type to develop July 19, 2024 20:17
@metamaskbot
Copy link
Collaborator

Builds ready [5ed20fc]
Page Load Metrics (572 ± 417 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint801861152311
domContentLoaded106331136
load522656572868417
domInteractive106331136
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@NiranjanaBinoy NiranjanaBinoy marked this pull request as ready for review July 19, 2024 20:58
@NiranjanaBinoy NiranjanaBinoy requested a review from a team as a code owner July 19, 2024 20:58
Copy link
Contributor

@MajorLift MajorLift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the implicit any typing of the Infer type (and downstream types InferWithParameters, JsonRpcRequest etc.) has not been resolved.

Perhaps we could look into upgrading @metamask/utils in extension to 8.5.0+, and migrating to @metamask/superstruct? It looks like a number of different versions for utils and superstruct are being used in extension.

Leaving a blocking review for now. I don't believe we should be shipping implicit anys into the codebase under any circumstance. Restoring the JavaScript file would be preferable as it would at least correctly set expectations.

@MajorLift MajorLift dismissed their stale review September 4, 2024 12:39

The implicit any seems to be an issue with local dev environment. So long as tsc is passing in CI, we should be good to proceed (See: #23504 (comment))

Copy link
Contributor

@MajorLift MajorLift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implicit any typing needs to be resolved (see https://github.com/MetaMask/metamask-extension/pull/23738/files#r1743724867).

@MajorLift
Copy link
Contributor

MajorLift commented Sep 4, 2024

I was able to fix the implicit any issue by cherry-picking #26551.

All resulting type errors are resolved with these changes:

diff --git a/app/scripts/lib/rpc-method-middleware/handlers/send-metadata.ts b/app/scripts/lib/rpc-method-middleware/handlers/send-metadata.ts
index a6b4cec847..5bec59b57a 100644
--- a/app/scripts/lib/rpc-method-middleware/handlers/send-metadata.ts
+++ b/app/scripts/lib/rpc-method-middleware/handlers/send-metadata.ts
@@ -3,12 +3,8 @@ import {
   JsonRpcEngineEndCallback,
   JsonRpcEngineNextCallback,
 } from 'json-rpc-engine';
-import {
-  Json,
-  JsonRpcParams,
-  JsonRpcRequest,
-  PendingJsonRpcResponse,
-} from '@metamask/utils';
+import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
+import { isObject } from '@metamask/utils';
 import {
   PermissionSubjectMetadata,
   SubjectType,
@@ -21,7 +17,7 @@ type SubjectMetadataToAdd = PermissionSubjectMetadata & {
   subjectType?: SubjectType | null;
   extensionId?: string | null;
   iconUrl?: string | null;
-} & Record<string, Json>;
+};
 
 type AddSubjectMetadata = (metadata: SubjectMetadataToAdd) => void;
 
@@ -30,9 +26,11 @@ type SendMetadataOptions = {
   subjectType: SubjectType;
 };
 
-type SendMetadataConstraint<Params extends JsonRpcParams = JsonRpcParams> = {
+type SendMetadataConstraint<
+  Params extends SubjectMetadataToAdd = SubjectMetadataToAdd,
+> = {
   implementation: (
-    req: JsonRpcRequest<Params>,
+    req: JsonRpcRequest<Params> & { origin: string },
     res: PendingJsonRpcResponse<true>,
     _next: JsonRpcEngineNextCallback,
     end: JsonRpcEngineEndCallback,
@@ -64,15 +62,17 @@ export default sendMetadata;
  * metadata, bound to the requesting origin.
  * @param options.subjectType - The type of the requesting origin / subject.
  */
-function sendMetadataHandler<Params extends JsonRpcParams = JsonRpcParams>(
-  req: JsonRpcRequest<Params>,
+function sendMetadataHandler<
+  Params extends SubjectMetadataToAdd = SubjectMetadataToAdd,
+>(
+  req: JsonRpcRequest<Params> & { origin: string },
   res: PendingJsonRpcResponse<true>,
   _next: JsonRpcEngineNextCallback,
   end: JsonRpcEngineEndCallback,
   { addSubjectMetadata, subjectType }: SendMetadataOptions,
 ): void {
   const { origin, params } = req;
-  if (params && typeof params === 'object' && !Array.isArray(params)) {
+  if (isObject(params)) {
     const { icon = null, name = null, ...remainingParams } = params;
 
     addSubjectMetadata({

These are valid errors we should be seeing, but they're being suppressed by the implicit any types. This issue probably affects all of the TypeScript conversion PRs.

MajorLift added a commit that referenced this pull request Sep 4, 2024
## **Description**

Major version bump for `@metamask/utils`. 

- Fixes critical issue of all `@metamask/utils` and `superstruct` types
downstream of `Infer` being typed as implicit `any`, suppressing valid
type errors.
- See
#23738 (comment)
- Causes large number of type error regressions which are resolved here:
#26143

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26551?quickstart=1)

## **Related issues**

- [x] Blocked by
#26143

## **Manual testing steps**

## **Screenshots/Recordings**

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: MetaMask Bot <metamaskbot@users.noreply.github.com>
@metamaskbot
Copy link
Collaborator

Builds ready [68cb2b4]
Page Load Metrics (1860 ± 111 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint156623451854229110
domContentLoaded155323271823229110
load156623511860232111
domInteractive2493452311
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 88 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Copy link

sonarcloud bot commented Oct 11, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
75.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarCloud

@metamaskbot
Copy link
Collaborator

Builds ready [d6f2439]
Page Load Metrics (1876 ± 127 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint155325321858254122
domContentLoaded154324671822231111
load155126301876265127
domInteractive26151532914
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 88 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert send-metadata.js to Typescript
3 participants