Added some helper utils
This commit is contained in:
15
utils/decodeBinary.ts
Normal file
15
utils/decodeBinary.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { decodeRestrictedBase64ToBytes } from "./decodeRestrictedBase64ToBytes";
|
||||||
|
import { isZlibData, decompressZlib } from "./zlib";
|
||||||
|
import { fromByteArray } from "base64-js";
|
||||||
|
|
||||||
|
export async function decodeBinary(input: string): Promise<ArrayBuffer> {
|
||||||
|
let data = decodeRestrictedBase64ToBytes(input);
|
||||||
|
if (isZlibData(data)) {
|
||||||
|
data = await decompressZlib(data);
|
||||||
|
}
|
||||||
|
return data.buffer as ArrayBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function encodeBinary(input: ArrayBuffer): string {
|
||||||
|
return fromByteArray(new Uint8Array(input));
|
||||||
|
}
|
||||||
44
utils/decodeRestrictedBase64ToBytes.ts
Normal file
44
utils/decodeRestrictedBase64ToBytes.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
const base64DecodeMap = [ // starts at 0x2B
|
||||||
|
62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, // 0x3A-0x40
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
||||||
|
19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, // 0x5B-0x0x60
|
||||||
|
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
|
||||||
|
44, 45, 46, 47, 48, 49, 50, 51
|
||||||
|
];
|
||||||
|
|
||||||
|
const base64DecodeMapOffset = 0x2B;
|
||||||
|
const base64EOF = 0x3D;
|
||||||
|
|
||||||
|
export function decodeRestrictedBase64ToBytes(encoded: string) {
|
||||||
|
let ch: any;
|
||||||
|
let code: any;
|
||||||
|
let code2: any;
|
||||||
|
|
||||||
|
const len = encoded.length;
|
||||||
|
const padding = encoded.charAt(len - 2) === "=" ? 2 : encoded.charAt(len - 1) === "=" ? 1 : 0;
|
||||||
|
const decoded = new Uint8Array((encoded.length >> 2) * 3 - padding);
|
||||||
|
|
||||||
|
for (let i = 0, j = 0; i < encoded.length;) {
|
||||||
|
ch = encoded.charCodeAt(i++);
|
||||||
|
code = base64DecodeMap[ch - base64DecodeMapOffset];
|
||||||
|
ch = encoded.charCodeAt(i++);
|
||||||
|
code2 = base64DecodeMap[ch - base64DecodeMapOffset];
|
||||||
|
decoded[j++] = (code << 2) | ((code2 & 0x30) >> 4);
|
||||||
|
|
||||||
|
ch = encoded.charCodeAt(i++);
|
||||||
|
if (ch === base64EOF) {
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
code = base64DecodeMap[ch - base64DecodeMapOffset];
|
||||||
|
decoded[j++] = ((code2 & 0x0f) << 4) | ((code & 0x3c) >> 2);
|
||||||
|
|
||||||
|
ch = encoded.charCodeAt(i++);
|
||||||
|
if (ch === base64EOF) {
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
code2 = base64DecodeMap[ch - base64DecodeMapOffset];
|
||||||
|
decoded[j++] = ((code & 0x03) << 6) | code2;
|
||||||
|
}
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { MessageConnection } from "@codingame/monaco-jsonrpc";
|
import { MessageConnection } from "@codingame/monaco-jsonrpc";
|
||||||
import { MonacoLanguageClient, ErrorAction, CloseAction, createConnection } from "@codingame/monaco-languageclient";
|
import { MonacoLanguageClient, ErrorAction, CloseAction, createConnection } from "@codingame/monaco-languageclient";
|
||||||
|
import Router from "next/router";
|
||||||
import normalizeUrl from "normalize-url";
|
import normalizeUrl from "normalize-url";
|
||||||
import ReconnectingWebSocket from "reconnecting-websocket";
|
import ReconnectingWebSocket from "reconnecting-websocket";
|
||||||
|
|
||||||
@@ -12,7 +13,13 @@ export function createLanguageClient(connection: MessageConnection): MonacoLangu
|
|||||||
// disable the default error handler
|
// disable the default error handler
|
||||||
errorHandler: {
|
errorHandler: {
|
||||||
error: () => ErrorAction.Continue,
|
error: () => ErrorAction.Continue,
|
||||||
closed: () => CloseAction.Restart
|
closed: () => {
|
||||||
|
if (Router.pathname.includes('/develop')) {
|
||||||
|
return CloseAction.Restart
|
||||||
|
} else {
|
||||||
|
return CloseAction.DoNotRestart
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
},
|
},
|
||||||
@@ -30,7 +37,7 @@ export function createUrl(path: string): string {
|
|||||||
return normalizeUrl(`${protocol}://${location.host}${location.pathname}${path}`);
|
return normalizeUrl(`${protocol}://${location.host}${location.pathname}${path}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createWebSocket(url: string): any {
|
export function createWebSocket(url: string) {
|
||||||
const socketOptions = {
|
const socketOptions = {
|
||||||
maxReconnectionDelay: 10000,
|
maxReconnectionDelay: 10000,
|
||||||
minReconnectionDelay: 1000,
|
minReconnectionDelay: 1000,
|
||||||
|
|||||||
59
utils/libwabt.js
Normal file
59
utils/libwabt.js
Normal file
File diff suppressed because one or more lines are too long
10
utils/zlib.ts
Normal file
10
utils/zlib.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
export function isZlibData(data: Uint8Array): boolean {
|
||||||
|
// @ts-expect-error
|
||||||
|
const [firstByte, secondByte] = data;
|
||||||
|
return firstByte === 0x78 && (secondByte === 0x01 || secondByte === 0x9C || secondByte === 0xDA);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function decompressZlib(data: Uint8Array): Promise<Uint8Array> {
|
||||||
|
const { inflate } = await import("pako");
|
||||||
|
return inflate(data);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user