Skip to content

Commit

Permalink
Change to ESM and dual publish ESM+CJS (#9)
Browse files Browse the repository at this point in the history
* change to esm and dual publish

* 2.2.1

* 2.3.0

* fix entrypoints

* 2.3.1

* add files

* 2.3.2

* fix exports

* 2.3.3

* add tests for cjs and esm

* add node16

* add cross-fetch
  • Loading branch information
zirkelc authored Feb 9, 2024
1 parent 57d4136 commit b532d92
Show file tree
Hide file tree
Showing 45 changed files with 1,917 additions and 262 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "CI"
name: CI

on:
push:
Expand All @@ -18,10 +18,10 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Setup Node.js 16
- name: Setup Node.js 18
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
cache: 'pnpm'

- name: Install
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ typings/

# TernJS port file
.tern-port

.DS_Store
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"quickfix.biome": "explicit",
"source.organizeImports.biome": true
"source.organizeImports.biome": "explicit"
},
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome",
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ AWS SignatureV4 fetch API function to automatically sign HTTP request with given
[AWS documentation on Signature Version 4 signing process](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html)

## ⚠️ Breaking Chang in v2
In v2, the dependency to `cross-fetch` has been removed since native `fetch` is now supported in Node.js >= v18. If you are running in an environment where native `fetch` is **not** available, it must either be polyfilled or provided as an argument to `createSignedFetcher`. This allows to use the same `fetch` function that is already used in your application.
## ⚠️ ESM Support
Since v3, this package ships with ESM and CommonJS support. That means you can `import` or `require` the package in your project.

See [Fetch](#fetch) for more information.
```ts
// ESM
import { createSignedFetcher } from 'aws-sigv4-fetch';

// CommonJS
const { createSignedFetcher } = require('aws-sigv4-fetch');
```

## Install
```sh
Expand Down Expand Up @@ -72,7 +78,7 @@ const result = await client.request(query, variables);
```

### Fetch
By default, `createSignedFetcher` uses the `fetch` function from the environment. If you are running in an environment where native `fetch` is **not** available, the `fetch` function must be polyfilled or provided as an argument to `createSignedFetcher`. There are several ways to do this:
By default, `createSignedFetcher` uses the `fetch` function from the environment. Native `fetch` is supported in Node.js >= v18. If you are running in an environment where native `fetch` is **not** available, the `fetch` function must be polyfilled or provided as an argument to `createSignedFetcher`. This allows to use the same `fetch` function that is already used in your application. There are several ways to do this:

#### Native `fetch`
If native `fetch` is available, you don't have to pass it as argument to `createSignedFetcher`.
Expand Down
2 changes: 0 additions & 2 deletions dist/create-signed-fetcher.d.ts

This file was deleted.

52 changes: 0 additions & 52 deletions dist/create-signed-fetcher.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/create-signed-fetcher.js.map

This file was deleted.

1 change: 0 additions & 1 deletion dist/get-fetch.d.ts

This file was deleted.

18 changes: 0 additions & 18 deletions dist/get-fetch.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/get-fetch.js.map

This file was deleted.

1 change: 0 additions & 1 deletion dist/get-headers.d.ts

This file was deleted.

23 changes: 0 additions & 23 deletions dist/get-headers.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/get-headers.js.map

This file was deleted.

110 changes: 110 additions & 0 deletions dist/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/index.ts
var src_exports = {};
__export(src_exports, {
createSignedFetcher: () => createSignedFetcher
});
module.exports = __toCommonJS(src_exports);

// src/create-signed-fetcher.ts
var import_sha256_js = require("@aws-crypto/sha256-js");
var import_credential_provider_node = require("@aws-sdk/credential-provider-node");
var import_protocol_http = require("@aws-sdk/protocol-http");
var import_signature_v4 = require("@aws-sdk/signature-v4");

// src/get-fetch.ts
var getFetchFn = (customFetchFn) => {
if (customFetchFn) {
return customFetchFn;
}
if (typeof window !== "undefined" && typeof window.fetch === "function") {
return window.fetch.bind(window);
}
if (typeof globalThis !== "undefined" && typeof globalThis.fetch === "function") {
return globalThis.fetch.bind(globalThis);
}
throw new Error("No fetch implementation found");
};

// src/get-headers.ts
var getHeaders = (init) => {
const headers = /* @__PURE__ */ new Map();
if (init === void 0)
return headers;
if (Array.isArray(init)) {
init.forEach((header) => headers.set(header[0], header[1]));
return headers;
}
if ("forEach" in init && typeof init.forEach === "function") {
init.forEach((value, key) => headers.set(key, value));
return headers;
}
if (typeof init === "object") {
Object.entries(init).forEach(([key, value]) => headers.set(key, value));
return headers;
}
throw new Error("Invalid headers");
};

// src/create-signed-fetcher.ts
var createSignedFetcher = (opts) => {
const service = opts.service;
const region = opts.region || "us-east-1";
const credentials = opts.credentials || (0, import_credential_provider_node.defaultProvider)();
const fetchFn = getFetchFn(opts.fetch);
return async (input, init) => {
const url = new URL(
typeof input === "string" ? input : input instanceof URL ? input.href : input.url
);
const headers = getHeaders(init?.headers);
headers.set("host", url.host);
const request = new import_protocol_http.HttpRequest({
hostname: url.hostname,
path: url.pathname,
protocol: url.protocol,
port: url.port ? Number(url.port) : void 0,
username: url.username,
password: url.password,
method: init?.method.toUpperCase(),
// method must be uppercase
body: init?.body,
query: Object.fromEntries(url.searchParams.entries()),
fragment: url.hash,
headers: Object.fromEntries(headers.entries())
});
const signer = new import_signature_v4.SignatureV4({
credentials,
service,
region,
sha256: import_sha256_js.Sha256
});
const signedRequest = await signer.sign(request);
return fetchFn(input, {
...init,
headers: signedRequest.headers,
body: signedRequest.body,
method: signedRequest.method
});
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createSignedFetcher
});
12 changes: 12 additions & 0 deletions dist/index.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AwsCredentialIdentity, Provider } from '@aws-sdk/types';

type SignedFetcherOptions = {
service: string;
region?: string;
credentials?: AwsCredentialIdentity | Provider<AwsCredentialIdentity>;
fetch?: typeof fetch;
};
type CreateSignedFetcher = (init: SignedFetcherOptions) => typeof fetch;
declare const createSignedFetcher: CreateSignedFetcher;

export { type CreateSignedFetcher, type SignedFetcherOptions, createSignedFetcher };
16 changes: 12 additions & 4 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export * from "./create-signed-fetcher";
export * from "./get-headers";
export * from "./get-fetch";
export * from "./types";
import { AwsCredentialIdentity, Provider } from '@aws-sdk/types';

type SignedFetcherOptions = {
service: string;
region?: string;
credentials?: AwsCredentialIdentity | Provider<AwsCredentialIdentity>;
fetch?: typeof fetch;
};
type CreateSignedFetcher = (init: SignedFetcherOptions) => typeof fetch;
declare const createSignedFetcher: CreateSignedFetcher;

export { type CreateSignedFetcher, type SignedFetcherOptions, createSignedFetcher };
Loading

0 comments on commit b532d92

Please sign in to comment.