| 
									
										
										
										
											2020-04-16 20:05:40 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // data URL scheme: "data:text/javascript;charset=utf-8;base64,some-string"
 | 
					
						
							|  |  |  | // http://www.ietf.org/rfc/rfc2397.txt
 | 
					
						
							| 
									
										
										
										
											2020-06-26 05:14:50 +08:00
										 |  |  | const URIRegEx = /^data:([^;,]+)?((?:;(?:[^;,]+))*?)(;base64)?,(.*)$/i; | 
					
						
							| 
									
										
										
										
											2020-04-16 20:05:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const decodeDataURI = uri => { | 
					
						
							|  |  |  | 	const match = URIRegEx.exec(uri); | 
					
						
							|  |  |  | 	if (!match) return null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const isBase64 = match[3]; | 
					
						
							|  |  |  | 	const body = match[4]; | 
					
						
							|  |  |  | 	return isBase64 | 
					
						
							|  |  |  | 		? Buffer.from(body, "base64") | 
					
						
							|  |  |  | 		: Buffer.from(decodeURIComponent(body), "ascii"); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 15:00:28 +08:00
										 |  |  | const getMimetype = uri => { | 
					
						
							|  |  |  | 	const match = URIRegEx.exec(uri); | 
					
						
							| 
									
										
										
										
											2020-06-05 21:08:06 +08:00
										 |  |  | 	if (!match) return ""; | 
					
						
							| 
									
										
										
										
											2020-05-18 15:00:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return match[1] || "text/plain"; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-16 20:05:40 +08:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-05-18 15:00:28 +08:00
										 |  |  | 	decodeDataURI, | 
					
						
							|  |  |  | 	getMimetype | 
					
						
							| 
									
										
										
										
											2020-04-16 20:05:40 +08:00
										 |  |  | }; |