mirror of
				https://kkgithub.com/actions/setup-node.git
				synced 2025-11-04 12:46:16 +08:00 
			
		
		
		
	
		
			
	
	
		
			38 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			38 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| 
								 | 
							
								# btoa-lite
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Smallest/simplest possible means of using btoa with both Node and browserify.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								In the browser, encoding base64 strings is done using:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								``` javascript
							 | 
						||
| 
								 | 
							
								var encoded = btoa(decoded)
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								However in Node, it's done like so:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								``` javascript
							 | 
						||
| 
								 | 
							
								var encoded = new Buffer(decoded).toString('base64')
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								You can easily check if `Buffer` exists and switch between the approaches
							 | 
						||
| 
								 | 
							
								accordingly, but using `Buffer` anywhere in your browser source will pull
							 | 
						||
| 
								 | 
							
								in browserify's `Buffer` shim which is pretty hefty. This package uses
							 | 
						||
| 
								 | 
							
								the `main` and `browser` fields in its `package.json` to perform this
							 | 
						||
| 
								 | 
							
								check at build time and avoid pulling `Buffer` in unnecessarily.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								## Usage
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[](https://nodei.co/npm/btoa-lite/)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								### `encoded = btoa(decoded)`
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Returns the base64-encoded value of a string.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								## License
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								MIT. See [LICENSE.md](http://github.com/hughsk/btoa-lite/blob/master/LICENSE.md) for details.
							 |