@quase/memory-fs
Advanced tools
+75
-30
@@ -6,9 +6,2 @@ "use strict"; | ||
| // TODO import { makeAbsolutePath } from "../../pathname/src/path-url"; | ||
| const path = require("path"); | ||
| function makeAbsolutePath(file) { | ||
| return path.resolve(file); | ||
| } | ||
| const getFileBuffer = require("@quase/get-file").getFileBuffer; | ||
@@ -18,3 +11,5 @@ | ||
| const LRU = require("lru-cache"); | ||
| const { | ||
| makeAbsolute | ||
| } = require("@quase/path-url"); | ||
@@ -26,2 +21,5 @@ const Buffer = typeof global !== "undefined" && global.Buffer; | ||
| const TextEncoder = typeof window !== "undefined" && window.TextEncoder || require("util").TextEncoder; // eslint-disable-line no-undef | ||
| function bufferToString(buf) { | ||
@@ -35,27 +33,72 @@ if (Buffer && buf instanceof Buffer) { | ||
| function stringToBuffer(str) { | ||
| if (Buffer) { | ||
| return Buffer.from(str); | ||
| } | ||
| return new TextEncoder().encode(str); | ||
| } | ||
| class FileSystem { | ||
| constructor(opts) { | ||
| const { | ||
| caches, | ||
| files | ||
| files, | ||
| data | ||
| } = opts || {}; | ||
| this.caches = caches || Object.create(null); | ||
| this.files = files || new Set(); | ||
| this.data = data || Object.create(null); | ||
| this.filesUsed = new Set(); | ||
| } | ||
| _provide(providerName, fn, name) { | ||
| this.files.add(name); | ||
| this.filesUsed.add(name); | ||
| const cache = this.caches[providerName] || (this.caches[providerName] = new LRU(100)); | ||
| let promise = cache.get(name); | ||
| _objFile(file) { | ||
| file = makeAbsolute(file); | ||
| let obj = this.data[file]; | ||
| if (!promise) { | ||
| promise = fn(name); | ||
| cache.set(name, promise); | ||
| if (!obj) { | ||
| this.files.add(file); | ||
| this.filesUsed.add(file); | ||
| obj = this.data[file] = { | ||
| name: file, | ||
| isFile: null, | ||
| isFileP: null, | ||
| content: null, | ||
| contentP: null | ||
| }; | ||
| } | ||
| return promise; | ||
| return obj; | ||
| } | ||
| async isFile(file) { | ||
| const obj = this._objFile(file); | ||
| if (obj.isFile != null) { | ||
| return obj.isFile; | ||
| } | ||
| obj.isFileP = obj.isFileP || isFile(obj.name).then(b => { | ||
| obj.isFile = b; | ||
| obj.isFileP = null; | ||
| return b; | ||
| }); | ||
| return obj.isFileP; | ||
| } | ||
| async getFileBuffer(file) { | ||
| const obj = this._objFile(file); | ||
| if (obj.content != null) { | ||
| return obj.content; | ||
| } | ||
| obj.contentP = obj.contentP || getFileBuffer(obj.name).then(c => { | ||
| obj.isFile = true; | ||
| obj.isFileP = null; | ||
| obj.content = c; | ||
| obj.contentP = null; | ||
| return c; | ||
| }); | ||
| return obj.contentP; | ||
| } | ||
| async getFile(file) { | ||
@@ -65,17 +108,19 @@ return bufferToString((await this.getFileBuffer(file))); | ||
| getFileBuffer(file) { | ||
| return this._provide("getFileBuffer", getFileBuffer, makeAbsolutePath(file)); | ||
| putFileBuffer(file, content) { | ||
| const obj = this._objFile(file); | ||
| obj.isFile = true; | ||
| obj.isFileP = null; | ||
| obj.content = content; | ||
| obj.contentP = null; | ||
| return obj.content; | ||
| } | ||
| isFile(file) { | ||
| return this._provide("isFile", isFile, makeAbsolutePath(file)); | ||
| putFile(file, str) { | ||
| return this.putFileBuffer(file, stringToBuffer(str)); | ||
| } | ||
| purge(what) { | ||
| const file = makeAbsolutePath(what); | ||
| for (const name in this.caches) { | ||
| this.caches[name].del(file); | ||
| } | ||
| const file = makeAbsolute(what); | ||
| this.data[file] = null; | ||
| this.files.delete(file); | ||
@@ -82,0 +127,0 @@ } |
+3
-4
| { | ||
| "name": "@quase/memory-fs", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "In memory file system", | ||
@@ -10,7 +10,6 @@ "license": "MIT", | ||
| "dependencies": { | ||
| "@quase/get-file": "^0.1.0", | ||
| "@quase/get-file": "^0.1.1", | ||
| "@quase/is-file": "^0.1.0", | ||
| "fs-extra": "^4.0.2", | ||
| "lru-cache": "^4.1.1" | ||
| "@quase/path-url": "^0.1.0" | ||
| } | ||
| } |
+67
-27
@@ -1,14 +0,8 @@ | ||
| // TODO import { makeAbsolutePath } from "../../pathname/src/path-url"; | ||
| const path = require( "path" ); | ||
| function makeAbsolutePath( file ) { | ||
| return path.resolve( file ); | ||
| } | ||
| const getFileBuffer = require( "@quase/get-file" ).getFileBuffer; | ||
| const isFile = require( "@quase/is-file" ).default; | ||
| const { makeAbsolute } = require( "@quase/path-url" ); | ||
| const LRU = require( "lru-cache" ); | ||
| const Buffer = typeof global !== "undefined" && global.Buffer; | ||
| const TextDecoder = ( typeof window !== "undefined" && window.TextDecoder ) || require( "util" ).TextDecoder; // eslint-disable-line no-undef | ||
| const TextEncoder = ( typeof window !== "undefined" && window.TextEncoder ) || require( "util" ).TextEncoder; // eslint-disable-line no-undef | ||
@@ -22,25 +16,68 @@ function bufferToString( buf ) { | ||
| function stringToBuffer( str ) { | ||
| if ( Buffer ) { | ||
| return Buffer.from( str ); | ||
| } | ||
| return new TextEncoder().encode( str ); | ||
| } | ||
| export default class FileSystem { | ||
| constructor( opts ) { | ||
| const { caches, files } = opts || {}; | ||
| this.caches = caches || Object.create( null ); | ||
| const { files, data } = opts || {}; | ||
| this.files = files || new Set(); | ||
| this.data = data || Object.create( null ); | ||
| this.filesUsed = new Set(); | ||
| } | ||
| _provide( providerName, fn, name ) { | ||
| this.files.add( name ); | ||
| this.filesUsed.add( name ); | ||
| _objFile( file ) { | ||
| file = makeAbsolute( file ); | ||
| let obj = this.data[ file ]; | ||
| if ( !obj ) { | ||
| this.files.add( file ); | ||
| this.filesUsed.add( file ); | ||
| obj = this.data[ file ] = { | ||
| name: file, | ||
| isFile: null, | ||
| isFileP: null, | ||
| content: null, | ||
| contentP: null | ||
| }; | ||
| } | ||
| return obj; | ||
| } | ||
| const cache = this.caches[ providerName ] || ( this.caches[ providerName ] = new LRU( 100 ) ); | ||
| let promise = cache.get( name ); | ||
| async isFile( file ) { | ||
| const obj = this._objFile( file ); | ||
| if ( !promise ) { | ||
| promise = fn( name ); | ||
| cache.set( name, promise ); | ||
| if ( obj.isFile != null ) { | ||
| return obj.isFile; | ||
| } | ||
| return promise; | ||
| obj.isFileP = obj.isFileP || isFile( obj.name ).then( b => { | ||
| obj.isFile = b; | ||
| obj.isFileP = null; | ||
| return b; | ||
| } ); | ||
| return obj.isFileP; | ||
| } | ||
| async getFileBuffer( file ) { | ||
| const obj = this._objFile( file ); | ||
| if ( obj.content != null ) { | ||
| return obj.content; | ||
| } | ||
| obj.contentP = obj.contentP || getFileBuffer( obj.name ).then( c => { | ||
| obj.isFile = true; | ||
| obj.isFileP = null; | ||
| obj.content = c; | ||
| obj.contentP = null; | ||
| return c; | ||
| } ); | ||
| return obj.contentP; | ||
| } | ||
| async getFile( file ) { | ||
@@ -50,15 +87,18 @@ return bufferToString( await this.getFileBuffer( file ) ); | ||
| getFileBuffer( file ) { | ||
| return this._provide( "getFileBuffer", getFileBuffer, makeAbsolutePath( file ) ); | ||
| putFileBuffer( file, content ) { | ||
| const obj = this._objFile( file ); | ||
| obj.isFile = true; | ||
| obj.isFileP = null; | ||
| obj.content = content; | ||
| obj.contentP = null; | ||
| return obj.content; | ||
| } | ||
| isFile( file ) { | ||
| return this._provide( "isFile", isFile, makeAbsolutePath( file ) ); | ||
| putFile( file, str ) { | ||
| return this.putFileBuffer( file, stringToBuffer( str ) ); | ||
| } | ||
| purge( what ) { | ||
| const file = makeAbsolutePath( what ); | ||
| for ( const name in this.caches ) { | ||
| this.caches[ name ].del( file ); | ||
| } | ||
| const file = makeAbsolute( what ); | ||
| this.data[ file ] = null; | ||
| this.files.delete( file ); | ||
@@ -65,0 +105,0 @@ } |
5679
37.97%3
-25%188
64.91%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
Updated