@sveltejs/kit 1.0.0-next.49 → 1.0.0-next.490
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -9
- package/package.json +93 -66
- package/scripts/special-types/$env+dynamic+private.md +10 -0
- package/scripts/special-types/$env+dynamic+public.md +8 -0
- package/scripts/special-types/$env+static+private.md +19 -0
- package/scripts/special-types/$env+static+public.md +7 -0
- package/scripts/special-types/$lib.md +5 -0
- package/src/cli.js +112 -0
- package/src/constants.js +7 -0
- package/src/core/adapt/builder.js +206 -0
- package/src/core/adapt/index.js +31 -0
- package/src/core/config/default-error.html +56 -0
- package/src/core/config/index.js +110 -0
- package/src/core/config/options.js +504 -0
- package/src/core/config/types.d.ts +1 -0
- package/src/core/env.js +121 -0
- package/src/core/generate_manifest/index.js +92 -0
- package/src/core/prerender/crawl.js +198 -0
- package/src/core/prerender/entities.js +2252 -0
- package/src/core/prerender/prerender.js +431 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/index.js +467 -0
- package/src/core/sync/create_manifest_data/types.d.ts +37 -0
- package/src/core/sync/sync.js +59 -0
- package/src/core/sync/utils.js +33 -0
- package/src/core/sync/write_ambient.js +53 -0
- package/src/core/sync/write_client_manifest.js +106 -0
- package/src/core/sync/write_matchers.js +25 -0
- package/src/core/sync/write_root.js +91 -0
- package/src/core/sync/write_tsconfig.js +195 -0
- package/src/core/sync/write_types/index.js +678 -0
- package/src/core/utils.js +70 -0
- package/src/exports/hooks/index.js +1 -0
- package/src/exports/hooks/sequence.js +44 -0
- package/src/exports/index.js +45 -0
- package/src/exports/node/index.js +168 -0
- package/src/exports/node/polyfills.js +41 -0
- package/src/exports/vite/build/build_server.js +372 -0
- package/src/exports/vite/build/build_service_worker.js +90 -0
- package/src/exports/vite/build/utils.js +162 -0
- package/src/exports/vite/dev/index.js +576 -0
- package/src/exports/vite/graph_analysis/index.js +277 -0
- package/src/exports/vite/graph_analysis/types.d.ts +5 -0
- package/src/exports/vite/graph_analysis/utils.js +30 -0
- package/src/exports/vite/index.js +598 -0
- package/src/exports/vite/preview/index.js +189 -0
- package/src/exports/vite/types.d.ts +3 -0
- package/src/exports/vite/utils.js +157 -0
- package/src/runtime/app/env.js +1 -0
- package/src/runtime/app/environment.js +11 -0
- package/src/runtime/app/forms.js +108 -0
- package/src/runtime/app/navigation.js +23 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +102 -0
- package/src/runtime/client/ambient.d.ts +26 -0
- package/src/runtime/client/client.js +1581 -0
- package/src/runtime/client/fetcher.js +107 -0
- package/src/runtime/client/parse.js +60 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +37 -0
- package/src/runtime/client/types.d.ts +85 -0
- package/src/runtime/client/utils.js +159 -0
- package/src/runtime/components/error.svelte +16 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/src/runtime/control.js +98 -0
- package/src/runtime/env/dynamic/private.js +1 -0
- package/src/runtime/env/dynamic/public.js +1 -0
- package/src/runtime/env-private.js +6 -0
- package/src/runtime/env-public.js +6 -0
- package/src/runtime/env.js +6 -0
- package/src/runtime/hash.js +16 -0
- package/src/runtime/paths.js +11 -0
- package/src/runtime/server/cookie.js +115 -0
- package/src/runtime/server/data/index.js +136 -0
- package/src/runtime/server/endpoint.js +83 -0
- package/src/runtime/server/index.js +337 -0
- package/src/runtime/server/page/actions.js +243 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +249 -0
- package/src/runtime/server/page/fetch.js +288 -0
- package/src/runtime/server/page/index.js +304 -0
- package/src/runtime/server/page/load_data.js +124 -0
- package/src/runtime/server/page/render.js +342 -0
- package/src/runtime/server/page/respond_with_error.js +104 -0
- package/src/runtime/server/page/serialize_data.js +87 -0
- package/src/runtime/server/page/types.d.ts +41 -0
- package/src/runtime/server/utils.js +179 -0
- package/src/utils/array.js +9 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +46 -0
- package/src/utils/filesystem.js +137 -0
- package/src/utils/functions.js +16 -0
- package/src/utils/http.js +55 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/routing.js +117 -0
- package/src/utils/unit_test.js +11 -0
- package/src/utils/url.js +142 -0
- package/svelte-kit.js +1 -1
- package/types/ambient.d.ts +426 -0
- package/types/index.d.ts +428 -0
- package/types/internal.d.ts +378 -0
- package/types/private.d.ts +209 -0
- package/CHANGELOG.md +0 -476
- package/assets/components/error.svelte +0 -13
- package/assets/runtime/app/env.js +0 -5
- package/assets/runtime/app/navigation.js +0 -44
- package/assets/runtime/app/paths.js +0 -1
- package/assets/runtime/app/stores.js +0 -93
- package/assets/runtime/chunks/utils.js +0 -22
- package/assets/runtime/internal/singletons.js +0 -23
- package/assets/runtime/internal/start.js +0 -773
- package/assets/runtime/paths.js +0 -12
- package/dist/chunks/index.js +0 -3517
- package/dist/chunks/index2.js +0 -587
- package/dist/chunks/index3.js +0 -246
- package/dist/chunks/index4.js +0 -530
- package/dist/chunks/index5.js +0 -763
- package/dist/chunks/index6.js +0 -322
- package/dist/chunks/standard.js +0 -99
- package/dist/chunks/utils.js +0 -83
- package/dist/cli.js +0 -553
- package/dist/ssr.js +0 -2584
- package/types.d.ts +0 -32
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { error, json } from '../../../exports/index.js';
|
|
2
|
+
import { normalize_error } from '../../../utils/error.js';
|
|
3
|
+
import { negotiate } from '../../../utils/http.js';
|
|
4
|
+
import { HttpError, Redirect, ValidationError } from '../../control.js';
|
|
5
|
+
import { handle_error_and_jsonify } from '../utils.js';
|
|
6
|
+
|
|
7
|
+
/** @param {import('types').RequestEvent} event */
|
|
8
|
+
export function is_action_json_request(event) {
|
|
9
|
+
const accept = negotiate(event.request.headers.get('accept') ?? '*/*', [
|
|
10
|
+
'application/json',
|
|
11
|
+
'text/html'
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
return accept === 'application/json' && event.request.method === 'POST';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {import('types').RequestEvent} event
|
|
19
|
+
* @param {import('types').SSROptions} options
|
|
20
|
+
* @param {import('types').SSRNode['server']} server
|
|
21
|
+
*/
|
|
22
|
+
export async function handle_action_json_request(event, options, server) {
|
|
23
|
+
const actions = server.actions;
|
|
24
|
+
|
|
25
|
+
if (!actions) {
|
|
26
|
+
maybe_throw_migration_error(server);
|
|
27
|
+
// TODO should this be a different error altogether?
|
|
28
|
+
return new Response('POST method not allowed. No actions exist for this page', {
|
|
29
|
+
status: 405,
|
|
30
|
+
headers: {
|
|
31
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
|
|
32
|
+
// "The server must generate an Allow header field in a 405 status code response"
|
|
33
|
+
allow: 'GET'
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
check_named_default_separate(actions);
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const data = await call_action(event, actions);
|
|
42
|
+
|
|
43
|
+
if (data instanceof ValidationError) {
|
|
44
|
+
check_serializability(data.data, /** @type {string} */ (event.routeId), 'data');
|
|
45
|
+
return action_json({ type: 'invalid', status: data.status, data: data.data });
|
|
46
|
+
} else {
|
|
47
|
+
check_serializability(data, /** @type {string} */ (event.routeId), 'data');
|
|
48
|
+
return action_json({
|
|
49
|
+
type: 'success',
|
|
50
|
+
status: data ? 200 : 204,
|
|
51
|
+
data: /** @type {Record<string, any> | undefined} */ (data)
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
} catch (e) {
|
|
55
|
+
const error = normalize_error(e);
|
|
56
|
+
|
|
57
|
+
if (error instanceof Redirect) {
|
|
58
|
+
return action_json({
|
|
59
|
+
type: 'redirect',
|
|
60
|
+
status: error.status,
|
|
61
|
+
location: error.location
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!(error instanceof HttpError)) {
|
|
66
|
+
options.handle_error(error, event);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return action_json(
|
|
70
|
+
{
|
|
71
|
+
type: 'error',
|
|
72
|
+
error: handle_error_and_jsonify(event, options, error)
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
status: error instanceof HttpError ? error.status : 500
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param {import('types').ActionResult} data
|
|
83
|
+
* @param {ResponseInit} [init]
|
|
84
|
+
*/
|
|
85
|
+
function action_json(data, init) {
|
|
86
|
+
return json(data, init);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {import('types').RequestEvent} event
|
|
91
|
+
* @param {import('types').SSRNode} leaf_node
|
|
92
|
+
*/
|
|
93
|
+
export function is_action_request(event, leaf_node) {
|
|
94
|
+
return leaf_node.server && event.request.method !== 'GET' && event.request.method !== 'HEAD';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {import('types').RequestEvent} event
|
|
99
|
+
* @param {import('types').SSRNode['server']} server
|
|
100
|
+
* @returns {Promise<import('types').ActionResult>}
|
|
101
|
+
*/
|
|
102
|
+
export async function handle_action_request(event, server) {
|
|
103
|
+
const actions = server.actions;
|
|
104
|
+
|
|
105
|
+
if (!actions) {
|
|
106
|
+
maybe_throw_migration_error(server);
|
|
107
|
+
// TODO should this be a different error altogether?
|
|
108
|
+
event.setHeaders({
|
|
109
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
|
|
110
|
+
// "The server must generate an Allow header field in a 405 status code response"
|
|
111
|
+
allow: 'GET'
|
|
112
|
+
});
|
|
113
|
+
return {
|
|
114
|
+
type: 'error',
|
|
115
|
+
error: error(405, 'POST method not allowed. No actions exist for this page')
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
check_named_default_separate(actions);
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
const data = await call_action(event, actions);
|
|
123
|
+
|
|
124
|
+
if (data instanceof ValidationError) {
|
|
125
|
+
return { type: 'invalid', status: data.status, data: data.data };
|
|
126
|
+
} else {
|
|
127
|
+
return {
|
|
128
|
+
type: 'success',
|
|
129
|
+
status: 200,
|
|
130
|
+
data: /** @type {Record<string, any> | undefined} */ (data)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
} catch (e) {
|
|
134
|
+
const error = normalize_error(e);
|
|
135
|
+
|
|
136
|
+
if (error instanceof Redirect) {
|
|
137
|
+
return {
|
|
138
|
+
type: 'redirect',
|
|
139
|
+
status: error.status,
|
|
140
|
+
location: error.location
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return { type: 'error', error };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @param {import('types').Actions} actions
|
|
150
|
+
*/
|
|
151
|
+
function check_named_default_separate(actions) {
|
|
152
|
+
if (actions.default && Object.keys(actions).length > 1) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`When using named actions, the default action cannot be used. See the docs for more info: https://kit.svelte.dev/docs/form-actions#named-actions`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @param {import('types').RequestEvent} event
|
|
161
|
+
* @param {NonNullable<import('types').SSRNode['server']['actions']>} actions
|
|
162
|
+
* @throws {Redirect | ValidationError | HttpError | Error}
|
|
163
|
+
*/
|
|
164
|
+
export async function call_action(event, actions) {
|
|
165
|
+
const url = new URL(event.request.url);
|
|
166
|
+
|
|
167
|
+
let name = 'default';
|
|
168
|
+
for (const param of url.searchParams) {
|
|
169
|
+
if (param[0].startsWith('/')) {
|
|
170
|
+
name = param[0].slice(1);
|
|
171
|
+
if (name === 'default') {
|
|
172
|
+
throw new Error('Cannot use reserved action name "default"');
|
|
173
|
+
}
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const action = actions[name];
|
|
179
|
+
if (!action) {
|
|
180
|
+
throw new Error(`No action with name '${name}' found`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const type = event.request.headers.get('content-type')?.split('; ')[0];
|
|
184
|
+
if (type !== 'application/x-www-form-urlencoded' && type !== 'multipart/form-data') {
|
|
185
|
+
throw new Error(`Actions expect form-encoded data (received ${type})`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return action(event);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* @param {import('types').SSRNode['server']} server
|
|
193
|
+
*/
|
|
194
|
+
function maybe_throw_migration_error(server) {
|
|
195
|
+
for (const method of ['POST', 'PUT', 'PATCH', 'DELETE']) {
|
|
196
|
+
if (/** @type {any} */ (server)[method]) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
`${method} method no longer allowed in +page.server, use actions instead. See the PR for more info: https://github.com/sveltejs/kit/pull/6469`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Check that the data can safely be serialized to JSON
|
|
206
|
+
* @param {any} value
|
|
207
|
+
* @param {string} id
|
|
208
|
+
* @param {string} path
|
|
209
|
+
*/
|
|
210
|
+
function check_serializability(value, id, path) {
|
|
211
|
+
const type = typeof value;
|
|
212
|
+
|
|
213
|
+
if (type === 'string' || type === 'boolean' || type === 'number' || type === 'undefined') {
|
|
214
|
+
// primitives are fine
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (type === 'object') {
|
|
219
|
+
// nulls are fine...
|
|
220
|
+
if (!value) return;
|
|
221
|
+
|
|
222
|
+
// ...so are plain arrays...
|
|
223
|
+
if (Array.isArray(value)) {
|
|
224
|
+
value.forEach((child, i) => {
|
|
225
|
+
check_serializability(child, id, `${path}[${i}]`);
|
|
226
|
+
});
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ...and objects
|
|
231
|
+
// This simple check might potentially run into some weird edge cases
|
|
232
|
+
// Refer to https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/isPlainObject.js?rgh-link-date=2022-07-20T12%3A48%3A07Z#L30
|
|
233
|
+
// if that ever happens
|
|
234
|
+
if (Object.getPrototypeOf(value) === Object.prototype) {
|
|
235
|
+
for (const key in value) {
|
|
236
|
+
check_serializability(value[key], id, `${path}.${key}`);
|
|
237
|
+
}
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
throw new Error(`${path} returned from action in ${id} cannot be serialized as JSON`);
|
|
243
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SHA-256 hashing function adapted from https://bitwiseshiftleft.github.io/sjcl
|
|
5
|
+
* modified and redistributed under BSD license
|
|
6
|
+
* @param {string} data
|
|
7
|
+
*/
|
|
8
|
+
export function sha256(data) {
|
|
9
|
+
if (!key[0]) precompute();
|
|
10
|
+
|
|
11
|
+
const out = init.slice(0);
|
|
12
|
+
const array = encode(data);
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < array.length; i += 16) {
|
|
15
|
+
const w = array.subarray(i, i + 16);
|
|
16
|
+
|
|
17
|
+
let tmp;
|
|
18
|
+
let a;
|
|
19
|
+
let b;
|
|
20
|
+
|
|
21
|
+
let out0 = out[0];
|
|
22
|
+
let out1 = out[1];
|
|
23
|
+
let out2 = out[2];
|
|
24
|
+
let out3 = out[3];
|
|
25
|
+
let out4 = out[4];
|
|
26
|
+
let out5 = out[5];
|
|
27
|
+
let out6 = out[6];
|
|
28
|
+
let out7 = out[7];
|
|
29
|
+
|
|
30
|
+
/* Rationale for placement of |0 :
|
|
31
|
+
* If a value can overflow is original 32 bits by a factor of more than a few
|
|
32
|
+
* million (2^23 ish), there is a possibility that it might overflow the
|
|
33
|
+
* 53-bit mantissa and lose precision.
|
|
34
|
+
*
|
|
35
|
+
* To avoid this, we clamp back to 32 bits by |'ing with 0 on any value that
|
|
36
|
+
* propagates around the loop, and on the hash state out[]. I don't believe
|
|
37
|
+
* that the clamps on out4 and on out0 are strictly necessary, but it's close
|
|
38
|
+
* (for out4 anyway), and better safe than sorry.
|
|
39
|
+
*
|
|
40
|
+
* The clamps on out[] are necessary for the output to be correct even in the
|
|
41
|
+
* common case and for short inputs.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < 64; i++) {
|
|
45
|
+
// load up the input word for this round
|
|
46
|
+
|
|
47
|
+
if (i < 16) {
|
|
48
|
+
tmp = w[i];
|
|
49
|
+
} else {
|
|
50
|
+
a = w[(i + 1) & 15];
|
|
51
|
+
|
|
52
|
+
b = w[(i + 14) & 15];
|
|
53
|
+
|
|
54
|
+
tmp = w[i & 15] =
|
|
55
|
+
(((a >>> 7) ^ (a >>> 18) ^ (a >>> 3) ^ (a << 25) ^ (a << 14)) +
|
|
56
|
+
((b >>> 17) ^ (b >>> 19) ^ (b >>> 10) ^ (b << 15) ^ (b << 13)) +
|
|
57
|
+
w[i & 15] +
|
|
58
|
+
w[(i + 9) & 15]) |
|
|
59
|
+
0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
tmp =
|
|
63
|
+
tmp +
|
|
64
|
+
out7 +
|
|
65
|
+
((out4 >>> 6) ^ (out4 >>> 11) ^ (out4 >>> 25) ^ (out4 << 26) ^ (out4 << 21) ^ (out4 << 7)) +
|
|
66
|
+
(out6 ^ (out4 & (out5 ^ out6))) +
|
|
67
|
+
key[i]; // | 0;
|
|
68
|
+
|
|
69
|
+
// shift register
|
|
70
|
+
out7 = out6;
|
|
71
|
+
out6 = out5;
|
|
72
|
+
out5 = out4;
|
|
73
|
+
|
|
74
|
+
out4 = (out3 + tmp) | 0;
|
|
75
|
+
|
|
76
|
+
out3 = out2;
|
|
77
|
+
out2 = out1;
|
|
78
|
+
out1 = out0;
|
|
79
|
+
|
|
80
|
+
out0 =
|
|
81
|
+
(tmp +
|
|
82
|
+
((out1 & out2) ^ (out3 & (out1 ^ out2))) +
|
|
83
|
+
((out1 >>> 2) ^
|
|
84
|
+
(out1 >>> 13) ^
|
|
85
|
+
(out1 >>> 22) ^
|
|
86
|
+
(out1 << 30) ^
|
|
87
|
+
(out1 << 19) ^
|
|
88
|
+
(out1 << 10))) |
|
|
89
|
+
0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
out[0] = (out[0] + out0) | 0;
|
|
93
|
+
out[1] = (out[1] + out1) | 0;
|
|
94
|
+
out[2] = (out[2] + out2) | 0;
|
|
95
|
+
out[3] = (out[3] + out3) | 0;
|
|
96
|
+
out[4] = (out[4] + out4) | 0;
|
|
97
|
+
out[5] = (out[5] + out5) | 0;
|
|
98
|
+
out[6] = (out[6] + out6) | 0;
|
|
99
|
+
out[7] = (out[7] + out7) | 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const bytes = new Uint8Array(out.buffer);
|
|
103
|
+
reverse_endianness(bytes);
|
|
104
|
+
|
|
105
|
+
return base64(bytes);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The SHA-256 initialization vector */
|
|
109
|
+
const init = new Uint32Array(8);
|
|
110
|
+
|
|
111
|
+
/** The SHA-256 hash key */
|
|
112
|
+
const key = new Uint32Array(64);
|
|
113
|
+
|
|
114
|
+
/** Function to precompute init and key. */
|
|
115
|
+
function precompute() {
|
|
116
|
+
/** @param {number} x */
|
|
117
|
+
function frac(x) {
|
|
118
|
+
return (x - Math.floor(x)) * 0x100000000;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let prime = 2;
|
|
122
|
+
|
|
123
|
+
for (let i = 0; i < 64; prime++) {
|
|
124
|
+
let is_prime = true;
|
|
125
|
+
|
|
126
|
+
for (let factor = 2; factor * factor <= prime; factor++) {
|
|
127
|
+
if (prime % factor === 0) {
|
|
128
|
+
is_prime = false;
|
|
129
|
+
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (is_prime) {
|
|
135
|
+
if (i < 8) {
|
|
136
|
+
init[i] = frac(prime ** (1 / 2));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
key[i] = frac(prime ** (1 / 3));
|
|
140
|
+
|
|
141
|
+
i++;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** @param {Uint8Array} bytes */
|
|
147
|
+
function reverse_endianness(bytes) {
|
|
148
|
+
for (let i = 0; i < bytes.length; i += 4) {
|
|
149
|
+
const a = bytes[i + 0];
|
|
150
|
+
const b = bytes[i + 1];
|
|
151
|
+
const c = bytes[i + 2];
|
|
152
|
+
const d = bytes[i + 3];
|
|
153
|
+
|
|
154
|
+
bytes[i + 0] = d;
|
|
155
|
+
bytes[i + 1] = c;
|
|
156
|
+
bytes[i + 2] = b;
|
|
157
|
+
bytes[i + 3] = a;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** @param {string} str */
|
|
162
|
+
function encode(str) {
|
|
163
|
+
const encoded = encoder.encode(str);
|
|
164
|
+
const length = encoded.length * 8;
|
|
165
|
+
|
|
166
|
+
// result should be a multiple of 512 bits in length,
|
|
167
|
+
// with room for a 1 (after the data) and two 32-bit
|
|
168
|
+
// words containing the original input bit length
|
|
169
|
+
const size = 512 * Math.ceil((length + 65) / 512);
|
|
170
|
+
const bytes = new Uint8Array(size / 8);
|
|
171
|
+
bytes.set(encoded);
|
|
172
|
+
|
|
173
|
+
// append a 1
|
|
174
|
+
bytes[encoded.length] = 0b10000000;
|
|
175
|
+
|
|
176
|
+
reverse_endianness(bytes);
|
|
177
|
+
|
|
178
|
+
// add the input bit length
|
|
179
|
+
const words = new Uint32Array(bytes.buffer);
|
|
180
|
+
words[words.length - 2] = Math.floor(length / 0x100000000); // this will always be zero for us
|
|
181
|
+
words[words.length - 1] = length;
|
|
182
|
+
|
|
183
|
+
return words;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/*
|
|
187
|
+
Based on https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
|
|
188
|
+
|
|
189
|
+
MIT License
|
|
190
|
+
Copyright (c) 2020 Egor Nepomnyaschih
|
|
191
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
192
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
193
|
+
in the Software without restriction, including without limitation the rights
|
|
194
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
195
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
196
|
+
furnished to do so, subject to the following conditions:
|
|
197
|
+
The above copyright notice and this permission notice shall be included in all
|
|
198
|
+
copies or substantial portions of the Software.
|
|
199
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
200
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
201
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
202
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
203
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
204
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
205
|
+
SOFTWARE.
|
|
206
|
+
*/
|
|
207
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
|
|
208
|
+
|
|
209
|
+
/** @param {Uint8Array} bytes */
|
|
210
|
+
export function base64(bytes) {
|
|
211
|
+
const l = bytes.length;
|
|
212
|
+
|
|
213
|
+
let result = '';
|
|
214
|
+
let i;
|
|
215
|
+
|
|
216
|
+
for (i = 2; i < l; i += 3) {
|
|
217
|
+
result += chars[bytes[i - 2] >> 2];
|
|
218
|
+
result += chars[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
|
|
219
|
+
result += chars[((bytes[i - 1] & 0x0f) << 2) | (bytes[i] >> 6)];
|
|
220
|
+
result += chars[bytes[i] & 0x3f];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (i === l + 1) {
|
|
224
|
+
// 1 octet yet to write
|
|
225
|
+
result += chars[bytes[i - 2] >> 2];
|
|
226
|
+
result += chars[(bytes[i - 2] & 0x03) << 4];
|
|
227
|
+
result += '==';
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (i === l) {
|
|
231
|
+
// 2 octets yet to write
|
|
232
|
+
result += chars[bytes[i - 2] >> 2];
|
|
233
|
+
result += chars[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
|
|
234
|
+
result += chars[(bytes[i - 1] & 0x0f) << 2];
|
|
235
|
+
result += '=';
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return result;
|
|
239
|
+
}
|