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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 17x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 17x 17x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 17x 17x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 1x 1x | import {
CopyObjectCommand,
DeleteObjectCommand,
GetObjectCommand,
HeadObjectCommand,
ListObjectsV2Command,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3';
import {adapter, type Errors, type IErrorMap, type IMeta} from '@feasibleone/blong/types';
import {createReadStream, statSync} from 'fs';
import {Readable} from 'stream';
export interface IConfig {
s3: {
region?: string;
endpoint?: string;
credentials?: {
accessKeyId: string;
secretAccessKey: string;
};
forcePathStyle?: boolean;
requestStreamBufferSize?: number;
};
bucket?: {
Bucket?: string;
};
url?: string;
context: {
s3?: S3Client;
};
}
const errorMap: IErrorMap = {
's3.generic': 'S3 Error',
's3.invalid': 'Invalid S3 Operation',
's3.notFound': 'S3 Object Not Found',
's3.exists': 'S3 Object Already Exists',
's3.accessDenied': 'S3 Access Denied',
's3.missingKey': 'Missing key value for {key}',
's3.missingBucket': 'Missing bucket parameter',
};
let _errors: Errors<typeof errorMap>;
export default adapter<IConfig>(({utError}) => {
_errors ||= utError.register(errorMap);
return {
activation: {
default: {
type: 's3',
s3: {
requestStreamBufferSize: 64 * 1024,
},
bucket: {},
},
},
async start() {
this.config.context = {s3: new S3Client(this.config.s3)};
super.connect();
return super.start();
},
async stop(...params: unknown[]) {
let result;
try {
this.config.context.s3!.destroy();
} finally {
this.config.context = {};
result = await super.stop(...params);
}
return result;
},
async exec(
params:
| ({
bucket?: string;
key?: string;
body?: PutObjectCommand['input']['Body'];
url?: string;
contentType?: string;
metadata?: Record<string, string>;
prefix?: string;
maxKeys?: number;
sourceBucket?: string;
sourceKey?: string;
} & Record<string, unknown>)
| unknown[],
$meta: IMeta,
) {
const {method} = $meta;
const [, , operation] = method!.split('.');
let bucket: string | undefined;
let actualParams = params;
if (!Array.isArray(params) && params.bucket) {
bucket = params.bucket;
const {bucket: _bucketParam, ...rest} = params;
actualParams = rest;
}
if (!bucket && !this.config.bucket?.Bucket) {
throw this.error(_errors['s3.missingBucket'](), $meta);
}
switch (operation) {
case 'get': {
// Get object from S3
if (Array.isArray(actualParams)) {
throw this.error(_errors['s3.invalid'](), $meta);
}
const {key} = actualParams;
if (!key) {
throw this.error(_errors['s3.missingKey']({key: 'key'}), $meta);
}
const command = new GetObjectCommand({
Bucket: bucket ?? this.config.bucket?.Bucket ?? '',
Key: key,
});
const response = await this.config.context.s3!.send(command);
return {
body: await response.Body?.transformToByteArray(),
contentType: response.ContentType,
contentLength: response.ContentLength,
metadata: response.Metadata,
lastModified: response.LastModified,
etag: response.ETag,
};
}
case 'add': {
// Put object to S3
if (Array.isArray(actualParams)) {
throw this.error(_errors['s3.invalid'](), $meta);
}
const {url, key, metadata} = actualParams;
let {body, contentType} = actualParams;
if (!key) {
throw this.error(_errors['s3.missingKey']({key: 'key'}), $meta);
}
let contentLength: number | undefined;
if (url && !body) {
if (/^https?:\/\//.test(url)) {
try {
const response = await fetch(url);
contentType ||= response.headers.get('content-type') ?? undefined;
contentLength = Number(response.headers.get('content-length'));
body =
contentLength > 0
? Readable.fromWeb(
response.body as import('stream/web').ReadableStream,
)
: Buffer.from(await response.arrayBuffer());
} catch (error) {
this.log?.error?.(
`Error fetching report from ${url}: ${(error as Error).message}`,
);
throw error;
}
} else {
contentLength = statSync(url).size;
body = createReadStream(url);
contentType ||= 'text/html';
}
}
if (body === undefined) {
throw this.error(_errors['s3.missingKey']({key: 'body'}), $meta);
}
const command = new PutObjectCommand({
Bucket: bucket ?? this.config.bucket?.Bucket ?? '',
Key: key,
Body: body as import('@aws-sdk/client-s3').PutObjectCommandInput['Body'],
ContentType: contentType ?? undefined,
...(contentLength != null &&
contentLength > 0 && {ContentLength: contentLength}),
Metadata: metadata,
});
const putResult = await this.config.context.s3!.send(command);
return this.config.url?.replace?.('{key}', key) ?? {key, etag: putResult.ETag};
}
case 'delete':
case 'remove': {
// Delete object from S3
if (Array.isArray(actualParams)) {
throw this.error(_errors['s3.invalid'](), $meta);
}
const {key} = actualParams;
if (!key) {
throw this.error(_errors['s3.missingKey']({key: 'key'}), $meta);
}
const command = new DeleteObjectCommand({
Bucket: bucket ?? this.config.bucket?.Bucket ?? '',
Key: key,
});
return this.config.context.s3!.send(command);
}
case 'list':
case 'find': {
// List objects in S3 bucket
if (Array.isArray(actualParams)) {
throw this.error(_errors['s3.invalid'](), $meta);
}
const {prefix, maxKeys = 1000} = actualParams;
const command = new ListObjectsV2Command({
Bucket: bucket ?? this.config.bucket?.Bucket ?? '',
Prefix: prefix,
MaxKeys: maxKeys,
});
return this.config.context.s3!.send(command);
}
case 'head':
case 'metadata': {
// Get object metadata
if (Array.isArray(actualParams)) {
throw this.error(_errors['s3.invalid'](), $meta);
}
const {key} = actualParams;
if (!key) {
throw this.error(_errors['s3.missingKey']({key: 'key'}), $meta);
}
const command = new HeadObjectCommand({
Bucket: bucket ?? this.config.bucket?.Bucket ?? '',
Key: key,
});
return this.config.context.s3!.send(command);
}
case 'copy': {
// Copy object within S3
if (Array.isArray(actualParams)) {
throw this.error(_errors['s3.invalid'](), $meta);
}
const {key, sourceBucket, sourceKey} = actualParams;
if (!key) {
throw this.error(_errors['s3.missingKey']({key: 'key'}), $meta);
}
if (!sourceBucket) {
throw this.error(_errors['s3.missingKey']({key: 'sourceBucket'}), $meta);
}
if (!sourceKey) {
throw this.error(_errors['s3.missingKey']({key: 'sourceKey'}), $meta);
}
const command = new CopyObjectCommand({
Bucket: bucket ?? this.config.bucket?.Bucket ?? '',
Key: key,
CopySource: `${sourceBucket}/${sourceKey}`,
});
return this.config.context.s3!.send(command);
}
}
throw this.error(_errors['s3.generic']({}), $meta);
},
};
});
|