0.39.0 • Published 1 month ago

@blitzjs/file-pipeline v0.39.0

Weekly downloads
3,741
License
MIT
Repository
github
Last release
1 month ago

@blitzjs/file-pipeline

File Transformation Pipeline

Design goals

  • Stages: Related logic needs to live together
  • Everything is a stream
  • Efficiency
  • Parallel processing
  • Cleaner Architecture for Dirty restart functionality
  • Agnostic input file watcher / glob
  • Simplify tests

This package provides a gulp based pipeline for transforming files from a source to a destination across a series of stages.

import {transformFiles} from '@blitzjs/file-pipeline'
import through from 'through2'
import File from 'vinyl'

// This is an example of a stage that does nothing to the
// files as they pass through the pipeline
const myStage = () => ({
  stream: through.obj((file:File, _, next) => {
    // Normally transformation will take place here
    next(null, file)
  })
})

const mySecondStage = () => ({
  stream: through.obj((file:File, _, next) => {
    // Normally transformation will take place here
    next(null, file)
  })
})

// Files start off at the source
const src = '/path/to/src'
// Pass through the stages one by one
const stages = [
  myStage,
  mySecondStage
]
// Then end up at the destination
const dest = '/path/to/dest'

// We can set various options they are all optional
const options = {
  // This indicates if the file watcher will be turned on
  watch:true,
  // this is a list of source files globs to ignore
  ignore: [],
  // this is a list of source files globs to include
  include: ['**/*']
  // pass in an optional transform stream that will be used as an event bus
  bus: someTransformStream
}

// run the transform
transformFiles(src, stages, dest, options),

Stages

Stages are how you provide special behaviour to your file-pipeline.

The anatomy of your stage looks like this:

function myStage({
  // Stage config holds the basic info you need for the stage
  config: {
    // src folder
    src,
    // dest folder
    dest,
    // current working directory
    cwd,
    // include globs
    include,
    // ignore globs
    ignore,
    // if we are in watch mode
    watch,
  },
  // Input writable stream - use input.write(file) to send a file the input of the pipeline
  input,
  // Event bus stream - use this to send events to listeners within and outside of the pipeline
  bus,
  // Get the input cache.
  // This is an object that contains cached objects for all the files ingested.
  // Use this for things that require lists of files
  getInputCache,
}: StageArgs) {
  // Create some kind of transform stream
  const stream = createSomeKindOfTransformStream()

  // Ready - is an object that will be merged with all other
  // Stages and returned in a promise by  transformFiles()
  const ready = {foo: 'This will appear in the object returned by transformation promise'}

  // Export the stream and the ready info
  return {stream, ready}
}

Why Streams?

Initially, Blitz will be used by people with small projects however as the number files and throughput increases we will need to use an architecture that allows for large parallel throughput with low memory consumption. Node is built on streams as a primitive so it makes sense to utilize what is available. The Gulp ecosystems provide several tools for managing streams of files so that makes sense to use those tools when available. Because refactoring to streams later would be extremely difficult and painful not starting with streams would be a design mistake.

Why not RxJS?

RxJS could be a good match for streaming architectures and introduces some really powerful tools for managing stream operations. As we are using object streams it would also possibly simplify some of the boilerplate using RxJS. However, certain operators in RxJS can be inapproachable for newer developers and tend to encourage too much abstraction. It is also an extra dependency that increases the learning surface of the codebase and as we are stuck with understanding basic node streams, in any case, it makes sense to avoid RxJS until absolutely necessary.

File Transform Pipeline

Stream helpers

So Node streams are a little incompatible on old versions of Node and there are a few compatibility libs we are using to help us work with streams.

https://www.freecodecamp.org/news/rxjs-and-node-8f4e0acebc7c/

Helper Libs

A good way to work with streams

A pattern we have found that works well is using a constructor function to accept connectors and return a stream as well as any shared data you need to provide to other components connectors. You will see this a lot around the synchronizer.

type CreatorFn = ConnectionsOrConfig => StreamAsWellAsSharedData

An example might look like this:

// Config -> Merged Glob && FileWatcher
const source = agnosticSource({cwd: src, include, ignore, watch})

// you can then pipe the stream to a pipeline
pipe(source.stream, fileTransformPipeline)

The reason we don't just return a stream is that often we need to return other data and share it elsewhere, for example, to analyze input file structure in the pages rule we use a file cache.

// Here
const fileCache = createFileCache(config)
const pageRule = createPageRule(fileCache.cache)

pipeline(
  fileCache.stream, // manages the fileCache to be used by other streams
  // ...
  pageRule.stream, // has access to the fileCache
)

View rendering and error handling

The cli view is provided by a stream which accepts Events which it manages and displays. This is responsible for rendering stuff to the view.

If you push an Error to the transform stream next(new Error) an Error Event will be sent over the event bus. This can get handled by some event handling logic in your Stage.

Evented Vinyl Files

Evented Vinyl Files are Vinyl Files with events attached to them

const isDelete = (file) => file.isNull() && file.event === 'unlink'

// The input file at '/path/to/foo' was deleted
// This can be transformed during the process phase
return new Vinyl({
  path: '/path/to/foo',
  content: null,
  event: 'unlink',
})
// Add file at '/path/to/foo'
new Vinyl({
  path: '/path/to/foo',
  content: someContentStream,
})

Input agnostic

Pipeline is input agnostic ie. it should not matter if it comes from watch or a folder glob so to help with that we have created an agnostic input stream that takes glob config and returns a file stream. It consumes input from both chokidar and vinyl-fs.

Optimization

Input manages inputting of evented vinyl file. Files that have already been processed or are currently being processed should not be processed again. Here we try and manage a running list of files to work on based on the hash of their filename and mtime.

Analysis

Some types of analysis need a list of all the files other types do not

Analysis needs to be done in stream as new information comes in. Eg. when someone renames a file that file goes to the analysis engine which works out invariants as they occur without requiring a sweep of the entire file system.

For this, we can create file caches which represent projections of the file system and update based on input file events.

Stages

Stage streams represent specific things we need the file-pipeline to do

Possible things it can do:

  • Change its path or contents
  • Drop the file from further processing. Don't copy it.
  • Add new files to the input stream - Associating the new files with the original
  • Write an error to the error stream
import {through} from './streams'

// Typical Stage
export default myStage({config, input, getInputCache}) => {
  const service = createSomeService()

  // This is an incremental file cache that
  // gets built as Files are read
  const cache = getInputCache()

  // You should not write to the file system use input.write() instead to send
  // a new file down the pipelin
  if (!pathExistsSync(resolve(config.src, 'blitz.config.js'))) {
    input.write(
      new File({
        path:resolve(config.src, 'blitz.config.js'),
        content:Buffer.from('Hello World')
      })
    )
  }

  const stream = through.obj(function (file, enc, next) {
    // You can test for changes in the input cache
    if (cache.filter(/next\.config\.js$/.exec).length > -1) {
      const err = new Error('Cannot have next config!')
      err.name = 'NextConfigError'
      next(err) // Will cause an Error Event to be fired on the event bus
    }

    // process file in some way
    file.path = file.path.toUpperCase()

    // you can push to the stream output (note you cannot use arrow fns)
    this.push(file)

    // You can push new files to the output too but this is a little dodgy
    // One issue here is that this file will not be registered as a piece
    // of work and this may have unintended consequences.
    // This is still ok in many situations but it is safer to write to the input.
    // (note input.write vs this.push)
    this.push(
      new File({
        path: '/path/to/foo',
        content: Buffer.from('This is a file')
      })
    )

    // or send file onwards to be written this does the same thing as this.push()
    next(null, file)
  })

  // provide an object that will be merged with other ready
  // objects to form the return of the transformFiles return promise value
  const ready = {
    foo: 'I am foo'
  }

  return {
    stream,
    ready,
    service, // provide that service to consumers outside the stream
  }
}
0.40.0-canary.3

1 month ago

0.40.0-canary.4

1 month ago

0.40.0-canary.1

1 month ago

0.40.0-canary.0

1 month ago

0.39.0

1 month ago

0.39.0-canary.0

2 months ago

0.38.6

2 months ago

0.38.5

3 months ago

0.38.4

3 months ago

0.38.3

3 months ago

0.38.3-canary.1

3 months ago

0.38.2

3 months ago

0.38.3-canary.0

3 months ago

0.38.1

3 months ago

0.38.0

3 months ago

0.36.4

4 months ago

0.36.3

4 months ago

0.36.2

4 months ago

0.37.0

4 months ago

0.35.2

4 months ago

0.35.1

4 months ago

0.35.0-canary.4

5 months ago

0.35.0-canary.3

5 months ago

0.35.0-canary.6

5 months ago

0.35.0-canary.5

5 months ago

0.35.0-canary.0

5 months ago

0.35.0-canary.2

5 months ago

0.35.0-canary.1

5 months ago

0.34.2-canary.0

5 months ago

0.34.3

5 months ago

0.34.2

5 months ago

0.35.0

4 months ago

0.34.1

5 months ago

0.34.0

5 months ago

0.34.0-y.0

5 months ago

0.34.0-canary.4

5 months ago

0.34.0-canary.5

5 months ago

0.34.0-canary.3

6 months ago

0.34.0-canary.2

6 months ago

0.34.0-canary.1

6 months ago

0.34.0-canary.0

6 months ago

0.33.1

6 months ago

0.33.0

6 months ago

0.32.1

7 months ago

0.32.0

7 months ago

0.31.1

7 months ago

0.31.0

7 months ago

0.30.7

7 months ago

0.30.6

7 months ago

0.30.5

7 months ago

0.30.4

7 months ago

0.30.3

7 months ago

0.30.2

7 months ago

0.30.1

7 months ago

0.30.0-canary.15

7 months ago

0.30.0

7 months ago

0.30.0-canary.14

7 months ago

0.30.0-canary.13

7 months ago

0.30.0-canary.11

7 months ago

0.30.0-canary.12

7 months ago

0.30.0-canary.10

7 months ago

0.30.0-canary.9

7 months ago

0.30.0-canary.8

7 months ago

0.30.0-canary.7

8 months ago

0.30.0-canary.6

8 months ago

0.30.0-canary.5

8 months ago

0.30.0-canary.4

8 months ago

0.30.0-canary.3

8 months ago

0.30.0-canary.2

8 months ago

0.29.7

8 months ago

0.29.6

8 months ago

0.30.0-canary.1

8 months ago

0.30.0-canary.0

8 months ago

0.29.5

8 months ago

0.29.3

8 months ago

0.29.2

8 months ago

0.29.0

8 months ago

0.29.1

8 months ago

0.29.0-canary.0

9 months ago

0.28.0

9 months ago

0.28.0-canary.2

9 months ago

0.28.0-canary.1

9 months ago

0.28.0-canary.0

9 months ago

0.27.1

10 months ago

0.27.0

10 months ago

0.27.0-canary.2

10 months ago

0.27.0-canary.3

10 months ago

0.27.0-canary.1

10 months ago

0.27.0-canary.0

10 months ago

0.26.2

10 months ago

0.26.1

10 months ago

0.26.0

10 months ago

0.25.1-canary.3

10 months ago

0.25.1-canary.2

10 months ago

0.25.1-canary.1

10 months ago

0.25.1-canary.0

10 months ago

0.25.0-canary.6

11 months ago

0.25.0

11 months ago

0.25.0-canary.5

11 months ago

0.25.0-canary.4

11 months ago

0.25.0-canary.3

11 months ago

0.25.0-canary.2

11 months ago

0.25.0-canary.1

11 months ago

0.25.0-canary.0

11 months ago

0.24.3

11 months ago

0.24.2

11 months ago

0.24.1

12 months ago

0.24.0

12 months ago

0.24.0-canary.4

12 months ago

0.24.0-canary.3

12 months ago

0.24.0-canary.2

12 months ago

0.24.0-canary.1

12 months ago

0.23.4

12 months ago

0.24.0-canary.0

12 months ago

0.23.3

12 months ago

0.23.2-canary.3

12 months ago

0.23.2-canary.2

12 months ago

0.23.2

12 months ago

0.23.2-canary.1

12 months ago

0.23.1

12 months ago

0.23.2-canary.0

12 months ago

0.23.1-canary.0

12 months ago

0.23.0

1 year ago

0.22.3-canary.0

1 year ago

0.22.2-canary.0

1 year ago

0.22.1

1 year ago

0.22.0

1 year ago

0.21.2-canary.2

1 year ago

0.21.2-canary.1

1 year ago

0.21.2-canary.0

1 year ago

0.21.1

1 year ago

0.21.0

1 year ago

0.20.0

1 year ago

0.18.1-canary.0

1 year ago

0.19.0

1 year ago

0.18.1-canary.1

1 year ago

0.18.0

1 year ago

0.17.1-canary.7

1 year ago

0.17.1-canary.6

1 year ago

0.17.1-canary.5

1 year ago

0.17.1-canary.4

1 year ago

0.17.1-canary.3

1 year ago

0.17.1-canary.2

1 year ago

0.17.1-canary.0

1 year ago

0.17.1-canary.1

1 year ago

0.17.0

1 year ago

0.16.5-canary.8

1 year ago

0.16.5-canary.7

1 year ago

0.16.5-danger.3

1 year ago

0.16.5-canary.5

1 year ago

0.16.5-danger.4

1 year ago

0.16.5-canary.4

1 year ago

0.16.5-canary.3

1 year ago

0.16.5-canary.2

1 year ago

0.16.5-canary.1

1 year ago

0.16.5-canary.0

1 year ago

0.16.4

1 year ago

0.16.3

1 year ago

0.16.2

1 year ago

0.16.1

1 year ago

0.16.0

1 year ago

0.15.3

1 year ago

0.15.2-canary.3

1 year ago

0.15.2

1 year ago

0.15.2-canary.1

1 year ago

0.15.2-canary.2

1 year ago

0.15.2-canary.0

1 year ago

0.15.1

1 year ago

0.15.1-canary.0

1 year ago

0.15.0

1 year ago

0.14.2-canary.0

1 year ago

0.14.1

1 year ago

0.14.0-canary.0

1 year ago

0.14.0

1 year ago

0.13.0

1 year ago

0.13.0-canary.3

1 year ago

0.13.0-canary.2

1 year ago