Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locker complience fixed #131

Merged
merged 6 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Start a local, blank cluster. See [Integrate](https://github.com/opentdf/opentdf
### Usage

```typescript
const { AuthProviders, NanoTDFClient } = require('@opentdf/client');
// currently we support only ESM import
import { AuthProviders, NanoTDFClient } from '@opentdf/client';

const oidcCredentials: RefreshTokenCredentials = {
clientId: keycloakClientId,
Expand All @@ -29,7 +30,8 @@ Start a local, blank cluster. See [Integrate](https://github.com/opentdf/opentdf
```
For files:
```typescript
const { FileClient } = require('@opentdf/client');
// currently we support only ESM import
import { FileClient } from '@opentdf/client';

// for file Encryption
const fileClient = new FileClient(
Expand Down
20 changes: 8 additions & 12 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opentdf/client",
"version": "0.4.0",
"version": "0.4.1",
"description": "Access and generate tdf protected content",
"homepage": "https://github.com/opentdf/client-web",
"bugs": {
Expand All @@ -23,38 +23,34 @@
"license": "BSD-3-Clause-Clear",
"author": "Virtru",
"browser": "./dist/esm/web/tdf3/index-web.js",
"main": "./dist/cjs/node/tdf3/index.js",
"main": "./dist/esm/node/tdf3/index.js",
"types": "./dist/types/tdf3/index.d.ts",
"exports": {
".": {
"node": {
"types": "./dist/types/tdf3/index.d.ts",
"import": "./dist/esm/node/tdf3/index.js",
"require": "./dist/cjs/node/tdf3/index.js"
"import": "./dist/esm/node/tdf3/index.js"
},
"default": {
"types": "./dist/types/tdf3/index.d.ts",
"import": "./dist/esm/web/tdf3/index-web.js",
"require": "./dist/cjs/web/tdf3/index-web.js"
"import": "./dist/esm/web/tdf3/index-web.js"
}
},
"./nano": {
"node": {
"types": "./dist/types/src/index.d.ts",
"import": "./dist/esm/node/src/index.node.js",
"require": "./dist/cjs/node/src/index.node.js"
"import": "./dist/esm/node/src/index.node.js"
},
"default": {
"types": "./dist/types/src/index.d.ts",
"import": "./dist/esm/web/src/index.js",
"require": "./dist/cjs/web/src/index.js"
"import": "./dist/esm/web/src/index.js"
}
}
},
"scripts": {
"build": "npm run clean && tsc -p tsconfig-node-cjs.json && tsc -p tsconfig-node-esm.json && tsc -p tsconfig-web-cjs.json && tsc -p tsconfig-web-esm.json && ../scripts/add-module-types.sh",
"build": "npm run clean && tsc -p tsconfig-node-esm.json && tsc -p tsconfig-web-esm.json && ../scripts/add-module-types.sh",
"clean": "rm -rf {build,dist}",
"doc": "typedoc --out dist/docs src/index.ts",
"doc": "typedoc --tsconfig ./tsconfig-web-esm.json --out dist/docs src/index.ts",
"format": "prettier --write \"{src,tdf3,tests}/**/*.ts\"",
"license-check": "license-checker --production --onlyAllow 'Apache-2.0; BSD; CC-BY-4.0; ISC; MIT'",
"lint": "eslint ./src/**/*.ts ./tdf3/**/*.ts ./tests/**/*.ts",
Expand Down
2 changes: 1 addition & 1 deletion lib/src/auth/AccessToken.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { default as dpopFn } from 'dpop';
import { IllegalArgumentError } from '../../tdf3/src/errors.js';
import { rstrip } from '../utils.js';

Expand Down Expand Up @@ -136,7 +137,6 @@ export class AccessToken {
}
if (this.signing_key) {
// TODO: dpop does not have a CommonJS variant.
const { default: dpopFn } = await import('dpop');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is to work around the dpop library not exporting a CJS variant. Node supports dynamic import even in CJS so we may have to do something else here - or fork the library, most likely

See: https://stackoverflow.com/a/71093947

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn it. Welp, last time i had such issues with locker complience, ive just putted lib as a folder. Not whole thing but what we use. Also we could fork it and add webpack to bundle CJS variant. Or maybe theres other libs that doing what drop does

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we can fork as opentdf and publish to GitHub's package registry. Adding the CommonJS output should be pretty similar to what we are doing with this library now

headers['DPoP'] = await dpopFn(this.signing_key, url, 'POST');
}
if (!this.virtru_client_pubkey && !this.signing_key) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/auth/virtru-oidc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
import { default as dpopFn } from 'dpop';
import { AccessToken, AccessTokenConfig } from './AccessToken.js';
import { IVirtruOIDC } from '../nanotdf/interfaces/OIDCInterface.js';
import { HttpRequest, withHeaders } from './auth.js';
Expand Down Expand Up @@ -128,7 +129,6 @@ export default class VirtruOIDC {
async withCreds(httpReq: HttpRequest): Promise<HttpRequest> {
const accessToken = await this.getCurrentAccessToken();
if (this.accessTokenGetter.signing_key) {
const { default: dpopFn } = await import('dpop');
const dpopToken = await dpopFn(
this.accessTokenGetter.signing_key,
httpReq.url,
Expand Down
8 changes: 5 additions & 3 deletions lib/src/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
clientType,
} from './index.js';

globalThis.crypto ??= crypto as unknown as Crypto;
// @ts-expect-error transform type incompatible
globalThis.ReadableStream ??= ReadableStream;
if (globalThis) {
globalThis.crypto ??= crypto as unknown as Crypto;
// @ts-expect-error transform type incompatible
globalThis.ReadableStream ??= ReadableStream;
}

export { NanoTDFClient, NanoTDFDatasetClient, AuthProviders, version, clientType };
// for cjs build a default export is needed
Expand Down
13 changes: 8 additions & 5 deletions lib/tdf3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import { AuthProvider, AppIdAuthProvider, HttpRequest } from '../src/auth/auth.j
import { registerModuleType } from './src/client/tdf-stream.js';
import { NodeTdfStream } from './src/client/NodeTdfStream.js';

globalThis.crypto ??= crypto as unknown as Crypto;
//@ts-expect-error assignment to any type
globalThis.CryptoKey ??= crypto.CryptoKey;
// @ts-expect-error transform type incompatible
globalThis.ReadableStream ??= ReadableStream;
if (globalThis) {
globalThis.crypto ??= crypto as unknown as Crypto;
//@ts-expect-error assignment to any type
globalThis?.CryptoKey ??= crypto.CryptoKey;
// @ts-expect-error transform type incompatible
globalThis?.ReadableStream ??= ReadableStream;
}

registerModuleType(NodeTdfStream);

export {
Expand Down
2 changes: 1 addition & 1 deletion lib/tdf3/src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {

// Used to pass into native crypto functions
const METHODS: KeyUsage[] = ['encrypt', 'decrypt'];
export const isSupported = typeof globalThis.crypto !== 'undefined';
export const isSupported = typeof globalThis?.crypto !== 'undefined';

export const method = 'http://www.w3.org/2001/04/xmlenc#aes256-cbc';
export const name = 'BrowserNativeCryptoService';
Expand Down
9 changes: 0 additions & 9 deletions lib/tsconfig-node-cjs.json

This file was deleted.

9 changes: 0 additions & 9 deletions lib/tsconfig-web-cjs.json

This file was deleted.