(str: string): Result.Result<Uint8Array, EncodingError>Decodes a URL-safe base64 string into bytes safely.
When to use
Use to decode padded or unpadded Base64Url text into bytes without throwing on invalid input.
Details
Returns Result.succeed with a Uint8Array when decoding succeeds, or
Result.fail with an EncodingError when the input is not valid URL-safe
base64. Both padded and unpadded URL-safe base64 forms are accepted when
otherwise valid.
Example (Decoding URL-safe Base64 bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64Url("SGVsbG8_")
if (Result.isSuccess(result)) {
console.log(Array.from(result.success)) // [72, 101, 108, 108, 111, 63]
}export const const decodeBase64Url: (
str: string
) => Result.Result<Uint8Array, EncodingError>
Decodes a URL-safe base64 string into bytes safely.
When to use
Use to decode padded or unpadded Base64Url text into bytes without throwing
on invalid input.
Details
Returns Result.succeed with a Uint8Array when decoding succeeds, or
Result.fail with an EncodingError when the input is not valid URL-safe
base64. Both padded and unpadded URL-safe base64 forms are accepted when
otherwise valid.
Example (Decoding URL-safe Base64 bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64Url("SGVsbG8_")
if (Result.isSuccess(result)) {
console.log(Array.from(result.success)) // [72, 101, 108, 108, 111, 63]
}
decodeBase64Url = (str: stringstr: string): import ResultResult.type Result<A, E = never> = Result.Success<A, E> | Result.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Uint8Array, class EncodingErrorclass EncodingError {
name: string;
message: string;
stack: string;
cause: unknown;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
kind: 'Decode' | 'Encode';
module: string;
input: unknown;
}
Error returned when an encoding or decoding operation cannot process its
input.
When to use
Use when you need to handle or inspect failures from encoding or decoding
operations.
Details
The error records whether the failure happened during encoding or decoding,
which encoding module reported it, the original input, and a human-readable
message.
EncodingError> => {
const const stripped: stringstripped = const stripCrlf: (str: string) => stringstripCrlf(str: stringstr)
const const length: numberlength = const stripped: stringstripped.String.length: numberReturns the length of a String object.
length
if (const length: numberlength % 4 === 1) {
return import ResultResult.const fail: <E>(
left: E
) => Result<never, E>
Creates a Result holding a Failure value.
When to use
Use to represent a failed Result with a typed failure value.
Details
- The success type
A defaults to never
Example (Creating a failure)
import { Result } from "effect"
const result = Result.fail("Something went wrong")
console.log(Result.isFailure(result))
// Output: true
fail(
new constructor EncodingError(): EncodingErrorError returned when an encoding or decoding operation cannot process its
input.
When to use
Use when you need to handle or inspect failures from encoding or decoding
operations.
Details
The error records whether the failure happened during encoding or decoding,
which encoding module reported it, the original input, and a human-readable
message.
EncodingError({
module: stringmodule: "Base64Url",
kind: stringkind: "Decode",
input: stringinput: const stripped: stringstripped,
message: stringmessage: `Length should be a multiple of 4, but is ${const length: numberlength}`
})
)
}
if (!/^[-_A-Z0-9]*?={0,2}$/i.RegExp.test(string: string): booleanReturns a Boolean value that indicates whether or not a pattern exists in a searched string.
test(const stripped: stringstripped)) {
return import ResultResult.const fail: <E>(
left: E
) => Result<never, E>
Creates a Result holding a Failure value.
When to use
Use to represent a failed Result with a typed failure value.
Details
- The success type
A defaults to never
Example (Creating a failure)
import { Result } from "effect"
const result = Result.fail("Something went wrong")
console.log(Result.isFailure(result))
// Output: true
fail(
new constructor EncodingError(): EncodingErrorError returned when an encoding or decoding operation cannot process its
input.
When to use
Use when you need to handle or inspect failures from encoding or decoding
operations.
Details
The error records whether the failure happened during encoding or decoding,
which encoding module reported it, the original input, and a human-readable
message.
EncodingError({
module: stringmodule: "Base64Url",
kind: stringkind: "Decode",
input: stringinput: const stripped: stringstripped,
message: stringmessage: "Invalid input"
})
)
}
// Some variants allow or require omitting the padding '=' signs
let let sanitized: stringsanitized = const length: numberlength % 4 === 2 ? `${const stripped: stringstripped}==` : const length: numberlength % 4 === 3 ? `${const stripped: stringstripped}=` : const stripped: stringstripped
let sanitized: stringsanitized = let sanitized: stringsanitized.String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)
Passes a string and
{@linkcode
replaceValue
}
to the [Symbol.replace] method on
{@linkcode
searchValue
}
. This method is expected to implement its own replacement algorithm.
replace(/-/g, "+").String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)
Passes a string and
{@linkcode
replaceValue
}
to the [Symbol.replace] method on
{@linkcode
searchValue
}
. This method is expected to implement its own replacement algorithm.
replace(/_/g, "/")
return const decodeBase64: (
str: string
) => Result.Result<Uint8Array, EncodingError>
Decodes a base64 (RFC4648) string into bytes safely.
When to use
Use to decode a standard padded Base64 string into bytes without throwing on
invalid input.
Details
Returns Result.succeed with a Uint8Array when decoding succeeds, or
Result.fail with an EncodingError when the input is not valid base64.
Example (Decoding Base64 bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64("SGVsbG8=")
if (Result.isSuccess(result)) {
console.log(Array.from(result.success)) // [72, 101, 108, 108, 111]
}
decodeBase64(let sanitized: stringsanitized)
}