Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
alvagi
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
alvagi
Commits
fb764f1a
Commit
fb764f1a
authored
Jul 06, 2015
by
antirek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changes in context
parent
9b5e7e71
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
14 deletions
+42
-14
context.js
lib/context.js
+38
-12
index.js
test/index.js
+4
-2
No files found.
lib/context.js
View file @
fb764f1a
...
...
@@ -6,19 +6,27 @@ var commands = require('./command');
var
Context
=
function
(
stream
,
debug
)
{
EventEmitter
.
call
(
this
);
this
.
debug
=
debug
;
this
.
stream
=
new
Readable
();
//this.stream = stream;
this
.
stream
.
setEncoding
(
'
utf8
'
);
this
.
stream
.
wrap
(
stream
);
this
.
state
=
state
.
init
;
this
.
msg
=
""
;
var
self
=
this
;
this
.
stream
.
on
(
'
readable
'
,
function
()
{
//always keep the 'leftover' part of the message
self
.
msg
=
self
.
read
();
});
this
.
msg
=
this
.
read
();
//this.msg = this.read();
this
.
variables
=
{};
this
.
pending
=
null
;
this
.
stream
.
on
(
'
error
'
,
this
.
emit
.
bind
(
this
,
'
error
'
));
this
.
stream
.
on
(
'
close
'
,
this
.
emit
.
bind
(
this
,
'
close
'
));
};
...
...
@@ -27,46 +35,64 @@ require('util').inherits(Context, EventEmitter);
Context
.
prototype
.
read
=
function
()
{
var
buffer
=
this
.
stream
.
read
();
if
(
!
buffer
)
return
this
.
msg
;
this
.
msg
+=
buffer
.
toString
(
'
utf8
'
);
if
(
this
.
state
===
state
.
init
)
{
if
(
!
buffer
)
return
this
.
msg
;
this
.
msg
+=
buffer
;
//.toString('utf8');
if
(
this
.
state
===
state
.
init
)
{
if
(
this
.
msg
.
indexOf
(
'
\n\n
'
)
<
0
)
return
this
.
msg
;
//we don't have whole message
this
.
readVariables
(
this
.
msg
);
}
else
if
(
this
.
state
===
state
.
waiting
)
{
}
else
if
(
this
.
state
===
state
.
waiting
)
{
if
(
this
.
msg
.
indexOf
(
'
\n
'
)
<
0
)
return
this
.
msg
;
//we don't have whole message
this
.
readResponse
(
this
.
msg
);
}
return
""
;
};
Context
.
prototype
.
readVariables
=
function
(
msg
)
{
var
lines
=
msg
.
split
(
'
\n
'
);
/*
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
var split = line.split(':')
var name = split[0];
var value = split[1];
this.variables[name] = (value||'').trim();
}
}*/
lines
.
map
(
function
(
line
)
{
var
split
=
line
.
split
(
'
:
'
);
var
name
=
split
[
0
];
var
value
=
split
[
1
];
this
.
variables
[
name
]
=
(
value
||
''
).
trim
();
},
this
);
this
.
emit
(
'
variables
'
,
this
.
variables
);
this
.
setState
(
state
.
waiting
);
return
""
;
//
return "";
};
Context
.
prototype
.
readResponse
=
function
(
msg
)
{
var
lines
=
msg
.
split
(
'
\n
'
);
/*
for(var i = 0; i < lines.length; i++) {
this.readResponseLine(lines[i]);
}
return
""
;
*/
lines
.
map
(
function
(
line
)
{
this
.
readResponseLine
(
line
);
},
this
);
//return "";
};
Context
.
prototype
.
readResponseLine
=
function
(
line
)
{
if
(
!
line
)
return
;
if
(
!
line
)
return
;
var
parsed
=
/^
(\d{3})(?:
result=
)(
.*
)
/
.
exec
(
line
);
if
(
!
parsed
)
{
if
(
!
parsed
)
{
return
this
.
emit
(
'
hangup
'
);
}
var
response
=
{
...
...
@@ -75,7 +101,7 @@ Context.prototype.readResponseLine = function (line) {
};
//our last command had a pending callback
if
(
this
.
pending
)
{
if
(
this
.
pending
)
{
var
pending
=
this
.
pending
;
this
.
pending
=
null
;
pending
(
null
,
response
);
...
...
@@ -137,7 +163,7 @@ commands.map(function (command) {
};
});
var
prepareArgs
=
function
(
args
,
argsRules
)
{
var
prepareArgs
=
function
(
args
,
argsRules
)
{
var
q
;
if
(
argsRules
)
{
q
=
args
.
map
(
function
(
arg
,
i
)
{
...
...
test/index.js
View file @
fb764f1a
...
...
@@ -21,16 +21,18 @@ var context = function(cb) {
ctx
.
sent
=
ctx
.
sent
||
[];
ctx
.
sent
.
push
(
msg
);
};
ctx
.
once
(
'
variables
'
,
function
(
vars
)
{
cb
(
ctx
);
});
writeVars
(
stream
);
};
describe
(
'
Context
'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
(
done
)
{
var
self
=
this
;
context
(
function
(
context
)
{
context
(
function
(
context
)
{
self
.
context
=
context
;
done
();
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment