chore(core): monorepo, esm targeting, improved management (#8707)

This commit is contained in:
Nathan Walker
2020-08-25 20:00:59 -07:00
committed by GitHub
parent 6f15334934
commit 020ad4da37
4271 changed files with 148599 additions and 149734 deletions

View File

@@ -0,0 +1 @@
Allows you to send web requests and receive the responses.

View File

@@ -0,0 +1,18 @@
import * as fsModule from '../../file-system';
export function getFilenameFromUrl(url: string) {
const fs: typeof fsModule = require('../../file-system');
const slashPos = url.lastIndexOf('/') + 1;
const questionMarkPos = url.lastIndexOf('?');
let actualFileName: string;
if (questionMarkPos !== -1) {
actualFileName = url.substring(slashPos, questionMarkPos);
} else {
actualFileName = url.substring(slashPos);
}
const result = fs.path.join(fs.knownFolders.documents().path, actualFileName);
return result;
}

View File

@@ -0,0 +1,263 @@
// imported for definition purposes only
import * as httpModule from '../../http';
import * as imageSourceModule from '../../image-source';
import { Screen } from '../../platform';
import * as fsModule from '../../file-system';
import { getFilenameFromUrl } from './http-request-common';
import { NetworkAgent } from '../../debugger';
export enum HttpResponseEncoding {
UTF8,
GBK,
}
function parseJSON(source: string): any {
const src = source.trim();
if (src.lastIndexOf(')') === src.length - 1) {
return JSON.parse(src.substring(src.indexOf('(') + 1, src.lastIndexOf(')')));
}
return JSON.parse(src);
}
let requestIdCounter = 0;
const pendingRequests = {};
let imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require('../../image-source');
}
}
let fs: typeof fsModule;
function ensureFileSystem() {
if (!fs) {
fs = require('../../file-system');
}
}
let completeCallback: org.nativescript.widgets.Async.CompleteCallback;
function ensureCompleteCallback() {
if (completeCallback) {
return;
}
completeCallback = new org.nativescript.widgets.Async.CompleteCallback({
onComplete: function (result: any, context: any) {
// as a context we will receive the id of the request
onRequestComplete(context, result);
},
onError: function (error: string, context: any) {
onRequestError(error, context);
},
});
}
function onRequestComplete(requestId: number, result: org.nativescript.widgets.Async.Http.RequestResult) {
const callbacks = pendingRequests[requestId];
delete pendingRequests[requestId];
if (result.error) {
callbacks.rejectCallback(new Error(result.error.toString()));
return;
}
// read the headers
const headers: httpModule.Headers = {};
if (result.headers) {
const jHeaders = result.headers;
const length = jHeaders.size();
let pair: org.nativescript.widgets.Async.Http.KeyValuePair;
for (let i = 0; i < length; i++) {
pair = jHeaders.get(i);
addHeader(headers, pair.key, pair.value);
}
}
// send response data (for requestId) to network debugger
if (global.__inspector && global.__inspector.isConnected) {
NetworkAgent.responseReceived(requestId, result, headers);
}
callbacks.resolveCallback({
content: {
raw: result.raw,
toArrayBuffer: () => Uint8Array.from(result.raw.toByteArray()).buffer,
toString: (encoding?: HttpResponseEncoding) => {
let str: string;
if (encoding) {
str = decodeResponse(result.raw, encoding);
} else {
str = result.responseAsString;
}
if (typeof str === 'string') {
return str;
} else {
throw new Error('Response content may not be converted to string');
}
},
toJSON: (encoding?: HttpResponseEncoding) => {
let str: string;
if (encoding) {
str = decodeResponse(result.raw, encoding);
} else {
str = result.responseAsString;
}
return parseJSON(str);
},
toImage: () => {
ensureImageSource();
return new Promise<any>((resolveImage, rejectImage) => {
if (result.responseAsImage != null) {
resolveImage(new imageSource.ImageSource(result.responseAsImage));
} else {
rejectImage(new Error('Response content may not be converted to an Image'));
}
});
},
toFile: (destinationFilePath: string) => {
ensureFileSystem();
if (!destinationFilePath) {
destinationFilePath = getFilenameFromUrl(callbacks.url);
}
let stream: java.io.FileOutputStream;
try {
// ensure destination path exists by creating any missing parent directories
const file = fs.File.fromPath(destinationFilePath);
const javaFile = new java.io.File(destinationFilePath);
stream = new java.io.FileOutputStream(javaFile);
stream.write(result.raw.toByteArray());
return file;
} catch (exception) {
throw new Error(`Cannot save file with path: ${destinationFilePath}.`);
} finally {
if (stream) {
stream.close();
}
}
},
},
statusCode: result.statusCode,
headers: headers,
});
}
function onRequestError(error: string, requestId: number) {
const callbacks = pendingRequests[requestId];
delete pendingRequests[requestId];
if (callbacks) {
callbacks.rejectCallback(new Error(error));
}
}
function buildJavaOptions(options: httpModule.HttpRequestOptions) {
if (typeof options.url !== 'string') {
throw new Error('Http request must provide a valid url.');
}
const javaOptions = new org.nativescript.widgets.Async.Http.RequestOptions();
javaOptions.url = options.url;
if (typeof options.method === 'string') {
javaOptions.method = options.method;
}
if (typeof options.content === 'string' || options.content instanceof FormData) {
const nativeString = new java.lang.String(options.content.toString());
const nativeBytes = nativeString.getBytes('UTF-8');
const nativeBuffer = java.nio.ByteBuffer.wrap(nativeBytes);
javaOptions.content = nativeBuffer;
} else if (options.content instanceof ArrayBuffer) {
const typedArray = new Uint8Array(options.content as ArrayBuffer);
const nativeBuffer = java.nio.ByteBuffer.wrap(Array.from(typedArray));
javaOptions.content = nativeBuffer;
}
if (typeof options.timeout === 'number') {
javaOptions.timeout = options.timeout;
}
if (typeof options.dontFollowRedirects === 'boolean') {
javaOptions.dontFollowRedirects = options.dontFollowRedirects;
}
if (options.headers) {
const arrayList = new java.util.ArrayList<org.nativescript.widgets.Async.Http.KeyValuePair>();
const pair = org.nativescript.widgets.Async.Http.KeyValuePair;
for (let key in options.headers) {
arrayList.add(new pair(key, options.headers[key] + ''));
}
javaOptions.headers = arrayList;
}
// pass the maximum available image size to the request options in case we need a bitmap conversion
javaOptions.screenWidth = Screen.mainScreen.widthPixels;
javaOptions.screenHeight = Screen.mainScreen.heightPixels;
return javaOptions;
}
export function request(options: httpModule.HttpRequestOptions): Promise<httpModule.HttpResponse> {
if (options === undefined || options === null) {
// TODO: Shouldn't we throw an error here - defensive programming
return;
}
return new Promise<httpModule.HttpResponse>((resolve, reject) => {
try {
// initialize the options
const javaOptions = buildJavaOptions(options);
// send request data to network debugger
if (global.__inspector && global.__inspector.isConnected) {
NetworkAgent.requestWillBeSent(requestIdCounter, options);
}
// remember the callbacks so that we can use them when the CompleteCallback is called
const callbacks = {
url: options.url,
resolveCallback: resolve,
rejectCallback: reject,
};
pendingRequests[requestIdCounter] = callbacks;
ensureCompleteCallback();
//make the actual async call
org.nativescript.widgets.Async.Http.MakeRequest(javaOptions, completeCallback, new java.lang.Integer(requestIdCounter));
// increment the id counter
requestIdCounter++;
} catch (ex) {
reject(ex);
}
});
}
function decodeResponse(raw: any, encoding?: HttpResponseEncoding) {
let charsetName = 'UTF-8';
if (encoding === HttpResponseEncoding.GBK) {
charsetName = 'GBK';
}
return raw.toString(charsetName);
}
export function addHeader(headers: httpModule.Headers, key: string, value: string): void {
if (!headers[key]) {
headers[key] = value;
} else if (Array.isArray(headers[key])) {
(<string[]>headers[key]).push(value);
} else {
const values: string[] = [<string>headers[key]];
values.push(value);
headers[key] = values;
}
}

View File

@@ -0,0 +1,3 @@
import { HttpRequestOptions, HttpResponse, Headers } from '..';
export const request: (options: HttpRequestOptions) => Promise<HttpResponse>;
export function addHeader(headers: Headers, key: string, value: string): void;

View File

@@ -0,0 +1,237 @@
// imported for definition purposes only
import * as httpModule from '../../http';
import * as imageSourceModule from '../../image-source';
import * as fsModule from '../../file-system';
import * as types from '../../utils/types';
import * as domainDebugger from '../../debugger';
import { getFilenameFromUrl } from './http-request-common';
export enum HttpResponseEncoding {
UTF8,
GBK,
}
const currentDevice = UIDevice.currentDevice;
const device = currentDevice.userInterfaceIdiom === UIUserInterfaceIdiom.Phone ? 'Phone' : 'Pad';
const osVersion = currentDevice.systemVersion;
const GET = 'GET';
const USER_AGENT_HEADER = 'User-Agent';
const USER_AGENT = `Mozilla/5.0 (i${device}; CPU OS ${osVersion.replace('.', '_')} like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/${osVersion} Mobile/10A5355d Safari/8536.25`;
const sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration;
const queue = NSOperationQueue.mainQueue;
function parseJSON(source: string): any {
const src = source.trim();
if (src.lastIndexOf(')') === src.length - 1) {
return JSON.parse(src.substring(src.indexOf('(') + 1, src.lastIndexOf(')')));
}
return JSON.parse(src);
}
@NativeClass
class NSURLSessionTaskDelegateImpl extends NSObject implements NSURLSessionTaskDelegate {
public static ObjCProtocols = [NSURLSessionTaskDelegate];
public URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session: NSURLSession, task: NSURLSessionTask, response: NSHTTPURLResponse, request: NSURLRequest, completionHandler: (p1: NSURLRequest) => void): void {
completionHandler(null);
}
}
const sessionTaskDelegateInstance: NSURLSessionTaskDelegateImpl = <NSURLSessionTaskDelegateImpl>NSURLSessionTaskDelegateImpl.new();
let defaultSession;
function ensureDefaultSession() {
if (!defaultSession) {
defaultSession = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
}
}
let sessionNotFollowingRedirects;
function ensureSessionNotFollowingRedirects() {
if (!sessionNotFollowingRedirects) {
sessionNotFollowingRedirects = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, sessionTaskDelegateInstance, queue);
}
}
let imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require('../../image-source');
}
}
let fs: typeof fsModule;
function ensureFileSystem() {
if (!fs) {
fs = require('../../file-system');
}
}
export function request(options: httpModule.HttpRequestOptions): Promise<httpModule.HttpResponse> {
return new Promise<httpModule.HttpResponse>((resolve, reject) => {
if (!options.url) {
reject(new Error('Request url was empty.'));
return;
}
try {
const network = domainDebugger.getNetwork();
const debugRequest = network && network.create();
const urlRequest = NSMutableURLRequest.requestWithURL(NSURL.URLWithString(options.url));
urlRequest.HTTPMethod = types.isDefined(options.method) ? options.method : GET;
urlRequest.setValueForHTTPHeaderField(USER_AGENT, USER_AGENT_HEADER);
if (options.headers) {
for (let header in options.headers) {
urlRequest.setValueForHTTPHeaderField(options.headers[header] + '', header);
}
}
if (types.isString(options.content) || options.content instanceof FormData) {
urlRequest.HTTPBody = NSString.stringWithString(options.content.toString()).dataUsingEncoding(4);
} else if (options.content instanceof ArrayBuffer) {
const buffer = options.content as ArrayBuffer;
urlRequest.HTTPBody = NSData.dataWithData(buffer as any);
}
if (types.isNumber(options.timeout)) {
urlRequest.timeoutInterval = options.timeout / 1000;
}
let session;
if (types.isBoolean(options.dontFollowRedirects) && options.dontFollowRedirects) {
ensureSessionNotFollowingRedirects();
session = sessionNotFollowingRedirects;
} else {
ensureDefaultSession();
session = defaultSession;
}
const dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest, function (data: NSData, response: NSHTTPURLResponse, error: NSError) {
if (error) {
reject(new Error(error.localizedDescription));
} else {
const headers: httpModule.Headers = {};
if (response && response.allHeaderFields) {
const headerFields = response.allHeaderFields;
headerFields.enumerateKeysAndObjectsUsingBlock((key, value, stop) => {
addHeader(headers, key, value);
});
}
if (debugRequest) {
debugRequest.mimeType = response.MIMEType;
debugRequest.data = data;
const debugResponse = {
url: options.url,
status: response.statusCode,
statusText: NSHTTPURLResponse.localizedStringForStatusCode(response.statusCode),
headers: headers,
mimeType: response.MIMEType,
fromDiskCache: false,
};
debugRequest.responseReceived(debugResponse);
debugRequest.loadingFinished();
}
resolve({
content: {
raw: data,
toArrayBuffer: () => interop.bufferFromData(data),
toString: (encoding?: any) => {
const str = NSDataToString(data, encoding);
if (typeof str === 'string') {
return str;
} else {
throw new Error('Response content may not be converted to string');
}
},
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
toImage: () => {
ensureImageSource();
return new Promise((resolve, reject) => {
(<any>UIImage).tns_decodeImageWithDataCompletion(data, (image) => {
if (image) {
resolve(new imageSource.ImageSource(image));
} else {
reject(new Error('Response content may not be converted to an Image'));
}
});
});
},
toFile: (destinationFilePath?: string) => {
ensureFileSystem();
if (!destinationFilePath) {
destinationFilePath = getFilenameFromUrl(options.url);
}
if (data instanceof NSData) {
// ensure destination path exists by creating any missing parent directories
const file = fs.File.fromPath(destinationFilePath);
data.writeToFileAtomically(destinationFilePath, true);
return file;
} else {
reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
}
},
},
statusCode: response.statusCode,
headers: headers,
});
}
});
if (options.url && debugRequest) {
const request = {
url: options.url,
method: 'GET',
headers: options.headers,
};
debugRequest.requestWillBeSent(request);
}
dataTask.resume();
} catch (ex) {
reject(ex);
}
});
}
function NSDataToString(data: any, encoding?: HttpResponseEncoding): string {
let code = NSUTF8StringEncoding; // long:4
if (encoding === HttpResponseEncoding.GBK) {
code = CFStringEncodings.kCFStringEncodingGB_18030_2000; // long:1586
}
let encodedString = NSString.alloc().initWithDataEncoding(data, code);
// If UTF8 string encoding fails try with ISO-8859-1
if (!encodedString) {
code = NSISOLatin1StringEncoding; // long:5
encodedString = NSString.alloc().initWithDataEncoding(data, code);
}
return encodedString.toString();
}
export function addHeader(headers: httpModule.Headers, key: string, value: string): void {
if (!headers[key]) {
headers[key] = value;
} else if (Array.isArray(headers[key])) {
(<string[]>headers[key]).push(value);
} else {
const values: string[] = [<string>headers[key]];
values.push(value);
headers[key] = values;
}
}

166
packages/core/http/index.d.ts vendored Normal file
View File

@@ -0,0 +1,166 @@
import { ImageSource } from '../image-source';
import { File } from '../file-system';
/**
* Downloads the content from the specified URL as a string.
* @param url The URL to request from.
*/
export function getString(url: string): Promise<string>;
/**
* Downloads the content from the specified URL as a string.
* @param options An object that specifies various request options.
*/
export function getString(options: HttpRequestOptions): Promise<string>;
/**
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
* @param url The URL to request from.
*/
export function getJSON<T>(url: string): Promise<T>;
/**
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
* @param options An object that specifies various request options.
*/
export function getJSON<T>(options: HttpRequestOptions): Promise<T>;
/**
* Downloads the content from the specified URL and attempts to decode it as an image.
* @param url The URL to request from.
*/
export function getImage(url: string): Promise<ImageSource>;
/**
* Downloads the content from the specified URL and attempts to decode it as an image.
* @param options An object that specifies various request options.
*/
export function getImage(options: HttpRequestOptions): Promise<ImageSource>;
/**
* Downloads the content from the specified URL and attempts to save it as file.
* @param url The URL to request from.
* @param destinationFilePath Optional. The downloaded file path.
*/
export function getFile(url: string, destinationFilePath?: string): Promise<File>;
/**
* Downloads the content from the specified URL and attempts to save it as file.
* @param options An object that specifies various request options.
* @param destinationFilePath Optional. The downloaded file path.
*/
export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise<File>;
/**
* Downloads the content from the specified URL as binary and returns an ArrayBuffer.
* @param url The URL to request from.
*/
export function getBinary(url: string): Promise<ArrayBuffer>;
/**
* Downloads the content from the specified URL as binary and returns an ArrayBuffer.
* @param options An object that specifies various request options.
*/
export function getBinary(options: HttpRequestOptions): Promise<ArrayBuffer>;
/**
* Makes a generic http request using the provided options and returns a HttpResponse Object.
* @param options An object that specifies various request options.
*/
export function request(options: HttpRequestOptions): Promise<HttpResponse>;
/**
* Provides options for the http requests.
*/
export interface HttpRequestOptions {
/**
* Gets or sets the request url.
*/
url: string;
/**
* Gets or sets the request method.
*/
method: string;
/**
* Gets or sets the request headers in JSON format.
*/
headers?: any;
/**
* Gets or sets the request body.
*/
content?: string | FormData | ArrayBuffer;
/**
* Gets or sets the request timeout in milliseconds.
*/
timeout?: number;
/**
* Gets or sets wether to *not* follow server's redirection responses.
*/
dontFollowRedirects?: boolean;
}
/**
* Encapsulates HTTP-response information from an HTTP-request.
*/
export interface HttpResponse {
/**
* Gets the response status code.
*/
statusCode: number;
/**
* Gets the response headers.
*/
headers: Headers;
/**
* Gets the response content.
*/
content?: HttpContent;
}
export type Headers = { [key: string]: string | string[] };
export enum HttpResponseEncoding {
UTF8,
GBK,
}
/**
* Encapsulates the content of an HttpResponse.
*/
export interface HttpContent {
/**
* Gets the response body as raw data.
*/
raw: any;
/**
* Gets the response body as ArrayBuffer
*/
toArrayBuffer: () => ArrayBuffer;
/**
* Gets the response body as string.
*/
toString: (encoding?: HttpResponseEncoding) => string;
/**
* Gets the response body as JSON object.
*/
toJSON: (encoding?: HttpResponseEncoding) => any;
/**
* Gets the response body as ImageSource.
*/
toImage: () => Promise<ImageSource>;
/**
* Gets the response body as file.
*/
toFile: (destinationFilePath?: string) => File;
}

181
packages/core/http/index.ts Normal file
View File

@@ -0,0 +1,181 @@
import { ImageSource } from '../image-source';
import { File } from '../file-system';
import * as httpRequest from './http-request';
export * from './http-request';
/**
* Provides options for the http requests.
*/
export interface HttpRequestOptions {
/**
* Gets or sets the request url.
*/
url: string;
/**
* Gets or sets the request method.
*/
method: string;
/**
* Gets or sets the request headers in JSON format.
*/
headers?: any;
/**
* Gets or sets the request body.
*/
content?: string | FormData | ArrayBuffer;
/**
* Gets or sets the request timeout in milliseconds.
*/
timeout?: number;
/**
* Gets or sets whether to *not* follow server's redirection responses.
*/
dontFollowRedirects?: boolean;
}
/**
* Encapsulates HTTP-response information from an HTTP-request.
*/
export interface HttpResponse {
/**
* Gets the response status code.
*/
statusCode: number;
/**
* Gets the response headers.
*/
headers: Headers;
/**
* Gets the response content.
*/
content?: HttpContent;
}
export type Headers = { [key: string]: string | string[] };
export enum HttpResponseEncoding {
UTF8,
GBK,
}
/**
* Encapsulates the content of an HttpResponse.
*/
export interface HttpContent {
/**
* Gets the response body as raw data.
*/
raw: any;
/**
* Gets the response body as ArrayBuffer
*/
toArrayBuffer: () => ArrayBuffer;
/**
* Gets the response body as string.
*/
toString: (encoding?: HttpResponseEncoding) => string;
/**
* Gets the response body as JSON object.
*/
toJSON: (encoding?: HttpResponseEncoding) => any;
/**
* Gets the response body as ImageSource.
*/
toImage: () => Promise<ImageSource>;
/**
* Gets the response body as file.
*/
toFile: (destinationFilePath?: string) => File;
}
export function getString(arg: any): Promise<string> {
return new Promise<string>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const str = r.content.toString();
resolve(str);
} catch (e) {
reject(e);
}
},
(e) => reject(e)
);
});
}
export function getJSON<T>(arg: any): Promise<T> {
return new Promise<T>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const json = r.content.toJSON();
resolve(json);
} catch (e) {
reject(e);
}
},
(e) => reject(e)
);
});
}
export function getImage(arg: any): Promise<ImageSource> {
return new Promise<any>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
resolve(r.content.toImage());
} catch (err) {
reject(err);
}
},
(err) => {
reject(err);
}
);
});
}
export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
return new Promise<any>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const file = r.content.toFile(destinationFilePath);
resolve(file);
} catch (e) {
reject(e);
}
},
(e) => reject(e)
);
});
}
export function getBinary(arg: any): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const arrayBuffer = r.content.toArrayBuffer();
resolve(arrayBuffer);
} catch (e) {
reject(e);
}
},
(e) => reject(e)
);
});
}