Skip to content

Commit

Permalink
Merge pull request #6 from Josh-McFarlin/add-encoders
Browse files Browse the repository at this point in the history
Add encoders
  • Loading branch information
Josh-McFarlin authored Feb 9, 2022
2 parents 1d82ac8 + 700e358 commit bdb37f0
Show file tree
Hide file tree
Showing 159 changed files with 32,697 additions and 1,379 deletions.
8 changes: 6 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ node_modules/
server.d.ts
serverPure.d.ts

example/node_modules/
example/package-lock.json/
examples/
docs/

rollup.config.js
jest.config.js
jest.setup.js
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,5 @@
"react/no-unescaped-entities": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-empty-function": "off"
},
"ignorePatterns": ["rollup.config.js"]
}
}
45 changes: 42 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@

# Created by https://www.toptal.com/developers/gitignore/api/node,webstorm+all
# Edit at https://www.toptal.com/developers/gitignore?templates=node,webstorm+all
# Created by https://www.toptal.com/developers/gitignore/api/node,webstorm+all,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=node,webstorm+all,macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
Expand Down Expand Up @@ -237,7 +266,17 @@ modules.xml
# Sonarlint plugin
.idea/sonarlint

# End of https://www.toptal.com/developers/gitignore/api/node,webstorm+all
# End of https://www.toptal.com/developers/gitignore/api/node,webstorm+all,macos

node_modules
build
examples/**/node_modules
examples/**/build
examples/**/.cache
examples/**/tmp
examples/**/.mf

docs/.docusaurus
docs/build
docs/node_modules
docs/.env
14 changes: 12 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ dist/
node_modules/
.idea_modules/
.idea/
package-lock.json

example/node_modules/
example/package-lock.json/
examples/**/node_modules
example/**/package-lock.json
examples/**/build
examples/**/.cache
examples/**/tmp
examples/**/.mf

docs/.docusaurus
docs/build
docs/node_modules
docs/.env
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- add supported content type fields to transformers
- modify how transform defaults are set and their types
- Simplify transformers by removing buffer dependency for external transformers, move sharp
transformer to examples, and create wasm transformer
- **BREAKING**: Modified transformer to use a new function type, Sharp can no longer be passed in
as is

### Docs

- Updated docs to use new transformer format
- Created multiple new examples for various transformers
- Created documentation for new usage of Sharp

## [0.2.0] - 2022-01-26

### Docs
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

A React component for responsive images in Remix.

This library turns:
This library lets you:
* Resize images to the minimum size needed for faster page loading
* Transform images to more efficient file types for faster speed
* Cache commonly requested assets for the best performance

Turning:

```typescript jsx
<Image
Expand Down Expand Up @@ -51,11 +56,6 @@ yarn add remix-image

## Other

### Status

At the moment this library is experimental and has not been used in a production environment.
Further development is ongoing, but I welcome all pull-requests and issues created on GitHub.

### Credit

This repo expands on the following gists by:
Expand Down
2 changes: 1 addition & 1 deletion docs/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
presets: [require.resolve("@docusaurus/core/lib/babel/preset")],
};
31 changes: 22 additions & 9 deletions docs/docs/advanced/creating-a-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,37 @@ export interface CacheConfig {
tbd: number;
}

export enum CacheStatus {
/**
* The cache contains the key and it has not yet expired
*/
HIT = "hit",
/**
* The cache contains the key but it has expired
*/
STALE = "stale",
/**
* The cache does not contain the key
*/
MISS = "miss",
}

export abstract class Cache {
abstract config: CacheConfig;

abstract has(key: string): Promise<boolean>;

abstract status(key: string): Promise<"hit" | "stale" | "miss">;
abstract status(key: string): Promise<CacheStatus>;

abstract get(key: string): Promise<{
resultImg: Buffer;
contentType: string;
} | null>;
abstract get(key: string): Promise<Uint8Array | null>;

abstract set(key: string, resultImg: Buffer): Promise<void>;
abstract set(key: string, resultImg: Uint8Array): Promise<void>;

abstract clear(): Promise<void>;
}
```

You will then provide an instance of this class to the cache field of the ‘loader’ config
You will then provide an instance of this class to the `cache` field of the ‘loader’ config
```typescript jsx
import type { LoaderFunction } from "remix";
import { imageLoader } from "remix-image/server";
Expand All @@ -57,9 +69,10 @@ export const loader: LoaderFunction = ({ request }) => {
};
```

## Example
## Examples

To see an example, look at [`diskCache`](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/caches/diskCache) in the library.
* [diskCache](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/caches/diskCache)
* [memoryCache](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/caches/memoryCache)

## Show Off

Expand Down
22 changes: 9 additions & 13 deletions docs/docs/advanced/creating-a-resolver.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ export type Resolver = (
asset: string,
url: string
) => Promise<{
buffer: Buffer;
contentType: string;
buffer: Uint8Array;
contentType: MimeType;
}>;
```
such as:
```typescript
import { fsResolver, fetchResolver } from "remix-image/server";
import { fsResolver, fetchResolver, Resolver } from "remix-image/server";

export const myResolver = async (
asset: string,
src: string
): Promise<{
buffer: Buffer;
contentType: string;
}> => {
export const myResolver: Resolver = async (asset, src) => {
if (src.startsWith("/") && (src.length === 1 || src[1] !== "/")) {
return fsResolver(asset, src);
} else {
Expand All @@ -38,7 +32,7 @@ export const myResolver = async (
};
```

You will then provide this function to the resolver field of the loader config
You will then provide this function to the `resolver` field of the loader config
```typescript jsx
import type { LoaderFunction } from "remix";
import { imageLoader } from "remix-image/server";
Expand All @@ -55,9 +49,11 @@ export const loader: LoaderFunction = ({ request }) => {
};
```

## Example
## Examples

To see an example, look at [`fsResolver`](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/resolvers/fsResolver) in the library.
* [fsResolver](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/resolvers/fsResolver)
* [fetchResolver](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/resolvers/fetchResolver)
* [kvResolver](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/resolvers/kvResolver)

## Show Off

Expand Down
38 changes: 17 additions & 21 deletions docs/docs/advanced/creating-a-transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,21 @@ An example could be resizing images using Cloudinary or Cloudflare Images.

To make your own, just make a class that follows the [Transformer format](https://github.com/Josh-McFarlin/remix-image/blob/master/src/types/transformer.ts):
```typescript
import { JpegOptions, PngOptions, ResizeOptions, WebpOptions } from "sharp";

export abstract class ImageTransformer {
abstract jpeg(options?: JpegOptions): this;
abstract png(options?: PngOptions): this;
abstract webp(options?: WebpOptions): this;

abstract resize(options: ResizeOptions): this;
abstract toBuffer(): Promise<Buffer>;
}
```

**Note**: Transformers use Buffer, a default Node package that is not available on platforms like Cloudflare Workers.
If you would like to use your custom transformer on these platforms, you will need to polyfill your custom transformer using a library like [`buffer`](https://www.npmjs.com/package/buffer).

Then make a function that takes in a buffer as the single parameter, and creates an instance of your Transformer class
```typescript
export type TransformerMaker = (buffer: Buffer) => ImageTransformer;
export type Transformer = {
name: string;
supportedInputs: Set<MimeType>;
supportedOutputs: Set<MimeType>;
transform: (
input: {
data: Uint8Array;
contentType: MimeType;
},
output: TransformOptions
) => Promise<Uint8Array>;
};
```

You will then provide this function to the transformer field of the ‘loader’ config
You will then provide this object to the `transformer` field of the ‘loader’ config
```typescript jsx
import type { LoaderFunction } from "remix";
import { imageLoader } from "remix-image/server";
Expand All @@ -48,9 +42,11 @@ export const loader: LoaderFunction = ({ request }) => {
};
```

## Example
## Examples

To see an example, look at [`pureTransformer`](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/transformers/pureTransformer) in the library.
* [pureTransformer](https://github.com/Josh-McFarlin/remix-image/tree/master/src/server/transformers/pureTransformer)
* [sharp](https://github.com/Josh-McFarlin/remix-image/tree/master/examples/sharp/sharpTransformer)
* [gif-resizer](https://github.com/Josh-McFarlin/remix-image/tree/master/examples/gif-resizer/gifTransformer)

## Show Off

Expand Down
11 changes: 6 additions & 5 deletions docs/docs/component.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import { Image } from "remix-image";
```

## PropTypes
| Name | Type | Required | Default | Description |
|:----------:|:------------------------------------------------------------------:|:--------:|:------------:|:-------------------------------------------------------------------------------------------------------------------------------------------------:|
| loaderUrl | string | | "/api/image" | The path of the image loader resource route. The `loaderUrl` prop is optional if the resource route has been created at the path `"/api/image"`. |
| responsive | { size: { width: number; height: number; }; maxWidth?: number; }[] | | [] | An array of responsive sizes. The resource route is not called if this prop is not provided. |
| Name | Type | Required | Default | Description |
|:----------:|:------------------------------------------------------------------:|:--------:|:------------:|:------------------------------------------------------------------------------------------------------------------------------------------------:|
| loaderUrl | string | | "/api/image" | The path of the image loader resource route. The `loaderUrl` prop is optional if the resource route has been created at the path `"/api/image"`. |
| responsive | { size: { width: number; height: number; }; maxWidth?: number; }[] | | [] | An array of responsive sizes. The resource route is not called if this prop is not provided. |
| options | TransformOptions | | | TransformOptions that can be used to override the defaults provided to the loader. |

**Note**: The `Image` component extends the native `img` element, so any props used with `img` can be provided to the `Image` component.
**Note**: The `Image` component extends the native `img` element, so any props used with `img` can be provided to the `Image` component.
11 changes: 6 additions & 5 deletions docs/docs/hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ const Image: React.FC<ImageProps> = ({
```

## Parameters
| Name | Type | Required | Default | Description |
|:----------:|:------------------------------------------------------------------:|:--------:|:-------:|:-----------------------------------------------:|
| imgProps | ComponentPropsWithoutRef<"img"> | X | | The props to be passed to the base img element. |
| loaderUrl | string | X | [] | The path of the image loader resource route. |
| responsive | { size: { width: number; height: number; }; maxWidth?: number; }[] | | [] | An array of responsive sizes. |
| Name | Type | Required | Default | Description |
|:----------:|:------------------------------------------------------------------:|:--------:|:-------:|:-----------------------------------------------------------------------------------:|
| imgProps | { src: string } | X | | The props to be passed to the base img element. |
| loaderUrl | string | X | [] | The path of the image loader resource route. |
| responsive | { size: { width: number; height: number; }; maxWidth?: number; }[] | | [] | An array of responsive sizes. |
| options | TransformOptions | | | TransformOptions that can be used to override the defaults provided to the loader. |
7 changes: 6 additions & 1 deletion docs/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ sidebar_position: 1

A React component for responsive images in Remix.

This library turns:
This library lets you:
* Resize images to the minimum size needed for faster page loading
* Transform images to more efficient file types for faster speed
* Cache commonly requested assets for the best performance

Turning:

```typescript jsx
<Image
Expand Down
Loading

0 comments on commit bdb37f0

Please sign in to comment.