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

"ReferenceError: process is not defined" When running in Browser/Deno/WebContainers/esbuild/Webpack 5/Vite #1882

Closed
FossPrime opened this issue Apr 20, 2022 · 4 comments
Assignees
Labels
api: storage Issues related to the googleapis/nodejs-storage API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. web

Comments

@FossPrime
Copy link

FossPrime commented Apr 20, 2022

Thanks for stopping by to let us know something could be better!

  1. Is this a client library issue or a product issue? Yes.

  2. Did someone already solve this? No.

Environment details

  • Node.js version: 16
  • npm version: 7.17
  • @google-cloud/storage version: 5.19.2

Steps to reproduce

  1. Attempt to run the upload from memory example on the browser.

Expected

Running from the browser should work as long as the user has a service account key.

Actual

A mysterious dependency on process arises... Why would you need to access the process to write from a JavaScript string.

image

And after implementing the googleapis/gaxios#337 (comment) workaround or googleapis/gaxios#483

Screenshot 2022-04-20 11 55 23
safe-buffer depends on the non-WHATWG Node.js built-in Buffer module.

Example Code:

Runnable in https://vite.new/vite-ts

import { getSecrets } from './secrets'
import { Storage } from '@google-cloud/storage'
import './style.css'

const bucketName = 'my-bucket'
const contents = 'these are my file contents'
const destFileName = `file-${Date.now}.txt`

class UI {
  promise: Promise<void>
  appEl: HTMLDivElement
  storage!: Storage
  gscKey: any
  secrets: any

  constructor() {
    this.appEl = document.querySelector<HTMLDivElement>('#app')!
    this.promise = this.main()
    this.appEl.innerHTML = `<h1>Hello Vite!</h1>`
  }

  async main() {
    this.secrets = await getSecrets()
    this.gscKey = this.secrets.GCS_KEY
    this.storage = new Storage({
      projectId: this.gscKey.project_id,
      credentials: this.gscKey
    })
    await this.uploadFromMemory()
  }

  async uploadFromMemory() {
    await this.storage.bucket(bucketName).file(destFileName).save(contents)

    console.log(
      `${destFileName} with contents ${contents} uploaded to ${bucketName}.`
    )
  }
}

new UI()

Workaround

  1. Use https://www.npmjs.com/package/@azure/storage-blob instead (Browsers supported since July 2020). You can use cloudflare or an Nginx reverse proxy to mask their URL's in order to make it portable.

Related:

@FossPrime FossPrime added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Apr 20, 2022
@product-auto-label product-auto-label bot added the api: storage Issues related to the googleapis/nodejs-storage API. label Apr 20, 2022
@FossPrime FossPrime changed the title "ReferenceError: process is not defined" When running in Browser/Deno/WebContainers "ReferenceError: process is not defined" When running in Browser/Deno/WebContainers/Vite Apr 20, 2022
@shaffeeullah
Copy link
Contributor

Hi @rayfoss ,

This client library is not currently supported in the browser. We are working toward getting this supported in the future.

@shaffeeullah shaffeeullah added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. web and removed type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Apr 20, 2022
@shaffeeullah shaffeeullah self-assigned this Apr 20, 2022
@shaffeeullah
Copy link
Contributor

I've created a feature request to track this #1883

@FossPrime FossPrime changed the title "ReferenceError: process is not defined" When running in Browser/Deno/WebContainers/Vite "ReferenceError: process is not defined" When running in Browser/Deno/WebContainers/esbuild/Webpack 5/Vite Apr 20, 2022
@shaffeeullah
Copy link
Contributor

shaffeeullah commented Apr 20, 2022

Hi @rayfoss ,
Have you considered using signed URLs to perform this upload? https://cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#storage-signed-url-object-nodejs This would allow you to perform this upload using a PUT request without needing the client library in the the browser

@FossPrime
Copy link
Author

Hi @rayfoss , Have you considered using signed URLs to perform this upload? cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#storage-signed-url-object-nodejs This would allow you to perform this upload using a PUT request without needing the client library in the the browser

Thanks for the suggestion, unfortunately this will not work. I built a tool used by employees that's built in and runs on StackBlitz's Node.js WebContainers. It clones git repositories, manipulates them and publishes to APIs... the last thing I need to do is automate the static asset upload process, which apparently can't be done with Google Cloud Storage at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/nodejs-storage API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. web
Projects
None yet
Development

No branches or pull requests

2 participants