Skip to content

Commit

Permalink
Update README with Awaitable
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Dec 21, 2024
1 parent c9727a4 commit 870187c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ const metadata = parquetMetadata(arrayBuffer)

### AsyncBuffer

Hyparquet accepts argument `file` of type `AsyncBuffer` which is like a js `ArrayBuffer` but the `slice` method returns `Promise<ArrayBuffer>`.
Hyparquet accepts argument `file` of type `AsyncBuffer` which is like a js `ArrayBuffer` but the `slice` method can return `Promise<ArrayBuffer>`.
You can pass an `ArrayBuffer` anywhere that an `AsyncBuffer` is expected, if you have the entire file in memory.

```typescript
type Awaitable<T> = T | Promise<T>
interface AsyncBuffer {
byteLength: number
slice(start: number, end?: number): Promise<ArrayBuffer>
slice(start: number, end?: number): Awaitable<ArrayBuffer>
}
```

Expand Down
Binary file modified hyparquet.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/files/hyparquet.jpg.snappy
Binary file not shown.
9 changes: 5 additions & 4 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ describe('asyncBufferFromUrl', () => {
})
})

expect(asyncBufferFromUrl({ url: 'https://example.com' })).rejects.toThrow('fetch head failed 401')
await expect(asyncBufferFromUrl({ url: 'https://example.com' }))
.rejects.toThrow('fetch head failed 401')

const buffer = await asyncBufferFromUrl({ url: 'https://example.com', requestInit: { headers: { Authorization: 'Bearer token' } } } )
expect(buffer.byteLength).toBe(1024)
Expand Down Expand Up @@ -195,11 +196,11 @@ describe('asyncBufferFromUrl', () => {
})

const noHeaders = await asyncBufferFromUrl({ url: 'https://example.com', byteLength: 1024 })
expect(noHeaders.slice(0, 100)).rejects.toThrow('fetch failed 401')
await expect(noHeaders.slice(0, 100)).rejects.toThrow('fetch failed 401')

const withHeaders = await asyncBufferFromUrl({ url: 'https://example.com', byteLength: 1024, requestInit: { headers: { Authorization: 'Bearer token' } } } )
expect (await withHeaders.slice(0, 100)).toBe(mockArrayBuffer)
await expect(withHeaders.slice(0, 100)).resolves.toBe(mockArrayBuffer)

expect (withHeaders.slice(0, 10)).rejects.toThrow('fetch failed 404')
await expect(withHeaders.slice(0, 10)).rejects.toThrow('fetch failed 404')
})
})

0 comments on commit 870187c

Please sign in to comment.