Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { version as nodeVersion } from 'process'
import semver from 'semver'
import { describe, test, expect, beforeAll } from 'vitest'

import { streamToString } from '../test/util.js'

import { Blobs } from './main.js'

beforeAll(async () => {
Expand Down Expand Up @@ -55,9 +57,12 @@ describe('get', () => {
fetcher,
siteID,
})
const val = await blobs.get(key)

expect(val).toBe(value)
const string = await blobs.get(key)
expect(string).toBe(value)

const stream = await blobs.get(key, { type: 'stream' })
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
})

test('Returns `null` when the pre-signed URL returns a 404', async () => {
Expand Down
30 changes: 12 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ enum HTTPMethod {
Put = 'put',
}

enum ResponseType {
ArrayBuffer = 'arrayBuffer',
Blob = 'blob',
JSON = 'json',
Stream = 'stream',
Text = 'text',
}

interface SetOptions {
ttl?: Date | number
}
Expand Down Expand Up @@ -153,13 +145,15 @@ export class Blobs {
}

async get(key: string): Promise<string>
async get(key: string, { type }: { type: ResponseType.ArrayBuffer }): Promise<ArrayBuffer>
async get(key: string, { type }: { type: ResponseType.Blob }): Promise<Blob>
async get(key: string, { type }: { type: ResponseType.Stream }): Promise<ReadableStream | null>
async get(key: string, { type }: { type: ResponseType.Text }): Promise<string>
async get(key: string, { type }: { type: 'arrayBuffer' }): Promise<ArrayBuffer>
async get(key: string, { type }: { type: 'blob' }): Promise<Blob>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async get(key: string, { type }: { type: 'json' }): Promise<any>
async get(key: string, { type }: { type: 'stream' }): Promise<ReadableStream>
async get(key: string, { type }: { type: 'text' }): Promise<string>
async get(
key: string,
options?: { type: ResponseType },
options?: { type: 'arrayBuffer' | 'blob' | 'json' | 'stream' | 'text' },
): Promise<ArrayBuffer | Blob | ReadableStream | string | null> {
const { type } = options ?? {}
const res = await this.makeStoreRequest(key, HTTPMethod.Get)
Expand All @@ -177,23 +171,23 @@ export class Blobs {
return res
}

if (type === undefined || type === ResponseType.Text) {
if (type === undefined || type === 'text') {
return res.text()
}

if (type === ResponseType.ArrayBuffer) {
if (type === 'arrayBuffer') {
return res.arrayBuffer()
}

if (type === ResponseType.Blob) {
if (type === 'blob') {
return res.blob()
}

if (type === ResponseType.JSON) {
if (type === 'json') {
return res.json()
}

if (type === ResponseType.Stream) {
if (type === 'stream') {
return res.body
}

Expand Down
14 changes: 14 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Buffer } from 'node:buffer'

export const streamToString = async function streamToString(stream: NodeJS.ReadableStream): Promise<string> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chunks: Array<any> = []

for await (const chunk of stream) {
chunks.push(chunk)
}

const buffer = Buffer.concat(chunks)

return buffer.toString('utf-8')
}