Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
run_install: false
- uses: actions/setup-node@v4
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v6
with:
cache: pnpm
node-version: 24
node-version: latest
registry-url: https://registry.npmjs.org

- run: pnpm install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
with:
build: pnpm -rF @moeru/std-docs build
path: docs/out
node-version: 24
node-version: latest
10 changes: 4 additions & 6 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ jobs:
contents: write
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
fetch-depth: 0
- uses: pnpm/action-setup@v4
with:
run_install: false
- uses: actions/setup-node@v4
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v6
with:
cache: pnpm
node-version: 24
node-version: latest
registry-url: https://registry.npmjs.org
- run: pnpm install --frozen-lockfile
- run: pnpm dlx changelogithub
Expand Down
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dictionaryDefinitions: []
dictionaries: []
words:
- bumpp
- changelogithub
- defu
- denoland
- ependencies
Expand Down
1 change: 0 additions & 1 deletion packages/std/src/base64/_base64abc.ts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/std/src/base64/_validate-binary-like.ts

This file was deleted.

48 changes: 0 additions & 48 deletions packages/std/src/base64/base64.ts

This file was deleted.

34 changes: 0 additions & 34 deletions packages/std/src/base64/base64url.ts

This file was deleted.

35 changes: 33 additions & 2 deletions packages/std/src/base64/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
export { decodeBase64, encodeBase64 } from './base64'
export { decodeBase64Url, encodeBase64Url } from './base64url'
const getTypeName = (value: unknown): string => {
const type = typeof value

if (type !== 'object')
return type
else if (value === null)
return 'null'
else
return value?.constructor?.name ?? 'object'
}

const validateBinaryLike = (source: unknown): Uint8Array => {
if (typeof source === 'string')
return new TextEncoder().encode(source)
else if (source instanceof Uint8Array)
return source
else if (source instanceof ArrayBuffer)
return new Uint8Array(source)

throw new TypeError(`The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type ${getTypeName(source)}.`)
}

export const decodeBase64Url = (b64url: string): Uint8Array =>
Uint8Array.fromBase64(b64url, { alphabet: 'base64url' })

export const encodeBase64Url = (data: ArrayBuffer | string | Uint8Array): string =>
validateBinaryLike(data).toBase64({ alphabet: 'base64url', omitPadding: true })

export const encodeBase64 = (data: ArrayBuffer | string | Uint8Array): string =>
validateBinaryLike(data).toBase64()

export const decodeBase64 = (b64: string): Uint8Array =>
Uint8Array.fromBase64(b64)
32 changes: 32 additions & 0 deletions packages/std/test/base64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest'

import {
decodeBase64,
decodeBase64Url,
encodeBase64,
encodeBase64Url,
} from '../src/base64'

describe('base64', () => {
it('should encode strings, Uint8Array, and ArrayBuffer values', () => {
const bytes = new Uint8Array([72, 101, 108, 108, 111])

expect(encodeBase64('Hello')).toBe('SGVsbG8=')
expect(encodeBase64(bytes)).toBe('SGVsbG8=')
expect(encodeBase64(bytes.buffer)).toBe('SGVsbG8=')
})

it('should decode base64 strings', () => {
expect([...decodeBase64('SGVsbG8=')]).toEqual([72, 101, 108, 108, 111])
})
})

describe('base64url', () => {
it('should encode without padding using the base64url alphabet', () => {
expect(encodeBase64Url(new Uint8Array([251, 255]))).toBe('-_8')
})

it('should decode base64url strings', () => {
expect([...decodeBase64Url('-_8')]).toEqual([251, 255])
})
})
Loading