-
Notifications
You must be signed in to change notification settings - Fork 4
feat: open in Deepnote command #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7b0fe38
feat: open in Deepnote command
saltenasl aa755bd
feat: disable ssl checks settings option
saltenasl b765a76
feat: rm disabling ssl checks option
saltenasl 63cc72a
feat: tweak config
saltenasl cf52b47
feat: config name
saltenasl 3ea2618
fix: create new deepnote file with the correct attributes
saltenasl 27cf287
fix: create new blocks in their own group instead of sharing a single…
saltenasl 21bf3ce
chore: fix test
saltenasl 605536b
Merge branch 'main' into ls/open-in-deepnote
saltenasl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { workspace } from 'vscode'; | ||
| import { logger } from '../../platform/logging'; | ||
| import * as https from 'https'; | ||
| import fetch from 'node-fetch'; | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Response from the import initialization endpoint | ||
| */ | ||
| export interface InitImportResponse { | ||
| importId: string; | ||
| uploadUrl: string; | ||
| expiresAt: string; | ||
| } | ||
|
|
||
| /** | ||
| * Error response from the API | ||
| */ | ||
| export interface ApiError { | ||
| message: string; | ||
| statusCode: number; | ||
| } | ||
|
|
||
| /** | ||
| * Maximum file size for uploads (100MB) | ||
| */ | ||
| export const MAX_FILE_SIZE = 100 * 1024 * 1024; | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Gets the API endpoint from configuration | ||
| */ | ||
| function getApiEndpoint(): string { | ||
| const config = workspace.getConfiguration('deepnote'); | ||
| return config.get<string>('apiEndpoint', 'https://api.deepnote.com'); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if SSL verification should be disabled | ||
| */ | ||
| function shouldDisableSSLVerification(): boolean { | ||
| const config = workspace.getConfiguration('deepnote'); | ||
| return config.get<boolean>('disableSSLVerification', false); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an HTTPS agent with optional SSL verification disabled | ||
| */ | ||
| function createHttpsAgent(): https.Agent | undefined { | ||
| if (shouldDisableSSLVerification()) { | ||
| logger.warn('SSL certificate verification is disabled. This should only be used in development.'); | ||
| // Create agent with options that bypass both certificate and hostname verification | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const agentOptions: any = { | ||
| rejectUnauthorized: false, | ||
| checkServerIdentity: () => { | ||
| // Return undefined to indicate the check passed | ||
| return undefined; | ||
| } | ||
| }; | ||
| return new https.Agent(agentOptions); | ||
| } | ||
| return undefined; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Initializes an import by requesting a presigned upload URL | ||
| * | ||
| * @param fileName - Name of the file to import | ||
| * @param fileSize - Size of the file in bytes | ||
| * @returns Promise with import ID, upload URL, and expiration time | ||
| * @throws ApiError if the request fails | ||
| */ | ||
| export async function initImport(fileName: string, fileSize: number): Promise<InitImportResponse> { | ||
| const apiEndpoint = getApiEndpoint(); | ||
| const url = `${apiEndpoint}/v1/import/init`; | ||
|
|
||
| // Temporarily disable SSL verification at the process level if configured | ||
| const originalEnvValue = process.env.NODE_TLS_REJECT_UNAUTHORIZED; | ||
| if (shouldDisableSSLVerification()) { | ||
| process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | ||
| logger.debug('Set NODE_TLS_REJECT_UNAUTHORIZED=0'); | ||
| } | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| const agent = createHttpsAgent(); | ||
| logger.debug(`SSL verification disabled: ${shouldDisableSSLVerification()}`); | ||
| logger.debug(`Agent created: ${!!agent}`); | ||
| if (agent) { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| logger.debug(`Agent rejectUnauthorized: ${(agent as any).options?.rejectUnauthorized}`); | ||
| } | ||
|
|
||
| interface FetchOptions { | ||
| method: string; | ||
| headers: Record<string, string>; | ||
| body: string; | ||
| agent?: https.Agent; | ||
| } | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const options: FetchOptions = { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| body: JSON.stringify({ | ||
| fileName, | ||
| fileSize | ||
| }) | ||
| }; | ||
|
|
||
| if (agent) { | ||
| options.agent = agent; | ||
| logger.debug('Agent attached to request'); | ||
| logger.debug(`Options agent set: ${!!options.agent}`); | ||
| } | ||
|
|
||
| const response = await fetch(url, options); | ||
|
|
||
| if (!response.ok) { | ||
| const responseBody = await response.text(); | ||
| logger.error(`Init import failed - Status: ${response.status}, URL: ${url}, Body: ${responseBody}`); | ||
|
|
||
| const error: ApiError = { | ||
| message: responseBody, | ||
| statusCode: response.status | ||
| }; | ||
| throw error; | ||
| } | ||
|
|
||
| return await response.json(); | ||
| } finally { | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Restore original SSL verification setting | ||
| if (shouldDisableSSLVerification()) { | ||
| if (originalEnvValue === undefined) { | ||
| delete process.env.NODE_TLS_REJECT_UNAUTHORIZED; | ||
| } else { | ||
| process.env.NODE_TLS_REJECT_UNAUTHORIZED = originalEnvValue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Uploads a file to the presigned S3 URL using node-fetch | ||
| * | ||
| * @param uploadUrl - Presigned S3 URL for uploading | ||
| * @param fileBuffer - File contents as a Buffer | ||
| * @param onProgress - Optional callback for upload progress (0-100) | ||
| * @returns Promise that resolves when upload is complete | ||
| * @throws ApiError if the upload fails | ||
| */ | ||
| export async function uploadFile( | ||
| uploadUrl: string, | ||
| fileBuffer: Buffer, | ||
| onProgress?: (progress: number) => void | ||
| ): Promise<void> { | ||
| // Note: Progress tracking is limited in Node.js without additional libraries | ||
| // For now, we'll report 50% at start and 100% at completion | ||
| if (onProgress) { | ||
| onProgress(50); | ||
| } | ||
|
|
||
| const response = await fetch(uploadUrl, { | ||
| method: 'PUT', | ||
| headers: { | ||
| 'Content-Type': 'application/octet-stream', | ||
| 'Content-Length': fileBuffer.length.toString() | ||
| }, | ||
| body: fileBuffer | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const responseText = await response.text(); | ||
| logger.error(`Upload failed - Status: ${response.status}, Response: ${responseText}, URL: ${uploadUrl}`); | ||
| const error: ApiError = { | ||
| message: responseText || 'Upload failed', | ||
| statusCode: response.status | ||
| }; | ||
| throw error; | ||
| } | ||
|
|
||
| if (onProgress) { | ||
| onProgress(100); | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Gets a user-friendly error message for an API error | ||
| * Logs the full error details for debugging | ||
| * | ||
| * @param error - The error object | ||
| * @returns A user-friendly error message | ||
| */ | ||
| export function getErrorMessage(error: unknown): string { | ||
| // Log the full error details for debugging | ||
| logger.error('Import error details:', error); | ||
|
|
||
| if (typeof error === 'object' && error !== null && 'statusCode' in error) { | ||
| const apiError = error as ApiError; | ||
|
|
||
| // Log API error specifics | ||
| logger.error(`API Error - Status: ${apiError.statusCode}, Message: ${apiError.message}`); | ||
|
|
||
| // Handle rate limiting specifically | ||
| if (apiError.statusCode === 429) { | ||
| return 'Too many requests. Please try again in a few minutes.'; | ||
| } | ||
|
|
||
| // All other API errors return the message from the server | ||
| if (apiError.statusCode >= 400) { | ||
| return apiError.message || 'An error occurred. Please try again.'; | ||
| } | ||
| } | ||
|
|
||
| if (error instanceof Error) { | ||
| logger.error(`Error message: ${error.message}`, error.stack); | ||
| if (error.message.includes('fetch') || error.message.includes('Network')) { | ||
| return 'Failed to connect. Check your connection and try again.'; | ||
| } | ||
| return error.message; | ||
| } | ||
|
|
||
| logger.error('Unknown error type:', typeof error, error); | ||
| return 'An unknown error occurred'; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.