@pgtyped/wire
Advanced tools
Comparing version
@@ -33,2 +33,4 @@ /// <reference types="node" /> | ||
export declare const messages: { | ||
/** SSLRequest message requests SSL from a remote host */ | ||
sslRequest: IClientMessage<{}>; | ||
/** ReadyForQuery message informs the frontend that it can safely send a new command. */ | ||
@@ -35,0 +37,0 @@ readyForQuery: IServerMessage<{ |
@@ -21,2 +21,12 @@ "use strict"; | ||
exports.messages = { | ||
/** SSLRequest message requests SSL from a remote host */ | ||
sslRequest: { | ||
name: 'SSLRequest', | ||
type: 'CLIENT', | ||
indicator: null, | ||
pattern: () => [ | ||
// The SSL request code. | ||
helpers_1.int32(80877103), | ||
], | ||
}, | ||
/** ReadyForQuery message informs the frontend that it can safely send a new command. */ | ||
@@ -23,0 +33,0 @@ readyForQuery: { |
/// <reference types="node" /> | ||
import * as net from 'net'; | ||
import * as tls from 'tls'; | ||
import { ParseResult } from './protocol'; | ||
@@ -22,2 +23,3 @@ import { IClientMessage, IServerMessage } from './messages'; | ||
host: string; | ||
ssl?: tls.ConnectionOptions | boolean; | ||
}): Promise<void>; | ||
@@ -28,7 +30,7 @@ send<Params extends object>(message: IClientMessage<Params>, params: Params): Promise<void>; | ||
* Waits for the next message to arrive and parses it, resolving with the parsed value. | ||
* @param messages The message type to parse or an array of messages to match any of them | ||
* @param serverMessages The message type to parse or an array of messages to match any of them | ||
* @returns The parsed params | ||
*/ | ||
reply<Messages extends Array<IServerMessage<any>>>(...messages: Messages): Promise<Boxified<Messages>[number]>; | ||
reply<Messages extends Array<IServerMessage<any>>>(...serverMessages: Messages): Promise<Boxified<Messages>[number]>; | ||
} | ||
export {}; |
@@ -11,2 +11,13 @@ "use strict"; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
@@ -24,3 +35,5 @@ if (mod && mod.__esModule) return mod; | ||
const net = __importStar(require("net")); | ||
const tls = __importStar(require("tls")); | ||
const protocol_1 = require("./protocol"); | ||
const messages_1 = require("./messages"); | ||
const debug_1 = __importDefault(require("debug")); | ||
@@ -34,15 +47,59 @@ const debug = debug_1.default('pg-wire:socket'); | ||
this.socket = new net.Socket({}); | ||
this.socket.on('data', (buffer) => { | ||
debug('received %o bytes', buffer.length); | ||
this.queue.push(buffer); | ||
this.processQueue(); | ||
}); | ||
} | ||
connect(passedOptions) { | ||
const { ssl } = passedOptions, connectOptions = __rest(passedOptions, ["ssl"]); | ||
const sslEnabled = ssl === true || ssl != null; | ||
const attachDataListener = () => { | ||
this.socket.on('data', (buffer) => { | ||
debug('received %o bytes', buffer.length); | ||
this.queue.push(buffer); | ||
this.processQueue(); | ||
}); | ||
}; | ||
return new Promise((resolve) => { | ||
this.socket.on('connect', () => { | ||
debug('socket connected'); | ||
resolve(); | ||
if (sslEnabled) { | ||
this.send(messages_1.messages.sslRequest, {}); | ||
} | ||
else { | ||
attachDataListener(); | ||
resolve(); | ||
} | ||
}); | ||
this.socket.connect(passedOptions); | ||
if (sslEnabled) { | ||
this.socket.once('data', (buffer) => { | ||
const responseCode = buffer.toString('utf8'); | ||
switch (responseCode) { | ||
case 'S': | ||
break; | ||
case 'N': | ||
this.socket.end(); | ||
throw new Error('The server does not support SSL connections'); | ||
default: | ||
this.socket.end(); | ||
throw new Error('There was an error establishing an SSL connection'); | ||
} | ||
const options = { | ||
socket: this.socket, | ||
}; | ||
if (ssl !== true) { | ||
Object.assign(options, ssl); | ||
} | ||
if (net.isIP(connectOptions.host) === 0) { | ||
options.servername = connectOptions.host; | ||
} | ||
try { | ||
this.socket = tls.connect(options); | ||
} | ||
catch (err) { | ||
debug('ssl error', err); | ||
this.socket.end(); | ||
throw new Error('There was an error establishing an SSL connection'); | ||
} | ||
attachDataListener(); | ||
resolve(); | ||
}); | ||
} | ||
this.socket.connect(connectOptions); | ||
}); | ||
@@ -87,13 +144,13 @@ } | ||
* Waits for the next message to arrive and parses it, resolving with the parsed value. | ||
* @param messages The message type to parse or an array of messages to match any of them | ||
* @param serverMessages The message type to parse or an array of messages to match any of them | ||
* @returns The parsed params | ||
*/ | ||
reply(...messages) { | ||
reply(...serverMessages) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let parser; | ||
if (messages instanceof Array) { | ||
parser = (buf, offset) => protocol_1.parseOneOf(messages, buf, offset); | ||
if (serverMessages instanceof Array) { | ||
parser = (buf, offset) => protocol_1.parseOneOf(serverMessages, buf, offset); | ||
} | ||
else { | ||
parser = (buf, offset) => protocol_1.parseMessage(messages, buf, offset); | ||
parser = (buf, offset) => protocol_1.parseMessage(serverMessages, buf, offset); | ||
} | ||
@@ -100,0 +157,0 @@ return new Promise((resolve, reject) => { |
{ | ||
"name": "@pgtyped/wire", | ||
"version": "0.9.0", | ||
"version": "0.10.1", | ||
"main": "lib/index.js", | ||
@@ -26,3 +26,3 @@ "types": "lib/index.d.ts", | ||
}, | ||
"gitHead": "ef0a980b5ef6f414129de066bee2e02553fd457e" | ||
"gitHead": "0b90a730754d9caac31b401b68c3ed16e8ff0f1e" | ||
} |
@@ -55,2 +55,12 @@ import { | ||
export const messages = { | ||
/** SSLRequest message requests SSL from a remote host */ | ||
sslRequest: { | ||
name: 'SSLRequest', | ||
type: 'CLIENT', | ||
indicator: null, | ||
pattern: () => [ | ||
// The SSL request code. | ||
int32(80877103), | ||
], | ||
} as IClientMessage<{}>, | ||
/** ReadyForQuery message informs the frontend that it can safely send a new command. */ | ||
@@ -57,0 +67,0 @@ readyForQuery: { |
import * as net from 'net'; | ||
import * as util from 'util'; | ||
import * as tls from 'tls'; | ||
@@ -11,3 +12,3 @@ import { | ||
import { IClientMessage, TMessage, IServerMessage } from './messages'; | ||
import { IClientMessage, TMessage, IServerMessage, messages } from './messages'; | ||
@@ -31,15 +32,76 @@ import debugBase from 'debug'; | ||
this.socket = new net.Socket({}); | ||
this.socket.on('data', (buffer: Buffer) => { | ||
debug('received %o bytes', buffer.length); | ||
this.queue.push(buffer); | ||
this.processQueue(); | ||
}); | ||
} | ||
public connect(passedOptions: { port: number; host: string }): Promise<void> { | ||
public connect(passedOptions: { | ||
port: number; | ||
host: string; | ||
ssl?: tls.ConnectionOptions | boolean; | ||
}): Promise<void> { | ||
const { ssl, ...connectOptions } = passedOptions; | ||
const sslEnabled = ssl === true || ssl != null; | ||
const attachDataListener = () => { | ||
this.socket.on('data', (buffer: Buffer) => { | ||
debug('received %o bytes', buffer.length); | ||
this.queue.push(buffer); | ||
this.processQueue(); | ||
}); | ||
}; | ||
return new Promise((resolve) => { | ||
this.socket.on('connect', () => { | ||
debug('socket connected'); | ||
resolve(); | ||
if (sslEnabled) { | ||
this.send(messages.sslRequest, {}); | ||
} else { | ||
attachDataListener(); | ||
resolve(); | ||
} | ||
}); | ||
this.socket.connect(passedOptions); | ||
if (sslEnabled) { | ||
this.socket.once('data', (buffer) => { | ||
const responseCode = buffer.toString('utf8'); | ||
switch (responseCode) { | ||
case 'S': | ||
break; | ||
case 'N': | ||
this.socket.end(); | ||
throw new Error('The server does not support SSL connections'); | ||
default: | ||
this.socket.end(); | ||
throw new Error( | ||
'There was an error establishing an SSL connection', | ||
); | ||
} | ||
const options: tls.ConnectionOptions = { | ||
socket: this.socket, | ||
}; | ||
if (ssl !== true) { | ||
Object.assign(options, ssl); | ||
} | ||
if (net.isIP(connectOptions.host) === 0) { | ||
options.servername = connectOptions.host; | ||
} | ||
try { | ||
this.socket = tls.connect(options); | ||
} catch (err) { | ||
debug('ssl error', err); | ||
this.socket.end(); | ||
throw new Error( | ||
'There was an error establishing an SSL connection', | ||
); | ||
} | ||
attachDataListener(); | ||
resolve(); | ||
}); | ||
} | ||
this.socket.connect(connectOptions); | ||
}); | ||
@@ -84,15 +146,15 @@ } | ||
* Waits for the next message to arrive and parses it, resolving with the parsed value. | ||
* @param messages The message type to parse or an array of messages to match any of them | ||
* @param serverMessages The message type to parse or an array of messages to match any of them | ||
* @returns The parsed params | ||
*/ | ||
public async reply<Messages extends Array<IServerMessage<any>>>( | ||
...messages: Messages | ||
...serverMessages: Messages | ||
): Promise<Boxified<Messages>[number]> { | ||
let parser: (buf: Buffer, offset: number) => ParseResult<object>; | ||
if (messages instanceof Array) { | ||
if (serverMessages instanceof Array) { | ||
parser = (buf: Buffer, offset: number) => | ||
parseOneOf(messages, buf, offset); | ||
parseOneOf(serverMessages, buf, offset); | ||
} else { | ||
parser = (buf: Buffer, offset: number) => | ||
parseMessage(messages, buf, offset); | ||
parseMessage(serverMessages, buf, offset); | ||
} | ||
@@ -99,0 +161,0 @@ return new Promise((resolve, reject) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
98103
6.84%2647
5.25%2
100%