Skip to content

Commit

Permalink
feat(bundle): Added support for running without bundler
Browse files Browse the repository at this point in the history
  • Loading branch information
grantila committed Jul 15, 2019
1 parent fe9c6ed commit 113f940
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
generated-*
dist
dist-bundle
bundle.js
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ U2F API for browsers

## History

- 1.1.0
- Can be used [without bundler](#using-without-bundler)
- 1.0.0
- Support for custom promise libraries removed
- Promises no longer cancellable
Expand Down Expand Up @@ -103,6 +105,16 @@ var u2fApi = require( 'u2f-api' ); // CommonJS
import u2fApi from 'u2f-api' // ES modules
```

#### Using without bundler

`u2f-api` can be used without a bundler (like Webpack). Just include:

```html
<head><script src="https://cdn.jsdelivr.net/npm/u2f-api@latest/bundle.js"></script></head>
```

The functionality will be in the `window.u2fApi` object.


### Registering a passkey

Expand Down
1 change: 1 addition & 0 deletions external.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "jsdom";
11 changes: 11 additions & 0 deletions google/modules.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- generated-google-u2f-api.js 2019-07-15 16:08:47.000000000 +0200
+++ generated-google-u2f-api.js 2019-07-15 16:08:52.000000000 +0200
@@ -16,7 +16,7 @@
*/
var u2f = u2f || {};

-module.exports.chromeApi = u2f; // Adaptation for u2f-api package
+export const chromeApi = u2f; // Adaptation for u2f-api package

/**
* FIDO U2F Javascript API Version
11 changes: 10 additions & 1 deletion google/this.patch
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
--- google-u2f-api.js 2018-03-20 12:07:45.000000000 +0100
+++ generated-google-u2f-api.js 2018-03-20 13:38:17.000000000 +0100
@@ -7,7 +7,7 @@
/**
* @fileoverview The U2F api.
*/
-'use strict';
+// 'use strict';


/**
@@ -16,6 +16,8 @@
*/
var u2f = u2f || {};

+module.exports = u2f; // Adaptation for u2f-api package
+module.exports.chromeApi = u2f; // Adaptation for u2f-api package
+
/**
* FIDO U2F Javascript API Version
Expand Down
5 changes: 5 additions & 0 deletions index.bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

import * as u2fApi from './lib/u2f-api'

( < any >window ).u2fApi = u2fApi;
25 changes: 16 additions & 9 deletions lib/u2f-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import * as chromeApi from './generated-google-u2f-api'
// @ts-ignore
import { chromeApi } from './generated-google-u2f-api'


// Feature detection (yes really)
Expand Down Expand Up @@ -40,6 +41,12 @@ export interface SignResponse {
signatureData: string;
}

interface BackendError {
errorCode: keyof typeof ErrorCodes;
}

type OrError< T > = T & BackendError;

export type Transport = 'bt' | 'ble' | 'nfc' | 'usb';
export type Transports = Array< Transport >;

Expand All @@ -50,7 +57,7 @@ export interface RegisteredKey {
appId: string;
}

var _backend: Promise< API > = null;
var _backend: Promise< API > | null = null;
function getBackend( )
{
if ( _backend )
Expand Down Expand Up @@ -93,7 +100,7 @@ function getBackend( )
return notSupported( );

// Test for google extension support
chromeApi.isSupported( function( ok )
chromeApi.isSupported( function( ok: any )
{
if ( ok )
resolve( { u2f: chromeApi } );
Expand Down Expand Up @@ -128,10 +135,10 @@ export const ErrorNames = {
"5": "TIMEOUT"
};

function makeError( msg, err )
function makeError( msg: string, err: BackendError )
{
const code = err != null ? err.errorCode : 1; // Default to OTHER_ERROR
const type = ErrorNames[ '' + code ];
const type = ErrorNames[ < keyof typeof ErrorNames >( '' + code ) ];
const error = new Error( msg );
( < any >error ).metaData = { type, code };
return error;
Expand All @@ -143,7 +150,7 @@ export function isSupported( )
.then( backend => !!backend.u2f );
}

function _ensureSupport( backend )
function _ensureSupport( backend: API )
{
if ( !backend.u2f )
{
Expand Down Expand Up @@ -196,7 +203,7 @@ export function register(
if ( typeof signRequests === 'number' && typeof timeout === 'undefined' )
{
timeout = signRequests;
signRequests = null;
signRequests = [ ];
}

const _signRequests = arrayify(
Expand All @@ -212,7 +219,7 @@ export function register(

return new Promise< RegisterResponse >( function( resolve, reject )
{
function callback( response )
function callback( response: OrError< RegisterResponse > )
{
if ( response.errorCode )
reject( makeError( "Registration failed", response ) );
Expand Down Expand Up @@ -248,7 +255,7 @@ export function sign(

return new Promise< SignResponse >( function( resolve, reject )
{
function callback( response )
function callback( response: OrError< SignResponse > )
{
if ( response.errorCode )
reject( makeError( "Sign failed", response ) );
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
"url": "https://github.com/grantila/u2f-api.git"
},
"files": [
"dist"
"dist",
"bundle.js"
],
"scripts": {
"build": "scripts/build.sh",
"build:lib": "scripts/build.sh",
"build:rollup": "node_modules/.bin/rimraf bundle.js && node_modules/.bin/rollup dist-bundle/index.bundle.js --file bundle.js --format iife",
"build": "yarn build:lib && yarn build:rollup",
"test": "node_modules/.bin/mocha dist/test",
"buildtest": "npm run build && npm run test",
"travis-deploy-once": "travis-deploy-once",
Expand All @@ -35,6 +38,7 @@
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "5.x",
"already": "1.x",
"chai": "4.x",
Expand All @@ -44,9 +48,11 @@
"mocha": "6.x",
"pre-commit": "1.x",
"rimraf": "2.x",
"rollup": "^1.17.0",
"semantic-release": "15.x",
"source-map-support": "0.x",
"travis-deploy-once": "5.x",
"ts-node": "^8.3.0",
"typescript": "3.x"
},
"config": {
Expand Down
8 changes: 6 additions & 2 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
patch google/google-u2f-api.js google/this.patch -o lib/generated-google-u2f-api.js

node_modules/.bin/rimraf dist

node_modules/.bin/tsc -p .

find test lib -name '*.js' | cpio -pdm dist/

patch lib/generated-google-u2f-api.js < google/modules.patch

node_modules/.bin/rimraf dist-bundle
node_modules/.bin/tsc -p tsconfig.bundle.json
find test lib -name '*.js' | cpio -pdm dist-bundle/
24 changes: 11 additions & 13 deletions test/u2f-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

declare var global: any;
declare var require: any;

Expand Down Expand Up @@ -41,8 +39,8 @@ type KeyValues = Array< KeyValue >;
class MonkeyPatcher
{
_object: any;
_values: Array< string >;
_overwrittenValues: KeyValues;
_values: Array< string > = [ ];
_overwrittenValues: KeyValues = [ ];

constructor( obj: any )
{
Expand Down Expand Up @@ -133,7 +131,7 @@ interface MockProps

function handleTimeout(
props: MockProps,
timeout,
timeout: number,
fn: ( ) => any
)
: Promise< any >
Expand Down Expand Up @@ -161,11 +159,11 @@ function u2fMock( props: MockProps = { } )

return {
sign(
appId,
challenge,
appId: string,
challenge: any,
registeredKeys: Array< u2fApi.RegisteredKey >,
cbNative,
timeout
cbNative: ( ) => void,
timeout: number
)
{
return handleTimeout( props, timeout, ( ) =>
Expand All @@ -186,11 +184,11 @@ function u2fMock( props: MockProps = { } )
.then( cbNative );
},
register(
appId,
appId: string,
registerRequests: Array< FakeRequest >,
signRequests: Array< FakeRequest >,
cbNative,
timeout
cbNative: ( ) => void,
timeout: number
)
{
return handleTimeout( props, timeout, ( ) =>
Expand Down Expand Up @@ -229,7 +227,7 @@ function wrappedTest(
{
const mock = ( props || { } ).mock || { };

dom.window.u2f = u2fMock( );
( < any >dom.window ).u2f = u2fMock( );
}

const gmp = new GlobalMonkeyPatcher( );
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.bundle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"lib": [ "es2015", "es2017", "dom" ],
"outDir": "dist-bundle",
"sourceMap": true,
"module": "es6",
"target": "es5"
},
"include": [
"external.d.ts",
"index.bundle.ts",
"src"
]
}
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"lib": [ "es2015", "es2017", "dom" ],
"outDir": "dist",
"sourceMap": true,
"strict": true,
"module": "CommonJS",
"target": "es5"
},
"include": [
"external.d.ts",
"index.ts",
"src",
"test"
Expand Down

0 comments on commit 113f940

Please sign in to comment.