Skip to content

Commit

Permalink
feat: uuid: add a fallback to the crypto.randomUUID API (#24)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
doberkofler authored Nov 7, 2024
1 parent 8546f88 commit a7418fb
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/dropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit a7418fb

Please sign in to comment.