Skip to content

Commit

Permalink
src: convert project from CJS to ESM
Browse files Browse the repository at this point in the history
This is a substantial change that moves the release-1.x branch from
CommonJS to ESM as discussed in
kubernetes-client#2014. Please
review/test carefully.

This commit does the following things:

- Updates the tsconfig to generate ESM code. The formatter also
  automatically formatted this file.
- Add `"type": "module"` to the `package.json` so that consumers
  treat this as ESM instead of CJS.
- Updates all of the imports to be valid ESM imports. ESM requires
  that file extensions are provided. TypeScript is smart enough to
  handle the `.js` extensions, but TypeScript does not support
  adding the extensions at build time. See
  microsoft/TypeScript#16577 for more
  details about this. The `tsc-esm-fix` utility on npm was used to
  automatically add the extensions. The code generator probably
  needs to add support for this too if it doesn't already.
- Fixup the `jsonpath` imports, as this module does not have a
  default ESM export. This just means adding `* as` to the
  existing imports.
- Remove the `AbortError` export originating from `node-fetch`.
  Apparently this is not actually there.

I was able to successfully import the transpiled code using
`import * as k8s from './dist/index.js';`. This should hopefully
unblock running tests against the transpiled code as well (it
currently does not work because the openid-client and chai
dependencies have both moved to ESM only).
  • Loading branch information
cjihrig committed Dec 4, 2024
1 parent b165a02 commit 4aa97ae
Show file tree
Hide file tree
Showing 50 changed files with 170 additions and 162 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@kubernetes/client-node",
"version": "1.0.0-rc7",
"description": "NodeJS client for kubernetes",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/kubernetes-client/javascript.git"
Expand Down
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './gen';
export * from './gen/index.js';
6 changes: 3 additions & 3 deletions src/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import WebSocket from 'isomorphic-ws';
import querystring from 'node:querystring';
import stream from 'node:stream';

import { KubeConfig } from './config';
import { isResizable, ResizableStream, TerminalSizeQueue } from './terminal-size-queue';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
import { KubeConfig } from './config.js';
import { isResizable, ResizableStream, TerminalSizeQueue } from './terminal-size-queue.js';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';

export class Attach {
public 'handler': WebSocketInterface;
Expand Down
8 changes: 4 additions & 4 deletions src/attach_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';

import { CallAwaiter, matchBuffer, ResizableWriteableStreamBuffer } from '../test';
import { Attach } from './attach';
import { KubeConfig } from './config';
import { TerminalSize } from './terminal-size-queue';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
import { Attach } from './attach.js';
import { KubeConfig } from './config.js';
import { TerminalSize } from './terminal-size-queue.js';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';

describe('Attach', () => {
describe('basic', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import https from 'node:https';

import { User } from './config_types';
import { User } from './config_types.js';
import WebSocket from 'isomorphic-ws';

export interface Authenticator {
Expand Down
6 changes: 3 additions & 3 deletions src/azure_auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import proc from 'node:child_process';
import https from 'node:https';
import jsonpath from 'jsonpath-plus';
import * as jsonpath from 'jsonpath-plus';

import { Authenticator } from './auth';
import { User } from './config_types';
import { Authenticator } from './auth.js';
import { User } from './config_types.js';

/* FIXME: maybe we can extend the User and User.authProvider type to have a proper type.
Currently user.authProvider has `any` type and so we don't have a type for user.authProvider.config.
Expand Down
8 changes: 4 additions & 4 deletions src/azure_auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { use, expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { join } from 'node:path';

import { User, Cluster } from './config_types';
import { AzureAuth } from './azure_auth';
import { KubeConfig } from './config';
import { HttpMethod, RequestContext } from '.';
import { User, Cluster } from './config_types.js';
import { AzureAuth } from './azure_auth.js';
import { KubeConfig } from './config.js';
import { HttpMethod, RequestContext } from './index.js';

use(chaiAsPromised);

Expand Down
8 changes: 4 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
ListPromise,
ObjectCallback,
UPDATE,
} from './informer';
import { KubernetesObject } from './types';
import { ObjectSerializer } from './serializer';
import { Watch } from './watch';
} from './informer.js';
import { KubernetesObject } from './types.js';
import { ObjectSerializer } from './serializer.js';
import { Watch } from './watch.js';

export interface ObjectCache<T> {
get(name: string, namespace?: string): T | undefined;
Expand Down
19 changes: 13 additions & 6 deletions src/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ import chaiAsPromised from 'chai-as-promised';

import mock from 'ts-mockito';

import { V1Namespace, V1NamespaceList, V1ObjectMeta, V1Pod, V1PodList, V1ListMeta } from './api';
import { deleteObject, ListWatch, deleteItems, CacheMap, cacheMapFromList, addOrUpdateObject } from './cache';
import { KubeConfig } from './config';
import { Cluster, Context, User } from './config_types';
import { ListPromise } from './informer';
import { V1Namespace, V1NamespaceList, V1ObjectMeta, V1Pod, V1PodList, V1ListMeta } from './api.js';
import {
deleteObject,
ListWatch,
deleteItems,
CacheMap,
cacheMapFromList,
addOrUpdateObject,
} from './cache.js';
import { KubeConfig } from './config.js';
import { Cluster, Context, User } from './config_types.js';
import { ListPromise } from './informer.js';

use(chaiAsPromised);

import nock from 'nock';
import { Watch } from './watch';
import { Watch } from './watch.js';

const server = 'http://foo.company.com';

Expand Down
18 changes: 9 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import net from 'node:net';
import path from 'node:path';

import { Headers, RequestInit } from 'node-fetch';
import { RequestContext } from './api';
import { Authenticator } from './auth';
import { AzureAuth } from './azure_auth';
import { RequestContext } from './api.js';
import { Authenticator } from './auth.js';
import { AzureAuth } from './azure_auth.js';
import {
Cluster,
ConfigOptions,
Expand All @@ -19,18 +19,18 @@ import {
newContexts,
newUsers,
User,
} from './config_types';
import { ExecAuth } from './exec_auth';
import { FileAuth } from './file_auth';
import { GoogleCloudPlatformAuth } from './gcp_auth';
} from './config_types.js';
import { ExecAuth } from './exec_auth.js';
import { FileAuth } from './file_auth.js';
import { GoogleCloudPlatformAuth } from './gcp_auth.js';
import {
AuthMethodsConfiguration,
Configuration,
createConfiguration,
SecurityAuthentication,
ServerConfiguration,
} from './gen';
import { OpenIDConnectAuth } from './oidc_auth';
} from './gen/index.js';
import { OpenIDConnectAuth } from './oidc_auth.js';
import WebSocket from 'isomorphic-ws';
import child_process from 'node:child_process';

Expand Down
10 changes: 5 additions & 5 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import chaiAsPromised from 'chai-as-promised';
import mockfs from 'mock-fs';

import { Headers } from 'node-fetch';
import { HttpMethod } from '.';
import { HttpMethod } from './index.js';
import { assertRequestAgentsEqual, assertRequestOptionsEqual } from '../test/match-buffer';
import { CoreV1Api, RequestContext } from './api';
import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolutePath } from './config';
import { ActionOnInvalid, Cluster, newClusters, newContexts, newUsers, User } from './config_types';
import { ExecAuth } from './exec_auth';
import { CoreV1Api, RequestContext } from './api.js';
import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolutePath } from './config.js';
import { ActionOnInvalid, Cluster, newClusters, newContexts, newUsers, User } from './config_types.js';
import { ExecAuth } from './exec_auth.js';

const kcFileName = 'testdata/kubeconfig.yaml';
const kc2FileName = 'testdata/kubeconfig-2.yaml';
Expand Down
4 changes: 2 additions & 2 deletions src/cp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { WritableStreamBuffer } from 'stream-buffers';
import * as tar from 'tar';
import tmp from 'tmp-promise';

import { KubeConfig } from './config';
import { Exec } from './exec';
import { KubeConfig } from './config.js';
import { Exec } from './exec.js';

export class Cp {
public execInstance: Exec;
Expand Down
8 changes: 4 additions & 4 deletions src/cp_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import querystring from 'node:querystring';
import WebSocket from 'isomorphic-ws';

import { CallAwaiter } from '../test';
import { KubeConfig } from './config';
import { Exec } from './exec';
import { Cp } from './cp';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
import { KubeConfig } from './config.js';
import { Exec } from './exec.js';
import { Cp } from './cp.js';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';

describe('Cp', () => {
describe('cpFromPod', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import WebSocket from 'isomorphic-ws';
import querystring from 'node:querystring';
import stream from 'stream';

import { V1Status } from './api';
import { KubeConfig } from './config';
import { isResizable, ResizableStream, TerminalSizeQueue } from './terminal-size-queue';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
import { V1Status } from './api.js';
import { KubeConfig } from './config.js';
import { isResizable, ResizableStream, TerminalSizeQueue } from './terminal-size-queue.js';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';

export class Exec {
public 'handler': WebSocketInterface;
Expand Down
4 changes: 2 additions & 2 deletions src/exec_auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { OutgoingHttpHeaders } from 'node:http';
import https from 'node:https';

import { Authenticator } from './auth';
import { User } from './config_types';
import { Authenticator } from './auth.js';
import { User } from './config_types.js';

import child_process from 'node:child_process';

Expand Down
4 changes: 2 additions & 2 deletions src/exec_auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use(chaiAsPromised);
import https from 'node:https';
import { OutgoingHttpHeaders } from 'node:http';

import { ExecAuth } from './exec_auth';
import { User } from './config_types';
import { ExecAuth } from './exec_auth.js';
import { User } from './config_types.js';

import child_process from 'node:child_process';

Expand Down
10 changes: 5 additions & 5 deletions src/exec_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';

import { CallAwaiter, matchBuffer, ResizableWriteableStreamBuffer } from '../test';
import { V1Status } from './api';
import { KubeConfig } from './config';
import { Exec } from './exec';
import { TerminalSize } from './terminal-size-queue';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
import { V1Status } from './api.js';
import { KubeConfig } from './config.js';
import { Exec } from './exec.js';
import { TerminalSize } from './terminal-size-queue.js';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';

describe('Exec', () => {
describe('basic', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/file_auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'node:fs';
import https from 'node:https';

import { Authenticator } from './auth';
import { User } from './config_types';
import { Authenticator } from './auth.js';
import { User } from './config_types.js';

export class FileAuth implements Authenticator {
private token: string | null = null;
Expand Down
4 changes: 2 additions & 2 deletions src/file_auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { OutgoingHttpHeaders } from 'node:http';
import https from 'node:https';
import mockfs from 'mock-fs';

import { User } from './config_types';
import { FileAuth } from './file_auth';
import { User } from './config_types.js';
import { FileAuth } from './file_auth.js';

describe('FileAuth', () => {
it('should refresh when null', async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/gcp_auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import proc from 'node:child_process';
import https from 'node:https';
import jsonpath from 'jsonpath-plus';
import * as jsonpath from 'jsonpath-plus';

import { Authenticator } from './auth';
import { User } from './config_types';
import { Authenticator } from './auth.js';
import { User } from './config_types.js';

/* FIXME: maybe we can extend the User and User.authProvider type to have a proper type.
Currently user.authProvider has `any` type and so we don't have a type for user.authProvider.config.
Expand Down
8 changes: 4 additions & 4 deletions src/gcp_auth_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect } from 'chai';
import { join } from 'node:path';

import { User, Cluster } from './config_types';
import { GoogleCloudPlatformAuth } from './gcp_auth';
import { KubeConfig } from './config';
import { HttpMethod, RequestContext } from './gen';
import { User, Cluster } from './config_types.js';
import { GoogleCloudPlatformAuth } from './gcp_auth.js';
import { KubeConfig } from './config.js';
import { HttpMethod, RequestContext } from './gen/index.js';
import { Agent } from 'node:https';

describe('GoogleCloudPlatformAuth', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/health.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch, { AbortError } from 'node-fetch';
import { KubeConfig } from './config';
import fetch from 'node-fetch';
import { KubeConfig } from './config.js';
import { RequestOptions } from 'node:https';

export class Health {
Expand Down
6 changes: 3 additions & 3 deletions src/health_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect } from 'chai';
import nock from 'nock';

import { KubeConfig } from './config';
import { Health } from './health';
import { Cluster, User } from './config_types';
import { KubeConfig } from './config.js';
import { Health } from './health.js';
import { Cluster, User } from './config_types.js';

describe('Health', () => {
describe('livez', () => {
Expand Down
40 changes: 20 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
export * from './config';
export * from './cache';
export * from './api';
export * from './attach';
export * from './watch';
export * from './exec';
export * from './portforward';
export * from './types';
export * from './yaml';
export * from './log';
export * from './informer';
export * from './top';
export * from './cp';
export * from './patch';
export * from './metrics';
export * from './object';
export * from './health';
export { ConfigOptions, User, Cluster, Context } from './config_types';
export * from './config.js';
export * from './cache.js';
export * from './api.js';
export * from './attach.js';
export * from './watch.js';
export * from './exec.js';
export * from './portforward.js';
export * from './types.js';
export * from './yaml.js';
export * from './log.js';
export * from './informer.js';
export * from './top.js';
export * from './cp.js';
export * from './patch.js';
export * from './metrics.js';
export * from './object.js';
export * from './health.js';
export { ConfigOptions, User, Cluster, Context } from './config_types.js';

// Export AbortError and FetchError so that instanceof checks in user code will definitely use the same instances
export { AbortError, FetchError } from 'node-fetch';
// Export FetchError so that instanceof checks in user code will definitely use the same instance
export { FetchError } from 'node-fetch';
8 changes: 4 additions & 4 deletions src/informer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ListWatch } from './cache';
import { KubeConfig } from './config';
import { KubernetesListObject, KubernetesObject } from './types';
import { Watch } from './watch';
import { ListWatch } from './cache.js';
import { KubeConfig } from './config.js';
import { KubernetesListObject, KubernetesObject } from './types.js';
import { Watch } from './watch.js';

export type ObjectCallback<T extends KubernetesObject> = (obj: T) => void;
export type ErrorCallback = (err?: any) => void;
Expand Down
6 changes: 3 additions & 3 deletions src/integration_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import nock from 'nock';

import { CoreV1Api } from './api';
import { KubeConfig } from './config';
import { Cluster, User } from './config_types';
import { CoreV1Api } from './api.js';
import { KubeConfig } from './config.js';
import { Cluster, User } from './config_types.js';

use(chaiAsPromised);

Expand Down
Loading

0 comments on commit 4aa97ae

Please sign in to comment.