Skip to content

Commit 4aa97ae

Browse files
committed
src: convert project from CJS to ESM
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).
1 parent b165a02 commit 4aa97ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+170
-162
lines changed

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@kubernetes/client-node",
33
"version": "1.0.0-rc7",
44
"description": "NodeJS client for kubernetes",
5+
"type": "module",
56
"repository": {
67
"type": "git",
78
"url": "https://github.com/kubernetes-client/javascript.git"

Diff for: src/api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './gen';
1+
export * from './gen/index.js';

Diff for: src/attach.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import WebSocket from 'isomorphic-ws';
22
import querystring from 'node:querystring';
33
import stream from 'node:stream';
44

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

99
export class Attach {
1010
public 'handler': WebSocketInterface;

Diff for: src/attach_test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
44
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';
55

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

1212
describe('Attach', () => {
1313
describe('basic', () => {

Diff for: src/auth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import https from 'node:https';
22

3-
import { User } from './config_types';
3+
import { User } from './config_types.js';
44
import WebSocket from 'isomorphic-ws';
55

66
export interface Authenticator {

Diff for: src/azure_auth.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import proc from 'node:child_process';
22
import https from 'node:https';
3-
import jsonpath from 'jsonpath-plus';
3+
import * as jsonpath from 'jsonpath-plus';
44

5-
import { Authenticator } from './auth';
6-
import { User } from './config_types';
5+
import { Authenticator } from './auth.js';
6+
import { User } from './config_types.js';
77

88
/* FIXME: maybe we can extend the User and User.authProvider type to have a proper type.
99
Currently user.authProvider has `any` type and so we don't have a type for user.authProvider.config.

Diff for: src/azure_auth_test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { use, expect } from 'chai';
22
import chaiAsPromised from 'chai-as-promised';
33
import { join } from 'node:path';
44

5-
import { User, Cluster } from './config_types';
6-
import { AzureAuth } from './azure_auth';
7-
import { KubeConfig } from './config';
8-
import { HttpMethod, RequestContext } from '.';
5+
import { User, Cluster } from './config_types.js';
6+
import { AzureAuth } from './azure_auth.js';
7+
import { KubeConfig } from './config.js';
8+
import { HttpMethod, RequestContext } from './index.js';
99

1010
use(chaiAsPromised);
1111

Diff for: src/cache.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
ListPromise,
1010
ObjectCallback,
1111
UPDATE,
12-
} from './informer';
13-
import { KubernetesObject } from './types';
14-
import { ObjectSerializer } from './serializer';
15-
import { Watch } from './watch';
12+
} from './informer.js';
13+
import { KubernetesObject } from './types.js';
14+
import { ObjectSerializer } from './serializer.js';
15+
import { Watch } from './watch.js';
1616

1717
export interface ObjectCache<T> {
1818
get(name: string, namespace?: string): T | undefined;

Diff for: src/cache_test.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ import chaiAsPromised from 'chai-as-promised';
33

44
import mock from 'ts-mockito';
55

6-
import { V1Namespace, V1NamespaceList, V1ObjectMeta, V1Pod, V1PodList, V1ListMeta } from './api';
7-
import { deleteObject, ListWatch, deleteItems, CacheMap, cacheMapFromList, addOrUpdateObject } from './cache';
8-
import { KubeConfig } from './config';
9-
import { Cluster, Context, User } from './config_types';
10-
import { ListPromise } from './informer';
6+
import { V1Namespace, V1NamespaceList, V1ObjectMeta, V1Pod, V1PodList, V1ListMeta } from './api.js';
7+
import {
8+
deleteObject,
9+
ListWatch,
10+
deleteItems,
11+
CacheMap,
12+
cacheMapFromList,
13+
addOrUpdateObject,
14+
} from './cache.js';
15+
import { KubeConfig } from './config.js';
16+
import { Cluster, Context, User } from './config_types.js';
17+
import { ListPromise } from './informer.js';
1118

1219
use(chaiAsPromised);
1320

1421
import nock from 'nock';
15-
import { Watch } from './watch';
22+
import { Watch } from './watch.js';
1623

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

Diff for: src/config.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import net from 'node:net';
55
import path from 'node:path';
66

77
import { Headers, RequestInit } from 'node-fetch';
8-
import { RequestContext } from './api';
9-
import { Authenticator } from './auth';
10-
import { AzureAuth } from './azure_auth';
8+
import { RequestContext } from './api.js';
9+
import { Authenticator } from './auth.js';
10+
import { AzureAuth } from './azure_auth.js';
1111
import {
1212
Cluster,
1313
ConfigOptions,
@@ -19,18 +19,18 @@ import {
1919
newContexts,
2020
newUsers,
2121
User,
22-
} from './config_types';
23-
import { ExecAuth } from './exec_auth';
24-
import { FileAuth } from './file_auth';
25-
import { GoogleCloudPlatformAuth } from './gcp_auth';
22+
} from './config_types.js';
23+
import { ExecAuth } from './exec_auth.js';
24+
import { FileAuth } from './file_auth.js';
25+
import { GoogleCloudPlatformAuth } from './gcp_auth.js';
2626
import {
2727
AuthMethodsConfiguration,
2828
Configuration,
2929
createConfiguration,
3030
SecurityAuthentication,
3131
ServerConfiguration,
32-
} from './gen';
33-
import { OpenIDConnectAuth } from './oidc_auth';
32+
} from './gen/index.js';
33+
import { OpenIDConnectAuth } from './oidc_auth.js';
3434
import WebSocket from 'isomorphic-ws';
3535
import child_process from 'node:child_process';
3636

Diff for: src/config_test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import chaiAsPromised from 'chai-as-promised';
88
import mockfs from 'mock-fs';
99

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

1818
const kcFileName = 'testdata/kubeconfig.yaml';
1919
const kc2FileName = 'testdata/kubeconfig-2.yaml';

Diff for: src/cp.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { WritableStreamBuffer } from 'stream-buffers';
33
import * as tar from 'tar';
44
import tmp from 'tmp-promise';
55

6-
import { KubeConfig } from './config';
7-
import { Exec } from './exec';
6+
import { KubeConfig } from './config.js';
7+
import { Exec } from './exec.js';
88

99
export class Cp {
1010
public execInstance: Exec;

Diff for: src/cp_test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import querystring from 'node:querystring';
33
import WebSocket from 'isomorphic-ws';
44

55
import { CallAwaiter } from '../test';
6-
import { KubeConfig } from './config';
7-
import { Exec } from './exec';
8-
import { Cp } from './cp';
9-
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
6+
import { KubeConfig } from './config.js';
7+
import { Exec } from './exec.js';
8+
import { Cp } from './cp.js';
9+
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';
1010

1111
describe('Cp', () => {
1212
describe('cpFromPod', () => {

Diff for: src/exec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import WebSocket from 'isomorphic-ws';
22
import querystring from 'node:querystring';
33
import stream from 'stream';
44

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

1010
export class Exec {
1111
public 'handler': WebSocketInterface;

Diff for: src/exec_auth.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { OutgoingHttpHeaders } from 'node:http';
22
import https from 'node:https';
33

4-
import { Authenticator } from './auth';
5-
import { User } from './config_types';
4+
import { Authenticator } from './auth.js';
5+
import { User } from './config_types.js';
66

77
import child_process from 'node:child_process';
88

Diff for: src/exec_auth_test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use(chaiAsPromised);
55
import https from 'node:https';
66
import { OutgoingHttpHeaders } from 'node:http';
77

8-
import { ExecAuth } from './exec_auth';
9-
import { User } from './config_types';
8+
import { ExecAuth } from './exec_auth.js';
9+
import { User } from './config_types.js';
1010

1111
import child_process from 'node:child_process';
1212

Diff for: src/exec_test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
44
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';
55

66
import { CallAwaiter, matchBuffer, ResizableWriteableStreamBuffer } from '../test';
7-
import { V1Status } from './api';
8-
import { KubeConfig } from './config';
9-
import { Exec } from './exec';
10-
import { TerminalSize } from './terminal-size-queue';
11-
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
7+
import { V1Status } from './api.js';
8+
import { KubeConfig } from './config.js';
9+
import { Exec } from './exec.js';
10+
import { TerminalSize } from './terminal-size-queue.js';
11+
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';
1212

1313
describe('Exec', () => {
1414
describe('basic', () => {

Diff for: src/file_auth.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fs from 'node:fs';
22
import https from 'node:https';
33

4-
import { Authenticator } from './auth';
5-
import { User } from './config_types';
4+
import { Authenticator } from './auth.js';
5+
import { User } from './config_types.js';
66

77
export class FileAuth implements Authenticator {
88
private token: string | null = null;

Diff for: src/file_auth_test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { OutgoingHttpHeaders } from 'node:http';
33
import https from 'node:https';
44
import mockfs from 'mock-fs';
55

6-
import { User } from './config_types';
7-
import { FileAuth } from './file_auth';
6+
import { User } from './config_types.js';
7+
import { FileAuth } from './file_auth.js';
88

99
describe('FileAuth', () => {
1010
it('should refresh when null', async () => {

Diff for: src/gcp_auth.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import proc from 'node:child_process';
22
import https from 'node:https';
3-
import jsonpath from 'jsonpath-plus';
3+
import * as jsonpath from 'jsonpath-plus';
44

5-
import { Authenticator } from './auth';
6-
import { User } from './config_types';
5+
import { Authenticator } from './auth.js';
6+
import { User } from './config_types.js';
77

88
/* FIXME: maybe we can extend the User and User.authProvider type to have a proper type.
99
Currently user.authProvider has `any` type and so we don't have a type for user.authProvider.config.

Diff for: src/gcp_auth_test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { expect } from 'chai';
22
import { join } from 'node:path';
33

4-
import { User, Cluster } from './config_types';
5-
import { GoogleCloudPlatformAuth } from './gcp_auth';
6-
import { KubeConfig } from './config';
7-
import { HttpMethod, RequestContext } from './gen';
4+
import { User, Cluster } from './config_types.js';
5+
import { GoogleCloudPlatformAuth } from './gcp_auth.js';
6+
import { KubeConfig } from './config.js';
7+
import { HttpMethod, RequestContext } from './gen/index.js';
88
import { Agent } from 'node:https';
99

1010
describe('GoogleCloudPlatformAuth', () => {

Diff for: src/health.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fetch, { AbortError } from 'node-fetch';
2-
import { KubeConfig } from './config';
1+
import fetch from 'node-fetch';
2+
import { KubeConfig } from './config.js';
33
import { RequestOptions } from 'node:https';
44

55
export class Health {

Diff for: src/health_test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { expect } from 'chai';
22
import nock from 'nock';
33

4-
import { KubeConfig } from './config';
5-
import { Health } from './health';
6-
import { Cluster, User } from './config_types';
4+
import { KubeConfig } from './config.js';
5+
import { Health } from './health.js';
6+
import { Cluster, User } from './config_types.js';
77

88
describe('Health', () => {
99
describe('livez', () => {

Diff for: src/index.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
export * from './config';
2-
export * from './cache';
3-
export * from './api';
4-
export * from './attach';
5-
export * from './watch';
6-
export * from './exec';
7-
export * from './portforward';
8-
export * from './types';
9-
export * from './yaml';
10-
export * from './log';
11-
export * from './informer';
12-
export * from './top';
13-
export * from './cp';
14-
export * from './patch';
15-
export * from './metrics';
16-
export * from './object';
17-
export * from './health';
18-
export { ConfigOptions, User, Cluster, Context } from './config_types';
1+
export * from './config.js';
2+
export * from './cache.js';
3+
export * from './api.js';
4+
export * from './attach.js';
5+
export * from './watch.js';
6+
export * from './exec.js';
7+
export * from './portforward.js';
8+
export * from './types.js';
9+
export * from './yaml.js';
10+
export * from './log.js';
11+
export * from './informer.js';
12+
export * from './top.js';
13+
export * from './cp.js';
14+
export * from './patch.js';
15+
export * from './metrics.js';
16+
export * from './object.js';
17+
export * from './health.js';
18+
export { ConfigOptions, User, Cluster, Context } from './config_types.js';
1919

20-
// Export AbortError and FetchError so that instanceof checks in user code will definitely use the same instances
21-
export { AbortError, FetchError } from 'node-fetch';
20+
// Export FetchError so that instanceof checks in user code will definitely use the same instance
21+
export { FetchError } from 'node-fetch';

Diff for: src/informer.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ListWatch } from './cache';
2-
import { KubeConfig } from './config';
3-
import { KubernetesListObject, KubernetesObject } from './types';
4-
import { Watch } from './watch';
1+
import { ListWatch } from './cache.js';
2+
import { KubeConfig } from './config.js';
3+
import { KubernetesListObject, KubernetesObject } from './types.js';
4+
import { Watch } from './watch.js';
55

66
export type ObjectCallback<T extends KubernetesObject> = (obj: T) => void;
77
export type ErrorCallback = (err?: any) => void;

Diff for: src/integration_test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { expect, use } from 'chai';
22
import chaiAsPromised from 'chai-as-promised';
33
import nock from 'nock';
44

5-
import { CoreV1Api } from './api';
6-
import { KubeConfig } from './config';
7-
import { Cluster, User } from './config_types';
5+
import { CoreV1Api } from './api.js';
6+
import { KubeConfig } from './config.js';
7+
import { Cluster, User } from './config_types.js';
88

99
use(chaiAsPromised);
1010

0 commit comments

Comments
 (0)