Skip to content

Commit

Permalink
⬆️ Require Node 20, npm 10
Browse files Browse the repository at this point in the history
Node.js version 20 is the current LTS version. Node.js 18 will be
end-of-life in April 2025. This commit updates the minimum required
Node.js version to 20.

Node 20 supports ESM out of the box. The WebCrypto API is available in
the global scope. The `--experimental-global-webcrypto` flag is no
longer needed to run the examples.

The manual exposure of the API in the global scope has been removed from
the tests.
  • Loading branch information
ralfstx committed Oct 5, 2024
1 parent a0cb442 commit a3ba5a3
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: '18.x'
node-version: '20.x'
- run: npm ci
- run: npm run lint
- run: npm run test
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [0.5.5] - Unreleased

Minimum requirements bumped to Node 20 and npm 10.

### Deprecated

- `TextAttrs` in favor of `TextProps`.
Expand Down
35 changes: 3 additions & 32 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,11 @@ If you're using [Deno], you can skip this step.

## Run the examples

### Node

Node.js does not yet expose the [WebCrypto API] in the global scope.
Since PDF Maker uses this API, the `--experimental-global-webcrypto`
flag must be used:

```sh
$ node --experimental-global-webcrypto src/hello-world.js
```

Alternatively, you can expose the API in the global scope yourself:

```js
import * as crypto from 'crypto';
global.crypto ??= crypto;
```
Also note that Node.js still relies on the `module: true` flag in the
`package.json` file to enable ESM mode.
### Bun
[Bun] works out of the box:
```sh
$ node src/hello-world.js
# OR
$ bun run src/hello-world.js
```
### Deno
[Deno] works out of the box:
```sh
# OR
$ deno run --allow-read --allow-write src/hello-world.js
```

Expand All @@ -80,4 +52,3 @@ $ bun run --watch src/hello-world.js
[Node]: https://nodejs.org/en/
[Bun]: https://bun.sh/
[Deno]: https://deno.land/
[WebCrypto API]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"module": "./dist/index.js"
},
"engines": {
"node": ">=16.13.0",
"npm": ">=8"
"node": ">=20",
"npm": ">=10"
},
"scripts": {
"build": "rm -rf build/ dist/ && tsc && esbuild src/index.ts --bundle --sourcemap --platform=node --target=es2021,node18 --outdir=dist --format=esm --external:pdf-lib --external:@pdf-lib/fontkit && cp -a build/index.d.ts build/api/ dist/",
Expand Down
4 changes: 0 additions & 4 deletions src/api/make-pdf.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import crypto from 'node:crypto';

import { describe, expect, it } from 'vitest';

import { makePdf } from './make-pdf.ts';

global.crypto ??= (crypto as any).webcrypto;

describe('make-pdf', () => {
describe('makePdf', () => {
it('creates data that starts with a PDF 1.7 header', async () => {
Expand Down
3 changes: 0 additions & 3 deletions src/image-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import crypto from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

Expand All @@ -7,8 +6,6 @@ import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { ImageLoader, ImageStore } from './image-loader.ts';
import type { ImageSelector } from './images.ts';

global.crypto ??= (crypto as any).webcrypto;

describe('image-loader', () => {
let libertyJpg: Uint8Array;
let torusPng: Uint8Array;
Expand Down
3 changes: 0 additions & 3 deletions src/images.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import crypto from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

Expand All @@ -8,8 +7,6 @@ import type { Image } from './images.ts';
import { readImages, registerImage } from './images.ts';
import { fakePDFDocument, mkData } from './test/test-utils.ts';

global.crypto ??= (crypto as any).webcrypto;

describe('images', () => {
describe('readImages', () => {
it('returns an empty array for missing images definition', () => {
Expand Down
18 changes: 13 additions & 5 deletions src/render/render-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import crypto from 'node:crypto';

import type { PDFStream } from 'pdf-lib';
import type { PDFArray, PDFStream } from 'pdf-lib';
import { PDFDict, PDFDocument, PDFHexString, PDFName, PDFString } from 'pdf-lib';
import { describe, expect, it } from 'vitest';

import { renderDocument } from './render-document.ts';

global.crypto ??= (crypto as any).webcrypto;

describe('render-document', () => {
describe('renderDocument', () => {
it('renders all info properties', async () => {
Expand Down Expand Up @@ -45,6 +41,18 @@ describe('render-document', () => {
expect(getInfo('bar')).toEqual(PDFHexString.fromText('bar-value'));
});

it('generates file ID', async () => {
const def = { content: [] };

const pdfData = await renderDocument(def, []);

const pdfDoc = await PDFDocument.load(pdfData, { updateMetadata: false });
const fileId = pdfDoc.context.lookup(pdfDoc.context.trailerInfo.ID) as PDFArray;
expect(fileId.size()).toBe(2);
expect(fileId.get(0).toString()).toMatch(/^<[0-9A-F]{64}>$/);
expect(fileId.get(1).toString()).toMatch(/^<[0-9A-F]{64}>$/);
});

it('renders custom data', async () => {
const def = {
content: [],
Expand Down

0 comments on commit a3ba5a3

Please sign in to comment.