Skip to content

Commit

Permalink
fix(typescript): refactored how types are published
Browse files Browse the repository at this point in the history
resolves #119
  • Loading branch information
panva committed Nov 26, 2020
1 parent ac4aef8 commit 2937363
Show file tree
Hide file tree
Showing 32 changed files with 59 additions and 116 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
path: dist
key: dist-${{ hashFiles('src/**/*.ts') }}-${{ hashFiles('tsconfig/*.json') }}
- run: sed -i -e "s/-i ''/-i/g" package.json
- run: sed -i -e "s/gcp/cp/g" package.json
- name: Build
run: npm run build-all
if: ${{ steps.dist.outputs.cache-hit != 'true' }}
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@
"files": [
"dist/**/package.json",
"dist/**/*.js",
"src/**/*.d.ts",
"src/**/*.ts",
"!src/runtime/*/*.ts",
"!src/runtime/*.ts",
"src/runtime/*.d.ts"
"dist/types/**/*.d.ts",
"!dist/types/**/*.i.d.ts",
"!dist/types/runtime*",
"!dist/types/lib*",
"dist/types/lib/jwt_producer.d.ts"
],
"scripts": {
"build": "tsc",
Expand All @@ -281,6 +281,7 @@
"build-fast:node-webcrypto-cjs": "npm run-script runtime-node-webcrypto && npm run-script -s esbuild-find | xargs -0 esbuild --platform=node --target=esnext --outdir=dist/node/webcrypto/cjs --format=cjs",
"build-fast:node-webcrypto-esm": "npm run-script runtime-node-webcrypto && npm run-script -s esbuild-find | xargs -0 esbuild --platform=node --target=esnext --outdir=dist/node/webcrypto/esm --format=esm && echo '{\"type\": \"module\"}'> dist/node/webcrypto/esm/package.json",
"build:browser": "run-s runtime-browser lint 'build -- -p ./tsconfig/browser.json' && echo '{\"type\": \"module\"}'> dist/browser/package.json",
"build:types": "npm run-script build -- -p ./tsconfig/types.json && cd src && find . -name '*.d.ts' -maxdepth 2 -type f -exec gcp --parents \"{}\" ../dist/types \\; && cd ..",
"build:node-cjs": "run-s runtime-node lint 'build -- -p ./tsconfig/node-cjs.json'",
"build:node-esm": "run-s runtime-node lint 'build -- -p ./tsconfig/node-esm.json' && echo '{\"type\": \"module\"}'> dist/node/esm/package.json",
"build:node-webcrypto-cjs": "run-s runtime-node-webcrypto lint 'build -- -p ./tsconfig/node-webcrypto-cjs.json'",
Expand Down Expand Up @@ -395,10 +396,10 @@
"typesVersions": {
"*": {
"webcrypto/*": [
"./src/*"
"./dist/types/*"
],
"*": [
"./src/*"
"./dist/types/*"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/jwk/embedded.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable jsdoc/require-param */
import type { KeyObject } from 'crypto'
import { FlattenedJWSInput, JWSHeaderParameters } from '../types.d'
import type { FlattenedJWSInput, JWSHeaderParameters } from '../types.d'
import parseJwk from './parse.js'
import isObject from '../lib/is_object.js'
import { JWSInvalid } from '../util/errors.js'
Expand Down
2 changes: 1 addition & 1 deletion src/jwk/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { decode as base64url } from '../runtime/base64url.js'
import asKeyObject from '../runtime/jwk_to_key.js'
import { JOSENotSupported } from '../util/errors.js'
import isObject from '../lib/is_object.js'
import type { JWK, KeyLike } from '../types.js'
import type { JWK, KeyLike } from '../types.d'

/**
* Converts a JWK to a runtime-specific key representation (KeyLike). Either
Expand Down
2 changes: 1 addition & 1 deletion src/jwt/encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-underscore-dangle */

import CompactEncrypt from '../jwe/compact/encrypt.js'
import {
import type {
EncryptOptions,
JWEHeaderParameters,
JWEKeyManagementHeaderParameters,
Expand Down
2 changes: 1 addition & 1 deletion src/jwt/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import CompactSign from '../jws/compact/sign.js'
import { JWTInvalid } from '../util/errors.js'
import { JWSHeaderParameters, JWTPayload, KeyLike } from '../types.d'
import type { JWSHeaderParameters, JWTPayload, KeyLike } from '../types.d'
import { encoder } from '../lib/buffer_utils.js'
import ProduceJWT from '../lib/jwt_producer.js'

Expand Down
2 changes: 1 addition & 1 deletion src/jwt/unsecured.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-underscore-dangle */

import { JWSHeaderParameters, JWTClaimVerificationOptions, JWTPayload } from '../types.d'
import type { JWSHeaderParameters, JWTClaimVerificationOptions, JWTPayload } from '../types.d'
import { decoder } from '../lib/buffer_utils.js'
import * as base64url from '../runtime/base64url.js'
import { JWTInvalid } from '../util/errors.js'
Expand Down
3 changes: 0 additions & 3 deletions src/runtime/aesgcmkw.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/aeskw.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/base64url.d.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/runtime/browser/base64url.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Base64UrlDecode, Base64UrlEncode } from '../interfaces.d'
import { encoder, decoder } from '../../lib/buffer_utils.js'
import globalThis from './global.js'

export const encode: Base64UrlEncode = (input) => {
export const encode = (input: Uint8Array | string) => {
let unencoded = input
if (typeof unencoded === 'string') {
unencoded = encoder.encode(unencoded)
Expand All @@ -11,7 +10,7 @@ export const encode: Base64UrlEncode = (input) => {
return base64string.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
}

export const decode: Base64UrlDecode = (input) => {
export const decode = (input: Uint8Array | string) => {
let encoded = input
if (encoded instanceof Uint8Array) {
encoded = decoder.decode(encoded)
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/browser/random.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { GetRandomValuesFunction } from '../interfaces.d'
import crypto from './webcrypto.js'

const random: GetRandomValuesFunction = crypto.getRandomValues.bind(crypto)
const random = crypto.getRandomValues.bind(crypto)

export default random
3 changes: 0 additions & 3 deletions src/runtime/decrypt.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/digest.d.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/runtime/ecdhes.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/encrypt.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/fetch.d.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/runtime/generate.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/runtime/interfaces.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import type { JWK, KeyLike } from '../types.d'
import type { EpkJwk, AsyncOrSync } from '../types.i.d'

/**
* Utility function to encode a string or Uint8Array as a base64url string.
*
* @param input Value that will be base64url-encoded.
*/
export interface Base64UrlEncode {
(input: Uint8Array | string): string
}
/**
* Utility function to decode a base64url encoded string.
*
* @param input Value that will be base64url-decoded.
*/
export interface Base64UrlDecode {
(input: Uint8Array | string): Uint8Array
}
export interface TimingSafeEqual {
(a: Uint8Array, b: Uint8Array): boolean
}
Expand All @@ -26,9 +10,6 @@ export interface SignFunction {
export interface VerifyFunction {
(alg: string, key: any, signature: Uint8Array, data: Uint8Array): Promise<boolean>
}
export interface GetRandomValuesFunction {
(array: Uint8Array): Uint8Array
}
export interface ExportCekFunction {
(key: any): AsyncOrSync<Uint8Array>
}
Expand Down
3 changes: 0 additions & 3 deletions src/runtime/jwk_to_key.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/key_to_jwk.d.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/runtime/node/base64url.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Base64UrlDecode, Base64UrlEncode } from '../interfaces.d'
import { decoder } from '../../lib/buffer_utils.js'

export const encode: Base64UrlEncode = (input) =>
export const encode = (input: Uint8Array | string) =>
Buffer.from(input).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')

export const decode: Base64UrlDecode = (input) => {
export const decode = (input: Uint8Array | string) => {
let encoded = input
if (encoded instanceof Uint8Array) {
encoded = decoder.decode(encoded)
Expand Down
5 changes: 1 addition & 4 deletions src/runtime/node/random.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { randomFillSync } from 'crypto'
import type { GetRandomValuesFunction } from '../interfaces.d'

const random: GetRandomValuesFunction = randomFillSync

export default random
export default randomFillSync
3 changes: 0 additions & 3 deletions src/runtime/pbes2kw.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/random.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/rsaes.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/sign.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/verify.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/runtime/zlib.d.ts

This file was deleted.

24 changes: 23 additions & 1 deletion src/util/base64url.ts
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
export * from '../runtime/base64url.js'
/* eslint-disable prefer-destructuring */

import * as base64url from '../runtime/base64url.js'

/**
* Utility function to encode a string or Uint8Array as a base64url string.
*
* @param input Value that will be base64url-encoded.
*/
interface Base64UrlEncode {
(input: Uint8Array | string): string
}
/**
* Utility function to decode a base64url encoded string.
*
* @param input Value that will be base64url-decoded.
*/
interface Base64UrlDecode {
(input: Uint8Array | string): Uint8Array
}

export const encode: Base64UrlEncode = base64url.encode
export const decode: Base64UrlDecode = base64url.decode
7 changes: 6 additions & 1 deletion src/util/random.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import random from '../runtime/random.js'
import runtimeRandom from '../runtime/random.js'

interface GetRandomValuesFunction {
(array: Uint8Array): Uint8Array
}

const random: GetRandomValuesFunction = runtimeRandom
export default random
10 changes: 10 additions & 0 deletions tsconfig/types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./base.json",
"compilerOptions": {
"target": "ESNext",
"outDir": "../dist/types",
"removeComments": false,
"declaration": true,
"emitDeclarationOnly": true
}
}

0 comments on commit 2937363

Please sign in to comment.