From 619fba7bc1ebb26e09c1a6d9ee7b09848ad6e0d7 Mon Sep 17 00:00:00 2001 From: jackwotherspoon Date: Mon, 16 Dec 2024 15:39:33 +0000 Subject: [PATCH] chore: cleanup node14 unneeded code --- .npmrc | 1 - package.json | 2 - scripts/tap16-adapter.js | 27 -------- src/crypto.ts | 87 ++++++-------------------- system-test/tedious-connect.cjs | 105 +++++++++++++++---------------- system-test/tedious-connect.mjs | 91 +++++++++++++-------------- system-test/tedious-connect.ts | 107 +++++++++++++++----------------- 7 files changed, 160 insertions(+), 260 deletions(-) delete mode 100644 .npmrc delete mode 100644 scripts/tap16-adapter.js diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 4812751a..00000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -lockfile-version=2 diff --git a/package.json b/package.json index fa22299c..bdd656f0 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,6 @@ "@sequelize/core": "^7.0.0-alpha.29", "@types/node": "^22.0.0", "@types/pg": "^8.10.1", - "@types/semver": "^7.5.0", "@types/tap": "^18.0.0", "@types/tedious": "^4.0.9", "@typescript-eslint/eslint-plugin": "^7.0.0", @@ -71,7 +70,6 @@ "mysql2": "^3.2.0", "nock": "^13.3.0", "pg": "^8.10.0", - "semver": "^7.5.1", "tap": "^21.0.0", "tedious": "^16.1.0", "typeorm": "^0.3.19", diff --git a/scripts/tap16-adapter.js b/scripts/tap16-adapter.js deleted file mode 100644 index 565d4de8..00000000 --- a/scripts/tap16-adapter.js +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2023 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Test adapter used to bridge test files to tap16-compatible -// APIs in order to enable running tests in node@14 -// This file (and its usage in .github/tests.yml) can be safely -// removed once node14 is no longer supported -const originalJsHandler = require.extensions['.js'] -require.extensions['.js'] = function(module, filename) { - const compile = module._compile - module._compile = function (code, filename) { - const source = code.replace(/t\.mockRequire/g, 't.mock') - return compile.call(module, source, filename) - } - originalJsHandler(module, filename) -} diff --git a/src/crypto.ts b/src/crypto.ts index 751ed2ed..c459f5dd 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -18,54 +18,6 @@ import {SslCert} from './ssl-cert'; import {cryptoModule} from './node-crypto'; import {CloudSQLConnectorError} from './errors'; -// The following is a fallback certificate parser for node14 to work around -// its lack of support to the X509Certificate class parser, this block of code -// can be safely removed once node14 is no longer supported, along with any -// `node14ParseCert` call and its unit tests. -// --- node@14 cert parse fallback start -import net from 'node:net'; -import tls from 'node:tls'; - -const node14ParseCert = (cert: string): SslCert => { - const isPeerCertificate = ( - obj: object | tls.PeerCertificate - ): obj is tls.PeerCertificate => - (obj as tls.PeerCertificate).valid_to !== undefined; - - let socket; - let parsed; - try { - socket = new tls.TLSSocket(new net.Socket(), { - secureContext: tls.createSecureContext({cert}), - }); - parsed = socket.getCertificate(); - } catch (err: unknown) { - throw new CloudSQLConnectorError({ - message: 'Failed to parse as X.509 certificate.', - code: 'EPARSESQLADMINEPH', - errors: [err as Error], - }); - } - - if (parsed && isPeerCertificate(parsed)) { - const expirationTime = parsed.valid_to; - socket.destroy(); - socket = undefined; - parsed = undefined; - - return { - cert, - expirationTime, - }; - } - /* c8 ignore next 5 */ - throw new CloudSQLConnectorError({ - message: 'Could not read ephemeral certificate.', - code: 'EPARSESQLADMINEPH', - }); -}; -// --- node@14 cert parse fallback end - export async function generateKeys(): Promise { const crypto = await cryptoModule(); const keygen = promisify(crypto.generateKeyPair); @@ -90,27 +42,24 @@ export async function generateKeys(): Promise { export async function parseCert(cert: string): Promise { const {X509Certificate} = await cryptoModule(); - if (X509Certificate) { - try { - const parsed = new X509Certificate(cert); - if (parsed && parsed.validTo) { - return { - cert, - expirationTime: parsed.validTo, - }; - } - - throw new CloudSQLConnectorError({ - message: 'Could not read ephemeral certificate.', - code: 'EPARSESQLADMINEPH', - }); - } catch (err: unknown) { - throw new CloudSQLConnectorError({ - message: 'Failed to parse as X.509 certificate.', - code: 'EPARSESQLADMINEPH', - errors: [err as Error], - }); + try { + const parsed = new X509Certificate(cert); + if (parsed && parsed.validTo) { + return { + cert, + expirationTime: parsed.validTo, + }; } + + throw new CloudSQLConnectorError({ + message: 'Could not read ephemeral certificate.', + code: 'EPARSESQLADMINEPH', + }); + } catch (err: unknown) { + throw new CloudSQLConnectorError({ + message: 'Failed to parse as X.509 certificate.', + code: 'EPARSESQLADMINEPH', + errors: [err as Error], + }); } - return node14ParseCert(cert); } diff --git a/system-test/tedious-connect.cjs b/system-test/tedious-connect.cjs index 1c7881c5..f2ead835 100644 --- a/system-test/tedious-connect.cjs +++ b/system-test/tedious-connect.cjs @@ -13,69 +13,62 @@ // limitations under the License. const t = require('tap'); -const semver = require('semver'); const {Connector} = require('@google-cloud/cloud-sql-connector'); +const {Connection, Request} = require('tedious'); -t.test( - 'open connection and run basic sqlserver commands', - // the connector-supported versions of tedious do not support node14 - {skip: semver.lt(process.versions.node, '16.0.0')}, - async t => { - // lazy-load tedious here in order to allow for skipping node14 - const {Connection, Request} = require('tedious'); - const connector = new Connector(); - const clientOpts = await connector.getTediousOptions({ - instanceConnectionName: process.env.SQLSERVER_CONNECTION_NAME, - ipType: 'PUBLIC', - }); - const connection = new Connection({ - server: '0.0.0.0', - authentication: { - type: 'default', - options: { - userName: process.env.SQLSERVER_USER, - password: process.env.SQLSERVER_PASS, - }, - }, +t.test('open connection and run basic sqlserver commands', async t => { + const connector = new Connector(); + const clientOpts = await connector.getTediousOptions({ + instanceConnectionName: process.env.SQLSERVER_CONNECTION_NAME, + ipType: 'PUBLIC', + }); + const connection = new Connection({ + server: '0.0.0.0', + authentication: { + type: 'default', options: { - ...clientOpts, - port: 9999, - database: process.env.SQLSERVER_DB, + userName: process.env.SQLSERVER_USER, + password: process.env.SQLSERVER_PASS, }, - }); + }, + options: { + ...clientOpts, + port: 9999, + database: process.env.SQLSERVER_DB, + }, + }); - await new Promise((res, rej) => { - connection.connect(err => { - if (err) { - return rej(err); - } - res(); - }); + await new Promise((res, rej) => { + connection.connect(err => { + if (err) { + return rej(err); + } + res(); }); + }); - const res = await new Promise((res, rej) => { - let result; - const req = new Request('SELECT GETUTCDATE()', err => { - if (err) { - throw err; - } - }); - req.on('error', err => { - rej(err); - }); - req.on('row', columns => { - result = columns; - }); - req.on('requestCompleted', () => { - res(result); - }); - connection.execSql(req); + const res = await new Promise((res, rej) => { + let result; + const req = new Request('SELECT GETUTCDATE()', err => { + if (err) { + throw err; + } + }); + req.on('error', err => { + rej(err); + }); + req.on('row', columns => { + result = columns; + }); + req.on('requestCompleted', () => { + res(result); }); + connection.execSql(req); + }); - const [{value: utcDateResult}] = res; - t.ok(utcDateResult.getTime(), 'should have valid returned date object'); + const [{value: utcDateResult}] = res; + t.ok(utcDateResult.getTime(), 'should have valid returned date object'); - connection.close(); - connector.close(); - } -); + connection.close(); + connector.close(); +}); diff --git a/system-test/tedious-connect.mjs b/system-test/tedious-connect.mjs index 76040e41..c77c0857 100644 --- a/system-test/tedious-connect.mjs +++ b/system-test/tedious-connect.mjs @@ -13,62 +13,57 @@ // limitations under the License. import t from 'tap'; -import semver from 'semver'; -import {Connector} from '@google-cloud/cloud-sql-connector'; - +import { Connector } from '@google-cloud/cloud-sql-connector'; +import { Connection, Request } t.test( 'open connection and run basic sqlserver commands', - // the connector-supported versions of tedious do not support node14 - {skip: semver.lt(process.versions.node, '16.0.0')}, async t => { - // dynamically load tedious in order to allow for skipping node14 - const {Connection, Request} = await import('tedious'); - const connector = new Connector(); - const clientOpts = await connector.getTediousOptions({ - instanceConnectionName: process.env.SQLSERVER_CONNECTION_NAME, - ipType: 'PUBLIC' - }); - const connection = new Connection({ - server: '0.0.0.0', - authentication: { - type: 'default', + const connector = new Connector(); + const clientOpts = await connector.getTediousOptions({ + instanceConnectionName: process.env.SQLSERVER_CONNECTION_NAME, + ipType: 'PUBLIC' + }); + const connection = new Connection({ + server: '0.0.0.0', + authentication: { + type: 'default', + options: { + userName: process.env.SQLSERVER_USER, + password: process.env.SQLSERVER_PASS, + }, + }, options: { - userName: process.env.SQLSERVER_USER, - password: process.env.SQLSERVER_PASS, + ...clientOpts, + port: 9999, + database: process.env.SQLSERVER_DB, }, - }, - options: { - ...clientOpts, - port: 9999, - database: process.env.SQLSERVER_DB, - }, - }) + }) - await new Promise((res, rej) => { - connection.connect(err => { - if (err) { - return rej(err) - } - res() + await new Promise((res, rej) => { + connection.connect(err => { + if (err) { + return rej(err) + } + res() + }) }) - }) - const res = await new Promise((res, rej) => { - let result; - const req = new Request('SELECT GETUTCDATE()', (err) => { - if (err) { - throw err; - } + const res = await new Promise((res, rej) => { + let result; + const req = new Request('SELECT GETUTCDATE()', (err) => { + if (err) { + throw err; + } + }) + req.on('error', (err) => { rej(err); }); + req.on('row', (columns) => { result = columns; }); + req.on('requestCompleted', () => { res(result); }); + connection.execSql(req); }) - req.on('error', (err) => { rej(err); }); - req.on('row', (columns) => { result = columns; }); - req.on('requestCompleted', () => { res(result); }); - connection.execSql(req); - }) - const [{value: utcDateResult}] = res; - t.ok(utcDateResult.getTime(), 'should have valid returned date object'); + const [{ value: utcDateResult }] = res; + t.ok(utcDateResult.getTime(), 'should have valid returned date object'); - connection.close(); - connector.close(); -}); + connection.close(); + connector.close(); + }); diff --git a/system-test/tedious-connect.ts b/system-test/tedious-connect.ts index 759bf3b4..e789e273 100644 --- a/system-test/tedious-connect.ts +++ b/system-test/tedious-connect.ts @@ -13,70 +13,63 @@ // limitations under the License. import t from 'tap'; -import semver from 'semver'; import {Connector, IpAddressTypes} from '@google-cloud/cloud-sql-connector'; +import {Connection, Request} from 'tedious'; -t.test( - 'open connection and run basic sqlserver commands', - // the connector-supported versions of tedious do not support node14 - {skip: semver.lt(process.versions.node, '16.0.0')}, - async t => { - // dynamically load tedious in order to allow for skipping node14 - const {Connection, Request} = await import('tedious'); - const connector = new Connector(); - const clientOpts = await connector.getTediousOptions({ - instanceConnectionName: String(process.env.SQLSERVER_CONNECTION_NAME), - ipType: IpAddressTypes.PUBLIC, - }); - const connection = new Connection({ - server: '0.0.0.0', - authentication: { - type: 'default', - options: { - userName: String(process.env.SQLSERVER_USER), - password: String(process.env.SQLSERVER_PASS), - }, - }, +t.test('open connection and run basic sqlserver commands', async t => { + const connector = new Connector(); + const clientOpts = await connector.getTediousOptions({ + instanceConnectionName: String(process.env.SQLSERVER_CONNECTION_NAME), + ipType: IpAddressTypes.PUBLIC, + }); + const connection = new Connection({ + server: '0.0.0.0', + authentication: { + type: 'default', options: { - ...clientOpts, - port: 9999, - database: String(process.env.SQLSERVER_DB), + userName: String(process.env.SQLSERVER_USER), + password: String(process.env.SQLSERVER_PASS), }, - }); + }, + options: { + ...clientOpts, + port: 9999, + database: String(process.env.SQLSERVER_DB), + }, + }); - await new Promise((res, rej) => { - connection.connect(err => { - if (err) { - return rej(err); - } - res(null); - }); + await new Promise((res, rej) => { + connection.connect(err => { + if (err) { + return rej(err); + } + res(null); }); + }); - type ColumnValue = import('tedious').ColumnValue; - const res: ColumnValue[] = await new Promise((res, rej) => { - let result: ColumnValue[]; - const req = new Request('SELECT GETUTCDATE()', err => { - if (err) { - throw err; - } - }); - req.on('error', err => { - rej(err); - }); - req.on('row', columns => { - result = columns; - }); - req.on('requestCompleted', () => { - res(result); - }); - connection.execSql(req); + type ColumnValue = import('tedious').ColumnValue; + const res: ColumnValue[] = await new Promise((res, rej) => { + let result: ColumnValue[]; + const req = new Request('SELECT GETUTCDATE()', err => { + if (err) { + throw err; + } + }); + req.on('error', err => { + rej(err); + }); + req.on('row', columns => { + result = columns; + }); + req.on('requestCompleted', () => { + res(result); }); + connection.execSql(req); + }); - const [{value: utcDateResult}] = res; - t.ok(utcDateResult.getTime(), 'should have valid returned date object'); + const [{value: utcDateResult}] = res; + t.ok(utcDateResult.getTime(), 'should have valid returned date object'); - connection.close(); - connector.close(); - } -); + connection.close(); + connector.close(); +});