feat: use scratchFetch

BREAKING CHANGE: servers must accept new header x-projectid
This commit is contained in:
Christopher Willis-Ford 2023-03-13 11:03:02 -07:00
parent c2ee02bc28
commit 9fca9ee583
5 changed files with 47 additions and 29 deletions

49
package-lock.json generated
View file

@ -25,6 +25,7 @@
"scratch-sb1-converter": "0.2.7",
"scratch-translate-extension-languages": "0.0.20191118205314",
"text-encoding": "0.7.0",
"uuid": "8.3.2",
"worker-loader": "^1.1.1"
},
"devDependencies": {
@ -17463,15 +17464,6 @@
"node": ">=8"
}
},
"node_modules/istanbul-lib-processinfo/node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"dev": true,
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/istanbul-lib-processinfo/node_modules/which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
@ -23846,6 +23838,16 @@
}
]
},
"node_modules/request/node_modules/uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.",
"dev": true,
"bin": {
"uuid": "bin/uuid"
}
},
"node_modules/require-directory": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
@ -30520,13 +30522,11 @@
}
},
"node_modules/uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.",
"dev": true,
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "bin/uuid"
"uuid": "dist/bin/uuid"
}
},
"node_modules/v8-compile-cache": {
@ -45947,12 +45947,6 @@
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
"dev": true
},
"uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"dev": true
},
"which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
@ -50769,6 +50763,12 @@
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"dev": true
},
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"dev": true
}
}
},
@ -55771,10 +55771,9 @@
"dev": true
},
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"dev": true
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
},
"v8-compile-cache": {
"version": "2.0.2",

View file

@ -58,6 +58,7 @@
"scratch-sb1-converter": "0.2.7",
"scratch-translate-extension-languages": "0.0.20191118205314",
"text-encoding": "0.7.0",
"uuid": "8.3.2",
"worker-loader": "^1.1.1"
},
"peerDependencies": {

View file

@ -1,5 +1,8 @@
const EventEmitter = require('events');
const {OrderedMap} = require('immutable');
const uuid = require('uuid');
const {setMetadata, RequestMetadata} = require('scratch-storage/src/scratchFetch.js');
const ArgumentType = require('../extension-support/argument-type');
const Blocks = require('./blocks');
@ -401,6 +404,8 @@ class Runtime extends EventEmitter {
this.origin = null;
this._initScratchLink();
this.resetRunId();
}
/**
@ -2015,6 +2020,14 @@ class Runtime extends EventEmitter {
}
}
/**
* Reset the Run ID. Call this any time the project logically starts, stops, or changes identity.
*/
resetRunId () {
const newRunId = uuid.v1();
setMetadata(RequestMetadata.RunId, newRunId);
}
/**
* Start all threads that start with the green flag.
*/
@ -2055,6 +2068,8 @@ class Runtime extends EventEmitter {
}
// Remove all remaining threads from executing in the next tick.
this.threads = [];
this.resetRunId();
}
/**
@ -2458,10 +2473,11 @@ class Runtime extends EventEmitter {
}
/**
* Report that the project has loaded in the Virtual Machine.
* Handle that the project has loaded in the Virtual Machine.
*/
emitProjectLoaded () {
handleProjectLoaded () {
this.emit(Runtime.PROJECT_LOADED);
this.resetRunId();
}
/**

View file

@ -1,3 +1,5 @@
const {scratchFetch} = require('scratch-storage/src/scratchFetch.js');
/**
* Fetch a remote resource like `fetch` does, but with a time limit.
* @param {Request|string} resource Remote resource to fetch.
@ -12,7 +14,7 @@ const fetchWithTimeout = (resource, init, timeout) => {
const signal = controller ? controller.signal : null;
// The fetch call races a timer.
return Promise.race([
fetch(resource, Object.assign({signal}, init)).then(response => {
scratchFetch(resource, Object.assign({signal}, init)).then(response => {
clearTimeout(timeoutID);
return response;
}),

View file

@ -351,7 +351,7 @@ class VirtualMachine extends EventEmitter {
return validationPromise
.then(validatedInput => this.deserializeProject(validatedInput[0], validatedInput[1]))
.then(() => this.runtime.emitProjectLoaded())
.then(() => this.runtime.handleProjectLoaded())
.catch(error => {
// Intentionally rejecting here (want errors to be handled by caller)
if (error.hasOwnProperty('validationError')) {