denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.8k stars 5.22k forks source link

`node/http.ts` `http.request` should support `socketPath` option #17910

Open loynoir opened 1 year ago

loynoir commented 1 year ago

Describe the bug

Steps to Reproduce

> var Docker = (await import('npm:dockerode@3.3.4')).default
undefined
> var docker = new Docker({socketPath: '/var/run/docker.sock'});
undefined
> await docker.listContainers()
Uncaught TypeError: error sending request for url (http://localhost/containers/json): error trying to connect: tcp connect error: Connection refused (os error 111)
    at async mainFetch (internal:ext/fetch/26_fetch.js:267:14)
    at async fetch (internal:ext/fetch/26_fetch.js:491:9)
    at async ClientRequest._final (https://deno.land/std@0.177.0/node/http.ts:147:7)

Expected behavior

> var Docker = (await import('dockerode')).default
undefined
> var docker = new Docker({socketPath: '/var/run/docker.sock'});
undefined
> await docker.listContainers()
[
  {
    Id: ...

Environment

loynoir commented 1 year ago

minimal reproduce

void http.request(
  'http://localhost/version',
  {
    socketPath: '/var/run/docker.sock',
  },
  (res) => res.on('data', (c) => console.log('' + c)),
).end()

Expected

$ node
> void http.request('http://localhost/version', {socketPath: '/var/run/docker.sock'}, (res)=>res.on('data',c=>console.log(''+c))).end()
// {"Platform": ...

Actual


$ deno
> import http from 'node:http'
> void http.request('http://localhost/version', {socketPath: '/var/run/docker.sock'}, (res)=>res.on('data',c=>console.log(''+c))).end()
// nothing
loynoir commented 1 year ago

@kt3k

https://github.com/denoland/deno/issues/8821#issuecomment-1408439707

https://deno.land/x/socket_fetch@v0.1/mod.ts

Maybe can merge that library into node/http.ts logic?

danopia commented 1 year ago

https://deno.land/x/socket_fetch@v0.1/mod.ts

Maybe can merge that library into node/http.ts logic?

Unfortunately my socket_fetch library is incomplete :( It can only perform GET requests and is not optimized. I use it to query specific things but it's not in a shape to be dropped into npm libraries I think.

Ideally Deno ships some sort of Unix socket fetch client instead :)

loynoir commented 1 year ago

Workaround

import { fetch, Agent } from 'undici'

const resp = await fetch('http://localhost/version', {
  dispatcher: new Agent({
    connect: {
      socketPath: '/var/run/docker.sock'
    }
  })
})

console.log(await resp.text())
$ deno run --unstable -A ./reproduce.mjs
{"Platform":...
loynoir commented 10 months ago

Related

[feat] Make backend js ecosystem future better

https://github.com/nodejs/node/issues/50195