Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev/config/src/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export { default as ViteEsmConfig } from "./vite.esm.config.js";
export { default as VitePluginRemoveUnusedTranslations } from "./vite-plugin-remove-unused-translations.js";
export * from "./TsNoCheckPlugin.js";
export { default as VitePluginCopy } from "./vite-plugin-copy.js";
export { default as VitePluginModifyExportsFiles } from "./vite-plugin-modify-exports-files.js";
35 changes: 35 additions & 0 deletions dev/config/src/vite/vite-plugin-modify-exports-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021-2026 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { Plugin } from "vite";

/**
* Empties the generated `exports2.FILES` array in every emitted chunk so that
* bundled file listings are not leaked into the output.
*/
export default function modifyExportsFilesPlugin(): Plugin {
return {
name: "modify-exports-files",
generateBundle(_options, bundle) {
for (const fileName in bundle) {
const chunk = bundle[fileName];
if (chunk && chunk.type === "chunk") {
chunk.code = chunk.code.replace(
/exports2\.FILES = \[[\s\S]*?\];/g,
"exports2.FILES = [];\n",
);

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings starting with 'exports2.FILES = [' and with many repetitions of 'exports2.FILES = ['.
Comment on lines +27 to +30
}
}
},
};
}
24 changes: 24 additions & 0 deletions packages/util/src/arrayBufferToBase64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021-2026 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/** Encodes an ArrayBuffer as a base64 string. */
export function arrayBufferToBase64(arrayBuffer: ArrayBuffer): string {
let binary = "";
const bytes = new Uint8Array(arrayBuffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i] as number);
}
return btoa(binary);
}
16 changes: 16 additions & 0 deletions packages/util/src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* Returns the start (first ms) and end (final ms) of the current calendar month,
* in local time.
*/
export const getCurrentPeriod = (): { start: Date; end: Date } => {
const now = new Date();
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
// set to final millisecond of the day
lastDayOfMonth.setHours(23, 59, 59, 999);
return {
start: firstDayOfMonth,
end: lastDayOfMonth,
};
};

export const getUTCDate = (date: Date) =>
new Date(
Date.UTC(
Expand Down
23 changes: 23 additions & 0 deletions packages/util/src/hasTouchSupport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021-2026 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Classifies the current device as "mobile" or "desktop" based on touch support.
* Browser-only: relies on `window`/`navigator`.
*/
export const hasTouchSupport = (): "mobile" | "desktop" => {
return "ontouchstart" in window || navigator.maxTouchPoints > 0
? "mobile"
: "desktop";
};
3 changes: 3 additions & 0 deletions packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export * from "./choice.js";
export * from "./permutations.js";
export * from "./version.js";
export * from "./hex.js";
export * from "./hammingDistance.js";
export * from "./arrayBufferToBase64.js";
export * from "./hasTouchSupport.js";
export * from "./checks.js";
export * from "./ip.js";
export { verifyRecency } from "./verifyRecency.js";
Expand Down
8 changes: 8 additions & 0 deletions packages/util/src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ const isIPAddress = (hostname: string): boolean => {
}
};

/** Prepends `https://` to a URL that has no http(s) protocol. Empty input is returned unchanged. */
export const prependProtocolToUrl = (url: string): string => {
if (url && url.length > 0 && !url.startsWith("http")) {
return `https://${url}`;
}
return url;
};

export const getURLProtocol = (url: URL) => {
if (!isIPAddress(url.hostname)) {
return "https";
Expand Down