0.0.6 • Published 1 year ago

@halfmatthalfcat/svgals v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

svgals

SVG.js + svgdom + AsyncLocalStorage

svgals is a combination of technologies to enable isolated SVG creation in Node-specific contexts only. Specifically, svgals seeks to create isolated documents to create SVGs between async contexts, such as Serverless handler requests.

Originally in SVG.js + svgdom, you needed to create a new svgdom document instance via createSVGWindow and then attach that window to a global instance (registerWindow), which doesn't necessarily work in async contexts as you may override an existing document.

svgals leverages Node's AsyncLocalStorage construct to persist a window in any given async context so that each async context can operate on windows independently of each other.

Usage

// Cloudflare Worker

import { SVG, withWindow } from '@halfmatthalfcat/svgals'

export default {
  async fetch(req) {
    return withWindow(() => {
      const svg = SVG().rect(100, 100).fill('#f06').svg()

      return Response(svg, {
        headers: {
          'Content-Type': 'image/svg+xml'
        }
      })
    })
  }
}

Installation

Npm:

npm install @halfmatthalfcat/svgals

Yarn:

yarn add @halfmatthalfcat/svgals

Documentation

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago