| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {Filesystem} from '@angular/service-worker/config'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 15:59:57 -07:00
										 |  |  | import {sha1Binary} from './sha1'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class NodeFilesystem implements Filesystem { | 
					
						
							|  |  |  |   constructor(private base: string) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async list(_path: string): Promise<string[]> { | 
					
						
							|  |  |  |     const dir = this.canonical(_path); | 
					
						
							|  |  |  |     const entries = fs.readdirSync(dir).map( | 
					
						
							|  |  |  |         (entry: string) => ({entry, stats: fs.statSync(path.join(dir, entry))})); | 
					
						
							|  |  |  |     const files = entries.filter((entry: any) => !entry.stats.isDirectory()) | 
					
						
							| 
									
										
										
										
											2017-10-10 15:58:13 +03:00
										 |  |  |                       .map((entry: any) => path.posix.join(_path, entry.entry)); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return entries.filter((entry: any) => entry.stats.isDirectory()) | 
					
						
							| 
									
										
										
										
											2017-10-10 15:58:13 +03:00
										 |  |  |         .map((entry: any) => path.posix.join(_path, entry.entry)) | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |         .reduce( | 
					
						
							| 
									
										
										
										
											2017-10-10 15:58:13 +03:00
										 |  |  |             async(list: Promise<string[]>, subdir: string) => | 
					
						
							|  |  |  |                 (await list).concat(await this.list(subdir)), | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |             Promise.resolve(files)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async read(_path: string): Promise<string> { | 
					
						
							|  |  |  |     const file = this.canonical(_path); | 
					
						
							|  |  |  |     return fs.readFileSync(file).toString(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 15:59:57 -07:00
										 |  |  |   async hash(_path: string): Promise<string> { | 
					
						
							|  |  |  |     const file = this.canonical(_path); | 
					
						
							|  |  |  |     const contents: Buffer = fs.readFileSync(file); | 
					
						
							|  |  |  |     return sha1Binary(contents as any as ArrayBuffer); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |   async write(_path: string, contents: string): Promise<void> { | 
					
						
							|  |  |  |     const file = this.canonical(_path); | 
					
						
							|  |  |  |     fs.writeFileSync(file, contents); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 15:58:13 +03:00
										 |  |  |   private canonical(_path: string): string { return path.posix.join(this.base, _path); } | 
					
						
							|  |  |  | } |