Skip to content

Commit

Permalink
Create MistFetch.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Mistium authored Aug 31, 2024
1 parent ca7cccf commit 757225f
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions files/MistFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
class MistFetch {
constructor() {
this.requests = {};
}

getInfo() {
return {
id: 'mistfetch',
name: 'Mist Fetch',
blocks: [
{
opcode: 'fetchUrlWithId',
blockType: Scratch.BlockType.COMMAND,
text: 'fetch [URL] with ID [ID]',
arguments: {
URL: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'https://example.com'
},
ID: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'request1'
}
}
},
{
opcode: 'getBytesById',
blockType: Scratch.BlockType.REPORTER,
text: 'bytes downloaded for ID [ID]',
arguments: {
ID: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'request1'
}
}
},
{
opcode: 'getResponseBodyById',
blockType: Scratch.BlockType.REPORTER,
text: 'response body for ID [ID]',
arguments: {
ID: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'request1'
}
}
},
{
opcode: 'isRequestCompleted',
blockType: Scratch.BlockType.BOOLEAN,
text: 'is request [ID] completed?',
arguments: {
ID: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'request1'
}
}
},
{
opcode: 'deleteRequestById',
blockType: Scratch.BlockType.COMMAND,
text: 'delete request with ID [ID]',
arguments: {
ID: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'request1'
}
}
}
]
};
}

fetchUrlWithId({ URL, ID }) {
if (this.requests[ID]) {
return `Request with ID ${ID} is already in progress.`;
}

this.requests[ID] = { totalBytes: 0, response: '', completed: false };

fetch(URL)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.body.getReader();
})
.then(reader => {
let done = false;
const decoder = new TextDecoder();
const processStream = async () => {
while (!done) {
const { done: doneReading, value } = await reader.read();
if (doneReading) {
done = true;
this.requests[ID].completed = true;
} else {
this.requests[ID].totalBytes += value.length;
this.requests[ID].response += decoder.decode(value, { stream: true });
}
}
};
return processStream();
})
.catch(error => {
this.requests[ID].error = error.message;
});

return `Started fetching URL with ID: ${ID}`;
}

getBytesById({ ID }) {
if (this.requests[ID]) {
if (this.requests[ID].error) {
return `Error: ${this.requests[ID].error}`;
}
return this.requests[ID].totalBytes;
}
return `No request found for ID: ${ID}`;
}

getResponseBodyById({ ID }) {
if (this.requests[ID]) {
if (this.requests[ID].error) {
return `Error: ${this.requests[ID].error}`;
}
return this.requests[ID].response;
}
return `No request found for ID: ${ID}`;
}

isRequestCompleted({ ID }) {
if (this.requests[ID]) {
return this.requests[ID].completed;
}
return `No request found for ID: ${ID}`;
}

deleteRequestById({ ID }) {
if (this.requests[ID]) {
delete this.requests[ID];
return `Request with ID ${ID} has been deleted.`;
}
return `No request found for ID: ${ID}`;
}
}

// Register the extension
Scratch.extensions.register(new MistFetch());

0 comments on commit 757225f

Please sign in to comment.