Azion unenv Preset

A preset configuration for unenv that provides polyfills and environment compatibility for Azion Edge Runtime.

Go to Azion Libraries Overview

Usage

The preset can be used with unenv to provide compatibility between Node.js and Azion Edge Runtime environments:

import { preset } from '@azion/unenv-preset'
// Use with unenv
export default {
preset: preset
}

Features

Node.js Polyfills

This preset provides polyfills for common Node.js APIs to ensure compatibility when running code in the Azion Edge Runtime environment. The following modules are polyfilled:

ModuleDescription
cryptoComplete cryptographic functionality including hashing, encryption, and UUID generation.
eventsEvent handling through events/events.js.
httpHTTP client functionality via stream-http.
moduleBasic module system compatibility (limited functionality).
streamStream implementations via stream-browserify.
string_decoderString decoding utilities.
urlURL parsing and formatting.
utilUtility functions.
timersTimer functions via timers-browserify.
vmVirtual Machine functionality via vm-browserify.
zlibCompression functionality via browserify-zlib.

Example:

This exemple is using the crypto polyfill.

import { createHash, randomUUID } from '@azion/unenv-preset/polyfills/node/crypto'
// Create a hash
const hash = createHash('sha256')
hash.update('some data')
console.log(hash.digest('hex'))
// Generate a UUID
const uuid = randomUUID()

File System Polyfills

The preset includes comprehensive file system polyfills that mirror Node.js’s fs module functionality. These polyfills work with an in-memory file system when running in the Edge Runtime.

Basic File Operations

import { readFileSync, writeFileSync } from '@azion/unenv-preset/polyfills/node/fs'
// Read a file
const content = readFileSync('/path/to/file.txt', 'utf8')
// Write to a file
writeFileSync('/path/to/new-file.txt', 'Hello World', 'utf8')

Directory Operations

import { readdirSync, mkdirSync } from '@azion/unenv-preset/polyfills/node/fs'
// List directory contents
const files = readdirSync('/path/to/dir')
// Create a directory
mkdirSync('/path/to/new-dir')

File Stats and Information

import { statSync, existsSync } from '@azion/unenv-preset/polyfills/node/fs'
// Check if file exists
if (existsSync('/path/to/file.txt')) {
// Get file stats
const stats = statSync('/path/to/file.txt')
console.log(`File size: ${stats.size}`)
console.log(`Is directory: ${stats.isDirectory()}`)
console.log(`Is file: ${stats.isFile()}`)
}

File Descriptors

import { openSync, closeSync, readSync } from '@azion/unenv-preset/polyfills/node/fs'
// Open file and get file descriptor
const fd = openSync('/path/to/file.txt', 'r')
// Read from file descriptor
const buffer = Buffer.alloc(1024)
readSync(fd, buffer, 0, 1024, 0)
// Close file descriptor
closeSync(fd)

Global Polyfills

The preset also provides polyfills for Node.js global variables and objects:

Global ObjectDescription
__dirnameCurrent directory name.
__filenameCurrent file name.
processProcess information and environment (including env variables).
performancePerformance timing functionality.
navigatorBrowser-compatible navigator object.

The preset also handles injection of these globals through the unenv configuration:

import { preset } from '@azion/unenv-preset'
// Preset configuration automatically injects globals
export default {
inject: {
__dirname: preset.inject.__dirname,
__filename: preset.inject.__filename,
process: preset.inject.process,
performance: preset.inject.performance,
navigator: preset.inject.navigator
}
}

API Reference

getFileContent

Decodes file content and returns it as either a Buffer or string.

Parameters:

ParameterDescriptionValues
fileThe file object containing content in base64 format.
returnBufferDetermines return type (default: true).true → Returns a Buffer,false → Returns a string.

Returns:

Return TypeDescription
Buffer | stringThe decoded file content.

Example:

const fileBuffer = getFileContent(file) // returns Buffer
const fileString = getFileContent(file, false) // returns string

closeSync

Synchronously closes the file descriptor.

Parameters:

ParameterDescription
fdThe file descriptor to close.

Example:

closeSync(fd)

openSync

Synchronously opens a file.

Parameters:

ParameterDescription
pathThe path to the file.
flagsThe opening mode (for example, ‘r’, ‘w’).
mode(Optional) The file mode.

Returns:

Return TypeDescription
numberThe file descriptor.

Example:

const fd = openSync('/path/to/file.txt', 'r')

statSync

Synchronously retrieves the fs.Stats for the specified path.

Parameters:

ParameterDescription
pathThe path to the file or directory.
options(Optional) Options for the stat call.

Returns:

Return TypeDescription
fs.StatsThe stats object for the file or directory.

Example:

const stats = statSync('/path/to/file.txt')

readFileSync

Synchronously reads the entire contents of a file.

Parameters:

ParameterDescription
pathThe file path.
options(Optional) Options for reading the file.

Returns:

Return TypeDescription
string | BufferThe file contents.

Example:

const content = readFileSync('/path/to/file.txt', 'utf8')

readdirSync

Synchronously reads the contents of a directory.

Parameters:

ParameterDescription
pathThe directory path.
options(Optional) Options for reading the directory.

Returns:

Return TypeDescription
string[]An array of file names in the directory.

Example:

const files = readdirSync('/path/to/dir')

Contributors