migratingFromV1.md
 1  # socks
 2  
 3  ## Migrating from v1
 4  
 5  For the most part, migrating from v1 takes minimal effort as v2 still supports factory creation of proxy connections with callback support.
 6  
 7  ### Notable breaking changes
 8  
 9  - In an options object, the proxy 'command' is now required and does not default to 'connect'.
10  - **In an options object, 'target' is now known as 'destination'.**
11  - Sockets are no longer paused after a SOCKS connection is made, so socket.resume() is no longer required. (Please be sure to attach data handlers immediately to the Socket to avoid losing data).
12  - In v2, only the 'connect' command is supported via the factory SocksClient.createConnection function. (BIND and ASSOCIATE must be used with a SocksClient instance via event handlers).
13  - In v2, the factory SocksClient.createConnection function callback is called with a single object rather than separate socket and info object.
14  - A SOCKS http/https agent is no longer bundled into the library.
15  
16  For informational purposes, here is the original getting started example from v1 converted to work with v2.
17  
18  ### Before (v1)
19  
20  ```javascript
21  var Socks = require('socks');
22  
23  var options = {
24      proxy: {
25          ipaddress: "202.101.228.108",
26          port: 1080,
27          type: 5
28      },
29      target: {
30          host: "google.com",
31          port: 80
32      },
33      command: 'connect'
34  };
35  
36  Socks.createConnection(options, function(err, socket, info) {
37      if (err)
38          console.log(err);
39      else {
40          socket.write("GET / HTTP/1.1\nHost: google.com\n\n");
41          socket.on('data', function(data) {
42              console.log(data.length);
43              console.log(data);
44          });
45  
46          // PLEASE NOTE: sockets need to be resumed before any data will come in or out as they are paused right before this callback is fired.
47          socket.resume();
48  
49          // 569
50          // <Buffer 48 54 54 50 2f 31 2e 31 20 33 30 31 20 4d 6f 76 65 64 20 50 65...
51      }
52  });
53  ```
54  
55  ### After (v2)
56  ```javascript
57  const SocksClient = require('socks').SocksClient;
58  
59  let options = {
60      proxy: {
61          ipaddress: "202.101.228.108",
62          port: 1080,
63          type: 5
64      },
65      destination: {
66          host: "google.com",
67          port: 80
68      },
69      command: 'connect'
70  };
71  
72  SocksClient.createConnection(options, function(err, result) {
73      if (err)
74          console.log(err);
75      else {
76          result.socket.write("GET / HTTP/1.1\nHost: google.com\n\n");
77          result.socket.on('data', function(data) {
78              console.log(data.length);
79              console.log(data);
80          });
81  
82          // 569
83          // <Buffer 48 54 54 50 2f 31 2e 31 20 33 30 31 20 4d 6f 76 65 64 20 50 65...
84      }
85  });
86  ```