npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nativescript-https-thelonecabbage

v2.2.0

Published

Secure HTTP client with SSL pinning for Nativescript - iOS/Android.

Downloads

17

Readme

NativeScript-HTTPS

NPM version Downloads TotalDownloads Twitter Follow

The definitive way to hit HTTP based APIs in Nativescript.

Easily integrate the most reliable native networking libraries with the latest and greatest HTTPS security features.

Plugin version 2.0.0 bumps AFNetworking on iOS to 4.0.0 which no longer relies on UIWebView. Make sure to run pod repo update to get the latest AFNetworking pod on your development machine.

A drop-in replacement for the default http module.

Features

  • Modern TLS & SSL security features
  • Shared connection pooling reduces request latency
  • Silently recovers from common connection problems
  • Everything runs on a native background thread
  • Transparent GZIP
  • HTTP/2 support
  • Multiform part
  • Cache
  • Basic Cookie support

FAQ

What the flip is SSL pinning and all this security mumbo jumbo?

How to make your apps more secure with SSL pinning.

Do I have to use SSL pinning?

No. This plugin works out of the box without any security configurations needed. Either way you'll still benefit from all the features listed above.

Demo

git clone https://github.com/EddyVerbruggen/nativescript-https
cd nativescript-https/src
npm run demo.ios
npm run demo.android

Installation

Add tns-platform-declarations for Android and iOS to your references.d.ts!

/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />

We also recommend adding "skipLibCheck": true, to your tsconfig.json. More information on that can be found here.

Install the plugin:

tns plugin add nativescript-https

Examples

Hitting an API using GET method

import * as Https from 'nativescript-https'
Https.request({
	url: 'https://httpbin.org/get',
	method: 'GET',
	timeout: 30 // seconds (default 10)
}).then(function(response) {
	console.log('Https.request response', response)
}).catch(function(error) {
	console.error('Https.request error', error)
})

Configuration

Installing your SSL certificate

Create a folder called assets in your projects app folder like so <project>/app/assets. Using chrome, go to the URL where the SSL certificate resides. View the details then drag and drop the certificate image into the assets folder.

Installing your SSL certificate

Enabling SSL pinning

import { knownFolders } from 'file-system'
import * as Https from 'nativescript-https'
let dir = knownFolders.currentApp().getFolder('assets')
let certificate = dir.getFile('httpbin.org.cer').path
Https.enableSSLPinning({ host: 'httpbin.org', certificate })

Once you've enabled SSL pinning you CAN NOT re-enable with a different host or certificate file.

Disabling SSL pinning

import * as Https from 'nativescript-https'
Https.disableSSLPinning()

All requests after calling this method will no longer utilize SSL pinning until it is re-enabled once again.

useLegacy

There is a new option called useLegacy. You can set of every request options. When using that option the request will behave more like {N} http module.

  • the content returned by a request is not the resulting string but an object. It follows HTTPContent format for the most part. You can call toJSON or toFile. The only difference is that toFile returns a Promise<File> which means that it is async and run in a background thread!
  • an error return a content too allowing you to read its content.

Cookie

By default basic Cookie support is enabled to work like in {N} http module. In the future more options will be added

Enabling Cache

import { knownFolders, path } from '@nativescript/core/file-system';
import * as Https from 'nativescript-https'
Https.setCache({
    diskLocation: path.join(knownFolders.documents().path, 'httpcache'),
    diskSize: 10 * 1024 * 1024 // 10 MiB
});

/// later on when calling your request you can use the cachePolicy option

Multipart form data

If you set the Content-Type header to "multipart/form-data" the request body will be evaluated as a multipart form data. Each body parameter is expected to be in this format:

{
	data: any
    parameterName: string,
    fileName?: string
    contentType?: string
}

if fileName and contentType are set then data is expected to be either a NSData on iOS or a native.Array<number> on Android.

Options

export interface HttpsSSLPinningOptions {
	host: string
	certificate: string
	allowInvalidCertificates?: boolean
	validatesDomainName?: boolean
	commonName?: string
}
import { HttpRequestOptions } from 'tns-core-modules/http';
export interface HttpsRequestOptions extends HTTPOptions{
	useLegacy?: boolean
	cachePolicy?: 'noCache' | 'onlyCache' | 'ignoreCache'
	onProgress?: (current: number, total: number) => void
}

SSLPinning Option | Description ------------ | ------------- host: string | This must be the request domain name eg sales.company.org. commonName?: string | Default: options.host, set if certificate CN is different from the host eg *.company.org (Android specific) certificate: string | The uri path to your .cer certificate file. allowInvalidCertificates?: boolean | Default: false. This should always be false if you are using SSL pinning. Set this to true if you're using a self-signed certificate. validatesDomainName?: boolean | Default: true. Determines if the domain name should be validated with your pinned certificate.

Requests Option | Description ------------ | ------------- useLegacy?: boolean | Default: false. [IOS only] set to true in order to get the response data (when status >= 300)in the content directly instead of response.body.content. cachePolicy?: 'noCache' | 'onlyCache' | 'ignoreCache' | Set the cache policy to use with that request. This only works with GET requests for now. onProgress?: (current: number, total: number) => void | [IOS only] Set the progress callback.

Webpack / bundling

Since you're probably shipping a certificate with your app (like our demo does), make sure it's bundled by Webpack as well. You can do this by adding the certificate(s) with the CopyWebpackPlugin.

iOS Troubleshooting

Please educate yourself on iOS's App Transport Security before starting beef!

If you try and hit an https route without adding it to App Transport Security's whitelist it will not work! You can bypass this behavior by adding the following to your projects Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

This plugin does not add NSAllowsArbitraryLoads to your projects Info.plist for you.

Android troubleshooting

If you app crashes with a message that it's doing too much networkin on the main thread, then pass the option allowLargeResponse with value true to the request function.

Thanks

Who | Why ------------ | ------------- Robert Laverty | For creating and maintaining this plugin for a long time, before transfering it to me, with the help of Jeff Whelpley of GetHuman. AFNetworking | AFNetworking A delightful networking framework for iOS, OS X, watchOS, and tvOS. Square | okhttp An HTTP+HTTP/2 client for Android and Java applications.