test('correctly binds to the server and redirects its events.', () => {
  const server = createServer();
  proxifier.bind(server);

  const eventsAndListeners = new Map(
    ['listening', 'error', 'clientError', 'connection'].map(eventName => {
      const listener = jest.fn();
      proxifier.addListener(eventName, listener);

      return [eventName, listener] as [string, () => void];
    })
  );

  for (const [eventName, listener] of eventsAndListeners) {
    expect(listener).not.toHaveBeenCalled();

    // Emit several events, to make sure that server is not being listened with `once`.
    server.emit(eventName, 1, 2, 3, 4);
    server.emit(eventName, 5, 6, 7, 8);

    expect(listener).toHaveBeenCalledTimes(2);
    expect(listener).toHaveBeenCalledWith(1, 2, 3, 4);
    expect(listener).toHaveBeenCalledWith(5, 6, 7, 8);
  }
});
        test("should successfully send a message with args", function(done: MochaDone) {
            const args = ["android"];
            let receivedMessage: ExtensionMessage;
            let receivedArgs: any;

            mockServer = net.createServer(function(client: net.Socket): void {
                mockServer.close();
                client.on("data", function(data: Buffer) {
                    const messageData: any = JSON.parse(data.toString("utf8"));
                    client.end();

                    receivedMessage = messageData.message;
                    receivedArgs = messageData.args;
                });
            });

            mockServer.on("error", done);
            mockServer.listen(port);

            const sender = RemoteExtension.atProjectRootPath(projectRootPath);

            Q({})
                .then(function() {
                    return sender.prewarmBundleCache(args[0]);
                })
                .then(function() {
                    assert.equal(receivedMessage, ExtensionMessage.PREWARM_BUNDLE_CACHE);
                    assert.deepEqual(receivedArgs, args);
                }).done(() => done(), done);
        });
示例#3
0
                PortFinder.getPort((err, port) => {
                    let fatJar = Path.resolve(context.extensionPath, "out", "fat-jar.jar");
                    
                    let args = [
                        '-cp', fatJar, 
                        '-Djavacs.port=' + port,
                        'org.javacs.Main'
                    ];
                    
                    console.log(javaExecutablePath + ' ' + args.join(' '));
                    
                    Net.createServer(socket => {
                        console.log('Child process connected on port ' + port);

                        resolve({
                            reader: socket,
                            writer: socket
                        });
                    }).listen(port, () => {
                        let options = { stdio: 'inherit', cwd: VSCode.workspace.rootPath };
                        
                        // Start the child java process
                        ChildProcess.execFile(javaExecutablePath, args, options);
                    });
                });
示例#4
0
文件: app.ts 项目: 52M/LightSword
 constructor(options: { dstAddr: string, dstPort?: number, localPort?: number }) {
   let dstAddr = options.dstAddr;
   let dstPort = options.dstPort || defaultServerPort;
   let localPort = options.localPort || defaultServerPort;
   
   let server = net.createServer((socket) => {
     let transitSocket = net.createConnection(dstPort, dstAddr, () => {
       socket.pipe(transitSocket);
       transitSocket.pipe(socket);
     });
     
     function dispose() {
       transitSocket.dispose();
       socket.dispose();
     }
     
     transitSocket.on('end', dispose);
     transitSocket.on('error', dispose);
     socket.on('end', dispose);
     socket.on('error', dispose);
   });
   
   server.on('error', (err) => {
     console.log(err.message);
     process.exit(1);
   });
   
   server.listen(localPort);
 }
示例#5
0
    function _getPort(portNumber: number) {
      if (portNumber > 65535) {
        reject(new Error(`There is no port available.`));
      }

      const server = net.createServer();

      server.once('error', (err: Error & {code: string}) => {
        if (err.code !== 'EADDRINUSE') {
          reject(err);
        } else if (port === 0) {
          _getPort(portNumber + 1);
        } else {
          // If the port isn't available and we weren't looking for any port, throw error.
          reject(
            new Error(`Port ${port} is already in use. Use '--port' to specify a different port.`),
          );
        }
      })
      .once('listening', () => {
        server.close();
        resolve(portNumber);
      })
      .listen(portNumber, host);
    }
        test("should successfully send a message", function(done: MochaDone) {
            let receivedMessage: ExtensionMessage;

            mockServer = net.createServer(function(client: net.Socket): void {
                mockServer.close();
                client.on("data", function(data: Buffer) {
                    const messageData: any = JSON.parse(data.toString("utf8"));
                    client.end();

                    receivedMessage = messageData.message;
                });
            });

            mockServer.on("error", done);
            mockServer.listen(port);

            const sender = RemoteExtension.atProjectRootPath(projectRootPath);

            Q({})
                .then(function() {
                    return sender.startPackager();
                })
                .then(function() {
                    assert.equal(receivedMessage, ExtensionMessage.START_PACKAGER);
                }).done(() => done(), done);
        });
示例#7
0
文件: index.ts 项目: Pajn/RAXA
  async start() {
    const broker = createMqttBroker()
    const server = createServer(broker.handle)
    this.server = server

    this.listenOn(
      plugin.interfaces.MQTT.id,
      plugin.interfaces.MQTT.events.publish.id,
      ({
        clientId,
        topic,
        value,
      }: {
        clientId: string
        topic: string
        value: string
      }) => {
        this.log.debug('Publishing packet', {
          clientId,
          topic,
          payload: value,
          qos: 0,
          retain: false,
        })
        const packet: IPublishPacket = {
          cmd: 'publish',
          topic,
          payload: value,
          qos: 0,
          retain: false,
          dup: false,
        }
        if (clientId) {
          ;(broker.publish as any)(packet, clientId, () => {})
        } else {
          broker.publish(packet, () => {})
        }
      },
    )

    broker.on('publish', (packet, client) => {
      this.log.debug('Received packet', {
        clientId: client && client.id,
        topic: packet.topic,
        value: packet.payload.toString(),
      })
      this.fireEvent(
        plugin.interfaces.MQTT.id,
        plugin.interfaces.MQTT.events.received.id,
        {
          clientId: client && client.id,
          topic: packet.topic,
          value: packet.payload.toString(),
        },
      )
    })

    await new Promise(resolve => server.listen(1883, resolve))
  }
示例#8
0
export function findNextPort(port: number) {
  const server = net.createServer();
  return new Promise<number>((resolve) => {
    server.once("close", () => resolve(port));
    server.on("error", () => resolve(findNextPort(port + 1)));
    server.listen(port, () => server.close());
  });
}
test('correctly binds to the server.', () => {
  const server = createServer();
  jest.spyOn(server, 'addListener');
  proxifier.bind(server);

  expect(server.addListener).toHaveBeenCalledTimes(4);
  for (const eventName of ['listening', 'error', 'clientError', 'connection']) {
    expect(server.addListener).toHaveBeenCalledWith(eventName, expect.any(Function));
  }
});
示例#10
0
    new Promise((resolve, reject) => {
      const nodeNet = require("net")
      const server = nodeNet.createServer()

      server.on("error", (err: any) => reject(err))
      server.on("listening", () => resolve(server))

      server.listen(p, host, () => {
        logger.info(`test server is up on port ${p}`)
      })
    })