Skip to content

Commit a7418fb

Browse files
authored
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
1 parent 8546f88 commit a7418fb

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/dropzone.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ export default class Dropzone extends Emitter {
740740
addFile(file) {
741741
file.upload = {
742742
// note: this only works if window.isSecureContext is true, which includes localhost in http
743-
uuid: self.crypto.randomUUID(),
743+
uuid: window.isSecureContext ? self.crypto.randomUUID() : Dropzone.uuidv4(),
744744
progress: 0,
745745
// Setting the total upload size to file.size for the beginning
746746
// It's actual different than the size to be transmitted.
@@ -1707,6 +1707,17 @@ export default class Dropzone extends Emitter {
17071707
return this.processQueue();
17081708
}
17091709
}
1710+
1711+
static uuidv4() {
1712+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
1713+
/[xy]/g,
1714+
function (c) {
1715+
let r = (Math.random() * 16) | 0,
1716+
v = c === "x" ? r : (r & 0x3) | 0x8;
1717+
return v.toString(16);
1718+
}
1719+
);
1720+
}
17101721
}
17111722
Dropzone.initClass();
17121723

0 commit comments

Comments
 (0)