Skip to content

Commit

Permalink
This re-enables package import tests (#99)
Browse files Browse the repository at this point in the history
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
ezolenko/rollup-plugin-typescript2#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
ezolenko/rollup-plugin-typescript2#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.
  • Loading branch information
NSeydoux authored Sep 15, 2021
1 parent 78631e2 commit 9762864
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 79 deletions.
24 changes: 6 additions & 18 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
{
dir: "umd",
format: "umd",
name: "SolidClient",
name: "SolidClientVc",
},
],
plugins: [
Expand All @@ -57,5 +57,5 @@ export default {
},
}),
],
external: ["cross-fetch"],
external: ["cross-fetch", "http-link-header"],
};
59 changes: 20 additions & 39 deletions src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
63 changes: 44 additions & 19 deletions src/issue/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion src/lookup/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 9762864

Please sign in to comment.