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

esbuild #229

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,12 @@ On Amazon S3, this means adding the following line to your bucket policy, inside

```ts
// Set only the base URL by passing a string
penumbra.setWorkerLocation('/penumbra-workers/');
penumbra.setWorkerLocation('/penumbra-workers/worker.penumbra.js');

// Set all worker URLs by passing a WorkerLocation object
penumbra.setWorkerLocation({
base: '/penumbra-workers/',
penumbra: 'worker.penumbra.js',
StreamSaver: 'StreamSaver.js',
});

// Set a single worker's location
Expand Down
54 changes: 54 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const {
NodeGlobalsPolyfillPlugin,
} = require('@esbuild-plugins/node-globals-polyfill');

const { build } = require('esbuild');

/**
* @type {import('esbuild').BuildOptions}
*/
const sharedOptions = {
entryPoints: ['./src/index.ts', './src/worker.penumbra.js'],
sourcemap: true,
bundle: true,
};

/**
* @type {import('esbuild').BuildOptions}
*/
const browserOptions = {
platform: 'browser',
plugins: [
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true,
}),
],
define: {
global: 'globalThis',
},
};

/**
* @type {import('esbuild').BuildOptions}
*/
const cjsOptions = {
...sharedOptions,
...browserOptions,
format: 'iife',
minify: true,
outdir: './dist/cjs/',
globalName: 'penumbra',
};
build(cjsOptions).catch(() => process.exit(1));

/**
* @type {import('esbuild').BuildOptions}
*/
const esmOptions = {
...sharedOptions,
...browserOptions,
format: 'esm',
outdir: './dist/esm/',
};
build(esmOptions).catch(() => process.exit(1));
4 changes: 2 additions & 2 deletions demo/index.html → demo/cjs.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ <h1>Penumbra Tests</h1>
const demo = penumbra.cloneNode();

penumbra.dataset.penumbra = '';
penumbra.dataset.worker = `worker.penumbra.js?${cacheBuster}`;
penumbra.src = `main.penumbra.js?${cacheBuster}`;
penumbra.dataset.worker = `/dist/cjs/worker.penumbra.js?${cacheBuster}`;
penumbra.src = `/dist/cjs/index.js?${cacheBuster}`;
demo.src = `demo.js?${cacheBuster}`;

scripts.appendChild(demo);
Expand Down
5 changes: 2 additions & 3 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const view = self;

const tests = [];
const results = [];
let failures = 0;
const logger = console;

Expand Down Expand Up @@ -55,8 +54,8 @@ const onReady = async (
);
return false;
}
const cacheBuster = Math.random().toString(10).slice(2);
await penumbra.setWorkerLocation(`worker.penumbra.js?${cacheBuster}`);
// const cacheBuster = Math.random().toString(10).slice(2);
// await penumbra.setWorkerLocation(`worker.penumbra.js?${cacheBuster}`);
const NYT = {
url: 'https://s3-us-west-2.amazonaws.com/bencmbrook/NYT.txt.enc',
filePrefix: 'NYT',
Expand Down
26 changes: 26 additions & 0 deletions demo/esm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Penumbra Tests</title>
<meta charset="UTF-8" />
<!--<link rel="stylesheet" type="text/css" href="style.css" />-->
</head>

<body>
<div id="app">
<h1>Penumbra Tests</h1>
</div>
<div>
<p>Open your console (Option+Command+I) to view test results.</p>
</div>
<script type="module">
const cacheBuster = Math.random().toString(10).slice(2);
const { penumbra } = await import(`/dist/esm/index.js?${cacheBuster}`);
// ESM workers blocked by Firefox support
penumbra.setWorkerLocation(`/dist/cjs/worker.penumbra.js?${cacheBuster}`);
const demo = document.createElement('script');
demo.src = `demo.js?${cacheBuster}`;
document.body.appendChild(demo);
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h2>The data below is downloaded and decrypted</h2>
<script src="jsAnimation.js"></script>
<script src="files.js"></script>
<script src="displayTable.js"></script>
<script src="main.penumbra.js" async defer></script>
<script src="/dist/cjs/index.js" async defer></script>
<script src="index.js"></script>
</body>
</html>
3 changes: 0 additions & 3 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable require-jsdoc */

// This is all of the Penumbra-relevant code:
const onReady = async (
{ detail: { penumbra } } = {
Expand Down
5 changes: 0 additions & 5 deletions netlify.toml

This file was deleted.

36 changes: 23 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{
"name": "@transcend-io/penumbra",
"version": "5.3.2",
"version": "6.0.0",
"description": "Crypto streams for the browser.",
"main": "dist/main.penumbra.js",
"types": "ts-build/src/index.d.ts",
"main": "./dist/node/cjs/index.js",
"module": "./dist/node/esm/index.js",
"jsdelivr": "./dist/cjs/index.js",
"unpkg": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts",
"browser": {
"./dist/cjs/index.js": "./dist/cjs/index.js",
"./dist/esm/index.js": "./dist/esm/index.js",
"stream": "stream-browserify"
},
"directories": {
"example": "example"
},
Expand All @@ -21,18 +29,21 @@
"####### Linting #######": "",
"lint": "yarn pnpify eslint src --ext .ts",
"####### Start #########": "",
"start:example": "yarn build:example && http-server example/build/ --port 8080 -o",
"start:demo": "yarn build:demo && http-server demo/build/ --port 8081 -o",
"start:example": "yarn build:dist && open http://localhost:8080/example && http-server --port 8080",
"start:demo": "yarn build:dist && open http://localhost:8081/demo/cjs.html && http-server --port 8081",
"start:demo:esm": "yarn build:dist && open http://localhost:8081/demo/esm.html && http-server --port 8081",
"####### Testing #######": "",
"test:interactive": "yarn start:demo",
"test:interactive:esm": "yarn start:demo:esm",
"test:local": "karma start karma.local.js",
"test:local:rebuild": "yarn && karma start karma.local.js",
"test:browserstack": "karma start karma.browserstack.js",
"####### Build #######": "",
"clean": "rimraf dist && rimraf ts-build && rimraf coverage",
"build": "yarn clean && mkdir -p dist && webpack --config webpack.prod.js && tsc",
"build:example": "rimraf example/build && yarn build && mkdir -p example/build && cp example/*.* example/build/ && cp -a dist/. example/build/",
"build:demo": "rimraf demo/build && yarn build && mkdir -p demo/build && cp demo/*.* demo/build/ && cp -a dist/. demo/build/",
"build": "yarn clean && yarn build:dist && yarn build:types",
"build:dist": "yarn node build.js",
"build:example": "yarn build:dist",
"build:types": "tsc --emitDeclarationOnly --declaration --project tsconfig.json",
"webpack:watch": "yarn clean && webpack --config webpack.dev.js --watch"
},
"repository": {
Expand All @@ -55,7 +66,6 @@
},
"files": [
"dist/**/*",
"ts-build/**/*",
"src/**/*",
"tsconfig.json"
],
Expand All @@ -66,28 +76,28 @@
"comlink": "^4.3.1",
"core-js": "^3.22.8",
"crypto-browserify": "^3.12.0",
"mime-types": "^2.1.35",
"mime": "^3.0.0",
"promise.allsettled": "^1.0.5",
"streamsaver": "^2.0.6",
"typedarray-to-buffer": "^4.0.0",
"web-streams-node": "^0.4.0",
"web-streams-polyfill": "^3.2.1"
},
"devDependencies": {
"@babel/core": "^7.18.2",
"@babel/plugin-proposal-class-properties": "^7.17.12",
"@babel/preset-env": "^7.18.2",
"@babel/preset-typescript": "^7.17.12",
"@types/mime-types": "^2.1.1",
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
"@types/mime": "^2.0.3",
"@types/node": "^17.0.40",
"@types/tape": "^4.13.2",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"@yarnpkg/pnpify": "^3.1.3",
"@yarnpkg/sdks": "^2.6.2",
"babel-loader": "^8.2.5",
"buffer": "^6.0.3",
"depcheck": "^1.4.3",
"esbuild": "^0.14.43",
"eslint": "^8.17.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-import-resolver-typescript": "^2.7.1",
Expand Down
10 changes: 3 additions & 7 deletions src/@types/worker-loader/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/** Webpack worker-loader type setup */
declare module 'worker-loader!*' {
declare module 'web-worker:*' {
/** Worker */
class WebpackWorker extends Worker {
/** Webpack's Worker constructor */
public constructor();
}

export default WebpackWorker;
class PenumbraWebWorker extends Worker {}
export default PenumbraWebWorker;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not in use - can delete

4 changes: 2 additions & 2 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
RemoteReadableStream,
RemoteWritableStream,
} from '@transcend-io/remote-web-streams';
import mime from 'mime-types';
import { getExtension } from 'mime';
import { streamSaver } from './streamsaver';
import { ReadableStream } from './streams';

Expand Down Expand Up @@ -178,7 +178,7 @@ function save(
'stream' in files ? (files as unknown as PenumbraFile) : files[0];
const [
filename,
extension = file.mimetype ? mime.extension(file.mimetype) : '',
extension = file.mimetype ? `.${getExtension(file.mimetype)}` : '',
Copy link
Member Author

@bencmbrook bencmbrook Jun 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lighter mime type lib meant for browsers. also noticed neither library returns with a . prefix which would result in imagepng

] = (fileName || file.filePrefix || DEFAULT_FILENAME)
.split(/(\.\w+\s*$)/) // split filename extension
.filter(Boolean); // filter empty matches
Expand Down
8 changes: 3 additions & 5 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const settings = (
document.currentScript ||
document.querySelector('script[data-penumbra]') ||
({ dataset: {} } as HTMLScriptElement)
).dataset;
export const settings =
(document.currentScript || document.querySelector('script[data-penumbra]'))
?.dataset || ({} as DOMStringMap);
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ export interface WorkerLocation {
base: URL;
/** The location of the Penumbra Worker script */
penumbra: URL;
/** The location of the StreamSaver ServiceWorker script */
StreamSaver: URL;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ export { default as getTextFromRS } from './getTextFromRS';
export { default as toBuff } from './toBuff';
export { default as blobCache } from './blobCache';
export { default as isViewableText } from './isViewableText';
export { default as intoStream } from './intoStream';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused + is the only thing depending on web-streams-node, so we can drop it and the npm lib (which itself requires a bunch of polyfills to support)

export { default as isNumber } from './isNumber';
export { default as throwOutside } from './throwOutside';
27 changes: 0 additions & 27 deletions src/utils/intoStream.ts

This file was deleted.

12 changes: 6 additions & 6 deletions src/workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import { settings } from './settings';
*/
const DEFAULT_WORKERS = {
penumbra: 'worker.penumbra.js',
StreamSaver: 'streamsaver.penumbra.serviceworker.js',
};

const SHOULD_LOG_EVENTS = process.env.PENUMBRA_LOG_START === 'true';
const SHOULD_LOG_EVENTS = process?.env?.PENUMBRA_LOG_START === 'true';

// //// //
// Init //
Expand Down Expand Up @@ -104,14 +103,13 @@ export function getWorkerLocation(): WorkerLocation {
penumbra: settings.worker || DEFAULT_WORKERS.penumbra,
...(typeof config === 'object' ? config : {}),
};
const { base, penumbra, StreamSaver } = options;
const { base, penumbra } = options;

const context = resolve(base || scriptUrl);

return {
base: context,
penumbra: new URL(penumbra, context),
StreamSaver: new URL(StreamSaver, context),
};
}

Expand Down Expand Up @@ -148,8 +146,11 @@ let workerID = 0;
export async function createPenumbraWorker(
url: URL | string,
): Promise<PenumbraWorker> {
const worker = new Worker(url /* , { type: 'module' } */);
const id = workerID++;
const worker = new Worker(url, {
// type: 'module', // waiting on Firefox
name: `penumbra-worker-${id}`,
});
const penumbraWorker: PenumbraWorker = {
worker,
id,
Expand Down Expand Up @@ -267,7 +268,6 @@ view.addEventListener('beforeunload', cleanup);
* penumbra.setWorkerLocation({
* base: '/penumbra-workers/',
* penumbra: 'worker.penumbra.js',
* StreamSaver: 'StreamSaver.js',
* });
* // Set a single worker's location
* penumbra.setWorkerLocation({decrypt: 'penumbra.decrypt.js'});
Expand Down
4 changes: 2 additions & 2 deletions src/zip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-lines */
import allSettled from 'promise.allsettled';
import { Writer } from '@transcend-io/conflux';
import mime from 'mime-types';
import { getExtension } from 'mime';
import { streamSaver } from './streamsaver';
import { PenumbraFile, ZipOptions } from './types';
import { isNumber, emitZipProgress, emitZipCompletion } from './utils';
Expand Down Expand Up @@ -209,7 +209,7 @@ export class PenumbraZipWriter extends EventTarget {
}
const [
filename,
extension = mimetype ? mime.extension(mimetype) : '',
extension = mimetype ? `.${getExtension(mimetype)}` : '',
] = name
.split(/(\.\w+\s*$)/) // split filename extension
.filter(Boolean); // filter empty matches
Expand Down
Loading