Skip to content

Convert everything to TypeScript#8

Draft
mcmire wants to merge 1 commit intomainfrom
convert-to-typescript
Draft

Convert everything to TypeScript#8
mcmire wants to merge 1 commit intomainfrom
convert-to-typescript

Conversation

@mcmire
Copy link

@mcmire mcmire commented Jul 10, 2023

  • Convert the existing JavaScript code to TypeScript, removing the
    need for a type definitions file
  • Add typedoc to generate JSDocs
  • BREAKING: Change default export to a named export to align with other
    libraries
  • Export Provider type, as controller-utils and other packages
    within core import it

Progresses MetaMask/core#1471

@socket-security
Copy link

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
ts-node 10.9.1 filesystem, shell, environment +15 70.2 MB cspotcode
typescript 4.8.4 filesystem +0 68.8 MB typescript-bot
@metamask/eslint-config-typescript 11.1.0 None +1 68.8 MB metamaskbot
typedoc 0.23.28 network, filesystem, shell, environment +9 82.3 MB typedoc-bot

🚮 Removed packages: xtend@4.0.2

@socket-security
Copy link

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Issue Package Version Note Source
Filesystem access @cspotcode/source-map-support 0.8.1
No contributors or author data @cspotcode/source-map-support 0.8.1
Unmaintained @cspotcode/source-map-support 0.8.1
  • Last Publish: 5/2/2022, 1:24:38 PM
Filesystem access create-require 1.1.1
Unmaintained create-require 1.1.1
  • Last Publish: 11/26/2020, 1:41:32 PM
Filesystem access globby 11.1.0
Filesystem access lunr 2.3.9
Unmaintained lunr 2.3.9
  • Last Publish: 8/19/2020, 8:30:07 PM
Filesystem access marked 4.3.0
Filesystem access shiki 0.14.3
Network access shiki 0.14.3
Filesystem access ts-node 10.9.1
Shell access ts-node 10.9.1
Filesystem access typedoc 0.23.28
No contributors or author data typedoc 0.23.28
Shell access typedoc 0.23.28
Filesystem access typescript 4.8.4
Filesystem access v8-compile-cache-lib 3.0.1
Unmaintained v8-compile-cache-lib 3.0.1
  • Last Publish: 4/16/2022, 1:03:39 AM
Network access vscode-oniguruma 1.7.0
New author vscode-oniguruma 1.7.0
No contributors or author data @metamask/eslint-config-typescript 11.1.0
No contributors or author data @tsconfig/node10 1.0.9
Unmaintained @tsconfig/node10 1.0.9
  • Last Publish: 6/11/2022, 4:15:35 AM
No contributors or author data @tsconfig/node12 1.0.11
No contributors or author data @tsconfig/node14 1.0.3
No contributors or author data @tsconfig/node16 1.0.4
No contributors or author data acorn 8.10.0
No contributors or author data acorn-walk 8.2.0
Unmaintained acorn-walk 8.2.0
  • Last Publish: 9/6/2021, 8:21:10 AM
No contributors or author data merge2 1.4.1
Unmaintained merge2 1.4.1
  • Last Publish: 6/3/2020, 7:39:10 AM
Unmaintained dir-glob 3.0.1
  • Last Publish: 6/29/2019, 4:22:06 PM
Unmaintained yn 3.1.1
  • Last Publish: 8/14/2021, 8:48:04 PM
Unmaintained make-error 1.3.6
  • Last Publish: 2/19/2020, 9:34:36 AM
Unmaintained arg 4.1.3
  • Last Publish: 6/5/2022, 6:20:09 PM

Next steps

What is filesystem access?

Accesses the file system, and could potentially read sensitive data.

If a package must read the file system, clarify what it will read and ensure it reads only what it claims to. If appropriate, packages can leave file system access to consumers and operate on data passed to it instead.

Why is contributor and author data important?

Package does not specify a list of contributors or an author in package.json.

Add a author field or contributors array to package.json.

What are unmaintained packages?

Package has not been updated in more than a year and may be unmaintained. Problems with the package may go unaddressed.

Package should publish periodic maintenance releases if they are maintained, or deprecate if they have no intention in further maintenance.

What is network access?

This module accesses the network.

Packages should remove all network access that is functionally unnecessary. Consumers should audit network access to ensure legitimate use.

What is shell access?

This module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.

Packages should avoid accessing the shell which can reduce portability, and make it easier for malicious shell access to be introduced.

What is new author?

A new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.

Scrutinize new collaborator additions to packages because they now have the ability to publish code into your dependency tree. Packages should avoid frequent or unnecessary additions or changes to publishing rights.

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of package-name@version specifiers. e.g. @SocketSecurity ignore foo@1.0.0 bar@* or ignore all packages with @SocketSecurity ignore-all

  • @SocketSecurity ignore vscode-oniguruma@1.7.0
  • @SocketSecurity ignore ts-node@10.9.1
  • @SocketSecurity ignore typedoc@0.23.28
  • @SocketSecurity ignore @cspotcode/source-map-support@0.8.1
  • @SocketSecurity ignore @metamask/eslint-config-typescript@11.1.0
  • @SocketSecurity ignore @tsconfig/node10@1.0.9
  • @SocketSecurity ignore @tsconfig/node12@1.0.11
  • @SocketSecurity ignore @tsconfig/node14@1.0.3
  • @SocketSecurity ignore @tsconfig/node16@1.0.4
  • @SocketSecurity ignore acorn@8.10.0
  • @SocketSecurity ignore acorn-walk@8.2.0
  • @SocketSecurity ignore merge2@1.4.1
  • @SocketSecurity ignore dir-glob@3.0.1
  • @SocketSecurity ignore yn@3.1.1
  • @SocketSecurity ignore make-error@1.3.6
  • @SocketSecurity ignore arg@4.1.3
  • @SocketSecurity ignore v8-compile-cache-lib@3.0.1
  • @SocketSecurity ignore create-require@1.1.1
  • @SocketSecurity ignore lunr@2.3.9
  • @SocketSecurity ignore shiki@0.14.3
  • @SocketSecurity ignore globby@11.1.0
  • @SocketSecurity ignore marked@4.3.0
  • @SocketSecurity ignore typescript@4.8.4

@mcmire mcmire force-pushed the convert-to-typescript branch from f756c34 to 4b32637 Compare July 11, 2023 17:01
@mcmire mcmire changed the base branch from TEMP-combined to main July 11, 2023 17:01
- Convert the existing JavaScript code to TypeScript, removing the
  need for a type definitions file
- Add typedoc to generate JSDocs
- BREAKING: Change default export to a named export to align with other
  libraries
- Export `Provider` type, as `controller-utils` and other packages
  within `core` import it
@mcmire mcmire force-pushed the convert-to-typescript branch from 4b32637 to 76a922a Compare July 11, 2023 17:03
@@ -0,0 +1,8 @@
declare module 'json-rpc-random-id' {
Copy link
Author

Choose a reason for hiding this comment

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

@@ -0,0 +1,656 @@
import createRandomIdFactory from 'json-rpc-random-id';
Copy link
Author

@mcmire mcmire Jul 11, 2023

Choose a reason for hiding this comment

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

Since we are backfilling the types for this library here, I tested building the project to make sure that this import line doesn't show up to prevent the same sort of error that we are having with eth-query, and while it does appear in the compiled JS, it does not appear in the .d.ts. So I think we're okay on this.

if (response.result) {
return callback(null, response.result);
}
throw new Error(
Copy link
Author

@mcmire mcmire Jul 11, 2023

Choose a reason for hiding this comment

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

This error does not get thrown in the original implementation, but I added it because of the type that we are assigning to the response:

type ProviderSendAsyncResponse<Result> = {
  error?: { message: string };
  result?: Result;
};

In other words, both error and result can be undefined and so TypeScript indicates that not all code paths return a value.

Of course we control the type here. That said, I derived it from the implementation. It would be a little more accurate to use:

type ProviderSendAsyncResponse<Result> =
  | {
      error: { message: string }
    }
  | {
      result: Result
    };

Or even:

type ProviderSendAsyncResponse<Result> =
  | {
      error: unknown
    }
  | {
      result: Result
    };

but then we'd have to change the implementation.

) {
const callback = args.pop();
// Typecast: The remaining arguments must be the params.
const params = args.slice(0, -1) as Params;
Copy link
Author

Choose a reason for hiding this comment

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

This is unfortunate, but I wasn't sure of another way to do this.

const callback = args.pop();
// Typecast: The remaining arguments must be the params.
const params = args.slice(0, -1) as Params;
if (callback === undefined) {
Copy link
Author

Choose a reason for hiding this comment

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

These checks are new. It makes sense that callback could be undefined because args may be empty. However, even though we've assigned a type of [...Params, SendAsyncCallback<Params>], TypeScript isn't willing to assume that callback is SendAsyncCallback<Params>, but gives it a type of SendAsyncCallback<Params> | Params[number].

@mcmire mcmire marked this pull request as ready for review July 11, 2023 17:18
@mcmire
Copy link
Author

mcmire commented Jul 11, 2023

All of the Socket warnings make sense:

@SocketSecurity ignore vscode-oniguruma@1.7.0
@SocketSecurity ignore ts-node@10.9.1
@SocketSecurity ignore typedoc@0.23.28
@SocketSecurity ignore @cspotcode/source-map-support@0.8.1
@SocketSecurity ignore @metamask/eslint-config-typescript@11.1.0
@SocketSecurity ignore @tsconfig/node10@1.0.9
@SocketSecurity ignore @tsconfig/node12@1.0.11
@SocketSecurity ignore @tsconfig/node14@1.0.3
@SocketSecurity ignore @tsconfig/node16@1.0.4
@SocketSecurity ignore acorn@8.10.0
@SocketSecurity ignore acorn-walk@8.2.0
@SocketSecurity ignore merge2@1.4.1
@SocketSecurity ignore dir-glob@3.0.1
@SocketSecurity ignore yn@3.1.1
@SocketSecurity ignore make-error@1.3.6
@SocketSecurity ignore arg@4.1.3
@SocketSecurity ignore v8-compile-cache-lib@3.0.1
@SocketSecurity ignore create-require@1.1.1
@SocketSecurity ignore lunr@2.3.9
@SocketSecurity ignore shiki@0.14.3
@SocketSecurity ignore globby@11.1.0
@SocketSecurity ignore marked@4.3.0
@SocketSecurity ignore typescript@4.8.4

@mcmire mcmire marked this pull request as draft July 17, 2023 22:04
@mcmire
Copy link
Author

mcmire commented Jul 17, 2023

I'm going to move this to draft. I don't like how I've defined the methods here (no reason to make them bound methods, and we might be able to assign better types to the individual methods that match the Ethereum spec). But also, I think the type definitions we have are good enough for now.

@legobeat
Copy link

legobeat commented Jul 22, 2023

While it looks great as such, I'm not sure if this is desirable for a base-library package like eth-query which would be good to have source portable. That there isn't a build-step makes it accessible. It's also small enough to not be unmanageable despite the ugly prototype-building IMO.

If this is revisited, perhaps it would be possible to port some of this to jsdoc and perhaps reconfigure tooling to get some of the benefits in this PR without introducing Typescript wholesale?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Publish new version of eth-query with type definitions from this repo

2 participants