Comparing version 1.0.1 to 1.0.2
@@ -0,1 +1,5 @@ | ||
# v1.0.2 | ||
- Lazily enable async hook and disable it when possible | ||
# v1.0.1 | ||
@@ -2,0 +6,0 @@ |
@@ -5,7 +5,7 @@ 'use strict'; | ||
var path = require('path'); | ||
var EventEmitter = require('events'); | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var async_hooks = require('async_hooks'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const util = require('util'); | ||
const EventEmitter = require('events'); | ||
const async_hooks = require('async_hooks'); | ||
@@ -26,8 +26,8 @@ class File { | ||
} | ||
set path(path$$1) { | ||
if (typeof path$$1 !== 'string') { | ||
set path(path) { | ||
if (typeof path !== 'string') { | ||
throw new TypeError('file.path must be a string'); | ||
} | ||
if (this._path !== path$$1) { | ||
this._path = path$$1; | ||
if (this._path !== path) { | ||
this._path = path; | ||
this._dir = this._filename = this._ext = null; | ||
@@ -118,3 +118,3 @@ } | ||
this._queue = []; | ||
this._isProcessing = false; | ||
this._is_processing = false; | ||
Object.assign(this, data); | ||
@@ -124,19 +124,16 @@ } | ||
await this._recurse(this.dir); | ||
return [...this._stats.entries()].map(([path$$1, stats]) => ({ | ||
path: path$$1, | ||
stats, | ||
})); | ||
return [...this._stats.entries()].map(([path, stats]) => ({ path, stats })); | ||
} | ||
async _recurse(full) { | ||
const path$$1 = full.slice(this.dir.length + 1); | ||
const path = full.slice(this.dir.length + 1); | ||
const stats = await stat(full); | ||
if (this.filter && !(await this.filter({ path: path$$1, stats }))) { | ||
if (this.filter && !(await this.filter({ path, stats }))) { | ||
return; | ||
} | ||
if (stats.isFile()) { | ||
this._stats.set(path$$1, stats); | ||
this._stats.set(path, stats); | ||
} | ||
else if (stats.isDirectory()) { | ||
if (this.watch) { | ||
this._watchers.set(path$$1, fs.watch(full, this._handle.bind(this, full))); | ||
this._watchers.set(path, fs.watch(full, this._handle.bind(this, full))); | ||
} | ||
@@ -158,23 +155,23 @@ await Promise.all((await readdir(full)).map(sub => this._recurse(full + '/' + sub))); | ||
this._queue.push(full); | ||
if (this._isProcessing) { | ||
if (this._is_processing) { | ||
return; | ||
} | ||
this._isProcessing = true; | ||
this._is_processing = true; | ||
while (this._queue.length) { | ||
const full = this._queue.shift(); | ||
const path$$1 = full.slice(this.dir.length + 1); | ||
const path = full.slice(this.dir.length + 1); | ||
try { | ||
const stats = await stat(full); | ||
if (this.filter && !(await this.filter({ path: path$$1, stats }))) { | ||
if (this.filter && !(await this.filter({ path, stats }))) { | ||
continue; | ||
} | ||
if (stats.isFile()) { | ||
this._stats.set(path$$1, stats); | ||
this.emit('', { event: '+', path: path$$1, stats }); | ||
this._stats.set(path, stats); | ||
this.emit('', { event: '+', path, stats }); | ||
} | ||
else if (stats.isDirectory() && !this._watchers.has(path$$1)) { | ||
else if (stats.isDirectory() && !this._watchers.has(path)) { | ||
await this._recurse(full); | ||
for (const [newPath, stats] of this._stats.entries()) { | ||
if (newPath.startsWith(path$$1 + '/')) { | ||
this.emit('', { event: '+', path: newPath, stats }); | ||
for (const [new_path, stats] of this._stats.entries()) { | ||
if (new_path.startsWith(path + '/')) { | ||
this.emit('', { event: '+', path: new_path, stats }); | ||
} | ||
@@ -185,9 +182,9 @@ } | ||
catch (e) { | ||
if (this._stats.has(path$$1)) { | ||
this._stats.delete(path$$1); | ||
this.emit('', { event: '-', path: path$$1 }); | ||
if (this._stats.has(path)) { | ||
this._stats.delete(path); | ||
this.emit('', { event: '-', path }); | ||
} | ||
else if (this._watchers.has(path$$1)) { | ||
else if (this._watchers.has(path)) { | ||
for (const old of this._watchers.keys()) { | ||
if (old === path$$1 || old.startsWith(path$$1 + '/')) { | ||
if (old === path || old.startsWith(path + '/')) { | ||
this._watchers.get(old).close(); | ||
@@ -198,3 +195,3 @@ this._watchers.delete(old); | ||
for (const old of this._stats.keys()) { | ||
if (old.startsWith(path$$1 + '/')) { | ||
if (old.startsWith(path + '/')) { | ||
this._stats.delete(old); | ||
@@ -207,3 +204,3 @@ this.emit('', { event: '-', path: old }); | ||
} | ||
this._isProcessing = false; | ||
this._is_processing = false; | ||
} | ||
@@ -213,6 +210,13 @@ } | ||
const contexts = new Map(); | ||
async_hooks.createHook({ | ||
const hook = async_hooks.createHook({ | ||
init: (id, _, trigger) => contexts.set(id, contexts.get(trigger)), | ||
destroy: id => contexts.delete(id), | ||
}).enable(); | ||
}); | ||
let refs = 0; | ||
const ref = () => { | ||
refs++ || hook.enable(); | ||
}; | ||
const unref = () => { | ||
--refs || hook.disable(); | ||
}; | ||
const create = (data) => { | ||
@@ -227,11 +231,11 @@ contexts.set(async_hooks.executionAsyncId(), data); | ||
this.paths = new Set(); | ||
this._origData = new Map(); | ||
this._orig_data = new Map(); | ||
this.files = new Map(); | ||
this._status = 0; | ||
this._active = new Set(); | ||
this._whenFound = new Map(); | ||
this._when_found = new Map(); | ||
this._deps = []; | ||
this._queue = []; | ||
this._isProcessing = false; | ||
this._endWave = null; | ||
this._is_processing = false; | ||
this._end_wave = null; | ||
const { transform, generators = [], resolver, onerror } = args.pop(); | ||
@@ -280,2 +284,3 @@ if (typeof transform !== 'function') { | ||
async exec() { | ||
ref(); | ||
if (this._status !== 0) { | ||
@@ -285,4 +290,4 @@ throw new Error('defiler.exec: cannot call more than once'); | ||
this._status = 1; | ||
this._isProcessing = true; | ||
const done = this._startWave(); | ||
this._is_processing = true; | ||
const done = this._start_wave(); | ||
const files = []; | ||
@@ -293,3 +298,3 @@ await Promise.all(this._watchers.map(async (watcher) => { | ||
await Promise.all((await watcher.init()).map(async (file) => { | ||
const { path: path$$1 } = file; | ||
const { path } = file; | ||
if (watcher.pre) { | ||
@@ -300,3 +305,3 @@ await watcher.pre(file); | ||
this._active.add(file.path); | ||
files.push([watcher, path$$1, file]); | ||
files.push([watcher, path, file]); | ||
})); | ||
@@ -307,12 +312,17 @@ })); | ||
} | ||
for (const [watcher, path$$1, file] of files) { | ||
this._processPhysicalFile(watcher, path$$1, file); | ||
for (const [watcher, path, file] of files) { | ||
this._process_physical_file(watcher, path, file); | ||
} | ||
for (const generator of this._generators) { | ||
this._processGenerator(generator); | ||
this._process_generator(generator); | ||
} | ||
await done; | ||
this._status = 2; | ||
this._isProcessing = false; | ||
this._enqueue(); | ||
this._is_processing = false; | ||
if (this._watchers.some(watcher => watcher.watch)) { | ||
this._enqueue(); | ||
} | ||
else { | ||
unref(); | ||
} | ||
} | ||
@@ -324,3 +334,3 @@ async get(_) { | ||
if (Array.isArray(_)) { | ||
return Promise.all(_.map(path$$1 => this.get(path$$1))); | ||
return Promise.all(_.map(path => this.get(path))); | ||
} | ||
@@ -330,10 +340,10 @@ if (typeof _ !== 'string' && typeof _ !== 'function') { | ||
} | ||
const current$$1 = current(); | ||
if (current$$1) { | ||
this._deps.push([current$$1, _]); | ||
const current$1 = current(); | ||
if (current$1) { | ||
this._deps.push([current$1, _]); | ||
} | ||
if (this._status === 1 && current$$1 && (typeof _ === 'function' || !this.files.has(_))) { | ||
if (this._whenFound.has(_)) { | ||
const { promise, paths } = this._whenFound.get(_); | ||
paths.push(current$$1); | ||
if (this._status === 1 && current$1 && (typeof _ === 'function' || !this.files.has(_))) { | ||
if (this._when_found.has(_)) { | ||
const { promise, paths } = this._when_found.get(_); | ||
paths.push(current$1); | ||
await promise; | ||
@@ -344,3 +354,3 @@ } | ||
const promise = new Promise(res => (resolve = res)); | ||
this._whenFound.set(_, { promise, resolve, paths: [current$$1] }); | ||
this._when_found.set(_, { promise, resolve, paths: [current$1] }); | ||
await promise; | ||
@@ -359,10 +369,10 @@ } | ||
file.path = this.resolve(file.path); | ||
this._origData.set(file.path, file); | ||
this._processFile(file, 'add'); | ||
this._orig_data.set(file.path, file); | ||
this._process_file(file, 'add'); | ||
} | ||
resolve(path$$1) { | ||
return this._resolver && typeof current() === 'string' ? this._resolver(current(), path$$1) : path$$1; | ||
resolve(path) { | ||
return this._resolver && typeof current() === 'string' ? this._resolver(current(), path) : path; | ||
} | ||
_startWave() { | ||
return new Promise(res => (this._endWave = res)); | ||
_start_wave() { | ||
return new Promise(res => (this._end_wave = res)); | ||
} | ||
@@ -373,10 +383,10 @@ async _enqueue(watcher, event) { | ||
} | ||
if (this._isProcessing) { | ||
if (this._is_processing) { | ||
return; | ||
} | ||
this._isProcessing = true; | ||
this._is_processing = true; | ||
while (this._queue.length) { | ||
const done = this._startWave(); | ||
const [watcher, { event, path: path$$1, stats }] = this._queue.shift(); | ||
const file = { path: path$$1, stats }; | ||
const done = this._start_wave(); | ||
const [watcher, { event, path, stats }] = this._queue.shift(); | ||
const file = { path, stats }; | ||
if (watcher.pre) { | ||
@@ -386,48 +396,48 @@ await watcher.pre(file); | ||
if (event === '+') { | ||
this._processPhysicalFile(watcher, path$$1, file); | ||
this._process_physical_file(watcher, path, file); | ||
} | ||
else if (event === '-') { | ||
const { path: path$$1 } = file; | ||
const oldFile = this.files.get(path$$1); | ||
this.paths.delete(path$$1); | ||
this._origData.delete(path$$1); | ||
this.files.delete(path$$1); | ||
await this._callTransform(oldFile, 'delete'); | ||
this._processDependents(path$$1); | ||
const { path } = file; | ||
const old_file = this.files.get(path); | ||
this.paths.delete(path); | ||
this._orig_data.delete(path); | ||
this.files.delete(path); | ||
await this._call_transform(old_file, 'delete'); | ||
this._process_dependents(path); | ||
} | ||
await done; | ||
} | ||
this._isProcessing = false; | ||
this._is_processing = false; | ||
} | ||
async _processPhysicalFile({ dir, read, enc }, path$$1, file) { | ||
async _process_physical_file({ dir, read, enc }, path, file) { | ||
if (typeof read === 'function') { | ||
read = await read({ path: path$$1, stats: file.stats }); | ||
read = await read({ path, stats: file.stats }); | ||
} | ||
if (read) { | ||
file.bytes = await readFile(dir + '/' + path$$1); | ||
file.bytes = await readFile(dir + '/' + path); | ||
} | ||
if (typeof enc === 'function') { | ||
enc = await enc({ path: path$$1, stats: file.stats, bytes: file.bytes }); | ||
enc = await enc({ path, stats: file.stats, bytes: file.bytes }); | ||
} | ||
file.enc = enc; | ||
this.paths.add(file.path); | ||
this._origData.set(file.path, file); | ||
await this._processFile(file, 'read'); | ||
this._orig_data.set(file.path, file); | ||
await this._process_file(file, 'read'); | ||
} | ||
async _processFile(data, event) { | ||
async _process_file(data, event) { | ||
const file = Object.assign(new File(), data); | ||
const { path: path$$1 } = file; | ||
this._active.add(path$$1); | ||
await this._callTransform(file, event); | ||
this.files.set(path$$1, file); | ||
const { path } = file; | ||
this._active.add(path); | ||
await this._call_transform(file, event); | ||
this.files.set(path, file); | ||
if (this._status === 1) { | ||
this._markFound(path$$1); | ||
this._mark_found(path); | ||
} | ||
else { | ||
this._processDependents(path$$1); | ||
this._process_dependents(path); | ||
} | ||
this._active.delete(path$$1); | ||
this._checkWave(); | ||
this._active.delete(path); | ||
this._check_wave(); | ||
} | ||
async _callTransform(file, event) { | ||
async _call_transform(file, event) { | ||
await null; | ||
@@ -444,3 +454,3 @@ create(file.path); | ||
} | ||
async _processGenerator(generator) { | ||
async _process_generator(generator) { | ||
this._active.add(generator); | ||
@@ -458,8 +468,8 @@ await null; | ||
this._active.delete(generator); | ||
this._checkWave(); | ||
this._check_wave(); | ||
} | ||
_processDependents(path$$1) { | ||
_process_dependents(path) { | ||
const dependents = new Set(); | ||
for (const [dependent, dependency] of this._deps) { | ||
if (typeof dependency === 'string' ? dependency === path$$1 : dependency(path$$1)) { | ||
if (typeof dependency === 'string' ? dependency === path : dependency(path)) { | ||
dependents.add(dependent); | ||
@@ -471,34 +481,34 @@ } | ||
if (typeof dependent === 'function') { | ||
this._processGenerator(dependent); | ||
this._process_generator(dependent); | ||
} | ||
else if (this._origData.has(dependent)) { | ||
this._processFile(this._origData.get(dependent), 'retransform'); | ||
else if (this._orig_data.has(dependent)) { | ||
this._process_file(this._orig_data.get(dependent), 'retransform'); | ||
} | ||
} | ||
this._checkWave(); | ||
this._check_wave(); | ||
} | ||
_checkWave() { | ||
_check_wave() { | ||
if (!this._active.size) { | ||
this._endWave(); | ||
this._end_wave(); | ||
} | ||
else if (this._status === 1) { | ||
const filterWaiting = new Set(); | ||
const allWaiting = new Set(); | ||
for (const [path$$1, { paths }] of this._whenFound) { | ||
if (typeof path$$1 === 'function' || this._active.has(path$$1)) { | ||
paths.forEach(path$$1 => filterWaiting.add(path$$1)); | ||
const filter_waiting = new Set(); | ||
const all_waiting = new Set(); | ||
for (const [path, { paths }] of this._when_found) { | ||
if (typeof path === 'function' || this._active.has(path)) { | ||
paths.forEach(path => filter_waiting.add(path)); | ||
} | ||
paths.forEach(path$$1 => allWaiting.add(path$$1)); | ||
paths.forEach(path => all_waiting.add(path)); | ||
} | ||
if ([...this._active].every(path$$1 => filterWaiting.has(path$$1))) { | ||
for (const path$$1 of this._whenFound.keys()) { | ||
if (typeof path$$1 === 'function') { | ||
this._markFound(path$$1); | ||
if ([...this._active].every(path => filter_waiting.has(path))) { | ||
for (const path of this._when_found.keys()) { | ||
if (typeof path === 'function') { | ||
this._mark_found(path); | ||
} | ||
} | ||
} | ||
else if ([...this._active].every(path$$1 => allWaiting.has(path$$1))) { | ||
for (const path$$1 of this._whenFound.keys()) { | ||
if (typeof path$$1 === 'string' && !this._active.has(path$$1)) { | ||
this._markFound(path$$1); | ||
else if ([...this._active].every(path => all_waiting.has(path))) { | ||
for (const path of this._when_found.keys()) { | ||
if (typeof path === 'string' && !this._active.has(path)) { | ||
this._mark_found(path); | ||
} | ||
@@ -509,6 +519,6 @@ } | ||
} | ||
_markFound(path$$1) { | ||
if (this._whenFound.has(path$$1)) { | ||
this._whenFound.get(path$$1).resolve(); | ||
this._whenFound.delete(path$$1); | ||
_mark_found(path) { | ||
if (this._when_found.has(path)) { | ||
this._when_found.get(path).resolve(); | ||
this._when_found.delete(path); | ||
} | ||
@@ -518,4 +528,4 @@ } | ||
exports.Defiler = Defiler; | ||
exports.File = File; | ||
exports.Defiler = Defiler; | ||
//# sourceMappingURL=index.cjs.js.map |
{ | ||
"name": "defiler", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A small, strange building block", | ||
@@ -12,3 +12,3 @@ "keywords": [ | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.es.js", | ||
"module": "dist/index.esm.js", | ||
"types": "types/index.d.ts", | ||
@@ -31,7 +31,7 @@ "files": [ | ||
}, | ||
"homepage": "https://conduitry.io/defiler", | ||
"homepage": "https://cndtr.dev/defiler", | ||
"devDependencies": { | ||
"rollup": "*", | ||
"rollup-plugin-cheap-ts": "Conduitry/rollup-plugin-cheap-ts#semver:*", | ||
"typescript": "*", | ||
"rollup": "^1", | ||
"rollup-plugin-cheap-ts": "Conduitry/rollup-plugin-cheap-ts#semver:^1", | ||
"typescript": "^3", | ||
"@types/node": "*" | ||
@@ -38,0 +38,0 @@ }, |
@@ -16,11 +16,9 @@ # Defiler: A small, strange building block. | ||
- [guide](GUIDE.md#readme) | ||
- [api](API.md#readme) | ||
- [changelog](CHANGELOG.md#readme) | ||
- [homepage](https://conduitry.io/defiler) | ||
- [Guide](GUIDE.md) | ||
- [API Reference](API.md) | ||
- [Changelog](CHANGELOG.md) | ||
- [Homepage](https://cndtr.dev/defiler) | ||
## License | ||
Copyright (c) 2017-2018 Conduitry | ||
- [MIT](LICENSE) | ||
[MIT](LICENSE) |
import * as fs from 'fs'; | ||
export class Defiler { | ||
paths: Set<string>; | ||
files: Map<string, File>; | ||
constructor(...args: any[]); | ||
exec(): Promise<void>; | ||
get(path: string): Promise<File>; | ||
get(paths: string[]): Promise<File[]>; | ||
get(filter: Filter): Promise<File[]>; | ||
add(file: FileData): void; | ||
resolve(path: string): string; | ||
paths: Set<string>; | ||
files: Map<string, File>; | ||
constructor(...args: any[]); | ||
exec(): Promise<void>; | ||
get(path: string): Promise<File>; | ||
get(paths: string[]): Promise<File[]>; | ||
get(filter: Filter): Promise<File[]>; | ||
add(file: FileData): void; | ||
resolve(path: string): string; | ||
} | ||
@@ -21,3 +21,3 @@ | ||
ext: string; | ||
enc: string; | ||
enc: BufferEncoding; | ||
bytes: Buffer; | ||
@@ -28,8 +28,8 @@ text: string; | ||
interface Filter { | ||
(path: string): boolean; | ||
(path: string): boolean; | ||
} | ||
interface FileData { | ||
path: string; | ||
[propName: string]: any; | ||
path: string; | ||
[propName: string]: any; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
127387
1022
24
1