From a7418fb811b67c25ffd776508bfabd2c1e3cb05a Mon Sep 17 00:00:00 2001 From: Dieter Oberkofler Date: Thu, 7 Nov 2024 12:50:01 +0100 Subject: [PATCH] feat: uuid: add a fallback to the crypto.randomUUID API (#24) In a secureContext, browser's function to generate uuid will be used. This adds a fallback to use a custom local function in insecure contexts (for instance tests). Fix #22 --- src/dropzone.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/dropzone.js b/src/dropzone.js index 56d9d8d28..d50b8c739 100644 --- a/src/dropzone.js +++ b/src/dropzone.js @@ -740,7 +740,7 @@ export default class Dropzone extends Emitter { addFile(file) { file.upload = { // note: this only works if window.isSecureContext is true, which includes localhost in http - uuid: self.crypto.randomUUID(), + uuid: window.isSecureContext ? self.crypto.randomUUID() : Dropzone.uuidv4(), progress: 0, // Setting the total upload size to file.size for the beginning // It's actual different than the size to be transmitted. @@ -1707,6 +1707,17 @@ export default class Dropzone extends Emitter { return this.processQueue(); } } + + static uuidv4() { + return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace( + /[xy]/g, + function (c) { + let r = (Math.random() * 16) | 0, + v = c === "x" ? r : (r & 0x3) | 0x8; + return v.toString(16); + } + ); + } } Dropzone.initClass();