mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-01 11:33:41 +03:00
s3: lazy load aws sdk for code splitting
This commit is contained in:
parent
e3b2e166d7
commit
6a5b0e3a83
35
pkg/interface/src/logic/lib/S3Client.ts
Normal file
35
pkg/interface/src/logic/lib/S3Client.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { StorageClient, StorageUpload, UploadParams } from './StorageClient';
|
||||
import type S3 from 'aws-sdk/clients/s3';
|
||||
|
||||
export default class S3Client implements StorageClient {
|
||||
config: S3.ClientConfiguration;
|
||||
client: S3 | null = null;
|
||||
S3: typeof import('aws-sdk/clients/s3');
|
||||
|
||||
constructor(config: S3.ClientConfiguration) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
async initAndUpload(params: UploadParams) {
|
||||
if (!this.S3) {
|
||||
await this.loadLib();
|
||||
}
|
||||
|
||||
if (!this.client) {
|
||||
this.client = new this.S3(this.config);
|
||||
}
|
||||
|
||||
return this.client.upload(params);
|
||||
}
|
||||
|
||||
upload(params: UploadParams): StorageUpload {
|
||||
const upload = this.initAndUpload.bind(this);
|
||||
return {
|
||||
promise: () => upload(params)
|
||||
};
|
||||
}
|
||||
|
||||
async loadLib() {
|
||||
this.S3 = (await import('aws-sdk/clients/s3')).default;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import S3 from 'aws-sdk/clients/s3';
|
||||
import S3Client from './S3Client';
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import useStorageState from '../state/storage';
|
||||
import GcpClient from './GcpClient';
|
||||
@ -32,7 +32,7 @@ const useStorage = ({ accept = '*' } = { accept: '*' }): IuseStorage => {
|
||||
!s3.credentials.secretAccessKey) {
|
||||
return;
|
||||
}
|
||||
client.current = new S3({
|
||||
client.current = new S3Client({
|
||||
credentials: s3.credentials,
|
||||
endpoint: s3.credentials.endpoint
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user