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

Fix importing node-only modules in browser env #2491

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ Retrieves credentials using OIDC token from a file on disk. It's commonly used i
- **v3**: [`fromTokenFile`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_web_identity.html#fromtokenfile-1)

```javascript
import { fromTokenFile } from "@aws-sdk/credential-provider-web-identity";
import { fromTokenFile } from "@aws-sdk/credential-provider-node-identity";
import { getDefaultRoleAssumerWithWebIdentity } from "@aws-sdk/client-sts"; // ES6 import
// const { fromIni } from("@aws-sdk/credential-provider-ini");
// const { getDefaultRoleAssumerWithWebIdentity } = require("@aws-sdk/client-sts"); // CommonJS import
Expand Down
7 changes: 7 additions & 0 deletions clients/client-cognito-identity/buildPackage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { readFileSync, writeFileSync } = require("fs");

const encoding = "utf8";
const file = readFileSync("./package.json", { encoding });
const code = `export default ${file}`;
writeFileSync("./dist/es/package.js", code, { encoding });
writeFileSync("./dist/cjs/package.js", code, { encoding });
3 changes: 2 additions & 1 deletion clients/client-cognito-identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
"test": "yarn test:unit",
"build:cjs": "tsc -p tsconfig.json",
"build:es": "tsc -p tsconfig.es.json",
"build": "yarn build:cjs && yarn build:es"
"build": "yarn build:cjs && yarn build:es && node buildPackage.js"
},
"main": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts",
"module": "./dist/es/index.js",
"browser": {
"./dist/es/runtimeConfig": "./dist/es/runtimeConfig.browser",
"./runtimeConfig": "./runtimeConfig.browser"
},
"react-native": {
Expand Down
5 changes: 3 additions & 2 deletions clients/client-cognito-identity/runtimeConfig.browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import packageInfo from "./package.json";

import { Sha256 } from "@aws-crypto/sha256-browser";
import { FetchHttpHandler, streamCollector } from "@aws-sdk/fetch-http-handler";
import { invalidProvider } from "@aws-sdk/invalid-dependency";
Expand All @@ -8,7 +6,10 @@ import { fromBase64, toBase64 } from "@aws-sdk/util-base64-browser";
import { calculateBodyLength } from "@aws-sdk/util-body-length-browser";
import { defaultUserAgent } from "@aws-sdk/util-user-agent-browser";
import { fromUtf8, toUtf8 } from "@aws-sdk/util-utf8-browser";

import { ClientDefaults } from "./CognitoIdentityClient";
// @ts-ignore
import packageInfo from "./package.js";
import { ClientSharedValues } from "./runtimeConfig.shared";

/**
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
"clients/*",
"lib/*",
"packages/*",
"protocol_tests/*"
"protocol_tests/*",
"tests/vite-project"
],
"nohoist": [
"**/karma*",
Expand All @@ -118,4 +119,4 @@
],
"**/*.{ts,js,md,json}": "prettier --write"
}
}
}
3 changes: 3 additions & 0 deletions packages/credential-provider-imds/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
"dependencies": {
"@aws-sdk/property-provider": "3.18.0",
"@aws-sdk/types": "3.18.0",
"buffer": "^6.0.3",
"stream-http": "^3.2.0",
"tslib": "^2.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.4",
"@types/node": "^10.0.0",
"@types/readable-stream": "^2.3.10",
"jest": "^26.1.0",
"nock": "^13.0.2",
"typescript": "~4.3.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ProviderError } from "@aws-sdk/property-provider";
import { CredentialProvider } from "@aws-sdk/types";
import { RequestOptions } from "http";
import { RequestOptions } from "stream-http";
import { parse } from "url";

import { httpRequest } from "./remoteProvider/httpRequest";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ProviderError } from "@aws-sdk/property-provider";
import { CredentialProvider, Credentials } from "@aws-sdk/types";
import { RequestOptions } from "http";
import { RequestOptions } from "stream-http";

import { httpRequest } from "./remoteProvider/httpRequest";
import { fromImdsCredentials, isImdsCredentials } from "./remoteProvider/ImdsCredentials";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ProviderError } from "@aws-sdk/property-provider";
import { createServer } from "http";
import http, { createServer } from "http";
import nock from "nock";

import { httpRequest } from "./httpRequest";

jest.mock("stream-http", () => http);

describe("httpRequest", () => {
let port: number;
const host = "localhost";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ProviderError } from "@aws-sdk/property-provider";
import { Buffer } from "buffer";
import { IncomingMessage, request, RequestOptions } from "http";
import { IncomingMessage, request, RequestOptions } from "stream-http";

/**
* @internal
Expand Down
3 changes: 2 additions & 1 deletion packages/credential-provider-imds/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"declarationDir": "./dist/types",
"rootDir": "./src",
"outDir": "./dist/cjs",
"baseUrl": "."
"baseUrl": ".",
"typeRoots": ["./types", "./node_modules/@types"]
},
"extends": "../../tsconfig.cjs.json",
"include": ["src/"]
Expand Down
3 changes: 2 additions & 1 deletion packages/credential-provider-imds/tsconfig.es.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"declarationDir": "./dist/types",
"rootDir": "./src",
"outDir": "./dist/es",
"baseUrl": "."
"baseUrl": ".",
"typeRoots": ["./types", "./node_modules/@types"]
},
"extends": "../../tsconfig.es.json",
"include": ["src/"]
Expand Down
42 changes: 42 additions & 0 deletions packages/credential-provider-imds/types/stream-http/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* This file has been extracted from @types/http but matching stream-http api */
declare module "stream-http" {
export * from "stream-http";
}

declare module "stream-http" {
import * as stream from "readable-stream";
class URL {}

// outgoing headers allows numbers (as they are converted internally to strings)
interface OutgoingHttpHeaders {
[header: string]: number | string | string[] | undefined;
}

interface ClientRequestArgs {
headers?: OutgoingHttpHeaders;
timeout?: number;
hostname?: string | null;
path?: string | null;
port?: number | string | null;
host?: string | null;
method?: string;
}

class ClientRequest extends stream.Writable {}

class IncomingMessage extends stream.Readable {
statusCode?: number;
}

interface AgentOptions {}

class Agent {}

interface RequestOptions extends ClientRequestArgs {}
function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
function request(
url: string | URL,
options: RequestOptions,
callback?: (res: IncomingMessage) => void
): ClientRequest;
}
1 change: 1 addition & 0 deletions packages/credential-provider-ini/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"@aws-sdk/credential-provider-env": "3.18.0",
"@aws-sdk/credential-provider-imds": "3.18.0",
"@aws-sdk/credential-provider-node-identity": "3.18.0",
"@aws-sdk/credential-provider-web-identity": "3.18.0",
"@aws-sdk/property-provider": "3.18.0",
"@aws-sdk/shared-ini-file-loader": "3.18.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/credential-provider-ini/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fromEnv } from "@aws-sdk/credential-provider-env";
import { fromContainerMetadata, fromInstanceMetadata } from "@aws-sdk/credential-provider-imds";
import { fromTokenFile } from "@aws-sdk/credential-provider-web-identity";
import { fromTokenFile } from "@aws-sdk/credential-provider-node-identity";
import { ENV_CONFIG_PATH, ENV_CREDENTIALS_PATH } from "@aws-sdk/shared-ini-file-loader";
import { Credentials } from "@aws-sdk/types";
import { join, sep } from "path";
Expand Down Expand Up @@ -54,7 +54,7 @@ jest.mock("os", () => {

import { homedir } from "os";

jest.mock("@aws-sdk/credential-provider-web-identity");
jest.mock("@aws-sdk/credential-provider-node-identity");

jest.mock("@aws-sdk/credential-provider-imds");

Expand Down
3 changes: 2 additions & 1 deletion packages/credential-provider-ini/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fromEnv } from "@aws-sdk/credential-provider-env";
import { fromContainerMetadata, fromInstanceMetadata } from "@aws-sdk/credential-provider-imds";
import { AssumeRoleWithWebIdentityParams, fromTokenFile } from "@aws-sdk/credential-provider-web-identity";
import { fromTokenFile } from "@aws-sdk/credential-provider-node-identity";
import { AssumeRoleWithWebIdentityParams } from "@aws-sdk/credential-provider-web-identity";
import { ProviderError } from "@aws-sdk/property-provider";
import {
loadSharedConfigFiles,
Expand Down
Loading