From 0e73aa32f69592f67768c0c015f10a9b7bafe6d9 Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Fri, 20 Sep 2024 13:27:51 -0500 Subject: [PATCH] refactor: Split out packages (#133) - Moved the jsapi fetching code into a sub npm package - Refactored the jsapi fetching code to have zero dependencies on `vscode` The new `packages` directory provides a better silo for temporary code that will eventually become proper npm packages. e.g. enterprise jsapi types will be put in here on subsequent PR. Should also help with early dev of https://github.com/deephaven/deephaven-core/issues/5537 --- .eslintrc.json | 3 +- .gitignore | 3 +- .vscodeignore | 2 + package-lock.json | 16 ++ package.json | 8 +- packages/require-jsapi/LICENSE | 201 ++++++++++++++++++ packages/require-jsapi/README.md | 2 + packages/require-jsapi/package.json | 19 ++ packages/require-jsapi/src/dhc.ts | 82 +++++++ packages/require-jsapi/src/dhe.ts | 17 ++ packages/require-jsapi/src/errorUtils.ts | 34 +++ packages/require-jsapi/src/index.ts | 15 ++ .../require-jsapi/src/polyfill.ts | 0 .../require-jsapi/src/serverUtils.ts | 53 ++--- packages/require-jsapi/tsconfig.json | 11 + src/common/constants.ts | 1 - src/controllers/ExtensionController.ts | 2 +- src/controllers/PanelController.ts | 2 +- src/controllers/PipServerController.ts | 2 +- src/dh/dhc.ts | 84 +------- src/dh/dhe.ts | 18 -- src/{util => dh}/errorUtils.ts | 51 +---- src/services/DhService.ts | 13 +- src/services/DhcService.ts | 11 +- src/services/ServerManager.ts | 6 +- src/types/commonTypes.d.ts | 5 - src/types/global.d.ts | 7 - src/util/ErrorTypes.ts | 11 - src/util/index.ts | 5 +- src/util/serverUtils.ts | 6 +- ...downloadUtils.spec.ts => tmpUtils.spec.ts} | 6 +- src/util/tmpUtils.ts | 37 ++++ tsconfig.json | 5 +- tsconfig.unit.json | 3 +- 34 files changed, 508 insertions(+), 233 deletions(-) create mode 100644 packages/require-jsapi/LICENSE create mode 100644 packages/require-jsapi/README.md create mode 100644 packages/require-jsapi/package.json create mode 100644 packages/require-jsapi/src/dhc.ts create mode 100644 packages/require-jsapi/src/dhe.ts create mode 100644 packages/require-jsapi/src/errorUtils.ts create mode 100644 packages/require-jsapi/src/index.ts rename src/util/polyfillUtils.ts => packages/require-jsapi/src/polyfill.ts (100%) rename src/util/downloadUtils.ts => packages/require-jsapi/src/serverUtils.ts (72%) create mode 100644 packages/require-jsapi/tsconfig.json rename src/{util => dh}/errorUtils.ts (59%) delete mode 100644 src/types/global.d.ts delete mode 100644 src/util/ErrorTypes.ts rename src/util/{downloadUtils.spec.ts => tmpUtils.spec.ts} (91%) create mode 100644 src/util/tmpUtils.ts diff --git a/.eslintrc.json b/.eslintrc.json index 16a99626..4a516cd6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -28,7 +28,8 @@ "project": [ "./tsconfig.json", "./e2e/tsconfig.json", - "./tsconfig.unit.json" + "./tsconfig.unit.json", + "./packages/*/tsconfig.json" ] }, diff --git a/.gitignore b/.gitignore index 0bbae65b..e57e4136 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ node_modules .DS_Store .wdio-vscode-service e2e/reports/ -test-reports/ \ No newline at end of file +test-reports/ +tsconfig.tsbuildinfo \ No newline at end of file diff --git a/.vscodeignore b/.vscodeignore index b405fb0e..dc58f0d8 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -3,11 +3,13 @@ _ignore/** .vscode/** .vscode-test/** src/** +packages/** .gitignore .yarnrc vsc-extension-quickstart.md **/tsconfig.json **/tsconfig.unit.json +**/tsconfig.tsbuildinfo **/.eslintrc.json **/*.map **/*.ts diff --git a/package-lock.json b/package-lock.json index 684c134f..91e0f64a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,11 @@ "": { "name": "vscode-deephaven", "version": "0.1.12", + "workspaces": [ + "packages/*" + ], "dependencies": { + "@deephaven/require-jsapi": "file:./packages/require-jsapi", "ws": "^8.18.0" }, "devDependencies": { @@ -434,6 +438,10 @@ "integrity": "sha512-0NMh2eRXT16ro4sE/wH0q5+fAdQMitqgKgQ7SwUEtEXq6mp0JWA0xr/x4msdyP3kJB+e6pveTYcgMcHwqrGO/A==", "dev": true }, + "node_modules/@deephaven/require-jsapi": { + "resolved": "packages/require-jsapi", + "link": true + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.21.5", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz", @@ -14112,6 +14120,11 @@ "dependencies": { "safe-buffer": "~5.2.0" } + }, + "packages/require-jsapi": { + "name": "@deephaven/require-jsapi", + "version": "0.0.1", + "license": "SEE LICENSE IN LICENSE.md" } }, "dependencies": { @@ -14443,6 +14456,9 @@ "integrity": "sha512-0NMh2eRXT16ro4sE/wH0q5+fAdQMitqgKgQ7SwUEtEXq6mp0JWA0xr/x4msdyP3kJB+e6pveTYcgMcHwqrGO/A==", "dev": true }, + "@deephaven/require-jsapi": { + "version": "file:packages/require-jsapi" + }, "@esbuild/aix-ppc64": { "version": "0.21.5", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz", diff --git a/package.json b/package.json index 60309fb9..ee300e36 100644 --- a/package.json +++ b/package.json @@ -25,15 +25,18 @@ "onStartupFinished" ], "main": "./out/extension.js", + "workspaces": [ + "packages/*" + ], "scripts": { - "clean": "rm -rf out", + "clean": "rm -rf out packages/*/tsconfig.tsbuildinfo", "icon:gen": "node icons/generate.mjs", "test": "npm run test:unit", "test:ci": "npm run ts:build && npm run ts:check && npm run test:lint && npm run test:unit", "test:e2e": "npm run ts:build && cd e2e && wdio run ./wdio.conf.ts", "test:lint": "eslint . --ext ts", "test:unit": "vitest --reporter=default --reporter=junit --outputFile=./test-reports/vitest.junit.xml", - "ts:build": "npm run clean && tsc -p ./tsconfig.json", + "ts:build": "npm run clean && tsc --build ./tsconfig.json", "ts:check": "tsc -p ./tsconfig.json --noEmit --module preserve --moduleResolution bundler && tsc -p tsconfig.unit.json --noEmit --module preserve --moduleResolution bundler && tsc -p e2e/tsconfig.json --noEmit --skipLibCheck", "ts:watch": "npm run ts:build -- --watch", "package": "vsce package -o releases/", @@ -750,6 +753,7 @@ "wdio-vscode-service": "^6.1.0" }, "dependencies": { + "@deephaven/require-jsapi": "file:./packages/require-jsapi", "ws": "^8.18.0" } } diff --git a/packages/require-jsapi/LICENSE b/packages/require-jsapi/LICENSE new file mode 100644 index 00000000..c61b6639 --- /dev/null +++ b/packages/require-jsapi/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +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 + + 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. diff --git a/packages/require-jsapi/README.md b/packages/require-jsapi/README.md new file mode 100644 index 00000000..4aab4972 --- /dev/null +++ b/packages/require-jsapi/README.md @@ -0,0 +1,2 @@ +# Deephaven Require Jsapi +This package allows downloading `jsapi` modules from a running Deephaven server. It should eventually be moved to a different repo and become a formal `npm` package. See https://github.com/deephaven/deephaven-core/issues/5537. For now it exists to internally serve the `vscode` extension and has been split out into an internal package to keep encapsulation boundaries cleaner. \ No newline at end of file diff --git a/packages/require-jsapi/package.json b/packages/require-jsapi/package.json new file mode 100644 index 00000000..dfaf0b9f --- /dev/null +++ b/packages/require-jsapi/package.json @@ -0,0 +1,19 @@ +{ + "name": "@deephaven/require-jsapi", + "version": "0.0.1", + "description": "Deephaven dynamic import utils for Jsapi", + "author": "Deephaven Data Labs LLC", + "license": "SEE LICENSE IN LICENSE.md", + "type": "commonjs", + "private": false, + "source": "src/index.ts", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "files": [ + "dist" + ], + "sideEffects": false, + "scripts": { + "build": "tsc --build" + } +} diff --git a/packages/require-jsapi/src/dhc.ts b/packages/require-jsapi/src/dhc.ts new file mode 100644 index 00000000..036e66c9 --- /dev/null +++ b/packages/require-jsapi/src/dhc.ts @@ -0,0 +1,82 @@ +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import type { dh as DhType } from '@deephaven/jsapi-types'; +import { polyfillDh } from './polyfill'; +import { downloadFromURL, hasStatusCode } from './serverUtils'; + +/** + * Check if a given server is running by checking if the `dh-core.js` file is + * accessible. + * @param serverUrl + */ +export async function isDhcServerRunning(serverUrl: URL): Promise { + try { + return await hasStatusCode( + new URL('jsapi/dh-core.js', serverUrl.toString()), + [200, 204] + ); + } catch { + return false; + } +} + +/** + * Polyfill browser apis, download jsapi to a local directory, and return the + * default export. + * @param serverUrl URL of the server to download the jsapi from + * @param storageDir Directory to save the downloaded jsapi + * @returns Default export of downloaded jsapi + */ +export async function initDhcApi( + serverUrl: URL, + storageDir: string +): Promise { + polyfillDh(); + return getDhc(serverUrl, true, storageDir); +} + +/** + * Download and import the Deephaven JS API from the server. + * 1. Download `dh-internal.js` and `dh-core.js` from the server and save them + * to `out/tmp` as `.cjs` files (renaming of import / export to cjs compatible code). + * 2. requires `dh-core.mjs` and return the default export. + * Copy / modified from https://github.com/deephaven/deephaven.io/blob/main/tools/run-examples/includeAPI.mjs + * NOTE: there is a limitation in current vscode extension apis such that es6 imports are not supported. This is why + * we have to save / convert to .cjs. + * See https://stackoverflow.com/questions/70620025/how-do-i-import-an-es6-javascript-module-in-my-vs-code-extension-written-in-type + */ +async function getDhc( + serverUrl: URL, + download: boolean, + storageDir: string +): Promise { + if (download) { + const dhInternal = await downloadFromURL( + path.join(serverUrl.toString(), 'jsapi/dh-internal.js') + ); + // Convert to .cjs + fs.writeFileSync( + path.join(storageDir, 'dh-internal.cjs'), + dhInternal.replace( + `export{__webpack_exports__dhinternal as dhinternal};`, + `module.exports={dhinternal:__webpack_exports__dhinternal};` + ) + ); + + const dhCore = await downloadFromURL( + path.join(serverUrl.toString(), 'jsapi/dh-core.js') + ); + fs.writeFileSync( + path.join(storageDir, 'dh-core.cjs'), + // Convert to .cjs + dhCore + .replace( + `import {dhinternal} from './dh-internal.js';`, + `const {dhinternal} = require("./dh-internal.cjs");` + ) + .replace(`export default dh;`, `module.exports = dh;`) + ); + } + + return require(path.join(storageDir, 'dh-core.cjs')); +} diff --git a/packages/require-jsapi/src/dhe.ts b/packages/require-jsapi/src/dhe.ts new file mode 100644 index 00000000..091b716f --- /dev/null +++ b/packages/require-jsapi/src/dhe.ts @@ -0,0 +1,17 @@ +import { hasStatusCode } from './serverUtils'; + +/** + * Check if a given server is running by checking if the `irisapi/irisapi.nocache.js` + * file is accessible. + * @param serverUrl + */ +export async function isDheServerRunning(serverUrl: URL): Promise { + try { + return await hasStatusCode( + new URL('irisapi/irisapi.nocache.js', serverUrl.toString()), + [200, 204] + ); + } catch { + return false; + } +} diff --git a/packages/require-jsapi/src/errorUtils.ts b/packages/require-jsapi/src/errorUtils.ts new file mode 100644 index 00000000..e71ec2a5 --- /dev/null +++ b/packages/require-jsapi/src/errorUtils.ts @@ -0,0 +1,34 @@ +/** + * Return true if given error has a code:string prop. Optionally check if the + * code matches a given value. + * @param err Error to check + * @param code Optional code to check + */ +export function hasErrorCode( + err: unknown, + code?: string +): err is { code: string } { + if ( + err != null && + typeof err === 'object' && + 'code' in err && + typeof err.code === 'string' + ) { + return code == null || err.code === code; + } + + return false; +} + +/** + * Returns true if the given error is an AggregateError. Optionally checks if + * a given code matches the error's code. + * @param err Error to check + * @param code Optional code to check + */ +export function isAggregateError( + err: unknown, + code?: string +): err is { code: string } { + return hasErrorCode(err, code) && String(err) === 'AggregateError'; +} diff --git a/packages/require-jsapi/src/index.ts b/packages/require-jsapi/src/index.ts new file mode 100644 index 00000000..91f10d26 --- /dev/null +++ b/packages/require-jsapi/src/index.ts @@ -0,0 +1,15 @@ +export * from './dhc'; +export * from './dhe'; +export * from './errorUtils'; +export * from './polyfill'; +export * from './serverUtils'; + +// TODO: https://github.com/deephaven/deephaven-core/issues/5911 to address the +// underlying issue of jsapi-types being unaware of `dhinternal`. Once that is +// addressed, this can be removed. +declare global { + // eslint-disable-next-line no-unused-vars + module dhinternal.io.deephaven.proto.ticket_pb { + export type TypedTicket = unknown; + } +} diff --git a/src/util/polyfillUtils.ts b/packages/require-jsapi/src/polyfill.ts similarity index 100% rename from src/util/polyfillUtils.ts rename to packages/require-jsapi/src/polyfill.ts diff --git a/src/util/downloadUtils.ts b/packages/require-jsapi/src/serverUtils.ts similarity index 72% rename from src/util/downloadUtils.ts rename to packages/require-jsapi/src/serverUtils.ts index cdef683f..a4012eb4 100644 --- a/src/util/downloadUtils.ts +++ b/packages/require-jsapi/src/serverUtils.ts @@ -1,52 +1,24 @@ -import * as fs from 'node:fs'; import * as http from 'node:http'; import * as https from 'node:https'; -import * as path from 'node:path'; -import { SERVER_STATUS_CHECK_TIMEOUT, TMP_DIR_ROOT } from '../common'; -import { Logger } from './Logger'; import { hasErrorCode, isAggregateError } from './errorUtils'; -const logger = new Logger('downloadUtils'); - -/** - * Return the path of the temp directory with optional sub directory. If recreate - * is true, the directory will be deleted and recreated. - * @param recreate If true, delete and recreate the directory - * @param subDirectory Optional sub directory to create - * @returns The path of the temp directory - */ -export function getTempDir(recreate: boolean, subDirectory?: string): string { - let tempDir = TMP_DIR_ROOT; - if (subDirectory != null) { - tempDir = path.join(tempDir, subDirectory); - } - - if (recreate) { - try { - fs.rmSync(tempDir, { recursive: true }); - } catch { - // Ignore if can't delete. Likely doesn't exist - } - } - - if (!fs.existsSync(tempDir)) { - fs.mkdirSync(tempDir); - } - - return tempDir; -} +export const SERVER_STATUS_CHECK_TIMEOUT = 3000; /** * Require a JS module from a URL. Loads the module in memory and returns its exports * Copy / modified from https://github.com/deephaven/deephaven.io/blob/main/tools/run-examples/includeAPI.mjs * - * @param {string} url The URL with protocol to require from. Supports http or https - * @returns {Promise} Promise which resolves to the module's exports + * @param url The URL with protocol to require from. Supports http or https + * @param retries The number of retries on failure + * @param retryDelay The delay between retries in milliseconds + * @param logger An optional logger object. Defaults to `console` + * @returns Promise which resolves to the module's exports */ export async function downloadFromURL( url: string, retries = 10, - retryDelay = 1000 + retryDelay = 1000, + logger: { error: (...args: unknown[]) => void } = console ): Promise { return new Promise((resolve, reject) => { const urlObj = new URL(url); @@ -83,7 +55,7 @@ export async function downloadFromURL( logger.error('Retrying url:', url); setTimeout( () => - downloadFromURL(url, retries - 1, retryDelay).then( + downloadFromURL(url, retries - 1, retryDelay, logger).then( resolve, reject ), @@ -102,10 +74,15 @@ export async function downloadFromURL( /** * Check if a given url returns an expected status code. + * @param url The URL to check + * @param statusCodes The expected status codes + * @param logger An optional logger object. Defaults to `console` + * @returns Promise which resolves to true if the status code matches, false otherwise */ export async function hasStatusCode( url: URL, - ...statusCodes: number[] + statusCodes: number[], + logger: { error: (...args: unknown[]) => void } = console ): Promise { return new Promise(resolve => { const transporter = url.protocol === 'http:' ? http : https; diff --git a/packages/require-jsapi/tsconfig.json b/packages/require-jsapi/tsconfig.json new file mode 100644 index 00000000..d5b0579d --- /dev/null +++ b/packages/require-jsapi/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "composite": true, + "rootDir": "src", + "outDir": "dist", + "types": ["node"] + }, + "include": ["src/**/*.ts"], + "exclude": ["node_modules"] +} diff --git a/src/common/constants.ts b/src/common/constants.ts index d84b7517..a89e60ad 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -18,7 +18,6 @@ export const DEFAULT_PIP_PORT_RANGE: ReadonlySet = new Set([ export const PYTHON_ENV_WAIT = 1500 as const; -export const SERVER_STATUS_CHECK_TIMEOUT = 3000; export const PIP_SERVER_STATUS_CHECK_INTERVAL = 3000; export const PIP_SERVER_STATUS_CHECK_TIMEOUT = 30000; diff --git a/src/controllers/ExtensionController.ts b/src/controllers/ExtensionController.ts index 5cc49acb..1c1f6d58 100644 --- a/src/controllers/ExtensionController.ts +++ b/src/controllers/ExtensionController.ts @@ -293,7 +293,7 @@ export class ExtensionController implements Disposable { */ initializeTempDirectory = (): void => { // recreate tmp dir that will be used to dowload JS Apis - getTempDir(true /*recreate*/); + getTempDir({ recreate: true }); }; /** diff --git a/src/controllers/PanelController.ts b/src/controllers/PanelController.ts index a03f2b9a..d56fa374 100644 --- a/src/controllers/PanelController.ts +++ b/src/controllers/PanelController.ts @@ -6,13 +6,13 @@ import type { VariableDefintion, } from '../types'; import { assertDefined, getDHThemeKey, getPanelHtml, Logger } from '../util'; -import { getEmbedWidgetUrl } from '../dh/dhc'; import { DhcService } from '../services'; import { OPEN_VARIABLE_PANELS_CMD, REFRESH_VARIABLE_PANELS_CMD, } from '../common'; import { waitFor } from '../util/promiseUtils'; +import { getEmbedWidgetUrl } from '../dh/dhc'; const logger = new Logger('PanelController'); diff --git a/src/controllers/PipServerController.ts b/src/controllers/PipServerController.ts index 9ab9fc9d..217e48c8 100644 --- a/src/controllers/PipServerController.ts +++ b/src/controllers/PipServerController.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; import * as fs from 'node:fs'; +import { isDhcServerRunning } from '@deephaven/require-jsapi'; import { getPipServerUrl, getPipStatusFilePath, @@ -13,7 +14,6 @@ import { PIP_SERVER_SUPPORTED_PLATFORMS, PYTHON_ENV_WAIT, } from '../common'; -import { isDhcServerRunning } from '../dh/dhc'; import { pollUntilTrue, waitFor } from '../util/promiseUtils'; const logger = new Logger('PipServerController'); diff --git a/src/dh/dhc.ts b/src/dh/dhc.ts index c086b9bd..ba681a8c 100644 --- a/src/dh/dhc.ts +++ b/src/dh/dhc.ts @@ -1,15 +1,5 @@ -import * as fs from 'node:fs'; -import * as path from 'node:path'; import type { dh as DhType } from '@deephaven/jsapi-types'; -import { - downloadFromURL, - getTempDir, - hasStatusCode, - NoConsoleTypesError, - polyfillDh, - urlToDirectoryName, -} from '../util'; -import type { ConnectionAndSession } from '../types'; +import { NoConsoleTypesError } from './errorUtils'; export const AUTH_HANDLER_TYPE_ANONYMOUS = 'io.deephaven.auth.AnonymousAuthenticationHandler'; @@ -17,22 +7,10 @@ export const AUTH_HANDLER_TYPE_ANONYMOUS = export const AUTH_HANDLER_TYPE_PSK = 'io.deephaven.authentication.psk.PskAuthenticationHandler'; -/** - * Check if a given server is running by checking if the `dh-core.js` file is - * accessible. - * @param serverUrl - */ -export async function isDhcServerRunning(serverUrl: URL): Promise { - try { - return await hasStatusCode( - new URL('jsapi/dh-core.js', serverUrl.toString()), - 200, - 204 - ); - } catch { - return false; - } -} +export type ConnectionAndSession = { + cn: TConnection; + session: TSession; +}; /** * Get embed widget url for a widget. @@ -51,11 +29,6 @@ export function getEmbedWidgetUrl( return `${serverUrlStr}/iframe/widget/?theme=${themeKey}&name=${title}${psk ? `&psk=${psk}` : ''}`; } -export async function initDhcApi(serverUrl: URL): Promise { - polyfillDh(); - return getDhc(serverUrl, true); -} - export async function initDhcSession( client: DhType.CoreClient, credentials: DhType.LoginCredentials @@ -74,50 +47,3 @@ export async function initDhcSession( return { cn, session }; } - -/** - * Download and import the Deephaven JS API from the server. - * 1. Download `dh-internal.js` and `dh-core.js` from the server and save them - * to `out/tmp` as `.cjs` files (renaming of import / export to cjs compatible code). - * 2. requires `dh-core.mjs` and return the default export. - * Copy / modified from https://github.com/deephaven/deephaven.io/blob/main/tools/run-examples/includeAPI.mjs - * NOTE: there is a limitation in current vscode extension apis such that es6 imports are not supported. This is why - * we have to save / convert to .cjs. - * See https://stackoverflow.com/questions/70620025/how-do-i-import-an-es6-javascript-module-in-my-vs-code-extension-written-in-type - */ -async function getDhc( - serverUrl: URL, - download: boolean -): Promise { - const tmpDir = getTempDir(false, urlToDirectoryName(serverUrl.toString())); - - if (download) { - const dhInternal = await downloadFromURL( - path.join(serverUrl.toString(), 'jsapi/dh-internal.js') - ); - // Convert to .cjs - fs.writeFileSync( - path.join(tmpDir, 'dh-internal.cjs'), - dhInternal.replace( - `export{__webpack_exports__dhinternal as dhinternal};`, - `module.exports={dhinternal:__webpack_exports__dhinternal};` - ) - ); - - const dhCore = await downloadFromURL( - path.join(serverUrl.toString(), 'jsapi/dh-core.js') - ); - fs.writeFileSync( - path.join(tmpDir, 'dh-core.cjs'), - // Convert to .cjs - dhCore - .replace( - `import {dhinternal} from './dh-internal.js';`, - `const {dhinternal} = require("./dh-internal.cjs");` - ) - .replace(`export default dh;`, `module.exports = dh;`) - ); - } - - return require(path.join(tmpDir, 'dh-core.cjs')); -} diff --git a/src/dh/dhe.ts b/src/dh/dhe.ts index fdfe04ad..e69de29b 100644 --- a/src/dh/dhe.ts +++ b/src/dh/dhe.ts @@ -1,18 +0,0 @@ -import { hasStatusCode } from '../util'; - -/** - * Check if a given server is running by checking if the `irisapi/irisapi.nocache.js` - * file is accessible. - * @param serverUrl - */ -export async function isDheServerRunning(serverUrl: URL): Promise { - try { - return await hasStatusCode( - new URL('irisapi/irisapi.nocache.js', serverUrl.toString()), - 200, - 204 - ); - } catch { - return false; - } -} diff --git a/src/util/errorUtils.ts b/src/dh/errorUtils.ts similarity index 59% rename from src/util/errorUtils.ts rename to src/dh/errorUtils.ts index 4fc291bb..b7a3d022 100644 --- a/src/util/errorUtils.ts +++ b/src/dh/errorUtils.ts @@ -1,6 +1,8 @@ -import { Logger } from './Logger'; - -const logger = new Logger('errorUtils'); +export class NoConsoleTypesError extends Error { + constructor() { + super('No console types available'); + } +} export interface ParsedError { [key: string]: string | number | undefined; @@ -12,46 +14,15 @@ export interface ParsedError { traceback?: string; } -/** - * Returns true if the given error is an AggregateError. Optionally checks if - * a given code matches the error's code. - * @param err Error to check - * @param code Optional code to check - */ -export function isAggregateError( - err: unknown, - code?: string -): err is { code: string } { - return hasErrorCode(err, code) && String(err) === 'AggregateError'; -} - -/** - * Return true if given error has a code:string prop. Optionally check if the - * code matches a given value. - * @param err Error to check - * @param code Optional code to check - */ -export function hasErrorCode( - err: unknown, - code?: string -): err is { code: string } { - if ( - err != null && - typeof err === 'object' && - 'code' in err && - typeof err.code === 'string' - ) { - return code == null || err.code === code; - } - - return false; -} - /** * Parse a server error string into a key-value object. - * @param error + * @param error Error string to parse. + * @param logger Optional logger for debugging. Defaluts to console. */ -export function parseServerError(error: string): ParsedError { +export function parseServerError( + error: string, + logger: { debug: (...args: unknown[]) => void } = console +): ParsedError { const errorDetails: ParsedError = {}; const lines = error.split('\n'); diff --git a/src/services/DhService.ts b/src/services/DhService.ts index 7ee39ebc..d3d46da0 100644 --- a/src/services/DhService.ts +++ b/src/services/DhService.ts @@ -1,8 +1,8 @@ import * as vscode from 'vscode'; import type { dh as DhcType } from '@deephaven/jsapi-types'; +import { isAggregateError } from '@deephaven/require-jsapi'; import { hasErrorCode } from '../util/typeUtils'; import type { - ConnectionAndSession, ConsoleType, IDhService, IPanelService, @@ -11,19 +11,14 @@ import type { VariableDefintion, VariableID, } from '../types'; -import { - formatTimestamp, - getCombinedSelectedLinesText, - isAggregateError, - Logger, - NoConsoleTypesError, - parseServerError, -} from '../util'; +import { formatTimestamp, getCombinedSelectedLinesText, Logger } from '../util'; import { OPEN_VARIABLE_PANELS_CMD, REFRESH_VARIABLE_PANELS_CMD, VARIABLE_UNICODE_ICONS, } from '../common'; +import type { ConnectionAndSession } from '../dh/dhc'; +import { NoConsoleTypesError, parseServerError } from '../dh/errorUtils'; const logger = new Logger('DhService'); diff --git a/src/services/DhcService.ts b/src/services/DhcService.ts index 8f7f12d3..ee044382 100644 --- a/src/services/DhcService.ts +++ b/src/services/DhcService.ts @@ -1,14 +1,14 @@ import * as vscode from 'vscode'; import type { dh as DhcType } from '@deephaven/jsapi-types'; +import { initDhcApi } from '@deephaven/require-jsapi'; import DhService from './DhService'; +import { getTempDir, Logger, urlToDirectoryName } from '../util'; import { AUTH_HANDLER_TYPE_ANONYMOUS, AUTH_HANDLER_TYPE_PSK, - initDhcApi, initDhcSession, + type ConnectionAndSession, } from '../dh/dhc'; -import { Logger } from '../util'; -import type { ConnectionAndSession } from '../types'; const logger = new Logger('DhcService'); @@ -24,7 +24,10 @@ export class DhcService extends DhService { } protected async initApi(): Promise { - return initDhcApi(this.serverUrl); + return initDhcApi( + this.serverUrl, + getTempDir({ subDirectory: urlToDirectoryName(this.serverUrl) }) + ); } protected async createClient( diff --git a/src/services/ServerManager.ts b/src/services/ServerManager.ts index 4291717a..e460cf50 100644 --- a/src/services/ServerManager.ts +++ b/src/services/ServerManager.ts @@ -1,8 +1,10 @@ import * as vscode from 'vscode'; import { randomUUID } from 'node:crypto'; +import { + isDhcServerRunning, + isDheServerRunning, +} from '@deephaven/require-jsapi'; import { UnsupportedConsoleTypeError } from '../common'; -import { isDhcServerRunning } from '../dh/dhc'; -import { isDheServerRunning } from '../dh/dhe'; import type { ConsoleType, IConfigService, diff --git a/src/types/commonTypes.d.ts b/src/types/commonTypes.d.ts index 2c380c80..e5ddfa7a 100644 --- a/src/types/commonTypes.d.ts +++ b/src/types/commonTypes.d.ts @@ -11,11 +11,6 @@ export type Port = Brand<'Port', number>; export type ConnectionType = 'DHC'; -export type ConnectionAndSession = { - cn: TConnection; - session: TSession; -}; - export type ConsoleType = 'groovy' | 'python'; export type CoreConnectionConfigStored = diff --git a/src/types/global.d.ts b/src/types/global.d.ts deleted file mode 100644 index 0aa3cf27..00000000 --- a/src/types/global.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -// TODO: https://github.com/deephaven/deephaven-core/issues/5911 to address the -// underlying issue of jsapi-types being unaware of `dhinternal`. Once that is -// addressed, this can be removed and `global.d.ts` can be removed from tsconfig -// (assuming we have not introduced any new global types here). -declare module dhinternal.io.deephaven.proto.ticket_pb { - export type TypedTicket = unknown; -} diff --git a/src/util/ErrorTypes.ts b/src/util/ErrorTypes.ts deleted file mode 100644 index 5da22ed1..00000000 --- a/src/util/ErrorTypes.ts +++ /dev/null @@ -1,11 +0,0 @@ -export class InvalidConsoleTypeError extends Error { - constructor(type: string) { - super(`Invalid console type: '${type}'`); - } -} - -export class NoConsoleTypesError extends Error { - constructor() { - super('No console types available'); - } -} diff --git a/src/util/index.ts b/src/util/index.ts index 5bcdc833..23eebddd 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -1,16 +1,13 @@ export * from './assertUtil'; export * from './dataUtils'; -export * from './downloadUtils'; -export * from './errorUtils'; -export * from './ErrorTypes'; export * from './isDisposable'; export * from './Logger'; export * from './OutputChannelWithHistory'; export * from './panelUtils'; -export * from './polyfillUtils'; export * from './selectionUtils'; export * from './serverUtils'; export * from './testUtils'; +export * from './tmpUtils'; export * from './treeViewUtils'; export * from './Toaster'; export * from './uiUtils'; diff --git a/src/util/serverUtils.ts b/src/util/serverUtils.ts index 9334ac88..4e10e1d4 100644 --- a/src/util/serverUtils.ts +++ b/src/util/serverUtils.ts @@ -8,7 +8,7 @@ import type { ServerConnectionConfig, } from '../types'; import { PIP_SERVER_STATUS_DIRECTORY, SERVER_LANGUAGE_SET } from '../common'; -import { getTempDir } from './downloadUtils'; +import { getTempDir } from './tmpUtils'; /** * Get initial server states based on server configs. @@ -81,7 +81,9 @@ export function getPipServerUrl(port: Port): URL { * @returns The path to the pip server status file */ export function getPipStatusFilePath(): string { - const dirPath = getTempDir(false, PIP_SERVER_STATUS_DIRECTORY); + const dirPath = getTempDir({ + subDirectory: PIP_SERVER_STATUS_DIRECTORY, + }); const statusFileName = `status-pip.txt`; return path.join(dirPath, statusFileName); } diff --git a/src/util/downloadUtils.spec.ts b/src/util/tmpUtils.spec.ts similarity index 91% rename from src/util/downloadUtils.spec.ts rename to src/util/tmpUtils.spec.ts index 4c204c05..028f3123 100644 --- a/src/util/downloadUtils.spec.ts +++ b/src/util/tmpUtils.spec.ts @@ -1,7 +1,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; import * as fs from 'node:fs'; import * as path from 'node:path'; -import { getTempDir } from './downloadUtils'; +import { getTempDir } from './tmpUtils'; import { TMP_DIR_ROOT } from '../common'; vi.mock('node:fs'); @@ -20,7 +20,7 @@ describe('getTempDir', () => { 'should create temp directory if it does not already exist: %s, %s', (dirExists, subDirectory, expectedPath) => { vi.mocked(fs.existsSync).mockReturnValue(dirExists); - getTempDir(true, subDirectory); + getTempDir({ recreate: true, subDirectory }); expect(fs.existsSync).toHaveBeenCalledWith(expectedPath); @@ -40,7 +40,7 @@ describe('getTempDir', () => { ])( 'should remove directory if recreate is true: %s, %s, %s', (recreate, subDirectory, expectedPath) => { - getTempDir(recreate, subDirectory); + getTempDir({ recreate, subDirectory }); if (recreate) { expect(fs.rmSync).toHaveBeenCalledWith(expectedPath, { diff --git a/src/util/tmpUtils.ts b/src/util/tmpUtils.ts new file mode 100644 index 00000000..da1d49ec --- /dev/null +++ b/src/util/tmpUtils.ts @@ -0,0 +1,37 @@ +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { TMP_DIR_ROOT } from '../common'; + +/** + * Return the path of the temp directory with optional sub directory. If recreate + * is true, the directory will be deleted and recreated. + * @param recreate If true, delete and recreate the directory + * @param subDirectory Optional sub directory to create + * @returns The path of the temp directory + */ +export function getTempDir({ + recreate, + subDirectory, +}: { + recreate?: boolean; + subDirectory?: string; +}): string { + let tempDir = TMP_DIR_ROOT; + if (subDirectory != null) { + tempDir = path.join(tempDir, subDirectory); + } + + if (recreate) { + try { + fs.rmSync(tempDir, { recursive: true }); + } catch { + // Ignore if can't delete. Likely doesn't exist + } + } + + if (!fs.existsSync(tempDir)) { + fs.mkdirSync(tempDir); + } + + return tempDir; +} diff --git a/tsconfig.json b/tsconfig.json index b1d4f025..52f7ac1c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,8 +7,8 @@ "sourceMap": true, "rootDir": "src", "strict": true /* enable all strict type-checking options */, - "types": ["./src/types/global"], "paths": { + "@deephaven/require-jsapi": ["./packages/require-jsapi/src"], // workaround for: https://github.com/rollup/rollup/issues/5199#issuecomment-2095374821 "rollup/parseAst": ["./node_modules/rollup/dist/parseAst"] } @@ -19,5 +19,6 @@ // Exclude tests as they have their own tsconfigs. "e2e", "**/*.spec.ts" - ] + ], + "references": [{ "path": "./packages/require-jsapi" }] } diff --git a/tsconfig.unit.json b/tsconfig.unit.json index cc2b4772..bbbed895 100644 --- a/tsconfig.unit.json +++ b/tsconfig.unit.json @@ -2,5 +2,6 @@ "extends": "./tsconfig.json", "include": ["src/**/*.spec.ts"], // Override ./tsconfig `exclude` so that *.spec.ts files are included - "exclude": ["node_modules"] + "exclude": ["node_modules"], + "references": [{ "path": "./packages/require-jsapi" }] }