Skip to content

Commit

Permalink
Pull in some changes from pull request jonnyreeves#15.
Browse files Browse the repository at this point in the history
  • Loading branch information
ariutta committed Aug 20, 2016
1 parent dbc76f7 commit 4b07bf6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
"env": {
"browser": true
},
"globals": {
"Uint8Array": false,
"TextEncoder": false,
"TextDecoder": false
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"root": true
}
}
10 changes: 10 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ export const root = rootCandidate;

export const TextEncoder = (typeof root.TextEncoder !== 'undefined') ? root.TextEncoder : textEncoding.TextEncoder;
export const TextDecoder = (typeof root.TextDecoder !== 'undefined') ? root.TextDecoder : textEncoding.TextDecoder;

export function uint8ArrayFromString(str) {
const encoder = new TextEncoder();
return encoder.encode(str);
}

export function stringFromUint8Array(arr) {
const decoder = new TextDecoder();
return decoder.decode(arr);
}
40 changes: 17 additions & 23 deletions test/integ/chunked-request.spec.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
import chunkedRequest from '../../src/index';
import isEqual from 'lodash/isEqual';
import isObject from 'lodash/isObject';

// NOTE intentionally using different utf8 implementation here than
// in the lib in order to lower the chance of getting erroneous
// results in both.
import defaults from 'lodash';
import typedArrayPolyfill from 'typedarray';
import { getStringFromBytes } from 'utf-8';
/**
* Root reference for iframes.
* Taken from https://github.com/visionmedia/superagent/blob/master/lib/client.js
*/
let rootCandidate;
if (typeof window !== 'undefined') { // Browser window
rootCandidate = window;
} else if (typeof self !== 'undefined') { // Web Worker
rootCandidate = self;
} else { // Other environments
// TODO should we warn here?
//console.warn('Using browser-only version of superagent in non-browser environment');
rootCandidate = this;
}
defaults(rootCandidate, typedArrayPolyfill);
import { stringFromUint8Array } from '../../src/util';

// These integration tests run through Karma; check `karma.conf.js` for
// configuration. Note that the dev-server which provides the `/chunked-response`
Expand All @@ -49,6 +28,21 @@ describe('chunked-request', () => {
});
});

it('should supply a Uint8Array to the chunkParser', done => {
let actual = false;

const onComplete = () => {
expect(actual).toBe(true);
done();
};

chunkedRequest({
url: `/chunked-response?numChunks=1&entriesPerChunk=1&delimitLast=1`,
chunkParser: bytes => { actual = (bytes instanceof Uint8Array); },
onComplete
});
});

it('should parse utf8 responses', done => {
const receivedChunks = [];

Expand Down Expand Up @@ -154,7 +148,7 @@ describe('chunked-request', () => {
expect(chunkErrors.length).toBe(1, 'one errors caught');
expect(chunkErrors[0].message).toBe('expected');

const rawChunkStr = getStringFromBytes(chunkErrors[0].chunkBytes);
const rawChunkStr = stringFromUint8Array(chunkErrors[0].chunkBytes);
expect(rawChunkStr).toBe(`{ "chunk": "#1", "data": "#0" }\n`);

done();
Expand Down

0 comments on commit 4b07bf6

Please sign in to comment.