Git Product home page Git Product logo

web-stream-tools's Introduction

OpenPGP.js BrowserStack Status Join the chat on Gitter

OpenPGP.js is a JavaScript implementation of the OpenPGP protocol. It implements RFC4880 and parts of RFC4880bis.

Table of Contents

Platform Support

  • The dist/openpgp.min.js bundle works well with recent versions of Chrome, Firefox, Safari and Edge.

  • The dist/node/openpgp.min.js bundle works well in Node.js. It is used by default when you require('openpgp') in Node.js.

  • Currently, Chrome, Safari and Edge have partial implementations of the Streams specification, and Firefox has a partial implementation behind feature flags. Chrome is the only browser that implements TransformStreams, which we need, so we include a polyfill for all other browsers. Please note that in those browsers, the global ReadableStream property gets overwritten with the polyfill version if it exists. In some edge cases, you might need to use the native ReadableStream (for example when using it to create a Response object), in which case you should store a reference to it before loading OpenPGP.js. There is also the web-streams-adapter library to convert back and forth between them.

Performance

  • Version 3.0.0 of the library introduces support for public-key cryptography using elliptic curves. We use native implementations on browsers and Node.js when available. Elliptic curve cryptography provides stronger security per bits of key, which allows for much faster operations. Currently the following curves are supported:

    Curve Encryption Signature NodeCrypto WebCrypto Constant-Time
    curve25519 ECDH N/A No No Algorithmically**
    ed25519 N/A EdDSA No No Algorithmically**
    p256 ECDH ECDSA Yes* Yes* If native***
    p384 ECDH ECDSA Yes* Yes* If native***
    p521 ECDH ECDSA Yes* Yes* If native***
    brainpoolP256r1 ECDH ECDSA Yes* No If native***
    brainpoolP384r1 ECDH ECDSA Yes* No If native***
    brainpoolP512r1 ECDH ECDSA Yes* No If native***
    secp256k1 ECDH ECDSA Yes* No If native***

    * when available
    ** the curve25519 and ed25519 implementations are algorithmically constant-time, but may not be constant-time after optimizations of the JavaScript compiler
    *** these curves are only constant-time if the underlying native implementation is available and constant-time

  • Version 2.x of the library has been built from the ground up with Uint8Arrays. This allows for much better performance and memory usage than strings.

  • If the user's browser supports native WebCrypto via the window.crypto.subtle API, this will be used. Under Node.js the native crypto module is used.

  • The library implements the RFC4880bis proposal for authenticated encryption using native AES-EAX, OCB, or GCM. This makes symmetric encryption up to 30x faster on supported platforms. Since the specification has not been finalized and other OpenPGP implementations haven't adopted it yet, the feature is currently behind a flag. Note: activating this setting can break compatibility with other OpenPGP implementations, and also with future versions of OpenPGP.js. Don't use it with messages you want to store on disk or in a database. You can enable it by setting openpgp.config.aeadProtect = true.

    You can change the AEAD mode by setting one of the following options:

    openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.eax // Default, native
    openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.ocb // Non-native
    openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.experimentalGCM // **Non-standard**, fastest
    
  • For environments that don't provide native crypto, the library falls back to asm.js implementations of AES, SHA-1, and SHA-256.

Getting started

Node.js

Install OpenPGP.js using npm and save it in your dependencies:

npm install --save openpgp

And import it as a CommonJS module:

const openpgp = require('openpgp');

Or as an ES6 module, from an .mjs file:

import * as openpgp from 'openpgp';

Deno (experimental)

Import as an ES6 module, using /dist/openpgp.mjs.

import * as openpgp from './openpgpjs/dist/openpgp.mjs';

Browser (webpack)

Install OpenPGP.js using npm and save it in your devDependencies:

npm install --save-dev openpgp

And import it as an ES6 module:

import * as openpgp from 'openpgp';

You can also only import the functions you need, as follows:

import { readMessage, decrypt } from 'openpgp';

Or, if you want to use the lightweight build (which is smaller, and lazily loads non-default curves on demand):

import * as openpgp from 'openpgp/lightweight';

To test whether the lazy loading works, try to generate a key with a non-standard curve:

import { generateKey } from 'openpgp/lightweight';
await generateKey({ curve: 'brainpoolP512r1',  userIDs: [{ name: 'Test', email: '[email protected]' }] });

For more examples of how to generate a key, see Generate new key pair. It is recommended to use curve25519 instead of brainpoolP512r1 by default.

Browser (plain files)

Grab openpgp.min.js from unpkg.com/openpgp/dist, and load it in a script tag:

<script src="openpgp.min.js"></script>

Or, to load OpenPGP.js as an ES6 module, grab openpgp.min.mjs from unpkg.com/openpgp/dist, and import it as follows:

<script type="module">
import * as openpgp from './openpgp.min.mjs';
</script>

To offload cryptographic operations off the main thread, you can implement a Web Worker in your application and load OpenPGP.js from there. For an example Worker implementation, see test/worker/worker_example.js.

TypeScript

Since TS is not fully integrated in the library, TS-only dependencies are currently listed as devDependencies, so to compile the project you’ll need to add @openpgp/web-stream-tools manually (NB: only versions below v0.12 are compatible with OpenPGP.js v5):

npm install --save-dev @openpgp/[email protected]

If you notice missing or incorrect type definitions, feel free to open a PR.

Examples

Here are some examples of how to use OpenPGP.js v5. For more elaborate examples and working code, please check out the public API unit tests. If you're upgrading from v4 it might help to check out the changelog and documentation.

Encrypt and decrypt Uint8Array data with a password

Encryption will use the algorithm specified in config.preferredSymmetricAlgorithm (defaults to aes256), and decryption will use the algorithm used for encryption.

(async () => {
    const message = await openpgp.createMessage({ binary: new Uint8Array([0x01, 0x01, 0x01]) });
    const encrypted = await openpgp.encrypt({
        message, // input as Message object
        passwords: ['secret stuff'], // multiple passwords possible
        format: 'binary' // don't ASCII armor (for Uint8Array output)
    });
    console.log(encrypted); // Uint8Array

    const encryptedMessage = await openpgp.readMessage({
        binaryMessage: encrypted // parse encrypted bytes
    });
    const { data: decrypted } = await openpgp.decrypt({
        message: encryptedMessage,
        passwords: ['secret stuff'], // decrypt with password
        format: 'binary' // output as Uint8Array
    });
    console.log(decrypted); // Uint8Array([0x01, 0x01, 0x01])
})();

Encrypt and decrypt String data with PGP keys

Encryption will use the algorithm preferred by the public (encryption) key (defaults to aes256 for keys generated in OpenPGP.js), and decryption will use the algorithm used for encryption.

const openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp

(async () => {
    // put keys in backtick (``) to avoid errors caused by spaces or tabs
    const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`;
    const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
    const passphrase = `yourPassphrase`; // what the private key is encrypted with

    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });

    const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
        passphrase
    });

    const encrypted = await openpgp.encrypt({
        message: await openpgp.createMessage({ text: 'Hello, World!' }), // input as Message object
        encryptionKeys: publicKey,
        signingKeys: privateKey // optional
    });
    console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'

    const message = await openpgp.readMessage({
        armoredMessage: encrypted // parse armored message
    });
    const { data: decrypted, signatures } = await openpgp.decrypt({
        message,
        verificationKeys: publicKey, // optional
        decryptionKeys: privateKey
    });
    console.log(decrypted); // 'Hello, World!'
    // check signature validity (signed messages only)
    try {
        await signatures[0].verified; // throws on invalid signature
        console.log('Signature is valid');
    } catch (e) {
        throw new Error('Signature could not be verified: ' + e.message);
    }
})();

Encrypt to multiple public keys:

(async () => {
    const publicKeysArmored = [
        `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`,
        `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`
    ];
    const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----`;    // encrypted private key
    const passphrase = `yourPassphrase`; // what the private key is encrypted with
    const plaintext = 'Hello, World!';

    const publicKeys = await Promise.all(publicKeysArmored.map(armoredKey => openpgp.readKey({ armoredKey })));

    const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readKey({ armoredKey: privateKeyArmored }),
        passphrase
    });

    const message = await openpgp.createMessage({ text: plaintext });
    const encrypted = await openpgp.encrypt({
        message, // input as Message object
        encryptionKeys: publicKeys,
        signingKeys: privateKey // optional
    });
    console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
})();

If you expect an encrypted message to be signed with one of the public keys you have, and do not want to trust the decrypted data otherwise, you can pass the decryption option expectSigned = true, so that the decryption operation will fail if no valid signature is found:

(async () => {
    // put keys in backtick (``) to avoid errors caused by spaces or tabs
    const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`;
    const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
    const passphrase = `yourPassphrase`; // what the private key is encrypted with

    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });

    const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
        passphrase
    });

    const encryptedAndSignedMessage = `-----BEGIN PGP MESSAGE-----
...
-----END PGP MESSAGE-----`;

    const message = await openpgp.readMessage({
        armoredMessage: encryptedAndSignedMessage // parse armored message
    });
    // decryption will fail if all signatures are invalid or missing
    const { data: decrypted, signatures } = await openpgp.decrypt({
        message,
        decryptionKeys: privateKey,
        expectSigned: true,
        verificationKeys: publicKey, // mandatory with expectSigned=true
    });
    console.log(decrypted); // 'Hello, World!'
})();

Encrypt symmetrically with compression

By default, encrypt will not use any compression when encrypting symmetrically only (i.e. when no encryptionKeys are given). It's possible to change that behaviour by enabling compression through the config, either for the single encryption:

(async () => {
    const message = await openpgp.createMessage({ binary: new Uint8Array([0x01, 0x02, 0x03]) }); // or createMessage({ text: 'string' })
    const encrypted = await openpgp.encrypt({
        message,
        passwords: ['secret stuff'], // multiple passwords possible
        config: { preferredCompressionAlgorithm: openpgp.enums.compression.zlib } // compress the data with zlib
    });
})();

or by changing the default global configuration:

openpgp.config.preferredCompressionAlgorithm = openpgp.enums.compression.zlib

Where the value can be any of:

  • openpgp.enums.compression.zip
  • openpgp.enums.compression.zlib
  • openpgp.enums.compression.uncompressed (default)

Streaming encrypt Uint8Array data with a password

(async () => {
    const readableStream = new ReadableStream({
        start(controller) {
            controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));
            controller.close();
        }
    });

    const message = await openpgp.createMessage({ binary: readableStream });
    const encrypted = await openpgp.encrypt({
        message, // input as Message object
        passwords: ['secret stuff'], // multiple passwords possible
        format: 'binary' // don't ASCII armor (for Uint8Array output)
    });
    console.log(encrypted); // raw encrypted packets as ReadableStream<Uint8Array>

    // Either pipe the above stream somewhere, pass it to another function,
    // or read it manually as follows:
    for await (const chunk of encrypted) {
        console.log('new chunk:', chunk); // Uint8Array
    }
})();

For more information on using ReadableStreams, see the MDN Documentation on the Streams API.

You can also pass a Node.js Readable stream, in which case OpenPGP.js will return a Node.js Readable stream as well, which you can .pipe() to a Writable stream, for example.

Streaming encrypt and decrypt String data with PGP keys

(async () => {
    const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`; // Public key
    const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----`; // Encrypted private key
    const passphrase = `yourPassphrase`; // Password that private key is encrypted with

    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });

    const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
        passphrase
    });

    const readableStream = new ReadableStream({
        start(controller) {
            controller.enqueue('Hello, world!');
            controller.close();
        }
    });

    const encrypted = await openpgp.encrypt({
        message: await openpgp.createMessage({ text: readableStream }), // input as Message object
        encryptionKeys: publicKey,
        signingKeys: privateKey // optional
    });
    console.log(encrypted); // ReadableStream containing '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'

    const message = await openpgp.readMessage({
        armoredMessage: encrypted // parse armored message
    });
    const decrypted = await openpgp.decrypt({
        message,
        verificationKeys: publicKey, // optional
        decryptionKeys: privateKey
    });
    const chunks = [];
    for await (const chunk of decrypted.data) {
        chunks.push(chunk);
    }
    const plaintext = chunks.join('');
    console.log(plaintext); // 'Hello, World!'
})();

Generate new key pair

ECC keys (smaller and faster to generate):

Possible values for curve are: curve25519, ed25519, p256, p384, p521, brainpoolP256r1, brainpoolP384r1, brainpoolP512r1, and secp256k1. Note that both the curve25519 and ed25519 options generate a primary key for signing using Ed25519 and a subkey for encryption using Curve25519.

(async () => {
    const { privateKey, publicKey, revocationCertificate } = await openpgp.generateKey({
        type: 'ecc', // Type of the key, defaults to ECC
        curve: 'curve25519', // ECC curve name, defaults to curve25519
        userIDs: [{ name: 'Jon Smith', email: '[email protected]' }], // you can pass multiple user IDs
        passphrase: 'super long and hard to guess secret', // protects the private key
        format: 'armored' // output key format, defaults to 'armored' (other options: 'binary' or 'object')
    });

    console.log(privateKey);     // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
    console.log(publicKey);      // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
    console.log(revocationCertificate); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
})();

RSA keys (increased compatibility):

(async () => {
    const { privateKey, publicKey } = await openpgp.generateKey({
        type: 'rsa', // Type of the key
        rsaBits: 4096, // RSA key size (defaults to 4096 bits)
        userIDs: [{ name: 'Jon Smith', email: '[email protected]' }], // you can pass multiple user IDs
        passphrase: 'super long and hard to guess secret' // protects the private key
    });
})();

Revoke a key

Using a revocation certificate:

(async () => {
    const { publicKey: revokedKeyArmored } = await openpgp.revokeKey({
        key: await openpgp.readKey({ armoredKey: publicKeyArmored }),
        revocationCertificate,
        format: 'armored' // output armored keys
    });
    console.log(revokedKeyArmored); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
})();

Using the private key:

(async () => {
    const { publicKey: revokedKeyArmored } = await openpgp.revokeKey({
        key: await openpgp.readKey({ armoredKey: privateKeyArmored }),
        format: 'armored' // output armored keys
    });
    console.log(revokedKeyArmored); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
})();

Sign and verify cleartext messages

(async () => {
    const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`;
    const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
    const passphrase = `yourPassphrase`; // what the private key is encrypted with

    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });

    const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
        passphrase
    });

    const unsignedMessage = await openpgp.createCleartextMessage({ text: 'Hello, World!' });
    const cleartextMessage = await openpgp.sign({
        message: unsignedMessage, // CleartextMessage or Message object
        signingKeys: privateKey
    });
    console.log(cleartextMessage); // '-----BEGIN PGP SIGNED MESSAGE ... END PGP SIGNATURE-----'

    const signedMessage = await openpgp.readCleartextMessage({
        cleartextMessage // parse armored message
    });
    const verificationResult = await openpgp.verify({
        message: signedMessage,
        verificationKeys: publicKey
    });
    const { verified, keyID } = verificationResult.signatures[0];
    try {
        await verified; // throws on invalid signature
        console.log('Signed by key id ' + keyID.toHex());
    } catch (e) {
        throw new Error('Signature could not be verified: ' + e.message);
    }
})();

Create and verify detached signatures

(async () => {
    const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`;
    const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
    const passphrase = `yourPassphrase`; // what the private key is encrypted with

    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });

    const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
        passphrase
    });

    const message = await openpgp.createMessage({ text: 'Hello, World!' });
    const detachedSignature = await openpgp.sign({
        message, // Message object
        signingKeys: privateKey,
        detached: true
    });
    console.log(detachedSignature);

    const signature = await openpgp.readSignature({
        armoredSignature: detachedSignature // parse detached signature
    });
    const verificationResult = await openpgp.verify({
        message, // Message object
        signature,
        verificationKeys: publicKey
    });
    const { verified, keyID } = verificationResult.signatures[0];
    try {
        await verified; // throws on invalid signature
        console.log('Signed by key id ' + keyID.toHex());
    } catch (e) {
        throw new Error('Signature could not be verified: ' + e.message);
    }
})();

Streaming sign and verify Uint8Array data

(async () => {
    var readableStream = new ReadableStream({
        start(controller) {
            controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));
            controller.close();
        }
    });

    const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`;
    const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
    const passphrase = `yourPassphrase`; // what the private key is encrypted with

    const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
        passphrase
    });

    const message = await openpgp.createMessage({ binary: readableStream }); // or createMessage({ text: ReadableStream<String> })
    const signatureArmored = await openpgp.sign({
        message,
        signingKeys: privateKey
    });
    console.log(signatureArmored); // ReadableStream containing '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'

    const verificationResult = await openpgp.verify({
        message: await openpgp.readMessage({ armoredMessage: signatureArmored }), // parse armored signature
        verificationKeys: await openpgp.readKey({ armoredKey: publicKeyArmored })
    });

    for await (const chunk of verificationResult.data) {}
    // Note: you *have* to read `verificationResult.data` in some way or other,
    // even if you don't need it, as that is what triggers the
    // verification of the data.

    try {
        await verificationResult.signatures[0].verified; // throws on invalid signature
        console.log('Signed by key id ' + verificationResult.signatures[0].keyID.toHex());
     } catch (e) {
        throw new Error('Signature could not be verified: ' + e.message);
    }
})();

Documentation

The full documentation is available at openpgpjs.org.

Security Audit

To date the OpenPGP.js code base has undergone two complete security audits from Cure53. The first audit's report has been published here.

Security recommendations

It should be noted that js crypto apps deployed via regular web hosting (a.k.a. host-based security) provide users with less security than installable apps with auditable static versions. Installable apps can be deployed as a Firefox or Chrome packaged app. These apps are basically signed zip files and their runtimes typically enforce a strict Content Security Policy (CSP) to protect users against XSS. This blogpost explains the trust model of the web quite well.

It is also recommended to set a strong passphrase that protects the user's private key on disk.

Development

To create your own build of the library, just run the following command after cloning the git repo. This will download all dependencies, run the tests and create a minified bundle under dist/openpgp.min.js to use in your project:

npm install && npm test

For debugging browser errors, you can run npm start and open http://localhost:8080/test/unittests.html in a browser, or run the following command:

npm run browsertest

How do I get involved?

You want to help, great! It's probably best to send us a message on Gitter before you start your undertaking, to make sure nobody else is working on it, and so we can discuss the best course of action. Other than that, just go ahead and fork our repo, make your changes and send us a pull request! :)

License

GNU Lesser General Public License (3.0 or any later version). Please take a look at the LICENSE file for more information.

web-stream-tools's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

web-stream-tools's Issues

Configure Reader stream chunk size of 64kB

// <input type="file" multiple>
document.querySelector("input[type=file]").addEventListener('change', encrypt, false);

function encrypt(e) {
    var file = e.target.files[0];
    var { message } = await openpgp.encrypt({
        message: openpgp.message.fromBinary(file.stream()),
        publicKeys: [KEYS],
        armor: false
    }),
        _reader = openpgp.stream.getReader(message.packets.write()),
        total = 0;
    console.time("counter");
    while (true) {
        const  { value, done } = await _reader.read();
        if (value) {
            total += value.length;
            console.log("Read %d bytes", value.length);
        }
        if (done) break;
    }
    console.timeEnd("counter");
    console.log("Total bytes read: %d", total);
}

Output when encrypting a 8MB file:

Read 3 bytes
Read 268 bytes
Read 3 bytes
Read 524 bytes
Read 1 bytes
Read 65537 bytes          (128 times)
Read 185 bytes
counter: 418 ms
Total bytes read: 8389720

Seems that a 64kB + 1 size is set by default.
How to configure this chunk size?

(btw I'm happy openpgp.js could encrypt 512MB in ~ 22 seconds using less than 11 MB of memory)

Cannot run in typescript

To reproduce:

Attempt to run in typescript:

import stream from '@openpgp/web-stream-tools'

Error:

/Users/bennett/src/shared-infra/node_modules/.pnpm/@[email protected][email protected]/node_modules/@openpgp/web-stream-tools/lib/streams.js:1
import { isNode, isStream, isArrayStream, isUint8Array, concatUint8Array } from './util';
^^^^^^
    
SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1149:20)
    at Module._compile (node:internal/modules/cjs/loader:1190:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Function.Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/bennett/src/shared-infra/apps/shared/src/lib/github-actions-iam-accesskeys.ts:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)

In #1, it was suggested that the right way to consume this module is to import it from openpgp

npm i openpgp
require('openpgp').stream

Is that still the recommended method? openpgp no longer exports stream, see openpgpjs/openpgpjs#1363

Another option might be to add "type: "module" to package.json, and then use a dynamic import, but I'm not sure what else that will affect.

include transpiled ES5 code on npm?

When building/running our TypeScript project which is transpiles into ES5, the TypeScript compiler expects all dependencies to already be also ES5 compatible. And so it trips on import statements etc in this library.

Disclaimer - I don't know much about npm packaging.

Would it be possible to include ES5 build in the npm package? Ideally in a way where you can continue using ES6 for your toolchain that expects ES6, but others could import ES5 when required.

I did resolve our immediate issue by adding a build step that first transpiles the library from node_modules/@openpgp/web-stream-tools ES6 down to an ES5 file that it puts in a local directory, and then our code imports and uses that.

I don't know what the contentions are - I've never had to transpile a library to use it before, so I suppose it's common to include transpiled code. Would that be something that you could consider?

addEventListener support for Reader

Couldn't the reader returned by getReader() implement the FileReader interface or is there another way to "track" reader progress outside a while loop?

var { message } = await openpgp.encrypt({
        message: openpgp.message.fromBinary((new File([], "foo.txt")).stream()),
        publicKeys: [KEYS],
        armor: false
    }),
_reader = openpgp.stream.getReader(message.packets.write());
_reader.addEventListener('progress', event => console.log(`${event.type}: ${event.loaded} bytes transferred`));

Could util:isStream recognize more varieties of node streams?

Hi,

util:isStream only returns 'node' if the input is an instance of stream.Readable, but there are many implementations of such streams that although compatible don't share the same prototype.

One of those cases is Fastify's mock HTTP requests that very conveniently allows tests to run without actually calling server.listen. It took me a long time to understand why all my encryption-enabled unit tests were failing with OpenPGP.js throwing "Error during parsing. This message / key probably does not conform to a valid OpenPGP format", as the same code worked fine outside of Jest.

Piping all requests to a Passthrough eventually fixed my problem, but it would be nice to just let OpenPGP recognize all compatible streams as such.

Could we change the test to something along the lines of:

if (NodeReadableStream &&
   (NodeReadableStream.prototype.isPrototypeOf(input) ||
    input.readable || typeof input._read === 'function')) {
    return 'node';
}

?

NodeReadableStream implies isNode so it seems safe to consider all compatible streams as being of the node kind.

Also (browser console):

> input = new ReadableStream()
> input.readable || typeof input._read === 'function'
> false

I'd be happy to submit a PR if we can agree on a good solution.

Cheers

Cannot run in node

To reproduce:

npm i web-stream-tools
require('web-stream-tools')

Results in this error:

/node_modules/web-stream-tools/lib/streams.js:1
(function (exports, require, module, __filename, __dirname) { import { isStream, isUint8Array, concatUint8Array } from './util';
                                                                     ^

SyntaxError: Unexpected token {

This code made its way into my dependencies by way of openpgpjs. Is this intended to only run in the web?

Using node v10.15.3

Need to properly handle Unit8Array

Browser: Firefox 112

When trying use openpgpjs.readMessage({ armoredMessage: myFile.stream()}) reading a file contain an armor text message (-----BEGIN PGP MESSAGE-----) I always got wrong format error.
After poking around I found out it is because at reader.js line97
value:Unit8Array from filereader turn to a number array litiral string ([56,78,90,133,42])

my temp solution is

+ if(value instanceof Uint8Array){
+      value = new TextDecoder('utf-8').decode(value);
+ }
value += '';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.