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

Disable shared worker, use a single IPFS instance per app #404

Merged
merged 3 commits into from
Aug 19, 2022
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
/lib
/docs
/node_modules
/src/vendor
/yarn-error.log
/fission-ipfs


# Created by https://www.toptal.com/developers/gitignore/api/yarn,node,elm
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- Make email optional at registration
- Add reset option to bootstrapFileSystem
- Add dependency-injected DNS lookup implementation
- No longer uses a locally-shared IPFS client (was originally using a shared worker). This fixes various error messages you may have seen relating to CIDs.


### v0.32.0

Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{
devShell = pkgs.mkShell {
name = "webnative";
buildInputs = with pkgs; [ nodejs yarn ];
buildInputs = with pkgs; [ nodejs-18_x yarn ];
};
});
}
36 changes: 23 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@
},
"scripts": {
"docs": "rimraf docs && typedoc",
"lint": "yarn eslint src/**/*.ts src/*.ts tests/**/*.ts tests/*.ts",
"lint": "yarn run eslint src/**/*.ts src/*.ts tests/**/*.ts tests/*.ts",
"prebuild": "rimraf lib dist && node scripts/gen-version.js",
"build": "tsc && yarn run build:minified",
"build": "yarn run build:ipfs && tsc && yarn run build:minified",
"build:ipfs": "node scripts/build-ipfs.js && copyfiles --flat src/vendor/*.* lib/vendor/",
"build:minified": "node scripts/build-minified.js",
"start": "tsc -w",
"test": "mocha",
Expand All @@ -66,21 +67,21 @@
"publish-latest": "npm publish --tag latest"
},
"devDependencies": {
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
"@types/expect": "^24.3.0",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.10",
"@types/throttle-debounce": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
"blockstore-core": "^1.0.5",
"datastore-core": "^7.0.0",
"esbuild": "^0.14.11",
"copyfiles": "^2.4.1",
"esbuild": "^0.15.5",
"eslint": "^8.7.0",
"events": "^3.3.0",
"expect": "^27.4.6",
"fast-check": "^2.21.0",
"ipfs-core": "^0.14.3",
"ipfs-core-types": "^0.10.3",
"ipfs-repo": "^14.0.1",
"globby": "^13.1.2",
"ipfs-core": "0.15.5-8f351a89.0",
"madge": "^5.0.1",
"mocha": "^9.1.4",
"puppeteer": "^13.1.1",
Expand All @@ -90,16 +91,25 @@
"typedoc": "^0.22.11",
"typedoc-plugin-missing-exports": "^0.22.6",
"typescript": "^4.5.5",
"yarn": "^1.22.17"
"util": "^0.12.4"
},
"dependencies": {
"@ipld/dag-cbor": "^7.0.0",
"@ipld/dag-pb": "^2.1.15",
"@ipld/dag-cbor": "^7.0.2",
"@ipld/dag-pb": "^2.1.17",
"@libp2p/interface-keys": "^1.0.3",
"@libp2p/peer-id": "^1.1.15",
"@multiformats/multiaddr": "^10.3.3",
"blockstore-core": "^2.0.1",
"blockstore-datastore-adapter": "^3.0.1",
"datastore-core": "^8.0.1",
"datastore-level": "^9.0.1",
"fission-bloom-filters": "1.7.1",
"ipfs-message-port-client": "^0.11.3",
"ipfs-message-port-protocol": "^0.11.3",
"ipfs-core-types": "0.11.2-8f351a89.0",
"ipfs-repo": "^15.0.1",
"ipfs-unixfs": "^6.0.9",
"ipld-dag-pb": "^0.22.3",
"keystore-idb": "^0.15.5",
"load-script": "^2.0.0",
"localforage": "^1.10.0",
"multiformats": "^9.5.4",
"one-webcrypto": "^1.0.3",
Expand Down
31 changes: 31 additions & 0 deletions scripts/build-ipfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill"
import esbuild from "esbuild"


const OUTFILE = "src/vendor/ipfs.js"


console.log("📦 Bundling ipfs-core")

await esbuild.build({
entryPoints: ["node_modules/ipfs-core/src/index.js"],
outfile: OUTFILE,
bundle: true,
minify: false,
sourcemap: true,
platform: "browser",
format: "esm",
target: "es2020",
external: [
"electron-fetch"
],
plugins: [
NodeGlobalsPolyfillPlugin()
],
define: {
"global": "globalThis",
"globalThis.process.env.NODE_ENV": "production"
}
})

console.log(`📝 Bundled ipfs-core into ${OUTFILE} and ${OUTFILE}.map`)
61 changes: 39 additions & 22 deletions scripts/build-minified.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import esbuild from "esbuild"
import fs from "fs"
import zlib from "zlib"
import { globby } from "globby"


const globalName = "webnative"
const outfile = "dist/index.umd.min.js"
const outfileGz = `${outfile}.gz`

// From https://github.com/umdjs/umd/blob/36fd1135ba44e758c7371e7af72295acdebce010/templates/returnExports.js
const umd = {

console.log("📦 Bundling & minifying...")

await esbuild.build({
entryPoints: ["src/index.ts"],
outdir: "dist",
bundle: true,
splitting: true,
minify: true,
sourcemap: true,
platform: "browser",

Choose a reason for hiding this comment

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

will / should this work in a web worker?

i tried

const webnativeCDNURL = 'https://unpkg.com/[email protected]//dist/index.umd.min.js'
const importRes = self.importScripts(webnativeCDNURL)

but i get an ReferenceError: document is not defined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good point, we totally forgot about that. Will fix 👍 Thanks!

format: "esm",
target: "es2020",
globalName,
define: {
"global": "globalThis",
"globalThis.process.env.NODE_ENV": "production"
}
})

const UMD = {
banner:
`(function (root, factory) {
if (typeof define === 'function' && define.amd) {
Expand All @@ -29,34 +47,33 @@ const umd = {
}));`
}

console.log("📦 bundling & minifying...")

esbuild.buildSync({
await esbuild.build({
entryPoints: ["src/index.ts"],
outfile,
outfile: "dist/index.umd.min.js",
bundle: true,
minify: true,
sourcemap: true,
platform: "browser",
format: "iife",
target: "es2020",
globalName,
banner: {
js: umd.banner,
},
footer: {
js: umd.footer,
},
define: {
"global": "globalThis",
"globalThis.process.env.NODE_ENV": "production"
}
})

console.log(`📝 Wrote ${outfile} and ${outfile}.map`)

console.log("💎 compressing into .gz")
;(await globby("dist/*.js")).forEach(jsFile => {
const outfile = jsFile
const outfileGz = `${outfile}.gz`

const fileContents = fs.createReadStream(outfile)
const writeStream = fs.createWriteStream(outfileGz)
const gzip = zlib.createGzip()
console.log(`📝 Wrote ${outfile} and ${outfile}.map`)
console.log("💎 Compressing into .gz")
const fileContents = fs.createReadStream(outfile)
const writeStream = fs.createWriteStream(outfileGz)
const gzip = zlib.createGzip()

fileContents.pipe(gzip).pipe(writeStream)
fileContents.pipe(gzip).pipe(writeStream)

console.log(`📝 Wrote ${outfileGz}`)
console.log(`📝 Wrote ${outfileGz}`)
})
10 changes: 5 additions & 5 deletions src/auth/linking/consumer.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import expect from "expect"
import * as uint8arrays from "uint8arrays"
import { webcrypto } from "one-webcrypto"
import aes from "keystore-idb/lib/aes/index.js"
import config from "keystore-idb/lib/config.js"
import rsa from "keystore-idb/lib/rsa/index.js"
import { CharSize, KeyUse, SymmAlg } from "keystore-idb/lib/types.js"
import utils from "keystore-idb/lib/utils.js"
import aes from "keystore-idb/aes/index.js"
import config from "keystore-idb/config.js"
import rsa from "keystore-idb/rsa/index.js"
import { CharSize, KeyUse, SymmAlg } from "keystore-idb/types.js"
import utils from "keystore-idb/utils.js"

import * as did from "../../../src/did/index.js"
import * as consumer from "./consumer.js"
Expand Down
10 changes: 5 additions & 5 deletions src/auth/linking/consumer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import aes from "keystore-idb/lib/aes/index.js"
import config from "keystore-idb/lib/config.js"
import rsa from "keystore-idb/lib/rsa/index.js"
import utils from "keystore-idb/lib/utils.js"
import { KeyUse, SymmAlg } from "keystore-idb/lib/types.js"
import aes from "keystore-idb/aes/index.js"
import config from "keystore-idb/config.js"
import rsa from "keystore-idb/rsa/index.js"
import utils from "keystore-idb/utils.js"
import { KeyUse, SymmAlg } from "keystore-idb/types.js"

import { webcrypto } from "one-webcrypto"
import * as uint8arrays from "uint8arrays"
Expand Down
12 changes: 6 additions & 6 deletions src/auth/linking/producer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import expect from "expect"
import * as fc from "fast-check"
import { webcrypto } from "one-webcrypto"
import * as uint8arrays from "uint8arrays"
import aes from "keystore-idb/lib/aes/index.js"
import { SymmAlg } from "keystore-idb/lib/types.js"
import utils from "keystore-idb/lib/utils.js"
import aes from "keystore-idb/aes/index.js"
import { SymmAlg } from "keystore-idb/types.js"
import utils from "keystore-idb/utils.js"

import * as did from "../../../src/did/index.js"
import * as producer from "./producer.js"
Expand Down Expand Up @@ -217,7 +217,7 @@ describe("delegate account", async () => {
beforeEach(async () => {
sessionKey = await aes.makeKey({ alg: SymmAlg.AES_GCM, length: 256 })
accountDelegated = null
approvedMessage = null
approvedMessage = null
})

it("delegates an account", async () => {
Expand Down Expand Up @@ -247,13 +247,13 @@ describe("decline delegation", async () => {
approvedMessage = approved

if (link.linkStatus === "DENIED") {
accountDelegated = false
accountDelegated = false
}
}

beforeEach(async () => {
sessionKey = await aes.makeKey({ alg: SymmAlg.AES_GCM, length: 256 })
accountDelegated = null
accountDelegated = null
approvedMessage = null
})

Expand Down
30 changes: 15 additions & 15 deletions src/auth/linking/producer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import aes from "keystore-idb/lib/aes/index.js"
import rsa from "keystore-idb/lib/rsa/index.js"
import utils from "keystore-idb/lib/utils.js"
import { KeyUse, SymmAlg, HashAlg, CharSize } from "keystore-idb/lib/types.js"
import aes from "keystore-idb/aes/index.js"
import rsa from "keystore-idb/rsa/index.js"
import utils from "keystore-idb/utils.js"
import { KeyUse, SymmAlg, HashAlg, CharSize } from "keystore-idb/types.js"

import { webcrypto } from "one-webcrypto"
import * as uint8arrays from "uint8arrays"
Expand Down Expand Up @@ -150,10 +150,10 @@ export const createProducer = async (options: { username: string }): Promise<Acc

/**
* BROADCAST
*
*
* Generate a session key and prepare a session key message to send to the consumer.
*
* @param didThrowaway
*
* @param didThrowaway
* @returns session key and session key message
*/
export const generateSessionKey = async (didThrowaway: string): Promise<{ sessionKey: CryptoKey; sessionKeyMessage: string }> => {
Expand Down Expand Up @@ -203,10 +203,10 @@ export const generateSessionKey = async (didThrowaway: string): Promise<{ sessio

/**
* NEGOTIATION
*
*
* Decrypt the user challenge and the consumer audience DID.
*
* @param data
*
* @param data
* @returns pin and audience
*/
export const handleUserChallenge = async (sessionKey: CryptoKey, data: string): Promise<Result<{ pin: number[]; audience: string }, Error>> => {
Expand Down Expand Up @@ -258,12 +258,12 @@ export const handleUserChallenge = async (sessionKey: CryptoKey, data: string):
/**
* DELEGATION: Delegate account
*
* Request delegation from the dependency injected delegateAccount function.
* Request delegation from the dependency injected delegateAccount function.
* Prepare a delegation message to send to the consumer.
*
* @param sesionKey
*
* @param sesionKey
* @param audience
* @param finishDelegation
* @param finishDelegation
*/
export const delegateAccount = async (
sessionKey: CryptoKey,
Expand Down Expand Up @@ -298,7 +298,7 @@ export const delegateAccount = async (
* DELEGATION: Decline delegation
*
* Prepare a delegation declined message to send to the consumer.
*
*
* @param sessionKey
* @param finishDelegation
*/
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tweetnacl from "tweetnacl"
import rsaOperations from "keystore-idb/lib/rsa/index.js"
import utils from "keystore-idb/lib/utils.js"
import rsaOperations from "keystore-idb/rsa/index.js"
import utils from "keystore-idb/utils.js"

import { Implementation } from "./implementation/types.js"
import { assertBrowser } from "../common/browser.js"
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as uint8arrays from "uint8arrays"
import { webcrypto } from "one-webcrypto"
import { SymmAlg } from "keystore-idb/lib/types.js"
import { importKey, encryptBytes, decryptBytes, makeKey, exportKey } from "keystore-idb/lib/aes/index.js"
import { SymmKeyLength } from "keystore-idb/lib/types.js"
import { SymmAlg } from "keystore-idb/types.js"
import { importKey, encryptBytes, decryptBytes, makeKey, exportKey } from "keystore-idb/aes/index.js"
import { SymmKeyLength } from "keystore-idb/types.js"

import { impl } from "./implementation.js"

Expand Down
2 changes: 1 addition & 1 deletion src/did/transformers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as uint8arrays from "uint8arrays"
import * as utils from "keystore-idb/lib/utils.js"
import * as utils from "keystore-idb/utils.js"

import { BASE58_DID_PREFIX, magicBytes, parseMagicBytes } from "./util.js"
import { KeyType } from "./types.js"
Expand Down
2 changes: 1 addition & 1 deletion src/did/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as crypto from "../crypto/index.js"
import * as utils from "keystore-idb/lib/utils.js"
import * as utils from "keystore-idb/utils.js"
import { didToPublicKey } from "./transformers.js"
import { KeyType } from "./types.js"

Expand Down
2 changes: 1 addition & 1 deletion src/fs/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cbor from "@ipld/dag-cbor"
import * as uint8arrays from "uint8arrays"
import { CID } from "multiformats/cid"
import { SymmAlg } from "keystore-idb/lib/types.js"
import { SymmAlg } from "keystore-idb/types.js"
import { throttle } from "throttle-debounce"

import { Links } from "./types.js"
Expand Down
Loading