Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Sep 10, 2024
1 parent 43890f2 commit 6fb3c21
Showing 1 changed file with 1 addition and 66 deletions.
67 changes: 1 addition & 66 deletions packages/core/src/lib/disk.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
import { debuglog } from 'node:util'
import { Readable } from 'stream'

import {
HeadBucketCommand,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3'
import { Result } from '@latitude-data/core/lib/Result'
import { env } from '@latitude-data/env'
import { Disk, errors } from 'flydrive'
import { FSDriver } from 'flydrive/drivers/fs'
import { S3Driver } from 'flydrive/drivers/s3'
import { WriteOptions } from 'flydrive/types'

var debug_default = debuglog('latitude:debug')

const generateUrl = (publicPath: string) => async (key: string) =>
`/${publicPath}/${key}`

function logRequestMiddleware() {
// @ts-ignore
const logRequestMiddleware = (next, _context) => async (args) => {
debug_default('Request: %O', JSON.stringify(args.request.headers))

return next(args)
}

// @ts-ignore
logRequestMiddleware.applyToStack = (clientStack) => {
clientStack.add(logRequestMiddleware, {
step: 'finalizeRequest', // use 'serialize' step for v3 of aws-sdk
name: 'logRequestMiddleware',
priority: 'high', // Ensure this middleware is run first
})
}

return logRequestMiddleware
}
/**
* These env variables are set in production.
* If you want to test this locally, you need to set them in your machine.
Expand Down Expand Up @@ -95,47 +68,9 @@ export class DiskWrapper {
return this.putStream(key, contents)
}

async rawS3ClientPutStream(
key: string,
contents: Readable,
options?: WriteOptions,
) {
debugger
const awsConfig = getAwsConfig()
const client = new S3Client({
credentials: awsConfig.credentials,
region: awsConfig.region,
})
client.middlewareStack.use(logRequestMiddleware())
return new Promise((resolve, reject) => {
debugger
contents.once('error', reject)
const commandOptions = {
Bucket: awsConfig.bucket,
Key: key,
ContentType: 'text/csv',
ContentLength: contents.readableLength,
Body: contents,
}
debug_default('commandOptions: %O', commandOptions)
const command = new PutObjectCommand(commandOptions)
return client
.send(command)
.then((data) => {
debugger
debug_default('file %s:%s written', awsConfig.bucket, key)
resolve(data)
})
.catch((e) => {
debugger
debug_default('Error: ', e)
reject(e)
})
})
}

async putStream(key: string, contents: Readable, options?: WriteOptions) {
try {
debugger

Check failure on line 73 in packages/core/src/lib/disk.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

Unexpected 'debugger' statement
await this.disk.putStream(key, contents, {
...options,
contentLength: contents.readableLength,
Expand Down

0 comments on commit 6fb3c21

Please sign in to comment.