file-mapping
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -22,3 +22,3 @@ import EventEmitter from 'node:events'; | ||
*/ | ||
declare function mapping<T>(file: string, fallback: T, write_cb?: (data: T, changes: number) => void): T; | ||
declare function mapping<T>(file: string, fallback?: T, write_cb?: (data: T, changes: number) => void): T; | ||
/** | ||
@@ -36,3 +36,3 @@ * Mapping a file, with event emitter interface. | ||
*/ | ||
constructor(file: string, fallback: T); | ||
constructor(file: string, fallback?: T); | ||
/** | ||
@@ -56,3 +56,3 @@ * Get the mapped value. | ||
*/ | ||
map(file: string, fallback: T): this; | ||
map(file: string, fallback?: T): this; | ||
} | ||
@@ -69,2 +69,12 @@ declare interface Mapping<T> { | ||
export { Mapping, Mapping as default, mapping }; | ||
declare class Collection<T> { | ||
private storage; | ||
private proto?; | ||
private collection; | ||
constructor(storage: string, proto?: T); | ||
get(id: string): Mapping<T>; | ||
data(id: string): T; | ||
delete(id: string): boolean; | ||
} | ||
export { Collection, Mapping, mapping }; |
@@ -1,1 +0,1 @@ | ||
var f=Object.create;var u=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var x=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var y=(e,n)=>{for(var t in n)u(e,t,{get:n[t],enumerable:!0})},g=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of _(n))!S.call(e,i)&&i!==t&&u(e,i,{get:()=>n[i],enumerable:!(r=b(n,i))||r.enumerable});return e};var w=(e,n,t)=>(t=e!=null?f(x(e)):{},g(n||!e||!e.__esModule?u(t,"default",{value:e,enumerable:!0}):t,e)),L=e=>g(u({},"__esModule",{value:!0}),e);var O={};y(O,{Mapping:()=>h,default:()=>P,mapping:()=>l});module.exports=L(O);var v=w(require("events")),c=w(require("fs"));function l(e,n,t){let r=c.default.existsSync(e)?JSON.parse(c.default.readFileSync(e,"utf8")):n,i=Promise.resolve(!0),o=0,p=new Set,m={get:(s,a)=>{let d=s[a];return typeof d=="object"&&d!==null&&!p.has(s[a])&&(s[a]=new Proxy(d,m),p.add(s[a])),s[a]},set:(s,a,d)=>(s[a]=d,o++,(async()=>(await i,o!==0&&(i=new Promise(T=>{c.default.writeFileSync(e,JSON.stringify(r),"utf8"),t&&t(r,o),o=0,T(!0)}))))(),!0)};return new Proxy(r,m)}var h=class extends v.default{constructor(t,r){super();this._written=0;this._file=t,this._data=this.map(t,r)._data}get data(){return this._data}get file(){return this._file}get written(){return this._written}map(t,r){return this._data=l(t,r,(i,o)=>{this._written++,this.emit("write",i,o)}),this._file=t,this}},P=h;0&&(module.exports={Mapping,mapping}); | ||
var O=Object.create;var h=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var J=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var N=(r,t)=>{for(var e in t)h(r,e,{get:t[e],enumerable:!0})},v=(r,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of j(t))!L.call(r,n)&&n!==e&&h(r,n,{get:()=>t[n],enumerable:!(i=M(t,n))||i.enumerable});return r};var g=(r,t,e)=>(e=r!=null?O(J(r)):{},v(t||!r||!r.__esModule?h(e,"default",{value:r,enumerable:!0}):e,r)),P=r=>v(h({},"__esModule",{value:!0}),r);var k={};N(k,{Collection:()=>f,Mapping:()=>p,mapping:()=>y});module.exports=P(k);var x=g(require("events")),u=g(require("fs"));function b(r,t=0){let e=new Set;return JSON.stringify(r,(i,n)=>{if(n&&typeof n=="object"){if(e.has(n))return;e.add(n)}else if(typeof n=="bigint")return n.toString();return n},t)}function y(r,t,e){let i=u.default.existsSync(r)?JSON.parse(u.default.readFileSync(r,"utf8")):JSON.parse(b(t))||Object.create(null),n=Promise.resolve(!0),a=0,w=new Set,T={get:(s,o)=>{let c=s[o];return typeof c=="object"&&c!==null&&!w.has(s[o])&&(s[o]=new Proxy(c,T),w.add(s[o])),s[o]},set:(s,o,c)=>(s[o]=c,a++,(async()=>(await n,a!==0&&(n=new Promise(_=>{u.default.writeFileSync(r,JSON.stringify(i),"utf8"),e&&e(i,a),a=0,_(!0)}))))(),!0)};return new Proxy(i,T)}var p=class extends x.default{constructor(e,i){super();this._written=0;this._file=e,this._data=this.map(e,i)._data}get data(){return this._data}get file(){return this._file}get written(){return this._written}map(e,i){return this._data=y(e,i,(n,a)=>{this._written++,this.emit("write",n,a)}),this._file=e,this}};var d=g(require("fs")),m=g(require("path")),S=require("crypto");var f=class{constructor(t,e){this.collection=new Map;this.storage=m.default.resolve(t),this.proto=e,d.default.existsSync(this.storage)||d.default.mkdirSync(this.storage,{recursive:!0})}get(t){return this.collection.has(t)||this.collection.set(t,new p(m.default.resolve(this.storage,(0,S.createHash)("sha256").update(t).digest("hex")+".json"),this.proto)),this.collection.get(t)}data(t){return this.get(t).data}delete(t){if(this.collection.has(t)){let e=this.get(t);return d.default.unlinkSync(e.file),this.collection.delete(t)}return!1}};0&&(module.exports={Collection,Mapping,mapping}); |
{ | ||
"name": "file-mapping", | ||
"version": "0.1.4", | ||
"description": "A library that maps JSON file and in-memory data, and using lazy-writing strategy to reduce disk I/O.", | ||
"author": "JacobLinCool <jacoblincool@gmail.com> (https://github.com/JacobLinCool)", | ||
"license": "MIT", | ||
"keywords": [ | ||
"file", | ||
"mapping", | ||
"json" | ||
], | ||
"main": "lib/index.js", | ||
"module": "lib/index.mjs", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"test": "jest --coverage", | ||
"dev": "tsup --watch", | ||
"build": "npm run format && tsup", | ||
"docs": "typedoc ./src/", | ||
"format": "prettier --write '**/*.{js,ts,jsx,tsx,json,yml,yaml,html}' --ignore-path .gitignore", | ||
"lint": "eslint ." | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^27.5.2", | ||
"@types/node": "^17.0.40", | ||
"@typescript-eslint/eslint-plugin": "^5.27.1", | ||
"@typescript-eslint/parser": "^5.27.1", | ||
"eslint": "^8.17.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"jest": "^27.5.1", | ||
"prettier": "^2.6.2", | ||
"ts-jest": "^27.1.5", | ||
"tsup": "^5.12.9", | ||
"typedoc": "^0.22.17", | ||
"typescript": "^4.7.3" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/JacobLinCool/file-mapping.git" | ||
}, | ||
"homepage": "https://github.com/JacobLinCool/file-mapping#readme", | ||
"bugs": { | ||
"url": "https://github.com/JacobLinCool/file-mapping/issues" | ||
}, | ||
"packageManager": "pnpm@7.1.7" | ||
} | ||
"name": "file-mapping", | ||
"version": "0.2.0", | ||
"description": "A library that maps JSON file and in-memory data, and using lazy-writing strategy to reduce disk I/O.", | ||
"author": "JacobLinCool <jacoblincool@gmail.com> (https://github.com/JacobLinCool)", | ||
"license": "MIT", | ||
"keywords": [ | ||
"file", | ||
"mapping", | ||
"json" | ||
], | ||
"main": "lib/index.js", | ||
"module": "lib/index.mjs", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"devDependencies": { | ||
"@types/jest": "^27.5.2", | ||
"@types/node": "^18.7.4", | ||
"@typescript-eslint/eslint-plugin": "^5.33.0", | ||
"@typescript-eslint/parser": "^5.33.0", | ||
"eslint": "^8.22.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"jest": "^27.5.1", | ||
"prettier": "^2.7.1", | ||
"ts-jest": "^27.1.5", | ||
"tsup": "^5.12.9", | ||
"typedoc": "0.23.2", | ||
"typescript": "^4.7.4" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/JacobLinCool/file-mapping.git" | ||
}, | ||
"homepage": "https://github.com/JacobLinCool/file-mapping#readme", | ||
"bugs": { | ||
"url": "https://github.com/JacobLinCool/file-mapping/issues" | ||
}, | ||
"packageManager": "pnpm@7.3.0", | ||
"scripts": { | ||
"test": "jest --coverage", | ||
"dev": "tsup --watch", | ||
"build": "npm run format && tsup", | ||
"docs": "typedoc ./src/", | ||
"format": "prettier --write '**/*.{js,ts,jsx,tsx,json,yml,yaml,html}' --ignore-path .gitignore", | ||
"lint": "eslint ." | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
10392
88
4