Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create unenv-preset package #96

Merged
merged 3 commits into from
Feb 3, 2025
Merged
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
736 changes: 501 additions & 235 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"README.md",
"package.json",
"packages/**/dist/**/*",
"cli/**/*"
"cli/**/*",
"packages/unenv-preset/src/polyfills/*"
],
"exports": {
".": {
Expand All @@ -87,6 +88,11 @@
"import": "./packages/presets/dist/index.mjs",
"types": "./packages/presets/dist/index.d.ts"
},
"./unenv-preset": {
"require": "./packages/unenv-preset/dist/index.js",
"import": "./packages/unenv-preset/dist/index.mjs",
"types": "./packages/unenv-preset/dist/index.d.ts"
},
"./ai": {
"require": "./packages/ai/dist/index.js",
"import": "./packages/ai/dist/index.mjs",
Expand Down Expand Up @@ -161,6 +167,9 @@
"presets": [
"./packages/presets/dist/index.d.ts"
],
"unenv-preset": [
"./packages/unenv-preset/dist/index.d.ts"
],
"application": [
"./packages/application/dist/index.d.ts"
],
Expand Down
2 changes: 2 additions & 0 deletions packages/unenv-preset/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
Empty file.
10 changes: 10 additions & 0 deletions packages/unenv-preset/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
displayName: 'Unenv Preset',
preset: 'ts-jest',
transform: {
'^.+\\.(t|j)s?$': '@swc/jest',
},
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
testEnvironment: 'node',
};
27 changes: 27 additions & 0 deletions packages/unenv-preset/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@lib/unenv-preset",
"version": "1.0.0",
"description": "Azion Unenv preset.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"scripts": {
"compile": "tsup --config ../../tsup.config.json",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"prettier": "prettier --write .",
"test": "jest --clearCache && jest -c jest.config.js .",
"test:watch": "jest -c jest.config.js . --watch",
"test:coverage": "jest --clearCache && jest -c jest.config.js . --coverage"
},
"author": "aziontech",
"license": "MIT",
"files": [
"dist",
"package.json",
"src/polyfills/*"
],
"dependencies": {
"crypto-browserify": "^3.12.1"
}
}
Empty file.
43 changes: 43 additions & 0 deletions packages/unenv-preset/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const getAbsolutePath = () => path.resolve(__dirname, '../', 'src');

const nextNodePresetPath = `${getAbsolutePath()}/polyfills/node/frameworks/next`;
const polyfillsPath = `${getAbsolutePath()}/polyfills`;

export default {
inject: {
__dirname: `${polyfillsPath}/node/globals/path-dirname.js`,
__filename: `${polyfillsPath}/node/globals/path-filename.js`,
process: `${polyfillsPath}/node/globals/process.cjs`,
},
alias: {
'azion/utils': 'azion/utils',
'@fastly/http-compute-js': '@fastly/http-compute-js',
'next/dist/compiled/etag': `${nextNodePresetPath}/custom-server/12.3.x/util/etag.js`,
accepts: 'accepts',
crypto: `${polyfillsPath}/node/crypto.js`,
events: 'events/events.js',
http: 'stream-http',
module: `${polyfillsPath}/node/module.js`,
stream: 'stream-browserify/',
string_decoder: 'string_decoder/lib/string_decoder.js',
url: 'url/url.js',
util: 'util/util.js',
timers: 'timers-browserify/',
inherits: 'inherits/inherits_browser.js',
vm: 'vm-browserify/',
zlib: 'browserify-zlib',
},
external: ['node:async_hooks', 'node:fs/promises'],
polyfill: [
'aziondev:async_hooks:/async-hooks/async-hooks.polyfills.js',
'aziondev:fs:/fs/fs.polyfills.js',
'aziondev:fs/promises:/fs/promises/promises.polyfills.js',
`azionprd:fs:/fs.js`,
],
};
70 changes: 70 additions & 0 deletions packages/unenv-preset/src/polyfills/node/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable */
import crypto from 'crypto-browserify';

crypto.webcrypto = globalThis.crypto;
export default crypto;
export var { Cipher } = crypto;
export var { Cipheriv } = crypto;
export var { Decipher } = crypto;
export var { Decipheriv } = crypto;
export var { DiffieHellman } = crypto;
export var { DiffieHellmanGroup } = crypto;
export var { Hash } = crypto;
export var { Hmac } = crypto;
export var { Sign } = crypto;
export var { Verify } = crypto;
export var { constants } = crypto;
export var { createCipher } = crypto;
export var { createCipheriv } = crypto;
export var { createCredentials } = crypto;
export var { createDecipher } = crypto;
export var { createDecipheriv } = crypto;
export var { createDiffieHellman } = crypto;
export var { createDiffieHellmanGroup } = crypto;
export var { createECDH } = crypto;
export var { createHash } = crypto;
export var { createHmac } = crypto;
export var { createSign } = crypto;
export var { createVerify } = crypto;
export var { getCiphers } = crypto;
export var { getDiffieHellman } = crypto;
export var { getHashes } = crypto;
export var { listCiphers } = crypto;
export var { pbkdf2 } = crypto;
export var { pbkdf2Sync } = crypto;
export var { privateDecrypt } = crypto;
export var { privateEncrypt } = crypto;
export var { prng } = crypto;
export var { pseudoRandomBytes } = crypto;
export var { publicDecrypt } = crypto;
export var { publicEncrypt } = crypto;
export var { randomBytes } = crypto;
export var { randomFill } = crypto;
export var { randomFillSync } = crypto;
export var { rng } = crypto;
export var { webcrypto } = crypto;

export var getRandomValues = function (abv) {
let l = abv.length;
while (l--) {
const bytes = randomBytes(7);
let randomFloat = (bytes[0] % 32) / 32;

for (let i = 0; i < bytes.length; i++) {
const byte = bytes[i];
randomFloat = (randomFloat + byte) / 256;
}

abv[l] = Math.floor(randomFloat * 256);
}
return abv;
};

export var randomUUID = function () {
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, function (c) {
return (
c ^
(getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
).toString(16);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Azion
* Licensed under the MIT license. See LICENSE file for details.
*
* Portions of this file Copyright Fastly, Inc, licensed under the MIT license. See LICENSE file for details.
*/

import { Buffer } from 'buffer';
import crypto from 'crypto';

/**
* Generates an etag string based on a payload.
* (An adaptation for Compute@Edge of function in Next.js of the same name,
* found at next/server/api-utils/web.ts)
* @param {string} payload data to generate etag
* @returns {string} the generated etag
*/
export default function generateETag(payload) {
if (payload.length === 0) {
// fast-path empty
return '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"';
}

// compute hash of entity
// Do this without using crypto.subtle, as the crypto
// polyfill doesn't support it.
const hash = crypto.createHash('sha1').update(payload, 'utf8').digest('base64').substring(0, 27);

// compute length of entity
const len = Buffer.byteLength(payload);

return `"${len.toString(16)}-${hash}"`;
}
Loading