Skip to content

CP-1104 Add noCredentials config option. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.project

/.packages
/.pub
packages
/pubspec.lock

/coverage
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,21 @@ presents them through WebSocket-like abstractions.

SockJS is intended to work for all modern browsers and in environments
which don't support WebSocket protocol, for example behind restrictive
corporate proxies.
corporate proxies.

## Development

### Dependencies
```
$ pub get && npm install
```

### Tests
```
$ pub run dart_dev test --integration
```

### Coverage
```
$ pub run dart_dev coverage --integration
```
4 changes: 2 additions & 2 deletions lib/sockjs_client.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library sockjs_client;

import "dart:html";
import "dart:html" as html;
import "dart:convert";
import "dart:async";
import "dart:js";
Expand All @@ -25,7 +25,7 @@ const OPEN = 1;
const CLOSING = 2;
const CLOSED = 3;

typedef TransformFactory(Client client, String transUrl, [String baseUrl]);
typedef TransformFactory(Client client, String transUrl, {String baseUrl, bool noCredentials});

class Protocol {
TransformFactory create;
Expand Down
22 changes: 11 additions & 11 deletions lib/src/ajax.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ class StatusEvent extends event.Event {
StatusEvent(String type, [this.status = 0, this.text = ""]) : super(type);
}

typedef AbstractXHRObject AjaxObjectFactory(String method, String baseUrl, [payload]);
typedef AbstractXHRObject AjaxObjectFactory(String method, String baseUrl, {bool noCredentials, payload});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any idea what sort of consumer base this breaking change will have?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only used internally, so I think it's fine


class AbstractXHRObject extends Object with event.Emitter {

HttpRequest xhr;
html.HttpRequest xhr;
StreamSubscription changeSubscription;

Stream get onChunk => this["chunk"];
Stream get onFinish => this["finish"];
Stream get onTimeout => this["timeout"];

_start(method, url, payload, {noCredentials: false, headers}) {
_start(method, url, payload, {bool noCredentials: false, headers}) {

try {
xhr = new HttpRequest();
xhr = new html.HttpRequest();
} catch(x) {};

if ( xhr == null ) {
Expand Down Expand Up @@ -61,7 +61,7 @@ class AbstractXHRObject extends Object with event.Emitter {
xhr.send(payload);
}

_readyStateHandler(Event evt) {
_readyStateHandler(html.Event evt) {
switch (xhr.readyState) {
case 3:
var text, status;
Expand Down Expand Up @@ -106,22 +106,22 @@ class AbstractXHRObject extends Object with event.Emitter {
}

class XHRCorsObject extends AbstractXHRObject {
XHRCorsObject(method, url, payload, {noCredentials, headers} ) {
Timer.run(() =>_start(method, url, payload, noCredentials: false));
XHRCorsObject(method, url, {headers, noCredentials, payload}) {
Timer.run(() =>_start(method, url, payload, noCredentials: noCredentials != null ? noCredentials : false));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use this null aware op for this bit -> x ?? y : z

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i read that right -> noCredentials: noCredentials ?? false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this project has an SDK constraint of >=1.0.0 <2.0.0. If we still want to try to PR this into the main one, that would be a breaking change. I'm okay with doing it though, especially if we just end up using this fork

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's skip it for now

}
}



class XHRLocalObject extends AbstractXHRObject {
XHRLocalObject (method, url, payload, {noCredentials, headers}) {
Timer.run(() =>_start(method, url, payload, noCredentials: true));
XHRLocalObject(method, url, {headers, noCredentials, payload}) {
Timer.run(() =>_start(method, url, payload, noCredentials: noCredentials != null ? noCredentials : true));
}
}

XHRLocalObjectFactory(method, baseUrl, [payload]) => new XHRLocalObject(method, baseUrl, payload);
XHRLocalObjectFactory(method, baseUrl, {bool noCredentials, payload}) => new XHRLocalObject(method, baseUrl, noCredentials: noCredentials, payload: payload);

XHRCorsObjectFactory(method, baseUrl, [payload]) => new XHRCorsObject(method, baseUrl, payload);
XHRCorsObjectFactory(method, baseUrl, {bool noCredentials, payload}) => new XHRCorsObject(method, baseUrl, noCredentials: noCredentials, payload: payload);

// 1. Is natively via XHR
// 2. Is natively via XDR
Expand Down
25 changes: 22 additions & 3 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Client extends Object with event.Emitter {
num roundTrips;
num timeout;

bool noCredentials;

var _ir;

var _transport = null;
Expand All @@ -42,6 +44,7 @@ class Client extends Object with event.Emitter {
this.debug: false,
this.protocolsWhitelist,
this.info,
this.noCredentials: false,
this.rtt: 0,
this.server,
this.roundTrips,
Expand Down Expand Up @@ -70,6 +73,22 @@ class Client extends Object with event.Emitter {
Stream get onClose => this["close"];
Stream get onHeartbeat => this["heartbeat"];

void close([int code, String reason]) {
if (_transport != null) {
if (_transport is WebSocketTransport) {
_transport.doClose(code, reason);
} else if (_transport is XhrStreamingTransport) {
if (code == null) {
code = 0;
}
if (reason == null) {
reason = '';
}
_didClose(code, reason);
}
}
}

send(data) {
if (readyState == CONNECTING) {
throw 'INVALID_STATE_ERR';
Expand Down Expand Up @@ -199,11 +218,11 @@ class Client extends Object with event.Emitter {
// the `head`?
if (PROTOCOLS.containsKey(protocol) &&
PROTOCOLS[protocol].needBody &&
( (document.body == null) || (document.readyState != null && document.readyState != 'complete'))
( (html.document.body == null) || (html.document.readyState != null && html.document.readyState != 'complete'))
) {
_protocols.insert(0, protocol);
this.protocol = 'waiting-for-load';
document.onLoad.listen( (_) => _tryNextProtocol());
html.document.onLoad.listen( (_) => _tryNextProtocol());
return true;
}

Expand Down Expand Up @@ -234,7 +253,7 @@ class Client extends Object with event.Emitter {
var connid = utils.random_string(8);
var trans_url = "$_baseUrl/$server/$connid";
_debug('Opening transport: $protocol url:$trans_url RTO:$rto roundTrips:$roundTrips timeout:$to');
_transport = PROTOCOLS[protocol].create(this, trans_url, _baseUrl);
_transport = PROTOCOLS[protocol].create(this, trans_url, baseUrl: _baseUrl, noCredentials: noCredentials);
return true;
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Info {
origins = json["origins"];
cookieNeeded = json["cookie_needed"];
entropy = json["entropy"];
nullOrigin = (document.domain == null);
nullOrigin = (html.document.domain == null);
}
}

Expand Down Expand Up @@ -84,8 +84,8 @@ class AjaxInfoReceiver extends InfoReceiver {
class InfoReceiverIframe extends InfoReceiver {

InfoReceiverIframe(base_url) : super._() {
if(document.body == null) {
document.onLoad.listen((_) => go());
if(html.document.body == null) {
html.document.onLoad.listen((_) => go());
} else {
go();
}
Expand Down
5 changes: 3 additions & 2 deletions lib/src/transport/polling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ class Polling {
AjaxObjectFactory xhrFactory;
XhrReceiver poll;
bool pollIsClosing = false;
bool noCredentials;

Polling(this.ri, this.receiverFactory, this.recvUrl, this.xhrFactory) {
Polling(this.ri, this.receiverFactory, this.recvUrl, this.xhrFactory, {bool this.noCredentials}) {
_scheduleRecv();
}

_scheduleRecv() {
poll = receiverFactory(recvUrl, xhrFactory);
poll = receiverFactory(recvUrl, xhrFactory, noCredentials: noCredentials);
var msg_counter = 0;
var msgHandler = (e) {
msg_counter += 1;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/transport/receiver-xhr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ part of sockjs_client;

AbstractXHRObject xo = null;

XhrReceiver(url, AjaxObjectFactory xhrFactory) {
XhrReceiver(url, AjaxObjectFactory xhrFactory, {bool noCredentials}) {
var buf_pos = 0;

xo = xhrFactory('POST', url);
xo = xhrFactory('POST', url, noCredentials: noCredentials);
xo.onChunk.listen((e){
if (e.status != 200) return;
while (true) {
Expand Down Expand Up @@ -36,4 +36,4 @@ part of sockjs_client;
}
}

XhrReceiverFactory(String recvUrl, AjaxObjectFactory xhrFactory) => new XhrReceiver(recvUrl, xhrFactory);
XhrReceiverFactory(String recvUrl, AjaxObjectFactory xhrFactory, {bool noCredentials}) => new XhrReceiver(recvUrl, xhrFactory, noCredentials: noCredentials);
24 changes: 12 additions & 12 deletions lib/src/transport/sender.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,39 @@ class BufferedSender {
// postMessage communication */
class JsonPGenericSender {

FormElement _sendForm = null;
TextAreaElement _sendArea = null;
html.FormElement _sendForm = null;
html.TextAreaElement _sendArea = null;

var completed;

JsonPGenericSender(url, payload, callback) {
FormElement form;
TextAreaElement area;
html.FormElement form;
html.TextAreaElement area;

if (_sendForm == null) {
form = _sendForm = new Element.tag('form');
area = _sendArea = new Element.tag('textarea');
form = _sendForm = new html.Element.tag('form');
area = _sendArea = new html.Element.tag('textarea');
area.name = 'd';
form.style.display = 'none';
form.style.position = 'absolute';
form.method = 'POST';
form.enctype = 'application/x-www-form-urlencoded';
form.acceptCharset = "UTF-8";
form.children.add(area);
document.body.children.add(form);
html.document.body.children.add(form);
}
form = _sendForm;
area = _sendArea;
var id = 'a${utils.random_string(8)}';
form.target = id;
form.action = '$url/jsonp_send?i=$id';

IFrameElement iframe;
html.IFrameElement iframe;
try {
// ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
iframe = new Element.html('<iframe name="$id">');
iframe = new html.Element.html('<iframe name="$id">');
} catch(x) {
iframe = new Element.tag('iframe');
iframe = new html.Element.tag('iframe');
iframe.name = id;
}
iframe.id = id;
Expand Down Expand Up @@ -142,9 +142,9 @@ class JsonPGenericSender {
}
}

createAjaxSender(AjaxObjectFactory xhrFactory)
createAjaxSender(AjaxObjectFactory xhrFactory, {bool noCredentials})
=> (url, payload, callback([status, reason])) {
AbstractXHRObject xo = xhrFactory('POST', '$url/xhr_send', payload);
AbstractXHRObject xo = xhrFactory('POST', '$url/xhr_send', payload: payload, noCredentials: noCredentials);
xo.onFinish.listen((e) {
callback(e.status);
});
Expand Down
18 changes: 14 additions & 4 deletions lib/src/transport/websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class WebSocketTransport {
Client ri;
String url;

WebSocket ws;
html.WebSocket ws;
StreamSubscription messageSubscription;
StreamSubscription closeSubscription;

static create(ri, transUrl, [baseUrl]) => new WebSocketTransport(ri, transUrl);
static create(ri, transUrl, {baseUrl, bool noCredentials}) => new WebSocketTransport(ri, transUrl);

WebSocketTransport(this.ri, transUrl) {
var url = '$transUrl/websocket';
Expand All @@ -20,7 +20,7 @@ class WebSocketTransport {

this.url = url;

ws = new WebSocket(url);
ws = new html.WebSocket(url);


messageSubscription = ws.onMessage.listen(_msgHandler);
Expand All @@ -37,7 +37,17 @@ class WebSocketTransport {

_msgHandler(m) => ri._didMessage(m.data);

_closeHandler(m) => ri._didMessage(utils.closeFrame(1006, "WebSocket connection broken"));
_closeHandler(html.CloseEvent c) {
var code = c.code != null ? c.code : 1006;
var reason = c.reason != null ? c.reason : 'WebSocket connection broken';
ri._didMessage(utils.closeFrame(code, reason));
}

doClose([int code, String reason]) {
if (ws != null) {
ws.close(code, reason);
}
}

doSend(data) => ws.send('[$data]');

Expand Down
12 changes: 6 additions & 6 deletions lib/src/transport/xhr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class AjaxBasedTransport extends BufferedSender {

Polling poll = null;

AjaxBasedTransport(Client ri, transUrl, urlSuffix, ReceiverFactory receiverFactory, AjaxObjectFactory xhrFactory) {
AjaxBasedTransport(Client ri, transUrl, urlSuffix, ReceiverFactory receiverFactory, AjaxObjectFactory xhrFactory, {bool noCredentials}) {
this.ri = ri;
this.transUrl = transUrl;
sendConstructor(createAjaxSender(xhrFactory));
this.poll = new Polling(ri, receiverFactory, "$transUrl$urlSuffix", xhrFactory);
sendConstructor(createAjaxSender(xhrFactory, noCredentials: noCredentials));
this.poll = new Polling(ri, receiverFactory, "$transUrl$urlSuffix", xhrFactory, noCredentials: noCredentials);
}

doCleanup() {
Expand All @@ -25,10 +25,10 @@ class AjaxBasedTransport extends BufferedSender {

class XhrStreamingTransport extends AjaxBasedTransport {

XhrStreamingTransport(ri, transUrl) :
super(ri, transUrl, '/xhr_streaming', XhrReceiverFactory, XHRCorsObjectFactory);
XhrStreamingTransport(ri, transUrl, {bool noCredentials}) :
super(ri, transUrl, '/xhr_streaming', XhrReceiverFactory, XHRCorsObjectFactory, noCredentials: noCredentials);

static create(ri, transUrl, [baseUrl]) => new XhrStreamingTransport(ri, transUrl);
static create(ri, transUrl, {baseUrl, bool noCredentials}) => new XhrStreamingTransport(ri, transUrl, noCredentials: noCredentials);

static bool get enabled {
return true;
Expand Down
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "sockjs-dart-client",
"version": "0.2.0",
"description": "A SockJS client in Dart",
"repository": {
"type": "git",
"url": "git://github.com/Workiva/sockjs-dart-client.git"
},
"bugs": {
"url": "https://github.com/Workiva/sockjs-dart-client/issues"
},
"homepage": "https://github.com/Workiva/sockjs-dart-client",
"devDependencies": {
"http": "0.0.0",
"sockjs": "^0.3.15"
}
}
5 changes: 5 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ authors:
dependencies:
browser: '>=0.9.0 <1.0.0'
logging: '>=0.9.0 <1.0.0'
dev_dependencies:
coverage: '>=0.7.2'
dart_dev: '>=1.0.2'
dart_style: '>=0.2.0'
test: '>=0.12.5'
environment:
sdk: '>=1.0.0 <2.0.0'
Loading