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

dnt on WebSocket base project. #194

Closed
UrielCh opened this issue Jul 16, 2022 · 7 comments
Closed

dnt on WebSocket base project. #194

UrielCh opened this issue Jul 16, 2022 · 7 comments

Comments

@UrielCh
Copy link
Contributor

UrielCh commented Jul 16, 2022

I'm trying to convert a simple project from nodeJS to deno.

Deno branch: chrome-remote-interface

but building nodejs version give me those Errors:

src/deps/deno.land/x/event@2.0.0/mod.ts:15:19 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'.

15   #globalWriters: WritableStreamDefaultWriter<Entry<E, keyof E>>[] = [];
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/deps/deno.land/x/event@2.0.0/mod.ts:17:22 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'.

17     [K in keyof E]?: WritableStreamDefaultWriter<E[K]>[];
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/deps/deno.land/x/event@2.0.0/mod.ts:65:42 - error TS2304: Cannot find name 'TransformStream'.

65       const { readable, writable } = new TransformStream<E[K], E[K]>();
                                            ~~~~~~~~~~~~~~~
src/deps/deno.land/x/event@2.0.0/mod.ts:140:14 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'.

140         ) as WritableStreamDefaultWriter<E[K]>[][]
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/deps/deno.land/x/event@2.0.0/mod.ts:193:40 - error TS2304: Cannot find name 'TransformStream'.

193     const { readable, writable } = new TransformStream<
                                           ~~~~~~~~~~~~~~~
src/lib/chrome.ts:165:27 - error TS2304: Cannot find name 'MessageEvent'.

165     ws.onmessage = (data: MessageEvent) => {
                              ~~~~~~~~~~~~
src/lib/devtools.ts:65:46 - error TS2339: Property 'resolveDns' does not exist on type 'typeof Deno'.

65         const [address] = await dntShim.Deno.resolveDns(u.hostname, "A");
                                                ~~~~~~~~~~

is https://deno.land/x/event have a dntShim? or can I use an alternative lib?

how can I fix the Deno.resolveDns(hostname, "A");

Just 2 mapping errors to fix.

@UrielCh UrielCh changed the title Property 'resolveDns' does not exist on type 'typeof Deno'. first deno project convertion 2 mapping error. Jul 16, 2022
@kitsonk
Copy link

kitsonk commented Jul 16, 2022

You need to add the following to your config:

{
  shims: {
    custom: [{
        package: {
          name: "stream/web",
        },
        globalNames: ["ReadableStream", "TransformStream"],
      },
  },
}

As well as you should update your compilerOptions as well to use lib: ["lib.esnext.full.d.ts"].

@UrielCh
Copy link
Contributor Author

UrielCh commented Jul 17, 2022

That better now, aside the resolveDns I still have a few error:

WritableStreamDefaultWriter errors

src/deps/deno.land/x/event@2.0.0/mod.ts:17:19 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'.

17   #globalWriters: WritableStreamDefaultWriter<Entry<E, keyof E>>[] = [];
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/deps/deno.land/x/event@2.0.0/mod.ts:19:22 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'.

19     [K in keyof E]?: WritableStreamDefaultWriter<E[K]>[];
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/deps/deno.land/x/event@2.0.0/mod.ts:142:14 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'.

142         ) as WritableStreamDefaultWriter<E[K]>[][]
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~

websocket onmessage() typing differences

src/lib/chrome.ts:167:27 - error TS2304: Cannot find name 'MessageEvent'.

167     ws.onmessage = (data: MessageEvent) => {
                              ~~~~~~~~~~~~

this one I can fix / hide it by replacing MessageEvent by a {data: string | unknown} and testing the type of data.

my build step:

await build({
    entryPoints: ["./mod.ts"],
    outDir: "./npm",
    shims: {
      // see JS docs for overview and more options
      deno: true,
      webSocket: true,
      undici: true,
      custom: [
        {
          package: {
            name: "stream/web",
          },
          globalNames: ["ReadableStream", "TransformStream"],
        }
      ],
    },
    compilerOptions: {
      lib: [ "esnext" ],
    },
    package: {...}
});

and "lib.esnext.full.d.ts" is not a valid lib value

for now WritableStreamDefaultWriter is the only unfixabe issue, WritableStreamDefaultWriter is inside https://deno.land/x/event@2.0.0/mod.ts dependency.

@kitsonk
Copy link

kitsonk commented Jul 17, 2022

Try esnext.full. Basically you are missing the DOM definitions.

@UrielCh
Copy link
Contributor Author

UrielCh commented Jul 17, 2022

Now I'im using:

    compilerOptions: {
      lib: [ "dom", "esnext" ],
    },

I think that there is a bug in dnt, my code:

  #connectToWebSocket(): Promise<WebSocket> {
    const ws = new WebSocket(this.webSocketDebuggerUrl);
    ws.onmessage = (message: MessageEvent) => {

is converted as:

  #connectToWebSocket(): Promise<dntShim.WebSocket> {
    const ws = new dntShim.WebSocket(this.webSocketDebuggerUrl);
    ws.onmessage = (message: MessageEvent) => {

but should had been converted do:

  #connectToWebSocket(): Promise<dntShim.WebSocket> {
    const ws = new dntShim.WebSocket(this.webSocketDebuggerUrl);
    ws.onmessage = (message: dntShim.WebSocket.MessageEvent) => {

and this code works.

to bypasse the isse

I replaced MessageEvent by {data: string | unknown}
having only text message it's works in my case.

My original nodeJS version work fine with the code:

import WebSocket from "ws";
...
  #connectToWebSocket(): Promise<WebSocket> {
    const ws = new WebSocket(this.webSocketDebuggerUrl);
    ws.onmessage = (message: WebSocket.MessageEvent) => {
      if (typeof message.data === "string") {

Now I just need to implement dntShim.Deno.resolveDns(u.hostname, "A");

that fine.
thx, I'll will give you update soon.

@UrielCh

This comment was marked as outdated.

@UrielCh
Copy link
Contributor Author

UrielCh commented Jul 17, 2022

after reading the source code of dns, I fix my build by adding:

  {
    globalNames: [ {name: "MessageEvent", typeOnly: true} ],
    package: {
      name: "ws",
    },
  }

My recomentation, add a logic to avoid using:

export interface GlobalName {
  name: string;
  exportName?: string;
  typeOnly?: boolean;
}

Allow export names:

"type TypeName"

and

"default as name"

and

"type theType as name"

Since a type can not contains space, you won't have any breaking change, and such a syntax is easy to read.

I can PR this change for you.

@kitsonk
Copy link

kitsonk commented Jul 17, 2022

I there anything actionable from your viewpoint on this issue now, other than maybe continue to improve the documentation?

@UrielCh UrielCh changed the title first deno project convertion 2 mapping error. dnt on WebSocket base project. Jul 18, 2022
@UrielCh UrielCh closed this as completed Jul 18, 2022
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

No branches or pull requests

2 participants