Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 2x 3x 1x 1x 1x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 5x 3x 3x 3x 3x 3x 2x 2x 2x 2x 3x 3x 3x 2x 3x 1x 1x 1x 3x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 8x 16x 16x 16x 16x 16x 4x 4x 4x 4x 4x 4x 3x 4x 4x 4x 5x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 11x 11x 11x 11x 11x 11x 11x 6x 4x 4x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 8x 8x 8x 8x 8x 8x 13x 13x 13x 1x 1x 8x 1x 1x 1x 1x 1x 1x 1x | import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import {
FileDescriptor,
MultiPageApplicationAndComponents,
UploadApplicationResponse,
UploadApplicationV1Response,
UploadApplicationV2Response,
} from "./types/types";
const SUPERBLOCKS_CLI_VERSION_HEADER = "x-superblocks-cli-version";
const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
const HEALTH_PATH = `/health`;
const INTEGRATIONS_PATH = `/integration`;
const COMPONENTS_V1_PATH = `/api/v1/components`;
const COMPONENTS_V2_PATH = `/api/v2/components`;
const APPLICATIONS_PATH = `/api/v1/applications`;
import FormData from "form-data";
const DEFAULT_MAX_TOTAL_FILE_SIZE_MB = 10;
export class Bucketeer {
public readonly baseUrl: string;
private token: string;
public readonly cliVersion: string;
private headers: Record<string, string>;
private maxTotalFileSizeMB: number;
constructor({
baseUrl,
token,
cliVersion,
headers,
maxTotalFileSizeMB,
}: {
baseUrl: string;
token: string;
cliVersion: string;
headers?: Record<string, string>;
maxTotalFileSizeMB?: number;
}) {
this.baseUrl = baseUrl.replace(/\/+$/, ""); // remove trailing slashes
this.token = token;
this.cliVersion = cliVersion;
this.headers = headers ?? {};
this.maxTotalFileSizeMB =
maxTotalFileSizeMB ?? DEFAULT_MAX_TOTAL_FILE_SIZE_MB;
}
public async isConnected(): Promise<boolean> {
try {
const response = await this.submitRequest({
method: "get",
path: `${HEALTH_PATH}`,
});
return response.data.status === "healthy";
} catch (e: any) {
const message = this.messageFromError(e);
throw new Error(`Could not connect to ${this.baseUrl}: ${message}`);
}
}
public async fetchIntegration(integrationId: string): Promise<string> {
try {
const response = await this.submitRequest({
method: "get",
path: `${INTEGRATIONS_PATH}/${integrationId}`,
headers: {
"Content-Type": "multipart/form-data",
},
});
return response.data.file;
} catch (e: any) {
const message = this.messageFromError(e);
throw new Error(`Could not fetch integration: ${message}`);
}
}
public async uploadIntegration(integrationId: string, file: FileDescriptor) {
// Check the total file size first
const error = this.checkContentWithinSizeLimit(file);
if (error) {
throw error;
}
try {
const fileDescriptorMap = createFileDescriptorMap(file);
const formData = createFormData(fileDescriptorMap);
const response = await this.submitRequest({
method: "post",
path: `${INTEGRATIONS_PATH}/${integrationId}`,
formData: formData,
});
return response.data.url;
} catch (e: any) {
const message = this.messageFromError(e);
throw new Error(`Could not upload integration: ${message}`);
}
}
public async fetchComponents(
applicationId: string,
branch?: string,
): Promise<MultiPageApplicationAndComponents> {
try {
const path = branch
? `${COMPONENTS_V1_PATH}/${applicationId}/branches/${branch}`
: `${COMPONENTS_V1_PATH}/${applicationId}`;
const response = await this.submitRequest({
method: "get",
path,
});
return response.data;
} catch (e: any) {
const message = this.messageFromError(e);
throw new Error(`Could not fetch components: ${message}`);
}
}
public async uploadComponents({
applicationId,
files,
branch,
componentConfigs,
}: {
applicationId: string;
files: FileDescriptor[];
branch?: string;
componentConfigs: Record<string, any>;
}): Promise<UploadApplicationV1Response> {
// Check the total file size first
const error = this.checkContentWithinSizeLimit(...files);
if (error) {
throw error;
}
const fileDescriptorMap = createMultiFileDescriptorMap(files);
const uploadFiles = createUploadFiles(fileDescriptorMap);
const srcFiles = uploadFiles.filter(
(file) => !file.filename.startsWith("dist"),
);
const buildFiles = uploadFiles.filter((file) =>
file.filename.startsWith("dist"),
);
const formatJSON = JSON.stringify({
applicationId,
branch,
registeredComponents: componentConfigs,
srcFiles,
buildFiles,
});
try {
const formData = createFormData(fileDescriptorMap);
const response = await this.submitRequest({
method: "post",
path: `${COMPONENTS_V1_PATH}/${applicationId}`,
data: formatJSON,
formData: formData,
});
return response.data;
} catch (e: any) {
const message = this.messageFromError(e);
throw new Error(`Could not fetch integration: ${message}`);
}
}
public async uploadComponentsV2({
applicationId,
files,
branch,
}: {
applicationId: string;
files: FileDescriptor[];
branch?: string;
}): Promise<UploadApplicationV2Response> {
// Check the total file size first
const error = this.checkContentWithinSizeLimit(...files);
if (error) {
throw error;
}
const fileDescriptorMap = createMultiFileDescriptorMap(files);
const uploadFiles = createUploadFiles(fileDescriptorMap);
const srcFiles = uploadFiles.filter(
(file) => !file.filename.startsWith("dist"),
);
const buildFiles = uploadFiles.filter((file) =>
file.filename.startsWith("dist"),
);
const formatJSON = JSON.stringify({
applicationId,
branch,
srcFiles,
buildFiles,
});
try {
const formData = createFormData(fileDescriptorMap);
const response = await this.submitRequest({
method: "post",
path: `${COMPONENTS_V2_PATH}/${applicationId}`,
data: formatJSON,
formData: formData,
});
return response.data;
} catch (e: any) {
const message = this.messageFromError(e);
throw new Error(`Could not fetch integration: ${message}`);
}
}
public async uploadApplication(
files: FileDescriptor[],
): Promise<UploadApplicationResponse> {
try {
const fileDescriptorMap = createMultiFileDescriptorMap(files);
const formData = createFormData(fileDescriptorMap);
const response = await this.submitRequest({
method: "post",
path: `${APPLICATIONS_PATH}/upload`,
headers: {
"Content-Type": "multipart/form-data",
},
formData: formData,
});
return response.data;
} catch (e: any) {
const message = this.messageFromError(e);
throw new Error(`Could not upload application: ${message}`);
}
}
private async submitRequest({
method,
path,
data,
formData,
headers = {},
}: {
method: string;
path: string;
data?: any;
formData?: FormData;
headers?: Record<string, string>;
}): Promise<AxiosResponse<any, any>> {
const requestUrl = new URL(`${this.baseUrl}${path}`).toString();
const config: AxiosRequestConfig = {
method,
url: requestUrl,
data: data,
headers: {
// First add the headers supplied by the caller
...headers,
// Then set the headers in the client, possibly overriding the caller's headers
...this.headers,
Authorization: "Bearer " + this.token,
[SUPERBLOCKS_CLI_VERSION_HEADER]: this.cliVersion,
[SUPERBLOCKS_URL_HEADER]: this.baseUrl,
},
};
if (formData) {
config.data = formData;
}
// Execute the request
return await axios(config);
}
private messageFromError(e: any): string {
let message: string;
if (axios.isAxiosError(e)) {
message =
(e.response?.data?.responseMeta?.message as string) ??
JSON.stringify(e.response?.data) ??
e.response?.statusText ??
e?.message ??
// eslint-disable-next-line @typescript-eslint/no-base-to-string
`${e}`;
} else {
message = `${e?.message ? e?.message : e}`;
}
return message;
}
private checkContentWithinSizeLimit(
...files: FileDescriptor[]
): Error | undefined {
const totalFileSizeMB = FileDescriptor.totalFileSizeMB(files);
if (totalFileSizeMB > this.maxTotalFileSizeMB) {
return new Error(`upload size exceeded maximum allowed size.
Current Size: ${totalFileSizeMB.toFixed(2)} MB
Max Allowed Size: ${this.maxTotalFileSizeMB.toFixed(2)} MB
You can reduce your component bundle size by uploading static assets to a separate service.`);
}
return undefined;
}
}
type UploadFile = {
name: string;
filename: string;
contentType: string | undefined;
};
function createFileDescriptorMap(
file: FileDescriptor,
): Record<string, FileDescriptor> {
return {
file: file,
};
}
function createMultiFileDescriptorMap(
files: FileDescriptor[],
): Record<string, FileDescriptor> {
return files.reduce(
(acc, file, currentIndex) => {
acc[`file${currentIndex + 1}`] = file;
return acc;
},
{} as Record<string, FileDescriptor>,
);
}
function createUploadFiles(
files: Record<string, FileDescriptor>,
): UploadFile[] {
return Object.entries(files).map(([name, file]) => ({
name,
filename: file.getNormalizedFilename(),
contentType: file.contentType,
}));
}
function createFormData(files: Record<string, FileDescriptor>) {
const formData = new FormData();
for (const [name, descriptor] of Object.entries(files)) {
const filename = descriptor.filename.replace(/\\/g, "/");
formData.append(name, descriptor.content, filename);
if (descriptor.relativePath) {
formData.append(
`${name}-relative-path`,
descriptor.getNormalizedRelativePath(),
);
}
}
return formData;
}
|