• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

StauroDEV / filebase-upload / 17125349975

21 Aug 2025 11:17AM UTC coverage: 83.509%. Remained the same
17125349975

push

github

talentlessguy
prepare for npm

10 of 25 branches covered (40.0%)

Branch coverage included in aggregate %.

228 of 260 relevant lines covered (87.69%)

3.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.27
/mod.ts
1
import { FILEBASE_API_URL } from './constants.ts'
1✔
2
import type { HeaderBag, HttpRequest as IHttpRequest, QueryParameterBag } from '@smithy/types'
3

4
import type aws4 from 'aws4'
5
import type { RequiredArgs } from './types.ts'
6

7
import { createBucket, formatUrl, fromEnv, generateFilebaseRequestOptions, parseUrl, presignRequest } from './utils.ts'
1✔
8

9
class HttpRequest {
1✔
10
  method: string
1✔
11
  protocol: string
2✔
12
  hostname: string
2✔
13
  port?: number
2✔
14
  path: string
2✔
15
  query?: QueryParameterBag | undefined
2✔
16
  headers: HeaderBag
2✔
17
  username?: string
2✔
18
  password?: string
2✔
19
  fragment?: string
2✔
20
  body?: unknown
1✔
21

22
  constructor(options: IHttpRequest) {
1✔
23
    this.method = options.method || 'GET'
×
24
    this.hostname = options.hostname || 'localhost'
×
25
    this.port = options.port
2✔
26
    this.query = options.query || {}
2✔
27
    this.headers = options.headers || {}
×
28
    this.body = options.body
2✔
29
    this.protocol = options.protocol
×
30
      ? options.protocol.slice(-1) !== ':' ? `${options.protocol}:` : options.protocol
×
31
      : 'https:'
×
32
    this.path = options.path ? (options.path.charAt(0) !== '/' ? `/${options.path}` : options.path) : '/'
×
33
    this.username = options.username
2✔
34
    this.password = options.password
2✔
35
    this.fragment = options.fragment
2✔
36
  }
2✔
37
}
1✔
38

39
export const createPresignedUrl = async (
1✔
40
  { bucketName, apiUrl, file, token }: {
1✔
41
    file: File
42
  } & RequiredArgs,
1✔
43
): Promise<string> => {
44
  await createBucket({ bucketName, apiUrl, token })
10✔
45
  const url = parseUrl(`https://${apiUrl ?? FILEBASE_API_URL}/${bucketName}/${file.name}`)
2✔
46
  const { accessKeyId, secretAccessKey } = fromEnv(token)()
2✔
47

48
  const signedRequest = presignRequest(
2✔
49
    new HttpRequest({ ...url, method: 'PUT', headers: {} }),
10✔
50
    {
2✔
51
      accessKeyId,
2✔
52
      secretAccessKey,
2✔
53
      region: 'us-east-1',
2✔
54
      expiresIn: 3600,
2✔
55
    },
2✔
56
  )
57
  return formatUrl(signedRequest)
2✔
58
}
1✔
59

60
export const uploadCar = async ({ file, ...args }: RequiredArgs & { file: File }): Promise<Response> => {
1✔
61
  const url = await createPresignedUrl({ ...args, file })
8✔
62

63
  const res = await fetch(decodeURIComponent(url), {
2✔
64
    method: 'PUT',
2✔
65
    body: file,
2✔
66
    headers: { 'x-amz-meta-import': 'car' },
6✔
67
  })
2✔
68

69
  return res
2✔
70
}
1✔
71

72
export const headObject = async (
1✔
73
  { bucketName, filename, apiUrl, token }: RequiredArgs & {
1✔
74
    filename: string
75
  },
1✔
76
): Promise<[boolean, string | null]> => {
77
  let requestOptions: aws4.Request & { key?: string } = {
2✔
78
    host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
2✔
79
    path: `/${filename}`,
2✔
80
    key: `/${filename}`,
2✔
81
    region: 'us-east-1',
2✔
82
    method: 'HEAD',
2✔
83
    service: 's3',
2✔
84
    headers: {},
2✔
85
  }
2✔
86
  requestOptions = generateFilebaseRequestOptions(
2✔
87
    token,
2✔
88
    requestOptions,
2✔
89
  )
90
  return await fetch(
2✔
91
    `https://${requestOptions.host}${requestOptions.path}`,
2✔
92
    requestOptions as RequestInit,
2✔
93
  )
94
    .then((res) => [res.status == 200, res.headers.get('x-amz-meta-cid')])
10✔
95
}
1✔
96

97
export const getObject = async (
1✔
98
  { bucketName, filename, apiUrl, token }: RequiredArgs & {
1✔
99
    filename: string
100
  },
1✔
101
): Promise<Response> => {
102
  let requestOptions: aws4.Request & { key?: string } = {
3✔
103
    host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
3✔
104
    path: `/${filename}`,
3✔
105
    key: `/${filename}`,
3✔
106
    region: 'us-east-1',
3✔
107
    method: 'GET',
3✔
108
    service: 's3',
3✔
109
    headers: {},
3✔
110
  }
3✔
111
  requestOptions = generateFilebaseRequestOptions(
3✔
112
    token,
3✔
113
    requestOptions,
3✔
114
  )
115
  return await fetch(
3✔
116
    `https://${requestOptions.host}${requestOptions.path}`,
3✔
117
    requestOptions as RequestInit,
3✔
118
  )
119
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc