File size: 3,915 Bytes
b968036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db2d194
b968036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
52
53
54
55
56
57
// Runs apps-script/Code.gs in Node with in-memory Google services.
import { createHash, createHmac, randomUUID } from 'node:crypto';
import { readFileSync } from 'node:fs';
import vm from 'node:vm';

const code = readFileSync(new URL('../apps-script/Code.gs', import.meta.url), 'utf8');
const signed = buffer => [...buffer].map(b => (b > 127 ? b - 256 : b));
const unsigned = bytes => Buffer.from(bytes.map(b => (b + 256) % 256));
const iterate = list => { let i = 0; return { hasNext: () => i < list.length, next: () => list[i++] }; };
class Blob {
  constructor(data, type, name) { this.bytes = typeof data === 'string' ? Buffer.from(data, 'utf8') : unsigned(data); this.type = type; this.name = name; }
  getBytes() { return signed(this.bytes); }
  getDataAsString() { return this.bytes.toString('utf8'); }
  getName() { return this.name; }
  setName(name) { this.name = name; return this; }
  copyBlob() { return new Blob(this.getBytes(), this.type, this.name); }
}
class Folder {
  constructor(name, drive) { this.name = name; this.id = randomUUID(); this.folders = []; this.files = []; drive.set(this.id, this); this.drive = drive; }
  getId() { return this.id; }
  getFoldersByName(name) { return iterate(this.folders.filter(item => item.name === name)); }
  createFolder(name) { const folder = new Folder(name, this.drive); this.folders.push(folder); return folder; }
  getFiles() { return iterate([...this.files]); }
  getFilesByName(name) { return iterate(this.files.filter(item => item.getName() === name)); }
  createFile(nameOrBlob, content) {
    const blob = typeof nameOrBlob === 'string' ? new Blob(content, 'text/plain', nameOrBlob) : nameOrBlob;
    const file = { blob, getName: () => file.blob.getName(), getBlob: () => file.blob, setContent: text => { file.blob = new Blob(text, 'text/plain', file.blob.getName()); } };
    this.files.push(file); return file;
  }
}

export function load(properties = { EXAM_ACCESS_CODE: 'code-secret' }) {
  const props = new Map(Object.entries(properties)), cache = new Map(), drive = new Map(), mails = [];
  const context = vm.createContext({
    console,
    PropertiesService: { getScriptProperties: () => ({ getProperty: key => props.get(key) ?? null, setProperty: (key, value) => props.set(key, value), getKeys: () => [...props.keys()], deleteProperty: key => props.delete(key) }) },
    CacheService: { getScriptCache: () => ({ get: key => cache.get(key) ?? null, put: (key, value) => cache.set(key, value), remove: key => cache.delete(key) }) },
    LockService: { getScriptLock: () => ({ waitLock() {}, releaseLock() {} }) },
    Session: { getEffectiveUser: () => ({ getEmail: () => 'teacher@example.org' }) },
    MailApp: { sendEmail: message => mails.push(message), getRemainingDailyQuota: () => 100 },
    ContentService: { MimeType: { JSON: 'json' }, createTextOutput: text => ({ text, setMimeType() { return this; } }) },
    DriveApp: { createFolder: name => new Folder(name, drive), getFolderById: id => { if (!drive.has(id)) throw new Error('missing'); return drive.get(id); } },
    Utilities: {
      DigestAlgorithm: { SHA_256: 'sha256' }, Charset: { UTF_8: 'utf8' }, getUuid: randomUUID,
      computeDigest: (_, value) => signed(createHash('sha256').update(value, 'utf8').digest()),
      computeHmacSha256Signature: (value, key) => signed(createHmac('sha256', key).update(value, 'utf8').digest()),
      base64EncodeWebSafe: data => (typeof data === 'string' ? Buffer.from(data, 'utf8') : unsigned(data)).toString('base64url'),
      base64DecodeWebSafe: text => signed(Buffer.from(text, 'base64url')),
      newBlob: (data, type, name) => new Blob(data, type, name),
    },
  });
  vm.runInContext(code, context);
  const call = body => JSON.parse(context.doPost({ postData: { contents: JSON.stringify(body) } }).text);
  const root = () => drive.get(props.get('EXAM_FOLDER_ID'));
  return { context, call, mails, props, root };
}