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

Enable CI #1

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e9fd270
Enable CI
chriskilding Jun 6, 2022
fd0f3de
WIP add npm-publish GH Action boilerplate
chriskilding Jun 6, 2022
facdf16
WIP add test and publish CI workflows
chriskilding Jun 6, 2022
4c90ab6
Update test.yml
chriskilding Jun 6, 2022
8fe8ffa
Update test.yml
chriskilding Jun 6, 2022
ef1a02f
Update test.yml
chriskilding Jun 6, 2022
e736677
Update test.yml
chriskilding Jun 6, 2022
a885c05
Rework based on napi-rs skeleton project
chriskilding Jun 6, 2022
9e97463
Add Win/Mac/Linux build matrix to GH Action
chriskilding Jun 6, 2022
3e8d39f
Update test.yml
chriskilding Jun 6, 2022
d2849c1
Use target option in GH Actions
chriskilding Jun 6, 2022
1b6bbf0
Revert target config in GH Action
chriskilding Jun 6, 2022
0c6312c
Just do npm run build on Actions
chriskilding Jun 6, 2022
c026806
Tweak cert_format rust function
chriskilding Jun 6, 2022
c70ba3d
Remove semicolon
chriskilding Jun 6, 2022
3f09e07
Tweaks
chriskilding Jun 6, 2022
1ed5402
Update .gitignore
chriskilding Jun 7, 2022
ba1be02
ISC license
chriskilding Jun 7, 2022
d87a577
Test cert_format function
chriskilding Jun 7, 2022
b624ad4
Override Rust toolchain target for architecture
chriskilding Jun 8, 2022
c2c1dd5
Add Apple Silicon target to build matrix
chriskilding Jun 8, 2022
95f9257
Fix
chriskilding Jun 8, 2022
dee7a55
try node 18
chriskilding Jun 8, 2022
b30469c
Tweaks
chriskilding Jun 8, 2022
f8560aa
Node 16 again
chriskilding Jun 8, 2022
3712b0a
Toolchain not target
chriskilding Jun 8, 2022
9c80691
Back to stable toolchain
chriskilding Jun 8, 2022
ea21a84
Tweak test
chriskilding Jun 8, 2022
4e0687c
Tweak test
chriskilding Jun 8, 2022
44212f2
Again
chriskilding Jun 8, 2022
01a1aa6
Portable ls for windows
chriskilding Jun 8, 2022
bdfcd63
Remove ls -lah debug
chriskilding Jun 8, 2022
dbf8cf7
Use x64 host architecture for aarch64-apple
chriskilding Jun 8, 2022
84925f1
WIP add downstream collator job on GH Actions
chriskilding Jun 13, 2022
06c845a
Run npm artifacts target
chriskilding Jun 13, 2022
6bc575a
Try different host in test
chriskilding Jun 13, 2022
0eb3d91
Fix snake case warning from Rust
chriskilding Jun 13, 2022
fe8ad2b
Tweak npm run artifacts invocation
chriskilding Jun 13, 2022
5974d49
npm install in the publish job
chriskilding Jun 13, 2022
2779a6c
add --target option
chriskilding Jun 13, 2022
f65c43f
WIP must skip tests for aarch64-darwin
chriskilding Jun 13, 2022
061afed
Tweak
chriskilding Jun 13, 2022
327759f
Go back to CJS example in README
chriskilding Jun 13, 2022
7899f98
Put under @bnoordhuis NPM package scope
chriskilding Jun 15, 2022
a7929f5
dry run the npm publish
chriskilding Jun 15, 2022
76a829b
Set repository field in package.jsons
chriskilding Jun 15, 2022
de8fd2e
Remove ava framework
chriskilding Jul 18, 2022
5a3d9e1
Propagate RusTLS errors instead of hiding them
chriskilding Jul 18, 2022
4bedd5d
Rework the cert_format function up into JS
chriskilding Jul 19, 2022
a56b61e
Update README
chriskilding Jul 19, 2022
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
64 changes: 64 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Node.js CI

on: [push, pull_request, workflow_dispatch]

jobs:
build:
strategy:
fail-fast: false
matrix:
include:
- os: macos-latest
target: x86_64-apple-darwin
architecture: x64
- os: macos-latest
target: aarch64-apple-darwin
architecture: x64 # Needed because the runner is an Intel Mac
skipTests: true # Needed because the compiled arm64 code won't execute on the Intel Mac runner
- os: windows-latest
target: x86_64-pc-windows-msvc
architecture: x64
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
architecture: x64
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
architecture: ${{ matrix.architecture }}
- uses: actions-rs/toolchain@v1
with:
profile: minimal
override: true
toolchain: stable
target: ${{ matrix.target }}
- run: npm install
- run: npm run build -- --target ${{ matrix.target }}
- run: npm test
if: ${{ !matrix.skipTests }}
- name: Upload platform-specific binary
uses: actions/upload-artifact@v3
with:
name: bindings-${{ matrix.target }}
path: native-certs.*.node
if-no-files-found: error
publish:
runs-on: ubuntu-latest
needs: build # this waits for *all* the matrix build actions to complete
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- name: Download all platform-specific binaries
uses: actions/download-artifact@v3
with:
path: artifacts
- run: npm install
- run: npm run artifacts
- name: List packages
run: ls -R ./npm
shell: bash
- run: npm publish --dry-run
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/binding.node
/node_modules/
/package-lock.json
/target/
node_modules/
package-lock.json
target/
lib.js
lib.d.ts
*.node
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
target
Cargo.lock
.cargo
.github
npm
.eslintrc
.prettierignore
rustfmt.toml
yarn.lock
chriskilding marked this conversation as resolved.
Show resolved Hide resolved
*.node
42 changes: 21 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
node-native-certs
=================
# node-native-certs

Load TLS root certificates from the system trust store.

- Windows: loads certificates from the system certificate store.

- macOS: loads certificates from the keychain.

- Linux, BSD: looks for the OpenSSL CA bundle in the usual places.
Honors the `SSL_CERT_FILE` and `SSL_CERT_DIR` environment variables.

The `SSL_CERT_FILE` environment variable overrides the default trust store
on all platforms.
## Setup

```shell
npm install native-certs
```

## Usage

```js
const https = require("https")
const nativeCerts = require("native-certs")
const ca = nativeCerts()
const { loadNativeCerts } = require("native-certs")

const ca = loadNativeCerts()

const req = https.get({ca, host: "google.com", path: "/"})
// do something with `req`
```

The `SSL_CERT_FILE` environment variable overrides the default trust store
on all platforms.

Node.js does not support globally overriding the built-in root certificates
but the list can be extended through the `NODE_EXTRA_CA_CERTS` environment
variable:

```
$ node -e 'fs.writeFileSync("cas.pem", require("native-certs")().join("\n"))'
```shell
node -e 'fs.writeFileSync("cas.pem", require("native-certs")().join("\n"))'

$ NODE_EXTRA_CA_CERTS=cas.pem node app.js
NODE_EXTRA_CA_CERTS=cas.pem node app.js
```

Performance consideration: loading certificates can be slow. Cache 'em.

license
=======
## License

ISC, see the LICENSE file.

## Development

```shell
npm install
npm run build
npm test
```

If you need to run `napi-rs` scaffolder commands you may also need to `npm install -g @napi-rs/cli`.
83 changes: 83 additions & 0 deletions __test__/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { notEqual, ok, strictEqual } from 'assert';
import { get } from 'https';
import { Buffer } from 'node:buffer';
import { loadNativeCerts, certFormat } from '../index.js';

// Promise-ify https.get
async function pget(params) {
return new Promise((resolve, reject) => {
const request = get(params, (response) => {
if (response.statusCode < 200 || response.statusCode > 399) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
const body = [];
response.on('data', (chunk) => body.push(chunk));
response.on('end', () => resolve(body.join('')));
});
request.on('error', (err) => reject(err))
})
}

// HTTPS request should use native certs
{
const ca = loadNativeCerts()
notEqual(ca.length, 0)

await pget({ca, host: "www.npmjs.com", path: "/"})

ok('request was ok')
}

// cert_format should format a certificate
{
const input = `-----BEGIN CERTIFICATE-----
MIIE5jCCAs4CCQD5/huzlZjclTANBgkqhkiG9w0BAQsFADA1MQswCQYDVQQGEwJV
UzEQMA4GA1UECgwHRXhhbXBsZTEUMBIGA1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+y
sB/CgpwDp0EbRk0zTa6Yd4emb0Ly41SPL4dQoYugbZqAjU5LRlHxjHsh9etMImrX
LCAhlbcK0PGe7S+UDmMvkL5hM5AjnssQg+06Hmii6Xbns6gfk2wtxim4Q8mVymst
XLqgrOvQ46HTPydi2uYahzwnkTp9KR20SEnP++S1793oGSI+FR43rL5a/lioNcl8
aY172JsHtNknRSkOQEGPvqqfutXOki27ArVfJdjzWyQvFTKghxb+5kK+NJ77D7px
YzVM8olT/mKMqvyO26+kjfpjSUs+KiwGaw14TAPizUes3dx47VBicT+XDr/mMYAy
gJBH3AFtO0wBlU8d+P3iDslu3FHUnLCQK+5An7N8iZDOjhGLI50XZ+2Wi4My6N5D
Mi5qSX4AxL8NnIUg20rkAF582W6/jD1xDAefWZKtHWRuYx0iKiisaWZ9ZCFqt/OK
RgJ6aGUHlpoSEYnBkMNJ/fL5WTu8HJ+l4RmDgpkiR+JfX05/tm5DkJjWVafsjs9e
0VP1X/7wvBPhEg==
-----END CERTIFICATE-----`

const inputWithoutAsciiArmor = input
.replace("-----BEGIN CERTIFICATE-----\n", "")
.replace("-----END CERTIFICATE-----\n", "")
const certBuffer = Buffer.from(inputWithoutAsciiArmor, 'base64')
const output = certFormat(certBuffer)

strictEqual(input.trim(), output.trim())
}

// cert_format should format an empty certificate
{
const input = Buffer.from([], 'base64')

const actual = certFormat(input)

var expected = ""
expected += "-----BEGIN CERTIFICATE-----\n"
expected += "-----END CERTIFICATE-----\n"

strictEqual(actual.trim(), expected.trim())
}
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* tslint:disable */
/* eslint-disable */

export function loadNativeCerts(): Array<String>
export function certFormat(buf: Buffer): String
Loading