Git Product home page Git Product logo

base64url's Introduction

base64url Build Status

Converting to, and from, base64url

Install

$ npm install base64url

After installing with npm you can require this library from JavaScript or TypeScript:

JavaScript

const base64url = require('base64url');

TypeScript:

import base64url from "base64url";

Usage

CLI

The CLI has been removed. For the time being, please install [email protected] if you need the CLI.

Library

base64url(input: string | Buffer, encoding: string = "utf8"): string

base64url.encode(input: string | Buffer, encoding: string = "utf8"): string

base64url encode input. Input should be a string or a Buffer.

Example

> base64url("ladies and gentlemen, we are floating in space")
'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ'

base64url.decode(input: string, encoding: string = "utf8"): string

Convert a base64url encoded string into a raw string. The encoding argument can be used if the input is a string that's not utf8.

> base64url.decode("cmlkZTogZHJlYW1zIGJ1cm4gZG93bg")
'ride: dreams burn down'

base64url.fromBase64(input: string): string

Convert a base64 encoded string to a base64url encoded string.

Example

> base64url.fromBase64('qL8R4QIcQ/ZsRqOAbeRfcZhilN/MksRtDaErMA==')
'qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA'

base64url.toBase64(input: string): string

Convert a base64url encoded string to a base64 encoded string.

> base64url.toBase64('qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA')
'qL8R4QIcQ/ZsRqOAbeRfcZhilN/MksRtDaErMA=='

base64url.toBuffer(input: string): Buffer

Convert a base64url encoded string to a Buffer containing the decoded bytes.

> base64url.toBuffer('c3Bpcml0dWFsaXplZA')
<Buffer 73 70 69 72 69 74 75 61 6c 69 7a 65 64>

Alternatives

Supported Node.js versions

This library should be used with current versions of the Node.js runtime's long-term stable (LTS) schedule. More information can be found at the Node.js Release Working Group repo.

License

MIT

Copyright (c) 2013โ€“2016 Brian J. Brennan

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

base64url's People

Contributors

brianloveswords avatar chuchuva avatar frantello avatar graingert avatar jeffwilcox avatar jfromaniello avatar kishorkunal-raj avatar marco-c avatar mazamachi avatar meirionhughes avatar mylesborins avatar simplesmiler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

base64url's Issues

TSD error

I'm using your lib from npm. But when I start to compile my oun module a have TS error with duplicated node.d.ts declarations. You add folder typings to npm lib. I think it would be better to distribute base64url via TSD. I created base64url.d.ts and made PR.

Can we please not put typescript definitions to the final package

By default typescript tries to resolve declaration by searching, among other locations, in node_modules project - that leads to duplicate definition error which is not that trivial to get rid off (at least some not that well documented steps should be taken).

Here's a link to give you an idea - microsoft/TypeScript#6964

It would be very nice if typescript definitions won't be included to the final package.
After all not a big deal but those a just redundant files that a increasing package size for no reason.

ES6 const

Hello. I've looked through the npm packages that implement base64 url encoding, and your one seems to be the best fit. But I've got a small issue with it.

Using es6 features (such as const) in code published to npm currently breaks environments that do not support these features, including IE10 and older (using browserify). This issue currently stops me from using this package in the app.

Would you mind if I make a PR replacing consts with vars?

Massive number of Dependencies via Meow

Hi, I was looking through the dependency tree of my dependencies and drilled down to this module via jwa/jws. This module relies on meow v2 which has a lot of dependencies, but not as many as v3.

Here's the current dependency tree this module, whose only job is to encode and decode strings: http://npm.anvaka.com/#/view/2d/base64url

Going forward I think it would be ideal if this module dropped CLI support. To me a CLI is a separate concern and should be a separate module, e.g. base64url-cli

Thoughts?

`base64url -D` doesn't decompress

With a debug print in there:

$ ./bin/base64url -D
XXX decode false { d: true,
  decode: false,
  h: false,
  help: false,
  v: false,
  version: false }

I'm guessing meow doesn't support distinguishing uppercase vs lowercase single-char options. A quick fix would be:

diff --git a/bin/base64url b/bin/base64url
index 112e0d6..a9445b7 100755
--- a/bin/base64url
+++ b/bin/base64url
@@ -27,7 +27,7 @@ var cli = require('meow')({
   ],
 });

-var decode = cli.flags.D || cli.flags.decode;
+var decode = cli.flags.d || cli.flags.decode;
 var fn = decode && base64url.decode || base64url.encode;

 function processStream(outputStream, breakLength) {

Node.js v7 Buffer Deprecation Warning

This modules uses Buffer(), causing the following warning to be displayed:

DeprecationWarning: Using Buffer without new will soon stop working. Use new Buffer(), or preferably Buffer.from(), Buffer.allocUnsafe() or Buffer.alloc() instead.

Buffer is not defined

we recently upgraded our application to angular 6 and RxJs 6. Now I get the following error when trying to encode/decode

Uncaught ReferenceError: Buffer is not defined
at encode (base64url.js:5)
at Observable._zoneSubscribe (file-upload-service.ts:160)
at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe [as __zone_symbol__subscribe] (Observable.js:161)
at Observable.value [as subscribe] (zone-patch-rxjs.js:95)
at CatchOperator.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchOperator.call (catchError.js:17)
at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe [as __zone_symbol__subscribe] (Observable.js:158)
at Observable.value [as subscribe] (zone-patch-rxjs.js:95)
at subscribeTo.js:21
at subscribeToResult (subscribeToResult.js:6)
at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._innerSub (mergeMap.js:127)

I'm using base64url 3.0.0 with typescript. I also needed to manually add the following to your package.json to get it to work with noImpliciteany set to true.

"typings": "./dist/base64url.d.ts"

this issue has already been reported (#29)

regards
Ramila

Image cat breaks zip

Hi, the test.jpg image cat always break my zip creations. I removed it manually but it seems corrupted or something.

README needs brackets for encode example

In the README, please change the following (lines 31, 33):

 ### base64url(input: string | Buffer, encoding: string = "utf8"): string
 ### base64url.encode(input: string | Buffer, encoding: string = "utf8"): string

to:

### base64url(input: string | Buffer, {encoding: "utf8"}): string
### base64url.encode(input: string | Buffer, {encoding: "utf8"}): string

Internal base64 decoder doesn't flag incorrect data

I added this to a local version of test:

test('from base64url to string', function (t) {
  const b64url = 'a';
  const result = base64url.encode(base64url.decode(b64url,'binary'));
  t.same(result, b64url, 'should NOT be able to decode, it should throw an error');
  t.end();
});

I also think there are issues with line-breaks which is outside of Base64URL

Documentation : browser / React Native compatibility

I'm having a hard time figuring out in what JS environment will this package work. It makes a clear mention of Node.js but no mention of browser or React Native.

  1. will it work in browsers?
  2. will it work in React Native?
  3. would you be interested in me making a PR to add this to the documentation? Feel free to reject this suggestion if it's obvious to everyone that it will work everywhere ๐Ÿ˜…

ES6 target should be replaced by ES5

ES5 compatible modules, such as 'node-jose', use this module as one of their dependencies. Because the module has been compiled to ES6 it causes problems in browsers that are not ES6 compatible. In our use case the iOS 9 browser.

Can you change 'es6' to 'es5' in the 'tsconfig.json' in order to resolve these type of issues?

Compatibility issue on IE 11 due to default parameters

Since this package is targeting to es6, default parameters will be omitted by ts compiler. However IE 11 doesn't support default parameters.

function decode(base64url: string, encoding: string = "utf8"): string {
    return Buffer.from(toBase64(base64url), "base64").toString(encoding);
}

function encode(input: string | Buffer, encoding: string = "utf8"): string {
    if (Buffer.isBuffer(input)) {
        return fromBase64(input.toString("base64"));
    }
    return fromBase64(Buffer.from(input as string, encoding).toString("base64"));
};

Suggested solution:
change tsconfig.json to target to 'es5' or
avoid using default parameters

Buffer created without "new"

L29 of index.js contains var buffer = Buffer(paddedStringLength);, where it should be var buffer = new Buffer(paddedStringLength);

Doing so will give the warning (node:25634) DeprecationWarning: Using Buffer withoutnewwill soon stop working. Usenew Buffer(), or preferablyBuffer.from(),Buffer.allocUnsafe()orBuffer.alloc()instead. in node v7

Do not publish global typings to npm

Hi,
Your package publishes global typings to npm. This causes conflicts with other (and more recent / acurate) definitions for Node. This module is simply unusable with full TS checks.

Since you use typings, you shouldn't even publish your typings directory but let typings resolve the dependencies with your typings.json file. typings is now deprecated in favor of @types, I'd recommend to switch to @types while fixing this issue.

Vunerability "npm audit"

I'm trying to install gh-pages and received from npm audit the message:
=== npm audit security report ===
Moderate: Out-of-bounds Read
Package: base64url
Patched in: >=3.0.0
Dependency of: gh-pages [dev]
Path: gh-pages > base64url
More info: https://nodesecurity.io/advisories/658

[!] 1 vulnerability found - Packages audited: 47 (47 dev, 0 optional)
Severity: 1 Moderate

Doesnt work with Angular 7 still getting buffer is not defined

Doesnt work with Angular 7 still getting buffer is not defined, can someone else confirm?

Originally posted by @Gabb1995 in #33 (comment)

Package.json

    "@angular/animations": "^7.1.4",
    "@angular/cdk": "^7.3.2",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/flex-layout": "^7.0.0-beta.23",
    "@angular/forms": "~7.1.0",
    "@angular/material": "^7.3.2",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "@fortawesome/fontawesome-free": "^5.7.2",
    "@ngx-translate/core": "^11.0.1",
    "@ngx-translate/http-loader": "^4.0.0",
    "base64url": "^3.0.1",
    "core-js": "^2.5.4",
    "dayjs": "^1.8.11",
    "hammerjs": "^2.0.8",
    "lodash": "^4.17.11",
    "ngx-color-picker": "^7.3.1",
    "ngx-openlayers": "^0.8.22",
    "rxjs": "^6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"

ReferenceError: "Buffer is not defined"
    decode base64url.js:14
    getConfigFromLegacy whitelabel.service.ts:294
    load whitelabel.service.ts:45
    ZoneAwarePromise Angular
    load whitelabel.service.ts:44
    ngOnInit app.component.ts:74
    RxJS 3
app.component.ts:80:32
    ngOnInit app.component.ts:80
    Angular 10

react-scripts build: Failed to minify the code from this file

By trying to build react application it comes to error:

Failed to minify the code from this file: 

 	./node_modules/base64url/dist/base64url.js:4 

Read more here: http://bit.ly/2tRViJ9

They suggest:

Open an issue on the dependency's issue tracker and ask that the package be published pre-compiled.

Note: Create React App can consume both CommonJS and ES modules. For Node.js compatibility, it is recommended that the main entry point is CommonJS. However, they can optionally provide an ES module entry point with the module field in package.json. Note that even if a library provides an ES Modules version, it should still precompile other ES6 features to ES5 if it intends to support older browsers.

base64url: 3.0.0
react-scripts: 1.1.4

Missing definition for the types file

Using [email protected] with [email protected] and Node 8.11.1

Code:

import base64url from 'base64url'

Results in the error:

Could not find a declaration file for module 'base64url'. '/Users/samu/projects/reaktor/vaana/vaana-login/node_modules/base64url/index.js' implicitly has an 'any' type.
Try npm install @types/base64url if it exists or add a new declaration (.d.ts) file containing declare module 'base64url';

Based on the instructions here: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#iincluding-declarations-in-your-npm-package

You need to either define the index.d.ts file explicitly using types in package.json or if your package is based on index.js as the entry point then a default of index.d.ts is used.

Adding "types": "./dist/base64url.d.ts" to the package.json file for base64url gets rid of the error message.

Decode base64 encoded string

I don't want to install another library to decode base64 encoded string, so should I use this lib in strange way like this ?
base64url.decode(base64url.fromBase64(string))

Encoding fails to create a universally decodable base64 string

Example:
Inp: fdsfdsafdsaff|https://cdsf.fdsf.fdsfsd.com/rep|https://lfdjsfjsdfj.fdskfjdsk.com/license

Outputs:
ZmRzZmRzYWZkc2FmZnxodHRwczovL2Nkc2YuZmRzZi5mZHNmc2QuY29tL3JlcHxodHRwczovL2xmZGpzZmpzZGZqLmZkc2tmamRzay5jb20vbGljZW5zZQ

Testing
Decoding:
$ echo | base64 -D

Outputs:
fdsfdsafdsaff|https://cdsf.fdsf.fdsfsd.com/rep|https://lfdjsfjsdfj.fdskfjdsk.com/licens

Expected
The common additional characters at the end of the base64 encoded string should be present, this prevents the loss of the last character when it is read by tools that aren't this library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.