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(std/node/url): add URL export #7132

Merged
merged 6 commits into from
Aug 22, 2020
Merged

feat(std/node/url): add URL export #7132

merged 6 commits into from
Aug 22, 2020

Conversation

balupton
Copy link
Contributor

@balupton balupton commented Aug 20, 2020

In Node.js, URL is not global, so TypeScript complains:

Screen Shot 2020-08-21 at 12 56 48 am

Solution is to import URL from the url builtin module:

Screen Shot 2020-08-21 at 12 57 34 am

Which can be seen to work with Node.js via:

node --input-type=module -e "import { URL } from 'url'; console.log(new URL('https://deno.land'))"
URL {
  href: 'https://deno.land/',
  origin: 'https://deno.land',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'deno.land',
  hostname: 'deno.land',
  port: '',
  pathname: '/',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

This PR adds compatibility to Deno:

deno eval "import { URL } from 'https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/node/url.ts'; console.log(new URL('https://deno.land'))"
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/node/url.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/_constants.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/node/path.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/mod.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/win32.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/posix.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/common.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/separator.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/_interface.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/glob.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/path/_util.ts
Download https://raw.githubusercontent.com/denoland/deno/01e55499aeebf80a04dbbe2e5bd3f5efa94b0cff/std/_util/assert.ts
Check file:///Users/balupton/Projects/active/filedirname/__$deno$eval.ts
URL { href: "https://deno.land/", origin: "https://deno.land", protocol: "https:", username: "", password: "", host: "deno.land", hostname: "deno.land", port: "", pathname: "/", hash: "", search: "" }

Which before this PR would fail with the following:

deno eval "import { URL } from 'https://cdn.deno.land/std/versions/0.65.0/raw/node/url.ts'; console.log(new URL('https://deno.land'))"
Download https://cdn.deno.land/std/versions/0.65.0/raw/node/url.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/_constants.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/node/path.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/mod.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/win32.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/posix.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/common.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/separator.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/_interface.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/glob.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/path/_util.ts
Download https://cdn.deno.land/std/versions/0.65.0/raw/_util/assert.ts
Check file:///Users/balupton/Projects/active/filedirname/__$deno$eval.ts
error: TS2305 [ERROR]: Module '"https://cdn.deno.land/std/versions/0.65.0/raw/node/url"' has no exported member 'URL'.
import { URL } from 'https://cdn.deno.land/std/versions/0.65.0/raw/node/url.ts'; console.log(new URL('https://deno.land'))
         ~~~
    at file:///Users/balupton/Projects/active/filedirname/__$deno$eval.ts:1:10

All good for review & squash merge.

Compatibility with Node.js:

``` bash
node --input-type=module -e "import { URL } from 'url'; console.log(new URL('https://deno.land'))"
```

Which Node.js outputs:

```
URL {
  href: 'https://deno.land/',
  origin: 'https://deno.land',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'deno.land',
  hostname: 'deno.land',
  port: '',
  pathname: '/',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}
```
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

Could you add a tiny little test to std/node/url_test.ts ?

@balupton
Copy link
Contributor Author

Could you add a tiny little test to std/node/url_test.ts ?

ok, give me a min

@balupton
Copy link
Contributor Author

balupton commented Aug 20, 2020

Done.

Question, do the tests load automatically, or do I need to import it somewhere? As I had to create the url_test.ts file.

@balupton
Copy link
Contributor Author

balupton commented Aug 20, 2020

Seems I can't format it as I can't get the formater to run:

> python tools/format.py
Traceback (most recent call last):
  File "tools/format.py", line 89, in <module>
    sys.exit(main())
  File "tools/format.py", line 51, in main
    dprint()
  File "tools/format.py", line 59, in dprint
    run(command, shell=False, quiet=True)
  File "/Users/balupton/Projects/active/deno/tools/util.py", line 63, in run
    rc = subprocess.call(args, cwd=cwd, env=env, shell=shell)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 172, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1047, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
[1]:02:00:30:/Users/balupton/Projects/active/deno:patch-1
> python --version
Python 2.7.16

Can someone else alter the PR with the necessary changes.

Update: Checked the CI logs instead, and submitted the fix.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

Thanks for the test @balupton - LGTM

@ry ry merged commit b7ad544 into denoland:master Aug 22, 2020
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 21, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Feb 1, 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.

2 participants