Subject: [bug#62375] [PATCH] import: Add binary npm importer.

From: Ludovic Courtès <[email-masked]>
Date: Tue, 28 Mar 2023 17:49:32 +0200

Hello Jelle!

[email-masked] skribis:

> From: Jelle Licht <[email-masked]>
>
> * guix/scripts/import.scm: (importers): Add "npm-binary".
> * guix/import/npm-binary.scm: New file.
> * guix/scripts/import/npm-binary.scm: New file.
> * Makefile.am: Add them.
>
> Co-authored-by: Timothy Sample <[email-masked]>
> Co-authored-by: Lars-Dominik Braun <[email-masked]>

Woohoo!  I think it’ll be useful to many, even if we know it’s far from
“ideal” by our standards.

We’ll need doc under “Invoking guix import” and tests in
‘tests/npm-binary.scm’, similar to what’s done for the other importers.
The doc should clarify why it’s called that way and what the limitations
are.

For tests, I’d recommend mocking the npmjs.org HTTP servers using
‘with-http-server’ as in ‘tests/cpan.scm’.

Also, please add docstrings to top-level procedures.

> +;; TODO: Support other registries
> +(define *registry* "https://registry.npmjs.org")
> +(define *default-page* "https://www.npmjs.com/package")

The convention currently is more like ‘%registry’.

> +(define (http-error-code arglist)
> +  (match arglist
> +    (('http-error _ _ _ (code)) code)
> +    (_ #f)))

Unused.  :-)

> +(define (hash-url url)
> +  "Downloads the resource at URL and computes the base32 hash for it."
> +  (call-with-temporary-output-file
> +   (lambda (temp port)
> +     (begin ((@ (guix import utils) url-fetch) url temp)
> +            (guix-hash-url temp)))))

Maybe something more like: (port-sha256 (http-fetch …)) ?

> +(define (npm-package->package-sexp npm-package)
> +  "Return the `package' s-expression for an NPM-PACKAGE."
> +  (define (new-or-existing-inputs resolved-deps)
> +    (map package-revision->input resolved-deps))
> +
> +  (match npm-package
> +    (($ <package-revision> name version home-page dependencies dev-dependencies peer-dependencies license description dist)

Please use ‘match-record’ instead and keep lines below 80 chars.  :-)

> +     (let* ((name (npm-name->name name))
> +            (url (dist-tarball dist))
> +            (home-page (if (string? home-page)
> +                           home-page
> +                           (string-append *default-page* "/" (uri-encode name))))
> +            (synopsis description)
> +            (resolved-deps (map (match-lambda (($ <versioned-package> name version)
> +                                               (resolve-package name (string->semver-range version)))) (append dependencies peer-dependencies)))

Likewise.

That’s it!  Could you send an updated patch?

Thanks,
Ludo’.