From 6d4e17652a411ea8b1576b7ba86ab674423e3cba Mon Sep 17 00:00:00 2001 From: stonegray Date: Mon, 29 Nov 2021 06:15:49 -0500 Subject: [PATCH 01/24] Add type definitions file All constants and methods are implemented. Deep access of the .raw object is partially supported, but does not duplicate the SocketLevel or SocketOption constants. --- index.d.ts | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e48053b --- /dev/null +++ b/index.d.ts @@ -0,0 +1,108 @@ +export interface SOptions { + bufferSize?: number, + addressFamily?: number, + protocol?: number +} + +export const SocketLevel: { + IPPROTO_IP: 0; + IPPROTO_IPV6: 41; + SOL_SOCKET: 65535; +}; + +export const SocketOption: { + IPV6_TTL: 4; + IPV6_UNICAST_HOPS: 4; + IPV6_V6ONLY: 27; + IP_HDRINCL: 2; + IP_OPTIONS: 1; + IP_TOS: 3; + IP_TTL: 4; + SO_BROADCAST: 32; + SO_RCVBUF: 4098; + SO_RCVTIMEO: 4102; + SO_SNDBUF: 4097; + SO_SNDTIMEO: 4101; +}; + +// The internal C++ socket: +export class SocketWrap { + htonl(n: number): number; + htons(n: number): number; + ntohl(n: number): number; + ntohs(n: number): number; + + // Also contains SocketLevel/SocketOption, but + // skipped. +} + +export declare interface Socket { + on(event: 'close'): this; + on(event: 'error', listener: (error: string) => void): this; +} + +export class Socket extends EventEmitter { + constructor(options?: SOptions); + + wrap: SocketWrap; + requests: any[]; + buffer: Buffer; + + recvPaused: boolean; + sendPaused: boolean; + + close(): this; + + getOption(level: number, option: number, value: Buffer, length: number): number; + + onClose(): void; + + onError(error: any): void; + + onRecvReady(): Socket; + + onSendReady(): void; + + pauseRecv(): Socket; + + pauseSend(): Socket; + + resumeRecv(): Socket; + + resumeSend(): Socket; + + send(buffer: Buffer, offset: number, length: number, address: any, beforeCallback: Function, afterCallback: Function): Socket; + + setOption(level: number, option: number, value: Buffer, length?: number): void; + +} + +export const AddressFamily: { + IPv4: 1; + IPv6: 2; +}; + +export const Protocol: { + None: 0; + ICMP: 1; + TCP: 6; + UDP: 17; + ICMPv6: 58; +}; + + +// Looks like this can take some object with a .buffer prop/method as well +interface BufferableObject { + buffer?: Buffer +} + +export function createChecksum(...args: Buffer[] | BufferableObject[]): number; + +export function createSocket(options?: SOptions): Socket; + +export function htonl(n: number): number; +export function htons(n: number): number; +export function ntohl(n: number): number; +export function ntohs(n: number): number; + +export function writeChecksum(buffer: Buffer, offset: number, checksum: any): Buffer; From 3cee9287fd46c599b7ad1740922e81b2650742b7 Mon Sep 17 00:00:00 2001 From: quantumsheep Date: Mon, 6 Dec 2021 23:52:08 +0100 Subject: [PATCH 02/24] Import EventEmitter in `index.d.ts` --- index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.d.ts b/index.d.ts index e48053b..c1967d1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,5 @@ +import { EventEmitter } from 'events'; + export interface SOptions { bufferSize?: number, addressFamily?: number, From 900fe3526e1581c323ba8082454e000dce189452 Mon Sep 17 00:00:00 2001 From: quantumsheep Date: Mon, 6 Dec 2021 23:56:34 +0100 Subject: [PATCH 03/24] Add `node-gyp` and package scripts --- package.json | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1728564..3e23d51 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,10 @@ "directories": { "example": "example" }, + "scripts": { + "build": "node-gyp configure build", + "prepare": "npm run build" + }, "dependencies": { "nan": "2.14.*" }, @@ -17,11 +21,15 @@ { "name": "NoSpaceships Ltd", "email": "hello@nospaceships.com" + }, + { + "name": "Nathanael Demacon", + "email": "nathanael.dmc@outlook.fr" } ], "repository": { "type": "git", - "url": "git://github.com/nospaceships/node-raw-socket.git" + "url": "git://github.com/quantumsheep/node-raw-socket.git" }, "keywords": [ "checksum", @@ -39,5 +47,8 @@ "sockets" ], "author": "NoSpaceships Ltd ", - "license": "MIT" -} + "license": "MIT", + "devDependencies": { + "node-gyp": "^8.4.1" + } +} \ No newline at end of file From 77b599f0de983f35b665fa945b49220136b8da01 Mon Sep 17 00:00:00 2001 From: quantumsheep Date: Mon, 6 Dec 2021 23:58:32 +0100 Subject: [PATCH 04/24] Update package's name and description --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3e23d51..970e2a1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "raw-socket", + "name": "raw-socket-ts", "version": "1.7.0", - "description": "Raw sockets for Node.js.", + "description": "Raw sockets for Node.js with TypeScript typing.", "main": "index.js", "directories": { "example": "example" From 7f4a1f28f7a9383653834c47c6ee01dab3c4b5e3 Mon Sep 17 00:00:00 2001 From: quantumsheep Date: Tue, 7 Dec 2021 00:03:20 +0100 Subject: [PATCH 05/24] Add ESLint and Prettier --- .eslintrc.js | 27 +++++++++++++++++++++++++++ .prettierrc | 4 ++++ package.json | 12 +++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .eslintrc.js create mode 100644 .prettierrc diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..0067ba9 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,27 @@ +module.exports = { + env: { + node: true, + }, + plugins: ['@typescript-eslint/eslint-plugin'], + extends: [ + 'plugin:@typescript-eslint/recommended', + 'plugin:prettier/recommended', + ], + parser: '@typescript-eslint/parser', + parserOptions: { + sourceType: 'module', + ecmaVersion: '12', + }, + rules: { + '@typescript-eslint/interface-name-prefix': 'off', + '@typescript-eslint/explicit-function-return-type': 'off', + '@typescript-eslint/explicit-module-boundary-types': 'off', + '@typescript-eslint/no-explicit-any': 'off', + 'prettier/prettier': [ + 'error', + { + endOfLine: 'auto', + }, + ], + }, +}; diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..4ff2b8b --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "singleQuote": true, + "trailingComma": "all" +} \ No newline at end of file diff --git a/package.json b/package.json index 970e2a1..7bc81c6 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "example": "example" }, "scripts": { + "lint": "eslint src/**/*.ts --fix", "build": "node-gyp configure build", "prepare": "npm run build" }, @@ -49,6 +50,15 @@ "author": "NoSpaceships Ltd ", "license": "MIT", "devDependencies": { - "node-gyp": "^8.4.1" + "@typescript-eslint/eslint-plugin": "^5.6.0", + "@typescript-eslint/parser": "^5.6.0", + "eslint": "^7.32.0", + "eslint-config-airbnb-base": "^15.0.0", + "eslint-config-prettier": "^8.3.0", + "eslint-plugin-import": "^2.25.3", + "eslint-plugin-prettier": "^4.0.0", + "node-gyp": "^8.4.1", + "prettier": "^2.5.1", + "prettier-eslint": "^13.0.0" } } \ No newline at end of file From 091b924137ba94d59ee163b824d91284a0ec6c91 Mon Sep 17 00:00:00 2001 From: quantumsheep Date: Tue, 7 Dec 2021 00:05:03 +0100 Subject: [PATCH 06/24] Disable some ESLint rules --- .eslintrc.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index 0067ba9..f9ae5e0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -17,6 +17,8 @@ module.exports = { '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/no-var-requires': 'off', + '@typescript-eslint/no-this-alias': 'off', 'prettier/prettier': [ 'error', { From 415962ad17f99f6f3711f12f89a64cfa6354480c Mon Sep 17 00:00:00 2001 From: quantumsheep Date: Tue, 7 Dec 2021 00:05:28 +0100 Subject: [PATCH 07/24] Prettify sources and examples --- example/create-checksum.js | 24 +-- example/get-option.js | 21 +- example/htonl.js | 11 +- example/htons.js | 11 +- example/ntohl.js | 11 +- example/ntohs.js | 11 +- example/ping-no-ip-header.js | 73 +++---- example/ping-set-option-ip-ttl.js | 67 +++---- example/ping.js | 75 ++++--- example/ping6-no-ip-header.js | 79 ++++---- example/ping6.js | 77 ++++--- index.d.ts | 129 +++++++----- index.js | 321 ++++++++++++++++-------------- 13 files changed, 469 insertions(+), 441 deletions(-) diff --git a/example/create-checksum.js b/example/create-checksum.js index 2292d76..fc7e6ef 100644 --- a/example/create-checksum.js +++ b/example/create-checksum.js @@ -1,21 +1,21 @@ - -var raw = require ("../"); +var raw = require('../'); var buffer1 = Buffer.from([ - 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x09, - 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, - 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70]); + 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x09, 0x61, 0x62, 0x63, 0x64, 0x65, + 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, +]); var buffer2 = Buffer.from([ - 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x61, - 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69]); + 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, +]); var option1 = { - buffer: buffer1, - offset: 0, - length: buffer1.length + buffer: buffer1, + offset: 0, + length: buffer1.length, }; -var sum = raw.createChecksum (option1, buffer2); +var sum = raw.createChecksum(option1, buffer2); -console.log (sum.toString (16)); +console.log(sum.toString(16)); diff --git a/example/get-option.js b/example/get-option.js index fd83d43..1b46d6e 100644 --- a/example/get-option.js +++ b/example/get-option.js @@ -1,26 +1,25 @@ - -var raw = require ("../"); +var raw = require('../'); if (process.argv.length < 4) { - console.log ("node get-option