Is this the correct method to accomplish this task?
Affirmative.
In further elaboration:
Presently, my approach involves checking if the URL commences with 'blob:'
.
Is this the appropriate technique? Are there alternative methods available?
To determine conclusively, referring to the specification outlining how URL.revokeObjectURL(url)
should function is recommended
(emphasis added, and relevant sub-instructions are included)
https://w3c.github.io/FileAPI/#dfn-revokeObjectURL
The revokeObjectURL(url)
static method must execute these steps:
- Identify
url record
as the result of parsing url
.
- If
url record
’s scheme is not "blob
", stop.
- Determine
origin
as the origin of url record
.
- Determine
settings
as the current settings object.
- If
origin
does not match settings
’s origin, stop.
- Remove an entry from the Blob URL Store concerning
url
:
- Designate store as the user agent’s blob URL store;
- Define url string as the outcome of serializing url.
- Remove store
url string
.
- Eradicate all entries from the
map
that satisfy a specific condition (i.e. match url
)
- ...or do nothing if none exist.
Additionally, it is worth noting that the specification mentions:
This implies that rather than causing an error, attempting to revoke an unregistered URL will fail silently. Browser software may display a message in the error console in such cases.
Therefore, the deduction can be made:
- If the Scheme of a URI is
blob:
, or if a string
URL begins with blob:
, then it constitutes a Blob URL.
- ...thus, it is possible for it to be revoked by a script (from the same origin) invoking
URL.revokeObjectURL(url)
.
- Employing
URL.revokeObjectURL(url)
with an invalid URL will have no adverse effects (such as triggering an Error
or other runtime exception). Examples of invalid URLs include:
- Non-
blob:
scheme URLs (due to step 2)
- URLs from different origins (due to step 5)
- Previously revoked ObjectURLs (Due to step 6.3.2)
- Syntactically valid, same-origin
blob:
URLs with false UUIDs (Also due to step 6.3.2)
Addendum: Regular Expression for Matching blob:
Object URLs:
The Web File API specification provides a precise format for blob:
URIs, enabling them to be matched using a regular-expression RegExp
(assuming no alterations to the specification):
- Assign the empty string to
result
.
- Append the text "
blob:
" to result
.
- Specify
settings
as the current settings object
- Set
origin
as the origin within settings.
- Compute
serialized
as the ASCII representation of origin.
- If
serialized
equals "null", adjust it to a value decided by implementation.
- Add serialized to
result
.
- Append
U+0024
SOLIDUS (/) to result
.
- Create a UUID RFC4122 which is represented as a string and attach it to
result
.
- Return
result
Hence, the corresponding RegExp
in JS would be as follows (derived from this RegExp used for matching HTTP Origin strings):
const blobObjectUrlRegex = /^blob:(?<origin>[\w\+]+:\/\/(?=.{1,254}(?::|$))(?:(?!\d|-)(?![a-z0-9\-]{1,62}-(?:\.|:|$))[a-z0-9\-]{1,63}\b(?!\.$)\.?)+(:\d+)?)\/(?<uuid>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/;
function isBlobOrObjectUrl( url ) {
return ( typeof url === 'string' ) && blobObjectUrlRegex.test( url );
}
Demonstration:
const objectUrlExample = 'blob:https://example.org/40a5fb5a-d56d-4a33-b4e2-0acf6a8e5f64';
const regex = /^blob:(?<origin>[\w\+]+:\/\/(?=.{1,254}(?::|$))(?:(?!\d|-)(?![a-z0-9\-]{1,62}-(?:\.|:|$))[a-z0-9\-]{1,63}\b(?!\.$)\.?)+(:\d+)?)\/(?<uuid>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/;
console.log( "Is \"%s\" an ObjectURL (aka Blob URL)? %o", objectUrlExample, regex.test( objectUrlExample ) ? "Yes" : "No" );
const nonObjectUrlExample = 'https://stackoverflow.com/questions/45941639/proper-way-to-check-if-a-url-is-the-result-of-a-createobjecturl-call';
console.log( "Is \"%s\" an ObjectURL (aka Blob URL)? %o", nonObjectUrlExample, regex.test( nonObjectUrlExample ) ? "Yes" : "No" );