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

TypeError: rpc.NotificationType is not a constructor #469

Closed
erizet opened this issue Mar 17, 2023 · 9 comments
Closed

TypeError: rpc.NotificationType is not a constructor #469

erizet opened this issue Mar 17, 2023 · 9 comments

Comments

@erizet
Copy link

erizet commented Mar 17, 2023

I try to run the following sample in Node using ts-node. I keep getting this error.

"TypeError: rpc.NotificationType is not a constructor"

I also get the same error when using rpc.RequestType och RequestType1.

import { stringify } from 'querystring';
import * as rpc from 'vscode-ws-jsonrpc';
import { WebSocket } from 'ws';
import type { HelloRequest, HelloReply } from './generated/jsonrpcserver';

const webSocket = new WebSocket('ws://host.docker.internal:5160/socket');
rpc.listen({
    webSocket,
    onConnection: async (connection: rpc.MessageConnection) => {
        // var req = new rpc.RequestType1<HelloRequest, HelloReply, void>("SayHelloAsync");
        // var resp = connection.sendRequest(req,  { name: "erik "});
        const notification = new rpc.NotificationType<string>('testNotification');
        connection.listen();
        connection.sendNotification(notification, 'Hello World');

        // console.log("From server: " + resp.message);
    }
});
@CGNonofr
Copy link
Collaborator

It seems more related to vscode-ws-jsonrpc that this repo then.

It you print rpc.NotificationType or rpc, what do you get?

@erizet
Copy link
Author

erizet commented Mar 17, 2023

Yes, but the vscode-ws-jsonrpc repo says it's deperecated and point to this?!? :)

This is a console.log of rpc.

[Module: null prototype] {
  AbstractMessageReader: [class AbstractMessageReader],
  AbstractMessageSignature: [class AbstractMessageSignature],
  AbstractMessageWriter: [class AbstractMessageWriter],
  CancellationReceiverStrategy: {
    Message: {
      createCancellationTokenSource: [Function: createCancellationTokenSource]
    },
    is: [Function: is]
  },
  CancellationSenderStrategy: {
    Message: {
      sendCancellation: [Function: sendCancellation],
      cleanup: [Function: cleanup]
    },
    is: [Function: is]
  },
  CancellationStrategy: {
    Message: { receiver: [Object], sender: [Object] },
    is: [Function: is]
  },
  CancellationToken: {
    None: {
      isCancellationRequested: false,
      onCancellationRequested: [Function (anonymous)]
    },
    Cancelled: {
      isCancellationRequested: true,
      onCancellationRequested: [Function (anonymous)]
    },
    is: [Function: is]
  },
  CancellationTokenSource: [class CancellationTokenSource],
  ConnectionError: [class ConnectionError extends Error],
  ConnectionErrors: {
    '1': 'Closed',
    '2': 'Disposed',
    '3': 'AlreadyListening',
    Closed: 1,
    Disposed: 2,
    AlreadyListening: 3
  },
  ConnectionOptions: { is: [Function: is] },
  ConnectionStrategy: { is: [Function: is] },
  ConsoleLogger: [class ConsoleLogger],
  Disposable: { create: [Function: create] },
  DisposableCollection: [class DisposableCollection],
  Emitter: [class Emitter] { _noop: [Function (anonymous)] },
  Event: { None: [Function (anonymous)] },
  IPCMessageReader: [class IPCMessageReader extends AbstractMessageReader],
  IPCMessageWriter: [class IPCMessageWriter extends AbstractMessageWriter],
  LRUCache: [class LRUCache extends LinkedMap],
  LinkedMap: [class LinkedMap],
  LogTraceNotification: {
    type: NotificationType {
      method: '$/logTrace',
      numberOfParams: 1,
      _parameterStructures: [ParameterStructures]
    }
  },
  MessageReader: { is: [Function: is] },
  MessageWriter: { is: [Function: is] },
  NullLogger: {
    error: [Function: error],
    warn: [Function: warn],
    info: [Function: info],
    log: [Function: log]
  },
  ProgressToken: { is: [Function: is] },
  ProgressType: [class ProgressType],
  RAL: [Function: RAL] { install: [Function: install] },
  ReadableStreamMessageReader: [class ReadableStreamMessageReader extends AbstractMessageReader],
  SetTraceNotification: {
    type: NotificationType {
      method: '$/setTrace',
      numberOfParams: 1,
      _parameterStructures: [ParameterStructures]
    }
  },
  SocketMessageReader: [class SocketMessageReader extends ReadableStreamMessageReader],
  SocketMessageWriter: [class SocketMessageWriter extends WriteableStreamMessageWriter],
  StreamMessageReader: [class StreamMessageReader extends ReadableStreamMessageReader],
  StreamMessageWriter: [class StreamMessageWriter extends WriteableStreamMessageWriter],
  Touch: { None: 0, First: 1, AsOld: 1, Last: 2, AsNew: 2 },
  Trace: {
    '0': 'Off',
    '1': 'Messages',
    '2': 'Compact',
    '3': 'Verbose',
    Off: 0,
    Messages: 1,
    Compact: 2,
    Verbose: 3,
    fromString: [Function: fromString],
    toString: [Function: toString]
  },
  TraceFormat: { Text: 'text', JSON: 'json', fromString: [Function: fromString] },
  TraceValues: {
    Off: 'off',
    Messages: 'messages',
    Compact: 'compact',
    Verbose: 'verbose'
  },
  WebSocketMessageReader: [class WebSocketMessageReader extends AbstractMessageReader],
  WebSocketMessageWriter: [class WebSocketMessageWriter extends AbstractMessageWriter],
  WriteableStreamMessageWriter: [class WriteableStreamMessageWriter extends AbstractMessageWriter],
  createClientPipeTransport: [Function: createClientPipeTransport],
  createClientSocketTransport: [Function: createClientSocketTransport],
  createMessageConnection: [Function: createMessageConnection],
  createServerPipeTransport: [Function: createServerPipeTransport],
  createServerSocketTransport: [Function: createServerSocketTransport],
  createWebSocketConnection: [Function: createWebSocketConnection],
  generateRandomPipeName: [Function: generateRandomPipeName],
  listen: [Function: listen],
  toSocket: [Function: toSocket]`


@CGNonofr
Copy link
Collaborator

Oops read too fast, vscode-ws-jsonrpc is here, it's not vscode-jsonrpc

vscode-ws-jsonrpc doesn't export things from NotificationType anymore, Typescript would have tell you, you need to import it from vscode-jsonrpc directly

@erizet
Copy link
Author

erizet commented Mar 17, 2023

Strange, have a look at the files in the npm package, https://www.npmjs.com/package/vscode-ws-jsonrpc?activeTab=code. To me it seems like the files are exported. I'm still using version 2.0.2.


/* --------------------------------------------------------------------------------------------
 * Copyright (c) 2018-2022 TypeFox GmbH (http://www.typefox.io). All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 * ------------------------------------------------------------------------------------------ */

export * from 'vscode-jsonrpc';
export * from 'vscode-jsonrpc/lib/common/messages.js';
export * from './disposable.js';
export * from './socket/index.js';
export * from './logger.js';
export * from './connection.js';

@CGNonofr
Copy link
Collaborator

Indeed that's strange, @kaisalmen do you have an idea?

@CGNonofr
Copy link
Collaborator

BTW, it's probably a good idea to import them from vscode-jsonrpc directly

@kaisalmen
Copy link
Collaborator

Yes, this is indeed strange. I will to try to reproduce this.

@kaisalmen
Copy link
Collaborator

@erizet sorry, it took a bit longer, but I now tried to reproduce your issue, but I wasn't able to get to your problem.
The listen function is for the client side and expects a WebSocket implementation from the browser. It does not work with node's ws implementation.
This code can be compiled with tsc, but it won't work in node/ts-node:

import { MessageConnection, NotificationType } from 'vscode-jsonrpc';
import { listen } from 'vscode-ws-jsonrpc';

listen({
    webSocket: new WebSocket('ws://host.docker.internal:5160/socket'),
    onConnection: async (connection: MessageConnection) => {
        const notification = new NotificationType<string>('testNotification');
        connection.listen();
        connection.sendNotification(notification, 'Hello World');
    }
});

I have to admit that we only have monaco-languageclient examples available and not specific ones for vscode-ws-jsonrpc. The only source for info is the README. We should change that.

@kaisalmen
Copy link
Collaborator

Adding vscode-ws-jsonrpc specific examples is tracked by #478.

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

3 participants