Skip to content

Commit

Permalink
Merge pull request #41 from digital-asset/webpack-5-fix
Browse files Browse the repository at this point in the history
Switch to jwt-decode
  • Loading branch information
alexmatson-da authored Mar 7, 2022
2 parents fd8b282 + 44ccf2b commit 5009ccd
Show file tree
Hide file tree
Showing 14 changed files with 9,420 additions and 592 deletions.
21 changes: 14 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2.1
orbs:
node: circleci/node@3.0.0
node: circleci/node@5.0.0

commands:
test_steps:
Expand All @@ -9,30 +9,37 @@ commands:
- node/install-packages:
pkg-manager: yarn
- run:
command: yarn install
name: Install dependencies
command: yarn install
- run:
name: Run unit tests (Jest)
command: yarn test
name: Run tests via yarn
- run:
command: yarn format-check
name: Check formatting
command: yarn format-check

build_steps:
steps:
- run:
name: Compile TS to JS
command: yarn build
- run:
name: Run build integration test
command: ./build_integration_test.sh
jobs:
test:
executor:
name: node/default
steps:
- test_steps
- build_steps

test_build_and_publish:
executor:
name: node/default
steps:
- test_steps
- run:
name: Compile TS to JS
command: yarn build
- build_steps
- run:
name: Authenticate with registry
command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
lib
node_modules
coverage
tests/
23 changes: 23 additions & 0 deletions build_integration_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# Integration test for @daml/hub-react when installed in
# a sample application built with webpack v5

set -o nounset
set -o errexit
set -o pipefail

echo "Establishing package link..."
yarn link

echo "Changing to webpack-5-test directory..."
cd tests/webpack-5-test

echo "Linking package..."
yarn link @daml/hub-react

echo "Running install..."
yarn install

echo "Running build..."
yarn build
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@daml/hub-react",
"version": "1.0.3",
"version": "1.1.0",
"description": "Daml React functions for Daml Hub",
"homepage": "https://hub.daml.com",
"keywords": [
Expand All @@ -16,10 +16,6 @@
"lib/**/*"
],
"license": "Apache-2.0",
"dependencies": {
"@mojotech/json-type-validation": "^3.1.0",
"jsonwebtoken": "^8.5.1"
},
"repository": "github:digital-asset/dabl-react",
"scripts": {
"build": "tsc --build",
Expand All @@ -29,6 +25,10 @@
"format-check": "prettier --check .",
"lint": "eslint --ext .ts --ignore-pattern lib/ --max-warnings 0 ./"
},
"dependencies": {
"@mojotech/json-type-validation": "^3.1.0",
"jwt-decode": "^3.1.2"
},
"devDependencies": {
"@testing-library/react-hooks": "^5.0.3",
"@trivago/prettier-plugin-sort-imports": "^2.0.4",
Expand All @@ -41,6 +41,7 @@
"eslint": "^6.8.0",
"jest": "^26.1.0",
"jest-fetch-mock": "^3.0.3",
"jsonwebtoken": "^8.5.1",
"prettier": "^2.3.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
Expand All @@ -56,13 +57,13 @@
"@types/react": "^16.9.20"
},
"jest": {
"testEnvironment": "jsdom",
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"testPathIgnorePatterns": [
"/lib/"
"/lib/",
"/tests/webpack-5-test/"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest",
Expand Down
13 changes: 6 additions & 7 deletions src/party-token/PartyToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decode } from 'jsonwebtoken';
import decodeJwt from 'jwt-decode';

import { array, Decoder, number, object, string } from '@mojotech/json-type-validation';

Expand Down Expand Up @@ -40,13 +40,12 @@ export class PartyToken {
constructor(token: string) {
this.token = token;

const decoded = partyTokenDecoder.run(decode(token));

if (decoded.ok) {
this.payload = decoded.result;
} else {
try {
const decoded = partyTokenDecoder.runWithException(decodeJwt(token));
this.payload = decoded;
} catch (err) {
throw new Error(`Access token not in Daml Hub format: ${token}.\n
\t${decoded.error.message}`);
\t${err}`);
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/webpack-5-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
34 changes: 34 additions & 0 deletions tests/webpack-5-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "webpack-5-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@daml/hub-react": "file:../../",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
43 changes: 43 additions & 0 deletions tests/webpack-5-test/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
33 changes: 33 additions & 0 deletions tests/webpack-5-test/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import DamlHub, { DamlHubLogin } from '@daml/hub-react';

function App() {
return (
<div className="App">
<header className="App-header">
<DamlHubLogin onLogin={(creds, error) => {
if (!creds) {
console.log("Something went wrong: ", error);
} else {
console.log("User credentials: ", creds);
}
}} />

<DamlHub>
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</DamlHub>
</header>
</div>
);
}

export default App;
16 changes: 16 additions & 0 deletions tests/webpack-5-test/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
13 changes: 13 additions & 0 deletions tests/webpack-5-test/src/reportWebVitals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const reportWebVitals = onPerfEntry => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry);
getFID(onPerfEntry);
getFCP(onPerfEntry);
getLCP(onPerfEntry);
getTTFB(onPerfEntry);
});
}
};

export default reportWebVitals;
5 changes: 5 additions & 0 deletions tests/webpack-5-test/src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
Loading

0 comments on commit 5009ccd

Please sign in to comment.