Make client more ESM-friendly (#2348)

* Use node: prefix for stdlib imports

* Make code more ESM-friendly

* Add missing mjs file

* Drop mjs file from package.json
This commit is contained in:
Josh Mock
2024-08-22 11:18:31 -05:00
committed by GitHub
parent 715292b501
commit bf4c57f7bc
13 changed files with 44 additions and 38 deletions

View File

@ -3,9 +3,11 @@
"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"
},
"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 +19,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"
},

View File

@ -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,
@ -48,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

View File

@ -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 * as timersPromises from 'node:timers/promises'
import { Readable } from 'node:stream'
import { errors, TransportResult, TransportRequestOptions, TransportRequestOptionsWithMeta } from '@elastic/transport'
import Client from './client'
import * as T from './api/types'
@ -163,8 +163,8 @@ export interface EsqlToRecords<TDocument> {
}
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')

View File

@ -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 {

View File

@ -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) {

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -1,6 +1,6 @@
'use strict'
const assert = require('assert')
const assert = require('node:assert')
const { create } = require('xmlbuilder2')
function createJunitReporter () {

View File

@ -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 }
]
},

View File

@ -108,4 +108,5 @@ test('Merge filter paths (snake_case)', async t => {
{ two: 'two' },
{ three: 'three' }
])
})
})

View File

@ -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'