0.0.56 • Published 6 months ago

react-hook-multipart v0.0.56

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

React Hook Multipart

Not for production yet 🚧 Tests & Type definitions missing

Is a fast and efficient hook for upload big files with multipart streams in a SSR React environment.

👀 It will split the file into multiple parts, upload them in parallel and retry failed parts.

I'm currently working on implementing Web Streams 🚬👷🏼 Al final, creo que no implementaré streams. Lo que estoy haciendo con el slice() del blob es suficientemente compatible. Mejor me concentro en el abort signal...

Install it like so:

npm i react-hook-multipart

Create ENV vars

You need to set all these five env variables, either by exporting them or write'em down in your .env file

AWS_REGION = "auto";
AWS_ENDPOINT_URL_S3 = "https://fly.storage.tigris.dev";
BUCKET_NAME = "blissmo-bucket";
AWS_ACCESS_KEY_ID = "Tu access key";
AWS_SECRET_ACCESS_KEY = "Tu secret";

How to use it

Use the handler in your React Router Framework server action

// app/routes/api/experiment.ts

import type { Route } from "./+types/experiment";
import { createAsset } from "~/.server/db";
import { getUserOrRedirect } from "~/.server/getters";
// import the handdler
import { handler } from "react-hook-multipart";

export const action = async ({ request }: Route.ActionArgs) => {
  const user = await getUserOrRedirect(request);
  // your cb is called only on complete
  return await handler(
    request,
    async ({ metadata, size, key, contentType }) => {
      // create on DB
      createAsset({
        metadata: metadata,
        size: size,
        storageKey: key,
        contentType: contentType,

        status: "uploaded",
        userId: user.id,
      });
      return new Response(JSON.stringify({ ok: true }));
    }
  );
};

Use the hook in your React Component

// any react component

import { useUploadMultipart } from "react-hook-multipart/react";

const { upload } = useUploadMultipart({
  onUploadProgress({ percentage }) {
    setProgress(percentage); // your own state ✅
  },
  access: "public-read", // or private
  handler: "/api/upload", // your own resource route ㊮
  signal: new AbortController(), // @todo about to implement... 👷🏼‍♂️
});

const handleUpload = async (event) => {
  const file = event.currentTarget.files?.[0];
  const { key, url } = await upload(file.name, file);
};

You can use try{}catch(){} blocks to capture any error.

// ...
const [progress, setProgress] = useState(0);

const putFile = async (file: File) => {
  await upload(file.name, file, ({ percentage }) => setProgress(percentage));
  //                                     ^ you can pass any function to update the progress
};
// ...

You can also pass the progress handler as the third paramether to the upload function.

Important!

You may want to externalize the dependency in vite.config file.

import { reactRouter } from "@react-router/dev/vite";
import autoprefixer from "autoprefixer";
import tailwindcss from "tailwindcss";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
  server: { port: 3000 },
  css: {
    postcss: {
      plugins: [tailwindcss, autoprefixer],
    },
  },
  plugins: [reactRouter(), tsconfigPaths()],
  build: {
    rollupOptions: {
      // make sure to externalize deps that shouldn't be bundled
      // into your library
      external: ["react-hook-multipart"], // <= here
    },
  },
});

Underneath

This package uses @aws-sdk/s3-request-presigner and @aws-sdk/client-s3 underneath.

Made with 🚬🫁 by Fixter.org

0.0.56

6 months ago

0.0.55

6 months ago

0.0.54

6 months ago

0.0.53

6 months ago

0.0.52

6 months ago

0.0.51

6 months ago

0.0.50

6 months ago

0.0.49

6 months ago

0.0.48

6 months ago

0.0.47

6 months ago

0.0.46

6 months ago

0.0.45

6 months ago

0.0.44

6 months ago

0.0.43

6 months ago

0.0.42

6 months ago

0.0.41

6 months ago

0.0.40

6 months ago

0.0.39

6 months ago

0.0.38

7 months ago

0.0.37

7 months ago

0.0.36

7 months ago

0.0.35

7 months ago

0.0.34

7 months ago

0.0.33

7 months ago

0.0.32

7 months ago

0.0.31

7 months ago

0.0.30

7 months ago

0.0.29

7 months ago

0.0.28

7 months ago

0.0.27

7 months ago

0.0.26

7 months ago

0.0.25

7 months ago

0.0.24

7 months ago

0.0.23

7 months ago

0.0.22

7 months ago

0.0.21

7 months ago

0.0.20

7 months ago

0.0.19

7 months ago

0.0.18

7 months ago

0.0.17

7 months ago

0.0.16

7 months ago

0.0.15

7 months ago

0.0.14

7 months ago

0.0.13

7 months ago

0.0.12

7 months ago

0.0.11

7 months ago

0.0.10

7 months ago

0.0.9

7 months ago

0.0.8

7 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago