npm.io
1.3.0 • Published 1 month ago

@sr-connect/convert

Licence
UNLICENSED
Version
1.3.0
Deps
0
Size
22 kB
Vulns
0
Weekly
0

A utility library for the ScriptRunner Connect runtime that provides conversion functions for text, buffers, base64 and form data.

Usage

Either import individual functions:

import { convertTextToBase64 } from '@sr-connect/convert';

export default async function(event: any, context: Context): Promise<void> {
	console.log(convertTextToBase64('HELLO WORLD', 'utf8'));
}

or import the entire namespace:

import { Convert } from '@sr-connect/convert';

export default async function(event: any, context: Context): Promise<void> {
	console.log(Convert.textToBase64('HELLO WORLD', 'utf8'));
}

Available functions

convertBase64ToBuffer(base64: string): Uint8Array;
convertBase64ToText(base64: string, encoding?: TextEncoding): string;
convertBufferToBase64(buffer: ArrayBuffer, fastTransfer?: boolean): string;
convertBufferToText(buffer: ArrayBuffer, encoding?: TextEncoding, fastTransfer?: boolean): string;
convertTextToBase64(text: string, encoding?: TextEncoding): string;
convertTextToBuffer(text: string, encoding?: TextEncoding): Uint8Array;
convertTextToText(text: string, sourceEncoding: TextEncoding, targetEncoding?: TextEncoding): string;
convertArrayBufferToFormDataBuffer(...chunks: FormDataChunk[]): BufferedFormData;

Every encoding parameter defaults to utf8. The same functions are available on the Convert namespace without the convert prefix, so convertTextToBase64 is Convert.textToBase64.

Changelog

1.3.0

  • Convert.bufferToText and Convert.bufferToBase64 now accept fastTransfer, matching the standalone convertBufferToText and convertBufferToBase64 functions.
  • TextEncoding now covers every encoding the runtime accepts, adding utf-16le and base64url.
  • Removed the unused delimiter property from the object returned by convertArrayBufferToFormDataBuffer. It was never part of the encoded payload.
  • convertArrayBufferToFormDataBuffer now terminates the payload with a closing boundary, which strict servers require.
  • Corrected the documentation of several functions.

1.2.0

  • Introduced fastTransfer mode for convertBufferToText and convertBufferToBase64. When enabled, the call consumes the buffer you pass in, which makes the conversion faster but leaves the buffer unreadable afterwards.

1.1.1

  • Internal package.json fix, no functional changes were introduced with this update.

1.1.0

  • Added convertArrayBufferToFormDataBuffer (or Convert.arrayBufferToFormDataBuffer) which encodes form data into a buffer using less memory and CPU than the FormData and Blob classes.