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

eslint: enable recommended ruleset #3263

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
{
"plugins": ["@typescript-eslint", "prettier"],
"parser": "@typescript-eslint/parser",
"extends": ["plugin:prettier/recommended", "prettier"],
"extends": ["eslint:recommended", "plugin:prettier/recommended", "prettier"],
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*", "packages/pg-query-stream/dist/**/*"],
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"env": {
"node": true,
"es6": true,
"es2020": true,
"mocha": true
},
"rules": {
"@typescript-eslint/no-unused-vars": ["error", {
"args": "none"
}],
"no-unused-vars": "off"
}
"no-unused-vars": "off",
"no-unreachable": "error"
},
"overrides": [
{
"files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
"rules": {
"no-undef": "off"
}
}
]
}
1 change: 1 addition & 0 deletions packages/pg-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class CloudflareSocket extends EventEmitter {
}

async _listen() {
// eslint-disable-next-line no-constant-condition
while (true) {
log('awaiting receive from CF socket')
const { done, value } = await this._cfReader!.read()
Expand Down
2 changes: 1 addition & 1 deletion packages/pg-connection-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function parse(str) {
let dummyHost = false
if (/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) {
// Ensure spaces are encoded as %20
str = encodeURI(str).replace(/\%25(\d\d)/g, '%$1')
str = encodeURI(str).replace(/%25(\d\d)/g, '%$1')
}

try {
Expand Down
6 changes: 4 additions & 2 deletions packages/pg-native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ Client.prototype._emitResult = function (pq) {
case 'PGRES_TUPLES_OK':
case 'PGRES_COMMAND_OK':
case 'PGRES_EMPTY_QUERY':
const result = this._consumeQueryResults(this.pq)
this.emit('result', result)
{
const result = this._consumeQueryResults(this.pq)
this.emit('result', result)
}
break

case 'PGRES_COPY_OUT':
Expand Down
1 change: 0 additions & 1 deletion packages/pg-pool/test/idle-timeout-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ if (module === require.main) {
pool.query('SELECT NOW()', (err, res) => console.log('completed first'))
pool.on('remove', () => {
console.log('removed')
done()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven’t really looked at this, but there’s probably more to do here than quietly deleting a line. Does none of this code run at all, then?

})

setTimeout(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/pg-protocol/src/buffer-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class BufferReader {
public cstring(): string {
const start = this.offset
let end = start
// eslint-disable-next-line no-empty
while (this.buffer[end++] !== 0) {}
this.offset = end
return this.buffer.toString(this.encoding, start, end - 1)
Expand Down
8 changes: 0 additions & 8 deletions packages/pg-protocol/src/inbound-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,8 @@ var rowWithBigOids = {
}
var bigOidDescBuff = buffers.rowDescription([rowWithBigOids])

var emptyRowFieldBuf = new BufferList().addInt16(0).join(true, 'D')

var emptyRowFieldBuf = buffers.dataRow([])

var oneFieldBuf = new BufferList()
.addInt16(1) // number of fields
.addInt32(5) // length of bytes of fields
.addCString('test')
.join(true, 'D')

var oneFieldBuf = buffers.dataRow(['test'])

var expectedAuthenticationOkayMessage = {
Expand Down
21 changes: 11 additions & 10 deletions packages/pg-protocol/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,17 @@ export class Parser {
}
break
case 10: // AuthenticationSASL
message.name = 'authenticationSASL'
message.mechanisms = []
let mechanism: string
do {
mechanism = this.reader.cstring()

if (mechanism) {
message.mechanisms.push(mechanism)
}
} while (mechanism)
{
message.name = 'authenticationSASL'
message.mechanisms = []
let mechanism: string
do {
mechanism = this.reader.cstring()
if (mechanism) {
message.mechanisms.push(mechanism)
}
} while (mechanism)
}
break
case 11: // AuthenticationSASLContinue
message.name = 'authenticationSASLContinue'
Expand Down
4 changes: 2 additions & 2 deletions packages/pg-query-stream/test/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('error recovery', () => {
const client = new Client()
const stmt = 'SELECT * FROM goose;'
await client.connect()
return new Promise(async (resolve) => {
return new Promise((resolve) => {
let queryError: Error | undefined
client.query(stmt).catch((e) => {
queryError = e
Expand All @@ -86,7 +86,7 @@ describe('error recovery', () => {
assert(queryError, 'query should have errored due to client ending')
resolve()
})
await client.end()
client.end()
})
})

Expand Down
8 changes: 3 additions & 5 deletions packages/pg/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ const exec = async (client, q) => {
const bench = async (client, q, time) => {
let start = Date.now()
let count = 0
while (true) {
do {
await exec(client, q)
count++
if (Date.now() - start > time) {
return count
}
}
} while (Date.now() - start > time)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here has been inverted.

return count
}

const run = async () => {
Expand Down
21 changes: 11 additions & 10 deletions packages/pg/lib/crypto/cert-signatures.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function x509Error(msg, cert) {
throw new Error('SASL channel binding: ' + msg + ' when parsing public certificate ' + cert.toString('base64'))
return new Error('SASL channel binding: ' + msg + ' when parsing public certificate ' + cert.toString('base64'))
}

function readASN1Length(data, index) {
let length = data[index++]
if (length < 0x80) return { length, index }

const lengthBytes = length & 0x7f
if (lengthBytes > 4) x509Error('bad length', data)
if (lengthBytes > 4) throw x509Error('bad length', data)

length = 0
for (let i = 0; i < lengthBytes; i++) {
Expand All @@ -18,11 +18,11 @@ function readASN1Length(data, index) {
}

function readASN1OID(data, index) {
if (data[index++] !== 0x6) x509Error('non-OID data', data) // 6 = OID
if (data[index++] !== 0x6) throw x509Error('non-OID data', data) // 6 = OID

const { length: OIDLength, index: indexAfterOIDLength } = readASN1Length(data, index)
index = indexAfterOIDLength
lastIndex = index + OIDLength
let lastIndex = index + OIDLength
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes! Good catch.


const byte1 = data[index++]
let oid = ((byte1 / 40) >> 0) + '.' + (byte1 % 40)
Expand All @@ -43,7 +43,7 @@ function readASN1OID(data, index) {
}

function expectASN1Seq(data, index) {
if (data[index++] !== 0x30) x509Error('non-sequence data', data) // 30 = Sequence
if (data[index++] !== 0x30) throw x509Error('non-sequence data', data) // 30 = Sequence
return readASN1Length(data, index)
}

Expand Down Expand Up @@ -85,10 +85,10 @@ function signatureAlgorithmHashFromCertificate(data, index) {
case '1.2.840.10045.4.3.4':
return 'SHA-512'
// RSASSA-PSS: hash is indicated separately
case '1.2.840.113549.1.1.10':
case '1.2.840.113549.1.1.10': {
index = indexAfterOID
index = expectASN1Seq(data, index).index
if (data[index++] !== 0xa0) x509Error('non-tag data', data) // a0 = constructed tag 0
if (data[index++] !== 0xa0) throw x509Error('non-tag data', data) // a0 = constructed tag 0
index = readASN1Length(data, index).index // skip over tag length field
index = expectASN1Seq(data, index).index // skip over sequence length field
const { oid: hashOID } = readASN1OID(data, index)
Expand All @@ -105,17 +105,18 @@ function signatureAlgorithmHashFromCertificate(data, index) {
case '2.16.840.1.101.3.4.2.3':
return 'SHA-512'
}
x509Error('unknown hash OID ' + hashOID, data)
throw x509Error('unknown hash OID ' + hashOID, data)
}
// Ed25519 -- see https: return//github.com/openssl/openssl/issues/15477
case '1.3.101.110':
case '1.3.101.112': // ph
return 'SHA-512'
// Ed448 -- still not in pg 17.2 (if supported, digest would be SHAKE256 x 64 bytes)
case '1.3.101.111':
case '1.3.101.113': // ph
x509Error('Ed448 certificate channel binding is not currently supported by Postgres')
throw x509Error('Ed448 certificate channel binding is not currently supported by Postgres')
}
x509Error('unknown OID ' + oid, data)
throw x509Error('unknown OID ' + oid, data)
}

module.exports = { signatureAlgorithmHashFromCertificate }
1 change: 1 addition & 0 deletions packages/pg/lib/native/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// eslint-disable-next-line
var Native
// eslint-disable-next-line no-useless-catch
try {
// Wrap this `require()` in a try-catch to avoid upstream bundlers from complaining that this might not be available since it is an optional import
Native = require('pg-native')
Expand Down
8 changes: 6 additions & 2 deletions packages/pg/lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ function getCloudflareStreamFuncs() {
function isCloudflareRuntime() {
// Since 2022-03-21 the `global_navigator` compatibility flag is on for Cloudflare Workers
// which means that `navigator.userAgent` will be defined.
if (typeof navigator === 'object' && navigator !== null && typeof navigator.userAgent === 'string') {
return navigator.userAgent === 'Cloudflare-Workers'
if (
typeof globalThis.navigator === 'object' &&
globalThis.navigator !== null &&
typeof globalThis.navigator.userAgent === 'string'
) {
return globalThis.navigator.userAgent === 'Cloudflare-Workers'
}
// In case `navigator` or `navigator.userAgent` is not defined then try a more sneaky approach
if (typeof Response === 'function') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var runBigQuery = function (client) {
function (err, result) {
if (err != null) {
console.log(err)
throw Err
throw err
}
assert.lengthIs(result.rows, 26)
}
Expand Down
1 change: 1 addition & 0 deletions packages/pg/test/integration/gh-issues/3174-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const startMockServer = (port, badBuffer, callback) => {
setImmediate(() => {
socket.write(badBuffer)
})
break
default:
// console.log('got code', code)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pg/test/unit/client/sasl-scram-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ suite.test('sasl/scram', function () {
0x0d, // signature algorithm length
0x06, // ASN.1 OID
0x09, // OID length
0x2a, // OID: 1.2.840.113549.1.1.11 (RSASSA-PKCS1-v1_5 / SHA-256)
0x2a, // OID: 1.2.840.113549.1.1.11 (RSASSA-PKCS1-v1_5 / SHA-256)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(note for other readers: the difference is a zero-width space after “SHA-256”)

0x86,
0x48,
0x86,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var defaults = require('../../../lib').defaults

// clear process.env
var realEnv = {}
for (var key in process.env) {
for (const key in process.env) {
realEnv[key] = process.env[key]
delete process.env[key]
}
Expand Down Expand Up @@ -122,6 +122,6 @@ testVal('verify-full', true)
testVal('no-verify', { rejectUnauthorized: false })

// restore process.env
for (var key in realEnv) {
for (const key in realEnv) {
process.env[key] = realEnv[key]
}