diff --git a/src/main.test.ts b/src/main.test.ts index 9f65759..a340616 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -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 () => { @@ -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 () => { diff --git a/src/main.ts b/src/main.ts index 751a629..9619ca8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 } @@ -153,13 +145,15 @@ export class Blobs { } async get(key: string): Promise - async get(key: string, { type }: { type: ResponseType.ArrayBuffer }): Promise - async get(key: string, { type }: { type: ResponseType.Blob }): Promise - async get(key: string, { type }: { type: ResponseType.Stream }): Promise - async get(key: string, { type }: { type: ResponseType.Text }): Promise + async get(key: string, { type }: { type: 'arrayBuffer' }): Promise + async get(key: string, { type }: { type: 'blob' }): Promise + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async get(key: string, { type }: { type: 'json' }): Promise + async get(key: string, { type }: { type: 'stream' }): Promise + async get(key: string, { type }: { type: 'text' }): Promise async get( key: string, - options?: { type: ResponseType }, + options?: { type: 'arrayBuffer' | 'blob' | 'json' | 'stream' | 'text' }, ): Promise { const { type } = options ?? {} const res = await this.makeStoreRequest(key, HTTPMethod.Get) @@ -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 } diff --git a/test/util.ts b/test/util.ts new file mode 100644 index 0000000..13174ed --- /dev/null +++ b/test/util.ts @@ -0,0 +1,14 @@ +import { Buffer } from 'node:buffer' + +export const streamToString = async function streamToString(stream: NodeJS.ReadableStream): Promise { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const chunks: Array = [] + + for await (const chunk of stream) { + chunks.push(chunk) + } + + const buffer = Buffer.concat(chunks) + + return buffer.toString('utf-8') +}