Skip to content

Commit 93cfbc7

Browse files
committedFeb 19, 2019
Removed text-encoding for smaller implementation
1 parent c7bc09f commit 93cfbc7

8 files changed

+254
-38
lines changed
 

‎Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ module.exports = function(grunt) {
1616
'build': {
1717
files: {
1818
'dist/xapiwrapper.min.js': [
19-
'node_modules/text-encoding/lib/encoding.js',
2019
'lib/cryptojs_v3.1.2.js',
20+
'lib/utf8-text-encoding.js',
2121
'src/activitytypes.js',
2222
'src/verbs.js',
2323
'src/xapiwrapper.js',

‎README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ which can be used to tell if you're using the latest version.
2323
### Dependencies
2424
The wrapper relies on external dependencies to perform some actions. Make sure you include
2525
our compilation of the necessary [CryptoJS](https://code.google.com/p/crypto-js/) components
26-
in your pages if you're not using `xapiwrapper.min.js`
26+
in your pages if you're not using `xapiwrapper.min.js`.
2727

2828
``` html
2929
<script type="text/javascript" src="./lib/cryptojs_v3.1.2.js"></script>
@@ -35,6 +35,12 @@ for current systems, but the CryptoJS compilation is recommended.
3535
* base64.js - https://code.google.com/p/javascriptbase64/downloads/list
3636
* 2.5.3-crypto-sha1.js - https://code.google.com/p/crypto-js/downloads/detail?name=2.5.3-crypto-sha1.js&can=4&q=
3737

38+
For [IE/Edge support](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder#Browser_compatibility) you
39+
will need to include a `TextEncoder` and `TextDecoder` shim if you're not using `xapiwrapper.min.js`
40+
``` html
41+
<script type="text/javascript" src="./lib/utf8-text-encoding.js"></script>
42+
```
43+
3844
## Installing
3945

4046
Using this wrapper could either be done by downloading the [latest release](https://github.com/adlnet/xAPIWrapper#downloading-the-latest-release-version) or [cloning the project](https://github.com/adlnet/xAPIWrapper#cloning-and-building-the-project).

‎dist/xapiwrapper.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/utf8-text-encoding.js

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
(function(root) {
2+
3+
// NOTES:
4+
// Provides utf8 only implementation of TextEncoder and TextDecoder for IE10+
5+
// https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
6+
// https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
7+
8+
// It is a modified version of https://gist.github.com/Yaffle/5458286 for IE10+ and Uint8Array compatibility
9+
// https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder#Methods
10+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
11+
12+
if (typeof root.TextEncoder === 'undefined') {
13+
14+
// Normalise String.prototype.codePointAt for IE10+
15+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt
16+
function StringToCodePointAt(string, position) {
17+
if (String.prototype.codePointAt) return string.codePointAt(position);
18+
string = String(string);
19+
var size = string.length;
20+
// `ToInteger`
21+
var index = position ? Number(position) : 0;
22+
if (index != index) { // better `isNaN`
23+
index = 0;
24+
}
25+
// Account for out-of-bounds indices:
26+
if (index < 0 || index >= size) {
27+
return undefined;
28+
}
29+
// Get the first code unit
30+
var first = string.charCodeAt(index);
31+
var second;
32+
if ( // check if it’s the start of a surrogate pair
33+
first >= 0xD800 && first <= 0xDBFF && // high surrogate
34+
size > index + 1 // there is a next code unit
35+
) {
36+
second = string.charCodeAt(index + 1);
37+
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
38+
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
39+
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
40+
}
41+
}
42+
return first;
43+
};
44+
45+
function TextEncoder(encoding) {
46+
if (!encoding) encoding = 'utf8';
47+
switch (encoding) {
48+
case 'utf-8':
49+
case 'utf8':
50+
break;
51+
default:
52+
throw 'TextEncoder only supports utf8';
53+
}
54+
}
55+
TextEncoder.prototype.encode = function (string) {
56+
var octets = new Uint8Array(string.length * 3);
57+
var pos = -1;
58+
var length = string.length;
59+
var i = 0;
60+
while (i < length) {
61+
var codePoint = StringToCodePointAt(string, i);
62+
var c = 0;
63+
var bits = 0;
64+
if (codePoint <= 0x0000007F) {
65+
c = 0;
66+
bits = 0x00;
67+
} else if (codePoint <= 0x000007FF) {
68+
c = 6;
69+
bits = 0xC0;
70+
} else if (codePoint <= 0x0000FFFF) {
71+
c = 12;
72+
bits = 0xE0;
73+
} else if (codePoint <= 0x001FFFFF) {
74+
c = 18;
75+
bits = 0xF0;
76+
}
77+
octets[pos += 1] = (bits | (codePoint >> c));
78+
c -= 6;
79+
while (c >= 0) {
80+
octets[pos += 1] = (0x80 | ((codePoint >> c) & 0x3F));
81+
c -= 6;
82+
}
83+
i += codePoint >= 0x10000 ? 2 : 1;
84+
}
85+
return octets.subarray(0, pos+1);
86+
};
87+
88+
root.TextEncoder = TextEncoder;
89+
90+
}
91+
92+
if (typeof root.TextDecoder === 'undefined') {
93+
94+
// Normalise String.fromCodePointAt for IE10+
95+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
96+
function StringFromCodePoint(_) {
97+
if (String.fromCodePoint) return String.fromCodePoint(_);
98+
var codeUnits = [], codeLen = 0, result = '';
99+
for (var index=0, len = arguments.length; index !== len; ++index) {
100+
var codePoint = +arguments[index];
101+
// correctly handles all cases including `NaN`, `-Infinity`, `+Infinity`
102+
// The surrounding `!(...)` is required to correctly handle `NaN` cases
103+
// The (codePoint>>>0) === codePoint clause handles decimals and negatives
104+
if (!(codePoint < 0x10FFFF && (codePoint>>>0) === codePoint))
105+
throw RangeError('Invalid code point: ' + codePoint);
106+
if (codePoint <= 0xFFFF) { // BMP code point
107+
codeLen = codeUnits.push(codePoint);
108+
} else { // Astral code point; split in surrogate halves
109+
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
110+
codePoint -= 0x10000;
111+
codeLen = codeUnits.push(
112+
(codePoint >> 10) + 0xD800, // highSurrogate
113+
(codePoint % 0x400) + 0xDC00 // lowSurrogate
114+
);
115+
}
116+
if (codeLen >= 0x3fff) {
117+
result += String.fromCharCode.apply(null, codeUnits);
118+
codeUnits.length = 0;
119+
}
120+
}
121+
return result + String.fromCharCode.apply(null, codeUnits);
122+
};
123+
124+
function TextDecoder(encoding) {
125+
if (!encoding) encoding = 'utf8';
126+
switch (encoding) {
127+
case 'utf-8':
128+
case 'utf8':
129+
break;
130+
default:
131+
throw 'TextDecoder only supports utf8';
132+
}
133+
}
134+
TextDecoder.prototype.decode = function (octets) {
135+
var string = '';
136+
var i = 0;
137+
while (i < octets.length) {
138+
var octet = octets[i];
139+
var bytesNeeded = 0;
140+
var codePoint = 0;
141+
if (octet <= 0x7F) {
142+
bytesNeeded = 0;
143+
codePoint = octet & 0xFF;
144+
} else if (octet <= 0xDF) {
145+
bytesNeeded = 1;
146+
codePoint = octet & 0x1F;
147+
} else if (octet <= 0xEF) {
148+
bytesNeeded = 2;
149+
codePoint = octet & 0x0F;
150+
} else if (octet <= 0xF4) {
151+
bytesNeeded = 3;
152+
codePoint = octet & 0x07;
153+
}
154+
if (octets.length - i - bytesNeeded > 0) {
155+
var k = 0;
156+
while (k < bytesNeeded) {
157+
octet = octets[i + k + 1];
158+
codePoint = (codePoint << 6) | (octet & 0x3F);
159+
k += 1;
160+
}
161+
} else {
162+
codePoint = 0xFFFD;
163+
bytesNeeded = octets.length - i;
164+
}
165+
string += StringFromCodePoint(codePoint);
166+
i += bytesNeeded + 1;
167+
}
168+
return string
169+
};
170+
171+
root.TextDecoder = TextDecoder;
172+
173+
}
174+
175+
})(this);

‎package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
},
1616
"dependencies": {
1717
"mocha": "^5.2.0",
18-
"should": "^13.2.3",
19-
"text-encoding": "^0.7.0"
18+
"should": "^13.2.3"
2019
}
2120
}

‎src/xapiwrapper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function isDate(date) {
8383
}
8484

8585
(function (ADL) {
86-
86+
8787
log.debug = false;
8888

8989
function getByteLen(normal_val) {
@@ -1525,7 +1525,7 @@ function isDate(date) {
15251525
url += (url.indexOf("?") > -1 ? "&" : "?") + extended.join("&");
15261526
}
15271527
}
1528-
1528+
15291529
//If it's not cross domain or we're not using IE, use the usual XmlHttpRequest
15301530
var windowsVersionCheck = window.XDomainRequest && (window.XMLHttpRequest && new XMLHttpRequest().responseType === undefined);
15311531
if (!xDomainRequest || windowsVersionCheck === undefined || windowsVersionCheck===false) {

‎test/testAttachments.html

+15-29
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,35 @@
1111
</head>
1212

1313
<body>
14-
14+
<div id="console"></div>
15+
<div id="mocha"></div>
16+
17+
<script src="./libs/expect/0.3.1/index.js"></script>
18+
<script src="./libs/mocha/2.2.5/mocha.js"></script>
19+
<script src="./libs/should.min.js"></script>
1520

1621
<!-- <script src="../dist/xapiwrapper.min.js"></script>-->
1722
<script type="text/javascript" src="../lib/cryptojs_v3.1.2.js"></script>
23+
<script type="text/javascript" src="../lib/utf8-text-encoding.js"></script>
1824
<script type="text/javascript" src="../src/verbs.js"></script>
1925
<script type="text/javascript" src="../src/xapistatement.js"></script>
2026
<script type="text/javascript" src="../src/xapiwrapper.js"></script>
21-
22-
<script type="text/javascript" src="../examples/stmtBank.js"></script>
23-
24-
<script src="../src/xapi-util.js" data-cover></script>
2527

28+
<script>
29+
mocha.setup('bdd')
30+
</script>
31+
<script src="./tests/test.attachments.js"></script>
2632
<script>
2733
var conf = {
2834
"endpoint" : "https://lrs.adlnet.gov/xapi/",
29-
"user" : "tom",
35+
"user" : "iitsecDemo",
3036
"password" : "1234",
3137
};
3238
ADL.XAPIWrapper.changeConfig(conf);
3339

34-
var statement = {"actor":{"mbox":"mailto:tom@tom.com"}, "verb":{"id":"http://verb.com/do1"}, "object":{"id":"http://from.tom/act1", "objectType":"Activity", "definition":{"name":{"en-US": "soccer", "fr": "football", "de": "foossball"}}}};
35-
36-
37-
var attachmentMetadata = {
38-
"usageType": "http://adlnet.gov/expapi/attachments/asdf",
39-
"display":
40-
{
41-
"en-US": "asdfasdf"
42-
},
43-
"description":
44-
{
45-
"en-US": "asdfasdfasd"
46-
},
47-
"contentType": "application/octet-stream"
48-
}
49-
var attachment = {
50-
value: "this is a simple string attachment",
51-
type:attachmentMetadata
52-
}
53-
54-
55-
ADL.XAPIWrapper.sendStatement(statement,null,[attachment]);
56-
40+
mocha.checkLeaks();
41+
mocha.globals(['jQuery','grandTotalTemplate','blanket_toggleSource']);
42+
mocha.run();
5743
</script>
5844
</body>
5945

‎test/tests/test.attachments.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
describe('testing xAPI attachments', function () {
2+
3+
var onBrowser, should;
4+
5+
before(function () {
6+
7+
onBrowser = typeof window !== 'undefined' ? true : false;
8+
if (!onBrowser) {
9+
should = require('should');
10+
}
11+
12+
});
13+
14+
it('should be able to send a simple string attachment', function (done) {
15+
16+
var conf = {
17+
"endpoint" : "https://lrs.adlnet.gov/xapi/",
18+
"user" : "tom",
19+
"password" : "1234",
20+
};
21+
ADL.XAPIWrapper.changeConfig(conf);
22+
23+
var statement = {"actor":{"mbox":"mailto:tom@tom.com"}, "verb":{"id":"http://verb.com/do1"}, "object":{"id":"http://from.tom/act1", "objectType":"Activity", "definition":{"name":{"en-US": "soccer", "fr": "football", "de": "foossball"}}}};
24+
25+
26+
var attachmentMetadata = {
27+
"usageType": "http://adlnet.gov/expapi/attachments/asdf",
28+
"display":
29+
{
30+
"en-US": "asdfasdf"
31+
},
32+
"description":
33+
{
34+
"en-US": "asdfasdfasd"
35+
},
36+
"contentType": "application/octet-stream"
37+
}
38+
var attachment = {
39+
value: "this is a simple string attachment",
40+
type:attachmentMetadata
41+
}
42+
43+
44+
ADL.XAPIWrapper.sendStatement(statement, function () {
45+
done();
46+
}, [attachment]);
47+
48+
});
49+
50+
});

0 commit comments

Comments
 (0)
Please sign in to comment.