From 97628646cbbdcbf461ff779469e966ae8003b417 Mon Sep 17 00:00:00 2001 From: Zwifi Date: Wed, 15 Sep 2021 08:55:05 +0200 Subject: [PATCH] This re-enables package import tests (#99) This re-enables package import tests. They had been disabled until the package was published on NPM, and mistakingly hadn't been re-enabled. * Fix umd module name to avoid conflicts * Work around rollup ts plugin ES issue: This prevents the bahaviour documented in https://github.com/ezolenko/rollup-plugin-typescript2/pull/170 from happening. Namely, a _virtual/tslib.js file would be emitted, which confuses NodeJS because we expect all ES modules to have the .mjs extension. The previous syntax feels more elegant though, so I think it should be re-enabled once the bug is resolved. Also, there's no guarantee that this issue can alway be worked around so easily. Issue tracking is available at https://github.com/ezolenko/rollup-plugin-typescript2/issues/282. * Disable defaulting to solid-client-authn-browser: Trying to import the package in an environment where it's not available causes Webpack to fail. Until a proper fix is found, this works around the issue by simply not trying to auto-import the @inrupt/solid-client-authn package. --- .github/workflows/cd.yml | 24 ++++----------- CHANGELOG.md | 5 ++++ rollup.config.js | 4 +-- src/fetcher.ts | 59 +++++++++++++------------------------ src/issue/issue.ts | 63 ++++++++++++++++++++++++++++------------ src/lookup/derive.ts | 8 ++++- 6 files changed, 84 insertions(+), 79 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index dcdece0e..3fdc93b3 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -147,20 +147,18 @@ jobs: run: | cd .github/workflows/cd-packaging-tests/node npm install @inrupt/solid-client-vc@$VERSION_NR - if: false env: VERSION_NR: ${{ needs.publish-npm.outputs.version-nr }} - name: Verify that the package can be imported in Node from a CommonJS module run: | cd .github/workflows/cd-packaging-tests/node node --unhandled-rejections=strict commonjs.cjs - if: false - name: Verify that the package can be imported in Node from an ES module run: | cd .github/workflows/cd-packaging-tests/node node --unhandled-rejections=strict esmodule.mjs # Node 10 does not support ES modules: - if: matrix.node-version != '10.x' && false + if: matrix.node-version != '10.x' verify-imports-parcel: runs-on: ubuntu-20.04 @@ -179,7 +177,6 @@ jobs: npx parcel@1.12.3 build index.ts env: VERSION_NR: ${{ needs.publish-npm.outputs.version-nr }} - if: false - name: Archive Parcel build artifacts uses: actions/upload-artifact@v2.2.2 @@ -205,7 +202,6 @@ jobs: npx webpack --devtool source-map env: VERSION_NR: ${{ needs.publish-npm.outputs.version-nr }} - if: false - name: Archive Webpack build artifacts uses: actions/upload-artifact@v2.2.2 @@ -236,25 +232,17 @@ jobs: npm install --force @inrupt/solid-client-vc@$VERSION_NR env: VERSION_NR: ${{ needs.publish-npm.outputs.version-nr }} - if: false - name: Make sure that the end-to-end tests run against the just-published package run: | cd e2e/node # For all files (`-type f`) in this directory, # replace `../index` (i.e. in the import statement) with `@inrupt/solid-client-vc`: find ./ -type f -exec sed --in-place --expression='s/\.\.\/index/@inrupt\/solid-client-vc/g' {} \; - if: false - name: Run the Node-based end-to-end tests run: npm run e2e-test-node - if: false env: - E2E_TEST_ESS_POD: ${{ secrets.E2E_TEST_ESS_PROD_POD }} - E2E_TEST_ESS_IDP_URL: ${{ secrets.E2E_TEST_ESS_PROD_IDP_URL }} - E2E_TEST_ESS_REFRESH_TOKEN: ${{ secrets.E2E_TEST_ESS_PROD_REFRESH_TOKEN }} - E2E_TEST_ESS_CLIENT_ID: ${{ secrets.E2E_TEST_ESS_PROD_CLIENT_ID }} - E2E_TEST_ESS_CLIENT_SECRET: ${{ secrets.E2E_TEST_ESS_PROD_CLIENT_SECRET }} - E2E_TEST_ESS_COMPAT_POD: ${{ secrets.E2E_TEST_ESS_COMPAT_PROD_POD }} - E2E_TEST_ESS_COMPAT_IDP_URL: ${{ secrets.E2E_TEST_ESS_COMPAT_PROD_IDP_URL }} - E2E_TEST_ESS_COMPAT_REFRESH_TOKEN: ${{ secrets.E2E_TEST_ESS_COMPAT_PROD_REFRESH_TOKEN }} - E2E_TEST_ESS_COMPAT_CLIENT_ID: ${{ secrets.E2E_TEST_ESS_COMPAT_PROD_CLIENT_ID }} - E2E_TEST_ESS_COMPAT_CLIENT_SECRET: ${{ secrets.E2E_TEST_ESS_COMPAT_PROD_CLIENT_SECRET }} + E2E_TEST_ESS_IDP_URL: https://broker.pod.inrupt.com + E2E_TEST_ESS_CLIENT_ID: ${{ secrets.POD_SPACES_CLIENT_ID }} + E2E_TEST_ESS_CLIENT_SECRET: ${{ secrets.POD_SPACES_CLIENT_SECRET }} + E2E_TEST_ESS_VC_ISSUER: https://consent.pod.inrupt.com/issue + E2E_TEST_ESS_VC_SUBJECT: https://pod.inrupt.com/solid-client-e2e-tester-ess/profile/card#me diff --git a/CHANGELOG.md b/CHANGELOG.md index ef70e62a..0fa894f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The following changes have been implemented but not released yet: ## [Unreleased] +### Bugfix + +- No longer uses the default session from `@inrupt/solid-client-authn-browser` + because it causes issues with Webpack. + The following sections document changes that have been released already: ## 0.1.0 - 2021-09-06 diff --git a/rollup.config.js b/rollup.config.js index 0cc8418b..5578e133 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -43,7 +43,7 @@ export default { { dir: "umd", format: "umd", - name: "SolidClient", + name: "SolidClientVc", }, ], plugins: [ @@ -57,5 +57,5 @@ export default { }, }), ], - external: ["cross-fetch"], + external: ["cross-fetch", "http-link-header"], }; diff --git a/src/fetcher.ts b/src/fetcher.ts index f14d61c8..1c5e7e18 100644 --- a/src/fetcher.ts +++ b/src/fetcher.ts @@ -29,47 +29,28 @@ const defaultFetch: typeof window.fetch = async (resource, init) => { if (typeof window === "object" && typeof require !== "function") { return window.fetch(resource, init); } - /* istanbul ignore if: `require` is always defined in the unit test environment */ - if (typeof require !== "function") { - // When using Node.js with ES Modules, require is not defined: - let fetch; - try { - // solid-client-authn-browser may be unresolved, we just try to autodetect it. - const { fetch: defaultSessionFetch } = await import( - /* eslint-disable import/no-unresolved */ - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - "@inrupt/solid-client-authn-browser" - ); - fetch = defaultSessionFetch; - } catch (e) { - const crossFetchModule = await import("cross-fetch"); - fetch = crossFetchModule.default; - return await fetch(resource, init); - } - } - // Implementation note: it's up to the client application to resolve these module names to their - // respective npm packages. At least one commonly used tool (Webpack) is only able to do that if - // the module names are literal strings. - // Additionally, Webpack throws a warning in a way that halts compilation for at least Next.js - // when using native JavaScript dynamic imports (`import()`), whereas `require()` just logs a - // warning. Since the use of package names instead of file names requires a bundler anyway, this - // should not have any practical consequences. For more background, see: - // https://github.com/webpack/webpack/issues/7713 - // eslint-disable-next-line no-shadow - let fetch; - try { - // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires - const sca = require("@inrupt/solid-client-authn-browser"); - /* istanbul ignore next : `solid-client-authn-browser` is not a dependency of this library */ - fetch = sca.fetch; - } catch (e) { - // eslint-disable-next-line prefer-const, global-require - fetch = require("cross-fetch"); - } + // eslint-disable-next-line no-shadow + // let fetch; + // try { + // // solid-client-authn-browser may be unresolved, we just try to autodetect it. + // const { fetch: defaultSessionFetch } = await import( + // /* eslint-disable import/no-unresolved */ + // // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // // @ts-ignore + // "@inrupt/solid-client-authn-browser" + // ); + // /* istanbul ignore next : `solid-client-authn-browser` is not a dependency of this library */ + // fetch = defaultSessionFetch; + // } catch (e) { + // const crossFetchModule = await import("cross-fetch"); + // fetch = crossFetchModule.default; + // // return await fetch(resource, init); + // } + // return fetch(resource, init); - return fetch(resource, init); + const crossFetchModule = await import("cross-fetch"); + return crossFetchModule.default(resource, init); }; export default defaultFetch; diff --git a/src/issue/issue.ts b/src/issue/issue.ts index 1ea7efd8..d13487c4 100644 --- a/src/issue/issue.ts +++ b/src/issue/issue.ts @@ -60,19 +60,41 @@ export default async function issueVerifiableCredential( internalOptions.fetch = fallbackFetch; } // credentialClaims should contain all the claims, but not the context. - const { - "@context": subjectClaimsContext, - ...contextlessSubjectClaims - } = subjectClaims; - const { - "@context": credentialClaimsContext, - ...contextlessCredentialClaims - } = credentialClaims !== undefined ? credentialClaims : { "@context": [] }; + // const { + // "@context": subjectClaimsContext, + // ...contextlessSubjectClaims + // } = subjectClaims; + // The following lines refactor the previous deconstruction in order to work + // around a misalignment between `rollup-plugin-typescript2` and NodeJS. + // Issue tracking: https://github.com/ezolenko/rollup-plugin-typescript2/issues/282 + const contextlessSubjectClaims = { ...subjectClaims }; + delete contextlessSubjectClaims["@context"]; + const subjectClaimsContext = subjectClaims["@context"]; + + // const { + // "@context": credentialClaimsContext, + // ...contextlessCredentialClaims + // } = credentialClaims !== undefined ? credentialClaims : { "@context": [] }; + // The following lines refactor the previous deconstruction in order to work + // around a misalignment between `rollup-plugin-typescript2` and NodeJS. + // Issue tracking: https://github.com/ezolenko/rollup-plugin-typescript2/issues/282 // When we add proper JSONLD parsing support, the following should be replaced. - const { - type: credentialTypeClaims, - ...nonTypeCredentialClaims - } = contextlessCredentialClaims; + const contextlessCredentialClaims = { ...credentialClaims }; + delete contextlessCredentialClaims["@context"]; + const credentialClaimsContext = + credentialClaims !== undefined ? credentialClaims["@context"] : []; + + // const { + // type: credentialTypeClaims, + // ...nonTypeCredentialClaims + // } = contextlessCredentialClaims; + // The following lines refactor the previous deconstruction in order to work + // around a misalignment between `rollup-plugin-typescript2` and NodeJS. + // Issue tracking: https://github.com/ezolenko/rollup-plugin-typescript2/issues/282 + const nonTypeCredentialClaims = { ...contextlessCredentialClaims }; + delete nonTypeCredentialClaims.type; + const credentialTypeClaims = contextlessCredentialClaims.type; + let credentialTypes = []; if (credentialTypeClaims !== undefined) { credentialTypes = Array.isArray(credentialTypeClaims) @@ -95,13 +117,16 @@ export default async function issueVerifiableCredential( }, }, }; - const response = await internalOptions.fetch(issuerEndpoint, { - headers: { - "Content-Type": "application/json", - }, - method: "POST", - body: JSON.stringify(credentialIssueBody), - }); + const response = await (internalOptions.fetch as typeof global.fetch)( + issuerEndpoint, + { + headers: { + "Content-Type": "application/json", + }, + method: "POST", + body: JSON.stringify(credentialIssueBody), + } + ); if (!response.ok) { // TODO: use the error library when available. throw new Error( diff --git a/src/lookup/derive.ts b/src/lookup/derive.ts index 6474b0ff..6a1dbc3d 100644 --- a/src/lookup/derive.ts +++ b/src/lookup/derive.ts @@ -114,7 +114,13 @@ export default async function getVerifiableCredentialAllFromShape( internalOptions.fetch = fallbackFetch; } // credentialClaims should contain all the claims, but not the context. - const { "@context": claimsContext, ...credentialClaims } = vcShape; + // const { "@context": claimsContext, ...credentialClaims } = vcShape; + // The following lines refactor the previous deconstruction in order to work + // around a misalignment between `rollup-plugin-typescript2` and NodeJS. + // Issue tracking: https://github.com/ezolenko/rollup-plugin-typescript2/issues/282 + const credentialClaims = { ...vcShape }; + delete credentialClaims["@context"]; + const claimsContext = vcShape["@context"]; const credentialRequestBody = { // See https://w3c-ccg.github.io/vc-http-api/holder.html credential: {