Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web/satellite: statically serve Papa Parse worker
Papa Parse, the library we use to parse CSV files in the satellite UI, uses a blob URL for its worker. This isn't allowed by our content security policy, so this change implements a Vite plugin that writes the worker code to a file that is statically served. Change-Id: I0ce58c37b86953a71b7433b789b72fbd8ede313d
- Loading branch information
Showing
11 changed files
with
140 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (C) 2023 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
import { Plugin } from 'vite'; | ||
import { build } from 'esbuild'; | ||
|
||
export default function papaParseWorker(): Plugin { | ||
const name = 'papa-parse-worker'; | ||
const virtualModuleId = 'virtual:' + name; | ||
const resolvedVirtualModuleId = '\0' + virtualModuleId; | ||
|
||
let refId = ''; | ||
|
||
return { | ||
name, | ||
|
||
async buildStart() { | ||
// Trick Papa Parse into thinking it's being imported by RequireJS | ||
// so we can capture the AMD callback. | ||
let factory: (() => unknown) | undefined; | ||
global.define = (_: unknown, callback: () => void) => { | ||
factory = callback; | ||
}; | ||
global.define.amd = true; | ||
await import('papaparse'); | ||
delete global.define; | ||
|
||
if (!factory) { | ||
throw new Error('Failed to capture Papa Parse AMD callback'); | ||
} | ||
|
||
const workerCode = ` | ||
var global = (function() { | ||
if (typeof self !== 'undefined') { return self; } | ||
if (typeof window !== 'undefined') { return window; } | ||
if (typeof global !== 'undefined') { return global; } | ||
return {}; | ||
})(); | ||
global.IS_PAPA_WORKER = true; | ||
(${factory.toString()})();`; | ||
|
||
const result = await build({ | ||
stdin: { | ||
contents: workerCode, | ||
}, | ||
write: false, | ||
minify: true, | ||
}); | ||
|
||
refId = this.emitFile({ | ||
type: 'asset', | ||
name: `papaparse-worker.js`, | ||
source: result.outputFiles[0].text, | ||
}); | ||
}, | ||
|
||
resolveId(id: string) { | ||
if (id === virtualModuleId) return resolvedVirtualModuleId; | ||
}, | ||
|
||
load(id: string) { | ||
if (id === resolvedVirtualModuleId) { | ||
return `export default '__VITE_ASSET__${refId}__';`; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (C) 2023 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
declare module 'virtual:papa-parse-worker' { | ||
const url: string; | ||
export default url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters