Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Building the Settings Audit Layer

Building the Settings Audit Layer

In this lesson, you combine the settings library’s parts into one audit layer for the desk’s settings, { level: 2, label: "desk" }. It audits every read, checks every level write, and keeps each event as a record whose values stay values. And it hands out only the audited object, never the raw record.

The Python version is the reference your port has to match. The Settings class keeps the raw settings in _target. __getattr__() records a read of level or label, names the wrapper doesn’t have. __setattr__() raises a ValueError for a bad level, or stores the value and records the write:

Language: Python
events = []

class Settings:
    def __init__(self, target):
        super().__setattr__("_target", target)

    def __getattr__(self, name):
        events.append(("read", name))
        return self._target[name]

    def __setattr__(self, name, value):
        if name == "level" and value not in range(1, 6):
            raise ValueError(f"bad level: {value}")
        self._target[name] = value
        events.append(("write", name, value))

settings = Settings({"level": 2, "label": "desk"})
settings.level = 4
try:
    settings.level = 9
except ValueError as err:
    print(err)
print(settings.level)
print(events)

In JavaScript, the events go in an array, and a factory builds the settings: a function that creates an object and returns it. This time, createSettings() makes the record itself, so no caller ever holds it, then wraps it in a proxy and returns only the proxy.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll combine the settings library’s parts into one audit layer for the desk’s settings. It audits every read, checks every level write, and keeps each event as a record whose values stay values.

00:14 And it hands out only the audited object, never the raw record. Here’s the Python version your port has to match. The Settings class keeps the raw settings in _target.

00:26 __getattr__() records a read of level or label, names the wrapper doesn’t have. __setattr__() raises a ValueError for a bad level, or stores the value and records the write.

00:41 In JavaScript, the events go in an array, and a factory builds the settings: a function that creates an object and returns it. This time, createSettings() makes the record itself, so no caller ever holds it, then wraps it in a proxy and returns only the proxy.

01:00 The record is a local constant, so no code outside the factory can name it. The proxy keeps it alive: it holds its target and hands it to each trap. Python’s _target asks callers not to touch it.

01:14 JavaScript gives callers no name for it at all.

01:19 The get trap is the read audit from earlier, now inside the factory. Each read pushes an event, built by the audit tag, which keeps the template’s parts and values apart.

01:30 Then Reflect.get() does the default read, with the receiver passed along so a getter would see the proxy as this. Python’s __getattr__() only hears these reads because the wrapper lacks the names.

01:44 The get trap runs for every read through the proxy, found or not.

01:50 The set trap is the write check. isValidLevel() asks for a whole number from 1 to 5, which Python’s range() test checked in one go.

02:00 For a bad level, the trap returns false, its refusal, where Python raised. Otherwise Reflect.set() does the default write, and only a write that succeeded lands in the log, with its key and value.

02:15 Now the script uses the layer. It creates the settings and sets level to 4. Then it asks Reflect.set() for a 9, which answers with a boolean, where assigning would throw.

02:28 Last, it reads level and logs the events. How many events are in the log? Your turn: pause the video and predict.

02:41 Two events: the write of 4, then the read of level.

02:47 The refused 9 left none: the script logged false for it, and level is still 4.

02:55 Python prints the same story. Its first line is the ValueError’s message, where JavaScript’s caller got false, and a plain assignment would have thrown a TypeError.

03:05 Then 4, and two events, a write and a read, as tuples. JavaScript’s events are the tag’s records: the parts in one array, the values in another. Like Python’s tuples, they keep the 4 a number, so the log stays data, not a finished string.

03:26 Is the raw record safe? Only while callers read and assign: the proxy checks only what its traps cover. It has no trap for Object.defineProperty() or delete, so it forwards those to the record, unlogged.

03:40 Defining level as 9 that way gets past the set trap, and the record holds 9. Python’s underscore is a convention too, and a weaker one: settings._target is one attribute away.

03:55 JavaScript callers can’t name the record, yet untrapped operations still reach it. So the factory is an API boundary, not a sandbox against untrusted code.

04:07 So in JavaScript, a factory builds the record and hands out only its proxy. Its get trap audits and forwards each read. Its set trap refuses a bad level with false and logs only successful writes.

04:22 And the tag keeps each event’s values as values.

04:26 Next up is the exercise Isolate Two Settings Audits. There, you’ll add a write rule for a retry count and keep two settings objects’ audits apart.

Become a Member to join the conversation.