Skip to content

Commit

Permalink
Merge pull request #38 from xapijs/issue-http-compatibility
Browse files Browse the repository at this point in the history
Issue http compatibility
  • Loading branch information
CookieCookson authored Aug 26, 2020
2 parents 4a21837 + e213f98 commit ee1c1e6
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 102 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/dist
/example
13 changes: 13 additions & 0 deletions example/require-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const XAPI = require("../dist/XAPI.cjs.js");

require("dotenv").config();

const endpoint = process.env.LRS_ENDPOINT || "";
const username = process.env.LRS_USERNAME || "";
const password = process.env.LRS_PASSWORD || "";
const auth = `Basic ${Buffer.from(username + ":" + password, "binary").toString("base64")}`;
const xapi = new XAPI(endpoint, auth);

xapi.getAbout().then((about) => {
console.log(about);
});
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require("dotenv").config();

module.exports = {
preset: "ts-jest",
setupFiles: ["./jest.setup.js"]
preset: "ts-jest"
};
1 change: 0 additions & 1 deletion jest.setup.js

This file was deleted.

68 changes: 43 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "@xapi/xapi",
"version": "0.2.1",
"description": "Communicate over xAPI using JavaScript.",
"main": "dist/XAPI.js",
"main": "dist/XAPI.cjs.js",
"module": "dist/XAPI.esm.js",
"browser": "dist/XAPI.umd.js",
"typings": "dist/types/XAPI.d.ts",
"files": [
"dist/**/*"
Expand Down Expand Up @@ -37,24 +38,25 @@
"@babel/preset-typescript": "^7.9.0",
"@rollup/plugin-babel": "^5.0.0",
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
"@types/jest": "^25.1.4",
"@types/node": "^13.11.1",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"babel-jest": "^25.2.4",
"babel-plugin-transform-class-properties": "^6.24.1",
"cross-fetch": "^3.0.4",
"crypto-js": "^4.0.0",
"dotenv": "^8.2.0",
"eslint": "^6.8.0",
"jest": "^25.2.4",
"rimraf": "^3.0.2",
"rollup": "^2.7.5",
"rollup": "^2.23.0",
"ts-jest": "^25.3.0",
"typescript": "^3.7.5"
},
"dependencies": {
"axios": "^0.19.2",
"deepmerge": "^4.2.2"
}
}
52 changes: 41 additions & 11 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,64 @@
import babel from "@rollup/plugin-babel";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import pkg from "./package.json";

const input = "./src/XAPI.ts";

const extensions = [
".js",
".ts"
];

export default {
input: "./src/XAPI.ts",
const resolveOptions = {
extensions: extensions
};

const babelPluginOptions = {
babelHelpers: "bundled",
extensions: extensions
};

export default [{
input: input,
plugins: [
resolve({
extensions: extensions
...resolveOptions,
browser: true
}),
commonjs(),
babel({
babelHelpers: "bundled",
extensions: extensions
})
commonjs(), // Used for Axios import
babel(babelPluginOptions)
],
output: [
{
file: pkg.main,
file: pkg.browser,
format: "umd",
name: "XAPI"
},
{
file: pkg.module,
format: "es"
format: "esm",
exports: "default"
}
]
};
}, {
input: input,
plugins: [
resolve({
...resolveOptions,
browser: false
}),
commonjs(), // Used for Axios import
json(),
babel(babelPluginOptions)
],
external: ["http", "https", "url", "zlib", "stream", "assert", "tty", "util", "os"],
output: [
{
file: pkg.main,
format: "cjs",
exports: "default"
}
]
}];
24 changes: 20 additions & 4 deletions src/XAPI.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import XAPI, { Agent, Statement, Activity, Attachment } from "./XAPI";
import * as CryptoJS from "crypto-js";
import CryptoJS from "crypto-js";
import { TextEncoder } from "util";
import axios from "axios";

interface WordArray {
iv: string;
salt: string;
ciphertext: string;
key?: string;
toString(encoder?: Encoder): string;
}

interface Encoder {
parse(encodedMessage: string): any;
stringify(words: any): string;
}

const endpoint: string = process.env.LRS_ENDPOINT || "";
const username: string = process.env.LRS_USERNAME || "";
Expand Down Expand Up @@ -30,7 +44,7 @@ const testStatement: Statement = {
object: testActivity
};

function arrayBufferToWordArray(ab: ArrayBuffer): CryptoJS.WordArray {
function arrayBufferToWordArray(ab: ArrayBuffer): WordArray {
const i8a = new Uint8Array(ab);
const a = [];
for (let i = 0; i < i8a.length; i += 4) {
Expand All @@ -56,8 +70,10 @@ describe("statement resource", () => {
test("can create a statement with a remote attachment", () => {
const statement: Statement = Object.assign({}, testStatement);
const imageURL: string = "https://raw.githubusercontent.com/RusticiSoftware/TinCanJS/8733f14ddcaeea77a0579505300bc8f38921a6b1/test/files/image.jpg";
return fetch(imageURL).then((image) => {
return image.arrayBuffer();
return axios.get(imageURL, {
responseType: "arraybuffer"
}).then((response) => {
return response.data as ArrayBuffer;
}).then((imageAsArrayBuffer) => {
const attachment: Attachment = {
usageType: XAPI.AttachmentUsages.SUPPORTING_MEDIA,
Expand Down
Loading

0 comments on commit ee1c1e6

Please sign in to comment.