├── tslint.json ├── .gitignore ├── SECURITY.md ├── .editorconfig ├── .travis.yml ├── src ├── index.spec.ts └── index.ts ├── tsconfig.json ├── LICENSE ├── README.md └── package.json /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["tslint-config-standard", "tslint-config-prettier"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .vscode 3 | coverage/ 4 | node_modules/ 5 | npm-debug.log 6 | dist/ 7 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Security contact information 4 | 5 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_size = 2 7 | indent_style = space 8 | end_of_line = lf 9 | charset = utf-8 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | 4 | notifications: 5 | email: 6 | on_success: never 7 | on_failure: change 8 | 9 | node_js: 10 | - "8" 11 | - stable 12 | 13 | after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" 14 | -------------------------------------------------------------------------------- /src/index.spec.ts: -------------------------------------------------------------------------------- 1 | import { decode, encode } from "./index"; 2 | 3 | describe("node", () => { 4 | it("should base64 encode", () => { 5 | expect(encode("test")).toEqual("dGVzdA"); 6 | }); 7 | 8 | it("should base64 decode", () => { 9 | expect(decode("dGVzdA")).toEqual("test"); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2015", 4 | "lib": ["es2015", "dom"], 5 | "rootDir": "src", 6 | "outDir": "dist", 7 | "module": "commonjs", 8 | "strict": true, 9 | "declaration": true, 10 | "sourceMap": true, 11 | "inlineSources": true 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { 2 | decode as decodeBase64, 3 | encode as encodeBase64 4 | } from "universal-base64"; 5 | 6 | export function decode(str: string): string { 7 | return decodeBase64(str.replace(/\-/g, "+").replace(/_/g, "/")); 8 | } 9 | 10 | export function encode(str: string): string { 11 | return encodeBase64(str) 12 | .replace(/\//g, "_") 13 | .replace(/\+/g, "-") 14 | .replace(/=+$/, ""); 15 | } 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Blake Embrey (hello@blakeembrey.com) 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Universal Base64 URL 2 | 3 | [![NPM version](https://img.shields.io/npm/v/universal-base64url.svg?style=flat)](https://npmjs.org/package/universal-base64url) 4 | [![NPM downloads](https://img.shields.io/npm/dm/universal-base64url.svg?style=flat)](https://npmjs.org/package/universal-base64url) 5 | [![Build status](https://img.shields.io/travis/blakeembrey/universal-base64url.svg?style=flat)](https://travis-ci.org/blakeembrey/universal-base64url) 6 | [![Test coverage](https://img.shields.io/coveralls/blakeembrey/universal-base64url.svg?style=flat)](https://coveralls.io/r/blakeembrey/universal-base64url?branch=master) 7 | [![Bundle size](https://img.shields.io/bundlephobia/minzip/universal-base64url.svg)](https://bundlephobia.com/result?p=universal-base64url) 8 | 9 | > Small universal [`base64url`](http://en.wikipedia.org/wiki/Base64#RFC_4648) functions for node.js and browsers. 10 | 11 | This is a small wrapper around [`universal-base64`](https://github.com/blakeembrey/universal-base64) to support URL-safe base64 in node.js and browsers. 12 | 13 | ## Installation 14 | 15 | ``` 16 | npm install universal-base64url --save 17 | ``` 18 | 19 | ## Usage 20 | 21 | ```js 22 | import { decode, encode } from "universal-base64url"; 23 | 24 | encode("test"); //=> "dGVzdA" 25 | decode("dGVzdA"); //=> "test" 26 | ``` 27 | 28 | ## TypeScript 29 | 30 | This module uses [TypeScript](https://github.com/Microsoft/TypeScript) and contains type definitions on NPM. 31 | 32 | ## Related 33 | 34 | As of October 2018, https://github.com/brianloveswords/base64url exists but is using the node.js `Buffer` API, instead of falling back on `atob`/`btoa` in browsers. It also has some issues with TypeScript definitions (includes node.js types in browser) and old browser compatibility. This libraries API is smaller and simpler by avoiding these issues. 35 | 36 | ## License 37 | 38 | Apache 2.0 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "universal-base64url", 3 | "version": "1.1.0", 4 | "description": "Small universal base64url functions for node.js and browsers", 5 | "main": "dist/index.js", 6 | "types": "dist/index.d.ts", 7 | "files": [ 8 | "dist/" 9 | ], 10 | "scripts": { 11 | "prettier": "prettier --write", 12 | "lint": "tslint \"src/**/*.ts\" --project tsconfig.json", 13 | "format": "npm run prettier -- \"*.{md,yml,yaml}\" \"src/**/*.{js,jsx,ts,tsx}\"", 14 | "build": "rimraf dist && tsc", 15 | "specs": "jest --coverage", 16 | "test": "npm run -s lint && npm run -s build && npm run -s specs && npm run -s size", 17 | "prepare": "npm run build", 18 | "size": "size-limit" 19 | }, 20 | "size-limit": [ 21 | { 22 | "path": "./dist/index.js", 23 | "limit": "230 B" 24 | } 25 | ], 26 | "repository": { 27 | "type": "git", 28 | "url": "git://github.com/blakeembrey/universal-base64url.git" 29 | }, 30 | "keywords": [ 31 | "base64url", 32 | "base64", 33 | "url", 34 | "universal", 35 | "node", 36 | "browser" 37 | ], 38 | "author": { 39 | "name": "Blake Embrey", 40 | "email": "hello@blakeembrey.com", 41 | "url": "http://blakeembrey.me" 42 | }, 43 | "license": "Apache-2.0", 44 | "bugs": { 45 | "url": "https://github.com/blakeembrey/universal-base64url/issues" 46 | }, 47 | "homepage": "https://github.com/blakeembrey/universal-base64url", 48 | "jest": { 49 | "roots": [ 50 | "/src/" 51 | ], 52 | "transform": { 53 | "\\.tsx?$": "ts-jest" 54 | }, 55 | "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$", 56 | "moduleFileExtensions": [ 57 | "ts", 58 | "tsx", 59 | "js", 60 | "jsx", 61 | "json", 62 | "node" 63 | ] 64 | }, 65 | "husky": { 66 | "hooks": { 67 | "pre-commit": "lint-staged" 68 | } 69 | }, 70 | "lint-staged": { 71 | "*.{js,jsx,ts,tsx,json,css,md,yml,yaml}": [ 72 | "npm run prettier", 73 | "git add" 74 | ] 75 | }, 76 | "publishConfig": { 77 | "access": "public" 78 | }, 79 | "devDependencies": { 80 | "@size-limit/preset-small-lib": "^2.1.6", 81 | "@types/jest": "^24.0.13", 82 | "@types/node": "^12.0.2", 83 | "husky": "^3.0.9", 84 | "jest": "^24.8.0", 85 | "lint-staged": "^9.4.2", 86 | "prettier": "^1.17.1", 87 | "rimraf": "^3.0.0", 88 | "ts-jest": "^24.0.2", 89 | "tslint": "^5.9.1", 90 | "tslint-config-prettier": "^1.18.0", 91 | "tslint-config-standard": "^8.0.1", 92 | "typescript": "^3.1.1" 93 | }, 94 | "dependencies": { 95 | "universal-base64": "^2.1.0" 96 | } 97 | } 98 | --------------------------------------------------------------------------------