Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rwv committed Dec 8, 2023
0 parents commit 0a613f7
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Build

on:
- push
- pull_request

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 18

- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: 8
run_install: true

- run: pnpm run build
27 changes: 27 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Publish NPM Package

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 18
registry-url: https://registry.npmjs.org/

- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: 8
run_install: true

- run: pnpm publish --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
132 changes: 132 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

lib/
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 📦 workbox-add-integrity
A workbox plugin that add integrity to workbox precache manifest.

[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/rwv/workbox-add-integrity/build.yml)](https://github.com/rwv/workbox-add-integrity/actions/workflows/build.yml)
[![npm](https://img.shields.io/npm/v/workbox-add-integrity)](https://www.npmjs.com/package/workbox-add-integrity)
![NPM](https://img.shields.io/npm/l/workbox-add-integrity)

## Usage

``` bash
npm install workbox-add-integrity
```

``` ts
import addIntegrity from "workbox-add-integrity";
import { injectManifest } from "workbox-build";

injectManifest({
manifestTransforms: [addIntegrity("dist")],
});
```

## License

MIT
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "workbox-add-integrity",
"version": "1.0.0",
"description": "A workbox plugin that add integrity to workbox precache manifest.",
"main": "lib/index.js",
"module": "lib/index.js",
"types": "lib/index.d.ts",
"type": "module",
"packageManager": "pnpm@8.10.3",
"scripts": {
"prepublishOnly": "npm run build",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"workbox",
"precache",
"integrity"
],
"author": "rwv",
"license": "MIT",
"devDependencies": {
"@types/node": "^20.10.4",
"typescript": "^5.3.2"
}
}
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

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

79 changes: 79 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import path from "path";
import { createHash } from "crypto";
import { createReadStream } from "fs";

interface ManifestEntry {
integrity?: string;
revision: string | null;
url: string;
}

type ManifestEntryWithSize = ManifestEntry & {
size: number;
};

type ManifestEntries = Array<ManifestEntryWithSize>;

type ManifestTransform = (
manifestEntries: Array<ManifestEntryWithSize>,
compilation?: unknown
) => Promise<ManifestTransformResult> | ManifestTransformResult;

interface ManifestTransformResult {
manifest: Array<ManifestEntryWithSize>;
warnings?: Array<string>;
}

async function addIntegrityRaw(
manifestEntries: ManifestEntries,
folder: string
): Promise<ManifestTransformResult> {
const newManifestEntries = await Promise.all(
manifestEntries.map((entry) => handleEntry(entry, folder))
);

console.log(newManifestEntries);

return { manifest: newManifestEntries };
}

async function handleEntry(
entry: ManifestEntry & {
size: number;
},
folder: string
) {
const filepath = path.join(folder, entry.url);
console.log(filepath);
const integrity = await calculateSRI(filepath);
return {
...entry,
integrity,
};
}

async function calculateSRI(filePath: string): Promise<string> {
const hash = createHash("sha256");
const fileStream = createReadStream(filePath);

return new Promise<string>((resolve, reject) => {
fileStream.on("data", (data) => {
hash.update(data);
});

fileStream.on("end", () => {
const fileHash = hash.digest("base64");
const sriString = `sha256-${fileHash}`;
resolve(sriString);
});

fileStream.on("error", (error) => {
reject(error);
});
});
}

export default function addIntegrity(folder = "dist"): ManifestTransform {
return (manifestEntries: ManifestEntries) =>
addIntegrityRaw(manifestEntries, folder);
}
Loading

0 comments on commit 0a613f7

Please sign in to comment.