Skip to content

Commit bbe1754

Browse files
feat: implement SubtleCrypto.prototype.digest method (#372)
Co-authored-by: Trevor Elliott <[email protected]>
1 parent 576a6b3 commit bbe1754

File tree

20 files changed

+1424
-349
lines changed

20 files changed

+1424
-349
lines changed

.github/workflows/dependencies.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ jobs:
1111
steps:
1212
- uses: actions/[email protected]
1313
with:
14-
allow-licenses: Apache-2.0, MIT, BSD-3-Clause, ISC, BSD-2-Clause, MIT OR (CC0-1.0 AND MIT), CC0-1.0 OR MIT OR (CC0-1.0 AND MIT), CC-BY-3.0, CC0-1.0, MIT OR Apache-2.0, MIT AND Apache-2.0, MIT OR WTFPL, BSD-2-Clause OR (MIT OR Apache-2.0), Python-2.0, ISC AND MIT
14+
allow-licenses: Apache-2.0, MIT, BSD-3-Clause, ISC, BSD-2-Clause, MIT OR (CC0-1.0 AND MIT), CC0-1.0 OR MIT OR (CC0-1.0 AND MIT), CC-BY-3.0, CC0-1.0, MIT OR Apache-2.0, MIT AND Apache-2.0, MIT OR WTFPL, BSD-2-Clause OR (MIT OR Apache-2.0), Python-2.0, ISC AND MIT, Apache-2.0 AND MIT
1515
fail-on-scopes: runtime

.github/workflows/release-please.yml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,17 @@ jobs:
4343
node-version: 18
4444
cache: 'yarn'
4545

46-
- name: Update yarn lock
47-
run: yarn install --mode=update-lockfile
46+
- run: yarn install --mode=update-lockfile
47+
working-directory: ./documentation
48+
- run: npm run docusaurus docs:version ${{ steps.release.outputs.js-compute--tag_name }}
49+
working-directory: ./documentation
50+
- run: DOCUSAURUS_SSR_CONCURRENCY=1 node --max-old-space-size=20480 ./node_modules/.bin/docusaurus build
51+
working-directory: ./documentation
52+
53+
- run: yarn install --mode=update-lockfile
54+
working-directory: ./documentation/app
55+
- run: npm run build:files
56+
working-directory: ./documentation/app
4857

4958
- name: Committing and push changes
5059
run: |
@@ -129,14 +138,16 @@ jobs:
129138
env:
130139
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
131140

132-
- run: cd ./docs-app && yarn
141+
- run: yarn
142+
working-directory: ./documentation/app
133143

134144
- name: Set up Fastly CLI
135145
uses: fastly/compute-actions/setup@v2
136146
with:
137147
token: ${{ secrets.GITHUB_TOKEN }}
138-
cli_version: '4.3.0'
148+
cli_version: '5.0.0'
139149

140-
- run: cd ./docs-app && yarn deploy
150+
- run: yarn deploy
141151
env:
142152
FASTLY_API_TOKEN: ${{secrets.FASTLY_API_TOKEN}}
153+
working-directory: ./documentation/app

c-dependencies/js-compute-runtime/builtins/crypto.cpp

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma clang diagnostic pop
77

88
#include "crypto.h"
9+
#include "subtle-crypto.h"
910
#include "xqd.h"
1011

1112
bool is_int_typed_array(JSObject *obj) {
@@ -24,9 +25,7 @@ namespace builtins {
2425
* of the buffer, but it's far from ideal.
2526
*/
2627
bool Crypto::get_random_values(JSContext *cx, unsigned argc, JS::Value *vp) {
27-
JS::CallArgs args = CallArgsFromVp(argc, vp);
28-
if (!args.requireAtLeast(cx, "crypto.getRandomValues", 1))
29-
return false;
28+
METHOD_HEADER(1)
3029

3130
if (!args[0].isObject() || !is_int_typed_array(&args[0].toObject())) {
3231
JS_ReportErrorUTF8(cx, "crypto.getRandomValues: input must be an integer-typed TypedArray");
@@ -128,7 +127,7 @@ struct UUID {
128127
Set all the other bits to randomly (or pseudo-randomly) chosen values.
129128
*/
130129
bool Crypto::random_uuid(JSContext *cx, unsigned argc, JS::Value *vp) {
131-
JS::CallArgs args = CallArgsFromVp(argc, vp);
130+
METHOD_HEADER(0)
132131
UUID id;
133132
random_get(reinterpret_cast<int32_t>(&id), sizeof(id));
134133

@@ -157,19 +156,50 @@ bool Crypto::random_uuid(JSContext *cx, unsigned argc, JS::Value *vp) {
157156
args.rval().setString(str);
158157
return true;
159158
}
159+
JS::PersistentRooted<JSObject *> Crypto::subtle;
160+
JS::PersistentRooted<JSObject *> crypto;
161+
162+
bool Crypto::subtle_get(JSContext *cx, unsigned argc, JS::Value *vp) {
163+
METHOD_HEADER(0);
164+
if (self != crypto.get()) {
165+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "subtle get",
166+
"Crypto");
167+
return false;
168+
}
169+
170+
args.rval().setObject(*subtle);
171+
return true;
172+
}
160173

161174
const JSFunctionSpec Crypto::methods[] = {
162175
JS_FN("getRandomValues", get_random_values, 1, JSPROP_ENUMERATE),
163176
JS_FN("randomUUID", random_uuid, 0, JSPROP_ENUMERATE), JS_FS_END};
164177

165-
const JSPropertySpec Crypto::properties[] = {JS_PS_END};
178+
const JSPropertySpec Crypto::properties[] = {
179+
JS_PSG("subtle", subtle_get, JSPROP_ENUMERATE),
180+
JS_STRING_SYM_PS(toStringTag, "Crypto", JSPROP_READONLY), JS_PS_END};
166181

167-
bool Crypto::create(JSContext *cx, JS::HandleObject global) {
168-
JS::RootedObject crypto(cx, JS_NewPlainObject(cx));
169-
if (!crypto)
182+
bool Crypto::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
183+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_ILLEGAL_CTOR);
184+
return false;
185+
}
186+
187+
bool Crypto::init_class(JSContext *cx, JS::HandleObject global) {
188+
if (!init_class_impl(cx, global)) {
170189
return false;
171-
if (!JS_DefineProperty(cx, global, "crypto", crypto, JSPROP_ENUMERATE))
190+
}
191+
192+
JS::RootedObject cryptoInstance(
193+
cx, JS_NewObjectWithGivenProto(cx, &Crypto::class_, Crypto::proto_obj));
194+
if (!cryptoInstance) {
172195
return false;
173-
return JS_DefineFunctions(cx, crypto, Crypto::methods);
196+
}
197+
crypto.init(cx, cryptoInstance);
198+
199+
JS::RootedObject subtleCrypto(
200+
cx, JS_NewObjectWithGivenProto(cx, &SubtleCrypto::class_, SubtleCrypto::proto_obj));
201+
subtle.init(cx, subtleCrypto);
202+
203+
return JS_DefineProperty(cx, global, "crypto", crypto, JSPROP_ENUMERATE);
174204
}
175205
} // namespace builtins

c-dependencies/js-compute-runtime/builtins/crypto.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55

66
namespace builtins {
77

8-
class Crypto : public BuiltinNoConstructor<Crypto> {
8+
class Crypto : public BuiltinImpl<Crypto> {
99
private:
1010
public:
1111
static constexpr const char *class_name = "Crypto";
12+
static const int ctor_length = 0;
13+
14+
static JS::PersistentRooted<JSObject *> subtle;
15+
1216
enum Slots { Count };
1317
static const JSFunctionSpec methods[];
1418
static const JSPropertySpec properties[];
1519

20+
static bool subtle_get(JSContext *cx, unsigned argc, JS::Value *vp);
1621
static bool get_random_values(JSContext *cx, unsigned argc, JS::Value *vp);
1722
static bool random_uuid(JSContext *cx, unsigned argc, JS::Value *vp);
18-
static bool create(JSContext *cx, JS::HandleObject global);
23+
24+
static bool constructor(JSContext *cx, unsigned argc, JS::Value *vp);
25+
static bool init_class(JSContext *cx, JS::HandleObject global);
1926
};
2027

2128
} // namespace builtins
2229

23-
#endif
30+
#endif

0 commit comments

Comments
 (0)