server: change JSON.parse/stringify to structuredClone

structuredClone is more typesafe than using JSON.parse and
JSON.stringify.

Now that Node 18.x is the new baseline, this should be safe to use now.

See https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
for details.
This commit is contained in:
Norm 2022-12-13 16:45:04 -05:00
parent a9d3cae511
commit 78717e85d3
Signed by untrusted user: norm
GPG key ID: 7123E30E441E80DE
3 changed files with 3 additions and 7 deletions

View file

@ -27,9 +27,7 @@ export const DriveFileRepository = db.getRepository(DriveFile).extend({
getPublicProperties(file: DriveFile): DriveFile['properties'] { getPublicProperties(file: DriveFile): DriveFile['properties'] {
if (file.properties.orientation != null) { if (file.properties.orientation != null) {
// TODO const properties = structuredClone(file.properties);
//const properties = structuredClone(file.properties);
const properties = JSON.parse(JSON.stringify(file.properties));
if (file.properties.orientation >= 5) { if (file.properties.orientation >= 5) {
[properties.width, properties.height] = [properties.height, properties.width]; [properties.width, properties.height] = [properties.height, properties.width];
} }

View file

@ -5,7 +5,7 @@ import manifest from './manifest.json' assert { type: 'json' };
export const manifestHandler = async (ctx: Koa.Context): Promise<void> => { export const manifestHandler = async (ctx: Koa.Context): Promise<void> => {
// TODO // TODO
//const res = structuredClone(manifest); //const res = structuredClone(manifest);
const res = JSON.parse(JSON.stringify(manifest)); const res = structuredClone(manifest);
const instance = await fetchMeta(true); const instance = await fetchMeta(true);

View file

@ -96,9 +96,7 @@ export async function deliverToRelays(user: { id: User['id']; host: null; }, act
const relays = await relaysCache.fetch(''); const relays = await relaysCache.fetch('');
if (relays == null || relays.length === 0) return; if (relays == null || relays.length === 0) return;
// TODO const copy = structuredClone(activity);
//const copy = structuredClone(activity);
const copy = JSON.parse(JSON.stringify(activity));
if (!copy.to) copy.to = ['https://www.w3.org/ns/activitystreams#Public']; if (!copy.to) copy.to = ['https://www.w3.org/ns/activitystreams#Public'];
const signed = await attachLdSignature(copy, user); const signed = await attachLdSignature(copy, user);