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

feat(cli): provide Deno.info() API #10758

Closed
wants to merge 5 commits into from
Closed

Conversation

kitsonk
Copy link
Contributor

@kitsonk kitsonk commented May 25, 2021

Resolves #2096

This is a work in progress PR. There are a couple of items still to address:

  • There should be a permission check for read permission to the DENO_DIR to access the API.
  • The API should support import-maps, like Deno.emit(), but no one is happy with the Deno.emit() solution, so we need to debate the best way to allow an import-map to be provided.
  • It needs tests.

I have implemented the import map in a more straight forward way and my opinion is that we should change Deno.emit() to follow. In addition I added two options that are off by default that suppress some sensitive data from being returned and documented the whole security implications of using the API. Flexing all the features looks something like this:

const info = await Deno.info("./test.ts", {
  checksums: true,
  importMap: "./import_map.json",
  paths: true,
});

console.log(info);

Or with an import object literal, something like this:

const info = await Deno.info("./test.ts", {
  importMap: {
    imports: {
      jquery: "https://cdn.skypack.dev/jquery",
    },
  },
});

console.log(info);

@crowlKats
Copy link
Member

Shouldn't it also support usage without a specifier, just like the info subcommand?

@kitsonk
Copy link
Contributor Author

kitsonk commented May 25, 2021

Shouldn't it also support usage without a specifier, just like the info subcommand?

I'm not personally convinced of that. What would be a legitimate use case for having that type of information available programatically?

@crowlKats
Copy link
Member

I'm not personally convinced of that. What would be a legitimate use case for having that type of information available programatically?

One use case is together with #10589, so one could get access to the location of the localstorage file (and other apis that would store data based on the origin)

@nayeemrmn
Copy link
Collaborator

What's your opinion on #10628, and maybe representing that change in advance with the runtime API? It would solve needing read permission to DENO_DIR when you just want a module graph and no cache info, so this is another justification IMO.

@kitsonk
Copy link
Contributor Author

kitsonk commented May 25, 2021

so one could get access to the location of the localstorage file (and other apis that would store data based on the origin)

Knowing a location isn't a use case... 😄 What problem does that solve having that information available via an API.

What's your opinion on #10628, and maybe representing that change in advance with the runtime API?

Maybe. I get the reasoning for it, but it complicates the API surface and as noted in the issue, it isn't something to be considered until 2.0 and I would then expect the Deno APIs to reflect the subcommands. My opinion is we shouldn't wait until 2.0 to introduce this API (it already has quite a few real world use cases, especially with those using Deno as an on the fly transpiler/bundler).

@crowlKats
Copy link
Member

Knowing a location isn't a use case... 😄 What problem does that solve having that information available via an API.

Well one example would be that one could make a backup by copying the file (useful for indexeddb)

@SyrupThinker
Copy link
Contributor

Shouldn't it also support usage without a specifier, just like the info subcommand?

I'm not personally convinced of that. What would be a legitimate use case for having that type of information available programatically?

I have an example use-case with Docuraptor.
It uses deno info to get the module cache directory to then aggregate a list of all locally cached modules.
This is used to provide a documentation index for said modules.

@nayeemrmn
Copy link
Collaborator

I think any info provided under deno info [mod.ts] has a use case in meta kind of tooling.

However I wouldn't be surprised if the runtime equivalents for deno info and deno info mod.ts were different functions. There is zero intersection in both arguments and return types. Other than the sheer justification that they are the same on the CLI, or that return type overloading is allowed by TS, I'm not a fan of keeping disjoint functions under the same name. We should consider having both Deno.info() and Deno.moduleInfo().

@kitsonk kitsonk changed the title [WIP] feat(cli): provide Deno.info() API feat(cli): provide Deno.info() API Jun 22, 2021
@kitsonk kitsonk requested a review from bartlomieju June 22, 2021 05:28
@kitsonk kitsonk marked this pull request as ready for review June 22, 2021 05:28
@kitsonk kitsonk requested a review from ry June 22, 2021 11:52
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Sorry for slow review @kitsonk, I completely missed the request 🤦

I have only minor comments

* In order to resolve relative modules, import maps need a "base" which is
* the import map itself. When an object is passed as a value the current
* working directory for the Deno process will be used. */
importMap?: string | URL | ImportMap;
Copy link
Member

Choose a reason for hiding this comment

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

In case this is ImportMap type - what is the "base URL" used for that import map?

* Similar to the command line functionality `deno info --json`, the API
* provides information about a module and its dependencies.
*
* Requires `allow/read` permissions to access local modules and cached
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: change allow/read to --allow-read to keep consistent with other docs?

Comment on lines +698 to +699
* checksums: true,
* paths: true,
Copy link
Member

Choose a reason for hiding this comment

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

Why not provide these things by default?

Comment on lines +159 to +162
#[serde(default)]
checksums: bool,
import_map: Option<StringOrImportMap>,
#[serde(default)]
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding Default to derive instead?

Comment on lines +188 to +189
let custom_root = std::env::var("DENO_DIR").map(String::into).ok();
let deno_dir = DenoDir::new(custom_root)?;
Copy link
Member

Choose a reason for hiding this comment

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

Skip reading env variable and use dir field from ProgramState instead?

match &options.import_map {
Some(StringOrImportMap::ImportMap(value)) => {
let cwd = std::env::current_dir()?;
let base_url = resolve_path(cwd.to_string_lossy().as_ref())?;
Copy link
Member

Choose a reason for hiding this comment

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

To answer myself to previous question - in case of passing "object" import map it uses CWD as default URL, could you please add it to the JSDoc on the API?

@timreichen
Copy link
Contributor

timreichen commented Jul 23, 2021

Just as an input: instead of a isDynamic in ModuleGraphDependency, maybe a dependencyType would be better.

enum DependencyType {
  Import,
  Export,
  DynamicImport
}

I ran into problems having just a dynamic flag when creating bundler. If one wants to add "Fetch" or some other type in the future, DependencyType can easily be extended.

@kitsonk
Copy link
Contributor Author

kitsonk commented Aug 24, 2021

Closing due to https://deno.land/x/deno_graph/ being available.

@kitsonk kitsonk closed this Aug 24, 2021
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.

Provide JS API to dependency information (Deno.info())
6 participants