|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { workspace } from 'vscode'; |
| 5 | +import { logger } from '../../platform/logging'; |
| 6 | +import fetch from 'node-fetch'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Response from the import initialization endpoint |
| 10 | + */ |
| 11 | +export interface InitImportResponse { |
| 12 | + importId: string; |
| 13 | + uploadUrl: string; |
| 14 | + expiresAt: string; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Error response from the API |
| 19 | + */ |
| 20 | +export interface ApiError { |
| 21 | + message: string; |
| 22 | + statusCode: number; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Maximum file size for uploads (100MB) |
| 27 | + */ |
| 28 | +export const MAX_FILE_SIZE = 100 * 1024 * 1024; |
| 29 | + |
| 30 | +/** |
| 31 | + * Gets the Deepnote domain from configuration |
| 32 | + */ |
| 33 | +function getDomain(): string { |
| 34 | + const config = workspace.getConfiguration('deepnote'); |
| 35 | + return config.get<string>('domain', 'deepnote.com'); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Gets the API endpoint from configuration |
| 40 | + */ |
| 41 | +function getApiEndpoint(): string { |
| 42 | + const domain = getDomain(); |
| 43 | + return `https://api.${domain}`; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Initializes an import by requesting a presigned upload URL |
| 48 | + * |
| 49 | + * @param fileName - Name of the file to import |
| 50 | + * @param fileSize - Size of the file in bytes |
| 51 | + * @returns Promise with import ID, upload URL, and expiration time |
| 52 | + * @throws ApiError if the request fails |
| 53 | + */ |
| 54 | +export async function initImport(fileName: string, fileSize: number): Promise<InitImportResponse> { |
| 55 | + const apiEndpoint = getApiEndpoint(); |
| 56 | + const url = `${apiEndpoint}/v1/import/init`; |
| 57 | + |
| 58 | + const response = await fetch(url, { |
| 59 | + method: 'POST', |
| 60 | + headers: { |
| 61 | + 'Content-Type': 'application/json' |
| 62 | + }, |
| 63 | + body: JSON.stringify({ |
| 64 | + fileName, |
| 65 | + fileSize |
| 66 | + }) |
| 67 | + }); |
| 68 | + |
| 69 | + if (!response.ok) { |
| 70 | + const responseBody = await response.text(); |
| 71 | + logger.error(`Init import failed - Status: ${response.status}, URL: ${url}, Body: ${responseBody}`); |
| 72 | + |
| 73 | + const error: ApiError = { |
| 74 | + message: responseBody, |
| 75 | + statusCode: response.status |
| 76 | + }; |
| 77 | + throw error; |
| 78 | + } |
| 79 | + |
| 80 | + return await response.json(); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Uploads a file to the presigned S3 URL using node-fetch |
| 85 | + * |
| 86 | + * @param uploadUrl - Presigned S3 URL for uploading |
| 87 | + * @param fileBuffer - File contents as a Buffer |
| 88 | + * @param onProgress - Optional callback for upload progress (0-100) |
| 89 | + * @returns Promise that resolves when upload is complete |
| 90 | + * @throws ApiError if the upload fails |
| 91 | + */ |
| 92 | +export async function uploadFile( |
| 93 | + uploadUrl: string, |
| 94 | + fileBuffer: Buffer, |
| 95 | + onProgress?: (progress: number) => void |
| 96 | +): Promise<void> { |
| 97 | + // Note: Progress tracking is limited in Node.js without additional libraries |
| 98 | + // For now, we'll report 50% at start and 100% at completion |
| 99 | + if (onProgress) { |
| 100 | + onProgress(50); |
| 101 | + } |
| 102 | + |
| 103 | + const response = await fetch(uploadUrl, { |
| 104 | + method: 'PUT', |
| 105 | + headers: { |
| 106 | + 'Content-Type': 'application/octet-stream', |
| 107 | + 'Content-Length': fileBuffer.length.toString() |
| 108 | + }, |
| 109 | + body: fileBuffer |
| 110 | + }); |
| 111 | + |
| 112 | + if (!response.ok) { |
| 113 | + const responseText = await response.text(); |
| 114 | + logger.error(`Upload failed - Status: ${response.status}, Response: ${responseText}, URL: ${uploadUrl}`); |
| 115 | + const error: ApiError = { |
| 116 | + message: responseText || 'Upload failed', |
| 117 | + statusCode: response.status |
| 118 | + }; |
| 119 | + throw error; |
| 120 | + } |
| 121 | + |
| 122 | + if (onProgress) { |
| 123 | + onProgress(100); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Gets a user-friendly error message for an API error |
| 129 | + * Logs the full error details for debugging |
| 130 | + * |
| 131 | + * @param error - The error object |
| 132 | + * @returns A user-friendly error message |
| 133 | + */ |
| 134 | +export function getErrorMessage(error: unknown): string { |
| 135 | + // Log the full error details for debugging |
| 136 | + logger.error('Import error details:', error); |
| 137 | + |
| 138 | + if (typeof error === 'object' && error !== null && 'statusCode' in error) { |
| 139 | + const apiError = error as ApiError; |
| 140 | + |
| 141 | + // Log API error specifics |
| 142 | + logger.error(`API Error - Status: ${apiError.statusCode}, Message: ${apiError.message}`); |
| 143 | + |
| 144 | + // Handle rate limiting specifically |
| 145 | + if (apiError.statusCode === 429) { |
| 146 | + return 'Too many requests. Please try again in a few minutes.'; |
| 147 | + } |
| 148 | + |
| 149 | + // All other API errors return the message from the server |
| 150 | + if (apiError.statusCode >= 400) { |
| 151 | + return apiError.message || 'An error occurred. Please try again.'; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (error instanceof Error) { |
| 156 | + logger.error(`Error message: ${error.message}`, error.stack); |
| 157 | + if (error.message.includes('fetch') || error.message.includes('Network')) { |
| 158 | + return 'Failed to connect. Check your connection and try again.'; |
| 159 | + } |
| 160 | + return error.message; |
| 161 | + } |
| 162 | + |
| 163 | + logger.error('Unknown error type:', typeof error, error); |
| 164 | + return 'An unknown error occurred'; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * Gets the Deepnote domain from configuration for building launch URLs |
| 169 | + */ |
| 170 | +export function getDeepnoteDomain(): string { |
| 171 | + return getDomain(); |
| 172 | +} |
0 commit comments