Skip to content
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
1 change: 1 addition & 0 deletions packages/gun-utils/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gun.js
71 changes: 71 additions & 0 deletions packages/gun-utils/builder.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { createRequire } from 'module'
import { readFile, writeFile } from 'fs/promises'
const require = createRequire(import.meta.url)
const files = await Promise.all(
[
require.resolve('gun/gun.js'),
require.resolve('gun/sea'),
require.resolve('gun/lib/radix'),
require.resolve('gun/lib/radisk'),
require.resolve('gun/lib/store'),
require.resolve('gun/lib/rindexed'),
].map((path) => readFile(path, 'utf8')),
)
function init() {
'use strict'
// This log is required by Gun's license.
globalThis.console.log(
'Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!',
)
function setTimeout() {
return globalThis.setTimeout.apply(globalThis, arguments)
}
const location = new URL('https://example.com')
const Object = {
prototype: globalThis.Object.prototype,
keys: globalThis.Object.keys,
create: globalThis.Object.create,
assign: globalThis.Object.assign,
}
const console = { log() {} }
function String() {
return globalThis.String.apply(this, arguments)
}
const window = {
setTimeout,
location,
Object,
console,
String,
TextEncoder: globalThis.TextEncoder,
TextDecoder: globalThis.TextDecoder,
crypto: globalThis.crypto,
get localStorage() {
return globalThis.localStorage
},
get sessionStorage() {
return globalThis.sessionStorage
},
indexedDB: globalThis.indexedDB,
}
try {
// Source Code Here
} finally {
return window
}
}

const patchedSource = files
.join('\n;')
// patch .constructor != Object to .constructor == globalThis.Object
.replace(/!==?\s+Object/g, '!== globalThis.Object')
.replace(/===?\s+Object/g, '=== globalThis.Object')
// patch instanceof Object to instanceof globalThis.Object
.replace(/instanceof\s+Object/g, 'instanceof globalThis.Object')

const result = `
${init.toString().replace('// Source Code Here', patchedSource)};
globalThis.Gun = ${init.name}().Gun;
undefined;
`
writeFile(new URL('./gun.js', import.meta.url), result)
3 changes: 3 additions & 0 deletions packages/gun-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"dependencies": {
"event-iterator": "^2.0.0",
"gun": "^0.2020.1234"
},
"scripts": {
"build": "node ./builder.mjs"
}
}
7 changes: 1 addition & 6 deletions packages/gun-utils/src/instance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import Gun from 'gun'
import 'gun/sea'
import 'gun/lib/radix'
import 'gun/lib/radisk'
import 'gun/lib/store'
import 'gun/lib/rindexed'
declare const Gun: typeof import('gun')
import { gunServers } from './server'

export type GunRoot = ReturnType<typeof createGun>
Expand Down
15 changes: 12 additions & 3 deletions packages/mask/.webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration {
javascript: {
// Treat as missing export as error
strictExportPresence: true,
// gun and @unstoppabledomains/resolution
exprContextCritical: false,
},
},
rules: [
Expand Down Expand Up @@ -203,6 +201,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration {
patterns: [
{ from: join(__dirname, '../public/'), to: distFolder },
{ from: join(__dirname, '../../injected-script/dist/injected-script.js'), to: distFolder },
{ from: join(__dirname, '../../gun-utils/gun.js'), to: distFolder },
{ from: join(__dirname, '../../mask-sdk/dist/mask-sdk.js'), to: distFolder },
{
context: join(__dirname, '../../polyfills/dist/'),
Expand Down Expand Up @@ -307,6 +306,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration {
chunks: ['background'],
filename: 'background.html',
secp256k1: true,
gun: true,
sourceMap: !!sourceMapKind,
}),
)
Expand All @@ -331,14 +331,23 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration {
}
}
}
function addHTMLEntry(options: HTMLPlugin.Options & { secp256k1?: boolean; sourceMap: boolean }) {
function addHTMLEntry(
options: HTMLPlugin.Options & {
secp256k1?: boolean
sourceMap: boolean
gun?: boolean
},
) {
let templateContent = readFileSync(join(__dirname, './template.html'), 'utf8')
if (options.secp256k1) {
templateContent = templateContent.replace(
`<!-- secp256k1 -->`,
'<script src="/polyfill/secp256k1.js"></script>',
)
}
if (options.gun) {
templateContent = templateContent.replace(`<!-- Gun -->`, '<script src="/gun.js"></script>')
}
if (options.sourceMap) {
templateContent = templateContent.replace(
`<!-- CSP -->`,
Expand Down
1 change: 1 addition & 0 deletions packages/mask/.webpack/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<!-- CSP -->
<script src="/patches.js"></script>
<!-- secp256k1 -->
<!-- Gun -->
<script src="/polyfill/ecmascript.js"></script>
<script src="/polyfill/intl.js"></script>
<script src="/polyfill/dom.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion packages/mask/public/manifest-v3.entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
},
})
}
importScripts('./worker_env.js', './polyfill/browser-polyfill.js', './js/background.js')
importScripts('./worker_env.js', './gun.js', './polyfill/browser-polyfill.js', './js/background.js')
6 changes: 3 additions & 3 deletions packages/scripts/src/extension/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { awaitChildProcess, PKG_PATH, watchTask } from '../utils'
import { buildInjectedScript, watchInjectedScript } from '../projects/injected-scripts'
import { buildMaskSDK, watchMaskSDK } from '../projects/mask-sdk'
import { buildPolyfill } from '../projects/polyfill'
import { buildGun } from '../projects/gun'

const presets = ['chromium', 'firefox', 'android', 'iOS', 'base'] as const
const otherFlags = ['beta', 'insider', 'reproducible', 'profile', 'mv3', 'readonlyCache', 'progress'] as const

export async function extension(f?: Function | ExtensionBuildArgs) {
await buildPolyfill()
await buildInjectedScript()
await buildMaskSDK()
await Promise.all([buildPolyfill(), buildInjectedScript(), buildMaskSDK(), buildGun()])
if (typeof f === 'function') return awaitChildProcess(webpack('build'))
return awaitChildProcess(webpack('build', f))
}
export async function extensionWatch(f?: Function | ExtensionBuildArgs) {
buildPolyfill()
buildGun()
watchInjectedScript()
watchMaskSDK()
if (typeof f === 'function') return awaitChildProcess(webpack('dev'))
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export { reorderSpellcheck } from './spellcheck'
export { buildInjectedScript, watchInjectedScript } from './projects/injected-scripts'
export { buildMaskSDK, watchMaskSDK } from './projects/mask-sdk'
export { buildPolyfill } from './projects/polyfill'
export { buildGun } from './projects/gun'
7 changes: 7 additions & 0 deletions packages/scripts/src/projects/gun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fromNPMTask } from '../utils'
import { resolve } from 'path'
export const [buildGun] = fromNPMTask(
resolve(__dirname, '../../../gun-utils'),
'gun',
'Build gun required for Mask Network.',
)