npm stats
  • Search
  • About
  • Repo
  • Sponsor
  • more
    • Search
    • About
    • Repo
    • Sponsor

Made by Antonio Ramirez

expo-file-stream

1.1.3

@d_cassidy

npmHomeRepoSnykSocket
Downloads:221
$ npm install expo-file-stream
DailyWeeklyMonthlyYearly

expo-file-stream

expo-file-stream is a small library for creating read streams from files on mobile.

  • Supports multiple files in parallel.
  • Enables content:// URI on Android to be streamed without copy/open.
  • Compatible with Bare runtime
  • Powered by streamx

Install

npm i expo-file-stream

Usage

Below is a minimal example that mirrors the one in the repository’s example/App.tsx.

import { useCallback } from 'react'
import { Button, View } from 'react-native'
import { getDocumentAsync } from 'expo-document-picker'
import getMimeType from 'get-mime-type'
import { streamFile } from 'file-stream'

export default function App() {
  const onUpload = useCallback(async () => {
    const result = await getDocumentAsync({
      multiple: true,
      copyToCacheDirectory: false
    })

    if (result.canceled || !result.assets) return

    const assets = await Promise.all(
      result.assets.map(async ({ name, size, mimeType, uri }) => {
        return {
          name,
          byteLength: size ?? 0,
          type: mimeType || getMimeType(uri),
          uri,
          isOutsideAppCache: true
        }
      })
    )

    const target = assets[0].uri
    const stream = streamFile(target)

    stream.on('data', chunk => {
      console.log(chunk)
    })

    stream.on('end', () => {
      console.log('Upload complete')
    })
  }, [])

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Button title="Upload" onPress={onUpload} />
    </View>
  )
}

Hyperblobs

For use with Bare the library is designed to be easily streamed in to P2P friendly storage.

const Hyperblobs = require('hyperblobs')
const Corestore = require('corestore')

const store = new Corestore("./storage")
const core = store.get({ name:"my-blobs" })
const blobs = new Hyperblobs(core)
await blobs.ready()

const readStream = streamFile('content://com.android.providers.media.documents/document/image%3A62')
const writeStream = blobs.createWriteStream()
const pipeStream = readStream.pipe(writeStream)

pipeStream.on('finish',async () => {
  const blob = await blobs.get(writeStream.id)
  console.log(blob)
})

API

  • streamFile(uri: string): ReadableStream – Returns a streamx Readable that emits data chunks and an end event when the file has been fully read.
const stream = streamFile('content://com.android.providers.media.documents/document/image%3A62')
stream.on('data', chunk => console.log(chunk))
stream.on('end', () => console.log('done'))

Notes

  • When picking files from the document picker, set copyToCacheDirectory: false to obtain a raw content:// URI.
  • The returned stream can be piped to any writable destination