UNPKG

oidc-lib

Version:

A library for creating OIDC Service Providers

124 lines (111 loc) 3.25 kB
e// abandoned until further study since github will not allow download of files greater than 1M // the data api blob mechanism can be used but the SHA1 must be known as calculated by github (strange mechanism) const GITHUB_ENDPOINT = 'https://api.github.com/repos/kimcameron/oidc-example-imports/contents'; const request_module = require('request'); const path = require('path'); const fs = require('fs'); startup(); async function startup(){ var import_file = 'README.md'; var content = await get_example(import_file); } async function get_example(import_file){ var importPath = path.join('../../oidc-example-imports', import_file); var content = fs.readFileSync(importPath); console.log('Content: ', content); } async function get_example_(import_file){ var options = { headers: [ {name: 'Accept', value: 'application/vnd.github.v3+json'}, {name: 'User-Agent', value: 'kimcameron/oidc-example-imports'} ], url: GITHUB_ENDPOINT + '/' + import_file } var content = await jsonHttpData(options); } function jsonHttpData(options){ return new Promise((resolve, reject) => { if (typeof window === 'undefined'){ // url var rOptions = { url: options.url }; // method var method = options.method; if (method === undefined){ method = 'GET'; } rOptions.method = method.toUpperCase(); // headers if (options.headers !== undefined){ var headers = {}; for (var i=0; i < options.headers.length; i++){ var header = options.headers[i]; headers[header.name] = header.value; } rOptions.headers = headers; } var postData = options.postData; if (postData){ if (typeof postData !== 'string'){ postData = JSON.stringify(postData); } rOptions.body = postData; } var request = request_module; request(rOptions, function (error, response, body) { if (error || !body){ var message; if (error && error.message){ message = error.message; } reject(message); return; } resolve(body); }); } else{ var xhr = new XMLHttpRequest(); var method = options.method; if (method === undefined){ method = 'GET'; } xhr.open(method, options.url, true); var authorizationHeaderPresent = false; if (options.headers !== undefined){ for (var i=0; i < options.headers.length; i++){ var header = options.headers[i]; if (header.name === 'Authorization'){ authorizationHeaderPresent = true; } xhr.setRequestHeader(header.name, header.value); } } xhr.onreadystatechange = function () { if (xhr.readyState === 4){ if (xhr.status === 200) { resolve(xhr.responseText); } else { reject(xhr.responseText); } } }; if (authorizationHeaderPresent){ xhr.withCredentials = true; } if (method.toUpperCase() === 'GET' || options.postData === undefined){ xhr.send(); } else{ var postData = options.postData; if (typeof postData !== 'string'){ postData = JSON.stringify(postData); } xhr.send(postData); } } }); }