Security update for permissions_by_term
[yaffs-website] / node_modules / fstream / examples / pipe.js
1 var fstream = require('../fstream.js')
2 var path = require('path')
3
4 var r = fstream.Reader({
5   path: path.dirname(__dirname),
6   filter: function () {
7     return !this.basename.match(/^\./) &&
8       !this.basename.match(/^node_modules$/) &&
9       !this.basename.match(/^deep-copy$/)
10   }
11 })
12
13 var w = fstream.Writer({
14   path: path.resolve(__dirname, 'deep-copy'),
15   type: 'Directory'
16 })
17
18 var indent = ''
19
20 r.on('entry', appears)
21 r.on('ready', function () {
22   console.error('ready to begin!', r.path)
23 })
24
25 function appears (entry) {
26   console.error(indent + 'a %s appears!', entry.type, entry.basename, typeof entry.basename, entry)
27   if (foggy) {
28     console.error('FOGGY!')
29     var p = entry
30     do {
31       console.error(p.depth, p.path, p._paused)
32       p = p.parent
33     } while (p)
34
35     throw new Error('\u001b[mshould not have entries while foggy')
36   }
37   indent += '\t'
38   entry.on('data', missile(entry))
39   entry.on('end', runaway(entry))
40   entry.on('entry', appears)
41 }
42
43 var foggy
44 function missile (entry) {
45   function liftFog (who) {
46     if (!foggy) return
47     if (who) {
48       console.error('%s breaks the spell!', who && who.path)
49     } else {
50       console.error('the spell expires!')
51     }
52     console.error('\u001b[mthe fog lifts!\n')
53     clearTimeout(foggy)
54     foggy = null
55     if (entry._paused) entry.resume()
56   }
57
58   if (entry.type === 'Directory') {
59     var ended = false
60     entry.once('end', function () { ended = true })
61     return function (c) {
62       // throw in some pathological pause()/resume() behavior
63       // just for extra fun.
64       process.nextTick(function () {
65         if (!foggy && !ended) { // && Math.random() < 0.3) {
66           console.error(indent + '%s casts a spell', entry.basename)
67           console.error('\na slowing fog comes over the battlefield...\n\u001b[32m')
68           entry.pause()
69           entry.once('resume', liftFog)
70           foggy = setTimeout(liftFog, 10)
71         }
72       })
73     }
74   }
75
76   return function (c) {
77     var e = Math.random() < 0.5
78     console.error(indent + '%s %s for %d damage!',
79       entry.basename,
80       e ? 'is struck' : 'fires a chunk',
81       c.length)
82   }
83 }
84
85 function runaway (entry) {
86   return function () {
87     var e = Math.random() < 0.5
88     console.error(indent + '%s %s',
89       entry.basename,
90       e ? 'turns to flee' : 'is vanquished!')
91     indent = indent.slice(0, -1)
92   }
93 }
94
95 w.on('entry', attacks)
96 // w.on('ready', function () { attacks(w) })
97 function attacks (entry) {
98   console.error(indent + '%s %s!', entry.basename,
99     entry.type === 'Directory' ? 'calls for backup' : 'attacks')
100   entry.on('entry', attacks)
101 }
102
103 var ended = false
104 r.on('end', function () {
105   if (foggy) clearTimeout(foggy)
106   console.error("\u001b[mIT'S OVER!!")
107   console.error('A WINNAR IS YOU!')
108
109   console.log('ok 1 A WINNAR IS YOU')
110   ended = true
111 })
112
113 process.on('exit', function () {
114   console.log((ended ? '' : 'not ') + 'ok 2 ended')
115   console.log('1..2')
116 })
117
118 r.pipe(w)