From f18a8b1e6a7aa8768b52910fcd0e4fbd18235166 Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Wed, 14 Aug 2024 13:15:46 -0500 Subject: [PATCH 1/4] Use node: prefix for stdlib imports --- src/client.ts | 9 +++++---- src/helpers.ts | 6 +++--- src/sniffingTransport.ts | 2 +- test/integration/helper.js | 2 +- test/integration/helpers/bulk.test.js | 4 ++-- test/integration/helpers/msearch.test.js | 4 ++-- test/integration/helpers/scroll.test.js | 4 ++-- test/integration/helpers/search.test.js | 4 ++-- test/integration/reporter.js | 2 +- test/unit/helpers/bulk.test.ts | 22 +++++++++++----------- test/unit/helpers/search.test.ts | 3 ++- test/utils/buildCluster.ts | 2 +- 12 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/client.ts b/src/client.ts index 12a88cc24..12725acb2 100644 --- a/src/client.ts +++ b/src/client.ts @@ -17,10 +17,11 @@ * under the License. */ -import { ConnectionOptions as TlsConnectionOptions } from 'tls' -import { URL } from 'url' -import buffer from 'buffer' -import os from 'os' +import process from 'node:process' +import { ConnectionOptions as TlsConnectionOptions } from 'node:tls' +import { URL } from 'node:url' +import buffer from 'node:buffer' +import os from 'node:os' import { Transport, UndiciConnection, diff --git a/src/helpers.ts b/src/helpers.ts index 39b1d6bba..e84f699f3 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -21,9 +21,9 @@ /* eslint-disable @typescript-eslint/promise-function-async */ /* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */ -import assert from 'assert' -import { promisify } from 'util' -import { Readable } from 'stream' +import assert from 'node:assert' +import { promisify } from 'node:util' +import { Readable } from 'node:stream' import { errors, TransportResult, TransportRequestOptions, TransportRequestOptionsWithMeta } from '@elastic/transport' import Client from './client' import * as T from './api/types' diff --git a/src/sniffingTransport.ts b/src/sniffingTransport.ts index 4b02038fb..7c9cec43c 100644 --- a/src/sniffingTransport.ts +++ b/src/sniffingTransport.ts @@ -17,7 +17,7 @@ * under the License. */ -import assert from 'assert' +import assert from 'node:assert' import { Transport, SniffOptions } from '@elastic/transport' export default class SniffingTransport extends Transport { diff --git a/test/integration/helper.js b/test/integration/helper.js index d58252580..fe4e0b422 100644 --- a/test/integration/helper.js +++ b/test/integration/helper.js @@ -19,7 +19,7 @@ 'use strict' -const assert = require('assert') +const assert = require('node:assert') const fetch = require('node-fetch') function runInParallel (client, operation, options, clientOptions) { diff --git a/test/integration/helpers/bulk.test.js b/test/integration/helpers/bulk.test.js index 011f524c3..a1b2be118 100644 --- a/test/integration/helpers/bulk.test.js +++ b/test/integration/helpers/bulk.test.js @@ -19,8 +19,8 @@ 'use strict' -const { createReadStream } = require('fs') -const { join } = require('path') +const { createReadStream } = require('node:fs') +const { join } = require('node:path') const split = require('split2') const { test, beforeEach, afterEach } = require('tap') const { waitCluster } = require('../../utils') diff --git a/test/integration/helpers/msearch.test.js b/test/integration/helpers/msearch.test.js index c9c726ecc..fb317b0f7 100644 --- a/test/integration/helpers/msearch.test.js +++ b/test/integration/helpers/msearch.test.js @@ -19,8 +19,8 @@ 'use strict' -const { createReadStream } = require('fs') -const { join } = require('path') +const { createReadStream } = require('node:fs') +const { join } = require('node:path') const split = require('split2') const { test, beforeEach, afterEach } = require('tap') const { waitCluster } = require('../../utils') diff --git a/test/integration/helpers/scroll.test.js b/test/integration/helpers/scroll.test.js index e197ce21a..36f3b8528 100644 --- a/test/integration/helpers/scroll.test.js +++ b/test/integration/helpers/scroll.test.js @@ -19,8 +19,8 @@ 'use strict' -const { createReadStream } = require('fs') -const { join } = require('path') +const { createReadStream } = require('node:fs') +const { join } = require('node:path') const split = require('split2') const { test, beforeEach, afterEach } = require('tap') const { waitCluster } = require('../../utils') diff --git a/test/integration/helpers/search.test.js b/test/integration/helpers/search.test.js index d4aa57c9a..7a6946a9f 100644 --- a/test/integration/helpers/search.test.js +++ b/test/integration/helpers/search.test.js @@ -19,8 +19,8 @@ 'use strict' -const { createReadStream } = require('fs') -const { join } = require('path') +const { createReadStream } = require('node:fs') +const { join } = require('node:path') const split = require('split2') const { test, beforeEach, afterEach } = require('tap') const { waitCluster } = require('../../utils') diff --git a/test/integration/reporter.js b/test/integration/reporter.js index 5db288b8e..d94e09ba3 100644 --- a/test/integration/reporter.js +++ b/test/integration/reporter.js @@ -1,6 +1,6 @@ 'use strict' -const assert = require('assert') +const assert = require('node:assert') const { create } = require('xmlbuilder2') function createJunitReporter () { diff --git a/test/unit/helpers/bulk.test.ts b/test/unit/helpers/bulk.test.ts index 0a15c3fc6..1f2ddf575 100644 --- a/test/unit/helpers/bulk.test.ts +++ b/test/unit/helpers/bulk.test.ts @@ -18,12 +18,12 @@ */ import FakeTimers from '@sinonjs/fake-timers' -import { AssertionError } from 'assert' -import { createReadStream } from 'fs' -import * as http from 'http' -import { join } from 'path' +import { AssertionError } from 'node:assert' +import { createReadStream } from 'node:fs' +import * as http from 'node:http' +import { join } from 'node:path' import split from 'split2' -import { Readable } from 'stream' +import { Readable } from 'node:stream' import { test } from 'tap' import { Client, errors } from '../../../' import { buildServer, connection } from '../../utils' @@ -936,11 +936,11 @@ test('bulk index', t => { onDocument (doc) { t.type(doc.user, 'string') // testing that doc is type of Document return [ - { - index: { - _index: 'test' - } - }, + { + index: { + _index: 'test' + } + }, { ...doc, updatedAt } ] }, @@ -1042,7 +1042,7 @@ test('bulk create', t => { _index: 'test', _id: String(id++) } - }, + }, { ...doc, updatedAt } ] }, diff --git a/test/unit/helpers/search.test.ts b/test/unit/helpers/search.test.ts index 697237ef3..9ed4605ab 100644 --- a/test/unit/helpers/search.test.ts +++ b/test/unit/helpers/search.test.ts @@ -108,4 +108,5 @@ test('Merge filter paths (snake_case)', async t => { { two: 'two' }, { three: 'three' } ]) -}) \ No newline at end of file +}) + diff --git a/test/utils/buildCluster.ts b/test/utils/buildCluster.ts index 608fcc268..79a8ba71b 100644 --- a/test/utils/buildCluster.ts +++ b/test/utils/buildCluster.ts @@ -18,7 +18,7 @@ */ import Debug from 'debug' -import * as http from 'http' +import * as http from 'node:http' import buildServer, { ServerHandler } from './buildServer' import { StoppableServer } from 'stoppable' From d040c9a926f18538aea79f56696504757504b071 Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Tue, 20 Aug 2024 15:37:42 -0500 Subject: [PATCH 2/4] Make code more ESM-friendly --- package.json | 9 ++++++--- src/client.ts | 6 ++++-- src/helpers.ts | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 0eb974659..f1b2a83f2 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,12 @@ "version": "8.15.0", "versionCanary": "8.15.0-canary.0", "description": "The official Elasticsearch client for Node.js", - "main": "index.js", + "main": "./index.js", "types": "index.d.ts", - "type": "commonjs", + "exports": { + "require": "./index.js", + "import": "./index.mjs" + }, "scripts": { "test": "npm run build && npm run lint && tap test/unit/{*,**/*}.test.ts", "test:unit": "npm run build && tap test/unit/{*,**/*}.test.ts", @@ -17,7 +20,7 @@ "lint:fix": "ts-standard --fix src", "license-checker": "license-checker --production --onlyAllow='MIT;Apache-2.0;Apache1.1;ISC;BSD-3-Clause;BSD-2-Clause;0BSD'", "prebuild": "npm run clean-build && npm run lint", - "build": "tsc", + "build": "tsc && rm lib/package.json && mv lib/src/* lib/ && rm -rf lib/src", "clean-build": "rimraf ./lib && mkdir lib", "prepublishOnly": "npm run build" }, diff --git a/src/client.ts b/src/client.ts index 12725acb2..068f3573b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -49,16 +49,18 @@ import BaseConnection, { prepareHeaders, ConnectionOptions } from '@elastic/tran import SniffingTransport from './sniffingTransport' import Helpers from './helpers' import API from './api' +import packageJson from '../package.json' +import transportPackageJson from '@elastic/transport/package.json' const kChild = Symbol('elasticsearchjs-child') const kInitialOptions = Symbol('elasticsearchjs-initial-options') -let clientVersion: string = require('../package.json').version // eslint-disable-line +let clientVersion: string = packageJson.version /* istanbul ignore next */ if (clientVersion.includes('-')) { // clean prerelease clientVersion = clientVersion.slice(0, clientVersion.indexOf('-')) + 'p' } -let transportVersion: string = require('@elastic/transport/package.json').version // eslint-disable-line +let transportVersion: string = transportPackageJson.version // eslint-disable-line /* istanbul ignore next */ if (transportVersion.includes('-')) { // clean prerelease diff --git a/src/helpers.ts b/src/helpers.ts index e84f699f3..62040083a 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -22,7 +22,7 @@ /* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */ import assert from 'node:assert' -import { promisify } from 'node:util' +import * as timersPromises from 'node:timers/promises' import { Readable } from 'node:stream' import { errors, TransportResult, TransportRequestOptions, TransportRequestOptionsWithMeta } from '@elastic/transport' import Client from './client' @@ -163,8 +163,8 @@ export interface EsqlToRecords { } const { ResponseError, ConfigurationError } = errors -const sleep = promisify(setTimeout) -const pImmediate = promisify(setImmediate) +const sleep = timersPromises.setTimeout +const pImmediate = timersPromises.setImmediate /* istanbul ignore next */ const noop = (): void => {} const kClient = Symbol('elasticsearch-client') From f86dc516fd918ff61cf471052ea3aefb733e8d2a Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Thu, 22 Aug 2024 10:38:20 -0500 Subject: [PATCH 3/4] Add missing mjs file --- index.mjs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 index.mjs diff --git a/index.mjs b/index.mjs new file mode 100644 index 000000000..f9362a348 --- /dev/null +++ b/index.mjs @@ -0,0 +1,53 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you 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 + * + * http://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. + */ + +import { + Diagnostic, + Transport, + WeightedConnectionPool, + ClusterConnectionPool, + BaseConnectionPool, + CloudConnectionPool, + BaseConnection, + HttpConnection, + UndiciConnection, + Serializer, + errors, + events +} from '@elastic/transport' + +import * as Client from './lib/client.js' +import * as SniffingTransport from './lib/sniffingTransport.js' + +export default { + Client, + SniffingTransport, + Diagnostic, + Transport, + WeightedConnectionPool, + ClusterConnectionPool, + BaseConnectionPool, + CloudConnectionPool, + BaseConnection, + HttpConnection, + UndiciConnection, + Serializer, + errors, + events +} From 463b64ecb0a578af2c2bacad1527dec6246fb734 Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Thu, 22 Aug 2024 10:41:07 -0500 Subject: [PATCH 4/4] Drop mjs file from package.json --- index.mjs | 53 ---------------------------------------------------- package.json | 3 +-- 2 files changed, 1 insertion(+), 55 deletions(-) delete mode 100644 index.mjs diff --git a/index.mjs b/index.mjs deleted file mode 100644 index f9362a348..000000000 --- a/index.mjs +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you 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 - * - * http://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. - */ - -import { - Diagnostic, - Transport, - WeightedConnectionPool, - ClusterConnectionPool, - BaseConnectionPool, - CloudConnectionPool, - BaseConnection, - HttpConnection, - UndiciConnection, - Serializer, - errors, - events -} from '@elastic/transport' - -import * as Client from './lib/client.js' -import * as SniffingTransport from './lib/sniffingTransport.js' - -export default { - Client, - SniffingTransport, - Diagnostic, - Transport, - WeightedConnectionPool, - ClusterConnectionPool, - BaseConnectionPool, - CloudConnectionPool, - BaseConnection, - HttpConnection, - UndiciConnection, - Serializer, - errors, - events -} diff --git a/package.json b/package.json index f1b2a83f2..520ed4b04 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,7 @@ "main": "./index.js", "types": "index.d.ts", "exports": { - "require": "./index.js", - "import": "./index.mjs" + "require": "./index.js" }, "scripts": { "test": "npm run build && npm run lint && tap test/unit/{*,**/*}.test.ts",